]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
Fix leak of default filter.
[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 (RFC 2255) related routines
10  *
11  *  LDAP URLs look like this:
12  *    ldap[s]://host:port[/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
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 2254
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
214         ludp->lud_filter = LDAP_STRDUP("(objectClass=*)");
215
216         if( ludp->lud_filter == NULL ) {
217                 LDAP_FREE( url );
218                 ldap_free_urldesc( ludp );
219                 return LDAP_URL_ERR_MEM;
220         }
221
222         /* scan forward for '/' that marks end of hostport and begin. of dn */
223         p = strchr( url, '/' );
224
225         if( p != NULL ) {
226                 /* terminate hostport; point to start of dn */
227                 *p++ = '\0';
228         }
229
230         if (( q = strchr( url, ':' )) != NULL ) {
231                 *q++ = '\0';
232                 hex_unescape( q );
233
234                 if( *q == '\0' ) {
235                         LDAP_FREE( url );
236                         ldap_free_urldesc( ludp );
237                         return LDAP_URL_ERR_BADURL;
238                 }
239
240                 ludp->lud_port = atoi( q );
241         }
242
243         hex_unescape( url );
244         ludp->lud_host = LDAP_STRDUP( url );
245
246         if( ludp->lud_host == NULL ) {
247                 LDAP_FREE( url );
248                 ldap_free_urldesc( ludp );
249                 return LDAP_URL_ERR_MEM;
250         }
251
252         if( p == NULL ) {
253                 LDAP_FREE( url );
254                 *ludpp = ludp;
255                 return LDAP_URL_SUCCESS;
256         }
257
258         /* scan forward for '?' that may marks end of dn */
259         q = strchr( p, '?' );
260
261         if( q != NULL ) {
262                 /* terminate dn part */
263                 *q++ = '\0';
264         }
265
266         if( *p != '\0' ) {
267                 /* parse dn part */
268                 hex_unescape( p );
269                 ludp->lud_dn = LDAP_STRDUP( p );
270         } else {
271                 ludp->lud_dn = LDAP_STRDUP( "" );
272         }
273
274         if( ludp->lud_dn == NULL ) {
275                 LDAP_FREE( url );
276                 ldap_free_urldesc( ludp );
277                 return LDAP_URL_ERR_MEM;
278         }
279
280         if( q == NULL ) {
281                 /* no more */
282                 LDAP_FREE( url );
283                 *ludpp = ludp;
284                 return LDAP_URL_SUCCESS;
285         }
286
287         /* scan forward for '?' that may marks end of attributes */
288         p = q;
289         q = strchr( p, '?' );
290
291         if( q != NULL ) {
292                 /* terminate attributes part */
293                 *q++ = '\0';
294         }
295
296         if( *p != '\0' ) {
297                 /* parse attributes */
298                 hex_unescape( p );
299                 ludp->lud_attrs = ldap_str2charray( p, "," );
300
301                 if( ludp->lud_attrs == NULL ) {
302                         LDAP_FREE( url );
303                         ldap_free_urldesc( ludp );
304                         return LDAP_URL_ERR_BADATTRS;
305                 }
306         }
307
308         if ( q == NULL ) {
309                 /* no more */
310                 LDAP_FREE( url );
311                 *ludpp = ludp;
312                 return LDAP_URL_SUCCESS;
313         }
314
315         /* scan forward for '?' that may marks end of scope */
316         p = q;
317         q = strchr( p, '?' );
318
319         if( q != NULL ) {
320                 /* terminate the scope part */
321                 *q++ = '\0';
322         }
323
324         if( *p != '\0' ) {
325                 /* parse the scope */
326                 hex_unescape( p );
327                 ludp->lud_scope = str2scope( p );
328
329                 if( ludp->lud_scope == -1 ) {
330                         LDAP_FREE( url );
331                         ldap_free_urldesc( ludp );
332                         return LDAP_URL_ERR_BADSCOPE;
333                 }
334         }
335
336         if ( q == NULL ) {
337                 /* no more */
338                 LDAP_FREE( url );
339                 *ludpp = ludp;
340                 return LDAP_URL_SUCCESS;
341         }
342
343         /* scan forward for '?' that may marks end of filter */
344         p = q;
345         q = strchr( p, '?' );
346
347         if( q != NULL ) {
348                 /* terminate the filter part */
349                 *q++ = '\0';
350         }
351
352         if( *p != '\0' ) {
353                 /* parse the filter */
354                 hex_unescape( p );
355
356                 if( ! *p ) {
357                         /* missing filter */
358                         LDAP_FREE( url );
359                         ldap_free_urldesc( ludp );
360                         return LDAP_URL_ERR_BADFILTER;
361                 }
362
363                 LDAP_FREE( ludp->lud_filter );
364                 ludp->lud_filter = LDAP_STRDUP( p );
365
366                 if( ludp->lud_filter == NULL ) {
367                         LDAP_FREE( url );
368                         ldap_free_urldesc( ludp );
369                         return LDAP_URL_ERR_MEM;
370                 }
371         }
372
373         if ( q == NULL ) {
374                 /* no more */
375                 LDAP_FREE( url );
376                 *ludpp = ludp;
377                 return LDAP_URL_SUCCESS;
378         }
379
380         /* scan forward for '?' that may marks end of extensions */
381         p = q;
382         q = strchr( p, '?' );
383
384         if( q != NULL ) {
385                 /* extra '?' */
386                 LDAP_FREE( url );
387                 ldap_free_urldesc( ludp );
388                 return LDAP_URL_ERR_BADURL;
389         }
390
391         /* parse the extensions */
392         ludp->lud_exts = ldap_str2charray( p, "," );
393
394         if( ludp->lud_exts == NULL ) {
395                 LDAP_FREE( url );
396                 ldap_free_urldesc( ludp );
397                 return LDAP_URL_ERR_BADEXTS;
398         }
399
400         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
401                 hex_unescape( ludp->lud_exts[i] );
402         }
403
404         if( i == 0 ) {
405                 /* must have 1 or more */
406                 ldap_charray_free( ludp->lud_exts );
407                 LDAP_FREE( url );
408                 ldap_free_urldesc( ludp );
409                 return LDAP_URL_ERR_BADEXTS;
410         }
411
412         /* no more */
413         *ludpp = ludp;
414         LDAP_FREE( url );
415         return LDAP_URL_SUCCESS;
416 }
417
418
419 void
420 ldap_free_urldesc( LDAPURLDesc *ludp )
421 {
422         if ( ludp == NULL ) {
423                 return;
424         }
425         
426         if ( ludp->lud_host != NULL ) {
427                 LDAP_FREE( ludp->lud_host );
428         }
429
430         if ( ludp->lud_dn != NULL ) {
431                 LDAP_FREE( ludp->lud_dn );
432         }
433
434         if ( ludp->lud_filter != NULL ) {
435                 LDAP_FREE( ludp->lud_filter);
436         }
437
438         if ( ludp->lud_attrs != NULL ) {
439                 LDAP_VFREE( ludp->lud_attrs );
440         }
441
442         if ( ludp->lud_exts != NULL ) {
443                 LDAP_VFREE( ludp->lud_exts );
444         }
445
446         LDAP_FREE( ludp );
447 }
448
449
450
451 int
452 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
453 {
454         int             err;
455         LDAPURLDesc     *ludp;
456         BerElement      *ber;
457         LDAPServer      *srv = NULL;
458
459         if ( ldap_url_parse( url, &ludp ) != 0 ) {
460                 ld->ld_errno = LDAP_PARAM_ERROR;
461                 return( -1 );
462         }
463
464         ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
465             ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
466                 -1, -1 );
467
468         if ( ber == NULL ) {
469                 return( -1 );
470         }
471
472         err = 0;
473
474         if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
475                 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer )))
476                     == NULL || ( srv->lsrv_host = LDAP_STRDUP( ludp->lud_host ==
477                     NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
478                         if ( srv != NULL ) {
479                                 LDAP_FREE( srv );
480                         }
481                         ld->ld_errno = LDAP_NO_MEMORY;
482                         err = -1;
483                 } else {
484                         if ( ludp->lud_port == 0 ) {
485                                 srv->lsrv_port = ldap_int_global_options.ldo_defport;
486                         } else {
487                                 srv->lsrv_port = ludp->lud_port;
488                         }
489                 }
490         }
491
492         if ( err != 0 ) {
493                 ber_free( ber, 1 );
494         } else {
495                 err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
496                     NULL, 1 );
497         }
498
499         ldap_free_urldesc( ludp );
500
501         return( err );
502 }
503
504
505 int
506 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
507         struct timeval *timeout, LDAPMessage **res )
508 {
509         int     msgid;
510
511         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
512                 return( ld->ld_errno );
513         }
514
515         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
516                 return( ld->ld_errno );
517         }
518
519         if ( ld->ld_errno == LDAP_TIMEOUT ) {
520                 (void) ldap_abandon( ld, msgid );
521                 ld->ld_errno = LDAP_TIMEOUT;
522                 return( ld->ld_errno );
523         }
524
525         return( ldap_result2error( ld, *res, 0 ));
526 }
527
528
529 int
530 ldap_url_search_s(
531         LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
532 {
533         int     msgid;
534
535         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
536                 return( ld->ld_errno );
537         }
538
539         if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
540                 return( ld->ld_errno );
541         }
542
543         return( ldap_result2error( ld, *res, 0 ));
544 }
545
546
547 static void
548 hex_unescape( char *s )
549 {
550 /*
551  * Remove URL hex escapes from s... done in place.  The basic concept for
552  * this routine is borrowed from the WWW library HTUnEscape() routine.
553  */
554         char    *p;
555
556         for ( p = s; *s != '\0'; ++s ) {
557                 if ( *s == '%' ) {
558                         if ( *++s != '\0' ) {
559                                 *p = unhex( *s ) << 4;
560                         }
561                         if ( *++s != '\0' ) {
562                                 *p++ += unhex( *s );
563                         }
564                 } else {
565                         *p++ = *s;
566                 }
567         }
568
569         *p = '\0';
570 }
571
572
573 static int
574 unhex( char c )
575 {
576         return( c >= '0' && c <= '9' ? c - '0'
577             : c >= 'A' && c <= 'F' ? c - 'A' + 10
578             : c - 'a' + 10 );
579 }