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