]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/dhclient/parse.c
Merge lld trunk r321414 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / sbin / dhclient / parse.c
1 /*      $OpenBSD: parse.c,v 1.11 2004/05/05 23:07:47 deraadt Exp $      */
2
3 /* Common parser code for dhcpd and dhclient. */
4
5 /*-
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of The Internet Software Consortium nor the names
21  *    of its contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
25  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
26  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * This software has been written for the Internet Software Consortium
39  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
40  * Enterprises.  To learn more about the Internet Software Consortium,
41  * see ``http://www.vix.com/isc''.  To learn more about Vixie
42  * Enterprises, see ``http://www.vix.com''.
43  */
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include "dhcpd.h"
49 #include "dhctoken.h"
50
51 /* Skip to the semicolon ending the current statement.   If we encounter
52  * braces, the matching closing brace terminates the statement.   If we
53  * encounter a right brace but haven't encountered a left brace, return
54  * leaving the brace in the token buffer for the caller.   If we see a
55  * semicolon and haven't seen a left brace, return.   This lets us skip
56  * over:
57  *
58  *      statement;
59  *      statement foo bar { }
60  *      statement foo bar { statement { } }
61  *      statement}
62  *
63  *      ...et cetera.
64  */
65 void
66 skip_to_semi(FILE *cfile)
67 {
68         int brace_count = 0, token;
69         char *val;
70
71         do {
72                 token = peek_token(&val, cfile);
73                 if (token == RBRACE) {
74                         if (brace_count) {
75                                 token = next_token(&val, cfile);
76                                 if (!--brace_count)
77                                         return;
78                         } else
79                                 return;
80                 } else if (token == LBRACE) {
81                         brace_count++;
82                 } else if (token == SEMI && !brace_count) {
83                         token = next_token(&val, cfile);
84                         return;
85                 } else if (token == '\n') {
86                         /*
87                          * EOL only happens when parsing
88                          * /etc/resolv.conf, and we treat it like a
89                          * semicolon because the resolv.conf file is
90                          * line-oriented.
91                          */
92                         token = next_token(&val, cfile);
93                         return;
94                 }
95                 token = next_token(&val, cfile);
96         } while (token != EOF);
97 }
98
99 int
100 parse_semi(FILE *cfile)
101 {
102         int token;
103         char *val;
104
105         token = next_token(&val, cfile);
106         if (token != SEMI) {
107                 parse_warn("semicolon expected.");
108                 skip_to_semi(cfile);
109                 return (0);
110         }
111         return (1);
112 }
113
114 /*
115  * string-parameter :== STRING SEMI
116  */
117 char *
118 parse_string(FILE *cfile)
119 {
120         char *val, *s;
121         size_t valsize;
122         int token;
123
124         token = next_token(&val, cfile);
125         if (token != STRING) {
126                 parse_warn("filename must be a string");
127                 skip_to_semi(cfile);
128                 return (NULL);
129         }
130         valsize = strlen(val) + 1;
131         s = malloc(valsize);
132         if (!s)
133                 error("no memory for string %s.", val);
134         memcpy(s, val, valsize);
135
136         if (!parse_semi(cfile)) {
137                 free(s);
138                 return (NULL);
139         }
140         return (s);
141 }
142
143 int
144 parse_ip_addr(FILE *cfile, struct iaddr *addr)
145 {
146         addr->len = 4;
147         if (parse_numeric_aggregate(cfile, addr->iabuf,
148             &addr->len, DOT, 10, 8))
149                 return (1);
150         return (0);
151 }
152
153 /*
154  * hardware-parameter :== HARDWARE ETHERNET csns SEMI
155  * csns :== NUMBER | csns COLON NUMBER
156  */
157 void
158 parse_hardware_param(FILE *cfile, struct hardware *hardware)
159 {
160         unsigned char *t;
161         int token;
162         size_t hlen;
163         char *val;
164
165         token = next_token(&val, cfile);
166         switch (token) {
167         case ETHERNET:
168                 hardware->htype = HTYPE_ETHER;
169                 break;
170         case TOKEN_RING:
171                 hardware->htype = HTYPE_IEEE802;
172                 break;
173         case FDDI:
174                 hardware->htype = HTYPE_FDDI;
175                 break;
176         default:
177                 parse_warn("expecting a network hardware type");
178                 skip_to_semi(cfile);
179                 return;
180         }
181
182         /*
183          * Parse the hardware address information.   Technically, it
184          * would make a lot of sense to restrict the length of the data
185          * we'll accept here to the length of a particular hardware
186          * address type.   Unfortunately, there are some broken clients
187          * out there that put bogus data in the chaddr buffer, and we
188          * accept that data in the lease file rather than simply failing
189          * on such clients.   Yuck.
190          */
191         hlen = 0;
192         t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
193         if (!t)
194                 return;
195         if (hlen > sizeof(hardware->haddr)) {
196                 free(t);
197                 parse_warn("hardware address too long");
198         } else {
199                 hardware->hlen = hlen;
200                 memcpy((unsigned char *)&hardware->haddr[0], t,
201                     hardware->hlen);
202                 if (hlen < sizeof(hardware->haddr))
203                         memset(&hardware->haddr[hlen], 0,
204                             sizeof(hardware->haddr) - hlen);
205                 free(t);
206         }
207
208         token = next_token(&val, cfile);
209         if (token != SEMI) {
210                 parse_warn("expecting semicolon.");
211                 skip_to_semi(cfile);
212         }
213 }
214
215 /*
216  * lease-time :== NUMBER SEMI
217  */
218 void
219 parse_lease_time(FILE *cfile, time_t *timep)
220 {
221         char *val;
222         int token;
223
224         token = next_token(&val, cfile);
225         if (token != NUMBER) {
226                 parse_warn("Expecting numeric lease time");
227                 skip_to_semi(cfile);
228                 return;
229         }
230         convert_num((unsigned char *)timep, val, 10, 32);
231         /* Unswap the number - convert_num returns stuff in NBO. */
232         *timep = ntohl(*timep); /* XXX */
233
234         parse_semi(cfile);
235 }
236
237 /*
238  * No BNF for numeric aggregates - that's defined by the caller.  What
239  * this function does is to parse a sequence of numbers separated by the
240  * token specified in separator.  If max is zero, any number of numbers
241  * will be parsed; otherwise, exactly max numbers are expected.  Base
242  * and size tell us how to internalize the numbers once they've been
243  * tokenized.
244  */
245 unsigned char *
246 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, size_t *max,
247     int separator, unsigned base, int size)
248 {
249         unsigned char *bufp = buf, *s = NULL;
250         int token;
251         char *val, *t;
252         size_t valsize, count = 0;
253         pair c = NULL;
254         unsigned char *lbufp = NULL;
255
256         if (!bufp && *max) {
257                 lbufp = bufp = malloc(*max * size / 8);
258                 if (!bufp)
259                         error("can't allocate space for numeric aggregate");
260         } else
261                 s = bufp;
262
263         do {
264                 if (count) {
265                         token = peek_token(&val, cfile);
266                         if (token != separator) {
267                                 if (!*max)
268                                         break;
269                                 if (token != RBRACE && token != LBRACE)
270                                         token = next_token(&val, cfile);
271                                 parse_warn("too few numbers.");
272                                 if (token != SEMI)
273                                         skip_to_semi(cfile);
274                                 free(lbufp);
275                                 return (NULL);
276                         }
277                         token = next_token(&val, cfile);
278                 }
279                 token = next_token(&val, cfile);
280
281                 if (token == EOF) {
282                         parse_warn("unexpected end of file");
283                         break;
284                 }
285
286                 /* Allow NUMBER_OR_NAME if base is 16. */
287                 if (token != NUMBER &&
288                     (base != 16 || token != NUMBER_OR_NAME)) {
289                         parse_warn("expecting numeric value.");
290                         skip_to_semi(cfile);
291                         free(lbufp);
292                         return (NULL);
293                 }
294                 /*
295                  * If we can, convert the number now; otherwise, build a
296                  * linked list of all the numbers.
297                  */
298                 if (s) {
299                         convert_num(s, val, base, size);
300                         s += size / 8;
301                 } else {
302                         valsize = strlen(val) + 1;
303                         t = malloc(valsize);
304                         if (!t)
305                                 error("no temp space for number.");
306                         memcpy(t, val, valsize);
307                         c = cons(t, c);
308                 }
309         } while (++count != *max);
310
311         /* If we had to cons up a list, convert it now. */
312         if (c) {
313                 free(lbufp);
314                 bufp = malloc(count * size / 8);
315                 if (!bufp)
316                         error("can't allocate space for numeric aggregate.");
317                 s = bufp + count - size / 8;
318                 *max = count;
319         }
320         while (c) {
321                 pair cdr = c->cdr;
322                 convert_num(s, (char *)c->car, base, size);
323                 s -= size / 8;
324                 /* Free up temp space. */
325                 free(c->car);
326                 free(c);
327                 c = cdr;
328         }
329         return (bufp);
330 }
331
332 void
333 convert_num(unsigned char *buf, char *str, unsigned base, int size)
334 {
335         bool negative = false;
336         unsigned tval, max;
337         u_int32_t val = 0;
338         char *ptr = str;
339
340         if (*ptr == '-') {
341                 negative = true;
342                 ptr++;
343         }
344
345         /* If base wasn't specified, figure it out from the data. */
346         if (!base) {
347                 if (ptr[0] == '0') {
348                         if (ptr[1] == 'x') {
349                                 base = 16;
350                                 ptr += 2;
351                         } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
352                                 base = 8;
353                                 ptr += 1;
354                         } else
355                                 base = 10;
356                 } else
357                         base = 10;
358         }
359
360         do {
361                 tval = *ptr++;
362                 /* XXX assumes ASCII... */
363                 if (tval >= 'a')
364                         tval = tval - 'a' + 10;
365                 else if (tval >= 'A')
366                         tval = tval - 'A' + 10;
367                 else if (tval >= '0')
368                         tval -= '0';
369                 else {
370                         warning("Bogus number: %s.", str);
371                         break;
372                 }
373                 if (tval >= base) {
374                         warning("Bogus number: %s: digit %d not in base %d",
375                             str, tval, base);
376                         break;
377                 }
378                 val = val * base + tval;
379         } while (*ptr);
380
381         if (negative)
382                 max = (1 << (size - 1));
383         else
384                 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
385         if (val > max) {
386                 switch (base) {
387                 case 8:
388                         warning("value %s%o exceeds max (%d) for precision.",
389                             negative ? "-" : "", val, max);
390                         break;
391                 case 16:
392                         warning("value %s%x exceeds max (%d) for precision.",
393                             negative ? "-" : "", val, max);
394                         break;
395                 default:
396                         warning("value %s%u exceeds max (%d) for precision.",
397                             negative ? "-" : "", val, max);
398                         break;
399                 }
400         }
401
402         if (negative)
403                 switch (size) {
404                 case 8:
405                         *buf = -(unsigned long)val;
406                         break;
407                 case 16:
408                         putShort(buf, -(unsigned long)val);
409                         break;
410                 case 32:
411                         putLong(buf, -(unsigned long)val);
412                         break;
413                 default:
414                         warning("Unexpected integer size: %d", size);
415                         break;
416                 }
417         else
418                 switch (size) {
419                 case 8:
420                         *buf = (u_int8_t)val;
421                         break;
422                 case 16:
423                         putUShort(buf, (u_int16_t)val);
424                         break;
425                 case 32:
426                         putULong(buf, val);
427                         break;
428                 default:
429                         warning("Unexpected integer size: %d", size);
430                         break;
431                 }
432 }
433
434 /*
435  * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
436  *              NUMBER COLON NUMBER COLON NUMBER SEMI
437  *
438  * Dates are always in GMT; first number is day of week; next is
439  * year/month/day; next is hours:minutes:seconds on a 24-hour
440  * clock.
441  */
442 time_t
443 parse_date(FILE *cfile)
444 {
445         static int months[11] = { 31, 59, 90, 120, 151, 181,
446             212, 243, 273, 304, 334 };
447         int guess, token;
448         struct tm tm;
449         char *val;
450
451         /* Day of week... */
452         token = next_token(&val, cfile);
453         if (token != NUMBER) {
454                 parse_warn("numeric day of week expected.");
455                 if (token != SEMI)
456                         skip_to_semi(cfile);
457                 return (0);
458         }
459         tm.tm_wday = atoi(val);
460
461         /* Year... */
462         token = next_token(&val, cfile);
463         if (token != NUMBER) {
464                 parse_warn("numeric year expected.");
465                 if (token != SEMI)
466                         skip_to_semi(cfile);
467                 return (0);
468         }
469         tm.tm_year = atoi(val);
470         if (tm.tm_year > 1900)
471                 tm.tm_year -= 1900;
472
473         /* Slash separating year from month... */
474         token = next_token(&val, cfile);
475         if (token != SLASH) {
476                 parse_warn("expected slash separating year from month.");
477                 if (token != SEMI)
478                         skip_to_semi(cfile);
479                 return (0);
480         }
481
482         /* Month... */
483         token = next_token(&val, cfile);
484         if (token != NUMBER) {
485                 parse_warn("numeric month expected.");
486                 if (token != SEMI)
487                         skip_to_semi(cfile);
488                 return (0);
489         }
490         tm.tm_mon = atoi(val) - 1;
491
492         /* Slash separating month from day... */
493         token = next_token(&val, cfile);
494         if (token != SLASH) {
495                 parse_warn("expected slash separating month from day.");
496                 if (token != SEMI)
497                         skip_to_semi(cfile);
498                 return (0);
499         }
500
501         /* Month... */
502         token = next_token(&val, cfile);
503         if (token != NUMBER) {
504                 parse_warn("numeric day of month expected.");
505                 if (token != SEMI)
506                         skip_to_semi(cfile);
507                 return (0);
508         }
509         tm.tm_mday = atoi(val);
510
511         /* Hour... */
512         token = next_token(&val, cfile);
513         if (token != NUMBER) {
514                 parse_warn("numeric hour expected.");
515                 if (token != SEMI)
516                         skip_to_semi(cfile);
517                 return (0);
518         }
519         tm.tm_hour = atoi(val);
520
521         /* Colon separating hour from minute... */
522         token = next_token(&val, cfile);
523         if (token != COLON) {
524                 parse_warn("expected colon separating hour from minute.");
525                 if (token != SEMI)
526                         skip_to_semi(cfile);
527                 return (0);
528         }
529
530         /* Minute... */
531         token = next_token(&val, cfile);
532         if (token != NUMBER) {
533                 parse_warn("numeric minute expected.");
534                 if (token != SEMI)
535                         skip_to_semi(cfile);
536                 return (0);
537         }
538         tm.tm_min = atoi(val);
539
540         /* Colon separating minute from second... */
541         token = next_token(&val, cfile);
542         if (token != COLON) {
543                 parse_warn("expected colon separating hour from minute.");
544                 if (token != SEMI)
545                         skip_to_semi(cfile);
546                 return (0);
547         }
548
549         /* Minute... */
550         token = next_token(&val, cfile);
551         if (token != NUMBER) {
552                 parse_warn("numeric minute expected.");
553                 if (token != SEMI)
554                         skip_to_semi(cfile);
555                 return (0);
556         }
557         tm.tm_sec = atoi(val);
558         tm.tm_isdst = 0;
559
560         /* XXX: We assume that mktime does not use tm_yday. */
561         tm.tm_yday = 0;
562
563         /* Make sure the date ends in a semicolon... */
564         token = next_token(&val, cfile);
565         if (token != SEMI) {
566                 parse_warn("semicolon expected.");
567                 skip_to_semi(cfile);
568                 return (0);
569         }
570
571         /* Guess the time value... */
572         guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */
573                     (tm.tm_year - 69) / 4 +     /* Leap days since '70 */
574                     (tm.tm_mon                  /* Days in months this year */
575                     ? months[tm.tm_mon - 1]
576                     : 0) +
577                     (tm.tm_mon > 1 &&           /* Leap day this year */
578                     !((tm.tm_year - 72) & 3)) +
579                     tm.tm_mday - 1) * 24) +     /* Day of month */
580                     tm.tm_hour) * 60) +
581                     tm.tm_min) * 60) + tm.tm_sec;
582
583         /*
584          * This guess could be wrong because of leap seconds or other
585          * weirdness we don't know about that the system does.   For
586          * now, we're just going to accept the guess, but at some point
587          * it might be nice to do a successive approximation here to get
588          * an exact value.   Even if the error is small, if the server
589          * is restarted frequently (and thus the lease database is
590          * reread), the error could accumulate into something
591          * significant.
592          */
593         return (guess);
594 }