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-2006 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 len += hex_escape_len_list( u->lud_exts, URLESC_COMMA );
549 if ( u->lud_filter ) {
550 len += hex_escape_len( u->lud_filter, URLESC_NONE );
556 if ( ldap_pvt_scope2bv( u->lud_scope, &scope ) == LDAP_SUCCESS ) {
563 if ( u->lud_attrs ) {
564 len += hex_escape_len_list( u->lud_attrs, URLESC_NONE );
570 if ( u->lud_dn && u->lud_dn[0] ) {
571 len += hex_escape_len( u->lud_dn, URLESC_NONE );
580 char buf[] = ":65535";
582 len += snprintf( buf, sizeof( buf ), ":%d", u->lud_port );
583 if ( u->lud_host && u->lud_host[0] ) {
584 len += strlen( u->lud_host );
588 if ( u->lud_host && u->lud_host[0] ) {
589 len += hex_escape_len( u->lud_host, URLESC_SLASH );
593 len += strlen( u->lud_scheme ) + STRLENOF( "://" );
599 desc2str( LDAPURLDesc *u, char *s, int len )
604 struct berval scope = BER_BVNULL;
614 ldap_pvt_scope2bv( u->lud_scope, &scope );
618 } else if ( u->lud_filter ) {
620 } else if ( !BER_BVISEMPTY( &scope ) ) {
622 } else if ( u->lud_attrs ) {
624 } else if ( u->lud_dn && u->lud_dn[0] ) {
629 len -= sprintf( s, "%s://%s:%d%n", u->lud_scheme,
630 u->lud_host ? u->lud_host : "",
631 u->lud_port, &sofar );
634 len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar );
635 if ( u->lud_host && u->lud_host[0] ) {
636 i = hex_escape( &s[sofar], len, u->lud_host, URLESC_SLASH );
653 if ( u->lud_dn && u->lud_dn[0] ) {
654 i = hex_escape( &s[sofar], len, u->lud_dn, URLESC_NONE );
669 i = hex_escape_list( &s[sofar], len, u->lud_attrs, URLESC_NONE );
683 if ( !BER_BVISNULL( &scope ) ) {
684 strcpy( &s[sofar], scope.bv_val );
685 sofar += scope.bv_len;
699 i = hex_escape( &s[sofar], len, u->lud_filter, URLESC_NONE );
713 i = hex_escape_list( &s[sofar], len, u->lud_exts, URLESC_COMMA );
728 ldap_url_desc2str( LDAPURLDesc *u )
737 len = desc2str_len( u );
742 /* allocate enough to hex escape everything -- overkill */
743 s = LDAP_MALLOC( len + 1 );
749 if ( desc2str( u, s, len ) != len ) {
760 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp, unsigned flags )
763 * Pick apart the pieces of an LDAP URL.
769 const char *scheme = NULL;
775 if( url_in == NULL || ludpp == NULL ) {
776 return LDAP_URL_ERR_PARAM;
779 #ifndef LDAP_INT_IN_KERNEL
780 /* Global options may not be created yet
781 * We can't test if the global options are initialized
782 * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
783 * the options and cause infinite recursion
785 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
788 *ludpp = NULL; /* pessimistic */
790 url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
792 if ( url_tmp == NULL ) {
793 return LDAP_URL_ERR_BADSCHEME;
796 assert( scheme != NULL );
798 /* make working copy of the remainder of the URL */
799 url = LDAP_STRDUP( url_tmp );
801 return LDAP_URL_ERR_MEM;
805 p = &url[strlen(url)-1];
809 return LDAP_URL_ERR_BADENCLOSURE;
815 /* allocate return struct */
816 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
818 if ( ludp == NULL ) {
820 return LDAP_URL_ERR_MEM;
823 ludp->lud_next = NULL;
824 ludp->lud_host = NULL;
827 ludp->lud_attrs = NULL;
828 ludp->lud_scope = ( flags & LDAP_PVT_URL_PARSE_NODEF_SCOPE ) ? LDAP_SCOPE_BASE : LDAP_SCOPE_DEFAULT;
829 ludp->lud_filter = NULL;
830 ludp->lud_exts = NULL;
832 ludp->lud_scheme = LDAP_STRDUP( scheme );
834 if ( ludp->lud_scheme == NULL ) {
836 ldap_free_urldesc( ludp );
837 return LDAP_URL_ERR_MEM;
840 /* scan forward for '/' that marks end of hostport and begin. of dn */
841 p = strchr( url, '/' );
844 /* terminate hostport; point to start of dn */
848 /* IPv6 syntax with [ip address]:port */
850 r = strchr( url, ']' );
853 ldap_free_urldesc( ludp );
854 return LDAP_URL_ERR_BADURL;
857 q = strchr( r, ':' );
859 q = strchr( url, ':' );
866 ldap_pvt_hex_unescape( q );
870 ldap_free_urldesc( ludp );
871 return LDAP_URL_ERR_BADURL;
874 ludp->lud_port = strtol( q, &next, 10 );
875 if ( next == q || next[0] != '\0' ) {
877 ldap_free_urldesc( ludp );
878 return LDAP_URL_ERR_BADURL;
882 if ( ( flags & LDAP_PVT_URL_PARSE_DEF_PORT ) && ludp->lud_port == 0 ) {
883 if ( strcmp( ludp->lud_scheme, "ldap" ) == 0 ) {
884 ludp->lud_port = LDAP_PORT;
885 #ifdef LDAP_CONNECTIONLESS
886 } else if ( strcmp( ludp->lud_scheme, "cldap" ) == 0 ) {
887 ludp->lud_port = LDAP_PORT;
889 } else if ( strcmp( ludp->lud_scheme, "ldaps" ) == 0 ) {
890 ludp->lud_port = LDAPS_PORT;
894 ldap_pvt_hex_unescape( url );
896 /* If [ip address]:port syntax, url is [ip and we skip the [ */
897 ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
899 if( ludp->lud_host == NULL ) {
901 ldap_free_urldesc( ludp );
902 return LDAP_URL_ERR_MEM;
905 if ( ( flags & LDAP_PVT_URL_PARSE_NOEMPTY_HOST )
906 && ludp->lud_host != NULL
907 && *ludp->lud_host == '\0' )
909 LDAP_FREE( ludp->lud_host );
910 ludp->lud_host = NULL;
914 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
916 * On early Novell releases, search references/referrals were returned
917 * in this format, i.e., the dn was kind of in the scope position,
918 * but the required slash is missing. The whole thing is illegal syntax,
919 * but we need to account for it. Fortunately it can't be confused with
922 if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
924 /* ? immediately followed by question */
929 ldap_pvt_hex_unescape( q );
930 ludp->lud_dn = LDAP_STRDUP( q );
932 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
933 ludp->lud_dn = LDAP_STRDUP( "" );
939 if ( check_dn && ludp->lud_dn == NULL ) {
941 ldap_free_urldesc( ludp );
942 return LDAP_URL_ERR_MEM;
950 return LDAP_URL_SUCCESS;
953 /* scan forward for '?' that may marks end of dn */
954 q = strchr( p, '?' );
957 /* terminate dn part */
963 ldap_pvt_hex_unescape( p );
964 ludp->lud_dn = LDAP_STRDUP( p );
966 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
967 ludp->lud_dn = LDAP_STRDUP( "" );
973 if( check_dn && ludp->lud_dn == NULL ) {
975 ldap_free_urldesc( ludp );
976 return LDAP_URL_ERR_MEM;
983 return LDAP_URL_SUCCESS;
986 /* scan forward for '?' that may marks end of attributes */
988 q = strchr( p, '?' );
991 /* terminate attributes part */
996 /* parse attributes */
997 ldap_pvt_hex_unescape( p );
998 ludp->lud_attrs = ldap_str2charray( p, "," );
1000 if( ludp->lud_attrs == NULL ) {
1002 ldap_free_urldesc( ludp );
1003 return LDAP_URL_ERR_BADATTRS;
1011 return LDAP_URL_SUCCESS;
1014 /* scan forward for '?' that may marks end of scope */
1016 q = strchr( p, '?' );
1019 /* terminate the scope part */
1024 /* parse the scope */
1025 ldap_pvt_hex_unescape( p );
1026 ludp->lud_scope = ldap_pvt_str2scope( p );
1028 if( ludp->lud_scope == -1 ) {
1030 ldap_free_urldesc( ludp );
1031 return LDAP_URL_ERR_BADSCOPE;
1039 return LDAP_URL_SUCCESS;
1042 /* scan forward for '?' that may marks end of filter */
1044 q = strchr( p, '?' );
1047 /* terminate the filter part */
1052 /* parse the filter */
1053 ldap_pvt_hex_unescape( p );
1056 /* missing filter */
1058 ldap_free_urldesc( ludp );
1059 return LDAP_URL_ERR_BADFILTER;
1062 ludp->lud_filter = LDAP_STRDUP( p );
1064 if( ludp->lud_filter == NULL ) {
1066 ldap_free_urldesc( ludp );
1067 return LDAP_URL_ERR_MEM;
1075 return LDAP_URL_SUCCESS;
1078 /* scan forward for '?' that may marks end of extensions */
1080 q = strchr( p, '?' );
1085 ldap_free_urldesc( ludp );
1086 return LDAP_URL_ERR_BADURL;
1089 /* parse the extensions */
1090 ludp->lud_exts = ldap_str2charray( p, "," );
1092 if( ludp->lud_exts == NULL ) {
1094 ldap_free_urldesc( ludp );
1095 return LDAP_URL_ERR_BADEXTS;
1098 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
1099 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
1101 if( *ludp->lud_exts[i] == '!' ) {
1102 /* count the number of critical extensions */
1103 ludp->lud_crit_exts++;
1108 /* must have 1 or more */
1110 ldap_free_urldesc( ludp );
1111 return LDAP_URL_ERR_BADEXTS;
1117 return LDAP_URL_SUCCESS;
1121 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
1123 return ldap_url_parse_ext( url_in, ludpp, LDAP_PVT_URL_PARSE_HISTORIC );
1127 ldap_url_dup ( LDAPURLDesc *ludp )
1131 if ( ludp == NULL ) {
1135 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
1140 dest->lud_scheme = NULL;
1141 dest->lud_host = NULL;
1142 dest->lud_dn = NULL;
1143 dest->lud_filter = NULL;
1144 dest->lud_attrs = NULL;
1145 dest->lud_exts = NULL;
1146 dest->lud_next = NULL;
1148 if ( ludp->lud_scheme != NULL ) {
1149 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
1150 if (dest->lud_scheme == NULL) {
1151 ldap_free_urldesc(dest);
1156 if ( ludp->lud_host != NULL ) {
1157 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
1158 if (dest->lud_host == NULL) {
1159 ldap_free_urldesc(dest);
1164 if ( ludp->lud_dn != NULL ) {
1165 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
1166 if (dest->lud_dn == NULL) {
1167 ldap_free_urldesc(dest);
1172 if ( ludp->lud_filter != NULL ) {
1173 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
1174 if (dest->lud_filter == NULL) {
1175 ldap_free_urldesc(dest);
1180 if ( ludp->lud_attrs != NULL ) {
1181 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
1182 if (dest->lud_attrs == NULL) {
1183 ldap_free_urldesc(dest);
1188 if ( ludp->lud_exts != NULL ) {
1189 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
1190 if (dest->lud_exts == NULL) {
1191 ldap_free_urldesc(dest);
1200 ldap_url_duplist (LDAPURLDesc *ludlist)
1202 LDAPURLDesc *dest, *tail, *ludp, *newludp;
1206 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1207 newludp = ldap_url_dup(ludp);
1208 if (newludp == NULL) {
1209 ldap_free_urllist(dest);
1215 tail->lud_next = newludp;
1222 ldap_url_parselist_int (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1229 assert( ludlist != NULL );
1230 assert( url != NULL );
1234 if ( sep == NULL ) {
1238 urls = ldap_str2charray( url, sep );
1240 return LDAP_URL_ERR_MEM;
1242 /* count the URLs... */
1243 for (i = 0; urls[i] != NULL; i++) ;
1244 /* ...and put them in the "stack" backward */
1246 rc = ldap_url_parse_ext( urls[i], &ludp, flags );
1248 ldap_charray_free( urls );
1249 ldap_free_urllist( *ludlist );
1253 ludp->lud_next = *ludlist;
1256 ldap_charray_free( urls );
1257 return LDAP_URL_SUCCESS;
1261 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
1263 return ldap_url_parselist_int( ludlist, url, ", ", LDAP_PVT_URL_PARSE_HISTORIC );
1267 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1269 return ldap_url_parselist_int( ludlist, url, sep, flags );
1273 ldap_url_parsehosts(
1274 LDAPURLDesc **ludlist,
1282 assert( ludlist != NULL );
1283 assert( hosts != NULL );
1287 specs = ldap_str2charray(hosts, ", ");
1289 return LDAP_NO_MEMORY;
1291 /* count the URLs... */
1292 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
1294 /* ...and put them in the "stack" backward */
1296 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
1298 ldap_charray_free(specs);
1299 ldap_free_urllist(*ludlist);
1301 return LDAP_NO_MEMORY;
1303 ludp->lud_port = port;
1304 ludp->lud_host = specs[i];
1306 p = strchr(ludp->lud_host, ':');
1308 /* more than one :, IPv6 address */
1309 if ( strchr(p+1, ':') != NULL ) {
1310 /* allow [address] and [address]:port */
1311 if ( *ludp->lud_host == '[' ) {
1312 p = LDAP_STRDUP(ludp->lud_host+1);
1313 /* copied, make sure we free source later */
1314 specs[i] = ludp->lud_host;
1316 p = strchr( ludp->lud_host, ']' );
1319 ldap_charray_free(specs);
1320 return LDAP_PARAM_ERROR;
1326 ldap_charray_free(specs);
1327 return LDAP_PARAM_ERROR;
1339 ldap_pvt_hex_unescape(p);
1340 ludp->lud_port = strtol( p, &next, 10 );
1341 if ( next == p || next[0] != '\0' ) {
1343 ldap_charray_free(specs);
1344 return LDAP_PARAM_ERROR;
1348 ldap_pvt_hex_unescape(ludp->lud_host);
1349 ludp->lud_scheme = LDAP_STRDUP("ldap");
1350 ludp->lud_next = *ludlist;
1354 /* this should be an array of NULLs now */
1355 /* except entries starting with [ */
1356 ldap_charray_free(specs);
1357 return LDAP_SUCCESS;
1361 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1365 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
1367 if (ludlist == NULL)
1370 /* figure out how big the string is */
1371 size = 1; /* nul-term */
1372 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1373 size += strlen(ludp->lud_host) + 1; /* host and space */
1374 if (strchr(ludp->lud_host, ':')) /* will add [ ] below */
1376 if (ludp->lud_port != 0)
1377 size += sprintf(buf, ":%d", ludp->lud_port);
1379 s = LDAP_MALLOC(size);
1384 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1385 if (strchr(ludp->lud_host, ':')) {
1386 p += sprintf(p, "[%s]", ludp->lud_host);
1388 strcpy(p, ludp->lud_host);
1389 p += strlen(ludp->lud_host);
1391 if (ludp->lud_port != 0)
1392 p += sprintf(p, ":%d", ludp->lud_port);
1396 p--; /* nuke that extra space */
1403 LDAPURLDesc *ludlist )
1409 if ( ludlist == NULL ) {
1413 /* figure out how big the string is */
1414 for ( size = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1415 int len = desc2str_len( ludp );
1422 s = LDAP_MALLOC( size );
1428 for ( sofar = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1431 len = desc2str( ludp, &s[sofar], size );
1444 assert( size >= 0 );
1447 s[sofar - 1] = '\0';
1453 ldap_free_urllist( LDAPURLDesc *ludlist )
1455 LDAPURLDesc *ludp, *next;
1457 for (ludp = ludlist; ludp != NULL; ludp = next) {
1458 next = ludp->lud_next;
1459 ldap_free_urldesc(ludp);
1464 ldap_free_urldesc( LDAPURLDesc *ludp )
1466 if ( ludp == NULL ) {
1470 if ( ludp->lud_scheme != NULL ) {
1471 LDAP_FREE( ludp->lud_scheme );
1474 if ( ludp->lud_host != NULL ) {
1475 LDAP_FREE( ludp->lud_host );
1478 if ( ludp->lud_dn != NULL ) {
1479 LDAP_FREE( ludp->lud_dn );
1482 if ( ludp->lud_filter != NULL ) {
1483 LDAP_FREE( ludp->lud_filter);
1486 if ( ludp->lud_attrs != NULL ) {
1487 LDAP_VFREE( ludp->lud_attrs );
1490 if ( ludp->lud_exts != NULL ) {
1491 LDAP_VFREE( ludp->lud_exts );
1498 ldap_int_is_hexpair( char *s )
1502 for ( i = 0; i < 2; i++ ) {
1503 if ( s[i] >= '0' && s[i] <= '9' ) {
1507 if ( s[i] >= 'A' && s[i] <= 'F' ) {
1511 if ( s[i] >= 'a' && s[i] <= 'f' ) {
1522 ldap_int_unhex( int c )
1524 return( c >= '0' && c <= '9' ? c - '0'
1525 : c >= 'A' && c <= 'F' ? c - 'A' + 10
1530 ldap_pvt_hex_unescape( char *s )
1533 * Remove URL hex escapes from s... done in place. The basic concept for
1534 * this routine is borrowed from the WWW library HTUnEscape() routine.
1539 for ( p = s; *s != '\0'; ++s ) {
1542 * FIXME: what if '%' is followed
1543 * by non-hexpair chars?
1545 if ( !ldap_int_is_hexpair( s + 1 ) ) {
1550 if ( *++s == '\0' ) {
1553 *p = ldap_int_unhex( *s ) << 4;
1554 if ( *++s == '\0' ) {
1557 *p++ += ldap_int_unhex( *s );