1 /* LIBLDAP url.c -- LDAP URL (RFC 2255) 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 2254
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 static int str2scope( const char *p )
263 if ( strcasecmp( p, "one" ) == 0 ) {
264 return LDAP_SCOPE_ONELEVEL;
266 } else if ( strcasecmp( p, "onelevel" ) == 0 ) {
267 return LDAP_SCOPE_ONELEVEL;
269 } else if ( strcasecmp( p, "base" ) == 0 ) {
270 return LDAP_SCOPE_BASE;
272 } else if ( strcasecmp( p, "sub" ) == 0 ) {
273 return LDAP_SCOPE_SUBTREE;
275 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
276 return LDAP_SCOPE_SUBTREE;
278 } else if ( strcasecmp( p, "subordinate" ) == 0 ) {
279 return LDAP_SCOPE_SUBORDINATE;
281 } else if ( strcasecmp( p, "children" ) == 0 ) {
282 return LDAP_SCOPE_SUBORDINATE;
288 static const char hex[] = "0123456789ABCDEF";
290 #define URLESC_NONE 0x0000U
291 #define URLESC_COMMA 0x0001U
292 #define URLESC_SLASH 0x0002U
295 hex_escape_len( const char *s, unsigned list )
303 for ( len = 0; s[0]; s++ ) {
305 /* RFC 2396: reserved */
311 if ( list & URLESC_COMMA ) {
319 if ( list & URLESC_SLASH ) {
334 /* RFC 2396: unreserved mark */
347 /* RFC 2396: unreserved alphanum */
349 if ( !isalnum( (unsigned char) s[0] ) ) {
362 hex_escape( char *buf, int len, const char *s, unsigned list )
371 for ( pos = 0, i = 0; s[i] && pos < len; i++ ) {
375 /* RFC 2396: reserved */
381 if ( list & URLESC_COMMA ) {
387 if ( list & URLESC_SLASH ) {
400 /* RFC 2396: unreserved mark */
412 /* RFC 2396: unreserved alphanum */
414 if ( !isalnum( (unsigned char) s[i] ) ) {
422 buf[pos++] = hex[ (s[i] >> 4) & 0x0f ];
423 buf[pos++] = hex[ s[i] & 0x0f ];
436 hex_escape_len_list( char **s, unsigned flags )
446 for ( i = 0; s[i] != NULL; i++ ) {
450 len += hex_escape_len( s[i], flags );
457 hex_escape_list( char *buf, int len, char **s, unsigned flags )
467 for ( i = 0; s[i] != NULL; i++ ) {
474 curlen = hex_escape( &buf[pos], len, s[i], flags );
483 desc2str_len( LDAPURLDesc *u )
493 len += hex_escape_len_list( u->lud_exts, URLESC_COMMA );
499 if ( u->lud_filter ) {
500 len += hex_escape_len( u->lud_filter, URLESC_NONE );
506 switch ( u->lud_scope ) {
507 case LDAP_SCOPE_BASE:
508 case LDAP_SCOPE_ONELEVEL:
509 case LDAP_SCOPE_SUBTREE:
510 case LDAP_SCOPE_SUBORDINATE:
511 switch ( u->lud_scope ) {
512 case LDAP_SCOPE_BASE:
513 len += STRLENOF( "base" );
516 case LDAP_SCOPE_ONELEVEL:
517 len += STRLENOF( "one" );
520 case LDAP_SCOPE_SUBTREE:
521 len += STRLENOF( "sub" );
524 case LDAP_SCOPE_SUBORDINATE:
525 len += STRLENOF( "subordinate" );
538 if ( u->lud_attrs ) {
539 len += hex_escape_len_list( u->lud_attrs, URLESC_NONE );
545 if ( u->lud_dn && u->lud_dn[0] ) {
546 len += hex_escape_len( u->lud_dn, URLESC_NONE );
555 char buf[] = ":65535";
557 len += snprintf( buf, sizeof( buf ), ":%d", u->lud_port );
558 if ( u->lud_host && u->lud_host[0] ) {
559 len += strlen( u->lud_host );
563 if ( u->lud_host && u->lud_host[0] ) {
564 len += hex_escape_len( u->lud_host, URLESC_SLASH );
568 len += strlen( u->lud_scheme ) + STRLENOF( "://" );
574 desc2str( LDAPURLDesc *u, char *s, int len )
589 switch ( u->lud_scope ) {
590 case LDAP_SCOPE_BASE:
591 case LDAP_SCOPE_ONELEVEL:
592 case LDAP_SCOPE_SUBTREE:
593 case LDAP_SCOPE_SUBORDINATE:
600 } else if ( u->lud_filter ) {
602 } else if ( gotscope ) {
604 } else if ( u->lud_attrs ) {
606 } else if ( u->lud_dn && u->lud_dn[0] ) {
611 len -= sprintf( s, "%s://%s:%d%n", u->lud_scheme,
612 u->lud_host ? u->lud_host : "",
613 u->lud_port, &sofar );
616 len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar );
617 if ( u->lud_host && u->lud_host[0] ) {
618 i = hex_escape( &s[sofar], len, u->lud_host, URLESC_SLASH );
635 if ( u->lud_dn && u->lud_dn[0] ) {
636 i = hex_escape( &s[sofar], len, u->lud_dn, URLESC_NONE );
651 i = hex_escape_list( &s[sofar], len, u->lud_attrs, URLESC_NONE );
665 switch ( u->lud_scope ) {
666 case LDAP_SCOPE_BASE:
667 strcpy( &s[sofar], "base" );
668 sofar += STRLENOF("base");
669 len -= STRLENOF("base");
672 case LDAP_SCOPE_ONELEVEL:
673 strcpy( &s[sofar], "one" );
674 sofar += STRLENOF("one");
675 len -= STRLENOF("one");
678 case LDAP_SCOPE_SUBTREE:
679 strcpy( &s[sofar], "sub" );
680 sofar += STRLENOF("sub");
681 len -= STRLENOF("sub");
684 case LDAP_SCOPE_SUBORDINATE:
685 strcpy( &s[sofar], "children" );
686 sofar += STRLENOF("children");
687 len -= STRLENOF("children");
701 i = hex_escape( &s[sofar], len, u->lud_filter, URLESC_NONE );
715 i = hex_escape_list( &s[sofar], len, u->lud_exts, URLESC_COMMA );
730 ldap_url_desc2str( LDAPURLDesc *u )
739 len = desc2str_len( u );
744 /* allocate enough to hex escape everything -- overkill */
745 s = LDAP_MALLOC( len + 1 );
751 if ( desc2str( u, s, len ) != len ) {
762 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp, unsigned flags )
765 * Pick apart the pieces of an LDAP URL.
771 const char *scheme = NULL;
777 if( url_in == NULL || ludpp == NULL ) {
778 return LDAP_URL_ERR_PARAM;
781 #ifndef LDAP_INT_IN_KERNEL
782 /* Global options may not be created yet
783 * We can't test if the global options are initialized
784 * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
785 * the options and cause infinite recursion
787 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
790 *ludpp = NULL; /* pessimistic */
792 url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
794 if ( url_tmp == NULL ) {
795 return LDAP_URL_ERR_BADSCHEME;
798 assert( scheme != NULL );
800 /* make working copy of the remainder of the URL */
801 url = LDAP_STRDUP( url_tmp );
803 return LDAP_URL_ERR_MEM;
807 p = &url[strlen(url)-1];
811 return LDAP_URL_ERR_BADENCLOSURE;
817 /* allocate return struct */
818 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
820 if ( ludp == NULL ) {
822 return LDAP_URL_ERR_MEM;
825 ludp->lud_next = NULL;
826 ludp->lud_host = NULL;
829 ludp->lud_attrs = NULL;
830 ludp->lud_scope = ( flags & LDAP_PVT_URL_PARSE_NODEF_SCOPE ) ? LDAP_SCOPE_BASE : LDAP_SCOPE_DEFAULT;
831 ludp->lud_filter = NULL;
832 ludp->lud_exts = NULL;
834 ludp->lud_scheme = LDAP_STRDUP( scheme );
836 if ( ludp->lud_scheme == NULL ) {
838 ldap_free_urldesc( ludp );
839 return LDAP_URL_ERR_MEM;
842 /* scan forward for '/' that marks end of hostport and begin. of dn */
843 p = strchr( url, '/' );
846 /* terminate hostport; point to start of dn */
850 /* IPv6 syntax with [ip address]:port */
852 r = strchr( url, ']' );
855 ldap_free_urldesc( ludp );
856 return LDAP_URL_ERR_BADURL;
859 q = strchr( r, ':' );
861 q = strchr( url, ':' );
868 ldap_pvt_hex_unescape( q );
872 ldap_free_urldesc( ludp );
873 return LDAP_URL_ERR_BADURL;
876 ludp->lud_port = strtol( q, &next, 10 );
877 if ( next == q || next[0] != '\0' ) {
879 ldap_free_urldesc( ludp );
880 return LDAP_URL_ERR_BADURL;
884 if ( ( flags & LDAP_PVT_URL_PARSE_DEF_PORT ) && ludp->lud_port == 0 ) {
885 if ( strcmp( ludp->lud_scheme, "ldap" ) == 0 ) {
886 ludp->lud_port = LDAP_PORT;
887 #ifdef LDAP_CONNECTIONLESS
888 } else if ( strcmp( ludp->lud_scheme, "cldap" ) == 0 ) {
889 ludp->lud_port = LDAP_PORT;
891 } else if ( strcmp( ludp->lud_scheme, "ldaps" ) == 0 ) {
892 ludp->lud_port = LDAPS_PORT;
896 ldap_pvt_hex_unescape( url );
898 /* If [ip address]:port syntax, url is [ip and we skip the [ */
899 ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
901 if( ludp->lud_host == NULL ) {
903 ldap_free_urldesc( ludp );
904 return LDAP_URL_ERR_MEM;
907 if ( ( flags & LDAP_PVT_URL_PARSE_NOEMPTY_HOST )
908 && ludp->lud_host != NULL
909 && *ludp->lud_host == '\0' )
911 LDAP_FREE( ludp->lud_host );
912 ludp->lud_host = NULL;
916 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
918 * On early Novell releases, search references/referrals were returned
919 * in this format, i.e., the dn was kind of in the scope position,
920 * but the required slash is missing. The whole thing is illegal syntax,
921 * but we need to account for it. Fortunately it can't be confused with
924 if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
926 /* ? immediately followed by question */
931 ldap_pvt_hex_unescape( q );
932 ludp->lud_dn = LDAP_STRDUP( q );
934 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
935 ludp->lud_dn = LDAP_STRDUP( "" );
941 if ( check_dn && ludp->lud_dn == NULL ) {
943 ldap_free_urldesc( ludp );
944 return LDAP_URL_ERR_MEM;
952 return LDAP_URL_SUCCESS;
955 /* scan forward for '?' that may marks end of dn */
956 q = strchr( p, '?' );
959 /* terminate dn part */
965 ldap_pvt_hex_unescape( p );
966 ludp->lud_dn = LDAP_STRDUP( p );
968 } else if ( !( flags & LDAP_PVT_URL_PARSE_NOEMPTY_DN ) ) {
969 ludp->lud_dn = LDAP_STRDUP( "" );
975 if( check_dn && ludp->lud_dn == NULL ) {
977 ldap_free_urldesc( ludp );
978 return LDAP_URL_ERR_MEM;
985 return LDAP_URL_SUCCESS;
988 /* scan forward for '?' that may marks end of attributes */
990 q = strchr( p, '?' );
993 /* terminate attributes part */
998 /* parse attributes */
999 ldap_pvt_hex_unescape( p );
1000 ludp->lud_attrs = ldap_str2charray( p, "," );
1002 if( ludp->lud_attrs == NULL ) {
1004 ldap_free_urldesc( ludp );
1005 return LDAP_URL_ERR_BADATTRS;
1013 return LDAP_URL_SUCCESS;
1016 /* scan forward for '?' that may marks end of scope */
1018 q = strchr( p, '?' );
1021 /* terminate the scope part */
1026 /* parse the scope */
1027 ldap_pvt_hex_unescape( p );
1028 ludp->lud_scope = str2scope( p );
1030 if( ludp->lud_scope == -1 ) {
1032 ldap_free_urldesc( ludp );
1033 return LDAP_URL_ERR_BADSCOPE;
1041 return LDAP_URL_SUCCESS;
1044 /* scan forward for '?' that may marks end of filter */
1046 q = strchr( p, '?' );
1049 /* terminate the filter part */
1054 /* parse the filter */
1055 ldap_pvt_hex_unescape( p );
1058 /* missing filter */
1060 ldap_free_urldesc( ludp );
1061 return LDAP_URL_ERR_BADFILTER;
1064 ludp->lud_filter = LDAP_STRDUP( p );
1066 if( ludp->lud_filter == NULL ) {
1068 ldap_free_urldesc( ludp );
1069 return LDAP_URL_ERR_MEM;
1077 return LDAP_URL_SUCCESS;
1080 /* scan forward for '?' that may marks end of extensions */
1082 q = strchr( p, '?' );
1087 ldap_free_urldesc( ludp );
1088 return LDAP_URL_ERR_BADURL;
1091 /* parse the extensions */
1092 ludp->lud_exts = ldap_str2charray( p, "," );
1094 if( ludp->lud_exts == NULL ) {
1096 ldap_free_urldesc( ludp );
1097 return LDAP_URL_ERR_BADEXTS;
1100 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
1101 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
1103 if( *ludp->lud_exts[i] == '!' ) {
1104 /* count the number of critical extensions */
1105 ludp->lud_crit_exts++;
1110 /* must have 1 or more */
1112 ldap_free_urldesc( ludp );
1113 return LDAP_URL_ERR_BADEXTS;
1119 return LDAP_URL_SUCCESS;
1123 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
1125 return ldap_url_parse_ext( url_in, ludpp, LDAP_PVT_URL_PARSE_HISTORIC );
1129 ldap_url_dup ( LDAPURLDesc *ludp )
1133 if ( ludp == NULL ) {
1137 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
1142 dest->lud_scheme = NULL;
1143 dest->lud_host = NULL;
1144 dest->lud_dn = NULL;
1145 dest->lud_filter = NULL;
1146 dest->lud_attrs = NULL;
1147 dest->lud_exts = NULL;
1148 dest->lud_next = NULL;
1150 if ( ludp->lud_scheme != NULL ) {
1151 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
1152 if (dest->lud_scheme == NULL) {
1153 ldap_free_urldesc(dest);
1158 if ( ludp->lud_host != NULL ) {
1159 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
1160 if (dest->lud_host == NULL) {
1161 ldap_free_urldesc(dest);
1166 if ( ludp->lud_dn != NULL ) {
1167 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
1168 if (dest->lud_dn == NULL) {
1169 ldap_free_urldesc(dest);
1174 if ( ludp->lud_filter != NULL ) {
1175 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
1176 if (dest->lud_filter == NULL) {
1177 ldap_free_urldesc(dest);
1182 if ( ludp->lud_attrs != NULL ) {
1183 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
1184 if (dest->lud_attrs == NULL) {
1185 ldap_free_urldesc(dest);
1190 if ( ludp->lud_exts != NULL ) {
1191 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
1192 if (dest->lud_exts == NULL) {
1193 ldap_free_urldesc(dest);
1202 ldap_url_duplist (LDAPURLDesc *ludlist)
1204 LDAPURLDesc *dest, *tail, *ludp, *newludp;
1208 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1209 newludp = ldap_url_dup(ludp);
1210 if (newludp == NULL) {
1211 ldap_free_urllist(dest);
1217 tail->lud_next = newludp;
1224 ldap_url_parselist_int (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1231 assert( ludlist != NULL );
1232 assert( url != NULL );
1236 if ( sep == NULL ) {
1240 urls = ldap_str2charray( url, sep );
1242 return LDAP_URL_ERR_MEM;
1244 /* count the URLs... */
1245 for (i = 0; urls[i] != NULL; i++) ;
1246 /* ...and put them in the "stack" backward */
1248 rc = ldap_url_parse_ext( urls[i], &ludp, flags );
1250 ldap_charray_free( urls );
1251 ldap_free_urllist( *ludlist );
1255 ludp->lud_next = *ludlist;
1258 ldap_charray_free( urls );
1259 return LDAP_URL_SUCCESS;
1263 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
1265 return ldap_url_parselist_int( ludlist, url, ", ", LDAP_PVT_URL_PARSE_HISTORIC );
1269 ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep, unsigned flags )
1271 return ldap_url_parselist_int( ludlist, url, sep, flags );
1275 ldap_url_parsehosts(
1276 LDAPURLDesc **ludlist,
1284 assert( ludlist != NULL );
1285 assert( hosts != NULL );
1289 specs = ldap_str2charray(hosts, ", ");
1291 return LDAP_NO_MEMORY;
1293 /* count the URLs... */
1294 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
1296 /* ...and put them in the "stack" backward */
1298 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
1300 ldap_charray_free(specs);
1301 ldap_free_urllist(*ludlist);
1303 return LDAP_NO_MEMORY;
1305 ludp->lud_port = port;
1306 ludp->lud_host = specs[i];
1308 p = strchr(ludp->lud_host, ':');
1310 /* more than one :, IPv6 address */
1311 if ( strchr(p+1, ':') != NULL ) {
1312 /* allow [address] and [address]:port */
1313 if ( *ludp->lud_host == '[' ) {
1314 p = LDAP_STRDUP(ludp->lud_host+1);
1315 /* copied, make sure we free source later */
1316 specs[i] = ludp->lud_host;
1318 p = strchr( ludp->lud_host, ']' );
1321 ldap_charray_free(specs);
1322 return LDAP_PARAM_ERROR;
1328 ldap_charray_free(specs);
1329 return LDAP_PARAM_ERROR;
1341 ldap_pvt_hex_unescape(p);
1342 ludp->lud_port = strtol( p, &next, 10 );
1343 if ( next == p || next[0] != '\0' ) {
1345 ldap_charray_free(specs);
1346 return LDAP_PARAM_ERROR;
1350 ldap_pvt_hex_unescape(ludp->lud_host);
1351 ludp->lud_scheme = LDAP_STRDUP("ldap");
1352 ludp->lud_next = *ludlist;
1356 /* this should be an array of NULLs now */
1357 /* except entries starting with [ */
1358 ldap_charray_free(specs);
1359 return LDAP_SUCCESS;
1363 ldap_url_list2hosts (LDAPURLDesc *ludlist)
1367 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
1369 if (ludlist == NULL)
1372 /* figure out how big the string is */
1373 size = 1; /* nul-term */
1374 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1375 size += strlen(ludp->lud_host) + 1; /* host and space */
1376 if (strchr(ludp->lud_host, ':')) /* will add [ ] below */
1378 if (ludp->lud_port != 0)
1379 size += sprintf(buf, ":%d", ludp->lud_port);
1381 s = LDAP_MALLOC(size);
1386 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1387 if (strchr(ludp->lud_host, ':')) {
1388 p += sprintf(p, "[%s]", ludp->lud_host);
1390 strcpy(p, ludp->lud_host);
1391 p += strlen(ludp->lud_host);
1393 if (ludp->lud_port != 0)
1394 p += sprintf(p, ":%d", ludp->lud_port);
1398 p--; /* nuke that extra space */
1405 LDAPURLDesc *ludlist )
1411 if ( ludlist == NULL ) {
1415 /* figure out how big the string is */
1416 for ( size = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1417 int len = desc2str_len( ludp );
1424 s = LDAP_MALLOC( size );
1430 for ( sofar = 0, ludp = ludlist; ludp != NULL; ludp = ludp->lud_next ) {
1433 len = desc2str( ludp, &s[sofar], size );
1446 assert( size >= 0 );
1449 s[sofar - 1] = '\0';
1455 ldap_free_urllist( LDAPURLDesc *ludlist )
1457 LDAPURLDesc *ludp, *next;
1459 for (ludp = ludlist; ludp != NULL; ludp = next) {
1460 next = ludp->lud_next;
1461 ldap_free_urldesc(ludp);
1466 ldap_free_urldesc( LDAPURLDesc *ludp )
1468 if ( ludp == NULL ) {
1472 if ( ludp->lud_scheme != NULL ) {
1473 LDAP_FREE( ludp->lud_scheme );
1476 if ( ludp->lud_host != NULL ) {
1477 LDAP_FREE( ludp->lud_host );
1480 if ( ludp->lud_dn != NULL ) {
1481 LDAP_FREE( ludp->lud_dn );
1484 if ( ludp->lud_filter != NULL ) {
1485 LDAP_FREE( ludp->lud_filter);
1488 if ( ludp->lud_attrs != NULL ) {
1489 LDAP_VFREE( ludp->lud_attrs );
1492 if ( ludp->lud_exts != NULL ) {
1493 LDAP_VFREE( ludp->lud_exts );
1500 ldap_int_is_hexpair( char *s )
1504 for ( i = 0; i < 2; i++ ) {
1505 if ( s[i] >= '0' && s[i] <= '9' ) {
1509 if ( s[i] >= 'A' && s[i] <= 'F' ) {
1513 if ( s[i] >= 'a' && s[i] <= 'f' ) {
1524 ldap_int_unhex( int c )
1526 return( c >= '0' && c <= '9' ? c - '0'
1527 : c >= 'A' && c <= 'F' ? c - 'A' + 10
1532 ldap_pvt_hex_unescape( char *s )
1535 * Remove URL hex escapes from s... done in place. The basic concept for
1536 * this routine is borrowed from the WWW library HTUnEscape() routine.
1541 for ( p = s; *s != '\0'; ++s ) {
1544 * FIXME: what if '%' is followed
1545 * by non-hexpair chars?
1547 if ( !ldap_int_is_hexpair( s + 1 ) ) {
1552 if ( *++s == '\0' ) {
1555 *p = ldap_int_unhex( *s ) << 4;
1556 if ( *++s == '\0' ) {
1559 *p++ += ldap_int_unhex( *s );