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/types.h>
33 #include <sys/param.h>
35 #include <netinet/in.h>
36 #include <arpa/nameser.h>
37 #include <arpa/inet.h>
43 #include <res_update.h>
54 #include "port_after.h"
56 /* Options. Leave them on. */
60 static int getnum_str(u_char **, u_char *);
61 static int gethexnum_str(u_char **, u_char *);
62 static int getword_str(char *, int, u_char **, u_char *);
63 static int getstr_str(char *, int, u_char **, u_char *);
65 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
72 int res_protocolnumber(const char *);
76 int res_servicenumber(const char *);
79 * Form update packets.
80 * Returns the size of the resulting packet if no error
84 *\li -1 if error in reading a word/number in rdata
85 * portion for update packets
86 *\li -2 if length of buffer passed is insufficient
87 *\li -3 if zone section is not the first section in
88 * the linked list, or section order has a problem
89 *\li -4 on a number overflow
90 *\li -5 unknown operation or no records
93 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
94 ns_updrec *rrecp_start = rrecp_in;
96 u_char *cp, *sp2, *startp, *endp;
97 int n, i, soanum, multiline;
100 struct in6_addr in6a;
102 u_char buf3[MAXDNAME];
103 int section, numrrs = 0, counts[ns_s_max];
104 u_int16_t rtype, rclass;
106 u_char *dnptrs[20], **dpp, **lastdnptr;
113 * Initialize header fields.
115 if ((buf == NULL) || (buflen < HFIXEDSZ))
117 memset(buf, 0, HFIXEDSZ);
119 statp->id = res_nrandomid(statp);
120 hp->id = htons(statp->id);
121 hp->opcode = ns_o_update;
128 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
130 if (rrecp_start == NULL)
132 else if (rrecp_start->r_section != S_ZONE)
135 memset(counts, 0, sizeof counts);
136 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
138 section = rrecp->r_section;
139 if (section < 0 || section >= ns_s_max)
142 for (i = section + 1; i < ns_s_max; i++)
145 rtype = rrecp->r_type;
146 rclass = rrecp->r_class;
148 /* overload class and type */
149 if (section == S_PREREQ) {
151 switch (rrecp->r_opcode) {
167 if (rrecp->r_size == 0)
172 "res_mkupdate: incorrect opcode: %d\n",
177 } else if (section == S_UPDATE) {
178 switch (rrecp->r_opcode) {
180 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
186 "res_mkupdate: incorrect opcode: %d\n",
194 * XXX appending default domain to owner name is omitted,
195 * fqdn must be provided
197 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
201 ShrinkBuffer(n + 2*INT16SZ);
203 PUTSHORT(rclass, cp);
204 if (section == S_ZONE) {
205 if (numrrs != 1 || rrecp->r_type != T_SOA)
209 ShrinkBuffer(INT32SZ + INT16SZ);
211 sp2 = cp; /*%< save pointer to length byte */
213 if (rrecp->r_size == 0) {
214 if (section == S_UPDATE && rclass != C_ANY)
221 startp = rrecp->r_data;
222 endp = startp + rrecp->r_size - 1;
223 /* XXX this should be done centrally. */
224 switch (rrecp->r_type) {
226 if (!getword_str(buf2, sizeof buf2, &startp, endp))
228 if (!inet_aton(buf2, &ina))
230 n1 = ntohl(ina.s_addr);
231 ShrinkBuffer(INT32SZ);
241 if (!getword_str(buf2, sizeof buf2, &startp, endp))
243 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
252 for (i = 0; i < 2; i++) {
253 if (!getword_str(buf2, sizeof buf2, &startp,
256 n = dn_comp(buf2, cp, buflen,
263 if (rrecp->r_type == T_SOA) {
264 ShrinkBuffer(5 * INT32SZ);
265 while (isspace(*startp) || !*startp)
267 if (*startp == '(') {
272 /* serial, refresh, retry, expire, minimum */
273 for (i = 0; i < 5; i++) {
274 soanum = getnum_str(&startp, endp);
280 while (isspace(*startp) || !*startp)
290 n = getnum_str(&startp, endp);
293 ShrinkBuffer(INT16SZ);
295 if (!getword_str(buf2, sizeof buf2, &startp, endp))
297 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
304 n = getnum_str(&startp, endp);
307 ShrinkBuffer(INT16SZ);
310 n = getnum_str(&startp, endp);
313 ShrinkBuffer(INT16SZ);
316 n = getnum_str(&startp, endp);
319 ShrinkBuffer(INT16SZ);
322 if (!getword_str(buf2, sizeof buf2, &startp, endp))
324 n = dn_comp(buf2, cp, buflen, NULL, NULL);
331 n = getnum_str(&startp, endp);
335 ShrinkBuffer(INT16SZ);
336 for (i = 0; i < 2; i++) {
337 if (!getword_str(buf2, sizeof buf2, &startp,
340 n = dn_comp(buf2, cp, buflen, dnptrs,
350 unsigned int maxbm = 0;
352 if (!getword_str(buf2, sizeof buf2, &startp, endp))
354 if (!inet_aton(buf2, &ina))
356 n1 = ntohl(ina.s_addr);
357 ShrinkBuffer(INT32SZ);
360 if (!getword_str(buf2, sizeof buf2, &startp, endp))
362 if ((i = res_protocolnumber(buf2)) < 0)
367 for (i = 0; i < MAXPORT/8 ; i++)
370 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
371 if ((n = res_servicenumber(buf2)) <= 0)
375 bm[n/8] |= (0x80>>(n%8));
376 if ((unsigned)n > maxbm)
383 memcpy(cp, bm, maxbm);
388 for (i = 0; i < 2; i++) {
389 if ((n = getstr_str(buf2, sizeof buf2,
402 if ((n = getstr_str(buf2, sizeof buf2,
403 &startp, endp)) < 0) {
404 if (cp != (sp2 + INT16SZ))
418 if ((n = getstr_str(buf2, sizeof buf2, &startp,
430 if ((n = getstr_str(buf2, sizeof buf2, &startp,
433 if ((n > 255) || (n == 0))
439 if ((n = getstr_str(buf2, sizeof buf2, &startp,
450 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
459 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
471 int sig_type, success, dateerror;
472 u_int32_t exptime, timesigned;
475 if ((n = getword_str(buf2, sizeof buf2,
478 sig_type = sym_ston(__p_type_syms, buf2, &success);
479 if (!success || sig_type == ns_t_any)
481 ShrinkBuffer(INT16SZ);
482 PUTSHORT(sig_type, cp);
484 n = getnum_str(&startp, endp);
490 n = getnum_str(&startp, endp);
491 if (n <= 0 || n > 255)
496 if (!getword_str(buf2, sizeof buf2, &startp, endp))
498 exptime = ns_datetosecs(buf2, &dateerror);
500 ShrinkBuffer(INT32SZ);
508 ottl = strtoul(buf2, &ulendp, 10);
510 (ulendp != NULL && *ulendp != '\0'))
512 ShrinkBuffer(INT32SZ);
514 if (!getword_str(buf2, sizeof buf2, &startp,
517 exptime = ns_datetosecs(buf2, &dateerror);
522 ShrinkBuffer(INT32SZ);
523 PUTLONG(exptime, cp);
525 if (!getword_str(buf2, sizeof buf2, &startp, endp))
527 timesigned = ns_datetosecs(buf2, &dateerror);
529 ShrinkBuffer(INT32SZ);
530 PUTLONG(timesigned, cp);
535 n = getnum_str(&startp, endp);
538 ShrinkBuffer(INT16SZ);
541 if (!getword_str(buf2, sizeof buf2, &startp, endp))
543 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
549 if ((n = getword_str(buf2, sizeof buf2,
552 siglen = b64_pton(buf2, buf3, sizeof(buf3));
555 ShrinkBuffer(siglen);
556 memcpy(cp, buf3, siglen);
563 n = gethexnum_str(&startp, endp);
566 ShrinkBuffer(INT16SZ);
569 n = getnum_str(&startp, endp);
575 n = getnum_str(&startp, endp);
581 if ((n = getword_str(buf2, sizeof buf2,
584 keylen = b64_pton(buf2, buf3, sizeof(buf3));
587 ShrinkBuffer(keylen);
588 memcpy(cp, buf3, keylen);
593 int success, nxt_type;
598 if (!getword_str(buf2, sizeof buf2, &startp, endp))
600 n = dn_comp(buf2, cp, buflen, NULL, NULL);
606 memset(data, 0, sizeof data);
608 if (!getword_str(buf2, sizeof buf2, &startp,
611 nxt_type = sym_ston(__p_type_syms, buf2,
613 if (!success || !ns_t_rr_p(nxt_type))
615 NS_NXT_BIT_SET(nxt_type, data);
616 if (nxt_type > maxtype)
619 n = maxtype/NS_NXT_BITS+1;
627 n = getnum_str(&startp, endp);
630 ShrinkBuffer(INT16SZ);
633 n = getnum_str(&startp, endp);
636 ShrinkBuffer(INT16SZ);
639 n = getnum_str(&startp, endp);
645 if ((n = getword_str(buf2, sizeof buf2,
648 certlen = b64_pton(buf2, buf3, sizeof(buf3));
651 ShrinkBuffer(certlen);
652 memcpy(cp, buf3, certlen);
656 if (!getword_str(buf2, sizeof buf2, &startp, endp))
658 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
660 ShrinkBuffer(NS_IN6ADDRSZ);
661 memcpy(cp, &in6a, NS_IN6ADDRSZ);
665 /* Order Preference Flags Service Replacement Regexp */
667 n = getnum_str(&startp, endp);
668 if (n < 0 || n > 65535)
670 ShrinkBuffer(INT16SZ);
673 n = getnum_str(&startp, endp);
674 if (n < 0 || n > 65535)
676 ShrinkBuffer(INT16SZ);
679 if ((n = getstr_str(buf2, sizeof buf2,
680 &startp, endp)) < 0) {
689 /* Service Classes */
690 if ((n = getstr_str(buf2, sizeof buf2,
691 &startp, endp)) < 0) {
701 if ((n = getstr_str(buf2, sizeof buf2,
702 &startp, endp)) < 0) {
712 if (!getword_str(buf2, sizeof buf2, &startp, endp))
714 n = dn_comp(buf2, cp, buflen, NULL, NULL);
723 n = (u_int16_t)((cp - sp2) - INT16SZ);
727 hp->qdcount = htons(counts[0]);
728 hp->ancount = htons(counts[1]);
729 hp->nscount = htons(counts[2]);
730 hp->arcount = htons(counts[3]);
735 * Get a whitespace delimited word from a string (not file)
736 * into buf. modify the start pointer to point after the
737 * word in the string.
740 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
744 for (cp = buf; *startpp <= endp; ) {
746 if (isspace(c) || c == '\0') {
747 if (cp != buf) /*%< trailing whitespace */
749 else { /*%< leading whitespace */
755 if (cp >= buf+size-1)
764 * get a white spae delimited string from memory. Process quoted strings
765 * and \\DDD escapes. Return length or -1 on error. Returned string may
768 static char digits[] = "0123456789";
770 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
778 for (cp = buf; *startpp <= endp; ) {
779 if ((c = **startpp) == '\0')
781 /* leading white space */
782 if ((cp == buf) && !seen_quote && isspace(c)) {
820 (strchr(digits, c) - digits);
830 } else if (!inquote && isspace(c))
832 if (cp >= buf+size-1)
840 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
844 * Get a whitespace delimited base 16 number from a string (not file) into buf
845 * update the start pointer to point after the number in the string.
848 gethexnum_str(u_char **startpp, u_char *endp) {
853 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
854 return getnum_str(startpp, endp);
856 for (n = 0; *startpp <= endp; ) {
858 if (isspace(c) || c == '\0') {
859 if (seendigit) /*%< trailing whitespace */
861 else { /*%< leading whitespace */
867 while ((*startpp <= endp) &&
868 ((c = **startpp) != '\n'))
875 if (c == ')' && seendigit) {
883 n = n * 16 + (c - '0');
885 n = n * 16 + (tolower(c) - 'a' + 10);
892 * Get a whitespace delimited base 10 number from a string (not file) into buf
893 * update the start pointer to point after the number in the string.
896 getnum_str(u_char **startpp, u_char *endp) {
901 for (n = 0; *startpp <= endp; ) {
903 if (isspace(c) || c == '\0') {
904 if (seendigit) /*%< trailing whitespace */
906 else { /*%< leading whitespace */
912 while ((*startpp <= endp) &&
913 ((c = **startpp) != '\n'))
920 if (c == ')' && seendigit) {
927 n = n * 10 + (c - '0');
934 * Allocate a resource record buffer & save rr info.
937 res_mkupdrec(int section, const char *dname,
938 u_int class, u_int type, u_long ttl) {
939 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
941 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
946 INIT_LINK(rrecp, r_link);
947 INIT_LINK(rrecp, r_glink);
948 rrecp->r_class = (ns_class)class;
949 rrecp->r_type = (ns_type)type;
951 rrecp->r_section = (ns_sect)section;
956 * Free a resource record buffer created by res_mkupdrec.
959 res_freeupdrec(ns_updrec *rrecp) {
960 /* Note: freeing r_dp is the caller's responsibility. */
961 if (rrecp->r_dname != NULL)
962 free(rrecp->r_dname);
967 struct valuelist * next;
968 struct valuelist * prev;
973 static struct valuelist *servicelist, *protolist;
976 res_buildservicelist() {
978 struct valuelist *slp;
985 while ((sp = getservent()) != NULL) {
986 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
989 slp->name = strdup(sp->s_name);
990 slp->proto = strdup(sp->s_proto);
991 if ((slp->name == NULL) || (slp->proto == NULL)) {
992 if (slp->name) free(slp->name);
993 if (slp->proto) free(slp->proto);
997 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
998 slp->next = servicelist;
1001 servicelist->prev = slp;
1009 res_destroyservicelist() {
1010 struct valuelist *slp, *slp_next;
1012 for (slp = servicelist; slp != NULL; slp = slp_next) {
1013 slp_next = slp->next;
1018 servicelist = (struct valuelist *)0;
1026 res_buildprotolist(void) {
1027 struct protoent *pp;
1028 struct valuelist *slp;
1035 while ((pp = getprotoent()) != NULL) {
1036 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1039 slp->name = strdup(pp->p_name);
1040 if (slp->name == NULL) {
1044 slp->port = pp->p_proto; /*%< host byte order */
1045 slp->next = protolist;
1048 protolist->prev = slp;
1056 res_destroyprotolist(void) {
1057 struct valuelist *plp, *plp_next;
1059 for (plp = protolist; plp != NULL; plp = plp_next) {
1060 plp_next = plp->next;
1064 protolist = (struct valuelist *)0;
1069 findservice(const char *s, struct valuelist **list) {
1070 struct valuelist *lp = *list;
1073 for (; lp != NULL; lp = lp->next)
1074 if (strcasecmp(lp->name, s) == 0) {
1076 lp->prev->next = lp->next;
1078 lp->next->prev = lp->prev;
1083 return (lp->port); /*%< host byte order */
1085 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1091 * Convert service name or (ascii) number to int.
1097 res_servicenumber(const char *p) {
1098 if (servicelist == (struct valuelist *)0)
1099 res_buildservicelist();
1100 return (findservice(p, &servicelist));
1104 * Convert protocol name or (ascii) number to int.
1110 res_protocolnumber(const char *p) {
1111 if (protolist == (struct valuelist *)0)
1112 res_buildprotolist();
1113 return (findservice(p, &protolist));
1117 static struct servent *
1118 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1119 struct valuelist **list = &servicelist;
1120 struct valuelist *lp = *list;
1121 static struct servent serv;
1124 for (; lp != NULL; lp = lp->next) {
1125 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1127 if (strcasecmp(lp->proto, proto) == 0) {
1129 lp->prev->next = lp->next;
1131 lp->next->prev = lp->prev;
1136 serv.s_name = lp->name;
1137 serv.s_port = htons((u_int16_t)lp->port);
1138 serv.s_proto = lp->proto;
1145 static struct protoent *
1146 cgetprotobynumber(int proto) { /*%< Host byte order. */
1147 struct valuelist **list = &protolist;
1148 struct valuelist *lp = *list;
1149 static struct protoent prot;
1151 for (; lp != NULL; lp = lp->next)
1152 if (lp->port == proto) { /*%< Host byte order. */
1154 lp->prev->next = lp->next;
1156 lp->next->prev = lp->prev;
1161 prot.p_name = lp->name;
1162 prot.p_proto = lp->port; /*%< Host byte order. */
1169 res_protocolname(int num) {
1170 static char number[8];
1171 struct protoent *pp;
1173 if (protolist == (struct valuelist *)0)
1174 res_buildprotolist();
1175 pp = cgetprotobynumber(num);
1177 (void) sprintf(number, "%d", num);
1180 return (pp->p_name);
1184 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1185 static char number[8];
1188 if (servicelist == (struct valuelist *)0)
1189 res_buildservicelist();
1190 ss = cgetservbyport(htons(port), proto);
1192 (void) sprintf(number, "%d", port);
1195 return (ss->s_name);