blib/lib/Lib/PWQuality.pm

filter: Statementstmt 51.7% 30 / 58 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.8% 10 / 17 Podpod n/a totaltotal 37.0% 40 / 108 CC 15 SCAR 31.1
CC
15
Cov
35%
new
72.0
set_value
12.0
get_value
12.0
CRAP 22.5
↑ Summary
1 package Lib::PWQuality;
2 our $AUTHORITY = 'cpan:XSAWYERX';
3 # ABSTRACT: Perl interface to the libpwquality C library
4 $Lib::PWQuality::VERSION = '0.002';
5 ## no critic
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Ref::Util qw< is_ref is_hashref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use FFI::CheckLib 0.06 qw< find_lib_or_die >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use FFI::Platypus;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use FFI::C;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 'SETTINGS_INT' => {
18 'DIFF_OK' => undef,
19 'MIN_LENGTH' => undef,
20 'DIG_CREDIT' => undef,
21 'UP_CREDIT' => undef,
22 'LOW_CREDIT' => undef,
23 'OTH_CREDIT' => undef,
24 'MIN_CLASS' => undef,
25 'MAX_REPEAT' => undef,
26 'MAX_CLASS_REPEAT' => undef,
27 'MAX_SEQUENCE' => undef,
28 'GECOS_CHECK' => undef,
29 'DICT_CHECK' => undef,
30 'USER_CHECK' => undef,
31 'USER_SUBSTR' => undef,
32 'ENFORCING' => undef,
33 'RETRY_TIMES' => undef,
34 'ENFORCE_ROOT' => undef,
35 'LOCAL_USERS' => undef,
36 },
37  
38 'SETTINGS_STR' => {
39 'BAD_WORDS' => undef,
40 'DICT_PATH' => undef,
41 },
42  
43 'SETTINGS_ALL' => {
44 'difok' => undef,
45 'minlen' => undef,
46 'dcredit' => undef,
47 'ucredit' => undef,
48 'lcredit' => undef,
49 'ocredit' => undef,
50 'minclass' => undef,
51 'maxrepeat' => undef,
52 'maxclassrepeat' => undef,
53 'maxsequence' => undef,
54 'gecoscheck' => undef,
55 'dictcheck' => undef,
56 'usercheck' => undef,
57 'usersubstr' => undef,
58 'enforcing' => undef,
59 'badwords' => undef,
60 'dictpath' => undef,
61 'retry' => undef,
62 'enforce_for_root' => undef,
63 'local_users_only' => undef,
64 },
65 4 };
66  
67 my $ffi = FFI::Platypus->new( 'api' => 1 );
68 FFI::C->ffi($ffi);
69  
70 $ffi->lib( find_lib_or_die( 'lib' => 'pwquality' ) );
71  
72 package Lib::PWQuality::Setting {
73 our $AUTHORITY = 'cpan:XSAWYERX';
74  
75 $Lib::PWQuality::Setting::VERSION = '0.002';
76 FFI::C->enum( 'pwquality_setting' => [
77 [ 'DIFF_OK' => 1 ],
78 [ 'MIN_LENGTH' => 3 ],
79 [ 'DIG_CREDIT' => 4 ],
80 [ 'UP_CREDIT' => 5 ],
81 [ 'LOW_CREDIT' => 6 ],
82 [ 'OTH_CREDIT' => 7 ],
83 [ 'MIN_CLASS' => 8 ],
84 [ 'MAX_REPEAT' => 9 ],
85 [ 'DICT_PATH' => 10 ],
86 [ 'MAX_CLASS_REPEAT' => 11 ],
87 [ 'GECOS_CHECK' => 12 ],
88 [ 'BAD_WORDS' => 13 ],
89 [ 'MAX_SEQUENCE' => 14 ],
90 [ 'DICT_CHECK' => 15 ],
91 [ 'USER_CHECK' => 16 ],
92 [ 'ENFORCING' => 17 ],
93 [ 'RETRY_TIMES' => 18 ],
94 [ 'ENFORCE_ROOT' => 19 ],
95 [ 'LOCAL_USERS' => 20 ],
96 [ 'USER_SUBSTR' => 21 ],
97 ]);
98 }
99  
100 package Lib::PWQaulity::Return {
101 our $AUTHORITY = 'cpan:XSAWYERX';
102  
103 $Lib::PWQaulity::Return::VERSION = '0.002';
104 FFI::C->enum( 'pwquality_return' => [
105 [ 'SUCCESS' => 0 ],
106 [ 'FATAL_FAILURE' => -1 ],
107 [ 'INTEGER' => -2 ],
108 [ 'CFGFILE_OPEN' => -3 ],
109 [ 'CFGFILE_MALFORMED' => -4 ],
110 [ 'UNKNOWN_SETTING' => -5 ],
111 [ 'NON_INT_SETTING' => -6 ],
112 [ 'NON_STR_SETTING' => -7 ],
113 [ 'MEM_ALLOC' => -8 ],
114 [ 'TOO_SIMILAR' => -9 ],
115 [ 'MIN_DIGITS' => -10 ],
116 [ 'MIN_UPPERS' => -11 ],
117 [ 'MIN_LOWERS' => -12 ],
118 [ 'MIN_OTHERS' => -13 ],
119 [ 'MIN_LENGTH' => -14 ],
120 [ 'PALINDROME' => -15 ],
121 [ 'CASE_CHANGES_ONLY' => -16 ],
122 [ 'ROTATED' => -17 ],
123 [ 'MIN_CLASSES' => -18 ],
124 [ 'MAX_CONSECUTIVE' => -19 ],
125 [ 'EMPTY_PASSWORD' => -20 ],
126 [ 'SAME_PASSWORD' => -21 ],
127 [ 'CRACKLIB_CHECK' => -22 ],
128 [ 'RNG' => -23 ],
129 [ 'GENERATION_FAILED' => -24 ],
130 [ 'USER_CHECK' => -25 ],
131 [ 'GECOS_CHECK' => -26 ],
132 [ 'MAX_CLASS_REPEAT' => -27 ],
133 [ 'BAD_WORDS' => -28 ],
134 [ 'MAX_SEQUENCE' => -29 ],
135 ]);
136 }
137  
138 package Lib::PWQuality::Settings {
139 our $AUTHORITY = 'cpan:XSAWYERX';
140  
141 $Lib::PWQuality::Settings::VERSION = '0.002';
142 4 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 FFI::C->struct( 'pwquality_settings_t' => [
145 'diff_ok' => 'int',
146 'min_length' => 'int',
147 'dig_credit' => 'int',
148 'up_credit' => 'int',
149 'low_credit' => 'int',
150 'oth_credit' => 'int',
151 'min_class' => 'int',
152 'max_repeat' => 'int',
153 'max_class_repeat' => 'int',
154 'max_sequence' => 'int',
155 'gecos_check' => 'int',
156 'dict_check' => 'int',
157 'user_check' => 'int',
158 'user_substr' => 'int',
159 'enforcing' => 'int',
160 'retry_times' => 'int',
161 'enforce_for_root' => 'int',
162 'local_users_only' => 'int',
163  
164 '_bad_words' => 'opaque',
165 '_dict_path' => 'opaque',
166 ]);
167  
168 0 ▶ sub bad_words ($self) {
Subroutine bad_words: not called CC=2 SCAR=17.9
169 0 ▶ return $self->{'bad_words'}
Condition0%0/3
$self->{"bad_words"} //= $ffi->cast("opaque", "string", $self->_bad_words)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"bad_words"} //= $ffi->cast("opaque", "string", $self->_bad_words)
$self->{"bad_words"} $ffi->cast("opaque", "string", $self->_bad_words)
Truth table0%0/3
A //= B
A= $self->{"bad_words"}B= $ffi->cast("opaque", "string", $self->_bad_words)
ABresult
1X1
011
000
170 //= $ffi->cast( 'opaque', 'string', $self->_bad_words() );
171 }
172  
173 0 ▶ sub dict_path ($self) {
Subroutine dict_path: not called CC=2 SCAR=17.9
174 0 ▶ return $self->{'dict_path'}
Condition0%0/3
$self->{"dict_path"} //= $ffi->cast("opaque", "string", $self->_dict_path)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"dict_path"} //= $ffi->cast("opaque", "string", $self->_dict_path)
$self->{"dict_path"} $ffi->cast("opaque", "string", $self->_dict_path)
Truth table0%0/3
A //= B
A= $self->{"dict_path"}B= $ffi->cast("opaque", "string", $self->_dict_path)
ABresult
1X1
011
000
175 //= $ffi->cast( 'opaque', 'string', $self->_dict_path() );
176 }
177 }
178  
179 $ffi->mangler( sub ($symbol) {
180 return "pwquality_$symbol";
181 });
182  
183 $ffi->attach(
184 [ 'default_settings' => '_default_settings' ], [], 'pwquality_settings_t'
185 );
186  
187 $ffi->attach(
188 [ 'free_settings' => '_free_settings' ],
189 ['pwquality_settings_t'],
190 'void',
191 );
192  
193 $ffi->attach(
194 'read_config' => [ 'pwquality_settings_t', 'string', 'opaque*' ] => 'pwquality_return',
195 sub ( $xsub, $self, $filename ) {
196 return $xsub->( $self->settings(), $filename, undef );
197 },
198 );
199  
200 $ffi->attach(
201 'set_option' => [ 'pwquality_settings_t', 'string' ] => 'pwquality_return',
202 sub ( $xsub, $self, $pair ) {
203 my ($name) = split /=/xms, $pair;
204 exists SETTINGS_ALL()->{$name}
205 or Carp::croak("Unrecognized option: '$name'");
206  
207 return $xsub->( $self->settings(), $pair );
208 },
209 );
210  
211 $ffi->attach(
212 'set_int_value',
213 [ 'pwquality_settings_t', 'pwquality_setting', 'int' ],
214 'pwquality_return',
215 sub ( $xsub, $self, $key, $value ) {
216 exists SETTINGS_INT()->{$key}
217 or Carp::croak("Unrecognized value: '$key'");
218  
219 return $xsub->( $self->settings(), $key, $value );
220 },
221 );
222  
223 $ffi->attach(
224 'set_str_value',
225 [ 'pwquality_settings_t', 'pwquality_setting', 'string' ],
226 'pwquality_return',
227 sub ( $xsub, $self, $key, $value ) {
228 exists SETTINGS_STR()->{$key}
229 or Carp::croak("Unrecognized value: '$key'");
230  
231 return $xsub->( $self->settings(), $key, $value );
232 },
233 );
234  
235 $ffi->attach(
236 'get_int_value',
237 [ 'pwquality_settings_t', 'pwquality_setting', 'int*' ],
238 'pwquality_return',
239 sub ( $xsub, $self, $key ) {
240 exists SETTINGS_INT()->{$key}
241 or Carp::croak("Unrecognized value: '$key'");
242  
243 my $value;
244 $xsub->( $self->settings(), $key, \$value );
245 return $value;
246 },
247 );
248  
249 $ffi->attach(
250 'get_str_value',
251 [ 'pwquality_settings_t', 'pwquality_setting', 'string*' ],
252 'pwquality_return',
253 sub ( $xsub, $self, $key ) {
254 exists SETTINGS_STR()->{$key}
255 or Carp::croak("Unrecognized value: '$key'");
256  
257 my $value;
258 $xsub->( $self->settings(), $key, \$value );
259 return $value;
260 },
261 );
262  
263 $ffi->attach(
264 'generate' => [ 'pwquality_settings_t', 'int', 'string*' ] => 'pwquality_return',
265 sub ( $xsub, $self, $entropy_bits ) {
266 my $password;
267 $xsub->( $self->settings(), $entropy_bits, \$password );
268 return $password;
269 },
270 );
271  
272 $ffi->attach(
273 'check',
274 # settings, passwod, oldpassword, user, auxerror
275 # oldpassword, user, and auxerror can all be NULL
276 [ 'pwquality_settings_t', 'string', 'string', 'string', 'opaque*' ],
277 'int',
278 sub ( $xsub, $self, @args ) {
279 my $auxerror;
280 if ( @args > 3 ) {
281 Carp::croak('Too many arguments to check()');
282 } elsif ( @args == 3 ) {
283 push @args, \$auxerror;
284 }
285  
286 my $result = $xsub->( $self->settings(),@args );
287  
288 if ( $result <= 0 ) {
289 return {
290 'status' => $ffi->cast( 'int', 'pwquality_return', $result ),
291 'score' => -1,
292 };
293 }
294  
295 return {
296 'status' => $ffi->cast( 'int', 'pwquality_return', 0 ),
297 'score' => $result,
298 };
299 },
300 );
301  
302 $ffi->attach(
303 [ 'strerror' => '_strerror' ],
304 [ 'string', 'size_t', 'int', 'opaque' ],
305 'string',
306 );
307  
308 0 ▶sub new ( $class, $opts = {} ) {
Subroutine new: not called CC=8 SCAR=42.8
309 0 my $settings = _default_settings();
310 0 my $self = bless { 'settings' => $settings }, $class;
311  
312 # in case $opts is a configuration filename
313 0 ▶ if ( !is_ref($opts) && length $opts ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not is_ref($opts) and length $opts) { } 0 0 0
elsif (not is_hashref($opts)) { } 0 0 0
Condition0%0/3
not is_ref($opts) and length $opts
!ll&&!rl&&r
000
MC/DC0%0/2
not is_ref($opts) and length $opts
not is_ref($opts) length $opts
Truth table0%0/3
not(A) and B
A= not is_ref($opts)B= length $opts
ABresult
0X0
100
111
314 0 $self->read_config($opts);
315 0 $opts = {};
316 } elsif ( !is_hashref($opts) ) {
317 0 Carp::croak("Incorrect argument to new(): $opts");
318 }
319  
320 0 foreach my $opt_name ( keys $opts->%* ) {
321 0 ▶ if ( exists SETTINGS_INT()->{$opt_name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists {DICT_CHECK => undef, DIFF_OK => undef, DIG_CREDIT => undef, ENFORCE_ROOT => undef, ENFORCING => undef, GECOS_CHECK => undef, LOCAL_USERS => undef, LOW_CREDIT => undef, MAX_CLASS_REPEAT => undef, MAX_REPEAT => undef, MAX_SEQUENCE => undef, MIN_CLASS => undef, MIN_LENGTH => undef, OTH_CREDIT => undef, RETRY_TIMES => undef, UP_CREDIT => undef, USER_CHECK => undef, USER_SUBSTR => undef}->{$opt_name}) { } 0 0 0
elsif (exists {BAD_WORDS => undef, DICT_PATH => undef}->{$opt_name}) { } 0 0 0
322 0 $self->set_int_value( $opt_name, $opts->{$opt_name} );
323 } elsif ( exists SETTINGS_STR()->{$opt_name} ) {
324 0 $self->set_str_value( $opt_name, $opts->{$opt_name} );
325 } else {
326 0 Carp::croak("Option not recognized: '$opt_name'");
327 }
328 }
329  
330 0 return $self;
331 }
332  
333 0 ▶sub set_value ( $self, $key, $value ) {
Subroutine set_value: not called CC=3 SCAR=24.8
334 0 ▶ if ( exists SETTINGS_INT()->{$key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists {DICT_CHECK => undef, DIFF_OK => undef, DIG_CREDIT => undef, ENFORCE_ROOT => undef, ENFORCING => undef, GECOS_CHECK => undef, LOCAL_USERS => undef, LOW_CREDIT => undef, MAX_CLASS_REPEAT => undef, MAX_REPEAT => undef, MAX_SEQUENCE => undef, MIN_CLASS => undef, MIN_LENGTH => undef, OTH_CREDIT => undef, RETRY_TIMES => undef, UP_CREDIT => undef, USER_CHECK => undef, USER_SUBSTR => undef}->{$key}) { } 0 0 0
elsif (exists {BAD_WORDS => undef, DICT_PATH => undef}->{$key}) { } 0 0 0
335 0 $self->set_int_value( $key, $value );
336 } elsif ( exists SETTINGS_STR()->{$key} ) {
337 0 $self->set_str_value( $key, $value );
338 } else {
339 0 Carp::croak("Option not recognized: '$key'");
340 }
341 }
342  
343 0 ▶sub get_value ( $self, $key ) {
Subroutine get_value: not called CC=3 SCAR=24.8
344 0 ▶ if ( exists SETTINGS_INT()->{$key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists {DICT_CHECK => undef, DIFF_OK => undef, DIG_CREDIT => undef, ENFORCE_ROOT => undef, ENFORCING => undef, GECOS_CHECK => undef, LOCAL_USERS => undef, LOW_CREDIT => undef, MAX_CLASS_REPEAT => undef, MAX_REPEAT => undef, MAX_SEQUENCE => undef, MIN_CLASS => undef, MIN_LENGTH => undef, OTH_CREDIT => undef, RETRY_TIMES => undef, UP_CREDIT => undef, USER_CHECK => undef, USER_SUBSTR => undef}->{$key}) { } 0 0 0
elsif (exists {BAD_WORDS => undef, DICT_PATH => undef}->{$key}) { } 0 0 0
345 0 $self->get_int_value($key);
346 } elsif ( exists SETTINGS_STR()->{$key} ) {
347 0 $self->get_str_value($key);
348 } else {
349 0 Carp::croak("Option not recognized: '$key'");
350 }
351 }
352  
353 0 ▶sub settings ($self) {
Subroutine settings: not called CC=1 SCAR=6.9
354 0 return $self->{'settings'};
355 }
356  
357 0 ▶sub DESTROY ($self) {
Subroutine DESTROY: not called CC=2 SCAR=17.9
358 0 ▶ my $settings = $self->{'settings'}
Branch: 1 branch
BranchTrueFalseTotal
my $settings = $self->{"settings"} or die "Cannot clear instance without settings" 0 0 0
359 or die "Cannot clear instance without settings";
360  
361 # FIXME: This fails in tests - not sure when it should be cleaned up
362 0 eval { _free_settings($settings); 1; };
363 }
364  
365 1;
366  
367 __END__
↑ Summary