]> git.sur5r.net Git - openldap/blob - libraries/libldap/kbind.c
ITS#2484, set sasl_maxbuf to SASL_MAX_BUFF_SIZE if it was negotiated
[openldap] / libraries / libldap / kbind.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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  * Portions Copyright (C) The Internet Society (1997)
14  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
15  */
16
17 /*
18  *      BindRequest ::= SEQUENCE {
19  *              version         INTEGER,
20  *              name            DistinguishedName,       -- who
21  *              authentication  CHOICE {
22  *                      simple          [0] OCTET STRING -- passwd
23 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
24  *                      krbv42ldap      [1] OCTET STRING
25  *                      krbv42dsa       [2] OCTET STRING
26 #endif
27  *                      sasl            [3] SaslCredentials     -- LDAPv3
28  *              }
29  *      }
30  *
31  *      BindResponse ::= SEQUENCE {
32  *              COMPONENTS OF LDAPResult,
33  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
34  *      }
35  *
36  */
37
38 #include "portable.h"
39
40 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
41
42 #include <stdio.h>
43 #include <ac/stdlib.h>
44
45 #include <ac/krb.h>
46 #include <ac/socket.h>
47 #include <ac/string.h>
48 #include <ac/time.h>
49
50 #include "ldap-int.h"
51
52 /*
53  * ldap_kerberos_bind1 - initiate a bind to the ldap server using
54  * kerberos authentication.  The dn is supplied.  It is assumed the user
55  * already has a valid ticket granting ticket.  The msgid of the
56  * request is returned on success (suitable for passing to ldap_result()),
57  * -1 is returned if there's trouble.
58  *
59  * Example:
60  *      ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" )
61  */
62 int
63 ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
64 {
65         BerElement      *ber;
66         char            *cred;
67         int             rc;
68         ber_len_t credlen;
69         ber_int_t       id;
70
71 #ifdef NEW_LOGGING
72         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind1\n", 0, 0, 0 );
73 #else
74         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1\n", 0, 0, 0 );
75 #endif
76
77         if( ld->ld_version > LDAP_VERSION2 ) {
78                 ld->ld_errno = LDAP_NOT_SUPPORTED;
79                 return -1;
80         }
81
82         if ( dn == NULL )
83                 dn = "";
84
85         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
86             &credlen )) == NULL ) {
87                 return( -1 );   /* ld_errno should already be set */
88         }
89
90         /* create a message to send */
91         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
92                 LDAP_FREE( cred );
93                 return( -1 );
94         }
95
96         LDAP_NEXT_MSGID( ld, id );
97         /* fill it in */
98         rc = ber_printf( ber, "{it{istoN}N}", id, LDAP_REQ_BIND,
99             ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
100
101         if ( rc == -1 ) {
102                 LDAP_FREE( cred );
103                 ber_free( ber, 1 );
104                 ld->ld_errno = LDAP_ENCODING_ERROR;
105                 return( -1 );
106         }
107
108         LDAP_FREE( cred );
109
110
111         /* send the message */
112         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id ));
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 ( OPERATION, ENTRY, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
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, 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         ber_int_t id;
158
159 #ifdef NEW_LOGGING
160         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind2\n", 0, 0, 0 );
161 #else
162         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
163 #endif
164
165         if( ld->ld_version > LDAP_VERSION2 ) {
166                 ld->ld_errno = LDAP_NOT_SUPPORTED;
167                 return -1;
168         }
169
170         if ( dn == NULL )
171                 dn = "";
172
173         if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
174             == NULL ) {
175                 return( -1 );   /* ld_errno should already be set */
176         }
177
178         /* create a message to send */
179         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
180                 LDAP_FREE( cred );
181                 return( -1 );
182         }
183
184         LDAP_NEXT_MSGID( ld, id );
185         /* fill it in */
186         rc = ber_printf( ber, "{it{istoN}N}", id, LDAP_REQ_BIND,
187             ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
188
189         LDAP_FREE( cred );
190
191         if ( rc == -1 ) {
192                 ber_free( ber, 1 );
193                 ld->ld_errno = LDAP_ENCODING_ERROR;
194                 return( -1 );
195         }
196
197         /* send the message */
198         return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id ));
199 }
200
201 /* synchronous bind to DSA using kerberos */
202 int
203 ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
204 {
205         int             msgid;
206         LDAPMessage     *res;
207
208 #ifdef NEW_LOGGING
209         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind2_s\n" , 0, 0, 0);
210 #else
211         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
212 #endif
213
214         /* initiate the bind */
215         if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
216                 return( ld->ld_errno );
217
218         /* wait for a result */
219         if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &res )
220             == -1 ) {
221                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
222         }
223
224         return( ldap_result2error( ld, res, 1 ) );
225 }
226
227 /* synchronous bind to ldap and DSA using kerberos */
228 int
229 ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
230 {
231         int     err;
232
233 #ifdef NEW_LOGGING
234         LDAP_LOG ( OPERATION, ENTRY, "ldap_kerberos_bind_s\n", 0, 0, 0 );
235 #else
236         Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
237 #endif
238
239         if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
240                 return( err );
241
242         return( ldap_kerberos_bind2_s( ld, dn ) );
243 }
244
245
246 #ifndef AUTHMAN
247 /*
248  * ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
249  * The dn of the entry to which to bind is supplied.  It's assumed the
250  * user already has a tgt.
251  */
252
253 char *
254 ldap_get_kerberosv4_credentials(
255         LDAP *ld,
256         LDAP_CONST char *who,
257         LDAP_CONST char *service,
258         ber_len_t *len )
259 {
260         KTEXT_ST        ktxt;
261         int             err;
262         char            realm[REALM_SZ], *cred, *krbinstance;
263
264 #ifdef NEW_LOGGING
265         LDAP_LOG ( OPERATION, ENTRY, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
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 ( OPERATION, ERR, 
273                         "ldap_get_kerberosv4_credentials: krb_get_tf_realm failed: %s\n",
274                         krb_err_txt[err], 0, 0 );
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 ( OPERATION, ERR, 
297                         "ldap_get_kerberosv4_credentials: krb_mk_req failed: %s\n",
298                         krb_err_txt[err], 0, 0 );
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 */