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-2007 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 )
543 if ( u->lud_scheme && !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 char buf[] = ":65535";
587 len += snprintf( buf, sizeof( buf ), ":%d", u->lud_port );
588 if ( u->lud_host && u->lud_host[0] ) {
589 len += strlen( u->lud_host );
593 if ( u->lud_host && u->lud_host[0] ) {
594 len += hex_escape_len( u->lud_host, URLESC_SLASH );
595 if ( !is_ipc && strchr( u->lud_host, ':' )) {
596 len += 2; /* IPv6, [] */
601 len += strlen( u->lud_scheme ) + STRLENOF( "://" );
607 desc2str( LDAPURLDesc *u, char *s, int len )
614 struct berval scope = BER_BVNULL;
624 if ( u->lud_scheme && !strcmp( "ldapi", u->lud_scheme )) {
628 ldap_pvt_scope2bv( u->lud_scope, &scope );
632 } else if ( u->lud_filter ) {
634 } else if ( !BER_BVISEMPTY( &scope ) ) {
636 } else if ( u->lud_attrs ) {
638 } else if ( u->lud_dn && u->lud_dn[0] ) {
642 if ( !is_ipc && u->lud_host && strchr( u->lud_host, ':' )) {
647 len -= sprintf( s, "%s://%s%s%s:%d%n", u->lud_scheme,
649 u->lud_host ? u->lud_host : "",
651 u->lud_port, &sofar );
654 len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar );
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 */
935 if ( ( flags & LDAP_PVT_URL_PARSE_DEF_PORT ) && ludp->lud_port == 0 ) {
936 if ( strcmp( ludp->lud_scheme, "ldaps" ) == 0 ) {
937 ludp->lud_port = LDAPS_PORT;
939 ludp->lud_port = LDAP_PORT;
944 ldap_pvt_hex_unescape( url );
946 /* If [ip address]:port syntax, url is [ip and we skip the [ */
947 ludp->lud_host = LDAP_STRDUP( url + is_v6 );
949 if( ludp->lud_host == NULL ) {
951 ldap_free_urldesc( ludp );
952 return LDAP_URL_ERR_MEM;
955 if ( ( flags & LDAP_PVT_URL_PARSE_NOEMPTY_HOST )
956 && ludp->lud_host != NULL
957 && *ludp->lud_host == '\0' )
959 LDAP_FREE( ludp->lud_host );
960 ludp->lud_host = NULL;
964 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
966 * On early Novell releases, search references/referrals were returned
967 * in this format, i.e., the dn was kind of in the scope position,
968 * but the required slash is missing. The whole thing is illegal syntax,
969 * but we need to account for it. Fortunately it can't be confused with
972 if( (p == NULL) && (q != NULL) && (*q == '?') ) {
973 /* ? immediately followed by question */
977 ldap_pvt_hex_unescape( q );
978 ludp->lud_dn = LDAP_STRDUP( q );
980 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
981 ludp->lud_dn = LDAP_STRDUP( "" );
987 if ( check_dn && ludp->lud_dn == NULL ) {
989 ldap_free_urldesc( ludp );
990 return LDAP_URL_ERR_MEM;
997 return LDAP_URL_SUCCESS;
1000 /* scan forward for '?' that may marks end of dn */
1001 q = strchr( p, '?' );
1004 /* terminate dn part */
1010 ldap_pvt_hex_unescape( p );
1011 ludp->lud_dn = LDAP_STRDUP( p );
1013 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
1014 ludp->lud_dn = LDAP_STRDUP( "" );
1020 if( check_dn && ludp->lud_dn == NULL ) {
1022 ldap_free_urldesc( ludp );
1023 return LDAP_URL_ERR_MEM;
1030 return LDAP_URL_SUCCESS;
1033 /* scan forward for '?' that may marks end of attributes */
1035 q = strchr( p, '?' );
1038 /* terminate attributes part */
1043 /* parse attributes */
1044 ldap_pvt_hex_unescape( p );
1045 ludp->lud_attrs = ldap_str2charray( p, "," );
1047 if( ludp->lud_attrs == NULL ) {
1049 ldap_free_urldesc( ludp );
1050 return LDAP_URL_ERR_BADATTRS;
1058 return LDAP_URL_SUCCESS;
1061 /* scan forward for '?' that may marks end of scope */
1063 q = strchr( p, '?' );
1066 /* terminate the scope part */
1071 /* parse the scope */
1072 ldap_pvt_hex_unescape( p );
1073 ludp->lud_scope = ldap_pvt_str2scope( p );
1075 if( ludp->lud_scope == -1 ) {
1077 ldap_free_urldesc( ludp );
1078 return LDAP_URL_ERR_BADSCOPE;
1086 return LDAP_URL_SUCCESS;
1089 /* scan forward for '?' that may marks end of filter */
1091 q = strchr( p, '?' );
1094 /* terminate the filter part */
1099 /* parse the filter */
1100 ldap_pvt_hex_unescape( p );
1103 /* missing filter */
1105 ldap_free_urldesc( ludp );
1106 return LDAP_URL_ERR_BADFILTER;
1109 ludp->lud_filter = LDAP_STRDUP( p );
1111 if( ludp->lud_filter == NULL ) {
1113 ldap_free_urldesc( ludp );
1114 return LDAP_URL_ERR_MEM;
1122 return LDAP_URL_SUCCESS;
1125 /* scan forward for '?' that may marks end of extensions */
1127 q = strchr( p, '?' );
1132 ldap_free_urldesc( ludp );
1133 return LDAP_URL_ERR_BADURL;
1136 /* parse the extensions */
1137 ludp->lud_exts = ldap_str2charray( p, "," );
1139 if( ludp->lud_exts == NULL ) {
1141 ldap_free_urldesc( ludp );
1142 return LDAP_URL_ERR_BADEXTS;
1145 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
1146 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
1148 if( *ludp->lud_exts[i] == '!' ) {
1149 /* count the number of critical extensions */
1150 ludp->lud_crit_exts++;
1155 /* must have 1 or more */
1157 ldap_free_urldesc( ludp );
1158 return LDAP_URL_ERR_BADEXTS;
1164 return LDAP_URL_SUCCESS;
1168 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
1170 return ldap_url_parse_ext( url_in, ludpp, LDAP_PVT_URL_PARSE_HISTORIC );
1174 ldap_url_dup ( LDAPURLDesc *ludp )
1178 if ( ludp == NULL ) {
1182 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
1187 dest->lud_scheme = NULL;
1188 dest->lud_host = NULL;
1189 dest->lud_dn = NULL;
1190 dest->lud_filter = NULL;
1191 dest->lud_attrs = NULL;
1192 dest->lud_exts = NULL;
1193 dest->lud_next = NULL;
1195 if ( ludp->lud_scheme != NULL ) {
1196 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
1197 if (dest->lud_scheme == NULL) {
1198 ldap_free_urldesc(dest);
1203 if ( ludp->lud_host != NULL ) {
1204 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
1205 if (dest->lud_host == NULL) {
1206 ldap_free_urldesc(dest);
1211 if ( ludp->lud_dn != NULL ) {
1212 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
1213 if (dest->lud_dn == NULL) {
1214 ldap_free_urldesc(dest);
1219 if ( ludp->lud_filter != NULL ) {
1220 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
1221 if (dest->lud_filter == NULL) {
1222 ldap_free_urldesc(dest);
1227 if ( ludp->lud_attrs != NULL ) {
1228 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
1229 if (dest->lud_attrs == NULL) {
1230 ldap_free_urldesc(dest);
1235 if ( ludp->lud_exts != NULL ) {
1236 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
1237 if (dest->lud_exts == NULL) {
1238 ldap_free_urldesc(dest);
1247 ldap_url_duplist (LDAPURLDesc *ludlist)
1249 LDAPURLDesc *dest, *tail, *ludp, *newludp;
1253 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1254 newludp = ldap_url_dup(ludp);
1255 if (newludp == NULL) {
1256 ldap_free_urllist(dest);
1262 tail->lud_next = newludp;
1269 ldap_url_parselist_int (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1276 assert( ludlist != NULL );
1277 assert( url != NULL );
1281 if ( sep == NULL ) {
1285 urls = ldap_str2charray( url, sep );
1287 return LDAP_URL_ERR_MEM;
1289 /* count the URLs... */
1290 for (i = 0; urls[i] != NULL; i++) ;
1291 /* ...and put them in the "stack" backward */
1293 rc = ldap_url_parse_ext( urls[i], &ludp, flags );
1295 ldap_charray_free( urls );
1296 ldap_free_urllist( *ludlist );
1300 ludp->lud_next = *ludlist;
1303 ldap_charray_free( urls );
1304 return LDAP_URL_SUCCESS;
1308 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
1310 return ldap_url_parselist_int( ludlist, url, ", ", LDAP_PVT_URL_PARSE_HISTORIC );
1314 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1316 return ldap_url_parselist_int( ludlist, url, sep, flags );
1320 ldap_url_parsehosts(
1321 LDAPURLDesc **ludlist,
1329 assert( ludlist != NULL );
1330 assert( hosts != NULL );
1334 specs = ldap_str2charray(hosts, ", ");
1336 return LDAP_NO_MEMORY;
1338 /* count the URLs... */
1339 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
1341 /* ...and put them in the "stack" backward */
1343 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
1345 ldap_charray_free(specs);
1346 ldap_free_urllist(*ludlist);
1348 return LDAP_NO_MEMORY;
1350 ludp->lud_port = port;
1351 ludp->lud_host = specs[i];
1353 p = strchr(ludp->lud_host, ':');
1355 /* more than one :, IPv6 address */
1356 if ( strchr(p+1, ':') != NULL ) {
1357 /* allow [address] and [address]:port */
1358 if ( *ludp->lud_host == '[' ) {
1359 p = LDAP_STRDUP(ludp->lud_host+1);
1360 /* copied, make sure we free source later */
1361 specs[i] = ludp->lud_host;
1363 p = strchr( ludp->lud_host, ']' );
1366 ldap_charray_free(specs);
1367 return LDAP_PARAM_ERROR;
1373 ldap_charray_free(specs);
1374 return LDAP_PARAM_ERROR;
1386 ldap_pvt_hex_unescape(p);
1387 ludp->lud_port = strtol( p, &next, 10 );
1388 if ( next == p || next[0] != '\0' ) {
1390 ldap_charray_free(specs);
1391 return LDAP_PARAM_ERROR;
1395 ldap_pvt_hex_unescape(ludp->lud_host);
1396 ludp->lud_scheme = LDAP_STRDUP("ldap");
1397 ludp->lud_next = *ludlist;
1401 /* this should be an array of NULLs now */
1402 /* except entries starting with [ */
1403 ldap_charray_free(specs);
1404 return LDAP_SUCCESS;
1408 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1412 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
1414 if (ludlist == NULL)
1417 /* figure out how big the string is */
1418 size = 1; /* nul-term */
1419 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1420 size += strlen(ludp->lud_host) + 1; /* host and space */
1421 if (strchr(ludp->lud_host, ':')) /* will add [ ] below */
1423 if (ludp->lud_port != 0)
1424 size += sprintf(buf, ":%d", ludp->lud_port);
1426 s = LDAP_MALLOC(size);
1431 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1432 if (strchr(ludp->lud_host, ':')) {
1433 p += sprintf(p, "[%s]", ludp->lud_host);
1435 strcpy(p, ludp->lud_host);
1436 p += strlen(ludp->lud_host);
1438 if (ludp->lud_port != 0)
1439 p += sprintf(p, ":%d", ludp->lud_port);
1443 p--; /* nuke that extra space */
1450 LDAPURLDesc *ludlist )
1456 if ( ludlist == NULL ) {
1460 /* figure out how big the string is */
1461 for ( size = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1462 int len = desc2str_len( ludp );
1469 s = LDAP_MALLOC( size );
1475 for ( sofar = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1478 len = desc2str( ludp, &s[sofar], size );
1491 assert( size >= 0 );
1494 s[sofar - 1] = '\0';
1500 ldap_free_urllist( LDAPURLDesc *ludlist )
1502 LDAPURLDesc *ludp, *next;
1504 for (ludp = ludlist; ludp != NULL; ludp = next) {
1505 next = ludp->lud_next;
1506 ldap_free_urldesc(ludp);
1511 ldap_free_urldesc( LDAPURLDesc *ludp )
1513 if ( ludp == NULL ) {
1517 if ( ludp->lud_scheme != NULL ) {
1518 LDAP_FREE( ludp->lud_scheme );
1521 if ( ludp->lud_host != NULL ) {
1522 LDAP_FREE( ludp->lud_host );
1525 if ( ludp->lud_dn != NULL ) {
1526 LDAP_FREE( ludp->lud_dn );
1529 if ( ludp->lud_filter != NULL ) {
1530 LDAP_FREE( ludp->lud_filter);
1533 if ( ludp->lud_attrs != NULL ) {
1534 LDAP_VFREE( ludp->lud_attrs );
1537 if ( ludp->lud_exts != NULL ) {
1538 LDAP_VFREE( ludp->lud_exts );
1545 ldap_int_is_hexpair( char *s )
1549 for ( i = 0; i < 2; i++ ) {
1550 if ( s[i] >= '0' && s[i] <= '9' ) {
1554 if ( s[i] >= 'A' && s[i] <= 'F' ) {
1558 if ( s[i] >= 'a' && s[i] <= 'f' ) {
1569 ldap_int_unhex( int c )
1571 return( c >= '0' && c <= '9' ? c - '0'
1572 : c >= 'A' && c <= 'F' ? c - 'A' + 10
1577 ldap_pvt_hex_unescape( char *s )
1580 * Remove URL hex escapes from s... done in place. The basic concept for
1581 * this routine is borrowed from the WWW library HTUnEscape() routine.
1586 for ( p = s; *s != '\0'; ++s ) {
1589 * FIXME: what if '%' is followed
1590 * by non-hexpair chars?
1592 if ( !ldap_int_is_hexpair( s + 1 ) ) {
1597 if ( *++s == '\0' ) {
1600 *p = ldap_int_unhex( *s ) << 4;
1601 if ( *++s == '\0' ) {
1604 *p++ += ldap_int_unhex( *s );