1 /* $NetBSD: password_quality.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $ */
4 * Copyright (c) 1997-2000, 2003-2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "kadm5_locl.h"
37 #include "kadm5-pwcheck.h"
39 #ifdef HAVE_SYS_WAIT_H
47 min_length_passwd_quality (krb5_context context
,
48 krb5_principal principal
,
54 uint32_t min_length
= krb5_config_get_int_default(context
, NULL
, 6,
59 if (pwd
->length
< min_length
) {
60 strlcpy(message
, "Password too short", length
);
67 min_length_passwd_quality_v0 (krb5_context context
,
68 krb5_principal principal
,
71 static char message
[1024];
76 ret
= min_length_passwd_quality(context
, principal
, pwd
, NULL
,
77 message
, sizeof(message
));
85 char_class_passwd_quality (krb5_context context
,
86 krb5_principal principal
,
92 const char *classes
[] = {
93 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
94 "abcdefghijklmnopqrstuvwxyz",
96 " !\"#$%&'()*+,-./:;<=>?@\\]^_`{|}~"
98 int counter
= 0, req_classes
;
102 req_classes
= krb5_config_get_int_default(context
, NULL
, 3,
107 len
= pwd
->length
+ 1;
110 strlcpy(message
, "out of memory", length
);
113 strlcpy(pw
, pwd
->data
, len
);
116 for (i
= 0; i
< sizeof(classes
)/sizeof(classes
[0]); i
++) {
117 if (strcspn(pw
, classes
[i
]) < len
)
120 memset(pw
, 0, pwd
->length
+ 1);
122 if (counter
< req_classes
) {
123 snprintf(message
, length
,
124 "Password doesn't meet complexity requirement.\n"
125 "Add more characters from at least %d of the\n"
126 "following classes:\n"
127 "1. English uppercase characters (A through Z)\n"
128 "2. English lowercase characters (a through z)\n"
129 "3. Base 10 digits (0 through 9)\n"
130 "4. Nonalphanumeric characters (e.g., !, $, #, %%)", req_classes
);
137 external_passwd_quality (krb5_context context
,
138 krb5_principal principal
,
150 FILE *in
= NULL
, *out
= NULL
, *error
= NULL
;
152 if (memchr(pwd
->data
, '\n', pwd
->length
) != NULL
) {
153 snprintf(message
, length
, "password contains newline, "
154 "not valid for external test");
158 program
= krb5_config_get_string(context
, NULL
,
162 if (program
== NULL
) {
163 snprintf(message
, length
, "external password quality "
164 "program not configured");
168 ret
= krb5_unparse_name(context
, principal
, &p
);
170 strlcpy(message
, "out of memory", length
);
174 child
= pipe_execv(&in
, &out
, &error
, program
, program
, p
, NULL
);
176 snprintf(message
, length
, "external password quality "
177 "program failed to execute for principal %s", p
);
182 fprintf(in
, "principal: %s\n"
183 "new-password: %.*s\n"
185 p
, (int)pwd
->length
, (char *)pwd
->data
);
189 if (fgets(reply
, sizeof(reply
), out
) == NULL
) {
191 if (fgets(reply
, sizeof(reply
), error
) == NULL
) {
192 snprintf(message
, length
, "external password quality "
193 "program failed without error");
196 reply
[strcspn(reply
, "\n")] = '\0';
197 snprintf(message
, length
, "External password quality "
198 "program failed: %s", reply
);
203 wait_for_process(child
);
206 reply
[strcspn(reply
, "\n")] = '\0';
211 status
= wait_for_process(child
);
213 if (SE_IS_ERROR(status
) || SE_PROCSTATUS(status
) != 0) {
214 snprintf(message
, length
, "external program failed: %s", reply
);
219 if (strcmp(reply
, "APPROVED") != 0) {
220 snprintf(message
, length
, "%s", reply
);
231 static kadm5_passwd_quality_check_func_v0 passwd_quality_check
=
232 min_length_passwd_quality_v0
;
234 struct kadm5_pw_policy_check_func builtin_funcs
[] = {
235 { "minimum-length", min_length_passwd_quality
},
236 { "character-class", char_class_passwd_quality
},
237 { "external-check", external_passwd_quality
},
240 struct kadm5_pw_policy_verifier builtin_verifier
= {
242 KADM5_PASSWD_VERSION_V1
,
247 static struct kadm5_pw_policy_verifier
**verifiers
;
248 static int num_verifiers
;
251 * setup the password quality hook
259 kadm5_setup_passwd_quality_check(krb5_context context
,
260 const char *check_library
,
261 const char *check_function
)
269 if(check_library
== NULL
) {
270 tmp
= krb5_config_get_string(context
, NULL
,
277 if(check_function
== NULL
) {
278 tmp
= krb5_config_get_string(context
, NULL
,
283 check_function
= tmp
;
285 if(check_library
!= NULL
&& check_function
== NULL
)
286 check_function
= "passwd_check";
288 if(check_library
== NULL
)
290 handle
= dlopen(check_library
, RTLD_NOW
);
292 krb5_warnx(context
, "failed to open `%s'", check_library
);
295 version
= (int *) dlsym(handle
, "version");
296 if(version
== NULL
) {
298 "didn't find `version' symbol in `%s'", check_library
);
302 if(*version
!= KADM5_PASSWD_VERSION_V0
) {
304 "version of loaded library is %d (expected %d)",
305 *version
, KADM5_PASSWD_VERSION_V0
);
309 sym
= dlsym(handle
, check_function
);
312 "didn't find `%s' symbol in `%s'",
313 check_function
, check_library
);
317 passwd_quality_check
= (kadm5_passwd_quality_check_func_v0
) sym
;
318 #endif /* HAVE_DLOPEN */
323 static krb5_error_code
324 add_verifier(krb5_context context
, const char *check_library
)
326 struct kadm5_pw_policy_verifier
*v
, **tmp
;
330 handle
= dlopen(check_library
, RTLD_NOW
);
332 krb5_warnx(context
, "failed to open `%s'", check_library
);
335 v
= (struct kadm5_pw_policy_verifier
*) dlsym(handle
, "kadm5_password_verifier");
338 "didn't find `kadm5_password_verifier' symbol "
339 "in `%s'", check_library
);
343 if(v
->version
!= KADM5_PASSWD_VERSION_V1
) {
345 "version of loaded library is %d (expected %d)",
346 v
->version
, KADM5_PASSWD_VERSION_V1
);
350 for (i
= 0; i
< num_verifiers
; i
++) {
351 if (strcmp(v
->name
, verifiers
[i
]->name
) == 0)
354 if (i
< num_verifiers
) {
355 krb5_warnx(context
, "password verifier library `%s' is already loaded",
361 tmp
= realloc(verifiers
, (num_verifiers
+ 1) * sizeof(*verifiers
));
363 krb5_warnx(context
, "out of memory");
368 verifiers
[num_verifiers
] = v
;
377 kadm5_add_passwd_quality_verifier(krb5_context context
,
378 const char *check_library
)
382 if(check_library
== NULL
) {
386 tmp
= krb5_config_get_strings(context
, NULL
,
390 if(tmp
== NULL
|| *tmp
== NULL
)
394 ret
= add_verifier(context
, *tmp
);
401 return add_verifier(context
, check_library
);
405 #endif /* HAVE_DLOPEN */
412 static const struct kadm5_pw_policy_check_func
*
413 find_func(krb5_context context
, const char *name
)
415 const struct kadm5_pw_policy_check_func
*f
;
417 const char *p
, *func
;
420 p
= strchr(name
, ':');
422 size_t len
= p
- name
+ 1;
424 module
= malloc(len
);
427 strlcpy(module
, name
, len
);
431 /* Find module in loaded modules first */
432 for (i
= 0; i
< num_verifiers
; i
++) {
433 if (module
&& strcmp(module
, verifiers
[i
]->name
) != 0)
435 for (f
= verifiers
[i
]->funcs
; f
->name
; f
++)
436 if (strcmp(func
, f
->name
) == 0) {
442 /* Lets try try the builtin modules */
443 if (module
== NULL
|| strcmp(module
, "builtin") == 0) {
444 for (f
= builtin_verifier
.funcs
; f
->name
; f
++)
445 if (strcmp(func
, f
->name
) == 0) {
457 kadm5_check_password_quality (krb5_context context
,
458 krb5_principal principal
,
461 const struct kadm5_pw_policy_check_func
*proc
;
462 static char error_msg
[1024];
468 * Check if we should use the old version of policy function.
471 v
= krb5_config_get_strings(context
, NULL
,
476 msg
= (*passwd_quality_check
) (context
, principal
, pwd_data
);
478 krb5_set_error_message(context
, 0, "password policy failed: %s", msg
);
485 for(vp
= v
; *vp
; vp
++) {
486 proc
= find_func(context
, *vp
);
488 msg
= "failed to find password verifier function";
489 krb5_set_error_message(context
, 0, "Failed to find password policy "
490 "function: %s", *vp
);
493 ret
= (proc
->func
)(context
, principal
, pwd_data
, NULL
,
494 error_msg
, sizeof(error_msg
));
496 krb5_set_error_message(context
, 0, "Password policy "
498 proc
->name
, error_msg
);
503 krb5_config_free_strings(v
);
505 /* If the default quality check isn't used, lets check that the
506 * old quality function the user have set too */
507 if (msg
== NULL
&& passwd_quality_check
!= min_length_passwd_quality_v0
) {
508 msg
= (*passwd_quality_check
) (context
, principal
, pwd_data
);
510 krb5_set_error_message(context
, 0, "(old) password policy "
511 "failed with %s", msg
);