1*5bbd2a12Schristos /* $NetBSD: res_mkupdate.c,v 1.1.1.2 2012/09/09 16:08:09 christos Exp $ */
2b5677b36Schristos
3b5677b36Schristos /*
4b5677b36Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos * Copyright (c) 1996-1999 by Internet Software Consortium.
6b5677b36Schristos *
7b5677b36Schristos * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos *
11b5677b36Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos */
19b5677b36Schristos
20b5677b36Schristos /*! \file
21b5677b36Schristos * \brief
22b5677b36Schristos * Based on the Dynamic DNS reference implementation by Viraj Bais
23b5677b36Schristos * <viraj_bais@ccm.fm.intel.com>
24b5677b36Schristos */
25b5677b36Schristos
26b5677b36Schristos #if !defined(lint) && !defined(SABER)
27b5677b36Schristos static const char rcsid[] = "Id: res_mkupdate.c,v 1.10 2008/12/11 09:59:00 marka Exp ";
28b5677b36Schristos #endif /* not lint */
29b5677b36Schristos
30b5677b36Schristos #include "port_before.h"
31b5677b36Schristos
32b5677b36Schristos #include <sys/types.h>
33b5677b36Schristos #include <sys/param.h>
34b5677b36Schristos
35b5677b36Schristos #include <netinet/in.h>
36b5677b36Schristos #include <arpa/nameser.h>
37b5677b36Schristos #include <arpa/inet.h>
38b5677b36Schristos
39b5677b36Schristos #include <errno.h>
40b5677b36Schristos #include <limits.h>
41b5677b36Schristos #include <netdb.h>
42b5677b36Schristos #include <resolv.h>
43b5677b36Schristos #include <res_update.h>
44b5677b36Schristos #include <stdio.h>
45b5677b36Schristos #include <stdlib.h>
46b5677b36Schristos #include <string.h>
47b5677b36Schristos #include <unistd.h>
48b5677b36Schristos #include <ctype.h>
49b5677b36Schristos
50b5677b36Schristos #include "port_after.h"
51b5677b36Schristos
52b5677b36Schristos /* Options. Leave them on. */
53b5677b36Schristos #define DEBUG
54b5677b36Schristos #define MAXPORT 1024
55b5677b36Schristos
56b5677b36Schristos static int getnum_str(u_char **, u_char *);
57b5677b36Schristos static int gethexnum_str(u_char **, u_char *);
58b5677b36Schristos static int getword_str(char *, int, u_char **, u_char *);
59b5677b36Schristos static int getstr_str(char *, int, u_char **, u_char *);
60b5677b36Schristos
61b5677b36Schristos #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
62b5677b36Schristos
63b5677b36Schristos /* Forward. */
64b5677b36Schristos
65b5677b36Schristos int res_protocolnumber(const char *);
66b5677b36Schristos int res_servicenumber(const char *);
67b5677b36Schristos
68b5677b36Schristos /*%
69b5677b36Schristos * Form update packets.
70b5677b36Schristos * Returns the size of the resulting packet if no error
71b5677b36Schristos *
72b5677b36Schristos * On error,
73b5677b36Schristos * returns
74b5677b36Schristos *\li -1 if error in reading a word/number in rdata
75b5677b36Schristos * portion for update packets
76b5677b36Schristos *\li -2 if length of buffer passed is insufficient
77b5677b36Schristos *\li -3 if zone section is not the first section in
78b5677b36Schristos * the linked list, or section order has a problem
79b5677b36Schristos *\li -4 on a number overflow
80b5677b36Schristos *\li -5 unknown operation or no records
81b5677b36Schristos */
82b5677b36Schristos int
res_nmkupdate(res_state statp,ns_updrec * rrecp_in,u_char * buf,int buflen)83b5677b36Schristos res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
84b5677b36Schristos ns_updrec *rrecp_start = rrecp_in;
85b5677b36Schristos HEADER *hp;
86b5677b36Schristos u_char *cp, *sp2, *startp, *endp;
87b5677b36Schristos int n, i, soanum, multiline;
88b5677b36Schristos ns_updrec *rrecp;
89b5677b36Schristos struct in_addr ina;
90b5677b36Schristos struct in6_addr in6a;
91b5677b36Schristos char buf2[MAXDNAME];
92b5677b36Schristos u_char buf3[MAXDNAME];
93b5677b36Schristos int section, numrrs = 0, counts[ns_s_max];
94b5677b36Schristos u_int16_t rtype, rclass;
95b5677b36Schristos u_int32_t n1, rttl;
96b5677b36Schristos u_char *dnptrs[20], **dpp, **lastdnptr;
97b5677b36Schristos int siglen, keylen, certlen;
98b5677b36Schristos
99b5677b36Schristos /*
100b5677b36Schristos * Initialize header fields.
101b5677b36Schristos */
102b5677b36Schristos if ((buf == NULL) || (buflen < HFIXEDSZ))
103b5677b36Schristos return (-1);
104b5677b36Schristos memset(buf, 0, HFIXEDSZ);
105b5677b36Schristos hp = (HEADER *) buf;
106b5677b36Schristos statp->id = res_nrandomid(statp);
107b5677b36Schristos hp->id = htons(statp->id);
108b5677b36Schristos hp->opcode = ns_o_update;
109b5677b36Schristos hp->rcode = NOERROR;
110b5677b36Schristos cp = buf + HFIXEDSZ;
111b5677b36Schristos buflen -= HFIXEDSZ;
112b5677b36Schristos dpp = dnptrs;
113b5677b36Schristos *dpp++ = buf;
114b5677b36Schristos *dpp++ = NULL;
115b5677b36Schristos lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
116b5677b36Schristos
117b5677b36Schristos if (rrecp_start == NULL)
118b5677b36Schristos return (-5);
119b5677b36Schristos else if (rrecp_start->r_section != S_ZONE)
120b5677b36Schristos return (-3);
121b5677b36Schristos
122b5677b36Schristos memset(counts, 0, sizeof counts);
123b5677b36Schristos for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
124b5677b36Schristos numrrs++;
125b5677b36Schristos section = rrecp->r_section;
126b5677b36Schristos if (section < 0 || section >= ns_s_max)
127b5677b36Schristos return (-1);
128b5677b36Schristos counts[section]++;
129b5677b36Schristos for (i = section + 1; i < ns_s_max; i++)
130b5677b36Schristos if (counts[i])
131b5677b36Schristos return (-3);
132b5677b36Schristos rtype = rrecp->r_type;
133b5677b36Schristos rclass = rrecp->r_class;
134b5677b36Schristos rttl = rrecp->r_ttl;
135b5677b36Schristos /* overload class and type */
136b5677b36Schristos if (section == S_PREREQ) {
137b5677b36Schristos rttl = 0;
138b5677b36Schristos switch (rrecp->r_opcode) {
139b5677b36Schristos case YXDOMAIN:
140b5677b36Schristos rclass = C_ANY;
141b5677b36Schristos rtype = T_ANY;
142b5677b36Schristos rrecp->r_size = 0;
143b5677b36Schristos break;
144b5677b36Schristos case NXDOMAIN:
145b5677b36Schristos rclass = C_NONE;
146b5677b36Schristos rtype = T_ANY;
147b5677b36Schristos rrecp->r_size = 0;
148b5677b36Schristos break;
149b5677b36Schristos case NXRRSET:
150b5677b36Schristos rclass = C_NONE;
151b5677b36Schristos rrecp->r_size = 0;
152b5677b36Schristos break;
153b5677b36Schristos case YXRRSET:
154b5677b36Schristos if (rrecp->r_size == 0)
155b5677b36Schristos rclass = C_ANY;
156b5677b36Schristos break;
157b5677b36Schristos default:
158b5677b36Schristos fprintf(stderr,
159b5677b36Schristos "res_mkupdate: incorrect opcode: %d\n",
160b5677b36Schristos rrecp->r_opcode);
161b5677b36Schristos fflush(stderr);
162b5677b36Schristos return (-1);
163b5677b36Schristos }
164b5677b36Schristos } else if (section == S_UPDATE) {
165b5677b36Schristos switch (rrecp->r_opcode) {
166b5677b36Schristos case DELETE:
167b5677b36Schristos rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
168b5677b36Schristos break;
169b5677b36Schristos case ADD:
170b5677b36Schristos break;
171b5677b36Schristos default:
172b5677b36Schristos fprintf(stderr,
173b5677b36Schristos "res_mkupdate: incorrect opcode: %d\n",
174b5677b36Schristos rrecp->r_opcode);
175b5677b36Schristos fflush(stderr);
176b5677b36Schristos return (-1);
177b5677b36Schristos }
178b5677b36Schristos }
179b5677b36Schristos
180b5677b36Schristos /*
181b5677b36Schristos * XXX appending default domain to owner name is omitted,
182b5677b36Schristos * fqdn must be provided
183b5677b36Schristos */
184b5677b36Schristos if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
185b5677b36Schristos lastdnptr)) < 0)
186b5677b36Schristos return (-1);
187b5677b36Schristos cp += n;
188b5677b36Schristos ShrinkBuffer(n + 2*INT16SZ);
189b5677b36Schristos PUTSHORT(rtype, cp);
190b5677b36Schristos PUTSHORT(rclass, cp);
191b5677b36Schristos if (section == S_ZONE) {
192b5677b36Schristos if (numrrs != 1 || rrecp->r_type != T_SOA)
193b5677b36Schristos return (-3);
194b5677b36Schristos continue;
195b5677b36Schristos }
196b5677b36Schristos ShrinkBuffer(INT32SZ + INT16SZ);
197b5677b36Schristos PUTLONG(rttl, cp);
198b5677b36Schristos sp2 = cp; /*%< save pointer to length byte */
199b5677b36Schristos cp += INT16SZ;
200b5677b36Schristos if (rrecp->r_size == 0) {
201b5677b36Schristos if (section == S_UPDATE && rclass != C_ANY)
202b5677b36Schristos return (-1);
203b5677b36Schristos else {
204b5677b36Schristos PUTSHORT(0, sp2);
205b5677b36Schristos continue;
206b5677b36Schristos }
207b5677b36Schristos }
208b5677b36Schristos startp = rrecp->r_data;
209b5677b36Schristos endp = startp + rrecp->r_size - 1;
210b5677b36Schristos /* XXX this should be done centrally. */
211b5677b36Schristos switch (rrecp->r_type) {
212b5677b36Schristos case T_A:
213b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
214b5677b36Schristos return (-1);
215b5677b36Schristos if (!inet_aton(buf2, &ina))
216b5677b36Schristos return (-1);
217b5677b36Schristos n1 = ntohl(ina.s_addr);
218b5677b36Schristos ShrinkBuffer(INT32SZ);
219b5677b36Schristos PUTLONG(n1, cp);
220b5677b36Schristos break;
221b5677b36Schristos case T_CNAME:
222b5677b36Schristos case T_MB:
223b5677b36Schristos case T_MG:
224b5677b36Schristos case T_MR:
225b5677b36Schristos case T_NS:
226b5677b36Schristos case T_PTR:
227b5677b36Schristos case ns_t_dname:
228b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
229b5677b36Schristos return (-1);
230b5677b36Schristos n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
231b5677b36Schristos if (n < 0)
232b5677b36Schristos return (-1);
233b5677b36Schristos cp += n;
234b5677b36Schristos ShrinkBuffer(n);
235b5677b36Schristos break;
236b5677b36Schristos case T_MINFO:
237b5677b36Schristos case T_SOA:
238b5677b36Schristos case T_RP:
239b5677b36Schristos for (i = 0; i < 2; i++) {
240b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp,
241b5677b36Schristos endp))
242b5677b36Schristos return (-1);
243b5677b36Schristos n = dn_comp(buf2, cp, buflen,
244b5677b36Schristos dnptrs, lastdnptr);
245b5677b36Schristos if (n < 0)
246b5677b36Schristos return (-1);
247b5677b36Schristos cp += n;
248b5677b36Schristos ShrinkBuffer(n);
249b5677b36Schristos }
250b5677b36Schristos if (rrecp->r_type == T_SOA) {
251b5677b36Schristos ShrinkBuffer(5 * INT32SZ);
252b5677b36Schristos while (isspace(*startp) || !*startp)
253b5677b36Schristos startp++;
254b5677b36Schristos if (*startp == '(') {
255b5677b36Schristos multiline = 1;
256b5677b36Schristos startp++;
257b5677b36Schristos } else
258b5677b36Schristos multiline = 0;
259b5677b36Schristos /* serial, refresh, retry, expire, minimum */
260b5677b36Schristos for (i = 0; i < 5; i++) {
261b5677b36Schristos soanum = getnum_str(&startp, endp);
262b5677b36Schristos if (soanum < 0)
263b5677b36Schristos return (-1);
264b5677b36Schristos PUTLONG(soanum, cp);
265b5677b36Schristos }
266b5677b36Schristos if (multiline) {
267b5677b36Schristos while (isspace(*startp) || !*startp)
268b5677b36Schristos startp++;
269b5677b36Schristos if (*startp != ')')
270b5677b36Schristos return (-1);
271b5677b36Schristos }
272b5677b36Schristos }
273b5677b36Schristos break;
274b5677b36Schristos case T_MX:
275b5677b36Schristos case T_AFSDB:
276b5677b36Schristos case T_RT:
277b5677b36Schristos n = getnum_str(&startp, endp);
278b5677b36Schristos if (n < 0)
279b5677b36Schristos return (-1);
280b5677b36Schristos ShrinkBuffer(INT16SZ);
281b5677b36Schristos PUTSHORT(n, cp);
282b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
283b5677b36Schristos return (-1);
284b5677b36Schristos n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
285b5677b36Schristos if (n < 0)
286b5677b36Schristos return (-1);
287b5677b36Schristos cp += n;
288b5677b36Schristos ShrinkBuffer(n);
289b5677b36Schristos break;
290b5677b36Schristos case T_SRV:
291b5677b36Schristos n = getnum_str(&startp, endp);
292b5677b36Schristos if (n < 0)
293b5677b36Schristos return (-1);
294b5677b36Schristos ShrinkBuffer(INT16SZ);
295b5677b36Schristos PUTSHORT(n, cp);
296b5677b36Schristos
297b5677b36Schristos n = getnum_str(&startp, endp);
298b5677b36Schristos if (n < 0)
299b5677b36Schristos return (-1);
300b5677b36Schristos ShrinkBuffer(INT16SZ);
301b5677b36Schristos PUTSHORT(n, cp);
302b5677b36Schristos
303b5677b36Schristos n = getnum_str(&startp, endp);
304b5677b36Schristos if (n < 0)
305b5677b36Schristos return (-1);
306b5677b36Schristos ShrinkBuffer(INT16SZ);
307b5677b36Schristos PUTSHORT(n, cp);
308b5677b36Schristos
309b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
310b5677b36Schristos return (-1);
311b5677b36Schristos n = dn_comp(buf2, cp, buflen, NULL, NULL);
312b5677b36Schristos if (n < 0)
313b5677b36Schristos return (-1);
314b5677b36Schristos cp += n;
315b5677b36Schristos ShrinkBuffer(n);
316b5677b36Schristos break;
317b5677b36Schristos case T_PX:
318b5677b36Schristos n = getnum_str(&startp, endp);
319b5677b36Schristos if (n < 0)
320b5677b36Schristos return (-1);
321b5677b36Schristos PUTSHORT(n, cp);
322b5677b36Schristos ShrinkBuffer(INT16SZ);
323b5677b36Schristos for (i = 0; i < 2; i++) {
324b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp,
325b5677b36Schristos endp))
326b5677b36Schristos return (-1);
327b5677b36Schristos n = dn_comp(buf2, cp, buflen, dnptrs,
328b5677b36Schristos lastdnptr);
329b5677b36Schristos if (n < 0)
330b5677b36Schristos return (-1);
331b5677b36Schristos cp += n;
332b5677b36Schristos ShrinkBuffer(n);
333b5677b36Schristos }
334b5677b36Schristos break;
335b5677b36Schristos case T_WKS: {
336b5677b36Schristos char bm[MAXPORT/8];
337b5677b36Schristos unsigned int maxbm = 0;
338b5677b36Schristos
339b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
340b5677b36Schristos return (-1);
341b5677b36Schristos if (!inet_aton(buf2, &ina))
342b5677b36Schristos return (-1);
343b5677b36Schristos n1 = ntohl(ina.s_addr);
344b5677b36Schristos ShrinkBuffer(INT32SZ);
345b5677b36Schristos PUTLONG(n1, cp);
346b5677b36Schristos
347b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
348b5677b36Schristos return (-1);
349b5677b36Schristos if ((i = res_protocolnumber(buf2)) < 0)
350b5677b36Schristos return (-1);
351b5677b36Schristos ShrinkBuffer(1);
352b5677b36Schristos *cp++ = i & 0xff;
353b5677b36Schristos
354b5677b36Schristos for (i = 0; i < MAXPORT/8 ; i++)
355b5677b36Schristos bm[i] = 0;
356b5677b36Schristos
357b5677b36Schristos while (getword_str(buf2, sizeof buf2, &startp, endp)) {
358b5677b36Schristos if ((n = res_servicenumber(buf2)) <= 0)
359b5677b36Schristos return (-1);
360b5677b36Schristos
361b5677b36Schristos if (n < MAXPORT) {
362b5677b36Schristos bm[n/8] |= (0x80>>(n%8));
363b5677b36Schristos if ((unsigned)n > maxbm)
364b5677b36Schristos maxbm = n;
365b5677b36Schristos } else
366b5677b36Schristos return (-1);
367b5677b36Schristos }
368b5677b36Schristos maxbm = maxbm/8 + 1;
369b5677b36Schristos ShrinkBuffer(maxbm);
370b5677b36Schristos memcpy(cp, bm, maxbm);
371b5677b36Schristos cp += maxbm;
372b5677b36Schristos break;
373b5677b36Schristos }
374b5677b36Schristos case T_HINFO:
375b5677b36Schristos for (i = 0; i < 2; i++) {
376b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2,
377b5677b36Schristos &startp, endp)) < 0)
378b5677b36Schristos return (-1);
379b5677b36Schristos if (n > 255)
380b5677b36Schristos return (-1);
381b5677b36Schristos ShrinkBuffer(n+1);
382b5677b36Schristos *cp++ = n;
383b5677b36Schristos memcpy(cp, buf2, n);
384b5677b36Schristos cp += n;
385b5677b36Schristos }
386b5677b36Schristos break;
387b5677b36Schristos case T_TXT:
388b5677b36Schristos for (;;) {
389b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2,
390b5677b36Schristos &startp, endp)) < 0) {
391b5677b36Schristos if (cp != (sp2 + INT16SZ))
392b5677b36Schristos break;
393b5677b36Schristos return (-1);
394b5677b36Schristos }
395b5677b36Schristos if (n > 255)
396b5677b36Schristos return (-1);
397b5677b36Schristos ShrinkBuffer(n+1);
398b5677b36Schristos *cp++ = n;
399b5677b36Schristos memcpy(cp, buf2, n);
400b5677b36Schristos cp += n;
401b5677b36Schristos }
402b5677b36Schristos break;
403b5677b36Schristos case T_X25:
404b5677b36Schristos /* RFC1183 */
405b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2, &startp,
406b5677b36Schristos endp)) < 0)
407b5677b36Schristos return (-1);
408b5677b36Schristos if (n > 255)
409b5677b36Schristos return (-1);
410b5677b36Schristos ShrinkBuffer(n+1);
411b5677b36Schristos *cp++ = n;
412b5677b36Schristos memcpy(cp, buf2, n);
413b5677b36Schristos cp += n;
414b5677b36Schristos break;
415b5677b36Schristos case T_ISDN:
416b5677b36Schristos /* RFC1183 */
417b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2, &startp,
418b5677b36Schristos endp)) < 0)
419b5677b36Schristos return (-1);
420b5677b36Schristos if ((n > 255) || (n == 0))
421b5677b36Schristos return (-1);
422b5677b36Schristos ShrinkBuffer(n+1);
423b5677b36Schristos *cp++ = n;
424b5677b36Schristos memcpy(cp, buf2, n);
425b5677b36Schristos cp += n;
426b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2, &startp,
427b5677b36Schristos endp)) < 0)
428b5677b36Schristos n = 0;
429b5677b36Schristos if (n > 255)
430b5677b36Schristos return (-1);
431b5677b36Schristos ShrinkBuffer(n+1);
432b5677b36Schristos *cp++ = n;
433b5677b36Schristos memcpy(cp, buf2, n);
434b5677b36Schristos cp += n;
435b5677b36Schristos break;
436b5677b36Schristos case T_NSAP:
437b5677b36Schristos if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
438b5677b36Schristos ShrinkBuffer(n);
439b5677b36Schristos memcpy(cp, buf2, n);
440b5677b36Schristos cp += n;
441b5677b36Schristos } else {
442b5677b36Schristos return (-1);
443b5677b36Schristos }
444b5677b36Schristos break;
445b5677b36Schristos case T_LOC:
446b5677b36Schristos if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
447b5677b36Schristos ShrinkBuffer(n);
448b5677b36Schristos memcpy(cp, buf2, n);
449b5677b36Schristos cp += n;
450b5677b36Schristos } else
451b5677b36Schristos return (-1);
452b5677b36Schristos break;
453b5677b36Schristos case ns_t_sig:
454b5677b36Schristos {
455b5677b36Schristos int sig_type, success, dateerror;
456b5677b36Schristos u_int32_t exptime, timesigned;
457b5677b36Schristos
458b5677b36Schristos /* type */
459b5677b36Schristos if ((n = getword_str(buf2, sizeof buf2,
460b5677b36Schristos &startp, endp)) < 0)
461b5677b36Schristos return (-1);
462b5677b36Schristos sig_type = sym_ston(__p_type_syms, buf2, &success);
463b5677b36Schristos if (!success || sig_type == ns_t_any)
464b5677b36Schristos return (-1);
465b5677b36Schristos ShrinkBuffer(INT16SZ);
466b5677b36Schristos PUTSHORT(sig_type, cp);
467b5677b36Schristos /* alg */
468b5677b36Schristos n = getnum_str(&startp, endp);
469b5677b36Schristos if (n < 0)
470b5677b36Schristos return (-1);
471b5677b36Schristos ShrinkBuffer(1);
472b5677b36Schristos *cp++ = n;
473b5677b36Schristos /* labels */
474b5677b36Schristos n = getnum_str(&startp, endp);
475b5677b36Schristos if (n <= 0 || n > 255)
476b5677b36Schristos return (-1);
477b5677b36Schristos ShrinkBuffer(1);
478b5677b36Schristos *cp++ = n;
479b5677b36Schristos /* ottl & expire */
480b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
481b5677b36Schristos return (-1);
482b5677b36Schristos exptime = ns_datetosecs(buf2, &dateerror);
483b5677b36Schristos if (!dateerror) {
484b5677b36Schristos ShrinkBuffer(INT32SZ);
485b5677b36Schristos PUTLONG(rttl, cp);
486b5677b36Schristos }
487b5677b36Schristos else {
488b5677b36Schristos char *ulendp;
489b5677b36Schristos u_int32_t ottl;
490b5677b36Schristos
491b5677b36Schristos errno = 0;
492b5677b36Schristos ottl = strtoul(buf2, &ulendp, 10);
493b5677b36Schristos if (errno != 0 ||
494b5677b36Schristos (ulendp != NULL && *ulendp != '\0'))
495b5677b36Schristos return (-1);
496b5677b36Schristos ShrinkBuffer(INT32SZ);
497b5677b36Schristos PUTLONG(ottl, cp);
498b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp,
499b5677b36Schristos endp))
500b5677b36Schristos return (-1);
501b5677b36Schristos exptime = ns_datetosecs(buf2, &dateerror);
502b5677b36Schristos if (dateerror)
503b5677b36Schristos return (-1);
504b5677b36Schristos }
505b5677b36Schristos /* expire */
506b5677b36Schristos ShrinkBuffer(INT32SZ);
507b5677b36Schristos PUTLONG(exptime, cp);
508b5677b36Schristos /* timesigned */
509b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
510b5677b36Schristos return (-1);
511b5677b36Schristos timesigned = ns_datetosecs(buf2, &dateerror);
512b5677b36Schristos if (!dateerror) {
513b5677b36Schristos ShrinkBuffer(INT32SZ);
514b5677b36Schristos PUTLONG(timesigned, cp);
515b5677b36Schristos }
516b5677b36Schristos else
517b5677b36Schristos return (-1);
518b5677b36Schristos /* footprint */
519b5677b36Schristos n = getnum_str(&startp, endp);
520b5677b36Schristos if (n < 0)
521b5677b36Schristos return (-1);
522b5677b36Schristos ShrinkBuffer(INT16SZ);
523b5677b36Schristos PUTSHORT(n, cp);
524b5677b36Schristos /* signer name */
525b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
526b5677b36Schristos return (-1);
527b5677b36Schristos n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
528b5677b36Schristos if (n < 0)
529b5677b36Schristos return (-1);
530b5677b36Schristos cp += n;
531b5677b36Schristos ShrinkBuffer(n);
532b5677b36Schristos /* sig */
533b5677b36Schristos if ((n = getword_str(buf2, sizeof buf2,
534b5677b36Schristos &startp, endp)) < 0)
535b5677b36Schristos return (-1);
536b5677b36Schristos siglen = b64_pton(buf2, buf3, sizeof(buf3));
537b5677b36Schristos if (siglen < 0)
538b5677b36Schristos return (-1);
539b5677b36Schristos ShrinkBuffer(siglen);
540b5677b36Schristos memcpy(cp, buf3, siglen);
541b5677b36Schristos cp += siglen;
542b5677b36Schristos break;
543b5677b36Schristos }
544b5677b36Schristos case ns_t_key:
545b5677b36Schristos /* flags */
546b5677b36Schristos n = gethexnum_str(&startp, endp);
547b5677b36Schristos if (n < 0)
548b5677b36Schristos return (-1);
549b5677b36Schristos ShrinkBuffer(INT16SZ);
550b5677b36Schristos PUTSHORT(n, cp);
551b5677b36Schristos /* proto */
552b5677b36Schristos n = getnum_str(&startp, endp);
553b5677b36Schristos if (n < 0)
554b5677b36Schristos return (-1);
555b5677b36Schristos ShrinkBuffer(1);
556b5677b36Schristos *cp++ = n;
557b5677b36Schristos /* alg */
558b5677b36Schristos n = getnum_str(&startp, endp);
559b5677b36Schristos if (n < 0)
560b5677b36Schristos return (-1);
561b5677b36Schristos ShrinkBuffer(1);
562b5677b36Schristos *cp++ = n;
563b5677b36Schristos /* key */
564b5677b36Schristos if ((n = getword_str(buf2, sizeof buf2,
565b5677b36Schristos &startp, endp)) < 0)
566b5677b36Schristos return (-1);
567b5677b36Schristos keylen = b64_pton(buf2, buf3, sizeof(buf3));
568b5677b36Schristos if (keylen < 0)
569b5677b36Schristos return (-1);
570b5677b36Schristos ShrinkBuffer(keylen);
571b5677b36Schristos memcpy(cp, buf3, keylen);
572b5677b36Schristos cp += keylen;
573b5677b36Schristos break;
574b5677b36Schristos case ns_t_nxt:
575b5677b36Schristos {
576b5677b36Schristos int success, nxt_type;
577b5677b36Schristos u_char data[32];
578b5677b36Schristos int maxtype;
579b5677b36Schristos
580b5677b36Schristos /* next name */
581b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
582b5677b36Schristos return (-1);
583b5677b36Schristos n = dn_comp(buf2, cp, buflen, NULL, NULL);
584b5677b36Schristos if (n < 0)
585b5677b36Schristos return (-1);
586b5677b36Schristos cp += n;
587b5677b36Schristos ShrinkBuffer(n);
588b5677b36Schristos maxtype = 0;
589b5677b36Schristos memset(data, 0, sizeof data);
590b5677b36Schristos for (;;) {
591b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp,
592b5677b36Schristos endp))
593b5677b36Schristos break;
594b5677b36Schristos nxt_type = sym_ston(__p_type_syms, buf2,
595b5677b36Schristos &success);
596b5677b36Schristos if (!success || !ns_t_rr_p(nxt_type))
597b5677b36Schristos return (-1);
598b5677b36Schristos NS_NXT_BIT_SET(nxt_type, data);
599b5677b36Schristos if (nxt_type > maxtype)
600b5677b36Schristos maxtype = nxt_type;
601b5677b36Schristos }
602b5677b36Schristos n = maxtype/NS_NXT_BITS+1;
603b5677b36Schristos ShrinkBuffer(n);
604b5677b36Schristos memcpy(cp, data, n);
605b5677b36Schristos cp += n;
606b5677b36Schristos break;
607b5677b36Schristos }
608b5677b36Schristos case ns_t_cert:
609b5677b36Schristos /* type */
610b5677b36Schristos n = getnum_str(&startp, endp);
611b5677b36Schristos if (n < 0)
612b5677b36Schristos return (-1);
613b5677b36Schristos ShrinkBuffer(INT16SZ);
614b5677b36Schristos PUTSHORT(n, cp);
615b5677b36Schristos /* key tag */
616b5677b36Schristos n = getnum_str(&startp, endp);
617b5677b36Schristos if (n < 0)
618b5677b36Schristos return (-1);
619b5677b36Schristos ShrinkBuffer(INT16SZ);
620b5677b36Schristos PUTSHORT(n, cp);
621b5677b36Schristos /* alg */
622b5677b36Schristos n = getnum_str(&startp, endp);
623b5677b36Schristos if (n < 0)
624b5677b36Schristos return (-1);
625b5677b36Schristos ShrinkBuffer(1);
626b5677b36Schristos *cp++ = n;
627b5677b36Schristos /* cert */
628b5677b36Schristos if ((n = getword_str(buf2, sizeof buf2,
629b5677b36Schristos &startp, endp)) < 0)
630b5677b36Schristos return (-1);
631b5677b36Schristos certlen = b64_pton(buf2, buf3, sizeof(buf3));
632b5677b36Schristos if (certlen < 0)
633b5677b36Schristos return (-1);
634b5677b36Schristos ShrinkBuffer(certlen);
635b5677b36Schristos memcpy(cp, buf3, certlen);
636b5677b36Schristos cp += certlen;
637b5677b36Schristos break;
638b5677b36Schristos case ns_t_aaaa:
639b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
640b5677b36Schristos return (-1);
641b5677b36Schristos if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
642b5677b36Schristos return (-1);
643b5677b36Schristos ShrinkBuffer(NS_IN6ADDRSZ);
644b5677b36Schristos memcpy(cp, &in6a, NS_IN6ADDRSZ);
645b5677b36Schristos cp += NS_IN6ADDRSZ;
646b5677b36Schristos break;
647b5677b36Schristos case ns_t_naptr:
648b5677b36Schristos /* Order Preference Flags Service Replacement Regexp */
649b5677b36Schristos /* Order */
650b5677b36Schristos n = getnum_str(&startp, endp);
651b5677b36Schristos if (n < 0 || n > 65535)
652b5677b36Schristos return (-1);
653b5677b36Schristos ShrinkBuffer(INT16SZ);
654b5677b36Schristos PUTSHORT(n, cp);
655b5677b36Schristos /* Preference */
656b5677b36Schristos n = getnum_str(&startp, endp);
657b5677b36Schristos if (n < 0 || n > 65535)
658b5677b36Schristos return (-1);
659b5677b36Schristos ShrinkBuffer(INT16SZ);
660b5677b36Schristos PUTSHORT(n, cp);
661b5677b36Schristos /* Flags */
662b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2,
663b5677b36Schristos &startp, endp)) < 0) {
664b5677b36Schristos return (-1);
665b5677b36Schristos }
666b5677b36Schristos if (n > 255)
667b5677b36Schristos return (-1);
668b5677b36Schristos ShrinkBuffer(n+1);
669b5677b36Schristos *cp++ = n;
670b5677b36Schristos memcpy(cp, buf2, n);
671b5677b36Schristos cp += n;
672b5677b36Schristos /* Service Classes */
673b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2,
674b5677b36Schristos &startp, endp)) < 0) {
675b5677b36Schristos return (-1);
676b5677b36Schristos }
677b5677b36Schristos if (n > 255)
678b5677b36Schristos return (-1);
679b5677b36Schristos ShrinkBuffer(n+1);
680b5677b36Schristos *cp++ = n;
681b5677b36Schristos memcpy(cp, buf2, n);
682b5677b36Schristos cp += n;
683b5677b36Schristos /* Pattern */
684b5677b36Schristos if ((n = getstr_str(buf2, sizeof buf2,
685b5677b36Schristos &startp, endp)) < 0) {
686b5677b36Schristos return (-1);
687b5677b36Schristos }
688b5677b36Schristos if (n > 255)
689b5677b36Schristos return (-1);
690b5677b36Schristos ShrinkBuffer(n+1);
691b5677b36Schristos *cp++ = n;
692b5677b36Schristos memcpy(cp, buf2, n);
693b5677b36Schristos cp += n;
694b5677b36Schristos /* Replacement */
695b5677b36Schristos if (!getword_str(buf2, sizeof buf2, &startp, endp))
696b5677b36Schristos return (-1);
697b5677b36Schristos n = dn_comp(buf2, cp, buflen, NULL, NULL);
698b5677b36Schristos if (n < 0)
699b5677b36Schristos return (-1);
700b5677b36Schristos cp += n;
701b5677b36Schristos ShrinkBuffer(n);
702b5677b36Schristos break;
703b5677b36Schristos default:
704b5677b36Schristos return (-1);
705b5677b36Schristos } /*switch*/
706b5677b36Schristos n = (u_int16_t)((cp - sp2) - INT16SZ);
707b5677b36Schristos PUTSHORT(n, sp2);
708b5677b36Schristos } /*for*/
709b5677b36Schristos
710b5677b36Schristos hp->qdcount = htons(counts[0]);
711b5677b36Schristos hp->ancount = htons(counts[1]);
712b5677b36Schristos hp->nscount = htons(counts[2]);
713b5677b36Schristos hp->arcount = htons(counts[3]);
714b5677b36Schristos return (cp - buf);
715b5677b36Schristos }
716b5677b36Schristos
717b5677b36Schristos /*%
718b5677b36Schristos * Get a whitespace delimited word from a string (not file)
719b5677b36Schristos * into buf. modify the start pointer to point after the
720b5677b36Schristos * word in the string.
721b5677b36Schristos */
722b5677b36Schristos static int
getword_str(char * buf,int size,u_char ** startpp,u_char * endp)723b5677b36Schristos getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
724b5677b36Schristos char *cp;
725b5677b36Schristos int c;
726b5677b36Schristos
727b5677b36Schristos for (cp = buf; *startpp <= endp; ) {
728b5677b36Schristos c = **startpp;
729b5677b36Schristos if (isspace(c) || c == '\0') {
730b5677b36Schristos if (cp != buf) /*%< trailing whitespace */
731b5677b36Schristos break;
732b5677b36Schristos else { /*%< leading whitespace */
733b5677b36Schristos (*startpp)++;
734b5677b36Schristos continue;
735b5677b36Schristos }
736b5677b36Schristos }
737b5677b36Schristos (*startpp)++;
738b5677b36Schristos if (cp >= buf+size-1)
739b5677b36Schristos break;
740b5677b36Schristos *cp++ = (u_char)c;
741b5677b36Schristos }
742b5677b36Schristos *cp = '\0';
743b5677b36Schristos return (cp != buf);
744b5677b36Schristos }
745b5677b36Schristos
746b5677b36Schristos /*%
747b5677b36Schristos * get a white spae delimited string from memory. Process quoted strings
748b5677b36Schristos * and \\DDD escapes. Return length or -1 on error. Returned string may
749b5677b36Schristos * contain nulls.
750b5677b36Schristos */
751b5677b36Schristos static char digits[] = "0123456789";
752b5677b36Schristos static int
getstr_str(char * buf,int size,u_char ** startpp,u_char * endp)753b5677b36Schristos getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
754b5677b36Schristos char *cp;
755b5677b36Schristos int c, c1 = 0;
756b5677b36Schristos int inquote = 0;
757b5677b36Schristos int seen_quote = 0;
758b5677b36Schristos int escape = 0;
759b5677b36Schristos int dig = 0;
760b5677b36Schristos
761b5677b36Schristos for (cp = buf; *startpp <= endp; ) {
762b5677b36Schristos if ((c = **startpp) == '\0')
763b5677b36Schristos break;
764b5677b36Schristos /* leading white space */
765b5677b36Schristos if ((cp == buf) && !seen_quote && isspace(c)) {
766b5677b36Schristos (*startpp)++;
767b5677b36Schristos continue;
768b5677b36Schristos }
769b5677b36Schristos
770b5677b36Schristos switch (c) {
771b5677b36Schristos case '\\':
772b5677b36Schristos if (!escape) {
773b5677b36Schristos escape = 1;
774b5677b36Schristos dig = 0;
775b5677b36Schristos c1 = 0;
776b5677b36Schristos (*startpp)++;
777b5677b36Schristos continue;
778b5677b36Schristos }
779b5677b36Schristos goto do_escape;
780b5677b36Schristos case '"':
781b5677b36Schristos if (!escape) {
782b5677b36Schristos inquote = !inquote;
783b5677b36Schristos seen_quote = 1;
784b5677b36Schristos (*startpp)++;
785b5677b36Schristos continue;
786b5677b36Schristos }
787b5677b36Schristos /* fall through */
788b5677b36Schristos default:
789b5677b36Schristos do_escape:
790b5677b36Schristos if (escape) {
791b5677b36Schristos switch (c) {
792b5677b36Schristos case '0':
793b5677b36Schristos case '1':
794b5677b36Schristos case '2':
795b5677b36Schristos case '3':
796b5677b36Schristos case '4':
797b5677b36Schristos case '5':
798b5677b36Schristos case '6':
799b5677b36Schristos case '7':
800b5677b36Schristos case '8':
801b5677b36Schristos case '9':
802b5677b36Schristos c1 = c1 * 10 +
803b5677b36Schristos (strchr(digits, c) - digits);
804b5677b36Schristos
805b5677b36Schristos if (++dig == 3) {
806b5677b36Schristos c = c1 &0xff;
807b5677b36Schristos break;
808b5677b36Schristos }
809b5677b36Schristos (*startpp)++;
810b5677b36Schristos continue;
811b5677b36Schristos }
812b5677b36Schristos escape = 0;
813b5677b36Schristos } else if (!inquote && isspace(c))
814b5677b36Schristos goto done;
815b5677b36Schristos if (cp >= buf+size-1)
816b5677b36Schristos goto done;
817b5677b36Schristos *cp++ = (u_char)c;
818b5677b36Schristos (*startpp)++;
819b5677b36Schristos }
820b5677b36Schristos }
821b5677b36Schristos done:
822b5677b36Schristos *cp = '\0';
823b5677b36Schristos return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
824b5677b36Schristos }
825b5677b36Schristos
826b5677b36Schristos /*%
827b5677b36Schristos * Get a whitespace delimited base 16 number from a string (not file) into buf
828b5677b36Schristos * update the start pointer to point after the number in the string.
829b5677b36Schristos */
830b5677b36Schristos static int
gethexnum_str(u_char ** startpp,u_char * endp)831b5677b36Schristos gethexnum_str(u_char **startpp, u_char *endp) {
832b5677b36Schristos int c, n;
833b5677b36Schristos int seendigit = 0;
834b5677b36Schristos int m = 0;
835b5677b36Schristos
836b5677b36Schristos if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
837b5677b36Schristos return getnum_str(startpp, endp);
838b5677b36Schristos (*startpp)+=2;
839b5677b36Schristos for (n = 0; *startpp <= endp; ) {
840b5677b36Schristos c = **startpp;
841b5677b36Schristos if (isspace(c) || c == '\0') {
842b5677b36Schristos if (seendigit) /*%< trailing whitespace */
843b5677b36Schristos break;
844b5677b36Schristos else { /*%< leading whitespace */
845b5677b36Schristos (*startpp)++;
846b5677b36Schristos continue;
847b5677b36Schristos }
848b5677b36Schristos }
849b5677b36Schristos if (c == ';') {
850b5677b36Schristos while ((*startpp <= endp) &&
851b5677b36Schristos ((c = **startpp) != '\n'))
852b5677b36Schristos (*startpp)++;
853b5677b36Schristos if (seendigit)
854b5677b36Schristos break;
855b5677b36Schristos continue;
856b5677b36Schristos }
857b5677b36Schristos if (!isxdigit(c)) {
858b5677b36Schristos if (c == ')' && seendigit) {
859b5677b36Schristos (*startpp)--;
860b5677b36Schristos break;
861b5677b36Schristos }
862b5677b36Schristos return (-1);
863b5677b36Schristos }
864b5677b36Schristos (*startpp)++;
865b5677b36Schristos if (isdigit(c))
866b5677b36Schristos n = n * 16 + (c - '0');
867b5677b36Schristos else
868b5677b36Schristos n = n * 16 + (tolower(c) - 'a' + 10);
869b5677b36Schristos seendigit = 1;
870b5677b36Schristos }
871b5677b36Schristos return (n + m);
872b5677b36Schristos }
873b5677b36Schristos
874b5677b36Schristos /*%
875b5677b36Schristos * Get a whitespace delimited base 10 number from a string (not file) into buf
876b5677b36Schristos * update the start pointer to point after the number in the string.
877b5677b36Schristos */
878b5677b36Schristos static int
getnum_str(u_char ** startpp,u_char * endp)879b5677b36Schristos getnum_str(u_char **startpp, u_char *endp) {
880b5677b36Schristos int c, n;
881b5677b36Schristos int seendigit = 0;
882b5677b36Schristos int m = 0;
883b5677b36Schristos
884b5677b36Schristos for (n = 0; *startpp <= endp; ) {
885b5677b36Schristos c = **startpp;
886b5677b36Schristos if (isspace(c) || c == '\0') {
887b5677b36Schristos if (seendigit) /*%< trailing whitespace */
888b5677b36Schristos break;
889b5677b36Schristos else { /*%< leading whitespace */
890b5677b36Schristos (*startpp)++;
891b5677b36Schristos continue;
892b5677b36Schristos }
893b5677b36Schristos }
894b5677b36Schristos if (c == ';') {
895b5677b36Schristos while ((*startpp <= endp) &&
896b5677b36Schristos ((c = **startpp) != '\n'))
897b5677b36Schristos (*startpp)++;
898b5677b36Schristos if (seendigit)
899b5677b36Schristos break;
900b5677b36Schristos continue;
901b5677b36Schristos }
902b5677b36Schristos if (!isdigit(c)) {
903b5677b36Schristos if (c == ')' && seendigit) {
904b5677b36Schristos (*startpp)--;
905b5677b36Schristos break;
906b5677b36Schristos }
907b5677b36Schristos return (-1);
908b5677b36Schristos }
909b5677b36Schristos (*startpp)++;
910b5677b36Schristos n = n * 10 + (c - '0');
911b5677b36Schristos seendigit = 1;
912b5677b36Schristos }
913b5677b36Schristos return (n + m);
914b5677b36Schristos }
915b5677b36Schristos
916b5677b36Schristos /*%
917b5677b36Schristos * Allocate a resource record buffer & save rr info.
918b5677b36Schristos */
919b5677b36Schristos ns_updrec *
res_mkupdrec(int section,const char * dname,u_int class,u_int type,u_long ttl)920b5677b36Schristos res_mkupdrec(int section, const char *dname,
921b5677b36Schristos u_int class, u_int type, u_long ttl) {
922b5677b36Schristos ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
923b5677b36Schristos
924b5677b36Schristos if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
925b5677b36Schristos if (rrecp)
926b5677b36Schristos free((char *)rrecp);
927b5677b36Schristos return (NULL);
928b5677b36Schristos }
929b5677b36Schristos INIT_LINK(rrecp, r_link);
930b5677b36Schristos INIT_LINK(rrecp, r_glink);
931b5677b36Schristos rrecp->r_class = (ns_class)class;
932b5677b36Schristos rrecp->r_type = (ns_type)type;
933b5677b36Schristos rrecp->r_ttl = ttl;
934b5677b36Schristos rrecp->r_section = (ns_sect)section;
935b5677b36Schristos return (rrecp);
936b5677b36Schristos }
937b5677b36Schristos
938b5677b36Schristos /*%
939b5677b36Schristos * Free a resource record buffer created by res_mkupdrec.
940b5677b36Schristos */
941b5677b36Schristos void
res_freeupdrec(ns_updrec * rrecp)942b5677b36Schristos res_freeupdrec(ns_updrec *rrecp) {
943b5677b36Schristos /* Note: freeing r_dp is the caller's responsibility. */
944b5677b36Schristos if (rrecp->r_dname != NULL)
945b5677b36Schristos free(rrecp->r_dname);
946b5677b36Schristos free(rrecp);
947b5677b36Schristos }
948b5677b36Schristos
949b5677b36Schristos struct valuelist {
950b5677b36Schristos struct valuelist * next;
951b5677b36Schristos struct valuelist * prev;
952b5677b36Schristos char * name;
953b5677b36Schristos char * proto;
954b5677b36Schristos int port;
955b5677b36Schristos };
956b5677b36Schristos static struct valuelist *servicelist, *protolist;
957b5677b36Schristos
958b5677b36Schristos static void
res_buildservicelist()959b5677b36Schristos res_buildservicelist() {
960b5677b36Schristos struct servent *sp;
961b5677b36Schristos struct valuelist *slp;
962b5677b36Schristos
963b5677b36Schristos #ifdef MAYBE_HESIOD
964b5677b36Schristos setservent(0);
965b5677b36Schristos #else
966b5677b36Schristos setservent(1);
967b5677b36Schristos #endif
968b5677b36Schristos while ((sp = getservent()) != NULL) {
969b5677b36Schristos slp = (struct valuelist *)malloc(sizeof(struct valuelist));
970b5677b36Schristos if (!slp)
971b5677b36Schristos break;
972b5677b36Schristos slp->name = strdup(sp->s_name);
973b5677b36Schristos slp->proto = strdup(sp->s_proto);
974b5677b36Schristos if ((slp->name == NULL) || (slp->proto == NULL)) {
975b5677b36Schristos if (slp->name) free(slp->name);
976b5677b36Schristos if (slp->proto) free(slp->proto);
977b5677b36Schristos free(slp);
978b5677b36Schristos break;
979b5677b36Schristos }
980b5677b36Schristos slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
981b5677b36Schristos slp->next = servicelist;
982b5677b36Schristos slp->prev = NULL;
983b5677b36Schristos if (servicelist)
984b5677b36Schristos servicelist->prev = slp;
985b5677b36Schristos servicelist = slp;
986b5677b36Schristos }
987b5677b36Schristos endservent();
988b5677b36Schristos }
989b5677b36Schristos
990b5677b36Schristos void
res_destroyservicelist()991b5677b36Schristos res_destroyservicelist() {
992b5677b36Schristos struct valuelist *slp, *slp_next;
993b5677b36Schristos
994b5677b36Schristos for (slp = servicelist; slp != NULL; slp = slp_next) {
995b5677b36Schristos slp_next = slp->next;
996b5677b36Schristos free(slp->name);
997b5677b36Schristos free(slp->proto);
998b5677b36Schristos free(slp);
999b5677b36Schristos }
1000b5677b36Schristos servicelist = (struct valuelist *)0;
1001b5677b36Schristos }
1002b5677b36Schristos
1003b5677b36Schristos void
res_buildprotolist(void)1004b5677b36Schristos res_buildprotolist(void) {
1005b5677b36Schristos struct protoent *pp;
1006b5677b36Schristos struct valuelist *slp;
1007b5677b36Schristos
1008b5677b36Schristos #ifdef MAYBE_HESIOD
1009b5677b36Schristos setprotoent(0);
1010b5677b36Schristos #else
1011b5677b36Schristos setprotoent(1);
1012b5677b36Schristos #endif
1013b5677b36Schristos while ((pp = getprotoent()) != NULL) {
1014b5677b36Schristos slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1015b5677b36Schristos if (!slp)
1016b5677b36Schristos break;
1017b5677b36Schristos slp->name = strdup(pp->p_name);
1018b5677b36Schristos if (slp->name == NULL) {
1019b5677b36Schristos free(slp);
1020b5677b36Schristos break;
1021b5677b36Schristos }
1022b5677b36Schristos slp->port = pp->p_proto; /*%< host byte order */
1023b5677b36Schristos slp->next = protolist;
1024b5677b36Schristos slp->prev = NULL;
1025b5677b36Schristos if (protolist)
1026b5677b36Schristos protolist->prev = slp;
1027b5677b36Schristos protolist = slp;
1028b5677b36Schristos }
1029b5677b36Schristos endprotoent();
1030b5677b36Schristos }
1031b5677b36Schristos
1032b5677b36Schristos void
res_destroyprotolist(void)1033b5677b36Schristos res_destroyprotolist(void) {
1034b5677b36Schristos struct valuelist *plp, *plp_next;
1035b5677b36Schristos
1036b5677b36Schristos for (plp = protolist; plp != NULL; plp = plp_next) {
1037b5677b36Schristos plp_next = plp->next;
1038b5677b36Schristos free(plp->name);
1039b5677b36Schristos free(plp);
1040b5677b36Schristos }
1041b5677b36Schristos protolist = (struct valuelist *)0;
1042b5677b36Schristos }
1043b5677b36Schristos
1044b5677b36Schristos static int
findservice(const char * s,struct valuelist ** list)1045b5677b36Schristos findservice(const char *s, struct valuelist **list) {
1046b5677b36Schristos struct valuelist *lp = *list;
1047b5677b36Schristos int n;
1048b5677b36Schristos
1049b5677b36Schristos for (; lp != NULL; lp = lp->next)
1050b5677b36Schristos if (strcasecmp(lp->name, s) == 0) {
1051b5677b36Schristos if (lp != *list) {
1052b5677b36Schristos lp->prev->next = lp->next;
1053b5677b36Schristos if (lp->next)
1054b5677b36Schristos lp->next->prev = lp->prev;
1055b5677b36Schristos (*list)->prev = lp;
1056b5677b36Schristos lp->next = *list;
1057b5677b36Schristos *list = lp;
1058b5677b36Schristos }
1059b5677b36Schristos return (lp->port); /*%< host byte order */
1060b5677b36Schristos }
1061b5677b36Schristos if (sscanf(s, "%d", &n) != 1 || n <= 0)
1062b5677b36Schristos n = -1;
1063b5677b36Schristos return (n);
1064b5677b36Schristos }
1065b5677b36Schristos
1066b5677b36Schristos /*%
1067b5677b36Schristos * Convert service name or (ascii) number to int.
1068b5677b36Schristos */
1069b5677b36Schristos int
res_servicenumber(const char * p)1070b5677b36Schristos res_servicenumber(const char *p) {
1071b5677b36Schristos if (servicelist == (struct valuelist *)0)
1072b5677b36Schristos res_buildservicelist();
1073b5677b36Schristos return (findservice(p, &servicelist));
1074b5677b36Schristos }
1075b5677b36Schristos
1076b5677b36Schristos /*%
1077b5677b36Schristos * Convert protocol name or (ascii) number to int.
1078b5677b36Schristos */
1079b5677b36Schristos int
res_protocolnumber(const char * p)1080b5677b36Schristos res_protocolnumber(const char *p) {
1081b5677b36Schristos if (protolist == (struct valuelist *)0)
1082b5677b36Schristos res_buildprotolist();
1083b5677b36Schristos return (findservice(p, &protolist));
1084b5677b36Schristos }
1085b5677b36Schristos
1086b5677b36Schristos static struct servent *
cgetservbyport(u_int16_t port,const char * proto)1087b5677b36Schristos cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1088b5677b36Schristos struct valuelist **list = &servicelist;
1089b5677b36Schristos struct valuelist *lp = *list;
1090b5677b36Schristos static struct servent serv;
1091b5677b36Schristos
1092b5677b36Schristos port = ntohs(port);
1093b5677b36Schristos for (; lp != NULL; lp = lp->next) {
1094b5677b36Schristos if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1095b5677b36Schristos continue;
1096b5677b36Schristos if (strcasecmp(lp->proto, proto) == 0) {
1097b5677b36Schristos if (lp != *list) {
1098b5677b36Schristos lp->prev->next = lp->next;
1099b5677b36Schristos if (lp->next)
1100b5677b36Schristos lp->next->prev = lp->prev;
1101b5677b36Schristos (*list)->prev = lp;
1102b5677b36Schristos lp->next = *list;
1103b5677b36Schristos *list = lp;
1104b5677b36Schristos }
1105b5677b36Schristos serv.s_name = lp->name;
1106b5677b36Schristos serv.s_port = htons((u_int16_t)lp->port);
1107b5677b36Schristos serv.s_proto = lp->proto;
1108b5677b36Schristos return (&serv);
1109b5677b36Schristos }
1110b5677b36Schristos }
1111b5677b36Schristos return (0);
1112b5677b36Schristos }
1113b5677b36Schristos
1114b5677b36Schristos static struct protoent *
cgetprotobynumber(int proto)1115b5677b36Schristos cgetprotobynumber(int proto) { /*%< Host byte order. */
1116b5677b36Schristos struct valuelist **list = &protolist;
1117b5677b36Schristos struct valuelist *lp = *list;
1118b5677b36Schristos static struct protoent prot;
1119b5677b36Schristos
1120b5677b36Schristos for (; lp != NULL; lp = lp->next)
1121b5677b36Schristos if (lp->port == proto) { /*%< Host byte order. */
1122b5677b36Schristos if (lp != *list) {
1123b5677b36Schristos lp->prev->next = lp->next;
1124b5677b36Schristos if (lp->next)
1125b5677b36Schristos lp->next->prev = lp->prev;
1126b5677b36Schristos (*list)->prev = lp;
1127b5677b36Schristos lp->next = *list;
1128b5677b36Schristos *list = lp;
1129b5677b36Schristos }
1130b5677b36Schristos prot.p_name = lp->name;
1131b5677b36Schristos prot.p_proto = lp->port; /*%< Host byte order. */
1132b5677b36Schristos return (&prot);
1133b5677b36Schristos }
1134b5677b36Schristos return (0);
1135b5677b36Schristos }
1136b5677b36Schristos
1137b5677b36Schristos const char *
res_protocolname(int num)1138b5677b36Schristos res_protocolname(int num) {
1139b5677b36Schristos static char number[8];
1140b5677b36Schristos struct protoent *pp;
1141b5677b36Schristos
1142b5677b36Schristos if (protolist == (struct valuelist *)0)
1143b5677b36Schristos res_buildprotolist();
1144b5677b36Schristos pp = cgetprotobynumber(num);
1145b5677b36Schristos if (pp == 0) {
1146b5677b36Schristos (void) sprintf(number, "%d", num);
1147b5677b36Schristos return (number);
1148b5677b36Schristos }
1149b5677b36Schristos return (pp->p_name);
1150b5677b36Schristos }
1151b5677b36Schristos
1152b5677b36Schristos const char *
res_servicename(u_int16_t port,const char * proto)1153b5677b36Schristos res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1154b5677b36Schristos static char number[8];
1155b5677b36Schristos struct servent *ss;
1156b5677b36Schristos
1157b5677b36Schristos if (servicelist == (struct valuelist *)0)
1158b5677b36Schristos res_buildservicelist();
1159b5677b36Schristos ss = cgetservbyport(htons(port), proto);
1160b5677b36Schristos if (ss == 0) {
1161b5677b36Schristos (void) sprintf(number, "%d", port);
1162b5677b36Schristos return (number);
1163b5677b36Schristos }
1164b5677b36Schristos return (ss->s_name);
1165b5677b36Schristos }
1166