]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
df9be5e0a23f25c0d218c13da8b481ece52e1c2e
[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-2006 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 #include <ac/ctype.h>
41
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45
46 #include "ldap-int.h"
47
48 /* local functions */
49 static const char* skip_url_prefix LDAP_P((
50         const char *url,
51         int *enclosedp,
52         const char **scheme ));
53
54 int ldap_pvt_url_scheme2proto( const char *scheme )
55 {
56         assert( scheme != NULL );
57
58         if( scheme == NULL ) {
59                 return -1;
60         }
61
62         if( strcmp("ldap", scheme) == 0 ) {
63                 return LDAP_PROTO_TCP;
64         }
65
66         if( strcmp("ldapi", scheme) == 0 ) {
67                 return LDAP_PROTO_IPC;
68         }
69
70         if( strcmp("ldaps", scheme) == 0 ) {
71                 return LDAP_PROTO_TCP;
72         }
73 #ifdef LDAP_CONNECTIONLESS
74         if( strcmp("cldap", scheme) == 0 ) {
75                 return LDAP_PROTO_UDP;
76         }
77 #endif
78
79         return -1;
80 }
81
82 int ldap_pvt_url_scheme_port( const char *scheme, int port )
83 {
84         assert( scheme != NULL );
85
86         if( port ) return port;
87         if( scheme == NULL ) return port;
88
89         if( strcmp("ldap", scheme) == 0 ) {
90                 return LDAP_PORT;
91         }
92
93         if( strcmp("ldapi", scheme) == 0 ) {
94                 return -1;
95         }
96
97         if( strcmp("ldaps", scheme) == 0 ) {
98                 return LDAPS_PORT;
99         }
100
101 #ifdef LDAP_CONNECTIONLESS
102         if( strcmp("cldap", scheme) == 0 ) {
103                 return LDAP_PORT;
104         }
105 #endif
106
107         return -1;
108 }
109
110 int
111 ldap_pvt_url_scheme2tls( const char *scheme )
112 {
113         assert( scheme != NULL );
114
115         if( scheme == NULL ) {
116                 return -1;
117         }
118
119         return strcmp("ldaps", scheme) == 0;
120 }
121
122 int
123 ldap_is_ldap_url( LDAP_CONST char *url )
124 {
125         int     enclosed;
126         const char * scheme;
127
128         if( url == NULL ) {
129                 return 0;
130         }
131
132         if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
133                 return 0;
134         }
135
136         return 1;
137 }
138
139 int
140 ldap_is_ldaps_url( LDAP_CONST char *url )
141 {
142         int     enclosed;
143         const char * scheme;
144
145         if( url == NULL ) {
146                 return 0;
147         }
148
149         if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
150                 return 0;
151         }
152
153         return strcmp(scheme, "ldaps") == 0;
154 }
155
156 int
157 ldap_is_ldapi_url( LDAP_CONST char *url )
158 {
159         int     enclosed;
160         const char * scheme;
161
162         if( url == NULL ) {
163                 return 0;
164         }
165
166         if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
167                 return 0;
168         }
169
170         return strcmp(scheme, "ldapi") == 0;
171 }
172
173 #ifdef LDAP_CONNECTIONLESS
174 int
175 ldap_is_ldapc_url( LDAP_CONST char *url )
176 {
177         int     enclosed;
178         const char * scheme;
179
180         if( url == NULL ) {
181                 return 0;
182         }
183
184         if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
185                 return 0;
186         }
187
188         return strcmp(scheme, "cldap") == 0;
189 }
190 #endif
191
192 static const char*
193 skip_url_prefix(
194         const char *url,
195         int *enclosedp,
196         const char **scheme )
197 {
198         /*
199          * return non-zero if this looks like a LDAP URL; zero if not
200          * if non-zero returned, *urlp will be moved past "ldap://" part of URL
201          */
202         const char *p;
203
204         if ( url == NULL ) {
205                 return( NULL );
206         }
207
208         p = url;
209
210         /* skip leading '<' (if any) */
211         if ( *p == '<' ) {
212                 *enclosedp = 1;
213                 ++p;
214         } else {
215                 *enclosedp = 0;
216         }
217
218         /* skip leading "URL:" (if any) */
219         if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
220                 p += LDAP_URL_URLCOLON_LEN;
221         }
222
223         /* check for "ldap://" prefix */
224         if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
225                 /* skip over "ldap://" prefix and return success */
226                 p += LDAP_URL_PREFIX_LEN;
227                 *scheme = "ldap";
228                 return( p );
229         }
230
231         /* check for "ldaps://" prefix */
232         if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
233                 /* skip over "ldaps://" prefix and return success */
234                 p += LDAPS_URL_PREFIX_LEN;
235                 *scheme = "ldaps";
236                 return( p );
237         }
238
239         /* check for "ldapi://" prefix */
240         if ( strncasecmp( p, LDAPI_URL_PREFIX, LDAPI_URL_PREFIX_LEN ) == 0 ) {
241                 /* skip over "ldapi://" prefix and return success */
242                 p += LDAPI_URL_PREFIX_LEN;
243                 *scheme = "ldapi";
244                 return( p );
245         }
246
247 #ifdef LDAP_CONNECTIONLESS
248         /* check for "cldap://" prefix */
249         if ( strncasecmp( p, LDAPC_URL_PREFIX, LDAPC_URL_PREFIX_LEN ) == 0 ) {
250                 /* skip over "cldap://" prefix and return success */
251                 p += LDAPC_URL_PREFIX_LEN;
252                 *scheme = "cldap";
253                 return( p );
254         }
255 #endif
256
257         return( NULL );
258 }
259
260 int
261 ldap_pvt_scope2bv( int scope, struct berval *bv )
262 {
263         switch ( scope ) {
264         case LDAP_SCOPE_BASE:
265                 BER_BVSTR( bv, "base" );
266                 break;
267
268         case LDAP_SCOPE_ONELEVEL:
269                 BER_BVSTR( bv, "one" );
270                 break;
271
272         case LDAP_SCOPE_SUBTREE:
273                 BER_BVSTR( bv, "sub" );
274                 break;
275
276         case LDAP_SCOPE_SUBORDINATE:
277                 BER_BVSTR( bv, "subordinate" );
278                 break;
279
280         default:
281                 return LDAP_OTHER;
282         }
283
284         return LDAP_SUCCESS;
285 }
286
287 const char *
288 ldap_pvt_scope2str( int scope )
289 {
290         struct berval   bv;
291
292         if ( ldap_pvt_scope2bv( scope, &bv ) == LDAP_SUCCESS ) {
293                 return bv.bv_val;
294         }
295
296         return NULL;
297 }
298
299 int
300 ldap_pvt_bv2scope( struct berval *bv )
301 {
302         static struct {
303                 struct berval   bv;
304                 int             scope;
305         }       v[] = {
306                 { BER_BVC( "one" ),             LDAP_SCOPE_ONELEVEL },
307                 { BER_BVC( "onelevel" ),        LDAP_SCOPE_ONELEVEL },
308                 { BER_BVC( "base" ),            LDAP_SCOPE_BASE },
309                 { BER_BVC( "sub" ),             LDAP_SCOPE_SUBTREE },
310                 { BER_BVC( "subtree" ),         LDAP_SCOPE_SUBTREE },
311                 { BER_BVC( "subord" ),          LDAP_SCOPE_SUBORDINATE },
312                 { BER_BVC( "subordinate" ),     LDAP_SCOPE_SUBORDINATE },
313                 { BER_BVC( "children" ),        LDAP_SCOPE_SUBORDINATE },
314                 { BER_BVNULL,                   -1 }
315         };
316         int     i;
317
318         for ( i = 0; v[ i ].scope != -1; i++ ) {
319                 if ( ber_bvstrcasecmp( bv, &v[ i ].bv ) == 0 ) {
320                         return v[ i ].scope;
321                 }
322         }
323
324         return( -1 );
325 }
326
327 int
328 ldap_pvt_str2scope( const char *p )
329 {
330         struct berval   bv;
331
332         ber_str2bv( p, 0, 0, &bv );
333
334         return ldap_pvt_bv2scope( &bv );
335 }
336
337 static const char       hex[] = "0123456789ABCDEF";
338
339 #define URLESC_NONE     0x0000U
340 #define URLESC_COMMA    0x0001U
341 #define URLESC_SLASH    0x0002U
342
343 static int
344 hex_escape_len( const char *s, unsigned list )
345 {
346         int     len;
347
348         if ( s == NULL ) {
349                 return 0;
350         }
351
352         for ( len = 0; s[0]; s++ ) {
353                 switch ( s[0] ) {
354                 /* RFC 2396: reserved */
355                 case '?':
356                         len += 3;
357                         break;
358
359                 case ',':
360                         if ( list & URLESC_COMMA ) {
361                                 len += 3;
362                         } else {
363                                 len++;
364                         }
365                         break;
366
367                 case '/':
368                         if ( list & URLESC_SLASH ) {
369                                 len += 3;
370                         } else {
371                                 len++;
372                         }
373                         break;
374
375                 case ';':
376                 case ':':
377                 case '@':
378                 case '&':
379                 case '=':
380                 case '+':
381                 case '$':
382
383                 /* RFC 2396: unreserved mark */
384                 case '-':
385                 case '_':
386                 case '.':
387                 case '!':
388                 case '~':
389                 case '*':
390                 case '\'':
391                 case '(':
392                 case ')':
393                         len++;
394                         break;
395                         
396                 /* RFC 2396: unreserved alphanum */
397                 default:
398                         if ( !isalnum( (unsigned char) s[0] ) ) {
399                                 len += 3;
400                         } else {
401                                 len++;
402                         }
403                         break;
404                 }
405         }
406
407         return len;
408 }
409
410 static int
411 hex_escape( char *buf, int len, const char *s, unsigned list )
412 {
413         int     i;
414         int     pos;
415
416         if ( s == NULL ) {
417                 return 0;
418         }
419
420         for ( pos = 0, i = 0; s[i] && pos < len; i++ ) {
421                 int     escape = 0;
422
423                 switch ( s[i] ) {
424                 /* RFC 2396: reserved */
425                 case '?':
426                         escape = 1;
427                         break;
428
429                 case ',':
430                         if ( list & URLESC_COMMA ) {
431                                 escape = 1;
432                         }
433                         break;
434
435                 case '/':
436                         if ( list & URLESC_SLASH ) {
437                                 escape = 1;
438                         }
439                         break;
440
441                 case ';':
442                 case ':':
443                 case '@':
444                 case '&':
445                 case '=':
446                 case '+':
447                 case '$':
448
449                 /* RFC 2396: unreserved mark */
450                 case '-':
451                 case '_':
452                 case '.':
453                 case '!':
454                 case '~':
455                 case '*':
456                 case '\'':
457                 case '(':
458                 case ')':
459                         break;
460                         
461                 /* RFC 2396: unreserved alphanum */
462                 default:
463                         if ( !isalnum( (unsigned char) s[i] ) ) {
464                                 escape = 1;
465                         }
466                         break;
467                 }
468
469                 if ( escape ) {
470                         buf[pos++] = '%';
471                         buf[pos++] = hex[ (s[i] >> 4) & 0x0f ];
472                         buf[pos++] = hex[ s[i] & 0x0f ];
473
474                 } else {
475                         buf[pos++] = s[i];
476                 }
477         }
478
479         buf[pos] = '\0';
480
481         return pos;
482 }
483
484 static int
485 hex_escape_len_list( char **s, unsigned flags )
486 {
487         int     len;
488         int     i;
489
490         if ( s == NULL ) {
491                 return 0;
492         }
493
494         len = 0;
495         for ( i = 0; s[i] != NULL; i++ ) {
496                 if ( len ) {
497                         len++;
498                 }
499                 len += hex_escape_len( s[i], flags );
500         }
501
502         return len;
503 }
504
505 static int
506 hex_escape_list( char *buf, int len, char **s, unsigned flags )
507 {
508         int     pos;
509         int     i;
510
511         if ( s == NULL ) {
512                 return 0;
513         }
514
515         pos = 0;
516         for ( i = 0; s[i] != NULL; i++ ) {
517                 int     curlen;
518
519                 if ( pos ) {
520                         buf[pos++] = ',';
521                         len--;
522                 }
523                 curlen = hex_escape( &buf[pos], len, s[i], flags );
524                 len -= curlen;
525                 pos += curlen;
526         }
527
528         return pos;
529 }
530
531 static int
532 desc2str_len( LDAPURLDesc *u )
533 {
534         int             sep = 0;
535         int             len = 0;
536         struct berval   scope;
537
538         if ( u == NULL ) {
539                 return -1;
540         }
541
542         if ( u->lud_exts ) {
543                 len += hex_escape_len_list( u->lud_exts, URLESC_COMMA );
544                 if ( !sep ) {
545                         sep = 5;
546                 }
547         }
548
549         if ( u->lud_filter ) {
550                 len +=  hex_escape_len( u->lud_filter, URLESC_NONE );
551                 if ( !sep ) {
552                         sep = 4;
553                 }
554         }
555
556         if ( ldap_pvt_scope2bv( u->lud_scope, &scope ) == LDAP_SUCCESS ) {
557                 len += scope.bv_len;
558                 if ( !sep ) {
559                         sep = 3;
560                 }
561         }
562
563         if ( u->lud_attrs ) {
564                 len +=  hex_escape_len_list( u->lud_attrs, URLESC_NONE );
565                 if ( !sep ) {
566                         sep = 2;
567                 }
568         }
569
570         if ( u->lud_dn && u->lud_dn[0] ) {
571                 len += hex_escape_len( u->lud_dn, URLESC_NONE );
572                 if ( !sep ) {
573                         sep = 1;
574                 }
575         };
576
577         len += sep;
578
579         if ( u->lud_port ) {
580                 char    buf[] = ":65535";
581
582                 len += snprintf( buf, sizeof( buf ), ":%d", u->lud_port );
583                 if ( u->lud_host && u->lud_host[0] ) {
584                         len += strlen( u->lud_host );
585                 }
586
587         } else {
588                 if ( u->lud_host && u->lud_host[0] ) {
589                         len += hex_escape_len( u->lud_host, URLESC_SLASH );
590                 }
591         }
592
593         len += strlen( u->lud_scheme ) + STRLENOF( "://" );
594
595         return len;
596 }
597
598 int
599 desc2str( LDAPURLDesc *u, char *s, int len )
600 {
601         int             i;
602         int             sep = 0;
603         int             sofar = 0;
604         struct berval   scope = BER_BVNULL;
605
606         if ( u == NULL ) {
607                 return -1;
608         }
609
610         if ( s == NULL ) {
611                 return -1;
612         }
613
614         ldap_pvt_scope2bv( u->lud_scope, &scope );
615
616         if ( u->lud_exts ) {
617                 sep = 5;
618         } else if ( u->lud_filter ) {
619                 sep = 4;
620         } else if ( !BER_BVISEMPTY( &scope ) ) {
621                 sep = 3;
622         } else if ( u->lud_attrs ) {
623                 sep = 2;
624         } else if ( u->lud_dn && u->lud_dn[0] ) {
625                 sep = 1;
626         }
627
628         if ( u->lud_port ) {
629                 len -= sprintf( s, "%s://%s:%d%n", u->lud_scheme,
630                                 u->lud_host ? u->lud_host : "",
631                                 u->lud_port, &sofar );
632
633         } else {
634                 len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar );
635                 if ( u->lud_host && u->lud_host[0] ) {
636                         i = hex_escape( &s[sofar], len, u->lud_host, URLESC_SLASH );
637                         sofar += i;
638                         len -= i;
639                 }
640         }
641
642         assert( len >= 0 );
643
644         if ( sep < 1 ) {
645                 goto done;
646         }
647
648         s[sofar++] = '/';
649         len--;
650
651         assert( len >= 0 );
652
653         if ( u->lud_dn && u->lud_dn[0] ) {
654                 i = hex_escape( &s[sofar], len, u->lud_dn, URLESC_NONE );
655                 sofar += i;
656                 len -= i;
657
658                 assert( len >= 0 );
659         }
660
661         if ( sep < 2 ) {
662                 goto done;
663         }
664         s[sofar++] = '?';
665         len--;
666
667         assert( len >= 0 );
668
669         i = hex_escape_list( &s[sofar], len, u->lud_attrs, URLESC_NONE );
670         sofar += i;
671         len -= i;
672
673         assert( len >= 0 );
674
675         if ( sep < 3 ) {
676                 goto done;
677         }
678         s[sofar++] = '?';
679         len--;
680
681         assert( len >= 0 );
682
683         if ( !BER_BVISNULL( &scope ) ) {
684                 strcpy( &s[sofar], scope.bv_val );
685                 sofar += scope.bv_len;
686                 len -= scope.bv_len;
687         }
688
689         assert( len >= 0 );
690
691         if ( sep < 4 ) {
692                 goto done;
693         }
694         s[sofar++] = '?';
695         len--;
696
697         assert( len >= 0 );
698
699         i = hex_escape( &s[sofar], len, u->lud_filter, URLESC_NONE );
700         sofar += i;
701         len -= i;
702
703         assert( len >= 0 );
704
705         if ( sep < 5 ) {
706                 goto done;
707         }
708         s[sofar++] = '?';
709         len--;
710
711         assert( len >= 0 );
712
713         i = hex_escape_list( &s[sofar], len, u->lud_exts, URLESC_COMMA );
714         sofar += i;
715         len -= i;
716
717         assert( len >= 0 );
718
719 done:
720         if ( len < 0 ) {
721                 return -1;
722         }
723
724         return sofar;
725 }
726
727 char *
728 ldap_url_desc2str( LDAPURLDesc *u )
729 {
730         int     len;
731         char    *s;
732
733         if ( u == NULL ) {
734                 return NULL;
735         }
736
737         len = desc2str_len( u );
738         if ( len < 0 ) {
739                 return NULL;
740         }
741         
742         /* allocate enough to hex escape everything -- overkill */
743         s = LDAP_MALLOC( len + 1 );
744
745         if ( s == NULL ) {
746                 return NULL;
747         }
748
749         if ( desc2str( u, s, len ) != len ) {
750                 LDAP_FREE( s );
751                 return NULL;
752         }
753
754         s[len] = '\0';
755
756         return s;
757 }
758
759 int
760 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp, unsigned flags )
761 {
762 /*
763  *  Pick apart the pieces of an LDAP URL.
764  */
765
766         LDAPURLDesc     *ludp;
767         char    *p, *q, *r;
768         int             i, enclosed;
769         const char *scheme = NULL;
770         const char *url_tmp;
771         char *url;
772
773         int     check_dn = 1;
774
775         if( url_in == NULL || ludpp == NULL ) {
776                 return LDAP_URL_ERR_PARAM;
777         }
778
779 #ifndef LDAP_INT_IN_KERNEL
780         /* Global options may not be created yet
781          * We can't test if the global options are initialized
782          * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
783          * the options and cause infinite recursion
784          */
785         Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
786 #endif
787
788         *ludpp = NULL;  /* pessimistic */
789
790         url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
791
792         if ( url_tmp == NULL ) {
793                 return LDAP_URL_ERR_BADSCHEME;
794         }
795
796         assert( scheme != NULL );
797
798         /* make working copy of the remainder of the URL */
799         url = LDAP_STRDUP( url_tmp );
800         if ( url == NULL ) {
801                 return LDAP_URL_ERR_MEM;
802         }
803
804         if ( enclosed ) {
805                 p = &url[strlen(url)-1];
806
807                 if( *p != '>' ) {
808                         LDAP_FREE( url );
809                         return LDAP_URL_ERR_BADENCLOSURE;
810                 }
811
812                 *p = '\0';
813         }
814
815         /* allocate return struct */
816         ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
817
818         if ( ludp == NULL ) {
819                 LDAP_FREE( url );
820                 return LDAP_URL_ERR_MEM;
821         }
822
823         ludp->lud_next = NULL;
824         ludp->lud_host = NULL;
825         ludp->lud_port = 0;
826         ludp->lud_dn = NULL;
827         ludp->lud_attrs = NULL;
828         ludp->lud_scope = ( flags & LDAP_PVT_URL_PARSE_NODEF_SCOPE ) ? LDAP_SCOPE_BASE : LDAP_SCOPE_DEFAULT;
829         ludp->lud_filter = NULL;
830         ludp->lud_exts = NULL;
831
832         ludp->lud_scheme = LDAP_STRDUP( scheme );
833
834         if ( ludp->lud_scheme == NULL ) {
835                 LDAP_FREE( url );
836                 ldap_free_urldesc( ludp );
837                 return LDAP_URL_ERR_MEM;
838         }
839
840         /* scan forward for '/' that marks end of hostport and begin. of dn */
841         p = strchr( url, '/' );
842
843         if( p != NULL ) {
844                 /* terminate hostport; point to start of dn */
845                 *p++ = '\0';
846         }
847
848         /* IPv6 syntax with [ip address]:port */
849         if ( *url == '[' ) {
850                 r = strchr( url, ']' );
851                 if ( r == NULL ) {
852                         LDAP_FREE( url );
853                         ldap_free_urldesc( ludp );
854                         return LDAP_URL_ERR_BADURL;
855                 }
856                 *r++ = '\0';
857                 q = strchr( r, ':' );
858         } else {
859                 q = strchr( url, ':' );
860         }
861
862         if ( q != NULL ) {
863                 char    *next;
864
865                 *q++ = '\0';
866                 ldap_pvt_hex_unescape( q );
867
868                 if( *q == '\0' ) {
869                         LDAP_FREE( url );
870                         ldap_free_urldesc( ludp );
871                         return LDAP_URL_ERR_BADURL;
872                 }
873
874                 ludp->lud_port = strtol( q, &next, 10 );
875                 if ( next == q || next[0] != '\0' ) {
876                         LDAP_FREE( url );
877                         ldap_free_urldesc( ludp );
878                         return LDAP_URL_ERR_BADURL;
879                 }
880         }
881
882         if ( ( flags & LDAP_PVT_URL_PARSE_DEF_PORT ) && ludp->lud_port == 0 ) {
883                 if ( strcmp( ludp->lud_scheme, "ldap" ) == 0 ) {
884                         ludp->lud_port = LDAP_PORT;
885 #ifdef LDAP_CONNECTIONLESS
886                 } else if ( strcmp( ludp->lud_scheme, "cldap" ) == 0 ) {
887                         ludp->lud_port = LDAP_PORT;
888 #endif
889                 } else if ( strcmp( ludp->lud_scheme, "ldaps" ) == 0 ) {
890                         ludp->lud_port = LDAPS_PORT;
891                 }
892         }
893
894         ldap_pvt_hex_unescape( url );
895
896         /* If [ip address]:port syntax, url is [ip and we skip the [ */
897         ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
898
899         if( ludp->lud_host == NULL ) {
900                 LDAP_FREE( url );
901                 ldap_free_urldesc( ludp );
902                 return LDAP_URL_ERR_MEM;
903         }
904
905         if ( ( flags & LDAP_PVT_URL_PARSE_NOEMPTY_HOST )
906                 && ludp->lud_host != NULL
907                 && *ludp->lud_host == '\0' )
908         {
909                 LDAP_FREE( ludp->lud_host );
910                 ludp->lud_host = NULL;
911         }
912
913         /*
914          * Kludge.  ldap://111.222.333.444:389??cn=abc,o=company
915          *
916          * On early Novell releases, search references/referrals were returned
917          * in this format, i.e., the dn was kind of in the scope position,
918          * but the required slash is missing. The whole thing is illegal syntax,
919          * but we need to account for it. Fortunately it can't be confused with
920          * anything real.
921          */
922         if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
923                 q++;            
924                 /* ? immediately followed by question */
925                 if( *q == '?') {
926                         q++;
927                         if( *q != '\0' ) {
928                                 /* parse dn part */
929                                 ldap_pvt_hex_unescape( q );
930                                 ludp->lud_dn = LDAP_STRDUP( q );
931
932                         } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
933                                 ludp->lud_dn = LDAP_STRDUP( "" );
934
935                         } else {
936                                 check_dn = 0;
937                         }
938
939                         if ( check_dn && ludp->lud_dn == NULL ) {
940                                 LDAP_FREE( url );
941                                 ldap_free_urldesc( ludp );
942                                 return LDAP_URL_ERR_MEM;
943                         }
944                 }
945         }
946
947         if( p == NULL ) {
948                 LDAP_FREE( url );
949                 *ludpp = ludp;
950                 return LDAP_URL_SUCCESS;
951         }
952
953         /* scan forward for '?' that may marks end of dn */
954         q = strchr( p, '?' );
955
956         if( q != NULL ) {
957                 /* terminate dn part */
958                 *q++ = '\0';
959         }
960
961         if( *p != '\0' ) {
962                 /* parse dn part */
963                 ldap_pvt_hex_unescape( p );
964                 ludp->lud_dn = LDAP_STRDUP( p );
965
966         } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
967                 ludp->lud_dn = LDAP_STRDUP( "" );
968
969         } else {
970                 check_dn = 0;
971         }
972
973         if( check_dn && ludp->lud_dn == NULL ) {
974                 LDAP_FREE( url );
975                 ldap_free_urldesc( ludp );
976                 return LDAP_URL_ERR_MEM;
977         }
978
979         if( q == NULL ) {
980                 /* no more */
981                 LDAP_FREE( url );
982                 *ludpp = ludp;
983                 return LDAP_URL_SUCCESS;
984         }
985
986         /* scan forward for '?' that may marks end of attributes */
987         p = q;
988         q = strchr( p, '?' );
989
990         if( q != NULL ) {
991                 /* terminate attributes part */
992                 *q++ = '\0';
993         }
994
995         if( *p != '\0' ) {
996                 /* parse attributes */
997                 ldap_pvt_hex_unescape( p );
998                 ludp->lud_attrs = ldap_str2charray( p, "," );
999
1000                 if( ludp->lud_attrs == NULL ) {
1001                         LDAP_FREE( url );
1002                         ldap_free_urldesc( ludp );
1003                         return LDAP_URL_ERR_BADATTRS;
1004                 }
1005         }
1006
1007         if ( q == NULL ) {
1008                 /* no more */
1009                 LDAP_FREE( url );
1010                 *ludpp = ludp;
1011                 return LDAP_URL_SUCCESS;
1012         }
1013
1014         /* scan forward for '?' that may marks end of scope */
1015         p = q;
1016         q = strchr( p, '?' );
1017
1018         if( q != NULL ) {
1019                 /* terminate the scope part */
1020                 *q++ = '\0';
1021         }
1022
1023         if( *p != '\0' ) {
1024                 /* parse the scope */
1025                 ldap_pvt_hex_unescape( p );
1026                 ludp->lud_scope = ldap_pvt_str2scope( p );
1027
1028                 if( ludp->lud_scope == -1 ) {
1029                         LDAP_FREE( url );
1030                         ldap_free_urldesc( ludp );
1031                         return LDAP_URL_ERR_BADSCOPE;
1032                 }
1033         }
1034
1035         if ( q == NULL ) {
1036                 /* no more */
1037                 LDAP_FREE( url );
1038                 *ludpp = ludp;
1039                 return LDAP_URL_SUCCESS;
1040         }
1041
1042         /* scan forward for '?' that may marks end of filter */
1043         p = q;
1044         q = strchr( p, '?' );
1045
1046         if( q != NULL ) {
1047                 /* terminate the filter part */
1048                 *q++ = '\0';
1049         }
1050
1051         if( *p != '\0' ) {
1052                 /* parse the filter */
1053                 ldap_pvt_hex_unescape( p );
1054
1055                 if( ! *p ) {
1056                         /* missing filter */
1057                         LDAP_FREE( url );
1058                         ldap_free_urldesc( ludp );
1059                         return LDAP_URL_ERR_BADFILTER;
1060                 }
1061
1062                 ludp->lud_filter = LDAP_STRDUP( p );
1063
1064                 if( ludp->lud_filter == NULL ) {
1065                         LDAP_FREE( url );
1066                         ldap_free_urldesc( ludp );
1067                         return LDAP_URL_ERR_MEM;
1068                 }
1069         }
1070
1071         if ( q == NULL ) {
1072                 /* no more */
1073                 LDAP_FREE( url );
1074                 *ludpp = ludp;
1075                 return LDAP_URL_SUCCESS;
1076         }
1077
1078         /* scan forward for '?' that may marks end of extensions */
1079         p = q;
1080         q = strchr( p, '?' );
1081
1082         if( q != NULL ) {
1083                 /* extra '?' */
1084                 LDAP_FREE( url );
1085                 ldap_free_urldesc( ludp );
1086                 return LDAP_URL_ERR_BADURL;
1087         }
1088
1089         /* parse the extensions */
1090         ludp->lud_exts = ldap_str2charray( p, "," );
1091
1092         if( ludp->lud_exts == NULL ) {
1093                 LDAP_FREE( url );
1094                 ldap_free_urldesc( ludp );
1095                 return LDAP_URL_ERR_BADEXTS;
1096         }
1097
1098         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
1099                 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
1100
1101                 if( *ludp->lud_exts[i] == '!' ) {
1102                         /* count the number of critical extensions */
1103                         ludp->lud_crit_exts++;
1104                 }
1105         }
1106
1107         if( i == 0 ) {
1108                 /* must have 1 or more */
1109                 LDAP_FREE( url );
1110                 ldap_free_urldesc( ludp );
1111                 return LDAP_URL_ERR_BADEXTS;
1112         }
1113
1114         /* no more */
1115         *ludpp = ludp;
1116         LDAP_FREE( url );
1117         return LDAP_URL_SUCCESS;
1118 }
1119
1120 int
1121 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
1122 {
1123         return ldap_url_parse_ext( url_in, ludpp, LDAP_PVT_URL_PARSE_HISTORIC );
1124 }
1125
1126 LDAPURLDesc *
1127 ldap_url_dup ( LDAPURLDesc *ludp )
1128 {
1129         LDAPURLDesc *dest;
1130
1131         if ( ludp == NULL ) {
1132                 return NULL;
1133         }
1134
1135         dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
1136         if (dest == NULL)
1137                 return NULL;
1138         
1139         *dest = *ludp;
1140         dest->lud_scheme = NULL;
1141         dest->lud_host = NULL;
1142         dest->lud_dn = NULL;
1143         dest->lud_filter = NULL;
1144         dest->lud_attrs = NULL;
1145         dest->lud_exts = NULL;
1146         dest->lud_next = NULL;
1147
1148         if ( ludp->lud_scheme != NULL ) {
1149                 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
1150                 if (dest->lud_scheme == NULL) {
1151                         ldap_free_urldesc(dest);
1152                         return NULL;
1153                 }
1154         }
1155
1156         if ( ludp->lud_host != NULL ) {
1157                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
1158                 if (dest->lud_host == NULL) {
1159                         ldap_free_urldesc(dest);
1160                         return NULL;
1161                 }
1162         }
1163
1164         if ( ludp->lud_dn != NULL ) {
1165                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
1166                 if (dest->lud_dn == NULL) {
1167                         ldap_free_urldesc(dest);
1168                         return NULL;
1169                 }
1170         }
1171
1172         if ( ludp->lud_filter != NULL ) {
1173                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
1174                 if (dest->lud_filter == NULL) {
1175                         ldap_free_urldesc(dest);
1176                         return NULL;
1177                 }
1178         }
1179
1180         if ( ludp->lud_attrs != NULL ) {
1181                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
1182                 if (dest->lud_attrs == NULL) {
1183                         ldap_free_urldesc(dest);
1184                         return NULL;
1185                 }
1186         }
1187
1188         if ( ludp->lud_exts != NULL ) {
1189                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
1190                 if (dest->lud_exts == NULL) {
1191                         ldap_free_urldesc(dest);
1192                         return NULL;
1193                 }
1194         }
1195
1196         return dest;
1197 }
1198
1199 LDAPURLDesc *
1200 ldap_url_duplist (LDAPURLDesc *ludlist)
1201 {
1202         LDAPURLDesc *dest, *tail, *ludp, *newludp;
1203
1204         dest = NULL;
1205         tail = NULL;
1206         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1207                 newludp = ldap_url_dup(ludp);
1208                 if (newludp == NULL) {
1209                         ldap_free_urllist(dest);
1210                         return NULL;
1211                 }
1212                 if (tail == NULL)
1213                         dest = newludp;
1214                 else
1215                         tail->lud_next = newludp;
1216                 tail = newludp;
1217         }
1218         return dest;
1219 }
1220
1221 static int
1222 ldap_url_parselist_int (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1223         
1224 {
1225         int i, rc;
1226         LDAPURLDesc *ludp;
1227         char **urls;
1228
1229         assert( ludlist != NULL );
1230         assert( url != NULL );
1231
1232         *ludlist = NULL;
1233
1234         if ( sep == NULL ) {
1235                 sep = ", ";
1236         }
1237
1238         urls = ldap_str2charray( url, sep );
1239         if (urls == NULL)
1240                 return LDAP_URL_ERR_MEM;
1241
1242         /* count the URLs... */
1243         for (i = 0; urls[i] != NULL; i++) ;
1244         /* ...and put them in the "stack" backward */
1245         while (--i >= 0) {
1246                 rc = ldap_url_parse_ext( urls[i], &ludp, flags );
1247                 if ( rc != 0 ) {
1248                         ldap_charray_free( urls );
1249                         ldap_free_urllist( *ludlist );
1250                         *ludlist = NULL;
1251                         return rc;
1252                 }
1253                 ludp->lud_next = *ludlist;
1254                 *ludlist = ludp;
1255         }
1256         ldap_charray_free( urls );
1257         return LDAP_URL_SUCCESS;
1258 }
1259
1260 int
1261 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
1262 {
1263         return ldap_url_parselist_int( ludlist, url, ", ", LDAP_PVT_URL_PARSE_HISTORIC );
1264 }
1265
1266 int
1267 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1268 {
1269         return ldap_url_parselist_int( ludlist, url, sep, flags );
1270 }
1271
1272 int
1273 ldap_url_parsehosts(
1274         LDAPURLDesc **ludlist,
1275         const char *hosts,
1276         int port )
1277 {
1278         int i;
1279         LDAPURLDesc *ludp;
1280         char **specs, *p;
1281
1282         assert( ludlist != NULL );
1283         assert( hosts != NULL );
1284
1285         *ludlist = NULL;
1286
1287         specs = ldap_str2charray(hosts, ", ");
1288         if (specs == NULL)
1289                 return LDAP_NO_MEMORY;
1290
1291         /* count the URLs... */
1292         for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
1293
1294         /* ...and put them in the "stack" backward */
1295         while (--i >= 0) {
1296                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
1297                 if (ludp == NULL) {
1298                         ldap_charray_free(specs);
1299                         ldap_free_urllist(*ludlist);
1300                         *ludlist = NULL;
1301                         return LDAP_NO_MEMORY;
1302                 }
1303                 ludp->lud_port = port;
1304                 ludp->lud_host = specs[i];
1305                 specs[i] = NULL;
1306                 p = strchr(ludp->lud_host, ':');
1307                 if (p != NULL) {
1308                         /* more than one :, IPv6 address */
1309                         if ( strchr(p+1, ':') != NULL ) {
1310                                 /* allow [address] and [address]:port */
1311                                 if ( *ludp->lud_host == '[' ) {
1312                                         p = LDAP_STRDUP(ludp->lud_host+1);
1313                                         /* copied, make sure we free source later */
1314                                         specs[i] = ludp->lud_host;
1315                                         ludp->lud_host = p;
1316                                         p = strchr( ludp->lud_host, ']' );
1317                                         if ( p == NULL ) {
1318                                                 LDAP_FREE(ludp);
1319                                                 ldap_charray_free(specs);
1320                                                 return LDAP_PARAM_ERROR;
1321                                         }
1322                                         *p++ = '\0';
1323                                         if ( *p != ':' ) {
1324                                                 if ( *p != '\0' ) {
1325                                                         LDAP_FREE(ludp);
1326                                                         ldap_charray_free(specs);
1327                                                         return LDAP_PARAM_ERROR;
1328                                                 }
1329                                                 p = NULL;
1330                                         }
1331                                 } else {
1332                                         p = NULL;
1333                                 }
1334                         }
1335                         if (p != NULL) {
1336                                 char    *next;
1337
1338                                 *p++ = 0;
1339                                 ldap_pvt_hex_unescape(p);
1340                                 ludp->lud_port = strtol( p, &next, 10 );
1341                                 if ( next == p || next[0] != '\0' ) {
1342                                         LDAP_FREE(ludp);
1343                                         ldap_charray_free(specs);
1344                                         return LDAP_PARAM_ERROR;
1345                                 }
1346                         }
1347                 }
1348                 ldap_pvt_hex_unescape(ludp->lud_host);
1349                 ludp->lud_scheme = LDAP_STRDUP("ldap");
1350                 ludp->lud_next = *ludlist;
1351                 *ludlist = ludp;
1352         }
1353
1354         /* this should be an array of NULLs now */
1355         /* except entries starting with [ */
1356         ldap_charray_free(specs);
1357         return LDAP_SUCCESS;
1358 }
1359
1360 char *
1361 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1362 {
1363         LDAPURLDesc *ludp;
1364         int size;
1365         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
1366
1367         if (ludlist == NULL)
1368                 return NULL;
1369
1370         /* figure out how big the string is */
1371         size = 1;       /* nul-term */
1372         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1373                 size += strlen(ludp->lud_host) + 1;             /* host and space */
1374                 if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
1375                         size += 2;
1376                 if (ludp->lud_port != 0)
1377                         size += sprintf(buf, ":%d", ludp->lud_port);
1378         }
1379         s = LDAP_MALLOC(size);
1380         if (s == NULL)
1381                 return NULL;
1382
1383         p = s;
1384         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1385                 if (strchr(ludp->lud_host, ':')) {
1386                         p += sprintf(p, "[%s]", ludp->lud_host);
1387                 } else {
1388                         strcpy(p, ludp->lud_host);
1389                         p += strlen(ludp->lud_host);
1390                 }
1391                 if (ludp->lud_port != 0)
1392                         p += sprintf(p, ":%d", ludp->lud_port);
1393                 *p++ = ' ';
1394         }
1395         if (p != s)
1396                 p--;    /* nuke that extra space */
1397         *p = 0;
1398         return s;
1399 }
1400
1401 char *
1402 ldap_url_list2urls(
1403         LDAPURLDesc *ludlist )
1404 {
1405         LDAPURLDesc     *ludp;
1406         int             size, sofar;
1407         char            *s;
1408
1409         if ( ludlist == NULL ) {
1410                 return NULL;
1411         }
1412
1413         /* figure out how big the string is */
1414         for ( size = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1415                 int     len = desc2str_len( ludp );
1416                 if ( len < 0 ) {
1417                         return NULL;
1418                 }
1419                 size += len + 1;
1420         }
1421         
1422         s = LDAP_MALLOC( size );
1423
1424         if ( s == NULL ) {
1425                 return NULL;
1426         }
1427
1428         for ( sofar = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1429                 int     len;
1430
1431                 len = desc2str( ludp, &s[sofar], size );
1432                 
1433                 if ( len < 0 ) {
1434                         LDAP_FREE( s );
1435                         return NULL;
1436                 }
1437
1438                 sofar += len;
1439                 size -= len;
1440
1441                 s[sofar++] = ' ';
1442                 size--;
1443
1444                 assert( size >= 0 );
1445         }
1446
1447         s[sofar - 1] = '\0';
1448
1449         return s;
1450 }
1451
1452 void
1453 ldap_free_urllist( LDAPURLDesc *ludlist )
1454 {
1455         LDAPURLDesc *ludp, *next;
1456
1457         for (ludp = ludlist; ludp != NULL; ludp = next) {
1458                 next = ludp->lud_next;
1459                 ldap_free_urldesc(ludp);
1460         }
1461 }
1462
1463 void
1464 ldap_free_urldesc( LDAPURLDesc *ludp )
1465 {
1466         if ( ludp == NULL ) {
1467                 return;
1468         }
1469         
1470         if ( ludp->lud_scheme != NULL ) {
1471                 LDAP_FREE( ludp->lud_scheme );
1472         }
1473
1474         if ( ludp->lud_host != NULL ) {
1475                 LDAP_FREE( ludp->lud_host );
1476         }
1477
1478         if ( ludp->lud_dn != NULL ) {
1479                 LDAP_FREE( ludp->lud_dn );
1480         }
1481
1482         if ( ludp->lud_filter != NULL ) {
1483                 LDAP_FREE( ludp->lud_filter);
1484         }
1485
1486         if ( ludp->lud_attrs != NULL ) {
1487                 LDAP_VFREE( ludp->lud_attrs );
1488         }
1489
1490         if ( ludp->lud_exts != NULL ) {
1491                 LDAP_VFREE( ludp->lud_exts );
1492         }
1493
1494         LDAP_FREE( ludp );
1495 }
1496
1497 static int
1498 ldap_int_is_hexpair( char *s )
1499 {
1500         int     i;
1501
1502         for ( i = 0; i < 2; i++ ) {
1503                 if ( s[i] >= '0' && s[i] <= '9' ) {
1504                         continue;
1505                 }
1506
1507                 if ( s[i] >= 'A' && s[i] <= 'F' ) {
1508                         continue;
1509                 }
1510
1511                 if ( s[i] >= 'a' && s[i] <= 'f' ) {
1512                         continue;
1513                 }
1514
1515                 return 0;
1516         }
1517         
1518         return 1;       
1519 }
1520         
1521 static int
1522 ldap_int_unhex( int c )
1523 {
1524         return( c >= '0' && c <= '9' ? c - '0'
1525             : c >= 'A' && c <= 'F' ? c - 'A' + 10
1526             : c - 'a' + 10 );
1527 }
1528
1529 void
1530 ldap_pvt_hex_unescape( char *s )
1531 {
1532         /*
1533          * Remove URL hex escapes from s... done in place.  The basic concept for
1534          * this routine is borrowed from the WWW library HTUnEscape() routine.
1535          */
1536         char    *p,
1537                 *save_s = s;
1538
1539         for ( p = s; *s != '\0'; ++s ) {
1540                 if ( *s == '%' ) {
1541                         /*
1542                          * FIXME: what if '%' is followed
1543                          * by non-hexpair chars?
1544                          */
1545                         if ( !ldap_int_is_hexpair( s + 1 ) ) {
1546                                 p = save_s;
1547                                 break;
1548                         }
1549
1550                         if ( *++s == '\0' ) {
1551                                 break;
1552                         }
1553                         *p = ldap_int_unhex( *s ) << 4;
1554                         if ( *++s == '\0' ) {
1555                                 break;
1556                         }
1557                         *p++ += ldap_int_unhex( *s );
1558                 } else {
1559                         *p++ = *s;
1560                 }
1561         }
1562
1563         *p = '\0';
1564 }
1565