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. */
62 static int getnum_str(u_char **, u_char *);
63 static int gethexnum_str(u_char **, u_char *);
64 static int getword_str(char *, int, u_char **, u_char *);
65 static int getstr_str(char *, int, u_char **, u_char *);
67 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
74 int res_protocolnumber(const char *);
78 int res_servicenumber(const char *);
81 * Form update packets.
82 * Returns the size of the resulting packet if no error
86 *\li -1 if error in reading a word/number in rdata
87 * portion for update packets
88 *\li -2 if length of buffer passed is insufficient
89 *\li -3 if zone section is not the first section in
90 * the linked list, or section order has a problem
91 *\li -4 on a number overflow
92 *\li -5 unknown operation or no records
95 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
96 ns_updrec *rrecp_start = rrecp_in;
98 u_char *cp, *sp2, *startp, *endp;
99 int n, i, soanum, multiline;
102 struct in6_addr in6a;
104 u_char buf3[MAXDNAME];
105 int section, numrrs = 0, counts[ns_s_max];
106 u_int16_t rtype, rclass;
108 u_char *dnptrs[20], **dpp, **lastdnptr;
115 * Initialize header fields.
117 if ((buf == NULL) || (buflen < HFIXEDSZ))
119 memset(buf, 0, HFIXEDSZ);
121 statp->id = res_nrandomid(statp);
122 hp->id = htons(statp->id);
123 hp->opcode = ns_o_update;
130 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
132 if (rrecp_start == NULL)
134 else if (rrecp_start->r_section != S_ZONE)
137 memset(counts, 0, sizeof counts);
138 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
140 section = rrecp->r_section;
141 if (section < 0 || section >= ns_s_max)
144 for (i = section + 1; i < ns_s_max; i++)
147 rtype = rrecp->r_type;
148 rclass = rrecp->r_class;
150 /* overload class and type */
151 if (section == S_PREREQ) {
153 switch (rrecp->r_opcode) {
169 if (rrecp->r_size == 0)
174 "res_mkupdate: incorrect opcode: %d\n",
179 } else if (section == S_UPDATE) {
180 switch (rrecp->r_opcode) {
182 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
188 "res_mkupdate: incorrect opcode: %d\n",
196 * XXX appending default domain to owner name is omitted,
197 * fqdn must be provided
199 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
203 ShrinkBuffer(n + 2*INT16SZ);
205 PUTSHORT(rclass, cp);
206 if (section == S_ZONE) {
207 if (numrrs != 1 || rrecp->r_type != T_SOA)
211 ShrinkBuffer(INT32SZ + INT16SZ);
213 sp2 = cp; /*%< save pointer to length byte */
215 if (rrecp->r_size == 0) {
216 if (section == S_UPDATE && rclass != C_ANY)
223 startp = rrecp->r_data;
224 endp = startp + rrecp->r_size - 1;
225 /* XXX this should be done centrally. */
226 switch (rrecp->r_type) {
228 if (!getword_str(buf2, sizeof buf2, &startp, endp))
230 if (!inet_aton(buf2, &ina))
232 n1 = ntohl(ina.s_addr);
233 ShrinkBuffer(INT32SZ);
243 if (!getword_str(buf2, sizeof buf2, &startp, endp))
245 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
254 for (i = 0; i < 2; i++) {
255 if (!getword_str(buf2, sizeof buf2, &startp,
258 n = dn_comp(buf2, cp, buflen,
265 if (rrecp->r_type == T_SOA) {
266 ShrinkBuffer(5 * INT32SZ);
267 while (isspace(*startp) || !*startp)
269 if (*startp == '(') {
274 /* serial, refresh, retry, expire, minimum */
275 for (i = 0; i < 5; i++) {
276 soanum = getnum_str(&startp, endp);
282 while (isspace(*startp) || !*startp)
292 n = getnum_str(&startp, endp);
295 ShrinkBuffer(INT16SZ);
297 if (!getword_str(buf2, sizeof buf2, &startp, endp))
299 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
306 n = getnum_str(&startp, endp);
309 ShrinkBuffer(INT16SZ);
312 n = getnum_str(&startp, endp);
315 ShrinkBuffer(INT16SZ);
318 n = getnum_str(&startp, endp);
321 ShrinkBuffer(INT16SZ);
324 if (!getword_str(buf2, sizeof buf2, &startp, endp))
326 n = dn_comp(buf2, cp, buflen, NULL, NULL);
333 n = getnum_str(&startp, endp);
337 ShrinkBuffer(INT16SZ);
338 for (i = 0; i < 2; i++) {
339 if (!getword_str(buf2, sizeof buf2, &startp,
342 n = dn_comp(buf2, cp, buflen, dnptrs,
352 unsigned int maxbm = 0;
354 if (!getword_str(buf2, sizeof buf2, &startp, endp))
356 if (!inet_aton(buf2, &ina))
358 n1 = ntohl(ina.s_addr);
359 ShrinkBuffer(INT32SZ);
362 if (!getword_str(buf2, sizeof buf2, &startp, endp))
364 if ((i = res_protocolnumber(buf2)) < 0)
369 for (i = 0; i < MAXPORT/8 ; i++)
372 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
373 if ((n = res_servicenumber(buf2)) <= 0)
377 bm[n/8] |= (0x80>>(n%8));
378 if ((unsigned)n > maxbm)
385 memcpy(cp, bm, maxbm);
390 for (i = 0; i < 2; i++) {
391 if ((n = getstr_str(buf2, sizeof buf2,
404 if ((n = getstr_str(buf2, sizeof buf2,
405 &startp, endp)) < 0) {
406 if (cp != (sp2 + INT16SZ))
420 if ((n = getstr_str(buf2, sizeof buf2, &startp,
432 if ((n = getstr_str(buf2, sizeof buf2, &startp,
435 if ((n > 255) || (n == 0))
441 if ((n = getstr_str(buf2, sizeof buf2, &startp,
452 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
461 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
473 int sig_type, success, dateerror;
474 u_int32_t exptime, timesigned;
477 if ((n = getword_str(buf2, sizeof buf2,
480 sig_type = sym_ston(__p_type_syms, buf2, &success);
481 if (!success || sig_type == ns_t_any)
483 ShrinkBuffer(INT16SZ);
484 PUTSHORT(sig_type, cp);
486 n = getnum_str(&startp, endp);
492 n = getnum_str(&startp, endp);
493 if (n <= 0 || n > 255)
498 if (!getword_str(buf2, sizeof buf2, &startp, endp))
500 exptime = ns_datetosecs(buf2, &dateerror);
502 ShrinkBuffer(INT32SZ);
510 ottl = strtoul(buf2, &ulendp, 10);
512 (ulendp != NULL && *ulendp != '\0'))
514 ShrinkBuffer(INT32SZ);
516 if (!getword_str(buf2, sizeof buf2, &startp,
519 exptime = ns_datetosecs(buf2, &dateerror);
524 ShrinkBuffer(INT32SZ);
525 PUTLONG(exptime, cp);
527 if (!getword_str(buf2, sizeof buf2, &startp, endp))
529 timesigned = ns_datetosecs(buf2, &dateerror);
531 ShrinkBuffer(INT32SZ);
532 PUTLONG(timesigned, cp);
537 n = getnum_str(&startp, endp);
540 ShrinkBuffer(INT16SZ);
543 if (!getword_str(buf2, sizeof buf2, &startp, endp))
545 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
551 if ((n = getword_str(buf2, sizeof buf2,
554 siglen = b64_pton(buf2, buf3, sizeof(buf3));
557 ShrinkBuffer(siglen);
558 memcpy(cp, buf3, siglen);
565 n = gethexnum_str(&startp, endp);
568 ShrinkBuffer(INT16SZ);
571 n = getnum_str(&startp, endp);
577 n = getnum_str(&startp, endp);
583 if ((n = getword_str(buf2, sizeof buf2,
586 keylen = b64_pton(buf2, buf3, sizeof(buf3));
589 ShrinkBuffer(keylen);
590 memcpy(cp, buf3, keylen);
595 int success, nxt_type;
600 if (!getword_str(buf2, sizeof buf2, &startp, endp))
602 n = dn_comp(buf2, cp, buflen, NULL, NULL);
608 memset(data, 0, sizeof data);
610 if (!getword_str(buf2, sizeof buf2, &startp,
613 nxt_type = sym_ston(__p_type_syms, buf2,
615 if (!success || !ns_t_rr_p(nxt_type))
617 NS_NXT_BIT_SET(nxt_type, data);
618 if (nxt_type > maxtype)
621 n = maxtype/NS_NXT_BITS+1;
629 n = getnum_str(&startp, endp);
632 ShrinkBuffer(INT16SZ);
635 n = getnum_str(&startp, endp);
638 ShrinkBuffer(INT16SZ);
641 n = getnum_str(&startp, endp);
647 if ((n = getword_str(buf2, sizeof buf2,
650 certlen = b64_pton(buf2, buf3, sizeof(buf3));
653 ShrinkBuffer(certlen);
654 memcpy(cp, buf3, certlen);
658 if (!getword_str(buf2, sizeof buf2, &startp, endp))
660 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
662 ShrinkBuffer(NS_IN6ADDRSZ);
663 memcpy(cp, &in6a, NS_IN6ADDRSZ);
667 /* Order Preference Flags Service Replacement Regexp */
669 n = getnum_str(&startp, endp);
670 if (n < 0 || n > 65535)
672 ShrinkBuffer(INT16SZ);
675 n = getnum_str(&startp, endp);
676 if (n < 0 || n > 65535)
678 ShrinkBuffer(INT16SZ);
681 if ((n = getstr_str(buf2, sizeof buf2,
682 &startp, endp)) < 0) {
691 /* Service Classes */
692 if ((n = getstr_str(buf2, sizeof buf2,
693 &startp, endp)) < 0) {
703 if ((n = getstr_str(buf2, sizeof buf2,
704 &startp, endp)) < 0) {
714 if (!getword_str(buf2, sizeof buf2, &startp, endp))
716 n = dn_comp(buf2, cp, buflen, NULL, NULL);
725 n = (u_int16_t)((cp - sp2) - INT16SZ);
729 hp->qdcount = htons(counts[0]);
730 hp->ancount = htons(counts[1]);
731 hp->nscount = htons(counts[2]);
732 hp->arcount = htons(counts[3]);
737 * Get a whitespace delimited word from a string (not file)
738 * into buf. modify the start pointer to point after the
739 * word in the string.
742 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
746 for (cp = buf; *startpp <= endp; ) {
748 if (isspace(c) || c == '\0') {
749 if (cp != buf) /*%< trailing whitespace */
751 else { /*%< leading whitespace */
757 if (cp >= buf+size-1)
766 * get a white spae delimited string from memory. Process quoted strings
767 * and \\DDD escapes. Return length or -1 on error. Returned string may
770 static char digits[] = "0123456789";
772 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
780 for (cp = buf; *startpp <= endp; ) {
781 if ((c = **startpp) == '\0')
783 /* leading white space */
784 if ((cp == buf) && !seen_quote && isspace(c)) {
822 (strchr(digits, c) - digits);
832 } else if (!inquote && isspace(c))
834 if (cp >= buf+size-1)
842 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
846 * Get a whitespace delimited base 16 number from a string (not file) into buf
847 * update the start pointer to point after the number in the string.
850 gethexnum_str(u_char **startpp, u_char *endp) {
855 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
856 return getnum_str(startpp, endp);
858 for (n = 0; *startpp <= endp; ) {
860 if (isspace(c) || c == '\0') {
861 if (seendigit) /*%< trailing whitespace */
863 else { /*%< leading whitespace */
869 while ((*startpp <= endp) &&
870 ((c = **startpp) != '\n'))
877 if (c == ')' && seendigit) {
885 n = n * 16 + (c - '0');
887 n = n * 16 + (tolower(c) - 'a' + 10);
894 * Get a whitespace delimited base 10 number from a string (not file) into buf
895 * update the start pointer to point after the number in the string.
898 getnum_str(u_char **startpp, u_char *endp) {
903 for (n = 0; *startpp <= endp; ) {
905 if (isspace(c) || c == '\0') {
906 if (seendigit) /*%< trailing whitespace */
908 else { /*%< leading whitespace */
914 while ((*startpp <= endp) &&
915 ((c = **startpp) != '\n'))
922 if (c == ')' && seendigit) {
929 n = n * 10 + (c - '0');
936 * Allocate a resource record buffer & save rr info.
939 res_mkupdrec(int section, const char *dname,
940 u_int class, u_int type, u_long ttl) {
941 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
943 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
948 INIT_LINK(rrecp, r_link);
949 INIT_LINK(rrecp, r_glink);
950 rrecp->r_class = (ns_class)class;
951 rrecp->r_type = (ns_type)type;
953 rrecp->r_section = (ns_sect)section;
958 * Free a resource record buffer created by res_mkupdrec.
961 res_freeupdrec(ns_updrec *rrecp) {
962 /* Note: freeing r_dp is the caller's responsibility. */
963 if (rrecp->r_dname != NULL)
964 free(rrecp->r_dname);
969 struct valuelist * next;
970 struct valuelist * prev;
975 static struct valuelist *servicelist, *protolist;
978 res_buildservicelist() {
980 struct valuelist *slp;
987 while ((sp = getservent()) != NULL) {
988 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
991 slp->name = strdup(sp->s_name);
992 slp->proto = strdup(sp->s_proto);
993 if ((slp->name == NULL) || (slp->proto == NULL)) {
994 if (slp->name) free(slp->name);
995 if (slp->proto) free(slp->proto);
999 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
1000 slp->next = servicelist;
1003 servicelist->prev = slp;
1011 res_destroyservicelist() {
1012 struct valuelist *slp, *slp_next;
1014 for (slp = servicelist; slp != NULL; slp = slp_next) {
1015 slp_next = slp->next;
1020 servicelist = (struct valuelist *)0;
1028 res_buildprotolist(void) {
1029 struct protoent *pp;
1030 struct valuelist *slp;
1037 while ((pp = getprotoent()) != NULL) {
1038 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1041 slp->name = strdup(pp->p_name);
1042 if (slp->name == NULL) {
1046 slp->port = pp->p_proto; /*%< host byte order */
1047 slp->next = protolist;
1050 protolist->prev = slp;
1058 res_destroyprotolist(void) {
1059 struct valuelist *plp, *plp_next;
1061 for (plp = protolist; plp != NULL; plp = plp_next) {
1062 plp_next = plp->next;
1066 protolist = (struct valuelist *)0;
1071 findservice(const char *s, struct valuelist **list) {
1072 struct valuelist *lp = *list;
1075 for (; lp != NULL; lp = lp->next)
1076 if (strcasecmp(lp->name, s) == 0) {
1078 lp->prev->next = lp->next;
1080 lp->next->prev = lp->prev;
1085 return (lp->port); /*%< host byte order */
1087 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1093 * Convert service name or (ascii) number to int.
1099 res_servicenumber(const char *p) {
1100 if (servicelist == (struct valuelist *)0)
1101 res_buildservicelist();
1102 return (findservice(p, &servicelist));
1106 * Convert protocol name or (ascii) number to int.
1112 res_protocolnumber(const char *p) {
1113 if (protolist == (struct valuelist *)0)
1114 res_buildprotolist();
1115 return (findservice(p, &protolist));
1119 static struct servent *
1120 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1121 struct valuelist **list = &servicelist;
1122 struct valuelist *lp = *list;
1123 static struct servent serv;
1126 for (; lp != NULL; lp = lp->next) {
1127 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1129 if (strcasecmp(lp->proto, proto) == 0) {
1131 lp->prev->next = lp->next;
1133 lp->next->prev = lp->prev;
1138 serv.s_name = lp->name;
1139 serv.s_port = htons((u_int16_t)lp->port);
1140 serv.s_proto = lp->proto;
1147 static struct protoent *
1148 cgetprotobynumber(int proto) { /*%< Host byte order. */
1149 struct valuelist **list = &protolist;
1150 struct valuelist *lp = *list;
1151 static struct protoent prot;
1153 for (; lp != NULL; lp = lp->next)
1154 if (lp->port == proto) { /*%< Host byte order. */
1156 lp->prev->next = lp->next;
1158 lp->next->prev = lp->prev;
1163 prot.p_name = lp->name;
1164 prot.p_proto = lp->port; /*%< Host byte order. */
1171 res_protocolname(int num) {
1172 static char number[8];
1173 struct protoent *pp;
1175 if (protolist == (struct valuelist *)0)
1176 res_buildprotolist();
1177 pp = cgetprotobynumber(num);
1179 (void) sprintf(number, "%d", num);
1182 return (pp->p_name);
1186 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1187 static char number[8];
1190 if (servicelist == (struct valuelist *)0)
1191 res_buildservicelist();
1192 ss = cgetservbyport(htons(port), proto);
1194 (void) sprintf(number, "%d", port);
1197 return (ss->s_name);