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