summaryrefslogtreecommitdiff
path: root/bin/raps2
blob: 0bd8f5249915f49b8a85c91c606dcf5380cc2ad8 (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
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
#!/usr/bin/env perl
## Copyright © 2011 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL:
##   0. You just DO WHAT THE FUCK YOU WANT TO.
use strict;
use warnings;
use 5.010;

use App::Raps2;
use File::BaseDir qw(data_files data_home);
use File::Slurp qw(read_dir);
use Getopt::Long qw(:config no_ignore_case);

my ( $default_cost, $no_echo, $paste, $pwgen_cmd, $xclip_cmd );

our $VERSION = '0.52';

GetOptions(
	'c|cost=i'      => \$default_cost,
	'E|no-echo'     => \$no_echo,
	'h|help'        => sub { cmd_help(0) },
	'p|pwgen-cmd=s' => \$pwgen_cmd,
	'P|paste'       => \$paste,
	'V|version'     => sub { say "raps2 version ${VERSION}"; exit 0 },
	'x|xclip-cmd=s' => \$xclip_cmd,

) or cmd_help(1);

my ( $action, @args ) = @ARGV;
my $raps2 = App::Raps2->new(
	cost      => $default_cost,
	pwgen_cmd => $pwgen_cmd,
	xclip_cmd => $xclip_cmd,
);

sub file_must_exist {
	my ( $file, $name ) = @_;

	if ( not defined $file ) {
		say STDERR "No such account: ${name}";
		exit 2;
	}

	return;
}

sub file_must_not_exist {
	my ( $file, $name ) = @_;

	if ( -e $file ) {
		say STDERR "Account already exists: ${name}";
		exit 2;
	}

	return;
}

sub cmd_add {
	my ($name) = @_;

	if ( not $name ) {
		cmd_help( 1, 'add <account>' );
	}

	my $pwfile = data_home('raps2') . "/${name}";

	file_must_not_exist( $pwfile, $name );

	$raps2->get_master_password();

	my $url   = $raps2->ui->read_line('URL');
	my $login = $raps2->ui->read_line('Login');
	my $pass  = $raps2->ui->read_pw( 'Password', 1 );
	my $extra = $raps2->ui->read_multiline('Additional content');

	if ( length($pass) == 0 ) {
		$pass = $raps2->generate_password();

		if ( not $pass ) {
			say STDERR "Password generation failed: ${!}: "
			  . $raps2->conf('pwgen_cmd');
			exit 3;
		}

		if ($paste) {
			$raps2->ui->to_clipboard( $pass, $raps2->conf('xclip_cmd') );
		}
		elsif ( not $no_echo ) {
			$raps2->ui->output( [ 'Generated password', $pass ] );
		}
	}

	$raps2->pw_save(
		file     => $pwfile,
		url      => $url,
		login    => $login,
		password => $pass,
		extra    => $extra,
	);

	return;
}

sub cmd_dump {
	my ($name) = @_;

	if ( not $name ) {
		cmd_help( 1, 'dump <account>' );
	}

	my $pwfile = data_files("raps2/${name}");

	file_must_exist( $pwfile, $name );

	$raps2->get_master_password();

	my $key = $raps2->pw_load( file => $pwfile );

	$raps2->ui->output(
		[ 'URL',      $key->{url} ],
		[ 'Login',    $key->{login} ],
		[ 'Password', $key->{password} ],
	);
	if ( $key->{extra} ) {
		print $key->{extra};
	}

	return;
}

sub cmd_edit {
	my ($name) = @_;

	if ( not $name ) {
		cmd_help( 1, 'edit <account>' );
	}

	my $pwfile = data_files("raps2/${name}");

	file_must_exist( $pwfile, $name );

	$raps2->get_master_password();

	my $key = $raps2->pw_load( file => $pwfile );

	my $salt     = $key->{salt};
	my $cost     = $key->{cost};
	my $url      = $raps2->ui->read_line( 'URL', $key->{url} );
	my $login    = $raps2->ui->read_line( 'Login', $key->{login} );
	my $pass     = $key->{password};
	my $new_pass = $raps2->ui->read_pw( 'New password (empty to keep old)', 1 );
	my $extra    = $key->{extra} // q{};

	if ( length($new_pass) ) {
		$pass = $new_pass;
	}

	$raps2->pw_save(
		file     => $pwfile,
		salt     => $salt,
		cost     => $cost,
		url      => $url,
		login    => $login,
		password => $pass,
		extra    => $extra,
	);

	return;
}

sub cmd_get {
	my ($name) = @_;

	if ( not $name ) {
		cmd_help( 1, 'get <account>' );
	}

	my $pwfile = data_files("raps2/${name}");

	file_must_exist( $pwfile, $name );

	$raps2->get_master_password();

	my $key = $raps2->pw_load( file => $pwfile );

	$raps2->ui->to_clipboard( $key->{password}, $raps2->conf('xclip_cmd') )
	  or die("Could not place password in clipboard: ${!}\n");

	if ( $key->{extra} ) {
		print $key->{extra};
	}

	return;
}

sub cmd_help {
	my ( $exit_status, $subcmd ) = @_;

	$subcmd //= 'add|get|dump|... [account]';

	say "Usage: raps2 ${subcmd}";
	say 'See also: "man raps2"';

	exit $exit_status;
}

sub cmd_info {
	my ($name) = @_;

	if ( not $name ) {
		cmd_help( 1, 'info <account>' );
	}

	my $pwfile = data_files("raps2/${name}");

	file_must_exist( $pwfile, $name );

	my $key = $raps2->pw_load_info( file => $pwfile );
	$raps2->ui->output( [ 'URL', $key->{url} ], [ 'Login', $key->{login} ], );

	return;
}

sub cmd_list {
	my @files = read_dir( data_home('raps2') );

	for my $file ( sort @files ) {
		my $key = $raps2->pw_load_info( name => $file );
		$raps2->ui->list(
			[ 'Account', $file ],
			[ 'Login',   $key->{login} ],
			[ 'URL',     $key->{url} ],
		);
	}

	return;
}

sub cmd_remove {
	my ($name) = @_;

	if ( not $name ) {
		cmd_help( 1, 'del <account>' );
	}

	my $pwfile = data_files("raps2/${name}");

	file_must_exist( $pwfile, $name );

	unlink($pwfile)
	  or die("Could not unlink ${pwfile}: ${!}\n");

	return;
}

given ($action) {
	when ('add')  { cmd_add(@args) }
	when ('del')  { cmd_remove(@args) }
	when ('dump') { cmd_dump(@args) }
	when ('edit') { cmd_edit(@args) }
	when ('get')  { cmd_get(@args) }
	when ('info') { cmd_info(@args) }
	when ('list') { cmd_list(@args) }

	default { cmd_help(1) }
}

__END__

=head1 NAME

raps2 - "Right, Another Password Store" take two

=head1 SYNOPSIS

B<raps2> [I<options>] I<action> [I<args ...>]

=head1 VERSION

This manual documents B<raps2> version 0.52

=head1 DESCRIPTION

raps2 is a simple password safe.  You give it a name, a password and optional
metadata, and it will encrypt and store them for you.  You probably want to
start with C<< raps2 add accountname >>, and then later use C<< raps2 get
accountname >> and paste the corresponding password into whatever application
requires it.  B<raps2> will automatically initialize its store when used for
the first time.

Supported metadata are "URL", "Login" and the multiline "Extra" field.  URL
and Login will be saved as plaintext, Extra is encrypted like the password.

=head1 ACTIONS

=over

=item B<add> I<account>

Adds I<account> to the store.  It will ask you for the store's master
password, some metadata and the new password and then store them.

If you do not provide a password (that is, leave both "Password" and "Verify"
lines blank), B<raps2> will use the B<pwgen> command to create one for you and
print the generated password on stdout.

See also the B<-c>, B<-E>, B<-p> and B<-P> options.

=item B<del> I<account>

Remove I<account> from the store.

=item B<dump> I<account>

Dump everything saved for I<account>, including the clear-text password, to
stdout.

=item B<edit> I<account>

Edit saved data for I<account>.  Note that editing the multiline "extra" field
is not yet possible.

=item B<get> I<account>

Decrypt I<account>'s password and store it in the primary X Clipboard.  Note
that it can only be pasted once.  Prints the content of the multiline B<extra>
field (if present) to stdout.

See also the B<-x> option.

=item B<info> I<account>

Show information about I<account>, does not require the master password.

=item B<list>

List all saved accounts with their respective Logins and URLs

=item B<version>

Show version information

=back

=head1 OPTIONS

=over

=item B<-c>, B<--cost> I<int>

Key setup cost to use for new passwords, overrides the configuration file.
Only makes sense with B<raps2 add>.

Please be aware that the key setup time is an exponential function. That is,
when you increment the cost by 1, the key setup time will double.  See
Crypt::Eksblowfish(3pm).

Default: 12

=item B<-E>, B<--no-echo>

When using the pwgen functionality of B<raps2 add>, do not print the generated
password on stdout.

=item B<-P>, B<--paste>

When using the pwgen functionality of B<raps2 add>, do not print the generated
password on stdout.  Place it in the X Clipboard instead.

Note that it can only be pasted once.

=item B<-p>, B<--pwgen-cmd> I<command>

When the user does not enter a password in B<raps2 add>, it will execute
I<command> to create one. The first line of output is taken as password.
If the output contains spaces, anything after the first space (plus the space
itself) is discarded.

Default: pwgen -s 23 1

=item B<-V>, B<--version>

Show version information.

=item B<-x>, B<--xclip-cmd> I<command>

Command to run for B<raps2 get>.  Set this to C<< xclip -l 2 >> if you are
using chrome.  The password will be available on I<command>'s stdin.

Default: xclip -l 1

=back

=head1 EXIT STATUS

zero on success, non-zero otherwise.

=head1 CONFIGURATION

raps2 saves the master password hash in F<~/.config/raps2/password>.

The configuration (key setup cost and pwgen command) is stored in
F<~/.config/raps2/defaults> in an INI-like format.

Additional encrypted passwords are stored in F<~/.local/share/raps2/>.

These directories can be changed by setting the B<XDG_CONFIG_HOME> and
B<XDG_DATA_HOME> environment variables.

The following settings are available in F<~/.config/raps2/defaults>:

=over

=item B<cost> = 12

Default key setup cost.  See the B<-c> option.

=item B<pwgen_cmd> = pwgen -s 23 1

Command used to generate passwords.  See the B<-p> option.

=item B<xclip_cmd> = xclip -l 1

Command used to place passwords in the clipboard.  See the B<-x> option.

=back

Note that commandline arguments always override options set in the
configuration file.

=head1 DEPENDENCIES

=over

=item * Config::Tiny

=item * Crypt::CBC

=item * Crypt::Eksblowfish

=item * File::BaseDir

=item * File::Path (usually included with perl core)

=item * File::Slurp

=item * pwgen (if you want C<< raps2 add >> to generate passwords)

=item * xclip (for C<< raps2 get >>)

=back

=head1 BUGS AND LIMITATIONS

This is alpha software, the store format may change without further notice.
Backwards-compatibility is not guaranteed.

When running for the first time, raps2 will ask for the master passphrase
three times. Two would be better.

=head1 AUTHOR

Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.