-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwdman.sh
executable file
·854 lines (822 loc) · 22.5 KB
/
pwdman.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#!/bin/bash
: "${DEFAULT_DATABASE:=$HOME/pwdman.db}"
: "${CLIPBOARD_TIMEOUT:=7}"
: "${DEFAULT_LENGTH:=128}"
: "${DEFAULT_ALPHABET:=abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123\
456789}"
SCRIPT_VERSION="1.0"
BUFFER=""
#
# Encrypt Database
#
# $1 Database
# $2 Password
# $3 [ Data ]
#
# $BUFFER
#
function pwdman_encrypt_database() {
local data gpg_options
if [[ $# -lt 2 ]]; then
pwdman_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
data="Username,Password"$'\n'"$3"
gpg_options=(
--armor
--batch
--symmetric
--yes
--passphrase-fd 3
--no-symkey-cache
--cipher-algo AES256
--output "$1"
)
if ! printf "%s" "$data" | gpg "${gpg_options[@]}" 3< \
<(printf "%s" "$2"); then
pwdman_exit "Error: database encryption error."
fi
}
#
# Decrypt Database
#
# $1 Database
# $2 Password
#
# $BUFFER
#
function pwdman_decrypt_database() {
local gpg_options result
if [[ $# -lt 2 ]]; then
pwdman_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
if [[ ! -f "$1" ]]; then
pwdman_exit "Error: database not found."
fi
gpg_options=(
--armor
--batch
--no-symkey-cache
--decrypt
--passphrase-fd 0
--cipher-algo AES256
)
if ! result=$(printf "%s\\n" "$2" | gpg "${gpg_options[@]}" \
"$1" 2>/dev/null); then
pwdman_exit "Error: database decryption error."
fi
BUFFER=$(printf "%s" "$result" | tail -n +2)
}
#
# Write Password
#
# $1 [ Database ]
# $2 [ Username ]
#
#
function pwdman_write_password() {
local database database_password username plain_password
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
pwdman_get_input_password "Database password: "
database_password="$PASSWORD"
pwdman_decrypt_database "$database" "$database_password"
if [[ $# -gt 1 && -n "$2" ]]; then
username="$2"
else
pwdman_get_input "Username: "
username="$INPUT"
fi
if ! pwdman_check_reverse_entries "$username" "$BUFFER" || \
[[ $(pwdman_count_entries "$username" "$BUFFER") -gt 0 ]]; then
echo "Warning: there's already a matching entry in the database."
pwdman_ask_continue 0
fi
pwdman_get_input_password "Entry password [random]: "
if [[ "$PASSWORD" == "" ]]; then
pwdman_get_input "Random password length [$DEFAULT_LENGTH]: "
if [[ -z "$INPUT" ]]; then
length="$DEFAULT_LENGTH"
else
length="$INPUT"
fi
pwdman_get_random_password "$length"
fi
plain_password="$PASSWORD"
PASSWORD=$(printf "%s" "$PASSWORD" | base64 -w 0)
if [[ "$BUFFER" != "" ]]; then
BUFFER+=$'\n'
fi
username=$(printf "%s" "$username" | base64 -w 0)
BUFFER+="$username,$PASSWORD"
pwdman_encrypt_database "$database" "$database_password" "$BUFFER"
pwdman_copy_to_clipboard "$plain_password" "$CLIPBOARD_TIMEOUT"
echo "Password successfully written."
}
#
# Check DB Reverse Matching Entries
#
# $1 Username
# $2 Database Buffer
#
function pwdman_check_reverse_entries() {
local database_buffer decoded_line line
if [[ $# -lt 2 ]]; then
pwdman_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
database_buffer=$(printf "%s" "$2" | cut -d "," -f 1)
if [[ "$database_buffer" == "" ]]; then
return 0
fi
while IFS= read -r line; do
decoded_line=$(printf "%s" "$line" | base64 --decode)
if printf "%s" "$1" | grep -q "$decoded_line"; then
return 1
fi
done <<< "$database_buffer"
return 0
}
#
# Count DB Entries
#
# $1 Username
# $2 Database Buffer
#
function pwdman_count_entries() {
local database_buffer decoded_database line
if [[ $# -lt 2 ]]; then
pwdman_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
database_buffer=$(printf "%s" "$2" | cut -d "," -f 1)
decoded_database=""
while IFS= read -r line; do
decoded_database+=$(printf "%s" "$line" | base64 --decode)$'\n'
done <<< "$database_buffer"
printf "%s" "$decoded_database" | grep -c "$1"
}
#
# Read Password
#
# $1 [ Database ]
# $2 [ Username ]
#
function pwdman_read_password() {
local database_password count timeout
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
pwdman_get_input_password "Database password: "
database_password="$PASSWORD"
pwdman_decrypt_database "$database" "$database_password"
encoded_usernames=$(printf "%s" "$BUFFER" | cut -d "," -f 1)
decoded_usernames=""
while IFS= read -r line; do
decoded_usernames+=$(printf "%s" "$line" | base64 --decode)$'\n'
done <<< "$encoded_usernames"
if [[ $# -gt 1 && -n "$2" ]]; then
username="$2"
else
pwdman_get_input "Username: "
username="$INPUT"
fi
count=$(printf "%s" "$decoded_usernames" | grep -c "$username")
if [[ $count -eq 0 ]]; then
pwdman_exit "Error: username not found in the database."
elif [[ $count -gt 1 ]]; then
echo "Warning: multiple entries matching in the database."
fi
password=$(printf "%s" "$BUFFER" | cut -d "," -f 2 | \
sed -n "$(printf "%s" "$(printf "%s" "$decoded_usernames" | \
grep -n "$username")" | cut -d ":" -f 1 | sed -n 1p)"p | base64 --decode)
pwdman_copy_to_clipboard "$password" "$CLIPBOARD_TIMEOUT"
echo "Password successfully read."
}
# Copy To Clipboard
#
# $1 String
# $2 [ Timeout ]
#
function pwdman_copy_to_clipboard() {
local timeout
if [[ $# -lt 1 ]]; then
pwdman_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
if [[ $# -gt 1 && -n "$2" ]]; then
timeout="$1"
else
timeout="$CLIPBOARD_TIMEOUT"
fi
printf "%s" "$1" | xclip -selection clipboard
timeout="$CLIPBOARD_TIMEOUT"
shift
while [[ $timeout -gt 0 ]]; do
printf "\\rPassword on clipboard! Clearing clipboard in %.d" \
$((timeout--))
sleep 1
done
printf "%s" $'\n'"Clipboard cleared."$'\n'
printf "%s" "" | xclip -selection clipboard
}
#
# Update Password
#
# $1 [ Database ]
# $2 [ Username ]
#
function pwdman_update_password() {
local username database database_password count length line_number
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
pwdman_get_input_password "Database password: "
database_password="$PASSWORD"
pwdman_decrypt_database "$database" "$database_password"
if [[ $# -gt 1 && -n "$2" ]]; then
username="$2"
else
pwdman_get_input "Username: "
username="$INPUT"
fi
count=$(pwdman_count_entries "$username" "$BUFFER")
if [[ $count -eq 0 ]]; then
pwdman_exit "Error: username not found in the database."
elif [[ $count -gt 1 ]]; then
echo "Warning: multiple entries matching in the database."
pwdman_ask_continue 0
fi
username=$(printf "%s" "$username" | base64 -w 0)
while :
do
line_number=$(printf "%s" "$BUFFER" | cut -d "," -f 1 | \
grep -n "$username" | cut -d ":" -f 1 | head -n 1)
if [[ "$line_number" == "" ]]; then
break;
fi
BUFFER=$(printf "%s" "$BUFFER" | sed "${line_number}d")
done
pwdman_get_input_password "New password [random]: "
if [[ "$PASSWORD" == "" ]]; then
pwdman_get_input "Random password length [128]: "
if [[ -z "$INPUT" ]]; then
length="$DEFAULT_LENGTH"
else
length="$INPUT"
fi
pwdman_get_random_password "$length"
fi
PASSWORD=$(printf "%s" "$PASSWORD" | base64 -w 0)
if [[ "$BUFFER" != "" ]]; then
BUFFER+=$'\n'
fi
BUFFER+="$username,$PASSWORD"
pwdman_encrypt_database "$database" "$database_password" "$BUFFER"
echo "Entry successfully updated."
}
#
# Delete Password
#
# $1 [ Database ]
# $2 [ Username ]
#
function pwdman_delete_password() {
local username database count line_number
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
pwdman_get_input_password "Database password: "
database_password="$PASSWORD"
pwdman_decrypt_database "$database" "$database_password"
if [[ $# -gt 1 && -n "$2" ]]; then
username="$2"
else
pwdman_get_input "Username: "
username="$INPUT"
fi
count=$(pwdman_count_entries "$username" "$BUFFER")
username=$(printf "%s" "$username" | base64 -w 0)
if [[ $count -eq 0 ]]; then
pwdman_exit "Error: username not found in the database."
elif [[ $count -gt 1 ]]; then
echo "Warning: multiple entries matching in the database."
pwdman_ask_continue 0
fi
while :
do
line_number=$(printf "%s" "$BUFFER" | cut -d "," -f 1 | \
grep -n "$username" | cut -d ":" -f 1 | head -n 1)
if [[ "$line_number" == "" ]]; then
break;
fi
BUFFER=$(printf "%s" "$BUFFER" | sed "${line_number}d")
done
pwdman_encrypt_database "$database" "$database_password" "$BUFFER"
echo "Entry successfully deleted."
}
#
# List Entries
#
# $1 [ Database ]
#
function pwdman_list() {
local database database_password username password decoded_data
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
pwdman_get_input_password "Database password: "
database_password="$PASSWORD"
pwdman_decrypt_database "$database" "$database_password"
if [[ "$BUFFER" == "" ]]; then
pwdman_exit "Database is empty."
fi
decoded_data="Username Password"$'\n'
printf "Database entries:\\n"
while IFS= read -r line; do
username=$(printf "%s" "$line" | cut -d "," -f 1 | base64 --decode)
password=$(printf "%s" "$line" | cut -d "," -f 2 | base64 --decode)
decoded_data+="$username $password"$'\n'
done <<< "$BUFFER"
printf "%s" "$decoded_data" | column -t -s " "
}
#
# Backup Database
#
# $1 [ Database ]
# $2 [ Filename ]
#
function pwdman_backup_database() {
local database username password decoded_data
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
pwdman_get_input_password "Database password: "
pwdman_decrypt_database "$database" "$PASSWORD"
if [[ "$BUFFER" == "" ]]; then
pwdman_exit "Database is empty."
fi
if [[ $# -gt 1 && -n "$2" ]]; then
backup_file="$2"
else
pwdman_get_input "Backup filename: "
backup_file="$INPUT"
if [[ -f "$backup_file" ]]; then
echo "Warning: file already exists, do you want to overwrite it?"
pwdman_ask_continue 0
fi
fi
decoded_data="Username,Password"$'\n'
while IFS= read -r line; do
username=$(printf "%s" "$line" | cut -d "," -f 1)
password=$(printf "%s" "$line" | cut -d "," -f 2)
decoded_data+="$username,$password"$'\n'
done <<< "$BUFFER"
printf "%s" "$decoded_data" > "$backup_file"
echo "Database successfully exported."
echo "Note: be aware that the export is NOT encrypted!"
}
#
# Reencrypt Database
#
# $1 [ Database ]
#
function pwdman_reencrypt_database() {
local database
if [[ $# -gt 0 ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
pwdman_get_input_password "Database password: "
database_password="$PASSWORD"
pwdman_decrypt_database "$database" "$database_password"
pwdman_get_input_password "New database password: "
pwdman_encrypt_database "$database" "$PASSWORD" "$BUFFER"
echo "Database successfully reencrypted."
}
#
# Create Database
#
# $1 [ Database ]
#
function pwdman_create_database() {
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
pwdman_get_input "Database name [$DEFAULT_DATABASE]: "
if [[ -z "$INPUT" ]]; then
database="$DEFAULT_DATABASE"
else
database="$INPUT"
fi
fi
if [[ -f "$database" ]]; then
echo "Warning: file already exists, do you want to overwrite it?"
pwdman_ask_continue 0
fi
pwdman_get_input_password "New database password: "
pwdman_encrypt_database "$database" "$PASSWORD"
echo "Database successfully created."
}
#
# Import Backup
#
# $1 [ Database ]
# $2 [ Backup filename]
#
function pwdman_import_backup() {
local database database_password
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
if [[ $# -gt 1 && -n "$2" ]]; then
backup_file="$2"
else
pwdman_get_input "Backup filename: "
backup_file="$INPUT"
fi
if [[ ! -f "$backup_file" ]]; then
pwdman_exit "Error: backup file not found."
fi
if [[ "$(head -n 1 "$backup_file")" != "Username,Password" ]]; then
pwdman_exit "Error: invalid backup file, be more careful."
fi
pwdman_get_input_password "Database password: "
pwdman_decrypt_database "$database" "$PASSWORD"
FIRST_LIST=()
while IFS= read -r line; do
FIRST_LIST+=("$(printf "%s" "$line" | base64 --decode)")
done <<< "$(printf "%s" "$BUFFER" | cut -d "," -f 1)"
SECOND_LIST=()
while IFS= read -r line; do
SECOND_LIST+=("$(printf "%s" "$line" | base64 --decode)")
done <<< "$(printf "%s" "$(tail -n +2 "$backup_file"| cut -d "," -f 1)")"
if [[ "${#FIRST_LIST[@]}" -ne 0 && \
"${#SECOND_LIST[@]}" -ne 0 ]]; then
pwdman_check_merge_conflicts
fi
if [[ "$BUFFER" != "" ]]; then
BUFFER+=$'\n'
fi
while IFS= read -r line; do
entry_username=$(printf "%s" "$line" | cut -d "," -f 1)
entry_password=$(printf "%s" "$line" | cut -d "," -f 2)
BUFFER+="$entry_username,$entry_password"$'\n'
done <<< "$(tail -n +2 "$backup_file")"
pwdman_encrypt_database "$database" "$PASSWORD" "$BUFFER"
echo "Database successfully imported."
}
#
# Import Database
#
# $1 [ Database ]
# $2 [ Import Database ]
#
function pwdman_import_database() {
local database database_password database_buffer
if [[ $# -gt 0 && -n "$1" ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
if [[ $# -gt 1 && -n "$2" ]]; then
import_database="$2"
else
pwdman_get_input "Import database filename: "
import_database="$INPUT"
fi
if [[ ! -f "$import_database" ]]; then
pwdman_exit "Error: import database not found."
fi
pwdman_get_input_password "Current database password: "
pwdman_decrypt_database "$database" "$PASSWORD"
database_password="$PASSWORD"
FIRST_LIST=()
database_buffer="$BUFFER"
while IFS= read -r line; do
FIRST_LIST+=("$(printf "%s" "$line" | base64 --decode)")
done <<< "$(printf "%s" "$BUFFER" | cut -d "," -f 1)"
pwdman_get_input_password "Import database password: "
pwdman_decrypt_database "$import_database" "$PASSWORD"
SECOND_LIST=()
while IFS= read -r line; do
SECOND_LIST+=("$(printf "%s" "$line" | base64 --decode)")
done <<< "$(printf "%s" "$BUFFER" | cut -d "," -f 1)"
if [[ "${#FIRST_LIST[@]}" -ne 0 && \
"${#SECOND_LIST[@]}" -ne 0 ]]; then
pwdman_check_merge_conflicts
fi
if [[ "$database_buffer" != "" ]]; then
database_buffer+=$'\n'
fi
database_buffer+="$BUFFER"
pwdman_encrypt_database "$database" "$database_password" "$database_buffer"
}
#
# Check Merge Conflicts
#
function pwdman_check_merge_conflicts() {
local ask
ask=0
for username in "${SECOND_LIST[@]}"; do
if printf "%s\\n" "${FIRST_LIST[@]}" | grep -q "$username"; then
echo "Warning: import database has an entry matching in the " \
"current database."
ask=1
break
fi
done
for username in "${FIRST_LIST[@]}"; do
if printf "%s\\n" "${SECOND_LIST[@]}" | grep -q "$username"; then
echo "Warning: current database has an entry matching the import " \
"database."
ask=1
break
fi
done
if [[ $ask == 1 ]]; then
pwdman_ask_continue 0
fi
}
#
# Continue Prompt
#
# $1 [ Default Yes ]
#
function pwdman_ask_continue() {
local yesno prompt
if [[ $# -gt 1 ]]; then
yesno="$1"
else
yesno=0
fi
if [[ $yesno -eq 0 ]]; then
prompt="Continue [y/N]? "
elif [[ $yesno -eq 1 ]]; then
prompt="Continue [Y/n]? "
else
prompt="Continue [y/n]? "
fi
pwdman_get_input "$prompt"
case "$INPUT" in
[Yy])
;;
[Nn])
pwdman_exit "Aborting."
;;
*)
if [[ $1 -eq 0 ]]; then
pwdman_exit "Aborting."
fi
;;
esac
}
#
# Get Random Password
#
# $1 [ Length ]
# $2 [ Alphabet ]
#
function pwdman_get_random_password() {
local length alphabet char
if [[ $# -gt 0 ]]; then
length="$1"
else
length="$DEFAULT_LENGTH"
fi
if [[ $# -gt 1 ]]; then
alphabet="$2"
else
alphabet="$DEFAULT_ALPHABET"
fi
PASSWORD=""
for _ in $(seq 1 "$length"); do
char=${alphabet:$RANDOM % ${#alphabet}:1}
PASSWORD+=$char
done
}
#
# Get Input Password
#
# $1 [ Prompt ]
#
function pwdman_get_input_password() {
pwdman_get_input "$1" "*"
PASSWORD="$INPUT"
}
#
# Get Input
#
# $1 [ Prompt ]
# $2 [ Hide character ]
#
function pwdman_get_input() {
local prompt
INPUT=""
prompt="${1:-Input: }"
while IFS= read -p "${prompt}" -r -s -n 1 char ; do
if [[ ${char} == $'\0' ]] ; then
break
elif [[ ${char} == $'\177' ]] ; then
if [[ -z "${INPUT}" ]] ; then
prompt=""
else
prompt=$'\b \b'
INPUT="${INPUT%?}"
fi
else
if [[ $# -gt 1 ]]; then
prompt="$2"
else
prompt="${char}"
fi
INPUT+="${char}"
fi
done
printf "%s" $'\n'
}
#
# Interactive Mode
#
# $1 [ Database ]
#
function pwdman_interactive() {
local database
if [[ $# -gt 0 ]]; then
database="$1"
else
database="$DEFAULT_DATABASE"
fi
while [[ -z "${action}" ]] ; do
# pwdman_version
# echo "Interactive Mode"
read -r -n 1 -p "> " action
printf "\\n"
done
case "$action" in
"h")
pwdman_help
;;
"v")
pwdman_version
;;
"r")
pwdman_read_password "$database"
;;
"w")
pwdman_write_password "$database"
;;
"u")
pwdman_update_password "$database"
;;
"d")
pwdman_delete_password "$database"
;;
"l")
pwdman_list "$database"
;;
"b")
pwdman_backup_database "$database"
;;
"x")
pwdman_reencrypt_database "$database"
;;
"c")
pwdman_create_database
;;
"m")
pwdman_import_backup "$database"
;;
"n")
pwdman_import_database "$database"
;;
*)
pwdman_exit "Error: invalid option. Press h for help."
esac
exit 0
}
#
# Password Manager Version
#
function pwdman_version() {
echo "Password Manager version $SCRIPT_VERSION"
}
#
# Password Manager Help
#
function pwdman_help(){
pwdman_version
cat <<EOF
usage: ./pwdman [options]
Options:
-h | (--)help Displays this information.
-v | (--)version Displays the script version.
-i | (--)interactive Runs the script in interactive mode.
-r | (--)read <arg> Reads a password from a database.
-w | (--)write <arg> Writes a new password in a database.
-u | (--)update <arg> Updates a password in a database.
-d | (--)delete <arg> Deletes a password from a database.
-l | (--)list <arg> Lists all passwords saved in a database.
-b | (--)backup <arg> Makes a backup dump of a database.
-x | (--)reencrypt <arg> Changes the password of a database.
-c | (--)create <arg> Creates a new database.
-m | (--)import-back <arg> Imports from an encoded backup.
-n | (--)import-enc <arg> Imports from an encrypted database.
EOF
}
#
# Exit Password Manager
#
# $1 Error Message
#
function pwdman_exit() {
if [[ $# -eq 0 ]]; then
echo "Error: undefined error."
else
echo "$1"
fi
exit 1
}
#
# Initialize Password Manager
#
function pwdman_initialize() {
pwdman_version
echo "Welcome! Set up pwdman."
pwdman_create_database "$DEFAULT_DATABASE"
}
#
# Password Manager Main
#
# $@ Arguments
#
function pwdman_main() {
if [[ ! -f "$DEFAULT_DATABASE" ]]; then
pwdman_initialize
if [[ $# -eq 0 ]]; then
exit 0
fi
fi
if [[ $# -eq 0 ]]; then
pwdman_exit "Please supply at least one argument. Type --help for help."
fi
case "$1" in
"-h" | "--help" | "help")
pwdman_help
;;
"-v" | "--version" | "version")
pwdman_version
;;
"-i" | "--interactive" | "interactive")
pwdman_interactive "${@:2}"
;;
"-r" | "--read" | "read")
pwdman_read_password "$3" "$2"
;;
"-w" | "--write" | "write")
pwdman_write_password "$3" "$2"
;;
"-u" | "--update" | "update")
pwdman_update_password "$3" "$2"
;;
"-d" | "--delete" | "delete")
pwdman_delete_password "$3" "$2"
;;
"-l" | "--list" | "list")
pwdman_list "${@:2}"
;;
"-b" | "--backup" | "backup")
pwdman_backup_database "$3" "$2"
;;
"-x" | "--reencrypt" | "reencrypt")
pwdman_reencrypt_database "${@:2}"
;;
"-c" | "--create" | "create")
pwdman_create_database "${@:2}"
;;
"-m" | "--import-plain" | "import-plain")
pwdman_import_backup "$3" "$2"
;;
"-n" | "--import-enc" | "import-enc")
pwdman_import_database "$3" "$2"
;;
*)
echo "Invalid argument(s). Type --help for help."
exit 1
;;
esac
exit 0
}
pwdman_main "$@"