summaryrefslogtreecommitdiff
path: root/bin/pwgrep.sh
blob: 54fb7bc84aed011b5abc4283145bc5fdf4e5c0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/bin/bash

# pwgrep (c) 2009, 2010, 2011, 2013 by Paul Buetow
# pwgrep helps you to manage all your passwords using GnuGP
# for encryption and a versioning system (subversion by default)
# for keeping track all changes of your password database. In
# combination to GnuPG you should use the versioning system in
# combination with SSL or SSH encryption.

# If you are using a *BSD, you may want to edit the shebang line.
#
# Usage: 
#
#  Searching for a database value: 
#  ./pwgrep.sh searchstring 
#
#  Editing the database (same but without args): 
#  ./pwgrep.sh 
#
# For more reasonable commands the following symlinks are recommended.
# Take a look at the create-symlinks.sh script.  

# You can overwrite the default values by setting env. variables
# or by just editing this file.
declare DEFAULTDB=private.gpg
declare DEFAULTFILESTOREDIR=filestore
declare DEFAULTFILESTORECATEGORY=default
declare -r PWGREP_VERSION=0.8.7

[ -z "$RCFILE" ] && RCFILE=~/.pwgreprc

# Only use mawk or gawk, but if possible not nawk. On *BSD awk=nawk. So try 
# awk/nawk last. You can use nawk but nawk will not match case insensitive.
[ -z "$TRYAWKLIST" ] && TRYAWKLIST="mawk gawk awk nawk"
# Find the correct command to wipe temporaly files after usage
[ -z "$TRYWIPELIST" ] && TRYWIPELIST="destroy shred"
# Same for sed
[ -z "$TRYSEDLIST" ] && TRYSEDLIST="sed gsed"

# From here, do not change stuff! You may edit the content of the file $RCFILE!

function source_config () {
  [ -f $RCFILE ] && source <($SED 's/^/export /' $RCFILE)
}


function configure () {
  # Reading the current configuration
  source_config

  # Setting default values if not set in the configuration file already
  (
  #[ -z "$SVN_EDITOR" ] && echo 'export SVN_EDITOR="ex -c 1"'
  [ -z "$GIT_EDITOR" ] && echo 'export GIT_EDITOR=vim'
  [ -z "$DB" ] && echo DB=$DEFAULTDB
  [ -z "$FILESTOREDIR" ] && echo export FILESTOREDIR=$DEFAULTFILESTOREDIR
  [ -z "$FILESTORECATEGORY" ] && echo export FILESTORECATEGORY=$DEFAULTFILESTORECATEGORY

  # The PWGREPWORDIR should be in its own versioning repository. 
  # For password revisions.
  [ -z "$WORKDIR" ] && echo export WORKDIR=~/git/pwdb

  # Enter here your GnuPG key ID
  [ -z "$GPGKEYID" ] && echo export GPGKEYID=37EC5C1D

  # Customizing the versioning commands (i.e. if you want to use another
  # versioning system).
  [ -z "$VERSIONCOMMIT" ] && echo 'export VERSIONCOMMIT="git commit -a"'
  [ -z "$VERSIONUPDATE" ] && echo 'export VERSIONUPDATE="git pull origin master"'
  [ -z "$VERSIONPUSH" ] && echo 'export VERSIONPUSH="git push origin master"'
  [ -z "$VERSIONADD" ] && echo 'export VERSIONADD="git add"'
  [ -z "$VERSIONDEL" ] && echo 'export VERSIONDEL="git rm"'
  ) >> $RCFILE

  # Re-reading the current configuration, because there might be new
  # variables by now
  source_config
}

function out () {
  echo "$@" 1>&2
}

function info () {
  out "=====> $@" 
}

function error () {
  echo "ERROR: $@"
  exit 666  
}

function findbin () {
  local -r trylist=$1
  found=""
  for bin in $trylist; do
    if [ -z $found ]; then
      which=$(which $bin)
      [ ! -z $which ] && found=$bin  
    fi
  done

  echo $found
}

function setawkcmd () {
  AWK=$(findbin "$TRYAWKLIST")
  [ -z $AWK ] && error No awk found in $PATH
}

function setsedcmd () {
  SED=$(findbin "$TRYSEDLIST")
  [ -z $SED ] && error No sed found in $PATH
}

