From e87e01b9c549776234086055d562c55b0125486d Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Wed, 10 May 2000 21:48:18 +0000 Subject: [PATCH] DNS SRV meat (a work in progress) --- libraries/libldap/dnssrv.c | 86 ++++++++++++++++++++++++++++- servers/slapd/back-dnssrv/request.c | 53 +++++++++++++++++- 2 files changed, 136 insertions(+), 3 deletions(-) diff --git a/libraries/libldap/dnssrv.c b/libraries/libldap/dnssrv.c index d5597c57ec..11e3ed5b00 100644 --- a/libraries/libldap/dnssrv.c +++ b/libraries/libldap/dnssrv.c @@ -37,8 +37,90 @@ int ldap_dn2domain( LDAP_CONST char *dn_in, char **domainp) { - /* not yet implemented */ - return LDAP_NOT_SUPPORTED; + int i; + char* domain = NULL; + char ** dn; + + if( dn_in == NULL || domainp == NULL ) { + return -1; + } + + dn = ldap_explode_dn( dn_in, 0 ); + + if( dn == NULL ) { + return -2; + } + + for( i=0; dn[i] != NULL; i++ ) { + char ** rdn = ldap_explode_rdn( dn[i], 0 ); + + if( rdn == NULL || *rdn == NULL ) { + LDAP_FREE( rdn ); + LDAP_FREE( domain ); + LDAP_VFREE( dn ); + return -3; + } + +#define LDAP_DC "dc=" +#define LDAP_DCOID "0.9.2342.19200300.100.1.25=" + + if( *rdn[1] == NULL ) { + char *dc; + /* single RDN */ + + if( strncasecmp( rdn[0], + LDAP_DC, sizeof(LDAP_DC)-1 ) == 0 ) + { + dc = &rdn[0][sizeof(LDAP_DC)-1]; + + } else if( strncmp( rdn[0], + LDAP_DCOID, sizeof(LDAP_DCOID)-1 ) == 0 ) + { + dc = &rdn[0][sizeof(LDAP_DCOID)-1]; + + } else { + dc == NULL; + } + + if( dc != NULL ) { + char *ndomain; + + if( *dc == '\0' ) { + /* dc value is empty! */ + LDAP_FREE( rdn ); + LDAP_FREE( domain ); + LDAP_VFREE( dn ); + LDAP_VFREE( rdn ); + return -4; + } + + ndomain = realloc( domain, + strlen(domain) + strlen(dc) + 2 ); + + if( ndomain == NULL ) { + LDAP_FREE( rdn ); + LDAP_FREE( domain ); + LDAP_VFREE( dn ); + LDAP_VFREE( rdn ); + return -5; + } + + if( domain != NULL ) { + strcat( ndomain, "." ); + } + strcat( ndomain, dc ); + domain = ndomain; + continue; + } + } + + LDAP_VFREE( rdn ); + LDAP_FREE( domain ); + domain = NULL; + } + + *domainp = domain; + return 0; } int ldap_domain2dn( diff --git a/servers/slapd/back-dnssrv/request.c b/servers/slapd/back-dnssrv/request.c index cab7f12994..963a9023df 100644 --- a/servers/slapd/back-dnssrv/request.c +++ b/servers/slapd/back-dnssrv/request.c @@ -23,5 +23,56 @@ dnssrv_back_request( const char *dn, const char *ndn ) { - return -1; + int i; + char *domain = NULL; + char *hostlist = NULL; + char **hosts = NULL; + struct berval **urls = NULL; + + if( ldap_dn2domain( dn, &domain ) ) { + send_ldap_result( conn, op, LDAP_REFERRAL, + NULL, NULL, default_referral, NULL ); + goto done; + } + + if( ldap_domain2hostlist( dn, &domain ) ) { + send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, + NULL, NULL, NULL, NULL ); + goto done; + } + + hosts = str2charray( hostlist, " " ); + + if( hosts == NULL ) { + send_ldap_result( conn, op, LDAP_OTHER, + NULL, NULL, NULL, NULL ); + goto done; + } + + for( i=0; hosts[i] != NULL; i++) { + struct berval *url = ch_malloc( sizeof( struct berval ) ); + + url->bv_len = sizeof("ldap://") + strlen(hosts[i]); + url->bv_val = ch_malloc( url->bv_len ); + + strcpy( url->bv_val, "ldap://" ); + strcpy( &url->bv_val[sizeof("ldap://")-1], hosts[i] ); + + if( ber_bvecadd( &urls, url ) < 0) { + ber_bvfree( url ); + send_ldap_result( conn, op, LDAP_OTHER, + NULL, NULL, NULL, NULL ); + goto done; + } + } + + send_ldap_result( conn, op, LDAP_REFERRAL, + NULL, NULL, urls, NULL ); + +done: + if( domain != NULL ) ch_free( domain ); + if( hostlist != NULL ) ch_free( hostlist ); + if( hosts != NULL ) charray_free( hosts ); + if( urls != NULL ) ber_bvecfree( urls ); + return 0; } -- 2.39.5