]>
git.saurik.com Git - apple/libresolv.git/blob - res_mkupdate.c
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>
24 #if !defined(lint) && !defined(SABER)
25 static const char rcsid
[] = "$Id: res_mkupdate.c,v 1.1 2006/03/01 19:01:38 majka Exp $";
30 #include "port_before.h"
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #include <arpa/inet.h>
44 #include <res_update.h>
51 #include "res_private.h"
54 #include "port_after.h"
57 /* Options. Leave them on. */
61 static int getnum_str(u_char
**, u_char
*);
62 static int gethexnum_str(u_char
**, u_char
*);
63 static int getword_str(char *, int, u_char
**, u_char
*);
64 static int getstr_str(char *, int, u_char
**, u_char
*);
66 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
73 int res_protocolnumber(const char *);
77 int res_servicenumber(const char *);
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
, ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
93 ns_updrec
*rrecp_start
= rrecp_in
;
95 u_char
*cp
, *sp1
, *sp2
, *startp
, *endp
;
96 int n
, i
, soanum
, multiline
;
100 char buf2
[NS_MAXDNAME
];
101 u_char buf3
[NS_MAXDNAME
];
102 int section
, numrrs
= 0, counts
[ns_s_max
];
103 u_int16_t rtype
, rclass
;
105 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
106 int siglen
, keylen
, certlen
;
109 * Initialize header fields.
111 if ((buf
== NULL
) || (buflen
< NS_HFIXEDSZ
))
113 memset(buf
, 0, NS_HFIXEDSZ
);
116 hp
->id
= res_randomid();
118 hp
->id
= htons(++statp
->id
);
120 hp
->opcode
= ns_o_update
;
121 hp
->rcode
= ns_r_noerror
;
122 sp1
= buf
+ 2*NS_INT16SZ
; /* save pointer to zocount */
123 cp
= buf
+ NS_HFIXEDSZ
;
124 buflen
-= NS_HFIXEDSZ
;
128 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
130 if (rrecp_start
== NULL
)
132 else if (rrecp_start
->r_section
!= ns_s_zn
)
135 memset(counts
, 0, sizeof counts
);
136 for (rrecp
= rrecp_start
; rrecp
; rrecp
= NEXT(rrecp
, r_glink
)) {
138 section
= rrecp
->r_section
;
139 if (section
< 0 || section
>= ns_s_max
)
142 for (i
= section
+ 1; i
< ns_s_max
; i
++)
145 rtype
= rrecp
->r_type
;
146 rclass
= rrecp
->r_class
;
148 /* overload class and type */
149 if (section
== ns_s_pr
) {
151 switch (rrecp
->r_opcode
) {
167 if (rrecp
->r_size
== 0)
172 "res_mkupdate: incorrect opcode: %d\n",
177 } else if (section
== ns_s_ud
) {
178 switch (rrecp
->r_opcode
) {
180 rclass
= rrecp
->r_size
== 0 ? ns_c_any
: ns_c_none
;
186 "res_mkupdate: incorrect opcode: %d\n",
194 * XXX appending default domain to owner name is omitted,
195 * fqdn must be provided
197 if ((n
= dn_comp(rrecp
->r_dname
, cp
, buflen
, dnptrs
,
201 ShrinkBuffer(n
+ 2*NS_INT16SZ
);
203 NS_PUT16(rclass
, cp
);
204 if (section
== ns_s_zn
) {
205 if (numrrs
!= 1 || rrecp
->r_type
!= ns_t_soa
)
209 ShrinkBuffer(NS_INT32SZ
+ NS_INT16SZ
);
211 sp2
= cp
; /* save pointer to length byte */
213 if (rrecp
->r_size
== 0) {
214 if (section
== ns_s_ud
&& rclass
!= ns_c_any
)
221 startp
= rrecp
->r_data
;
222 endp
= startp
+ rrecp
->r_size
- 1;
223 /* XXX this should be done centrally. */
224 switch (rrecp
->r_type
) {
226 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
228 if (!inet_aton(buf2
, &ina
))
230 n1
= ntohl(ina
.s_addr
);
231 ShrinkBuffer(NS_INT32SZ
);
240 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
242 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
251 for (i
= 0; i
< 2; i
++) {
252 if (!getword_str(buf2
, sizeof buf2
, &startp
,
255 n
= dn_comp(buf2
, cp
, buflen
,
262 if (rrecp
->r_type
== ns_t_soa
) {
263 ShrinkBuffer(5 * NS_INT32SZ
);
264 while (isspace(*startp
) || !*startp
)
266 if (*startp
== '(') {
271 /* serial, refresh, retry, expire, minimum */
272 for (i
= 0; i
< 5; i
++) {
273 soanum
= getnum_str(&startp
, endp
);
276 NS_PUT32(soanum
, cp
);
279 while (isspace(*startp
) || !*startp
)
289 n
= getnum_str(&startp
, endp
);
292 ShrinkBuffer(NS_INT16SZ
);
294 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
296 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
303 n
= getnum_str(&startp
, endp
);
306 ShrinkBuffer(NS_INT16SZ
);
309 n
= getnum_str(&startp
, endp
);
312 ShrinkBuffer(NS_INT16SZ
);
315 n
= getnum_str(&startp
, endp
);
318 ShrinkBuffer(NS_INT16SZ
);
321 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
323 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
330 n
= getnum_str(&startp
, endp
);
334 ShrinkBuffer(NS_INT16SZ
);
335 for (i
= 0; i
< 2; i
++) {
336 if (!getword_str(buf2
, sizeof buf2
, &startp
,
339 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
,
349 unsigned int maxbm
= 0;
351 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
353 if (!inet_aton(buf2
, &ina
))
355 n1
= ntohl(ina
.s_addr
);
356 ShrinkBuffer(NS_INT32SZ
);
359 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
361 if ((i
= res_protocolnumber(buf2
)) < 0)
366 for (i
= 0; i
< MAXPORT
/8 ; i
++)
369 while (getword_str(buf2
, sizeof buf2
, &startp
, endp
)) {
370 if ((n1
= res_servicenumber(buf2
)) <= 0)
374 bm
[n1
/8] |= (0x80>>(n1%8
));
382 memcpy(cp
, bm
, maxbm
);
387 for (i
= 0; i
< 2; i
++) {
388 if ((n
= getstr_str(buf2
, sizeof buf2
,
401 if ((n
= getstr_str(buf2
, sizeof buf2
,
402 &startp
, endp
)) < 0) {
403 if (cp
!= (sp2
+ NS_INT16SZ
))
417 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
429 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
432 if ((n
> 255) || (n
== 0))
438 if ((n
= getstr_str(buf2
, sizeof buf2
, &startp
,
449 if ((n
= inet_nsap_addr((char *)startp
, (u_char
*)buf2
, sizeof(buf2
))) != 0) {
458 if ((n
= loc_aton((char *)startp
, (u_char
*)buf2
)) != 0) {
467 int sig_type
, success
, dateerror
;
468 u_int32_t exptime
, timesigned
;
471 if ((n
= getword_str(buf2
, sizeof buf2
,
474 sig_type
= sym_ston(__p_type_syms
, buf2
, &success
);
475 if (!success
|| sig_type
== ns_t_any
)
477 ShrinkBuffer(NS_INT16SZ
);
478 NS_PUT16(sig_type
, cp
);
480 n
= getnum_str(&startp
, endp
);
486 n
= getnum_str(&startp
, endp
);
487 if (n
<= 0 || n
> 255)
492 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
494 exptime
= ns_datetosecs(buf2
, &dateerror
);
496 ShrinkBuffer(NS_INT32SZ
);
503 ottl
= strtoul(buf2
, &ulendp
, 10);
504 if (ulendp
!= NULL
&& *ulendp
!= '\0')
506 ShrinkBuffer(NS_INT32SZ
);
508 if (!getword_str(buf2
, sizeof buf2
, &startp
,
511 exptime
= ns_datetosecs(buf2
, &dateerror
);
516 ShrinkBuffer(NS_INT32SZ
);
517 NS_PUT32(exptime
, cp
);
519 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
521 timesigned
= ns_datetosecs(buf2
, &dateerror
);
523 ShrinkBuffer(NS_INT32SZ
);
524 NS_PUT32(timesigned
, cp
);
529 n
= getnum_str(&startp
, endp
);
532 ShrinkBuffer(NS_INT16SZ
);
535 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
537 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
543 if ((n
= getword_str(buf2
, sizeof buf2
,
546 siglen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
549 ShrinkBuffer(siglen
);
550 memcpy(cp
, buf3
, siglen
);
556 n
= gethexnum_str(&startp
, endp
);
559 ShrinkBuffer(NS_INT16SZ
);
562 n
= getnum_str(&startp
, endp
);
568 n
= getnum_str(&startp
, endp
);
574 if ((n
= getword_str(buf2
, sizeof buf2
,
577 keylen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
580 ShrinkBuffer(keylen
);
581 memcpy(cp
, buf3
, keylen
);
586 int success
, nxt_type
;
591 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
593 n
= dn_comp(buf2
, cp
, buflen
, dnptrs
, lastdnptr
);
599 memset(data
, 0, sizeof data
);
601 if (!getword_str(buf2
, sizeof buf2
, &startp
,
604 nxt_type
= sym_ston(__p_type_syms
, buf2
,
606 if (!success
|| !ns_t_rr_p(nxt_type
))
608 NS_NXT_BIT_SET(nxt_type
, data
);
609 if (nxt_type
> maxtype
)
612 n
= maxtype
/NS_NXT_BITS
+1;
620 n
= getnum_str(&startp
, endp
);
623 ShrinkBuffer(NS_INT16SZ
);
626 n
= getnum_str(&startp
, endp
);
629 ShrinkBuffer(NS_INT16SZ
);
632 n
= getnum_str(&startp
, endp
);
638 if ((n
= getword_str(buf2
, sizeof buf2
,
641 certlen
= b64_pton(buf2
, buf3
, sizeof(buf3
));
644 ShrinkBuffer(certlen
);
645 memcpy(cp
, buf3
, certlen
);
649 if (!getword_str(buf2
, sizeof buf2
, &startp
, endp
))
651 if (inet_pton(AF_INET6
, buf2
, &in6a
) <= 0)
653 ShrinkBuffer(NS_IN6ADDRSZ
);
654 memcpy(cp
, &in6a
, NS_IN6ADDRSZ
);
660 n
= (u_int16_t
)((cp
- sp2
) - NS_INT16SZ
);
664 hp
->qdcount
= htons(counts
[0]);
665 hp
->ancount
= htons(counts
[1]);
666 hp
->nscount
= htons(counts
[2]);
667 hp
->arcount
= htons(counts
[3]);
672 * Get a whitespace delimited word from a string (not file)
673 * into buf. modify the start pointer to point after the
674 * word in the string.
677 getword_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
681 for (cp
= buf
; *startpp
<= endp
; ) {
683 if (isspace(c
) || c
== '\0') {
684 if (cp
!= buf
) /* trailing whitespace */
686 else { /* leading whitespace */
692 if (cp
>= buf
+size
-1)
701 * get a white spae delimited string from memory. Process quoted strings
702 * and \DDD escapes. Return length or -1 on error. Returned string may
705 static char digits
[] = "0123456789";
707 getstr_str(char *buf
, int size
, u_char
**startpp
, u_char
*endp
) {
715 for (cp
= buf
; *startpp
<= endp
; ) {
716 if ((c
= **startpp
) == '\0')
718 /* leading white space */
719 if ((cp
== buf
) && !seen_quote
&& isspace(c
)) {
757 (strchr(digits
, c
) - digits
);
767 } else if (!inquote
&& isspace(c
))
769 if (cp
>= buf
+size
-1)
777 return ((cp
== buf
)? (seen_quote
? 0: -1): (cp
- buf
));
780 * Get a whitespace delimited base 16 number from a string (not file) into buf
781 * update the start pointer to point after the number in the string.
784 gethexnum_str(u_char
**startpp
, u_char
*endp
) {
789 if (*startpp
+ 2 >= endp
|| strncasecmp((char *)*startpp
, "0x", 2) != 0)
790 return getnum_str(startpp
, endp
);
792 for (n
= 0; *startpp
<= endp
; ) {
794 if (isspace(c
) || c
== '\0') {
795 if (seendigit
) /* trailing whitespace */
797 else { /* leading whitespace */
803 while ((*startpp
<= endp
) &&
804 ((c
= **startpp
) != '\n'))
811 if (c
== ')' && seendigit
) {
819 n
= n
* 16 + (c
- '0');
821 n
= n
* 16 + (tolower(c
) - 'a' + 10);
828 * Get a whitespace delimited base 16 number from a string (not file) into buf
829 * update the start pointer to point after the number in the string.
832 getnum_str(u_char
**startpp
, u_char
*endp
) {
837 for (n
= 0; *startpp
<= endp
; ) {
839 if (isspace(c
) || c
== '\0') {
840 if (seendigit
) /* trailing whitespace */
842 else { /* leading whitespace */
848 while ((*startpp
<= endp
) &&
849 ((c
= **startpp
) != '\n'))
856 if (c
== ')' && seendigit
) {
863 n
= n
* 10 + (c
- '0');
870 * Allocate a resource record buffer & save rr info.
873 res_mkupdrec(int section
, const char *dname
,
874 u_int
class, u_int type
, u_long ttl
) {
875 ns_updrec
*rrecp
= (ns_updrec
*)calloc(1, sizeof(ns_updrec
));
877 if (!rrecp
|| !(rrecp
->r_dname
= strdup(dname
))) {
882 INIT_LINK(rrecp
, r_link
);
883 INIT_LINK(rrecp
, r_glink
);
884 rrecp
->r_class
= class;
885 rrecp
->r_type
= type
;
887 rrecp
->r_section
= section
;
892 * Free a resource record buffer created by res_mkupdrec.
895 res_freeupdrec(ns_updrec
*rrecp
) {
896 /* Note: freeing r_dp is the caller's responsibility. */
897 if (rrecp
->r_dname
!= NULL
)
898 free(rrecp
->r_dname
);
903 struct valuelist
* next
;
904 struct valuelist
* prev
;
909 static struct valuelist
*servicelist
, *protolist
;
912 res_buildservicelist() {
914 struct valuelist
*slp
;
921 while ((sp
= getservent()) != NULL
) {
922 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
925 slp
->name
= strdup(sp
->s_name
);
926 slp
->proto
= strdup(sp
->s_proto
);
927 if ((slp
->name
== NULL
) || (slp
->proto
== NULL
)) {
928 if (slp
->name
) free(slp
->name
);
929 if (slp
->proto
) free(slp
->proto
);
933 slp
->port
= ntohs((u_int16_t
)sp
->s_port
); /* host byt order */
934 slp
->next
= servicelist
;
937 servicelist
->prev
= slp
;
944 res_destroyservicelist() {
945 struct valuelist
*slp
, *slp_next
;
947 for (slp
= servicelist
; slp
!= NULL
; slp
= slp_next
) {
948 slp_next
= slp
->next
;
953 servicelist
= (struct valuelist
*)0;
957 res_buildprotolist(void) {
959 struct valuelist
*slp
;
966 while ((pp
= getprotoent()) != NULL
) {
967 slp
= (struct valuelist
*)malloc(sizeof(struct valuelist
));
970 slp
->name
= strdup(pp
->p_name
);
971 if (slp
->name
== NULL
) {
975 slp
->port
= pp
->p_proto
; /* host byte order */
976 slp
->next
= protolist
;
979 protolist
->prev
= slp
;
986 res_destroyprotolist(void) {
987 struct valuelist
*plp
, *plp_next
;
989 for (plp
= protolist
; plp
!= NULL
; plp
= plp_next
) {
990 plp_next
= plp
->next
;
994 protolist
= (struct valuelist
*)0;
998 findservice(const char *s
, struct valuelist
**list
) {
999 struct valuelist
*lp
= *list
;
1002 for (; lp
!= NULL
; lp
= lp
->next
)
1003 if (strcasecmp(lp
->name
, s
) == 0) {
1005 lp
->prev
->next
= lp
->next
;
1007 lp
->next
->prev
= lp
->prev
;
1012 return (lp
->port
); /* host byte order */
1014 if (sscanf(s
, "%d", &n
) != 1 || n
<= 0)
1020 * Convert service name or (ascii) number to int.
1026 res_servicenumber(const char *p
) {
1027 if (servicelist
== (struct valuelist
*)0)
1028 res_buildservicelist();
1029 return (findservice(p
, &servicelist
));
1033 * Convert protocol name or (ascii) number to int.
1039 res_protocolnumber(const char *p
) {
1040 if (protolist
== (struct valuelist
*)0)
1041 res_buildprotolist();
1042 return (findservice(p
, &protolist
));
1045 static struct servent
*
1046 cgetservbyport(u_int16_t port
, const char *proto
) { /* Host byte order. */
1047 struct valuelist
**list
= &servicelist
;
1048 struct valuelist
*lp
= *list
;
1049 static struct servent serv
;
1052 for (; lp
!= NULL
; lp
= lp
->next
) {
1053 if (port
!= (u_int16_t
)lp
->port
) /* Host byte order. */
1055 if (strcasecmp(lp
->proto
, proto
) == 0) {
1057 lp
->prev
->next
= lp
->next
;
1059 lp
->next
->prev
= lp
->prev
;
1064 serv
.s_name
= lp
->name
;
1065 serv
.s_port
= htons((u_int16_t
)lp
->port
);
1066 serv
.s_proto
= lp
->proto
;
1073 static struct protoent
*
1074 cgetprotobynumber(int proto
) { /* Host byte order. */
1075 struct valuelist
**list
= &protolist
;
1076 struct valuelist
*lp
= *list
;
1077 static struct protoent prot
;
1079 for (; lp
!= NULL
; lp
= lp
->next
)
1080 if (lp
->port
== proto
) { /* Host byte order. */
1082 lp
->prev
->next
= lp
->next
;
1084 lp
->next
->prev
= lp
->prev
;
1089 prot
.p_name
= lp
->name
;
1090 prot
.p_proto
= lp
->port
; /* Host byte order. */
1097 res_protocolname(int num
) {
1098 static char number
[8];
1099 struct protoent
*pp
;
1101 if (protolist
== (struct valuelist
*)0)
1102 res_buildprotolist();
1103 pp
= cgetprotobynumber(num
);
1105 (void) sprintf(number
, "%d", num
);
1108 return (pp
->p_name
);
1112 res_servicename(u_int16_t port
, const char *proto
) { /* Host byte order. */
1113 static char number
[8];
1116 if (servicelist
== (struct valuelist
*)0)
1117 res_buildservicelist();
1118 ss
= cgetservbyport(htons(port
), proto
);
1120 (void) sprintf(number
, "%d", port
);
1123 return (ss
->s_name
);