]> git.sur5r.net Git - openldap/blob - servers/ldapd/kerberos.c
Initial implementation of Kerberos password verification for
[openldap] / servers / ldapd / kerberos.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1990 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 #include "portable.h"
15
16 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
17
18 #include <stdio.h>
19
20 #include <ac/krb.h>
21 #include <ac/socket.h>
22
23 #include <quipu/bind.h>
24 #if ISODEPACKAGE == IC
25 #include <quipu/DAS-types.h>
26 #else
27 #include <pepsy/DAS-types.h>
28 #endif
29
30 #include "lber.h"
31 #include "ldap.h"
32 #include "common.h"
33
34 int
35 kerberosv4_ldap_auth( char *cred, long  len )
36 {
37         KTEXT_ST        k;
38         KTEXT           ktxt = &k;
39         char            instance[INST_SZ];
40         int             err;
41         AUTH_DAT        ad;
42
43         Debug( LDAP_DEBUG_TRACE, "kerberosv4_ldap_auth\n", 0, 0, 0 );
44
45         SAFEMEMCPY( ktxt->dat, cred, len );
46         ktxt->length = len;
47
48         strcpy( instance, "*" );
49         if ( (err = krb_rd_req( ktxt, krb_ldap_service, instance, 0L,
50             &ad, kerberos_keyfile )) != KSUCCESS ) {
51                 Debug( LDAP_DEBUG_ANY, "krb_rd_req failed (%s)\n",
52                     krb_err_txt[err], 0, 0 );
53                 return( LDAP_INVALID_CREDENTIALS );
54         }
55
56         return( LDAP_SUCCESS );
57 }
58
59 int
60 kerberosv4_bindarg( 
61     struct ds_bind_arg  *ba,
62     DN                  dn,
63     char                *cred,
64     long                len,
65     u_long              *nonce
66 )
67 {
68         struct type_UNIV_EXTERNAL       *e;
69         struct kerberos_parms           kp;
70         PE                              pe;
71         struct timeval                  tv;
72         char                            realm[REALM_SZ];
73         int                             err;
74
75         Debug( LDAP_DEBUG_TRACE, "kerberosv4_bindarg\n", 0, 0, 0 );
76
77         e = (struct type_UNIV_EXTERNAL *) calloc( 1,
78             sizeof(struct type_UNIV_EXTERNAL) );
79         e->encoding = (struct choice_UNIV_0 *) calloc( 1,
80             sizeof(struct choice_UNIV_0) );
81         ba->dba_external = e;
82         ba->dba_version = DBA_VERSION_V1988;
83         ba->dba_auth_type = DBA_AUTH_EXTERNAL;
84
85         e->indirect__reference = AUTH_TYPE_KERBEROS_V4;
86         e->direct__reference = NULLOID;
87         e->data__value__descriptor = str2qb( "KRBv4 client credentials",
88             24, 1 );
89
90         kp.kp_dn = dn;
91         kp.kp_version = AUTH_TYPE_KERBEROS_V4;
92
93         if ( (err = krb_get_lrealm( realm, 1 )) != KSUCCESS ) {
94                 Debug( LDAP_DEBUG_ANY, "krb_get_lrealm failed (%s)\n",
95                     krb_err_txt[err], 0, 0 );
96                 return( LDAP_OPERATIONS_ERROR );
97         }
98
99         gettimeofday( &tv, NULL );
100         *nonce = tv.tv_sec;
101         SAFEMEMCPY( kp.kp_ktxt.dat, cred, len );
102         kp.kp_ktxt.length = len;
103         if ( encode_kerberos_parms( &pe, &kp ) == NOTOK ) {
104                 Debug( LDAP_DEBUG_ANY, "kerberos parms encoding failed\n", 0,
105                     0, 0 );
106                 return( LDAP_OPERATIONS_ERROR );
107         }
108
109         e->encoding->offset = choice_UNIV_0_single__ASN1__type;
110         e->encoding->un.single__ASN1__type = pe;
111
112         return( 0 );
113 }
114
115 int
116 kerberos_check_mutual(
117     struct ds_bind_arg  *res,
118     u_long              nonce
119 )
120 {
121         struct type_UNIV_EXTERNAL       *e = res->dba_external;
122         struct kerberos_parms           *kp;
123         int                             ret;
124
125         Debug( LDAP_DEBUG_TRACE, "kerberos_check_mutual\n", 0, 0, 0 );
126
127         if ( decode_kerberos_parms( e->encoding->un.single__ASN1__type, &kp )
128             == NOTOK )
129                 return( NOTOK );
130         ret = ((kp->kp_nonce == (nonce + 1)) ? OK : NOTOK );
131
132         Debug( LDAP_DEBUG_TRACE, "expecting %d got %d\n", nonce, kp->kp_nonce,
133             0 );
134
135         pe_free( e->encoding->un.single__ASN1__type );
136         dn_free( kp->kp_dn );
137         free( (char *) kp );
138
139         return( ret );
140 }
141
142 #endif