]> git.sur5r.net Git - openldap/blob - libraries/libldap/sasl.c
Fix #undef LDAP_UFN
[openldap] / libraries / libldap / sasl.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  *      BindRequest ::= SEQUENCE {
9  *              version         INTEGER,
10  *              name            DistinguishedName,       -- who
11  *              authentication  CHOICE {
12  *                      simple          [0] OCTET STRING -- passwd
13 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
14  *                      krbv42ldap      [1] OCTET STRING
15  *                      krbv42dsa       [2] OCTET STRING
16 #endif
17  *                      sasl            [3] SaslCredentials     -- LDAPv3
18  *              }
19  *      }
20  *
21  *      BindResponse ::= SEQUENCE {
22  *              COMPONENTS OF LDAPResult,
23  *              serverSaslCreds         OCTET STRING OPTIONAL -- LDAPv3
24  *      }
25  *
26  */
27
28 #include "portable.h"
29
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 #include <ac/time.h>
36 #include <ac/errno.h>
37
38 #include "ldap-int.h"
39
40
41 /*
42  * ldap_sasl_bind - bind to the ldap server (and X.500).
43  * The dn (usually NULL), mechanism, and credentials are provided.
44  * The message id of the request initiated is provided upon successful
45  * (LDAP_SUCCESS) return.
46  *
47  * Example:
48  *      ldap_sasl_bind( ld, NULL, "mechanism",
49  *              cred, NULL, NULL, &msgid )
50  */
51
52 int
53 ldap_sasl_bind(
54         LDAP                    *ld,
55         LDAP_CONST char *dn,
56         LDAP_CONST char *mechanism,
57         struct berval   *cred,
58         LDAPControl             **sctrls,
59         LDAPControl             **cctrls,
60         int                             *msgidp )
61 {
62         BerElement      *ber;
63         int rc;
64
65         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
66
67         assert( ld != NULL );
68         assert( LDAP_VALID( ld ) );
69         assert( msgidp != NULL );
70
71         if( msgidp == NULL ) {
72                 ld->ld_errno = LDAP_PARAM_ERROR;
73                 return ld->ld_errno;
74         }
75
76         if( mechanism == LDAP_SASL_SIMPLE ) {
77                 if( dn == NULL && cred != NULL ) {
78                         /* use default binddn */
79                         dn = ld->ld_defbinddn;
80                 }
81
82         } else if( ld->ld_version < LDAP_VERSION3 ) {
83                 ld->ld_errno = LDAP_NOT_SUPPORTED;
84                 return ld->ld_errno;
85         }
86
87         if ( dn == NULL ) {
88                 dn = "";
89         }
90
91         /* create a message to send */
92         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
93                 ld->ld_errno = LDAP_NO_MEMORY;
94                 return ld->ld_errno;
95         }
96
97         assert( BER_VALID( ber ) );
98
99         if( mechanism == LDAP_SASL_SIMPLE ) {
100                 /* simple bind */
101                 rc = ber_printf( ber, "{it{istON}" /*}*/,
102                         ++ld->ld_msgid, LDAP_REQ_BIND,
103                         ld->ld_version, dn, LDAP_AUTH_SIMPLE,
104                         cred );
105                 
106         } else if ( cred == NULL || !cred->bv_len ) {
107                 /* SASL bind w/o creditials */
108                 rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
109                         ++ld->ld_msgid, LDAP_REQ_BIND,
110                         ld->ld_version, dn, LDAP_AUTH_SASL,
111                         mechanism );
112
113         } else {
114                 /* SASL bind w/ creditials */
115                 rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
116                         ++ld->ld_msgid, LDAP_REQ_BIND,
117                         ld->ld_version, dn, LDAP_AUTH_SASL,
118                         mechanism, cred );
119         }
120
121         if( rc == -1 ) {
122                 ld->ld_errno = LDAP_ENCODING_ERROR;
123                 ber_free( ber, 1 );
124                 return( -1 );
125         }
126
127         /* Put Server Controls */
128         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
129                 ber_free( ber, 1 );
130                 return ld->ld_errno;
131         }
132
133         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
134                 ld->ld_errno = LDAP_ENCODING_ERROR;
135                 ber_free( ber, 1 );
136                 return ld->ld_errno;
137         }
138
139 #ifndef LDAP_NOCACHE
140         if ( ld->ld_cache != NULL ) {
141                 ldap_flush_cache( ld );
142         }
143 #endif /* !LDAP_NOCACHE */
144
145         /* send the message */
146         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
147
148         if(*msgidp < 0)
149                 return ld->ld_errno;
150
151         return LDAP_SUCCESS;
152 }
153
154
155 int
156 ldap_sasl_bind_s(
157         LDAP                    *ld,
158         LDAP_CONST char *dn,
159         LDAP_CONST char *mechanism,
160         struct berval   *cred,
161         LDAPControl             **sctrls,
162         LDAPControl             **cctrls,
163         struct berval   **servercredp )
164 {
165         int     rc, msgid;
166         LDAPMessage     *result;
167         struct berval   *scredp = NULL;
168
169         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
170
171         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
172         if( servercredp != NULL ) {
173                 if (ld->ld_version < LDAP_VERSION3) {
174                         ld->ld_errno = LDAP_NOT_SUPPORTED;
175                         return ld->ld_errno;
176                 }
177                 *servercredp = NULL;
178         }
179
180         rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
181
182         if ( rc != LDAP_SUCCESS ) {
183                 return( rc );
184         }
185
186         if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
187                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
188         }
189
190         /* parse the results */
191         scredp = NULL;
192         if( servercredp != NULL ) {
193                 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
194         }
195
196         if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
197                 ldap_msgfree( result );
198                 return( rc );
199         }
200
201         rc = ldap_result2error( ld, result, 1 );
202
203         if ( rc == LDAP_SUCCESS || rc == LDAP_SASL_BIND_IN_PROGRESS ) {
204                 if( servercredp != NULL ) {
205                         *servercredp = scredp;
206                         scredp = NULL;
207                 }
208         }
209
210         if ( scredp != NULL ) {
211                 ber_bvfree(scredp);
212         }
213
214         return rc;
215 }
216
217
218 /*
219 * Parse BindResponse:
220 *
221 *   BindResponse ::= [APPLICATION 1] SEQUENCE {
222 *     COMPONENTS OF LDAPResult,
223 *     serverSaslCreds  [7] OCTET STRING OPTIONAL }
224 *
225 *   LDAPResult ::= SEQUENCE {
226 *     resultCode      ENUMERATED,
227 *     matchedDN       LDAPDN,
228 *     errorMessage    LDAPString,
229 *     referral        [3] Referral OPTIONAL }
230 */
231
232 int
233 ldap_parse_sasl_bind_result(
234         LDAP                    *ld,
235         LDAPMessage             *res,
236         struct berval   **servercredp,
237         int                             freeit )
238 {
239         ber_int_t errcode;
240         struct berval* scred;
241
242         ber_tag_t tag;
243         BerElement      *ber;
244
245         Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
246
247         assert( ld != NULL );
248         assert( LDAP_VALID( ld ) );
249         assert( res != NULL );
250
251         if ( ld == NULL || res == NULL ) {
252                 return LDAP_PARAM_ERROR;
253         }
254
255         if( servercredp != NULL ) {
256                 if( ld->ld_version < LDAP_VERSION2 ) {
257                         return LDAP_NOT_SUPPORTED;
258                 }
259                 *servercredp = NULL;
260         }
261
262         if( res->lm_msgtype != LDAP_RES_BIND ) {
263                 ld->ld_errno = LDAP_PARAM_ERROR;
264                 return ld->ld_errno;
265         }
266
267         scred = NULL;
268
269         if ( ld->ld_error ) {
270                 LDAP_FREE( ld->ld_error );
271                 ld->ld_error = NULL;
272         }
273         if ( ld->ld_matched ) {
274                 LDAP_FREE( ld->ld_matched );
275                 ld->ld_matched = NULL;
276         }
277
278         /* parse results */
279
280         ber = ber_dup( res->lm_ber );
281
282         if( ber == NULL ) {
283                 ld->ld_errno = LDAP_NO_MEMORY;
284                 return ld->ld_errno;
285         }
286
287         if ( ld->ld_version < LDAP_VERSION2 ) {
288                 tag = ber_scanf( ber, "{ia}",
289                         &errcode, &ld->ld_error );
290
291                 if( tag == LBER_ERROR ) {
292                         ber_free( ber, 0 );
293                         ld->ld_errno = LDAP_DECODING_ERROR;
294                         return ld->ld_errno;
295                 }
296
297         } else {
298                 ber_len_t len;
299
300                 tag = ber_scanf( ber, "{iaa" /*}*/,
301                         &errcode, &ld->ld_matched, &ld->ld_error );
302
303                 if( tag == LBER_ERROR ) {
304                         ber_free( ber, 0 );
305                         ld->ld_errno = LDAP_DECODING_ERROR;
306                         return ld->ld_errno;
307                 }
308
309                 tag = ber_peek_tag(ber, &len);
310
311                 if( tag == LDAP_TAG_REFERRAL ) {
312                         /* skip 'em */
313                         if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
314                                 ber_free( ber, 0 );
315                                 ld->ld_errno = LDAP_DECODING_ERROR;
316                                 return ld->ld_errno;
317                         }
318
319                         tag = ber_peek_tag(ber, &len);
320                 }
321
322                 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
323                         if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
324                                 ber_free( ber, 0 );
325                                 ld->ld_errno = LDAP_DECODING_ERROR;
326                                 return ld->ld_errno;
327                         }
328                 }
329         }
330
331         ber_free( ber, 0 );
332
333         if ( servercredp != NULL ) {
334                 *servercredp = scred;
335
336         } else if ( scred != NULL ) {
337                 ber_bvfree( scred );
338         }
339
340         ld->ld_errno = errcode;
341
342         if ( freeit ) {
343                 ldap_msgfree( res );
344         }
345
346         return( ld->ld_errno );
347 }
348
349 int
350 ldap_pvt_sasl_getmechs ( LDAP *ld, char **pmechlist )
351 {
352         /* we need to query the server for supported mechs anyway */
353         LDAPMessage *res, *e;
354         char *attrs[] = { "supportedSASLMechanisms", NULL };
355         char **values, *mechlist;
356         int rc;
357
358         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_getmech\n", 0, 0, 0 );
359
360         rc = ldap_search_s( ld, "", LDAP_SCOPE_BASE,
361                 NULL, attrs, 0, &res );
362
363         if ( rc != LDAP_SUCCESS ) {
364                 return ld->ld_errno;
365         }
366                 
367         e = ldap_first_entry( ld, res );
368         if ( e == NULL ) {
369                 if ( ld->ld_errno == LDAP_SUCCESS ) {
370                         ld->ld_errno = LDAP_UNAVAILABLE;
371                 }
372                 return ld->ld_errno;
373         }
374
375         values = ldap_get_values( ld, e, "supportedSASLMechanisms" );
376         if ( values == NULL ) {
377                 ld->ld_errno = LDAP_NO_SUCH_ATTRIBUTE;
378                 ldap_msgfree( res );
379                 return ld->ld_errno;
380         }
381
382         mechlist = ldap_charray2str( values, " " );
383         if ( mechlist == NULL ) {
384                 ld->ld_errno = LDAP_NO_MEMORY;
385                 LDAP_VFREE( values );
386                 ldap_msgfree( res );
387                 return ld->ld_errno;
388         } 
389
390         LDAP_VFREE( values );
391         ldap_msgfree( res );
392
393         *pmechlist = mechlist;
394
395         return LDAP_SUCCESS;
396 }
397
398 /*
399  * ldap_sasl_interactive_bind_s - interactive SASL authentication
400  *
401  * This routine uses interactive callbacks.
402  *
403  * LDAP_SUCCESS is returned upon success, the ldap error code
404  * otherwise.
405  */
406 int
407 ldap_sasl_interactive_bind_s(
408         LDAP *ld,
409         LDAP_CONST char *dn, /* usually NULL */
410         LDAP_CONST char *mechs,
411         LDAPControl **serverControls,
412         LDAPControl **clientControls,
413         unsigned flags,
414         LDAP_SASL_INTERACT_PROC *interact,
415         void *defaults )
416 {
417         int rc;
418
419         if( mechs == NULL || *mechs == '\0' ) {
420                 char *smechs;
421
422                 rc = ldap_pvt_sasl_getmechs( ld, &smechs );
423
424                 if( rc != LDAP_SUCCESS ) {
425                         return rc;
426                 }
427
428                 Debug( LDAP_DEBUG_TRACE,
429                         "ldap_interactive_sasl_bind_s: server supports: %s\n",
430                         smechs, 0, 0 );
431
432                 mechs = smechs;
433
434         } else {
435                 Debug( LDAP_DEBUG_TRACE,
436                         "ldap_interactive_sasl_bind_s: user selected: %s\n",
437                         mechs, 0, 0 );
438         }
439
440         rc = ldap_int_sasl_bind( ld, dn, mechs,
441                 serverControls, clientControls,
442                 flags, interact, defaults );
443
444         return rc;
445 }