]>
Commit | Line | Data |
---|---|---|
d7ce7006 | 1 | /*\r |
2 | * Copyright (c) 1996 by Internet Software Consortium.\r | |
3 | *\r | |
4 | * Permission to use, copy, modify, and distribute this software for any\r | |
5 | * purpose with or without fee is hereby granted, provided that the above\r | |
6 | * copyright notice and this permission notice appear in all copies.\r | |
7 | *\r | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r | |
9 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r | |
10 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r | |
11 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r | |
12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r | |
13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r | |
14 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r | |
15 | * SOFTWARE.\r | |
16 | */\r | |
17 | \r | |
18 | /*\r | |
19 | * Portions copyright (c) 1999, 2000\r | |
20 | * Intel Corporation.\r | |
21 | * All rights reserved.\r | |
22 | *\r | |
23 | * Redistribution and use in source and binary forms, with or without\r | |
24 | * modification, are permitted provided that the following conditions\r | |
25 | * are met:\r | |
26 | *\r | |
27 | * 1. Redistributions of source code must retain the above copyright\r | |
28 | * notice, this list of conditions and the following disclaimer.\r | |
29 | *\r | |
30 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
31 | * notice, this list of conditions and the following disclaimer in the\r | |
32 | * documentation and/or other materials provided with the distribution.\r | |
33 | *\r | |
34 | * 3. All advertising materials mentioning features or use of this software\r | |
35 | * must display the following acknowledgement:\r | |
36 | *\r | |
37 | * This product includes software developed by Intel Corporation and\r | |
38 | * its contributors.\r | |
39 | *\r | |
40 | * 4. Neither the name of Intel Corporation or its contributors may be\r | |
41 | * used to endorse or promote products derived from this software\r | |
42 | * without specific prior written permission.\r | |
43 | *\r | |
44 | * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''\r | |
45 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE\r | |
48 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r | |
49 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r | |
50 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r | |
51 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r | |
52 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r | |
53 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r | |
54 | * THE POSSIBILITY OF SUCH DAMAGE.\r | |
55 | *\r | |
56 | */\r | |
57 | \r | |
58 | /*\r | |
59 | * Based on the Dynamic DNS reference implementation by Viraj Bais\r | |
60 | * <viraj_bais@ccm.fm.intel.com>\r | |
61 | */\r | |
62 | \r | |
63 | #if !defined(lint) && !defined(SABER)\r | |
64 | static char rcsid[] = "$Id: res_mkupdate.c,v 1.1.1.1 2003/11/19 01:51:38 kyu3 Exp $";\r | |
65 | #endif /* not lint */\r | |
66 | \r | |
67 | #include <sys/types.h>\r | |
68 | #include <sys/param.h>\r | |
69 | \r | |
70 | #include <netinet/in.h>\r | |
71 | #include <arpa/nameser.h>\r | |
72 | #include <arpa/inet.h>\r | |
73 | \r | |
74 | #include <errno.h>\r | |
75 | #include <limits.h>\r | |
76 | #include <netdb.h>\r | |
77 | #include <resolv.h>\r | |
78 | #include <stdio.h>\r | |
79 | #include <stdlib.h>\r | |
80 | #include <string.h>\r | |
81 | #include <unistd.h>\r | |
82 | #include <ctype.h>\r | |
83 | \r | |
84 | #include "res_config.h"\r | |
85 | \r | |
86 | static int getnum_str(u_char **, u_char *);\r | |
87 | static int getword_str(char *, int, u_char **, u_char *);\r | |
88 | \r | |
89 | #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);\r | |
90 | \r | |
91 | /*\r | |
92 | * Form update packets.\r | |
93 | * Returns the size of the resulting packet if no error\r | |
94 | * On error,\r | |
95 | * returns -1 if error in reading a word/number in rdata\r | |
96 | * portion for update packets\r | |
97 | * -2 if length of buffer passed is insufficient\r | |
98 | * -3 if zone section is not the first section in\r | |
99 | * the linked list, or section order has a problem\r | |
100 | * -4 on a number overflow\r | |
101 | * -5 unknown operation or no records\r | |
102 | */\r | |
103 | int\r | |
104 | res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {\r | |
105 | ns_updrec *rrecp_start = rrecp_in;\r | |
106 | HEADER *hp;\r | |
107 | u_char *cp, *sp1, *sp2, *startp, *endp;\r | |
108 | int n, i, soanum, multiline;\r | |
109 | ns_updrec *rrecp;\r | |
110 | struct in_addr ina;\r | |
111 | char buf2[MAXDNAME];\r | |
112 | int section, numrrs = 0, counts[ns_s_max];\r | |
113 | u_int16_t rtype, rclass;\r | |
114 | u_int32_t n1, rttl;\r | |
115 | u_char *dnptrs[20], **dpp, **lastdnptr;\r | |
116 | \r | |
117 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r | |
118 | h_errno = NETDB_INTERNAL;\r | |
119 | return (-1);\r | |
120 | }\r | |
121 | \r | |
122 | /*\r | |
123 | * Initialize header fields.\r | |
124 | */\r | |
125 | if ((buf == NULL) || (buflen < HFIXEDSZ))\r | |
126 | return (-1);\r | |
127 | memset(buf, 0, HFIXEDSZ);\r | |
128 | hp = (HEADER *) buf;\r | |
129 | hp->id = htons(++_res.id);\r | |
130 | hp->opcode = ns_o_update;\r | |
131 | hp->rcode = NOERROR;\r | |
132 | sp1 = buf + 2*INT16SZ; /* save pointer to zocount */\r | |
133 | cp = buf + HFIXEDSZ;\r | |
134 | buflen -= HFIXEDSZ;\r | |
135 | dpp = dnptrs;\r | |
136 | *dpp++ = buf;\r | |
137 | *dpp++ = NULL;\r | |
138 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];\r | |
139 | \r | |
140 | if (rrecp_start == NULL)\r | |
141 | return (-5);\r | |
142 | else if (rrecp_start->r_section != S_ZONE)\r | |
143 | return (-3);\r | |
144 | \r | |
145 | memset(counts, 0, sizeof counts);\r | |
146 | for (rrecp = rrecp_start; rrecp; rrecp = rrecp->r_grpnext) {\r | |
147 | numrrs++;\r | |
148 | section = rrecp->r_section;\r | |
149 | if (section < 0 || section >= ns_s_max)\r | |
150 | return (-1);\r | |
151 | counts[section]++;\r | |
152 | for (i = section + 1; i < ns_s_max; i++)\r | |
153 | if (counts[i])\r | |
154 | return (-3);\r | |
155 | rtype = rrecp->r_type;\r | |
156 | rclass = rrecp->r_class;\r | |
157 | rttl = rrecp->r_ttl;\r | |
158 | /* overload class and type */\r | |
159 | if (section == S_PREREQ) {\r | |
160 | rttl = 0;\r | |
161 | switch (rrecp->r_opcode) {\r | |
162 | case YXDOMAIN:\r | |
163 | rclass = C_ANY;\r | |
164 | rtype = T_ANY;\r | |
165 | rrecp->r_size = 0;\r | |
166 | break;\r | |
167 | case NXDOMAIN:\r | |
168 | rclass = C_NONE;\r | |
169 | rtype = T_ANY;\r | |
170 | rrecp->r_size = 0;\r | |
171 | break;\r | |
172 | case NXRRSET:\r | |
173 | rclass = C_NONE;\r | |
174 | rrecp->r_size = 0;\r | |
175 | break;\r | |
176 | case YXRRSET:\r | |
177 | if (rrecp->r_size == 0)\r | |
178 | rclass = C_ANY;\r | |
179 | break;\r | |
180 | default:\r | |
181 | fprintf(stderr,\r | |
182 | "res_mkupdate: incorrect opcode: %d\n",\r | |
183 | rrecp->r_opcode);\r | |
184 | fflush(stderr);\r | |
185 | return (-1);\r | |
186 | }\r | |
187 | } else if (section == S_UPDATE) {\r | |
188 | switch (rrecp->r_opcode) {\r | |
189 | case DELETE:\r | |
190 | rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;\r | |
191 | break;\r | |
192 | case ADD:\r | |
193 | break;\r | |
194 | default:\r | |
195 | fprintf(stderr,\r | |
196 | "res_mkupdate: incorrect opcode: %d\n",\r | |
197 | rrecp->r_opcode);\r | |
198 | fflush(stderr);\r | |
199 | return (-1);\r | |
200 | }\r | |
201 | }\r | |
202 | \r | |
203 | /*\r | |
204 | * XXX appending default domain to owner name is omitted,\r | |
205 | * fqdn must be provided\r | |
206 | */\r | |
207 | if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,\r | |
208 | lastdnptr)) < 0)\r | |
209 | return (-1);\r | |
210 | cp += n;\r | |
211 | ShrinkBuffer(n + 2*INT16SZ);\r | |
212 | PUTSHORT(rtype, cp);\r | |
213 | PUTSHORT(rclass, cp);\r | |
214 | if (section == S_ZONE) {\r | |
215 | if (numrrs != 1 || rrecp->r_type != T_SOA)\r | |
216 | return (-3);\r | |
217 | continue;\r | |
218 | }\r | |
219 | ShrinkBuffer(INT32SZ + INT16SZ);\r | |
220 | PUTLONG(rttl, cp);\r | |
221 | sp2 = cp; /* save pointer to length byte */\r | |
222 | cp += INT16SZ;\r | |
223 | if (rrecp->r_size == 0) {\r | |
224 | if (section == S_UPDATE && rclass != C_ANY)\r | |
225 | return (-1);\r | |
226 | else {\r | |
227 | PUTSHORT(0, sp2);\r | |
228 | continue;\r | |
229 | }\r | |
230 | }\r | |
231 | startp = rrecp->r_data;\r | |
232 | endp = startp + rrecp->r_size - 1;\r | |
233 | /* XXX this should be done centrally. */\r | |
234 | switch (rrecp->r_type) {\r | |
235 | case T_A:\r | |
236 | if (!getword_str(buf2, sizeof buf2, &startp, endp))\r | |
237 | return (-1);\r | |
238 | if (!inet_aton(buf2, &ina))\r | |
239 | return (-1);\r | |
240 | n1 = ntohl(ina.s_addr);\r | |
241 | ShrinkBuffer(INT32SZ);\r | |
242 | PUTLONG(n1, cp);\r | |
243 | break;\r | |
244 | case T_CNAME:\r | |
245 | case T_MB:\r | |
246 | case T_MG:\r | |
247 | case T_MR:\r | |
248 | case T_NS:\r | |
249 | case T_PTR:\r | |
250 | if (!getword_str(buf2, sizeof buf2, &startp, endp))\r | |
251 | return (-1);\r | |
252 | n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);\r | |
253 | if (n < 0)\r | |
254 | return (-1);\r | |
255 | cp += n;\r | |
256 | ShrinkBuffer(n);\r | |
257 | break;\r | |
258 | case T_MINFO:\r | |
259 | case T_SOA:\r | |
260 | case T_RP:\r | |
261 | for (i = 0; i < 2; i++) {\r | |
262 | if (!getword_str(buf2, sizeof buf2, &startp,\r | |
263 | endp))\r | |
264 | return (-1);\r | |
265 | n = dn_comp(buf2, cp, buflen,\r | |
266 | dnptrs, lastdnptr);\r | |
267 | if (n < 0)\r | |
268 | return (-1);\r | |
269 | cp += n;\r | |
270 | ShrinkBuffer(n);\r | |
271 | }\r | |
272 | if (rrecp->r_type == T_SOA) {\r | |
273 | ShrinkBuffer(5 * INT32SZ);\r | |
274 | while (isspace(*startp) || !*startp)\r | |
275 | startp++;\r | |
276 | if (*startp == '(') {\r | |
277 | multiline = 1;\r | |
278 | startp++;\r | |
279 | } else\r | |
280 | multiline = 0;\r | |
281 | /* serial, refresh, retry, expire, minimum */\r | |
282 | for (i = 0; i < 5; i++) {\r | |
283 | soanum = getnum_str(&startp, endp);\r | |
284 | if (soanum < 0)\r | |
285 | return (-1);\r | |
286 | PUTLONG(soanum, cp);\r | |
287 | }\r | |
288 | if (multiline) {\r | |
289 | while (isspace(*startp) || !*startp)\r | |
290 | startp++;\r | |
291 | if (*startp != ')')\r | |
292 | return (-1);\r | |
293 | }\r | |
294 | }\r | |
295 | break;\r | |
296 | case T_MX:\r | |
297 | case T_AFSDB:\r | |
298 | case T_RT:\r | |
299 | n = getnum_str(&startp, endp);\r | |
300 | if (n < 0)\r | |
301 | return (-1);\r | |
302 | PUTSHORT(n, cp);\r | |
303 | ShrinkBuffer(INT16SZ);\r | |
304 | if (!getword_str(buf2, sizeof buf2, &startp, endp))\r | |
305 | return (-1);\r | |
306 | n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);\r | |
307 | if (n < 0)\r | |
308 | return (-1);\r | |
309 | cp += n;\r | |
310 | ShrinkBuffer(n);\r | |
311 | break;\r | |
312 | case T_PX:\r | |
313 | n = getnum_str(&startp, endp);\r | |
314 | if (n < 0)\r | |
315 | return (-1);\r | |
316 | PUTSHORT(n, cp);\r | |
317 | ShrinkBuffer(INT16SZ);\r | |
318 | for (i = 0; i < 2; i++) {\r | |
319 | if (!getword_str(buf2, sizeof buf2, &startp,\r | |
320 | endp))\r | |
321 | return (-1);\r | |
322 | n = dn_comp(buf2, cp, buflen, dnptrs,\r | |
323 | lastdnptr);\r | |
324 | if (n < 0)\r | |
325 | return (-1);\r | |
326 | cp += n;\r | |
327 | ShrinkBuffer(n);\r | |
328 | }\r | |
329 | break;\r | |
330 | case T_WKS:\r | |
331 | case T_HINFO:\r | |
332 | case T_TXT:\r | |
333 | case T_X25:\r | |
334 | case T_ISDN:\r | |
335 | case T_NSAP:\r | |
336 | case T_LOC:\r | |
337 | /* XXX - more fine tuning needed here */\r | |
338 | ShrinkBuffer(rrecp->r_size);\r | |
339 | memcpy(cp, rrecp->r_data, rrecp->r_size);\r | |
340 | cp += rrecp->r_size;\r | |
341 | break;\r | |
342 | default:\r | |
343 | return (-1);\r | |
344 | } /*switch*/\r | |
345 | n = (u_int16_t)((cp - sp2) - INT16SZ);\r | |
346 | PUTSHORT(n, sp2);\r | |
347 | } /*for*/\r | |
348 | \r | |
349 | hp->qdcount = htons(counts[0]);\r | |
350 | hp->ancount = htons(counts[1]);\r | |
351 | hp->nscount = htons(counts[2]);\r | |
352 | hp->arcount = htons(counts[3]);\r | |
353 | return ((int)(cp - buf));\r | |
354 | }\r | |
355 | \r | |
356 | /*\r | |
357 | * Get a whitespace delimited word from a string (not file)\r | |
358 | * into buf. modify the start pointer to point after the\r | |
359 | * word in the string.\r | |
360 | */\r | |
361 | static int\r | |
362 | getword_str(char *buf, int size, u_char **startpp, u_char *endp) {\r | |
363 | char *cp;\r | |
364 | int c;\r | |
365 | \r | |
366 | for (cp = buf; *startpp <= endp; ) {\r | |
367 | c = **startpp;\r | |
368 | if (isspace(c) || c == '\0') {\r | |
369 | if (cp != buf) /* trailing whitespace */\r | |
370 | break;\r | |
371 | else { /* leading whitespace */\r | |
372 | (*startpp)++;\r | |
373 | continue;\r | |
374 | }\r | |
375 | }\r | |
376 | (*startpp)++;\r | |
377 | if (cp >= buf+size-1)\r | |
378 | break;\r | |
379 | *cp++ = (u_char)c;\r | |
380 | }\r | |
381 | *cp = '\0';\r | |
382 | return (cp != buf);\r | |
383 | }\r | |
384 | \r | |
385 | /*\r | |
386 | * Get a whitespace delimited number from a string (not file) into buf\r | |
387 | * update the start pointer to point after the number in the string.\r | |
388 | */\r | |
389 | static int\r | |
390 | getnum_str(u_char **startpp, u_char *endp) {\r | |
391 | int c;\r | |
392 | int n;\r | |
393 | int seendigit = 0;\r | |
394 | int m = 0;\r | |
395 | \r | |
396 | for (n = 0; *startpp <= endp; ) {\r | |
397 | c = **startpp;\r | |
398 | if (isspace(c) || c == '\0') {\r | |
399 | if (seendigit) /* trailing whitespace */\r | |
400 | break;\r | |
401 | else { /* leading whitespace */\r | |
402 | (*startpp)++;\r | |
403 | continue;\r | |
404 | }\r | |
405 | }\r | |
406 | if (c == ';') {\r | |
407 | while ((*startpp <= endp) &&\r | |
408 | ((c = **startpp) != '\n'))\r | |
409 | (*startpp)++;\r | |
410 | if (seendigit)\r | |
411 | break;\r | |
412 | continue;\r | |
413 | }\r | |
414 | if (!isdigit(c)) {\r | |
415 | if (c == ')' && seendigit) {\r | |
416 | (*startpp)--;\r | |
417 | break;\r | |
418 | }\r | |
419 | return (-1);\r | |
420 | }\r | |
421 | (*startpp)++;\r | |
422 | n = n * 10 + (c - '0');\r | |
423 | seendigit = 1;\r | |
424 | }\r | |
425 | return (n + m);\r | |
426 | }\r | |
427 | \r | |
428 | /*\r | |
429 | * Allocate a resource record buffer & save rr info.\r | |
430 | */\r | |
431 | ns_updrec *\r | |
432 | res_mkupdrec(int section, const char *dname,\r | |
433 | u_int class, u_int type, u_long ttl) {\r | |
434 | ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));\r | |
435 | \r | |
436 | if (!rrecp || !(rrecp->r_dname = strdup(dname)))\r | |
437 | return (NULL);\r | |
438 | rrecp->r_class = (u_int16_t)class;\r | |
439 | rrecp->r_type = (u_int16_t)type;\r | |
440 | rrecp->r_ttl = (u_int32_t)ttl;\r | |
441 | rrecp->r_section = (u_int8_t)section;\r | |
442 | return (rrecp);\r | |
443 | }\r | |
444 | \r | |
445 | /*\r | |
446 | * Free a resource record buffer created by res_mkupdrec.\r | |
447 | */\r | |
448 | void\r | |
449 | res_freeupdrec(ns_updrec *rrecp) {\r | |
450 | /* Note: freeing r_dp is the caller's responsibility. */\r | |
451 | if (rrecp->r_dname != NULL)\r | |
452 | free(rrecp->r_dname);\r | |
453 | free(rrecp);\r | |
454 | }\r |