summaryrefslogtreecommitdiffstats
path: root/usr.sbin/amd/amd/info_hes.c
blob: f9a835f3018f9c2a0bc7843a3b5f38c5fd002e41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
 * Copyright (c) 1989 Jan-Simon Pendry
 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Jan-Simon Pendry at Imperial College, London.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)info_hes.c	8.1 (Berkeley) 6/6/93
 *
 * $FreeBSD$
 *
 */

/*
 * Get info from Hesiod
 *
 * Zone transfer code from Bruce Cole <cole@cs.wisc.edu>
 */

#include "am.h"

#ifdef HAS_HESIOD_MAPS
#include <hesiod.h>

#define	HES_PREFIX	"hesiod."
#define	HES_PREFLEN	7

#ifdef HAS_HESIOD_RELOAD
#include <arpa/nameser.h>
#include <resolv.h>
#include <sys/uio.h>
#include <netdb.h>

/*
 * Patch up broken system include files
 */
#ifndef C_HS
#define C_HS	4
#endif
#ifndef T_TXT
#define	T_TXT	16
#endif

static int soacnt;
static struct timeval hs_timeout;
static int servernum;
#endif /* HAS_HESIOD_RELOAD */

/*
 * No easy way to probe the server - check the map name begins with "hesiod."
 */
int hesiod_init P((char *map, time_t *tp));
int hesiod_init(map, tp)
char *map;
time_t *tp;
{
#ifdef DEBUG
	dlog("hesiod_init(%s)", map);
#endif
	*tp = 0;
	return strncmp(map, HES_PREFIX, HES_PREFLEN) == 0 ? 0 : ENOENT;
}


/*
 * Make Hesiod name.  Skip past the "hesiod."
 * at the start of the map name and append
 * ".automount".  The net effect is that a lookup
 * of /defaults in hesiod.home will result in a
 * call to hes_resolve("/defaults", "home.automount");
 */
#ifdef notdef
#define MAKE_HES_NAME(dest, src) sprintf(dest, "%s%s", src + HES_PREFLEN, ".automount")
#endif

/*
 * Do a Hesiod nameserver call.
 * Modify time is ignored by Hesiod - XXX
 */
int hesiod_search P((mnt_map *m, char *map, char **pval, time_t *tp));
int hesiod_search(m, map, key, pval, tp)
mnt_map *m;
char *map;
char *key;
char **pval;
time_t *tp;
{
	int error;
	char hes_key[MAXPATHLEN];
	char **rvec;
#ifdef DEBUG
	dlog("hesiod_search(m=%x, map=%s, key=%s, pval=%x tp=%x)", m, map, key, pval, tp);
#endif
	/*MAKE_HES_NAME(hes_map, map);*/
	sprintf(hes_key, "%s.%s", key, map+HES_PREFLEN);

	/*
	 * Call the resolver
	 */
#ifdef DEBUG
	dlog("hesiod_search: hes_resolve(%s, %s)", hes_key, "automount");
#ifdef HAS_HESIOD_RELOAD
	if (debug_flags & D_FULL)
		_res.options |= RES_DEBUG;
#endif
#endif
	rvec = hes_resolve(hes_key, "automount");
	/*
	 * If a reply was forthcoming then return
	 * it (and free subsequent replies)
	 */
	if (rvec && *rvec) {
		*pval = *rvec;
		while (*++rvec)
			free(*rvec);
		return 0;
	}

	/*
	 * Otherwise reflect the hesiod error into a Un*x error
	 */
#ifdef DEBUG
	dlog("hesiod_search: Error: %d", hes_error());
#endif
	switch (hes_error()) {
	case HES_ER_NOTFOUND:	error = ENOENT; break;
	case HES_ER_CONFIG:	error = EIO; break;
	case HES_ER_NET:	error = ETIMEDOUT; break;
	default:		error = EINVAL; break;
	}
#ifdef DEBUG
	dlog("hesiod_search: Returning: %d", error);
#endif
	return error;
}

