version 1.91, 2007/06/18 23:02:42
|
version 1.92, 2007/06/19 20:58:20
|
Line 1
|
Line 1
|
/* common.c - common routines for the ldap client tools */ |
/* common.c - common routines for the ldap client tools */ |
/* $OpenLDAP: pkg/ldap/clients/tools/common.c,v 1.90 2007/06/09 16:25:51 ando Exp $ */ |
/* $OpenLDAP: pkg/ldap/clients/tools/common.c,v 1.91 2007/06/18 23:02:42 kurt Exp $ */ |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
* |
* |
* Copyright 1998-2007 The OpenLDAP Foundation. |
* Copyright 1998-2007 The OpenLDAP Foundation. |
Line 959 tool_conn_setup( int dont, void (*privat
|
Line 959 tool_conn_setup( int dont, void (*privat
|
ldapuri = ldap_url_desc2str( &url ); |
ldapuri = ldap_url_desc2str( &url ); |
|
|
} else if ( ldapuri != NULL ) { |
} else if ( ldapuri != NULL ) { |
LDAPURLDesc *lud; |
LDAPURLDesc *ludlist, **ludp; |
|
char **urls = NULL; |
|
int nurls = 0; |
|
|
rc = ldap_url_parse( ldapuri, &lud ); |
rc = ldap_url_parselist( &ludlist, ldapuri ); |
if ( rc != LDAP_URL_SUCCESS ) { |
if ( rc != LDAP_URL_SUCCESS ) { |
fprintf( stderr, |
fprintf( stderr, |
"Could not parse LDAP URI=%s (%d)\n", |
"Could not parse LDAP URI(s)=%s (%d)\n", |
ldapuri, rc ); |
ldapuri, rc ); |
exit( EXIT_FAILURE ); |
exit( EXIT_FAILURE ); |
} |
} |
|
|
if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' && |
for ( ludp = &ludlist; *ludp != NULL; ) { |
( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) ) |
LDAPURLDesc *lud = *ludp; |
{ |
char **tmp; |
/* if no host but a DN is provided, |
|
* use DNS SRV to gather the host list |
|
* and turn it into a list of URIs |
|
* using the scheme provided */ |
|
char *domain = NULL, |
|
*hostlist = NULL, |
|
**hosts = NULL, |
|
**urls = NULL; |
|
int i, |
|
len_proto = strlen( lud->lud_scheme ); |
|
|
|
ber_memfree( ldapuri ); |
|
ldapuri = NULL; |
|
|
|
if ( ldap_dn2domain( lud->lud_dn, &domain ) |
if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' && |
|| domain == NULL ) |
( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) ) |
{ |
{ |
fprintf( stderr, |
/* if no host but a DN is provided, |
"DNS SRV: Could not turn " |
* use DNS SRV to gather the host list |
"DN=\"%s\" into a domain\n", |
* and turn it into a list of URIs |
lud->lud_dn ); |
* using the scheme provided */ |
goto dnssrv_free; |
char *domain = NULL, |
} |
*hostlist = NULL, |
|
**hosts = NULL; |
rc = ldap_domain2hostlist( domain, &hostlist ); |
int i, |
if ( rc ) { |
len_proto = strlen( lud->lud_scheme ); |
fprintf( stderr, |
|
"DNS SRV: Could not turn " |
if ( ldap_dn2domain( lud->lud_dn, &domain ) |
"domain=%s into a hostlist\n", |
|| domain == NULL ) |
domain ); |
{ |
goto dnssrv_free; |
fprintf( stderr, |
} |
"DNS SRV: Could not turn " |
|
"DN=\"%s\" into a domain\n", |
|
lud->lud_dn ); |
|
goto dnssrv_free; |
|
} |
|
|
|
rc = ldap_domain2hostlist( domain, &hostlist ); |
|
if ( rc ) { |
|
fprintf( stderr, |
|
"DNS SRV: Could not turn " |
|
"domain=%s into a hostlist\n", |
|
domain ); |
|
goto dnssrv_free; |
|
} |
|
|
hosts = ldap_str2charray( hostlist, " " ); |
hosts = ldap_str2charray( hostlist, " " ); |
if ( hosts == NULL ) { |
if ( hosts == NULL ) { |
fprintf( stderr, |
fprintf( stderr, |
"DNS SRV: Could not parse " |
"DNS SRV: Could not parse " |
"hostlist=\"%s\"\n", |
"hostlist=\"%s\"\n", |
hostlist ); |
hostlist ); |
goto dnssrv_free; |
goto dnssrv_free; |
} |
} |
|
|
for ( i = 0; hosts[ i ] != NULL; i++ ) |
for ( i = 0; hosts[ i ] != NULL; i++ ) |
/* count'em */ ; |
/* count'em */ ; |
|
|
urls = (char **)calloc( sizeof( char * ), i + 1 ); |
tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) ); |
if ( urls == NULL ) { |
if ( tmp == NULL ) { |
fprintf( stderr, |
fprintf( stderr, |
"DNS SRV: out of memory?\n" ); |
"DNS SRV: out of memory?\n" ); |
goto dnssrv_free; |
goto dnssrv_free; |
} |
} |
|
urls = tmp; |
|
urls[ nurls ] = NULL; |
|
|
for ( i = 0; hosts[ i ] != NULL; i++ ) { |
for ( i = 0; hosts[ i ] != NULL; i++ ) { |
size_t len = len_proto |
size_t len = len_proto |
+ STRLENOF( "://" ) |
+ STRLENOF( "://" ) |
+ strlen( hosts[ i ] ) |
+ strlen( hosts[ i ] ) |
+ 1; |
+ 1; |
|
|
|
urls[ nurls + i + 1 ] = NULL; |
|
urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len ); |
|
if ( urls[ nurls + i ] == NULL ) { |
|
fprintf( stderr, |
|
"DNS SRV: out of memory?\n" ); |
|
goto dnssrv_free; |
|
} |
|
|
|
snprintf( urls[ nurls + i ], len, "%s://%s", |
|
lud->lud_scheme, hosts[ i ] ); |
|
} |
|
nurls += i; |
|
|
urls[ i ] = (char *)malloc( sizeof( char ) * len ); |
dnssrv_free:; |
if ( urls[ i ] == NULL ) { |
ber_memvfree( (void **)hosts ); |
|
ber_memfree( hostlist ); |
|
ber_memfree( domain ); |
|
|
|
} else { |
|
tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) ); |
|
if ( tmp == NULL ) { |
fprintf( stderr, |
fprintf( stderr, |
"DNS SRV: out of memory?\n" ); |
"DNS SRV: out of memory?\n" ); |
goto dnssrv_free; |
break; |
} |
} |
|
urls = tmp; |
|
urls[ nurls + 1 ] = NULL; |
|
|
snprintf( urls[ i ], len, "%s://%s", |
urls[ nurls ] = ldap_url_desc2str( lud ); |
lud->lud_scheme, hosts[ i ] ); |
if ( urls[ nurls ] == NULL ) { |
|
fprintf( stderr, |
|
"DNS SRV: out of memory?\n" ); |
|
break; |
|
} |
|
nurls++; |
} |
} |
|
|
ldapuri = ldap_charray2str( urls, " " ); |
*ludp = lud->lud_next; |
|
|
dnssrv_free:; |
lud->lud_next = NULL; |
ber_memvfree( (void **)hosts ); |
ldap_free_urldesc( lud ); |
ber_memvfree( (void **)urls ); |
|
ber_memfree( hostlist ); |
|
ber_memfree( domain ); |
|
} |
} |
|
|
ldap_free_urldesc( lud ); |
if ( ludlist != NULL ) { |
if ( ldapuri == NULL ) { |
ldap_free_urllist( ludlist ); |
|
exit( EXIT_FAILURE ); |
|
|
|
} else if ( urls == NULL ) { |
exit( EXIT_FAILURE ); |
exit( EXIT_FAILURE ); |
} |
} |
|
|
|
ldap_memfree( ldapuri ); |
|
ldapuri = ldap_charray2str( urls, " " ); |
|
ber_memvfree( (void **)urls ); |
} |
} |
|
|
if ( verbose ) { |
if ( verbose ) { |