]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/passwd/kerberos.c
Happy new year!
[openldap] / contrib / slapd-modules / passwd / kerberos.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2006 The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted only as authorized by the OpenLDAP
8  * Public License.
9  *
10  * A copy of this license is available in the file LICENSE in the
11  * top-level directory of the distribution or, alternatively, at
12  * <http://www.OpenLDAP.org/license.html>.
13  */
14
15 #include <unistd.h>
16
17 #include <lber.h>
18 #include <lber_pvt.h>   /* BER_BVC definition */
19 #include "lutil.h"
20 #include <ac/string.h>
21
22 #ifdef HAVE_KRB5
23 #include <krb5.h>
24 #elif defined(HAVE_KRB4)
25 #include <krb.h>
26 #endif
27
28 /* From <ldap_pvt.h> */
29 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
30
31 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
32 static const struct berval scheme = BER_BVC("{KERBEROS}");
33
34 static int chk_kerberos(
35         const struct berval *sc,
36         const struct berval * passwd,
37         const struct berval * cred,
38         const char **text )
39 {
40         unsigned int i;
41         int rtn;
42
43         for( i=0; i<cred->bv_len; i++) {
44                 if(cred->bv_val[i] == '\0') {
45                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
46                 }
47         }
48
49         if( cred->bv_val[i] != '\0' ) {
50                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
51         }
52
53         for( i=0; i<passwd->bv_len; i++) {
54                 if(passwd->bv_val[i] == '\0') {
55                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
56                 }
57         }
58
59         if( passwd->bv_val[i] != '\0' ) {
60                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
61         }
62
63         rtn = LUTIL_PASSWD_ERR;
64
65 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
66         {
67 /* Portions:
68  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
69  * (Royal Institute of Technology, Stockholm, Sweden).
70  * All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  *
76  * 1. Redistributions of source code must retain the above copyright
77  *    notice, this list of conditions and the following disclaimer.
78  *
79  * 2. Redistributions in binary form must reproduce the above copyright
80  *    notice, this list of conditions and the following disclaimer in the
81  *    documentation and/or other materials provided with the distribution.
82  *
83  * 3. Neither the name of the Institute nor the names of its contributors
84  *    may be used to endorse or promote products derived from this software
85  *    without specific prior written permission.
86  *
87  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
88  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
91  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97  * SUCH DAMAGE.
98  */
99
100                 krb5_context context;
101                 krb5_error_code ret;
102                 krb5_creds creds;
103                 krb5_get_init_creds_opt get_options;
104                 krb5_verify_init_creds_opt verify_options;
105                 krb5_principal client, server;
106 #ifdef notdef
107                 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
108 #endif
109
110                 ret = krb5_init_context( &context );
111                 if (ret) {
112                         return LUTIL_PASSWD_ERR;
113                 }
114
115 #ifdef notdef
116                 krb5_get_init_creds_opt_set_preauth_list(&get_options,
117                         pre_auth_types, 1);
118 #endif
119
120                 krb5_get_init_creds_opt_init( &get_options );
121
122                 krb5_verify_init_creds_opt_init( &verify_options );
123         
124                 ret = krb5_parse_name( context, passwd->bv_val, &client );
125
126                 if (ret) {
127                         krb5_free_context( context );
128                         return LUTIL_PASSWD_ERR;
129                 }
130
131                 ret = krb5_get_init_creds_password( context,
132                         &creds, client, cred->bv_val, NULL,
133                         NULL, 0, NULL, &get_options );
134
135                 if (ret) {
136                         krb5_free_principal( context, client );
137                         krb5_free_context( context );
138                         return LUTIL_PASSWD_ERR;
139                 }
140
141                 {
142                         char *host = ldap_pvt_get_fqdn( NULL );
143
144                         if( host == NULL ) {
145                                 krb5_free_principal( context, client );
146                                 krb5_free_context( context );
147                                 return LUTIL_PASSWD_ERR;
148                         }
149
150                         ret = krb5_sname_to_principal( context,
151                                 host, "ldap", KRB5_NT_SRV_HST, &server );
152
153                         ber_memfree( host );
154                 }
155
156                 if (ret) {
157                         krb5_free_principal( context, client );
158                         krb5_free_context( context );
159                         return LUTIL_PASSWD_ERR;
160                 }
161
162                 ret = krb5_verify_init_creds( context,
163                         &creds, server, NULL, NULL, &verify_options );
164
165                 krb5_free_principal( context, client );
166                 krb5_free_principal( context, server );
167                 krb5_free_cred_contents( context, &creds );
168                 krb5_free_context( context );
169
170                 rtn = ret ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
171         }
172 #elif   defined(HAVE_KRB4)
173         {
174                 /* Borrowed from Heimdal kpopper */
175 /* Portions:
176  * Copyright (c) 1989 Regents of the University of California.
177  * All rights reserved.  The Berkeley software License Agreement
178  * specifies the terms and conditions for redistribution.
179  */
180
181                 int status;
182                 char lrealm[REALM_SZ];
183                 char tkt[MAXHOSTNAMELEN];
184
185                 status = krb_get_lrealm(lrealm,1);
186                 if (status == KFAILURE) {
187                         return LUTIL_PASSWD_ERR;
188                 }
189
190                 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
191                         TKT_ROOT, (unsigned)getpid());
192                 krb_set_tkt_string (tkt);
193
194                 status = krb_verify_user( passwd->bv_val, "", lrealm,
195                         cred->bv_val, 1, "ldap");
196
197                 dest_tkt(); /* no point in keeping the tickets */
198
199                 return status == KFAILURE ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
200         }
201 #endif
202
203         return rtn;
204 }
205
206 int init_module(int argc, char *argv[]) {
207         return lutil_passwd_add( (struct berval *)&scheme, chk_kerberos, NULL );
208 }