]> git.sur5r.net Git - openldap/blob - servers/ldapd/kerberos.c
5f7d9767a1c461aea263d75e7fe6c6db8a343253
[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 #include "portable.h"
14
15 #ifdef HAVE_KERBEROS
16
17 #include <stdio.h>
18
19 #include <ac/krb.h>
20 #include <ac/socket.h>
21
22 #include <quipu/bind.h>
23 #if ISODEPACKAGE == IC
24 #include <quipu/DAS-types.h>
25 #else
26 #include <pepsy/DAS-types.h>
27 #endif
28
29 #include "lber.h"
30 #include "ldap.h"
31 #include "common.h"
32
33 int
34 kerberosv4_ldap_auth( char *cred, long  len )
35 {
36         KTEXT_ST        k;
37         KTEXT           ktxt = &k;
38         char            instance[INST_SZ];
39         int             err;
40         AUTH_DAT        ad;
41         extern char     *krb_ldap_service;
42         extern char     *kerberos_keyfile;
43
44         Debug( LDAP_DEBUG_TRACE, "kerberosv4_ldap_auth\n", 0, 0, 0 );
45
46         SAFEMEMCPY( ktxt->dat, cred, len );
47         ktxt->length = len;
48
49         strcpy( instance, "*" );
50         if ( (err = krb_rd_req( ktxt, krb_ldap_service, instance, 0L,
51             &ad, kerberos_keyfile )) != KSUCCESS ) {
52                 Debug( LDAP_DEBUG_ANY, "krb_rd_req failed (%s)\n",
53                     krb_err_txt[err], 0, 0 );
54                 return( LDAP_INVALID_CREDENTIALS );
55         }
56
57         return( LDAP_SUCCESS );
58 }
59
60 int
61 kerberosv4_bindarg( 
62     struct ds_bind_arg  *ba,
63     DN                  dn,
64     char                *cred,
65     long                len,
66     u_long              *nonce
67 )
68 {
69         struct type_UNIV_EXTERNAL       *e;
70         struct kerberos_parms           kp;
71         PE                              pe;
72         struct timeval                  tv;
73         char                            realm[REALM_SZ];
74         int                             err;
75         extern char                     *krb_x500_service;
76         extern char                     *krb_x500_instance;
77
78         Debug( LDAP_DEBUG_TRACE, "kerberosv4_bindarg\n", 0, 0, 0 );
79
80         e = (struct type_UNIV_EXTERNAL *) calloc( 1,
81             sizeof(struct type_UNIV_EXTERNAL) );
82         e->encoding = (struct choice_UNIV_0 *) calloc( 1,
83             sizeof(struct choice_UNIV_0) );
84         ba->dba_external = e;
85         ba->dba_version = DBA_VERSION_V1988;
86         ba->dba_auth_type = DBA_AUTH_EXTERNAL;
87
88         e->indirect__reference = AUTH_TYPE_KERBEROS_V4;
89         e->direct__reference = NULLOID;
90         e->data__value__descriptor = str2qb( "KRBv4 client credentials",
91             24, 1 );
92
93         kp.kp_dn = dn;
94         kp.kp_version = AUTH_TYPE_KERBEROS_V4;
95
96         if ( (err = krb_get_lrealm( realm, 1 )) != KSUCCESS ) {
97                 Debug( LDAP_DEBUG_ANY, "krb_get_lrealm failed (%s)\n",
98                     krb_err_txt[err], 0, 0 );
99                 return( LDAP_OPERATIONS_ERROR );
100         }
101
102         gettimeofday( &tv, NULL );
103         *nonce = tv.tv_sec;
104         SAFEMEMCPY( kp.kp_ktxt.dat, cred, len );
105         kp.kp_ktxt.length = len;
106         if ( encode_kerberos_parms( &pe, &kp ) == NOTOK ) {
107                 Debug( LDAP_DEBUG_ANY, "kerberos parms encoding failed\n", 0,
108                     0, 0 );
109                 return( LDAP_OPERATIONS_ERROR );
110         }
111
112         e->encoding->offset = choice_UNIV_0_single__ASN1__type;
113         e->encoding->un.single__ASN1__type = pe;
114
115         return( 0 );
116 }
117
118 int
119 kerberos_check_mutual(
120     struct ds_bind_arg  *res,
121     u_long              nonce
122 )
123 {
124         struct type_UNIV_EXTERNAL       *e = res->dba_external;
125         struct kerberos_parms           *kp;
126         int                             ret;
127
128         Debug( LDAP_DEBUG_TRACE, "kerberos_check_mutual\n", 0, 0, 0 );
129
130         if ( decode_kerberos_parms( e->encoding->un.single__ASN1__type, &kp )
131             == NOTOK )
132                 return( NOTOK );
133         ret = ((kp->kp_nonce == (nonce + 1)) ? OK : NOTOK );
134
135         Debug( LDAP_DEBUG_TRACE, "expecting %d got %d\n", nonce, kp->kp_nonce,
136             0 );
137
138         pe_free( e->encoding->un.single__ASN1__type );
139         dn_free( kp->kp_dn );
140         free( (char *) kp );
141
142         return( ret );
143 }
144
145 #endif