#ifdef HAS_HESIOD_RELOAD
/*
 * Zone transfer...
 */

#define MAXHSNS 8
#define MAX_NSADDR 16

static char *hs_domain;
static mnt_map *hs_map;
static int hs_nscount;
static char nsaddr_list[MAX_NSADDR][sizeof(struct in_addr)];

int hesiod_reload P((mnt_map *m, char *map, void (*fn)()));
int hesiod_reload(m, map, fn)
mnt_map *m;
char *map;
void (*fn)();
{
	char *zone_name, *cp;
	short domainlen;
	int status;

#ifdef DEBUG
	dlog("hesiod_reload (%x %s %x)", m, map, fn);
#endif DEBUG
	if (status = res_init()) {
#ifdef DEBUG
		dlog("hesiod_reload: res_init failed with %d", status);
#endif
		return(status);
	}
	_res.retrans = 90;
	hs_map = m;
	domainlen = strlen(hostdomain);
	zone_name = hes_to_bind(map+HES_PREFLEN, "automount");
	if (*zone_name == '.')
		zone_name++;
	hs_domain = zone_name;
	/* Traverse the DNS tree until we find an SOA we can transfer from.
	 (Our initial zone_name is likely to just be a subtree of a
	 real zone). */
	do {
		/* If we can't find any NS records, go up a level in the
		   DNS tree */
		if (hs_get_ns_list(zone_name) == 0 &&
		    hs_zone_transfer(zone_name) == 0)
			return(0);
		/* Move up DNS tree by one component */
		if (cp = strchr(zone_name, '.'))
			zone_name = ++cp;
		else
			break;
	} while (strlen(zone_name) >= domainlen);
#ifdef DEBUG
	dlog("hesiod_reload: Giving up on %s", hs_domain);
#endif
	return(-1);
}

hs_zone_transfer(domain)
char *domain;
{
	int status, len;
	char buf[PACKETSZ];
	/* Want to make sure ansbuf is well alligned */
	long ansbuf[PACKETSZ/sizeof(long)];

#ifdef DEBUG
	dlog("hs_zone_transfer (%s)", domain);
#endif
	if ((len = res_mkquery(QUERY, domain, C_HS, T_AXFR,
			       (char *)NULL, 0, NULL, buf, PACKETSZ)) == -1) {
#ifdef DEBUG
		dlog("hs_zone_transfer: res_mkquery failed");
#endif
		errno = 0;
		return(-1);
	}
	if ((status = hs_res_send(buf, len, (char *)ansbuf, PACKETSZ)) == -1) {
#ifdef DEBUG
	    dlog("hs_zone_transfer: hs_res_send failed.  status %d errno %d",
		 status, errno);
#endif
		errno = 0;
		return(-1);
	}
	return(0);
}

#define hs_server_addr(ns) ((struct in_addr *) nsaddr_list[ns])

