2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
17 * locate LDAP servers using DNS SRV records.
18 * Location code based on MIT Kerberos KDC location code.
24 #include <ac/stdlib.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 #include <arpa/nameser.h>
41 LDAP_CONST char *dn_in,
49 struct berval domain = BER_BVNULL;
50 static const struct berval DC = BER_BVC("DC");
51 static const struct berval DCOID = BER_BVC("0.9.2342.19200300.100.1.25");
53 assert( dn_in != NULL );
54 assert( domainp != NULL );
58 if ( ldap_str2dn( dn_in, &dn, LDAP_DN_FORMAT_LDAP ) != LDAP_SUCCESS ) {
62 if( dn ) for( i=0; dn[i] != NULL; i++ ) {
65 for( j=0; rdn[j] != NULL; j++ ) {
68 if( rdn[j+1] == NULL &&
69 (ava->la_flags & LDAP_AVA_STRING) &&
70 ava->la_value.bv_len &&
71 ( ber_bvstrcasecmp( &ava->la_attr, &DC ) == 0
72 || ber_bvcmp( &ava->la_attr, &DCOID ) == 0 ) )
74 if( domain.bv_len == 0 ) {
75 ndomain = LDAP_REALLOC( domain.bv_val,
76 ava->la_value.bv_len + 1);
78 if( ndomain == NULL ) {
82 domain.bv_val = ndomain;
84 AC_MEMCPY( domain.bv_val, ava->la_value.bv_val,
85 ava->la_value.bv_len );
87 domain.bv_len = ava->la_value.bv_len;
88 domain.bv_val[domain.bv_len] = '\0';
91 ndomain = LDAP_REALLOC( domain.bv_val,
92 ava->la_value.bv_len + sizeof(".") + domain.bv_len );
94 if( ndomain == NULL ) {
98 domain.bv_val = ndomain;
99 domain.bv_val[domain.bv_len++] = '.';
100 AC_MEMCPY( &domain.bv_val[domain.bv_len],
101 ava->la_value.bv_val, ava->la_value.bv_len );
102 domain.bv_len += ava->la_value.bv_len;
103 domain.bv_val[domain.bv_len] = '\0';
112 if( domain.bv_len == 0 && domain.bv_val != NULL ) {
113 LDAP_FREE( domain.bv_val );
114 domain.bv_val = NULL;
118 *domainp = domain.bv_val;
123 LDAP_FREE( domain.bv_val );
128 LDAP_CONST char *domain_in,
131 char *domain, *s, *tok_r, *dn, *dntmp;
134 assert( domain_in != NULL );
135 assert( dnp != NULL );
137 domain = LDAP_STRDUP(domain_in);
138 if (domain == NULL) {
139 return LDAP_NO_MEMORY;
144 for (s = ldap_pvt_strtok(domain, ".", &tok_r);
146 s = ldap_pvt_strtok(NULL, ".", &tok_r))
148 size_t len = strlen(s);
150 dntmp = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len );
155 return LDAP_NO_MEMORY;
161 /* not first time. */
162 strcpy(dn + loc, ",");
165 strcpy(dn + loc, "dc=");
166 loc += sizeof("dc=")-1;
177 #ifdef HAVE_RES_QUERY
178 #define DNSBUFSIZ (64*1024)
179 typedef struct srv_record {
183 char hostname[DNSBUFSIZ];
187 static int srv_cmp(const void *aa, const void *bb){
188 srv_record *a=(srv_record *)aa;
189 srv_record *b=(srv_record *)bb;
192 if(a->priority < b->priority) {
195 if(a->priority > b->priority) {
198 if(a->priority == b->priority){
199 /* targets with same priority are in psudeo random order */
200 if (a->weight == 0 && b->weight == 0) {
207 total = a->weight + b->weight;
208 if (rand() % total < a->weight) {
215 #endif /* HAVE_RES_QUERY */
218 * Lookup and return LDAP servers for domain (using the DNS
219 * SRV record _ldap._tcp.domain).
221 int ldap_domain2hostlist(
222 LDAP_CONST char *domain,
225 #ifdef HAVE_RES_QUERY
227 char *hostlist = NULL;
228 srv_record *hostent_head=NULL;
230 int rc, len, cur = 0;
231 unsigned char reply[DNSBUFSIZ];
234 assert( domain != NULL );
235 assert( list != NULL );
236 if( *domain == '\0' ) {
237 return LDAP_PARAM_ERROR;
240 request = LDAP_MALLOC(strlen(domain) + sizeof("_ldap._tcp."));
241 if (request == NULL) {
242 return LDAP_NO_MEMORY;
244 sprintf(request, "_ldap._tcp.%s", domain);
246 LDAP_MUTEX_LOCK(&ldap_int_resolv_mutex);
248 rc = LDAP_UNAVAILABLE;
250 /* Bind 8/9 interface */
251 len = res_query(request, ns_c_in, ns_t_srv, reply, sizeof(reply));
253 # define T_SRV ns_t_srv
256 /* Bind 4 interface */
261 len = res_query(request, C_IN, T_SRV, reply, sizeof(reply));
265 char host[DNSBUFSIZ];
267 u_short port, priority, weight;
269 /* Parse out query */
273 /* Bind 8/9 interface */
275 #elif defined(HFIXEDSZ)
276 /* Bind 4 interface w/ HFIXEDSZ */
279 /* Bind 4 interface w/o HFIXEDSZ */
283 status = dn_expand(reply, reply + len, p, host, sizeof(host));
290 while (p < reply + len) {
291 int type, class, ttl, size;
292 status = dn_expand(reply, reply + len, p, host, sizeof(host));
297 type = (p[0] << 8) | p[1];
299 class = (p[0] << 8) | p[1];
301 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
303 size = (p[0] << 8) | p[1];
306 status = dn_expand(reply, reply + len, p + 6, host, sizeof(host));
311 /* Get priority weight and port */
312 priority = (p[0] << 8) | p[1];
313 weight = (p[2] << 8) | p[3];
314 port = (p[4] << 8) | p[5];
316 if ( port == 0 || host[ 0 ] == '\0' ) {
320 hostent_head = (srv_record *) LDAP_REALLOC(hostent_head, (hostent_count+1)*(sizeof(srv_record)));
321 if(hostent_head==NULL){
325 hostent_head[hostent_count].priority=priority;
326 hostent_head[hostent_count].weight=priority;
327 hostent_head[hostent_count].port=port;
328 strncpy(hostent_head[hostent_count].hostname, host,255);
329 hostent_count=hostent_count+1;
335 qsort(hostent_head, hostent_count, sizeof(srv_record), srv_cmp);
337 for(i=0; i<hostent_count; i++){
339 buflen = strlen(hostent_head[i].hostname) + STRLENOF(":65355" );
340 hostlist = (char *) LDAP_REALLOC(hostlist, cur+buflen+1);
341 if (hostlist == NULL) {
348 cur += sprintf(&hostlist[cur], "%s:%hd", hostent_head[i].hostname, hostent_head[i].port);
351 if (hostlist == NULL) {
352 /* No LDAP servers found in DNS. */
353 rc = LDAP_UNAVAILABLE;
361 LDAP_MUTEX_UNLOCK(&ldap_int_resolv_mutex);
363 if (request != NULL) {
366 if (hostent_head != NULL) {
367 LDAP_FREE(hostent_head);
369 if (rc != LDAP_SUCCESS && hostlist != NULL) {
374 return LDAP_NOT_SUPPORTED;
375 #endif /* HAVE_RES_QUERY */