function setwipecmd () {
  WIPE=$(findbin "$TRYWIPELIST")

  if [ -z $WIPE ]; then
    # FreeBSDs rm includes -P which is secure enough
    if [ $(uname) = 'FreBSD' ]; then
      WIPE="rm -v -P"
    else
      error "No wipe command found in $PATH, please install shred or destroy"
    fi
  fi

  info Using $WIPE for secure file deletion
}

function pwgrep () {
  local -r search=$1
  local -a dbs=()

  if [ -z "$ALL" ]; then
    dbs=$DB
  else
    dbs=$(_pwdbls | sed 's/$/.gpg/')
  fi

  for db in $dbs; do  
    info Searching for $search in $db
    gpg --use-agent --decrypt $db | $AWK -v search="$search" '
      BEGIN { 
        flag=0 
        IGNORECASE=1
      } 
      !/^\t/ { 
        if (!flag && $0 ~ search) {
          flag=1
          print $0
        } else if (flag && $0 ~ search) {
          print $0
        } else if (flag) {
          flag=0
        }
      } /^\t/ && flag { 
        print $0 
      }' 
  done
}

function pwupdate () {
  if [ -z "$NOVERSIONING" ]; then
    info Updating repository
    $VERSIONUPDATE 2>&1 >/dev/null
  fi
}

function pwedit () {
  pwupdate
  cp -vp $DB $DB.$(date +'%s').snap && \
  gpg --decrypt $DB > .database && \
  vim --cmd 'set noswapfile' --cmd 'set nobackup' \
    --cmd 'set nowritebackup' .database && \
  gpg --output .$DB -e -r $GPGKEYID .database && \
  $WIPE .database && \
  mv .$DB $DB && \
  [ -z "$NOVERSIONING" ] && $VERSIONCOMMIT && [ ! -z "$VERSIONPUSH" ] && $VERSIONPUSH
}

function _pwdbls () {
  ls *.gpg | sed 's/\.gpg$//'
}

function pwdbls () {
  echo Available Databases:
  _pwdbls
  echo Current database: $DB
}

function pwfls () {
  local arg=$1
  
  if [ "$ALL" = "1" ]; then
    ALL=0
    local -r dir=$WORKDIR/$FILESTOREDIR
    [ ! -e $dir ] && error $dir does not exist
  
    info Showing all categories
    ls $dir | while read store; do
      pwfls $store 
    done
  else
    local dir=$WORKDIR/$FILESTOREDIR

    if [ -z "$USEFILESTORECATEGORY" ]; then
      info Available file store categories:
      dir=$WORKDIR/$FILESTOREDIR
      info "(You may use '`basename $0` -d <CATEGORY>' to display containing files.)"
    else
      info Available files in store $FILESTORECATEGORY
      dir=$WORKDIR/$FILESTOREDIR/$FILESTORECATEGORY
    fi

    [ ! -e $dir ] && error "Category ($dir) does not exist"
      ls $dir 
  fi
}

function pwfcat () {
  local arg=$1
  
  if [ -z "$arg" ]; then
    error "No file specified (hint: use pwfls)"
  
  else
    local -r dir=$WORKDIR/$FILESTOREDIR/$FILESTORECATEGORY
    local -r file=$(echo $arg | sed 's/.gpg$//')
  
    [ ! -e $dir ] && error "Category $FILESTORECATEGORY ($dir) does not exist"
    [ ! -e $dir/$file.gpg ] && error "File $file in category $FILESTORECATEGORY does not exist"
    gpg --decrypt $dir/$file.gpg 
  fi
}

function pwfadd () {
  local -r name=$(echo $1 | sed 's/.gpg$//')
  local srcfile=$1
  local outfile=''

  if [ $(echo "$srcfile" | grep -v '^/') ]; then
    srcfile=$CWD/$srcfile  
  fi

  if [ ! -z $2 ]; then
    outfile=$(basename $2)
  else
    outfile=$(basename $name)
  fi

  pwupdate

  [ -z "$name" ] && error Missing argument 
    if [ ! -e $FULLFILESTORE ]; then
      info Creating new category
      [ ! -z "$NOVERSIONING" ] && error Cannot add new category with versioning disabled
      local -r umaskbackup=$(umask)
      umask 0022
      mkdir $FULLFILESTORE && $VERSIONADD $FULLFILESTORE && $VERSIONCOMMIT && [ ! -z "$VERSIONPUSH" ] && $VERSIONPUSH

      umask $umaskbackup
    fi

  [ ! -e $FILESTOREWORKDIR ] && error $FILESTOREWORKDIR does not exist
  gpg --output $FULLFILESTORE/$outfile.gpg -e -r $GPGKEYID $srcfile && \

  if [ -z "$NOVERSIONING" ]; then
    $VERSIONADD $FULLFILESTORE/$outfile.gpg && $VERSIONCOMMIT && [ ! -z "$VERSIONPUSH" ] && $VERSIONPUSH

  fi
}

