]> git.sur5r.net Git - openldap/blob - libraries/libldap/kbind.c
Suck in HEAD changes since 2.1alpha
[openldap] / libraries / libldap / kbind.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1993 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  kbind.c
11  */
12
13 /*
14  *      BindRequest ::= SEQUENCE {
15  *              version         INTEGER,
16  *              name            DistinguishedName,       -- who
17  *              authentication  CHOICE {
18  *                      simple          [0] OCTET STRING -- passwd
19 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
20  *                      krbv42ldap      [1] OCTET STRING
21  *                      krbv42dsa       [2] OCTET STRING
22 #endif
23  *                      sasl            [3] SaslCredentials     -- LDAPv3
24  *              }
25  *      }
26  *
27  *      BindResponse ::= SEQUENCE {
28  *              COMPONENTS OF LDAPResult,
29  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
30  *      }
31  *
32  */
33
34 #include "portable.h"
35
36 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
37
38 #include <stdio.h>
39 #include <ac/stdlib.h>
40
41 #include <ac/krb.h>
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45
46 #include "ldap-int.h"
47
48
49 /*
50  * ldap_kerberos_bind1 - initiate a bind to the ldap server using
51  * kerberos authentication.  The dn is supplied.  It is assumed the user
52  * already has a valid ticket granting ticket.  The msgid of the
53  * request is returned on success (suitable for passing to ldap_result()),
54  * -1 is returned if there's trouble.
55  *
56  * Example:
57  *      ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" )
58  */
59 int
60 ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
61 {
62         BerElement      *ber;
63         char            *cred;
64         int             rc;
65         ber_len_t credlen;
66
67 #ifdef NEW_LOGGING
68         LDAP_LOG (( "kbind", LDAP_LEVEL_ENTRY, "ldap_kerberos_bind1\n" ));
69 #else
70         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1\n", 0, 0, 0 );
71 #endif
72
73         if( ld->ld_version > LDAP_VERSION2 ) {
74                 ld->ld_errno = LDAP_NOT_SUPPORTED;
75                 return -1;
76         }
77
78         if ( dn == NULL )
79                 dn = "";
80
81         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
82             &credlen )) == NULL ) {
83                 return( -1 );   /* ld_errno should already be set */
84         }
85
86         /* create a message to send */
87         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
88                 LDAP_FREE( cred );
89                 return( -1 );
90         }
91
92         /* fill it in */
93         rc = ber_printf( ber, "{it{istoN}N}", ++ld->ld_msgid, LDAP_REQ_BIND,
94             ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
95
96         if ( rc == -1 ) {
97                 LDAP_FREE( cred );
98                 ber_free( ber, 1 );
99                 ld->ld_errno = LDAP_ENCODING_ERROR;
100                 return( -1 );
101         }
102
103         LDAP_FREE( cred );
104
105 #ifndef LDAP_NOCACHE
106         if ( ld->ld_cache != NULL ) {
107                 ldap_flush_cache( ld );
108         }
109 #endif /* !LDAP_NOCACHE */
110
111         /* send the message */
112         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
113 }
114
115 int
116 ldap_kerberos_bind1_s( LDAP *ld, LDAP_CONST char *dn )
117 {
118         int             msgid;
119         LDAPMessage     *res;
120
121 #ifdef NEW_LOGGING
122         LDAP_LOG (( "kbind", LDAP_LEVEL_ENTRY, "ldap_kerberos_bind1_s\n" ));
123 #else
124         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
125 #endif
126
127         /* initiate the bind */
128         if ( (msgid = ldap_kerberos_bind1( ld, dn )) == -1 )
129                 return( ld->ld_errno );
130
131         /* wait for a result */
132         if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
133             == -1 ) {
134                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
135         }
136
137         return( ldap_result2error( ld, res, 1 ) );
138 }
139
140 /*
141  * ldap_kerberos_bind2 - initiate a bind to the X.500 server using
142  * kerberos authentication.  The dn is supplied.  It is assumed the user
143  * already has a valid ticket granting ticket.  The msgid of the
144  * request is returned on success (suitable for passing to ldap_result()),
145  * -1 is returned if there's trouble.
146  *
147  * Example:
148  *      ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" )
149  */
150 int
151 ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
152 {
153         BerElement      *ber;
154         char            *cred;
155         int             rc;
156         ber_len_t credlen;
157
158 #ifdef NEW_LOGGING
159         LDAP_LOG (( "kbind", LDAP_LEVEL_ENTRY, "ldap_kerberos_bind2\n" ));
160 #else
161         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
162 #endif
163
164         if( ld->ld_version > LDAP_VERSION2 ) {
165                 ld->ld_errno = LDAP_NOT_SUPPORTED;
166                 return -1;
167         }
168
169         if ( dn == NULL )
170                 dn = "";
171
172         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
173             == NULL ) {
174                 return( -1 );   /* ld_errno should already be set */
175         }
176
177         /* create a message to send */
178         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
179                 LDAP_FREE( cred );
180                 return( -1 );
181         }
182
183         /* fill it in */
184         rc = ber_printf( ber, "{it{istoN}N}", ++ld->ld_msgid, LDAP_REQ_BIND,
185             ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
186
187
188         LDAP_FREE( cred );
189
190         if ( rc == -1 ) {
191                 ber_free( ber, 1 );
192                 ld->ld_errno = LDAP_ENCODING_ERROR;
193                 return( -1 );
194         }
195
196         /* send the message */
197         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
198 }
199
200 /* synchronous bind to DSA using kerberos */
201 int
202 ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
203 {
204         int             msgid;
205         LDAPMessage     *res;
206
207 #ifdef NEW_LOGGING
208         LDAP_LOG (( "kbind", LDAP_LEVEL_ENTRY, "ldap_kerberos_bind2_s\n" ));
209 #else
210         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
211 #endif
212
213         /* initiate the bind */
214         if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
215                 return( ld->ld_errno );
216
217         /* wait for a result */
218         if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
219             == -1 ) {
220                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
221         }
222
223         return( ldap_result2error( ld, res, 1 ) );
224 }
225
226 /* synchronous bind to ldap and DSA using kerberos */
227 int
228 ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
229 {
230         int     err;
231
232 #ifdef NEW_LOGGING
233         LDAP_LOG (( "kbind", LDAP_LEVEL_ENTRY, "ldap_kerberos_bind_s\n" ));
234 #else
235         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
236 #endif
237
238         if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
239                 return( err );
240
241         return( ldap_kerberos_bind2_s( ld, dn ) );
242 }
243
244
245 #ifndef AUTHMAN
246 /*
247  * ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
248  * The dn of the entry to which to bind is supplied.  It's assumed the
249  * user already has a tgt.
250  */
251
252 char *
253 ldap_get_kerberosv4_credentials(
254         LDAP *ld,
255         LDAP_CONST char *who,
256         LDAP_CONST char *service,
257         ber_len_t *len )
258 {
259         KTEXT_ST        ktxt;
260         int             err;
261         char            realm[REALM_SZ], *cred, *krbinstance;
262
263 #ifdef NEW_LOGGING
264         LDAP_LOG (( "kbind", LDAP_LEVEL_ENTRY, 
265                 "ldap_get_kerberosv4_credentials\n" ));
266 #else
267         Debug( LDAP_DEBUG_TRACE, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
268 #endif
269
270         if ( (err = krb_get_tf_realm( tkt_string(), realm )) != KSUCCESS ) {
271 #ifdef NEW_LOGGING
272                 LDAP_LOG (( "kbind", LDAP_LEVEL_ERR, 
273                         "ldap_get_kerberosv4_credentials: krb_get_tf_realm failed: %s\n",
274                         krb_err_txt[err] ));
275 #else
276                 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
277                         "krb_get_tf_realm failed: %s\n", krb_err_txt[err], 0, 0 );
278 #endif
279                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
280                 return( NULL );
281         }
282
283         if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
284                 /* not connected yet */
285                 int rc = ldap_open_defconn( ld );
286
287                 if( rc < 0 ) return NULL;
288         }
289
290         krbinstance = ld->ld_defconn->lconn_krbinstance;
291
292         if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
293             != KSUCCESS )
294         {
295 #ifdef NEW_LOGGING
296                 LDAP_LOG (( "kbind", LDAP_LEVEL_ERR, 
297                         "ldap_get_kerberosv4_credentials: krb_mk_req failed: %s\n",
298                         krb_err_txt[err] ));
299 #else
300                 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
301                         "krb_mk_req failed (%s)\n", krb_err_txt[err], 0, 0 );
302 #endif
303                 ld->ld_errno = LDAP_AUTH_UNKNOWN;
304                 return( NULL );
305         }
306
307         if ( ( cred = LDAP_MALLOC( ktxt.length )) == NULL ) {
308                 ld->ld_errno = LDAP_NO_MEMORY;
309                 return( NULL );
310         }
311
312         *len = ktxt.length;
313         AC_MEMCPY( cred, ktxt.dat, ktxt.length );
314
315         return( cred );
316 }
317
318 #endif /* !AUTHMAN */
319 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */