]> git.sur5r.net Git - openldap/blob - libraries/libldap/dnssrv.c
a6f4e24b9c0e65fb2069f39af6cda46fd283628d
[openldap] / libraries / libldap / dnssrv.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 /*
8  * locate LDAP servers using DNS SRV records.
9  * Location code based on MIT Kerberos KDC location code.
10  */
11 #include "portable.h"
12
13 #include <stdio.h>
14
15 #include <ac/stdlib.h>
16
17 #include <ac/param.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap-int.h"
23
24 #ifdef HAVE_ARPA_NAMESER_H
25 #include <arpa/nameser.h>
26 #endif
27 #ifdef HAVE_RESOLV_H
28 #include <resolv.h>
29 #endif
30
31 /* Sometimes this is not defined. */
32 #ifndef T_SRV
33 #define T_SRV            33
34 #endif                          /* T_SRV */
35
36 int ldap_dn2domain(
37         LDAP_CONST char *dn_in,
38         char **domainp)
39 {
40         int i;
41         char* domain = NULL;
42         char ** dn;
43
44         if( dn_in == NULL || domainp == NULL ) {
45                 return -1;
46         }
47
48         dn = ldap_explode_dn( dn_in, 0 );
49
50         if( dn == NULL ) {
51                 return -2;
52         }
53
54         for( i=0; dn[i] != NULL; i++ ) {
55                 char ** rdn = ldap_explode_rdn( dn[i], 0 );
56
57                 if( rdn == NULL || *rdn == NULL ) {
58                         LDAP_FREE( rdn );
59                         LDAP_FREE( domain );
60                         LDAP_VFREE( dn );
61                         return -3;
62                 }
63
64 #define LDAP_DC "dc="
65 #define LDAP_DCOID "0.9.2342.19200300.100.1.25="
66
67                 if( rdn[1] == NULL ) {
68                         char *dc;
69                         /* single RDN */
70
71                         if( strncasecmp( rdn[0],
72                                 LDAP_DC, sizeof(LDAP_DC)-1 ) == 0 )
73                         {
74                                 dc = &rdn[0][sizeof(LDAP_DC)-1];
75
76                         } else if( strncmp( rdn[0],
77                                 LDAP_DCOID, sizeof(LDAP_DCOID)-1 ) == 0 )
78                         {
79                                 dc = &rdn[0][sizeof(LDAP_DCOID)-1];
80
81                         } else {
82                                 dc == NULL;
83                         }
84
85                         if( dc != NULL ) {
86                                 char *ndomain;
87
88                                 if( *dc == '\0' ) {
89                                         /* dc value is empty! */
90                                         LDAP_FREE( rdn );
91                                         LDAP_FREE( domain );
92                                         LDAP_VFREE( dn );
93                                         LDAP_VFREE( rdn );
94                                         return -4;
95                                 }
96
97                                 ndomain = LDAP_REALLOC( domain,
98                                         ( domain == NULL ? 0 : strlen(domain) )
99                                         + strlen(dc) + 2 );
100
101                                 if( ndomain == NULL ) {
102                                         LDAP_FREE( rdn );
103                                         LDAP_FREE( domain );
104                                         LDAP_VFREE( dn );
105                                         LDAP_VFREE( rdn );
106                                         return -5;
107                                 }
108
109                                 if( domain != NULL ) {
110                                         strcat( ndomain, "." );
111                                 }
112                                 strcat( ndomain, dc );
113                                 domain = ndomain;
114                                 continue;
115                         }
116                 }
117
118                 LDAP_VFREE( rdn );
119                 LDAP_FREE( domain );
120                 domain = NULL;
121         } 
122
123         *domainp = domain;
124         return 0;
125 }
126
127 int ldap_domain2dn(
128         LDAP_CONST char *domain_in,
129         char **dnp)
130 {
131     char *domain, *s, *tok_r, *dn;
132     size_t loc;
133
134     if (domain_in == NULL || dnp == NULL) {
135         return LDAP_NO_MEMORY;
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         size_t len = strlen(s);
148
149         dn = (char *) LDAP_REALLOC(dn, loc + sizeof(",dc=") + len );
150         if (dn == NULL) {
151             LDAP_FREE(domain);
152             return LDAP_NO_MEMORY;
153         }
154         if (loc > 0) {
155             /* not first time. */
156             strcpy(dn + loc, ",");
157             loc++;
158         }
159         strcpy(dn + loc, "dc=");
160         loc += sizeof("dc=")-1;
161
162         strcpy(dn + loc, s);
163         loc += len;
164     }
165
166     LDAP_FREE(domain);
167
168     *dnp = dn;
169
170     return LDAP_SUCCESS;
171 }
172
173 /*
174  * Lookup and return LDAP servers for domain (using the DNS
175  * SRV record _ldap._tcp.domain).
176  */
177 int ldap_domain2hostlist(
178         LDAP_CONST char *domain,
179         char **list )
180 {
181 #ifdef HAVE_RES_SEARCH
182     char *request;
183     char *dn;
184     char *hostlist = NULL;
185     int rc, len, cur = 0;
186     unsigned char reply[1024];
187
188         if( domain == NULL || *domain == '\0' ) {
189                 return LDAP_PARAM_ERROR;
190         }
191
192         if( list == NULL ) {
193                 return LDAP_PARAM_ERROR;
194         }
195
196     request = LDAP_MALLOC(strlen(domain) + sizeof("_ldap._tcp."));
197     if (request == NULL) {
198         rc = LDAP_NO_MEMORY;
199         goto out;
200     }
201     sprintf(request, "_ldap._tcp.%s", domain);
202
203 #ifdef LDAP_R_COMPILE
204     ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
205 #endif
206
207     len = res_search(request, C_IN, T_SRV, reply, sizeof(reply));
208     if (len >= 0) {
209         unsigned char *p;
210         char host[1024];
211         int status;
212         u_short port;
213         /* int priority, weight; */
214
215         /* Parse out query */
216         p = reply;
217         p += sizeof(HEADER);
218         status = dn_expand(reply, reply + len, p, host, sizeof(host));
219         if (status < 0) {
220             goto out;
221         }
222         p += status;
223         p += 4;
224
225         while (p < reply + len) {
226             int type, class, ttl, size;
227             status = dn_expand(reply, reply + len, p, host, sizeof(host));
228             if (status < 0) {
229                 goto out;
230             }
231             p += status;
232             type = (p[0] << 8) | p[1];
233             p += 2;
234             class = (p[0] << 8) | p[1];
235             p += 2;
236             ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
237             p += 4;
238             size = (p[0] << 8) | p[1];
239             p += 2;
240             if (type == T_SRV) {
241                 int buflen;
242                 status = dn_expand(reply, reply + len, p + 6, host, sizeof(host));
243                 if (status < 0) {
244                     goto out;
245                 }
246                 /* ignore priority and weight for now */
247                 /* priority = (p[0] << 8) | p[1]; */
248                 /* weight = (p[2] << 8) | p[3]; */
249                 port = (p[4] << 8) | p[5];
250
251                 buflen = strlen(host) + sizeof(":65355");
252                 hostlist = (char *) LDAP_REALLOC(hostlist, cur + buflen);
253                 if (hostlist == NULL) {
254                     rc = LDAP_NO_MEMORY;
255                     goto out;
256                 }
257                 if (cur > 0) {
258                     /* not first time around */
259                     hostlist[cur++] = ' ';
260                 }
261                 cur += sprintf(&hostlist[cur], "%s:%hd", host, port);
262             }
263             p += size;
264         }
265     }
266     if (hostlist == NULL) {
267         /* No LDAP servers found in DNS. */
268         rc = LDAP_UNAVAILABLE;
269         goto out;
270     }
271
272     rc = LDAP_SUCCESS;
273         *list = hostlist;
274
275   out:
276 #ifdef LDAP_R_COMPILE
277     ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
278 #endif
279
280     if (request != NULL) {
281         LDAP_FREE(request);
282     }
283     if (rc != LDAP_SUCCESS && hostlist != NULL) {
284         LDAP_FREE(hostlist);
285     }
286     return rc;
287 #else
288     return LDAP_NOT_SUPPORTED;
289 #endif                          /* HAVE_RES_SEARCH */
290 }