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
;
872 return LDAP_URL_ERR_BADENCLOSURE
;
874 p
= &url
[strlen(url
)-1];
878 return LDAP_URL_ERR_BADENCLOSURE
;
884 /* allocate return struct */
885 ludp
= (LDAPURLDesc
*)LDAP_CALLOC( 1, sizeof( LDAPURLDesc
));
887 if ( ludp
== NULL
) {
889 return LDAP_URL_ERR_MEM
;
892 ludp
->lud_next
= NULL
;
893 ludp
->lud_host
= NULL
;
896 ludp
->lud_attrs
= NULL
;
897 ludp
->lud_scope
= ( flags
& LDAP_PVT_URL_PARSE_NODEF_SCOPE
) ? LDAP_SCOPE_BASE
: LDAP_SCOPE_DEFAULT
;
898 ludp
->lud_filter
= NULL
;
899 ludp
->lud_exts
= NULL
;
901 ludp
->lud_scheme
= LDAP_STRDUP( scheme
);
903 if ( ludp
->lud_scheme
== NULL
) {
905 ldap_free_urldesc( ludp
);
906 return LDAP_URL_ERR_MEM
;
909 /* scan forward for '/' that marks end of hostport and begin. of dn */
910 p
= strchr( url
, '/' );
914 /* terminate hostport; point to start of dn */
917 /* check for Novell kludge, see below */
918 p
= strchr( url
, '?' );
926 if ( proto
!= LDAP_PROTO_IPC
) {
927 /* IPv6 syntax with [ip address]:port */
929 r
= strchr( url
, ']' );
932 ldap_free_urldesc( ludp
);
933 return LDAP_URL_ERR_BADURL
;
936 q
= strchr( r
, ':' );
939 ldap_free_urldesc( ludp
);
940 return LDAP_URL_ERR_BADURL
;
944 q
= strchr( url
, ':' );
951 ldap_pvt_hex_unescape( q
);
955 ldap_free_urldesc( ludp
);
956 return LDAP_URL_ERR_BADURL
;
959 ludp
->lud_port
= strtol( q
, &next
, 10 );
960 if ( next
== q
|| next
[0] != '\0' ) {
962 ldap_free_urldesc( ludp
);
963 return LDAP_URL_ERR_BADURL
;
965 /* check for Novell kludge */
967 if ( *next
!= '\0' ) {
975 if ( ( flags
& LDAP_PVT_URL_PARSE_DEF_PORT
) && ludp
->lud_port
== 0 ) {
976 if ( strcmp( ludp
->lud_scheme
, "ldaps" ) == 0 ) {
977 ludp
->lud_port
= LDAPS_PORT
;
979 ludp
->lud_port
= LDAP_PORT
;
984 ldap_pvt_hex_unescape( url
);
986 /* If [ip address]:port syntax, url is [ip and we skip the [ */
987 ludp
->lud_host
= LDAP_STRDUP( url
+ is_v6
);
989 if( ludp
->lud_host
== NULL
) {
991 ldap_free_urldesc( ludp
);
992 return LDAP_URL_ERR_MEM
;
995 if ( ( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_HOST
)
996 && ludp
->lud_host
!= NULL
997 && *ludp
->lud_host
== '\0' )
999 LDAP_FREE( ludp
->lud_host
);
1000 ludp
->lud_host
= NULL
;
1004 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
1006 * On early Novell releases, search references/referrals were returned
1007 * in this format, i.e., the dn was kind of in the scope position,
1008 * but the required slash is missing. The whole thing is illegal syntax,
1009 * but we need to account for it. Fortunately it can't be confused with
1012 if( (p
== NULL
) && (q
!= NULL
) && (*q
== '?') ) {
1013 /* ? immediately followed by question */
1017 ldap_pvt_hex_unescape( q
);
1018 ludp
->lud_dn
= LDAP_STRDUP( q
);
1020 } else if ( !( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_DN
) ) {
1021 ludp
->lud_dn
= LDAP_STRDUP( "" );
1027 if ( check_dn
&& ludp
->lud_dn
== NULL
) {
1029 ldap_free_urldesc( ludp
);
1030 return LDAP_URL_ERR_MEM
;
1037 return LDAP_URL_SUCCESS
;
1040 /* scan forward for '?' that may marks end of dn */
1041 q
= strchr( p
, '?' );
1044 /* terminate dn part */
1050 ldap_pvt_hex_unescape( p
);
1051 ludp
->lud_dn
= LDAP_STRDUP( p
);
1053 } else if ( !( flags
& LDAP_PVT_URL_PARSE_NOEMPTY_DN
) ) {
1054 ludp
->lud_dn
= LDAP_STRDUP( "" );
1060 if( check_dn
&& ludp
->lud_dn
== NULL
) {
1062 ldap_free_urldesc( ludp
);
1063 return LDAP_URL_ERR_MEM
;
1070 return LDAP_URL_SUCCESS
;
1073 /* scan forward for '?' that may marks end of attributes */
1075 q
= strchr( p
, '?' );
1078 /* terminate attributes part */
1083 /* parse attributes */
1084 ldap_pvt_hex_unescape( p
);
1085 ludp
->lud_attrs
= ldap_str2charray( p
, "," );
1087 if( ludp
->lud_attrs
== NULL
) {
1089 ldap_free_urldesc( ludp
);
1090 return LDAP_URL_ERR_BADATTRS
;
1098 return LDAP_URL_SUCCESS
;
1101 /* scan forward for '?' that may marks end of scope */
1103 q
= strchr( p
, '?' );
1106 /* terminate the scope part */
1111 /* parse the scope */
1112 ldap_pvt_hex_unescape( p
);
1113 ludp
->lud_scope
= ldap_pvt_str2scope( p
);
1115 if( ludp
->lud_scope
== -1 ) {
1117 ldap_free_urldesc( ludp
);
1118 return LDAP_URL_ERR_BADSCOPE
;
1126 return LDAP_URL_SUCCESS
;
1129 /* scan forward for '?' that may marks end of filter */
1131 q
= strchr( p
, '?' );
1134 /* terminate the filter part */
1139 /* parse the filter */
1140 ldap_pvt_hex_unescape( p
);
1143 /* missing filter */
1145 ldap_free_urldesc( ludp
);
1146 return LDAP_URL_ERR_BADFILTER
;
1149 ludp
->lud_filter
= LDAP_STRDUP( p
);
1151 if( ludp
->lud_filter
== NULL
) {
1153 ldap_free_urldesc( ludp
);
1154 return LDAP_URL_ERR_MEM
;
1162 return LDAP_URL_SUCCESS
;
1165 /* scan forward for '?' that may marks end of extensions */
1167 q
= strchr( p
, '?' );
1172 ldap_free_urldesc( ludp
);
1173 return LDAP_URL_ERR_BADURL
;
1176 /* parse the extensions */
1177 ludp
->lud_exts
= ldap_str2charray( p
, "," );
1179 if( ludp
->lud_exts
== NULL
) {
1181 ldap_free_urldesc( ludp
);
1182 return LDAP_URL_ERR_BADEXTS
;
1185 for( i
=0; ludp
->lud_exts
[i
] != NULL
; i
++ ) {
1186 ldap_pvt_hex_unescape( ludp
->lud_exts
[i
] );
1188 if( *ludp
->lud_exts
[i
] == '!' ) {
1189 /* count the number of critical extensions */
1190 ludp
->lud_crit_exts
++;
1195 /* must have 1 or more */
1197 ldap_free_urldesc( ludp
);
1198 return LDAP_URL_ERR_BADEXTS
;
1204 return LDAP_URL_SUCCESS
;
1208 ldap_url_parse( LDAP_CONST
char *url_in
, LDAPURLDesc
**ludpp
)
1210 return ldap_url_parse_ext( url_in
, ludpp
, LDAP_PVT_URL_PARSE_HISTORIC
);
1214 ldap_url_dup ( LDAPURLDesc
*ludp
)
1218 if ( ludp
== NULL
) {
1222 dest
= LDAP_MALLOC( sizeof(LDAPURLDesc
) );
1227 dest
->lud_scheme
= NULL
;
1228 dest
->lud_host
= NULL
;
1229 dest
->lud_dn
= NULL
;
1230 dest
->lud_filter
= NULL
;
1231 dest
->lud_attrs
= NULL
;
1232 dest
->lud_exts
= NULL
;
1233 dest
->lud_next
= NULL
;
1235 if ( ludp
->lud_scheme
!= NULL
) {
1236 dest
->lud_scheme
= LDAP_STRDUP( ludp
->lud_scheme
);
1237 if (dest
->lud_scheme
== NULL
) {
1238 ldap_free_urldesc(dest
);
1243 if ( ludp
->lud_host
!= NULL
) {
1244 dest
->lud_host
= LDAP_STRDUP( ludp
->lud_host
);
1245 if (dest
->lud_host
== NULL
) {
1246 ldap_free_urldesc(dest
);
1251 if ( ludp
->lud_dn
!= NULL
) {
1252 dest
->lud_dn
= LDAP_STRDUP( ludp
->lud_dn
);
1253 if (dest
->lud_dn
== NULL
) {
1254 ldap_free_urldesc(dest
);
1259 if ( ludp
->lud_filter
!= NULL
) {
1260 dest
->lud_filter
= LDAP_STRDUP( ludp
->lud_filter
);
1261 if (dest
->lud_filter
== NULL
) {
1262 ldap_free_urldesc(dest
);
1267 if ( ludp
->lud_attrs
!= NULL
) {
1268 dest
->lud_attrs
= ldap_charray_dup( ludp
->lud_attrs
);
1269 if (dest
->lud_attrs
== NULL
) {
1270 ldap_free_urldesc(dest
);
1275 if ( ludp
->lud_exts
!= NULL
) {
1276 dest
->lud_exts
= ldap_charray_dup( ludp
->lud_exts
);
1277 if (dest
->lud_exts
== NULL
) {
1278 ldap_free_urldesc(dest
);
1287 ldap_url_duplist (LDAPURLDesc
*ludlist
)
1289 LDAPURLDesc
*dest
, *tail
, *ludp
, *newludp
;
1293 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1294 newludp
= ldap_url_dup(ludp
);
1295 if (newludp
== NULL
) {
1296 ldap_free_urllist(dest
);
1302 tail
->lud_next
= newludp
;
1309 ldap_url_parselist_int (LDAPURLDesc
**ludlist
, const char *url
, const char *sep
, unsigned flags
)
1316 assert( ludlist
!= NULL
);
1317 assert( url
!= NULL
);
1321 if ( sep
== NULL
) {
1325 urls
= ldap_str2charray( url
, sep
);
1327 return LDAP_URL_ERR_MEM
;
1329 /* count the URLs... */
1330 for (i
= 0; urls
[i
] != NULL
; i
++) ;
1331 /* ...and put them in the "stack" backward */
1333 rc
= ldap_url_parse_ext( urls
[i
], &ludp
, flags
);
1335 ldap_charray_free( urls
);
1336 ldap_free_urllist( *ludlist
);
1340 ludp
->lud_next
= *ludlist
;
1343 ldap_charray_free( urls
);
1344 return LDAP_URL_SUCCESS
;
1348 ldap_url_parselist (LDAPURLDesc
**ludlist
, const char *url
)
1350 return ldap_url_parselist_int( ludlist
, url
, ", ", LDAP_PVT_URL_PARSE_HISTORIC
);
1354 ldap_url_parselist_ext (LDAPURLDesc
**ludlist
, const char *url
, const char *sep
, unsigned flags
)
1356 return ldap_url_parselist_int( ludlist
, url
, sep
, flags
);
1360 ldap_url_parsehosts(
1361 LDAPURLDesc
**ludlist
,
1369 assert( ludlist
!= NULL
);
1370 assert( hosts
!= NULL
);
1374 specs
= ldap_str2charray(hosts
, ", ");
1376 return LDAP_NO_MEMORY
;
1378 /* count the URLs... */
1379 for (i
= 0; specs
[i
] != NULL
; i
++) /* EMPTY */;
1381 /* ...and put them in the "stack" backward */
1383 ludp
= LDAP_CALLOC( 1, sizeof(LDAPURLDesc
) );
1385 ldap_charray_free(specs
);
1386 ldap_free_urllist(*ludlist
);
1388 return LDAP_NO_MEMORY
;
1390 ludp
->lud_port
= port
;
1391 ludp
->lud_host
= specs
[i
];
1392 p
= strchr(ludp
->lud_host
, ':');
1394 /* more than one :, IPv6 address */
1395 if ( strchr(p
+1, ':') != NULL
) {
1396 /* allow [address] and [address]:port */
1397 if ( *ludp
->lud_host
== '[' ) {
1398 p
= strchr( ludp
->lud_host
+1, ']' );
1401 ldap_charray_free(specs
);
1402 return LDAP_PARAM_ERROR
;
1404 /* Truncate trailing ']' and shift hostname down 1 char */
1406 AC_MEMCPY( ludp
->lud_host
, ludp
->lud_host
+1, p
- ludp
->lud_host
);
1411 ldap_charray_free(specs
);
1412 return LDAP_PARAM_ERROR
;
1424 ldap_pvt_hex_unescape(p
);
1425 ludp
->lud_port
= strtol( p
, &next
, 10 );
1426 if ( next
== p
|| next
[0] != '\0' ) {
1428 ldap_charray_free(specs
);
1429 return LDAP_PARAM_ERROR
;
1433 ludp
->lud_scheme
= LDAP_STRDUP("ldap");
1434 if ( ludp
->lud_scheme
== NULL
) {
1436 ldap_charray_free(specs
);
1437 return LDAP_NO_MEMORY
;
1440 ldap_pvt_hex_unescape(ludp
->lud_host
);
1441 ludp
->lud_next
= *ludlist
;
1445 /* this should be an array of NULLs now */
1446 ldap_charray_free(specs
);
1447 return LDAP_SUCCESS
;
1451 ldap_url_list2hosts (LDAPURLDesc
*ludlist
)
1455 char *s
, *p
, buf
[32]; /* big enough to hold a long decimal # (overkill) */
1457 if (ludlist
== NULL
)
1460 /* figure out how big the string is */
1461 size
= 1; /* nul-term */
1462 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1463 if ( ludp
->lud_host
== NULL
) continue;
1464 size
+= strlen(ludp
->lud_host
) + 1; /* host and space */
1465 if (strchr(ludp
->lud_host
, ':')) /* will add [ ] below */
1467 if (ludp
->lud_port
!= 0)
1468 size
+= sprintf(buf
, ":%d", ludp
->lud_port
);
1470 s
= LDAP_MALLOC(size
);
1475 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1476 if ( ludp
->lud_host
== NULL
) continue;
1477 if (strchr(ludp
->lud_host
, ':')) {
1478 p
+= sprintf(p
, "[%s]", ludp
->lud_host
);
1480 strcpy(p
, ludp
->lud_host
);
1481 p
+= strlen(ludp
->lud_host
);
1483 if (ludp
->lud_port
!= 0)
1484 p
+= sprintf(p
, ":%d", ludp
->lud_port
);
1488 p
--; /* nuke that extra space */
1495 LDAPURLDesc
*ludlist
)
1501 if ( ludlist
== NULL
) {
1505 /* figure out how big the string is */
1506 for ( size
= 0, ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1507 int len
= desc2str_len( ludp
);
1514 s
= LDAP_MALLOC( size
);
1520 for ( sofar
= 0, ludp
= ludlist
; ludp
!= NULL
; ludp
= ludp
->lud_next
) {
1523 len
= desc2str( ludp
, &s
[sofar
], size
);
1536 assert( size
>= 0 );
1539 s
[sofar
- 1] = '\0';
1545 ldap_free_urllist( LDAPURLDesc
*ludlist
)
1547 LDAPURLDesc
*ludp
, *next
;
1549 for (ludp
= ludlist
; ludp
!= NULL
; ludp
= next
) {
1550 next
= ludp
->lud_next
;
1551 ldap_free_urldesc(ludp
);
1556 ldap_free_urldesc( LDAPURLDesc
*ludp
)
1558 if ( ludp
== NULL
) {
1562 if ( ludp
->lud_scheme
!= NULL
) {
1563 LDAP_FREE( ludp
->lud_scheme
);
1566 if ( ludp
->lud_host
!= NULL
) {
1567 LDAP_FREE( ludp
->lud_host
);
1570 if ( ludp
->lud_dn
!= NULL
) {
1571 LDAP_FREE( ludp
->lud_dn
);
1574 if ( ludp
->lud_filter
!= NULL
) {
1575 LDAP_FREE( ludp
->lud_filter
);
1578 if ( ludp
->lud_attrs
!= NULL
) {
1579 LDAP_VFREE( ludp
->lud_attrs
);
1582 if ( ludp
->lud_exts
!= NULL
) {
1583 LDAP_VFREE( ludp
->lud_exts
);
1590 ldap_int_is_hexpair( char *s
)
1594 for ( i
= 0; i
< 2; i
++ ) {
1595 if ( s
[i
] >= '0' && s
[i
] <= '9' ) {
1599 if ( s
[i
] >= 'A' && s
[i
] <= 'F' ) {
1603 if ( s
[i
] >= 'a' && s
[i
] <= 'f' ) {
1614 ldap_int_unhex( int c
)
1616 return( c
>= '0' && c
<= '9' ? c
- '0'
1617 : c
>= 'A' && c
<= 'F' ? c
- 'A' + 10
1622 ldap_pvt_hex_unescape( char *s
)
1625 * Remove URL hex escapes from s... done in place. The basic concept for
1626 * this routine is borrowed from the WWW library HTUnEscape() routine.
1631 for ( p
= s
; *s
!= '\0'; ++s
) {
1634 * FIXME: what if '%' is followed
1635 * by non-hexpair chars?
1637 if ( !ldap_int_is_hexpair( s
+ 1 ) ) {
1642 if ( *++s
== '\0' ) {
1645 *p
= ldap_int_unhex( *s
) << 4;
1646 if ( *++s
== '\0' ) {
1649 *p
++ += ldap_int_unhex( *s
);