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