summaryrefslogtreecommitdiff
path: root/bin/raps2
blob: 42383b9d23b396b546ada73efaeb54fb01c8c5b1 (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
#!/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 autodie;

use Crypt::CBC;
use Crypt::Eksblowfish;
use Crypt::Eksblowfish::Bcrypt qw(bcrypt_hash en_base64 de_base64);
use File::Path qw(make_path);
use File::Slurp qw(slurp write_file);
use POSIX;

my $VERSION = '0.1';

my %state = (
	cost => 12,
	salt => new_salt(),
);

my ($action, @args) = @ARGV;

sub cmd_add {
	my ($name) = @_;
	my $init = get_xdg_config_file('init');
	my $store = get_xdg_data_file($name);
	my $pass;

	if (-e $store) {
		die("This password name already exists\n");
	}

	my $cipher;
	my $password;

	$password = get_password();
	undef %state;
	load_state_from($init);
	$state{'salt'} = new_salt();
	$state{'cost'} //= 12;

	$state{'url'} = read_input(
		prefix => 'URL'
	);
	$state{'login'} = read_input(
		prefix => 'Login'
	);
	$state{'extra'} = read_input(
		prefix => 'Extra'
	);
	$pass = read_input(
		prefix => 'Password',
		invisible => 1,
		verify => 1,
	);

	$cipher = setup_cipher($password);

	$state{'hash'} = $cipher->encrypt_hex($pass);

	save_state_to($store);

	return;
}

sub cmd_dump {
	my ($name) = @_;
	my $store = get_xdg_data_file($name);
	my $password;
	my $cipher;

	if (not -e $store) {
		die("No such password\n");
	}

	$password = get_password();
	load_state_from($store);
	$cipher = setup_cipher($password);

	printf(
		"%-12s %s\n" x 4,
		'URL'     , $state{'url'},
		'Login'   , $state{'login'},
		'Extra'   , $state{'extra'} // q{},
		'Password', $cipher->decrypt_hex($state{'hash'}),
	);

	return;
}

sub cmd_get {
	my ($name) = @_;
	my $store = get_xdg_data_file($name);
	my $password;
	my $cipher;

	if (not -e $store) {
		die("No such password\n");
	}

	$password = get_password();
	load_state_from($store);

	$cipher = setup_cipher($password);

	to_clipboard($cipher->decrypt_hex($state{'hash'}));

	return;
}

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

	if (not -e $store) {
		die("No such password\n");
	}

	load_state_from($store);

	printf(
		"URL  : %s\nLogin: %s\n",
		$state{'url'},
		$state{'login'}
	);

	return;
}

sub create_dot_dirs {
	make_path(get_xdg_config_file());
	make_path(get_xdg_data_file());
	return;
}

sub create_pass {
	my ($passfile) = @_;
	my $pass;
	my $hash;

	say 'raps2 was never run before. Please set master password first.';
	$pass = read_input(
		prefix => 'Password',
		invisible => 1,
		verify => 1,
	);

	$hash = en_base64(bcrypt_hash({
			key_nul => 1,
			cost => $state{'cost'},
			salt => $state{'salt'},
		}, $pass));
	write_file(
		$passfile,
		"cost $state{cost}\n",
		"salt $state{salt}\n",
		"hash $hash\n",
	);

	return $pass;
}

sub get_password {
	my $pass;
	my $passfile = get_xdg_config_file('password');

	if (not -e $passfile) {
		 return create_pass($passfile);
	}

	load_state_from($passfile);
	$pass = read_input(
		prefix => 'Master password',
		invisible => 1,
	);
	if (en_base64(bcrypt_hash({
				key_nul => 1,
				cost => $state{'cost'},
				salt => $state{'salt'},
			}, $pass)) ne $state{'hash'}) {
		die("Invalid passphrase\n");
	}

	return $pass;
}

sub get_xdg_config_file {
	my ($file) = @_;
	my $env    = $ENV{'XDG_CONFIG_HOME'};
	my $home   = $ENV{'HOME'};

	$file //= q{};
	$env  //= "${home}/.config";

	return "${env}/raps2/${file}";
}

sub get_xdg_data_file {
	my ($file) = @_;
	my $env    = $ENV{'XDG_DATA_HOME'};
	my $home   = $ENV{'HOME'};

	$file //= q{};
	$env  //= "${home}/.local/share";

	return "${env}/raps2/${file}";
}

sub load_state_from {
	my ($file) = @_;

	if (not -e $file) {
		return;
	}

	for my $line (slurp($file)) {
		my ($key, $value) = split(qr{\s+}, $line);

		if (not ($key and $value)) {
			next;
		}

		$state{$key} = $value;
	}
	return;
}

sub new_salt {
	my $salt = q{};

	for (1 .. 16) {
		$salt .= chr(0x21 + int(rand(90)));
	}

	return $salt;
}

sub save_state_to {
	my ($file) = @_;
	my $raw = q{};

	while (my ($key, $value) = each(%state)) {
		$raw .= "${key} ${value}\n";
	}

	write_file($file, $raw);
	return;
}

sub setup_cipher {
	my ($password) = @_;

	my $eksblowfish = Crypt::Eksblowfish->new(
		$state{'cost'},
		$state{'salt'},
		$password,
	);
	return Crypt::CBC->new(-cipher => $eksblowfish);
}

sub to_clipboard {
	my ($pw) = @_;

	open(my $clipboard, '|-', 'xclip -l 1');
	print $clipboard $pw;
	close($clipboard);
	return;
}

sub read_input {
	my %opts = @_;
	my ($prefix, $invisible, $verify)
		= @opts{'prefix', 'invisible', 'verify'};
	my $term = POSIX::Termios->new();
	my ($input1, $input2);

	if ($invisible) {
		$term->getattr(0);
		$term->setlflag($term->getlflag() & ~POSIX::ECHO);
		$term->setattr(0, POSIX::TCSANOW);
	}

	print "${prefix}: ";
	$input1 = readline(STDIN);

	if ($invisible) {
		print "\n";
	}

	if ($verify) {
		print 'Verify: ';
		$input2 = readline(STDIN);

		if ($invisible) {
			print "\n";
		}
	}

	if ($invisible) {
		$term->setlflag($term->getlflag() | POSIX::ECHO);
		$term->setattr(0, POSIX::TCSANOW);
	}

	if ($verify and $input1 ne $input2) {
		die("Lines do not match\n");
	}

	chomp $input1;
	return $input1;
}

create_dot_dirs();

given ($action) {
	when ('add')  { cmd_add(@args) }
	when ('dump') { cmd_dump(@args) }
	when ('get')  { cmd_get(@args) }
	when ('info') { cmd_info(@args) }
}

__END__

=head1 NAME

raps2 - "Right, Another Password Store" take two

=head1 SYNOPSIS

B<raps2> I<action> I<args ...>

=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.

=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.  Note that
only the password is encrypted, the metadata is saved as clear-text.

=item B<dump> I<account>

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

=item B<get> I<account>

Decrypt I<account>'s password and store it in the X Clipboard.  Note that it
can only be pasted once.

=item B<info> I<account>

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

=back

=head1 EXIT STATUS

zero on success, non-zero otherwise.

=head1 CONFIGURATION

None so far.

=head1 DEPENDENCIES

=over

=item * Crypt::CBC

=item * Crypt::Eksblowfish

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

=item * File::Slurp

=back

=head1 BUGS AND LIMITATIONS

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

=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.