]> git.sur5r.net Git - openldap/blob - libraries/libldap/sasl.c
Fix decoding of sasl bind and extended operation responses.
[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                 && ld->ld_version < LDAP_VERSION3)
75         {
76                 ld->ld_errno = LDAP_NOT_SUPPORTED;
77                 return ld->ld_errno;
78         }
79
80         if ( dn == NULL )
81                 dn = "";
82
83         /* create a message to send */
84         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
85                 ld->ld_errno = LDAP_NO_MEMORY;
86                 return ld->ld_errno;
87         }
88
89         assert( BER_VALID( ber ) );
90
91         if( mechanism == LDAP_SASL_SIMPLE ) {
92                 /* simple bind */
93                 rc = ber_printf( ber, "{it{istO}" /*}*/,
94                         ++ld->ld_msgid, LDAP_REQ_BIND,
95                         ld->ld_version, dn, LDAP_AUTH_SIMPLE,
96                         cred );
97                 
98         } else if ( cred == NULL ) {
99                 /* SASL bind w/o creditials */
100                 rc = ber_printf( ber, "{it{ist{s}}" /*}*/,
101                         ++ld->ld_msgid, LDAP_REQ_BIND,
102                         ld->ld_version, dn, LDAP_AUTH_SASL,
103                         mechanism );
104
105         } else {
106                 /* SASL bind w/ creditials */
107                 rc = ber_printf( ber, "{it{ist{sO}}" /*}*/,
108                         ++ld->ld_msgid, LDAP_REQ_BIND,
109                         ld->ld_version, dn, LDAP_AUTH_SASL,
110                         mechanism, cred );
111         }
112
113         if( rc == -1 ) {
114                 ld->ld_errno = LDAP_ENCODING_ERROR;
115                 ber_free( ber, 1 );
116                 return( -1 );
117         }
118
119         /* Put Server Controls */
120         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
121                 ber_free( ber, 1 );
122                 return ld->ld_errno;
123         }
124
125         if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
126                 ld->ld_errno = LDAP_ENCODING_ERROR;
127                 ber_free( ber, 1 );
128                 return ld->ld_errno;
129         }
130
131 #ifndef LDAP_NOCACHE
132         if ( ld->ld_cache != NULL ) {
133                 ldap_flush_cache( ld );
134         }
135 #endif /* !LDAP_NOCACHE */
136
137         /* send the message */
138         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
139
140         if(*msgidp < 0)
141                 return ld->ld_errno;
142
143         return LDAP_SUCCESS;
144 }
145
146 /*
147  * ldap_sasl_bind_s - bind to the ldap server (and X.500) using simple
148  * authentication.  The dn and password of the entry to which to bind are
149  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
150  * otherwise.
151  *
152  * Example:
153  *      ldap_sasl_bind_s( ld, "cn=manager, o=university of michigan, c=us",
154  *          "mechanism", "secret", NULL, NULL, &servercred )
155  */
156
157 int
158 ldap_sasl_bind_s(
159         LDAP                    *ld,
160         LDAP_CONST char *dn,
161         LDAP_CONST char *mechanism,
162         struct berval   *cred,
163         LDAPControl             **sctrls,
164         LDAPControl             **cctrls,
165         struct berval   **servercredp )
166 {
167         int     rc, msgid;
168         LDAPMessage     *result;
169         struct berval   *scredp = NULL;
170
171         Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind_s\n", 0, 0, 0 );
172
173         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
174         if( servercredp != NULL ) {
175                 if (ld->ld_version < LDAP_VERSION3) {
176                         ld->ld_errno = LDAP_NOT_SUPPORTED;
177                         return ld->ld_errno;
178                 }
179                 *servercredp = NULL;
180         }
181
182         rc = ldap_sasl_bind( ld, dn, mechanism, cred, sctrls, cctrls, &msgid );
183
184         if ( rc != LDAP_SUCCESS ) {
185                 return( rc );
186         }
187
188         if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
189                 return( ld->ld_errno ); /* ldap_result sets ld_errno */
190         }
191
192         /* parse the results */
193         scredp = NULL;
194         if( servercredp != NULL ) {
195                 rc = ldap_parse_sasl_bind_result( ld, result, &scredp, 0 );
196         }
197
198         if( rc != LDAP_SUCCESS ) {
199                 ldap_msgfree( result );
200                 return( rc );
201         }
202
203         rc = ldap_result2error( ld, result, 1 );
204
205         if( rc == LDAP_SUCCESS ) {
206                 if( servercredp != NULL ) {
207                         *servercredp = scredp;
208                 }
209
210         } else 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 }