-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeepass.pl
executable file
·437 lines (339 loc) · 9.21 KB
/
keepass.pl
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
#!/usr/bin/perl
##
# keepass - KeePassX Command Line Interface
#
# Author: Danilo Abbasciano <piuma _at_ piumalab.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
##
use Data::Dumper qw(Dumper);
use Getopt::Long qw(:config bundling);
use Term::ReadKey;
use String::Random;
use File::KeePass 2.03; # non-core, >=v0.03 needed due critical bug fixes
use Clipboard;
use constant DEFAULT_FILE => '~/.keepassx.kdb'; # default file
#
# main
#
$sFile = DEFAULT_FILE;
$sPassPhrase = null;
my $oKeePass = File::KeePass->new;
my $sPassPhrase = '';
options();
if ($fCreateDb) {
if (-e $sFile) {
print "$sFile already exists\n";
exit 1;
}
$oKeePass->add_group({title => 'Internet', });
$oKeePass->add_group({title => 'eMail', });
$oKeePass->unlock;
$oKeePass->save_db($sFile, readPassphrase());
exit 1;
}
msg("open file: $sFile");
if (! -e $sFile) {
print "Can't open $sFile: No such file or directory\n";
exit 1;
}
printWhatIDoMessage();
while (! eval { $oKeePass->load_db($sFile, readPassphrase()) }) {
print "Passphrase is incorrect\n";
msg("Couldn't load the file $sFile: $@");
}
# add entry
if ($fAddEntry) {
addEntry();
exit 1;
}
if ($fLongListing) {
msg("Long listing of all entries");
printLongList($oKeePass);
$oKeePass->clear;
exit 0;
}
#$oKeePass->unlock;
#print Dumper $oKeePass->groups; # passwords are now visible
my $oEntry = $oKeePass->find_entry({title => $sTitle});
if (!$oEntry) {
print "No matching entries\n";
exit 1;
}
msg(Dumper $oEntry);
if ($fUserName) {
msg('username copyed');
Clipboard->copy($oEntry->{'username'});
if ($fEcho) {
print "username for ${sTitle}: " . $oEntry->{'username'} . "\n";
}
}
if ($fPassword) {
msg('password copyed');
Clipboard->copy($oKeePass->locked_entry_password($oEntry));
if ($fEcho) {
print "password for ${sTitle}: " . $oKeePass->locked_entry_password($oEntry) . "\n";
}
}
$oKeePass->clear;
exit 0;
#
# Functions
#
sub options () {
# Process options.
if ( @ARGV > 0 ) {
GetOptions('f|file=s'=> \$sFile,
'l' => \$fLongListing,
'u|username' => \$fUserName,
'p|password' => \$fPassword,
'E|echo' => \$fEcho,
# 'o|output=s' => \$fFileOutput,
'a|add' => \$fAddEntry,
'c|createdb' => \$fCreateDb,
'h|?|help' => \$fHelp,
'v|verbose' => \$fVerbose,
'V|version' => \$fVersion)
or help();
}
if ($fHelp) {
help();
}
if ($fVersion) {
version();
}
($sTitle) = @ARGV;
if ($sTitle eq '' && !$fAddEntry) {
msg("long listing setted to true");
$fLongListing = true;
}
}
sub msg($) {
my ($sMessage) = shift;
print STDERR $sMessage . "\n" if $fVerbose;
}
sub addEntry() {
our $sPassPhrase;
msg("Add a new entry");
# title
# username (name)
# comment
# url
print "title: ";
my $sTitle = <STDIN>;
chomp $sTitle;
# group ??
print "username: ";
my $sUsername = <STDIN>;
chomp $sUsername;
PASSWORD:
print "password [return for random]: ";
my $sPassword = <STDIN>;
chomp $sPassword;
if ($sPassword eq '') {
print "Generate random password? [y] ";
$sLine = ReadLine(0);
if ($sLine eq "y\n" || $sLine eq "\n") {
my $sChoice = '';
my $nLength = 36;
my $nIndexPattern = 0;
my @sPattern = ("alpha/digit/symbol", "alpha/digit", "digits only");
do {
$sPassword = getRandomPassword($nLength, $nIndexPattern);
print "Use $sPassword \n";
print "type $sPattern[$nIndexPattern], length $nLength [y/N/ /+/-/q/?] ? ";
ReadMode("raw");
$sChoice = ReadKey(0);
ReadMode("normal");
print "$sChoice\n";
# chomp $sChoice;
if ($sChoice eq ' ') {
$nIndexPattern = ($nIndexPattern + 1) % 3;
}
elsif ($sChoice eq '+') {
$nLength += 4;
}
elsif ($sChoice eq '-') {
if ($nLength > 5) {
$nLength -= 4;
}
}
elsif ($sChoice eq '?') {
print "Commands:\n";
print " y Yes, accept this password\n";
print " N No, generate another password of same type\n";
print " <space> Cycle through password types\n";
print " - Lower the password length\n";
print " + Raise the password length\n";
print " q Quit\n";
print " ? Help\n";
}
elsif ($sChoice eq 'q') {
goto PASSWORD;
}
} while ($sChoice ne "y");
}
else {
print "password again: ";
$sPassword = <STDIN>;
chomp $sPassword;
}
}
print "url [<none>]: ";
my $sUrl = <STDIN>;
chomp $sUrl;
print "comment [<none>]: ";
my $sComment = <STDIN>;
chomp $sComment;
# I must unlock the passwords before adding new entries.
$oKeePass->unlock;
my $e = $oKeePass->add_entry({
title => $sTitle,
group => $gid, # group id o il risultato di find_group
# OR group => $group,
username => $sUsername,
password => $sPassword,
url => $sUrl,
comment => $sComment});
$oKeePass->save_db($sFile, $sPassPhrase);
}
sub getRandomPassword($$) {
my $nLength = shift;
my $nIndexPattern = shift;
my $oStringRandom = new String::Random;
# c Any lowercase character [a-z]
# C Any uppercase character [A-Z]
# n Any digit [0-9]
# ! A punctuation character [~`!@$%^&*()-_+={}[]|\:;"'.<>?/#,]
# . Any of the above
# s A "salt" character [A-Za-z0-9./]
# b Any binary data
$oStringRandom->{'A'} = [ 'A'..'Z', 'a'..'z', '0'..'9' ];
my @aPatterns = ('.', # alpha/digit/symbol
'A', # alpha/digit
'n' # digits only
);
msg("Index Pattern: $nIndexPattern");
msg("Password pattern: " . $aPatterns[$nIndexPattern] x $nLength);
$sPassword = $oStringRandom->randpattern($aPatterns[$nIndexPattern] x $nLength);
return $sPassword;
}
sub readPassphrase() {
our $sPassPhrase;
print "Enter passphrase for $sFile: ";
# uncomment for debug
#$sPassPhrase = 'password';
#print "\n";
#return $sPassPhrase;
ReadMode('noecho');
$sPassPhrase = ReadLine(0);
ReadMode 'normal';
chomp $sPassPhrase;
print "\n";
return $sPassPhrase;
}
sub printLongList() { # $oKeePass
my ($oKeePass) = shift;
# print Dumper $oKeePass; # passwords are locked
print "=" x 30 . "\n";
if (defined($oKeePass->{'groups'})) {
foreach $oGroup (values $oKeePass->{'groups'}) {
printLongListGroup($oGroup, '');
}
}
}
sub printLongListGroup() { # $oGroup $sGroupName
my ($oGroup) = shift;
my $sGroupName = shift;
if (defined($oGroup->{'title'})) {
$sGroupName = $sGroupName . '/' . $oGroup->{'title'};
print "##\n# Group: ${sGroupName}\n##\n";
printEntries($oGroup);
if (defined($oGroup->{'groups'})) {
foreach $oSubGroup (values $oGroup->{'groups'}) {
printLongListGroup($oSubGroup, $sGroupName);
}
}
}
}
sub printEntries() { # $oGroup
my ($oGroup) = shift;
if (defined($oGroup->{'entries'})) {
foreach $oEntry (values $oGroup->{'entries'}) {
# print Dumper $oEntry;
if ($oEntry->{'title'} eq 'Meta-Info' && $oEntry->{'username'} eq 'SYSTEM') {
next;
}
print "[" . $oEntry->{'title'} . "]\n";
print " Username: " . $oEntry->{'username'} . "\n";
if ($oEntry->{'comment'} ne '') {
print " Comment : " . $oEntry->{'comment'} . "\n";
}
if ($oEntry->{'url'} ne '') {
print " URL : " . $oEntry->{'url'} . "\n";
}
}
}
}
sub printWhatIDoMessage() {
if ($fAddEntry) {
return;
}
print "Going to ";
if ($fEcho || $fLongListing) {
print "print ";
}
else {
print "copy ";
}
if ($fUserName) {
print "username ";
if ($fPassword) {
print "and "
}
}
if ($fPassword) {
print "password ";
}
if ($fLongListing) {
print "list ";
}
if ($fEcho || $fLongListing) {
print "to stdout\n";
}
else {
print "to X selection\n";
}
}
sub version() {
print "keepass - commandline tool compatible with KeePassX\n";
print "Version 0.1\n";
exit 1;
}
sub help() {
$sMessage = <<END;
keepass - commandline tool compatible with KeePassX
Usage: keepass [OPTION] [NAME]
Options:
-f, --file=DATABASE_FILE specify the database file (default is ~/.keepass.kdb)
-l long listing (show username & notes) [default]
-u, --username emit username of listed account
-p, --password emit password of listed account
-E, --echo force echoing of entry to stdout
-a, --add add an entry
-c, --createdb create an empty database
-v, --verbose print more information (can be repeated)
-h, --help display this help and exit
-V, --version output version information and exit
END
print $sMessage;
exit 1;
}