5520a83b8081e2c4e16c0ddc1bd99089e47727cd
[dragonfly.git] / contrib / isc-dhcp / minires / res_mkupdate.c
1 /*
2  * Copyright (c) 1996-1999 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
18 /*
19  * Based on the Dynamic DNS reference implementation by Viraj Bais
20  * <viraj_bais@ccm.fm.intel.com>
21  */
22
23 #if !defined(lint) && !defined(SABER)
24 static const char rcsid[] = "$Id: res_mkupdate.c,v 1.7 2001/01/11 02:16:24 mellon Exp $";
25 #endif /* not lint */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
33
34 #include <errno.h>
35 #include <limits.h>
36 #include <netdb.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <ctype.h>
42
43 #include "minires/minires.h"
44 #include "arpa/nameser.h"
45
46 /* Options.  Leave them on. */
47 #define DEBUG
48 #define MAXPORT 1024
49
50 static int getnum_str(const u_char **, const u_char *);
51 static int gethexnum_str(const u_char **, const u_char *);
52 static int getword_str(char *, int,
53                        const unsigned char **,
54                        const unsigned char *);
55 static int getstr_str(char *, int, const u_char **, const u_char *);
56
57 struct valuelist {
58         struct valuelist *      next;
59         struct valuelist *      prev;
60         char *                  name;
61         char *                  proto;
62         int                     port;
63 };
64
65 static int findservice(const char *, struct valuelist **);
66 static struct servent *cgetservbyport(unsigned, const char *);
67
68 #define ShrinkBuffer(x)  if ((buflen -= x) < 0) return (-2);
69
70 /* Forward. */
71
72 int res_protocolnumber(const char *);
73 int res_servicenumber(const char *);
74 static struct protoent *cgetprotobynumber(int);
75
76 /*
77  * Form update packets.
78  * Returns the size of the resulting packet if no error
79  * On error,
80  *      returns -1 if error in reading a word/number in rdata
81  *                 portion for update packets
82  *              -2 if length of buffer passed is insufficient
83  *              -3 if zone section is not the first section in
84  *                 the linked list, or section order has a problem
85  *              -4 on a number overflow
86  *              -5 unknown operation or no records
87  */
88 int
89 res_nmkupdate(res_state statp,
90               ns_updrec *rrecp_in, double *bp, unsigned *blp) {
91         ns_updrec *rrecp_start = rrecp_in;
92         HEADER *hp;
93         u_char *cp, *sp1, *sp2;
94         const unsigned char *startp, *endp;
95         int n, i, soanum, multiline;
96         ns_updrec *rrecp;
97         struct in_addr ina;
98         char buf2[MAXDNAME];
99         u_char buf3[MAXDNAME];
100         int section, numrrs = 0, counts[ns_s_max];
101         u_int16_t rtype, rclass;
102         u_int32_t n1, rttl;
103         u_char *dnptrs[20], **dpp, **lastdnptr;
104         unsigned siglen, keylen, certlen;
105         unsigned buflen = *blp;
106         u_char *buf = (unsigned char *)bp;
107
108         /*
109          * Initialize header fields.
110          */
111         if ((buf == NULL) || (buflen < HFIXEDSZ))
112                 return -1;
113         memset(buf, 0, HFIXEDSZ);
114         hp = (HEADER *) buf;
115         hp->id = htons(++statp->id);
116         hp->opcode = ns_o_update;
117         hp->rcode = NOERROR;
118         sp1 = buf + 2*INT16SZ;  /* save pointer to zocount */
119         cp = buf + HFIXEDSZ;
120         buflen -= HFIXEDSZ;
121         dpp = dnptrs;
122         *dpp++ = buf;
123         *dpp++ = NULL;
124         lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
125
126         if (rrecp_start == NULL)
127                 return (-5);
128         else if (rrecp_start->r_section != S_ZONE)
129                 return (-3);
130
131         memset(counts, 0, sizeof counts);
132         for (rrecp = rrecp_start; rrecp; rrecp = ISC_LIST_NEXT(rrecp,
133                                                                r_glink)) {
134                 numrrs++;
135                 section = rrecp->r_section;
136                 if (section < 0 || section >= ns_s_max)
137                         return (-1);
138                 counts[section]++;
139                 for (i = section + 1; i < ns_s_max; i++)
140                         if (counts[i])
141                                 return (-3);
142                 rtype = rrecp->r_type;
143                 rclass = rrecp->r_class;
144                 rttl = rrecp->r_ttl;
145                 /* overload class and type */
146                 if (section == S_PREREQ) {
147                         rttl = 0;
148                         switch (rrecp->r_opcode) {
149                         case YXDOMAIN:
150                                 rclass = C_ANY;
151                                 rtype = T_ANY;
152                                 rrecp->r_size = 0;
153                                 break;
154                         case NXDOMAIN:
155                                 rclass = C_NONE;
156                                 rtype = T_ANY;
157                                 rrecp->r_size = 0;
158                                 break;
159                         case NXRRSET:
160                                 rclass = C_NONE;
161                                 rrecp->r_size = 0;
162                                 break;
163                         case YXRRSET:
164                                 if (rrecp->r_size == 0)
165                                         rclass = C_ANY;
166                                 break;
167                         default:
168                                 fprintf(stderr,
169                                         "res_mkupdate: incorrect opcode: %d\n",
170                                         rrecp->r_opcode);
171                                 fflush(stderr);
172                                 return (-1);
173                         }
174                 } else if (section == S_UPDATE) {
175                         switch (rrecp->r_opcode) {
176                         case DELETE:
177                                 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
178                                 break;
179                         case ADD:
180                                 break;
181                         default:
182                                 fprintf(stderr,
183                                         "res_mkupdate: incorrect opcode: %d\n",
184                                         rrecp->r_opcode);
185                                 fflush(stderr);
186                                 return (-1);
187                         }
188                 }
189
190                 /*
191                  * XXX  appending default domain to owner name is omitted,
192                  *      fqdn must be provided
193                  */
194                 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
195                                  lastdnptr)) < 0)
196                         return (-1);
197                 cp += n;
198                 ShrinkBuffer(n + 2*INT16SZ);
199                 PUTSHORT(rtype, cp);
200                 PUTSHORT(rclass, cp);
201                 if (section == S_ZONE) {
202                         if (numrrs != 1 || rrecp->r_type != T_SOA)
203                                 return (-3);
204                         continue;
205                 }
206                 ShrinkBuffer(INT32SZ + INT16SZ);
207                 PUTLONG(rttl, cp);
208                 sp2 = cp;  /* save pointer to length byte */
209                 cp += INT16SZ;
210                 if (rrecp->r_size == 0) {
211                         if (section == S_UPDATE && rclass != C_ANY)
212                                 return (-1);
213                         else {
214                                 PUTSHORT(0, sp2);
215                                 continue;
216                         }
217                 }
218                 startp = rrecp->r_data;
219                 endp = startp + rrecp->r_size - 1;
220                 /* XXX this should be done centrally. */
221                 switch (rrecp->r_type) {
222                 case T_A:
223                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
224                                 return (-1);
225                         if (!inet_aton(buf2, &ina))
226                                 return (-1);
227                         n1 = ntohl(ina.s_addr);
228                         ShrinkBuffer(INT32SZ);
229                         PUTLONG(n1, cp);
230                         break;
231                 case T_CNAME:
232                 case T_MB:
233                 case T_MG:
234                 case T_MR:
235                 case T_NS:
236                 case T_PTR:
237                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
238                                 return (-1);
239                         n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
240                         if (n < 0)
241                                 return (-1);
242                         cp += n;
243                         ShrinkBuffer(n);
244                         break;
245                 case T_MINFO:
246                 case T_SOA:
247                 case T_RP:
248                         for (i = 0; i < 2; i++) {
249                                 if (!getword_str(buf2, sizeof buf2, &startp,
250                                                  endp))
251                                 return (-1);
252                                 n = dn_comp(buf2, cp, buflen,
253                                             dnptrs, lastdnptr);
254                                 if (n < 0)
255                                         return (-1);
256                                 cp += n;
257                                 ShrinkBuffer(n);
258                         }
259                         if (rrecp->r_type == T_SOA) {
260                                 ShrinkBuffer(5 * INT32SZ);
261                                 while (isspace(*startp) || !*startp)
262                                         startp++;
263                                 if (*startp == '(') {
264                                         multiline = 1;
265                                         startp++;
266                                 } else
267                                         multiline = 0;
268                                 /* serial, refresh, retry, expire, minimum */
269                                 for (i = 0; i < 5; i++) {
270                                         soanum = getnum_str(&startp, endp);
271                                         if (soanum < 0)
272                                                 return (-1);
273                                         PUTLONG(soanum, cp);
274                                 }
275                                 if (multiline) {
276                                         while (isspace(*startp) || !*startp)
277                                                 startp++;
278                                         if (*startp != ')')
279                                                 return (-1);
280                                 }
281                         }
282                         break;
283                 case T_MX:
284                 case T_AFSDB:
285                 case T_RT:
286                         n = getnum_str(&startp, endp);
287                         if (n < 0)
288                                 return (-1);
289                         ShrinkBuffer(INT16SZ);
290                         PUTSHORT(n, cp);
291                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
292                                 return (-1);
293                         n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
294                         if (n < 0)
295                                 return (-1);
296                         cp += n;
297                         ShrinkBuffer(n);
298                         break;
299                 case T_SRV:
300                         n = getnum_str(&startp, endp);
301                         if (n < 0)
302                                 return (-1);
303                         ShrinkBuffer(INT16SZ);
304                         PUTSHORT(n, cp);
305
306                         n = getnum_str(&startp, endp);
307                         if (n < 0)
308                                 return (-1);
309                         ShrinkBuffer(INT16SZ);
310                         PUTSHORT(n, cp);
311
312                         n = getnum_str(&startp, endp);
313                         if (n < 0)
314                                 return (-1);
315                         ShrinkBuffer(INT16SZ);
316                         PUTSHORT(n, cp);
317
318                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
319                                 return (-1);
320                         n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
321                         if (n < 0)
322                                 return (-1);
323                         cp += n;
324                         ShrinkBuffer(n);
325                         break;
326                 case T_PX:
327                         n = getnum_str(&startp, endp);
328                         if (n < 0)
329                                 return (-1);
330                         PUTSHORT(n, cp);
331                         ShrinkBuffer(INT16SZ);
332                         for (i = 0; i < 2; i++) {
333                                 if (!getword_str(buf2, sizeof buf2, &startp,
334                                                  endp))
335                                         return (-1);
336                                 n = dn_comp(buf2, cp, buflen, dnptrs,
337                                             lastdnptr);
338                                 if (n < 0)
339                                         return (-1);
340                                 cp += n;
341                                 ShrinkBuffer(n);
342                         }
343                         break;
344                 case T_WKS: {
345                         char bm[MAXPORT/8];
346                         unsigned maxbm = 0;
347
348                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
349                                 return (-1);
350                         if (!inet_aton(buf2, &ina))
351                                 return (-1);
352                         n1 = ntohl(ina.s_addr);
353                         ShrinkBuffer(INT32SZ);
354                         PUTLONG(n1, cp);
355
356                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
357                                 return (-1);
358                         if ((i = res_protocolnumber(buf2)) < 0)
359                                 return (-1);
360                         ShrinkBuffer(1);
361                         *cp++ = i & 0xff;
362                          
363                         for (i = 0; i < MAXPORT/8 ; i++)
364                                 bm[i] = 0;
365
366                         while (getword_str(buf2, sizeof buf2, &startp, endp)) {
367                                 if ((n1 = res_servicenumber(buf2)) <= 0)
368                                         return (-1);
369
370                                 if (n1 < MAXPORT) {
371                                         bm[n1/8] |= (0x80>>(n1%8));
372                                         if (n1 > maxbm)
373                                                 maxbm = n1;
374                                 } else
375                                         return (-1);
376                         }
377                         maxbm = maxbm/8 + 1;
378                         ShrinkBuffer(maxbm);
379                         memcpy(cp, bm, maxbm);
380                         cp += maxbm;
381                         break;
382                 }
383                 case T_HINFO:
384                         for (i = 0; i < 2; i++) {
385                                 if ((n = getstr_str(buf2, sizeof buf2,
386                                                 &startp, endp)) < 0)
387                                         return (-1);
388                                 if (n > 255)
389                                         return (-1);
390                                 ShrinkBuffer(n+1);
391                                 *cp++ = n;
392                                 memcpy(cp, buf2, (unsigned)n);
393                                 cp += n;
394                         }
395                         break;
396                 case T_TXT:
397                         while (1) {
398                                 if ((n = getstr_str(buf2, sizeof buf2,
399                                                 &startp, endp)) < 0) {
400                                         if (cp != (sp2 + INT16SZ))
401                                                 break;
402                                         return (-1);
403                                 }
404                                 if (n > 255)
405                                         return (-1);
406                                 ShrinkBuffer(n+1);
407                                 *cp++ = n;
408                                 memcpy(cp, buf2, (unsigned)n);
409                                 cp += n;
410                         }
411                         break;
412                 case T_X25:
413                         /* RFC 1183 */
414                         if ((n = getstr_str(buf2, sizeof buf2, &startp,
415                                          endp)) < 0)
416                                 return (-1);
417                         if (n > 255)
418                                 return (-1);
419                         ShrinkBuffer(n+1);
420                         *cp++ = n;
421                         memcpy(cp, buf2, (unsigned)n);
422                         cp += n;
423                         break;
424                 case T_ISDN:
425                         /* RFC 1183 */
426                         if ((n = getstr_str(buf2, sizeof buf2, &startp,
427                                          endp)) < 0)
428                                 return (-1);
429                         if ((n > 255) || (n == 0))
430                                 return (-1);
431                         ShrinkBuffer(n+1);
432                         *cp++ = n;
433                         memcpy(cp, buf2, (unsigned)n);
434                         cp += n;
435                         if ((n = getstr_str(buf2, sizeof buf2, &startp,
436                                          endp)) < 0)
437                                 n = 0;
438                         if (n > 255)
439                                 return (-1);
440                         ShrinkBuffer(n+1);
441                         *cp++ = n;
442                         memcpy(cp, buf2, (unsigned)n);
443                         cp += n;
444                         break;
445 #if 0
446                 case T_NSAP:
447                         if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
448                                 ShrinkBuffer(n);
449                                 memcpy(cp, buf2, n);
450                                 cp += n;
451                         } else {
452                                 return (-1);
453                         }
454                         break;
455                 case T_LOC:
456                         if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
457                                 ShrinkBuffer(n);
458                                 memcpy(cp, buf2, n);
459                                 cp += n;
460                         } else
461                                 return (-1);
462                         break;
463                 case ns_t_sig:
464                     {
465                         int sig_type, success, dateerror;
466                         u_int32_t exptime, timesigned;
467
468                         /* type */
469                         if ((n = getword_str(buf2, sizeof buf2,
470                                              &startp, endp)) < 0)
471                                 return (-1);
472                         sig_type = sym_ston(__p_type_syms, buf2, &success);
473                         if (!success || sig_type == ns_t_any)
474                                 return (-1);
475                         ShrinkBuffer(INT16SZ);
476                         PUTSHORT(sig_type, cp);
477                         /* alg */
478                         n = getnum_str(&startp, endp);
479                         if (n < 0)
480                                 return (-1);
481                         ShrinkBuffer(1);
482                         *cp++ = n;
483                         /* labels */
484                         n = getnum_str(&startp, endp);
485                         if (n <= 0 || n > 255)
486                                 return (-1);
487                         ShrinkBuffer(1);
488                         *cp++ = n;
489                         /* ottl  & expire */
490                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
491                                 return (-1);
492                         exptime = ns_datetosecs(buf2, &dateerror);
493                         if (!dateerror) {
494                                 ShrinkBuffer(INT32SZ);
495                                 PUTLONG(rttl, cp);
496                         }
497                         else {
498                                 char *ulendp;
499                                 u_int32_t ottl;
500
501                                 ottl = strtoul(buf2, &ulendp, 10);
502                                 if (ulendp != NULL && *ulendp != '\0')
503                                         return (-1);
504                                 ShrinkBuffer(INT32SZ);
505                                 PUTLONG(ottl, cp);
506                                 if (!getword_str(buf2, sizeof buf2, &startp,
507                                                  endp))
508                                         return (-1);
509                                 exptime = ns_datetosecs(buf2, &dateerror);
510                                 if (dateerror)
511                                         return (-1);
512                         }
513                         /* expire */
514                         ShrinkBuffer(INT32SZ);
515                         PUTLONG(exptime, cp);
516                         /* timesigned */
517                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
518                                 return (-1);
519                         timesigned = ns_datetosecs(buf2, &dateerror);
520                         if (!dateerror) {
521                                 ShrinkBuffer(INT32SZ);
522                                 PUTLONG(timesigned, cp);
523                         }
524                         else
525                                 return (-1);
526                         /* footprint */
527                         n = getnum_str(&startp, endp);
528                         if (n < 0)
529                                 return (-1);
530                         ShrinkBuffer(INT16SZ);
531                         PUTSHORT(n, cp);
532                         /* signer name */
533                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
534                                 return (-1);
535                         n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
536                         if (n < 0)
537                                 return (-1);
538                         cp += n;
539                         ShrinkBuffer(n);
540                         /* sig */
541                         if ((n = getword_str(buf2, sizeof buf2,
542                                              &startp, endp)) < 0)
543                                 return (-1);
544                         siglen = b64_pton(buf2, buf3, sizeof(buf3));
545                         if (siglen < 0)
546                                 return (-1);
547                         ShrinkBuffer(siglen);
548                         memcpy(cp, buf3, siglen);
549                         cp += siglen;
550                         break;
551                     }
552                 case ns_t_key:
553                         /* flags */
554                         n = gethexnum_str(&startp, endp);
555                         if (n < 0)
556                                 return (-1);
557                         ShrinkBuffer(INT16SZ);
558                         PUTSHORT(n, cp);
559                         /* proto */
560                         n = getnum_str(&startp, endp);
561                         if (n < 0)
562                                 return (-1);
563                         ShrinkBuffer(1);
564                         *cp++ = n;
565                         /* alg */
566                         n = getnum_str(&startp, endp);
567                         if (n < 0)
568                                 return (-1);
569                         ShrinkBuffer(1);
570                         *cp++ = n;
571                         /* key */
572                         if ((n = getword_str(buf2, sizeof buf2,
573                                              &startp, endp)) < 0)
574                                 return (-1);
575                         keylen = b64_pton(buf2, buf3, sizeof(buf3));
576                         if (keylen < 0)
577                                 return (-1);
578                         ShrinkBuffer(keylen);
579                         memcpy(cp, buf3, keylen);
580                         cp += keylen;
581                         break;
582                 case ns_t_nxt:
583                     {
584                         int success, nxt_type;
585                         u_char data[32];
586                         int maxtype;
587
588                         /* next name */
589                         if (!getword_str(buf2, sizeof buf2, &startp, endp))
590                                 return (-1);
591                         n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
592                         if (n < 0)
593                                 return (-1);
594                         cp += n;
595                         ShrinkBuffer(n);
596                         maxtype = 0;
597                         memset(data, 0, sizeof data);
598                         while (1) {
599                                 if (!getword_str(buf2, sizeof buf2, &startp,
600                                                  endp))
601                                         break;
602                                 nxt_type = sym_ston(__p_type_syms, buf2,
603                                                     &success);
604                                 if (!success || !ns_t_rr_p(nxt_type))
605                                         return (-1);
606                                 NS_NXT_BIT_SET(nxt_type, data);
607                                 if (nxt_type > maxtype)
608                                         maxtype = nxt_type;
609                         }
610                         n = maxtype/NS_NXT_BITS+1;
611                         ShrinkBuffer(n);
612                         memcpy(cp, data, n);
613                         cp += n;
614                         break;
615                     }
616                 case ns_t_cert:
617                         /* type */
618                         n = getnum_str(&startp, endp);
619                         if (n < 0)
620                                 return (-1);
621                         ShrinkBuffer(INT16SZ);
622                         PUTSHORT(n, cp);
623                         /* key tag */
624                         n = getnum_str(&startp, endp);
625                         if (n < 0)
626                                 return (-1);
627                         ShrinkBuffer(INT16SZ);
628                         PUTSHORT(n, cp);
629                         /* alg */
630                         n = getnum_str(&startp, endp);
631                         if (n < 0)
632                                 return (-1);
633                         ShrinkBuffer(1);
634                         *cp++ = n;
635                         /* cert */
636                         if ((n = getword_str(buf2, sizeof buf2,
637                                              &startp, endp)) < 0)
638                                 return (-1);
639                         certlen = b64_pton(buf2, buf3, sizeof(buf3));
640                         if (certlen < 0)
641                                 return (-1);
642                         ShrinkBuffer(certlen);
643                         memcpy(cp, buf3, certlen);
644                         cp += certlen;
645                         break;
646 #endif
647                 default:
648                         return (-1);
649                 } /*switch*/
650                 n = (u_int16_t)((cp - sp2) - INT16SZ);
651                 PUTSHORT(n, sp2);
652         } /*for*/
653                 
654         hp->qdcount = htons(counts[0]);
655         hp->ancount = htons(counts[1]);
656         hp->nscount = htons(counts[2]);
657         hp->arcount = htons(counts[3]);
658         *blp = cp - buf;
659         return 0;
660 }
661
662 /*
663  * Get a whitespace delimited word from a string (not file)
664  * into buf. modify the start pointer to point after the
665  * word in the string.
666  */
667 static int
668 getword_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
669         char *cp;
670         int c;
671  
672         for (cp = buf; *startpp <= endp; ) {
673                 c = **startpp;
674                 if (isspace(c) || c == '\0') {
675                         if (cp != buf) /* trailing whitespace */
676                                 break;
677                         else { /* leading whitespace */
678                                 (*startpp)++;
679                                 continue;
680                         }
681                 }
682                 (*startpp)++;
683                 if (cp >= buf+size-1)
684                         break;
685                 *cp++ = (u_char)c;
686         }
687         *cp = '\0';
688         return (cp != buf);
689 }
690
691 /*
692  * get a white spae delimited string from memory.  Process quoted strings
693  * and \DDD escapes.  Return length or -1 on error.  Returned string may
694  * contain nulls.
695  */
696 static char digits[] = "0123456789";
697 static int
698 getstr_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
699         char *cp;
700         int c, c1 = 0;
701         int inquote = 0;
702         int seen_quote = 0;
703         int escape = 0;
704         int dig = 0;
705  
706         for (cp = buf; *startpp <= endp; ) {
707                 if ((c = **startpp) == '\0')
708                         break;
709                 /* leading white space */
710                 if ((cp == buf) && !seen_quote && isspace(c)) {
711                         (*startpp)++;
712                         continue;
713                 }
714
715                 switch (c) {
716                 case '\\':
717                         if (!escape)  {
718                                 escape = 1;
719                                 dig = 0;
720                                 c1 = 0;
721                                 (*startpp)++;
722                                 continue;
723                         } 
724                         goto do_escape;
725                 case '"':
726                         if (!escape) {
727                                 inquote = !inquote;
728                                 seen_quote = 1;
729                                 (*startpp)++;
730                                 continue;
731                         }
732                         /* fall through */
733                 default:
734                 do_escape:
735                         if (escape) {
736                                 switch (c) {
737                                 case '0':
738                                 case '1':
739                                 case '2':
740                                 case '3':
741                                 case '4':
742                                 case '5':
743                                 case '6':
744                                 case '7':
745                                 case '8':
746                                 case '9':
747                                         c1 = c1 * 10 + 
748                                                 (strchr(digits, c) - digits);
749
750                                         if (++dig == 3) {
751                                                 c = c1 &0xff;
752                                                 break;
753                                         }
754                                         (*startpp)++;
755                                         continue;
756                                 }
757                                 escape = 0;
758                         } else if (!inquote && isspace(c))
759                                 goto done;
760                         if (cp >= buf+size-1)
761                                 goto done;
762                         *cp++ = (u_char)c;
763                         (*startpp)++;
764                 }
765         }
766  done:
767         *cp = '\0';
768         return ((cp == buf)?  (seen_quote? 0: -1): (cp - buf));
769 }
770 /*
771  * Get a whitespace delimited base 16 number from a string (not file) into buf
772  * update the start pointer to point after the number in the string.
773  */
774 static int
775 gethexnum_str(const u_char **startpp, const u_char *endp) {
776         int c, n;
777         int seendigit = 0;
778         int m = 0;
779
780         if (*startpp + 2 >= endp ||
781             strncasecmp((const char *)*startpp, "0x", 2) != 0)
782                 return getnum_str(startpp, endp);
783         (*startpp)+=2;
784         for (n = 0; *startpp <= endp; ) {
785                 c = **startpp;
786                 if (isspace(c) || c == '\0') {
787                         if (seendigit) /* trailing whitespace */
788                                 break;
789                         else { /* leading whitespace */
790                                 (*startpp)++;
791                                 continue;
792                         }
793                 }
794                 if (c == ';') {
795                         while ((*startpp <= endp) &&
796                                ((c = **startpp) != '\n'))
797                                         (*startpp)++;
798                         if (seendigit)
799                                 break;
800                         continue;
801                 }
802                 if (!isxdigit(c)) {
803                         if (c == ')' && seendigit) {
804                                 (*startpp)--;
805                                 break;
806                         }
807                         return (-1);
808                 }        
809                 (*startpp)++;
810                 if (isdigit(c))
811                         n = n * 16 + (c - '0');
812                 else
813                         n = n * 16 + (tolower(c) - 'a' + 10);
814                 seendigit = 1;
815         }
816         return (n + m);
817 }
818
819 /*
820  * Get a whitespace delimited base 16 number from a string (not file) into buf
821  * update the start pointer to point after the number in the string.
822  */
823 static int
824 getnum_str(const u_char **startpp, const u_char *endp) {
825         int c, n;
826         int seendigit = 0;
827         int m = 0;
828
829         for (n = 0; *startpp <= endp; ) {
830                 c = **startpp;
831                 if (isspace(c) || c == '\0') {
832                         if (seendigit) /* trailing whitespace */
833                                 break;
834                         else { /* leading whitespace */
835                                 (*startpp)++;
836                                 continue;
837                         }
838                 }
839                 if (c == ';') {
840                         while ((*startpp <= endp) &&
841                                ((c = **startpp) != '\n'))
842                                         (*startpp)++;
843                         if (seendigit)
844                                 break;
845                         continue;
846                 }
847                 if (!isdigit(c)) {
848                         if (c == ')' && seendigit) {
849                                 (*startpp)--;
850                                 break;
851                         }
852                         return (-1);
853                 }        
854                 (*startpp)++;
855                 n = n * 10 + (c - '0');
856                 seendigit = 1;
857         }
858         return (n + m);
859 }
860
861 /*
862  * Allocate a resource record buffer & save rr info.
863  */
864 ns_updrec *
865 res_mkupdrec(int section, const char *dname,
866              u_int class, u_int type, u_long ttl) {
867         ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
868
869         if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
870                 if (rrecp)
871                         free((char *)rrecp);
872                 return (NULL);
873         }
874         rrecp->r_class = class;
875         rrecp->r_type = type;
876         rrecp->r_ttl = ttl;
877         rrecp->r_section = section;
878         return (rrecp);
879 }
880
881 /*
882  * Free a resource record buffer created by res_mkupdrec.
883  */
884 void
885 res_freeupdrec(ns_updrec *rrecp) {
886         /* Note: freeing r_dp is the caller's responsibility. */
887         if (rrecp->r_dname != NULL)
888                 free(rrecp->r_dname);
889         free(rrecp);
890 }
891
892 static struct valuelist *servicelist, *protolist;
893
894 void
895 res_buildservicelist() {
896         struct servent *sp;
897         struct valuelist *slp;
898
899 #ifdef MAYBE_HESIOD
900         setservent(0);
901 #else
902         setservent(1);
903 #endif
904         while ((sp = getservent()) != NULL) {
905                 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
906                 if (!slp)
907                         break;
908                 slp->name = strdup(sp->s_name);
909                 slp->proto = strdup(sp->s_proto);
910                 if ((slp->name == NULL) || (slp->proto == NULL)) {
911                         if (slp->name) free(slp->name);
912                         if (slp->proto) free(slp->proto);
913                         free(slp);
914                         break;
915                 }
916                 slp->port = ntohs((u_int16_t)sp->s_port);  /* host byt order */
917                 slp->next = servicelist;
918                 slp->prev = NULL;
919                 if (servicelist)
920                         servicelist->prev = slp;
921                 servicelist = slp;
922         }
923         endservent();
924 }
925
926 void
927 res_destroyservicelist() {
928         struct valuelist *slp, *slp_next;
929
930         for (slp = servicelist; slp != NULL; slp = slp_next) {
931                 slp_next = slp->next;
932                 free(slp->name);
933                 free(slp->proto);
934                 free(slp);
935         }
936         servicelist = (struct valuelist *)0;
937 }
938
939 void
940 res_buildprotolist() {
941         struct protoent *pp;
942         struct valuelist *slp;
943
944 #ifdef MAYBE_HESIOD
945         setprotoent(0);
946 #else
947         setprotoent(1);
948 #endif
949         while ((pp = getprotoent()) != NULL) {
950                 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
951                 if (!slp)
952                         break;
953                 slp->name = strdup(pp->p_name);
954                 if (slp->name == NULL) {
955                         free(slp);
956                         break;
957                 }
958                 slp->port = pp->p_proto;        /* host byte order */
959                 slp->next = protolist;
960                 slp->prev = NULL;
961                 if (protolist)
962                         protolist->prev = slp;
963                 protolist = slp;
964         }
965         endprotoent();
966 }
967
968 void
969 res_destroyprotolist() {
970         struct valuelist *plp, *plp_next;
971
972         for (plp = protolist; plp != NULL; plp = plp_next) {
973                 plp_next = plp->next;
974                 free(plp->name);
975                 free(plp);
976         }
977         protolist = (struct valuelist *)0;
978 }
979
980 static int
981 findservice(const char *s, struct valuelist **list) {
982         struct valuelist *lp = *list;
983         int n;
984
985         for (; lp != NULL; lp = lp->next)
986                 if (strcasecmp(lp->name, s) == 0) {
987                         if (lp != *list) {
988                                 lp->prev->next = lp->next;
989                                 if (lp->next)
990                                         lp->next->prev = lp->prev;
991                                 (*list)->prev = lp;
992                                 lp->next = *list;
993                                 *list = lp;
994                         }
995                         return (lp->port);      /* host byte order */
996                 }
997         if (sscanf(s, "%d", &n) != 1 || n <= 0)
998                 n = -1;
999         return (n);
1000 }
1001
1002 /*
1003  * Convert service name or (ascii) number to int.
1004  */
1005 int
1006 res_servicenumber(const char *p) {
1007         if (servicelist == (struct valuelist *)0)
1008                 res_buildservicelist();
1009         return (findservice(p, &servicelist));
1010 }
1011
1012 /*
1013  * Convert protocol name or (ascii) number to int.
1014  */
1015 int
1016 res_protocolnumber(const char *p) {
1017         if (protolist == (struct valuelist *)0)
1018                 res_buildprotolist();
1019         return (findservice(p, &protolist));
1020 }
1021
1022 static struct servent *
1023 cgetservbyport(unsigned port, const char *proto) {      /* Host byte order. */
1024         struct valuelist **list = &servicelist;
1025         struct valuelist *lp = *list;
1026         static struct servent serv;
1027
1028         port = ntohs(port);
1029         for (; lp != NULL; lp = lp->next) {
1030                 if (port != (u_int16_t)lp->port)        /* Host byte order. */
1031                         continue;
1032                 if (strcasecmp(lp->proto, proto) == 0) {
1033                         if (lp != *list) {
1034                                 lp->prev->next = lp->next;
1035                                 if (lp->next)
1036                                         lp->next->prev = lp->prev;
1037                                 (*list)->prev = lp;
1038                                 lp->next = *list;
1039                                 *list = lp;
1040                         }
1041                         serv.s_name = lp->name;
1042                         serv.s_port = htons((u_int16_t)lp->port);
1043                         serv.s_proto = lp->proto;
1044                         return (&serv);
1045                 }
1046         }
1047         return (0);
1048 }
1049
1050 static struct protoent *
1051 cgetprotobynumber(int proto) {                          /* Host byte order. */
1052         struct valuelist **list = &protolist;
1053         struct valuelist *lp = *list;
1054         static struct protoent prot;
1055
1056         for (; lp != NULL; lp = lp->next)
1057                 if (lp->port == proto) {                /* Host byte order. */
1058                         if (lp != *list) {
1059                                 lp->prev->next = lp->next;
1060                                 if (lp->next)
1061                                         lp->next->prev = lp->prev;
1062                                 (*list)->prev = lp;
1063                                 lp->next = *list;
1064                                 *list = lp;
1065                         }
1066                         prot.p_name = lp->name;
1067                         prot.p_proto = lp->port;        /* Host byte order. */
1068                         return (&prot);
1069                 }
1070         return (0);
1071 }
1072
1073 const char *
1074 res_protocolname(int num) {
1075         static char number[8];
1076         struct protoent *pp;
1077
1078         if (protolist == (struct valuelist *)0)
1079                 res_buildprotolist();
1080         pp = cgetprotobynumber(num);
1081         if (pp == 0)  {
1082                 (void) sprintf(number, "%d", num);
1083                 return (number);
1084         }
1085         return (pp->p_name);
1086 }
1087
1088 const char *
1089 res_servicename(u_int16_t port, const char *proto) {    /* Host byte order. */
1090         static char number[8];
1091         struct servent *ss;
1092
1093         if (servicelist == (struct valuelist *)0)
1094                 res_buildservicelist();
1095         ss = cgetservbyport(htons(port), proto);
1096         if (ss == 0)  {
1097                 (void) sprintf(number, "%d", port);
1098                 return (number);
1099         }
1100         return (ss->s_name);
1101 }