]> git.sur5r.net Git - openldap/blob - libraries/libldap/sasl.c
28e36768b39b3db712ae912332def7a931ae3a57
[openldap] / libraries / libldap / sasl.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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 HAVE_KERBEROS
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 <stdio.h>
31
32 #include <ac/socket.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
35
36 #include "ldap-int.h"
37
38
39 /*
40  * ldap_sasl_bind - bind to the ldap server (and X.500).  The dn, mechanism, and
41  * credentials of the entry to which to bind are supplied.  The message id
42  * of the request initiated is provided upon successful (LDAP_SUCCESS) return.
43  *
44  * Example:
45  *      ldap_sasl_bind( ld, "cn=manager, o=university of michigan, c=us",
46  *          "mechanism", "secret", NULL, NULL, &msgid )
47  */
48
49 int
50 ldap_sasl_bind(
51         LDAP                    *ld,
52         LDAP_CONST char *dn,
53         LDAP_CONST char *mechanism,
54         struct berval   *cred,
55         LDAPControl             **sctrls,
56         LDAPControl             **cctrls,
57         int                             *msgidp )
58 {
59         BerElement      *ber;
60         int rc;
61
62         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
63
64         assert( ld != NULL );
65         assert( LDAP_VALID( ld ) );
66         assert( msgidp != NULL );
67
68         if( msgidp == NULL ) {
69                 ld->ld_errno = LDAP_PARAM_ERROR;
70                 return ld->ld_errno;
71         }
72
73         if( mechanism == LDAP_SASL_SIMPLE ) {
74                 if( dn == NULL && cred != NULL ) {
75                         /* use default binddn */
76                         dn = ld->ld_defbinddn;
77                 }
78
79         } else if( ld->ld_version < LDAP_VERSION3 ) {
80                 ld->ld_errno = LDAP_NOT_SUPPORTED;
81                 return ld->ld_errno;
82         }
83
84         if ( dn == NULL ) {
85                 dn = "";
86         }
87
88         /* create a message to send */
89         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
90                 ld->ld_errno = LDAP_NO_MEMORY;
91                 return ld->ld_errno;
92         }
93
94         assert( BER_VALID( ber ) );
95
96         if( mechanism == LDAP_SASL_SIMPLE ) {
97                 /* simple bind */
98                 rc = ber_printf( ber, "{it{istO}" /*}*/,
99                         ++ld->ld_msgid, LDAP_REQ_BIND,
100                         ld->ld_version, dn, LDAP_AUTH_SIMPLE,
101                         cred );
102                 
103         } else if ( cred == NULL ) {
104                 /* SASL bind w/o creditials */
105                 rc = ber_printf( ber, "{it{ist{s}}" /*}*/,
106                         ++ld->ld_msgid, LDAP_REQ_BIND,
107                         ld->ld_version, dn, LDAP_AUTH_SASL,
108                         mechanism );
109
110         } else {
111                 /* SASL bind w/ creditials */
112                 rc = ber_printf( ber, "{it{ist{sO}}" /*}*/,
113                         ++ld->ld_msgid, LDAP_REQ_BIND,
114                         ld->ld_version, dn, LDAP_AUTH_SASL,
115                         mechanism, cred );
116         }
117
118         if( rc == -1 ) {
119                 ld->ld_errno = LDAP_ENCODING_ERROR;
120                 ber_free( ber, 1 );
121                 return( -1 );
122         }
123
124         /* Put Server Controls */
125         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
126                 ber_free( ber, 1 );
127                 return ld->ld_errno;
128         }
129
130         if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
131                 ld->ld_errno = LDAP_ENCODING_ERROR;
132                 ber_free( ber, 1 );
133                 return ld->ld_errno;
134         }
135
136 #ifndef LDAP_NOCACHE
137         if ( ld->ld_cache != NULL ) {
138                 ldap_flush_cache( ld );
139         }
140 #endif /* !LDAP_NOCACHE */
141
142         /* send the message */
143         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
144
145         if(*msgidp < 0)
146                 return ld->ld_errno;
147
148         return LDAP_SUCCESS;
149 }
150
151 /*
152  * ldap_sasl_bind_s - bind to the ldap server (and X.500) using simple
153  * authentication.  The dn and password of the entry to which to bind are
154  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
155  * otherwise.
156  *
157  * Example:
158  *      ldap_sasl_bind_s( ld, "cn=manager, o=university of michigan, c=us",
159  *          "mechanism", "secret", NULL, NULL, &servercred )
160  */
161
162 int
163 ldap_sasl_bind_s(
164         LDAP                    *ld,
165         LDAP_CONST char *dn,
166         LDAP_CONST char *mechanism,
167         struct berval   *cred,
168         LDAPControl             **sctrls,
169         LDAPControl             **cctrls,
170         struct berval   **servercredp )
171 {
172         int     rc, msgid;
173         LDAPMessage     *result;
174         struct berval   *scredp = NULL;
175
176         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
177
178         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
179         if( servercredp != NULL ) {
180                 if (ld->ld_version < LDAP_VERSION3) {
181                         ld->ld_errno = LDAP_NOT_SUPPORTED;
182                         return ld->ld_errno;
183                 }
184                 *servercredp = NULL;
185         }
186
187         rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
188
189         if ( rc != LDAP_SUCCESS ) {
190                 return( rc );
191         }
192
193         if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
194                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
195         }
196
197         /* parse the results */
198         scredp = NULL;
199         if( servercredp != NULL ) {
200                 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
201         }
202
203         if( rc != LDAP_SUCCESS ) {
204                 ldap_msgfree( result );
205                 return( rc );
206         }
207
208         rc = ldap_result2error( ld, result, 1 );
209
210         if( rc == LDAP_SUCCESS ) {
211                 if( servercredp != NULL ) {
212                         *servercredp = scredp;
213                 }
214
215         } else if (scredp != NULL ) {
216                 ber_bvfree(scredp);
217         }
218
219         return rc;
220 }
221
222
223 /*
224  * Parse BindResponse:
225  *
226  *   BindResponse ::= [APPLICATION 1] SEQUENCE {
227  *     COMPONENTS OF LDAPResult,
228  *     serverSaslCreds  [7] OCTET STRING OPTIONAL }
229  *
230  *   LDAPResult ::= SEQUENCE {
231  *     resultCode      ENUMERATED,
232  *     matchedDN       LDAPDN,
233  *     errorMessage    LDAPString,
234  *     referral        [3] Referral OPTIONAL }
235  */
236
237 int
238 ldap_parse_sasl_bind_result(
239         LDAP                    *ld,
240         LDAPMessage             *res,
241         struct berval   **servercredp,
242         int                             freeit )
243 {
244         ber_int_t errcode;
245         struct berval* scred;
246
247         ber_tag_t tag;
248         BerElement      *ber;
249
250         Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
251
252         assert( ld != NULL );
253         assert( LDAP_VALID( ld ) );
254         assert( res != NULL );
255
256         if ( ld == NULL || res == NULL ) {
257                 return LDAP_PARAM_ERROR;
258         }
259
260         if( servercredp != NULL ) {
261                 if( ld->ld_version < LDAP_VERSION2 ) {
262                         return LDAP_NOT_SUPPORTED;
263                 }
264                 *servercredp = NULL;
265         }
266
267         if( res->lm_msgtype != LDAP_RES_BIND ) {
268                 ld->ld_errno = LDAP_PARAM_ERROR;
269                 return ld->ld_errno;
270         }
271
272         scred = NULL;
273
274         if ( ld->ld_error ) {
275                 LDAP_FREE( ld->ld_error );
276                 ld->ld_error = NULL;
277         }
278         if ( ld->ld_matched ) {
279                 LDAP_FREE( ld->ld_matched );
280                 ld->ld_matched = NULL;
281         }
282
283         /* parse results */
284
285         ber = ber_dup( res->lm_ber );
286
287         if( ber == NULL ) {
288                 ld->ld_errno = LDAP_NO_MEMORY;
289                 return ld->ld_errno;
290         }
291
292         if ( ld->ld_version < LDAP_VERSION2 ) {
293                 tag = ber_scanf( ber, "{ia}",
294                         &errcode, &ld->ld_error );
295
296                 if( tag == LBER_ERROR ) {
297                         ber_free( ber, 0 );
298                         ld->ld_errno = LDAP_DECODING_ERROR;
299                         return ld->ld_errno;
300                 }
301
302         } else {
303                 ber_len_t len;
304
305                 tag = ber_scanf( ber, "{iaa" /*}*/,
306                         &errcode, &ld->ld_matched, &ld->ld_error );
307
308                 if( tag == LBER_ERROR ) {
309                         ber_free( ber, 0 );
310                         ld->ld_errno = LDAP_DECODING_ERROR;
311                         return ld->ld_errno;
312                 }
313
314                 tag = ber_peek_tag(ber, &len);
315
316                 if( tag == LDAP_TAG_REFERRAL ) {
317                         /* skip 'em */
318                         if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
319                                 ber_free( ber, 0 );
320                                 ld->ld_errno = LDAP_DECODING_ERROR;
321                                 return ld->ld_errno;
322                         }
323
324                         tag = ber_peek_tag(ber, &len);
325                 }
326
327                 if( tag == LDAP_TAG_SASL_RES_CREDS ) {
328                         if( ber_scanf( ber, "O", &scred ) == LBER_ERROR ) {
329                                 ber_free( ber, 0 );
330                                 ld->ld_errno = LDAP_DECODING_ERROR;
331                                 return ld->ld_errno;
332                         }
333                 }
334         }
335
336         ber_free( ber, 0 );
337
338         if ( servercredp != NULL ) {
339                 *servercredp = scred;
340
341         } else if ( scred != NULL ) {
342                 ber_bvfree( scred );
343         }
344
345         ld->ld_errno = errcode;
346
347         if ( freeit ) {
348                 ldap_msgfree( res );
349         }
350
351         return( ld->ld_errno );
352 }