hs_res_send(buf, buflen, answer, anslen)
char *buf;
int buflen;
char *answer;
int anslen;
{
	int retry, ns;
	u_short id, len;
	HEADER *hp = (HEADER *) buf;
	struct iovec iov[2];
	static int s = -1;
	int status;
	struct sockaddr_in server;

	soacnt = 0;
	id = hp->id;
	/*
	 * Send request, RETRY times, or until successful
	 */
	for (retry = _res.retry; retry > 0; retry--) {
		for (ns = 0; ns < hs_nscount; ns++) {
			hs_timeout.tv_sec =
				(_res.retrans << (_res.retry - retry))
				/ hs_nscount;
			if (hs_timeout.tv_sec <= 0)
				hs_timeout.tv_sec = 1;
			hs_timeout.tv_usec = 0;
			if (s < 0) {
				s = socket(AF_INET, SOCK_STREAM, 0);
				if (s < 0) {
					continue;
				}
				servernum = ns;
				bcopy(hs_server_addr(ns), &server.sin_addr,
				      sizeof(struct in_addr));
				server.sin_family = AF_INET;
				server.sin_port = htons(NAMESERVER_PORT);

				if (connect(s, &server,
					    sizeof(struct sockaddr)) < 0) {
					(void) close(s);
					s = -1;
					continue;
				}
			}
			/*
			 * Send length & message
			 */
			len = htons((u_short)buflen);
			iov[0].iov_base = (caddr_t)&len;
			iov[0].iov_len = sizeof(len);
			iov[1].iov_base = buf;
			iov[1].iov_len = buflen;
			if (writev(s, iov, 2) != sizeof(len) + buflen) {
				(void) close(s);
				s = -1;
				continue;
			}
			status = 0;
			while (s != -1 && soacnt < 2 && status != -2) {
				if ((status =
				     hs_readresp(s, answer, anslen)) == -1) {
					(void) close(s);
					s = -1;
					continue;
				}
			}
			if (status == -2) {
				/* There was a permanent error transfering this
				   zone.  Give up. */
				if (s != -1) {
					(void) close(s);
					s = -1;
				}
				return(-1);
			}
			if (s == -1)
				continue;
			return (0);
		}
	}
	if (errno == 0)
		errno = ETIMEDOUT;
	return (-1);
}

/* Returns:
   0: Success
   -1: Error
   -2: Permanent failure
*/
hs_readresp(s, answer, anslen)
int s;
char *answer;
int anslen;
{
	register int len, n;
	char *cp;

	cp = answer;
	len = sizeof(short);
	while (len != 0 &&
	       (n = hs_res_vcread(s, (char *)cp, (int)len, &hs_timeout)) > 0) {
		cp += n;
		len -= n;
	}
	if (n <= 0)
		return(-1);
	cp = answer;
	if ((len = _getshort(cp)) > anslen) {
#ifdef DEBUG
		dlog("hs_readresp: response too long: %d", len);
#endif
		return(-1);
	}
	while (len != 0 &&
	       (n = hs_res_vcread(s, (char *)cp, (int)len, &hs_timeout)) > 0) {
		cp += n;
		len -= n;
	}
	if (n <= 0)
		return(-1);
	return(hs_parse(answer, answer+PACKETSZ));
}

hs_res_vcread(sock, buf, buflen, timeout)
int sock, buflen;
char *buf;
struct timeval *timeout;
{
	register int n;

	if ((n = hs_res_selwait(sock, timeout)) > 0)
		return(read(sock, buf, buflen));
	else
		return(n);
}

hs_res_selwait(sock, timeout)
int sock;
struct timeval *timeout;
{
	fd_set dsmask;
	register int n;

	/*
	 * Wait for reply
	 */
	FD_ZERO(&dsmask);
	FD_SET(sock, &dsmask);
	n = select(sock+1, &dsmask, (fd_set *)NULL,
		   (fd_set *)NULL, timeout);
	return(n);
}

/* Returns:
   0: Success
   -1: Error
   -2: Permanent failure
*/
hs_parse(msg, eom)
char *msg, *eom;
{
	register char *cp;
	register HEADER *hp;
	register int n, len;
	int qdcount, ancount;
	char key[PACKETSZ];
	char *key_cpy, *value, *hs_make_value();
	short type;

	hp = (HEADER *)msg;
	if (hp->rcode != NOERROR || hp->opcode != QUERY) {
		char dq[20];
#ifdef DEBUG
		dlog("Bad response (%d) from nameserver %s", hp->rcode, inet_dquad(dq, hs_server_addr(servernum)->s_addr));
#endif DEBUG
		return(-1);
	}
	cp = msg + sizeof(HEADER);
	ancount = ntohs(hp->ancount);
	qdcount = ntohs(hp->qdcount);
	while (qdcount-- > 0)
		cp += dn_skipname(cp, eom) + QFIXEDSZ;
	if (soacnt == 0 && ancount == 0) {
		/* XXX We should look for NS records to find SOA */
#ifdef DEBUG
		dlog("No SOA found");
#endif
		return(-2);
	}
	while (ancount-- > 0 && cp < eom) {
		if ((n = dn_expand(msg, eom, cp, key, PACKETSZ)) < 0)
			break;
		cp += n;
		if ((type = _getshort(cp)) == T_SOA) {
			soacnt++;
		}
		cp += 2*sizeof(u_short) + sizeof(u_long);
		len = _getshort(cp);
		cp += sizeof(u_short);
		/* Check to see if key is in our domain */
		if (type == T_TXT && hs_strip_our_domain(key)) {
			value = hs_make_value(cp, len);
			if (value == NULL)
				return(-1);
			key_cpy = strdup(key);
#ifdef DEBUG
			dlog("hs_parse: Parsed key: %s, value: %s", key,
			     value);
#endif
			mapc_add_kv(hs_map, key_cpy, value);
		}
		cp += len;
		errno = 0;
	}
	return(0);
}

