1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | DNS utility library
|
---|
4 | Copyright (C) Gerald (Jerry) Carter 2006.
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 |
|
---|
23 | /* AIX resolv.h uses 'class' in struct ns_rr */
|
---|
24 |
|
---|
25 | #if defined(AIX)
|
---|
26 | # if defined(class)
|
---|
27 | # undef class
|
---|
28 | # endif
|
---|
29 | #endif /* AIX */
|
---|
30 |
|
---|
31 | /* resolver headers */
|
---|
32 |
|
---|
33 | #include <sys/types.h>
|
---|
34 | #include <netinet/in.h>
|
---|
35 | #include <arpa/nameser.h>
|
---|
36 | #include <resolv.h>
|
---|
37 | #include <netdb.h>
|
---|
38 |
|
---|
39 | #define MAX_DNS_PACKET_SIZE 0xffff
|
---|
40 |
|
---|
41 | #ifdef NS_HFIXEDSZ /* Bind 8/9 interface */
|
---|
42 | #if !defined(C_IN) /* AIX 5.3 already defines C_IN */
|
---|
43 | # define C_IN ns_c_in
|
---|
44 | #endif
|
---|
45 | #if !defined(T_A) /* AIX 5.3 already defines T_A */
|
---|
46 | # define T_A ns_t_a
|
---|
47 | #endif
|
---|
48 | # define T_SRV ns_t_srv
|
---|
49 | #if !defined(T_NS) /* AIX 5.3 already defines T_NS */
|
---|
50 | # define T_NS ns_t_ns
|
---|
51 | #endif
|
---|
52 | #else
|
---|
53 | # ifdef HFIXEDSZ
|
---|
54 | # define NS_HFIXEDSZ HFIXEDSZ
|
---|
55 | # else
|
---|
56 | # define NS_HFIXEDSZ sizeof(HEADER)
|
---|
57 | # endif /* HFIXEDSZ */
|
---|
58 | # ifdef PACKETSZ
|
---|
59 | # define NS_PACKETSZ PACKETSZ
|
---|
60 | # else /* 512 is usually the default */
|
---|
61 | # define NS_PACKETSZ 512
|
---|
62 | # endif /* PACKETSZ */
|
---|
63 | # define T_SRV 33
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | /*********************************************************************
|
---|
67 | *********************************************************************/
|
---|
68 |
|
---|
69 | static BOOL ads_dns_parse_query( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
|
---|
70 | uint8 **ptr, struct dns_query *q )
|
---|
71 | {
|
---|
72 | uint8 *p = *ptr;
|
---|
73 | pstring hostname;
|
---|
74 | int namelen;
|
---|
75 |
|
---|
76 | ZERO_STRUCTP( q );
|
---|
77 |
|
---|
78 | if ( !start || !end || !q || !*ptr)
|
---|
79 | return False;
|
---|
80 |
|
---|
81 | /* See RFC 1035 for details. If this fails, then return. */
|
---|
82 |
|
---|
83 | namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
|
---|
84 | if ( namelen < 0 ) {
|
---|
85 | return False;
|
---|
86 | }
|
---|
87 | p += namelen;
|
---|
88 | q->hostname = talloc_strdup( ctx, hostname );
|
---|
89 |
|
---|
90 | /* check that we have space remaining */
|
---|
91 |
|
---|
92 | if ( PTR_DIFF(p+4, end) > 0 )
|
---|
93 | return False;
|
---|
94 |
|
---|
95 | q->type = RSVAL( p, 0 );
|
---|
96 | q->in_class = RSVAL( p, 2 );
|
---|
97 | p += 4;
|
---|
98 |
|
---|
99 | *ptr = p;
|
---|
100 |
|
---|
101 | return True;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*********************************************************************
|
---|
105 | *********************************************************************/
|
---|
106 |
|
---|
107 | static BOOL ads_dns_parse_rr( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
|
---|
108 | uint8 **ptr, struct dns_rr *rr )
|
---|
109 | {
|
---|
110 | uint8 *p = *ptr;
|
---|
111 | pstring hostname;
|
---|
112 | int namelen;
|
---|
113 |
|
---|
114 | if ( !start || !end || !rr || !*ptr)
|
---|
115 | return -1;
|
---|
116 |
|
---|
117 | ZERO_STRUCTP( rr );
|
---|
118 | /* pull the name from the answer */
|
---|
119 |
|
---|
120 | namelen = dn_expand( start, end, p, hostname, sizeof(hostname) );
|
---|
121 | if ( namelen < 0 ) {
|
---|
122 | return -1;
|
---|
123 | }
|
---|
124 | p += namelen;
|
---|
125 | rr->hostname = talloc_strdup( ctx, hostname );
|
---|
126 |
|
---|
127 | /* check that we have space remaining */
|
---|
128 |
|
---|
129 | if ( PTR_DIFF(p+10, end) > 0 )
|
---|
130 | return False;
|
---|
131 |
|
---|
132 | /* pull some values and then skip onto the string */
|
---|
133 |
|
---|
134 | rr->type = RSVAL(p, 0);
|
---|
135 | rr->in_class = RSVAL(p, 2);
|
---|
136 | rr->ttl = RIVAL(p, 4);
|
---|
137 | rr->rdatalen = RSVAL(p, 8);
|
---|
138 |
|
---|
139 | p += 10;
|
---|
140 |
|
---|
141 | /* sanity check the available space */
|
---|
142 |
|
---|
143 | if ( PTR_DIFF(p+rr->rdatalen, end ) > 0 ) {
|
---|
144 | return False;
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* save a point to the rdata for this section */
|
---|
149 |
|
---|
150 | rr->rdata = p;
|
---|
151 | p += rr->rdatalen;
|
---|
152 |
|
---|
153 | *ptr = p;
|
---|
154 |
|
---|
155 | return True;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*********************************************************************
|
---|
159 | *********************************************************************/
|
---|
160 |
|
---|
161 | static BOOL ads_dns_parse_rr_srv( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
|
---|
162 | uint8 **ptr, struct dns_rr_srv *srv )
|
---|
163 | {
|
---|
164 | struct dns_rr rr;
|
---|
165 | uint8 *p;
|
---|
166 | pstring dcname;
|
---|
167 | int namelen;
|
---|
168 |
|
---|
169 | if ( !start || !end || !srv || !*ptr)
|
---|
170 | return -1;
|
---|
171 |
|
---|
172 | /* Parse the RR entry. Coming out of the this, ptr is at the beginning
|
---|
173 | of the next record */
|
---|
174 |
|
---|
175 | if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
|
---|
176 | DEBUG(1,("ads_dns_parse_rr_srv: Failed to parse RR record\n"));
|
---|
177 | return False;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if ( rr.type != T_SRV ) {
|
---|
181 | DEBUG(1,("ads_dns_parse_rr_srv: Bad answer type (%d)\n", rr.type));
|
---|
182 | return False;
|
---|
183 | }
|
---|
184 |
|
---|
185 | p = rr.rdata;
|
---|
186 |
|
---|
187 | srv->priority = RSVAL(p, 0);
|
---|
188 | srv->weight = RSVAL(p, 2);
|
---|
189 | srv->port = RSVAL(p, 4);
|
---|
190 |
|
---|
191 | p += 6;
|
---|
192 |
|
---|
193 | namelen = dn_expand( start, end, p, dcname, sizeof(dcname) );
|
---|
194 | if ( namelen < 0 ) {
|
---|
195 | DEBUG(1,("ads_dns_parse_rr_srv: Failed to uncompress name!\n"));
|
---|
196 | return False;
|
---|
197 | }
|
---|
198 |
|
---|
199 | srv->hostname = talloc_strdup( ctx, dcname );
|
---|
200 |
|
---|
201 | DEBUG(10,("ads_dns_parse_rr_srv: Parsed %s [%u, %u, %u]\n",
|
---|
202 | srv->hostname,
|
---|
203 | srv->priority,
|
---|
204 | srv->weight,
|
---|
205 | srv->port));
|
---|
206 |
|
---|
207 | return True;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /*********************************************************************
|
---|
211 | *********************************************************************/
|
---|
212 |
|
---|
213 | static BOOL ads_dns_parse_rr_ns( TALLOC_CTX *ctx, uint8 *start, uint8 *end,
|
---|
214 | uint8 **ptr, struct dns_rr_ns *nsrec )
|
---|
215 | {
|
---|
216 | struct dns_rr rr;
|
---|
217 | uint8 *p;
|
---|
218 | pstring nsname;
|
---|
219 | int namelen;
|
---|
220 |
|
---|
221 | if ( !start || !end || !nsrec || !*ptr)
|
---|
222 | return -1;
|
---|
223 |
|
---|
224 | /* Parse the RR entry. Coming out of the this, ptr is at the beginning
|
---|
225 | of the next record */
|
---|
226 |
|
---|
227 | if ( !ads_dns_parse_rr( ctx, start, end, ptr, &rr ) ) {
|
---|
228 | DEBUG(1,("ads_dns_parse_rr_ns: Failed to parse RR record\n"));
|
---|
229 | return False;
|
---|
230 | }
|
---|
231 |
|
---|
232 | if ( rr.type != T_NS ) {
|
---|
233 | DEBUG(1,("ads_dns_parse_rr_ns: Bad answer type (%d)\n", rr.type));
|
---|
234 | return False;
|
---|
235 | }
|
---|
236 |
|
---|
237 | p = rr.rdata;
|
---|
238 |
|
---|
239 | /* ame server hostname */
|
---|
240 |
|
---|
241 | namelen = dn_expand( start, end, p, nsname, sizeof(nsname) );
|
---|
242 | if ( namelen < 0 ) {
|
---|
243 | DEBUG(1,("ads_dns_parse_rr_ns: Failed to uncompress name!\n"));
|
---|
244 | return False;
|
---|
245 | }
|
---|
246 | nsrec->hostname = talloc_strdup( ctx, nsname );
|
---|
247 |
|
---|
248 | return True;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /*********************************************************************
|
---|
252 | Sort SRV record list based on weight and priority. See RFC 2782.
|
---|
253 | *********************************************************************/
|
---|
254 |
|
---|
255 | static int dnssrvcmp( struct dns_rr_srv *a, struct dns_rr_srv *b )
|
---|
256 | {
|
---|
257 | if ( a->priority == b->priority ) {
|
---|
258 |
|
---|
259 | /* randomize entries with an equal weight and priority */
|
---|
260 | if ( a->weight == b->weight )
|
---|
261 | return 0;
|
---|
262 |
|
---|
263 | /* higher weights should be sorted lower */
|
---|
264 | if ( a->weight > b->weight )
|
---|
265 | return -1;
|
---|
266 | else
|
---|
267 | return 1;
|
---|
268 | }
|
---|
269 |
|
---|
270 | if ( a->priority < b->priority )
|
---|
271 | return -1;
|
---|
272 |
|
---|
273 | return 1;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*********************************************************************
|
---|
277 | Simple wrapper for a DNS query
|
---|
278 | *********************************************************************/
|
---|
279 |
|
---|
280 | static NTSTATUS dns_send_req( TALLOC_CTX *ctx, const char *name, int q_type,
|
---|
281 | uint8 **buf, int *resp_length )
|
---|
282 | {
|
---|
283 | uint8 *buffer = NULL;
|
---|
284 | size_t buf_len = 0;
|
---|
285 | int resp_len = NS_PACKETSZ;
|
---|
286 |
|
---|
287 | do {
|
---|
288 | if ( buffer )
|
---|
289 | TALLOC_FREE( buffer );
|
---|
290 |
|
---|
291 | buf_len = resp_len * sizeof(uint8);
|
---|
292 |
|
---|
293 | if (buf_len) {
|
---|
294 | if ( (buffer = TALLOC_ARRAY(ctx, uint8, buf_len)) == NULL ) {
|
---|
295 | DEBUG(0,("ads_dns_lookup_srv: talloc() failed!\n"));
|
---|
296 | return NT_STATUS_NO_MEMORY;
|
---|
297 | }
|
---|
298 | } else {
|
---|
299 | buffer = NULL;
|
---|
300 | }
|
---|
301 |
|
---|
302 | if ( (resp_len = res_query(name, C_IN, q_type, buffer, buf_len)) < 0 ) {
|
---|
303 | DEBUG(3,("ads_dns_lookup_srv: Failed to resolve %s (%s)\n", name, strerror(errno)));
|
---|
304 | TALLOC_FREE( buffer );
|
---|
305 | if (errno == ETIMEDOUT) {
|
---|
306 | return NT_STATUS_IO_TIMEOUT;
|
---|
307 | }
|
---|
308 | if (errno == ECONNREFUSED) {
|
---|
309 | return NT_STATUS_CONNECTION_REFUSED;
|
---|
310 | }
|
---|
311 | return NT_STATUS_UNSUCCESSFUL;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /* On AIX, Solaris, and possibly some older glibc systems (e.g. SLES8)
|
---|
315 | truncated replies never give back a resp_len > buflen
|
---|
316 | which ends up causing DNS resolve failures on large tcp DNS replies */
|
---|
317 |
|
---|
318 | if (buf_len == resp_len) {
|
---|
319 | if (resp_len == MAX_DNS_PACKET_SIZE) {
|
---|
320 | DEBUG(1,("dns_send_req: DNS reply too large when resolving %s\n",
|
---|
321 | name));
|
---|
322 | TALLOC_FREE( buffer );
|
---|
323 | return NT_STATUS_BUFFER_TOO_SMALL;
|
---|
324 | }
|
---|
325 |
|
---|
326 | resp_len = MIN(resp_len*2, MAX_DNS_PACKET_SIZE);
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | } while ( buf_len < resp_len && resp_len <= MAX_DNS_PACKET_SIZE );
|
---|
331 |
|
---|
332 | *buf = buffer;
|
---|
333 | *resp_length = resp_len;
|
---|
334 |
|
---|
335 | return NT_STATUS_OK;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /*********************************************************************
|
---|
339 | Simple wrapper for a DNS SRV query
|
---|
340 | *********************************************************************/
|
---|
341 |
|
---|
342 | static NTSTATUS ads_dns_lookup_srv( TALLOC_CTX *ctx, const char *name, struct dns_rr_srv **dclist, int *numdcs )
|
---|
343 | {
|
---|
344 | uint8 *buffer = NULL;
|
---|
345 | int resp_len = 0;
|
---|
346 | struct dns_rr_srv *dcs = NULL;
|
---|
347 | int query_count, answer_count, auth_count, additional_count;
|
---|
348 | uint8 *p = buffer;
|
---|
349 | int rrnum;
|
---|
350 | int idx = 0;
|
---|
351 | NTSTATUS status;
|
---|
352 |
|
---|
353 | if ( !ctx || !name || !dclist ) {
|
---|
354 | return NT_STATUS_INVALID_PARAMETER;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /* Send the request. May have to loop several times in case
|
---|
358 | of large replies */
|
---|
359 |
|
---|
360 | status = dns_send_req( ctx, name, T_SRV, &buffer, &resp_len );
|
---|
361 | if ( !NT_STATUS_IS_OK(status) ) {
|
---|
362 | DEBUG(3,("ads_dns_lookup_srv: Failed to send DNS query (%s)\n",
|
---|
363 | nt_errstr(status)));
|
---|
364 | return status;
|
---|
365 | }
|
---|
366 | p = buffer;
|
---|
367 |
|
---|
368 | /* For some insane reason, the ns_initparse() et. al. routines are only
|
---|
369 | available in libresolv.a, and not the shared lib. Who knows why....
|
---|
370 | So we have to parse the DNS reply ourselves */
|
---|
371 |
|
---|
372 | /* Pull the answer RR's count from the header. Use the NMB ordering macros */
|
---|
373 |
|
---|
374 | query_count = RSVAL( p, 4 );
|
---|
375 | answer_count = RSVAL( p, 6 );
|
---|
376 | auth_count = RSVAL( p, 8 );
|
---|
377 | additional_count = RSVAL( p, 10 );
|
---|
378 |
|
---|
379 | DEBUG(4,("ads_dns_lookup_srv: %d records returned in the answer section.\n",
|
---|
380 | answer_count));
|
---|
381 |
|
---|
382 | if (answer_count) {
|
---|
383 | if ( (dcs = TALLOC_ZERO_ARRAY(ctx, struct dns_rr_srv, answer_count)) == NULL ) {
|
---|
384 | DEBUG(0,("ads_dns_lookup_srv: talloc() failure for %d char*'s\n",
|
---|
385 | answer_count));
|
---|
386 | return NT_STATUS_NO_MEMORY;
|
---|
387 | }
|
---|
388 | } else {
|
---|
389 | dcs = NULL;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /* now skip the header */
|
---|
393 |
|
---|
394 | p += NS_HFIXEDSZ;
|
---|
395 |
|
---|
396 | /* parse the query section */
|
---|
397 |
|
---|
398 | for ( rrnum=0; rrnum<query_count; rrnum++ ) {
|
---|
399 | struct dns_query q;
|
---|
400 |
|
---|
401 | if (!ads_dns_parse_query(ctx, buffer,
|
---|
402 | buffer+resp_len, &p, &q)) {
|
---|
403 | DEBUG(1,("ads_dns_lookup_srv: "
|
---|
404 | "Failed to parse query record [%d]!\n", rrnum));
|
---|
405 | return NT_STATUS_UNSUCCESSFUL;
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | /* now we are at the answer section */
|
---|
410 |
|
---|
411 | for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
|
---|
412 | if (!ads_dns_parse_rr_srv(ctx, buffer, buffer+resp_len,
|
---|
413 | &p, &dcs[rrnum])) {
|
---|
414 | DEBUG(1,("ads_dns_lookup_srv: "
|
---|
415 | "Failed to parse answer recordi [%d]!\n", rrnum));
|
---|
416 | return NT_STATUS_UNSUCCESSFUL;
|
---|
417 | }
|
---|
418 | }
|
---|
419 | idx = rrnum;
|
---|
420 |
|
---|
421 | /* Parse the authority section */
|
---|
422 | /* just skip these for now */
|
---|
423 |
|
---|
424 | for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
|
---|
425 | struct dns_rr rr;
|
---|
426 |
|
---|
427 | if (!ads_dns_parse_rr( ctx, buffer,
|
---|
428 | buffer+resp_len, &p, &rr)) {
|
---|
429 | DEBUG(1,("ads_dns_lookup_srv: "
|
---|
430 | "Failed to parse authority record! [%d]\n", rrnum));
|
---|
431 | return NT_STATUS_UNSUCCESSFUL;
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | /* Parse the additional records section */
|
---|
436 |
|
---|
437 | for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
|
---|
438 | struct dns_rr rr;
|
---|
439 | int i;
|
---|
440 |
|
---|
441 | if (!ads_dns_parse_rr(ctx, buffer, buffer+resp_len,
|
---|
442 | &p, &rr)) {
|
---|
443 | DEBUG(1,("ads_dns_lookup_srv: Failed "
|
---|
444 | "to parse additional records section! [%d]\n", rrnum));
|
---|
445 | return NT_STATUS_UNSUCCESSFUL;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /* only interested in A records as a shortcut for having to come
|
---|
449 | back later and lookup the name. For multi-homed hosts, the
|
---|
450 | number of additional records and exceed the number of answer
|
---|
451 | records. */
|
---|
452 |
|
---|
453 |
|
---|
454 | if ( (rr.type != T_A) || (rr.rdatalen != 4) )
|
---|
455 | continue;
|
---|
456 |
|
---|
457 | for ( i=0; i<idx; i++ ) {
|
---|
458 | if ( strcmp( rr.hostname, dcs[i].hostname ) == 0 ) {
|
---|
459 | int num_ips = dcs[i].num_ips;
|
---|
460 | uint8 *buf;
|
---|
461 | struct in_addr *tmp_ips;
|
---|
462 |
|
---|
463 | /* allocate new memory */
|
---|
464 |
|
---|
465 | if ( dcs[i].num_ips == 0 ) {
|
---|
466 | if ( (dcs[i].ips = TALLOC_ARRAY( dcs,
|
---|
467 | struct in_addr, 1 )) == NULL )
|
---|
468 | {
|
---|
469 | return NT_STATUS_NO_MEMORY;
|
---|
470 | }
|
---|
471 | } else {
|
---|
472 | if ( (tmp_ips = TALLOC_REALLOC_ARRAY( dcs, dcs[i].ips,
|
---|
473 | struct in_addr, dcs[i].num_ips+1)) == NULL )
|
---|
474 | {
|
---|
475 | return NT_STATUS_NO_MEMORY;
|
---|
476 | }
|
---|
477 |
|
---|
478 | dcs[i].ips = tmp_ips;
|
---|
479 | }
|
---|
480 | dcs[i].num_ips++;
|
---|
481 |
|
---|
482 | /* copy the new IP address */
|
---|
483 |
|
---|
484 | buf = (uint8*)&dcs[i].ips[num_ips].s_addr;
|
---|
485 | memcpy( buf, rr.rdata, 4 );
|
---|
486 | }
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | qsort( dcs, idx, sizeof(struct dns_rr_srv), QSORT_CAST dnssrvcmp );
|
---|
491 |
|
---|
492 | *dclist = dcs;
|
---|
493 | *numdcs = idx;
|
---|
494 |
|
---|
495 | return NT_STATUS_OK;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /*********************************************************************
|
---|
499 | Simple wrapper for a DNS NS query
|
---|
500 | *********************************************************************/
|
---|
501 |
|
---|
502 | NTSTATUS ads_dns_lookup_ns( TALLOC_CTX *ctx, const char *dnsdomain, struct dns_rr_ns **nslist, int *numns )
|
---|
503 | {
|
---|
504 | uint8 *buffer = NULL;
|
---|
505 | int resp_len = 0;
|
---|
506 | struct dns_rr_ns *nsarray = NULL;
|
---|
507 | int query_count, answer_count, auth_count, additional_count;
|
---|
508 | uint8 *p;
|
---|
509 | int rrnum;
|
---|
510 | int idx = 0;
|
---|
511 | NTSTATUS status;
|
---|
512 |
|
---|
513 | if ( !ctx || !dnsdomain || !nslist ) {
|
---|
514 | return NT_STATUS_INVALID_PARAMETER;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /* Send the request. May have to loop several times in case
|
---|
518 | of large replies */
|
---|
519 |
|
---|
520 | status = dns_send_req( ctx, dnsdomain, T_NS, &buffer, &resp_len );
|
---|
521 | if ( !NT_STATUS_IS_OK(status) ) {
|
---|
522 | DEBUG(3,("ads_dns_lookup_ns: Failed to send DNS query (%s)\n",
|
---|
523 | nt_errstr(status)));
|
---|
524 | return status;
|
---|
525 | }
|
---|
526 | p = buffer;
|
---|
527 |
|
---|
528 | /* For some insane reason, the ns_initparse() et. al. routines are only
|
---|
529 | available in libresolv.a, and not the shared lib. Who knows why....
|
---|
530 | So we have to parse the DNS reply ourselves */
|
---|
531 |
|
---|
532 | /* Pull the answer RR's count from the header. Use the NMB ordering macros */
|
---|
533 |
|
---|
534 | query_count = RSVAL( p, 4 );
|
---|
535 | answer_count = RSVAL( p, 6 );
|
---|
536 | auth_count = RSVAL( p, 8 );
|
---|
537 | additional_count = RSVAL( p, 10 );
|
---|
538 |
|
---|
539 | DEBUG(4,("ads_dns_lookup_ns: %d records returned in the answer section.\n",
|
---|
540 | answer_count));
|
---|
541 |
|
---|
542 | if (answer_count) {
|
---|
543 | if ( (nsarray = TALLOC_ARRAY(ctx, struct dns_rr_ns, answer_count)) == NULL ) {
|
---|
544 | DEBUG(0,("ads_dns_lookup_ns: talloc() failure for %d char*'s\n",
|
---|
545 | answer_count));
|
---|
546 | return NT_STATUS_NO_MEMORY;
|
---|
547 | }
|
---|
548 | } else {
|
---|
549 | nsarray = NULL;
|
---|
550 | }
|
---|
551 |
|
---|
552 | /* now skip the header */
|
---|
553 |
|
---|
554 | p += NS_HFIXEDSZ;
|
---|
555 |
|
---|
556 | /* parse the query section */
|
---|
557 |
|
---|
558 | for ( rrnum=0; rrnum<query_count; rrnum++ ) {
|
---|
559 | struct dns_query q;
|
---|
560 |
|
---|
561 | if ( !ads_dns_parse_query( ctx, buffer, buffer+resp_len, &p, &q ) ) {
|
---|
562 | DEBUG(1,("ads_dns_lookup_ns: Failed to parse query record!\n"));
|
---|
563 | return NT_STATUS_UNSUCCESSFUL;
|
---|
564 | }
|
---|
565 | }
|
---|
566 |
|
---|
567 | /* now we are at the answer section */
|
---|
568 |
|
---|
569 | for ( rrnum=0; rrnum<answer_count; rrnum++ ) {
|
---|
570 | if ( !ads_dns_parse_rr_ns( ctx, buffer, buffer+resp_len, &p, &nsarray[rrnum] ) ) {
|
---|
571 | DEBUG(1,("ads_dns_lookup_ns: Failed to parse answer record!\n"));
|
---|
572 | return NT_STATUS_UNSUCCESSFUL;
|
---|
573 | }
|
---|
574 | }
|
---|
575 | idx = rrnum;
|
---|
576 |
|
---|
577 | /* Parse the authority section */
|
---|
578 | /* just skip these for now */
|
---|
579 |
|
---|
580 | for ( rrnum=0; rrnum<auth_count; rrnum++ ) {
|
---|
581 | struct dns_rr rr;
|
---|
582 |
|
---|
583 | if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
|
---|
584 | DEBUG(1,("ads_dns_lookup_ns: Failed to parse authority record!\n"));
|
---|
585 | return NT_STATUS_UNSUCCESSFUL;
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | /* Parse the additional records section */
|
---|
590 |
|
---|
591 | for ( rrnum=0; rrnum<additional_count; rrnum++ ) {
|
---|
592 | struct dns_rr rr;
|
---|
593 | int i;
|
---|
594 |
|
---|
595 | if ( !ads_dns_parse_rr( ctx, buffer, buffer+resp_len, &p, &rr ) ) {
|
---|
596 | DEBUG(1,("ads_dns_lookup_ns: Failed to parse additional records section!\n"));
|
---|
597 | return NT_STATUS_UNSUCCESSFUL;
|
---|
598 | }
|
---|
599 |
|
---|
600 | /* only interested in A records as a shortcut for having to come
|
---|
601 | back later and lookup the name */
|
---|
602 |
|
---|
603 | if ( (rr.type != T_A) || (rr.rdatalen != 4) )
|
---|
604 | continue;
|
---|
605 |
|
---|
606 | for ( i=0; i<idx; i++ ) {
|
---|
607 | if ( strcmp( rr.hostname, nsarray[i].hostname ) == 0 ) {
|
---|
608 | uint8 *buf = (uint8*)&nsarray[i].ip.s_addr;
|
---|
609 | memcpy( buf, rr.rdata, 4 );
|
---|
610 | }
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | *nslist = nsarray;
|
---|
615 | *numns = idx;
|
---|
616 |
|
---|
617 | return NT_STATUS_OK;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /****************************************************************************
|
---|
621 | Store and fetch the AD client sitename.
|
---|
622 | ****************************************************************************/
|
---|
623 |
|
---|
624 | #define SITENAME_KEY "AD_SITENAME/DOMAIN/%s"
|
---|
625 |
|
---|
626 | static char *sitename_key(const char *realm)
|
---|
627 | {
|
---|
628 | char *keystr;
|
---|
629 |
|
---|
630 | if (asprintf(&keystr, SITENAME_KEY, strupper_static(realm)) == -1) {
|
---|
631 | return NULL;
|
---|
632 | }
|
---|
633 |
|
---|
634 | return keystr;
|
---|
635 | }
|
---|
636 |
|
---|
637 |
|
---|
638 | /****************************************************************************
|
---|
639 | Store the AD client sitename.
|
---|
640 | We store indefinately as every new CLDAP query will re-write this.
|
---|
641 | ****************************************************************************/
|
---|
642 |
|
---|
643 | BOOL sitename_store(const char *realm, const char *sitename)
|
---|
644 | {
|
---|
645 | time_t expire;
|
---|
646 | BOOL ret = False;
|
---|
647 | char *key;
|
---|
648 |
|
---|
649 | if (!gencache_init()) {
|
---|
650 | return False;
|
---|
651 | }
|
---|
652 |
|
---|
653 | if (!realm || (strlen(realm) == 0)) {
|
---|
654 | DEBUG(0,("sitename_store: no realm\n"));
|
---|
655 | return False;
|
---|
656 | }
|
---|
657 |
|
---|
658 | key = sitename_key(realm);
|
---|
659 |
|
---|
660 | if (!sitename || (sitename && !*sitename)) {
|
---|
661 | DEBUG(5,("sitename_store: deleting empty sitename!\n"));
|
---|
662 | ret = gencache_del(key);
|
---|
663 | SAFE_FREE(key);
|
---|
664 | return ret;
|
---|
665 | }
|
---|
666 |
|
---|
667 | expire = get_time_t_max(); /* Store indefinately. */
|
---|
668 |
|
---|
669 | DEBUG(10,("sitename_store: realm = [%s], sitename = [%s], expire = [%u]\n",
|
---|
670 | realm, sitename, (unsigned int)expire ));
|
---|
671 |
|
---|
672 | ret = gencache_set( key, sitename, expire );
|
---|
673 | SAFE_FREE(key);
|
---|
674 | return ret;
|
---|
675 | }
|
---|
676 |
|
---|
677 | /****************************************************************************
|
---|
678 | Fetch the AD client sitename.
|
---|
679 | Caller must free.
|
---|
680 | ****************************************************************************/
|
---|
681 |
|
---|
682 | char *sitename_fetch(const char *realm)
|
---|
683 | {
|
---|
684 | char *sitename = NULL;
|
---|
685 | time_t timeout;
|
---|
686 | BOOL ret = False;
|
---|
687 | const char *query_realm;
|
---|
688 | char *key;
|
---|
689 |
|
---|
690 | if (!gencache_init()) {
|
---|
691 | return NULL;
|
---|
692 | }
|
---|
693 |
|
---|
694 | if (!realm || (strlen(realm) == 0)) {
|
---|
695 | query_realm = lp_realm();
|
---|
696 | } else {
|
---|
697 | query_realm = realm;
|
---|
698 | }
|
---|
699 |
|
---|
700 | key = sitename_key(query_realm);
|
---|
701 |
|
---|
702 | ret = gencache_get( key, &sitename, &timeout );
|
---|
703 | SAFE_FREE(key);
|
---|
704 | if ( !ret ) {
|
---|
705 | DEBUG(5,("sitename_fetch: No stored sitename for %s\n",
|
---|
706 | query_realm));
|
---|
707 | } else {
|
---|
708 | DEBUG(5,("sitename_fetch: Returning sitename for %s: \"%s\"\n",
|
---|
709 | query_realm, sitename ));
|
---|
710 | }
|
---|
711 | return sitename;
|
---|
712 | }
|
---|
713 |
|
---|
714 | /****************************************************************************
|
---|
715 | Did the sitename change ?
|
---|
716 | ****************************************************************************/
|
---|
717 |
|
---|
718 | BOOL stored_sitename_changed(const char *realm, const char *sitename)
|
---|
719 | {
|
---|
720 | BOOL ret = False;
|
---|
721 |
|
---|
722 | char *new_sitename;
|
---|
723 |
|
---|
724 | if (!realm || (strlen(realm) == 0)) {
|
---|
725 | DEBUG(0,("stored_sitename_changed: no realm\n"));
|
---|
726 | return False;
|
---|
727 | }
|
---|
728 |
|
---|
729 | new_sitename = sitename_fetch(realm);
|
---|
730 |
|
---|
731 | if (sitename && new_sitename && !strequal(sitename, new_sitename)) {
|
---|
732 | ret = True;
|
---|
733 | } else if ((sitename && !new_sitename) ||
|
---|
734 | (!sitename && new_sitename)) {
|
---|
735 | ret = True;
|
---|
736 | }
|
---|
737 | SAFE_FREE(new_sitename);
|
---|
738 | return ret;
|
---|
739 | }
|
---|
740 |
|
---|
741 | /********************************************************************
|
---|
742 | Query with optional sitename.
|
---|
743 | ********************************************************************/
|
---|
744 |
|
---|
745 | NTSTATUS ads_dns_query_internal(TALLOC_CTX *ctx,
|
---|
746 | const char *servicename,
|
---|
747 | const char *realm,
|
---|
748 | const char *sitename,
|
---|
749 | struct dns_rr_srv **dclist,
|
---|
750 | int *numdcs )
|
---|
751 | {
|
---|
752 | char *name;
|
---|
753 | if (sitename) {
|
---|
754 | name = talloc_asprintf(ctx, "%s._tcp.%s._sites.dc._msdcs.%s",
|
---|
755 | servicename, sitename, realm );
|
---|
756 | } else {
|
---|
757 | name = talloc_asprintf(ctx, "%s._tcp.dc._msdcs.%s",
|
---|
758 | servicename, realm );
|
---|
759 | }
|
---|
760 | if (!name) {
|
---|
761 | return NT_STATUS_NO_MEMORY;
|
---|
762 | }
|
---|
763 | return ads_dns_lookup_srv( ctx, name, dclist, numdcs );
|
---|
764 | }
|
---|
765 |
|
---|
766 | /********************************************************************
|
---|
767 | Query for AD DC's.
|
---|
768 | ********************************************************************/
|
---|
769 |
|
---|
770 | NTSTATUS ads_dns_query_dcs(TALLOC_CTX *ctx,
|
---|
771 | const char *realm,
|
---|
772 | const char *sitename,
|
---|
773 | struct dns_rr_srv **dclist,
|
---|
774 | int *numdcs )
|
---|
775 | {
|
---|
776 | NTSTATUS status;
|
---|
777 |
|
---|
778 | status = ads_dns_query_internal(ctx, "_ldap", realm, sitename,
|
---|
779 | dclist, numdcs);
|
---|
780 |
|
---|
781 | if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
|
---|
782 | NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
|
---|
783 | return status;
|
---|
784 | }
|
---|
785 |
|
---|
786 | if (sitename && !NT_STATUS_IS_OK(status)) {
|
---|
787 | /* Sitename DNS query may have failed. Try without. */
|
---|
788 | status = ads_dns_query_internal(ctx, "_ldap", realm, NULL,
|
---|
789 | dclist, numdcs);
|
---|
790 | }
|
---|
791 | return status;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /********************************************************************
|
---|
795 | Query for AD KDC's.
|
---|
796 | Even if our underlying kerberos libraries are UDP only, this
|
---|
797 | is pretty safe as it's unlikely that a KDC supports TCP and not UDP.
|
---|
798 | ********************************************************************/
|
---|
799 |
|
---|
800 | NTSTATUS ads_dns_query_kdcs(TALLOC_CTX *ctx,
|
---|
801 | const char *realm,
|
---|
802 | const char *sitename,
|
---|
803 | struct dns_rr_srv **dclist,
|
---|
804 | int *numdcs )
|
---|
805 | {
|
---|
806 | NTSTATUS status;
|
---|
807 |
|
---|
808 | status = ads_dns_query_internal(ctx, "_kerberos", realm, sitename,
|
---|
809 | dclist, numdcs);
|
---|
810 |
|
---|
811 | if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
|
---|
812 | NT_STATUS_EQUAL(status, NT_STATUS_CONNECTION_REFUSED)) {
|
---|
813 | return status;
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (sitename && !NT_STATUS_IS_OK(status)) {
|
---|
817 | /* Sitename DNS query may have failed. Try without. */
|
---|
818 | status = ads_dns_query_internal(ctx, "_kerberos", realm, NULL,
|
---|
819 | dclist, numdcs);
|
---|
820 | }
|
---|
821 | return status;
|
---|
822 | }
|
---|