2000-01-03 10:33:22 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
2000-05-13 10:36:07 +08:00
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
2000-01-03 10:33:22 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2000-04-25 18:38:03 +08:00
|
|
|
* locate LDAP servers using DNS SRV records.
|
|
|
|
* Location code based on MIT Kerberos KDC location code.
|
2000-01-03 10:33:22 +08:00
|
|
|
*/
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
|
|
|
|
#include <ac/param.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
|
|
|
|
#include "ldap-int.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_ARPA_NAMESER_H
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_RESOLV_H
|
|
|
|
#include <resolv.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Sometimes this is not defined. */
|
|
|
|
#ifndef T_SRV
|
|
|
|
#define T_SRV 33
|
|
|
|
#endif /* T_SRV */
|
|
|
|
|
2000-04-25 18:38:03 +08:00
|
|
|
int ldap_dn2domain(
|
|
|
|
LDAP_CONST char *dn_in,
|
|
|
|
char **domainp)
|
|
|
|
{
|
2000-05-11 05:48:18 +08:00
|
|
|
int i;
|
2000-05-11 08:50:14 +08:00
|
|
|
char *domain = NULL;
|
|
|
|
char **dn;
|
2000-05-11 05:48:18 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-11 06:21:57 +08:00
|
|
|
if( rdn[1] == NULL ) {
|
2000-05-27 02:05:27 +08:00
|
|
|
/*
|
|
|
|
* single-valued RDN
|
|
|
|
*/
|
2000-05-11 05:48:18 +08:00
|
|
|
char *dc;
|
2000-05-27 02:05:27 +08:00
|
|
|
|
|
|
|
#define LDAP_DC "dc="
|
|
|
|
#define LDAP_DCOID "0.9.2342.19200300.100.1.25="
|
2000-05-11 05:48:18 +08:00
|
|
|
|
|
|
|
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 {
|
2000-05-11 08:50:14 +08:00
|
|
|
dc = NULL;
|
2000-05-11 05:48:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-05-11 06:21:57 +08:00
|
|
|
ndomain = LDAP_REALLOC( domain,
|
|
|
|
( domain == NULL ? 0 : strlen(domain) )
|
2000-05-17 02:32:45 +08:00
|
|
|
+ strlen(dc) + sizeof(".") );
|
2000-05-11 05:48:18 +08:00
|
|
|
|
|
|
|
if( ndomain == NULL ) {
|
|
|
|
LDAP_FREE( rdn );
|
|
|
|
LDAP_FREE( domain );
|
|
|
|
LDAP_VFREE( dn );
|
|
|
|
LDAP_VFREE( rdn );
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
2000-05-27 02:05:27 +08:00
|
|
|
if( domain == NULL ) ndomain[0] = '\0';
|
|
|
|
|
2000-05-11 05:48:18 +08:00
|
|
|
strcat( ndomain, dc );
|
2000-05-17 02:31:35 +08:00
|
|
|
strcat( ndomain, "." );
|
|
|
|
|
2000-05-11 05:48:18 +08:00
|
|
|
domain = ndomain;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-27 02:05:27 +08:00
|
|
|
/*
|
|
|
|
* multi-valued RDN or fall thru
|
|
|
|
*/
|
|
|
|
|
2000-05-11 05:48:18 +08:00
|
|
|
LDAP_VFREE( rdn );
|
|
|
|
LDAP_FREE( domain );
|
|
|
|
domain = NULL;
|
|
|
|
}
|
|
|
|
|
2000-05-27 02:05:27 +08:00
|
|
|
if( domain != NULL && *domain == '\0' ) {
|
|
|
|
LDAP_FREE( domain );
|
|
|
|
domain = NULL;
|
|
|
|
}
|
|
|
|
|
2000-05-11 05:48:18 +08:00
|
|
|
*domainp = domain;
|
|
|
|
return 0;
|
2000-04-25 18:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ldap_domain2dn(
|
|
|
|
LDAP_CONST char *domain_in,
|
|
|
|
char **dnp)
|
2000-01-03 10:33:22 +08:00
|
|
|
{
|
|
|
|
char *domain, *s, *tok_r, *dn;
|
|
|
|
size_t loc;
|
|
|
|
|
|
|
|
if (domain_in == NULL || dnp == NULL) {
|
|
|
|
return LDAP_NO_MEMORY;
|
|
|
|
}
|
|
|
|
domain = LDAP_STRDUP(domain_in);
|
|
|
|
if (domain == NULL) {
|
|
|
|
return LDAP_NO_MEMORY;
|
|
|
|
}
|
|
|
|
dn = NULL;
|
|
|
|
loc = 0;
|
|
|
|
|
|
|
|
for (s = ldap_pvt_strtok(domain, ".", &tok_r);
|
|
|
|
s != NULL;
|
|
|
|
s = ldap_pvt_strtok(NULL, ".", &tok_r)) {
|
|
|
|
size_t len = strlen(s);
|
|
|
|
|
2000-04-25 18:38:03 +08:00
|
|
|
dn = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len );
|
2000-01-03 10:33:22 +08:00
|
|
|
if (dn == NULL) {
|
|
|
|
LDAP_FREE(domain);
|
|
|
|
return LDAP_NO_MEMORY;
|
|
|
|
}
|
|
|
|
if (loc > 0) {
|
|
|
|
/* not first time. */
|
|
|
|
strcpy(dn + loc, ",");
|
|
|
|
loc++;
|
|
|
|
}
|
|
|
|
strcpy(dn + loc, "dc=");
|
2000-04-25 18:38:03 +08:00
|
|
|
loc += sizeof("dc=")-1;
|
2000-01-03 10:33:22 +08:00
|
|
|
|
|
|
|
strcpy(dn + loc, s);
|
|
|
|
loc += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
LDAP_FREE(domain);
|
|
|
|
|
|
|
|
*dnp = dn;
|
|
|
|
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-04-25 18:38:03 +08:00
|
|
|
* Lookup and return LDAP servers for domain (using the DNS
|
|
|
|
* SRV record _ldap._tcp.domain).
|
2000-01-03 10:33:22 +08:00
|
|
|
*/
|
2000-04-25 18:38:03 +08:00
|
|
|
int ldap_domain2hostlist(
|
|
|
|
LDAP_CONST char *domain,
|
|
|
|
char **list )
|
2000-01-03 10:33:22 +08:00
|
|
|
{
|
|
|
|
#ifdef HAVE_RES_SEARCH
|
|
|
|
char *request;
|
|
|
|
char *dn;
|
|
|
|
char *hostlist = NULL;
|
|
|
|
int rc, len, cur = 0;
|
|
|
|
unsigned char reply[1024];
|
|
|
|
|
2000-04-25 18:38:03 +08:00
|
|
|
if( domain == NULL || *domain == '\0' ) {
|
|
|
|
return LDAP_PARAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( list == NULL ) {
|
|
|
|
return LDAP_PARAM_ERROR;
|
|
|
|
}
|
|
|
|
|
2000-01-03 10:33:22 +08:00
|
|
|
request = LDAP_MALLOC(strlen(domain) + sizeof("_ldap._tcp."));
|
|
|
|
if (request == NULL) {
|
|
|
|
rc = LDAP_NO_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sprintf(request, "_ldap._tcp.%s", domain);
|
|
|
|
|
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
|
|
|
|
#endif
|
|
|
|
|
2000-05-30 03:47:48 +08:00
|
|
|
rc = LDAP_UNAVAILABLE;
|
2000-01-03 10:33:22 +08:00
|
|
|
len = res_search(request, C_IN, T_SRV, reply, sizeof(reply));
|
|
|
|
if (len >= 0) {
|
|
|
|
unsigned char *p;
|
|
|
|
char host[1024];
|
|
|
|
int status;
|
|
|
|
u_short port;
|
2000-04-25 18:38:03 +08:00
|
|
|
/* int priority, weight; */
|
2000-01-03 10:33:22 +08:00
|
|
|
|
|
|
|
/* Parse out query */
|
|
|
|
p = reply;
|
|
|
|
p += sizeof(HEADER);
|
|
|
|
status = dn_expand(reply, reply + len, p, host, sizeof(host));
|
|
|
|
if (status < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
p += status;
|
|
|
|
p += 4;
|
|
|
|
|
|
|
|
while (p < reply + len) {
|
|
|
|
int type, class, ttl, size;
|
|
|
|
status = dn_expand(reply, reply + len, p, host, sizeof(host));
|
|
|
|
if (status < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
p += status;
|
|
|
|
type = (p[0] << 8) | p[1];
|
|
|
|
p += 2;
|
|
|
|
class = (p[0] << 8) | p[1];
|
|
|
|
p += 2;
|
|
|
|
ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
|
|
|
|
p += 4;
|
|
|
|
size = (p[0] << 8) | p[1];
|
|
|
|
p += 2;
|
|
|
|
if (type == T_SRV) {
|
|
|
|
int buflen;
|
|
|
|
status = dn_expand(reply, reply + len, p + 6, host, sizeof(host));
|
|
|
|
if (status < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
2000-04-25 18:38:03 +08:00
|
|
|
/* ignore priority and weight for now */
|
|
|
|
/* priority = (p[0] << 8) | p[1]; */
|
|
|
|
/* weight = (p[2] << 8) | p[3]; */
|
2000-01-03 10:33:22 +08:00
|
|
|
port = (p[4] << 8) | p[5];
|
|
|
|
|
2000-04-25 18:38:03 +08:00
|
|
|
buflen = strlen(host) + sizeof(":65355");
|
2000-01-03 10:33:22 +08:00
|
|
|
hostlist = (char *) LDAP_REALLOC(hostlist, cur + buflen);
|
|
|
|
if (hostlist == NULL) {
|
|
|
|
rc = LDAP_NO_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (cur > 0) {
|
|
|
|
/* not first time around */
|
|
|
|
hostlist[cur++] = ' ';
|
|
|
|
}
|
|
|
|
cur += sprintf(&hostlist[cur], "%s:%hd", host, port);
|
|
|
|
}
|
|
|
|
p += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hostlist == NULL) {
|
|
|
|
/* No LDAP servers found in DNS. */
|
|
|
|
rc = LDAP_UNAVAILABLE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = LDAP_SUCCESS;
|
2000-04-25 18:38:03 +08:00
|
|
|
*list = hostlist;
|
2000-01-03 10:33:22 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
#ifdef LDAP_R_COMPILE
|
|
|
|
ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (request != NULL) {
|
|
|
|
LDAP_FREE(request);
|
|
|
|
}
|
2000-04-25 18:38:03 +08:00
|
|
|
if (rc != LDAP_SUCCESS && hostlist != NULL) {
|
2000-01-03 10:33:22 +08:00
|
|
|
LDAP_FREE(hostlist);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
#else
|
|
|
|
return LDAP_NOT_SUPPORTED;
|
|
|
|
#endif /* HAVE_RES_SEARCH */
|
|
|
|
}
|