]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
Fix off by one bug
[openldap] / libraries / libldap / url.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1996 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  LIBLDAP url.c -- LDAP URL (RFC 2255) related routines
11  *
12  *  LDAP URLs look like this:
13  *    ldap[is]://host:port[/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
14  *
15  *  where:
16  *   attributes is a comma separated list
17  *   scope is one of these three strings:  base one sub (default=base)
18  *   filter is an string-represented filter as in RFC 2254
19  *
20  *  e.g.,  ldap://host:port/dc=com?o,cn?base?o=openldap?extension
21  *
22  *  We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/stdlib.h>
30
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         const char **scheme ));
43
44 int ldap_pvt_url_scheme2proto( const char *scheme )
45 {
46         assert( scheme );
47
48         if( scheme == NULL ) {
49                 return -1;
50         }
51
52         if( strcmp("ldap", scheme) == 0 ) {
53                 return LDAP_PROTO_TCP;
54         }
55
56         if( strcmp("ldapi", scheme) == 0 ) {
57                 return LDAP_PROTO_IPC;
58         }
59
60         if( strcmp("ldaps", scheme) == 0 ) {
61                 return LDAP_PROTO_TCP;
62         }
63
64         return -1;
65 }
66
67 int ldap_pvt_url_scheme2tls( const char *scheme )
68 {
69         assert( scheme );
70
71         if( scheme == NULL ) {
72                 return -1;
73         }
74
75         return strcmp("ldaps", scheme) == 0;
76 }
77
78 int
79 ldap_is_ldap_url( LDAP_CONST char *url )
80 {
81         int     enclosed;
82         const char * scheme;
83
84         if( url == NULL ) {
85                 return 0;
86         }
87
88         if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
89                 return 0;
90         }
91
92         return 1;
93 }
94
95 int
96 ldap_is_ldaps_url( LDAP_CONST char *url )
97 {
98         int     enclosed;
99         const char * scheme;
100
101         if( url == NULL ) {
102                 return 0;
103         }
104
105         if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
106                 return 0;
107         }
108
109         return strcmp(scheme, "ldaps") == 0;
110 }
111
112 int
113 ldap_is_ldapi_url( LDAP_CONST char *url )
114 {
115         int     enclosed;
116         const char * scheme;
117
118         if( url == NULL ) {
119                 return 0;
120         }
121
122         if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
123                 return 0;
124         }
125
126         return strcmp(scheme, "ldapi") == 0;
127 }
128
129 static const char*
130 skip_url_prefix(
131         const char *url,
132         int *enclosedp,
133         const char **scheme )
134 {
135 /*
136  * return non-zero if this looks like a LDAP URL; zero if not
137  * if non-zero returned, *urlp will be moved past "ldap://" part of URL
138  */
139         const char *p;
140
141         if ( url == NULL ) {
142                 return( NULL );
143         }
144
145         p = url;
146
147         /* skip leading '<' (if any) */
148         if ( *p == '<' ) {
149                 *enclosedp = 1;
150                 ++p;
151         } else {
152                 *enclosedp = 0;
153         }
154
155         /* skip leading "URL:" (if any) */
156         if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
157                 p += LDAP_URL_URLCOLON_LEN;
158         }
159
160         /* check for "ldap://" prefix */
161         if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
162                 /* skip over "ldap://" prefix and return success */
163                 p += LDAP_URL_PREFIX_LEN;
164                 *scheme = "ldap";
165                 return( p );
166         }
167
168         /* check for "ldaps://" prefix */
169         if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
170                 /* skip over "ldaps://" prefix and return success */
171                 p += LDAPS_URL_PREFIX_LEN;
172                 *scheme = "ldaps";
173                 return( p );
174         }
175
176         /* check for "ldapi://" prefix */
177         if ( strncasecmp( p, LDAPI_URL_PREFIX, LDAPI_URL_PREFIX_LEN ) == 0 ) {
178                 /* skip over "ldapi://" prefix and return success */
179                 p += LDAPI_URL_PREFIX_LEN;
180                 *scheme = "ldapi";
181                 return( p );
182         }
183
184         return( NULL );
185 }
186
187
188 static int str2scope( const char *p )
189 {
190         if ( strcasecmp( p, "one" ) == 0 ) {
191                 return LDAP_SCOPE_ONELEVEL;
192
193         } else if ( strcasecmp( p, "onetree" ) == 0 ) {
194                 return LDAP_SCOPE_ONELEVEL;
195
196         } else if ( strcasecmp( p, "base" ) == 0 ) {
197                 return LDAP_SCOPE_BASE;
198
199         } else if ( strcasecmp( p, "sub" ) == 0 ) {
200                 return LDAP_SCOPE_SUBTREE;
201
202         } else if ( strcasecmp( p, "subtree" ) == 0 ) {
203                 return LDAP_SCOPE_SUBTREE;
204         }
205
206         return( -1 );
207 }
208
209
210 int
211 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
212 {
213 /*
214  *  Pick apart the pieces of an LDAP URL.
215  */
216
217         LDAPURLDesc     *ludp;
218         char    *p, *q, *r;
219         int             i, enclosed;
220         const char *scheme = NULL;
221         const char *url_tmp;
222         char *url;
223
224         if( url_in == NULL && ludpp == NULL ) {
225                 return LDAP_URL_ERR_PARAM;
226         }
227
228 #ifndef LDAP_INT_IN_KERNEL
229         /* Global options may not be created yet
230          * We can't test if the global options are initialized
231          * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
232          * the options and cause infinite recursion
233          */
234         Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
235 #endif
236
237         *ludpp = NULL;  /* pessimistic */
238
239         url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
240
241         if ( url_tmp == NULL ) {
242                 return LDAP_URL_ERR_BADSCHEME;
243         }
244
245         assert( scheme );
246
247         /* make working copy of the remainder of the URL */
248         url = LDAP_STRDUP( url_tmp );
249         if ( url == NULL ) {
250                 return LDAP_URL_ERR_MEM;
251         }
252
253         if ( enclosed ) {
254                 p = &url[strlen(url)-1];
255
256                 if( *p != '>' ) {
257                         LDAP_FREE( url );
258                         return LDAP_URL_ERR_BADENCLOSURE;
259                 }
260
261                 *p = '\0';
262         }
263
264         /* allocate return struct */
265         ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
266
267         if ( ludp == NULL ) {
268                 LDAP_FREE( url );
269                 return LDAP_URL_ERR_MEM;
270         }
271
272         ludp->lud_next = NULL;
273         ludp->lud_host = NULL;
274         ludp->lud_port = LDAP_PORT;
275         ludp->lud_dn = NULL;
276         ludp->lud_attrs = NULL;
277         ludp->lud_filter = NULL;
278         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
279         ludp->lud_filter = NULL;
280         ludp->lud_exts = NULL;
281
282         ludp->lud_scheme = LDAP_STRDUP( scheme );
283
284         if ( ludp->lud_scheme == NULL ) {
285                 LDAP_FREE( url );
286                 ldap_free_urldesc( ludp );
287                 return LDAP_URL_ERR_MEM;
288         }
289
290         if( strcasecmp( ludp->lud_scheme, "ldaps" ) == 0 ) {
291                 ludp->lud_port = LDAPS_PORT;
292         }
293
294         /* scan forward for '/' that marks end of hostport and begin. of dn */
295         p = strchr( url, '/' );
296
297         if( p != NULL ) {
298                 /* terminate hostport; point to start of dn */
299                 *p++ = '\0';
300         }
301
302         /* IPv6 syntax with [ip address]:port */
303         if ( *url == '[' ) {
304                 r = strchr( url, ']' );
305                 if ( r == NULL ) {
306                         LDAP_FREE( url );
307                         ldap_free_urldesc( ludp );
308                         return LDAP_URL_ERR_BADURL;
309                 }
310                 *r++ = '\0';
311                 q = strchr( r, ':' );
312         } else {
313                 q = strchr( url, ':' );
314         }
315
316         if ( q != NULL ) {
317                 *q++ = '\0';
318                 ldap_pvt_hex_unescape( q );
319
320                 if( *q == '\0' ) {
321                         LDAP_FREE( url );
322                         ldap_free_urldesc( ludp );
323                         return LDAP_URL_ERR_BADURL;
324                 }
325
326                 ludp->lud_port = atoi( q );
327         }
328
329         ldap_pvt_hex_unescape( url );
330
331         /* If [ip address]:port syntax, url is [ip and we skip the [ */
332         ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
333
334         if( ludp->lud_host == NULL ) {
335                 LDAP_FREE( url );
336                 ldap_free_urldesc( ludp );
337                 return LDAP_URL_ERR_MEM;
338         }
339
340         /*
341          * Kludge.  ldap://111.222.333.444:389??cn=abc,o=company
342          *
343          * On early Novell releases, search references/referrals were returned
344          * in this format, i.e., the dn was kind of in the scope position,
345          * but the required slash is missing. The whole thing is illegal syntax,
346          * but we need to account for it. Fortunately it can't be confused with
347          * anything real.
348          */
349         if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
350                 q++;            
351                 /* ? immediately followed by question */
352                 if( *q == '?') {
353                         q++;
354                         if( *q != '\0' ) {
355                                 /* parse dn part */
356                                 ldap_pvt_hex_unescape( q );
357                                 ludp->lud_dn = LDAP_STRDUP( q );
358                         } else {
359                                 ludp->lud_dn = LDAP_STRDUP( "" );
360                         }
361
362                         if( ludp->lud_dn == NULL ) {
363                                 LDAP_FREE( url );
364                                 ldap_free_urldesc( ludp );
365                                 return LDAP_URL_ERR_MEM;
366                         }
367                 }
368         }
369
370         if( p == NULL ) {
371                 LDAP_FREE( url );
372                 *ludpp = ludp;
373                 return LDAP_URL_SUCCESS;
374         }
375
376         /* scan forward for '?' that may marks end of dn */
377         q = strchr( p, '?' );
378
379         if( q != NULL ) {
380                 /* terminate dn part */
381                 *q++ = '\0';
382         }
383
384         if( *p != '\0' ) {
385                 /* parse dn part */
386                 ldap_pvt_hex_unescape( p );
387                 ludp->lud_dn = LDAP_STRDUP( p );
388         } else {
389                 ludp->lud_dn = LDAP_STRDUP( "" );
390         }
391
392         if( ludp->lud_dn == NULL ) {
393                 LDAP_FREE( url );
394                 ldap_free_urldesc( ludp );
395                 return LDAP_URL_ERR_MEM;
396         }
397
398         if( q == NULL ) {
399                 /* no more */
400                 LDAP_FREE( url );
401                 *ludpp = ludp;
402                 return LDAP_URL_SUCCESS;
403         }
404
405         /* scan forward for '?' that may marks end of attributes */
406         p = q;
407         q = strchr( p, '?' );
408
409         if( q != NULL ) {
410                 /* terminate attributes part */
411                 *q++ = '\0';
412         }
413
414         if( *p != '\0' ) {
415                 /* parse attributes */
416                 ldap_pvt_hex_unescape( p );
417                 ludp->lud_attrs = ldap_str2charray( p, "," );
418
419                 if( ludp->lud_attrs == NULL ) {
420                         LDAP_FREE( url );
421                         ldap_free_urldesc( ludp );
422                         return LDAP_URL_ERR_BADATTRS;
423                 }
424         }
425
426         if ( q == NULL ) {
427                 /* no more */
428                 LDAP_FREE( url );
429                 *ludpp = ludp;
430                 return LDAP_URL_SUCCESS;
431         }
432
433         /* scan forward for '?' that may marks end of scope */
434         p = q;
435         q = strchr( p, '?' );
436
437         if( q != NULL ) {
438                 /* terminate the scope part */
439                 *q++ = '\0';
440         }
441
442         if( *p != '\0' ) {
443                 /* parse the scope */
444                 ldap_pvt_hex_unescape( p );
445                 ludp->lud_scope = str2scope( p );
446
447                 if( ludp->lud_scope == -1 ) {
448                         LDAP_FREE( url );
449                         ldap_free_urldesc( ludp );
450                         return LDAP_URL_ERR_BADSCOPE;
451                 }
452         }
453
454         if ( q == NULL ) {
455                 /* no more */
456                 LDAP_FREE( url );
457                 *ludpp = ludp;
458                 return LDAP_URL_SUCCESS;
459         }
460
461         /* scan forward for '?' that may marks end of filter */
462         p = q;
463         q = strchr( p, '?' );
464
465         if( q != NULL ) {
466                 /* terminate the filter part */
467                 *q++ = '\0';
468         }
469
470         if( *p != '\0' ) {
471                 /* parse the filter */
472                 ldap_pvt_hex_unescape( p );
473
474                 if( ! *p ) {
475                         /* missing filter */
476                         LDAP_FREE( url );
477                         ldap_free_urldesc( ludp );
478                         return LDAP_URL_ERR_BADFILTER;
479                 }
480
481                 LDAP_FREE( ludp->lud_filter );
482                 ludp->lud_filter = LDAP_STRDUP( p );
483
484                 if( ludp->lud_filter == NULL ) {
485                         LDAP_FREE( url );
486                         ldap_free_urldesc( ludp );
487                         return LDAP_URL_ERR_MEM;
488                 }
489         }
490
491         if ( q == NULL ) {
492                 /* no more */
493                 LDAP_FREE( url );
494                 *ludpp = ludp;
495                 return LDAP_URL_SUCCESS;
496         }
497
498         /* scan forward for '?' that may marks end of extensions */
499         p = q;
500         q = strchr( p, '?' );
501
502         if( q != NULL ) {
503                 /* extra '?' */
504                 LDAP_FREE( url );
505                 ldap_free_urldesc( ludp );
506                 return LDAP_URL_ERR_BADURL;
507         }
508
509         /* parse the extensions */
510         ludp->lud_exts = ldap_str2charray( p, "," );
511
512         if( ludp->lud_exts == NULL ) {
513                 LDAP_FREE( url );
514                 ldap_free_urldesc( ludp );
515                 return LDAP_URL_ERR_BADEXTS;
516         }
517
518         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
519                 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
520
521                 if( *ludp->lud_exts[i] == '!' ) {
522                         /* count the number of critical extensions */
523                         ludp->lud_crit_exts++;
524                 }
525         }
526
527         if( i == 0 ) {
528                 /* must have 1 or more */
529                 ldap_charray_free( ludp->lud_exts );
530                 LDAP_FREE( url );
531                 ldap_free_urldesc( ludp );
532                 return LDAP_URL_ERR_BADEXTS;
533         }
534
535         /* no more */
536         *ludpp = ludp;
537         LDAP_FREE( url );
538         return LDAP_URL_SUCCESS;
539 }
540
541 int
542 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
543 {
544         int rc = ldap_url_parse_ext( url_in, ludpp );
545
546         if( rc != LDAP_URL_SUCCESS ) {
547                 return rc;
548         }
549
550         if ((*ludpp)->lud_scope == LDAP_SCOPE_DEFAULT) {
551                 (*ludpp)->lud_scope = LDAP_SCOPE_BASE;
552         }
553
554         if ((*ludpp)->lud_host != NULL && *(*ludpp)->lud_host == '\0') {
555                 LDAP_FREE( (*ludpp)->lud_host );
556                 (*ludpp)->lud_host = NULL;
557         }
558
559         return rc;
560 }
561
562 LDAPURLDesc *
563 ldap_url_dup ( LDAPURLDesc *ludp )
564 {
565         LDAPURLDesc *dest;
566
567         if ( ludp == NULL ) {
568                 return NULL;
569         }
570
571         dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
572         if (dest == NULL)
573                 return NULL;
574         
575         *dest = *ludp;
576         dest->lud_scheme = NULL;
577         dest->lud_host = NULL;
578         dest->lud_dn = NULL;
579         dest->lud_filter = NULL;
580         dest->lud_attrs = NULL;
581         dest->lud_exts = NULL;
582         dest->lud_next = NULL;
583
584         if ( ludp->lud_scheme != NULL ) {
585                 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
586                 if (dest->lud_scheme == NULL) {
587                         ldap_free_urldesc(dest);
588                         return NULL;
589                 }
590         }
591
592         if ( ludp->lud_host != NULL ) {
593                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
594                 if (dest->lud_host == NULL) {
595                         ldap_free_urldesc(dest);
596                         return NULL;
597                 }
598         }
599
600         if ( ludp->lud_dn != NULL ) {
601                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
602                 if (dest->lud_dn == NULL) {
603                         ldap_free_urldesc(dest);
604                         return NULL;
605                 }
606         }
607
608         if ( ludp->lud_filter != NULL ) {
609                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
610                 if (dest->lud_filter == NULL) {
611                         ldap_free_urldesc(dest);
612                         return NULL;
613                 }
614         }
615
616         if ( ludp->lud_attrs != NULL ) {
617                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
618                 if (dest->lud_attrs == NULL) {
619                         ldap_free_urldesc(dest);
620                         return NULL;
621                 }
622         }
623
624         if ( ludp->lud_exts != NULL ) {
625                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
626                 if (dest->lud_exts == NULL) {
627                         ldap_free_urldesc(dest);
628                         return NULL;
629                 }
630         }
631
632         return dest;
633 }
634
635 LDAPURLDesc *
636 ldap_url_duplist (LDAPURLDesc *ludlist)
637 {
638         LDAPURLDesc *dest, *tail, *ludp, *newludp;
639
640         dest = NULL;
641         tail = NULL;
642         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
643                 newludp = ldap_url_dup(ludp);
644                 if (newludp == NULL) {
645                         ldap_free_urllist(dest);
646                         return NULL;
647                 }
648                 if (tail == NULL)
649                         dest = newludp;
650                 else
651                         tail->lud_next = newludp;
652                 tail = newludp;
653         }
654         return dest;
655 }
656
657 int
658 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
659 {
660         int i, rc;
661         LDAPURLDesc *ludp;
662         char **urls;
663
664         *ludlist = NULL;
665
666         if (url == NULL)
667                 return LDAP_PARAM_ERROR;
668
669         urls = ldap_str2charray((char *)url, ", ");
670         if (urls == NULL)
671                 return LDAP_NO_MEMORY;
672
673         /* count the URLs... */
674         for (i = 0; urls[i] != NULL; i++) ;
675         /* ...and put them in the "stack" backward */
676         while (--i >= 0) {
677                 rc = ldap_url_parse( urls[i], &ludp );
678                 if ( rc != 0 ) {
679                         ldap_charray_free(urls);
680                         ldap_free_urllist(*ludlist);
681                         *ludlist = NULL;
682                         return rc;
683                 }
684                 ludp->lud_next = *ludlist;
685                 *ludlist = ludp;
686         }
687         ldap_charray_free(urls);
688         return LDAP_SUCCESS;
689 }
690
691 int
692 ldap_url_parsehosts(
693         LDAPURLDesc **ludlist,
694         const char *hosts,
695         int port )
696 {
697         int i;
698         LDAPURLDesc *ludp;
699         char **specs, *p;
700
701         *ludlist = NULL;
702
703         if (hosts == NULL)
704                 return LDAP_PARAM_ERROR;
705
706         specs = ldap_str2charray((char *)hosts, ", ");
707         if (specs == NULL)
708                 return LDAP_NO_MEMORY;
709
710         /* count the URLs... */
711         for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
712
713         /* ...and put them in the "stack" backward */
714         while (--i >= 0) {
715                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
716                 if (ludp == NULL) {
717                         ldap_charray_free(specs);
718                         ldap_free_urllist(*ludlist);
719                         *ludlist = NULL;
720                         return LDAP_NO_MEMORY;
721                 }
722                 ludp->lud_port = port;
723                 ludp->lud_host = specs[i];
724                 specs[i] = NULL;
725                 p = strchr(ludp->lud_host, ':');
726                 if (p != NULL) {
727                         /* more than one :, IPv6 address */
728                         if ( strchr(p+1, ':') != NULL ) {
729                                 /* allow [address] and [address]:port */
730                                 if ( *ludp->lud_host == '[' ) {
731                                         p = LDAP_STRDUP(ludp->lud_host+1);
732                                         /* copied, make sure we free source later */
733                                         specs[i] = ludp->lud_host;
734                                         ludp->lud_host = p;
735                                         p = strchr( ludp->lud_host, ']' );
736                                         if ( p == NULL )
737                                                 return LDAP_PARAM_ERROR;
738                                         *p++ = '\0';
739                                         if ( *p != ':' ) {
740                                                 if ( *p != '\0' )
741                                                         return LDAP_PARAM_ERROR;
742                                                 p = NULL;
743                                         }
744                                 } else {
745                                         p = NULL;
746                                 }
747                         }
748                         if (p != NULL) {
749                                 *p++ = 0;
750                                 ldap_pvt_hex_unescape(p);
751                                 ludp->lud_port = atoi(p);
752                         }
753                 }
754                 ldap_pvt_hex_unescape(ludp->lud_host);
755                 ludp->lud_scheme = LDAP_STRDUP("ldap");
756                 ludp->lud_next = *ludlist;
757                 *ludlist = ludp;
758         }
759
760         /* this should be an array of NULLs now */
761         /* except entries starting with [ */
762         ldap_charray_free(specs);
763         return LDAP_SUCCESS;
764 }
765
766 char *
767 ldap_url_list2hosts (LDAPURLDesc *ludlist)
768 {
769         LDAPURLDesc *ludp;
770         int size;
771         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
772
773         if (ludlist == NULL)
774                 return NULL;
775
776         /* figure out how big the string is */
777         size = 1;       /* nul-term */
778         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
779                 size += strlen(ludp->lud_host) + 1;             /* host and space */
780                 if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
781                         size += 2;
782                 if (ludp->lud_port != 0)
783                         size += sprintf(buf, ":%d", ludp->lud_port);
784         }
785         s = LDAP_MALLOC(size);
786         if (s == NULL)
787                 return NULL;
788
789         p = s;
790         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
791                 if (strchr(ludp->lud_host, ':')) {
792                         p += sprintf(p, "[%s]", ludp->lud_host);
793                 } else {
794                         strcpy(p, ludp->lud_host);
795                         p += strlen(ludp->lud_host);
796                 }
797                 if (ludp->lud_port != 0)
798                         p += sprintf(p, ":%d", ludp->lud_port);
799                 *p++ = ' ';
800         }
801         if (p != s)
802                 p--;    /* nuke that extra space */
803         *p = 0;
804         return s;
805 }
806
807 char *
808 ldap_url_list2urls(
809         LDAPURLDesc *ludlist )
810 {
811         LDAPURLDesc *ludp;
812         int size;
813         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
814
815         if (ludlist == NULL)
816                 return NULL;
817
818         /* figure out how big the string is */
819         size = 1;       /* nul-term */
820         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
821                 size += strlen(ludp->lud_scheme) + strlen(ludp->lud_host);
822                 if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
823                         size += 2;
824                 size += sizeof(":/// ");
825
826                 if (ludp->lud_port != 0) {
827                         size += sprintf(buf, ":%d", ludp->lud_port);
828                 }
829         }
830
831         s = LDAP_MALLOC(size);
832         if (s == NULL) {
833                 return NULL;
834         }
835
836         p = s;
837         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
838                 p += sprintf(p,
839                              strchr(ludp->lud_host, ':') ? "%s://[%s]" : "%s://%s",
840                              ludp->lud_scheme, ludp->lud_host);
841                 if (ludp->lud_port != 0)
842                         p += sprintf(p, ":%d", ludp->lud_port);
843                 *p++ = '/';
844                 *p++ = ' ';
845         }
846         if (p != s)
847                 p--;    /* nuke that extra space */
848         *p = 0;
849         return s;
850 }
851
852 void
853 ldap_free_urllist( LDAPURLDesc *ludlist )
854 {
855         LDAPURLDesc *ludp, *next;
856
857         for (ludp = ludlist; ludp != NULL; ludp = next) {
858                 next = ludp->lud_next;
859                 ldap_free_urldesc(ludp);
860         }
861 }
862
863 void
864 ldap_free_urldesc( LDAPURLDesc *ludp )
865 {
866         if ( ludp == NULL ) {
867                 return;
868         }
869         
870         if ( ludp->lud_scheme != NULL ) {
871                 LDAP_FREE( ludp->lud_scheme );
872         }
873
874         if ( ludp->lud_host != NULL ) {
875                 LDAP_FREE( ludp->lud_host );
876         }
877
878         if ( ludp->lud_dn != NULL ) {
879                 LDAP_FREE( ludp->lud_dn );
880         }
881
882         if ( ludp->lud_filter != NULL ) {
883                 LDAP_FREE( ludp->lud_filter);
884         }
885
886         if ( ludp->lud_attrs != NULL ) {
887                 LDAP_VFREE( ludp->lud_attrs );
888         }
889
890         if ( ludp->lud_exts != NULL ) {
891                 LDAP_VFREE( ludp->lud_exts );
892         }
893
894         LDAP_FREE( ludp );
895 }
896
897
898
899 int
900 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
901 {
902         int             err;
903         LDAPURLDesc     *ludp;
904         BerElement      *ber;
905         LDAPreqinfo  bind;
906
907         assert( ld != NULL );
908         assert( LDAP_VALID( ld ) );
909
910         if ( ldap_url_parse( url, &ludp ) != 0 ) {
911                 ld->ld_errno = LDAP_PARAM_ERROR;
912                 return( -1 );
913         }
914
915         if( ludp->lud_crit_exts ) {
916                 /* we don't support any extension (yet) */
917                 ld->ld_errno = LDAP_NOT_SUPPORTED;
918                 return( -1 );
919         }
920
921         ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
922             ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
923                 -1, -1 );
924
925         if ( ber == NULL ) {
926                 err = -1;
927         } else {
928                 bind.ri_request = LDAP_REQ_SEARCH;
929                 bind.ri_msgid = ld->ld_msgid;
930                 bind.ri_url = (char *)url;
931                 err = ldap_send_server_request(
932                                         ld, ber, ld->ld_msgid, NULL,
933                                         NULL, NULL, &bind );
934         }
935
936         ldap_free_urldesc( ludp );
937         return( err );
938 }
939
940
941 int
942 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
943         struct timeval *timeout, LDAPMessage **res )
944 {
945         int     msgid;
946
947         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
948                 return( ld->ld_errno );
949         }
950
951         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
952                 return( ld->ld_errno );
953         }
954
955         if ( ld->ld_errno == LDAP_TIMEOUT ) {
956                 (void) ldap_abandon( ld, msgid );
957                 ld->ld_errno = LDAP_TIMEOUT;
958                 return( ld->ld_errno );
959         }
960
961         return( ldap_result2error( ld, *res, 0 ));
962 }
963
964
965 int
966 ldap_url_search_s(
967         LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
968 {
969         int     msgid;
970
971         if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
972                 return( ld->ld_errno );
973         }
974
975         if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
976                 return( ld->ld_errno );
977         }
978
979         return( ldap_result2error( ld, *res, 0 ));
980 }
981
982
983 void
984 ldap_pvt_hex_unescape( char *s )
985 {
986         /*
987          * Remove URL hex escapes from s... done in place.  The basic concept for
988          * this routine is borrowed from the WWW library HTUnEscape() routine.
989          */
990         char    *p;
991
992         for ( p = s; *s != '\0'; ++s ) {
993                 if ( *s == '%' ) {
994                         if ( *++s != '\0' ) {
995                                 *p = ldap_pvt_unhex( *s ) << 4;
996                         }
997                         if ( *++s != '\0' ) {
998                                 *p++ += ldap_pvt_unhex( *s );
999                         }
1000                 } else {
1001                         *p++ = *s;
1002                 }
1003         }
1004
1005         *p = '\0';
1006 }
1007
1008
1009 int
1010 ldap_pvt_unhex( int c )
1011 {
1012         return( c >= '0' && c <= '9' ? c - '0'
1013             : c >= 'A' && c <= 'F' ? c - 'A' + 10
1014             : c - 'a' + 10 );
1015 }