2 * Copyright (c) 1996-1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
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.7 2001/01/11 02:16:24 mellon Exp $";
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
43 #include "minires/minires.h"
44 #include "arpa/nameser.h"
46 /* Options. Leave them on. */
50 static int getnum_str(const u_char **, const u_char *);
51 static int gethexnum_str(const u_char **, const u_char *);
52 static int getword_str(char *, int,
53 const unsigned char **,
54 const unsigned char *);
55 static int getstr_str(char *, int, const u_char **, const u_char *);
58 struct valuelist * next;
59 struct valuelist * prev;
65 static int findservice(const char *, struct valuelist **);
66 static struct servent *cgetservbyport(unsigned, const char *);
68 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
72 int res_protocolnumber(const char *);
73 int res_servicenumber(const char *);
74 static struct protoent *cgetprotobynumber(int);
77 * Form update packets.
78 * Returns the size of the resulting packet if no error
80 * returns -1 if error in reading a word/number in rdata
81 * portion for update packets
82 * -2 if length of buffer passed is insufficient
83 * -3 if zone section is not the first section in
84 * the linked list, or section order has a problem
85 * -4 on a number overflow
86 * -5 unknown operation or no records
89 res_nmkupdate(res_state statp,
90 ns_updrec *rrecp_in, double *bp, unsigned *blp) {
91 ns_updrec *rrecp_start = rrecp_in;
93 u_char *cp, *sp1, *sp2;
94 const unsigned char *startp, *endp;
95 int n, i, soanum, multiline;
99 u_char buf3[MAXDNAME];
100 int section, numrrs = 0, counts[ns_s_max];
101 u_int16_t rtype, rclass;
103 u_char *dnptrs[20], **dpp, **lastdnptr;
104 unsigned siglen, keylen, certlen;
105 unsigned buflen = *blp;
106 u_char *buf = (unsigned char *)bp;
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;
118 sp1 = buf + 2*INT16SZ; /* save pointer to zocount */
124 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
126 if (rrecp_start == NULL)
128 else if (rrecp_start->r_section != S_ZONE)
131 memset(counts, 0, sizeof counts);
132 for (rrecp = rrecp_start; rrecp; rrecp = ISC_LIST_NEXT(rrecp,
135 section = rrecp->r_section;
136 if (section < 0 || section >= ns_s_max)
139 for (i = section + 1; i < ns_s_max; i++)
142 rtype = rrecp->r_type;
143 rclass = rrecp->r_class;
145 /* overload class and type */
146 if (section == S_PREREQ) {
148 switch (rrecp->r_opcode) {
164 if (rrecp->r_size == 0)
169 "res_mkupdate: incorrect opcode: %d\n",
174 } else if (section == S_UPDATE) {
175 switch (rrecp->r_opcode) {
177 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
183 "res_mkupdate: incorrect opcode: %d\n",
191 * XXX appending default domain to owner name is omitted,
192 * fqdn must be provided
194 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
198 ShrinkBuffer(n + 2*INT16SZ);
200 PUTSHORT(rclass, cp);
201 if (section == S_ZONE) {
202 if (numrrs != 1 || rrecp->r_type != T_SOA)
206 ShrinkBuffer(INT32SZ + INT16SZ);
208 sp2 = cp; /* save pointer to length byte */
210 if (rrecp->r_size == 0) {
211 if (section == S_UPDATE && rclass != C_ANY)
218 startp = rrecp->r_data;
219 endp = startp + rrecp->r_size - 1;
220 /* XXX this should be done centrally. */
221 switch (rrecp->r_type) {
223 if (!getword_str(buf2, sizeof buf2, &startp, endp))
225 if (!inet_aton(buf2, &ina))
227 n1 = ntohl(ina.s_addr);
228 ShrinkBuffer(INT32SZ);
237 if (!getword_str(buf2, sizeof buf2, &startp, endp))
239 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
248 for (i = 0; i < 2; i++) {
249 if (!getword_str(buf2, sizeof buf2, &startp,
252 n = dn_comp(buf2, cp, buflen,
259 if (rrecp->r_type == T_SOA) {
260 ShrinkBuffer(5 * INT32SZ);
261 while (isspace(*startp) || !*startp)
263 if (*startp == '(') {
268 /* serial, refresh, retry, expire, minimum */
269 for (i = 0; i < 5; i++) {
270 soanum = getnum_str(&startp, endp);
276 while (isspace(*startp) || !*startp)
286 n = getnum_str(&startp, endp);
289 ShrinkBuffer(INT16SZ);
291 if (!getword_str(buf2, sizeof buf2, &startp, endp))
293 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
300 n = getnum_str(&startp, endp);
303 ShrinkBuffer(INT16SZ);
306 n = getnum_str(&startp, endp);
309 ShrinkBuffer(INT16SZ);
312 n = getnum_str(&startp, endp);
315 ShrinkBuffer(INT16SZ);
318 if (!getword_str(buf2, sizeof buf2, &startp, endp))
320 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
327 n = getnum_str(&startp, endp);
331 ShrinkBuffer(INT16SZ);
332 for (i = 0; i < 2; i++) {
333 if (!getword_str(buf2, sizeof buf2, &startp,
336 n = dn_comp(buf2, cp, buflen, dnptrs,
348 if (!getword_str(buf2, sizeof buf2, &startp, endp))
350 if (!inet_aton(buf2, &ina))
352 n1 = ntohl(ina.s_addr);
353 ShrinkBuffer(INT32SZ);
356 if (!getword_str(buf2, sizeof buf2, &startp, endp))
358 if ((i = res_protocolnumber(buf2)) < 0)
363 for (i = 0; i < MAXPORT/8 ; i++)
366 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
367 if ((n1 = res_servicenumber(buf2)) <= 0)
371 bm[n1/8] |= (0x80>>(n1%8));
379 memcpy(cp, bm, maxbm);
384 for (i = 0; i < 2; i++) {
385 if ((n = getstr_str(buf2, sizeof buf2,
392 memcpy(cp, buf2, (unsigned)n);
398 if ((n = getstr_str(buf2, sizeof buf2,
399 &startp, endp)) < 0) {
400 if (cp != (sp2 + INT16SZ))
408 memcpy(cp, buf2, (unsigned)n);
414 if ((n = getstr_str(buf2, sizeof buf2, &startp,
421 memcpy(cp, buf2, (unsigned)n);
426 if ((n = getstr_str(buf2, sizeof buf2, &startp,
429 if ((n > 255) || (n == 0))
433 memcpy(cp, buf2, (unsigned)n);
435 if ((n = getstr_str(buf2, sizeof buf2, &startp,
442 memcpy(cp, buf2, (unsigned)n);
447 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
456 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
465 int sig_type, success, dateerror;
466 u_int32_t exptime, timesigned;
469 if ((n = getword_str(buf2, sizeof buf2,
472 sig_type = sym_ston(__p_type_syms, buf2, &success);
473 if (!success || sig_type == ns_t_any)
475 ShrinkBuffer(INT16SZ);
476 PUTSHORT(sig_type, cp);
478 n = getnum_str(&startp, endp);
484 n = getnum_str(&startp, endp);
485 if (n <= 0 || n > 255)
490 if (!getword_str(buf2, sizeof buf2, &startp, endp))
492 exptime = ns_datetosecs(buf2, &dateerror);
494 ShrinkBuffer(INT32SZ);
501 ottl = strtoul(buf2, &ulendp, 10);
502 if (ulendp != NULL && *ulendp != '\0')
504 ShrinkBuffer(INT32SZ);
506 if (!getword_str(buf2, sizeof buf2, &startp,
509 exptime = ns_datetosecs(buf2, &dateerror);
514 ShrinkBuffer(INT32SZ);
515 PUTLONG(exptime, cp);
517 if (!getword_str(buf2, sizeof buf2, &startp, endp))
519 timesigned = ns_datetosecs(buf2, &dateerror);
521 ShrinkBuffer(INT32SZ);
522 PUTLONG(timesigned, cp);
527 n = getnum_str(&startp, endp);
530 ShrinkBuffer(INT16SZ);
533 if (!getword_str(buf2, sizeof buf2, &startp, endp))
535 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
541 if ((n = getword_str(buf2, sizeof buf2,
544 siglen = b64_pton(buf2, buf3, sizeof(buf3));
547 ShrinkBuffer(siglen);
548 memcpy(cp, buf3, siglen);
554 n = gethexnum_str(&startp, endp);
557 ShrinkBuffer(INT16SZ);
560 n = getnum_str(&startp, endp);
566 n = getnum_str(&startp, endp);
572 if ((n = getword_str(buf2, sizeof buf2,
575 keylen = b64_pton(buf2, buf3, sizeof(buf3));
578 ShrinkBuffer(keylen);
579 memcpy(cp, buf3, keylen);
584 int success, nxt_type;
589 if (!getword_str(buf2, sizeof buf2, &startp, endp))
591 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
597 memset(data, 0, sizeof data);
599 if (!getword_str(buf2, sizeof buf2, &startp,
602 nxt_type = sym_ston(__p_type_syms, buf2,
604 if (!success || !ns_t_rr_p(nxt_type))
606 NS_NXT_BIT_SET(nxt_type, data);
607 if (nxt_type > maxtype)
610 n = maxtype/NS_NXT_BITS+1;
618 n = getnum_str(&startp, endp);
621 ShrinkBuffer(INT16SZ);
624 n = getnum_str(&startp, endp);
627 ShrinkBuffer(INT16SZ);
630 n = getnum_str(&startp, endp);
636 if ((n = getword_str(buf2, sizeof buf2,
639 certlen = b64_pton(buf2, buf3, sizeof(buf3));
642 ShrinkBuffer(certlen);
643 memcpy(cp, buf3, certlen);
650 n = (u_int16_t)((cp - sp2) - INT16SZ);
654 hp->qdcount = htons(counts[0]);
655 hp->ancount = htons(counts[1]);
656 hp->nscount = htons(counts[2]);
657 hp->arcount = htons(counts[3]);
663 * Get a whitespace delimited word from a string (not file)
664 * into buf. modify the start pointer to point after the
665 * word in the string.
668 getword_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
672 for (cp = buf; *startpp <= endp; ) {
674 if (isspace(c) || c == '\0') {
675 if (cp != buf) /* trailing whitespace */
677 else { /* leading whitespace */
683 if (cp >= buf+size-1)
692 * get a white spae delimited string from memory. Process quoted strings
693 * and \DDD escapes. Return length or -1 on error. Returned string may
696 static char digits[] = "0123456789";
698 getstr_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
706 for (cp = buf; *startpp <= endp; ) {
707 if ((c = **startpp) == '\0')
709 /* leading white space */
710 if ((cp == buf) && !seen_quote && isspace(c)) {
748 (strchr(digits, c) - digits);
758 } else if (!inquote && isspace(c))
760 if (cp >= buf+size-1)
768 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
771 * Get a whitespace delimited base 16 number from a string (not file) into buf
772 * update the start pointer to point after the number in the string.
775 gethexnum_str(const u_char **startpp, const u_char *endp) {
780 if (*startpp + 2 >= endp ||
781 strncasecmp((const char *)*startpp, "0x", 2) != 0)
782 return getnum_str(startpp, endp);
784 for (n = 0; *startpp <= endp; ) {
786 if (isspace(c) || c == '\0') {
787 if (seendigit) /* trailing whitespace */
789 else { /* leading whitespace */
795 while ((*startpp <= endp) &&
796 ((c = **startpp) != '\n'))
803 if (c == ')' && seendigit) {
811 n = n * 16 + (c - '0');
813 n = n * 16 + (tolower(c) - 'a' + 10);
820 * Get a whitespace delimited base 16 number from a string (not file) into buf
821 * update the start pointer to point after the number in the string.
824 getnum_str(const u_char **startpp, const u_char *endp) {
829 for (n = 0; *startpp <= endp; ) {
831 if (isspace(c) || c == '\0') {
832 if (seendigit) /* trailing whitespace */
834 else { /* leading whitespace */
840 while ((*startpp <= endp) &&
841 ((c = **startpp) != '\n'))
848 if (c == ')' && seendigit) {
855 n = n * 10 + (c - '0');
862 * Allocate a resource record buffer & save rr info.
865 res_mkupdrec(int section, const char *dname,
866 u_int class, u_int type, u_long ttl) {
867 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
869 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
874 rrecp->r_class = class;
875 rrecp->r_type = type;
877 rrecp->r_section = section;
882 * Free a resource record buffer created by res_mkupdrec.
885 res_freeupdrec(ns_updrec *rrecp) {
886 /* Note: freeing r_dp is the caller's responsibility. */
887 if (rrecp->r_dname != NULL)
888 free(rrecp->r_dname);
892 static struct valuelist *servicelist, *protolist;
895 res_buildservicelist() {
897 struct valuelist *slp;
904 while ((sp = getservent()) != NULL) {
905 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
908 slp->name = strdup(sp->s_name);
909 slp->proto = strdup(sp->s_proto);
910 if ((slp->name == NULL) || (slp->proto == NULL)) {
911 if (slp->name) free(slp->name);
912 if (slp->proto) free(slp->proto);
916 slp->port = ntohs((u_int16_t)sp->s_port); /* host byt order */
917 slp->next = servicelist;
920 servicelist->prev = slp;
927 res_destroyservicelist() {
928 struct valuelist *slp, *slp_next;
930 for (slp = servicelist; slp != NULL; slp = slp_next) {
931 slp_next = slp->next;
936 servicelist = (struct valuelist *)0;
940 res_buildprotolist() {
942 struct valuelist *slp;
949 while ((pp = getprotoent()) != NULL) {
950 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
953 slp->name = strdup(pp->p_name);
954 if (slp->name == NULL) {
958 slp->port = pp->p_proto; /* host byte order */
959 slp->next = protolist;
962 protolist->prev = slp;
969 res_destroyprotolist() {
970 struct valuelist *plp, *plp_next;
972 for (plp = protolist; plp != NULL; plp = plp_next) {
973 plp_next = plp->next;
977 protolist = (struct valuelist *)0;
981 findservice(const char *s, struct valuelist **list) {
982 struct valuelist *lp = *list;
985 for (; lp != NULL; lp = lp->next)
986 if (strcasecmp(lp->name, s) == 0) {
988 lp->prev->next = lp->next;
990 lp->next->prev = lp->prev;
995 return (lp->port); /* host byte order */
997 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1003 * Convert service name or (ascii) number to int.
1006 res_servicenumber(const char *p) {
1007 if (servicelist == (struct valuelist *)0)
1008 res_buildservicelist();
1009 return (findservice(p, &servicelist));
1013 * Convert protocol name or (ascii) number to int.
1016 res_protocolnumber(const char *p) {
1017 if (protolist == (struct valuelist *)0)
1018 res_buildprotolist();
1019 return (findservice(p, &protolist));
1022 static struct servent *
1023 cgetservbyport(unsigned port, const char *proto) { /* Host byte order. */
1024 struct valuelist **list = &servicelist;
1025 struct valuelist *lp = *list;
1026 static struct servent serv;
1029 for (; lp != NULL; lp = lp->next) {
1030 if (port != (u_int16_t)lp->port) /* Host byte order. */
1032 if (strcasecmp(lp->proto, proto) == 0) {
1034 lp->prev->next = lp->next;
1036 lp->next->prev = lp->prev;
1041 serv.s_name = lp->name;
1042 serv.s_port = htons((u_int16_t)lp->port);
1043 serv.s_proto = lp->proto;
1050 static struct protoent *
1051 cgetprotobynumber(int proto) { /* Host byte order. */
1052 struct valuelist **list = &protolist;
1053 struct valuelist *lp = *list;
1054 static struct protoent prot;
1056 for (; lp != NULL; lp = lp->next)
1057 if (lp->port == proto) { /* Host byte order. */
1059 lp->prev->next = lp->next;
1061 lp->next->prev = lp->prev;
1066 prot.p_name = lp->name;
1067 prot.p_proto = lp->port; /* Host byte order. */
1074 res_protocolname(int num) {
1075 static char number[8];
1076 struct protoent *pp;
1078 if (protolist == (struct valuelist *)0)
1079 res_buildprotolist();
1080 pp = cgetprotobynumber(num);
1082 (void) sprintf(number, "%d", num);
1085 return (pp->p_name);
1089 res_servicename(u_int16_t port, const char *proto) { /* Host byte order. */
1090 static char number[8];
1093 if (servicelist == (struct valuelist *)0)
1094 res_buildservicelist();
1095 ss = cgetservbyport(htons(port), proto);
1097 (void) sprintf(number, "%d", port);
1100 return (ss->s_name);