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.4.18.4 2005/10/14 05:44:12 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>
48 #include "port_after.h"
50 /* Options. Leave them on. */
54 static int getnum_str(u_char **, u_char *);
55 static int gethexnum_str(u_char **, u_char *);
56 static int getword_str(char *, int, u_char **, u_char *);
57 static int getstr_str(char *, int, u_char **, u_char *);
59 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
63 int res_protocolnumber(const char *);
64 int res_servicenumber(const char *);
67 * Form update packets.
68 * Returns the size of the resulting packet if no error
72 *\li -1 if error in reading a word/number in rdata
73 * portion for update packets
74 *\li -2 if length of buffer passed is insufficient
75 *\li -3 if zone section is not the first section in
76 * the linked list, or section order has a problem
77 *\li -4 on a number overflow
78 *\li -5 unknown operation or no records
81 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
82 ns_updrec *rrecp_start = rrecp_in;
84 u_char *cp, *sp2, *startp, *endp;
85 int n, i, soanum, multiline;
90 u_char buf3[MAXDNAME];
91 int section, numrrs = 0, counts[ns_s_max];
92 u_int16_t rtype, rclass;
94 u_char *dnptrs[20], **dpp, **lastdnptr;
95 int siglen, keylen, certlen;
98 * Initialize header fields.
100 if ((buf == NULL) || (buflen < HFIXEDSZ))
102 memset(buf, 0, HFIXEDSZ);
104 hp->id = htons(++statp->id);
105 hp->opcode = ns_o_update;
112 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
114 if (rrecp_start == NULL)
116 else if (rrecp_start->r_section != S_ZONE)
119 memset(counts, 0, sizeof counts);
120 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
122 section = rrecp->r_section;
123 if (section < 0 || section >= ns_s_max)
126 for (i = section + 1; i < ns_s_max; i++)
129 rtype = rrecp->r_type;
130 rclass = rrecp->r_class;
132 /* overload class and type */
133 if (section == S_PREREQ) {
135 switch (rrecp->r_opcode) {
151 if (rrecp->r_size == 0)
156 "res_mkupdate: incorrect opcode: %d\n",
161 } else if (section == S_UPDATE) {
162 switch (rrecp->r_opcode) {
164 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
170 "res_mkupdate: incorrect opcode: %d\n",
178 * XXX appending default domain to owner name is omitted,
179 * fqdn must be provided
181 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
185 ShrinkBuffer(n + 2*INT16SZ);
187 PUTSHORT(rclass, cp);
188 if (section == S_ZONE) {
189 if (numrrs != 1 || rrecp->r_type != T_SOA)
193 ShrinkBuffer(INT32SZ + INT16SZ);
195 sp2 = cp; /*%< save pointer to length byte */
197 if (rrecp->r_size == 0) {
198 if (section == S_UPDATE && rclass != C_ANY)
205 startp = rrecp->r_data;
206 endp = startp + rrecp->r_size - 1;
207 /* XXX this should be done centrally. */
208 switch (rrecp->r_type) {
210 if (!getword_str(buf2, sizeof buf2, &startp, endp))
212 if (!inet_aton(buf2, &ina))
214 n1 = ntohl(ina.s_addr);
215 ShrinkBuffer(INT32SZ);
225 if (!getword_str(buf2, sizeof buf2, &startp, endp))
227 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
236 for (i = 0; i < 2; i++) {
237 if (!getword_str(buf2, sizeof buf2, &startp,
240 n = dn_comp(buf2, cp, buflen,
247 if (rrecp->r_type == T_SOA) {
248 ShrinkBuffer(5 * INT32SZ);
249 while (isspace(*startp) || !*startp)
251 if (*startp == '(') {
256 /* serial, refresh, retry, expire, minimum */
257 for (i = 0; i < 5; i++) {
258 soanum = getnum_str(&startp, endp);
264 while (isspace(*startp) || !*startp)
274 n = getnum_str(&startp, endp);
277 ShrinkBuffer(INT16SZ);
279 if (!getword_str(buf2, sizeof buf2, &startp, endp))
281 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
288 n = getnum_str(&startp, endp);
291 ShrinkBuffer(INT16SZ);
294 n = getnum_str(&startp, endp);
297 ShrinkBuffer(INT16SZ);
300 n = getnum_str(&startp, endp);
303 ShrinkBuffer(INT16SZ);
306 if (!getword_str(buf2, sizeof buf2, &startp, endp))
308 n = dn_comp(buf2, cp, buflen, NULL, NULL);
315 n = getnum_str(&startp, endp);
319 ShrinkBuffer(INT16SZ);
320 for (i = 0; i < 2; i++) {
321 if (!getword_str(buf2, sizeof buf2, &startp,
324 n = dn_comp(buf2, cp, buflen, dnptrs,
334 unsigned int maxbm = 0;
336 if (!getword_str(buf2, sizeof buf2, &startp, endp))
338 if (!inet_aton(buf2, &ina))
340 n1 = ntohl(ina.s_addr);
341 ShrinkBuffer(INT32SZ);
344 if (!getword_str(buf2, sizeof buf2, &startp, endp))
346 if ((i = res_protocolnumber(buf2)) < 0)
351 for (i = 0; i < MAXPORT/8 ; i++)
354 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
355 if ((n = res_servicenumber(buf2)) <= 0)
359 bm[n/8] |= (0x80>>(n%8));
360 if ((unsigned)n > maxbm)
367 memcpy(cp, bm, maxbm);
372 for (i = 0; i < 2; i++) {
373 if ((n = getstr_str(buf2, sizeof buf2,
386 if ((n = getstr_str(buf2, sizeof buf2,
387 &startp, endp)) < 0) {
388 if (cp != (sp2 + INT16SZ))
402 if ((n = getstr_str(buf2, sizeof buf2, &startp,
414 if ((n = getstr_str(buf2, sizeof buf2, &startp,
417 if ((n > 255) || (n == 0))
423 if ((n = getstr_str(buf2, sizeof buf2, &startp,
434 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
443 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
452 int sig_type, success, dateerror;
453 u_int32_t exptime, timesigned;
456 if ((n = getword_str(buf2, sizeof buf2,
459 sig_type = sym_ston(__p_type_syms, buf2, &success);
460 if (!success || sig_type == ns_t_any)
462 ShrinkBuffer(INT16SZ);
463 PUTSHORT(sig_type, cp);
465 n = getnum_str(&startp, endp);
471 n = getnum_str(&startp, endp);
472 if (n <= 0 || n > 255)
477 if (!getword_str(buf2, sizeof buf2, &startp, endp))
479 exptime = ns_datetosecs(buf2, &dateerror);
481 ShrinkBuffer(INT32SZ);
489 ottl = strtoul(buf2, &ulendp, 10);
491 (ulendp != NULL && *ulendp != '\0'))
493 ShrinkBuffer(INT32SZ);
495 if (!getword_str(buf2, sizeof buf2, &startp,
498 exptime = ns_datetosecs(buf2, &dateerror);
503 ShrinkBuffer(INT32SZ);
504 PUTLONG(exptime, cp);
506 if (!getword_str(buf2, sizeof buf2, &startp, endp))
508 timesigned = ns_datetosecs(buf2, &dateerror);
510 ShrinkBuffer(INT32SZ);
511 PUTLONG(timesigned, cp);
516 n = getnum_str(&startp, endp);
519 ShrinkBuffer(INT16SZ);
522 if (!getword_str(buf2, sizeof buf2, &startp, endp))
524 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
530 if ((n = getword_str(buf2, sizeof buf2,
533 siglen = b64_pton(buf2, buf3, sizeof(buf3));
536 ShrinkBuffer(siglen);
537 memcpy(cp, buf3, siglen);
543 n = gethexnum_str(&startp, endp);
546 ShrinkBuffer(INT16SZ);
549 n = getnum_str(&startp, endp);
555 n = getnum_str(&startp, endp);
561 if ((n = getword_str(buf2, sizeof buf2,
564 keylen = b64_pton(buf2, buf3, sizeof(buf3));
567 ShrinkBuffer(keylen);
568 memcpy(cp, buf3, keylen);
573 int success, nxt_type;
578 if (!getword_str(buf2, sizeof buf2, &startp, endp))
580 n = dn_comp(buf2, cp, buflen, NULL, NULL);
586 memset(data, 0, sizeof data);
588 if (!getword_str(buf2, sizeof buf2, &startp,
591 nxt_type = sym_ston(__p_type_syms, buf2,
593 if (!success || !ns_t_rr_p(nxt_type))
595 NS_NXT_BIT_SET(nxt_type, data);
596 if (nxt_type > maxtype)
599 n = maxtype/NS_NXT_BITS+1;
607 n = getnum_str(&startp, endp);
610 ShrinkBuffer(INT16SZ);
613 n = getnum_str(&startp, endp);
616 ShrinkBuffer(INT16SZ);
619 n = getnum_str(&startp, endp);
625 if ((n = getword_str(buf2, sizeof buf2,
628 certlen = b64_pton(buf2, buf3, sizeof(buf3));
631 ShrinkBuffer(certlen);
632 memcpy(cp, buf3, certlen);
636 if (!getword_str(buf2, sizeof buf2, &startp, endp))
638 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
640 ShrinkBuffer(NS_IN6ADDRSZ);
641 memcpy(cp, &in6a, NS_IN6ADDRSZ);
645 /* Order Preference Flags Service Replacement Regexp */
647 n = getnum_str(&startp, endp);
648 if (n < 0 || n > 65535)
650 ShrinkBuffer(INT16SZ);
653 n = getnum_str(&startp, endp);
654 if (n < 0 || n > 65535)
656 ShrinkBuffer(INT16SZ);
659 if ((n = getstr_str(buf2, sizeof buf2,
660 &startp, endp)) < 0) {
669 /* Service Classes */
670 if ((n = getstr_str(buf2, sizeof buf2,
671 &startp, endp)) < 0) {
681 if ((n = getstr_str(buf2, sizeof buf2,
682 &startp, endp)) < 0) {
692 if (!getword_str(buf2, sizeof buf2, &startp, endp))
694 n = dn_comp(buf2, cp, buflen, NULL, NULL);
703 n = (u_int16_t)((cp - sp2) - INT16SZ);
707 hp->qdcount = htons(counts[0]);
708 hp->ancount = htons(counts[1]);
709 hp->nscount = htons(counts[2]);
710 hp->arcount = htons(counts[3]);
715 * Get a whitespace delimited word from a string (not file)
716 * into buf. modify the start pointer to point after the
717 * word in the string.
720 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
724 for (cp = buf; *startpp <= endp; ) {
726 if (isspace(c) || c == '\0') {
727 if (cp != buf) /*%< trailing whitespace */
729 else { /*%< leading whitespace */
735 if (cp >= buf+size-1)
744 * get a white spae delimited string from memory. Process quoted strings
745 * and \\DDD escapes. Return length or -1 on error. Returned string may
748 static char digits[] = "0123456789";
750 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
758 for (cp = buf; *startpp <= endp; ) {
759 if ((c = **startpp) == '\0')
761 /* leading white space */
762 if ((cp == buf) && !seen_quote && isspace(c)) {
800 (strchr(digits, c) - digits);
810 } else if (!inquote && isspace(c))
812 if (cp >= buf+size-1)
820 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
824 * Get a whitespace delimited base 16 number from a string (not file) into buf
825 * update the start pointer to point after the number in the string.
828 gethexnum_str(u_char **startpp, u_char *endp) {
833 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
834 return getnum_str(startpp, endp);
836 for (n = 0; *startpp <= endp; ) {
838 if (isspace(c) || c == '\0') {
839 if (seendigit) /*%< trailing whitespace */
841 else { /*%< leading whitespace */
847 while ((*startpp <= endp) &&
848 ((c = **startpp) != '\n'))
855 if (c == ')' && seendigit) {
863 n = n * 16 + (c - '0');
865 n = n * 16 + (tolower(c) - 'a' + 10);
872 * Get a whitespace delimited base 10 number from a string (not file) into buf
873 * update the start pointer to point after the number in the string.
876 getnum_str(u_char **startpp, u_char *endp) {
881 for (n = 0; *startpp <= endp; ) {
883 if (isspace(c) || c == '\0') {
884 if (seendigit) /*%< trailing whitespace */
886 else { /*%< leading whitespace */
892 while ((*startpp <= endp) &&
893 ((c = **startpp) != '\n'))
900 if (c == ')' && seendigit) {
907 n = n * 10 + (c - '0');
914 * Allocate a resource record buffer & save rr info.
917 res_mkupdrec(int section, const char *dname,
918 u_int class, u_int type, u_long ttl) {
919 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
921 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
926 INIT_LINK(rrecp, r_link);
927 INIT_LINK(rrecp, r_glink);
928 rrecp->r_class = (ns_class)class;
929 rrecp->r_type = (ns_type)type;
931 rrecp->r_section = (ns_sect)section;
936 * Free a resource record buffer created by res_mkupdrec.
939 res_freeupdrec(ns_updrec *rrecp) {
940 /* Note: freeing r_dp is the caller's responsibility. */
941 if (rrecp->r_dname != NULL)
942 free(rrecp->r_dname);
947 struct valuelist * next;
948 struct valuelist * prev;
953 static struct valuelist *servicelist, *protolist;
956 res_buildservicelist() {
958 struct valuelist *slp;
965 while ((sp = getservent()) != NULL) {
966 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
969 slp->name = strdup(sp->s_name);
970 slp->proto = strdup(sp->s_proto);
971 if ((slp->name == NULL) || (slp->proto == NULL)) {
972 if (slp->name) free(slp->name);
973 if (slp->proto) free(slp->proto);
977 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
978 slp->next = servicelist;
981 servicelist->prev = slp;
988 res_destroyservicelist() {
989 struct valuelist *slp, *slp_next;
991 for (slp = servicelist; slp != NULL; slp = slp_next) {
992 slp_next = slp->next;
997 servicelist = (struct valuelist *)0;
1001 res_buildprotolist(void) {
1002 struct protoent *pp;
1003 struct valuelist *slp;
1010 while ((pp = getprotoent()) != NULL) {
1011 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1014 slp->name = strdup(pp->p_name);
1015 if (slp->name == NULL) {
1019 slp->port = pp->p_proto; /*%< host byte order */
1020 slp->next = protolist;
1023 protolist->prev = slp;
1030 res_destroyprotolist(void) {
1031 struct valuelist *plp, *plp_next;
1033 for (plp = protolist; plp != NULL; plp = plp_next) {
1034 plp_next = plp->next;
1038 protolist = (struct valuelist *)0;
1042 findservice(const char *s, struct valuelist **list) {
1043 struct valuelist *lp = *list;
1046 for (; lp != NULL; lp = lp->next)
1047 if (strcasecmp(lp->name, s) == 0) {
1049 lp->prev->next = lp->next;
1051 lp->next->prev = lp->prev;
1056 return (lp->port); /*%< host byte order */
1058 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1064 * Convert service name or (ascii) number to int.
1067 res_servicenumber(const char *p) {
1068 if (servicelist == (struct valuelist *)0)
1069 res_buildservicelist();
1070 return (findservice(p, &servicelist));
1074 * Convert protocol name or (ascii) number to int.
1077 res_protocolnumber(const char *p) {
1078 if (protolist == (struct valuelist *)0)
1079 res_buildprotolist();
1080 return (findservice(p, &protolist));
1083 static struct servent *
1084 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1085 struct valuelist **list = &servicelist;
1086 struct valuelist *lp = *list;
1087 static struct servent serv;
1090 for (; lp != NULL; lp = lp->next) {
1091 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1093 if (strcasecmp(lp->proto, proto) == 0) {
1095 lp->prev->next = lp->next;
1097 lp->next->prev = lp->prev;
1102 serv.s_name = lp->name;
1103 serv.s_port = htons((u_int16_t)lp->port);
1104 serv.s_proto = lp->proto;
1111 static struct protoent *
1112 cgetprotobynumber(int proto) { /*%< Host byte order. */
1113 struct valuelist **list = &protolist;
1114 struct valuelist *lp = *list;
1115 static struct protoent prot;
1117 for (; lp != NULL; lp = lp->next)
1118 if (lp->port == proto) { /*%< Host byte order. */
1120 lp->prev->next = lp->next;
1122 lp->next->prev = lp->prev;
1127 prot.p_name = lp->name;
1128 prot.p_proto = lp->port; /*%< Host byte order. */
1135 res_protocolname(int num) {
1136 static char number[8];
1137 struct protoent *pp;
1139 if (protolist == (struct valuelist *)0)
1140 res_buildprotolist();
1141 pp = cgetprotobynumber(num);
1143 (void) sprintf(number, "%d", num);
1146 return (pp->p_name);
1150 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1151 static char number[8];
1154 if (servicelist == (struct valuelist *)0)
1155 res_buildservicelist();
1156 ss = cgetservbyport(htons(port), proto);
1158 (void) sprintf(number, "%d", port);
1161 return (ss->s_name);