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.
20 * Based on the Dynamic DNS reference implementation by Viraj Bais
21 * <viraj_bais@ccm.fm.intel.com>
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <sys/param.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <arpa/inet.h>
37 #include <res_update.h>
44 #include "port_after.h"
46 /* Options. Leave them on. */
50 static int getnum_str(u_char
**, u_char
*);
51 static int gethexnum_str(u_char
**, u_char
*);
52 static int getword_str(char *, int, u_char
**, u_char
*);
53 static int getstr_str(char *, int, u_char
**, u_char
*);
55 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
59 int res_protocolnumber(const char *);
60 int res_servicenumber(const char *);
63 * Form update packets.
64 * Returns the size of the resulting packet if no error
68 *\li -1 if error in reading a word/number in rdata
69 * portion for update packets
70 *\li -2 if length of buffer passed is insufficient
71 *\li -3 if zone section is not the first section in
72 * the linked list, or section order has a problem
73 *\li -4 on a number overflow
74 *\li -5 unknown operation or no records
77 res_nmkupdate(res_state statp
, ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
78 ns_updrec
*rrecp_start
= rrecp_in
;
80 u_char
*cp
, *sp2
, *startp
, *endp
;
81 int n
, i
, soanum
, multiline
;
86 u_char buf3
[MAXDNAME
];
87 int section
, numrrs
= 0, counts
[ns_s_max
];
88 u_int16_t rtype
, rclass
;
90 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
91 int siglen
, keylen
, certlen
;
94 * Initialize header fields.
96 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
98 memset(buf
, 0, HFIXEDSZ
);
100 statp
->id
= res_nrandomid(statp
);
101 hp
->id
= htons(statp
->id
);
102 hp
->opcode
= ns_o_update
;
109 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
111 if (rrecp_start
== NULL
)
113 else if (rrecp_start
->r_section
!= S_ZONE
)
116 memset(counts
, 0, sizeof counts
);
117 for (rrecp
= rrecp_start
; rrecp
; rrecp
= NEXT(rrecp
, r_glink
)) {
119 section
= rrecp
->r_section
;
120 if (section
< 0 || section
>= ns_s_max
)
123 for (i
= section
+ 1; i
< ns_s_max
; i
++)
126 rtype
= rrecp
->r_type
;
127 rclass
= rrecp
->r_class
;
129 /* overload class and type */
130 if (section
== S_PREREQ
) {
132 switch (rrecp
->r_opcode
) {
148 if (rrecp
->r_size
== 0)
153 "res_mkupdate: incorrect opcode: %d\n",
158 } else if (section
== S_UPDATE
) {
159 switch (rrecp
->r_opcode
) {
161 rclass
= rrecp
->r_size
== 0 ? C_ANY
: C_NONE
;
167 "res_mkupdate: incorrect opcode: %d\n",
175 * XXX appending default domain to owner name is omitted,
176 * fqdn must be provided
178 if ((n
= dn_comp(rrecp
->r_dname
, cp
, buflen
, dnptrs
,
182 ShrinkBuffer(n
+ 2*INT16SZ
);
184 PUTSHORT(rclass
, cp
);
185 if (section
== S_ZONE
) {
186 if (numrrs
!= 1 || rrecp
->r_type
!= T_SOA
)
190 ShrinkBuffer(INT32SZ
+ INT16SZ
);
192 sp2
= cp
; /*%< save pointer to length byte */
194 if (rrecp
->r_size
== 0) {
195 if (section
== S_UPDATE
&& rclass
!= C_ANY
)
202 startp
= rrecp
->r_data
;
203 endp
= startp
+ rrecp
->r_size
- 1;
204 /* XXX this should be done centrally. */
205 switch (rrecp
->r_type
) {
207 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
209 if (!inet_aton(buf2
, &ina
))
211 n1
= ntohl(ina
.s_addr
);
212 ShrinkBuffer(INT32SZ
);
222 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
224 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
233 for (i
= 0; i
< 2; i
++) {
234 if (!getword_str(buf2
, sizeof buf2
, &startp
,
237 n
= dn_comp(buf2
, cp
, buflen
,
244 if (rrecp
->r_type
== T_SOA
) {
245 ShrinkBuffer(5 * INT32SZ
);
246 while (isspace(*startp
) || !*startp
)
248 if (*startp
== '(') {
253 /* serial, refresh, retry, expire, minimum */
254 for (i
= 0; i
< 5; i
++) {
255 soanum
= getnum_str(&startp
, endp
);
261 while (isspace(*startp
) || !*startp
)
271 n
= getnum_str(&startp
, endp
);
274 ShrinkBuffer(INT16SZ
);
276 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
278 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
285 n
= getnum_str(&startp
, endp
);
288 ShrinkBuffer(INT16SZ
);
291 n
= getnum_str(&startp
, endp
);
294 ShrinkBuffer(INT16SZ
);
297 n
= getnum_str(&startp
, endp
);
300 ShrinkBuffer(INT16SZ
);
303 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
305 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
312 n
= getnum_str(&startp
, endp
);
316 ShrinkBuffer(INT16SZ
);
317 for (i
= 0; i
< 2; i
++) {
318 if (!getword_str(buf2
, sizeof buf2
, &startp
,
321 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
,
331 unsigned int maxbm
= 0;
333 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
335 if (!inet_aton(buf2
, &ina
))
337 n1
= ntohl(ina
.s_addr
);
338 ShrinkBuffer(INT32SZ
);
341 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
343 if ((i
= res_protocolnumber(buf2
)) < 0)
348 for (i
= 0; i
< MAXPORT
/8 ; i
++)
351 while (getword_str(buf2
, sizeof buf2
, &startp
, endp
)) {
352 if ((n
= res_servicenumber(buf2
)) <= 0)
356 bm
[n
/8] |= (0x80>>(n
%8));
357 if ((unsigned)n
> maxbm
)
364 memcpy(cp
, bm
, maxbm
);
369 for (i
= 0; i
< 2; i
++) {
370 if ((n
= getstr_str(buf2
, sizeof buf2
,
383 if ((n
= getstr_str(buf2
, sizeof buf2
,
384 &startp
, endp
)) < 0) {
385 if (cp
!= (sp2
+ INT16SZ
))
399 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
411 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
414 if ((n
> 255) || (n
== 0))
420 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
431 if ((n
= inet_nsap_addr((char *)startp
, (u_char
*)buf2
, sizeof(buf2
))) != 0) {
440 if ((n
= loc_aton((char *)startp
, (u_char
*)buf2
)) != 0) {
449 int sig_type
, success
, dateerror
;
450 u_int32_t exptime
, timesigned
;
453 if ((n
= getword_str(buf2
, sizeof buf2
,
456 sig_type
= sym_ston(__p_type_syms
, buf2
, &success
);
457 if (!success
|| sig_type
== ns_t_any
)
459 ShrinkBuffer(INT16SZ
);
460 PUTSHORT(sig_type
, cp
);
462 n
= getnum_str(&startp
, endp
);
468 n
= getnum_str(&startp
, endp
);
469 if (n
<= 0 || n
> 255)
474 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
476 exptime
= ns_datetosecs(buf2
, &dateerror
);
478 ShrinkBuffer(INT32SZ
);
486 ottl
= strtoul(buf2
, &ulendp
, 10);
488 (ulendp
!= NULL
&& *ulendp
!= '\0'))
490 ShrinkBuffer(INT32SZ
);
492 if (!getword_str(buf2
, sizeof buf2
, &startp
,
495 exptime
= ns_datetosecs(buf2
, &dateerror
);
500 ShrinkBuffer(INT32SZ
);
501 PUTLONG(exptime
, cp
);
503 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
505 timesigned
= ns_datetosecs(buf2
, &dateerror
);
507 ShrinkBuffer(INT32SZ
);
508 PUTLONG(timesigned
, cp
);
513 n
= getnum_str(&startp
, endp
);
516 ShrinkBuffer(INT16SZ
);
519 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
521 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
527 if ((n
= getword_str(buf2
, sizeof buf2
,
530 siglen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
533 ShrinkBuffer(siglen
);
534 memcpy(cp
, buf3
, siglen
);
540 n
= gethexnum_str(&startp
, endp
);
543 ShrinkBuffer(INT16SZ
);
546 n
= getnum_str(&startp
, endp
);
552 n
= getnum_str(&startp
, endp
);
558 if ((n
= getword_str(buf2
, sizeof buf2
,
561 keylen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
564 ShrinkBuffer(keylen
);
565 memcpy(cp
, buf3
, keylen
);
570 int success
, nxt_type
;
575 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
577 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
583 memset(data
, 0, sizeof data
);
585 if (!getword_str(buf2
, sizeof buf2
, &startp
,
588 nxt_type
= sym_ston(__p_type_syms
, buf2
,
590 if (!success
|| !ns_t_rr_p(nxt_type
))
592 NS_NXT_BIT_SET(nxt_type
, data
);
593 if (nxt_type
> maxtype
)
596 n
= maxtype
/NS_NXT_BITS
+1;
604 n
= getnum_str(&startp
, endp
);
607 ShrinkBuffer(INT16SZ
);
610 n
= getnum_str(&startp
, endp
);
613 ShrinkBuffer(INT16SZ
);
616 n
= getnum_str(&startp
, endp
);
622 if ((n
= getword_str(buf2
, sizeof buf2
,
625 certlen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
628 ShrinkBuffer(certlen
);
629 memcpy(cp
, buf3
, certlen
);
633 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
635 if (inet_pton(AF_INET6
, buf2
, &in6a
) <= 0)
637 ShrinkBuffer(NS_IN6ADDRSZ
);
638 memcpy(cp
, &in6a
, NS_IN6ADDRSZ
);
642 /* Order Preference Flags Service Replacement Regexp */
644 n
= getnum_str(&startp
, endp
);
645 if (n
< 0 || n
> 65535)
647 ShrinkBuffer(INT16SZ
);
650 n
= getnum_str(&startp
, endp
);
651 if (n
< 0 || n
> 65535)
653 ShrinkBuffer(INT16SZ
);
656 if ((n
= getstr_str(buf2
, sizeof buf2
,
657 &startp
, endp
)) < 0) {
666 /* Service Classes */
667 if ((n
= getstr_str(buf2
, sizeof buf2
,
668 &startp
, endp
)) < 0) {
678 if ((n
= getstr_str(buf2
, sizeof buf2
,
679 &startp
, endp
)) < 0) {
689 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
691 n
= dn_comp(buf2
, cp
, buflen
, NULL
, NULL
);
700 n
= (u_int16_t
)((cp
- sp2
) - INT16SZ
);
704 hp
->qdcount
= htons(counts
[0]);
705 hp
->ancount
= htons(counts
[1]);
706 hp
->nscount
= htons(counts
[2]);
707 hp
->arcount
= htons(counts
[3]);
712 * Get a whitespace delimited word from a string (not file)
713 * into buf. modify the start pointer to point after the
714 * word in the string.
717 getword_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
721 for (cp
= buf
; *startpp
<= endp
; ) {
723 if (isspace(c
) || c
== '\0') {
724 if (cp
!= buf
) /*%< trailing whitespace */
726 else { /*%< leading whitespace */
732 if (cp
>= buf
+size
-1)
741 * get a white spae delimited string from memory. Process quoted strings
742 * and \\DDD escapes. Return length or -1 on error. Returned string may
745 static char digits
[] = "0123456789";
747 getstr_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
755 for (cp
= buf
; *startpp
<= endp
; ) {
756 if ((c
= **startpp
) == '\0')
758 /* leading white space */
759 if ((cp
== buf
) && !seen_quote
&& isspace(c
)) {
797 (strchr(digits
, c
) - digits
);
807 } else if (!inquote
&& isspace(c
))
809 if (cp
>= buf
+size
-1)
817 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
= (ns_class
)class;
926 rrecp
->r_type
= (ns_type
)type
;
928 rrecp
->r_section
= (ns_sect
)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 free(rrecp
->r_dname
);
943 struct valuelist
* next
;
944 struct valuelist
* prev
;
949 static struct valuelist
*servicelist
, *protolist
;
952 res_buildservicelist() {
954 struct valuelist
*slp
;
961 while ((sp
= getservent()) != NULL
) {
962 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
965 slp
->name
= strdup(sp
->s_name
);
966 slp
->proto
= strdup(sp
->s_proto
);
967 if ((slp
->name
== NULL
) || (slp
->proto
== NULL
)) {
973 slp
->port
= ntohs((u_int16_t
)sp
->s_port
); /*%< host byt order */
974 slp
->next
= servicelist
;
977 servicelist
->prev
= slp
;
984 res_destroyservicelist() {
985 struct valuelist
*slp
, *slp_next
;
987 for (slp
= servicelist
; slp
!= NULL
; slp
= slp_next
) {
988 slp_next
= slp
->next
;
997 res_buildprotolist(void) {
999 struct valuelist
*slp
;
1006 while ((pp
= getprotoent()) != NULL
) {
1007 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
1010 slp
->name
= strdup(pp
->p_name
);
1011 if (slp
->name
== NULL
) {
1015 slp
->port
= pp
->p_proto
; /*%< host byte order */
1016 slp
->next
= protolist
;
1019 protolist
->prev
= slp
;
1026 res_destroyprotolist(void) {
1027 struct valuelist
*plp
, *plp_next
;
1029 for (plp
= protolist
; plp
!= NULL
; plp
= plp_next
) {
1030 plp_next
= plp
->next
;
1038 findservice(const char *s
, struct valuelist
**list
) {
1039 struct valuelist
*lp
= *list
;
1042 for (; lp
!= NULL
; lp
= lp
->next
)
1043 if (strcasecmp(lp
->name
, s
) == 0) {
1045 lp
->prev
->next
= lp
->next
;
1047 lp
->next
->prev
= lp
->prev
;
1052 return (lp
->port
); /*%< host byte order */
1054 if (sscanf(s
, "%d", &n
) != 1 || n
<= 0)
1060 * Convert service name or (ascii) number to int.
1063 res_servicenumber(const char *p
) {
1064 if (servicelist
== NULL
)
1065 res_buildservicelist();
1066 return (findservice(p
, &servicelist
));
1070 * Convert protocol name or (ascii) number to int.
1073 res_protocolnumber(const char *p
) {
1074 if (protolist
== NULL
)
1075 res_buildprotolist();
1076 return (findservice(p
, &protolist
));
1079 static struct servent
*
1080 cgetservbyport(u_int16_t port
, const char *proto
) { /*%< Host byte order. */
1081 struct valuelist
**list
= &servicelist
;
1082 struct valuelist
*lp
= *list
;
1083 static struct servent serv
;
1086 for (; lp
!= NULL
; lp
= lp
->next
) {
1087 if (port
!= (u_int16_t
)lp
->port
) /*%< Host byte order. */
1089 if (strcasecmp(lp
->proto
, proto
) == 0) {
1091 lp
->prev
->next
= lp
->next
;
1093 lp
->next
->prev
= lp
->prev
;
1098 serv
.s_name
= lp
->name
;
1099 serv
.s_port
= htons((u_int16_t
)lp
->port
);
1100 serv
.s_proto
= lp
->proto
;
1107 static struct protoent
*
1108 cgetprotobynumber(int proto
) { /*%< Host byte order. */
1109 struct valuelist
**list
= &protolist
;
1110 struct valuelist
*lp
= *list
;
1111 static struct protoent prot
;
1113 for (; lp
!= NULL
; lp
= lp
->next
)
1114 if (lp
->port
== proto
) { /*%< Host byte order. */
1116 lp
->prev
->next
= lp
->next
;
1118 lp
->next
->prev
= lp
->prev
;
1123 prot
.p_name
= lp
->name
;
1124 prot
.p_proto
= lp
->port
; /*%< Host byte order. */
1131 res_protocolname(int num
) {
1132 static char number
[8];
1133 struct protoent
*pp
;
1135 if (protolist
== NULL
)
1136 res_buildprotolist();
1137 pp
= cgetprotobynumber(num
);
1139 (void) sprintf(number
, "%d", num
);
1142 return (pp
->p_name
);
1146 res_servicename(u_int16_t port
, const char *proto
) { /*%< Host byte order. */
1147 static char number
[8];
1150 if (servicelist
== NULL
)
1151 res_buildservicelist();
1152 ss
= cgetservbyport(htons(port
), proto
);
1154 (void) sprintf(number
, "%d", port
);
1157 return (ss
->s_name
);