2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Based on the Dynamic DNS reference implementation by Viraj Bais
21 * <viraj_bais@ccm.fm.intel.com>
24 #if !defined(lint) && !defined(SABER)
25 static const char rcsid[] = "$Id: res_mkupdate.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "port_before.h"
32 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
36 #include <arpa/inet.h>
42 #include <res_update.h>
53 #include "port_after.h"
55 /* Options. Leave them on. */
61 static int getnum_str(u_char **, u_char *);
62 static int gethexnum_str(u_char **, u_char *);
63 static int getword_str(char *, int, u_char **, u_char *);
64 static int getstr_str(char *, int, u_char **, u_char *);
66 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
73 int res_protocolnumber(const char *);
77 int res_servicenumber(const char *);
80 * Form update packets.
81 * Returns the size of the resulting packet if no error
85 *\li -1 if error in reading a word/number in rdata
86 * portion for update packets
87 *\li -2 if length of buffer passed is insufficient
88 *\li -3 if zone section is not the first section in
89 * the linked list, or section order has a problem
90 *\li -4 on a number overflow
91 *\li -5 unknown operation or no records
94 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
95 ns_updrec *rrecp_start = rrecp_in;
97 u_char *cp, *sp2, *startp, *endp;
98 int n, i, soanum, multiline;
101 struct in6_addr in6a;
103 u_char buf3[MAXDNAME];
104 int section, numrrs = 0, counts[ns_s_max];
105 u_int16_t rtype, rclass;
107 u_char *dnptrs[20], **dpp, **lastdnptr;
114 * Initialize header fields.
116 if ((buf == NULL) || (buflen < HFIXEDSZ))
118 memset(buf, 0, HFIXEDSZ);
120 statp->id = res_nrandomid(statp);
121 hp->id = htons(statp->id);
122 hp->opcode = ns_o_update;
129 lastdnptr = dnptrs + nitems(dnptrs);
131 if (rrecp_start == NULL)
133 else if (rrecp_start->r_section != S_ZONE)
136 memset(counts, 0, sizeof counts);
137 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
139 section = rrecp->r_section;
140 if (section < 0 || section >= ns_s_max)
143 for (i = section + 1; i < ns_s_max; i++)
146 rtype = rrecp->r_type;
147 rclass = rrecp->r_class;
149 /* overload class and type */
150 if (section == S_PREREQ) {
152 switch (rrecp->r_opcode) {
168 if (rrecp->r_size == 0)
173 "res_mkupdate: incorrect opcode: %d\n",
178 } else if (section == S_UPDATE) {
179 switch (rrecp->r_opcode) {
181 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
187 "res_mkupdate: incorrect opcode: %d\n",
195 * XXX appending default domain to owner name is omitted,
196 * fqdn must be provided
198 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
202 ShrinkBuffer(n + 2*INT16SZ);
204 PUTSHORT(rclass, cp);
205 if (section == S_ZONE) {
206 if (numrrs != 1 || rrecp->r_type != T_SOA)
210 ShrinkBuffer(INT32SZ + INT16SZ);
212 sp2 = cp; /*%< save pointer to length byte */
214 if (rrecp->r_size == 0) {
215 if (section == S_UPDATE && rclass != C_ANY)
222 startp = rrecp->r_data;
223 endp = startp + rrecp->r_size - 1;
224 /* XXX this should be done centrally. */
225 switch (rrecp->r_type) {
227 if (!getword_str(buf2, sizeof buf2, &startp, endp))
229 if (!inet_aton(buf2, &ina))
231 n1 = ntohl(ina.s_addr);
232 ShrinkBuffer(INT32SZ);
242 if (!getword_str(buf2, sizeof buf2, &startp, endp))
244 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
253 for (i = 0; i < 2; i++) {
254 if (!getword_str(buf2, sizeof buf2, &startp,
257 n = dn_comp(buf2, cp, buflen,
264 if (rrecp->r_type == T_SOA) {
265 ShrinkBuffer(5 * INT32SZ);
266 while (isspace(*startp) || !*startp)
268 if (*startp == '(') {
273 /* serial, refresh, retry, expire, minimum */
274 for (i = 0; i < 5; i++) {
275 soanum = getnum_str(&startp, endp);
281 while (isspace(*startp) || !*startp)
291 n = getnum_str(&startp, endp);
294 ShrinkBuffer(INT16SZ);
296 if (!getword_str(buf2, sizeof buf2, &startp, endp))
298 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
305 n = getnum_str(&startp, endp);
308 ShrinkBuffer(INT16SZ);
311 n = getnum_str(&startp, endp);
314 ShrinkBuffer(INT16SZ);
317 n = getnum_str(&startp, endp);
320 ShrinkBuffer(INT16SZ);
323 if (!getword_str(buf2, sizeof buf2, &startp, endp))
325 n = dn_comp(buf2, cp, buflen, NULL, NULL);
332 n = getnum_str(&startp, endp);
336 ShrinkBuffer(INT16SZ);
337 for (i = 0; i < 2; i++) {
338 if (!getword_str(buf2, sizeof buf2, &startp,
341 n = dn_comp(buf2, cp, buflen, dnptrs,
351 unsigned int maxbm = 0;
353 if (!getword_str(buf2, sizeof buf2, &startp, endp))
355 if (!inet_aton(buf2, &ina))
357 n1 = ntohl(ina.s_addr);
358 ShrinkBuffer(INT32SZ);
361 if (!getword_str(buf2, sizeof buf2, &startp, endp))
363 if ((i = res_protocolnumber(buf2)) < 0)
368 for (i = 0; i < MAXPORT/8 ; i++)
371 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
372 if ((n = res_servicenumber(buf2)) <= 0)
376 bm[n/8] |= (0x80>>(n%8));
377 if ((unsigned)n > maxbm)
384 memcpy(cp, bm, maxbm);
389 for (i = 0; i < 2; i++) {
390 if ((n = getstr_str(buf2, sizeof buf2,
403 if ((n = getstr_str(buf2, sizeof buf2,
404 &startp, endp)) < 0) {
405 if (cp != (sp2 + INT16SZ))
419 if ((n = getstr_str(buf2, sizeof buf2, &startp,
431 if ((n = getstr_str(buf2, sizeof buf2, &startp,
434 if ((n > 255) || (n == 0))
440 if ((n = getstr_str(buf2, sizeof buf2, &startp,
451 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
460 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
472 int sig_type, success, dateerror;
473 u_int32_t exptime, timesigned;
476 if ((n = getword_str(buf2, sizeof buf2,
479 sig_type = sym_ston(__p_type_syms, buf2, &success);
480 if (!success || sig_type == ns_t_any)
482 ShrinkBuffer(INT16SZ);
483 PUTSHORT(sig_type, cp);
485 n = getnum_str(&startp, endp);
491 n = getnum_str(&startp, endp);
492 if (n <= 0 || n > 255)
497 if (!getword_str(buf2, sizeof buf2, &startp, endp))
499 exptime = ns_datetosecs(buf2, &dateerror);
501 ShrinkBuffer(INT32SZ);
509 ottl = strtoul(buf2, &ulendp, 10);
511 (ulendp != NULL && *ulendp != '\0'))
513 ShrinkBuffer(INT32SZ);
515 if (!getword_str(buf2, sizeof buf2, &startp,
518 exptime = ns_datetosecs(buf2, &dateerror);
523 ShrinkBuffer(INT32SZ);
524 PUTLONG(exptime, cp);
526 if (!getword_str(buf2, sizeof buf2, &startp, endp))
528 timesigned = ns_datetosecs(buf2, &dateerror);
530 ShrinkBuffer(INT32SZ);
531 PUTLONG(timesigned, cp);
536 n = getnum_str(&startp, endp);
539 ShrinkBuffer(INT16SZ);
542 if (!getword_str(buf2, sizeof buf2, &startp, endp))
544 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
550 if ((n = getword_str(buf2, sizeof buf2,
553 siglen = b64_pton(buf2, buf3, sizeof(buf3));
556 ShrinkBuffer(siglen);
557 memcpy(cp, buf3, siglen);
564 n = gethexnum_str(&startp, endp);
567 ShrinkBuffer(INT16SZ);
570 n = getnum_str(&startp, endp);
576 n = getnum_str(&startp, endp);
582 if ((n = getword_str(buf2, sizeof buf2,
585 keylen = b64_pton(buf2, buf3, sizeof(buf3));
588 ShrinkBuffer(keylen);
589 memcpy(cp, buf3, keylen);
594 int success, nxt_type;
599 if (!getword_str(buf2, sizeof buf2, &startp, endp))
601 n = dn_comp(buf2, cp, buflen, NULL, NULL);
607 memset(data, 0, sizeof data);
609 if (!getword_str(buf2, sizeof buf2, &startp,
612 nxt_type = sym_ston(__p_type_syms, buf2,
614 if (!success || !ns_t_rr_p(nxt_type))
616 NS_NXT_BIT_SET(nxt_type, data);
617 if (nxt_type > maxtype)
620 n = maxtype/NS_NXT_BITS+1;
628 n = getnum_str(&startp, endp);
631 ShrinkBuffer(INT16SZ);
634 n = getnum_str(&startp, endp);
637 ShrinkBuffer(INT16SZ);
640 n = getnum_str(&startp, endp);
646 if ((n = getword_str(buf2, sizeof buf2,
649 certlen = b64_pton(buf2, buf3, sizeof(buf3));
652 ShrinkBuffer(certlen);
653 memcpy(cp, buf3, certlen);
657 if (!getword_str(buf2, sizeof buf2, &startp, endp))
659 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
661 ShrinkBuffer(NS_IN6ADDRSZ);
662 memcpy(cp, &in6a, NS_IN6ADDRSZ);
666 /* Order Preference Flags Service Replacement Regexp */
668 n = getnum_str(&startp, endp);
669 if (n < 0 || n > 65535)
671 ShrinkBuffer(INT16SZ);
674 n = getnum_str(&startp, endp);
675 if (n < 0 || n > 65535)
677 ShrinkBuffer(INT16SZ);
680 if ((n = getstr_str(buf2, sizeof buf2,
681 &startp, endp)) < 0) {
690 /* Service Classes */
691 if ((n = getstr_str(buf2, sizeof buf2,
692 &startp, endp)) < 0) {
702 if ((n = getstr_str(buf2, sizeof buf2,
703 &startp, endp)) < 0) {
713 if (!getword_str(buf2, sizeof buf2, &startp, endp))
715 n = dn_comp(buf2, cp, buflen, NULL, NULL);
724 n = (u_int16_t)((cp - sp2) - INT16SZ);
728 hp->qdcount = htons(counts[0]);
729 hp->ancount = htons(counts[1]);
730 hp->nscount = htons(counts[2]);
731 hp->arcount = htons(counts[3]);
736 * Get a whitespace delimited word from a string (not file)
737 * into buf. modify the start pointer to point after the
738 * word in the string.
741 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
745 for (cp = buf; *startpp <= endp; ) {
747 if (isspace(c) || c == '\0') {
748 if (cp != buf) /*%< trailing whitespace */
750 else { /*%< leading whitespace */
756 if (cp >= buf+size-1)
765 * get a white spae delimited string from memory. Process quoted strings
766 * and \\DDD escapes. Return length or -1 on error. Returned string may
769 static char digits[] = "0123456789";
771 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
779 for (cp = buf; *startpp <= endp; ) {
780 if ((c = **startpp) == '\0')
782 /* leading white space */
783 if ((cp == buf) && !seen_quote && isspace(c)) {
821 (strchr(digits, c) - digits);
831 } else if (!inquote && isspace(c))
833 if (cp >= buf+size-1)
841 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
845 * Get a whitespace delimited base 16 number from a string (not file) into buf
846 * update the start pointer to point after the number in the string.
849 gethexnum_str(u_char **startpp, u_char *endp) {
854 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
855 return getnum_str(startpp, endp);
857 for (n = 0; *startpp <= endp; ) {
859 if (isspace(c) || c == '\0') {
860 if (seendigit) /*%< trailing whitespace */
862 else { /*%< leading whitespace */
868 while ((*startpp <= endp) &&
869 ((c = **startpp) != '\n'))
876 if (c == ')' && seendigit) {
884 n = n * 16 + (c - '0');
886 n = n * 16 + (tolower(c) - 'a' + 10);
893 * Get a whitespace delimited base 10 number from a string (not file) into buf
894 * update the start pointer to point after the number in the string.
897 getnum_str(u_char **startpp, u_char *endp) {
902 for (n = 0; *startpp <= endp; ) {
904 if (isspace(c) || c == '\0') {
905 if (seendigit) /*%< trailing whitespace */
907 else { /*%< leading whitespace */
913 while ((*startpp <= endp) &&
914 ((c = **startpp) != '\n'))
921 if (c == ')' && seendigit) {
928 n = n * 10 + (c - '0');
935 * Allocate a resource record buffer & save rr info.
938 res_mkupdrec(int section, const char *dname,
939 u_int class, u_int type, u_long ttl) {
940 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
942 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
947 INIT_LINK(rrecp, r_link);
948 INIT_LINK(rrecp, r_glink);
949 rrecp->r_class = (ns_class)class;
950 rrecp->r_type = (ns_type)type;
952 rrecp->r_section = (ns_sect)section;
957 * Free a resource record buffer created by res_mkupdrec.
960 res_freeupdrec(ns_updrec *rrecp) {
961 /* Note: freeing r_dp is the caller's responsibility. */
962 if (rrecp->r_dname != NULL)
963 free(rrecp->r_dname);
968 struct valuelist * next;
969 struct valuelist * prev;
974 static struct valuelist *servicelist, *protolist;
977 res_buildservicelist(void) {
979 struct valuelist *slp;
986 while ((sp = getservent()) != NULL) {
987 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
990 slp->name = strdup(sp->s_name);
991 slp->proto = strdup(sp->s_proto);
992 if ((slp->name == NULL) || (slp->proto == NULL)) {
993 if (slp->name) free(slp->name);
994 if (slp->proto) free(slp->proto);
998 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
999 slp->next = servicelist;
1002 servicelist->prev = slp;
1010 res_destroyservicelist() {
1011 struct valuelist *slp, *slp_next;
1013 for (slp = servicelist; slp != NULL; slp = slp_next) {
1014 slp_next = slp->next;
1019 servicelist = (struct valuelist *)0;
1027 res_buildprotolist(void) {
1028 struct protoent *pp;
1029 struct valuelist *slp;
1036 while ((pp = getprotoent()) != NULL) {
1037 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1040 slp->name = strdup(pp->p_name);
1041 if (slp->name == NULL) {
1045 slp->port = pp->p_proto; /*%< host byte order */
1046 slp->next = protolist;
1049 protolist->prev = slp;
1057 res_destroyprotolist(void) {
1058 struct valuelist *plp, *plp_next;
1060 for (plp = protolist; plp != NULL; plp = plp_next) {
1061 plp_next = plp->next;
1065 protolist = (struct valuelist *)0;
1070 findservice(const char *s, struct valuelist **list) {
1071 struct valuelist *lp = *list;
1074 for (; lp != NULL; lp = lp->next)
1075 if (strcasecmp(lp->name, s) == 0) {
1077 lp->prev->next = lp->next;
1079 lp->next->prev = lp->prev;
1084 return (lp->port); /*%< host byte order */
1086 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1092 * Convert service name or (ascii) number to int.
1098 res_servicenumber(const char *p) {
1099 if (servicelist == (struct valuelist *)0)
1100 res_buildservicelist();
1101 return (findservice(p, &servicelist));
1105 * Convert protocol name or (ascii) number to int.
1111 res_protocolnumber(const char *p) {
1112 if (protolist == (struct valuelist *)0)
1113 res_buildprotolist();
1114 return (findservice(p, &protolist));
1118 static struct servent *
1119 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1120 struct valuelist **list = &servicelist;
1121 struct valuelist *lp = *list;
1122 static struct servent serv;
1125 for (; lp != NULL; lp = lp->next) {
1126 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1128 if (strcasecmp(lp->proto, proto) == 0) {
1130 lp->prev->next = lp->next;
1132 lp->next->prev = lp->prev;
1137 serv.s_name = lp->name;
1138 serv.s_port = htons((u_int16_t)lp->port);
1139 serv.s_proto = lp->proto;
1146 static struct protoent *
1147 cgetprotobynumber(int proto) { /*%< Host byte order. */
1148 struct valuelist **list = &protolist;
1149 struct valuelist *lp = *list;
1150 static struct protoent prot;
1152 for (; lp != NULL; lp = lp->next)
1153 if (lp->port == proto) { /*%< Host byte order. */
1155 lp->prev->next = lp->next;
1157 lp->next->prev = lp->prev;
1162 prot.p_name = lp->name;
1163 prot.p_proto = lp->port; /*%< Host byte order. */
1170 res_protocolname(int num) {
1171 static char number[8];
1172 struct protoent *pp;
1174 if (protolist == (struct valuelist *)0)
1175 res_buildprotolist();
1176 pp = cgetprotobynumber(num);
1178 (void) sprintf(number, "%d", num);
1181 return (pp->p_name);
1185 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1186 static char number[8];
1189 if (servicelist == (struct valuelist *)0)
1190 res_buildservicelist();
1191 ss = cgetservbyport(htons(port), proto);
1193 (void) sprintf(number, "%d", port);
1196 return (ss->s_name);