/* Check to see if the domain name in the supplied argument matches
   hs_domain.  Strip hs_domain from supplied argument if so. */
hs_strip_our_domain(name)
char *name;
{
	char *end_pos;
	short targ_len, cur_len;

	targ_len = strlen(hs_domain);
	cur_len = strlen(name);
	if (cur_len <= targ_len)
		return(0);
	end_pos = &name[cur_len - targ_len];
	if (strcmp(end_pos, hs_domain) != 0)
		return(0);
	if (*--end_pos != '.')
		return(0);
	*end_pos = '\0';
	return(1);
}

#define MAXDATA 8*1024

char *
hs_make_value(cp, len)
char *cp;
int len;
{
	char *value, *cpcpy, *valuep;
	int cnt, nextcnt, totalcnt, lencpy;
#ifdef DEBUG
	char *dbgname;

	dbgname = &cp[1];
#endif DEBUG

	lencpy = len;
	cpcpy = cp;
	totalcnt = 0;
	cnt = *cpcpy++;
	while (cnt) {
		totalcnt += cnt;
		lencpy -= cnt+1;
		if (lencpy == 0)
			break;
		nextcnt = cpcpy[cnt];
		cpcpy = &cpcpy[cnt+1];
		cnt = nextcnt;
	}
	if (totalcnt < 1 || totalcnt > MAXDATA || totalcnt > len) {
#ifdef DEBUG
		dlog("TXT RR not of expected length (%d %d): %s", totalcnt,
		     len, dbgname);
#endif DEBUG
		return(NULL);
	}
	/* Allocate null terminated string */
	value = (char *) xmalloc(totalcnt+1);
	value[totalcnt] = '\0';
	cnt = *cp++;
	valuep = value;
	while (cnt) {
		bcopy(cp, valuep, cnt);
		len -= cnt+1;
		if (len == 0)
			break;
		valuep = &valuep[cnt];
		nextcnt = cp[cnt];
		cp = &cp[cnt+1];
		cnt = nextcnt;
	}
	return(value);
}

hs_make_ns_query(domain, ansbuf)
char *domain;
char *ansbuf;
{
	int status, len;
	char buf[PACKETSZ];

	if ((len = res_mkquery(QUERY, domain, C_HS, T_NS,
			       (char *)NULL, 0, NULL, buf, PACKETSZ)) == -1) {
#ifdef DEBUG
		dlog("hs_get_ns_list: res_mkquery failed");
#endif
		errno = 0;
		return(-1);
	}
	if ((status = res_send(buf, len, (char *)ansbuf, PACKETSZ)) == -1) {
#ifdef DEBUG
	    dlog("hs_get_ns_list: res_send failed.  status %d errno %d",
		 status, errno);
#endif
		errno = 0;
		return(-1);
	}
	return(0);
}

