Back to index

openldap  2.4.31
kerberos.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include <unistd.h>
00017 
00018 #include <lber.h>
00019 #include <lber_pvt.h>       /* BER_BVC definition */
00020 #include "lutil.h"
00021 #include <ac/string.h>
00022 
00023 #ifdef HAVE_KRB5
00024 #include <krb5.h>
00025 #elif defined(HAVE_KRB4)
00026 #include <krb.h>
00027 #endif
00028 
00029 /* From <ldap_pvt.h> */
00030 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
00031 
00032 static LUTIL_PASSWD_CHK_FUNC chk_kerberos;
00033 static const struct berval scheme = BER_BVC("{KERBEROS}");
00034 
00035 static int chk_kerberos(
00036        const struct berval *sc,
00037        const struct berval * passwd,
00038        const struct berval * cred,
00039        const char **text )
00040 {
00041        unsigned int i;
00042        int rtn;
00043 
00044        for( i=0; i<cred->bv_len; i++) {
00045               if(cred->bv_val[i] == '\0') {
00046                      return LUTIL_PASSWD_ERR;    /* NUL character in password */
00047               }
00048        }
00049 
00050        if( cred->bv_val[i] != '\0' ) {
00051               return LUTIL_PASSWD_ERR;    /* cred must behave like a string */
00052        }
00053 
00054        for( i=0; i<passwd->bv_len; i++) {
00055               if(passwd->bv_val[i] == '\0') {
00056                      return LUTIL_PASSWD_ERR;    /* NUL character in password */
00057               }
00058        }
00059 
00060        if( passwd->bv_val[i] != '\0' ) {
00061               return LUTIL_PASSWD_ERR;    /* passwd must behave like a string */
00062        }
00063 
00064        rtn = LUTIL_PASSWD_ERR;
00065 
00066 #ifdef HAVE_KRB5 /* HAVE_HEIMDAL_KRB5 */
00067        {
00068 /* Portions:
00069  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska H\xf6gskolan
00070  * (Royal Institute of Technology, Stockholm, Sweden).
00071  * All rights reserved.
00072  *
00073  * Redistribution and use in source and binary forms, with or without
00074  * modification, are permitted provided that the following conditions
00075  * are met:
00076  *
00077  * 1. Redistributions of source code must retain the above copyright
00078  *    notice, this list of conditions and the following disclaimer.
00079  *
00080  * 2. Redistributions in binary form must reproduce the above copyright
00081  *    notice, this list of conditions and the following disclaimer in the
00082  *    documentation and/or other materials provided with the distribution.
00083  *
00084  * 3. Neither the name of the Institute nor the names of its contributors
00085  *    may be used to endorse or promote products derived from this software
00086  *    without specific prior written permission.
00087  *
00088  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00089  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00090  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00091  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00092  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00093  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00094  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00095  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00096  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00097  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00098  * SUCH DAMAGE.
00099  */
00100 
00101               krb5_context context;
00102               krb5_error_code ret;
00103               krb5_creds creds;
00104               krb5_get_init_creds_opt get_options;
00105               krb5_verify_init_creds_opt verify_options;
00106               krb5_principal client, server;
00107 #ifdef notdef
00108               krb5_preauthtype pre_auth_types[] = {KRB5_PADATA_ENC_TIMESTAMP};
00109 #endif
00110 
00111               ret = krb5_init_context( &context );
00112               if (ret) {
00113                      return LUTIL_PASSWD_ERR;
00114               }
00115 
00116 #ifdef notdef
00117               krb5_get_init_creds_opt_set_preauth_list(&get_options,
00118                      pre_auth_types, 1);
00119 #endif
00120 
00121               krb5_get_init_creds_opt_init( &get_options );
00122 
00123               krb5_verify_init_creds_opt_init( &verify_options );
00124        
00125               ret = krb5_parse_name( context, passwd->bv_val, &client );
00126 
00127               if (ret) {
00128                      krb5_free_context( context );
00129                      return LUTIL_PASSWD_ERR;
00130               }
00131 
00132               ret = krb5_get_init_creds_password( context,
00133                      &creds, client, cred->bv_val, NULL,
00134                      NULL, 0, NULL, &get_options );
00135 
00136               if (ret) {
00137                      krb5_free_principal( context, client );
00138                      krb5_free_context( context );
00139                      return LUTIL_PASSWD_ERR;
00140               }
00141 
00142               {
00143                      char *host = ldap_pvt_get_fqdn( NULL );
00144 
00145                      if( host == NULL ) {
00146                             krb5_free_principal( context, client );
00147                             krb5_free_context( context );
00148                             return LUTIL_PASSWD_ERR;
00149                      }
00150 
00151                      ret = krb5_sname_to_principal( context,
00152                             host, "ldap", KRB5_NT_SRV_HST, &server );
00153 
00154                      ber_memfree( host );
00155               }
00156 
00157               if (ret) {
00158                      krb5_free_principal( context, client );
00159                      krb5_free_context( context );
00160                      return LUTIL_PASSWD_ERR;
00161               }
00162 
00163               ret = krb5_verify_init_creds( context,
00164                      &creds, server, NULL, NULL, &verify_options );
00165 
00166               krb5_free_principal( context, client );
00167               krb5_free_principal( context, server );
00168               krb5_free_cred_contents( context, &creds );
00169               krb5_free_context( context );
00170 
00171               rtn = ret ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
00172        }
00173 #elif  defined(HAVE_KRB4)
00174        {
00175               /* Borrowed from Heimdal kpopper */
00176 /* Portions:
00177  * Copyright (c) 1989 Regents of the University of California.
00178  * All rights reserved.  The Berkeley software License Agreement
00179  * specifies the terms and conditions for redistribution.
00180  */
00181 
00182               int status;
00183               char lrealm[REALM_SZ];
00184               char tkt[MAXHOSTNAMELEN];
00185 
00186               status = krb_get_lrealm(lrealm,1);
00187               if (status == KFAILURE) {
00188                      return LUTIL_PASSWD_ERR;
00189               }
00190 
00191               snprintf(tkt, sizeof(tkt), "%s_slapd.%u",
00192                      TKT_ROOT, (unsigned)getpid());
00193               krb_set_tkt_string (tkt);
00194 
00195               status = krb_verify_user( passwd->bv_val, "", lrealm,
00196                      cred->bv_val, 1, "ldap");
00197 
00198               dest_tkt(); /* no point in keeping the tickets */
00199 
00200               return status == KFAILURE ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
00201        }
00202 #endif
00203 
00204        return rtn;
00205 }
00206 
00207 int init_module(int argc, char *argv[]) {
00208        return lutil_passwd_add( (struct berval *)&scheme, chk_kerberos, NULL );
00209 }