]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
ldap_url_parse was fundamentally broken. It would quit before
[openldap] / libraries / libldap / url.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1996 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  LIBLDAP url.c -- LDAP URL related routines
10  *
11  *  LDAP URLs look like this:
12  *    ldap[s]://host:port/dn[[?attributes[?scope[?filter[?extensions]]]]
13  *
14  *  where:
15  *   attributes is a comma separated list
16  *   scope is one of these three strings:  base one sub (default=base)
17  *   filter is an string-represented filter as in RFC 1558
18  *
19  *  e.g.,  ldap://host:port/dc=com?o,cn?base?o=openldap?extension
20  *
21  *  We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/stdlib.h>
29
30 #include <ac/ctype.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
33 #include <ac/time.h>
34
35 #include "ldap-int.h"
36
37
38 /* local functions */
39 static const char* skip_url_prefix LDAP_P((
40         const char *url,
41         int *enclosedp,
42         int *ldaps ));
43 static void hex_unescape LDAP_P(( char *s ));
44 static int unhex( char c );
45
46
47 int
48 ldap_is_ldap_url( LDAP_CONST char *url )
49 {
50         int     enclosed;
51         int ldaps;
52
53         if( url == NULL ) {
54                 return 0;
55         }
56
57         if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
58                 return 0;
59         }
60
61         return !ldaps;
62 }
63
64 int
65 ldap_is_ldaps_url( LDAP_CONST char *url )
66 {
67         int     enclosed;
68         int ldaps;
69
70         if( url == NULL ) {
71                 return 0;
72         }
73
74         if( skip_url_prefix( url, &enclosed, &ldaps) == NULL ) {
75                 return 0;
76         }
77
78         return ldaps;
79 }
80
81 static const char*
82 skip_url_prefix(
83         const char *url,
84         int *enclosedp,
85         int *ldaps )
86 {
87 /*
88  * return non-zero if this looks like a LDAP URL; zero if not
89  * if non-zero returned, *urlp will be moved past "ldap://" part of URL
90  */
91         char* p;
92
93         if ( url == NULL ) {
94                 return( NULL );
95         }
96
97         p = (char *) url;
98
99         /* skip leading '<' (if any) */
100         if ( *p == '<' ) {
101                 *enclosedp = 1;
102                 ++p;
103         } else {
104                 *enclosedp = 0;
105         }
106
107         /* skip leading "URL:" (if any) */
108         if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
109         {
110                 p += LDAP_URL_URLCOLON_LEN;
111         }
112
113         /* check for "ldap://" prefix */
114         if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
115                 /* skip over "ldap://" prefix and return success */
116                 p += LDAP_URL_PREFIX_LEN;
117                 *ldaps = 0;
118                 return( p );
119         }
120
121         /* check for "ldaps://" prefix */
122         if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
123                 /* skip over "ldaps://" prefix and return success */
124                 p += LDAPS_URL_PREFIX_LEN;
125                 *ldaps = 1;
126                 return( p );
127         }
128
129         return( NULL );
130 }
131
132
133 static int str2scope( const char *p )
134 {
135         if ( strcasecmp( p, "one" ) == 0 ) {
136                 return LDAP_SCOPE_ONELEVEL;
137
138         } else if ( strcasecmp( p, "onetree" ) == 0 ) {
139                 return LDAP_SCOPE_ONELEVEL;
140
141         } else if ( strcasecmp( p, "base" ) == 0 ) {
142                 return LDAP_SCOPE_BASE;
143
144         } else if ( strcasecmp( p, "sub" ) == 0 ) {
145                 return LDAP_SCOPE_SUBTREE;
146
147         } else if ( strcasecmp( p, "subtree" ) == 0 ) {
148                 return LDAP_SCOPE_SUBTREE;
149         }
150
151         return( -1 );
152 }
153
154
155 int
156 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
157 {
158 /*
159  *  Pick apart the pieces of an LDAP URL.
160  */
161
162         LDAPURLDesc     *ludp;
163         char    *p, *q;
164         int             i, enclosed, ldaps;
165         const char *url_tmp;
166         char *url;
167
168         if( url_in == NULL && ludpp == NULL ) {
169                 return LDAP_URL_ERR_PARAM;
170         }
171
172         Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
173
174         *ludpp = NULL;  /* pessimistic */
175
176         url_tmp = skip_url_prefix( url_in, &enclosed, &ldaps );
177
178         if ( url_tmp == NULL ) {
179                 return LDAP_URL_ERR_NOTLDAP;
180         }
181
182         /* make working copy of the remainder of the URL */
183         if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
184                 return( LDAP_URL_ERR_MEM );
185         }
186
187         if ( enclosed ) {
188                 p = &url[strlen(url)-1];
189
190                 if( *p != '>' ) {
191                         LDAP_FREE( url );
192                         return LDAP_URL_ERR_BADENCLOSURE;
193                 }
194
195                 *p = '\0';
196         }
197
198         /* allocate return struct */
199         ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
200
201         if ( ludp == NULL ) {
202                 LDAP_FREE( url );
203                 return LDAP_URL_ERR_MEM;
204         }
205
206         ludp->lud_host = NULL;
207         ludp->lud_port = 0;
208     ludp->lud_dn = NULL;
209     ludp->lud_attrs = NULL;
210     ludp->lud_filter = NULL;
211         ludp->lud_ldaps = ldaps;
212         ludp->lud_scope = LDAP_SCOPE_BASE;
213         ludp->lud_filter = LDAP_STRDUP("(objectClass=*)");
214
215         if( ludp->lud_filter == NULL ) {
216                 LDAP_FREE( url );
217                 ldap_free_urldesc( ludp );
218                 return LDAP_URL_ERR_MEM;
219         }
220
221         /* scan forward for '/' that marks end of hostport and begin. of dn */
222         p = strchr( url, '/' );
223
224         if( p == NULL ) {
225                 LDAP_FREE( url );
226                 ldap_free_urldesc( ludp );
227                 return LDAP_URL_ERR_BADURL;
228         }
229
230         /* terminate hostport; point to start of dn */
231         *p++ = '\0';
232
233         if (( q = strchr( url, ':' )) != NULL ) {
234                 *q++ = '\0';
235                 hex_unescape( q );
236
237                 if( *q == '\0' ) {
238                         LDAP_FREE( url );
239                         ldap_free_urldesc( ludp );
240                         return LDAP_URL_ERR_BADURL;
241                 }
242
243                 ludp->lud_port = atoi( q );
244         }
245
246         hex_unescape( url );
247         ludp->lud_host = LDAP_STRDUP( url );
248
249         if( ludp->lud_host == NULL ) {
250                 LDAP_FREE( url );
251                 ldap_free_urldesc( ludp );
252                 return LDAP_URL_ERR_MEM;
253         }
254
255         /* scan forward for '?' that may marks end of dn */
256         q = strchr( p, '?' );
257
258         if( q == NULL ) {
259                 /* no '?' */
260                 hex_unescape( p );
261                 ludp->lud_dn = LDAP_STRDUP( p );
262
263                 if( ludp->lud_dn == NULL ) {
264                         LDAP_FREE( url );
265                         ldap_free_urldesc( ludp );
266                         return LDAP_URL_ERR_MEM;
267                 }
268
269                 LDAP_FREE( url );
270                 *ludpp = ludp;
271                 return LDAP_URL_SUCCESS;
272         }
273
274         *q++ = '\0';
275         hex_unescape( p );
276         ludp->lud_dn = LDAP_STRDUP( p );
277
278         if( ludp->lud_dn == NULL ) {
279                 LDAP_FREE( url );
280                 ldap_free_urldesc( ludp );
281                 return LDAP_URL_ERR_MEM;
282         }
283
284         /* scan forward for '?' that may marks end of attributes */
285         p = q;
286         q = strchr( p, '?' );
287
288         if( q != NULL ) {
289                 *q++ = '\0';
290         }
291         if( *p != '\0' ) {
292                 hex_unescape( p );
293                 ludp->lud_attrs = ldap_str2charray( p, "," );
294
295                 if( ludp->lud_attrs == NULL ) {
296                         LDAP_FREE( url );
297                         ldap_free_urldesc( ludp );
298                         return LDAP_URL_ERR_BADATTRS;
299                 }
300         }
301         if ( q == NULL ) {
302                 /* no '?' */
303                 LDAP_FREE( url );
304                 *ludpp = ludp;
305                 return LDAP_URL_SUCCESS;
306         }
307
308         /* scan forward for '?' that may marks end of scope */
309         p = q;
310         q = strchr( p, '?' );
311
312         if( q != NULL ) {
313                 *q++ = '\0';
314         }
315         if( *p != '\0' ) {
316                 hex_unescape( p );
317                 ludp->lud_scope = str2scope( p );
318
319                 if( ludp->lud_scope == -1 ) {
320                         LDAP_FREE( url );
321                         ldap_free_urldesc( ludp );
322                         return LDAP_URL_ERR_BADSCOPE;
323                 }
324         }
325         if ( q == NULL ) {
326                 /* no '?' */
327                 LDAP_FREE( url );
328                 *ludpp = ludp;
329                 return LDAP_URL_SUCCESS;
330         }
331
332         /* scan forward for '?' that may marks end of filter */
333         p = q;
334         q = strchr( p, '?' );
335
336         if( q != NULL ) {
337                 *q++ = '\0';
338         }
339         if( *p != '\0' ) {
340                 hex_unescape( p );
341
342                 if( ! *p ) {
343                         /* missing filter */
344                         LDAP_FREE( url );
345                         ldap_free_urldesc( ludp );
346                         return LDAP_URL_ERR_BADFILTER;
347                 }
348
349                 ludp->lud_filter = LDAP_STRDUP( p );
350
351                 if( ludp->lud_filter == NULL ) {
352                         LDAP_FREE( url );
353                         ldap_free_urldesc( ludp );
354                         return LDAP_URL_ERR_MEM;
355                 }
356         }
357         if ( q == NULL ) {
358                 /* no '?' */
359                 LDAP_FREE( url );
360                 *ludpp = ludp;
361                 return LDAP_URL_SUCCESS;
362         }
363
364         /* scan forward for '?' that may marks end of extensions */
365         p = q;
366         q = strchr( p, '?' );
367
368         if( q != NULL ) {
369                 /* extra '?' */
370                 LDAP_FREE( url );
371                 ldap_free_urldesc( ludp );
372                 return LDAP_URL_ERR_BADURL;
373         }
374
375         ludp->lud_exts = ldap_str2charray( p, "," );
376
377         if( ludp->lud_exts == NULL ) {
378                 LDAP_FREE( url );
379                 ldap_free_urldesc( ludp );
380                 return LDAP_URL_ERR_BADEXTS;
381         }
382
383         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
384                 hex_unescape( ludp->lud_exts[i] );
385         }
386
387         if( i == 0 ) {
388                 ldap_charray_free( ludp->lud_exts );
389                 LDAP_FREE( url );
390                 ldap_free_urldesc( ludp );
391                 return LDAP_URL_ERR_BADEXTS;
392         }
393
394         *ludpp = ludp;
395
396         LDAP_FREE( url );
397         return LDAP_URL_SUCCESS;
398 }
399
400
401 void
402 ldap_free_urldesc( LDAPURLDesc *ludp )
403 {
404         if ( ludp == NULL ) {
405                 return;
406         }
407         
408         if ( ludp->lud_host != NULL ) {
409                 LDAP_FREE( ludp->lud_host );
410         }
411
412         if ( ludp->lud_dn != NULL ) {
413                 LDAP_FREE( ludp->lud_dn );
414         }
415
416         if ( ludp->lud_filter != NULL ) {
417                 LDAP_FREE( ludp->lud_filter);
418         }
419
420         if ( ludp->lud_attrs != NULL ) {
421                 LDAP_VFREE( ludp->lud_attrs );
422         }
423
424         if ( ludp->lud_exts != NULL ) {
425                 LDAP_VFREE( ludp->lud_exts );
426         }
427
428         LDAP_FREE( ludp );
429 }
430
431
432
433 int
434 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
435 {
436         int             err;
437         LDAPURLDesc     *ludp;
438         BerElement      *ber;
439         LDAPServer      *srv = NULL;
440
441         if ( ldap_url_parse( url, &ludp ) != 0 ) {
442                 ld->ld_errno = LDAP_PARAM_ERROR;
443                 return( -1 );
444         }
445
446         ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
447             ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
448                 -1, -1 );
449
450         if ( ber == NULL ) {
451                 return( -1 );
452         }
453
454         err = 0;
455
456         if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
457                 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer )))
458                     == NULL || ( srv->lsrv_host = LDAP_STRDUP( ludp->lud_host ==
459                     NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
460                         if ( srv != NULL ) {
461                                 LDAP_FREE( srv );
462                         }
463                         ld->ld_errno = LDAP_NO_MEMORY;
464                         err = -1;
465                 } else {
466                         if ( ludp->lud_port == 0 ) {
467                                 srv->lsrv_port = ldap_int_global_options.ldo_defport;
468                         } else {
469                                 srv->lsrv_port = ludp->lud_port;
470                         }
471                 }
472         }
473
474         if ( err != 0 ) {
475                 ber_free( ber, 1 );
476         } else {
477                 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
478                     NULL, 1 );
479         }
480
481         ldap_free_urldesc( ludp );
482
483         return( err );
484 }
485
486
487 int
488 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
489         struct timeval *timeout, LDAPMessage **res )
490 {
491         int     msgid;
492
493         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
494                 return( ld->ld_errno );
495         }
496
497         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
498                 return( ld->ld_errno );
499         }
500
501         if ( ld->ld_errno == LDAP_TIMEOUT ) {
502                 (void) ldap_abandon( ld, msgid );
503                 ld->ld_errno = LDAP_TIMEOUT;
504                 return( ld->ld_errno );
505         }
506
507         return( ldap_result2error( ld, *res, 0 ));
508 }
509
510
511 int
512 ldap_url_search_s(
513         LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
514 {
515         int     msgid;
516
517         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
518                 return( ld->ld_errno );
519         }
520
521         if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
522                 return( ld->ld_errno );
523         }
524
525         return( ldap_result2error( ld, *res, 0 ));
526 }
527
528
529 static void
530 hex_unescape( char *s )
531 {
532 /*
533  * Remove URL hex escapes from s... done in place.  The basic concept for
534  * this routine is borrowed from the WWW library HTUnEscape() routine.
535  */
536         char    *p;
537
538         for ( p = s; *s != '\0'; ++s ) {
539                 if ( *s == '%' ) {
540                         if ( *++s != '\0' ) {
541                                 *p = unhex( *s ) << 4;
542                         }
543                         if ( *++s != '\0' ) {
544                                 *p++ += unhex( *s );
545                         }
546                 } else {
547                         *p++ = *s;
548                 }
549         }
550
551         *p = '\0';
552 }
553
554
555 static int
556 unhex( char c )
557 {
558         return( c >= '0' && c <= '9' ? c - '0'
559             : c >= 'A' && c <= 'F' ? c - 'A' + 10
560             : c - 'a' + 10 );
561 }