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