Passwd/Keyring/PWSafe3.pm

filter: Statementstmt 49.4% 43 / 87 Branchbran 18.1% 4 / 22 Conditioncond 28.5% 6 / 21 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 62.5% 10 / 16 Podpod 100% 6 / 6 totaltotal 44.3% 71 / 160 CC 22 SCAR 30.5
CC
22
Cov
41%
_find_record
72.0
_prompt_for_password
12.0
set_password
12.0
CRAP 21.1
↑ Summary auto/share/dist/Passwd-Keyring-PWSafe3/imap_query_example.pl »
1 package Passwd::Keyring::PWSafe3;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use parent 'Keyring';
6 11 ▶use Crypt::PWSafe3;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Term::ReadKey; # For secure password prompt
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Passwd::Keyring::PWSafe3 - Password storage based on Password Safe encrypted files
15  
16 =head1 VERSION
17  
18 Version 0.61
19  
20 =cut
21  
22 11 our $VERSION = '0.61';
23  
24 11 our $APP_NAME = "Passwd::Keyring";
25 11 our $FOLDER_NAME = "Perl-Passwd-Keyring";
26  
27 =head1 SYNOPSIS
28  
29 Password Safe implementation of L<Passwd::Keyring>. Passwords are
30 stored in the L<Password Safe|http://passwordsafe.sourceforge.net>
31 encrypted file.
32  
33 use Passwd::Keyring::PWSafe3;
34  
35 my $keyring = Passwd::Keyring::PWSafe3->new(
36 app=>"blahblah scraper",
37 group=>"Johnny web scrapers",
38 file=>"/home/joe/secrets.pwsafe3", # ~/passwd-keyring.pwsafe3 by default
39 master_password=>"very secret password", # Or callback. See below
40 );
41  
42 my $username = "John"; # or get from .ini, or from .argv...
43  
44 my $password = $keyring->get_password($username, "blahblah.com");
45 unless( $password ) {
46 $password = <somehow interactively prompt for password>;
47  
48 # securely save password for future use
49 $keyring->set_password($username, $password, "blahblah.com");
50 }
51  
52 login_somewhere_using($username, $password);
53 if( password_was_wrong ) {
54 $keyring->clear_password($username, "blahblah.com");
55 }
56  
57 =head1 DESCRIPTION
58  
59 This module does not require Password Safe to be installed, and can be
60 used as generic "store many passwords in file encrypted with single
61 master password" storage. Password Safe GUI, if installed, may help
62 the user to review, modify, or delete saved passwords.
63  
64 =over 4
65  
66 Official GUIs can be freely downloaded from L<the official
67 site|http://passwordsafe.sourceforge.net> - both Windows and (beta)
68 Linux versions are available. Apart from them there exist
69 various L<compatible tools|http://passwordsafe.sourceforge.net/relatedprojects.shtml>,
70 for example <Pasaffe for Gnome|https://launchpad.net/pasaffe> or
71 <PwSafe for Mac|http://passwordsafe.sourceforge.net/relatedprojects.shtml>.
72  
73 =back
74  
75 Actual handling of Password Safe format is based on L<Crypt::PWSafe3>
76 module. Passwd::Keyring::PWSafe3 just wraps it into the interface
77 compatible with other Passwd::Keyring backends.
78  
79 See L<Passwd::Keyring::Auto::KeyringAPI> for detailed comments
80 on keyring methods (this document is installed with
81 C<Passwd::Keyring::Auto> package).
82  
83 =head1 CAVEATS
84  
85 Underlying module (L<Crypt::PWSafe3>) in fact rewrites the whole file
86 on every save and keeps all passwords cached in memory while active.
87 This means, that any attempts to use the file paralelly from a few
88 programs, or from a few objects within one program, are doomed to
89 cause lost updates. Also, all passwords from the file are kept in
90 (unprotected) memory while keyring object is active. Therefore, it is
91 recommended to use separate .psafe3 file for Passwd::Keyring::PWSafe3,
92 not mixing it with normal Password Safe database, and to keep keyring
93 object for a short time only, especially if modifications happen.
94  
95 There are some limitations in L<Crypt::PWSafe3> handling of Password
96 Safe format. Passwords are read and saved properly and it is possible
97 to alternate using them from perl, and via Password Safe GUI, but some
98 less important aspects of the format, like password expiraton policy,
99 may be ignored. Refer to L<Crypt::PWSafe3> docs for more details.
100  
101 =head1 DATA MAPPING
102  
103 Group name is mapped to Password Safe folder.
104  
105 Realm is mapped as password title.
106  
107 Username and password are ... well, used as username and password.
108  
109 =head1 SUBROUTINES/METHODS
110  
111 =head2 new(app=>'app name', group=>'passwords folder', file=>'pwsafe3 file', master_password=>'secret or callback', lazy_save=>1)
112  
113 Initializes the processing. Croaks if Crypt::PWSafe3 is not installed or
114 master password is invalid. May create password file if it is missing.
115  
116 Handled parameters:
117  
118 =over 4
119  
120 =item app
121  
122 Symbolic application name (used in password notes)
123  
124 =item group
125  
126 Name for the password group (used as folder name)
127  
128 =item file
129  
130 Location of .pwsafe3 file. If not given, C<passwd-keyring.pwsafe3> in
131 user home directory is used. Will be created if does not exist. Note:
132 absolute path is required, relative paths are very error prone.
133  
134 =item master_password
135  
136 Password required to unlock the file. Can be given as string, or
137 as callback returning a string (usually some way of interactively
138 asking user for the password). The callback gets two parameters: app
139 and file.
140  
141 If this param is missing, module will prompt interactively for this
142 password using console prompt.
143  
144 =item lazy_save
145  
146 if given, asks not to save the file after every change (saving is
147 fairly time consuming), but only when $keyring->save is called or when
148 keyring is destroyed.
149  
150 =back
151  
152 Note: it of course does not make much sense to keep app passwords in
153 encrypted storage if master password is saved in plain text. The
154 module most natural usage is to interactively ask for master password
155 (and use it to protect noticeable number of application-specific
156 passwords).
157  
158 Ideas of how to workaround this obstacle are welcome. I loosely
159 consider either caching master password per desktop session
160 (implementing sht. similar to ssh-agent/gpg-agent or using one of
161 those somehow), or integrating the tool with PAM to use actual system
162 password, or both - but while it seems doable on Linux, cross platform
163 solution is not so easy.
164  
165 =cut
166  
167 9 ▶sub new {
Subroutine new: called CC=8 SCAR=22.0
Pod documented
168 9 my ($cls, %args) = @_;
169  
170 9 my $self = {};
171 9 ▶ $self->{app} = $args{app} || 'Passwd::Keyring::PWSafe3';
Condition100%2/2
$args{'app'} || 'Passwd::Keyring::PWSafe3'
l!l
63
MC/DC100%1/1
$args{'app'} || 'Passwd::Keyring::PWSafe3'
$args{'app'}
Truth table100%2/2
A
A= $args{'app'}
Aresult
11
00
172 9 ▶ $self->{group} = $args{group} || 'Passwd::Keyring';
Condition100%2/2
$args{'group'} || 'Passwd::Keyring'
l!l
63
MC/DC100%1/1
$args{'group'} || 'Passwd::Keyring'
$args{'group'}
Truth table100%2/2
A
A= $args{'group'}
Aresult
11
00
173 9 $self->{lazy_save} = $args{lazy_save};
174 9 ▶ my $file = $args{file} || File::Spec->catfile(File::HomeDir->users_data, "passwd-keyring.pwsafe3");
Condition33%1/3
$args{'file'} || 'File::Spec'->catfile('File::HomeDir'->users_data, 'passwd-keyring.pwsafe3')
l!l&&r!l&&!r
900
MC/DC0%0/2
$args{'file'} || 'File::Spec'->catfile('File::HomeDir'->users_data, 'passwd-keyring.pwsafe3')
$args{'file'} 'File::Spec'->catfile('File::HomeDir'->users_data, 'passwd-keyring.pwsafe3')
Truth table33%1/3
A || B
A= $args{'file'}B= 'File::Spec'->catfile('File::HomeDir'->users_data, 'passwd-keyring.pwsafe3')
ABresult
1X1
011
000
175  
176 9 ▶ unless(File::Spec->file_name_is_absolute($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 0 9 9
177 0 croak("Absolute path to .pwsafe3 file is required, but relative path '$file' given");
178 }
179 9 my $parent_dir = dirname($file);
180 9 ▶ unless(-d $parent_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $parent_dir) 0 9 9
181 0 croak("Directory $parent_dir (parent directory of file $file) does not exist");
182 }
183  
184 # TODO: escape group (note that . are used for hierarchy!)
185 # TODO: some locking or maybe detect gui
186  
187 9 bless $self;
188  
189 9 ▶ my $master = $args{master_password} || \&_prompt_for_password;
Condition50%1/2
$args{'master_password'} || \&Passwd::Keyring::PWSafe3::_prompt_for_password
l!l
90
MC/DC0%0/1
$args{'master_password'} || \&Passwd::Keyring::PWSafe3::_prompt_for_password
$args{'master_password'}
Truth table50%1/2
A
A= $args{'master_password'}
Aresult
11
00
190 9 ▶ if(ref($master) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $master eq "CODE") 2 7 9
191 2 $master = $master->($self->{app}, $file);
192 }
193  
194 9 $self->{vault} = Crypt::PWSafe3->new(file=>$file, password=>$master);
195  
196 0 return $self;
197 }
198  
199 9 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
200 9 my $self = shift;
201 9 $self->save();
202 }
203  
204 0 ▶sub _prompt_for_password {
Subroutine _prompt_for_password: not called CC=3 SCAR=24.8
205 0 my ($app, $file) = @_;
206 0 print "* The applicaton $app is requesting to access\n";
207 0 print "* the Password Safe file $file\n";
208 0 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 0 0 0
209 0 print "* Enter master password necessary to unlock this file.\n";
210 } else {
211 0 print "* (the file does not exist and will be created on first password save)\n";
212 0 print "* Enter master password which will protect this file.\n";
213 }
214 0 while(1) {
215 0 print " Master password: ";
216 0 ReadMode 'noecho';
217 0 my $password = ReadLine 0; chomp($password);
218 0 ReadMode 'normal';
219 0 print "\n";
220 0 ▶ return $password if $password;
Branch: 1 branch
BranchTrueFalseTotal
if $password 0 0 0
221 }
222 }
223  
224 # Find data record for given params, or undef if not found
225 0 ▶sub _find_record {
Subroutine _find_record: not called CC=8 SCAR=42.8
226 0 my ($self, $username, $realm) = @_;
227 0 my $group = $self->{group};
228 0 foreach my $record($self->{vault}->getrecords()) {
229 0 ▶ if( ($record->group || '') eq $group
Branch: 1 branch
BranchTrueFalseTotal
if (($record->group || "") eq $group and ($record->user || "") eq $username and ($record->title || "") eq $realm) 0 0 0
Condition0%0/12
($record->group || "") eq $group and ($record->user || "") eq $username and ($record->title || "") eq $realm
!ll&&!rl&&r
000
($record->group || "") eq $group and ($record->user || "") eq $username
!ll&&!rl&&r
000
$record->group || ''
l!l
00
$record->user || ''
l!l
00
$record->title || ''
l!l
00
MC/DC0%0/3
($record->group || "") eq $group and ($record->user || "") eq $username and ($record->title || "") eq $realm
$record->group $record->user $record->title
Truth table0%0/4
A and B and C
A= $record->groupB= $record->userC= $record->title
ABCresult
0XX0
10X0
1100
1111
230 && ($record->user || '') eq $username
231 && ($record->title || '') eq $realm) {
232 0 return $record;
233 }
234 }
235 0 return undef;
236 }
237  
238 =head2 set_password(username, password, realm)
239  
240 Sets (stores) password identified by given realm for given user
241  
242 =cut
243  
244 0 ▶sub set_password {
Subroutine set_password: not called CC=3 SCAR=24.8
Pod documented
245 0 my ($self, $user_name, $user_password, $realm) = @_;
246  
247 0 my $rec = $self->_find_record($user_name, $realm);
248 0 ▶ if($rec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec) { } 0 0 0
249 $self->{vault}->modifyrecord(
250 0 $rec->uuid,
251 passwd => $user_password,
252 notes => "Saved by $self->{app}",
253 );
254 } else {
255 $self->{vault}->newrecord(
256 group => $self->{group},
257 0 title => $realm,
258 user => $user_name,
259 passwd => $user_password,
260 notes => "Saved by $self->{app}",
261 );
262 }
263 0 ▶ $self->save() unless $self->{lazy_save};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lazy_save"} 0 0 0
264 }
265  
266 =head2 get_password($user_name, $realm)
267  
268 Reads previously stored password for given user in given app.
269 If such password can not be found, returns undef.
270  
271 =cut
272  
273 0 ▶sub get_password {
Subroutine get_password: not called CC=2 SCAR=17.9
Pod documented
274 0 my ($self, $user_name, $realm) = @_;
275  
276 0 my $rec = $self->_find_record($user_name, $realm);
277 0 ▶ if($rec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec) 0 0 0
278 0 return $rec->passwd;
279 }
280 0 return undef;
281 }
282  
283 =head2 clear_password($user_name, $realm)
284  
285 Removes given password (if present)
286  
287 =cut
288  
289 0 ▶sub clear_password {
Subroutine clear_password: not called CC=3 SCAR=24.8
Pod documented
290 0 my ($self, $user_name, $realm) = @_;
291  
292 0 my $rec = $self->_find_record($user_name, $realm);
293 0 ▶ if($rec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec) { } 0 0 0
294 0 $self->{vault}->deleterecord($rec->uuid);
295 0 ▶ $self->save() unless $self->{lazy_save};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lazy_save"} 0 0 0
296 0 return 1;
297 } else {
298 0 return 0;
299 }
300 }
301  
302 =head2 save
303  
304 Saves unsaved changes, if any are present.
305  
306 Important only when lazy_save was given in constructor.
307  
308 =cut
309  
310 9 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
311 9 my ($self) = @_;
312 # Crypt::PWSafe3 internally keeps track of changes presence,
313 # and makes this noop if there are no changes. So just call it.
314 9 $self->{vault}->save();
315 }
316  
317 =head2 is_persistent
318  
319 Returns info, whether this keyring actually saves passwords persistently.
320  
321 (true in this case)
322  
323 =cut
324  
325 0 ▶sub is_persistent {
Subroutine is_persistent: not called CC=1 SCAR=6.9
Pod documented
326 0 my ($self) = @_;
327 0 return 1;
328 }
329  
330 =head1 AUTHOR
331  
332 Marcin Kasperski
333  
334 =head1 BUGS
335  
336 Please report any bugs or feature requests to
337 issue tracker at L<https://bitbucket.org/Mekk/perl-keyring-pwsafe3>.
338  
339 =head1 SUPPORT
340  
341 You can find documentation for this module with the perldoc command.
342  
343 perldoc Passwd::Keyring::PWSafe3
344  
345 You can also look for information at:
346  
347 L<http://search.cpan.org/~mekk/Passwd-Keyring-PWSafe3/>
348  
349 Source code is tracked at:
350  
351 L<https://bitbucket.org/Mekk/perl-keyring-pwsafe3>
352  
353 =head1 LICENSE AND COPYRIGHT
354  
355 Copyright 2012 Marcin Kasperski.
356  
357 This program is free software; you can redistribute it and/or modify it
358 under the terms of either: the GNU General Public License as published
359 by the Free Software Foundation; or the Artistic License.
360  
361 See http://dev.perl.org/licenses/ for more information.
362  
363 =cut
364  
365  
366 11 1; # End of Passwd::Keyring::PWSafe3
367  
368  
↑ Summary auto/share/dist/Passwd-Keyring-PWSafe3/imap_query_example.pl »