2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2011 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <lber_pvt.h> /* BER_BVC definition */
21 #include <ac/string.h>
25 #elif defined(HAVE_KRB4)
29 /* From <ldap_pvt.h> */
30 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
32 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
33 static const struct berval scheme = BER_BVC("{KERBEROS}");
35 static int chk_kerberos(
36 const struct berval *sc,
37 const struct berval * passwd,
38 const struct berval * cred,
44 for( i=0; i<cred->bv_len; i++) {
45 if(cred->bv_val[i] == '\0') {
46 return LUTIL_PASSWD_ERR; /* NUL character in password */
50 if( cred->bv_val[i] != '\0' ) {
51 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
54 for( i=0; i<passwd->bv_len; i++) {
55 if(passwd->bv_val[i] == '\0') {
56 return LUTIL_PASSWD_ERR; /* NUL character in password */
60 if( passwd->bv_val[i] != '\0' ) {
61 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
64 rtn = LUTIL_PASSWD_ERR;
66 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
69 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
70 * (Royal Institute of Technology, Stockholm, Sweden).
71 * All rights reserved.
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
84 * 3. Neither the name of the Institute nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
88 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101 krb5_context context;
104 krb5_get_init_creds_opt get_options;
105 krb5_verify_init_creds_opt verify_options;
106 krb5_principal client, server;
108 krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
111 ret = krb5_init_context( &context );
113 return LUTIL_PASSWD_ERR;
117 krb5_get_init_creds_opt_set_preauth_list(&get_options,
121 krb5_get_init_creds_opt_init( &get_options );
123 krb5_verify_init_creds_opt_init( &verify_options );
125 ret = krb5_parse_name( context, passwd->bv_val, &client );
128 krb5_free_context( context );
129 return LUTIL_PASSWD_ERR;
132 ret = krb5_get_init_creds_password( context,
133 &creds, client, cred->bv_val, NULL,
134 NULL, 0, NULL, &get_options );
137 krb5_free_principal( context, client );
138 krb5_free_context( context );
139 return LUTIL_PASSWD_ERR;
143 char *host = ldap_pvt_get_fqdn( NULL );
146 krb5_free_principal( context, client );
147 krb5_free_context( context );
148 return LUTIL_PASSWD_ERR;
151 ret = krb5_sname_to_principal( context,
152 host, "ldap", KRB5_NT_SRV_HST, &server );
158 krb5_free_principal( context, client );
159 krb5_free_context( context );
160 return LUTIL_PASSWD_ERR;
163 ret = krb5_verify_init_creds( context,
164 &creds, server, NULL, NULL, &verify_options );
166 krb5_free_principal( context, client );
167 krb5_free_principal( context, server );
168 krb5_free_cred_contents( context, &creds );
169 krb5_free_context( context );
171 rtn = ret ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
173 #elif defined(HAVE_KRB4)
175 /* Borrowed from Heimdal kpopper */
177 * Copyright (c) 1989 Regents of the University of California.
178 * All rights reserved. The Berkeley software License Agreement
179 * specifies the terms and conditions for redistribution.
183 char lrealm[REALM_SZ];
184 char tkt[MAXHOSTNAMELEN];
186 status = krb_get_lrealm(lrealm,1);
187 if (status == KFAILURE) {
188 return LUTIL_PASSWD_ERR;
191 snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
192 TKT_ROOT, (unsigned)getpid());
193 krb_set_tkt_string (tkt);
195 status = krb_verify_user( passwd->bv_val, "", lrealm,
196 cred->bv_val, 1, "ldap");
198 dest_tkt(); /* no point in keeping the tickets */
200 return status == KFAILURE ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
207 int init_module(int argc, char *argv[]) {
208 return lutil_passwd_add( (struct berval *)&scheme, chk_kerberos, NULL );