]> git.sur5r.net Git - openldap/blob - libraries/libldap/url.c
ITS#5300
[openldap] / libraries / libldap / url.c
1 /* LIBLDAP url.c -- LDAP URL (RFC 4516) related routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 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 4515
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         int             is_ipc = 0;
537         struct berval   scope;
538
539         if ( u == NULL || u->lud_scheme == NULL ) {
540                 return -1;
541         }
542
543         if ( !strcmp( "ldapi", u->lud_scheme )) {
544                 is_ipc = 1;
545         }
546
547         if ( u->lud_exts ) {
548                 len += hex_escape_len_list( u->lud_exts, URLESC_COMMA );
549                 if ( !sep ) {
550                         sep = 5;
551                 }
552         }
553
554         if ( u->lud_filter ) {
555                 len += hex_escape_len( u->lud_filter, URLESC_NONE );
556                 if ( !sep ) {
557                         sep = 4;
558                 }
559         }
560
561         if ( ldap_pvt_scope2bv( u->lud_scope, &scope ) == LDAP_SUCCESS ) {
562                 len += scope.bv_len;
563                 if ( !sep ) {
564                         sep = 3;
565                 }
566         }
567
568         if ( u->lud_attrs ) {
569                 len += hex_escape_len_list( u->lud_attrs, URLESC_NONE );
570                 if ( !sep ) {
571                         sep = 2;
572                 }
573         }
574
575         if ( u->lud_dn && u->lud_dn[0] ) {
576                 len += hex_escape_len( u->lud_dn, URLESC_NONE );
577                 if ( !sep ) {
578                         sep = 1;
579                 }
580         };
581
582         len += sep;
583
584         if ( u->lud_port ) {
585                 unsigned p = u->lud_port;
586                 if ( p > 65535 )
587                         return -1;
588
589                 len += (p > 999 ? 5 + (p > 9999) : p > 99 ? 4 : 2 + (p > 9));
590         }
591
592         if ( u->lud_host && u->lud_host[0] ) {
593                 len += hex_escape_len( u->lud_host, URLESC_SLASH );
594                 if ( !is_ipc && strchr( u->lud_host, ':' )) {
595                         len += 2;       /* IPv6, [] */
596                 }
597         }
598
599         len += strlen( u->lud_scheme ) + STRLENOF( "://" );
600
601         return len;
602 }
603
604 static int
605 desc2str( LDAPURLDesc *u, char *s, int len )
606 {
607         int             i;
608         int             sep = 0;
609         int             sofar = 0;
610         int             is_v6 = 0;
611         int             is_ipc = 0;
612         struct berval   scope = BER_BVNULL;
613
614         if ( u == NULL ) {
615                 return -1;
616         }
617
618         if ( s == NULL ) {
619                 return -1;
620         }
621
622         if ( u->lud_scheme && !strcmp( "ldapi", u->lud_scheme )) {
623                 is_ipc = 1;
624         }
625
626         ldap_pvt_scope2bv( u->lud_scope, &scope );
627
628         if ( u->lud_exts ) {
629                 sep = 5;
630         } else if ( u->lud_filter ) {
631                 sep = 4;
632         } else if ( !BER_BVISEMPTY( &scope ) ) {
633                 sep = 3;
634         } else if ( u->lud_attrs ) {
635                 sep = 2;
636         } else if ( u->lud_dn && u->lud_dn[0] ) {
637                 sep = 1;
638         }
639
640         if ( !is_ipc && u->lud_host && strchr( u->lud_host, ':' )) {
641                 is_v6 = 1;
642         }
643
644         if ( u->lud_port ) {
645                 len -= sprintf( s, "%s://%s%s%s:%d%n", u->lud_scheme,
646                                 is_v6 ? "[" : "",
647                                 u->lud_host ? u->lud_host : "",
648                                 is_v6 ? "]" : "",
649                                 u->lud_port, &sofar );
650
651         } else {
652                 len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar );
653                 if ( u->lud_host && u->lud_host[0] ) {
654                         if ( is_v6 ) {
655                                 s[sofar++] = '[';
656                                 len--;
657                         }
658                         i = hex_escape( &s[sofar], len, u->lud_host, URLESC_SLASH );
659                         sofar += i;
660                         len -= i;
661                         if ( is_v6 ) {
662                                 s[sofar++] = ']';
663                                 len--;
664                         }
665                 }
666         }
667
668         assert( len >= 0 );
669
670         if ( sep < 1 ) {
671                 goto done;
672         }
673
674         s[sofar++] = '/';
675         len--;
676
677         assert( len >= 0 );
678
679         if ( u->lud_dn && u->lud_dn[0] ) {
680                 i = hex_escape( &s[sofar], len, u->lud_dn, URLESC_NONE );
681                 sofar += i;
682                 len -= i;
683
684                 assert( len >= 0 );
685         }
686
687         if ( sep < 2 ) {
688                 goto done;
689         }
690         s[sofar++] = '?';
691         len--;
692
693         assert( len >= 0 );
694
695         i = hex_escape_list( &s[sofar], len, u->lud_attrs, URLESC_NONE );
696         sofar += i;
697         len -= i;
698
699         assert( len >= 0 );
700
701         if ( sep < 3 ) {
702                 goto done;
703         }
704         s[sofar++] = '?';
705         len--;
706
707         assert( len >= 0 );
708
709         if ( !BER_BVISNULL( &scope ) ) {
710                 strcpy( &s[sofar], scope.bv_val );
711                 sofar += scope.bv_len;
712                 len -= scope.bv_len;
713         }
714
715         assert( len >= 0 );
716
717         if ( sep < 4 ) {
718                 goto done;
719         }
720         s[sofar++] = '?';
721         len--;
722
723         assert( len >= 0 );
724
725         i = hex_escape( &s[sofar], len, u->lud_filter, URLESC_NONE );
726         sofar += i;
727         len -= i;
728
729         assert( len >= 0 );
730
731         if ( sep < 5 ) {
732                 goto done;
733         }
734         s[sofar++] = '?';
735         len--;
736
737         assert( len >= 0 );
738
739         i = hex_escape_list( &s[sofar], len, u->lud_exts, URLESC_COMMA );
740         sofar += i;
741         len -= i;
742
743         assert( len >= 0 );
744
745 done:
746         if ( len < 0 ) {
747                 return -1;
748         }
749
750         return sofar;
751 }
752
753 char *
754 ldap_url_desc2str( LDAPURLDesc *u )
755 {
756         int     len;
757         char    *s;
758
759         if ( u == NULL ) {
760                 return NULL;
761         }
762
763         len = desc2str_len( u );
764         if ( len < 0 ) {
765                 return NULL;
766         }
767         
768         /* allocate enough to hex escape everything -- overkill */
769         s = LDAP_MALLOC( len + 1 );
770
771         if ( s == NULL ) {
772                 return NULL;
773         }
774
775         if ( desc2str( u, s, len ) != len ) {
776                 LDAP_FREE( s );
777                 return NULL;
778         }
779
780         s[len] = '\0';
781
782         return s;
783 }
784
785 int
786 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp, unsigned flags )
787 {
788 /*
789  *  Pick apart the pieces of an LDAP URL.
790  */
791
792         LDAPURLDesc     *ludp;
793         char    *p, *q, *r;
794         int             i, enclosed, proto, is_v6 = 0;
795         const char *scheme = NULL;
796         const char *url_tmp;
797         char *url;
798
799         int     check_dn = 1;
800
801         if( url_in == NULL || ludpp == NULL ) {
802                 return LDAP_URL_ERR_PARAM;
803         }
804
805 #ifndef LDAP_INT_IN_KERNEL
806         /* Global options may not be created yet
807          * We can't test if the global options are initialized
808          * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
809          * the options and cause infinite recursion
810          */
811         Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
812 #endif
813
814         *ludpp = NULL;  /* pessimistic */
815
816         url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
817
818         if ( url_tmp == NULL ) {
819                 return LDAP_URL_ERR_BADSCHEME;
820         }
821
822         assert( scheme != NULL );
823
824         proto = ldap_pvt_url_scheme2proto( scheme );
825         if ( proto == -1 ) {
826                 return LDAP_URL_ERR_BADSCHEME;
827         }
828
829         /* make working copy of the remainder of the URL */
830         url = LDAP_STRDUP( url_tmp );
831         if ( url == NULL ) {
832                 return LDAP_URL_ERR_MEM;
833         }
834
835         if ( enclosed ) {
836                 p = &url[strlen(url)-1];
837
838                 if( *p != '>' ) {
839                         LDAP_FREE( url );
840                         return LDAP_URL_ERR_BADENCLOSURE;
841                 }
842
843                 *p = '\0';
844         }
845
846         /* allocate return struct */
847         ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
848
849         if ( ludp == NULL ) {
850                 LDAP_FREE( url );
851                 return LDAP_URL_ERR_MEM;
852         }
853
854         ludp->lud_next = NULL;
855         ludp->lud_host = NULL;
856         ludp->lud_port = 0;
857         ludp->lud_dn = NULL;
858         ludp->lud_attrs = NULL;
859         ludp->lud_scope = ( flags & LDAP_PVT_URL_PARSE_NODEF_SCOPE ) ? LDAP_SCOPE_BASE : LDAP_SCOPE_DEFAULT;
860         ludp->lud_filter = NULL;
861         ludp->lud_exts = NULL;
862
863         ludp->lud_scheme = LDAP_STRDUP( scheme );
864
865         if ( ludp->lud_scheme == NULL ) {
866                 LDAP_FREE( url );
867                 ldap_free_urldesc( ludp );
868                 return LDAP_URL_ERR_MEM;
869         }
870
871         /* scan forward for '/' that marks end of hostport and begin. of dn */
872         p = strchr( url, '/' );
873         q = NULL;
874
875         if( p != NULL ) {
876                 /* terminate hostport; point to start of dn */
877                 *p++ = '\0';
878         } else {
879                 /* check for Novell kludge, see below */
880                 p = strchr( url, '?' );
881                 if ( p ) {
882                         *p++ = '\0';
883                         q = p;
884                         p = NULL;
885                 }
886         }
887
888         if ( proto != LDAP_PROTO_IPC ) {
889                 /* IPv6 syntax with [ip address]:port */
890                 if ( *url == '[' ) {
891                         r = strchr( url, ']' );
892                         if ( r == NULL ) {
893                                 LDAP_FREE( url );
894                                 ldap_free_urldesc( ludp );
895                                 return LDAP_URL_ERR_BADURL;
896                         }
897                         *r++ = '\0';
898                         q = strchr( r, ':' );
899                         if ( q && q != r ) {
900                                 LDAP_FREE( url );
901                                 ldap_free_urldesc( ludp );
902                                 return LDAP_URL_ERR_BADURL;
903                         }
904                         is_v6 = 1;
905                 } else {
906                         q = strchr( url, ':' );
907                 }
908
909                 if ( q != NULL ) {
910                         char    *next;
911
912                         *q++ = '\0';
913                         ldap_pvt_hex_unescape( q );
914
915                         if( *q == '\0' ) {
916                                 LDAP_FREE( url );
917                                 ldap_free_urldesc( ludp );
918                                 return LDAP_URL_ERR_BADURL;
919                         }
920
921                         ludp->lud_port = strtol( q, &next, 10 );
922                         if ( next == q || next[0] != '\0' ) {
923                                 LDAP_FREE( url );
924                                 ldap_free_urldesc( ludp );
925                                 return LDAP_URL_ERR_BADURL;
926                         }
927                         /* check for Novell kludge */
928                         if ( !p ) {
929                                 if ( *next != '\0' ) {
930                                         q = &next[1];
931                                 } else {
932                                         q = NULL;
933                                 }
934                         }
935                 }
936
937                 if ( ( flags & LDAP_PVT_URL_PARSE_DEF_PORT ) && ludp->lud_port == 0 ) {
938                         if ( strcmp( ludp->lud_scheme, "ldaps" ) == 0 ) {
939                                 ludp->lud_port = LDAPS_PORT;
940                         } else {
941                                 ludp->lud_port = LDAP_PORT;
942                         }
943                 }
944         }
945
946         ldap_pvt_hex_unescape( url );
947
948         /* If [ip address]:port syntax, url is [ip and we skip the [ */
949         ludp->lud_host = LDAP_STRDUP( url + is_v6 );
950
951         if( ludp->lud_host == NULL ) {
952                 LDAP_FREE( url );
953                 ldap_free_urldesc( ludp );
954                 return LDAP_URL_ERR_MEM;
955         }
956
957         if ( ( flags & LDAP_PVT_URL_PARSE_NOEMPTY_HOST )
958                 && ludp->lud_host != NULL
959                 && *ludp->lud_host == '\0' )
960         {
961                 LDAP_FREE( ludp->lud_host );
962                 ludp->lud_host = NULL;
963         }
964
965         /*
966          * Kludge.  ldap://111.222.333.444:389??cn=abc,o=company
967          *
968          * On early Novell releases, search references/referrals were returned
969          * in this format, i.e., the dn was kind of in the scope position,
970          * but the required slash is missing. The whole thing is illegal syntax,
971          * but we need to account for it. Fortunately it can't be confused with
972          * anything real.
973          */
974         if( (p == NULL) && (q != NULL) && (*q == '?') ) {
975                 /* ? immediately followed by question */
976                 q++;
977                 if( *q != '\0' ) {
978                         /* parse dn part */
979                         ldap_pvt_hex_unescape( q );
980                         ludp->lud_dn = LDAP_STRDUP( q );
981
982                 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
983                         ludp->lud_dn = LDAP_STRDUP( "" );
984
985                 } else {
986                         check_dn = 0;
987                 }
988
989                 if ( check_dn && ludp->lud_dn == NULL ) {
990                         LDAP_FREE( url );
991                         ldap_free_urldesc( ludp );
992                         return LDAP_URL_ERR_MEM;
993                 }
994         }
995
996         if( p == NULL ) {
997                 LDAP_FREE( url );
998                 *ludpp = ludp;
999                 return LDAP_URL_SUCCESS;
1000         }
1001
1002         /* scan forward for '?' that may marks end of dn */
1003         q = strchr( p, '?' );
1004
1005         if( q != NULL ) {
1006                 /* terminate dn part */
1007                 *q++ = '\0';
1008         }
1009
1010         if( *p != '\0' ) {
1011                 /* parse dn part */
1012                 ldap_pvt_hex_unescape( p );
1013                 ludp->lud_dn = LDAP_STRDUP( p );
1014
1015         } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
1016                 ludp->lud_dn = LDAP_STRDUP( "" );
1017
1018         } else {
1019                 check_dn = 0;
1020         }
1021
1022         if( check_dn && ludp->lud_dn == NULL ) {
1023                 LDAP_FREE( url );
1024                 ldap_free_urldesc( ludp );
1025                 return LDAP_URL_ERR_MEM;
1026         }
1027
1028         if( q == NULL ) {
1029                 /* no more */
1030                 LDAP_FREE( url );
1031                 *ludpp = ludp;
1032                 return LDAP_URL_SUCCESS;
1033         }
1034
1035         /* scan forward for '?' that may marks end of attributes */
1036         p = q;
1037         q = strchr( p, '?' );
1038
1039         if( q != NULL ) {
1040                 /* terminate attributes part */
1041                 *q++ = '\0';
1042         }
1043
1044         if( *p != '\0' ) {
1045                 /* parse attributes */
1046                 ldap_pvt_hex_unescape( p );
1047                 ludp->lud_attrs = ldap_str2charray( p, "," );
1048
1049                 if( ludp->lud_attrs == NULL ) {
1050                         LDAP_FREE( url );
1051                         ldap_free_urldesc( ludp );
1052                         return LDAP_URL_ERR_BADATTRS;
1053                 }
1054         }
1055
1056         if ( q == NULL ) {
1057                 /* no more */
1058                 LDAP_FREE( url );
1059                 *ludpp = ludp;
1060                 return LDAP_URL_SUCCESS;
1061         }
1062
1063         /* scan forward for '?' that may marks end of scope */
1064         p = q;
1065         q = strchr( p, '?' );
1066
1067         if( q != NULL ) {
1068                 /* terminate the scope part */
1069                 *q++ = '\0';
1070         }
1071
1072         if( *p != '\0' ) {
1073                 /* parse the scope */
1074                 ldap_pvt_hex_unescape( p );
1075                 ludp->lud_scope = ldap_pvt_str2scope( p );
1076
1077                 if( ludp->lud_scope == -1 ) {
1078                         LDAP_FREE( url );
1079                         ldap_free_urldesc( ludp );
1080                         return LDAP_URL_ERR_BADSCOPE;
1081                 }
1082         }
1083
1084         if ( q == NULL ) {
1085                 /* no more */
1086                 LDAP_FREE( url );
1087                 *ludpp = ludp;
1088                 return LDAP_URL_SUCCESS;
1089         }
1090
1091         /* scan forward for '?' that may marks end of filter */
1092         p = q;
1093         q = strchr( p, '?' );
1094
1095         if( q != NULL ) {
1096                 /* terminate the filter part */
1097                 *q++ = '\0';
1098         }
1099
1100         if( *p != '\0' ) {
1101                 /* parse the filter */
1102                 ldap_pvt_hex_unescape( p );
1103
1104                 if( ! *p ) {
1105                         /* missing filter */
1106                         LDAP_FREE( url );
1107                         ldap_free_urldesc( ludp );
1108                         return LDAP_URL_ERR_BADFILTER;
1109                 }
1110
1111                 ludp->lud_filter = LDAP_STRDUP( p );
1112
1113                 if( ludp->lud_filter == NULL ) {
1114                         LDAP_FREE( url );
1115                         ldap_free_urldesc( ludp );
1116                         return LDAP_URL_ERR_MEM;
1117                 }
1118         }
1119
1120         if ( q == NULL ) {
1121                 /* no more */
1122                 LDAP_FREE( url );
1123                 *ludpp = ludp;
1124                 return LDAP_URL_SUCCESS;
1125         }
1126
1127         /* scan forward for '?' that may marks end of extensions */
1128         p = q;
1129         q = strchr( p, '?' );
1130
1131         if( q != NULL ) {
1132                 /* extra '?' */
1133                 LDAP_FREE( url );
1134                 ldap_free_urldesc( ludp );
1135                 return LDAP_URL_ERR_BADURL;
1136         }
1137
1138         /* parse the extensions */
1139         ludp->lud_exts = ldap_str2charray( p, "," );
1140
1141         if( ludp->lud_exts == NULL ) {
1142                 LDAP_FREE( url );
1143                 ldap_free_urldesc( ludp );
1144                 return LDAP_URL_ERR_BADEXTS;
1145         }
1146
1147         for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
1148                 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
1149
1150                 if( *ludp->lud_exts[i] == '!' ) {
1151                         /* count the number of critical extensions */
1152                         ludp->lud_crit_exts++;
1153                 }
1154         }
1155
1156         if( i == 0 ) {
1157                 /* must have 1 or more */
1158                 LDAP_FREE( url );
1159                 ldap_free_urldesc( ludp );
1160                 return LDAP_URL_ERR_BADEXTS;
1161         }
1162
1163         /* no more */
1164         *ludpp = ludp;
1165         LDAP_FREE( url );
1166         return LDAP_URL_SUCCESS;
1167 }
1168
1169 int
1170 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
1171 {
1172         return ldap_url_parse_ext( url_in, ludpp, LDAP_PVT_URL_PARSE_HISTORIC );
1173 }
1174
1175 LDAPURLDesc *
1176 ldap_url_dup ( LDAPURLDesc *ludp )
1177 {
1178         LDAPURLDesc *dest;
1179
1180         if ( ludp == NULL ) {
1181                 return NULL;
1182         }
1183
1184         dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
1185         if (dest == NULL)
1186                 return NULL;
1187         
1188         *dest = *ludp;
1189         dest->lud_scheme = NULL;
1190         dest->lud_host = NULL;
1191         dest->lud_dn = NULL;
1192         dest->lud_filter = NULL;
1193         dest->lud_attrs = NULL;
1194         dest->lud_exts = NULL;
1195         dest->lud_next = NULL;
1196
1197         if ( ludp->lud_scheme != NULL ) {
1198                 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
1199                 if (dest->lud_scheme == NULL) {
1200                         ldap_free_urldesc(dest);
1201                         return NULL;
1202                 }
1203         }
1204
1205         if ( ludp->lud_host != NULL ) {
1206                 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
1207                 if (dest->lud_host == NULL) {
1208                         ldap_free_urldesc(dest);
1209                         return NULL;
1210                 }
1211         }
1212
1213         if ( ludp->lud_dn != NULL ) {
1214                 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
1215                 if (dest->lud_dn == NULL) {
1216                         ldap_free_urldesc(dest);
1217                         return NULL;
1218                 }
1219         }
1220
1221         if ( ludp->lud_filter != NULL ) {
1222                 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
1223                 if (dest->lud_filter == NULL) {
1224                         ldap_free_urldesc(dest);
1225                         return NULL;
1226                 }
1227         }
1228
1229         if ( ludp->lud_attrs != NULL ) {
1230                 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
1231                 if (dest->lud_attrs == NULL) {
1232                         ldap_free_urldesc(dest);
1233                         return NULL;
1234                 }
1235         }
1236
1237         if ( ludp->lud_exts != NULL ) {
1238                 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
1239                 if (dest->lud_exts == NULL) {
1240                         ldap_free_urldesc(dest);
1241                         return NULL;
1242                 }
1243         }
1244
1245         return dest;
1246 }
1247
1248 LDAPURLDesc *
1249 ldap_url_duplist (LDAPURLDesc *ludlist)
1250 {
1251         LDAPURLDesc *dest, *tail, *ludp, *newludp;
1252
1253         dest = NULL;
1254         tail = NULL;
1255         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1256                 newludp = ldap_url_dup(ludp);
1257                 if (newludp == NULL) {
1258                         ldap_free_urllist(dest);
1259                         return NULL;
1260                 }
1261                 if (tail == NULL)
1262                         dest = newludp;
1263                 else
1264                         tail->lud_next = newludp;
1265                 tail = newludp;
1266         }
1267         return dest;
1268 }
1269
1270 static int
1271 ldap_url_parselist_int (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1272         
1273 {
1274         int i, rc;
1275         LDAPURLDesc *ludp;
1276         char **urls;
1277
1278         assert( ludlist != NULL );
1279         assert( url != NULL );
1280
1281         *ludlist = NULL;
1282
1283         if ( sep == NULL ) {
1284                 sep = ", ";
1285         }
1286
1287         urls = ldap_str2charray( url, sep );
1288         if (urls == NULL)
1289                 return LDAP_URL_ERR_MEM;
1290
1291         /* count the URLs... */
1292         for (i = 0; urls[i] != NULL; i++) ;
1293         /* ...and put them in the "stack" backward */
1294         while (--i >= 0) {
1295                 rc = ldap_url_parse_ext( urls[i], &ludp, flags );
1296                 if ( rc != 0 ) {
1297                         ldap_charray_free( urls );
1298                         ldap_free_urllist( *ludlist );
1299                         *ludlist = NULL;
1300                         return rc;
1301                 }
1302                 ludp->lud_next = *ludlist;
1303                 *ludlist = ludp;
1304         }
1305         ldap_charray_free( urls );
1306         return LDAP_URL_SUCCESS;
1307 }
1308
1309 int
1310 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
1311 {
1312         return ldap_url_parselist_int( ludlist, url, ", ", LDAP_PVT_URL_PARSE_HISTORIC );
1313 }
1314
1315 int
1316 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1317 {
1318         return ldap_url_parselist_int( ludlist, url, sep, flags );
1319 }
1320
1321 int
1322 ldap_url_parsehosts(
1323         LDAPURLDesc **ludlist,
1324         const char *hosts,
1325         int port )
1326 {
1327         int i;
1328         LDAPURLDesc *ludp;
1329         char **specs, *p;
1330
1331         assert( ludlist != NULL );
1332         assert( hosts != NULL );
1333
1334         *ludlist = NULL;
1335
1336         specs = ldap_str2charray(hosts, ", ");
1337         if (specs == NULL)
1338                 return LDAP_NO_MEMORY;
1339
1340         /* count the URLs... */
1341         for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
1342
1343         /* ...and put them in the "stack" backward */
1344         while (--i >= 0) {
1345                 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
1346                 if (ludp == NULL) {
1347                         ldap_charray_free(specs);
1348                         ldap_free_urllist(*ludlist);
1349                         *ludlist = NULL;
1350                         return LDAP_NO_MEMORY;
1351                 }
1352                 ludp->lud_port = port;
1353                 ludp->lud_host = specs[i];
1354                 specs[i] = NULL;
1355                 p = strchr(ludp->lud_host, ':');
1356                 if (p != NULL) {
1357                         /* more than one :, IPv6 address */
1358                         if ( strchr(p+1, ':') != NULL ) {
1359                                 /* allow [address] and [address]:port */
1360                                 if ( *ludp->lud_host == '[' ) {
1361                                         p = LDAP_STRDUP(ludp->lud_host+1);
1362                                         /* copied, make sure we free source later */
1363                                         specs[i] = ludp->lud_host;
1364                                         ludp->lud_host = p;
1365                                         p = strchr( ludp->lud_host, ']' );
1366                                         if ( p == NULL ) {
1367                                                 LDAP_FREE(ludp);
1368                                                 ldap_charray_free(specs);
1369                                                 return LDAP_PARAM_ERROR;
1370                                         }
1371                                         *p++ = '\0';
1372                                         if ( *p != ':' ) {
1373                                                 if ( *p != '\0' ) {
1374                                                         LDAP_FREE(ludp);
1375                                                         ldap_charray_free(specs);
1376                                                         return LDAP_PARAM_ERROR;
1377                                                 }
1378                                                 p = NULL;
1379                                         }
1380                                 } else {
1381                                         p = NULL;
1382                                 }
1383                         }
1384                         if (p != NULL) {
1385                                 char    *next;
1386
1387                                 *p++ = 0;
1388                                 ldap_pvt_hex_unescape(p);
1389                                 ludp->lud_port = strtol( p, &next, 10 );
1390                                 if ( next == p || next[0] != '\0' ) {
1391                                         LDAP_FREE(ludp);
1392                                         ldap_charray_free(specs);
1393                                         return LDAP_PARAM_ERROR;
1394                                 }
1395                         }
1396                 }
1397                 ldap_pvt_hex_unescape(ludp->lud_host);
1398                 ludp->lud_scheme = LDAP_STRDUP("ldap");
1399                 ludp->lud_next = *ludlist;
1400                 *ludlist = ludp;
1401         }
1402
1403         /* this should be an array of NULLs now */
1404         /* except entries starting with [ */
1405         ldap_charray_free(specs);
1406         return LDAP_SUCCESS;
1407 }
1408
1409 char *
1410 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1411 {
1412         LDAPURLDesc *ludp;
1413         int size;
1414         char *s, *p, buf[32];   /* big enough to hold a long decimal # (overkill) */
1415
1416         if (ludlist == NULL)
1417                 return NULL;
1418
1419         /* figure out how big the string is */
1420         size = 1;       /* nul-term */
1421         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1422                 size += strlen(ludp->lud_host) + 1;             /* host and space */
1423                 if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
1424                         size += 2;
1425                 if (ludp->lud_port != 0)
1426                         size += sprintf(buf, ":%d", ludp->lud_port);
1427         }
1428         s = LDAP_MALLOC(size);
1429         if (s == NULL)
1430                 return NULL;
1431
1432         p = s;
1433         for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1434                 if (strchr(ludp->lud_host, ':')) {
1435                         p += sprintf(p, "[%s]", ludp->lud_host);
1436                 } else {
1437                         strcpy(p, ludp->lud_host);
1438                         p += strlen(ludp->lud_host);
1439                 }
1440                 if (ludp->lud_port != 0)
1441                         p += sprintf(p, ":%d", ludp->lud_port);
1442                 *p++ = ' ';
1443         }
1444         if (p != s)
1445                 p--;    /* nuke that extra space */
1446         *p = 0;
1447         return s;
1448 }
1449
1450 char *
1451 ldap_url_list2urls(
1452         LDAPURLDesc *ludlist )
1453 {
1454         LDAPURLDesc     *ludp;
1455         int             size, sofar;
1456         char            *s;
1457
1458         if ( ludlist == NULL ) {
1459                 return NULL;
1460         }
1461
1462         /* figure out how big the string is */
1463         for ( size = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1464                 int     len = desc2str_len( ludp );
1465                 if ( len < 0 ) {
1466                         return NULL;
1467                 }
1468                 size += len + 1;
1469         }
1470         
1471         s = LDAP_MALLOC( size );
1472
1473         if ( s == NULL ) {
1474                 return NULL;
1475         }
1476
1477         for ( sofar = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1478                 int     len;
1479
1480                 len = desc2str( ludp, &s[sofar], size );
1481                 
1482                 if ( len < 0 ) {
1483                         LDAP_FREE( s );
1484                         return NULL;
1485                 }
1486
1487                 sofar += len;
1488                 size -= len;
1489
1490                 s[sofar++] = ' ';
1491                 size--;
1492
1493                 assert( size >= 0 );
1494         }
1495
1496         s[sofar - 1] = '\0';
1497
1498         return s;
1499 }
1500
1501 void
1502 ldap_free_urllist( LDAPURLDesc *ludlist )
1503 {
1504         LDAPURLDesc *ludp, *next;
1505
1506         for (ludp = ludlist; ludp != NULL; ludp = next) {
1507                 next = ludp->lud_next;
1508                 ldap_free_urldesc(ludp);
1509         }
1510 }
1511
1512 void
1513 ldap_free_urldesc( LDAPURLDesc *ludp )
1514 {
1515         if ( ludp == NULL ) {
1516                 return;
1517         }
1518         
1519         if ( ludp->lud_scheme != NULL ) {
1520                 LDAP_FREE( ludp->lud_scheme );
1521         }
1522
1523         if ( ludp->lud_host != NULL ) {
1524                 LDAP_FREE( ludp->lud_host );
1525         }
1526
1527         if ( ludp->lud_dn != NULL ) {
1528                 LDAP_FREE( ludp->lud_dn );
1529         }
1530
1531         if ( ludp->lud_filter != NULL ) {
1532                 LDAP_FREE( ludp->lud_filter);
1533         }
1534
1535         if ( ludp->lud_attrs != NULL ) {
1536                 LDAP_VFREE( ludp->lud_attrs );
1537         }
1538
1539         if ( ludp->lud_exts != NULL ) {
1540                 LDAP_VFREE( ludp->lud_exts );
1541         }
1542
1543         LDAP_FREE( ludp );
1544 }
1545
1546 static int
1547 ldap_int_is_hexpair( char *s )
1548 {
1549         int     i;
1550
1551         for ( i = 0; i < 2; i++ ) {
1552                 if ( s[i] >= '0' && s[i] <= '9' ) {
1553                         continue;
1554                 }
1555
1556                 if ( s[i] >= 'A' && s[i] <= 'F' ) {
1557                         continue;
1558                 }
1559
1560                 if ( s[i] >= 'a' && s[i] <= 'f' ) {
1561                         continue;
1562                 }
1563
1564                 return 0;
1565         }
1566         
1567         return 1;       
1568 }
1569         
1570 static int
1571 ldap_int_unhex( int c )
1572 {
1573         return( c >= '0' && c <= '9' ? c - '0'
1574             : c >= 'A' && c <= 'F' ? c - 'A' + 10
1575             : c - 'a' + 10 );
1576 }
1577
1578 void
1579 ldap_pvt_hex_unescape( char *s )
1580 {
1581         /*
1582          * Remove URL hex escapes from s... done in place.  The basic concept for
1583          * this routine is borrowed from the WWW library HTUnEscape() routine.
1584          */
1585         char    *p,
1586                 *save_s = s;
1587
1588         for ( p = s; *s != '\0'; ++s ) {
1589                 if ( *s == '%' ) {
1590                         /*
1591                          * FIXME: what if '%' is followed
1592                          * by non-hexpair chars?
1593                          */
1594                         if ( !ldap_int_is_hexpair( s + 1 ) ) {
1595                                 p = save_s;
1596                                 break;
1597                         }
1598
1599                         if ( *++s == '\0' ) {
1600                                 break;
1601                         }
1602                         *p = ldap_int_unhex( *s ) << 4;
1603                         if ( *++s == '\0' ) {
1604                                 break;
1605                         }
1606                         *p++ += ldap_int_unhex( *s );
1607                 } else {
1608                         *p++ = *s;
1609                 }
1610         }
1611
1612         *p = '\0';
1613 }
1614