-
Notifications
You must be signed in to change notification settings - Fork 0
/
createpass
executable file
·309 lines (258 loc) · 6.33 KB
/
createpass
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
#!/usr/bin/perl
use strict;
use Getopt::Long;
use Pod::Usage;
use Term::Complete;
use File::BaseDir qw(config_home config_dirs);
use File::Path "make_path";
use File::Copy "move";
use Path::Class;
=pod
=head1 NAME
createpass - Create derivating passwords from a master one
=head1 SYNOPSIS
createpass [options]
=cut
my $man = 0;
my $help = 0;
my $twice = 0;
my $batch = 0;
my $visible = 0;
my $length = 8;
my $master;
my $domain;
my $user;
my $info;
my $noinfo = 0;
my $key;
my $nokey;
my $algo="SHA-512";
=pod
=head1 OPTIONS
=over 8
=item B<--man>
Prints the manual page and exits.
=item B<--help>
Print a brief help message and exits.
=item B<--length>
num length of generated password (default 8)
=item B<--master str>
master password (required)
=item B<--user str>
user to target
=item B<--domain str>
domain to target
=item B<--info str>
info to target
=item B<--no-info str>
do not ask for info
=item B<--no-key>
ignore key defined in ~/.passtools.rc (usefull for password update)
=item B<--algo str>
algo to use to hash the password (default to SHA-512)
=item B<--twice>
prompt password twice
=item B<--batch>
batch mode: generate several passwords with one master
=item B<--visible>
print generated password to screen instead of copying it to clipboard
=back
=cut
my @USERS=('');
my @DOMAINS=('');
my @INFOS=('');
sub migrateOldConf() {
my $xdg_conffile=config_home('passtools', 'config.rc');
my $old_conffile=file($ENV{"HOME"},'.passtools.rc');
if (-e $old_conffile && ! -e $xdg_conffile) {
print STDERR "Moving old conffile to XDG compliant path\n",
"('$old_conffile' => '$xdg_conffile')\n";
make_path(config_home('passtools'));
move($old_conffile, $xdg_conffile) or
die "Cannot rename $old_conffile into $xdg_conffile: $!";
}
}
# Read configuration file
sub readConf() {
my @confdirs=config_dirs('passtools');
my $confFile;
foreach my $dir (@confdirs) {
$confFile=file($dir, 'config.rc');
my $st=$confFile->stat;
if ( $st && -r $st ) {
last;
}
$confFile=undef;
}
if (not defined($confFile)){
return "No config file";
}
# Put config data into a separate namespace
# Process the contents of the config file
our $err;
{
package CFG;
my $rc = do($confFile);
# Check for errors
if ($@) {
$::err = "ERROR: Failure compiling '$confFile' - $@";
} elsif (! defined($rc)) {
$err = "ERROR: Failure reading '$confFile' - $!";
} elsif (! $rc) {
$::err = "ERROR: Failure processing '$confFile'";
}
}
if($err){
print(STDERR $err,"\n");
exit(1);
}
# Update config
@USERS=@{$CFG::CFG{'USERS'}};
@DOMAINS=@{$CFG::CFG{'DOMAINS'}};
@INFOS=@{$CFG::CFG{'INFOS'}};
$length=$CFG::CFG{'length'};
$batch=$CFG::CFG{'batch'};
$twice=$CFG::CFG{'twice'};
$visible=$CFG::CFG{'visible'};
$algo=$CFG::CFG{'algo'};
$key=$CFG::CFG{'key'};
$noinfo=$CFG::CFG{'noinfo'};
}
migrateOldConf();
readConf();
my $opts={
'man' => \$man,
'help'=> \$help,
'length' => \$length,
'master' => \$master,
'domain' => \$domain,
'user' => \$user,
'info' => \$info,
'no-info' => \$noinfo,
'algo' => \$algo,
'twice'=> \$twice,
'batch' => \$batch,
'visible' => \$visible,
'no-key' => \$nokey,
};
GetOptions($opts, 'help|h', 'man', 'length=i', 'master=s', 'rules=s',
'domain=s', 'info=s', 'no-info|I', 'algo=s', 'user=s', 'twice|t','batch|b',
'visible|v', 'no-key|K') or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;
sub getpass($) {
my $prompt=shift;
$|=1; #Turn on AutoFlush
use Term::ReadKey;
print "$prompt";
ReadMode('noecho');
ReadMode('raw');
my $pass = '';
while (1) {
my $c;
1 until defined($c = ReadKey(-1));
last if $c eq "\n";
print "*";
$pass .= $c;
}
ReadMode('restore');
print "\n";
return $pass;
}
sub compute_pass($$) {
my $master=shift;
my $part=shift;
my $combined=$master."\n".$part."\n";
if (uc($algo) eq 'MD5SUM') {
use Digest::MD5 qw(md5_base64);
$combined=md5_base64($combined);
} else {
use Digest::SHA;
#print "algo: $algo\n";
my $d=Digest::SHA->new($algo);
$d->add($combined);
$combined=$d->b64digest();
}
return $combined;
}
sub generate_pass($){
my $master=shift;
if (defined($user)) {
$master=compute_pass($master, $user);
}
if (defined($domain)) {
$master=compute_pass($master, $domain);
}
if ($noinfo eq 0 && defined($info)) {
$master=compute_pass($master, $info);
}
if (!defined($nokey) && defined($key) && $key ne '') {
$master=compute_pass($master, $key);
}
$master=substr($master, 1, $length);
return $master
}
sub get_input($$@){
my $ret=shift;
my $name=shift;
my @COMPL=shift;
my $line= Complete("$name [$ret] : ",@COMPL);
chomp($line);
unless ($line eq ""){$ret=$line;};
return $ret;
}
sub set_master(){
if (!defined($master)) {
my ($pass1, $pass2);
while(1) {
$pass1=getpass("Master password : ");
last if( $twice eq 0);
$pass2=getpass("Master password again : ");
last if ($pass1 eq $pass2);
print "Different password, try again\n";
}
$master=$pass1;
}
}
my $continue=0;
while($continue ge 0){
if($user eq "" || $domain eq "" || $continue > 0){
$user=get_input($user, "Username", \@USERS);
$domain=get_input($domain, "Domain", \@DOMAINS);
if($noinfo eq 0){
$info=get_input($info, "Info", \@INFOS);
}
}
set_master();
if($visible eq 0){
print "Password copied to clipboard, use middle click to paste it.\n";
my $pid=fork();
if($pid eq 0){
open PIPE, "| xclip -verbose";
print PIPE generate_pass($master);
close(PIPE);
exit 0;
}else{
my $old=$SIG{INT};
$SIG{INT} = sub { print "\n"; };
wait;
$SIG{INT}=$old;
}
}else{
print generate_pass($master), "\n";
}
if($batch eq 0){
$continue=-1;
}else{
$continue++;
print "Exit batch mode [Y/n] : ";
my $answer=<STDIN>;
unless ($answer =~ /^[Nn][Oo]?$/){$continue=-1;};
}
}
__END__
=pod
=head1 DESCRIPTION
B<This program> will read the given input file(s) and do something
useful with the contents thereof.
=cut