-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpgpassman.sh
executable file
·491 lines (482 loc) · 22.6 KB
/
gpgpassman.sh
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/bin/bash
# A script that uses 'gpg' to encrypt and decrypt passwords stored in '~/.gpgpassman'.
# Dependencies: 'gpg', 'xclip', 'git' and 'wget' (optional; for updating gpgpassman), 'apg' (optional; for generationg passwords), 'zenity' (optional; for GUI)
# If you have 'zenity' installed, executing 'gpgpassman gui' will show a full GUI for all of the scripts options.
# Written by simonizor 3/22/2017 - http://www.simonizor.gq/linuxapps -- License: GPLv2 Only
GPMVER="1.5.0"
X="Change: Remove self updating function. tar-pkg is recommended for upgrading gpgpassman in the future."
# ^^Remember to update this every release and do not move their position!
SCRIPTNAME="$0"
GPMDIR="$(< ~/.config/gpgpassman/gpgpassman.conf)"
GPMINITDIR=~/.gpgpassman
GPMCONFDIR=~/.config/gpgpassman
SERVNAME="$2"
bold=$(tput bold)
normal=$(tput sgr0)
updatecheck () {
echo "Checking for new version..."
UPNOTES="$(wget -q "https://raw.githubusercontent.com/simoniz0r/gpgpassman/master/gpgpassman.sh" -O - | sed -n '9p' | tr -d 'X="')"
VERTEST="$(wget -q "https://raw.githubusercontent.com/simoniz0r/gpgpassman/master/gpgpassman.sh" -O - | sed -n '8p' | tr -d 'GPMVER="')"
if [[ $GPMVER < $VERTEST ]]; then
if [ "$ZENITYGUI" != "1" ]; then
echo "Installed version: $GPMVER -- Current version: $VERTEST"
echo "A new version is available!"
echo "$UPNOTES"
else
zenity --info --title=gpgpassman --text="A new version is available!\n\n$UPNOTES"
zenitystart
fi
else
if [ "$ZENITYGUI" = "1" ]; then
zenity --info --title=gpgpassman --text="gpgpassman is up to date."
zenitystart
else
echo "Installed version: $GPMVER -- Current version: $VERTEST"
echo "$UPNOTES"
echo "gpgpassman is up to date."
fi
fi
}
programisinstalled () {
return=1
type "$1" >/dev/null 2>&1 || { return=0; }
}
helpfunc () {
echo
echo "Currently managed services:"
ls $GPMDIR | tr ' ' '\n'
echo
echo "Current password storage directory: $GPMDIR"
echo
echo "${bold}Usage:"
echo "${bold}add${normal} - Add encrypted password file."
echo "- Ex: 'gpgpassman add servicename'"
echo "${bold}dec${normal} - Decrypt a stored password file using the service name and copy to clipboard for 45 seconds."
echo "- Ex: 'gpgpassman dec servicename'"
echo "${bold}bac${normal} - Backup your passwords to a new directory."
echo "- Ex: 'gpgpassman bac /home/simonizor/passwordbackup'"
echo "${bold}rem${normal} - Remove a stored password file using the service name."
echo "- Ex: 'gpgpassman rem servicename'"
echo "${bold}dir${normal} - Change default directory used by gpgpassman. Only passwords in the currently configured directory will be able to be managed."
echo "- Ex: 'gpgpassman dir /path/to/directory'."
echo "${bold}gen${normal} - Generate new passwords using 'apg'."
echo "- Ex: 'gpgpassman gen'"
echo "${bold}gui${normal} - If 'zenity' is installed, gpgpassman's GUI will be launched."
echo "- Ex: 'gpgpassman gui'"
}
zenitymain () {
case $1 in
Add*)
SERVNAME=$(zenity --entry --title=gpgpassman --cancel-label="Main menu" --width=540 --height=460 --text="Add a new encrypted password.\n\nYou will be prompted for two different password inputs.\nThe first is the password that you use to login to the service.\nThe second is the password used for gpg encryption.\n\nYou will be prompted to overwrite already managed services.\n\n\n\n\n\n\n\n\n\n\nEnter the service name to encrypt a password for:")
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
if [ -f "$GPMDIR/$SERVNAME/$SERVNAME.gpg" ];then
zenity --question --title=gpgpassman --text="Password for $SERVNAME is already stored; overwrite (clipboard will also be cleared)?" --cancel-label=No --ok-label=Yes
if [[ $? -eq 1 ]]; then
zenity --warning --title=gpgpassman --text="Password for $SERVNAME was not overwritten."
exec "$SCRIPTNAME" gui
else
echo -n "$(gpg -d $GPMDIR/$SERVNAME/$SERVNAME.gpg)" | xclip -selection c -i
if [ "$(xclip -selection c -o)" = "" ]; then
zenity --error --title=gpgpassman --text="Wrong password or gpg failure!"
exec "$SCRIPTNAME" gui
fi
zenity --warning --title=gpgpassman --text="Stored password for $SERVNAME removed"
rm -f $GPMDIR/$SERVNAME/$SERVNAME.gpg
fi
fi
if [ -z $SERVNAME ]; then
zenity --error --title=gpgpassman --timeout=5 --text="No service name entered; try again."
exec "$SCRIPTNAME" gui
fi
PASSINPUT=$(zenity --entry --title=gpgpassman --hide-text --text="Enter your password for $SERVNAME:")
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
PASSINPUT2=$(zenity --entry --title=gpgpassman --hide-text --text="Input password again for $SERVNAME:")
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
if [ "$PASSINPUT" != "$PASSINPUT2" ]; then
zenity --error --title=gpgpassman --text="Passwords to not match; try again!"
exec "$SCRIPTNAME" gui
fi
if [ ! -d "$GPMDIR" ]; then
mkdir $GPMDIR
fi
if [ ! -d "$GPMDIR/$SERVNAME" ]; then
mkdir $GPMDIR/$SERVNAME
fi
zenity --warning --title=gpgpassman --timeout=5 --text="Enter the password to be used for encryption/decryption:"
echo "$PASSINPUT" | gpg --no-tty -c -o $GPMDIR/$SERVNAME/$SERVNAME.gpg
if [ -f "$GPMDIR/$SERVNAME/$SERVNAME.gpg" ]; then
zenity --warning --title=gpgpassman --text="Password for $SERVNAME encrypted in $GPMDIR/$SERVNAME/$SERVNAME.gpg"
exec "$SCRIPTNAME" gui
else
zenity --error --title=gpgpassman --text="Failed to write encrypted file for $SERVNAME in $GPMDIR/$SERVNAME/$SERVNAME.gpg"
exec "$SCRIPTNAME" gui
fi
;;
Decrypt*)
SERVNAME="$(ls $GPMDIR | tr ' ' '\n' | zenity --list --cancel-label="Main menu" --width=540 --height=460 --title=gpgpassman --text="Password storage directory:\n$GPMDIR\n\nSelect a password to decrypt:" --column="Cases" --hide-header)"
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
if [ -z "$SERVNAME" ]; then
zenity --error --title=gpgpassman --timeout=5 --text="No service name entered; try again."
exec "$SCRIPTNAME" gui
fi
echo -n "$(gpg --no-tty -d $GPMDIR/$SERVNAME/$SERVNAME.gpg)" | xclip -selection c -i
if [ "$(xclip -selection c -o)" = "" ]; then
zenity --error --title=gpgpassman --text="Wrong password or gpg failure!"
exec "$SCRIPTNAME" gui
fi
zenity --forms --title=gpgpassman --timeout=45 --text="Copied password to clipboard; clipboard will be cleared after 45 seconds..." --cancel-label="Clear now and return to main" --ok-label="Clear now and close"
if [[ $? -eq 0 ]]; then
echo -n "Password cleared from clipboard" | xclip -selection c -i
exit 0
else
echo -n "Password cleared from clipboard" | xclip -selection c -i
exec "$SCRIPTNAME" gui
fi
;;
Backup*)
SERVNAME=$(zenity --file-selection --directory --title="gpgpassman -- Select a location to back up your passwords")
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
if [ ! -d $SERVNAME ]; then
mkdir $SERVNAME
zenity --warning --title=gpgpassman --timeout=5 --text="$SERVNAME directory created for password backup."
fi
cp -r $GPMDIR/* $SERVNAME/ || { zenity --error --title=gpgpassman --text="Backup failed!" ; zenitymain ; }
zenity --warning --title=gpgpassman --timeout=5 --text="Passwords have been backed up to $SERVNAME."
exec "$SCRIPTNAME" gui
;;
Remove*)
SERVNAME=$(zenity --entry --cancel-label="Main menu" --width=540 --height=460 --title=gpgpassman --text="Remove an encrypted password.\n\nThe password for the service name you enter will be deleted permanently!\nYou will be asked for the gpg encryption password before removal.\n\nPassword storage directory:\n$GPMDIR\n\nManaged services:\n$(dir $GPMDIR)\n\n\n\n\n\nEnter the service name to remove:")
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
if [ -z $SERVNAME ]; then
zenity --error --title=gpgpassman --timeout=5 --text="No service name entered; try again."
exec "$SCRIPTNAME" gui
fi
zenity --question --title=gpgpassman --text="Passwords cannot be recovered; are you sure you want to remove password for $SERVNAME?" --ok-label="Yes"
if [[ $? -eq 1 ]]; then
zenity --warning --title=gpgpassman --text="Password for $SERVNAME was not removed."
exec "$SCRIPTNAME" gui
else
echo -n "$(gpg --no-tty -d $GPMDIR/$SERVNAME/$SERVNAME.gpg)" | xclip -selection c -i
if [ "$(xclip -selection c -o)" = "" ]; then
zenity --error --title=gpgpassman --text="Wrong password or gpg failure!"
exec "$SCRIPTNAME" gui
fi
echo -n "Password cleared from clipboard" | xclip -selection c -i
rm -rf $GPMDIR/$SERVNAME
zenity --warning --title=gpgpassman --text="Password for $SERVNAME was removed!"
exec "$SCRIPTNAME" gui
fi
;;
Change*)
SERVNAME=$(zenity --file-selection --directory --title="gpgpassman -- Select a new password storage directory")
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
zenity --question --title=gpgpassman --text="Only one directory can be managed by gpgpassman at a time; change password storage directory to $SERVNAME?" --ok-label="Yes"
if [[ $? -eq 1 ]]; then
exec "$SCRIPTNAME" gui
fi
if [ ! -d $SERVNAME ]; then
mkdir $SERVNAME || { zenity --error --title=gpgpassman --text="Could not create directory!" ; zenitymain ; }
zenity --warning --title=gpgpassman --timeout=5 --text="$SERVNAME directory created for gpgpassman storage."
fi
echo "$SERVNAME" > $GPMCONFDIR/gpgpassman.conf
zenity --warning --title=gpgpassman --timeout=5 --text="gpgpassman storage directory changed to $(< ~/.config/gpgpassman/gpgpassman.conf)"
exec "$SCRIPTNAME" gui
;;
Generate*)
programisinstalled "apg"
if [ "$return" = "1" ]; then
{ echo "Passwords generated using 'apg -a 1 -m 30 -n 4':" ; apg -a 1 -m 30 -n 4 ; } | zenity --text-info --cancel-label=Exit --width=540 --height=460 --title=gpgpassman
if [[ $? -eq 1 ]]; then
exit 0
fi
exec "$SCRIPTNAME" gui
else
zenity --error --title=gpgpassman --text="apg is not installed; cannot generate passwords!"
exec "$SCRIPTNAME" gui
fi
;;
Check*)
programisinstalled "wget"
if [ "$return" = "1" ]; then
programisinstalled "git"
if [ "$return" = "1" ]; then
updatecheck
else
zenity --error --title=gpgpassman --text="'git' is not installed; cannot download updates!"
exec "$SCRIPTNAME" gui
fi
else
zenity --error --title=gpgpassman --text="'wget' is not installed; cannot check for updates!"
exec "$SCRIPTNAME" gui
fi
;;
*)
exit 0
;;
esac
}
zenitystart () {
ZMAINCASE=$(zenity --list --cancel-label=Exit --width=540 --height=460 --title=gpgpassman --text="Welcome to gpgpassman v$GPMVER\n\ngpgpassman is a password manager that uses 'gpg' for encryption.\n\nPassword storage directory:\n$GPMDIR\n\nManaged passwords:\n$(dir $GPMDIR)\n\nWhat would you like to do?" --column="Cases" --hide-header "Add a new encrypted password" "Decrypt a stored password" "Backup your stored passwords" "Remove a stored password" "Change password storage directory" "Generate passwords using 'apg'" "Check for gpgpassman update")
if [[ $? -eq 1 ]]; then
exit 0
fi
ZENITYGUI="1"
zenitymain "$ZMAINCASE"
}
main () {
case $1 in
add*|Add*)
if [ -z $SERVNAME ]; then
read -p "Input the service name to encrypt a password for: " SERVNAME
fi
if [ -f "$GPMDIR/$SERVNAME/$SERVNAME.gpg" ];then
read -p "Password for $SERVNAME is already stored; overwrite (clipboard will also be cleared)? Y/N"
if [[ $REPLY =~ ^[Nn]$ ]]; then
echo "Password for $SERVNAME was not overwritten."
exit 1
else
echo -n "$(gpg -d $GPMDIR/$SERVNAME/$SERVNAME.gpg)" | xclip -selection c -i
if [ "$(xclip -selection c -o)" = "" ]; then
echo "Wrong password or gpg failure!"
exit 1
fi
echo -n "Password cleared from clipboard" | xclip -selection c -i
fi
fi
if [ -z $SERVNAME ]; then
echo "No service name entered; exiting..."
exit 1
fi
echo "Input your password for $SERVNAME:"
read -s PASSINPUT
echo
echo "Input password again for $SERVNAME:"
read -s PASSINPUT2
echo
if [ "$PASSINPUT" != "$PASSINPUT2" ]; then
echo "Passwords do not match; exiting..."
exit 1
fi
if [ ! -d "$GPMDIR" ]; then
mkdir $GPMDIR
fi
if [ ! -d "$GPMDIR/$SERVNAME" ]; then
mkdir $GPMDIR/$SERVNAME
fi
echo "$PASSINPUT" | gpg -c -o $GPMDIR/$SERVNAME/$SERVNAME.gpg
if [ -f "$GPMDIR/$SERVNAME/$SERVNAME.gpg" ]; then
echo "Password for $SERVNAME encrypted in $GPMDIR/$SERVNAME/$SERVNAME.gpg"
exit 0
else
echo "Failed to write encrypted file for $SERVNAME in $GPMDIR/$SERVNAME/$SERVNAME.gpg"
exit 1
fi
;;
dec*|Dec*)
if [ -z "$SERVNAME" ]; then
echo "Currently managed services:"
ls $GPMDIR | tr ' ' '\n'
echo
echo "Current password storage directory: $GPMDIR"
echo
read -p "Enter the service name to decrypt password for: " SERVNAME
fi
if [ -f "$GPMDIR/$SERVNAME/$SERVNAME.gpg" ];then
echo "Decrypting password for $SERVNAME"
sleep 0.5
echo -n "$(gpg -d $GPMDIR/$SERVNAME/$SERVNAME.gpg)" | xclip -selection c -i
if [ "$(xclip -selection c -o)" = "" ]; then
echo "Wrong password or gpg failure!"
exit 1
fi
echo "$SERVNAME password copied to clipboard; clipboard will be cleared after 45 seconds..."
trap '{ echo ; echo "Keyboard interruppt; clearing password..." ; echo -n "Password cleared from clipboard" | xclip -selection c -i ; echo "Password cleard from clipboard." ; exit 0 ; }' INT
sleep 45
echo -n "Password cleared from clipboard" | xclip -selection c -i
echo "Password cleard from clipboard."
exit 0
else
echo "No password found for $SERVNAME"
exit 1
fi
;;
bac*|Bac*)
if [ -z $SERVNAME ]; then
read -p "Input the full directory to backup your passwords to. Ex: '/home/simonizor/passwordbackup': " SERVNAME
fi
if [ "${SERVNAME: -1}" = "/" ]; then
SERVNAME="${SERVNAME::-1}"
fi
if [[ "$SERVNAME" == /* ]]; then
if [ ! -d $SERVNAME ]; then
mkdir $SERVNAME
echo "$SERVNAME directory created for password backup."
fi
cp -r $GPMDIR/* $SERVNAME/ || { echo "Backup failed!" ; exit 1 ; }
echo "Passwords have been backed up to $SERVNAME."
exit 0
else
echo "$SERVNAME is not a valid directory; use full directory path. Ex: 'gpgpassman bac /home/simonizor/passwordbackup'"
helpfunc
exit 1
fi
;;
rem*|Rem*)
if [ -z "$SERVNAME" ]; then
echo "Currently managed services:"
ls $GPMDIR | tr ' ' '\n'
echo
echo "Current password storage directory: $GPMDIR"
echo
read -p "Input the service name for the password you want to remove: " SERVNAME
fi
if [ -z $SERVNAME ]; then
echo "No service name entered; exiting..."
exit 1
fi
if [ -f "$GPMDIR/$SERVNAME/$SERVNAME.gpg" ];then
read -p "Passwords cannot be recovered; are you sure you want to remove the encrypted password for $SERVNAME? Y/N " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -n "$(gpg -d $GPMDIR/$SERVNAME/$SERVNAME.gpg)" | xclip -selection c -i
if [ "$(xclip -selection c -o)" = "" ]; then
echo "Wrong password or gpg failure!"
exit 1
fi
echo -n "Password cleared from clipboard" | xclip -selection c -i
rm -rf $GPMDIR/$SERVNAME
echo "Password for $SERVNAME removed!"
exit 0
else
echo "Password for $SERVNAME was not removed."
exit 1
fi
else
echo "No password found for $SERVNAME"
exit 1
fi
;;
dir*)
if [ -z $SERVNAME ]; then
read -p "Input the full directory to change password storage to. Ex: '/home/simonizor/mypasswords': " SERVNAME
fi
if [ "${SERVNAME: -1}" = "/" ]; then
SERVNAME="${SERVNAME::-1}"
fi
if [[ "$SERVNAME" == /* ]]; then
read -p "Only one directory can be managed by gpgpassman at a time; change password storage directory to $SERVNAME? Y/N " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ ! -d $SERVNAME ]; then
mkdir $SERVNAME || { echo "Could not create directory!" ; exit 1 ; }
echo "$SERVNAME directory created for gpgpassman storage."
fi
echo "$SERVNAME" > $GPMCONFDIR/gpgpassman.conf
echo "gpgpassman storage directory changed to $(< ~/.config/gpgpassman/gpgpassman.conf)"
exit 0
else
echo "gpgpassman storage directory was not changed."
exit 0
fi
else
echo "$SERVNAME is not a valid directory; use full directory path. Ex: 'gpgpassman dir /home/simonizor/mypasswords'"
helpfunc
exit 1
fi
;;
gen*|Gen*)
programisinstalled "apg"
if [ "$return" = "1" ]; then
echo "Passwords generated using 'apg -a 1 -m 30 -n 4':"
apg -a 1 -m 30 -n 4
echo
read -p "Press ENTER to continue; terminal window will be cleared"
tput reset
exit 0
else
echo "apg is not installed!"
exit 1
fi
;;
h*)
echo "gpgpassman - http://www.simonizor.gq/linuxapps"
echo "A script that uses 'gpg' to encrypt and decrypt passwords."
helpfunc
echo
programisinstalled "wget"
if [ $return = "1" ]; then
programisinstalled "git"
if [ $return = "1" ]; then
updatecheck
fi
fi
exit 0
;;
help)
echo "gpgpassman - http://www.simonizor.gq/linuxapps"
echo "A script that uses 'gpg' to encrypt and decrypt passwords."
helpfunc
echo
programisinstalled "wget"
if [ $return = "1" ]; then
programisinstalled "git"
if [ $return = "1" ]; then
updatecheck
fi
fi
exit 0
;;
exit*|Exit*)
exit 0
;;
*)
programisinstalled "zenity"
if [ $return = "1" ]; then
zenitystart
else
echo "gpgpassman - http://www.simonizor.gq/linuxapps"
echo "A script that uses 'gpg' to encrypt and decrypt passwords."
echo "gpgpassman now has a GUI; install 'zenity' to check it out!"
echo
helpfunc
echo
exit 0
fi
;;
esac
}
if [ ! -f "$GPMCONFDIR/gpgpassman.conf" ]; then
echo "$GPMCONFDIR does not exist; creating..."
mkdir $GPMCONFDIR
mkdir $GPMINITDIR
echo "$GPMINITDIR" > $GPMCONFDIR/gpgpassman.conf
zenity --error --text="$GPMCONFDIR created and config file written; run gpgpassman again." || echo "$GPMCONFDIR created and config file written; run gpgpassman again."
exit 0
fi
programisinstalled "gpg"
if [ $return = "1" ]; then
programisinstalled "xclip"
if [ $return = "1" ]; then
main "$1"
else
zenity --error --text="xclip is not installed!" || echo "xclip is not installed!"
exit 1
fi
else
zenity --error --text="gpg is not installed!" || echo "gpg is not installed!"
exit 1
fi