static void
add_address(addr)
struct in_addr *addr;
{
	char dq[20];
	bcopy((char *)addr, nsaddr_list[hs_nscount++], sizeof(struct in_addr));
#ifdef DEBUG
	dlog("Adding NS address %s", inet_dquad(dq, addr->s_addr));
#endif DEBUG
}

hs_get_ns_list(domain)
char *domain;
{
	register HEADER *hp;
	int qdcount, nscount;
	register char *cp;
	register int n, len;
	char key[PACKETSZ], name[PACKETSZ], msg[PACKETSZ], *eom;
	register long **hptr;
	struct hostent *ghp;
	int numns;
	char nsname[MAXHSNS][MAXDATA];
	int nshaveaddr[MAXHSNS], i;
	short type;

	if (hs_make_ns_query(domain, msg) == -1)
		return(-1);
	numns = hs_nscount = 0;
	eom = &msg[PACKETSZ];
	bzero(nsname, sizeof(nsname));
	hp = (HEADER *)msg;
	if (hp->rcode != NOERROR || hp->opcode != QUERY) {
#ifdef DEBUG
		dlog("Bad response (%d) from nameserver %#x", hp->rcode,
		      hs_server_addr(servernum)->s_addr);
#endif DEBUG
		return(-1);
	}
	cp = msg + sizeof(HEADER);
	qdcount = ntohs(hp->qdcount);
	while (qdcount-- > 0)
		cp += dn_skipname(cp, eom) + QFIXEDSZ;
	nscount = ntohs(hp->ancount) + ntohs(hp->nscount) + ntohs(hp->arcount);
#ifdef DEBUG
	dlog("hs_get_ns_list: Processing %d response records", nscount);
#endif
	for (;nscount; nscount--) {
		if ((n = dn_expand(msg, eom, cp, key, PACKETSZ)) < 0)
			break;
		cp += n;
		type = _getshort(cp);
		cp += 2*sizeof(u_short) + sizeof(u_long);
		len = _getshort(cp);
		cp += sizeof(u_short);
#ifdef DEBUG
		dlog("hs_get_ns_list: Record type: %d", type);
#endif
		switch (type) {
		case T_NS:
			if (numns >= MAXHSNS || strcasecmp(domain, key) != 0)
				break;
			if ((n = dn_expand(msg, eom, cp, name, PACKETSZ)) < 0)
				break;
#ifdef DEBUG
			dlog("hs_get_ns_list: NS name: %s", name);
#endif
			for (i = 0; i < numns; i++)
				if (strcasecmp(nsname[i], name) == 0)
					break;
			if (i == numns) {
#ifdef DEBUG
				dlog("hs_get_ns_list: Saving name %s", name);
#endif
				strncpy(nsname[numns], name, MAXDATA);
				nshaveaddr[numns] = 0;
				numns++;
			}
			break;
		case T_A:
			if (hs_nscount == MAX_NSADDR)
				break;
			for (i = 0; i < numns; i++) {
				if (strcasecmp(nsname[i], domain) == 0) {
					nshaveaddr[i]++;
					add_address((struct in_addr *) cp);
					break;
				}
			}
			break;
		default:
			break;
		}
		if (hs_nscount == MAX_NSADDR)
			break;
		cp += len;
		errno = 0;
	}
#ifdef  DEBUG
	dlog("hs_get_ns_list: Found %d NS records", numns);
#endif
	for (i = 0; i < numns; i++) {
		if (nshaveaddr[i])
			continue;
		if ((ghp = gethostbyname(nsname[i])) == 0)
			continue;
		for (hptr = (long **)ghp->h_addr_list;
		     *hptr && hs_nscount < MAX_NSADDR; hptr++) {
			add_address((struct in_addr *) *hptr);
		}
	}
	if (hs_nscount)
		return(0);
#ifdef DEBUG
	dlog("No NS records found for %s", domain);
	return(-1);
#endif DEBUG
}
#endif /* HAS_HESIOD_RELOAD */
#endif /* HAS_HESIOD_MAPS */
OpenPOWER on IntegriCloud