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 )
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 len -= sprintf( s, "%s://%s%s%s:%d%n", u->lud_scheme,
647 u->lud_host ? u->lud_host : "",
649 u->lud_port, &sofar );
652 len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar );
653 if ( u->lud_host && u->lud_host[0] ) {
658 i = hex_escape( &s[sofar], len, u->lud_host, URLESC_SLASH );
679 if ( u->lud_dn && u->lud_dn[0] ) {
680 i = hex_escape( &s[sofar], len, u->lud_dn, URLESC_NONE );
695 i = hex_escape_list( &s[sofar], len, u->lud_attrs, URLESC_NONE );
709 if ( !BER_BVISNULL( &scope ) ) {
710 strcpy( &s[sofar], scope.bv_val );
711 sofar += scope.bv_len;
725 i = hex_escape( &s[sofar], len, u->lud_filter, URLESC_NONE );
739 i = hex_escape_list( &s[sofar], len, u->lud_exts, URLESC_COMMA );
754 ldap_url_desc2str( LDAPURLDesc *u )
763 len = desc2str_len( u );
768 /* allocate enough to hex escape everything -- overkill */
769 s = LDAP_MALLOC( len + 1 );
775 if ( desc2str( u, s, len ) != len ) {
786 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp, unsigned flags )
789 * Pick apart the pieces of an LDAP URL.
794 int i, enclosed, proto, is_v6 = 0;
795 const char *scheme = NULL;
801 if( url_in == NULL || ludpp == NULL ) {
802 return LDAP_URL_ERR_PARAM;
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
811 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
814 *ludpp = NULL; /* pessimistic */
816 url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
818 if ( url_tmp == NULL ) {
819 return LDAP_URL_ERR_BADSCHEME;
822 assert( scheme != NULL );
824 proto = ldap_pvt_url_scheme2proto( scheme );
826 return LDAP_URL_ERR_BADSCHEME;
829 /* make working copy of the remainder of the URL */
830 url = LDAP_STRDUP( url_tmp );
832 return LDAP_URL_ERR_MEM;
836 p = &url[strlen(url)-1];
840 return LDAP_URL_ERR_BADENCLOSURE;
846 /* allocate return struct */
847 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
849 if ( ludp == NULL ) {
851 return LDAP_URL_ERR_MEM;
854 ludp->lud_next = NULL;
855 ludp->lud_host = 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;
863 ludp->lud_scheme = LDAP_STRDUP( scheme );
865 if ( ludp->lud_scheme == NULL ) {
867 ldap_free_urldesc( ludp );
868 return LDAP_URL_ERR_MEM;
871 /* scan forward for '/' that marks end of hostport and begin. of dn */
872 p = strchr( url, '/' );
876 /* terminate hostport; point to start of dn */
879 /* check for Novell kludge, see below */
880 p = strchr( url, '?' );
888 if ( proto != LDAP_PROTO_IPC ) {
889 /* IPv6 syntax with [ip address]:port */
891 r = strchr( url, ']' );
894 ldap_free_urldesc( ludp );
895 return LDAP_URL_ERR_BADURL;
898 q = strchr( r, ':' );
901 ldap_free_urldesc( ludp );
902 return LDAP_URL_ERR_BADURL;
906 q = strchr( url, ':' );
913 ldap_pvt_hex_unescape( q );
917 ldap_free_urldesc( ludp );
918 return LDAP_URL_ERR_BADURL;
921 ludp->lud_port = strtol( q, &next, 10 );
922 if ( next == q || next[0] != '\0' ) {
924 ldap_free_urldesc( ludp );
925 return LDAP_URL_ERR_BADURL;
927 /* check for Novell kludge */
929 if ( *next != '\0' ) {
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;
941 ludp->lud_port = LDAP_PORT;
946 ldap_pvt_hex_unescape( url );
948 /* If [ip address]:port syntax, url is [ip and we skip the [ */
949 ludp->lud_host = LDAP_STRDUP( url + is_v6 );
951 if( ludp->lud_host == NULL ) {
953 ldap_free_urldesc( ludp );
954 return LDAP_URL_ERR_MEM;
957 if ( ( flags & LDAP_PVT_URL_PARSE_NOEMPTY_HOST )
958 && ludp->lud_host != NULL
959 && *ludp->lud_host == '\0' )
961 LDAP_FREE( ludp->lud_host );
962 ludp->lud_host = NULL;
966 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
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
974 if( (p == NULL) && (q != NULL) && (*q == '?') ) {
975 /* ? immediately followed by question */
979 ldap_pvt_hex_unescape( q );
980 ludp->lud_dn = LDAP_STRDUP( q );
982 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
983 ludp->lud_dn = LDAP_STRDUP( "" );
989 if ( check_dn && ludp->lud_dn == NULL ) {
991 ldap_free_urldesc( ludp );
992 return LDAP_URL_ERR_MEM;
999 return LDAP_URL_SUCCESS;
1002 /* scan forward for '?' that may marks end of dn */
1003 q = strchr( p, '?' );
1006 /* terminate dn part */
1012 ldap_pvt_hex_unescape( p );
1013 ludp->lud_dn = LDAP_STRDUP( p );
1015 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
1016 ludp->lud_dn = LDAP_STRDUP( "" );
1022 if( check_dn && ludp->lud_dn == NULL ) {
1024 ldap_free_urldesc( ludp );
1025 return LDAP_URL_ERR_MEM;
1032 return LDAP_URL_SUCCESS;
1035 /* scan forward for '?' that may marks end of attributes */
1037 q = strchr( p, '?' );
1040 /* terminate attributes part */
1045 /* parse attributes */
1046 ldap_pvt_hex_unescape( p );
1047 ludp->lud_attrs = ldap_str2charray( p, "," );
1049 if( ludp->lud_attrs == NULL ) {
1051 ldap_free_urldesc( ludp );
1052 return LDAP_URL_ERR_BADATTRS;
1060 return LDAP_URL_SUCCESS;
1063 /* scan forward for '?' that may marks end of scope */
1065 q = strchr( p, '?' );
1068 /* terminate the scope part */
1073 /* parse the scope */
1074 ldap_pvt_hex_unescape( p );
1075 ludp->lud_scope = ldap_pvt_str2scope( p );
1077 if( ludp->lud_scope == -1 ) {
1079 ldap_free_urldesc( ludp );
1080 return LDAP_URL_ERR_BADSCOPE;
1088 return LDAP_URL_SUCCESS;
1091 /* scan forward for '?' that may marks end of filter */
1093 q = strchr( p, '?' );
1096 /* terminate the filter part */
1101 /* parse the filter */
1102 ldap_pvt_hex_unescape( p );
1105 /* missing filter */
1107 ldap_free_urldesc( ludp );
1108 return LDAP_URL_ERR_BADFILTER;
1111 ludp->lud_filter = LDAP_STRDUP( p );
1113 if( ludp->lud_filter == NULL ) {
1115 ldap_free_urldesc( ludp );
1116 return LDAP_URL_ERR_MEM;
1124 return LDAP_URL_SUCCESS;
1127 /* scan forward for '?' that may marks end of extensions */
1129 q = strchr( p, '?' );
1134 ldap_free_urldesc( ludp );
1135 return LDAP_URL_ERR_BADURL;
1138 /* parse the extensions */
1139 ludp->lud_exts = ldap_str2charray( p, "," );
1141 if( ludp->lud_exts == NULL ) {
1143 ldap_free_urldesc( ludp );
1144 return LDAP_URL_ERR_BADEXTS;
1147 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
1148 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
1150 if( *ludp->lud_exts[i] == '!' ) {
1151 /* count the number of critical extensions */
1152 ludp->lud_crit_exts++;
1157 /* must have 1 or more */
1159 ldap_free_urldesc( ludp );
1160 return LDAP_URL_ERR_BADEXTS;
1166 return LDAP_URL_SUCCESS;
1170 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
1172 return ldap_url_parse_ext( url_in, ludpp, LDAP_PVT_URL_PARSE_HISTORIC );
1176 ldap_url_dup ( LDAPURLDesc *ludp )
1180 if ( ludp == NULL ) {
1184 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
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;
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);
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);
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);
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);
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);
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);
1249 ldap_url_duplist (LDAPURLDesc *ludlist)
1251 LDAPURLDesc *dest, *tail, *ludp, *newludp;
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);
1264 tail->lud_next = newludp;
1271 ldap_url_parselist_int (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1278 assert( ludlist != NULL );
1279 assert( url != NULL );
1283 if ( sep == NULL ) {
1287 urls = ldap_str2charray( url, sep );
1289 return LDAP_URL_ERR_MEM;
1291 /* count the URLs... */
1292 for (i = 0; urls[i] != NULL; i++) ;
1293 /* ...and put them in the "stack" backward */
1295 rc = ldap_url_parse_ext( urls[i], &ludp, flags );
1297 ldap_charray_free( urls );
1298 ldap_free_urllist( *ludlist );
1302 ludp->lud_next = *ludlist;
1305 ldap_charray_free( urls );
1306 return LDAP_URL_SUCCESS;
1310 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
1312 return ldap_url_parselist_int( ludlist, url, ", ", LDAP_PVT_URL_PARSE_HISTORIC );
1316 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1318 return ldap_url_parselist_int( ludlist, url, sep, flags );
1322 ldap_url_parsehosts(
1323 LDAPURLDesc **ludlist,
1331 assert( ludlist != NULL );
1332 assert( hosts != NULL );
1336 specs = ldap_str2charray(hosts, ", ");
1338 return LDAP_NO_MEMORY;
1340 /* count the URLs... */
1341 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
1343 /* ...and put them in the "stack" backward */
1345 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
1347 ldap_charray_free(specs);
1348 ldap_free_urllist(*ludlist);
1350 return LDAP_NO_MEMORY;
1352 ludp->lud_port = port;
1353 ludp->lud_host = specs[i];
1355 p = strchr(ludp->lud_host, ':');
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;
1365 p = strchr( ludp->lud_host, ']' );
1368 ldap_charray_free(specs);
1369 return LDAP_PARAM_ERROR;
1375 ldap_charray_free(specs);
1376 return LDAP_PARAM_ERROR;
1388 ldap_pvt_hex_unescape(p);
1389 ludp->lud_port = strtol( p, &next, 10 );
1390 if ( next == p || next[0] != '\0' ) {
1392 ldap_charray_free(specs);
1393 return LDAP_PARAM_ERROR;
1397 ldap_pvt_hex_unescape(ludp->lud_host);
1398 ludp->lud_scheme = LDAP_STRDUP("ldap");
1399 ludp->lud_next = *ludlist;
1403 /* this should be an array of NULLs now */
1404 /* except entries starting with [ */
1405 ldap_charray_free(specs);
1406 return LDAP_SUCCESS;
1410 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1414 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
1416 if (ludlist == NULL)
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 */
1425 if (ludp->lud_port != 0)
1426 size += sprintf(buf, ":%d", ludp->lud_port);
1428 s = LDAP_MALLOC(size);
1433 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1434 if (strchr(ludp->lud_host, ':')) {
1435 p += sprintf(p, "[%s]", ludp->lud_host);
1437 strcpy(p, ludp->lud_host);
1438 p += strlen(ludp->lud_host);
1440 if (ludp->lud_port != 0)
1441 p += sprintf(p, ":%d", ludp->lud_port);
1445 p--; /* nuke that extra space */
1452 LDAPURLDesc *ludlist )
1458 if ( ludlist == NULL ) {
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 );
1471 s = LDAP_MALLOC( size );
1477 for ( sofar = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1480 len = desc2str( ludp, &s[sofar], size );
1493 assert( size >= 0 );
1496 s[sofar - 1] = '\0';
1502 ldap_free_urllist( LDAPURLDesc *ludlist )
1504 LDAPURLDesc *ludp, *next;
1506 for (ludp = ludlist; ludp != NULL; ludp = next) {
1507 next = ludp->lud_next;
1508 ldap_free_urldesc(ludp);
1513 ldap_free_urldesc( LDAPURLDesc *ludp )
1515 if ( ludp == NULL ) {
1519 if ( ludp->lud_scheme != NULL ) {
1520 LDAP_FREE( ludp->lud_scheme );
1523 if ( ludp->lud_host != NULL ) {
1524 LDAP_FREE( ludp->lud_host );
1527 if ( ludp->lud_dn != NULL ) {
1528 LDAP_FREE( ludp->lud_dn );
1531 if ( ludp->lud_filter != NULL ) {
1532 LDAP_FREE( ludp->lud_filter);
1535 if ( ludp->lud_attrs != NULL ) {
1536 LDAP_VFREE( ludp->lud_attrs );
1539 if ( ludp->lud_exts != NULL ) {
1540 LDAP_VFREE( ludp->lud_exts );
1547 ldap_int_is_hexpair( char *s )
1551 for ( i = 0; i < 2; i++ ) {
1552 if ( s[i] >= '0' && s[i] <= '9' ) {
1556 if ( s[i] >= 'A' && s[i] <= 'F' ) {
1560 if ( s[i] >= 'a' && s[i] <= 'f' ) {
1571 ldap_int_unhex( int c )
1573 return( c >= '0' && c <= '9' ? c - '0'
1574 : c >= 'A' && c <= 'F' ? c - 'A' + 10
1579 ldap_pvt_hex_unescape( char *s )
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.
1588 for ( p = s; *s != '\0'; ++s ) {
1591 * FIXME: what if '%' is followed
1592 * by non-hexpair chars?
1594 if ( !ldap_int_is_hexpair( s + 1 ) ) {
1599 if ( *++s == '\0' ) {
1602 *p = ldap_int_unhex( *s ) << 4;
1603 if ( *++s == '\0' ) {
1606 *p++ += ldap_int_unhex( *s );