1 .\" $NetBSD: pw_policy.3,v 1.8 2006/03/23 19:31:37 wiz Exp $
3 .\" Copyright (c) 2005, 2006 Elad Efrat <elad@NetBSD.org>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. The name of the author may not be used to endorse or promote products
15 .\" derived from this software without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 .Nd password policy enforcement
40 .Fn pw_policy_load "void *key" "int how"
42 .Fn pw_policy_test "pw_policy_t policy" "char *pw"
44 .Fn pw_policy_free "pw_policy_t policy"
51 functions are used as an interface to the system's password policy
53 .Pa /etc/passwd.conf .
56 will load a password policy and return a pointer to a
59 It is the caller's responsibility to free this pointer using
68 to be used when searching
70 for a password policy.
71 This key contains various options describing different policies.
72 Some built-in ones are described along with their syntax below.
74 To allow calling from various program contexts
75 and using various password policy retrieval schemes,
86 .Bl -tag -width kungfuninja -compact
87 .It Li PW_POLICY_BYSTRING
91 looking up the string it contains in
92 .Pa /etc/passwd.conf .
94 .It Li PW_POLICY_BYPASSWD
98 first looking up the username in
100 and if no key can be found, it will try the login class in
103 .It Li PW_POLICY_BYGROUP
107 looking up the group name in
115 or no specified key can be found, the default key,
118 If even the default key can't be found,
119 the password is accepted as no policy is defined.
122 can be used to check if the password in
124 is compliant with the policy in
126 .Sh BUILT-IN POLICY SYNTAX
127 Available built-in policy options include the following:
129 .Bl -tag -width kungfuninja -compact
131 Length of the password.
133 Number of upper-case characters in the password.
135 Number of lower-case characters in the password.
137 Number of digits in the password.
139 Number of punctuation characters in the password.
141 Number of different character classes in the password.
143 How often a user has to toggle between character classes in the password.
146 Options are used inside keys.
147 An option uses a format of
149 For the built-in options, we use either
157 format, specifies a single length.
158 For example, the following option specifies that the password should
159 have exactly 3 upper-case characters:
160 .Bd -literal -offset indent
166 format, can be used to specify a range.
167 Forcing a policy for number of digits between 1 and 4 would be:
168 .Bd -literal -offset indent
176 can also be used to indicate
181 To illustrate, the following example states that the number of
182 punctuation characters should be at least two:
183 .Bd -literal -offset indent
187 No more than 7 digits:
188 .Bd -literal -offset indent
192 Any number of lower-case characters:
193 .Bd -literal -offset indent
197 Upper-case characters not allowed:
198 .Bd -literal -offset indent
202 To specify that the password must be at least 8 characters long:
203 .Bd -literal -offset indent
207 Specifying a password must have at least 3 different character classes:
208 .Bd -literal -offset indent
212 And that the user must change character class every 2 characters:
213 .Bd -literal -offset indent
217 Note that when using the
219 directive, the policy will be initialized to allow any number of characters
221 If desired, this should be overridden after the
230 on failure, in which case the
232 variable will be set to any of the following values indicating the
233 reason for the failure:
240 Invalid value for the
242 parameter or an invalid value in the password policy specification.
248 to a value returned by the called handlers and/or
252 returns 0 if the password follows the policy, or \-1 if it doesn't,
254 can be set to any of the following values:
257 The password does not follow the password policy.
260 pointer was passed as the password.
265 can be set to any error code returned by the handlers.
267 .Bl -tag -width /etc/passwd.conf -compact
268 .It Pa /etc/passwd.conf
269 password configuration file.
272 Declare a password policy storage:
273 .Bd -literal -offset indent
277 Load the system global password policy into
279 .Bd -literal -offset indent
280 policy = pw_policy_load(NULL, 0);
282 errx(1, "Can't load password policy");
285 Load a policy for a user whose password database entry is in
289 .Bd -literal -offset indent
290 policy = pw_policy_load(pw_entry, PW_POLICY_BYPASSWD);
292 errx(1, "Can't load password policy for \e"%s\e"", pw_entry-\*[Gt]pw_name);
297 will first look for a password policy for the username in
298 .Ar pw_entry-\*[Gt]pw_name ,
299 if not found, it will try looking for a policy for the login class in
300 .Ar pw_entry-\*[Gt]pw_class ,
301 and if it can't find such either it will fallback to the default key,
304 Load the password policy for a group
305 whose group database entry is in
309 .Bd -literal -offset indent
310 policy = pw_policy_load(grent, PW_POLICY_BYGROUP);
312 errx(1, "Can't load password policy for \e"%s\e"", grent-\*[Gt]gr_name);
317 follows the policy in
319 .Bd -literal -offset indent
320 if (pw_policy_test(policy, the_password) != 0)
321 warnx("Please refer to the password policy");
324 After finished using the password policy, free it:
325 .Bd -literal -offset indent
326 pw_policy_free(policy);
329 An example for a common default password policy in
330 .Pa /etc/passwd.conf :
331 .Bd -literal -offset indent
333 length = 8-* # At least 8 characters long,
334 lowercase = 1-* # combining lowercase,
335 uppercase = 1-* # uppercase,
336 digits = 1-* # and digits.
337 punctuation = * # Punctuation is optional.
340 A different policy that might be used:
341 .Bd -literal -offset indent
342 nclasses = 3-* # At least 3 different character classes,
343 ntoggles = *-2 # not more than 2 same class in a row.
354 functions first appeared in