]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
Sync with HEAD
[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-2003 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
250         return( -1 );
251 }
252
253 static int hex_escape( char *buf, const char *s, int list )
254 {
255         int i;
256         int pos;
257         static const char hex[] = "0123456789ABCDEF";
258
259         if( s == NULL ) return 0;
260
261         for( pos=0,i=0; s[i]; i++ ) {
262                 int escape = 0;
263                 switch( s[i] ) {
264                         case ',':
265                                 escape = list;
266                                 break;
267                         case '%':
268                         case '?':
269                         case ' ':
270                         case '<':
271                         case '>':
272                         case '"':
273                         case '#':
274                         case '{':
275                         case '}':
276                         case '|':
277                         case '\\':
278                         case '^':
279                         case '~':
280                         case '`':
281                         case '[':
282                         case ']':
283                                 escape = 1;
284                                 break;
285
286                         default:
287                                 escape = s[i] < 0x20 || 0x1f >= s[i];
288                 }
289
290                 if( escape ) {
291                         buf[pos++] = '%';
292                         buf[pos++] = hex[ (s[i] >> 4) & 0x0f ];
293                         buf[pos++] = hex[ s[i] & 0x0f ];
294                 } else {
295                         buf[pos++] = s[i];
296                 }
297         }
298
299         buf[pos] = '\0';
300         return pos;
301 }
302
303 static int hex_escape_args( char *buf, char **s )
304 {
305         int pos;
306         int i;
307
308         if( s == NULL ) return 0;
309
310         pos = 0;
311         for( i=0; s[i] != NULL; i++ ) {
312                 if( pos ) {
313                         buf[pos++] = ',';
314                 }
315                 pos += hex_escape( &buf[pos], s[i], 1 );
316         }
317
318         return pos;
319 }
320
321 char * ldap_url_desc2str( LDAPURLDesc *u )
322 {
323         char *s;
324         int i;
325         int sep = 0;
326         int sofar;
327         size_t len = 0;
328         if( u == NULL ) return NULL;
329
330         if( u->lud_exts ) {
331                 for( i=0; u->lud_exts[i]; i++ ) {
332                         len += strlen( u->lud_exts[i] ) + 1;
333                 }
334                 if( !sep ) sep = 5;
335         }
336
337         if( u->lud_filter ) {
338                 len += strlen( u->lud_filter );
339                 if( !sep ) sep = 4;
340         }
341         if ( len ) len++; /* ? */
342
343         switch( u->lud_scope ) {
344                 case LDAP_SCOPE_ONELEVEL:
345                 case LDAP_SCOPE_SUBTREE:
346                 case LDAP_SCOPE_BASE:
347                         len += sizeof("base");
348                         if( !sep ) sep = 3;
349                         break;
350
351                 default:
352                         if ( len ) len++; /* ? */
353         }
354
355         if( u->lud_attrs ) {
356                 for( i=0; u->lud_attrs[i]; i++ ) {
357                         len += strlen( u->lud_attrs[i] ) + 1;
358                 }
359                 if( !sep ) sep = 2;
360         } else if ( len ) len++; /* ? */
361
362         if( u->lud_dn ) {
363                 len += strlen( u->lud_dn ) + 1;
364                 if( !sep ) sep = 1;
365         };
366
367         if( u->lud_port ) {
368                 len += sizeof(":65535") - 1;
369         }
370
371         if( u->lud_host ) {
372                 len+=strlen( u->lud_host );
373         }
374
375         len += strlen( u->lud_scheme ) + sizeof("://");
376
377         /* allocate enough to hex escape everything -- overkill */
378         s = LDAP_MALLOC( 3*len );
379
380         if( s == NULL ) return NULL;
381
382         if( u->lud_port ) {
383                 sprintf( s,     "%s://%s:%d%n", u->lud_scheme,
384                         u->lud_host, u->lud_port, &sofar );
385         } else {
386                 sprintf( s,     "%s://%s%n", u->lud_scheme,
387                         u->lud_host, &sofar );
388         }
389         
390         if( sep < 1 ) goto done;
391         s[sofar++] = '/';
392
393         sofar += hex_escape( &s[sofar], u->lud_dn, 0 );
394
395         if( sep < 2 ) goto done;
396         s[sofar++] = '?';
397
398         sofar += hex_escape_args( &s[sofar], u->lud_attrs );
399
400         if( sep < 3 ) goto done;
401         s[sofar++] = '?';
402
403         switch( u->lud_scope ) {
404         case LDAP_SCOPE_BASE:
405                 strcpy( &s[sofar], "base" );
406                 sofar += sizeof("base") - 1;
407                 break;
408         case LDAP_SCOPE_ONELEVEL:
409                 strcpy( &s[sofar], "one" );
410                 sofar += sizeof("one") - 1;
411                 break;
412         case LDAP_SCOPE_SUBTREE:
413                 strcpy( &s[sofar], "sub" );
414                 sofar += sizeof("sub") - 1;
415                 break;
416         }
417
418         if( sep < 4 ) goto done;
419         s[sofar++] = '?';
420
421         sofar += hex_escape( &s[sofar], u->lud_filter, 0 );
422
423         if( sep < 5 ) goto done;
424         s[sofar++] = '?';
425
426         sofar += hex_escape_args( &s[sofar], u->lud_exts );
427
428 done:
429         s[sofar] = '\0';
430         return s;
431 }
432
433 int
434 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
435 {
436 /*
437  *  Pick apart the pieces of an LDAP URL.
438  */
439
440         LDAPURLDesc     *ludp;
441         char    *p, *q, *r;
442         int             i, enclosed;
443         const char *scheme = NULL;
444         const char *url_tmp;
445         char *url;
446
447         if( url_in == NULL || ludpp == NULL ) {
448                 return LDAP_URL_ERR_PARAM;
449         }
450
451 #ifndef LDAP_INT_IN_KERNEL
452         /* Global options may not be created yet
453          * We can't test if the global options are initialized
454          * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
455          * the options and cause infinite recursion
456          */
457 #ifdef NEW_LOGGING
458         LDAP_LOG ( OPERATION, ENTRY, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
459 #else
460         Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
461 #endif
462 #endif
463
464         *ludpp = NULL;  /* pessimistic */
465
466         url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
467
468         if ( url_tmp == NULL ) {
469                 return LDAP_URL_ERR_BADSCHEME;
470         }
471
472         assert( scheme );
473
474         /* make working copy of the remainder of the URL */
475         url = LDAP_STRDUP( url_tmp );
476         if ( url == NULL ) {
477                 return LDAP_URL_ERR_MEM;
478         }
479
480         if ( enclosed ) {
481                 p = &url[strlen(url)-1];
482
483                 if( *p != '>' ) {
484                         LDAP_FREE( url );
485                         return LDAP_URL_ERR_BADENCLOSURE;
486                 }
487
488                 *p = '\0';
489         }
490
491         /* allocate return struct */
492         ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
493
494         if ( ludp == NULL ) {
495                 LDAP_FREE( url );
496                 return LDAP_URL_ERR_MEM;
497         }
498
499         ludp->lud_next = NULL;
500         ludp->lud_host = NULL;
501         ludp->lud_port = 0;
502         ludp->lud_dn = NULL;
503         ludp->lud_attrs = NULL;
504         ludp->lud_filter = NULL;
505         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
506         ludp->lud_filter = NULL;
507         ludp->lud_exts = NULL;
508
509         ludp->lud_scheme = LDAP_STRDUP( scheme );
510
511         if ( ludp->lud_scheme == NULL ) {
512                 LDAP_FREE( url );
513                 ldap_free_urldesc( ludp );
514                 return LDAP_URL_ERR_MEM;
515         }
516
517         /* scan forward for '/' that marks end of hostport and begin. of dn */
518         p = strchr( url, '/' );
519
520         if( p != NULL ) {
521                 /* terminate hostport; point to start of dn */
522                 *p++ = '\0';
523         }
524
525         /* IPv6 syntax with [ip address]:port */
526         if ( *url == '[' ) {
527                 r = strchr( url, ']' );
528                 if ( r == NULL ) {
529                         LDAP_FREE( url );
530                         ldap_free_urldesc( ludp );
531                         return LDAP_URL_ERR_BADURL;
532                 }
533                 *r++ = '\0';
534                 q = strchr( r, ':' );
535         } else {
536                 q = strchr( url, ':' );
537         }
538
539         if ( q != NULL ) {
540                 *q++ = '\0';
541                 ldap_pvt_hex_unescape( q );
542
543                 if( *q == '\0' ) {
544                         LDAP_FREE( url );
545                         ldap_free_urldesc( ludp );
546                         return LDAP_URL_ERR_BADURL;
547                 }
548
549                 ludp->lud_port = atoi( q );
550         }
551
552         ldap_pvt_hex_unescape( url );
553
554         /* If [ip address]:port syntax, url is [ip and we skip the [ */
555         ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
556
557         if( ludp->lud_host == NULL ) {
558                 LDAP_FREE( url );
559                 ldap_free_urldesc( ludp );
560                 return LDAP_URL_ERR_MEM;
561         }
562
563         /*
564          * Kludge.  ldap://111.222.333.444:389??cn=abc,o=company
565          *
566          * On early Novell releases, search references/referrals were returned
567          * in this format, i.e., the dn was kind of in the scope position,
568          * but the required slash is missing. The whole thing is illegal syntax,
569          * but we need to account for it. Fortunately it can't be confused with
570          * anything real.
571          */
572         if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
573                 q++;            
574                 /* ? immediately followed by question */
575                 if( *q == '?') {
576                         q++;
577                         if( *q != '\0' ) {
578                                 /* parse dn part */
579                                 ldap_pvt_hex_unescape( q );
580                                 ludp->lud_dn = LDAP_STRDUP( q );
581                         } else {
582                                 ludp->lud_dn = LDAP_STRDUP( "" );
583                         }
584
585                         if( ludp->lud_dn == NULL ) {
586                                 LDAP_FREE( url );
587                                 ldap_free_urldesc( ludp );
588                                 return LDAP_URL_ERR_MEM;
589                         }
590                 }
591         }
592
593         if( p == NULL ) {
594                 LDAP_FREE( url );
595                 *ludpp = ludp;
596                 return LDAP_URL_SUCCESS;
597         }
598
599         /* scan forward for '?' that may marks end of dn */
600         q = strchr( p, '?' );
601
602         if( q != NULL ) {
603                 /* terminate dn part */
604                 *q++ = '\0';
605         }
606
607         if( *p != '\0' ) {
608                 /* parse dn part */
609                 ldap_pvt_hex_unescape( p );
610                 ludp->lud_dn = LDAP_STRDUP( p );
611         } else {
612                 ludp->lud_dn = LDAP_STRDUP( "" );
613         }
614
615         if( ludp->lud_dn == NULL ) {
616                 LDAP_FREE( url );
617                 ldap_free_urldesc( ludp );
618                 return LDAP_URL_ERR_MEM;
619         }
620
621         if( q == NULL ) {
622                 /* no more */
623                 LDAP_FREE( url );
624                 *ludpp = ludp;
625                 return LDAP_URL_SUCCESS;
626         }
627
628         /* scan forward for '?' that may marks end of attributes */
629         p = q;
630         q = strchr( p, '?' );
631
632         if( q != NULL ) {
633                 /* terminate attributes part */
634                 *q++ = '\0';
635         }
636
637         if( *p != '\0' ) {
638                 /* parse attributes */
639                 ldap_pvt_hex_unescape( p );
640                 ludp->lud_attrs = ldap_str2charray( p, "," );
641
642                 if( ludp->lud_attrs == NULL ) {
643                         LDAP_FREE( url );
644                         ldap_free_urldesc( ludp );
645                         return LDAP_URL_ERR_BADATTRS;
646                 }
647         }
648
649         if ( q == NULL ) {
650                 /* no more */
651                 LDAP_FREE( url );
652                 *ludpp = ludp;
653                 return LDAP_URL_SUCCESS;
654         }
655
656         /* scan forward for '?' that may marks end of scope */
657         p = q;
658         q = strchr( p, '?' );
659
660         if( q != NULL ) {
661                 /* terminate the scope part */
662                 *q++ = '\0';
663         }
664
665         if( *p != '\0' ) {
666                 /* parse the scope */
667                 ldap_pvt_hex_unescape( p );
668                 ludp->lud_scope = str2scope( p );
669
670                 if( ludp->lud_scope == -1 ) {
671                         LDAP_FREE( url );
672                         ldap_free_urldesc( ludp );
673                         return LDAP_URL_ERR_BADSCOPE;
674                 }
675         }
676
677         if ( q == NULL ) {
678                 /* no more */
679                 LDAP_FREE( url );
680                 *ludpp = ludp;
681                 return LDAP_URL_SUCCESS;
682         }
683
684         /* scan forward for '?' that may marks end of filter */
685         p = q;
686         q = strchr( p, '?' );
687
688         if( q != NULL ) {
689                 /* terminate the filter part */
690                 *q++ = '\0';
691         }
692
693         if( *p != '\0' ) {
694                 /* parse the filter */
695                 ldap_pvt_hex_unescape( p );
696
697                 if( ! *p ) {
698                         /* missing filter */
699                         LDAP_FREE( url );
700                         ldap_free_urldesc( ludp );
701                         return LDAP_URL_ERR_BADFILTER;
702                 }
703
704                 LDAP_FREE( ludp->lud_filter );
705                 ludp->lud_filter = LDAP_STRDUP( p );
706
707                 if( ludp->lud_filter == NULL ) {
708                         LDAP_FREE( url );
709                         ldap_free_urldesc( ludp );
710                         return LDAP_URL_ERR_MEM;
711                 }
712         }
713
714         if ( q == NULL ) {
715                 /* no more */
716                 LDAP_FREE( url );
717                 *ludpp = ludp;
718                 return LDAP_URL_SUCCESS;
719         }
720
721         /* scan forward for '?' that may marks end of extensions */
722         p = q;
723         q = strchr( p, '?' );
724
725         if( q != NULL ) {
726                 /* extra '?' */
727                 LDAP_FREE( url );
728                 ldap_free_urldesc( ludp );
729                 return LDAP_URL_ERR_BADURL;
730         }
731
732         /* parse the extensions */
733         ludp->lud_exts = ldap_str2charray( p, "," );
734
735         if( ludp->lud_exts == NULL ) {
736                 LDAP_FREE( url );
737                 ldap_free_urldesc( ludp );
738                 return LDAP_URL_ERR_BADEXTS;
739         }
740
741         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
742                 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
743
744                 if( *ludp->lud_exts[i] == '!' ) {
745                         /* count the number of critical extensions */
746                         ludp->lud_crit_exts++;
747                 }
748         }
749
750         if( i == 0 ) {
751                 /* must have 1 or more */
752                 LDAP_FREE( url );
753                 ldap_free_urldesc( ludp );
754                 return LDAP_URL_ERR_BADEXTS;
755         }
756
757         /* no more */
758         *ludpp = ludp;
759         LDAP_FREE( url );
760         return LDAP_URL_SUCCESS;
761 }
762
763 int
764 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
765 {
766         int rc = ldap_url_parse_ext( url_in, ludpp );
767
768         if( rc != LDAP_URL_SUCCESS ) {
769                 return rc;
770         }
771
772         if ((*ludpp)->lud_scope == LDAP_SCOPE_DEFAULT) {
773                 (*ludpp)->lud_scope = LDAP_SCOPE_BASE;
774         }
775
776         if ((*ludpp)->lud_host != NULL && *(*ludpp)->lud_host == '\0') {
777                 LDAP_FREE( (*ludpp)->lud_host );
778                 (*ludpp)->lud_host = NULL;
779         }
780
781         if ((*ludpp)->lud_port == 0) {
782                 if( strcmp((*ludpp)->lud_scheme, "ldap") == 0 ) {
783                         (*ludpp)->lud_port = LDAP_PORT;
784 #ifdef LDAP_CONNECTIONLESS
785                 } else if( strcmp((*ludpp)->lud_scheme, "cldap") == 0 ) {
786                         (*ludpp)->lud_port = LDAP_PORT;
787 #endif
788                 } else if( strcmp((*ludpp)->lud_scheme, "ldaps") == 0 ) {
789                         (*ludpp)->lud_port = LDAPS_PORT;
790                 }
791         }
792
793         return rc;
794 }
795
796 LDAPURLDesc *
797 ldap_url_dup ( LDAPURLDesc *ludp )
798 {
799         LDAPURLDesc *dest;
800
801         if ( ludp == NULL ) {
802                 return NULL;
803         }
804
805         dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
806         if (dest == NULL)
807                 return NULL;
808         
809         *dest = *ludp;
810         dest->lud_scheme = NULL;
811         dest->lud_host = NULL;
812         dest->lud_dn = NULL;
813         dest->lud_filter = NULL;
814         dest->lud_attrs = NULL;
815         dest->lud_exts = NULL;
816         dest->lud_next = NULL;
817
818         if ( ludp->lud_scheme != NULL ) {
819                 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
820                 if (dest->lud_scheme == NULL) {
821                         ldap_free_urldesc(dest);
822                         return NULL;
823                 }
824         }
825
826         if ( ludp->lud_host != NULL ) {
827                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
828                 if (dest->lud_host == NULL) {
829                         ldap_free_urldesc(dest);
830                         return NULL;
831                 }
832         }
833
834         if ( ludp->lud_dn != NULL ) {
835                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
836                 if (dest->lud_dn == NULL) {
837                         ldap_free_urldesc(dest);
838                         return NULL;
839                 }
840         }
841
842         if ( ludp->lud_filter != NULL ) {
843                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
844                 if (dest->lud_filter == NULL) {
845                         ldap_free_urldesc(dest);
846                         return NULL;
847                 }
848         }
849
850         if ( ludp->lud_attrs != NULL ) {
851                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
852                 if (dest->lud_attrs == NULL) {
853                         ldap_free_urldesc(dest);
854                         return NULL;
855                 }
856         }
857
858         if ( ludp->lud_exts != NULL ) {
859                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
860                 if (dest->lud_exts == NULL) {
861                         ldap_free_urldesc(dest);
862                         return NULL;
863                 }
864         }
865
866         return dest;
867 }
868
869 LDAPURLDesc *
870 ldap_url_duplist (LDAPURLDesc *ludlist)
871 {
872         LDAPURLDesc *dest, *tail, *ludp, *newludp;
873
874         dest = NULL;
875         tail = NULL;
876         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
877                 newludp = ldap_url_dup(ludp);
878                 if (newludp == NULL) {
879                         ldap_free_urllist(dest);
880                         return NULL;
881                 }
882                 if (tail == NULL)
883                         dest = newludp;
884                 else
885                         tail->lud_next = newludp;
886                 tail = newludp;
887         }
888         return dest;
889 }
890
891 int
892 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
893 {
894         return ldap_url_parselist_ext( ludlist, url, ", " );
895 }
896
897 int
898 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep )
899 {
900         int i, rc;
901         LDAPURLDesc *ludp;
902         char **urls;
903
904         assert( ludlist != NULL );
905         assert( url != NULL );
906
907         *ludlist = NULL;
908
909         urls = ldap_str2charray(url, sep);
910         if (urls == NULL)
911                 return LDAP_NO_MEMORY;
912
913         /* count the URLs... */
914         for (i = 0; urls[i] != NULL; i++) ;
915         /* ...and put them in the "stack" backward */
916         while (--i >= 0) {
917                 rc = ldap_url_parse( urls[i], &ludp );
918                 if ( rc != 0 ) {
919                         ldap_charray_free(urls);
920                         ldap_free_urllist(*ludlist);
921                         *ludlist = NULL;
922                         return rc;
923                 }
924                 ludp->lud_next = *ludlist;
925                 *ludlist = ludp;
926         }
927         ldap_charray_free(urls);
928         return LDAP_SUCCESS;
929 }
930
931 int
932 ldap_url_parsehosts(
933         LDAPURLDesc **ludlist,
934         const char *hosts,
935         int port )
936 {
937         int i;
938         LDAPURLDesc *ludp;
939         char **specs, *p;
940
941         assert( ludlist != NULL );
942         assert( hosts != NULL );
943
944         *ludlist = NULL;
945
946         specs = ldap_str2charray(hosts, ", ");
947         if (specs == NULL)
948                 return LDAP_NO_MEMORY;
949
950         /* count the URLs... */
951         for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
952
953         /* ...and put them in the "stack" backward */
954         while (--i >= 0) {
955                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
956                 if (ludp == NULL) {
957                         ldap_charray_free(specs);
958                         ldap_free_urllist(*ludlist);
959                         *ludlist = NULL;
960                         return LDAP_NO_MEMORY;
961                 }
962                 ludp->lud_port = port;
963                 ludp->lud_host = specs[i];
964                 specs[i] = NULL;
965                 p = strchr(ludp->lud_host, ':');
966                 if (p != NULL) {
967                         /* more than one :, IPv6 address */
968                         if ( strchr(p+1, ':') != NULL ) {
969                                 /* allow [address] and [address]:port */
970                                 if ( *ludp->lud_host == '[' ) {
971                                         p = LDAP_STRDUP(ludp->lud_host+1);
972                                         /* copied, make sure we free source later */
973                                         specs[i] = ludp->lud_host;
974                                         ludp->lud_host = p;
975                                         p = strchr( ludp->lud_host, ']' );
976                                         if ( p == NULL )
977                                                 return LDAP_PARAM_ERROR;
978                                         *p++ = '\0';
979                                         if ( *p != ':' ) {
980                                                 if ( *p != '\0' )
981                                                         return LDAP_PARAM_ERROR;
982                                                 p = NULL;
983                                         }
984                                 } else {
985                                         p = NULL;
986                                 }
987                         }
988                         if (p != NULL) {
989                                 *p++ = 0;
990                                 ldap_pvt_hex_unescape(p);
991                                 ludp->lud_port = atoi(p);
992                         }
993                 }
994                 ldap_pvt_hex_unescape(ludp->lud_host);
995                 ludp->lud_scheme = LDAP_STRDUP("ldap");
996                 ludp->lud_next = *ludlist;
997                 *ludlist = ludp;
998         }
999
1000         /* this should be an array of NULLs now */
1001         /* except entries starting with [ */
1002         ldap_charray_free(specs);
1003         return LDAP_SUCCESS;
1004 }
1005
1006 char *
1007 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1008 {
1009         LDAPURLDesc *ludp;
1010         int size;
1011         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
1012
1013         if (ludlist == NULL)
1014                 return NULL;
1015
1016         /* figure out how big the string is */
1017         size = 1;       /* nul-term */
1018         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1019                 size += strlen(ludp->lud_host) + 1;             /* host and space */
1020                 if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
1021                         size += 2;
1022                 if (ludp->lud_port != 0)
1023                         size += sprintf(buf, ":%d", ludp->lud_port);
1024         }
1025         s = LDAP_MALLOC(size);
1026         if (s == NULL)
1027                 return NULL;
1028
1029         p = s;
1030         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1031                 if (strchr(ludp->lud_host, ':')) {
1032                         p += sprintf(p, "[%s]", ludp->lud_host);
1033                 } else {
1034                         strcpy(p, ludp->lud_host);
1035                         p += strlen(ludp->lud_host);
1036                 }
1037                 if (ludp->lud_port != 0)
1038                         p += sprintf(p, ":%d", ludp->lud_port);
1039                 *p++ = ' ';
1040         }
1041         if (p != s)
1042                 p--;    /* nuke that extra space */
1043         *p = 0;
1044         return s;
1045 }
1046
1047 char *
1048 ldap_url_list2urls(
1049         LDAPURLDesc *ludlist )
1050 {
1051         LDAPURLDesc *ludp;
1052         int size;
1053         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
1054
1055         if (ludlist == NULL)
1056                 return NULL;
1057
1058         /* figure out how big the string is */
1059         size = 1;       /* nul-term */
1060         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1061                 size += strlen(ludp->lud_scheme);
1062                 if ( ludp->lud_host ) {
1063                         size += strlen(ludp->lud_host);
1064                         /* will add [ ] below */
1065                         if (strchr(ludp->lud_host, ':'))
1066                                 size += 2;
1067                 }
1068                 size += sizeof(":/// ");
1069
1070                 if (ludp->lud_port != 0) {
1071                         size += sprintf(buf, ":%d", ludp->lud_port);
1072                 }
1073         }
1074
1075         s = LDAP_MALLOC(size);
1076         if (s == NULL) {
1077                 return NULL;
1078         }
1079
1080         p = s;
1081         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1082                 p += sprintf(p, "%s://", ludp->lud_scheme);
1083                 if ( ludp->lud_host ) {
1084                         p += sprintf(p, strchr(ludp->lud_host, ':') 
1085                                         ? "[%s]" : "%s", ludp->lud_host);
1086                 }
1087                 if (ludp->lud_port != 0)
1088                         p += sprintf(p, ":%d", ludp->lud_port);
1089                 *p++ = '/';
1090                 *p++ = ' ';
1091         }
1092         if (p != s)
1093                 p--;    /* nuke that extra space */
1094         *p = 0;
1095         return s;
1096 }
1097
1098 void
1099 ldap_free_urllist( LDAPURLDesc *ludlist )
1100 {
1101         LDAPURLDesc *ludp, *next;
1102
1103         for (ludp = ludlist; ludp != NULL; ludp = next) {
1104                 next = ludp->lud_next;
1105                 ldap_free_urldesc(ludp);
1106         }
1107 }
1108
1109 void
1110 ldap_free_urldesc( LDAPURLDesc *ludp )
1111 {
1112         if ( ludp == NULL ) {
1113                 return;
1114         }
1115         
1116         if ( ludp->lud_scheme != NULL ) {
1117                 LDAP_FREE( ludp->lud_scheme );
1118         }
1119
1120         if ( ludp->lud_host != NULL ) {
1121                 LDAP_FREE( ludp->lud_host );
1122         }
1123
1124         if ( ludp->lud_dn != NULL ) {
1125                 LDAP_FREE( ludp->lud_dn );
1126         }
1127
1128         if ( ludp->lud_filter != NULL ) {
1129                 LDAP_FREE( ludp->lud_filter);
1130         }
1131
1132         if ( ludp->lud_attrs != NULL ) {
1133                 LDAP_VFREE( ludp->lud_attrs );
1134         }
1135
1136         if ( ludp->lud_exts != NULL ) {
1137                 LDAP_VFREE( ludp->lud_exts );
1138         }
1139
1140         LDAP_FREE( ludp );
1141 }
1142
1143 static int
1144 ldap_int_unhex( int c )
1145 {
1146         return( c >= '0' && c <= '9' ? c - '0'
1147             : c >= 'A' && c <= 'F' ? c - 'A' + 10
1148             : c - 'a' + 10 );
1149 }
1150
1151 void
1152 ldap_pvt_hex_unescape( char *s )
1153 {
1154         /*
1155          * Remove URL hex escapes from s... done in place.  The basic concept for
1156          * this routine is borrowed from the WWW library HTUnEscape() routine.
1157          */
1158         char    *p;
1159
1160         for ( p = s; *s != '\0'; ++s ) {
1161                 if ( *s == '%' ) {
1162                         if ( *++s == '\0' ) {
1163                                 break;
1164                         }
1165                         *p = ldap_int_unhex( *s ) << 4;
1166                         if ( *++s == '\0' ) {
1167                                 break;
1168                         }
1169                         *p++ += ldap_int_unhex( *s );
1170                 } else {
1171                         *p++ = *s;
1172                 }
1173         }
1174
1175         *p = '\0';
1176 }
1177
1178