1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/kadm5/srv/pwqual.c */
3 /*
4  * Copyright (C) 2010 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /*
28  *
29  * Consumer interface for password quality plugins.
30  */
31 
32 #include "k5-int.h"
33 #include "server_internal.h"
34 #include <krb5/pwqual_plugin.h>
35 
36 struct pwqual_handle_st {
37     struct krb5_pwqual_vtable_st vt;
38     krb5_pwqual_moddata data;
39 };
40 
41 krb5_error_code
k5_pwqual_load(krb5_context context,const char * dict_file,pwqual_handle ** handles_out)42 k5_pwqual_load(krb5_context context, const char *dict_file,
43                pwqual_handle **handles_out)
44 {
45     krb5_error_code ret;
46     krb5_plugin_initvt_fn *modules = NULL, *mod;
47     size_t count;
48     pwqual_handle *list = NULL, handle = NULL;
49 
50     *handles_out = NULL;
51 
52     ret = k5_plugin_load_all(context, PLUGIN_INTERFACE_PWQUAL, &modules);
53     if (ret != 0)
54         goto cleanup;
55 
56     /* Allocate a large enough list of handles. */
57     for (count = 0; modules[count] != NULL; count++);
58     list = k5calloc(count + 1, sizeof(*list), &ret);
59     if (list == NULL)
60         goto cleanup;
61 
62     /* For each module, allocate a handle, initialize its vtable, and bind the
63      * dictionary filename. */
64     count = 0;
65     for (mod = modules; *mod != NULL; mod++) {
66         handle = k5alloc(sizeof(*handle), &ret);
67         if (handle == NULL)
68             goto cleanup;
69         ret = (*mod)(context, 1, 1, (krb5_plugin_vtable)&handle->vt);
70         if (ret != 0) {         /* Failed vtable init is non-fatal. */
71             free(handle);
72             handle = NULL;
73             continue;
74         }
75         handle->data = NULL;
76         if (handle->vt.open != NULL) {
77             ret = handle->vt.open(context, dict_file, &handle->data);
78             if (ret != 0)       /* Failed dictionary binding is fatal. */
79                 goto cleanup;
80         }
81         list[count++] = handle;
82         list[count] = NULL;
83         handle = NULL;
84     }
85     list[count] = NULL;
86 
87     ret = 0;
88     *handles_out = list;
89     list = NULL;
90 
91 cleanup:
92     free(handle);
93     k5_plugin_free_modules(context, modules);
94     k5_pwqual_free_handles(context, list);
95     return ret;
96 }
97 
98 void
k5_pwqual_free_handles(krb5_context context,pwqual_handle * handles)99 k5_pwqual_free_handles(krb5_context context, pwqual_handle *handles)
100 {
101     pwqual_handle *hp, handle;
102 
103     if (handles == NULL)
104         return;
105     for (hp = handles; *hp != NULL; hp++) {
106         handle = *hp;
107         if (handle->vt.close != NULL)
108             handle->vt.close(context, handle->data);
109         free(handle);
110     }
111     free(handles);
112 }
113 
114 const char *
k5_pwqual_name(krb5_context context,pwqual_handle handle)115 k5_pwqual_name(krb5_context context, pwqual_handle handle)
116 {
117     return handle->vt.name;
118 }
119 
120 krb5_error_code
k5_pwqual_check(krb5_context context,pwqual_handle handle,const char * password,const char * policy_name,krb5_principal princ)121 k5_pwqual_check(krb5_context context, pwqual_handle handle,
122                 const char *password, const char *policy_name,
123                 krb5_principal princ)
124 {
125     return handle->vt.check(context, handle->data, password, policy_name,
126                             princ, NULL);
127 }
128