3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * locate LDAP servers using DNS SRV records.
9 * Location code based on MIT Kerberos KDC location code.
15 #include <ac/stdlib.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
24 #ifdef HAVE_ARPA_NAMESER_H
25 #include <arpa/nameser.h>
31 /* Sometimes this is not defined. */
37 LDAP_CONST char *dn_in,
44 if( dn_in == NULL || domainp == NULL ) {
48 dn = ldap_explode_dn( dn_in, 0 );
54 for( i=0; dn[i] != NULL; i++ ) {
55 char ** rdn = ldap_explode_rdn( dn[i], 0 );
57 if( rdn == NULL || *rdn == NULL ) {
65 #define LDAP_DCOID "0.9.2342.19200300.100.1.25="
67 if( rdn[1] == NULL ) {
71 if( strncasecmp( rdn[0],
72 LDAP_DC, sizeof(LDAP_DC)-1 ) == 0 )
74 dc = &rdn[0][sizeof(LDAP_DC)-1];
76 } else if( strncmp( rdn[0],
77 LDAP_DCOID, sizeof(LDAP_DCOID)-1 ) == 0 )
79 dc = &rdn[0][sizeof(LDAP_DCOID)-1];
89 /* dc value is empty! */
97 ndomain = LDAP_REALLOC( domain,
98 ( domain == NULL ? 0 : strlen(domain) )
101 if( ndomain == NULL ) {
109 if( domain == NULL ) {
112 strcat( ndomain, "." );
114 strcat( ndomain, dc );
130 LDAP_CONST char *domain_in,
133 char *domain, *s, *tok_r, *dn;
136 if (domain_in == NULL || dnp == NULL) {
137 return LDAP_NO_MEMORY;
139 domain = LDAP_STRDUP(domain_in);
140 if (domain == NULL) {
141 return LDAP_NO_MEMORY;
146 for (s = ldap_pvt_strtok(domain, ".", &tok_r);
148 s = ldap_pvt_strtok(NULL, ".", &tok_r)) {
149 size_t len = strlen(s);
151 dn = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len );
154 return LDAP_NO_MEMORY;
157 /* not first time. */
158 strcpy(dn + loc, ",");
161 strcpy(dn + loc, "dc=");
162 loc += sizeof("dc=")-1;
176 * Lookup and return LDAP servers for domain (using the DNS
177 * SRV record _ldap._tcp.domain).
179 int ldap_domain2hostlist(
180 LDAP_CONST char *domain,
183 #ifdef HAVE_RES_SEARCH
186 char *hostlist = NULL;
187 int rc, len, cur = 0;
188 unsigned char reply[1024];
190 if( domain == NULL || *domain == '\0' ) {
191 return LDAP_PARAM_ERROR;
195 return LDAP_PARAM_ERROR;
198 request = LDAP_MALLOC(strlen(domain) + sizeof("_ldap._tcp."));
199 if (request == NULL) {
203 sprintf(request, "_ldap._tcp.%s", domain);
205 #ifdef LDAP_R_COMPILE
206 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
209 len = res_search(request, C_IN, T_SRV, reply, sizeof(reply));
215 /* int priority, weight; */
217 /* Parse out query */
220 status = dn_expand(reply, reply + len, p, host, sizeof(host));
227 while (p < reply + len) {
228 int type, class, ttl, size;
229 status = dn_expand(reply, reply + len, p, host, sizeof(host));
234 type = (p[0] << 8) | p[1];
236 class = (p[0] << 8) | p[1];
238 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
240 size = (p[0] << 8) | p[1];
244 status = dn_expand(reply, reply + len, p + 6, host, sizeof(host));
248 /* ignore priority and weight for now */
249 /* priority = (p[0] << 8) | p[1]; */
250 /* weight = (p[2] << 8) | p[3]; */
251 port = (p[4] << 8) | p[5];
253 buflen = strlen(host) + sizeof(":65355");
254 hostlist = (char *) LDAP_REALLOC(hostlist, cur + buflen);
255 if (hostlist == NULL) {
260 /* not first time around */
261 hostlist[cur++] = ' ';
263 cur += sprintf(&hostlist[cur], "%s:%hd", host, port);
268 if (hostlist == NULL) {
269 /* No LDAP servers found in DNS. */
270 rc = LDAP_UNAVAILABLE;
278 #ifdef LDAP_R_COMPILE
279 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
282 if (request != NULL) {
285 if (rc != LDAP_SUCCESS && hostlist != NULL) {
290 return LDAP_NOT_SUPPORTED;
291 #endif /* HAVE_RES_SEARCH */