3 * Copyright 1998-2007 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 #include <lber_pvt.h> /* BER_BVC definition */
20 #include <ac/string.h>
24 #elif defined(HAVE_KRB4)
28 /* From <ldap_pvt.h> */
29 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
31 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
32 static const struct berval scheme = BER_BVC("{KERBEROS}");
34 static int chk_kerberos(
35 const struct berval *sc,
36 const struct berval * passwd,
37 const struct berval * cred,
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 */
49 if( cred->bv_val[i] != '\0' ) {
50 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
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 */
59 if( passwd->bv_val[i] != '\0' ) {
60 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
63 rtn = LUTIL_PASSWD_ERR;
65 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
68 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
69 * (Royal Institute of Technology, Stockholm, Sweden).
70 * All rights reserved.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
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.
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.
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
100 krb5_context context;
103 krb5_get_init_creds_opt get_options;
104 krb5_verify_init_creds_opt verify_options;
105 krb5_principal client, server;
107 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
110 ret = krb5_init_context( &context );
112 return LUTIL_PASSWD_ERR;
116 krb5_get_init_creds_opt_set_preauth_list(&get_options,
120 krb5_get_init_creds_opt_init( &get_options );
122 krb5_verify_init_creds_opt_init( &verify_options );
124 ret = krb5_parse_name( context, passwd->bv_val, &client );
127 krb5_free_context( context );
128 return LUTIL_PASSWD_ERR;
131 ret = krb5_get_init_creds_password( context,
132 &creds, client, cred->bv_val, NULL,
133 NULL, 0, NULL, &get_options );
136 krb5_free_principal( context, client );
137 krb5_free_context( context );
138 return LUTIL_PASSWD_ERR;
142 char *host = ldap_pvt_get_fqdn( NULL );
145 krb5_free_principal( context, client );
146 krb5_free_context( context );
147 return LUTIL_PASSWD_ERR;
150 ret = krb5_sname_to_principal( context,
151 host, "ldap", KRB5_NT_SRV_HST, &server );
157 krb5_free_principal( context, client );
158 krb5_free_context( context );
159 return LUTIL_PASSWD_ERR;
162 ret = krb5_verify_init_creds( context,
163 &creds, server, NULL, NULL, &verify_options );
165 krb5_free_principal( context, client );
166 krb5_free_principal( context, server );
167 krb5_free_cred_contents( context, &creds );
168 krb5_free_context( context );
170 rtn = ret ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
172 #elif defined(HAVE_KRB4)
174 /* Borrowed from Heimdal kpopper */
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.
182 char lrealm[REALM_SZ];
183 char tkt[MAXHOSTNAMELEN];
185 status = krb_get_lrealm(lrealm,1);
186 if (status == KFAILURE) {
187 return LUTIL_PASSWD_ERR;
190 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
191 TKT_ROOT, (unsigned)getpid());
192 krb_set_tkt_string (tkt);
194 status = krb_verify_user( passwd->bv_val, "", lrealm,
195 cred->bv_val, 1, "ldap");
197 dest_tkt(); /* no point in keeping the tickets */
199 return status == KFAILURE ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
206 int init_module(int argc, char *argv[]) {
207 return lutil_passwd_add( (struct berval *)&scheme, chk_kerberos, NULL );