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.8 2005/10/14 05:44:26 marka Exp $";
28 #include "port_before.h"
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <arpa/inet.h>
41 #include <res_update.h>
51 #include "port_after.h"
53 /* Options. Leave them on. */
57 static int getnum_str(u_char **, u_char *);
58 static int gethexnum_str(u_char **, u_char *);
59 static int getword_str(char *, int, u_char **, u_char *);
60 static int getstr_str(char *, int, u_char **, u_char *);
62 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
68 int res_protocolnumber(const char *);
72 int res_servicenumber(const char *);
75 * Form update packets.
76 * Returns the size of the resulting packet if no error
80 *\li -1 if error in reading a word/number in rdata
81 * portion for update packets
82 *\li -2 if length of buffer passed is insufficient
83 *\li -3 if zone section is not the first section in
84 * the linked list, or section order has a problem
85 *\li -4 on a number overflow
86 *\li -5 unknown operation or no records
89 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
90 ns_updrec *rrecp_start = rrecp_in;
92 u_char *cp, *sp2, *startp, *endp;
93 int n, i, soanum, multiline;
98 u_char buf3[MAXDNAME];
99 int section, numrrs = 0, counts[ns_s_max];
100 u_int16_t rtype, rclass;
102 u_char *dnptrs[20], **dpp, **lastdnptr;
109 * Initialize header fields.
111 if ((buf == NULL) || (buflen < HFIXEDSZ))
113 memset(buf, 0, HFIXEDSZ);
115 hp->id = htons(++statp->id);
116 hp->opcode = ns_o_update;
123 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
125 if (rrecp_start == NULL)
127 else if (rrecp_start->r_section != S_ZONE)
130 memset(counts, 0, sizeof counts);
131 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
133 section = rrecp->r_section;
134 if (section < 0 || section >= ns_s_max)
137 for (i = section + 1; i < ns_s_max; i++)
140 rtype = rrecp->r_type;
141 rclass = rrecp->r_class;
143 /* overload class and type */
144 if (section == S_PREREQ) {
146 switch (rrecp->r_opcode) {
162 if (rrecp->r_size == 0)
167 "res_mkupdate: incorrect opcode: %d\n",
172 } else if (section == S_UPDATE) {
173 switch (rrecp->r_opcode) {
175 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
181 "res_mkupdate: incorrect opcode: %d\n",
189 * XXX appending default domain to owner name is omitted,
190 * fqdn must be provided
192 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
196 ShrinkBuffer(n + 2*INT16SZ);
198 PUTSHORT(rclass, cp);
199 if (section == S_ZONE) {
200 if (numrrs != 1 || rrecp->r_type != T_SOA)
204 ShrinkBuffer(INT32SZ + INT16SZ);
206 sp2 = cp; /*%< save pointer to length byte */
208 if (rrecp->r_size == 0) {
209 if (section == S_UPDATE && rclass != C_ANY)
216 startp = rrecp->r_data;
217 endp = startp + rrecp->r_size - 1;
218 /* XXX this should be done centrally. */
219 switch (rrecp->r_type) {
221 if (!getword_str(buf2, sizeof buf2, &startp, endp))
223 if (!inet_aton(buf2, &ina))
225 n1 = ntohl(ina.s_addr);
226 ShrinkBuffer(INT32SZ);
236 if (!getword_str(buf2, sizeof buf2, &startp, endp))
238 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
247 for (i = 0; i < 2; i++) {
248 if (!getword_str(buf2, sizeof buf2, &startp,
251 n = dn_comp(buf2, cp, buflen,
258 if (rrecp->r_type == T_SOA) {
259 ShrinkBuffer(5 * INT32SZ);
260 while (isspace(*startp) || !*startp)
262 if (*startp == '(') {
267 /* serial, refresh, retry, expire, minimum */
268 for (i = 0; i < 5; i++) {
269 soanum = getnum_str(&startp, endp);
275 while (isspace(*startp) || !*startp)
285 n = getnum_str(&startp, endp);
288 ShrinkBuffer(INT16SZ);
290 if (!getword_str(buf2, sizeof buf2, &startp, endp))
292 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
299 n = getnum_str(&startp, endp);
302 ShrinkBuffer(INT16SZ);
305 n = getnum_str(&startp, endp);
308 ShrinkBuffer(INT16SZ);
311 n = getnum_str(&startp, endp);
314 ShrinkBuffer(INT16SZ);
317 if (!getword_str(buf2, sizeof buf2, &startp, endp))
319 n = dn_comp(buf2, cp, buflen, NULL, NULL);
326 n = getnum_str(&startp, endp);
330 ShrinkBuffer(INT16SZ);
331 for (i = 0; i < 2; i++) {
332 if (!getword_str(buf2, sizeof buf2, &startp,
335 n = dn_comp(buf2, cp, buflen, dnptrs,
345 unsigned int maxbm = 0;
347 if (!getword_str(buf2, sizeof buf2, &startp, endp))
349 if (!inet_aton(buf2, &ina))
351 n1 = ntohl(ina.s_addr);
352 ShrinkBuffer(INT32SZ);
355 if (!getword_str(buf2, sizeof buf2, &startp, endp))
357 if ((i = res_protocolnumber(buf2)) < 0)
362 for (i = 0; i < MAXPORT/8 ; i++)
365 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
366 if ((n = res_servicenumber(buf2)) <= 0)
370 bm[n/8] |= (0x80>>(n%8));
371 if ((unsigned)n > maxbm)
378 memcpy(cp, bm, maxbm);
383 for (i = 0; i < 2; i++) {
384 if ((n = getstr_str(buf2, sizeof buf2,
397 if ((n = getstr_str(buf2, sizeof buf2,
398 &startp, endp)) < 0) {
399 if (cp != (sp2 + INT16SZ))
413 if ((n = getstr_str(buf2, sizeof buf2, &startp,
425 if ((n = getstr_str(buf2, sizeof buf2, &startp,
428 if ((n > 255) || (n == 0))
434 if ((n = getstr_str(buf2, sizeof buf2, &startp,
445 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
454 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
466 int sig_type, success, dateerror;
467 u_int32_t exptime, timesigned;
470 if ((n = getword_str(buf2, sizeof buf2,
473 sig_type = sym_ston(__p_type_syms, buf2, &success);
474 if (!success || sig_type == ns_t_any)
476 ShrinkBuffer(INT16SZ);
477 PUTSHORT(sig_type, cp);
479 n = getnum_str(&startp, endp);
485 n = getnum_str(&startp, endp);
486 if (n <= 0 || n > 255)
491 if (!getword_str(buf2, sizeof buf2, &startp, endp))
493 exptime = ns_datetosecs(buf2, &dateerror);
495 ShrinkBuffer(INT32SZ);
503 ottl = strtoul(buf2, &ulendp, 10);
505 (ulendp != NULL && *ulendp != '\0'))
507 ShrinkBuffer(INT32SZ);
509 if (!getword_str(buf2, sizeof buf2, &startp,
512 exptime = ns_datetosecs(buf2, &dateerror);
517 ShrinkBuffer(INT32SZ);
518 PUTLONG(exptime, cp);
520 if (!getword_str(buf2, sizeof buf2, &startp, endp))
522 timesigned = ns_datetosecs(buf2, &dateerror);
524 ShrinkBuffer(INT32SZ);
525 PUTLONG(timesigned, cp);
530 n = getnum_str(&startp, endp);
533 ShrinkBuffer(INT16SZ);
536 if (!getword_str(buf2, sizeof buf2, &startp, endp))
538 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
544 if ((n = getword_str(buf2, sizeof buf2,
547 siglen = b64_pton(buf2, buf3, sizeof(buf3));
550 ShrinkBuffer(siglen);
551 memcpy(cp, buf3, siglen);
558 n = gethexnum_str(&startp, endp);
561 ShrinkBuffer(INT16SZ);
564 n = getnum_str(&startp, endp);
570 n = getnum_str(&startp, endp);
576 if ((n = getword_str(buf2, sizeof buf2,
579 keylen = b64_pton(buf2, buf3, sizeof(buf3));
582 ShrinkBuffer(keylen);
583 memcpy(cp, buf3, keylen);
588 int success, nxt_type;
593 if (!getword_str(buf2, sizeof buf2, &startp, endp))
595 n = dn_comp(buf2, cp, buflen, NULL, NULL);
601 memset(data, 0, sizeof data);
603 if (!getword_str(buf2, sizeof buf2, &startp,
606 nxt_type = sym_ston(__p_type_syms, buf2,
608 if (!success || !ns_t_rr_p(nxt_type))
610 NS_NXT_BIT_SET(nxt_type, data);
611 if (nxt_type > maxtype)
614 n = maxtype/NS_NXT_BITS+1;
622 n = getnum_str(&startp, endp);
625 ShrinkBuffer(INT16SZ);
628 n = getnum_str(&startp, endp);
631 ShrinkBuffer(INT16SZ);
634 n = getnum_str(&startp, endp);
640 if ((n = getword_str(buf2, sizeof buf2,
643 certlen = b64_pton(buf2, buf3, sizeof(buf3));
646 ShrinkBuffer(certlen);
647 memcpy(cp, buf3, certlen);
651 if (!getword_str(buf2, sizeof buf2, &startp, endp))
653 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
655 ShrinkBuffer(NS_IN6ADDRSZ);
656 memcpy(cp, &in6a, NS_IN6ADDRSZ);
660 /* Order Preference Flags Service Replacement Regexp */
662 n = getnum_str(&startp, endp);
663 if (n < 0 || n > 65535)
665 ShrinkBuffer(INT16SZ);
668 n = getnum_str(&startp, endp);
669 if (n < 0 || n > 65535)
671 ShrinkBuffer(INT16SZ);
674 if ((n = getstr_str(buf2, sizeof buf2,
675 &startp, endp)) < 0) {
684 /* Service Classes */
685 if ((n = getstr_str(buf2, sizeof buf2,
686 &startp, endp)) < 0) {
696 if ((n = getstr_str(buf2, sizeof buf2,
697 &startp, endp)) < 0) {
707 if (!getword_str(buf2, sizeof buf2, &startp, endp))
709 n = dn_comp(buf2, cp, buflen, NULL, NULL);
718 n = (u_int16_t)((cp - sp2) - INT16SZ);
722 hp->qdcount = htons(counts[0]);
723 hp->ancount = htons(counts[1]);
724 hp->nscount = htons(counts[2]);
725 hp->arcount = htons(counts[3]);
730 * Get a whitespace delimited word from a string (not file)
731 * into buf. modify the start pointer to point after the
732 * word in the string.
735 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
739 for (cp = buf; *startpp <= endp; ) {
741 if (isspace(c) || c == '\0') {
742 if (cp != buf) /*%< trailing whitespace */
744 else { /*%< leading whitespace */
750 if (cp >= buf+size-1)
759 * get a white spae delimited string from memory. Process quoted strings
760 * and \\DDD escapes. Return length or -1 on error. Returned string may
763 static char digits[] = "0123456789";
765 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
773 for (cp = buf; *startpp <= endp; ) {
774 if ((c = **startpp) == '\0')
776 /* leading white space */
777 if ((cp == buf) && !seen_quote && isspace(c)) {
815 (strchr(digits, c) - digits);
825 } else if (!inquote && isspace(c))
827 if (cp >= buf+size-1)
835 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
839 * Get a whitespace delimited base 16 number from a string (not file) into buf
840 * update the start pointer to point after the number in the string.
843 gethexnum_str(u_char **startpp, u_char *endp) {
848 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
849 return getnum_str(startpp, endp);
851 for (n = 0; *startpp <= endp; ) {
853 if (isspace(c) || c == '\0') {
854 if (seendigit) /*%< trailing whitespace */
856 else { /*%< leading whitespace */
862 while ((*startpp <= endp) &&
863 ((c = **startpp) != '\n'))
870 if (c == ')' && seendigit) {
878 n = n * 16 + (c - '0');
880 n = n * 16 + (tolower(c) - 'a' + 10);
887 * Get a whitespace delimited base 10 number from a string (not file) into buf
888 * update the start pointer to point after the number in the string.
891 getnum_str(u_char **startpp, u_char *endp) {
896 for (n = 0; *startpp <= endp; ) {
898 if (isspace(c) || c == '\0') {
899 if (seendigit) /*%< trailing whitespace */
901 else { /*%< leading whitespace */
907 while ((*startpp <= endp) &&
908 ((c = **startpp) != '\n'))
915 if (c == ')' && seendigit) {
922 n = n * 10 + (c - '0');
929 * Allocate a resource record buffer & save rr info.
932 res_mkupdrec(int section, const char *dname,
933 u_int class, u_int type, u_long ttl) {
934 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
936 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
941 INIT_LINK(rrecp, r_link);
942 INIT_LINK(rrecp, r_glink);
943 rrecp->r_class = (ns_class)class;
944 rrecp->r_type = (ns_type)type;
946 rrecp->r_section = (ns_sect)section;
951 * Free a resource record buffer created by res_mkupdrec.
954 res_freeupdrec(ns_updrec *rrecp) {
955 /* Note: freeing r_dp is the caller's responsibility. */
956 if (rrecp->r_dname != NULL)
957 free(rrecp->r_dname);
962 struct valuelist * next;
963 struct valuelist * prev;
968 static struct valuelist *servicelist, *protolist;
971 res_buildservicelist() {
973 struct valuelist *slp;
980 while ((sp = getservent()) != NULL) {
981 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
984 slp->name = strdup(sp->s_name);
985 slp->proto = strdup(sp->s_proto);
986 if ((slp->name == NULL) || (slp->proto == NULL)) {
987 if (slp->name) free(slp->name);
988 if (slp->proto) free(slp->proto);
992 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
993 slp->next = servicelist;
996 servicelist->prev = slp;
1004 res_destroyservicelist() {
1005 struct valuelist *slp, *slp_next;
1007 for (slp = servicelist; slp != NULL; slp = slp_next) {
1008 slp_next = slp->next;
1013 servicelist = (struct valuelist *)0;
1018 res_buildprotolist(void) {
1019 struct protoent *pp;
1020 struct valuelist *slp;
1027 while ((pp = getprotoent()) != NULL) {
1028 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1031 slp->name = strdup(pp->p_name);
1032 if (slp->name == NULL) {
1036 slp->port = pp->p_proto; /*%< host byte order */
1037 slp->next = protolist;
1040 protolist->prev = slp;
1048 res_destroyprotolist(void) {
1049 struct valuelist *plp, *plp_next;
1051 for (plp = protolist; plp != NULL; plp = plp_next) {
1052 plp_next = plp->next;
1056 protolist = (struct valuelist *)0;
1061 findservice(const char *s, struct valuelist **list) {
1062 struct valuelist *lp = *list;
1065 for (; lp != NULL; lp = lp->next)
1066 if (strcasecmp(lp->name, s) == 0) {
1068 lp->prev->next = lp->next;
1070 lp->next->prev = lp->prev;
1075 return (lp->port); /*%< host byte order */
1077 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1083 * Convert service name or (ascii) number to int.
1089 res_servicenumber(const char *p) {
1090 if (servicelist == (struct valuelist *)0)
1091 res_buildservicelist();
1092 return (findservice(p, &servicelist));
1096 * Convert protocol name or (ascii) number to int.
1102 res_protocolnumber(const char *p) {
1103 if (protolist == (struct valuelist *)0)
1104 res_buildprotolist();
1105 return (findservice(p, &protolist));
1109 static struct servent *
1110 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1111 struct valuelist **list = &servicelist;
1112 struct valuelist *lp = *list;
1113 static struct servent serv;
1116 for (; lp != NULL; lp = lp->next) {
1117 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1119 if (strcasecmp(lp->proto, proto) == 0) {
1121 lp->prev->next = lp->next;
1123 lp->next->prev = lp->prev;
1128 serv.s_name = lp->name;
1129 serv.s_port = htons((u_int16_t)lp->port);
1130 serv.s_proto = lp->proto;
1137 static struct protoent *
1138 cgetprotobynumber(int proto) { /*%< Host byte order. */
1139 struct valuelist **list = &protolist;
1140 struct valuelist *lp = *list;
1141 static struct protoent prot;
1143 for (; lp != NULL; lp = lp->next)
1144 if (lp->port == proto) { /*%< Host byte order. */
1146 lp->prev->next = lp->next;
1148 lp->next->prev = lp->prev;
1153 prot.p_name = lp->name;
1154 prot.p_proto = lp->port; /*%< Host byte order. */
1161 res_protocolname(int num) {
1162 static char number[8];
1163 struct protoent *pp;
1165 if (protolist == (struct valuelist *)0)
1166 res_buildprotolist();
1167 pp = cgetprotobynumber(num);
1169 (void) sprintf(number, "%d", num);
1172 return (pp->p_name);
1176 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1177 static char number[8];
1180 if (servicelist == (struct valuelist *)0)
1181 res_buildservicelist();
1182 ss = cgetservbyport(htons(port), proto);
1184 (void) sprintf(number, "%d", port);
1187 return (ss->s_name);