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.
19 * Based on the Dynamic DNS reference implementation by Viraj Bais
20 * <viraj_bais@ccm.fm.intel.com>
23 #if !defined(lint) && !defined(SABER)
24 static const char rcsid[] = "$Id: res_mkupdate.c,v 1.1.2.4 2004/06/03 04:45:16 marka Exp $";
27 #include "port_before.h"
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
34 #include <arpa/inet.h>
40 #include <res_update.h>
47 #include "port_after.h"
49 /* Options. Leave them on. */
53 static int getnum_str(u_char **, u_char *);
54 static int gethexnum_str(u_char **, u_char *);
55 static int getword_str(char *, int, u_char **, u_char *);
56 static int getstr_str(char *, int, u_char **, u_char *);
58 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
62 int res_protocolnumber(const char *);
63 int res_servicenumber(const char *);
66 * Form update packets.
67 * Returns the size of the resulting packet if no error
69 * returns -1 if error in reading a word/number in rdata
70 * portion for update packets
71 * -2 if length of buffer passed is insufficient
72 * -3 if zone section is not the first section in
73 * the linked list, or section order has a problem
74 * -4 on a number overflow
75 * -5 unknown operation or no records
78 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
79 ns_updrec *rrecp_start = rrecp_in;
81 u_char *cp, *sp1, *sp2, *startp, *endp;
82 int n, i, soanum, multiline;
87 u_char buf3[MAXDNAME];
88 int section, numrrs = 0, counts[ns_s_max];
89 u_int16_t rtype, rclass;
91 u_char *dnptrs[20], **dpp, **lastdnptr;
92 int siglen, keylen, certlen;
95 * Initialize header fields.
97 if ((buf == NULL) || (buflen < HFIXEDSZ))
99 memset(buf, 0, HFIXEDSZ);
101 hp->id = htons(++statp->id);
102 hp->opcode = ns_o_update;
104 sp1 = buf + 2*INT16SZ; /* save pointer to zocount */
110 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
112 if (rrecp_start == NULL)
114 else if (rrecp_start->r_section != S_ZONE)
117 memset(counts, 0, sizeof counts);
118 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
120 section = rrecp->r_section;
121 if (section < 0 || section >= ns_s_max)
124 for (i = section + 1; i < ns_s_max; i++)
127 rtype = rrecp->r_type;
128 rclass = rrecp->r_class;
130 /* overload class and type */
131 if (section == S_PREREQ) {
133 switch (rrecp->r_opcode) {
149 if (rrecp->r_size == 0)
154 "res_mkupdate: incorrect opcode: %d\n",
159 } else if (section == S_UPDATE) {
160 switch (rrecp->r_opcode) {
162 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
168 "res_mkupdate: incorrect opcode: %d\n",
176 * XXX appending default domain to owner name is omitted,
177 * fqdn must be provided
179 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
183 ShrinkBuffer(n + 2*INT16SZ);
185 PUTSHORT(rclass, cp);
186 if (section == S_ZONE) {
187 if (numrrs != 1 || rrecp->r_type != T_SOA)
191 ShrinkBuffer(INT32SZ + INT16SZ);
193 sp2 = cp; /* save pointer to length byte */
195 if (rrecp->r_size == 0) {
196 if (section == S_UPDATE && rclass != C_ANY)
203 startp = rrecp->r_data;
204 endp = startp + rrecp->r_size - 1;
205 /* XXX this should be done centrally. */
206 switch (rrecp->r_type) {
208 if (!getword_str(buf2, sizeof buf2, &startp, endp))
210 if (!inet_aton(buf2, &ina))
212 n1 = ntohl(ina.s_addr);
213 ShrinkBuffer(INT32SZ);
223 if (!getword_str(buf2, sizeof buf2, &startp, endp))
225 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
234 for (i = 0; i < 2; i++) {
235 if (!getword_str(buf2, sizeof buf2, &startp,
238 n = dn_comp(buf2, cp, buflen,
245 if (rrecp->r_type == T_SOA) {
246 ShrinkBuffer(5 * INT32SZ);
247 while (isspace(*startp) || !*startp)
249 if (*startp == '(') {
254 /* serial, refresh, retry, expire, minimum */
255 for (i = 0; i < 5; i++) {
256 soanum = getnum_str(&startp, endp);
262 while (isspace(*startp) || !*startp)
272 n = getnum_str(&startp, endp);
275 ShrinkBuffer(INT16SZ);
277 if (!getword_str(buf2, sizeof buf2, &startp, endp))
279 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
286 n = getnum_str(&startp, endp);
289 ShrinkBuffer(INT16SZ);
292 n = getnum_str(&startp, endp);
295 ShrinkBuffer(INT16SZ);
298 n = getnum_str(&startp, endp);
301 ShrinkBuffer(INT16SZ);
304 if (!getword_str(buf2, sizeof buf2, &startp, endp))
306 n = dn_comp(buf2, cp, buflen, NULL, NULL);
313 n = getnum_str(&startp, endp);
317 ShrinkBuffer(INT16SZ);
318 for (i = 0; i < 2; i++) {
319 if (!getword_str(buf2, sizeof buf2, &startp,
322 n = dn_comp(buf2, cp, buflen, dnptrs,
332 unsigned int maxbm = 0;
334 if (!getword_str(buf2, sizeof buf2, &startp, endp))
336 if (!inet_aton(buf2, &ina))
338 n1 = ntohl(ina.s_addr);
339 ShrinkBuffer(INT32SZ);
342 if (!getword_str(buf2, sizeof buf2, &startp, endp))
344 if ((i = res_protocolnumber(buf2)) < 0)
349 for (i = 0; i < MAXPORT/8 ; i++)
352 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
353 if ((n = res_servicenumber(buf2)) <= 0)
357 bm[n/8] |= (0x80>>(n%8));
358 if ((unsigned)n > maxbm)
365 memcpy(cp, bm, maxbm);
370 for (i = 0; i < 2; i++) {
371 if ((n = getstr_str(buf2, sizeof buf2,
384 if ((n = getstr_str(buf2, sizeof buf2,
385 &startp, endp)) < 0) {
386 if (cp != (sp2 + INT16SZ))
400 if ((n = getstr_str(buf2, sizeof buf2, &startp,
412 if ((n = getstr_str(buf2, sizeof buf2, &startp,
415 if ((n > 255) || (n == 0))
421 if ((n = getstr_str(buf2, sizeof buf2, &startp,
432 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
441 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
450 int sig_type, success, dateerror;
451 u_int32_t exptime, timesigned;
454 if ((n = getword_str(buf2, sizeof buf2,
457 sig_type = sym_ston(__p_type_syms, buf2, &success);
458 if (!success || sig_type == ns_t_any)
460 ShrinkBuffer(INT16SZ);
461 PUTSHORT(sig_type, cp);
463 n = getnum_str(&startp, endp);
469 n = getnum_str(&startp, endp);
470 if (n <= 0 || n > 255)
475 if (!getword_str(buf2, sizeof buf2, &startp, endp))
477 exptime = ns_datetosecs(buf2, &dateerror);
479 ShrinkBuffer(INT32SZ);
487 ottl = strtoul(buf2, &ulendp, 10);
489 (ulendp != NULL && *ulendp != '\0'))
491 ShrinkBuffer(INT32SZ);
493 if (!getword_str(buf2, sizeof buf2, &startp,
496 exptime = ns_datetosecs(buf2, &dateerror);
501 ShrinkBuffer(INT32SZ);
502 PUTLONG(exptime, cp);
504 if (!getword_str(buf2, sizeof buf2, &startp, endp))
506 timesigned = ns_datetosecs(buf2, &dateerror);
508 ShrinkBuffer(INT32SZ);
509 PUTLONG(timesigned, cp);
514 n = getnum_str(&startp, endp);
517 ShrinkBuffer(INT16SZ);
520 if (!getword_str(buf2, sizeof buf2, &startp, endp))
522 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
528 if ((n = getword_str(buf2, sizeof buf2,
531 siglen = b64_pton(buf2, buf3, sizeof(buf3));
534 ShrinkBuffer(siglen);
535 memcpy(cp, buf3, siglen);
541 n = gethexnum_str(&startp, endp);
544 ShrinkBuffer(INT16SZ);
547 n = getnum_str(&startp, endp);
553 n = getnum_str(&startp, endp);
559 if ((n = getword_str(buf2, sizeof buf2,
562 keylen = b64_pton(buf2, buf3, sizeof(buf3));
565 ShrinkBuffer(keylen);
566 memcpy(cp, buf3, keylen);
571 int success, nxt_type;
576 if (!getword_str(buf2, sizeof buf2, &startp, endp))
578 n = dn_comp(buf2, cp, buflen, NULL, NULL);
584 memset(data, 0, sizeof data);
586 if (!getword_str(buf2, sizeof buf2, &startp,
589 nxt_type = sym_ston(__p_type_syms, buf2,
591 if (!success || !ns_t_rr_p(nxt_type))
593 NS_NXT_BIT_SET(nxt_type, data);
594 if (nxt_type > maxtype)
597 n = maxtype/NS_NXT_BITS+1;
605 n = getnum_str(&startp, endp);
608 ShrinkBuffer(INT16SZ);
611 n = getnum_str(&startp, endp);
614 ShrinkBuffer(INT16SZ);
617 n = getnum_str(&startp, endp);
623 if ((n = getword_str(buf2, sizeof buf2,
626 certlen = b64_pton(buf2, buf3, sizeof(buf3));
629 ShrinkBuffer(certlen);
630 memcpy(cp, buf3, certlen);
634 if (!getword_str(buf2, sizeof buf2, &startp, endp))
636 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
638 ShrinkBuffer(NS_IN6ADDRSZ);
639 memcpy(cp, &in6a, NS_IN6ADDRSZ);
643 /* Order Preference Flags Service Replacement Regexp */
645 n = getnum_str(&startp, endp);
646 if (n < 0 || n > 65535)
648 ShrinkBuffer(INT16SZ);
651 n = getnum_str(&startp, endp);
652 if (n < 0 || n > 65535)
654 ShrinkBuffer(INT16SZ);
657 if ((n = getstr_str(buf2, sizeof buf2,
658 &startp, endp)) < 0) {
667 /* Service Classes */
668 if ((n = getstr_str(buf2, sizeof buf2,
669 &startp, endp)) < 0) {
679 if ((n = getstr_str(buf2, sizeof buf2,
680 &startp, endp)) < 0) {
690 if (!getword_str(buf2, sizeof buf2, &startp, endp))
692 n = dn_comp(buf2, cp, buflen, NULL, NULL);
701 n = (u_int16_t)((cp - sp2) - INT16SZ);
705 hp->qdcount = htons(counts[0]);
706 hp->ancount = htons(counts[1]);
707 hp->nscount = htons(counts[2]);
708 hp->arcount = htons(counts[3]);
713 * Get a whitespace delimited word from a string (not file)
714 * into buf. modify the start pointer to point after the
715 * word in the string.
718 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
722 for (cp = buf; *startpp <= endp; ) {
724 if (isspace(c) || c == '\0') {
725 if (cp != buf) /* trailing whitespace */
727 else { /* leading whitespace */
733 if (cp >= buf+size-1)
742 * get a white spae delimited string from memory. Process quoted strings
743 * and \DDD escapes. Return length or -1 on error. Returned string may
746 static char digits[] = "0123456789";
748 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
756 for (cp = buf; *startpp <= endp; ) {
757 if ((c = **startpp) == '\0')
759 /* leading white space */
760 if ((cp == buf) && !seen_quote && isspace(c)) {
798 (strchr(digits, c) - digits);
808 } else if (!inquote && isspace(c))
810 if (cp >= buf+size-1)
818 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
821 * Get a whitespace delimited base 16 number from a string (not file) into buf
822 * update the start pointer to point after the number in the string.
825 gethexnum_str(u_char **startpp, u_char *endp) {
830 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
831 return getnum_str(startpp, endp);
833 for (n = 0; *startpp <= endp; ) {
835 if (isspace(c) || c == '\0') {
836 if (seendigit) /* trailing whitespace */
838 else { /* leading whitespace */
844 while ((*startpp <= endp) &&
845 ((c = **startpp) != '\n'))
852 if (c == ')' && seendigit) {
860 n = n * 16 + (c - '0');
862 n = n * 16 + (tolower(c) - 'a' + 10);
869 * Get a whitespace delimited base 10 number from a string (not file) into buf
870 * update the start pointer to point after the number in the string.
873 getnum_str(u_char **startpp, u_char *endp) {
878 for (n = 0; *startpp <= endp; ) {
880 if (isspace(c) || c == '\0') {
881 if (seendigit) /* trailing whitespace */
883 else { /* leading whitespace */
889 while ((*startpp <= endp) &&
890 ((c = **startpp) != '\n'))
897 if (c == ')' && seendigit) {
904 n = n * 10 + (c - '0');
911 * Allocate a resource record buffer & save rr info.
914 res_mkupdrec(int section, const char *dname,
915 u_int class, u_int type, u_long ttl) {
916 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
918 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
923 INIT_LINK(rrecp, r_link);
924 INIT_LINK(rrecp, r_glink);
925 rrecp->r_class = class;
926 rrecp->r_type = type;
928 rrecp->r_section = section;
933 * Free a resource record buffer created by res_mkupdrec.
936 res_freeupdrec(ns_updrec *rrecp) {
937 /* Note: freeing r_dp is the caller's responsibility. */
938 if (rrecp->r_dname != NULL)
939 free(rrecp->r_dname);
944 struct valuelist * next;
945 struct valuelist * prev;
950 static struct valuelist *servicelist, *protolist;
953 res_buildservicelist() {
955 struct valuelist *slp;
962 while ((sp = getservent()) != NULL) {
963 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
966 slp->name = strdup(sp->s_name);
967 slp->proto = strdup(sp->s_proto);
968 if ((slp->name == NULL) || (slp->proto == NULL)) {
969 if (slp->name) free(slp->name);
970 if (slp->proto) free(slp->proto);
974 slp->port = ntohs((u_int16_t)sp->s_port); /* host byt order */
975 slp->next = servicelist;
978 servicelist->prev = slp;
985 res_destroyservicelist() {
986 struct valuelist *slp, *slp_next;
988 for (slp = servicelist; slp != NULL; slp = slp_next) {
989 slp_next = slp->next;
994 servicelist = (struct valuelist *)0;
998 res_buildprotolist(void) {
1000 struct valuelist *slp;
1007 while ((pp = getprotoent()) != NULL) {
1008 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1011 slp->name = strdup(pp->p_name);
1012 if (slp->name == NULL) {
1016 slp->port = pp->p_proto; /* host byte order */
1017 slp->next = protolist;
1020 protolist->prev = slp;
1027 res_destroyprotolist(void) {
1028 struct valuelist *plp, *plp_next;
1030 for (plp = protolist; plp != NULL; plp = plp_next) {
1031 plp_next = plp->next;
1035 protolist = (struct valuelist *)0;
1039 findservice(const char *s, struct valuelist **list) {
1040 struct valuelist *lp = *list;
1043 for (; lp != NULL; lp = lp->next)
1044 if (strcasecmp(lp->name, s) == 0) {
1046 lp->prev->next = lp->next;
1048 lp->next->prev = lp->prev;
1053 return (lp->port); /* host byte order */
1055 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1061 * Convert service name or (ascii) number to int.
1064 res_servicenumber(const char *p) {
1065 if (servicelist == (struct valuelist *)0)
1066 res_buildservicelist();
1067 return (findservice(p, &servicelist));
1071 * Convert protocol name or (ascii) number to int.
1074 res_protocolnumber(const char *p) {
1075 if (protolist == (struct valuelist *)0)
1076 res_buildprotolist();
1077 return (findservice(p, &protolist));
1080 static struct servent *
1081 cgetservbyport(u_int16_t port, const char *proto) { /* Host byte order. */
1082 struct valuelist **list = &servicelist;
1083 struct valuelist *lp = *list;
1084 static struct servent serv;
1087 for (; lp != NULL; lp = lp->next) {
1088 if (port != (u_int16_t)lp->port) /* Host byte order. */
1090 if (strcasecmp(lp->proto, proto) == 0) {
1092 lp->prev->next = lp->next;
1094 lp->next->prev = lp->prev;
1099 serv.s_name = lp->name;
1100 serv.s_port = htons((u_int16_t)lp->port);
1101 serv.s_proto = lp->proto;
1108 static struct protoent *
1109 cgetprotobynumber(int proto) { /* Host byte order. */
1110 struct valuelist **list = &protolist;
1111 struct valuelist *lp = *list;
1112 static struct protoent prot;
1114 for (; lp != NULL; lp = lp->next)
1115 if (lp->port == proto) { /* Host byte order. */
1117 lp->prev->next = lp->next;
1119 lp->next->prev = lp->prev;
1124 prot.p_name = lp->name;
1125 prot.p_proto = lp->port; /* Host byte order. */
1132 res_protocolname(int num) {
1133 static char number[8];
1134 struct protoent *pp;
1136 if (protolist == (struct valuelist *)0)
1137 res_buildprotolist();
1138 pp = cgetprotobynumber(num);
1140 (void) sprintf(number, "%d", num);
1143 return (pp->p_name);
1147 res_servicename(u_int16_t port, const char *proto) { /* Host byte order. */
1148 static char number[8];
1151 if (servicelist == (struct valuelist *)0)
1152 res_buildservicelist();
1153 ss = cgetservbyport(htons(port), proto);
1155 (void) sprintf(number, "%d", port);
1158 return (ss->s_name);