1 /* LIBLDAP url.c -- LDAP URL (RFC 4516) related routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2022 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 * [p]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 || strcmp("pldap", scheme
) == 0 ) {
63 return LDAP_PROTO_TCP
;
66 if( strcmp("ldapi", scheme
) == 0 ) {
67 return LDAP_PROTO_IPC
;
70 if( strcmp("ldaps", scheme
) == 0 || strcmp("pldaps", 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 || strcmp("pldap", scheme
) == 0 ) {
93 if( strcmp("ldapi", scheme
) == 0 ) {
97 if( strcmp("ldaps", scheme
) == 0 || strcmp("pldaps", 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 || strcmp("pldaps", scheme
) == 0;
123 ldap_pvt_url_scheme2proxied( const char *scheme
)
125 assert( scheme
!= NULL
);
127 if( scheme
== NULL
) {
131 return strcmp("pldap", scheme
) == 0 || strcmp("pldaps", scheme
) == 0;
135 ldap_is_ldap_url( LDAP_CONST
char *url
)
144 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
152 ldap_is_ldaps_url( LDAP_CONST
char *url
)
161 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
165 return strcmp(scheme
, "ldaps") == 0 || strcmp(scheme
, "pldaps") == 0;
169 ldap_is_ldapi_url( LDAP_CONST
char *url
)
178 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
182 return strcmp(scheme
, "ldapi") == 0;
185 #ifdef LDAP_CONNECTIONLESS
187 ldap_is_ldapc_url( LDAP_CONST
char *url
)
196 if( skip_url_prefix( url
, &enclosed
, &scheme
) == NULL
) {
200 return strcmp(scheme
, "cldap") == 0;
208 const char **scheme
)
211 * return non-zero if this looks like a LDAP URL; zero if not
212 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
222 /* skip leading '<' (if any) */
230 /* skip leading "URL:" (if any) */
231 if ( strncasecmp( p
, LDAP_URL_URLCOLON
, LDAP_URL_URLCOLON_LEN
) == 0 ) {
232 p
+= LDAP_URL_URLCOLON_LEN
;
235 /* check for "ldap://" prefix */
236 if ( strncasecmp( p
, LDAP_URL_PREFIX
, LDAP_URL_PREFIX_LEN
) == 0 ) {
237 /* skip over "ldap://" prefix and return success */
238 p
+= LDAP_URL_PREFIX_LEN
;
243 /* check for "pldap://" prefix */
244 if ( strncasecmp( p
, PLDAP_URL_PREFIX
, PLDAP_URL_PREFIX_LEN
) == 0 ) {
245 /* skip over "pldap://" prefix and return success */
246 p
+= PLDAP_URL_PREFIX_LEN
;
251 /* check for "ldaps://" prefix */
252 if ( strncasecmp( p
, LDAPS_URL_PREFIX
, LDAPS_URL_PREFIX_LEN
) == 0 ) {
253 /* skip over "ldaps://" prefix and return success */
254 p
+= LDAPS_URL_PREFIX_LEN
;
259 /* check for "pldaps://" prefix */
260 if ( strncasecmp( p
, PLDAPS_URL_PREFIX
, PLDAPS_URL_PREFIX_LEN
) == 0 ) {
261 /* skip over "pldaps://" prefix and return success */
262 p
+= PLDAPS_URL_PREFIX_LEN
;
267 /* check for "ldapi://" prefix */
268 if ( strncasecmp( p
, LDAPI_URL_PREFIX
, LDAPI_URL_PREFIX_LEN
) == 0 ) {
269 /* skip over "ldapi://" prefix and return success */
270 p
+= LDAPI_URL_PREFIX_LEN
;
275 #ifdef LDAP_CONNECTIONLESS
276 /* check for "cldap://" prefix */
277 if ( strncasecmp( p
, LDAPC_URL_PREFIX
, LDAPC_URL_PREFIX_LEN
) == 0 ) {
278 /* skip over "cldap://" prefix and return success */
279 p
+= LDAPC_URL_PREFIX_LEN
;
289 ldap_pvt_scope2bv( int scope
, struct berval
*bv
)
292 case LDAP_SCOPE_BASE
:
293 BER_BVSTR( bv
, "base" );
296 case LDAP_SCOPE_ONELEVEL
:
297 BER_BVSTR( bv
, "one" );
300 case LDAP_SCOPE_SUBTREE
:
301 BER_BVSTR( bv
, "sub" );
304 case LDAP_SCOPE_SUBORDINATE
:
305 BER_BVSTR( bv
, "subordinate" );
316 ldap_pvt_scope2str( int scope
)
320 if ( ldap_pvt_scope2bv( scope
, &bv
) == LDAP_SUCCESS
) {
328 ldap_pvt_bv2scope( struct berval
*bv
)
334 { BER_BVC( "one" ), LDAP_SCOPE_ONELEVEL
},
335 { BER_BVC( "onelevel" ), LDAP_SCOPE_ONELEVEL
},
336 { BER_BVC( "base" ), LDAP_SCOPE_BASE
},
337 { BER_BVC( "sub" ), LDAP_SCOPE_SUBTREE
},
338 { BER_BVC( "subtree" ), LDAP_SCOPE_SUBTREE
},
339 { BER_BVC( "subord" ), LDAP_SCOPE_SUBORDINATE
},
340 { BER_BVC( "subordinate" ), LDAP_SCOPE_SUBORDINATE
},
341 { BER_BVC( "children" ), LDAP_SCOPE_SUBORDINATE
},
346 for ( i
= 0; v
[ i
].scope
!= -1; i
++ ) {
347 if ( ber_bvstrcasecmp( bv
, &v
[ i
].bv
) == 0 ) {
356 ldap_pvt_str2scope( const char *p
)
360 ber_str2bv( p
, 0, 0, &bv
);
362 return ldap_pvt_bv2scope( &bv
);
365 static const char hex
[] = "0123456789ABCDEF";
367 #define URLESC_NONE 0x0000U
368 #define URLESC_COMMA 0x0001U
369 #define URLESC_SLASH 0x0002U
372 hex_escape_len( const char *s
, unsigned list
)
380 for ( len
= 0; s
[0]; s
++ ) {
382 /* RFC 2396: reserved */
388 if ( list
& URLESC_COMMA
) {
396 if ( list
& URLESC_SLASH
) {
411 /* RFC 2396: unreserved mark */
424 /* RFC 2396: unreserved alphanum */
426 if ( !isalnum( (unsigned char) s
[0] ) ) {
439 hex_escape( char *buf
, int len
, const char *s
, unsigned list
)
448 for ( pos
= 0, i
= 0; s
[i
] && pos
< len
; i
++ ) {
452 /* RFC 2396: reserved */
458 if ( list
& URLESC_COMMA
) {
464 if ( list
& URLESC_SLASH
) {
477 /* RFC 2396: unreserved mark */
489 /* RFC 2396: unreserved alphanum */
491 if ( !isalnum( (unsigned char) s
[i
] ) ) {
499 buf
[pos
++] = hex
[ (s
[i
] >> 4) & 0x0f ];
500 buf
[pos
++] = hex
[ s
[i
] & 0x0f ];
513 hex_escape_len_list( char **s
, unsigned flags
)
523 for ( i
= 0; s
[i
] != NULL
; i
++ ) {
527 len
+= hex_escape_len( s
[i
], flags
);
534 hex_escape_list( char *buf
, int len
, char **s
, unsigned flags
)
544 for ( i
= 0; s
[i
] != NULL
; i
++ ) {
551 curlen
= hex_escape( &buf
[pos
], len
, s
[i
], flags
);
560 desc2str_len( LDAPURLDesc
*u
)
567 if ( u
== NULL
|| u
->lud_scheme
== NULL
) {
571 if ( !strcmp( "ldapi", u
->lud_scheme
)) {
576 len
+= hex_escape_len_list( u
->lud_exts
, URLESC_COMMA
);
582 if ( u
->lud_filter
) {
583 len
+= hex_escape_len( u
->lud_filter
, URLESC_NONE
);
589 if ( ldap_pvt_scope2bv( u
->lud_scope
, &scope
) == LDAP_SUCCESS
) {
596 if ( u
->lud_attrs
) {
597 len
+= hex_escape_len_list( u
->lud_attrs
, URLESC_NONE
);
603 if ( u
->lud_dn
&& u
->lud_dn
[0] ) {
604 len
+= hex_escape_len( u
->lud_dn
, URLESC_NONE
);
613 unsigned p
= u
->lud_port
;
617 len
+= (p
> 999 ? 5 + (p
> 9999) : p
> 99 ? 4 : 2 + (p
> 9));
620 if ( u
->lud_host
&& u
->lud_host
[0] ) {
622 len
+= hex_escape_len( u
->lud_host
, URLESC_SLASH
);
623 if ( !is_ipc
&& ( ptr
= strchr( u
->lud_host
, ':' ))) {
624 if ( strchr( ptr
+1, ':' ))
625 len
+= 2; /* IPv6, [] */
629 len
+= strlen( u
->lud_scheme
) + STRLENOF( "://" );
635 desc2str( LDAPURLDesc
*u
, char *s
, int len
)
642 struct berval scope
= BER_BVNULL
;
653 if ( u
->lud_scheme
&& !strcmp( "ldapi", u
->lud_scheme
)) {
657 ldap_pvt_scope2bv( u
->lud_scope
, &scope
);
661 } else if ( u
->lud_filter
) {
663 } else if ( !BER_BVISEMPTY( &scope
) ) {
665 } else if ( u
->lud_attrs
) {
667 } else if ( u
->lud_dn
&& u
->lud_dn
[0] ) {
671 if ( !is_ipc
&& u
->lud_host
&& ( ptr
= strchr( u
->lud_host
, ':' ))) {
672 if ( strchr( ptr
+1, ':' ))
677 sofar
= sprintf( s
, "%s://%s%s%s:%d", u
->lud_scheme
,
679 u
->lud_host
? u
->lud_host
: "",
685 sofar
= sprintf( s
, "%s://", u
->lud_scheme
);
687 if ( u
->lud_host
&& u
->lud_host
[0] ) {
692 i
= hex_escape( &s
[sofar
], len
, u
->lud_host
, URLESC_SLASH
);
713 if ( u
->lud_dn
&& u
->lud_dn
[0] ) {
714 i
= hex_escape( &s
[sofar
], len
, u
->lud_dn
, URLESC_NONE
);
729 i
= hex_escape_list( &s
[sofar
], len
, u
->lud_attrs
, URLESC_NONE
);
743 if ( !BER_BVISNULL( &scope
) ) {
744 strcpy( &s
[sofar
], scope
.bv_val
);
745 sofar
+= scope
.bv_len
;
759 i
= hex_escape( &s
[sofar
], len
, u
->lud_filter
, URLESC_NONE
);
773 i
= hex_escape_list( &s
[sofar
], len
, u
->lud_exts
, URLESC_COMMA
);
788 ldap_url_desc2str( LDAPURLDesc
*u
)
797 len
= desc2str_len( u
);
802 /* allocate enough to hex escape everything -- overkill */
803 s
= LDAP_MALLOC( len
+ 1 );
809 if ( desc2str( u
, s
, len
) != len
) {
820 ldap_url_parse_ext( LDAP_CONST
char *url_in
, LDAPURLDesc
**ludpp
, unsigned flags
)
823 * Pick apart the pieces of an LDAP URL.
828 int i
, enclosed
, proto
, is_v6
= 0;
829 const char *scheme
= NULL
;
835 if( url_in
== NULL
|| ludpp
== NULL
) {
836 return LDAP_URL_ERR_PARAM
;
839 #ifndef LDAP_INT_IN_KERNEL
840 /* Global options may not be created yet
841 * We can't test if the global options are initialized
842 * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
843 * the options and cause infinite recursion
845 Debug1( LDAP_DEBUG_TRACE
, "ldap_url_parse_ext(%s)\n", url_in
);
848 *ludpp
= NULL
; /* pessimistic */
850 url_tmp
= skip_url_prefix( url_in
, &enclosed
, &scheme
);
852 if ( url_tmp
== NULL
) {
853 return LDAP_URL_ERR_BADSCHEME
;
856 assert( scheme
!= NULL
);
858 proto
= ldap_pvt_url_scheme2proto( scheme
);
860 return LDAP_URL_ERR_BADSCHEME
;
863 /* make working copy of the remainder of the URL */
864 url
= LDAP_STRDUP( url_tmp
);
866 return LDAP_URL_ERR_MEM
;
870 p
= &url
[strlen(url
)-1];
874 return LDAP_URL_ERR_BADENCLOSURE
;
880 /* allocate return struct */
881 ludp
= (LDAPURLDesc
*)LDAP_CALLOC( 1, sizeof( LDAPURLDesc
));
883 if ( ludp
== NULL
) {
885 return LDAP_URL_ERR_MEM
;
888 ludp
->lud_next
= NULL
;
889 ludp
->lud_host
= NULL
;
892 ludp
->lud_attrs
= NULL
;
893 ludp
->lud_scope
= ( flags
& LDAP_PVT_URL_PARSE_NODEF_SCOPE
) ? LDAP_SCOPE_BASE
: LDAP_SCOPE_DEFAULT
;
894 ludp
->lud_filter
= NULL
;
895 ludp
->lud_exts
= NULL
;
897 ludp
->lud_scheme
= LDAP_STRDUP( scheme
);
899 if ( ludp
->lud_scheme
== NULL
) {
901 ldap_free_urldesc( ludp
);
902 return LDAP_URL_ERR_MEM
;
905 /* scan forward for '/' that marks end of hostport and begin. of dn */
906 p
= strchr( url
, '/' );
910 /* terminate hostport; point to start of dn */
913 /* check for Novell kludge, see below */
914 p
= strchr( url
, '?' );
922 if ( proto
!= LDAP_PROTO_IPC
) {
923 /* IPv6 syntax with [ip address]:port */
925 r
= strchr( url
, ']' );
928 ldap_free_urldesc( ludp
);
929 return LDAP_URL_ERR_BADURL
;
932 q
= strchr( r
, ':' );
935 ldap_free_urldesc( ludp
);
936 return LDAP_URL_ERR_BADURL
;
940 q
= strchr( url
, ':' );
947 ldap_pvt_hex_unescape( q
);
951 ldap_free_urldesc( ludp
);
952 return LDAP_URL_ERR_BADURL
;
955 ludp
->lud_port
= strtol( q
, &next
, 10 );
956 if ( next
== q
|| next
[0] != '\0' ) {
958 ldap_free_urldesc( ludp
);
959 return LDAP_URL_ERR_BADURL
;
961 /* check for Novell kludge */
963 if ( *next
!= '\0' ) {
971 if ( ( flags
& LDAP_PVT_URL_PARSE_DEF_PORT
) && ludp
->lud_port
== 0 ) {
972 if ( strcmp( ludp
->lud_scheme
, "ldaps" ) == 0 ) {
973 ludp
->lud_port
= LDAPS_PORT
;
975 ludp
->lud_port
= LDAP_PORT
;
980 ldap_pvt_hex_unescape( url
);
982 /* If [ip address]:port syntax, url is [ip and we skip the [ */
983 ludp
->lud_host
= LDAP_STRDUP( url
+ is_v6
);
985 if( ludp
->lud_host
== NULL
) {
987 ldap_free_urldesc( ludp
);
988 return LDAP_URL_ERR_MEM
;
991 if ( ( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_HOST
)
992 && ludp
->lud_host
!= NULL
993 && *ludp
->lud_host
== '\0' )
995 LDAP_FREE( ludp
->lud_host
);
996 ludp
->lud_host
= NULL
;
1000 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
1002 * On early Novell releases, search references/referrals were returned
1003 * in this format, i.e., the dn was kind of in the scope position,
1004 * but the required slash is missing. The whole thing is illegal syntax,
1005 * but we need to account for it. Fortunately it can't be confused with
1008 if( (p
== NULL
) && (q
!= NULL
) && (*q
== '?') ) {
1009 /* ? immediately followed by question */
1013 ldap_pvt_hex_unescape( q
);
1014 ludp
->lud_dn
= LDAP_STRDUP( q
);
1016 } else if ( !( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_DN
) ) {
1017 ludp
->lud_dn
= LDAP_STRDUP( "" );
1023 if ( check_dn
&& ludp
->lud_dn
== NULL
) {
1025 ldap_free_urldesc( ludp
);
1026 return LDAP_URL_ERR_MEM
;
1033 return LDAP_URL_SUCCESS
;
1036 /* scan forward for '?' that may marks end of dn */
1037 q
= strchr( p
, '?' );
1040 /* terminate dn part */
1046 ldap_pvt_hex_unescape( p
);
1047 ludp
->lud_dn
= LDAP_STRDUP( p
);
1049 } else if ( !( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_DN
) ) {
1050 ludp
->lud_dn
= LDAP_STRDUP( "" );
1056 if( check_dn
&& ludp
->lud_dn
== NULL
) {
1058 ldap_free_urldesc( ludp
);
1059 return LDAP_URL_ERR_MEM
;
1066 return LDAP_URL_SUCCESS
;
1069 /* scan forward for '?' that may marks end of attributes */
1071 q
= strchr( p
, '?' );
1074 /* terminate attributes part */
1079 /* parse attributes */
1080 ldap_pvt_hex_unescape( p
);
1081 ludp
->lud_attrs
= ldap_str2charray( p
, "," );
1083 if( ludp
->lud_attrs
== NULL
) {
1085 ldap_free_urldesc( ludp
);
1086 return LDAP_URL_ERR_BADATTRS
;
1094 return LDAP_URL_SUCCESS
;
1097 /* scan forward for '?' that may marks end of scope */
1099 q
= strchr( p
, '?' );
1102 /* terminate the scope part */
1107 /* parse the scope */
1108 ldap_pvt_hex_unescape( p
);
1109 ludp
->lud_scope
= ldap_pvt_str2scope( p
);
1111 if( ludp
->lud_scope
== -1 ) {
1113 ldap_free_urldesc( ludp
);
1114 return LDAP_URL_ERR_BADSCOPE
;
1122 return LDAP_URL_SUCCESS
;
1125 /* scan forward for '?' that may marks end of filter */
1127 q
= strchr( p
, '?' );
1130 /* terminate the filter part */
1135 /* parse the filter */
1136 ldap_pvt_hex_unescape( p
);
1139 /* missing filter */
1141 ldap_free_urldesc( ludp
);
1142 return LDAP_URL_ERR_BADFILTER
;
1145 ludp
->lud_filter
= LDAP_STRDUP( p
);
1147 if( ludp
->lud_filter
== NULL
) {
1149 ldap_free_urldesc( ludp
);
1150 return LDAP_URL_ERR_MEM
;
1158 return LDAP_URL_SUCCESS
;
1161 /* scan forward for '?' that may marks end of extensions */
1163 q
= strchr( p
, '?' );
1168 ldap_free_urldesc( ludp
);
1169 return LDAP_URL_ERR_BADURL
;
1172 /* parse the extensions */
1173 ludp
->lud_exts
= ldap_str2charray( p
, "," );
1175 if( ludp
->lud_exts
== NULL
) {
1177 ldap_free_urldesc( ludp
);
1178 return LDAP_URL_ERR_BADEXTS
;
1181 for( i
=0; ludp
->lud_exts
[i
] != NULL
; i
++ ) {
1182 ldap_pvt_hex_unescape( ludp
->lud_exts
[i
] );
1184 if( *ludp
->lud_exts
[i
] == '!' ) {
1185 /* count the number of critical extensions */
1186 ludp
->lud_crit_exts
++;
1191 /* must have 1 or more */
1193 ldap_free_urldesc( ludp
);
1194 return LDAP_URL_ERR_BADEXTS
;
1200 return LDAP_URL_SUCCESS
;
1204 ldap_url_parse( LDAP_CONST
char *url_in
, LDAPURLDesc
**ludpp
)
1206 return ldap_url_parse_ext( url_in
, ludpp
, LDAP_PVT_URL_PARSE_HISTORIC
);
1210 ldap_url_dup ( LDAPURLDesc
*ludp
)
1214 if ( ludp
== NULL
) {
1218 dest
= LDAP_MALLOC( sizeof(LDAPURLDesc
) );
1223 dest
->lud_scheme
= NULL
;
1224 dest
->lud_host
= NULL
;
1225 dest
->lud_dn
= NULL
;
1226 dest
->lud_filter
= NULL
;
1227 dest
->lud_attrs
= NULL
;
1228 dest
->lud_exts
= NULL
;
1229 dest
->lud_next
= NULL
;
1231 if ( ludp
->lud_scheme
!= NULL
) {
1232 dest
->lud_scheme
= LDAP_STRDUP( ludp
->lud_scheme
);
1233 if (dest
->lud_scheme
== NULL
) {
1234 ldap_free_urldesc(dest
);
1239 if ( ludp
->lud_host
!= NULL
) {
1240 dest
->lud_host
= LDAP_STRDUP( ludp
->lud_host
);
1241 if (dest
->lud_host
== NULL
) {
1242 ldap_free_urldesc(dest
);
1247 if ( ludp
->lud_dn
!= NULL
) {
1248 dest
->lud_dn
= LDAP_STRDUP( ludp
->lud_dn
);
1249 if (dest
->lud_dn
== NULL
) {
1250 ldap_free_urldesc(dest
);
1255 if ( ludp
->lud_filter
!= NULL
) {
1256 dest
->lud_filter
= LDAP_STRDUP( ludp
->lud_filter
);
1257 if (dest
->lud_filter
== NULL
) {
1258 ldap_free_urldesc(dest
);
1263 if ( ludp
->lud_attrs
!= NULL
) {
1264 dest
->lud_attrs
= ldap_charray_dup( ludp
->lud_attrs
);
1265 if (dest
->lud_attrs
== NULL
) {
1266 ldap_free_urldesc(dest
);
1271 if ( ludp
->lud_exts
!= NULL
) {
1272 dest
->lud_exts
= ldap_charray_dup( ludp
->lud_exts
);
1273 if (dest
->lud_exts
== NULL
) {
1274 ldap_free_urldesc(dest
);
1283 ldap_url_duplist (LDAPURLDesc
*ludlist
)
1285 LDAPURLDesc
*dest
, *tail
, *ludp
, *newludp
;
1289 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1290 newludp
= ldap_url_dup(ludp
);
1291 if (newludp
== NULL
) {
1292 ldap_free_urllist(dest
);
1298 tail
->lud_next
= newludp
;
1305 ldap_url_parselist_int (LDAPURLDesc
**ludlist
, const char *url
, const char *sep
, unsigned flags
)
1312 assert( ludlist
!= NULL
);
1313 assert( url
!= NULL
);
1317 if ( sep
== NULL
) {
1321 urls
= ldap_str2charray( url
, sep
);
1323 return LDAP_URL_ERR_MEM
;
1325 /* count the URLs... */
1326 for (i
= 0; urls
[i
] != NULL
; i
++) ;
1327 /* ...and put them in the "stack" backward */
1329 rc
= ldap_url_parse_ext( urls
[i
], &ludp
, flags
);
1331 ldap_charray_free( urls
);
1332 ldap_free_urllist( *ludlist
);
1336 ludp
->lud_next
= *ludlist
;
1339 ldap_charray_free( urls
);
1340 return LDAP_URL_SUCCESS
;
1344 ldap_url_parselist (LDAPURLDesc
**ludlist
, const char *url
)
1346 return ldap_url_parselist_int( ludlist
, url
, ", ", LDAP_PVT_URL_PARSE_HISTORIC
);
1350 ldap_url_parselist_ext (LDAPURLDesc
**ludlist
, const char *url
, const char *sep
, unsigned flags
)
1352 return ldap_url_parselist_int( ludlist
, url
, sep
, flags
);
1356 ldap_url_parsehosts(
1357 LDAPURLDesc
**ludlist
,
1365 assert( ludlist
!= NULL
);
1366 assert( hosts
!= NULL
);
1370 specs
= ldap_str2charray(hosts
, ", ");
1372 return LDAP_NO_MEMORY
;
1374 /* count the URLs... */
1375 for (i
= 0; specs
[i
] != NULL
; i
++) /* EMPTY */;
1377 /* ...and put them in the "stack" backward */
1379 ludp
= LDAP_CALLOC( 1, sizeof(LDAPURLDesc
) );
1381 ldap_charray_free(specs
);
1382 ldap_free_urllist(*ludlist
);
1384 return LDAP_NO_MEMORY
;
1386 ludp
->lud_port
= port
;
1387 ludp
->lud_host
= specs
[i
];
1388 p
= strchr(ludp
->lud_host
, ':');
1390 /* more than one :, IPv6 address */
1391 if ( strchr(p
+1, ':') != NULL
) {
1392 /* allow [address] and [address]:port */
1393 if ( *ludp
->lud_host
== '[' ) {
1394 p
= strchr( ludp
->lud_host
+1, ']' );
1397 ldap_charray_free(specs
);
1398 return LDAP_PARAM_ERROR
;
1400 /* Truncate trailing ']' and shift hostname down 1 char */
1402 AC_MEMCPY( ludp
->lud_host
, ludp
->lud_host
+1, p
- ludp
->lud_host
);
1407 ldap_charray_free(specs
);
1408 return LDAP_PARAM_ERROR
;
1420 ldap_pvt_hex_unescape(p
);
1421 ludp
->lud_port
= strtol( p
, &next
, 10 );
1422 if ( next
== p
|| next
[0] != '\0' ) {
1424 ldap_charray_free(specs
);
1425 return LDAP_PARAM_ERROR
;
1429 ludp
->lud_scheme
= LDAP_STRDUP("ldap");
1430 if ( ludp
->lud_scheme
== NULL
) {
1432 ldap_charray_free(specs
);
1433 return LDAP_NO_MEMORY
;
1436 ldap_pvt_hex_unescape(ludp
->lud_host
);
1437 ludp
->lud_next
= *ludlist
;
1441 /* this should be an array of NULLs now */
1442 ldap_charray_free(specs
);
1443 return LDAP_SUCCESS
;
1447 ldap_url_list2hosts (LDAPURLDesc
*ludlist
)
1451 char *s
, *p
, buf
[32]; /* big enough to hold a long decimal # (overkill) */
1453 if (ludlist
== NULL
)
1456 /* figure out how big the string is */
1457 size
= 1; /* nul-term */
1458 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1459 if ( ludp
->lud_host
== NULL
) continue;
1460 size
+= strlen(ludp
->lud_host
) + 1; /* host and space */
1461 if (strchr(ludp
->lud_host
, ':')) /* will add [ ] below */
1463 if (ludp
->lud_port
!= 0)
1464 size
+= sprintf(buf
, ":%d", ludp
->lud_port
);
1466 s
= LDAP_MALLOC(size
);
1471 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1472 if ( ludp
->lud_host
== NULL
) continue;
1473 if (strchr(ludp
->lud_host
, ':')) {
1474 p
+= sprintf(p
, "[%s]", ludp
->lud_host
);
1476 strcpy(p
, ludp
->lud_host
);
1477 p
+= strlen(ludp
->lud_host
);
1479 if (ludp
->lud_port
!= 0)
1480 p
+= sprintf(p
, ":%d", ludp
->lud_port
);
1484 p
--; /* nuke that extra space */
1491 LDAPURLDesc
*ludlist
)
1497 if ( ludlist
== NULL
) {
1501 /* figure out how big the string is */
1502 for ( size
= 0, ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1503 int len
= desc2str_len( ludp
);
1510 s
= LDAP_MALLOC( size
);
1516 for ( sofar
= 0, ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1519 len
= desc2str( ludp
, &s
[sofar
], size
);
1532 assert( size
>= 0 );
1535 s
[sofar
- 1] = '\0';
1541 ldap_free_urllist( LDAPURLDesc
*ludlist
)
1543 LDAPURLDesc
*ludp
, *next
;
1545 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= next
) {
1546 next
= ludp
->lud_next
;
1547 ldap_free_urldesc(ludp
);
1552 ldap_free_urldesc( LDAPURLDesc
*ludp
)
1554 if ( ludp
== NULL
) {
1558 if ( ludp
->lud_scheme
!= NULL
) {
1559 LDAP_FREE( ludp
->lud_scheme
);
1562 if ( ludp
->lud_host
!= NULL
) {
1563 LDAP_FREE( ludp
->lud_host
);
1566 if ( ludp
->lud_dn
!= NULL
) {
1567 LDAP_FREE( ludp
->lud_dn
);
1570 if ( ludp
->lud_filter
!= NULL
) {
1571 LDAP_FREE( ludp
->lud_filter
);
1574 if ( ludp
->lud_attrs
!= NULL
) {
1575 LDAP_VFREE( ludp
->lud_attrs
);
1578 if ( ludp
->lud_exts
!= NULL
) {
1579 LDAP_VFREE( ludp
->lud_exts
);
1586 ldap_int_is_hexpair( char *s
)
1590 for ( i
= 0; i
< 2; i
++ ) {
1591 if ( s
[i
] >= '0' && s
[i
] <= '9' ) {
1595 if ( s
[i
] >= 'A' && s
[i
] <= 'F' ) {
1599 if ( s
[i
] >= 'a' && s
[i
] <= 'f' ) {
1610 ldap_int_unhex( int c
)
1612 return( c
>= '0' && c
<= '9' ? c
- '0'
1613 : c
>= 'A' && c
<= 'F' ? c
- 'A' + 10
1618 ldap_pvt_hex_unescape( char *s
)
1621 * Remove URL hex escapes from s... done in place. The basic concept for
1622 * this routine is borrowed from the WWW library HTUnEscape() routine.
1627 for ( p
= s
; *s
!= '\0'; ++s
) {
1630 * FIXME: what if '%' is followed
1631 * by non-hexpair chars?
1633 if ( !ldap_int_is_hexpair( s
+ 1 ) ) {
1638 if ( *++s
== '\0' ) {
1641 *p
= ldap_int_unhex( *s
) << 4;
1642 if ( *++s
== '\0' ) {
1645 *p
++ += ldap_int_unhex( *s
);