1 /* LIBLDAP url.c -- LDAP URL (RFC 4516) related routines */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/url.c,v 1.94.2.8 2008/02/11 23:41:37 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
17 * All rights reserved.
22 * LDAP URLs look like this:
23 * ldap[is]://host[:port][/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
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
30 * e.g., ldap://host:port/dc=com?o,cn?base?(o=openldap)?extension
32 * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
39 #include <ac/stdlib.h>
42 #include <ac/socket.h>
43 #include <ac/string.h>
49 static const char* skip_url_prefix
LDAP_P((
52 const char **scheme
));
54 int ldap_pvt_url_scheme2proto( const char *scheme
)
56 assert( scheme
!= NULL
);
58 if( scheme
== NULL
) {
62 if( strcmp("ldap", scheme
) == 0 ) {
63 return LDAP_PROTO_TCP
;
66 if( strcmp("ldapi", scheme
) == 0 ) {
67 return LDAP_PROTO_IPC
;
70 if( strcmp("ldaps", scheme
) == 0 ) {
71 return LDAP_PROTO_TCP
;
73 #ifdef LDAP_CONNECTIONLESS
74 if( strcmp("cldap", scheme
) == 0 ) {
75 return LDAP_PROTO_UDP
;
82 int ldap_pvt_url_scheme_port( const char *scheme
, int port
)
84 assert( scheme
!= NULL
);
86 if( port
) return port
;
87 if( scheme
== NULL
) return port
;
89 if( strcmp("ldap", scheme
) == 0 ) {
93 if( strcmp("ldapi", scheme
) == 0 ) {
97 if( strcmp("ldaps", scheme
) == 0 ) {
101 #ifdef LDAP_CONNECTIONLESS
102 if( strcmp("cldap", scheme
) == 0 ) {
111 ldap_pvt_url_scheme2tls( const char *scheme
)
113 assert( scheme
!= NULL
);
115 if( scheme
== NULL
) {
119 return strcmp("ldaps", scheme
) == 0;
123 ldap_is_ldap_url( LDAP_CONST
char *url
)
132 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
140 ldap_is_ldaps_url( LDAP_CONST
char *url
)
149 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
153 return strcmp(scheme
, "ldaps") == 0;
157 ldap_is_ldapi_url( LDAP_CONST
char *url
)
166 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
170 return strcmp(scheme
, "ldapi") == 0;
173 #ifdef LDAP_CONNECTIONLESS
175 ldap_is_ldapc_url( LDAP_CONST
char *url
)
184 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
188 return strcmp(scheme
, "cldap") == 0;
196 const char **scheme
)
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
210 /* skip leading '<' (if any) */
218 /* skip leading "URL:" (if any) */
219 if ( strncasecmp( p
, LDAP_URL_URLCOLON
, LDAP_URL_URLCOLON_LEN
) == 0 ) {
220 p
+= LDAP_URL_URLCOLON_LEN
;
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
;
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
;
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
;
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
;
261 ldap_pvt_scope2bv( int scope
, struct berval
*bv
)
264 case LDAP_SCOPE_BASE
:
265 BER_BVSTR( bv
, "base" );
268 case LDAP_SCOPE_ONELEVEL
:
269 BER_BVSTR( bv
, "one" );
272 case LDAP_SCOPE_SUBTREE
:
273 BER_BVSTR( bv
, "sub" );
276 case LDAP_SCOPE_SUBORDINATE
:
277 BER_BVSTR( bv
, "subordinate" );
288 ldap_pvt_scope2str( int scope
)
292 if ( ldap_pvt_scope2bv( scope
, &bv
) == LDAP_SUCCESS
) {
300 ldap_pvt_bv2scope( struct berval
*bv
)
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
},
318 for ( i
= 0; v
[ i
].scope
!= -1; i
++ ) {
319 if ( ber_bvstrcasecmp( bv
, &v
[ i
].bv
) == 0 ) {
328 ldap_pvt_str2scope( const char *p
)
332 ber_str2bv( p
, 0, 0, &bv
);
334 return ldap_pvt_bv2scope( &bv
);
337 static const char hex
[] = "0123456789ABCDEF";
339 #define URLESC_NONE 0x0000U
340 #define URLESC_COMMA 0x0001U
341 #define URLESC_SLASH 0x0002U
344 hex_escape_len( const char *s
, unsigned list
)
352 for ( len
= 0; s
[0]; s
++ ) {
354 /* RFC 2396: reserved */
360 if ( list
& URLESC_COMMA
) {
368 if ( list
& URLESC_SLASH
) {
383 /* RFC 2396: unreserved mark */
396 /* RFC 2396: unreserved alphanum */
398 if ( !isalnum( (unsigned char) s
[0] ) ) {
411 hex_escape( char *buf
, int len
, const char *s
, unsigned list
)
420 for ( pos
= 0, i
= 0; s
[i
] && pos
< len
; i
++ ) {
424 /* RFC 2396: reserved */
430 if ( list
& URLESC_COMMA
) {
436 if ( list
& URLESC_SLASH
) {
449 /* RFC 2396: unreserved mark */
461 /* RFC 2396: unreserved alphanum */
463 if ( !isalnum( (unsigned char) s
[i
] ) ) {
471 buf
[pos
++] = hex
[ (s
[i
] >> 4) & 0x0f ];
472 buf
[pos
++] = hex
[ s
[i
] & 0x0f ];
485 hex_escape_len_list( char **s
, unsigned flags
)
495 for ( i
= 0; s
[i
] != NULL
; i
++ ) {
499 len
+= hex_escape_len( s
[i
], flags
);
506 hex_escape_list( char *buf
, int len
, char **s
, unsigned flags
)
516 for ( i
= 0; s
[i
] != NULL
; i
++ ) {
523 curlen
= hex_escape( &buf
[pos
], len
, s
[i
], flags
);
532 desc2str_len( LDAPURLDesc
*u
)
539 if ( u
== NULL
|| u
->lud_scheme
== NULL
) {
543 if ( !strcmp( "ldapi", u
->lud_scheme
)) {
548 len
+= hex_escape_len_list( u
->lud_exts
, URLESC_COMMA
);
554 if ( u
->lud_filter
) {
555 len
+= hex_escape_len( u
->lud_filter
, URLESC_NONE
);
561 if ( ldap_pvt_scope2bv( u
->lud_scope
, &scope
) == LDAP_SUCCESS
) {
568 if ( u
->lud_attrs
) {
569 len
+= hex_escape_len_list( u
->lud_attrs
, URLESC_NONE
);
575 if ( u
->lud_dn
&& u
->lud_dn
[0] ) {
576 len
+= hex_escape_len( u
->lud_dn
, URLESC_NONE
);
585 unsigned p
= u
->lud_port
;
589 len
+= (p
> 999 ? 5 + (p
> 9999) : p
> 99 ? 4 : 2 + (p
> 9));
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, [] */
599 len
+= strlen( u
->lud_scheme
) + STRLENOF( "://" );
605 desc2str( LDAPURLDesc
*u
, char *s
, int len
)
612 struct berval scope
= BER_BVNULL
;
622 if ( u
->lud_scheme
&& !strcmp( "ldapi", u
->lud_scheme
)) {
626 ldap_pvt_scope2bv( u
->lud_scope
, &scope
);
630 } else if ( u
->lud_filter
) {
632 } else if ( !BER_BVISEMPTY( &scope
) ) {
634 } else if ( u
->lud_attrs
) {
636 } else if ( u
->lud_dn
&& u
->lud_dn
[0] ) {
640 if ( !is_ipc
&& u
->lud_host
&& strchr( u
->lud_host
, ':' )) {
645 sofar
= sprintf( s
, "%s://%s%s%s:%d", u
->lud_scheme
,
647 u
->lud_host
? u
->lud_host
: "",
653 sofar
= sprintf( s
, "%s://", u
->lud_scheme
);
655 if ( u
->lud_host
&& u
->lud_host
[0] ) {
660 i
= hex_escape( &s
[sofar
], len
, u
->lud_host
, URLESC_SLASH
);
681 if ( u
->lud_dn
&& u
->lud_dn
[0] ) {
682 i
= hex_escape( &s
[sofar
], len
, u
->lud_dn
, URLESC_NONE
);
697 i
= hex_escape_list( &s
[sofar
], len
, u
->lud_attrs
, URLESC_NONE
);
711 if ( !BER_BVISNULL( &scope
) ) {
712 strcpy( &s
[sofar
], scope
.bv_val
);
713 sofar
+= scope
.bv_len
;
727 i
= hex_escape( &s
[sofar
], len
, u
->lud_filter
, URLESC_NONE
);
741 i
= hex_escape_list( &s
[sofar
], len
, u
->lud_exts
, URLESC_COMMA
);
756 ldap_url_desc2str( LDAPURLDesc
*u
)
765 len
= desc2str_len( u
);
770 /* allocate enough to hex escape everything -- overkill */
771 s
= LDAP_MALLOC( len
+ 1 );
777 if ( desc2str( u
, s
, len
) != len
) {
788 ldap_url_parse_ext( LDAP_CONST
char *url_in
, LDAPURLDesc
**ludpp
, unsigned flags
)
791 * Pick apart the pieces of an LDAP URL.
796 int i
, enclosed
, proto
, is_v6
= 0;
797 const char *scheme
= NULL
;
803 if( url_in
== NULL
|| ludpp
== NULL
) {
804 return LDAP_URL_ERR_PARAM
;
807 #ifndef LDAP_INT_IN_KERNEL
808 /* Global options may not be created yet
809 * We can't test if the global options are initialized
810 * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
811 * the options and cause infinite recursion
813 Debug( LDAP_DEBUG_TRACE
, "ldap_url_parse_ext(%s)\n", url_in
, 0, 0 );
816 *ludpp
= NULL
; /* pessimistic */
818 url_tmp
= skip_url_prefix( url_in
, &enclosed
, &scheme
);
820 if ( url_tmp
== NULL
) {
821 return LDAP_URL_ERR_BADSCHEME
;
824 assert( scheme
!= NULL
);
826 proto
= ldap_pvt_url_scheme2proto( scheme
);
828 return LDAP_URL_ERR_BADSCHEME
;
831 /* make working copy of the remainder of the URL */
832 url
= LDAP_STRDUP( url_tmp
);
834 return LDAP_URL_ERR_MEM
;
838 p
= &url
[strlen(url
)-1];
842 return LDAP_URL_ERR_BADENCLOSURE
;
848 /* allocate return struct */
849 ludp
= (LDAPURLDesc
*)LDAP_CALLOC( 1, sizeof( LDAPURLDesc
));
851 if ( ludp
== NULL
) {
853 return LDAP_URL_ERR_MEM
;
856 ludp
->lud_next
= NULL
;
857 ludp
->lud_host
= NULL
;
860 ludp
->lud_attrs
= NULL
;
861 ludp
->lud_scope
= ( flags
& LDAP_PVT_URL_PARSE_NODEF_SCOPE
) ? LDAP_SCOPE_BASE
: LDAP_SCOPE_DEFAULT
;
862 ludp
->lud_filter
= NULL
;
863 ludp
->lud_exts
= NULL
;
865 ludp
->lud_scheme
= LDAP_STRDUP( scheme
);
867 if ( ludp
->lud_scheme
== NULL
) {
869 ldap_free_urldesc( ludp
);
870 return LDAP_URL_ERR_MEM
;
873 /* scan forward for '/' that marks end of hostport and begin. of dn */
874 p
= strchr( url
, '/' );
878 /* terminate hostport; point to start of dn */
881 /* check for Novell kludge, see below */
882 p
= strchr( url
, '?' );
890 if ( proto
!= LDAP_PROTO_IPC
) {
891 /* IPv6 syntax with [ip address]:port */
893 r
= strchr( url
, ']' );
896 ldap_free_urldesc( ludp
);
897 return LDAP_URL_ERR_BADURL
;
900 q
= strchr( r
, ':' );
903 ldap_free_urldesc( ludp
);
904 return LDAP_URL_ERR_BADURL
;
908 q
= strchr( url
, ':' );
915 ldap_pvt_hex_unescape( q
);
919 ldap_free_urldesc( ludp
);
920 return LDAP_URL_ERR_BADURL
;
923 ludp
->lud_port
= strtol( q
, &next
, 10 );
924 if ( next
== q
|| next
[0] != '\0' ) {
926 ldap_free_urldesc( ludp
);
927 return LDAP_URL_ERR_BADURL
;
929 /* check for Novell kludge */
931 if ( *next
!= '\0' ) {
939 if ( ( flags
& LDAP_PVT_URL_PARSE_DEF_PORT
) && ludp
->lud_port
== 0 ) {
940 if ( strcmp( ludp
->lud_scheme
, "ldaps" ) == 0 ) {
941 ludp
->lud_port
= LDAPS_PORT
;
943 ludp
->lud_port
= LDAP_PORT
;
948 ldap_pvt_hex_unescape( url
);
950 /* If [ip address]:port syntax, url is [ip and we skip the [ */
951 ludp
->lud_host
= LDAP_STRDUP( url
+ is_v6
);
953 if( ludp
->lud_host
== NULL
) {
955 ldap_free_urldesc( ludp
);
956 return LDAP_URL_ERR_MEM
;
959 if ( ( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_HOST
)
960 && ludp
->lud_host
!= NULL
961 && *ludp
->lud_host
== '\0' )
963 LDAP_FREE( ludp
->lud_host
);
964 ludp
->lud_host
= NULL
;
968 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
970 * On early Novell releases, search references/referrals were returned
971 * in this format, i.e., the dn was kind of in the scope position,
972 * but the required slash is missing. The whole thing is illegal syntax,
973 * but we need to account for it. Fortunately it can't be confused with
976 if( (p
== NULL
) && (q
!= NULL
) && (*q
== '?') ) {
977 /* ? immediately followed by question */
981 ldap_pvt_hex_unescape( q
);
982 ludp
->lud_dn
= LDAP_STRDUP( q
);
984 } else if ( !( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_DN
) ) {
985 ludp
->lud_dn
= LDAP_STRDUP( "" );
991 if ( check_dn
&& ludp
->lud_dn
== NULL
) {
993 ldap_free_urldesc( ludp
);
994 return LDAP_URL_ERR_MEM
;
1001 return LDAP_URL_SUCCESS
;
1004 /* scan forward for '?' that may marks end of dn */
1005 q
= strchr( p
, '?' );
1008 /* terminate dn part */
1014 ldap_pvt_hex_unescape( p
);
1015 ludp
->lud_dn
= LDAP_STRDUP( p
);
1017 } else if ( !( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_DN
) ) {
1018 ludp
->lud_dn
= LDAP_STRDUP( "" );
1024 if( check_dn
&& ludp
->lud_dn
== NULL
) {
1026 ldap_free_urldesc( ludp
);
1027 return LDAP_URL_ERR_MEM
;
1034 return LDAP_URL_SUCCESS
;
1037 /* scan forward for '?' that may marks end of attributes */
1039 q
= strchr( p
, '?' );
1042 /* terminate attributes part */
1047 /* parse attributes */
1048 ldap_pvt_hex_unescape( p
);
1049 ludp
->lud_attrs
= ldap_str2charray( p
, "," );
1051 if( ludp
->lud_attrs
== NULL
) {
1053 ldap_free_urldesc( ludp
);
1054 return LDAP_URL_ERR_BADATTRS
;
1062 return LDAP_URL_SUCCESS
;
1065 /* scan forward for '?' that may marks end of scope */
1067 q
= strchr( p
, '?' );
1070 /* terminate the scope part */
1075 /* parse the scope */
1076 ldap_pvt_hex_unescape( p
);
1077 ludp
->lud_scope
= ldap_pvt_str2scope( p
);
1079 if( ludp
->lud_scope
== -1 ) {
1081 ldap_free_urldesc( ludp
);
1082 return LDAP_URL_ERR_BADSCOPE
;
1090 return LDAP_URL_SUCCESS
;
1093 /* scan forward for '?' that may marks end of filter */
1095 q
= strchr( p
, '?' );
1098 /* terminate the filter part */
1103 /* parse the filter */
1104 ldap_pvt_hex_unescape( p
);
1107 /* missing filter */
1109 ldap_free_urldesc( ludp
);
1110 return LDAP_URL_ERR_BADFILTER
;
1113 ludp
->lud_filter
= LDAP_STRDUP( p
);
1115 if( ludp
->lud_filter
== NULL
) {
1117 ldap_free_urldesc( ludp
);
1118 return LDAP_URL_ERR_MEM
;
1126 return LDAP_URL_SUCCESS
;
1129 /* scan forward for '?' that may marks end of extensions */
1131 q
= strchr( p
, '?' );
1136 ldap_free_urldesc( ludp
);
1137 return LDAP_URL_ERR_BADURL
;
1140 /* parse the extensions */
1141 ludp
->lud_exts
= ldap_str2charray( p
, "," );
1143 if( ludp
->lud_exts
== NULL
) {
1145 ldap_free_urldesc( ludp
);
1146 return LDAP_URL_ERR_BADEXTS
;
1149 for( i
=0; ludp
->lud_exts
[i
] != NULL
; i
++ ) {
1150 ldap_pvt_hex_unescape( ludp
->lud_exts
[i
] );
1152 if( *ludp
->lud_exts
[i
] == '!' ) {
1153 /* count the number of critical extensions */
1154 ludp
->lud_crit_exts
++;
1159 /* must have 1 or more */
1161 ldap_free_urldesc( ludp
);
1162 return LDAP_URL_ERR_BADEXTS
;
1168 return LDAP_URL_SUCCESS
;
1172 ldap_url_parse( LDAP_CONST
char *url_in
, LDAPURLDesc
**ludpp
)
1174 return ldap_url_parse_ext( url_in
, ludpp
, LDAP_PVT_URL_PARSE_HISTORIC
);
1178 ldap_url_dup ( LDAPURLDesc
*ludp
)
1182 if ( ludp
== NULL
) {
1186 dest
= LDAP_MALLOC( sizeof(LDAPURLDesc
) );
1191 dest
->lud_scheme
= NULL
;
1192 dest
->lud_host
= NULL
;
1193 dest
->lud_dn
= NULL
;
1194 dest
->lud_filter
= NULL
;
1195 dest
->lud_attrs
= NULL
;
1196 dest
->lud_exts
= NULL
;
1197 dest
->lud_next
= NULL
;
1199 if ( ludp
->lud_scheme
!= NULL
) {
1200 dest
->lud_scheme
= LDAP_STRDUP( ludp
->lud_scheme
);
1201 if (dest
->lud_scheme
== NULL
) {
1202 ldap_free_urldesc(dest
);
1207 if ( ludp
->lud_host
!= NULL
) {
1208 dest
->lud_host
= LDAP_STRDUP( ludp
->lud_host
);
1209 if (dest
->lud_host
== NULL
) {
1210 ldap_free_urldesc(dest
);
1215 if ( ludp
->lud_dn
!= NULL
) {
1216 dest
->lud_dn
= LDAP_STRDUP( ludp
->lud_dn
);
1217 if (dest
->lud_dn
== NULL
) {
1218 ldap_free_urldesc(dest
);
1223 if ( ludp
->lud_filter
!= NULL
) {
1224 dest
->lud_filter
= LDAP_STRDUP( ludp
->lud_filter
);
1225 if (dest
->lud_filter
== NULL
) {
1226 ldap_free_urldesc(dest
);
1231 if ( ludp
->lud_attrs
!= NULL
) {
1232 dest
->lud_attrs
= ldap_charray_dup( ludp
->lud_attrs
);
1233 if (dest
->lud_attrs
== NULL
) {
1234 ldap_free_urldesc(dest
);
1239 if ( ludp
->lud_exts
!= NULL
) {
1240 dest
->lud_exts
= ldap_charray_dup( ludp
->lud_exts
);
1241 if (dest
->lud_exts
== NULL
) {
1242 ldap_free_urldesc(dest
);
1251 ldap_url_duplist (LDAPURLDesc
*ludlist
)
1253 LDAPURLDesc
*dest
, *tail
, *ludp
, *newludp
;
1257 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1258 newludp
= ldap_url_dup(ludp
);
1259 if (newludp
== NULL
) {
1260 ldap_free_urllist(dest
);
1266 tail
->lud_next
= newludp
;
1273 ldap_url_parselist_int (LDAPURLDesc
**ludlist
, const char *url
, const char *sep
, unsigned flags
)
1280 assert( ludlist
!= NULL
);
1281 assert( url
!= NULL
);
1285 if ( sep
== NULL
) {
1289 urls
= ldap_str2charray( url
, sep
);
1291 return LDAP_URL_ERR_MEM
;
1293 /* count the URLs... */
1294 for (i
= 0; urls
[i
] != NULL
; i
++) ;
1295 /* ...and put them in the "stack" backward */
1297 rc
= ldap_url_parse_ext( urls
[i
], &ludp
, flags
);
1299 ldap_charray_free( urls
);
1300 ldap_free_urllist( *ludlist
);
1304 ludp
->lud_next
= *ludlist
;
1307 ldap_charray_free( urls
);
1308 return LDAP_URL_SUCCESS
;
1312 ldap_url_parselist (LDAPURLDesc
**ludlist
, const char *url
)
1314 return ldap_url_parselist_int( ludlist
, url
, ", ", LDAP_PVT_URL_PARSE_HISTORIC
);
1318 ldap_url_parselist_ext (LDAPURLDesc
**ludlist
, const char *url
, const char *sep
, unsigned flags
)
1320 return ldap_url_parselist_int( ludlist
, url
, sep
, flags
);
1324 ldap_url_parsehosts(
1325 LDAPURLDesc
**ludlist
,
1333 assert( ludlist
!= NULL
);
1334 assert( hosts
!= NULL
);
1338 specs
= ldap_str2charray(hosts
, ", ");
1340 return LDAP_NO_MEMORY
;
1342 /* count the URLs... */
1343 for (i
= 0; specs
[i
] != NULL
; i
++) /* EMPTY */;
1345 /* ...and put them in the "stack" backward */
1347 ludp
= LDAP_CALLOC( 1, sizeof(LDAPURLDesc
) );
1349 ldap_charray_free(specs
);
1350 ldap_free_urllist(*ludlist
);
1352 return LDAP_NO_MEMORY
;
1354 ludp
->lud_port
= port
;
1355 ludp
->lud_host
= specs
[i
];
1357 p
= strchr(ludp
->lud_host
, ':');
1359 /* more than one :, IPv6 address */
1360 if ( strchr(p
+1, ':') != NULL
) {
1361 /* allow [address] and [address]:port */
1362 if ( *ludp
->lud_host
== '[' ) {
1363 p
= LDAP_STRDUP(ludp
->lud_host
+1);
1364 /* copied, make sure we free source later */
1365 specs
[i
] = ludp
->lud_host
;
1367 p
= strchr( ludp
->lud_host
, ']' );
1370 ldap_charray_free(specs
);
1371 return LDAP_PARAM_ERROR
;
1377 ldap_charray_free(specs
);
1378 return LDAP_PARAM_ERROR
;
1390 ldap_pvt_hex_unescape(p
);
1391 ludp
->lud_port
= strtol( p
, &next
, 10 );
1392 if ( next
== p
|| next
[0] != '\0' ) {
1394 ldap_charray_free(specs
);
1395 return LDAP_PARAM_ERROR
;
1399 ldap_pvt_hex_unescape(ludp
->lud_host
);
1400 ludp
->lud_scheme
= LDAP_STRDUP("ldap");
1401 ludp
->lud_next
= *ludlist
;
1405 /* this should be an array of NULLs now */
1406 /* except entries starting with [ */
1407 ldap_charray_free(specs
);
1408 return LDAP_SUCCESS
;
1412 ldap_url_list2hosts (LDAPURLDesc
*ludlist
)
1416 char *s
, *p
, buf
[32]; /* big enough to hold a long decimal # (overkill) */
1418 if (ludlist
== NULL
)
1421 /* figure out how big the string is */
1422 size
= 1; /* nul-term */
1423 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1424 size
+= strlen(ludp
->lud_host
) + 1; /* host and space */
1425 if (strchr(ludp
->lud_host
, ':')) /* will add [ ] below */
1427 if (ludp
->lud_port
!= 0)
1428 size
+= sprintf(buf
, ":%d", ludp
->lud_port
);
1430 s
= LDAP_MALLOC(size
);
1435 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1436 if (strchr(ludp
->lud_host
, ':')) {
1437 p
+= sprintf(p
, "[%s]", ludp
->lud_host
);
1439 strcpy(p
, ludp
->lud_host
);
1440 p
+= strlen(ludp
->lud_host
);
1442 if (ludp
->lud_port
!= 0)
1443 p
+= sprintf(p
, ":%d", ludp
->lud_port
);
1447 p
--; /* nuke that extra space */
1454 LDAPURLDesc
*ludlist
)
1460 if ( ludlist
== NULL
) {
1464 /* figure out how big the string is */
1465 for ( size
= 0, ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1466 int len
= desc2str_len( ludp
);
1473 s
= LDAP_MALLOC( size
);
1479 for ( sofar
= 0, ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1482 len
= desc2str( ludp
, &s
[sofar
], size
);
1495 assert( size
>= 0 );
1498 s
[sofar
- 1] = '\0';
1504 ldap_free_urllist( LDAPURLDesc
*ludlist
)
1506 LDAPURLDesc
*ludp
, *next
;
1508 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= next
) {
1509 next
= ludp
->lud_next
;
1510 ldap_free_urldesc(ludp
);
1515 ldap_free_urldesc( LDAPURLDesc
*ludp
)
1517 if ( ludp
== NULL
) {
1521 if ( ludp
->lud_scheme
!= NULL
) {
1522 LDAP_FREE( ludp
->lud_scheme
);
1525 if ( ludp
->lud_host
!= NULL
) {
1526 LDAP_FREE( ludp
->lud_host
);
1529 if ( ludp
->lud_dn
!= NULL
) {
1530 LDAP_FREE( ludp
->lud_dn
);
1533 if ( ludp
->lud_filter
!= NULL
) {
1534 LDAP_FREE( ludp
->lud_filter
);
1537 if ( ludp
->lud_attrs
!= NULL
) {
1538 LDAP_VFREE( ludp
->lud_attrs
);
1541 if ( ludp
->lud_exts
!= NULL
) {
1542 LDAP_VFREE( ludp
->lud_exts
);
1549 ldap_int_is_hexpair( char *s
)
1553 for ( i
= 0; i
< 2; i
++ ) {
1554 if ( s
[i
] >= '0' && s
[i
] <= '9' ) {
1558 if ( s
[i
] >= 'A' && s
[i
] <= 'F' ) {
1562 if ( s
[i
] >= 'a' && s
[i
] <= 'f' ) {
1573 ldap_int_unhex( int c
)
1575 return( c
>= '0' && c
<= '9' ? c
- '0'
1576 : c
>= 'A' && c
<= 'F' ? c
- 'A' + 10
1581 ldap_pvt_hex_unescape( char *s
)
1584 * Remove URL hex escapes from s... done in place. The basic concept for
1585 * this routine is borrowed from the WWW library HTUnEscape() routine.
1590 for ( p
= s
; *s
!= '\0'; ++s
) {
1593 * FIXME: what if '%' is followed
1594 * by non-hexpair chars?
1596 if ( !ldap_int_is_hexpair( s
+ 1 ) ) {
1601 if ( *++s
== '\0' ) {
1604 *p
= ldap_int_unhex( *s
) << 4;
1605 if ( *++s
== '\0' ) {
1608 *p
++ += ldap_int_unhex( *s
);