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