4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Based on the Dynamic DNS reference implementation by Viraj Bais
23 * <viraj_bais@ccm.fm.intel.com>
26 #if !defined(lint) && !defined(SABER)
27 static const char rcsid
[] = "Id: res_mkupdate.c,v 1.10 2008/12/11 09:59:00 marka Exp";
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>
50 #include "port_after.h"
52 /* Options. Leave them on. */
56 static int getnum_str(u_char
**, u_char
*);
57 static int gethexnum_str(u_char
**, u_char
*);
58 static int getword_str(char *, int, u_char
**, u_char
*);
59 static int getstr_str(char *, int, u_char
**, u_char
*);
61 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
65 int res_protocolnumber(const char *);
66 int res_servicenumber(const char *);
69 * Form update packets.
70 * Returns the size of the resulting packet if no error
74 *\li -1 if error in reading a word/number in rdata
75 * portion for update packets
76 *\li -2 if length of buffer passed is insufficient
77 *\li -3 if zone section is not the first section in
78 * the linked list, or section order has a problem
79 *\li -4 on a number overflow
80 *\li -5 unknown operation or no records
83 res_nmkupdate(res_state statp
, ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
84 ns_updrec
*rrecp_start
= rrecp_in
;
86 u_char
*cp
, *sp2
, *startp
, *endp
;
87 int n
, i
, soanum
, multiline
;
92 u_char buf3
[MAXDNAME
];
93 int section
, numrrs
= 0, counts
[ns_s_max
];
94 u_int16_t rtype
, rclass
;
96 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
97 int siglen
, keylen
, certlen
;
100 * Initialize header fields.
102 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
104 memset(buf
, 0, HFIXEDSZ
);
106 statp
->id
= res_nrandomid(statp
);
107 hp
->id
= htons(statp
->id
);
108 hp
->opcode
= ns_o_update
;
115 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
117 if (rrecp_start
== NULL
)
119 else if (rrecp_start
->r_section
!= S_ZONE
)
122 memset(counts
, 0, sizeof counts
);
123 for (rrecp
= rrecp_start
; rrecp
; rrecp
= NEXT(rrecp
, r_glink
)) {
125 section
= rrecp
->r_section
;
126 if (section
< 0 || section
>= ns_s_max
)
129 for (i
= section
+ 1; i
< ns_s_max
; i
++)
132 rtype
= rrecp
->r_type
;
133 rclass
= rrecp
->r_class
;
135 /* overload class and type */
136 if (section
== S_PREREQ
) {
138 switch (rrecp
->r_opcode
) {
154 if (rrecp
->r_size
== 0)
159 "res_mkupdate: incorrect opcode: %d\n",
164 } else if (section
== S_UPDATE
) {
165 switch (rrecp
->r_opcode
) {
167 rclass
= rrecp
->r_size
== 0 ? C_ANY
: C_NONE
;
173 "res_mkupdate: incorrect opcode: %d\n",
181 * XXX appending default domain to owner name is omitted,
182 * fqdn must be provided
184 if ((n
= dn_comp(rrecp
->r_dname
, cp
, buflen
, dnptrs
,
188 ShrinkBuffer(n
+ 2*INT16SZ
);
190 PUTSHORT(rclass
, cp
);
191 if (section
== S_ZONE
) {
192 if (numrrs
!= 1 || rrecp
->r_type
!= T_SOA
)
196 ShrinkBuffer(INT32SZ
+ INT16SZ
);
198 sp2
= cp
; /*%< save pointer to length byte */
200 if (rrecp
->r_size
== 0) {
201 if (section
== S_UPDATE
&& rclass
!= C_ANY
)
208 startp
= rrecp
->r_data
;
209 endp
= startp
+ rrecp
->r_size
- 1;
210 /* XXX this should be done centrally. */
211 switch (rrecp
->r_type
) {
213 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
215 if (!inet_aton(buf2
, &ina
))
217 n1
= ntohl(ina
.s_addr
);
218 ShrinkBuffer(INT32SZ
);
228 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
230 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
239 for (i
= 0; i
< 2; i
++) {
240 if (!getword_str(buf2
, sizeof buf2
, &startp
,
243 n
= dn_comp(buf2
, cp
, buflen
,
250 if (rrecp
->r_type
== T_SOA
) {
251 ShrinkBuffer(5 * INT32SZ
);
252 while (isspace(*startp
) || !*startp
)
254 if (*startp
== '(') {
259 /* serial, refresh, retry, expire, minimum */
260 for (i
= 0; i
< 5; i
++) {
261 soanum
= getnum_str(&startp
, endp
);
267 while (isspace(*startp
) || !*startp
)
277 n
= getnum_str(&startp
, endp
);
280 ShrinkBuffer(INT16SZ
);
282 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
284 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
291 n
= getnum_str(&startp
, endp
);
294 ShrinkBuffer(INT16SZ
);
297 n
= getnum_str(&startp
, endp
);
300 ShrinkBuffer(INT16SZ
);
303 n
= getnum_str(&startp
, endp
);
306 ShrinkBuffer(INT16SZ
);
309 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
311 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
318 n
= getnum_str(&startp
, endp
);
322 ShrinkBuffer(INT16SZ
);
323 for (i
= 0; i
< 2; i
++) {
324 if (!getword_str(buf2
, sizeof buf2
, &startp
,
327 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
,
337 unsigned int maxbm
= 0;
339 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
341 if (!inet_aton(buf2
, &ina
))
343 n1
= ntohl(ina
.s_addr
);
344 ShrinkBuffer(INT32SZ
);
347 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
349 if ((i
= res_protocolnumber(buf2
)) < 0)
354 for (i
= 0; i
< MAXPORT
/8 ; i
++)
357 while (getword_str(buf2
, sizeof buf2
, &startp
, endp
)) {
358 if ((n
= res_servicenumber(buf2
)) <= 0)
362 bm
[n
/8] |= (0x80>>(n
%8));
363 if ((unsigned)n
> maxbm
)
370 memcpy(cp
, bm
, maxbm
);
375 for (i
= 0; i
< 2; i
++) {
376 if ((n
= getstr_str(buf2
, sizeof buf2
,
389 if ((n
= getstr_str(buf2
, sizeof buf2
,
390 &startp
, endp
)) < 0) {
391 if (cp
!= (sp2
+ INT16SZ
))
405 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
417 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
420 if ((n
> 255) || (n
== 0))
426 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
437 if ((n
= inet_nsap_addr((char *)startp
, (u_char
*)buf2
, sizeof(buf2
))) != 0) {
446 if ((n
= loc_aton((char *)startp
, (u_char
*)buf2
)) != 0) {
455 int sig_type
, success
, dateerror
;
456 u_int32_t exptime
, timesigned
;
459 if ((n
= getword_str(buf2
, sizeof buf2
,
462 sig_type
= sym_ston(__p_type_syms
, buf2
, &success
);
463 if (!success
|| sig_type
== ns_t_any
)
465 ShrinkBuffer(INT16SZ
);
466 PUTSHORT(sig_type
, cp
);
468 n
= getnum_str(&startp
, endp
);
474 n
= getnum_str(&startp
, endp
);
475 if (n
<= 0 || n
> 255)
480 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
482 exptime
= ns_datetosecs(buf2
, &dateerror
);
484 ShrinkBuffer(INT32SZ
);
492 ottl
= strtoul(buf2
, &ulendp
, 10);
494 (ulendp
!= NULL
&& *ulendp
!= '\0'))
496 ShrinkBuffer(INT32SZ
);
498 if (!getword_str(buf2
, sizeof buf2
, &startp
,
501 exptime
= ns_datetosecs(buf2
, &dateerror
);
506 ShrinkBuffer(INT32SZ
);
507 PUTLONG(exptime
, cp
);
509 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
511 timesigned
= ns_datetosecs(buf2
, &dateerror
);
513 ShrinkBuffer(INT32SZ
);
514 PUTLONG(timesigned
, cp
);
519 n
= getnum_str(&startp
, endp
);
522 ShrinkBuffer(INT16SZ
);
525 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
527 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
533 if ((n
= getword_str(buf2
, sizeof buf2
,
536 siglen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
539 ShrinkBuffer(siglen
);
540 memcpy(cp
, buf3
, siglen
);
546 n
= gethexnum_str(&startp
, endp
);
549 ShrinkBuffer(INT16SZ
);
552 n
= getnum_str(&startp
, endp
);
558 n
= getnum_str(&startp
, endp
);
564 if ((n
= getword_str(buf2
, sizeof buf2
,
567 keylen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
570 ShrinkBuffer(keylen
);
571 memcpy(cp
, buf3
, keylen
);
576 int success
, nxt_type
;
581 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
583 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
589 memset(data
, 0, sizeof data
);
591 if (!getword_str(buf2
, sizeof buf2
, &startp
,
594 nxt_type
= sym_ston(__p_type_syms
, buf2
,
596 if (!success
|| !ns_t_rr_p(nxt_type
))
598 NS_NXT_BIT_SET(nxt_type
, data
);
599 if (nxt_type
> maxtype
)
602 n
= maxtype
/NS_NXT_BITS
+1;
610 n
= getnum_str(&startp
, endp
);
613 ShrinkBuffer(INT16SZ
);
616 n
= getnum_str(&startp
, endp
);
619 ShrinkBuffer(INT16SZ
);
622 n
= getnum_str(&startp
, endp
);
628 if ((n
= getword_str(buf2
, sizeof buf2
,
631 certlen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
634 ShrinkBuffer(certlen
);
635 memcpy(cp
, buf3
, certlen
);
639 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
641 if (inet_pton(AF_INET6
, buf2
, &in6a
) <= 0)
643 ShrinkBuffer(NS_IN6ADDRSZ
);
644 memcpy(cp
, &in6a
, NS_IN6ADDRSZ
);
648 /* Order Preference Flags Service Replacement Regexp */
650 n
= getnum_str(&startp
, endp
);
651 if (n
< 0 || n
> 65535)
653 ShrinkBuffer(INT16SZ
);
656 n
= getnum_str(&startp
, endp
);
657 if (n
< 0 || n
> 65535)
659 ShrinkBuffer(INT16SZ
);
662 if ((n
= getstr_str(buf2
, sizeof buf2
,
663 &startp
, endp
)) < 0) {
672 /* Service Classes */
673 if ((n
= getstr_str(buf2
, sizeof buf2
,
674 &startp
, endp
)) < 0) {
684 if ((n
= getstr_str(buf2
, sizeof buf2
,
685 &startp
, endp
)) < 0) {
695 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
697 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
706 n
= (u_int16_t
)((cp
- sp2
) - INT16SZ
);
710 hp
->qdcount
= htons(counts
[0]);
711 hp
->ancount
= htons(counts
[1]);
712 hp
->nscount
= htons(counts
[2]);
713 hp
->arcount
= htons(counts
[3]);
718 * Get a whitespace delimited word from a string (not file)
719 * into buf. modify the start pointer to point after the
720 * word in the string.
723 getword_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
727 for (cp
= buf
; *startpp
<= endp
; ) {
729 if (isspace(c
) || c
== '\0') {
730 if (cp
!= buf
) /*%< trailing whitespace */
732 else { /*%< leading whitespace */
738 if (cp
>= buf
+size
-1)
747 * get a white spae delimited string from memory. Process quoted strings
748 * and \\DDD escapes. Return length or -1 on error. Returned string may
751 static char digits
[] = "0123456789";
753 getstr_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
761 for (cp
= buf
; *startpp
<= endp
; ) {
762 if ((c
= **startpp
) == '\0')
764 /* leading white space */
765 if ((cp
== buf
) && !seen_quote
&& isspace(c
)) {
803 (strchr(digits
, c
) - digits
);
813 } else if (!inquote
&& isspace(c
))
815 if (cp
>= buf
+size
-1)
823 return ((cp
== buf
)? (seen_quote
? 0: -1): (cp
- buf
));
827 * Get a whitespace delimited base 16 number from a string (not file) into buf
828 * update the start pointer to point after the number in the string.
831 gethexnum_str(u_char
**startpp
, u_char
*endp
) {
836 if (*startpp
+ 2 >= endp
|| strncasecmp((char *)*startpp
, "0x", 2) != 0)
837 return getnum_str(startpp
, endp
);
839 for (n
= 0; *startpp
<= endp
; ) {
841 if (isspace(c
) || c
== '\0') {
842 if (seendigit
) /*%< trailing whitespace */
844 else { /*%< leading whitespace */
850 while ((*startpp
<= endp
) &&
851 ((c
= **startpp
) != '\n'))
858 if (c
== ')' && seendigit
) {
866 n
= n
* 16 + (c
- '0');
868 n
= n
* 16 + (tolower(c
) - 'a' + 10);
875 * Get a whitespace delimited base 10 number from a string (not file) into buf
876 * update the start pointer to point after the number in the string.
879 getnum_str(u_char
**startpp
, u_char
*endp
) {
884 for (n
= 0; *startpp
<= endp
; ) {
886 if (isspace(c
) || c
== '\0') {
887 if (seendigit
) /*%< trailing whitespace */
889 else { /*%< leading whitespace */
895 while ((*startpp
<= endp
) &&
896 ((c
= **startpp
) != '\n'))
903 if (c
== ')' && seendigit
) {
910 n
= n
* 10 + (c
- '0');
917 * Allocate a resource record buffer & save rr info.
920 res_mkupdrec(int section
, const char *dname
,
921 u_int
class, u_int type
, u_long ttl
) {
922 ns_updrec
*rrecp
= (ns_updrec
*)calloc(1, sizeof(ns_updrec
));
924 if (!rrecp
|| !(rrecp
->r_dname
= strdup(dname
))) {
929 INIT_LINK(rrecp
, r_link
);
930 INIT_LINK(rrecp
, r_glink
);
931 rrecp
->r_class
= (ns_class
)class;
932 rrecp
->r_type
= (ns_type
)type
;
934 rrecp
->r_section
= (ns_sect
)section
;
939 * Free a resource record buffer created by res_mkupdrec.
942 res_freeupdrec(ns_updrec
*rrecp
) {
943 /* Note: freeing r_dp is the caller's responsibility. */
944 if (rrecp
->r_dname
!= NULL
)
945 free(rrecp
->r_dname
);
950 struct valuelist
* next
;
951 struct valuelist
* prev
;
956 static struct valuelist
*servicelist
, *protolist
;
959 res_buildservicelist() {
961 struct valuelist
*slp
;
968 while ((sp
= getservent()) != NULL
) {
969 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
972 slp
->name
= strdup(sp
->s_name
);
973 slp
->proto
= strdup(sp
->s_proto
);
974 if ((slp
->name
== NULL
) || (slp
->proto
== NULL
)) {
975 if (slp
->name
) free(slp
->name
);
976 if (slp
->proto
) free(slp
->proto
);
980 slp
->port
= ntohs((u_int16_t
)sp
->s_port
); /*%< host byt order */
981 slp
->next
= servicelist
;
984 servicelist
->prev
= slp
;
991 res_destroyservicelist() {
992 struct valuelist
*slp
, *slp_next
;
994 for (slp
= servicelist
; slp
!= NULL
; slp
= slp_next
) {
995 slp_next
= slp
->next
;
1000 servicelist
= (struct valuelist
*)0;
1004 res_buildprotolist(void) {
1005 struct protoent
*pp
;
1006 struct valuelist
*slp
;
1013 while ((pp
= getprotoent()) != NULL
) {
1014 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
1017 slp
->name
= strdup(pp
->p_name
);
1018 if (slp
->name
== NULL
) {
1022 slp
->port
= pp
->p_proto
; /*%< host byte order */
1023 slp
->next
= protolist
;
1026 protolist
->prev
= slp
;
1033 res_destroyprotolist(void) {
1034 struct valuelist
*plp
, *plp_next
;
1036 for (plp
= protolist
; plp
!= NULL
; plp
= plp_next
) {
1037 plp_next
= plp
->next
;
1041 protolist
= (struct valuelist
*)0;
1045 findservice(const char *s
, struct valuelist
**list
) {
1046 struct valuelist
*lp
= *list
;
1049 for (; lp
!= NULL
; lp
= lp
->next
)
1050 if (strcasecmp(lp
->name
, s
) == 0) {
1052 lp
->prev
->next
= lp
->next
;
1054 lp
->next
->prev
= lp
->prev
;
1059 return (lp
->port
); /*%< host byte order */
1061 if (sscanf(s
, "%d", &n
) != 1 || n
<= 0)
1067 * Convert service name or (ascii) number to int.
1070 res_servicenumber(const char *p
) {
1071 if (servicelist
== (struct valuelist
*)0)
1072 res_buildservicelist();
1073 return (findservice(p
, &servicelist
));
1077 * Convert protocol name or (ascii) number to int.
1080 res_protocolnumber(const char *p
) {
1081 if (protolist
== (struct valuelist
*)0)
1082 res_buildprotolist();
1083 return (findservice(p
, &protolist
));
1086 static struct servent
*
1087 cgetservbyport(u_int16_t port
, const char *proto
) { /*%< Host byte order. */
1088 struct valuelist
**list
= &servicelist
;
1089 struct valuelist
*lp
= *list
;
1090 static struct servent serv
;
1093 for (; lp
!= NULL
; lp
= lp
->next
) {
1094 if (port
!= (u_int16_t
)lp
->port
) /*%< Host byte order. */
1096 if (strcasecmp(lp
->proto
, proto
) == 0) {
1098 lp
->prev
->next
= lp
->next
;
1100 lp
->next
->prev
= lp
->prev
;
1105 serv
.s_name
= lp
->name
;
1106 serv
.s_port
= htons((u_int16_t
)lp
->port
);
1107 serv
.s_proto
= lp
->proto
;
1114 static struct protoent
*
1115 cgetprotobynumber(int proto
) { /*%< Host byte order. */
1116 struct valuelist
**list
= &protolist
;
1117 struct valuelist
*lp
= *list
;
1118 static struct protoent prot
;
1120 for (; lp
!= NULL
; lp
= lp
->next
)
1121 if (lp
->port
== proto
) { /*%< Host byte order. */
1123 lp
->prev
->next
= lp
->next
;
1125 lp
->next
->prev
= lp
->prev
;
1130 prot
.p_name
= lp
->name
;
1131 prot
.p_proto
= lp
->port
; /*%< Host byte order. */
1138 res_protocolname(int num
) {
1139 static char number
[8];
1140 struct protoent
*pp
;
1142 if (protolist
== (struct valuelist
*)0)
1143 res_buildprotolist();
1144 pp
= cgetprotobynumber(num
);
1146 (void) sprintf(number
, "%d", num
);
1149 return (pp
->p_name
);
1153 res_servicename(u_int16_t port
, const char *proto
) { /*%< Host byte order. */
1154 static char number
[8];
1157 if (servicelist
== (struct valuelist
*)0)
1158 res_buildservicelist();
1159 ss
= cgetservbyport(htons(port
), proto
);
1161 (void) sprintf(number
, "%d", port
);
1164 return (ss
->s_name
);