]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
207c9d5fcba43852ee2f58f2233044595599ca00
[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_filter = NULL;
522         ludp->lud_scope = LDAP_SCOPE_DEFAULT;
523         ludp->lud_filter = NULL;
524         ludp->lud_exts = NULL;
525
526         ludp->lud_scheme = LDAP_STRDUP( scheme );
527
528         if ( ludp->lud_scheme == NULL ) {
529                 LDAP_FREE( url );
530                 ldap_free_urldesc( ludp );
531                 return LDAP_URL_ERR_MEM;
532         }
533
534         /* scan forward for '/' that marks end of hostport and begin. of dn */
535         p = strchr( url, '/' );
536
537         if( p != NULL ) {
538                 /* terminate hostport; point to start of dn */
539                 *p++ = '\0';
540         }
541
542         /* IPv6 syntax with [ip address]:port */
543         if ( *url == '[' ) {
544                 r = strchr( url, ']' );
545                 if ( r == NULL ) {
546                         LDAP_FREE( url );
547                         ldap_free_urldesc( ludp );
548                         return LDAP_URL_ERR_BADURL;
549                 }
550                 *r++ = '\0';
551                 q = strchr( r, ':' );
552         } else {
553                 q = strchr( url, ':' );
554         }
555
556         if ( q != NULL ) {
557                 char    *next;
558
559                 *q++ = '\0';
560                 ldap_pvt_hex_unescape( q );
561
562                 if( *q == '\0' ) {
563                         LDAP_FREE( url );
564                         ldap_free_urldesc( ludp );
565                         return LDAP_URL_ERR_BADURL;
566                 }
567
568                 ludp->lud_port = strtol( q, &next, 10 );
569                 if ( next == NULL || next[0] != '\0' ) {
570                         LDAP_FREE( url );
571                         ldap_free_urldesc( ludp );
572                         return LDAP_URL_ERR_BADURL;
573                 }
574         }
575
576         ldap_pvt_hex_unescape( url );
577
578         /* If [ip address]:port syntax, url is [ip and we skip the [ */
579         ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
580
581         if( ludp->lud_host == NULL ) {
582                 LDAP_FREE( url );
583                 ldap_free_urldesc( ludp );
584                 return LDAP_URL_ERR_MEM;
585         }
586
587         /*
588          * Kludge.  ldap://111.222.333.444:389??cn=abc,o=company
589          *
590          * On early Novell releases, search references/referrals were returned
591          * in this format, i.e., the dn was kind of in the scope position,
592          * but the required slash is missing. The whole thing is illegal syntax,
593          * but we need to account for it. Fortunately it can't be confused with
594          * anything real.
595          */
596         if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
597                 q++;            
598                 /* ? immediately followed by question */
599                 if( *q == '?') {
600                         q++;
601                         if( *q != '\0' ) {
602                                 /* parse dn part */
603                                 ldap_pvt_hex_unescape( q );
604                                 ludp->lud_dn = LDAP_STRDUP( q );
605                         } else {
606                                 ludp->lud_dn = LDAP_STRDUP( "" );
607                         }
608
609                         if( ludp->lud_dn == NULL ) {
610                                 LDAP_FREE( url );
611                                 ldap_free_urldesc( ludp );
612                                 return LDAP_URL_ERR_MEM;
613                         }
614                 }
615         }
616
617         if( p == NULL ) {
618                 LDAP_FREE( url );
619                 *ludpp = ludp;
620                 return LDAP_URL_SUCCESS;
621         }
622
623         /* scan forward for '?' that may marks end of dn */
624         q = strchr( p, '?' );
625
626         if( q != NULL ) {
627                 /* terminate dn part */
628                 *q++ = '\0';
629         }
630
631         if( *p != '\0' ) {
632                 /* parse dn part */
633                 ldap_pvt_hex_unescape( p );
634                 ludp->lud_dn = LDAP_STRDUP( p );
635         } else {
636                 ludp->lud_dn = LDAP_STRDUP( "" );
637         }
638
639         if( ludp->lud_dn == NULL ) {
640                 LDAP_FREE( url );
641                 ldap_free_urldesc( ludp );
642                 return LDAP_URL_ERR_MEM;
643         }
644
645         if( q == NULL ) {
646                 /* no more */
647                 LDAP_FREE( url );
648                 *ludpp = ludp;
649                 return LDAP_URL_SUCCESS;
650         }
651
652         /* scan forward for '?' that may marks end of attributes */
653         p = q;
654         q = strchr( p, '?' );
655
656         if( q != NULL ) {
657                 /* terminate attributes part */
658                 *q++ = '\0';
659         }
660
661         if( *p != '\0' ) {
662                 /* parse attributes */
663                 ldap_pvt_hex_unescape( p );
664                 ludp->lud_attrs = ldap_str2charray( p, "," );
665
666                 if( ludp->lud_attrs == NULL ) {
667                         LDAP_FREE( url );
668                         ldap_free_urldesc( ludp );
669                         return LDAP_URL_ERR_BADATTRS;
670                 }
671         }
672
673         if ( q == NULL ) {
674                 /* no more */
675                 LDAP_FREE( url );
676                 *ludpp = ludp;
677                 return LDAP_URL_SUCCESS;
678         }
679
680         /* scan forward for '?' that may marks end of scope */
681         p = q;
682         q = strchr( p, '?' );
683
684         if( q != NULL ) {
685                 /* terminate the scope part */
686                 *q++ = '\0';
687         }
688
689         if( *p != '\0' ) {
690                 /* parse the scope */
691                 ldap_pvt_hex_unescape( p );
692                 ludp->lud_scope = str2scope( p );
693
694                 if( ludp->lud_scope == -1 ) {
695                         LDAP_FREE( url );
696                         ldap_free_urldesc( ludp );
697                         return LDAP_URL_ERR_BADSCOPE;
698                 }
699         }
700
701         if ( q == NULL ) {
702                 /* no more */
703                 LDAP_FREE( url );
704                 *ludpp = ludp;
705                 return LDAP_URL_SUCCESS;
706         }
707
708         /* scan forward for '?' that may marks end of filter */
709         p = q;
710         q = strchr( p, '?' );
711
712         if( q != NULL ) {
713                 /* terminate the filter part */
714                 *q++ = '\0';
715         }
716
717         if( *p != '\0' ) {
718                 /* parse the filter */
719                 ldap_pvt_hex_unescape( p );
720
721                 if( ! *p ) {
722                         /* missing filter */
723                         LDAP_FREE( url );
724                         ldap_free_urldesc( ludp );
725                         return LDAP_URL_ERR_BADFILTER;
726                 }
727
728                 LDAP_FREE( ludp->lud_filter );
729                 ludp->lud_filter = LDAP_STRDUP( p );
730
731                 if( ludp->lud_filter == NULL ) {
732                         LDAP_FREE( url );
733                         ldap_free_urldesc( ludp );
734                         return LDAP_URL_ERR_MEM;
735                 }
736         }
737
738         if ( q == NULL ) {
739                 /* no more */
740                 LDAP_FREE( url );
741                 *ludpp = ludp;
742                 return LDAP_URL_SUCCESS;
743         }
744
745         /* scan forward for '?' that may marks end of extensions */
746         p = q;
747         q = strchr( p, '?' );
748
749         if( q != NULL ) {
750                 /* extra '?' */
751                 LDAP_FREE( url );
752                 ldap_free_urldesc( ludp );
753                 return LDAP_URL_ERR_BADURL;
754         }
755
756         /* parse the extensions */
757         ludp->lud_exts = ldap_str2charray( p, "," );
758
759         if( ludp->lud_exts == NULL ) {
760                 LDAP_FREE( url );
761                 ldap_free_urldesc( ludp );
762                 return LDAP_URL_ERR_BADEXTS;
763         }
764
765         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
766                 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
767
768                 if( *ludp->lud_exts[i] == '!' ) {
769                         /* count the number of critical extensions */
770                         ludp->lud_crit_exts++;
771                 }
772         }
773
774         if( i == 0 ) {
775                 /* must have 1 or more */
776                 LDAP_FREE( url );
777                 ldap_free_urldesc( ludp );
778                 return LDAP_URL_ERR_BADEXTS;
779         }
780
781         /* no more */
782         *ludpp = ludp;
783         LDAP_FREE( url );
784         return LDAP_URL_SUCCESS;
785 }
786
787 int
788 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
789 {
790         int rc = ldap_url_parse_ext( url_in, ludpp );
791
792         if( rc != LDAP_URL_SUCCESS ) {
793                 return rc;
794         }
795
796         if ((*ludpp)->lud_scope == LDAP_SCOPE_DEFAULT) {
797                 (*ludpp)->lud_scope = LDAP_SCOPE_BASE;
798         }
799
800         if ((*ludpp)->lud_host != NULL && *(*ludpp)->lud_host == '\0') {
801                 LDAP_FREE( (*ludpp)->lud_host );
802                 (*ludpp)->lud_host = NULL;
803         }
804
805         if ((*ludpp)->lud_port == 0) {
806                 if( strcmp((*ludpp)->lud_scheme, "ldap") == 0 ) {
807                         (*ludpp)->lud_port = LDAP_PORT;
808 #ifdef LDAP_CONNECTIONLESS
809                 } else if( strcmp((*ludpp)->lud_scheme, "cldap") == 0 ) {
810                         (*ludpp)->lud_port = LDAP_PORT;
811 #endif
812                 } else if( strcmp((*ludpp)->lud_scheme, "ldaps") == 0 ) {
813                         (*ludpp)->lud_port = LDAPS_PORT;
814                 }
815         }
816
817         return rc;
818 }
819
820 LDAPURLDesc *
821 ldap_url_dup ( LDAPURLDesc *ludp )
822 {
823         LDAPURLDesc *dest;
824
825         if ( ludp == NULL ) {
826                 return NULL;
827         }
828
829         dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
830         if (dest == NULL)
831                 return NULL;
832         
833         *dest = *ludp;
834         dest->lud_scheme = NULL;
835         dest->lud_host = NULL;
836         dest->lud_dn = NULL;
837         dest->lud_filter = NULL;
838         dest->lud_attrs = NULL;
839         dest->lud_exts = NULL;
840         dest->lud_next = NULL;
841
842         if ( ludp->lud_scheme != NULL ) {
843                 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
844                 if (dest->lud_scheme == NULL) {
845                         ldap_free_urldesc(dest);
846                         return NULL;
847                 }
848         }
849
850         if ( ludp->lud_host != NULL ) {
851                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
852                 if (dest->lud_host == NULL) {
853                         ldap_free_urldesc(dest);
854                         return NULL;
855                 }
856         }
857
858         if ( ludp->lud_dn != NULL ) {
859                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
860                 if (dest->lud_dn == NULL) {
861                         ldap_free_urldesc(dest);
862                         return NULL;
863                 }
864         }
865
866         if ( ludp->lud_filter != NULL ) {
867                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
868                 if (dest->lud_filter == NULL) {
869                         ldap_free_urldesc(dest);
870                         return NULL;
871                 }
872         }
873
874         if ( ludp->lud_attrs != NULL ) {
875                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
876                 if (dest->lud_attrs == NULL) {
877                         ldap_free_urldesc(dest);
878                         return NULL;
879                 }
880         }
881
882         if ( ludp->lud_exts != NULL ) {
883                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
884                 if (dest->lud_exts == NULL) {
885                         ldap_free_urldesc(dest);
886                         return NULL;
887                 }
888         }
889
890         return dest;
891 }
892
893 LDAPURLDesc *
894 ldap_url_duplist (LDAPURLDesc *ludlist)
895 {
896         LDAPURLDesc *dest, *tail, *ludp, *newludp;
897
898         dest = NULL;
899         tail = NULL;
900         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
901                 newludp = ldap_url_dup(ludp);
902                 if (newludp == NULL) {
903                         ldap_free_urllist(dest);
904                         return NULL;
905                 }
906                 if (tail == NULL)
907                         dest = newludp;
908                 else
909                         tail->lud_next = newludp;
910                 tail = newludp;
911         }
912         return dest;
913 }
914
915 int
916 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
917 {
918         return ldap_url_parselist_ext( ludlist, url, ", " );
919 }
920
921 int
922 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep )
923 {
924         int i, rc;
925         LDAPURLDesc *ludp;
926         char **urls;
927
928         assert( ludlist != NULL );
929         assert( url != NULL );
930
931         *ludlist = NULL;
932
933         urls = ldap_str2charray(url, sep);
934         if (urls == NULL)
935                 return LDAP_NO_MEMORY;
936
937         /* count the URLs... */
938         for (i = 0; urls[i] != NULL; i++) ;
939         /* ...and put them in the "stack" backward */
940         while (--i >= 0) {
941                 rc = ldap_url_parse( urls[i], &ludp );
942                 if ( rc != 0 ) {
943                         ldap_charray_free(urls);
944                         ldap_free_urllist(*ludlist);
945                         *ludlist = NULL;
946                         return rc;
947                 }
948                 ludp->lud_next = *ludlist;
949                 *ludlist = ludp;
950         }
951         ldap_charray_free(urls);
952         return LDAP_SUCCESS;
953 }
954
955 int
956 ldap_url_parsehosts(
957         LDAPURLDesc **ludlist,
958         const char *hosts,
959         int port )
960 {
961         int i;
962         LDAPURLDesc *ludp;
963         char **specs, *p;
964
965         assert( ludlist != NULL );
966         assert( hosts != NULL );
967
968         *ludlist = NULL;
969
970         specs = ldap_str2charray(hosts, ", ");
971         if (specs == NULL)
972                 return LDAP_NO_MEMORY;
973
974         /* count the URLs... */
975         for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
976
977         /* ...and put them in the "stack" backward */
978         while (--i >= 0) {
979                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
980                 if (ludp == NULL) {
981                         ldap_charray_free(specs);
982                         ldap_free_urllist(*ludlist);
983                         *ludlist = NULL;
984                         return LDAP_NO_MEMORY;
985                 }
986                 ludp->lud_port = port;
987                 ludp->lud_host = specs[i];
988                 specs[i] = NULL;
989                 p = strchr(ludp->lud_host, ':');
990                 if (p != NULL) {
991                         /* more than one :, IPv6 address */
992                         if ( strchr(p+1, ':') != NULL ) {
993                                 /* allow [address] and [address]:port */
994                                 if ( *ludp->lud_host == '[' ) {
995                                         p = LDAP_STRDUP(ludp->lud_host+1);
996                                         /* copied, make sure we free source later */
997                                         specs[i] = ludp->lud_host;
998                                         ludp->lud_host = p;
999                                         p = strchr( ludp->lud_host, ']' );
1000                                         if ( p == NULL )
1001                                                 return LDAP_PARAM_ERROR;
1002                                         *p++ = '\0';
1003                                         if ( *p != ':' ) {
1004                                                 if ( *p != '\0' )
1005                                                         return LDAP_PARAM_ERROR;
1006                                                 p = NULL;
1007                                         }
1008                                 } else {
1009                                         p = NULL;
1010                                 }
1011                         }
1012                         if (p != NULL) {
1013                                 char    *next;
1014
1015                                 *p++ = 0;
1016                                 ldap_pvt_hex_unescape(p);
1017                                 ludp->lud_port = strtol( p, &next, 10 );
1018                                 if ( next == NULL || next[0] != '\0' ) {
1019                                         return LDAP_PARAM_ERROR;
1020                                 }
1021                         }
1022                 }
1023                 ldap_pvt_hex_unescape(ludp->lud_host);
1024                 ludp->lud_scheme = LDAP_STRDUP("ldap");
1025                 ludp->lud_next = *ludlist;
1026                 *ludlist = ludp;
1027         }
1028
1029         /* this should be an array of NULLs now */
1030         /* except entries starting with [ */
1031         ldap_charray_free(specs);
1032         return LDAP_SUCCESS;
1033 }
1034
1035 char *
1036 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1037 {
1038         LDAPURLDesc *ludp;
1039         int size;
1040         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
1041
1042         if (ludlist == NULL)
1043                 return NULL;
1044
1045         /* figure out how big the string is */
1046         size = 1;       /* nul-term */
1047         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1048                 size += strlen(ludp->lud_host) + 1;             /* host and space */
1049                 if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
1050                         size += 2;
1051                 if (ludp->lud_port != 0)
1052                         size += sprintf(buf, ":%d", ludp->lud_port);
1053         }
1054         s = LDAP_MALLOC(size);
1055         if (s == NULL)
1056                 return NULL;
1057
1058         p = s;
1059         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1060                 if (strchr(ludp->lud_host, ':')) {
1061                         p += sprintf(p, "[%s]", ludp->lud_host);
1062                 } else {
1063                         strcpy(p, ludp->lud_host);
1064                         p += strlen(ludp->lud_host);
1065                 }
1066                 if (ludp->lud_port != 0)
1067                         p += sprintf(p, ":%d", ludp->lud_port);
1068                 *p++ = ' ';
1069         }
1070         if (p != s)
1071                 p--;    /* nuke that extra space */
1072         *p = 0;
1073         return s;
1074 }
1075
1076 char *
1077 ldap_url_list2urls(
1078         LDAPURLDesc *ludlist )
1079 {
1080         LDAPURLDesc *ludp;
1081         int size;
1082         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
1083
1084         if (ludlist == NULL)
1085                 return NULL;
1086
1087         /* figure out how big the string is */
1088         size = 1;       /* nul-term */
1089         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1090                 size += strlen(ludp->lud_scheme);
1091                 if ( ludp->lud_host ) {
1092                         size += strlen(ludp->lud_host);
1093                         /* will add [ ] below */
1094                         if (strchr(ludp->lud_host, ':'))
1095                                 size += 2;
1096                 }
1097                 size += sizeof(":/// ");
1098
1099                 if (ludp->lud_port != 0) {
1100                         size += sprintf(buf, ":%d", ludp->lud_port);
1101                 }
1102         }
1103
1104         s = LDAP_MALLOC(size);
1105         if (s == NULL) {
1106                 return NULL;
1107         }
1108
1109         p = s;
1110         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1111                 p += sprintf(p, "%s://", ludp->lud_scheme);
1112                 if ( ludp->lud_host ) {
1113                         p += sprintf(p, strchr(ludp->lud_host, ':') 
1114                                         ? "[%s]" : "%s", ludp->lud_host);
1115                 }
1116                 if (ludp->lud_port != 0)
1117                         p += sprintf(p, ":%d", ludp->lud_port);
1118                 *p++ = '/';
1119                 *p++ = ' ';
1120         }
1121         if (p != s)
1122                 p--;    /* nuke that extra space */
1123         *p = 0;
1124         return s;
1125 }
1126
1127 void
1128 ldap_free_urllist( LDAPURLDesc *ludlist )
1129 {
1130         LDAPURLDesc *ludp, *next;
1131
1132         for (ludp = ludlist; ludp != NULL; ludp = next) {
1133                 next = ludp->lud_next;
1134                 ldap_free_urldesc(ludp);
1135         }
1136 }
1137
1138 void
1139 ldap_free_urldesc( LDAPURLDesc *ludp )
1140 {
1141         if ( ludp == NULL ) {
1142                 return;
1143         }
1144         
1145         if ( ludp->lud_scheme != NULL ) {
1146                 LDAP_FREE( ludp->lud_scheme );
1147         }
1148
1149         if ( ludp->lud_host != NULL ) {
1150                 LDAP_FREE( ludp->lud_host );
1151         }
1152
1153         if ( ludp->lud_dn != NULL ) {
1154                 LDAP_FREE( ludp->lud_dn );
1155         }
1156
1157         if ( ludp->lud_filter != NULL ) {
1158                 LDAP_FREE( ludp->lud_filter);
1159         }
1160
1161         if ( ludp->lud_attrs != NULL ) {
1162                 LDAP_VFREE( ludp->lud_attrs );
1163         }
1164
1165         if ( ludp->lud_exts != NULL ) {
1166                 LDAP_VFREE( ludp->lud_exts );
1167         }
1168
1169         LDAP_FREE( ludp );
1170 }
1171
1172 static int
1173 ldap_int_unhex( int c )
1174 {
1175         return( c >= '0' && c <= '9' ? c - '0'
1176             : c >= 'A' && c <= 'F' ? c - 'A' + 10
1177             : c - 'a' + 10 );
1178 }
1179
1180 void
1181 ldap_pvt_hex_unescape( char *s )
1182 {
1183         /*
1184          * Remove URL hex escapes from s... done in place.  The basic concept for
1185          * this routine is borrowed from the WWW library HTUnEscape() routine.
1186          */
1187         char    *p;
1188
1189         for ( p = s; *s != '\0'; ++s ) {
1190                 if ( *s == '%' ) {
1191                         if ( *++s == '\0' ) {
1192                                 break;
1193                         }
1194                         *p = ldap_int_unhex( *s ) << 4;
1195                         if ( *++s == '\0' ) {
1196                                 break;
1197                         }
1198                         *p++ += ldap_int_unhex( *s );
1199                 } else {
1200                         *p++ = *s;
1201                 }
1202         }
1203
1204         *p = '\0';
1205 }
1206
1207