xref: /netbsd/lib/libresolv/res_mkupdate.c (revision d0f9e065)
1*d0f9e065Sandvar /*	$NetBSD: res_mkupdate.c,v 1.3 2022/06/09 14:02:30 andvar Exp $	*/
2446e541fSchristos 
3446e541fSchristos /*
4446e541fSchristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5446e541fSchristos  * Copyright (c) 1996-1999 by Internet Software Consortium.
6446e541fSchristos  *
7446e541fSchristos  * Permission to use, copy, modify, and distribute this software for any
8446e541fSchristos  * purpose with or without fee is hereby granted, provided that the above
9446e541fSchristos  * copyright notice and this permission notice appear in all copies.
10446e541fSchristos  *
11446e541fSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12446e541fSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13446e541fSchristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14446e541fSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15446e541fSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16446e541fSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17446e541fSchristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18446e541fSchristos  */
19446e541fSchristos 
20446e541fSchristos /*! \file
21446e541fSchristos  * \brief
22446e541fSchristos  * Based on the Dynamic DNS reference implementation by Viraj Bais
23446e541fSchristos  * <viraj_bais@ccm.fm.intel.com>
24446e541fSchristos  */
25446e541fSchristos #include <sys/cdefs.h>
26446e541fSchristos #if 0
27446e541fSchristos static const char rcsid[] = "Id: res_mkupdate.c,v 1.10 2008/12/11 09:59:00 marka Exp ";
28446e541fSchristos #else
29*d0f9e065Sandvar __RCSID("$NetBSD: res_mkupdate.c,v 1.3 2022/06/09 14:02:30 andvar Exp $");
30446e541fSchristos #endif
31446e541fSchristos 
32446e541fSchristos #include "port_before.h"
33446e541fSchristos 
34446e541fSchristos #include <sys/types.h>
35446e541fSchristos #include <sys/param.h>
36446e541fSchristos 
37446e541fSchristos #include <netinet/in.h>
38446e541fSchristos #include <arpa/nameser.h>
39446e541fSchristos #include <arpa/inet.h>
40446e541fSchristos 
41446e541fSchristos #include <errno.h>
42446e541fSchristos #include <limits.h>
43446e541fSchristos #include <netdb.h>
44446e541fSchristos #include <resolv.h>
45446e541fSchristos #include <res_update.h>
46446e541fSchristos #include <stdio.h>
47446e541fSchristos #include <stdlib.h>
48446e541fSchristos #include <string.h>
49446e541fSchristos #include <unistd.h>
50446e541fSchristos #include <ctype.h>
51446e541fSchristos 
52446e541fSchristos #include "port_after.h"
53446e541fSchristos 
54446e541fSchristos /* Options.  Leave them on. */
55446e541fSchristos #define MAXPORT 1024
56446e541fSchristos 
57446e541fSchristos static int getnum_str(u_char **, u_char *);
58446e541fSchristos static int gethexnum_str(u_char **, u_char *);
59446e541fSchristos static int getword_str(char *, size_t, u_char **, u_char *);
60446e541fSchristos static int getstr_str(char *, size_t, u_char **, u_char *);
61446e541fSchristos 
62446e541fSchristos #define ShrinkBuffer(x)  if ((buflen -= x) < 0) return (-2);
63446e541fSchristos 
64446e541fSchristos /* Forward. */
65446e541fSchristos 
66446e541fSchristos int res_protocolnumber(const char *);
67446e541fSchristos int res_servicenumber(const char *);
68446e541fSchristos 
69446e541fSchristos /*%
70446e541fSchristos  * Form update packets.
71446e541fSchristos  * Returns the size of the resulting packet if no error
72446e541fSchristos  *
73446e541fSchristos  * On error,
74446e541fSchristos  *	returns
75446e541fSchristos  *\li              -1 if error in reading a word/number in rdata
76446e541fSchristos  *		   portion for update packets
77446e541fSchristos  *\li		-2 if length of buffer passed is insufficient
78446e541fSchristos  *\li		-3 if zone section is not the first section in
79446e541fSchristos  *		   the linked list, or section order has a problem
80446e541fSchristos  *\li		-4 on a number overflow
81446e541fSchristos  *\li		-5 unknown operation or no records
82446e541fSchristos  */
83446e541fSchristos int
res_nmkupdate(res_state statp,ns_updrec * rrecp_in,u_char * buf,int buflen)84446e541fSchristos res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
85446e541fSchristos 	ns_updrec *rrecp_start = rrecp_in;
86446e541fSchristos 	HEADER *hp;
87446e541fSchristos 	u_char *cp, *sp2, *startp, *endp;
88446e541fSchristos 	int n, i, soanum, multiline;
89446e541fSchristos 	ns_updrec *rrecp;
90446e541fSchristos 	struct in_addr ina;
91446e541fSchristos 	struct in6_addr in6a;
92446e541fSchristos         char buf2[MAXDNAME];
93446e541fSchristos 	u_char buf3[MAXDNAME];
94446e541fSchristos 	int section, numrrs = 0, counts[ns_s_max];
95446e541fSchristos 	u_int16_t rtype, rclass;
96446e541fSchristos 	u_int32_t n1, rttl;
97446e541fSchristos 	u_char *dnptrs[20], **dpp, **lastdnptr;
98446e541fSchristos 	int siglen, keylen, certlen;
99446e541fSchristos 
100446e541fSchristos 	/*
101446e541fSchristos 	 * Initialize header fields.
102446e541fSchristos 	 */
103446e541fSchristos 	if ((buf == NULL) || (buflen < HFIXEDSZ))
104446e541fSchristos 		return (-1);
105446e541fSchristos 	memset(buf, 0, HFIXEDSZ);
106446e541fSchristos 	hp = (void *)buf;
107446e541fSchristos 	statp->id = res_nrandomid(statp);
108446e541fSchristos 	hp->id = htons(statp->id);
109446e541fSchristos 	hp->opcode = ns_o_update;
110446e541fSchristos 	hp->rcode = NOERROR;
111446e541fSchristos 	cp = buf + HFIXEDSZ;
112446e541fSchristos 	buflen -= HFIXEDSZ;
113446e541fSchristos 	dpp = dnptrs;
114446e541fSchristos 	*dpp++ = buf;
115446e541fSchristos 	*dpp++ = NULL;
116446e541fSchristos 	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
117446e541fSchristos 
118446e541fSchristos 	if (rrecp_start == NULL)
119446e541fSchristos 		return (-5);
120446e541fSchristos 	else if (rrecp_start->r_section != S_ZONE)
121446e541fSchristos 		return (-3);
122446e541fSchristos 
123446e541fSchristos 	memset(counts, 0, sizeof counts);
124446e541fSchristos 	for (rrecp = rrecp_start; rrecp; rrecp = TAILQ_NEXT(rrecp, r_glink)) {
125446e541fSchristos 		numrrs++;
126446e541fSchristos                 section = rrecp->r_section;
127446e541fSchristos 		if (section < 0 || section >= ns_s_max)
128446e541fSchristos 			return (-1);
129446e541fSchristos 		counts[section]++;
130446e541fSchristos 		for (i = section + 1; i < ns_s_max; i++)
131446e541fSchristos 			if (counts[i])
132446e541fSchristos 				return (-3);
133446e541fSchristos 		rtype = rrecp->r_type;
134446e541fSchristos 		rclass = rrecp->r_class;
135446e541fSchristos 		rttl = rrecp->r_ttl;
136446e541fSchristos 		/* overload class and type */
137446e541fSchristos 		if (section == S_PREREQ) {
138446e541fSchristos 			rttl = 0;
139446e541fSchristos 			switch (rrecp->r_opcode) {
140446e541fSchristos 			case YXDOMAIN:
141446e541fSchristos 				rclass = C_ANY;
142446e541fSchristos 				rtype = T_ANY;
143446e541fSchristos 				rrecp->r_size = 0;
144446e541fSchristos 				break;
145446e541fSchristos 			case NXDOMAIN:
146446e541fSchristos 				rclass = C_NONE;
147446e541fSchristos 				rtype = T_ANY;
148446e541fSchristos 				rrecp->r_size = 0;
149446e541fSchristos 				break;
150446e541fSchristos 			case NXRRSET:
151446e541fSchristos 				rclass = C_NONE;
152446e541fSchristos 				rrecp->r_size = 0;
153446e541fSchristos 				break;
154446e541fSchristos 			case YXRRSET:
155446e541fSchristos 				if (rrecp->r_size == 0)
156446e541fSchristos 					rclass = C_ANY;
157446e541fSchristos 				break;
158446e541fSchristos 			default:
159446e541fSchristos 				fprintf(stderr,
160446e541fSchristos 					"res_mkupdate: incorrect opcode: %d\n",
161446e541fSchristos 					rrecp->r_opcode);
162446e541fSchristos 				fflush(stderr);
163446e541fSchristos 				return (-1);
164446e541fSchristos 			}
165446e541fSchristos 		} else if (section == S_UPDATE) {
166446e541fSchristos 			switch (rrecp->r_opcode) {
167446e541fSchristos 			case DELETE:
168446e541fSchristos 				rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
169446e541fSchristos 				break;
170446e541fSchristos 			case ADD:
171446e541fSchristos 				break;
172446e541fSchristos 			default:
173446e541fSchristos 				fprintf(stderr,
174446e541fSchristos 					"res_mkupdate: incorrect opcode: %d\n",
175446e541fSchristos 					rrecp->r_opcode);
176446e541fSchristos 				fflush(stderr);
177446e541fSchristos 				return (-1);
178446e541fSchristos 			}
179446e541fSchristos 		}
180446e541fSchristos 
181446e541fSchristos 		/*
182446e541fSchristos 		 * XXX	appending default domain to owner name is omitted,
183446e541fSchristos 		 *	fqdn must be provided
184446e541fSchristos 		 */
185446e541fSchristos 		if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
186446e541fSchristos 				 lastdnptr)) < 0)
187446e541fSchristos 			return (-1);
188446e541fSchristos 		cp += n;
189446e541fSchristos 		ShrinkBuffer(n + 2*INT16SZ);
190446e541fSchristos 		PUTSHORT(rtype, cp);
191446e541fSchristos 		PUTSHORT(rclass, cp);
192446e541fSchristos 		if (section == S_ZONE) {
193446e541fSchristos 			if (numrrs != 1 || rrecp->r_type != T_SOA)
194446e541fSchristos 				return (-3);
195446e541fSchristos 			continue;
196446e541fSchristos 		}
197446e541fSchristos 		ShrinkBuffer(INT32SZ + INT16SZ);
198446e541fSchristos 		PUTLONG(rttl, cp);
199446e541fSchristos 		sp2 = cp;  /*%< save pointer to length byte */
200446e541fSchristos 		cp += INT16SZ;
201446e541fSchristos 		if (rrecp->r_size == 0) {
202446e541fSchristos 			if (section == S_UPDATE && rclass != C_ANY)
203446e541fSchristos 				return (-1);
204446e541fSchristos 			else {
205446e541fSchristos 				PUTSHORT(0, sp2);
206446e541fSchristos 				continue;
207446e541fSchristos 			}
208446e541fSchristos 		}
209446e541fSchristos 		startp = rrecp->r_data;
210446e541fSchristos 		endp = startp + rrecp->r_size - 1;
211446e541fSchristos 		/* XXX this should be done centrally. */
212446e541fSchristos 		switch (rrecp->r_type) {
213446e541fSchristos 		case T_A:
214446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
215446e541fSchristos 				return (-1);
216446e541fSchristos 			if (!inet_aton(buf2, &ina))
217446e541fSchristos 				return (-1);
218446e541fSchristos 			n1 = ntohl(ina.s_addr);
219446e541fSchristos 			ShrinkBuffer(INT32SZ);
220446e541fSchristos 			PUTLONG(n1, cp);
221446e541fSchristos 			break;
222446e541fSchristos 		case T_CNAME:
223446e541fSchristos 		case T_MB:
224446e541fSchristos 		case T_MG:
225446e541fSchristos 		case T_MR:
226446e541fSchristos 		case T_NS:
227446e541fSchristos 		case T_PTR:
228446e541fSchristos 		case ns_t_dname:
229446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
230446e541fSchristos 				return (-1);
231446e541fSchristos 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
232446e541fSchristos 			if (n < 0)
233446e541fSchristos 				return (-1);
234446e541fSchristos 			cp += n;
235446e541fSchristos 			ShrinkBuffer(n);
236446e541fSchristos 			break;
237446e541fSchristos 		case T_MINFO:
238446e541fSchristos 		case T_SOA:
239446e541fSchristos 		case T_RP:
240446e541fSchristos 			for (i = 0; i < 2; i++) {
241446e541fSchristos 				if (!getword_str(buf2, sizeof buf2, &startp,
242446e541fSchristos 						 endp))
243446e541fSchristos 				return (-1);
244446e541fSchristos 				n = dn_comp(buf2, cp, buflen,
245446e541fSchristos 					    dnptrs, lastdnptr);
246446e541fSchristos 				if (n < 0)
247446e541fSchristos 					return (-1);
248446e541fSchristos 				cp += n;
249446e541fSchristos 				ShrinkBuffer(n);
250446e541fSchristos 			}
251446e541fSchristos 			if (rrecp->r_type == T_SOA) {
252446e541fSchristos 				ShrinkBuffer(5 * INT32SZ);
253446e541fSchristos 				while (isspace(*startp) || !*startp)
254446e541fSchristos 					startp++;
255446e541fSchristos 				if (*startp == '(') {
256446e541fSchristos 					multiline = 1;
257446e541fSchristos 					startp++;
258446e541fSchristos 				} else
259446e541fSchristos 					multiline = 0;
260446e541fSchristos 				/* serial, refresh, retry, expire, minimum */
261446e541fSchristos 				for (i = 0; i < 5; i++) {
262446e541fSchristos 					soanum = getnum_str(&startp, endp);
263446e541fSchristos 					if (soanum < 0)
264446e541fSchristos 						return (-1);
265446e541fSchristos 					PUTLONG(soanum, cp);
266446e541fSchristos 				}
267446e541fSchristos 				if (multiline) {
268446e541fSchristos 					while (isspace(*startp) || !*startp)
269446e541fSchristos 						startp++;
270446e541fSchristos 					if (*startp != ')')
271446e541fSchristos 						return (-1);
272446e541fSchristos 				}
273446e541fSchristos 			}
274446e541fSchristos 			break;
275446e541fSchristos 		case T_MX:
276446e541fSchristos 		case T_AFSDB:
277446e541fSchristos 		case T_RT:
278446e541fSchristos 			n = getnum_str(&startp, endp);
279446e541fSchristos 			if (n < 0)
280446e541fSchristos 				return (-1);
281446e541fSchristos 			ShrinkBuffer(INT16SZ);
282446e541fSchristos 			PUTSHORT(n, cp);
283446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
284446e541fSchristos 				return (-1);
285446e541fSchristos 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
286446e541fSchristos 			if (n < 0)
287446e541fSchristos 				return (-1);
288446e541fSchristos 			cp += n;
289446e541fSchristos 			ShrinkBuffer(n);
290446e541fSchristos 			break;
291446e541fSchristos 		case T_SRV:
292446e541fSchristos 			n = getnum_str(&startp, endp);
293446e541fSchristos 			if (n < 0)
294446e541fSchristos 				return (-1);
295446e541fSchristos 			ShrinkBuffer(INT16SZ);
296446e541fSchristos 			PUTSHORT(n, cp);
297446e541fSchristos 
298446e541fSchristos 			n = getnum_str(&startp, endp);
299446e541fSchristos 			if (n < 0)
300446e541fSchristos 				return (-1);
301446e541fSchristos 			ShrinkBuffer(INT16SZ);
302446e541fSchristos 			PUTSHORT(n, cp);
303446e541fSchristos 
304446e541fSchristos 			n = getnum_str(&startp, endp);
305446e541fSchristos 			if (n < 0)
306446e541fSchristos 				return (-1);
307446e541fSchristos 			ShrinkBuffer(INT16SZ);
308446e541fSchristos 			PUTSHORT(n, cp);
309446e541fSchristos 
310446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
311446e541fSchristos 				return (-1);
312446e541fSchristos 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
313446e541fSchristos 			if (n < 0)
314446e541fSchristos 				return (-1);
315446e541fSchristos 			cp += n;
316446e541fSchristos 			ShrinkBuffer(n);
317446e541fSchristos 			break;
318446e541fSchristos 		case T_PX:
319446e541fSchristos 			n = getnum_str(&startp, endp);
320446e541fSchristos 			if (n < 0)
321446e541fSchristos 				return (-1);
322446e541fSchristos 			PUTSHORT(n, cp);
323446e541fSchristos 			ShrinkBuffer(INT16SZ);
324446e541fSchristos 			for (i = 0; i < 2; i++) {
325446e541fSchristos 				if (!getword_str(buf2, sizeof buf2, &startp,
326446e541fSchristos 						 endp))
327446e541fSchristos 					return (-1);
328446e541fSchristos 				n = dn_comp(buf2, cp, buflen, dnptrs,
329446e541fSchristos 					    lastdnptr);
330446e541fSchristos 				if (n < 0)
331446e541fSchristos 					return (-1);
332446e541fSchristos 				cp += n;
333446e541fSchristos 				ShrinkBuffer(n);
334446e541fSchristos 			}
335446e541fSchristos 			break;
336446e541fSchristos 		case T_WKS: {
337446e541fSchristos 			char bm[MAXPORT/8];
338446e541fSchristos 			unsigned int maxbm = 0;
339446e541fSchristos 
340446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
341446e541fSchristos 				return (-1);
342446e541fSchristos 			if (!inet_aton(buf2, &ina))
343446e541fSchristos 				return (-1);
344446e541fSchristos 			n1 = ntohl(ina.s_addr);
345446e541fSchristos 			ShrinkBuffer(INT32SZ);
346446e541fSchristos 			PUTLONG(n1, cp);
347446e541fSchristos 
348446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
349446e541fSchristos 				return (-1);
350446e541fSchristos 			if ((i = res_protocolnumber(buf2)) < 0)
351446e541fSchristos 				return (-1);
352446e541fSchristos 			ShrinkBuffer(1);
353446e541fSchristos 			*cp++ = i & 0xff;
354446e541fSchristos 
355446e541fSchristos 			for (i = 0; i < MAXPORT/8 ; i++)
356446e541fSchristos 				bm[i] = 0;
357446e541fSchristos 
358446e541fSchristos 			while (getword_str(buf2, sizeof buf2, &startp, endp)) {
359446e541fSchristos 				if ((n = res_servicenumber(buf2)) <= 0)
360446e541fSchristos 					return (-1);
361446e541fSchristos 
362446e541fSchristos 				if (n < MAXPORT) {
363446e541fSchristos 					bm[n/8] |= (0x80>>(n%8));
364446e541fSchristos 					if ((unsigned)n > maxbm)
365446e541fSchristos 						maxbm = n;
366446e541fSchristos 				} else
367446e541fSchristos 					return (-1);
368446e541fSchristos 			}
369446e541fSchristos 			maxbm = maxbm/8 + 1;
370446e541fSchristos 			ShrinkBuffer(maxbm);
371446e541fSchristos 			memcpy(cp, bm, maxbm);
372446e541fSchristos 			cp += maxbm;
373446e541fSchristos 			break;
374446e541fSchristos 		}
375446e541fSchristos 		case T_HINFO:
376446e541fSchristos 			for (i = 0; i < 2; i++) {
377446e541fSchristos 				if ((n = getstr_str(buf2, sizeof buf2,
378446e541fSchristos 						&startp, endp)) < 0)
379446e541fSchristos 					return (-1);
380446e541fSchristos 				if (n > 255)
381446e541fSchristos 					return (-1);
382446e541fSchristos 				ShrinkBuffer(n+1);
383446e541fSchristos 				*cp++ = n;
384446e541fSchristos 				memcpy(cp, buf2, n);
385446e541fSchristos 				cp += n;
386446e541fSchristos 			}
387446e541fSchristos 			break;
388446e541fSchristos 		case T_TXT:
389446e541fSchristos 			for (;;) {
390446e541fSchristos 				if ((n = getstr_str(buf2, sizeof buf2,
391446e541fSchristos 						&startp, endp)) < 0) {
392446e541fSchristos 					if (cp != (sp2 + INT16SZ))
393446e541fSchristos 						break;
394446e541fSchristos 					return (-1);
395446e541fSchristos 				}
396446e541fSchristos 				if (n > 255)
397446e541fSchristos 					return (-1);
398446e541fSchristos 				ShrinkBuffer(n+1);
399446e541fSchristos 				*cp++ = n;
400446e541fSchristos 				memcpy(cp, buf2, n);
401446e541fSchristos 				cp += n;
402446e541fSchristos 			}
403446e541fSchristos 			break;
404446e541fSchristos 		case T_X25:
405446e541fSchristos 			/* RFC1183 */
406446e541fSchristos 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
407446e541fSchristos 					 endp)) < 0)
408446e541fSchristos 				return (-1);
409446e541fSchristos 			if (n > 255)
410446e541fSchristos 				return (-1);
411446e541fSchristos 			ShrinkBuffer(n+1);
412446e541fSchristos 			*cp++ = n;
413446e541fSchristos 			memcpy(cp, buf2, n);
414446e541fSchristos 			cp += n;
415446e541fSchristos 			break;
416446e541fSchristos 		case T_ISDN:
417446e541fSchristos 			/* RFC1183 */
418446e541fSchristos 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
419446e541fSchristos 					 endp)) < 0)
420446e541fSchristos 				return (-1);
421446e541fSchristos 			if ((n > 255) || (n == 0))
422446e541fSchristos 				return (-1);
423446e541fSchristos 			ShrinkBuffer(n+1);
424446e541fSchristos 			*cp++ = n;
425446e541fSchristos 			memcpy(cp, buf2, n);
426446e541fSchristos 			cp += n;
427446e541fSchristos 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
428446e541fSchristos 					 endp)) < 0)
429446e541fSchristos 				n = 0;
430446e541fSchristos 			if (n > 255)
431446e541fSchristos 				return (-1);
432446e541fSchristos 			ShrinkBuffer(n+1);
433446e541fSchristos 			*cp++ = n;
434446e541fSchristos 			memcpy(cp, buf2, n);
435446e541fSchristos 			cp += n;
436446e541fSchristos 			break;
437446e541fSchristos 		case T_NSAP:
438446e541fSchristos 			if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2,
439446e541fSchristos 						(int)sizeof(buf2))) != 0) {
440446e541fSchristos 				ShrinkBuffer(n);
441446e541fSchristos 				memcpy(cp, buf2, n);
442446e541fSchristos 				cp += n;
443446e541fSchristos 			} else {
444446e541fSchristos 				return (-1);
445446e541fSchristos 			}
446446e541fSchristos 			break;
447446e541fSchristos 		case T_LOC:
448446e541fSchristos 			if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
449446e541fSchristos 				ShrinkBuffer(n);
450446e541fSchristos 				memcpy(cp, buf2, n);
451446e541fSchristos 				cp += n;
452446e541fSchristos 			} else
453446e541fSchristos 				return (-1);
454446e541fSchristos 			break;
455446e541fSchristos 		case ns_t_sig:
456446e541fSchristos 		    {
457446e541fSchristos 			int sig_type, success, dateerror;
458446e541fSchristos 			u_int32_t exptime, timesigned;
459446e541fSchristos 
460446e541fSchristos 			/* type */
461446e541fSchristos 			if ((n = getword_str(buf2, sizeof buf2,
462446e541fSchristos 					     &startp, endp)) < 0)
463446e541fSchristos 				return (-1);
464446e541fSchristos 			sig_type = sym_ston(__p_type_syms, buf2, &success);
465446e541fSchristos 			if (!success || sig_type == ns_t_any)
466446e541fSchristos 				return (-1);
467446e541fSchristos 			ShrinkBuffer(INT16SZ);
468446e541fSchristos 			PUTSHORT(sig_type, cp);
469446e541fSchristos 			/* alg */
470446e541fSchristos 			n = getnum_str(&startp, endp);
471446e541fSchristos 			if (n < 0)
472446e541fSchristos 				return (-1);
473446e541fSchristos 			ShrinkBuffer(1);
474446e541fSchristos 			*cp++ = n;
475446e541fSchristos 			/* labels */
476446e541fSchristos 			n = getnum_str(&startp, endp);
477446e541fSchristos 			if (n <= 0 || n > 255)
478446e541fSchristos 				return (-1);
479446e541fSchristos 			ShrinkBuffer(1);
480446e541fSchristos 			*cp++ = n;
481446e541fSchristos 			/* ottl  & expire */
482446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
483446e541fSchristos 				return (-1);
484446e541fSchristos 			exptime = ns_datetosecs(buf2, &dateerror);
485446e541fSchristos 			if (!dateerror) {
486446e541fSchristos 				ShrinkBuffer(INT32SZ);
487446e541fSchristos 				PUTLONG(rttl, cp);
488446e541fSchristos 			}
489446e541fSchristos 			else {
490446e541fSchristos 				char *ulendp;
491446e541fSchristos 				u_int32_t ottl;
492446e541fSchristos 				unsigned long ul;
493446e541fSchristos 
494446e541fSchristos 				errno = 0;
495446e541fSchristos 				ul = strtoul(buf2, &ulendp, 10);
496446e541fSchristos 				if (ul > 0xffffffffU)
497446e541fSchristos 					errno = ERANGE;
498446e541fSchristos 				ottl = (u_int32_t)ul;
499446e541fSchristos 				if (errno != 0 ||
500446e541fSchristos 				    (ulendp != NULL && *ulendp != '\0'))
501446e541fSchristos 					return (-1);
502446e541fSchristos 				ShrinkBuffer(INT32SZ);
503446e541fSchristos 				PUTLONG(ottl, cp);
504446e541fSchristos 				if (!getword_str(buf2, sizeof buf2, &startp,
505446e541fSchristos 						 endp))
506446e541fSchristos 					return (-1);
507446e541fSchristos 				exptime = ns_datetosecs(buf2, &dateerror);
508446e541fSchristos 				if (dateerror)
509446e541fSchristos 					return (-1);
510446e541fSchristos 			}
511446e541fSchristos 			/* expire */
512446e541fSchristos 			ShrinkBuffer(INT32SZ);
513446e541fSchristos 			PUTLONG(exptime, cp);
514446e541fSchristos 			/* timesigned */
515446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
516446e541fSchristos 				return (-1);
517446e541fSchristos 			timesigned = ns_datetosecs(buf2, &dateerror);
518446e541fSchristos 			if (!dateerror) {
519446e541fSchristos 				ShrinkBuffer(INT32SZ);
520446e541fSchristos 				PUTLONG(timesigned, cp);
521446e541fSchristos 			}
522446e541fSchristos 			else
523446e541fSchristos 				return (-1);
524446e541fSchristos 			/* footprint */
525446e541fSchristos 			n = getnum_str(&startp, endp);
526446e541fSchristos 			if (n < 0)
527446e541fSchristos 				return (-1);
528446e541fSchristos 			ShrinkBuffer(INT16SZ);
529446e541fSchristos 			PUTSHORT(n, cp);
530446e541fSchristos 			/* signer name */
531446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
532446e541fSchristos 				return (-1);
533446e541fSchristos 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
534446e541fSchristos 			if (n < 0)
535446e541fSchristos 				return (-1);
536446e541fSchristos 			cp += n;
537446e541fSchristos 			ShrinkBuffer(n);
538446e541fSchristos 			/* sig */
539446e541fSchristos 			if ((n = getword_str(buf2, sizeof buf2,
540446e541fSchristos 					     &startp, endp)) < 0)
541446e541fSchristos 				return (-1);
542446e541fSchristos 			siglen = b64_pton(buf2, buf3, sizeof(buf3));
543446e541fSchristos 			if (siglen < 0)
544446e541fSchristos 				return (-1);
545446e541fSchristos 			ShrinkBuffer(siglen);
546446e541fSchristos 			memcpy(cp, buf3, siglen);
547446e541fSchristos 			cp += siglen;
548446e541fSchristos 			break;
549446e541fSchristos 		    }
550446e541fSchristos 		case ns_t_key:
551446e541fSchristos 			/* flags */
552446e541fSchristos 			n = gethexnum_str(&startp, endp);
553446e541fSchristos 			if (n < 0)
554446e541fSchristos 				return (-1);
555446e541fSchristos 			ShrinkBuffer(INT16SZ);
556446e541fSchristos 			PUTSHORT(n, cp);
557446e541fSchristos 			/* proto */
558446e541fSchristos 			n = getnum_str(&startp, endp);
559446e541fSchristos 			if (n < 0)
560446e541fSchristos 				return (-1);
561446e541fSchristos 			ShrinkBuffer(1);
562446e541fSchristos 			*cp++ = n;
563446e541fSchristos 			/* alg */
564446e541fSchristos 			n = getnum_str(&startp, endp);
565446e541fSchristos 			if (n < 0)
566446e541fSchristos 				return (-1);
567446e541fSchristos 			ShrinkBuffer(1);
568446e541fSchristos 			*cp++ = n;
569446e541fSchristos 			/* key */
570446e541fSchristos 			if ((n = getword_str(buf2, sizeof buf2,
571446e541fSchristos 					     &startp, endp)) < 0)
572446e541fSchristos 				return (-1);
573446e541fSchristos 			keylen = b64_pton(buf2, buf3, sizeof(buf3));
574446e541fSchristos 			if (keylen < 0)
575446e541fSchristos 				return (-1);
576446e541fSchristos 			ShrinkBuffer(keylen);
577446e541fSchristos 			memcpy(cp, buf3, keylen);
578446e541fSchristos 			cp += keylen;
579446e541fSchristos 			break;
580446e541fSchristos 		case ns_t_nxt:
581446e541fSchristos 		    {
582446e541fSchristos 			int success, nxt_type;
583446e541fSchristos 			u_char data[32];
584446e541fSchristos 			int maxtype;
585446e541fSchristos 
586446e541fSchristos 			/* next name */
587446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
588446e541fSchristos 				return (-1);
589446e541fSchristos 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
590446e541fSchristos 			if (n < 0)
591446e541fSchristos 				return (-1);
592446e541fSchristos 			cp += n;
593446e541fSchristos 			ShrinkBuffer(n);
594446e541fSchristos 			maxtype = 0;
595446e541fSchristos 			memset(data, 0, sizeof data);
596446e541fSchristos 			for (;;) {
597446e541fSchristos 				if (!getword_str(buf2, sizeof buf2, &startp,
598446e541fSchristos 						 endp))
599446e541fSchristos 					break;
600446e541fSchristos 				nxt_type = sym_ston(__p_type_syms, buf2,
601446e541fSchristos 						    &success);
602446e541fSchristos 				if (!success || !ns_t_rr_p(nxt_type))
603446e541fSchristos 					return (-1);
604446e541fSchristos 				NS_NXT_BIT_SET(nxt_type, data);
605446e541fSchristos 				if (nxt_type > maxtype)
606446e541fSchristos 					maxtype = nxt_type;
607446e541fSchristos 			}
608446e541fSchristos 			n = maxtype/NS_NXT_BITS+1;
609446e541fSchristos 			ShrinkBuffer(n);
610446e541fSchristos 			memcpy(cp, data, n);
611446e541fSchristos 			cp += n;
612446e541fSchristos 			break;
613446e541fSchristos 		    }
614446e541fSchristos 		case ns_t_cert:
615446e541fSchristos 			/* type */
616446e541fSchristos 			n = getnum_str(&startp, endp);
617446e541fSchristos 			if (n < 0)
618446e541fSchristos 				return (-1);
619446e541fSchristos 			ShrinkBuffer(INT16SZ);
620446e541fSchristos 			PUTSHORT(n, cp);
621446e541fSchristos 			/* key tag */
622446e541fSchristos 			n = getnum_str(&startp, endp);
623446e541fSchristos 			if (n < 0)
624446e541fSchristos 				return (-1);
625446e541fSchristos 			ShrinkBuffer(INT16SZ);
626446e541fSchristos 			PUTSHORT(n, cp);
627446e541fSchristos 			/* alg */
628446e541fSchristos 			n = getnum_str(&startp, endp);
629446e541fSchristos 			if (n < 0)
630446e541fSchristos 				return (-1);
631446e541fSchristos 			ShrinkBuffer(1);
632446e541fSchristos 			*cp++ = n;
633446e541fSchristos 			/* cert */
634446e541fSchristos 			if ((n = getword_str(buf2, sizeof buf2,
635446e541fSchristos 					     &startp, endp)) < 0)
636446e541fSchristos 				return (-1);
637446e541fSchristos 			certlen = b64_pton(buf2, buf3, sizeof(buf3));
638446e541fSchristos 			if (certlen < 0)
639446e541fSchristos 				return (-1);
640446e541fSchristos 			ShrinkBuffer(certlen);
641446e541fSchristos 			memcpy(cp, buf3, certlen);
642446e541fSchristos 			cp += certlen;
643446e541fSchristos 			break;
644446e541fSchristos 		case ns_t_aaaa:
645446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
646446e541fSchristos 				return (-1);
647446e541fSchristos 			if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
648446e541fSchristos 				return (-1);
649446e541fSchristos 			ShrinkBuffer(NS_IN6ADDRSZ);
650446e541fSchristos 			memcpy(cp, &in6a, NS_IN6ADDRSZ);
651446e541fSchristos 			cp += NS_IN6ADDRSZ;
652446e541fSchristos 			break;
653446e541fSchristos 		case ns_t_naptr:
654446e541fSchristos 			/* Order Preference Flags Service Replacement Regexp */
655446e541fSchristos 			/* Order */
656446e541fSchristos 			n = getnum_str(&startp, endp);
657446e541fSchristos 			if (n < 0 || n > 65535)
658446e541fSchristos 				return (-1);
659446e541fSchristos 			ShrinkBuffer(INT16SZ);
660446e541fSchristos 			PUTSHORT(n, cp);
661446e541fSchristos 			/* Preference */
662446e541fSchristos 			n = getnum_str(&startp, endp);
663446e541fSchristos 			if (n < 0 || n > 65535)
664446e541fSchristos 				return (-1);
665446e541fSchristos 			ShrinkBuffer(INT16SZ);
666446e541fSchristos 			PUTSHORT(n, cp);
667446e541fSchristos 			/* Flags */
668446e541fSchristos 			if ((n = getstr_str(buf2, sizeof buf2,
669446e541fSchristos 					&startp, endp)) < 0) {
670446e541fSchristos 				return (-1);
671446e541fSchristos 			}
672446e541fSchristos 			if (n > 255)
673446e541fSchristos 				return (-1);
674446e541fSchristos 			ShrinkBuffer(n+1);
675446e541fSchristos 			*cp++ = n;
676446e541fSchristos 			memcpy(cp, buf2, n);
677446e541fSchristos 			cp += n;
678446e541fSchristos 			/* Service Classes */
679446e541fSchristos 			if ((n = getstr_str(buf2, sizeof buf2,
680446e541fSchristos 					&startp, endp)) < 0) {
681446e541fSchristos 				return (-1);
682446e541fSchristos 			}
683446e541fSchristos 			if (n > 255)
684446e541fSchristos 				return (-1);
685446e541fSchristos 			ShrinkBuffer(n+1);
686446e541fSchristos 			*cp++ = n;
687446e541fSchristos 			memcpy(cp, buf2, n);
688446e541fSchristos 			cp += n;
689446e541fSchristos 			/* Pattern */
690446e541fSchristos 			if ((n = getstr_str(buf2, sizeof buf2,
691446e541fSchristos 					&startp, endp)) < 0) {
692446e541fSchristos 				return (-1);
693446e541fSchristos 			}
694446e541fSchristos 			if (n > 255)
695446e541fSchristos 				return (-1);
696446e541fSchristos 			ShrinkBuffer(n+1);
697446e541fSchristos 			*cp++ = n;
698446e541fSchristos 			memcpy(cp, buf2, n);
699446e541fSchristos 			cp += n;
700446e541fSchristos 			/* Replacement */
701446e541fSchristos 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
702446e541fSchristos 				return (-1);
703446e541fSchristos 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
704446e541fSchristos 			if (n < 0)
705446e541fSchristos 				return (-1);
706446e541fSchristos 			cp += n;
707446e541fSchristos 			ShrinkBuffer(n);
708446e541fSchristos 			break;
709446e541fSchristos 		default:
710446e541fSchristos 			return (-1);
711446e541fSchristos 		} /*switch*/
712446e541fSchristos 		n = (u_int16_t)((cp - sp2) - INT16SZ);
713446e541fSchristos 		PUTSHORT(n, sp2);
714446e541fSchristos 	} /*for*/
715446e541fSchristos 
716446e541fSchristos 	hp->qdcount = htons(counts[0]);
717446e541fSchristos 	hp->ancount = htons(counts[1]);
718446e541fSchristos 	hp->nscount = htons(counts[2]);
719446e541fSchristos 	hp->arcount = htons(counts[3]);
720446e541fSchristos 	return (int)(cp - buf);
721446e541fSchristos }
722446e541fSchristos 
723446e541fSchristos /*%
724446e541fSchristos  * Get a whitespace delimited word from a string (not file)
725446e541fSchristos  * into buf. modify the start pointer to point after the
726446e541fSchristos  * word in the string.
727446e541fSchristos  */
728446e541fSchristos static int
getword_str(char * buf,size_t size,u_char ** startpp,u_char * endp)729446e541fSchristos getword_str(char *buf, size_t size, u_char **startpp, u_char *endp) {
730446e541fSchristos         char *cp;
731446e541fSchristos         int c;
732446e541fSchristos 
733446e541fSchristos         for (cp = buf; *startpp <= endp; ) {
734446e541fSchristos                 c = **startpp;
735446e541fSchristos                 if (isspace(c) || c == '\0') {
736446e541fSchristos                         if (cp != buf) /*%< trailing whitespace */
737446e541fSchristos                                 break;
738446e541fSchristos                         else { /*%< leading whitespace */
739446e541fSchristos                                 (*startpp)++;
740446e541fSchristos                                 continue;
741446e541fSchristos                         }
742446e541fSchristos                 }
743446e541fSchristos                 (*startpp)++;
744446e541fSchristos                 if (cp >= buf+size-1)
745446e541fSchristos                         break;
746446e541fSchristos                 *cp++ = (u_char)c;
747446e541fSchristos         }
748446e541fSchristos         *cp = '\0';
749446e541fSchristos         return (cp != buf);
750446e541fSchristos }
751446e541fSchristos 
752446e541fSchristos /*%
753*d0f9e065Sandvar  * get a whitespace delimited string from memory.  Process quoted strings
754446e541fSchristos  * and \\DDD escapes.  Return length or -1 on error.  Returned string may
755446e541fSchristos  * contain nulls.
756446e541fSchristos  */
757446e541fSchristos static char digits[] = "0123456789";
758446e541fSchristos static int
getstr_str(char * buf,size_t size,u_char ** startpp,u_char * endp)759446e541fSchristos getstr_str(char *buf, size_t size, u_char **startpp, u_char *endp) {
760446e541fSchristos         char *cp;
761446e541fSchristos         int c, c1 = 0;
762446e541fSchristos 	int inquote = 0;
763446e541fSchristos 	int seen_quote = 0;
764446e541fSchristos 	int escape = 0;
765446e541fSchristos 	int dig = 0;
766446e541fSchristos 
767446e541fSchristos 	for (cp = buf; *startpp <= endp; ) {
768446e541fSchristos                 if ((c = **startpp) == '\0')
769446e541fSchristos 			break;
770446e541fSchristos 		/* leading white space */
771446e541fSchristos 		if ((cp == buf) && !seen_quote && isspace(c)) {
772446e541fSchristos 			(*startpp)++;
773446e541fSchristos 			continue;
774446e541fSchristos 		}
775446e541fSchristos 
776446e541fSchristos 		switch (c) {
777446e541fSchristos 		case '\\':
778446e541fSchristos 			if (!escape)  {
779446e541fSchristos 				escape = 1;
780446e541fSchristos 				dig = 0;
781446e541fSchristos 				c1 = 0;
782446e541fSchristos 				(*startpp)++;
783446e541fSchristos 				continue;
784446e541fSchristos 			}
785446e541fSchristos 			goto do_escape;
786446e541fSchristos 		case '"':
787446e541fSchristos 			if (!escape) {
788446e541fSchristos 				inquote = !inquote;
789446e541fSchristos 				seen_quote = 1;
790446e541fSchristos 				(*startpp)++;
791446e541fSchristos 				continue;
792446e541fSchristos 			}
793446e541fSchristos 			/*FALLTHROUGH*/
794446e541fSchristos 		default:
795446e541fSchristos 		do_escape:
796446e541fSchristos 			if (escape) {
797446e541fSchristos 				switch (c) {
798446e541fSchristos 				case '0':
799446e541fSchristos 				case '1':
800446e541fSchristos 				case '2':
801446e541fSchristos 				case '3':
802446e541fSchristos 				case '4':
803446e541fSchristos 				case '5':
804446e541fSchristos 				case '6':
805446e541fSchristos 				case '7':
806446e541fSchristos 				case '8':
807446e541fSchristos 				case '9':
808446e541fSchristos 					c1 = c1 * 10 +
809446e541fSchristos 					    (int)(strchr(digits, c) - digits);
810446e541fSchristos 
811446e541fSchristos 					if (++dig == 3) {
812446e541fSchristos 						c = c1 &0xff;
813446e541fSchristos 						break;
814446e541fSchristos 					}
815446e541fSchristos 					(*startpp)++;
816446e541fSchristos 					continue;
817446e541fSchristos 				}
818446e541fSchristos 				escape = 0;
819446e541fSchristos 			} else if (!inquote && isspace(c))
820446e541fSchristos 				goto done;
821446e541fSchristos 			if (cp >= buf+size-1)
822446e541fSchristos 				goto done;
823446e541fSchristos 			*cp++ = (u_char)c;
824446e541fSchristos 			(*startpp)++;
825446e541fSchristos 		}
826446e541fSchristos 	}
827446e541fSchristos  done:
828446e541fSchristos 	*cp = '\0';
829446e541fSchristos 	return ((cp == buf)?  (seen_quote? 0: -1): (int)(cp - buf));
830446e541fSchristos }
831446e541fSchristos 
832446e541fSchristos /*%
833446e541fSchristos  * Get a whitespace delimited base 16 number from a string (not file) into buf
834446e541fSchristos  * update the start pointer to point after the number in the string.
835446e541fSchristos  */
836446e541fSchristos static int
gethexnum_str(u_char ** startpp,u_char * endp)837446e541fSchristos gethexnum_str(u_char **startpp, u_char *endp) {
838446e541fSchristos         int c, n;
839446e541fSchristos         int seendigit = 0;
840446e541fSchristos         int m = 0;
841446e541fSchristos 
842446e541fSchristos 	if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
843446e541fSchristos 		return getnum_str(startpp, endp);
844446e541fSchristos 	(*startpp)+=2;
845446e541fSchristos         for (n = 0; *startpp <= endp; ) {
846446e541fSchristos                 c = **startpp;
847446e541fSchristos                 if (isspace(c) || c == '\0') {
848446e541fSchristos                         if (seendigit) /*%< trailing whitespace */
849446e541fSchristos                                 break;
850446e541fSchristos                         else { /*%< leading whitespace */
851446e541fSchristos                                 (*startpp)++;
852446e541fSchristos                                 continue;
853446e541fSchristos                         }
854446e541fSchristos                 }
855446e541fSchristos                 if (c == ';') {
856446e541fSchristos                         while ((*startpp <= endp) &&
857446e541fSchristos 			       ((c = **startpp) != '\n'))
858446e541fSchristos 					(*startpp)++;
859446e541fSchristos                         if (seendigit)
860446e541fSchristos                                 break;
861446e541fSchristos                         continue;
862446e541fSchristos                 }
863446e541fSchristos                 if (!isxdigit(c)) {
864446e541fSchristos                         if (c == ')' && seendigit) {
865446e541fSchristos                                 (*startpp)--;
866446e541fSchristos                                 break;
867446e541fSchristos                         }
868446e541fSchristos 			return (-1);
869446e541fSchristos                 }
870446e541fSchristos                 (*startpp)++;
871446e541fSchristos 		if (isdigit(c))
872446e541fSchristos 	                n = n * 16 + (c - '0');
873446e541fSchristos 		else
874446e541fSchristos 			n = n * 16 + (tolower(c) - 'a' + 10);
875446e541fSchristos                 seendigit = 1;
876446e541fSchristos         }
877446e541fSchristos         return (n + m);
878446e541fSchristos }
879446e541fSchristos 
880446e541fSchristos /*%
881446e541fSchristos  * Get a whitespace delimited base 10 number from a string (not file) into buf
882446e541fSchristos  * update the start pointer to point after the number in the string.
883446e541fSchristos  */
884446e541fSchristos static int
getnum_str(u_char ** startpp,u_char * endp)885446e541fSchristos getnum_str(u_char **startpp, u_char *endp) {
886446e541fSchristos         int c, n;
887446e541fSchristos         int seendigit = 0;
888446e541fSchristos         int m = 0;
889446e541fSchristos 
890446e541fSchristos         for (n = 0; *startpp <= endp; ) {
891446e541fSchristos                 c = **startpp;
892446e541fSchristos                 if (isspace(c) || c == '\0') {
893446e541fSchristos                         if (seendigit) /*%< trailing whitespace */
894446e541fSchristos                                 break;
895446e541fSchristos                         else { /*%< leading whitespace */
896446e541fSchristos                                 (*startpp)++;
897446e541fSchristos                                 continue;
898446e541fSchristos                         }
899446e541fSchristos                 }
900446e541fSchristos                 if (c == ';') {
901446e541fSchristos                         while ((*startpp <= endp) &&
902446e541fSchristos 			       ((c = **startpp) != '\n'))
903446e541fSchristos 					(*startpp)++;
904446e541fSchristos                         if (seendigit)
905446e541fSchristos                                 break;
906446e541fSchristos                         continue;
907446e541fSchristos                 }
908446e541fSchristos                 if (!isdigit(c)) {
909446e541fSchristos                         if (c == ')' && seendigit) {
910446e541fSchristos                                 (*startpp)--;
911446e541fSchristos                                 break;
912446e541fSchristos                         }
913446e541fSchristos 			return (-1);
914446e541fSchristos                 }
915446e541fSchristos                 (*startpp)++;
916446e541fSchristos                 n = n * 10 + (c - '0');
917446e541fSchristos                 seendigit = 1;
918446e541fSchristos         }
919446e541fSchristos         return (n + m);
920446e541fSchristos }
921446e541fSchristos 
922446e541fSchristos /*%
923446e541fSchristos  * Allocate a resource record buffer & save rr info.
924446e541fSchristos  */
925446e541fSchristos ns_updrec *
res_mkupdrec(int section,const char * dname,u_int class,u_int type,u_long ttl)926446e541fSchristos res_mkupdrec(int section, const char *dname,
927446e541fSchristos 	     u_int class, u_int type, u_long ttl) {
928446e541fSchristos 	ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
929446e541fSchristos 
930446e541fSchristos 	if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
931446e541fSchristos 		if (rrecp)
932446e541fSchristos 			free(rrecp);
933446e541fSchristos 		return (NULL);
934446e541fSchristos 	}
935446e541fSchristos  	rrecp->r_class = (ns_class)class;
936446e541fSchristos 	rrecp->r_type = (ns_type)type;
937446e541fSchristos 	rrecp->r_ttl = (u_int)ttl;
938446e541fSchristos 	rrecp->r_section = (ns_sect)section;
939446e541fSchristos 	return (rrecp);
940446e541fSchristos }
941446e541fSchristos 
942446e541fSchristos /*%
943446e541fSchristos  * Free a resource record buffer created by res_mkupdrec.
944446e541fSchristos  */
945446e541fSchristos void
res_freeupdrec(ns_updrec * rrecp)946446e541fSchristos res_freeupdrec(ns_updrec *rrecp) {
947446e541fSchristos 	/* Note: freeing r_dp is the caller's responsibility. */
948446e541fSchristos 	if (rrecp->r_dname != NULL)
949446e541fSchristos 		free(rrecp->r_dname);
950446e541fSchristos 	free(rrecp);
951446e541fSchristos }
952446e541fSchristos 
953446e541fSchristos struct valuelist {
954446e541fSchristos 	struct valuelist *	next;
955446e541fSchristos 	struct valuelist *	prev;
956446e541fSchristos 	char *			name;
957446e541fSchristos 	char *			proto;
958446e541fSchristos 	int			port;
959446e541fSchristos };
960446e541fSchristos static struct valuelist *servicelist, *protolist;
961446e541fSchristos 
962446e541fSchristos static void
res_buildservicelist(void)963446e541fSchristos res_buildservicelist(void) {
964446e541fSchristos 	struct servent *sp;
965446e541fSchristos 	struct valuelist *slp;
966446e541fSchristos 
967446e541fSchristos #ifdef MAYBE_HESIOD
968446e541fSchristos 	setservent(0);
969446e541fSchristos #else
970446e541fSchristos 	setservent(1);
971446e541fSchristos #endif
972446e541fSchristos 	while ((sp = getservent()) != NULL) {
973446e541fSchristos 		slp = (struct valuelist *)malloc(sizeof(struct valuelist));
974446e541fSchristos 		if (!slp)
975446e541fSchristos 			break;
976446e541fSchristos 		slp->name = strdup(sp->s_name);
977446e541fSchristos 		slp->proto = strdup(sp->s_proto);
978446e541fSchristos 		if ((slp->name == NULL) || (slp->proto == NULL)) {
979446e541fSchristos 			if (slp->name) free(slp->name);
980446e541fSchristos 			if (slp->proto) free(slp->proto);
981446e541fSchristos 			free(slp);
982446e541fSchristos 			break;
983446e541fSchristos 		}
984446e541fSchristos 		slp->port = ntohs((u_int16_t)sp->s_port);  /*%< host byt order */
985446e541fSchristos 		slp->next = servicelist;
986446e541fSchristos 		slp->prev = NULL;
987446e541fSchristos 		if (servicelist)
988446e541fSchristos 			servicelist->prev = slp;
989446e541fSchristos 		servicelist = slp;
990446e541fSchristos 	}
991446e541fSchristos 	endservent();
992446e541fSchristos }
993446e541fSchristos 
994446e541fSchristos void
res_destroyservicelist(void)995c53e4613Schristos res_destroyservicelist(void) {
996446e541fSchristos 	struct valuelist *slp, *slp_next;
997446e541fSchristos 
998446e541fSchristos 	for (slp = servicelist; slp != NULL; slp = slp_next) {
999446e541fSchristos 		slp_next = slp->next;
1000446e541fSchristos 		free(slp->name);
1001446e541fSchristos 		free(slp->proto);
1002446e541fSchristos 		free(slp);
1003446e541fSchristos 	}
1004446e541fSchristos 	servicelist = (struct valuelist *)0;
1005446e541fSchristos }
1006446e541fSchristos 
1007446e541fSchristos void
res_buildprotolist(void)1008446e541fSchristos res_buildprotolist(void) {
1009446e541fSchristos 	struct protoent *pp;
1010446e541fSchristos 	struct valuelist *slp;
1011446e541fSchristos 
1012446e541fSchristos #ifdef MAYBE_HESIOD
1013446e541fSchristos 	setprotoent(0);
1014446e541fSchristos #else
1015446e541fSchristos 	setprotoent(1);
1016446e541fSchristos #endif
1017446e541fSchristos 	while ((pp = getprotoent()) != NULL) {
1018446e541fSchristos 		slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1019446e541fSchristos 		if (!slp)
1020446e541fSchristos 			break;
1021446e541fSchristos 		slp->name = strdup(pp->p_name);
1022446e541fSchristos 		if (slp->name == NULL) {
1023446e541fSchristos 			free(slp);
1024446e541fSchristos 			break;
1025446e541fSchristos 		}
1026446e541fSchristos 		slp->port = pp->p_proto;	/*%< host byte order */
1027446e541fSchristos 		slp->next = protolist;
1028446e541fSchristos 		slp->prev = NULL;
1029446e541fSchristos 		if (protolist)
1030446e541fSchristos 			protolist->prev = slp;
1031446e541fSchristos 		protolist = slp;
1032446e541fSchristos 	}
1033446e541fSchristos 	endprotoent();
1034446e541fSchristos }
1035446e541fSchristos 
1036446e541fSchristos void
res_destroyprotolist(void)1037446e541fSchristos res_destroyprotolist(void) {
1038446e541fSchristos 	struct valuelist *plp, *plp_next;
1039446e541fSchristos 
1040446e541fSchristos 	for (plp = protolist; plp != NULL; plp = plp_next) {
1041446e541fSchristos 		plp_next = plp->next;
1042446e541fSchristos 		free(plp->name);
1043446e541fSchristos 		free(plp);
1044446e541fSchristos 	}
1045446e541fSchristos 	protolist = (struct valuelist *)0;
1046446e541fSchristos }
1047446e541fSchristos 
1048446e541fSchristos static int
findservice(const char * s,struct valuelist ** list)1049446e541fSchristos findservice(const char *s, struct valuelist **list) {
1050446e541fSchristos 	struct valuelist *lp = *list;
1051446e541fSchristos 	int n;
1052446e541fSchristos 
1053446e541fSchristos 	for (; lp != NULL; lp = lp->next)
1054446e541fSchristos 		if (strcasecmp(lp->name, s) == 0) {
1055446e541fSchristos 			if (lp != *list) {
1056446e541fSchristos 				lp->prev->next = lp->next;
1057446e541fSchristos 				if (lp->next)
1058446e541fSchristos 					lp->next->prev = lp->prev;
1059446e541fSchristos 				(*list)->prev = lp;
1060446e541fSchristos 				lp->next = *list;
1061446e541fSchristos 				*list = lp;
1062446e541fSchristos 			}
1063446e541fSchristos 			return (lp->port);	/*%< host byte order */
1064446e541fSchristos 		}
1065446e541fSchristos 	if (sscanf(s, "%d", &n) != 1 || n <= 0)
1066446e541fSchristos 		n = -1;
1067446e541fSchristos 	return (n);
1068446e541fSchristos }
1069446e541fSchristos 
1070446e541fSchristos /*%
1071446e541fSchristos  * Convert service name or (ascii) number to int.
1072446e541fSchristos  */
1073446e541fSchristos int
res_servicenumber(const char * p)1074446e541fSchristos res_servicenumber(const char *p) {
1075446e541fSchristos 	if (servicelist == (struct valuelist *)0)
1076446e541fSchristos 		res_buildservicelist();
1077446e541fSchristos 	return (findservice(p, &servicelist));
1078446e541fSchristos }
1079446e541fSchristos 
1080446e541fSchristos /*%
1081446e541fSchristos  * Convert protocol name or (ascii) number to int.
1082446e541fSchristos  */
1083446e541fSchristos int
res_protocolnumber(const char * p)1084446e541fSchristos res_protocolnumber(const char *p) {
1085446e541fSchristos 	if (protolist == (struct valuelist *)0)
1086446e541fSchristos 		res_buildprotolist();
1087446e541fSchristos 	return (findservice(p, &protolist));
1088446e541fSchristos }
1089446e541fSchristos 
1090446e541fSchristos static struct servent *
cgetservbyport(u_int16_t port,const char * proto)1091446e541fSchristos cgetservbyport(u_int16_t port, const char *proto) {	/*%< Host byte order. */
1092446e541fSchristos 	struct valuelist **list = &servicelist;
1093446e541fSchristos 	struct valuelist *lp = *list;
1094446e541fSchristos 	static struct servent serv;
1095446e541fSchristos 
1096446e541fSchristos 	port = ntohs(port);
1097446e541fSchristos 	for (; lp != NULL; lp = lp->next) {
1098446e541fSchristos 		if (port != (u_int16_t)lp->port)	/*%< Host byte order. */
1099446e541fSchristos 			continue;
1100446e541fSchristos 		if (strcasecmp(lp->proto, proto) == 0) {
1101446e541fSchristos 			if (lp != *list) {
1102446e541fSchristos 				lp->prev->next = lp->next;
1103446e541fSchristos 				if (lp->next)
1104446e541fSchristos 					lp->next->prev = lp->prev;
1105446e541fSchristos 				(*list)->prev = lp;
1106446e541fSchristos 				lp->next = *list;
1107446e541fSchristos 				*list = lp;
1108446e541fSchristos 			}
1109446e541fSchristos 			serv.s_name = lp->name;
1110446e541fSchristos 			serv.s_port = htons((u_int16_t)lp->port);
1111446e541fSchristos 			serv.s_proto = lp->proto;
1112446e541fSchristos 			return (&serv);
1113446e541fSchristos 		}
1114446e541fSchristos 	}
1115446e541fSchristos 	return (0);
1116446e541fSchristos }
1117446e541fSchristos 
1118446e541fSchristos static struct protoent *
cgetprotobynumber(int proto)1119446e541fSchristos cgetprotobynumber(int proto) {				/*%< Host byte order. */
1120446e541fSchristos 	struct valuelist **list = &protolist;
1121446e541fSchristos 	struct valuelist *lp = *list;
1122446e541fSchristos 	static struct protoent prot;
1123446e541fSchristos 
1124446e541fSchristos 	for (; lp != NULL; lp = lp->next)
1125446e541fSchristos 		if (lp->port == proto) {		/*%< Host byte order. */
1126446e541fSchristos 			if (lp != *list) {
1127446e541fSchristos 				lp->prev->next = lp->next;
1128446e541fSchristos 				if (lp->next)
1129446e541fSchristos 					lp->next->prev = lp->prev;
1130446e541fSchristos 				(*list)->prev = lp;
1131446e541fSchristos 				lp->next = *list;
1132446e541fSchristos 				*list = lp;
1133446e541fSchristos 			}
1134446e541fSchristos 			prot.p_name = lp->name;
1135446e541fSchristos 			prot.p_proto = lp->port;	/*%< Host byte order. */
1136446e541fSchristos 			return (&prot);
1137446e541fSchristos 		}
1138446e541fSchristos 	return (0);
1139446e541fSchristos }
1140446e541fSchristos 
1141446e541fSchristos const char *
res_protocolname(int num)1142446e541fSchristos res_protocolname(int num) {
1143446e541fSchristos 	static char number[8];
1144446e541fSchristos 	struct protoent *pp;
1145446e541fSchristos 
1146446e541fSchristos 	if (protolist == (struct valuelist *)0)
1147446e541fSchristos 		res_buildprotolist();
1148446e541fSchristos 	pp = cgetprotobynumber(num);
1149446e541fSchristos 	if (pp == 0)  {
1150446e541fSchristos 		(void) sprintf(number, "%d", num);
1151446e541fSchristos 		return (number);
1152446e541fSchristos 	}
1153446e541fSchristos 	return (pp->p_name);
1154446e541fSchristos }
1155446e541fSchristos 
1156446e541fSchristos const char *
res_servicename(u_int16_t port,const char * proto)1157446e541fSchristos res_servicename(u_int16_t port, const char *proto) {	/*%< Host byte order. */
1158446e541fSchristos 	static char number[8];
1159446e541fSchristos 	struct servent *ss;
1160446e541fSchristos 
1161446e541fSchristos 	if (servicelist == (struct valuelist *)0)
1162446e541fSchristos 		res_buildservicelist();
1163446e541fSchristos 	ss = cgetservbyport(htons(port), proto);
1164446e541fSchristos 	if (ss == 0)  {
1165446e541fSchristos 		(void) sprintf(number, "%d", port);
1166446e541fSchristos 		return (number);
1167446e541fSchristos 	}
1168446e541fSchristos 	return (ss->s_name);
1169446e541fSchristos }
1170