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.1.4.5 2005/10/14 05:43:47 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>
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);
67 int res_protocolnumber(const char *);
71 int res_servicenumber(const char *);
74 * Form update packets.
75 * Returns the size of the resulting packet if no error
77 * returns -1 if error in reading a word/number in rdata
78 * portion for update packets
79 * -2 if length of buffer passed is insufficient
80 * -3 if zone section is not the first section in
81 * the linked list, or section order has a problem
82 * -4 on a number overflow
83 * -5 unknown operation or no records
86 res_nmkupdate(res_state statp
, ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
87 ns_updrec
*rrecp_start
= rrecp_in
;
89 u_char
*cp
, *sp2
, *startp
, *endp
;
90 int n
, i
, soanum
, multiline
;
95 u_char buf3
[MAXDNAME
];
96 int section
, numrrs
= 0, counts
[ns_s_max
];
97 u_int16_t rtype
, rclass
;
99 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
106 * Initialize header fields.
108 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
110 memset(buf
, 0, HFIXEDSZ
);
112 hp
->id
= htons(++statp
->id
);
113 hp
->opcode
= ns_o_update
;
120 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
122 if (rrecp_start
== NULL
)
124 else if (rrecp_start
->r_section
!= S_ZONE
)
127 memset(counts
, 0, sizeof counts
);
128 for (rrecp
= rrecp_start
; rrecp
; rrecp
= NEXT(rrecp
, r_glink
)) {
130 section
= rrecp
->r_section
;
131 if (section
< 0 || section
>= ns_s_max
)
134 for (i
= section
+ 1; i
< ns_s_max
; i
++)
137 rtype
= rrecp
->r_type
;
138 rclass
= rrecp
->r_class
;
140 /* overload class and type */
141 if (section
== S_PREREQ
) {
143 switch (rrecp
->r_opcode
) {
159 if (rrecp
->r_size
== 0)
164 "res_mkupdate: incorrect opcode: %d\n",
169 } else if (section
== S_UPDATE
) {
170 switch (rrecp
->r_opcode
) {
172 rclass
= rrecp
->r_size
== 0 ? C_ANY
: C_NONE
;
178 "res_mkupdate: incorrect opcode: %d\n",
186 * XXX appending default domain to owner name is omitted,
187 * fqdn must be provided
189 if ((n
= dn_comp(rrecp
->r_dname
, cp
, buflen
, dnptrs
,
193 ShrinkBuffer(n
+ 2*INT16SZ
);
195 PUTSHORT(rclass
, cp
);
196 if (section
== S_ZONE
) {
197 if (numrrs
!= 1 || rrecp
->r_type
!= T_SOA
)
201 ShrinkBuffer(INT32SZ
+ INT16SZ
);
203 sp2
= cp
; /* save pointer to length byte */
205 if (rrecp
->r_size
== 0) {
206 if (section
== S_UPDATE
&& rclass
!= C_ANY
)
213 startp
= rrecp
->r_data
;
214 endp
= startp
+ rrecp
->r_size
- 1;
215 /* XXX this should be done centrally. */
216 switch (rrecp
->r_type
) {
218 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
220 if (!inet_aton(buf2
, &ina
))
222 n1
= ntohl(ina
.s_addr
);
223 ShrinkBuffer(INT32SZ
);
233 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
235 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
244 for (i
= 0; i
< 2; i
++) {
245 if (!getword_str(buf2
, sizeof buf2
, &startp
,
248 n
= dn_comp(buf2
, cp
, buflen
,
255 if (rrecp
->r_type
== T_SOA
) {
256 ShrinkBuffer(5 * INT32SZ
);
257 while (isspace(*startp
) || !*startp
)
259 if (*startp
== '(') {
264 /* serial, refresh, retry, expire, minimum */
265 for (i
= 0; i
< 5; i
++) {
266 soanum
= getnum_str(&startp
, endp
);
272 while (isspace(*startp
) || !*startp
)
282 n
= getnum_str(&startp
, endp
);
285 ShrinkBuffer(INT16SZ
);
287 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
289 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
296 n
= getnum_str(&startp
, endp
);
299 ShrinkBuffer(INT16SZ
);
302 n
= getnum_str(&startp
, endp
);
305 ShrinkBuffer(INT16SZ
);
308 n
= getnum_str(&startp
, endp
);
311 ShrinkBuffer(INT16SZ
);
314 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
316 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
323 n
= getnum_str(&startp
, endp
);
327 ShrinkBuffer(INT16SZ
);
328 for (i
= 0; i
< 2; i
++) {
329 if (!getword_str(buf2
, sizeof buf2
, &startp
,
332 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
,
342 unsigned int maxbm
= 0;
344 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
346 if (!inet_aton(buf2
, &ina
))
348 n1
= ntohl(ina
.s_addr
);
349 ShrinkBuffer(INT32SZ
);
352 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
354 if ((i
= res_protocolnumber(buf2
)) < 0)
359 for (i
= 0; i
< MAXPORT
/8 ; i
++)
362 while (getword_str(buf2
, sizeof buf2
, &startp
, endp
)) {
363 if ((n
= res_servicenumber(buf2
)) <= 0)
367 bm
[n
/8] |= (0x80>>(n
%8));
368 if ((unsigned)n
> maxbm
)
375 memcpy(cp
, bm
, maxbm
);
380 for (i
= 0; i
< 2; i
++) {
381 if ((n
= getstr_str(buf2
, sizeof buf2
,
394 if ((n
= getstr_str(buf2
, sizeof buf2
,
395 &startp
, endp
)) < 0) {
396 if (cp
!= (sp2
+ INT16SZ
))
410 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
422 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
425 if ((n
> 255) || (n
== 0))
431 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
442 if ((n
= inet_nsap_addr((char *)startp
, (u_char
*)buf2
, sizeof(buf2
))) != 0) {
451 if ((n
= loc_aton((char *)startp
, (u_char
*)buf2
)) != 0) {
463 int sig_type
, success
, dateerror
;
464 u_int32_t exptime
, timesigned
;
467 if ((n
= getword_str(buf2
, sizeof buf2
,
470 sig_type
= sym_ston(__p_type_syms
, buf2
, &success
);
471 if (!success
|| sig_type
== ns_t_any
)
473 ShrinkBuffer(INT16SZ
);
474 PUTSHORT(sig_type
, cp
);
476 n
= getnum_str(&startp
, endp
);
482 n
= getnum_str(&startp
, endp
);
483 if (n
<= 0 || n
> 255)
488 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
490 exptime
= ns_datetosecs(buf2
, &dateerror
);
492 ShrinkBuffer(INT32SZ
);
500 ottl
= strtoul(buf2
, &ulendp
, 10);
502 (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
);
555 n
= gethexnum_str(&startp
, endp
);
558 ShrinkBuffer(INT16SZ
);
561 n
= getnum_str(&startp
, endp
);
567 n
= getnum_str(&startp
, endp
);
573 if ((n
= getword_str(buf2
, sizeof buf2
,
576 keylen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
579 ShrinkBuffer(keylen
);
580 memcpy(cp
, buf3
, keylen
);
585 int success
, nxt_type
;
590 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
592 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
598 memset(data
, 0, sizeof data
);
600 if (!getword_str(buf2
, sizeof buf2
, &startp
,
603 nxt_type
= sym_ston(__p_type_syms
, buf2
,
605 if (!success
|| !ns_t_rr_p(nxt_type
))
607 NS_NXT_BIT_SET(nxt_type
, data
);
608 if (nxt_type
> maxtype
)
611 n
= maxtype
/NS_NXT_BITS
+1;
619 n
= getnum_str(&startp
, endp
);
622 ShrinkBuffer(INT16SZ
);
625 n
= getnum_str(&startp
, endp
);
628 ShrinkBuffer(INT16SZ
);
631 n
= getnum_str(&startp
, endp
);
637 if ((n
= getword_str(buf2
, sizeof buf2
,
640 certlen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
643 ShrinkBuffer(certlen
);
644 memcpy(cp
, buf3
, certlen
);
648 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
650 if (inet_pton(AF_INET6
, buf2
, &in6a
) <= 0)
652 ShrinkBuffer(NS_IN6ADDRSZ
);
653 memcpy(cp
, &in6a
, NS_IN6ADDRSZ
);
657 /* Order Preference Flags Service Replacement Regexp */
659 n
= getnum_str(&startp
, endp
);
660 if (n
< 0 || n
> 65535)
662 ShrinkBuffer(INT16SZ
);
665 n
= getnum_str(&startp
, endp
);
666 if (n
< 0 || n
> 65535)
668 ShrinkBuffer(INT16SZ
);
671 if ((n
= getstr_str(buf2
, sizeof buf2
,
672 &startp
, endp
)) < 0) {
681 /* Service Classes */
682 if ((n
= getstr_str(buf2
, sizeof buf2
,
683 &startp
, endp
)) < 0) {
693 if ((n
= getstr_str(buf2
, sizeof buf2
,
694 &startp
, endp
)) < 0) {
704 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
706 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
715 n
= (u_int16_t
)((cp
- sp2
) - INT16SZ
);
719 hp
->qdcount
= htons(counts
[0]);
720 hp
->ancount
= htons(counts
[1]);
721 hp
->nscount
= htons(counts
[2]);
722 hp
->arcount
= htons(counts
[3]);
727 * Get a whitespace delimited word from a string (not file)
728 * into buf. modify the start pointer to point after the
729 * word in the string.
732 getword_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
736 for (cp
= buf
; *startpp
<= endp
; ) {
738 if (isspace(c
) || c
== '\0') {
739 if (cp
!= buf
) /* trailing whitespace */
741 else { /* leading whitespace */
747 if (cp
>= buf
+size
-1)
756 * get a white spae delimited string from memory. Process quoted strings
757 * and \DDD escapes. Return length or -1 on error. Returned string may
760 static char digits
[] = "0123456789";
762 getstr_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
770 for (cp
= buf
; *startpp
<= endp
; ) {
771 if ((c
= **startpp
) == '\0')
773 /* leading white space */
774 if ((cp
== buf
) && !seen_quote
&& isspace(c
)) {
812 (strchr(digits
, c
) - digits
);
822 } else if (!inquote
&& isspace(c
))
824 if (cp
>= buf
+size
-1)
832 return ((cp
== buf
)? (seen_quote
? 0: -1): (cp
- buf
));
835 * Get a whitespace delimited base 16 number from a string (not file) into buf
836 * update the start pointer to point after the number in the string.
839 gethexnum_str(u_char
**startpp
, u_char
*endp
) {
844 if (*startpp
+ 2 >= endp
|| strncasecmp((char *)*startpp
, "0x", 2) != 0)
845 return getnum_str(startpp
, endp
);
847 for (n
= 0; *startpp
<= endp
; ) {
849 if (isspace(c
) || c
== '\0') {
850 if (seendigit
) /* trailing whitespace */
852 else { /* leading whitespace */
858 while ((*startpp
<= endp
) &&
859 ((c
= **startpp
) != '\n'))
866 if (c
== ')' && seendigit
) {
874 n
= n
* 16 + (c
- '0');
876 n
= n
* 16 + (tolower(c
) - 'a' + 10);
883 * Get a whitespace delimited base 10 number from a string (not file) into buf
884 * update the start pointer to point after the number in the string.
887 getnum_str(u_char
**startpp
, u_char
*endp
) {
892 for (n
= 0; *startpp
<= endp
; ) {
894 if (isspace(c
) || c
== '\0') {
895 if (seendigit
) /* trailing whitespace */
897 else { /* leading whitespace */
903 while ((*startpp
<= endp
) &&
904 ((c
= **startpp
) != '\n'))
911 if (c
== ')' && seendigit
) {
918 n
= n
* 10 + (c
- '0');
925 * Allocate a resource record buffer & save rr info.
928 res_mkupdrec(int section
, const char *dname
,
929 u_int
class, u_int type
, u_long ttl
) {
930 ns_updrec
*rrecp
= (ns_updrec
*)calloc(1, sizeof(ns_updrec
));
932 if (!rrecp
|| !(rrecp
->r_dname
= strdup(dname
))) {
937 INIT_LINK(rrecp
, r_link
);
938 INIT_LINK(rrecp
, r_glink
);
939 rrecp
->r_class
= (ns_class
)class;
940 rrecp
->r_type
= (ns_type
)type
;
942 rrecp
->r_section
= (ns_sect
)section
;
947 * Free a resource record buffer created by res_mkupdrec.
950 res_freeupdrec(ns_updrec
*rrecp
) {
951 /* Note: freeing r_dp is the caller's responsibility. */
952 if (rrecp
->r_dname
!= NULL
)
953 free(rrecp
->r_dname
);
958 struct valuelist
* next
;
959 struct valuelist
* prev
;
964 static struct valuelist
*servicelist
, *protolist
;
967 res_buildservicelist() {
969 struct valuelist
*slp
;
976 while ((sp
= getservent()) != NULL
) {
977 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
980 slp
->name
= strdup(sp
->s_name
);
981 slp
->proto
= strdup(sp
->s_proto
);
982 if ((slp
->name
== NULL
) || (slp
->proto
== NULL
)) {
983 if (slp
->name
) free(slp
->name
);
984 if (slp
->proto
) free(slp
->proto
);
988 slp
->port
= ntohs((u_int16_t
)sp
->s_port
); /* host byt order */
989 slp
->next
= servicelist
;
992 servicelist
->prev
= slp
;
1000 res_destroyservicelist() {
1001 struct valuelist
*slp
, *slp_next
;
1003 for (slp
= servicelist
; slp
!= NULL
; slp
= slp_next
) {
1004 slp_next
= slp
->next
;
1009 servicelist
= (struct valuelist
*)0;
1014 res_buildprotolist(void) {
1015 struct protoent
*pp
;
1016 struct valuelist
*slp
;
1023 while ((pp
= getprotoent()) != NULL
) {
1024 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
1027 slp
->name
= strdup(pp
->p_name
);
1028 if (slp
->name
== NULL
) {
1032 slp
->port
= pp
->p_proto
; /* host byte order */
1033 slp
->next
= protolist
;
1036 protolist
->prev
= slp
;
1044 res_destroyprotolist(void) {
1045 struct valuelist
*plp
, *plp_next
;
1047 for (plp
= protolist
; plp
!= NULL
; plp
= plp_next
) {
1048 plp_next
= plp
->next
;
1052 protolist
= (struct valuelist
*)0;
1057 findservice(const char *s
, struct valuelist
**list
) {
1058 struct valuelist
*lp
= *list
;
1061 for (; lp
!= NULL
; lp
= lp
->next
)
1062 if (strcasecmp(lp
->name
, s
) == 0) {
1064 lp
->prev
->next
= lp
->next
;
1066 lp
->next
->prev
= lp
->prev
;
1071 return (lp
->port
); /* host byte order */
1073 if (sscanf(s
, "%d", &n
) != 1 || n
<= 0)
1079 * Convert service name or (ascii) number to int.
1085 res_servicenumber(const char *p
) {
1086 if (servicelist
== (struct valuelist
*)0)
1087 res_buildservicelist();
1088 return (findservice(p
, &servicelist
));
1092 * Convert protocol name or (ascii) number to int.
1098 res_protocolnumber(const char *p
) {
1099 if (protolist
== (struct valuelist
*)0)
1100 res_buildprotolist();
1101 return (findservice(p
, &protolist
));
1105 static struct servent
*
1106 cgetservbyport(u_int16_t port
, const char *proto
) { /* Host byte order. */
1107 struct valuelist
**list
= &servicelist
;
1108 struct valuelist
*lp
= *list
;
1109 static struct servent serv
;
1112 for (; lp
!= NULL
; lp
= lp
->next
) {
1113 if (port
!= (u_int16_t
)lp
->port
) /* Host byte order. */
1115 if (strcasecmp(lp
->proto
, proto
) == 0) {
1117 lp
->prev
->next
= lp
->next
;
1119 lp
->next
->prev
= lp
->prev
;
1124 serv
.s_name
= lp
->name
;
1125 serv
.s_port
= htons((u_int16_t
)lp
->port
);
1126 serv
.s_proto
= lp
->proto
;
1133 static struct protoent
*
1134 cgetprotobynumber(int proto
) { /* Host byte order. */
1135 struct valuelist
**list
= &protolist
;
1136 struct valuelist
*lp
= *list
;
1137 static struct protoent prot
;
1139 for (; lp
!= NULL
; lp
= lp
->next
)
1140 if (lp
->port
== proto
) { /* Host byte order. */
1142 lp
->prev
->next
= lp
->next
;
1144 lp
->next
->prev
= lp
->prev
;
1149 prot
.p_name
= lp
->name
;
1150 prot
.p_proto
= lp
->port
; /* Host byte order. */
1157 res_protocolname(int num
) {
1158 static char number
[8];
1159 struct protoent
*pp
;
1161 if (protolist
== (struct valuelist
*)0)
1162 res_buildprotolist();
1163 pp
= cgetprotobynumber(num
);
1165 (void) sprintf(number
, "%d", num
);
1168 return (pp
->p_name
);
1172 res_servicename(u_int16_t port
, const char *proto
) { /* Host byte order. */
1173 static char number
[8];
1176 if (servicelist
== (struct valuelist
*)0)
1177 res_buildservicelist();
1178 ss
= cgetservbyport(htons(port
), proto
);
1180 (void) sprintf(number
, "%d", port
);
1183 return (ss
->s_name
);