2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Based on the Dynamic DNS reference implementation by Viraj Bais
23 * <viraj_bais@ccm.fm.intel.com>
26 #if !defined(lint) && !defined(SABER)
27 static const char rcsid[] = "$Id: res_mkupdate.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "port_before.h"
34 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #include <arpa/inet.h>
44 #include <res_update.h>
55 #include "port_after.h"
57 /* Options. Leave them on. */
63 static int getnum_str(u_char **, u_char *);
64 static int gethexnum_str(u_char **, u_char *);
65 static int getword_str(char *, int, u_char **, u_char *);
66 static int getstr_str(char *, int, u_char **, u_char *);
68 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
75 int res_protocolnumber(const char *);
79 int res_servicenumber(const char *);
82 * Form update packets.
83 * Returns the size of the resulting packet if no error
87 *\li -1 if error in reading a word/number in rdata
88 * portion for update packets
89 *\li -2 if length of buffer passed is insufficient
90 *\li -3 if zone section is not the first section in
91 * the linked list, or section order has a problem
92 *\li -4 on a number overflow
93 *\li -5 unknown operation or no records
96 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
97 ns_updrec *rrecp_start = rrecp_in;
99 u_char *cp, *sp2, *startp, *endp;
100 int n, i, soanum, multiline;
103 struct in6_addr in6a;
105 u_char buf3[MAXDNAME];
106 int section, numrrs = 0, counts[ns_s_max];
107 u_int16_t rtype, rclass;
109 u_char *dnptrs[20], **dpp, **lastdnptr;
116 * Initialize header fields.
118 if ((buf == NULL) || (buflen < HFIXEDSZ))
120 memset(buf, 0, HFIXEDSZ);
122 statp->id = res_nrandomid(statp);
123 hp->id = htons(statp->id);
124 hp->opcode = ns_o_update;
131 lastdnptr = dnptrs + nitems(dnptrs);
133 if (rrecp_start == NULL)
135 else if (rrecp_start->r_section != S_ZONE)
138 memset(counts, 0, sizeof counts);
139 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
141 section = rrecp->r_section;
142 if (section < 0 || section >= ns_s_max)
145 for (i = section + 1; i < ns_s_max; i++)
148 rtype = rrecp->r_type;
149 rclass = rrecp->r_class;
151 /* overload class and type */
152 if (section == S_PREREQ) {
154 switch (rrecp->r_opcode) {
170 if (rrecp->r_size == 0)
175 "res_mkupdate: incorrect opcode: %d\n",
180 } else if (section == S_UPDATE) {
181 switch (rrecp->r_opcode) {
183 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
189 "res_mkupdate: incorrect opcode: %d\n",
197 * XXX appending default domain to owner name is omitted,
198 * fqdn must be provided
200 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
204 ShrinkBuffer(n + 2*INT16SZ);
206 PUTSHORT(rclass, cp);
207 if (section == S_ZONE) {
208 if (numrrs != 1 || rrecp->r_type != T_SOA)
212 ShrinkBuffer(INT32SZ + INT16SZ);
214 sp2 = cp; /*%< save pointer to length byte */
216 if (rrecp->r_size == 0) {
217 if (section == S_UPDATE && rclass != C_ANY)
224 startp = rrecp->r_data;
225 endp = startp + rrecp->r_size - 1;
226 /* XXX this should be done centrally. */
227 switch (rrecp->r_type) {
229 if (!getword_str(buf2, sizeof buf2, &startp, endp))
231 if (!inet_aton(buf2, &ina))
233 n1 = ntohl(ina.s_addr);
234 ShrinkBuffer(INT32SZ);
244 if (!getword_str(buf2, sizeof buf2, &startp, endp))
246 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
255 for (i = 0; i < 2; i++) {
256 if (!getword_str(buf2, sizeof buf2, &startp,
259 n = dn_comp(buf2, cp, buflen,
266 if (rrecp->r_type == T_SOA) {
267 ShrinkBuffer(5 * INT32SZ);
268 while (isspace(*startp) || !*startp)
270 if (*startp == '(') {
275 /* serial, refresh, retry, expire, minimum */
276 for (i = 0; i < 5; i++) {
277 soanum = getnum_str(&startp, endp);
283 while (isspace(*startp) || !*startp)
293 n = getnum_str(&startp, endp);
296 ShrinkBuffer(INT16SZ);
298 if (!getword_str(buf2, sizeof buf2, &startp, endp))
300 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
307 n = getnum_str(&startp, endp);
310 ShrinkBuffer(INT16SZ);
313 n = getnum_str(&startp, endp);
316 ShrinkBuffer(INT16SZ);
319 n = getnum_str(&startp, endp);
322 ShrinkBuffer(INT16SZ);
325 if (!getword_str(buf2, sizeof buf2, &startp, endp))
327 n = dn_comp(buf2, cp, buflen, NULL, NULL);
334 n = getnum_str(&startp, endp);
338 ShrinkBuffer(INT16SZ);
339 for (i = 0; i < 2; i++) {
340 if (!getword_str(buf2, sizeof buf2, &startp,
343 n = dn_comp(buf2, cp, buflen, dnptrs,
353 unsigned int maxbm = 0;
355 if (!getword_str(buf2, sizeof buf2, &startp, endp))
357 if (!inet_aton(buf2, &ina))
359 n1 = ntohl(ina.s_addr);
360 ShrinkBuffer(INT32SZ);
363 if (!getword_str(buf2, sizeof buf2, &startp, endp))
365 if ((i = res_protocolnumber(buf2)) < 0)
370 for (i = 0; i < MAXPORT/8 ; i++)
373 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
374 if ((n = res_servicenumber(buf2)) <= 0)
378 bm[n/8] |= (0x80>>(n%8));
379 if ((unsigned)n > maxbm)
386 memcpy(cp, bm, maxbm);
391 for (i = 0; i < 2; i++) {
392 if ((n = getstr_str(buf2, sizeof buf2,
405 if ((n = getstr_str(buf2, sizeof buf2,
406 &startp, endp)) < 0) {
407 if (cp != (sp2 + INT16SZ))
421 if ((n = getstr_str(buf2, sizeof buf2, &startp,
433 if ((n = getstr_str(buf2, sizeof buf2, &startp,
436 if ((n > 255) || (n == 0))
442 if ((n = getstr_str(buf2, sizeof buf2, &startp,
453 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
462 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
474 int sig_type, success, dateerror;
475 u_int32_t exptime, timesigned;
478 if ((n = getword_str(buf2, sizeof buf2,
481 sig_type = sym_ston(__p_type_syms, buf2, &success);
482 if (!success || sig_type == ns_t_any)
484 ShrinkBuffer(INT16SZ);
485 PUTSHORT(sig_type, cp);
487 n = getnum_str(&startp, endp);
493 n = getnum_str(&startp, endp);
494 if (n <= 0 || n > 255)
499 if (!getword_str(buf2, sizeof buf2, &startp, endp))
501 exptime = ns_datetosecs(buf2, &dateerror);
503 ShrinkBuffer(INT32SZ);
511 ottl = strtoul(buf2, &ulendp, 10);
513 (ulendp != NULL && *ulendp != '\0'))
515 ShrinkBuffer(INT32SZ);
517 if (!getword_str(buf2, sizeof buf2, &startp,
520 exptime = ns_datetosecs(buf2, &dateerror);
525 ShrinkBuffer(INT32SZ);
526 PUTLONG(exptime, cp);
528 if (!getword_str(buf2, sizeof buf2, &startp, endp))
530 timesigned = ns_datetosecs(buf2, &dateerror);
532 ShrinkBuffer(INT32SZ);
533 PUTLONG(timesigned, cp);
538 n = getnum_str(&startp, endp);
541 ShrinkBuffer(INT16SZ);
544 if (!getword_str(buf2, sizeof buf2, &startp, endp))
546 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
552 if ((n = getword_str(buf2, sizeof buf2,
555 siglen = b64_pton(buf2, buf3, sizeof(buf3));
558 ShrinkBuffer(siglen);
559 memcpy(cp, buf3, siglen);
566 n = gethexnum_str(&startp, endp);
569 ShrinkBuffer(INT16SZ);
572 n = getnum_str(&startp, endp);
578 n = getnum_str(&startp, endp);
584 if ((n = getword_str(buf2, sizeof buf2,
587 keylen = b64_pton(buf2, buf3, sizeof(buf3));
590 ShrinkBuffer(keylen);
591 memcpy(cp, buf3, keylen);
596 int success, nxt_type;
601 if (!getword_str(buf2, sizeof buf2, &startp, endp))
603 n = dn_comp(buf2, cp, buflen, NULL, NULL);
609 memset(data, 0, sizeof data);
611 if (!getword_str(buf2, sizeof buf2, &startp,
614 nxt_type = sym_ston(__p_type_syms, buf2,
616 if (!success || !ns_t_rr_p(nxt_type))
618 NS_NXT_BIT_SET(nxt_type, data);
619 if (nxt_type > maxtype)
622 n = maxtype/NS_NXT_BITS+1;
630 n = getnum_str(&startp, endp);
633 ShrinkBuffer(INT16SZ);
636 n = getnum_str(&startp, endp);
639 ShrinkBuffer(INT16SZ);
642 n = getnum_str(&startp, endp);
648 if ((n = getword_str(buf2, sizeof buf2,
651 certlen = b64_pton(buf2, buf3, sizeof(buf3));
654 ShrinkBuffer(certlen);
655 memcpy(cp, buf3, certlen);
659 if (!getword_str(buf2, sizeof buf2, &startp, endp))
661 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
663 ShrinkBuffer(NS_IN6ADDRSZ);
664 memcpy(cp, &in6a, NS_IN6ADDRSZ);
668 /* Order Preference Flags Service Replacement Regexp */
670 n = getnum_str(&startp, endp);
671 if (n < 0 || n > 65535)
673 ShrinkBuffer(INT16SZ);
676 n = getnum_str(&startp, endp);
677 if (n < 0 || n > 65535)
679 ShrinkBuffer(INT16SZ);
682 if ((n = getstr_str(buf2, sizeof buf2,
683 &startp, endp)) < 0) {
692 /* Service Classes */
693 if ((n = getstr_str(buf2, sizeof buf2,
694 &startp, endp)) < 0) {
704 if ((n = getstr_str(buf2, sizeof buf2,
705 &startp, endp)) < 0) {
715 if (!getword_str(buf2, sizeof buf2, &startp, endp))
717 n = dn_comp(buf2, cp, buflen, NULL, NULL);
726 n = (u_int16_t)((cp - sp2) - INT16SZ);
730 hp->qdcount = htons(counts[0]);
731 hp->ancount = htons(counts[1]);
732 hp->nscount = htons(counts[2]);
733 hp->arcount = htons(counts[3]);
738 * Get a whitespace delimited word from a string (not file)
739 * into buf. modify the start pointer to point after the
740 * word in the string.
743 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
747 for (cp = buf; *startpp <= endp; ) {
749 if (isspace(c) || c == '\0') {
750 if (cp != buf) /*%< trailing whitespace */
752 else { /*%< leading whitespace */
758 if (cp >= buf+size-1)
767 * get a white spae delimited string from memory. Process quoted strings
768 * and \\DDD escapes. Return length or -1 on error. Returned string may
771 static char digits[] = "0123456789";
773 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
781 for (cp = buf; *startpp <= endp; ) {
782 if ((c = **startpp) == '\0')
784 /* leading white space */
785 if ((cp == buf) && !seen_quote && isspace(c)) {
823 (strchr(digits, c) - digits);
833 } else if (!inquote && isspace(c))
835 if (cp >= buf+size-1)
843 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
847 * Get a whitespace delimited base 16 number from a string (not file) into buf
848 * update the start pointer to point after the number in the string.
851 gethexnum_str(u_char **startpp, u_char *endp) {
856 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
857 return getnum_str(startpp, endp);
859 for (n = 0; *startpp <= endp; ) {
861 if (isspace(c) || c == '\0') {
862 if (seendigit) /*%< trailing whitespace */
864 else { /*%< leading whitespace */
870 while ((*startpp <= endp) &&
871 ((c = **startpp) != '\n'))
878 if (c == ')' && seendigit) {
886 n = n * 16 + (c - '0');
888 n = n * 16 + (tolower(c) - 'a' + 10);
895 * Get a whitespace delimited base 10 number from a string (not file) into buf
896 * update the start pointer to point after the number in the string.
899 getnum_str(u_char **startpp, u_char *endp) {
904 for (n = 0; *startpp <= endp; ) {
906 if (isspace(c) || c == '\0') {
907 if (seendigit) /*%< trailing whitespace */
909 else { /*%< leading whitespace */
915 while ((*startpp <= endp) &&
916 ((c = **startpp) != '\n'))
923 if (c == ')' && seendigit) {
930 n = n * 10 + (c - '0');
937 * Allocate a resource record buffer & save rr info.
940 res_mkupdrec(int section, const char *dname,
941 u_int class, u_int type, u_long ttl) {
942 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
944 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
949 INIT_LINK(rrecp, r_link);
950 INIT_LINK(rrecp, r_glink);
951 rrecp->r_class = (ns_class)class;
952 rrecp->r_type = (ns_type)type;
954 rrecp->r_section = (ns_sect)section;
959 * Free a resource record buffer created by res_mkupdrec.
962 res_freeupdrec(ns_updrec *rrecp) {
963 /* Note: freeing r_dp is the caller's responsibility. */
964 if (rrecp->r_dname != NULL)
965 free(rrecp->r_dname);
970 struct valuelist * next;
971 struct valuelist * prev;
976 static struct valuelist *servicelist, *protolist;
979 res_buildservicelist(void) {
981 struct valuelist *slp;
988 while ((sp = getservent()) != NULL) {
989 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
992 slp->name = strdup(sp->s_name);
993 slp->proto = strdup(sp->s_proto);
994 if ((slp->name == NULL) || (slp->proto == NULL)) {
995 if (slp->name) free(slp->name);
996 if (slp->proto) free(slp->proto);
1000 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
1001 slp->next = servicelist;
1004 servicelist->prev = slp;
1012 res_destroyservicelist() {
1013 struct valuelist *slp, *slp_next;
1015 for (slp = servicelist; slp != NULL; slp = slp_next) {
1016 slp_next = slp->next;
1021 servicelist = (struct valuelist *)0;
1029 res_buildprotolist(void) {
1030 struct protoent *pp;
1031 struct valuelist *slp;
1038 while ((pp = getprotoent()) != NULL) {
1039 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1042 slp->name = strdup(pp->p_name);
1043 if (slp->name == NULL) {
1047 slp->port = pp->p_proto; /*%< host byte order */
1048 slp->next = protolist;
1051 protolist->prev = slp;
1059 res_destroyprotolist(void) {
1060 struct valuelist *plp, *plp_next;
1062 for (plp = protolist; plp != NULL; plp = plp_next) {
1063 plp_next = plp->next;
1067 protolist = (struct valuelist *)0;
1072 findservice(const char *s, struct valuelist **list) {
1073 struct valuelist *lp = *list;
1076 for (; lp != NULL; lp = lp->next)
1077 if (strcasecmp(lp->name, s) == 0) {
1079 lp->prev->next = lp->next;
1081 lp->next->prev = lp->prev;
1086 return (lp->port); /*%< host byte order */
1088 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1094 * Convert service name or (ascii) number to int.
1100 res_servicenumber(const char *p) {
1101 if (servicelist == (struct valuelist *)0)
1102 res_buildservicelist();
1103 return (findservice(p, &servicelist));
1107 * Convert protocol name or (ascii) number to int.
1113 res_protocolnumber(const char *p) {
1114 if (protolist == (struct valuelist *)0)
1115 res_buildprotolist();
1116 return (findservice(p, &protolist));
1120 static struct servent *
1121 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1122 struct valuelist **list = &servicelist;
1123 struct valuelist *lp = *list;
1124 static struct servent serv;
1127 for (; lp != NULL; lp = lp->next) {
1128 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1130 if (strcasecmp(lp->proto, proto) == 0) {
1132 lp->prev->next = lp->next;
1134 lp->next->prev = lp->prev;
1139 serv.s_name = lp->name;
1140 serv.s_port = htons((u_int16_t)lp->port);
1141 serv.s_proto = lp->proto;
1148 static struct protoent *
1149 cgetprotobynumber(int proto) { /*%< Host byte order. */
1150 struct valuelist **list = &protolist;
1151 struct valuelist *lp = *list;
1152 static struct protoent prot;
1154 for (; lp != NULL; lp = lp->next)
1155 if (lp->port == proto) { /*%< Host byte order. */
1157 lp->prev->next = lp->next;
1159 lp->next->prev = lp->prev;
1164 prot.p_name = lp->name;
1165 prot.p_proto = lp->port; /*%< Host byte order. */
1172 res_protocolname(int num) {
1173 static char number[8];
1174 struct protoent *pp;
1176 if (protolist == (struct valuelist *)0)
1177 res_buildprotolist();
1178 pp = cgetprotobynumber(num);
1180 (void) sprintf(number, "%d", num);
1183 return (pp->p_name);
1187 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1188 static char number[8];
1191 if (servicelist == (struct valuelist *)0)
1192 res_buildservicelist();
1193 ss = cgetservbyport(htons(port), proto);
1195 (void) sprintf(number, "%d", port);
1198 return (ss->s_name);