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
17 * $DragonFly: src/contrib/isc-dhcp/minires/Attic/res_mkupdate.c,v 1.2 2003/10/11 21:14:23 dillon Exp $
21 * Based on the Dynamic DNS reference implementation by Viraj Bais
22 * <viraj_bais@ccm.fm.intel.com>
25 #if !defined(lint) && !defined(SABER)
26 static const char rcsid[] = "$Id: res_mkupdate.c,v 1.7.2.2 2003/05/19 00:33:22 dhankins Exp $";
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
45 #include "minires/minires.h"
46 #include "arpa/nameser.h"
48 /* Options. Leave them on. */
52 static int getnum_str(const u_char **, const u_char *);
53 static int gethexnum_str(const u_char **, const u_char *);
54 static int getword_str(char *, int,
55 const unsigned char **,
56 const unsigned char *);
57 static int getphrase_str(char *, int, const u_char **, const u_char *);
58 static int getstr_str(char *, int, const u_char **, const u_char *);
61 struct valuelist * next;
62 struct valuelist * prev;
68 static int findservice(const char *, struct valuelist **);
69 static struct servent *cgetservbyport(unsigned, const char *);
71 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
75 int res_protocolnumber(const char *);
76 int res_servicenumber(const char *);
77 static struct protoent *cgetprotobynumber(int);
80 * Form update packets.
81 * Returns the size of the resulting packet if no error
83 * returns -1 if error in reading a word/number in rdata
84 * portion for update packets
85 * -2 if length of buffer passed is insufficient
86 * -3 if zone section is not the first section in
87 * the linked list, or section order has a problem
88 * -4 on a number overflow
89 * -5 unknown operation or no records
92 res_nmkupdate(res_state statp,
93 ns_updrec *rrecp_in, double *bp, unsigned *blp) {
94 ns_updrec *rrecp_start = rrecp_in;
96 u_char *cp, *sp1, *sp2;
97 const unsigned char *startp, *endp;
98 int n, i, soanum, multiline;
102 u_char buf3[MAXDNAME];
103 int section, numrrs = 0, counts[ns_s_max];
104 u_int16_t rtype, rclass;
106 u_char *dnptrs[20], **dpp, **lastdnptr;
107 unsigned siglen, certlen;
109 unsigned buflen = *blp;
110 u_char *buf = (unsigned char *)bp;
113 * Initialize header fields.
115 if ((buf == NULL) || (buflen < HFIXEDSZ))
117 memset(buf, 0, HFIXEDSZ);
119 hp->id = htons(++statp->id);
120 hp->opcode = ns_o_update;
122 sp1 = buf + 2*INT16SZ; /* save pointer to zocount */
128 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
130 if (rrecp_start == NULL)
132 else if (rrecp_start->r_section != S_ZONE)
135 memset(counts, 0, sizeof counts);
136 for (rrecp = rrecp_start; rrecp; rrecp = ISC_LIST_NEXT(rrecp,
139 section = rrecp->r_section;
140 if (section < 0 || section >= ns_s_max)
143 for (i = section + 1; i < ns_s_max; i++)
146 rtype = rrecp->r_type;
147 rclass = rrecp->r_class;
149 /* overload class and type */
150 if (section == S_PREREQ) {
152 switch (rrecp->r_opcode) {
168 if (rrecp->r_size == 0)
173 "res_mkupdate: incorrect opcode: %d\n",
178 } else if (section == S_UPDATE) {
179 switch (rrecp->r_opcode) {
181 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
187 "res_mkupdate: incorrect opcode: %d\n",
195 * XXX appending default domain to owner name is omitted,
196 * fqdn must be provided
198 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
202 ShrinkBuffer(n + 2*INT16SZ);
204 PUTSHORT(rclass, cp);
205 if (section == S_ZONE) {
206 if (numrrs != 1 || rrecp->r_type != T_SOA)
210 ShrinkBuffer(INT32SZ + INT16SZ);
212 sp2 = cp; /* save pointer to length byte */
214 if (rrecp->r_size == 0) {
215 if (section == S_UPDATE && rclass != C_ANY)
222 startp = rrecp->r_data;
223 endp = startp + rrecp->r_size - 1;
224 /* XXX this should be done centrally. */
225 switch (rrecp->r_type) {
227 if (!getword_str(buf2, sizeof buf2, &startp, endp))
229 if (!inet_aton(buf2, &ina))
231 n1 = ntohl(ina.s_addr);
232 ShrinkBuffer(INT32SZ);
241 if (!getphrase_str(buf2, sizeof buf2, &startp, endp))
243 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
252 for (i = 0; i < 2; i++) {
253 if (!getword_str(buf2, sizeof buf2, &startp,
256 n = dn_comp(buf2, cp, buflen,
263 if (rrecp->r_type == T_SOA) {
264 ShrinkBuffer(5 * INT32SZ);
265 while (isspace(*startp) || !*startp)
267 if (*startp == '(') {
272 /* serial, refresh, retry, expire, minimum */
273 for (i = 0; i < 5; i++) {
274 soanum = getnum_str(&startp, endp);
280 while (isspace(*startp) || !*startp)
290 n = getnum_str(&startp, endp);
293 ShrinkBuffer(INT16SZ);
295 if (!getword_str(buf2, sizeof buf2, &startp, endp))
297 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
304 n = getnum_str(&startp, endp);
307 ShrinkBuffer(INT16SZ);
310 n = getnum_str(&startp, endp);
313 ShrinkBuffer(INT16SZ);
316 n = getnum_str(&startp, endp);
319 ShrinkBuffer(INT16SZ);
322 if (!getword_str(buf2, sizeof buf2, &startp, endp))
324 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
331 n = getnum_str(&startp, endp);
335 ShrinkBuffer(INT16SZ);
336 for (i = 0; i < 2; i++) {
337 if (!getword_str(buf2, sizeof buf2, &startp,
340 n = dn_comp(buf2, cp, buflen, dnptrs,
352 if (!getword_str(buf2, sizeof buf2, &startp, endp))
354 if (!inet_aton(buf2, &ina))
356 n1 = ntohl(ina.s_addr);
357 ShrinkBuffer(INT32SZ);
360 if (!getword_str(buf2, sizeof buf2, &startp, endp))
362 if ((i = res_protocolnumber(buf2)) < 0)
367 for (i = 0; i < MAXPORT/8 ; i++)
370 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
371 if ((n1 = res_servicenumber(buf2)) <= 0)
375 bm[n1/8] |= (0x80>>(n1%8));
383 memcpy(cp, bm, maxbm);
388 for (i = 0; i < 2; i++) {
389 if ((n = getstr_str(buf2, sizeof buf2,
396 memcpy(cp, buf2, (unsigned)n);
402 if ((n = getstr_str(buf2, sizeof buf2,
403 &startp, endp)) < 0) {
404 if (cp != (sp2 + INT16SZ))
412 memcpy(cp, buf2, (unsigned)n);
418 if ((n = getstr_str(buf2, sizeof buf2, &startp,
425 memcpy(cp, buf2, (unsigned)n);
430 if ((n = getstr_str(buf2, sizeof buf2, &startp,
433 if ((n > 255) || (n == 0))
437 memcpy(cp, buf2, (unsigned)n);
439 if ((n = getstr_str(buf2, sizeof buf2, &startp,
446 memcpy(cp, buf2, (unsigned)n);
451 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
460 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
469 int sig_type, success, dateerror;
470 u_int32_t exptime, timesigned;
473 if ((n = getword_str(buf2, sizeof buf2,
476 sig_type = sym_ston(__p_type_syms, buf2, &success);
477 if (!success || sig_type == ns_t_any)
479 ShrinkBuffer(INT16SZ);
480 PUTSHORT(sig_type, cp);
482 n = getnum_str(&startp, endp);
488 n = getnum_str(&startp, endp);
489 if (n <= 0 || n > 255)
494 if (!getword_str(buf2, sizeof buf2, &startp, endp))
496 exptime = ns_datetosecs(buf2, &dateerror);
498 ShrinkBuffer(INT32SZ);
505 ottl = strtoul(buf2, &ulendp, 10);
506 if (ulendp != NULL && *ulendp != '\0')
508 ShrinkBuffer(INT32SZ);
510 if (!getword_str(buf2, sizeof buf2, &startp,
513 exptime = ns_datetosecs(buf2, &dateerror);
518 ShrinkBuffer(INT32SZ);
519 PUTLONG(exptime, cp);
521 if (!getword_str(buf2, sizeof buf2, &startp, endp))
523 timesigned = ns_datetosecs(buf2, &dateerror);
525 ShrinkBuffer(INT32SZ);
526 PUTLONG(timesigned, cp);
531 n = getnum_str(&startp, endp);
534 ShrinkBuffer(INT16SZ);
537 if (!getword_str(buf2, sizeof buf2, &startp, endp))
539 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
545 if ((n = getword_str(buf2, sizeof buf2,
548 siglen = b64_pton(buf2, buf3, sizeof(buf3));
551 ShrinkBuffer(siglen);
552 memcpy(cp, buf3, siglen);
558 int success, nxt_type;
563 if (!getword_str(buf2, sizeof buf2, &startp, endp))
565 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
571 memset(data, 0, sizeof data);
573 if (!getword_str(buf2, sizeof buf2, &startp,
576 nxt_type = sym_ston(__p_type_syms, buf2,
578 if (!success || !ns_t_rr_p(nxt_type))
580 NS_NXT_BIT_SET(nxt_type, data);
581 if (nxt_type > maxtype)
584 n = maxtype/NS_NXT_BITS+1;
594 n = gethexnum_str(&startp, endp);
597 ShrinkBuffer(INT16SZ);
600 n = getnum_str(&startp, endp);
606 n = getnum_str(&startp, endp);
612 if ((n = getword_str(buf2, sizeof buf2,
615 keylen = b64_pton(buf2, buf3, sizeof(buf3));
618 ShrinkBuffer(keylen);
619 memcpy(cp, buf3, keylen);
624 n = getnum_str(&startp, endp);
627 ShrinkBuffer(INT16SZ);
630 n = getnum_str(&startp, endp);
633 ShrinkBuffer(INT16SZ);
636 n = getnum_str(&startp, endp);
642 if ((n = getword_str(buf2, sizeof buf2,
645 certlen = b64_pton(buf2, buf3, sizeof(buf3));
648 ShrinkBuffer(certlen);
649 memcpy(cp, buf3, certlen);
654 fprintf(stderr, "NSupdate of RR type: %d not implemented\n",
658 n = (u_int16_t)((cp - sp2) - INT16SZ);
662 hp->qdcount = htons(counts[0]);
663 hp->ancount = htons(counts[1]);
664 hp->nscount = htons(counts[2]);
665 hp->arcount = htons(counts[3]);
671 * Get a whitespace delimited word from a string (not file)
672 * into buf. modify the start pointer to point after the
673 * word in the string.
676 getword_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
680 for (cp = buf; *startpp <= endp; ) {
682 if (isspace(c) || c == '\0') {
683 if (cp != buf) /* trailing whitespace */
685 else { /* leading whitespace */
691 if (cp >= buf+size-1)
700 * Get a phrase - possibly containing blanks - from a string (not file)
701 * into buf. modify the start pointer to point after the
702 * phrase in the string.
705 getphrase_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
709 for (cp = buf; *startpp <= endp; ) {
711 if (isspace(c) && cp == buf ) {
712 /* leading whitespace */
716 else if ( c == '\0' ) {
720 if (cp >= buf+size-1)
729 * get a white spae delimited string from memory. Process quoted strings
730 * and \DDD escapes. Return length or -1 on error. Returned string may
733 static char digits[] = "0123456789";
735 getstr_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
743 for (cp = buf; *startpp <= endp; ) {
744 if ((c = **startpp) == '\0')
746 /* leading white space */
747 if ((cp == buf) && !seen_quote && isspace(c)) {
785 (strchr(digits, c) - digits);
795 } else if (!inquote && isspace(c))
797 if (cp >= buf+size-1)
805 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
808 * Get a whitespace delimited base 16 number from a string (not file) into buf
809 * update the start pointer to point after the number in the string.
812 gethexnum_str(const u_char **startpp, const u_char *endp) {
817 if (*startpp + 2 >= endp ||
818 strncasecmp((const char *)*startpp, "0x", 2) != 0)
819 return getnum_str(startpp, endp);
821 for (n = 0; *startpp <= endp; ) {
823 if (isspace(c) || c == '\0') {
824 if (seendigit) /* trailing whitespace */
826 else { /* leading whitespace */
832 while ((*startpp <= endp) &&
833 ((c = **startpp) != '\n'))
840 if (c == ')' && seendigit) {
848 n = n * 16 + (c - '0');
850 n = n * 16 + (tolower(c) - 'a' + 10);
857 * Get a whitespace delimited base 16 number from a string (not file) into buf
858 * update the start pointer to point after the number in the string.
861 getnum_str(const u_char **startpp, const u_char *endp) {
866 for (n = 0; *startpp <= endp; ) {
868 if (isspace(c) || c == '\0') {
869 if (seendigit) /* trailing whitespace */
871 else { /* leading whitespace */
877 while ((*startpp <= endp) &&
878 ((c = **startpp) != '\n'))
885 if (c == ')' && seendigit) {
892 n = n * 10 + (c - '0');
899 * Allocate a resource record buffer & save rr info.
902 res_mkupdrec(int section, const char *dname,
903 u_int class, u_int type, u_long ttl) {
904 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
906 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
911 rrecp->r_class = class;
912 rrecp->r_type = type;
914 rrecp->r_section = section;
919 * Free a resource record buffer created by res_mkupdrec.
922 res_freeupdrec(ns_updrec *rrecp) {
923 /* Note: freeing r_dp is the caller's responsibility. */
924 if (rrecp->r_dname != NULL)
925 free(rrecp->r_dname);
929 static struct valuelist *servicelist, *protolist;
932 res_buildservicelist() {
934 struct valuelist *slp;
941 while ((sp = getservent()) != NULL) {
942 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
945 slp->name = strdup(sp->s_name);
946 slp->proto = strdup(sp->s_proto);
947 if ((slp->name == NULL) || (slp->proto == NULL)) {
948 if (slp->name) free(slp->name);
949 if (slp->proto) free(slp->proto);
953 slp->port = ntohs((u_int16_t)sp->s_port); /* host byt order */
954 slp->next = servicelist;
957 servicelist->prev = slp;
964 res_destroyservicelist() {
965 struct valuelist *slp, *slp_next;
967 for (slp = servicelist; slp != NULL; slp = slp_next) {
968 slp_next = slp->next;
973 servicelist = (struct valuelist *)0;
977 res_buildprotolist() {
979 struct valuelist *slp;
986 while ((pp = getprotoent()) != NULL) {
987 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
990 slp->name = strdup(pp->p_name);
991 if (slp->name == NULL) {
995 slp->port = pp->p_proto; /* host byte order */
996 slp->next = protolist;
999 protolist->prev = slp;
1006 res_destroyprotolist() {
1007 struct valuelist *plp, *plp_next;
1009 for (plp = protolist; plp != NULL; plp = plp_next) {
1010 plp_next = plp->next;
1014 protolist = (struct valuelist *)0;
1018 findservice(const char *s, struct valuelist **list) {
1019 struct valuelist *lp = *list;
1022 for (; lp != NULL; lp = lp->next)
1023 if (strcasecmp(lp->name, s) == 0) {
1025 lp->prev->next = lp->next;
1027 lp->next->prev = lp->prev;
1032 return (lp->port); /* host byte order */
1034 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1040 * Convert service name or (ascii) number to int.
1043 res_servicenumber(const char *p) {
1044 if (servicelist == (struct valuelist *)0)
1045 res_buildservicelist();
1046 return (findservice(p, &servicelist));
1050 * Convert protocol name or (ascii) number to int.
1053 res_protocolnumber(const char *p) {
1054 if (protolist == (struct valuelist *)0)
1055 res_buildprotolist();
1056 return (findservice(p, &protolist));
1059 static struct servent *
1060 cgetservbyport(unsigned port, const char *proto) { /* Host byte order. */
1061 struct valuelist **list = &servicelist;
1062 struct valuelist *lp = *list;
1063 static struct servent serv;
1066 for (; lp != NULL; lp = lp->next) {
1067 if (port != (u_int16_t)lp->port) /* Host byte order. */
1069 if (strcasecmp(lp->proto, proto) == 0) {
1071 lp->prev->next = lp->next;
1073 lp->next->prev = lp->prev;
1078 serv.s_name = lp->name;
1079 serv.s_port = htons((u_int16_t)lp->port);
1080 serv.s_proto = lp->proto;
1087 static struct protoent *
1088 cgetprotobynumber(int proto) { /* Host byte order. */
1089 struct valuelist **list = &protolist;
1090 struct valuelist *lp = *list;
1091 static struct protoent prot;
1093 for (; lp != NULL; lp = lp->next)
1094 if (lp->port == proto) { /* Host byte order. */
1096 lp->prev->next = lp->next;
1098 lp->next->prev = lp->prev;
1103 prot.p_name = lp->name;
1104 prot.p_proto = lp->port; /* Host byte order. */
1111 res_protocolname(int num) {
1112 static char number[8];
1113 struct protoent *pp;
1115 if (protolist == (struct valuelist *)0)
1116 res_buildprotolist();
1117 pp = cgetprotobynumber(num);
1119 (void) sprintf(number, "%d", num);
1122 return (pp->p_name);
1126 res_servicename(u_int16_t port, const char *proto) { /* Host byte order. */
1127 static char number[8];
1130 if (servicelist == (struct valuelist *)0)
1131 res_buildservicelist();
1132 ss = cgetservbyport(htons(port), proto);
1134 (void) sprintf(number, "%d", port);
1137 return (ss->s_name);