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