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