Optionaler Passwortcheck mit cracklib
[kivitendo-erp.git] / SL / Auth / PasswordPolicy.pm
1 package SL::Auth::PasswordPolicy;
2
3 use strict;
4
5 use parent qw(Rose::Object);
6
7 use constant OK                   =>   0;
8 use constant TOO_SHORT            =>   1;
9 use constant TOO_LONG             =>   2;
10 use constant MISSING_LOWERCASE    =>   4;
11 use constant MISSING_UPPERCASE    =>   8;
12 use constant MISSING_DIGIT        =>  16;
13 use constant MISSING_SPECIAL_CHAR =>  32;
14 use constant INVALID_CHAR         =>  64;
15 use constant WEAK                 => 128;
16
17 use Rose::Object::MakeMethods::Generic
18 (
19  'scalar --get_set_init' => 'config',
20 );
21
22 sub verify {
23   my ($self, $password, $is_admin) = @_;
24
25   my $cfg = $self->config;
26   return OK() unless $cfg && %{ $cfg };
27   return OK() if $is_admin && $cfg->{disable_policy_for_admin};
28
29   my $result = OK();
30   $result |= TOO_SHORT()            if $cfg->{min_length}                && (length($password) < $cfg->{min_length});
31   $result |= TOO_LONG()             if $cfg->{max_length}                && (length($password) > $cfg->{max_length});
32   $result |= MISSING_LOWERCASE()    if $cfg->{require_lowercase}         && $password !~ m/[a-z]/;
33   $result |= MISSING_UPPERCASE()    if $cfg->{require_uppercase}         && $password !~ m/[A-Z]/;
34   $result |= MISSING_DIGIT()        if $cfg->{require_digit}             && $password !~ m/[0-9]/;
35   $result |= MISSING_SPECIAL_CHAR() if $cfg->{require_special_character} && $password !~ $cfg->{special_characters_re};
36   $result |= INVALID_CHAR()         if $cfg->{invalid_characters_re}     && $password =~ $cfg->{invalid_characters_re};
37
38   if ($cfg->{use_cracklib}) {
39     require Crypt::Cracklib;
40     $result |= WEAK() if !Crypt::Cracklib::check($password);
41   }
42
43   return $result;
44 }
45
46 sub errors {
47   my ($self, $result) = @_;
48
49   my @errors;
50
51   push @errors, $::locale->text('The password is too short (minimum length: #1).', $self->config->{min_length}) if $result & TOO_SHORT();
52   push @errors, $::locale->text('The password is too long (maximum length: #1).',  $self->config->{max_length}) if $result & TOO_LONG();
53   push @errors, $::locale->text('A lower-case character is required.')                                          if $result & MISSING_LOWERCASE();
54   push @errors, $::locale->text('An upper-case character is required.')                                         if $result & MISSING_UPPERCASE();
55   push @errors, $::locale->text('A digit is required.')                                                         if $result & MISSING_DIGIT();
56   push @errors, $::locale->text('The password is weak (e.g. it can be found in a dictionary).')                 if $result & WEAK();
57
58   if ($result & MISSING_SPECIAL_CHAR()) {
59     my $char_list = join ' ', sort split(m//, $self->config->{special_characters});
60     push @errors, $::locale->text('A special character is required (valid characters: #1).', $char_list);
61   }
62
63   if (($result & INVALID_CHAR())) {
64     my $char_list = join ' ', sort split(m//, $self->config->{ $self->config->{invalid_characters} ? 'invalid_characters' : 'valid_characters' });
65     push @errors, $::locale->text('An invalid character was used (invalid characters: #1).', $char_list) if $self->config->{invalid_characters};
66     push @errors, $::locale->text('An invalid character was used (valid characters: #1).',   $char_list) if $self->config->{valid_characters};
67   }
68
69   return @errors;
70 }
71
72
73 sub init_config {
74   my ($self) = @_;
75
76   my %cfg = %{ $::emmvee_conf{password_policy} || {} };
77
78   $cfg{valid_characters}      =~ s/[ \n\r]//g if $cfg{valid_characters};
79   $cfg{invalid_characters}    =~ s/[ \n\r]//g if $cfg{invalid_characters};
80   $cfg{invalid_characters_re} =  '[^' . quotemeta($cfg{valid_characters})   . ']' if $cfg{valid_characters};
81   $cfg{invalid_characters_re} =  '['  . quotemeta($cfg{invalid_characters}) . ']' if $cfg{invalid_characters};
82   $cfg{special_characters}    =  '!@#$%^&*()_+=[]{}<>\'"|\\,;.:?-';
83   $cfg{special_characters_re} =  '[' . quotemeta($cfg{special_characters}) . ']';
84   print $cfg{special_characters_re}, "\n";
85
86   map { $cfg{"require_${_}"} = $cfg{"require_${_}"} =~ m/^(?:1|true|t|yes|y)$/i } qw(lowercase uppercase digit special_char);
87
88   $self->config(\%cfg);
89 }
90
91 1;
92 __END__
93
94 =pod
95
96 =encoding utf8
97
98 =head1 NAME
99
100 SL::Auth::PasswordPolicy - Verify a given password against the policy
101 set in the configuration file
102
103 =head1 SYNOPSIS
104
105  my $verifier = SL::Auth::PasswordPolicy->new;
106  my $result   = $verifier->verify($password);
107  if ($result != SL::Auth::PasswordPolicy->OK()) {
108    print "Errors: " . join(' ', $verifier->errors($result)) . "\n";
109  }
110
111 =head1 CONSTANTS
112
113 =over 4
114
115 =item C<OK>
116
117 Password is OK.
118
119 =item C<TOO_SHORT>
120
121 The password is too short.
122
123 =item C<TOO_LONG>
124
125 The password is too long.
126
127 =item C<MISSING_LOWERCASE>
128
129 The password is missing a lower-case character.
130
131 =item C<MISSING_UPPERCASE>
132
133 The password is missing an upper-case character.
134
135 =item C<MISSING_DIGIT>
136
137 The password is missing a digit.
138
139 =item C<MISSING_SPECIAL_CHAR>
140
141 The password is missing a special character. Special characters are
142 the following: ! " # $ % & ' ( ) * + , - . : ; E<lt> = E<gt> ? @ [ \ ]
143 ^ _ { | }
144
145 =item C<INVALID_CHAR>
146
147 The password contains an invalid character.
148
149 =back
150
151 =head1 FUNCTIONS
152
153 =over 4
154
155 =item C<verify $password, $is_admin>
156
157 Checks whether or not the password matches the policy. Returns C<OK()>
158 if it does and an error code otherwise (binary or'ed of the error
159 constants).
160
161 If C<$is_admin> is trueish and the configuration specifies that the
162 policy checks are disabled for the administrator then C<verify> will
163 always return C<OK()>.
164
165 =item C<errors $code>
166
167 Returns an array of human-readable strings describing the issues set
168 in C<$code> which should be the result of L</verify>.
169
170 =back
171
172 =head1 BUGS
173
174 Nothing here yet.
175
176 =head1 AUTHOR
177
178 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
179
180 =cut