]> git.sur5r.net Git - openldap/blob - libraries/libldap/dnssrv.c
ITS#7027 Implement priority/weight for DNS SRV records
[openldap] / libraries / libldap / dnssrv.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2014 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 /*
17  * locate LDAP servers using DNS SRV records.
18  * Location code based on MIT Kerberos KDC location code.
19  */
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/param.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "ldap-int.h"
32
33 #ifdef HAVE_ARPA_NAMESER_H
34 #include <arpa/nameser.h>
35 #endif
36 #ifdef HAVE_RESOLV_H
37 #include <resolv.h>
38 #endif
39
40 int ldap_dn2domain(
41         LDAP_CONST char *dn_in,
42         char **domainp)
43 {
44         int i, j;
45         char *ndomain;
46         LDAPDN dn = NULL;
47         LDAPRDN rdn = NULL;
48         LDAPAVA *ava = NULL;
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");
52
53         assert( dn_in != NULL );
54         assert( domainp != NULL );
55
56         *domainp = NULL;
57
58         if ( ldap_str2dn( dn_in, &dn, LDAP_DN_FORMAT_LDAP ) != LDAP_SUCCESS ) {
59                 return -2;
60         }
61
62         if( dn ) for( i=0; dn[i] != NULL; i++ ) {
63                 rdn = dn[i];
64
65                 for( j=0; rdn[j] != NULL; j++ ) {
66                         ava = rdn[j];
67
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 ) )
73                         {
74                                 if( domain.bv_len == 0 ) {
75                                         ndomain = LDAP_REALLOC( domain.bv_val,
76                                                 ava->la_value.bv_len + 1);
77
78                                         if( ndomain == NULL ) {
79                                                 goto return_error;
80                                         }
81
82                                         domain.bv_val = ndomain;
83
84                                         AC_MEMCPY( domain.bv_val, ava->la_value.bv_val,
85                                                 ava->la_value.bv_len );
86
87                                         domain.bv_len = ava->la_value.bv_len;
88                                         domain.bv_val[domain.bv_len] = '\0';
89
90                                 } else {
91                                         ndomain = LDAP_REALLOC( domain.bv_val,
92                                                 ava->la_value.bv_len + sizeof(".") + domain.bv_len );
93
94                                         if( ndomain == NULL ) {
95                                                 goto return_error;
96                                         }
97
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';
104                                 }
105                         } else {
106                                 domain.bv_len = 0;
107                         }
108                 } 
109         }
110
111
112         if( domain.bv_len == 0 && domain.bv_val != NULL ) {
113                 LDAP_FREE( domain.bv_val );
114                 domain.bv_val = NULL;
115         }
116
117         ldap_dnfree( dn );
118         *domainp = domain.bv_val;
119         return 0;
120
121 return_error:
122         ldap_dnfree( dn );
123         LDAP_FREE( domain.bv_val );
124         return -1;
125 }
126
127 int ldap_domain2dn(
128         LDAP_CONST char *domain_in,
129         char **dnp)
130 {
131         char *domain, *s, *tok_r, *dn, *dntmp;
132         size_t loc;
133
134         assert( domain_in != NULL );
135         assert( dnp != NULL );
136
137         domain = LDAP_STRDUP(domain_in);
138         if (domain == NULL) {
139                 return LDAP_NO_MEMORY;
140         }
141         dn = NULL;
142         loc = 0;
143
144         for (s = ldap_pvt_strtok(domain, ".", &tok_r);
145                 s != NULL;
146                 s = ldap_pvt_strtok(NULL, ".", &tok_r))
147         {
148                 size_t len = strlen(s);
149
150                 dntmp = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len );
151                 if (dntmp == NULL) {
152                     if (dn != NULL)
153                         LDAP_FREE(dn);
154                     LDAP_FREE(domain);
155                     return LDAP_NO_MEMORY;
156                 }
157
158                 dn = dntmp;
159
160                 if (loc > 0) {
161                     /* not first time. */
162                     strcpy(dn + loc, ",");
163                     loc++;
164                 }
165                 strcpy(dn + loc, "dc=");
166                 loc += sizeof("dc=")-1;
167
168                 strcpy(dn + loc, s);
169                 loc += len;
170     }
171
172         LDAP_FREE(domain);
173         *dnp = dn;
174         return LDAP_SUCCESS;
175 }
176
177 #ifdef HAVE_RES_QUERY
178 #define DNSBUFSIZ (64*1024)
179 typedef struct srv_record {
180     u_short priority;
181     u_short weight;
182     u_short port;
183     char hostname[DNSBUFSIZ];
184 } srv_record;
185
186
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;
190     u_long total;
191
192     if(a->priority < b->priority) {
193         return -1;
194     }
195     if(a->priority > b->priority) {
196         return 1;
197     }
198     if(a->priority == b->priority){
199         /* targets with same priority are in psudeo random order */
200         if (a->weight == 0 && b->weight == 0) {
201             if (rand() % 2) {
202                 return -1;
203             } else {
204                 return 1;
205             }
206         }
207         total = a->weight + b->weight;
208         if (rand() % total < a->weight) {
209             return -1;
210         } else {
211             return 1;
212         }
213     }
214 }
215 #endif /* HAVE_RES_QUERY */
216
217 /*
218  * Lookup and return LDAP servers for domain (using the DNS
219  * SRV record _ldap._tcp.domain).
220  */
221 int ldap_domain2hostlist(
222         LDAP_CONST char *domain,
223         char **list )
224 {
225 #ifdef HAVE_RES_QUERY
226     char *request;
227     char *hostlist = NULL;
228     srv_record *hostent_head=NULL;
229     int i;
230     int rc, len, cur = 0;
231     unsigned char reply[DNSBUFSIZ];
232     int hostent_count=0;
233
234         assert( domain != NULL );
235         assert( list != NULL );
236         if( *domain == '\0' ) {
237                 return LDAP_PARAM_ERROR;
238         }
239
240     request = LDAP_MALLOC(strlen(domain) + sizeof("_ldap._tcp."));
241     if (request == NULL) {
242                 return LDAP_NO_MEMORY;
243     }
244     sprintf(request, "_ldap._tcp.%s", domain);
245
246     LDAP_MUTEX_LOCK(&ldap_int_resolv_mutex);
247
248     rc = LDAP_UNAVAILABLE;
249 #ifdef NS_HFIXEDSZ
250         /* Bind 8/9 interface */
251     len = res_query(request, ns_c_in, ns_t_srv, reply, sizeof(reply));
252 #       ifndef T_SRV
253 #               define T_SRV ns_t_srv
254 #       endif
255 #else
256         /* Bind 4 interface */
257 #       ifndef T_SRV
258 #               define T_SRV 33
259 #       endif
260
261     len = res_query(request, C_IN, T_SRV, reply, sizeof(reply));
262 #endif
263     if (len >= 0) {
264         unsigned char *p;
265         char host[DNSBUFSIZ];
266         int status;
267         u_short port, priority, weight;
268
269         /* Parse out query */
270         p = reply;
271
272 #ifdef NS_HFIXEDSZ
273         /* Bind 8/9 interface */
274         p += NS_HFIXEDSZ;
275 #elif defined(HFIXEDSZ)
276         /* Bind 4 interface w/ HFIXEDSZ */
277         p += HFIXEDSZ;
278 #else
279         /* Bind 4 interface w/o HFIXEDSZ */
280         p += sizeof(HEADER);
281 #endif
282
283         status = dn_expand(reply, reply + len, p, host, sizeof(host));
284         if (status < 0) {
285             goto out;
286         }
287         p += status;
288         p += 4;
289
290         while (p < reply + len) {
291             int type, class, ttl, size;
292             status = dn_expand(reply, reply + len, p, host, sizeof(host));
293             if (status < 0) {
294                 goto out;
295             }
296             p += status;
297             type = (p[0] << 8) | p[1];
298             p += 2;
299             class = (p[0] << 8) | p[1];
300             p += 2;
301             ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
302             p += 4;
303             size = (p[0] << 8) | p[1];
304             p += 2;
305             if (type == T_SRV) {
306                 status = dn_expand(reply, reply + len, p + 6, host, sizeof(host));
307                 if (status < 0) {
308                     goto out;
309                 }
310
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];
315
316                 if ( port == 0 || host[ 0 ] == '\0' ) {
317                     goto add_size;
318                 }
319
320                 hostent_head = (srv_record *) LDAP_REALLOC(hostent_head, (hostent_count+1)*(sizeof(srv_record)));
321                 if(hostent_head==NULL){
322                     rc=LDAP_NO_MEMORY;
323                     goto out;
324                 }
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;
330             }
331 add_size:;
332             p += size;
333         }
334     }
335     qsort(hostent_head, hostent_count, sizeof(srv_record), srv_cmp);
336
337     for(i=0; i<hostent_count; i++){
338         int buflen;
339         buflen = strlen(hostent_head[i].hostname) + STRLENOF(":65355" );
340         hostlist = (char *) LDAP_REALLOC(hostlist, cur+buflen+1);
341         if (hostlist == NULL) {
342             rc = LDAP_NO_MEMORY;
343             goto out;
344         }
345         if(cur>0){
346             hostlist[cur++]=' ';
347         }
348         cur += sprintf(&hostlist[cur], "%s:%hd", hostent_head[i].hostname, hostent_head[i].port);
349     }
350
351     if (hostlist == NULL) {
352         /* No LDAP servers found in DNS. */
353         rc = LDAP_UNAVAILABLE;
354         goto out;
355     }
356
357     rc = LDAP_SUCCESS;
358         *list = hostlist;
359
360   out:
361     LDAP_MUTEX_UNLOCK(&ldap_int_resolv_mutex);
362
363     if (request != NULL) {
364         LDAP_FREE(request);
365     }
366     if (hostent_head != NULL) {
367         LDAP_FREE(hostent_head);
368     }
369     if (rc != LDAP_SUCCESS && hostlist != NULL) {
370         LDAP_FREE(hostlist);
371     }
372     return rc;
373 #else
374     return LDAP_NOT_SUPPORTED;
375 #endif /* HAVE_RES_QUERY */
376 }