summaryrefslogtreecommitdiff
path: root/sbin/dhclient/parse.c
blob: fd0f7cbadc97c03b188c766ff48824655c8d77eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*	$OpenBSD: parse.c,v 1.83 2019/07/22 17:20:06 krw Exp $	*/

/* Common parser code for dhcpd and dhclient. */

/*
 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of The Internet Software Consortium nor the names
 *    of its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This software has been written for the Internet Software Consortium
 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
 * Enterprises.  To learn more about the Internet Software Consortium,
 * see ``http://www.vix.com/isc''.  To learn more about Vixie
 * Enterprises, see ``http://www.vix.com''.
 */

#include <sys/queue.h>
#include <sys/socket.h>

#include <net/if.h>

#include <netinet/in.h>
#include <netinet/if_ether.h>

#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <vis.h>

#include "dhcp.h"
#include "dhcpd.h"
#include "dhctoken.h"
#include "log.h"

/*
 * Skip to the semicolon ending the current statement.   If we encounter
 * braces, the matching closing brace terminates the statement.   If we
 * encounter a right brace but haven't encountered a left brace, return
 * leaving the brace in the token buffer for the caller.   If we see a
 * semicolon and haven't seen a left brace, return.   This lets us skip
 * over:
 *
 *	statement;
 *	statement foo bar { }
 *	statement foo bar { statement { } }
 *	statement}
 *
 *	...et cetera.
 */
void
skip_to_semi(FILE *cfile)
{
	int		 token;
	int		 brace_count = 0;

	do {
		token = peek_token(NULL, cfile);
		if (token == '}') {
			if (brace_count > 0) {
				if (--brace_count == 0) {
					token = next_token(NULL, cfile);
					return;
				}
			} else
				return;
		} else if (token == '{') {
			brace_count++;
		} else if (token == ';' && brace_count == 0) {
			token = next_token(NULL, cfile);
			return;
		}
		token = next_token(NULL, cfile);
	} while (token != EOF);
}

int
parse_semi(FILE *cfile)
{
	int token;

	token = next_token(NULL, cfile);
	if (token == ';')
		return 1;

	parse_warn("expecting semicolon.");
	skip_to_semi(cfile);

	return 0;
}

int
parse_string(FILE *cfile, char **string)
{
	static char	 unvisbuf[1500];
	char		*val;
	int		 i, token;

	token = next_token(&val, cfile);
	if (token == TOK_STRING) {
		i = strnunvis(unvisbuf, val, sizeof(unvisbuf));
		if (i >= 0) {
			*string = strdup(unvisbuf);
			if (*string == NULL)
				fatal("strdup(unvisbuf)");
			return 1;
		}
	}

	parse_warn("expecting string.");

	if (token != ';')
		skip_to_semi(cfile);

	return 0;
}

/*
 * cidr :== ip-address "/" bit-count
 * ip-address :== NUMBER [ DOT NUMBER [ DOT NUMBER [ DOT NUMBER ] ] ]
 * bit-count :== 0..32
 */
int
parse_cidr(FILE *cfile, unsigned char *cidr)
{
	uint8_t		 buf[5];
	const char	*errstr;
	char		*val;
	long long	 numval;
	unsigned int	 i;
	int		 token;

	memset(buf, 0, sizeof(buf));
	i = 1;	/* Last four octets hold subnet, first octet the # of bits. */
	do {
		token = next_token(&val, cfile);
		if (i == 0)
			numval = strtonum(val, 0, 32, &errstr);
		else
			numval = strtonum(val, 0, UINT8_MAX, &errstr);
		if (errstr != NULL)
			break;
		buf[i++] = numval;
		if (i == 1) {
			memcpy(cidr, buf, sizeof(buf)); /* XXX Need cidr_t */
			return 1;
		}
		token = next_token(NULL, cfile);
		if (token == '/')
			i = 0;
		if (i == sizeof(buf))
			break;
	} while (token == '.' || token == '/');

	parse_warn("expecting IPv4 CIDR block.");

	if (token != ';')
		skip_to_semi(cfile);

	return 0;
}

int
parse_ip_addr(FILE *cfile, struct in_addr *addr)
{
	struct in_addr	 buf;
	const char	*errstr;
	char		*val;
	long long	 numval;
	unsigned int	 i;
	int		 token;

	i = 0;
	do {
		token = next_token(&val, cfile);
		numval = strtonum(val, 0, UINT8_MAX, &errstr);
		if (errstr != NULL)
			break;
		((uint8_t *)&buf)[i++] = numval;
		if (i == sizeof(buf)) {
			memcpy(addr, &buf, sizeof(*addr));
			return 1;
		}
		token = next_token(NULL, cfile);
	} while (token == '.');

	parse_warn("expecting IPv4 address.");

	if (token != ';')
		skip_to_semi(cfile);

	return 0;
}

int
parse_boolean(FILE *cfile, unsigned char *buf)
{
	char	*val;
	int	 token;

	token = next_token(&val, cfile);
	if (is_identifier(token) != 0) {
		if (strcasecmp(val, "true") == 0 ||
		    strcasecmp(val, "on") == 0) {
			buf[0] = 1;
			return 1;
		}
		if (strcasecmp(val, "false") == 0 ||
		    strcasecmp(val, "off") == 0) {
			buf[0] = 0;
			return 1;
		}
	}

	parse_warn("expecting boolean.");
	if (token != ';')
		skip_to_semi(cfile);

	return 0;
}

int
parse_number(FILE *cfile, long long *number, long long low, long long high)
{
	const char	*errstr;
	char		*val, *msg;
	int		 rslt, token;
	long long	 numval;

	token = next_token(&val, cfile);

	numval = strtonum(val, low, high, &errstr);
	if (errstr == NULL) {
		*number = numval;
		return 1;
	}

	rslt = asprintf(&msg, "expecting integer between %lld and %lld", low,
	    high);
	if (rslt != -1) {
		parse_warn(msg);
		free(msg);
	}

	if (token != ';')
		skip_to_semi(cfile);

	return 0;
}

void
parse_warn(char *msg)
{
	static char	 spaces[81];
	unsigned int	 i;

	log_warnx("%s: %s line %d: %s", log_procname, tlname, lexline, msg);
	log_warnx("%s: %s", log_procname, token_line);
	if ((unsigned int)lexchar < sizeof(spaces)) {
		memset(spaces, 0, sizeof(spaces));
		for (i = 0; (int)i < lexchar - 1; i++) {
			if (token_line[i] == '\t')
				spaces[i] = '\t';
			else
				spaces[i] = ' ';
		}
		log_warnx("%s: %s^", log_procname, spaces);
	}
}