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.
17 * $Id: res_mkupdate.c,v 1.8 2005/10/14 05:44:26 marka Exp $
22 * Based on the Dynamic DNS reference implementation by Viraj Bais
23 * <viraj_bais@ccm.fm.intel.com>
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <arpa/inet.h>
39 #include <res_update.h>
49 #include "port_after.h"
51 /* Options. Leave them on. */
55 static int getnum_str(u_char **, u_char *);
56 static int gethexnum_str(u_char **, u_char *);
57 static int getword_str(char *, int, u_char **, u_char *);
58 static int getstr_str(char *, int, u_char **, u_char *);
60 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
66 int res_protocolnumber(const char *);
70 int res_servicenumber(const char *);
73 * Form update packets.
74 * Returns the size of the resulting packet if no error
78 *\li -1 if error in reading a word/number in rdata
79 * portion for update packets
80 *\li -2 if length of buffer passed is insufficient
81 *\li -3 if zone section is not the first section in
82 * the linked list, or section order has a problem
83 *\li -4 on a number overflow
84 *\li -5 unknown operation or no records
87 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
88 ns_updrec *rrecp_start = rrecp_in;
90 u_char *cp, *sp2, *startp, *endp;
91 int n, i, soanum, multiline;
96 u_char buf3[MAXDNAME];
97 int section, numrrs = 0, counts[ns_s_max];
98 u_int16_t rtype, rclass;
100 u_char *dnptrs[20], **dpp, **lastdnptr;
107 * Initialize header fields.
109 if ((buf == NULL) || (buflen < HFIXEDSZ))
111 memset(buf, 0, HFIXEDSZ);
113 hp->id = htons(++statp->id);
114 hp->opcode = ns_o_update;
121 lastdnptr = dnptrs + NELEM(dnptrs);
123 if (rrecp_start == NULL)
125 else if (rrecp_start->r_section != S_ZONE)
128 memset(counts, 0, sizeof counts);
129 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
131 section = rrecp->r_section;
132 if (section < 0 || section >= ns_s_max)
135 for (i = section + 1; i < ns_s_max; i++)
138 rtype = rrecp->r_type;
139 rclass = rrecp->r_class;
141 /* overload class and type */
142 if (section == S_PREREQ) {
144 switch (rrecp->r_opcode) {
160 if (rrecp->r_size == 0)
165 "res_mkupdate: incorrect opcode: %d\n",
170 } else if (section == S_UPDATE) {
171 switch (rrecp->r_opcode) {
173 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
179 "res_mkupdate: incorrect opcode: %d\n",
187 * XXX appending default domain to owner name is omitted,
188 * fqdn must be provided
190 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
194 ShrinkBuffer(n + 2*INT16SZ);
196 PUTSHORT(rclass, cp);
197 if (section == S_ZONE) {
198 if (numrrs != 1 || rrecp->r_type != T_SOA)
202 ShrinkBuffer(INT32SZ + INT16SZ);
204 sp2 = cp; /*%< save pointer to length byte */
206 if (rrecp->r_size == 0) {
207 if (section == S_UPDATE && rclass != C_ANY)
214 startp = rrecp->r_data;
215 endp = startp + rrecp->r_size - 1;
216 /* XXX this should be done centrally. */
217 switch (rrecp->r_type) {
219 if (!getword_str(buf2, sizeof buf2, &startp, endp))
221 if (!inet_aton(buf2, &ina))
223 n1 = ntohl(ina.s_addr);
224 ShrinkBuffer(INT32SZ);
234 if (!getword_str(buf2, sizeof buf2, &startp, endp))
236 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
245 for (i = 0; i < 2; i++) {
246 if (!getword_str(buf2, sizeof buf2, &startp,
249 n = dn_comp(buf2, cp, buflen,
256 if (rrecp->r_type == T_SOA) {
257 ShrinkBuffer(5 * INT32SZ);
258 while (isspace(*startp) || !*startp)
260 if (*startp == '(') {
265 /* serial, refresh, retry, expire, minimum */
266 for (i = 0; i < 5; i++) {
267 soanum = getnum_str(&startp, endp);
273 while (isspace(*startp) || !*startp)
283 n = getnum_str(&startp, endp);
286 ShrinkBuffer(INT16SZ);
288 if (!getword_str(buf2, sizeof buf2, &startp, endp))
290 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
297 n = getnum_str(&startp, endp);
300 ShrinkBuffer(INT16SZ);
303 n = getnum_str(&startp, endp);
306 ShrinkBuffer(INT16SZ);
309 n = getnum_str(&startp, endp);
312 ShrinkBuffer(INT16SZ);
315 if (!getword_str(buf2, sizeof buf2, &startp, endp))
317 n = dn_comp(buf2, cp, buflen, NULL, NULL);
324 n = getnum_str(&startp, endp);
328 ShrinkBuffer(INT16SZ);
329 for (i = 0; i < 2; i++) {
330 if (!getword_str(buf2, sizeof buf2, &startp,
333 n = dn_comp(buf2, cp, buflen, dnptrs,
343 unsigned int maxbm = 0;
345 if (!getword_str(buf2, sizeof buf2, &startp, endp))
347 if (!inet_aton(buf2, &ina))
349 n1 = ntohl(ina.s_addr);
350 ShrinkBuffer(INT32SZ);
353 if (!getword_str(buf2, sizeof buf2, &startp, endp))
355 if ((i = res_protocolnumber(buf2)) < 0)
360 for (i = 0; i < MAXPORT/8 ; i++)
363 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
364 if ((n = res_servicenumber(buf2)) <= 0)
368 bm[n/8] |= (0x80>>(n%8));
369 if ((unsigned)n > maxbm)
376 memcpy(cp, bm, maxbm);
381 for (i = 0; i < 2; i++) {
382 if ((n = getstr_str(buf2, sizeof buf2,
395 if ((n = getstr_str(buf2, sizeof buf2,
396 &startp, endp)) < 0) {
397 if (cp != (sp2 + INT16SZ))
411 if ((n = getstr_str(buf2, sizeof buf2, &startp,
423 if ((n = getstr_str(buf2, sizeof buf2, &startp,
426 if ((n > 255) || (n == 0))
432 if ((n = getstr_str(buf2, sizeof buf2, &startp,
443 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
452 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
464 int sig_type, success, dateerror;
465 u_int32_t exptime, timesigned;
468 if ((n = getword_str(buf2, sizeof buf2,
471 sig_type = sym_ston(__p_type_syms, buf2, &success);
472 if (!success || sig_type == ns_t_any)
474 ShrinkBuffer(INT16SZ);
475 PUTSHORT(sig_type, cp);
477 n = getnum_str(&startp, endp);
483 n = getnum_str(&startp, endp);
484 if (n <= 0 || n > 255)
489 if (!getword_str(buf2, sizeof buf2, &startp, endp))
491 exptime = ns_datetosecs(buf2, &dateerror);
493 ShrinkBuffer(INT32SZ);
501 ottl = strtoul(buf2, &ulendp, 10);
503 (ulendp != NULL && *ulendp != '\0'))
505 ShrinkBuffer(INT32SZ);
507 if (!getword_str(buf2, sizeof buf2, &startp,
510 exptime = ns_datetosecs(buf2, &dateerror);
515 ShrinkBuffer(INT32SZ);
516 PUTLONG(exptime, cp);
518 if (!getword_str(buf2, sizeof buf2, &startp, endp))
520 timesigned = ns_datetosecs(buf2, &dateerror);
522 ShrinkBuffer(INT32SZ);
523 PUTLONG(timesigned, cp);
528 n = getnum_str(&startp, endp);
531 ShrinkBuffer(INT16SZ);
534 if (!getword_str(buf2, sizeof buf2, &startp, endp))
536 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
542 if ((n = getword_str(buf2, sizeof buf2,
545 siglen = b64_pton(buf2, buf3, sizeof(buf3));
548 ShrinkBuffer(siglen);
549 memcpy(cp, buf3, siglen);
556 n = gethexnum_str(&startp, endp);
559 ShrinkBuffer(INT16SZ);
562 n = getnum_str(&startp, endp);
568 n = getnum_str(&startp, endp);
574 if ((n = getword_str(buf2, sizeof buf2,
577 keylen = b64_pton(buf2, buf3, sizeof(buf3));
580 ShrinkBuffer(keylen);
581 memcpy(cp, buf3, keylen);
586 int success, nxt_type;
591 if (!getword_str(buf2, sizeof buf2, &startp, endp))
593 n = dn_comp(buf2, cp, buflen, NULL, NULL);
599 memset(data, 0, sizeof data);
601 if (!getword_str(buf2, sizeof buf2, &startp,
604 nxt_type = sym_ston(__p_type_syms, buf2,
606 if (!success || !ns_t_rr_p(nxt_type))
608 NS_NXT_BIT_SET(nxt_type, data);
609 if (nxt_type > maxtype)
612 n = maxtype/NS_NXT_BITS+1;
620 n = getnum_str(&startp, endp);
623 ShrinkBuffer(INT16SZ);
626 n = getnum_str(&startp, endp);
629 ShrinkBuffer(INT16SZ);
632 n = getnum_str(&startp, endp);
638 if ((n = getword_str(buf2, sizeof buf2,
641 certlen = b64_pton(buf2, buf3, sizeof(buf3));
644 ShrinkBuffer(certlen);
645 memcpy(cp, buf3, certlen);
649 if (!getword_str(buf2, sizeof buf2, &startp, endp))
651 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
653 ShrinkBuffer(NS_IN6ADDRSZ);
654 memcpy(cp, &in6a, NS_IN6ADDRSZ);
658 /* Order Preference Flags Service Replacement Regexp */
660 n = getnum_str(&startp, endp);
661 if (n < 0 || n > 65535)
663 ShrinkBuffer(INT16SZ);
666 n = getnum_str(&startp, endp);
667 if (n < 0 || n > 65535)
669 ShrinkBuffer(INT16SZ);
672 if ((n = getstr_str(buf2, sizeof buf2,
673 &startp, endp)) < 0) {
682 /* Service Classes */
683 if ((n = getstr_str(buf2, sizeof buf2,
684 &startp, endp)) < 0) {
694 if ((n = getstr_str(buf2, sizeof buf2,
695 &startp, endp)) < 0) {
705 if (!getword_str(buf2, sizeof buf2, &startp, endp))
707 n = dn_comp(buf2, cp, buflen, NULL, NULL);
716 n = (u_int16_t)((cp - sp2) - INT16SZ);
720 hp->qdcount = htons(counts[0]);
721 hp->ancount = htons(counts[1]);
722 hp->nscount = htons(counts[2]);
723 hp->arcount = htons(counts[3]);
728 * Get a whitespace delimited word from a string (not file)
729 * into buf. modify the start pointer to point after the
730 * word in the string.
733 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
737 for (cp = buf; *startpp <= endp; ) {
739 if (isspace(c) || c == '\0') {
740 if (cp != buf) /*%< trailing whitespace */
742 else { /*%< leading whitespace */
748 if (cp >= buf+size-1)
757 * get a white spae delimited string from memory. Process quoted strings
758 * and \\DDD escapes. Return length or -1 on error. Returned string may
761 static char digits[] = "0123456789";
763 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
771 for (cp = buf; *startpp <= endp; ) {
772 if ((c = **startpp) == '\0')
774 /* leading white space */
775 if ((cp == buf) && !seen_quote && isspace(c)) {
813 (strchr(digits, c) - digits);
823 } else if (!inquote && isspace(c))
825 if (cp >= buf+size-1)
833 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
837 * Get a whitespace delimited base 16 number from a string (not file) into buf
838 * update the start pointer to point after the number in the string.
841 gethexnum_str(u_char **startpp, u_char *endp) {
846 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
847 return getnum_str(startpp, endp);
849 for (n = 0; *startpp <= endp; ) {
851 if (isspace(c) || c == '\0') {
852 if (seendigit) /*%< trailing whitespace */
854 else { /*%< leading whitespace */
860 while ((*startpp <= endp) &&
861 ((c = **startpp) != '\n'))
868 if (c == ')' && seendigit) {
876 n = n * 16 + (c - '0');
878 n = n * 16 + (tolower(c) - 'a' + 10);
885 * Get a whitespace delimited base 10 number from a string (not file) into buf
886 * update the start pointer to point after the number in the string.
889 getnum_str(u_char **startpp, u_char *endp) {
894 for (n = 0; *startpp <= endp; ) {
896 if (isspace(c) || c == '\0') {
897 if (seendigit) /*%< trailing whitespace */
899 else { /*%< leading whitespace */
905 while ((*startpp <= endp) &&
906 ((c = **startpp) != '\n'))
913 if (c == ')' && seendigit) {
920 n = n * 10 + (c - '0');
927 * Allocate a resource record buffer & save rr info.
930 res_mkupdrec(int section, const char *dname,
931 u_int class, u_int type, u_long ttl) {
932 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
934 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
939 INIT_LINK(rrecp, r_link);
940 INIT_LINK(rrecp, r_glink);
941 rrecp->r_class = (ns_class)class;
942 rrecp->r_type = (ns_type)type;
944 rrecp->r_section = (ns_sect)section;
949 * Free a resource record buffer created by res_mkupdrec.
952 res_freeupdrec(ns_updrec *rrecp) {
953 /* Note: freeing r_dp is the caller's responsibility. */
954 if (rrecp->r_dname != NULL)
955 free(rrecp->r_dname);
960 struct valuelist * next;
961 struct valuelist * prev;
966 static struct valuelist *servicelist, *protolist;
969 res_buildservicelist(void) {
971 struct valuelist *slp;
978 while ((sp = getservent()) != NULL) {
979 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
982 slp->name = strdup(sp->s_name);
983 slp->proto = strdup(sp->s_proto);
984 if ((slp->name == NULL) || (slp->proto == NULL)) {
985 if (slp->name) free(slp->name);
986 if (slp->proto) free(slp->proto);
990 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
991 slp->next = servicelist;
994 servicelist->prev = slp;
1002 res_destroyservicelist() {
1003 struct valuelist *slp, *slp_next;
1005 for (slp = servicelist; slp != NULL; slp = slp_next) {
1006 slp_next = slp->next;
1011 servicelist = (struct valuelist *)0;
1016 res_buildprotolist(void) {
1017 struct protoent *pp;
1018 struct valuelist *slp;
1025 while ((pp = getprotoent()) != NULL) {
1026 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1029 slp->name = strdup(pp->p_name);
1030 if (slp->name == NULL) {
1034 slp->port = pp->p_proto; /*%< host byte order */
1035 slp->next = protolist;
1038 protolist->prev = slp;
1046 res_destroyprotolist(void) {
1047 struct valuelist *plp, *plp_next;
1049 for (plp = protolist; plp != NULL; plp = plp_next) {
1050 plp_next = plp->next;
1054 protolist = (struct valuelist *)0;
1059 findservice(const char *s, struct valuelist **list) {
1060 struct valuelist *lp = *list;
1063 for (; lp != NULL; lp = lp->next)
1064 if (strcasecmp(lp->name, s) == 0) {
1066 lp->prev->next = lp->next;
1068 lp->next->prev = lp->prev;
1073 return (lp->port); /*%< host byte order */
1075 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1081 * Convert service name or (ascii) number to int.
1087 res_servicenumber(const char *p) {
1088 if (servicelist == (struct valuelist *)0)
1089 res_buildservicelist();
1090 return (findservice(p, &servicelist));
1094 * Convert protocol name or (ascii) number to int.
1100 res_protocolnumber(const char *p) {
1101 if (protolist == (struct valuelist *)0)
1102 res_buildprotolist();
1103 return (findservice(p, &protolist));
1107 static struct servent *
1108 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1109 struct valuelist **list = &servicelist;
1110 struct valuelist *lp = *list;
1111 static struct servent serv;
1114 for (; lp != NULL; lp = lp->next) {
1115 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1117 if (strcasecmp(lp->proto, proto) == 0) {
1119 lp->prev->next = lp->next;
1121 lp->next->prev = lp->prev;
1126 serv.s_name = lp->name;
1127 serv.s_port = htons((u_int16_t)lp->port);
1128 serv.s_proto = lp->proto;
1135 static struct protoent *
1136 cgetprotobynumber(int proto) { /*%< Host byte order. */
1137 struct valuelist **list = &protolist;
1138 struct valuelist *lp = *list;
1139 static struct protoent prot;
1141 for (; lp != NULL; lp = lp->next)
1142 if (lp->port == proto) { /*%< Host byte order. */
1144 lp->prev->next = lp->next;
1146 lp->next->prev = lp->prev;
1151 prot.p_name = lp->name;
1152 prot.p_proto = lp->port; /*%< Host byte order. */
1159 res_protocolname(int num) {
1160 static char number[8];
1161 struct protoent *pp;
1163 if (protolist == (struct valuelist *)0)
1164 res_buildprotolist();
1165 pp = cgetprotobynumber(num);
1167 (void) sprintf(number, "%d", num);
1170 return (pp->p_name);
1174 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1175 static char number[8];
1178 if (servicelist == (struct valuelist *)0)
1179 res_buildservicelist();
1180 ss = cgetservbyport(htons(port), proto);
1182 (void) sprintf(number, "%d", port);
1185 return (ss->s_name);