summaryrefslogtreecommitdiff
path: root/pwgrep.sh
blob: ed6ce8a3a58131328db065dd6f008ee9bcf44f51 (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
#!/bin/bash

# pwgrep v0.2 (c) 2009 by Dipl.-Inform. (FH) Paul C. 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 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: 
#	ln -s ~/svn/pwgrep/pwgrep.sh ~/bin/pwgrep
#	ln -s ~/svn/pwgrep/pwgrep.sh ~/bin/pwedit

# You can overwrite the default values by setting env. variables
# or by just editing this file.

[ -z $PWGREPDB] && PWGREPDB=database.gpg
[ -z $PWGREPWORKDIR ] && PWGREPWORKDIR=~/svn/pwgrep

[ -z $GPGKEYID ] && GPGKEYID=F4B6FFF0
[ -z $VERSIONCOMMIT ] && VERSIONCOMMIT="svn commit"
[ -z $VERSIONUPDATE ] && VERSIONUPDATE="svn update"

# 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"

# Default perms. for new files is 600
umask 177

function info {
	echo "=====> $@"
}

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

function findbin {
	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
	info Using $AWK
}

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
}

function pwgrep () {
	search=$1
	$VERSIONUPDATE
	info Searching for $search

	gpg --decrypt $PWGREPDB | $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 
		}' 
}

function pwedit () {
	cd $PWGREPWORKDIR || exit 1 
	$VERSIONUPDATE
	cp -vp $PWGREPDB $PWGREPDB.`date +'%s'`.snap && \
	gpg --decrypt $PWGREPDB > .database && \
	vim --cmd 'set noswapfile' --cmd 'set nobackup' \
		--cmd 'set nowritebackup' .database && \
	gpg --output .database.gpg -e -r $GPGKEYID .database && \
	$WIPE .database && \
	mv .database.gpg $PWGREPDB && \
	[ -z $DONOTUSEVERSIONING ] && $VERSIONCOMMIT
}

setawkcmd
setwipecmd

# Edit the database file if no argument is given
if [ -z $1 ]; then
	pwedit
else # Otherwise just grep the database
	pwgrep $1
fi