function pwfdel () {
  local arg=$1
  
  if [ -z "$arg" ]; then
    error "No file specified (hint: use pwfls)"
  
  else
    local -r dir=$WORKDIR/$FILESTOREDIR/$FILESTORECATEGORY
    local -r file=$(echo $arg | sed 's/.gpg$//')
    local -r filepath=$dir/$file.gpg
  
    [ ! -e $dir ] && error "Category $FILESTORECATEGORY ($dir) does not exist"
    [ ! -e $filepath ] && error "File $file in category $FILESTORECATEGORY does not exist"
  
    if [ -z "$NOVERSIONING" ]; then
      # Wipe even encrypted file securely
      $WIPE $filepath && \
      touch $filepath && $VERSIONCOMMIT && \
      $VERSIONDEL $filepath && $VERSIONCOMMIT
      [ ! -z "$VERSIONPUSH" ] && $VERSIONPUSH
    else
      $WIPE $filepath
    fi
  fi
}

function fwipe () {
  [ -z $1 ] && error Missing argument
  $WIPE $CWD/$1
}

function pwhelp () {
  info $PWGREP_VERSION
  info Possible operations are:
cat <<END
  fwipe <FILE>            - Wiping a file
  pwdbls                  - Listing available DBs
  pwedit [OPTS]           - Editing current DB
  pwfadd <FILE>           - Adding a file to FDB
  pwfcat <NAME>           - Printing a file from filestore to stdout
  pwfdel <NAME>           - Deleting a file from filestore
  pwgrep [OPTS] <REGEX>   - Grepping current DB
  pwldb                   - Synonym for pwdbls
  pwupdate                - Updating FDB and all DBs
  pwhelp                  - Printing this help screen
Where OPTS are:
  -o                      - Offline mode
  -d <DB NAME>            - Using a specific DB
  -a                 - Searching all available DBs or categories at once
END
}

setawkcmd
setsedcmd
setwipecmd

configure

CWD=$(pwd)
#umask 177

cd $WORKDIR || error "No such file or directory: $WORKDIR"

BASENAME=$(basename $0)
ARGS=$@

function set_opts () {
  case $ARGS in 
    -o*)
      # Offlinemode 
      NOVERSIONING=1
      ARGS=${ARGS[@]:2}
      set_opts
     ;; 

     -d*)
      # Alternate DB
      DB=$(echo $ARGS | $AWK '{ print $2 }')
                  FILESTORECATEGORY=$DB
      USEFILESTORECATEGORY=1
      ARGS=$(echo $ARGS | $SED "s/-d $DB//")
      DB=$DB.gpg
      set_opts
     ;;

     -a*)
      # All DBs at once
      which gpg-agent  
      if [ $? == "0" ]; then   
        ALL=1
        ARGS=${ARGS[@]:2}
        set_opts
      else
        error You need gpg-agent installed    
      fi
     ;;

     *)
  esac
}

set_opts $ARGS
FULLFILESTORE=$FILESTOREDIR/$FILESTORECATEGORY
FILESTOREWORKDIR=$WORKDIR/$FULLFILESTORE

case $BASENAME in 
  pwgrep) 
    pwgrep $ARGS
  ;;
  pwupdate) 
    pwupdate
  ;;
  pwedit) 
    pwedit
  ;;
  pwdbls) 
    pwdbls
        ;;
  pwldb) 
    pwdbls
  ;;
  pwfls) 
    pwfls $ARGS
  ;;
  pwfcat) 
    pwfcat $ARGS
  ;;
  pwfadd) 
    pwfadd $ARGS
  ;;
  pwfdel) 
    pwfdel $ARGS
  ;;
  fwipe) 
    fwipe $ARGS
  ;;
  *)
    pwhelp
  ;;
esac