option to disable password checks for an agent, RT#40236
[freeside.git] / FS / FS / Password_Mixin.pm
1 package FS::Password_Mixin;
2
3 use FS::Record qw(qsearch);
4 use FS::Conf;
5 use FS::password_history;
6 use Authen::Passphrase;
7 use Authen::Passphrase::BlowfishCrypt;
8 # https://rt.cpan.org/Ticket/Display.html?id=72743
9 use Data::Password qw(:all);
10
11 our $DEBUG = 0;
12 our $conf;
13 FS::UID->install_callback( sub {
14     $conf = FS::Conf->new;
15     # this is safe
16     #eval "use Authen::Passphrase::BlowfishCrypt;";
17 });
18
19 our $me = '[' . __PACKAGE__ . ']';
20
21 our $BLOWFISH_COST = 10;
22
23 =head1 NAME
24
25 FS::Password_Mixin - Object methods for accounts that have passwords governed
26 by the password policy.
27
28 =head1 METHODS
29
30 =over 4
31
32 =item is_password_allowed PASSWORD
33
34 Checks the password against the system password policy. Returns an error
35 message on failure, an empty string on success.
36
37 This MUST NOT be called from check(). It should be called by the office UI,
38 self-service ClientAPI, or other I<user-interactive> code that processes a
39 password change, and only if the user has taken some action with the intent
40 of setting the password.
41
42 =cut
43
44 sub is_password_allowed {
45   my $self = shift;
46   my $password = shift;
47
48   my $cust_main = $self->cust_main;
49   return '' if $cust_main && $conf->config_bool('password-insecure', $cust_main->agentnum);
50
51   # basic checks using Data::Password;
52   # options for Data::Password
53   $DICTIONARY = 4;   # minimum length of disallowed words
54   $MINLEN = $conf->config('passwordmin') || 6;
55   $MAXLEN = $conf->config('passwordmax') || 8;
56   $GROUPS = 4;       # must have all 4 'character groups': numbers, symbols, uppercase, lowercase
57   # other options use the defaults listed below:
58   # $FOLLOWING = 3;    # disallows more than 3 chars in a row, by alphabet or keyboard (ie abcd or asdf)
59   # $SKIPCHAR = undef; # set to true to skip checking for bad characters
60   # # lists of disallowed words
61   # @DICTIONARIES = qw( /usr/share/dict/web2 /usr/share/dict/words /usr/share/dict/linux.words );
62
63   my $error = IsBadPassword($password);
64   $error = 'must contain at least one each of numbers, symbols, and lowercase and uppercase letters'
65     if $error eq 'contains less than 4 character groups'; # avoid confusion
66   $error = 'Invalid password - ' . $error if $error;
67   return $error if $error;
68
69   #check against service fields
70   $error = $self->password_svc_check($password);
71   return $error if $error;
72
73   return '' unless $self->get($self->primary_key); # for validating new passwords pre-insert
74
75   #check against customer fields
76   if ($cust_main) {
77     my @words;
78     # words from cust_main
79     foreach my $field ( qw( last first daytime night fax mobile ) ) {
80         push @words, split(/\W/,$cust_main->get($field));
81     }
82     # words from cust_location
83     foreach my $loc ($cust_main->cust_location) {
84       foreach my $field ( qw(address1 address2 city county state zip) ) {
85         push @words, split(/\W/,$loc->get($field));
86       }
87     }
88     # do the actual checking
89     foreach my $word (@words) {
90       next unless length($word) > 2;
91       if ($password =~ /$word/i) {
92         return qq(Password contains account information '$word');
93       }
94     }
95   }
96
97   if ( $conf->config('password-no_reuse') =~ /^(\d+)$/ ) {
98
99     my $no_reuse = $1;
100
101     # "the last N" passwords includes the current password and the N-1
102     # passwords before that.
103     warn "$me checking password reuse limit of $no_reuse\n" if $DEBUG;
104     my @latest = qsearch({
105         'table'     => 'password_history',
106         'hashref'   => { $self->password_history_key => $self->get($self->primary_key) },
107         'order_by'  => " ORDER BY created DESC LIMIT $no_reuse",
108     });
109
110     # don't check the first one; reusing the current password is allowed.
111     shift @latest;
112
113     foreach my $history (@latest) {
114       warn "$me previous password created ".$history->created."\n" if $DEBUG;
115       if ( $history->password_equals($password) ) {
116         my $message;
117         if ( $no_reuse == 1 ) {
118           $message = "This password is the same as your previous password.";
119         } else {
120           $message = "This password was one of the last $no_reuse passwords on this account.";
121         }
122         return $message;
123       }
124     } #foreach $history
125
126   } # end of no_reuse checking
127
128   '';
129 }
130
131 =item password_svc_check
132
133 Override to run additional service-specific password checks.
134
135 =cut
136
137 sub password_svc_check {
138   my ($self, $password) = @_;
139   return '';
140 }
141
142 =item password_history_key
143
144 Returns the name of the field in L<FS::password_history> that's the foreign
145 key to this table.
146
147 =cut
148
149 sub password_history_key {
150   my $self = shift;
151   $self->table . '__' . $self->primary_key;
152 }
153
154 =item insert_password_history
155
156 Creates a L<FS::password_history> record linked to this object, with its
157 current password.
158
159 =cut
160
161 sub insert_password_history {
162   my $self = shift;
163   my $encoding = $self->_password_encoding;
164   my $password = $self->_password;
165   my $auth;
166
167   if ( $encoding eq 'bcrypt' ) {
168     # our format, used for contact and access_user passwords
169     my ($cost, $salt, $hash) = split(',', $password);
170     $auth = Authen::Passphrase::BlowfishCrypt->new(
171       cost        => $cost,
172       salt_base64 => $salt,
173       hash_base64 => $hash,
174     );
175
176   } elsif ( $encoding eq 'crypt' ) {
177
178     # it's smart enough to figure this out
179     $auth = Authen::Passphrase->from_crypt($password);
180
181   } elsif ( $encoding eq 'ldap' ) {
182
183     $password =~ s/^{PLAIN}/{CLEARTEXT}/i; # normalize
184     $auth = Authen::Passphrase->from_rfc2307($password);
185     if ( $auth->isa('Authen::Passphrase::Clear') ) {
186       # then we've been given the password in cleartext
187       $auth = $self->_blowfishcrypt( $auth->passphrase );
188     }
189   
190   } else {
191     warn "unrecognized password encoding '$encoding'; treating as plain text"
192       unless $encoding eq 'plain';
193
194     $auth = $self->_blowfishcrypt( $password );
195
196   }
197
198   my $password_history = FS::password_history->new({
199       _password => $auth->as_rfc2307,
200       created   => time,
201       $self->password_history_key => $self->get($self->primary_key),
202   });
203
204   my $error = $password_history->insert;
205   return "recording password history: $error" if $error;
206   '';
207
208 }
209
210 =item _blowfishcrypt PASSWORD
211
212 For internal use: takes PASSWORD and returns a new
213 L<Authen::Passphrase::BlowfishCrypt> object representing it.
214
215 =cut
216
217 sub _blowfishcrypt {
218   my $class = shift;
219   my $passphrase = shift;
220   return Authen::Passphrase::BlowfishCrypt->new(
221     cost => $BLOWFISH_COST,
222     salt_random => 1,
223     passphrase => $passphrase,
224   );
225 }
226
227 =back
228
229 =head1 SEE ALSO
230
231 L<FS::password_history>
232
233 =cut
234
235 1;