libcr copy: Retarget build paths from ../libc to ../libcr and retarget
[dragonfly.git] / lib / libcr / net / res_mkupdate.c
1 /*
2  * Copyright (c) 1996 by Internet Software Consortium.
3  *
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.
7  *
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
15  * SOFTWARE.
16  *
17  * $FreeBSD: src/lib/libc/net/res_mkupdate.c,v 1.2.2.1 2001/03/05 10:47:11 obrien Exp $
18  * $DragonFly: src/lib/libcr/net/Attic/res_mkupdate.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
19  */
20
21 /*
22  * Based on the Dynamic DNS reference implementation by Viraj Bais
23  * <viraj_bais@ccm.fm.intel.com>
24  */
25
26 #include <sys/types.h>
27 #include <sys/param.h>
28
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <arpa/inet.h>
32
33 #include <limits.h>
34 #include <netdb.h>
35 #include <resolv.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <ctype.h>
41
42 #include "res_config.h"
43
44 static int getnum_str(u_char **, u_char *);
45 static int getword_str(char *, int, u_char **, u_char *);
46
47 #define ShrinkBuffer(x)  if ((buflen -= x) < 0) return (-2);
48
49 /*
50  * Form update packets.
51  * Returns the size of the resulting packet if no error
52  * On error,
53  *      returns -1 if error in reading a word/number in rdata
54  *                 portion for update packets
55  *              -2 if length of buffer passed is insufficient
56  *              -3 if zone section is not the first section in
57  *                 the linked list, or section order has a problem
58  *              -4 on a number overflow
59  *              -5 unknown operation or no records
60  */
61 int
62 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
63         ns_updrec *rrecp_start = rrecp_in;
64         HEADER *hp;
65         u_char c, *cp, *cp1, *sp1, *sp2, *startp, *endp;
66         int n, i, j, found, soanum, multiline;
67         ns_updrec *rrecp, *tmprrecp, *recptr = NULL;
68         struct in_addr ina;
69         char buf2[MAXDNAME];
70         int section, numrrs = 0, counts[ns_s_max];
71         u_int16_t rtype, rclass;
72         u_int32_t n1, rttl;
73         u_char *dnptrs[20], **dpp, **lastdnptr;
74
75         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
76                 h_errno = NETDB_INTERNAL;
77                 return (-1);
78         }
79
80         /*
81          * Initialize header fields.
82          */
83         if ((buf == NULL) || (buflen < HFIXEDSZ))
84                 return (-1);
85         memset(buf, 0, HFIXEDSZ);
86         hp = (HEADER *) buf;
87         hp->id = htons(++_res.id);
88         hp->opcode = ns_o_update;
89         hp->rcode = NOERROR;
90         sp1 = buf + 2*INT16SZ;  /* save pointer to zocount */
91         cp = buf + HFIXEDSZ;
92         buflen -= HFIXEDSZ;
93         dpp = dnptrs;
94         *dpp++ = buf;
95         *dpp++ = NULL;
96         lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
97
98         if (rrecp_start == NULL)
99                 return (-5);
100         else if (rrecp_start->r_section != S_ZONE)
101                 return (-3);
102
103         memset(counts, 0, sizeof counts);
104         for (rrecp = rrecp_start; rrecp; rrecp = rrecp->r_grpnext) {
105                 numrrs++;
106                 section = rrecp->r_section;
107                 if (section < 0 || section >= ns_s_max)
108                         return (-1);
109                 counts[section]++;
110                 for (i = section + 1; i < ns_s_max; i++)
111                         if (counts[i])
112                                 return (-3);
113                 rtype = rrecp->r_type;
114                 rclass = rrecp->r_class;
115                 rttl = rrecp->r_ttl;
116                 /* overload class and type */
117                 if (section == S_PREREQ) {
118                         rttl = 0;
119                         switch (rrecp->r_opcode) {
120                         case YXDOMAIN:
121                                 rclass = C_ANY;
122                                 rtype = T_ANY;
123                                 rrecp->r_size = 0;
124                                 break;
125                         case NXDOMAIN:
126                                 rclass = C_NONE;
127                                 rtype = T_ANY;
128                                 rrecp->r_size = 0;
129                                 break;
130                         case NXRRSET:
131                                 rclass = C_NONE;
132                                 rrecp->r_size = 0;
133                                 break;
134                         case YXRRSET:
135                                 if (rrecp->r_size == 0)
136                                         rclass = C_ANY;
137                                 break;
138                         default:
139                                 fprintf(stderr,
140                                         "res_mkupdate: incorrect opcode: %d\n",
141                                         rrecp->r_opcode);
142                                 fflush(stderr);
143                                 return (-1);
144                         }
145                 } else if (section == S_UPDATE) {
146                         switch (rrecp->r_opcode) {
147                         case DELETE:
148                                 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
149                                 break;
150                         case ADD:
151                                 break;
152                         default:
153                                 fprintf(stderr,
154                                         "res_mkupdate: incorrect opcode: %d\n",
155                                         rrecp->r_opcode);
156                                 fflush(stderr);
157                                 return (-1);
158                         }
159                 }
160
161                 /*
162                  * XXX  appending default domain to owner name is omitted,
163                  *      fqdn must be provided
164                  */
165                 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
166                                  lastdnptr)) < 0)
167                         return (-1);
168                 cp += n;
169                 ShrinkBuffer(n + 2*INT16SZ);
170                 PUTSHORT(rtype, cp);
171                 PUTSHORT(rclass, cp);
172                 if (section == S_ZONE) {
173                         if (numrrs != 1 || rrecp->r_type != T_SOA)
174                                 return (-3);
175                         continue;
176                 }
177                 ShrinkBuffer(INT32SZ + INT16SZ);
178                 PUTLONG(rttl, cp);
179                 sp2 = cp;  /* save pointer to length byte */
180                 cp += INT16SZ;
181                 if (rrecp->r_size == 0) {
182                         if (section == S_UPDATE && rclass != C_ANY)
183                                 return (-1);
184                         else {
185                                 PUTSHORT(0, sp2);
186                                 continue;
187                         }
188                 }
189                 startp = rrecp->r_data;
190                 endp = startp + rrecp->r_size - 1;
191                 /* XXX this should be done centrally. */
192                 switch (rrecp->r_type) {
193                 case T_A:
194                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
195                                 return (-1);
196                         if (!inet_aton(buf2, &ina))
197                                 return (-1);
198                         n1 = ntohl(ina.s_addr);
199                         ShrinkBuffer(INT32SZ);
200                         PUTLONG(n1, cp);
201                         break;
202                 case T_CNAME:
203                 case T_MB:
204                 case T_MG:
205                 case T_MR:
206                 case T_NS:
207                 case T_PTR:
208                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
209                                 return (-1);
210                         n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
211                         if (n < 0)
212                                 return (-1);
213                         cp += n;
214                         ShrinkBuffer(n);
215                         break;
216                 case T_MINFO:
217                 case T_SOA:
218                 case T_RP:
219                         for (i = 0; i < 2; i++) {
220                                 if (!getword_str(buf2, sizeof buf2, &startp,
221                                                  endp))
222                                 return (-1);
223                                 n = dn_comp(buf2, cp, buflen,
224                                             dnptrs, lastdnptr);
225                                 if (n < 0)
226                                         return (-1);
227                                 cp += n;
228                                 ShrinkBuffer(n);
229                         }
230                         if (rrecp->r_type == T_SOA) {
231                                 ShrinkBuffer(5 * INT32SZ);
232                                 while (isspace(*startp) || !*startp)
233                                         startp++;
234                                 if (*startp == '(') {
235                                         multiline = 1;
236                                         startp++;
237                                 } else
238                                         multiline = 0;
239                                 /* serial, refresh, retry, expire, minimum */
240                                 for (i = 0; i < 5; i++) {
241                                         soanum = getnum_str(&startp, endp);
242                                         if (soanum < 0)
243                                                 return (-1);
244                                         PUTLONG(soanum, cp);
245                                 }
246                                 if (multiline) {
247                                         while (isspace(*startp) || !*startp)
248                                                 startp++;
249                                         if (*startp != ')')
250                                                 return (-1);
251                                 }
252                         }
253                         break;
254                 case T_MX:
255                 case T_AFSDB:
256                 case T_RT:
257                         n = getnum_str(&startp, endp);
258                         if (n < 0)
259                                 return (-1);
260                         PUTSHORT(n, cp);
261                         ShrinkBuffer(INT16SZ);
262                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
263                                 return (-1);
264                         n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
265                         if (n < 0)
266                                 return (-1);
267                         cp += n;
268                         ShrinkBuffer(n);
269                         break;
270                 case T_PX:
271                         n = getnum_str(&startp, endp);
272                         if (n < 0)
273                                 return (-1);
274                         PUTSHORT(n, cp);
275                         ShrinkBuffer(INT16SZ);
276                         for (i = 0; i < 2; i++) {
277                                 if (!getword_str(buf2, sizeof buf2, &startp,
278                                                  endp))
279                                         return (-1);
280                                 n = dn_comp(buf2, cp, buflen, dnptrs,
281                                             lastdnptr);
282                                 if (n < 0)
283                                         return (-1);
284                                 cp += n;
285                                 ShrinkBuffer(n);
286                         }
287                         break;
288                 case T_WKS:
289                 case T_HINFO:
290                 case T_TXT:
291                 case T_X25:
292                 case T_ISDN:
293                 case T_NSAP:
294                 case T_LOC:
295                         /* XXX - more fine tuning needed here */
296                         ShrinkBuffer(rrecp->r_size);
297                         memcpy(cp, rrecp->r_data, rrecp->r_size);
298                         cp += rrecp->r_size;
299                         break;
300                 default:
301                         return (-1);
302                 } /*switch*/
303                 n = (u_int16_t)((cp - sp2) - INT16SZ);
304                 PUTSHORT(n, sp2);
305         } /*for*/
306                 
307         hp->qdcount = htons(counts[0]);
308         hp->ancount = htons(counts[1]);
309         hp->nscount = htons(counts[2]);
310         hp->arcount = htons(counts[3]);
311         return (cp - buf);
312 }
313
314 /*
315  * Get a whitespace delimited word from a string (not file)
316  * into buf. modify the start pointer to point after the
317  * word in the string.
318  */
319 static int
320 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
321         char *cp;
322         int c;
323  
324         for (cp = buf; *startpp <= endp; ) {
325                 c = **startpp;
326                 if (isspace(c) || c == '\0') {
327                         if (cp != buf) /* trailing whitespace */
328                                 break;
329                         else { /* leading whitespace */
330                                 (*startpp)++;
331                                 continue;
332                         }
333                 }
334                 (*startpp)++;
335                 if (cp >= buf+size-1)
336                         break;
337                 *cp++ = (u_char)c;
338         }
339         *cp = '\0';
340         return (cp != buf);
341 }
342
343 /*
344  * Get a whitespace delimited number from a string (not file) into buf
345  * update the start pointer to point after the number in the string.
346  */
347 static int
348 getnum_str(u_char **startpp, u_char *endp) {
349         int c, n;
350         int seendigit = 0;
351         int seendecimal = 0;
352         int m = 0;
353
354         for (n = 0; *startpp <= endp; ) {
355                 c = **startpp;
356                 if (isspace(c) || c == '\0') {
357                         if (seendigit) /* trailing whitespace */
358                                 break;
359                         else { /* leading whitespace */
360                                 (*startpp)++;
361                                 continue;
362                         }
363                 }
364                 if (c == ';') {
365                         while ((*startpp <= endp) &&
366                                ((c = **startpp) != '\n'))
367                                         (*startpp)++;
368                         if (seendigit)
369                                 break;
370                         continue;
371                 }
372                 if (!isdigit(c)) {
373                         if (c == ')' && seendigit) {
374                                 (*startpp)--;
375                                 break;
376                         }
377                         return (-1);
378                 }        
379                 (*startpp)++;
380                 n = n * 10 + (c - '0');
381                 seendigit = 1;
382         }
383         return (n + m);
384 }
385
386 /*
387  * Allocate a resource record buffer & save rr info.
388  */
389 ns_updrec *
390 res_mkupdrec(int section, const char *dname,
391              u_int class, u_int type, u_long ttl) {
392         ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
393
394         if (!rrecp || !(rrecp->r_dname = strdup(dname)))
395                 return (NULL);
396         rrecp->r_class = class;
397         rrecp->r_type = type;
398         rrecp->r_ttl = ttl;
399         rrecp->r_section = section;
400         return (rrecp);
401 }
402
403 /*
404  * Free a resource record buffer created by res_mkupdrec.
405  */
406 void
407 res_freeupdrec(ns_updrec *rrecp) {
408         /* Note: freeing r_dp is the caller's responsibility. */
409         if (rrecp->r_dname != NULL)
410                 free(rrecp->r_dname);
411         free(rrecp);
412 }