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