1 |
|
|
/* $OpenBSD: parser.c,v 1.13 2014/11/17 21:53:55 tobias Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2004 Esben Norby <norby@openbsd.org> |
5 |
|
|
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
6 |
|
|
* |
7 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
8 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
9 |
|
|
* copyright notice and this permission notice appear in all copies. |
10 |
|
|
* |
11 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
14 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
16 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
17 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include <sys/types.h> |
21 |
|
|
#include <sys/socket.h> |
22 |
|
|
#include <netinet/in.h> |
23 |
|
|
#include <arpa/inet.h> |
24 |
|
|
#include <err.h> |
25 |
|
|
#include <errno.h> |
26 |
|
|
#include <limits.h> |
27 |
|
|
#include <netdb.h> |
28 |
|
|
#include <stdio.h> |
29 |
|
|
#include <stdlib.h> |
30 |
|
|
#include <string.h> |
31 |
|
|
|
32 |
|
|
#include "ospf6d.h" |
33 |
|
|
|
34 |
|
|
#include "parser.h" |
35 |
|
|
|
36 |
|
|
enum token_type { |
37 |
|
|
NOTOKEN, |
38 |
|
|
ENDTOKEN, |
39 |
|
|
KEYWORD, |
40 |
|
|
ADDRESS, |
41 |
|
|
FLAG, |
42 |
|
|
PREFIX, |
43 |
|
|
IFNAME |
44 |
|
|
}; |
45 |
|
|
|
46 |
|
|
struct token { |
47 |
|
|
enum token_type type; |
48 |
|
|
const char *keyword; |
49 |
|
|
int value; |
50 |
|
|
const struct token *next; |
51 |
|
|
}; |
52 |
|
|
|
53 |
|
|
static const struct token t_main[]; |
54 |
|
|
static const struct token t_fib[]; |
55 |
|
|
static const struct token t_show[]; |
56 |
|
|
static const struct token t_show_iface[]; |
57 |
|
|
static const struct token t_show_db[]; |
58 |
|
|
static const struct token t_show_area[]; |
59 |
|
|
static const struct token t_show_nbr[]; |
60 |
|
|
static const struct token t_show_rib[]; |
61 |
|
|
static const struct token t_show_fib[]; |
62 |
|
|
static const struct token t_log[]; |
63 |
|
|
|
64 |
|
|
static const struct token t_main[] = { |
65 |
|
|
{KEYWORD, "reload", RELOAD, NULL}, |
66 |
|
|
{KEYWORD, "fib", FIB, t_fib}, |
67 |
|
|
{KEYWORD, "show", SHOW, t_show}, |
68 |
|
|
{KEYWORD, "log", NONE, t_log}, |
69 |
|
|
{ENDTOKEN, "", NONE, NULL} |
70 |
|
|
}; |
71 |
|
|
|
72 |
|
|
static const struct token t_fib[] = { |
73 |
|
|
{ KEYWORD, "couple", FIB_COUPLE, NULL}, |
74 |
|
|
{ KEYWORD, "decouple", FIB_DECOUPLE, NULL}, |
75 |
|
|
{ ENDTOKEN, "", NONE, NULL} |
76 |
|
|
}; |
77 |
|
|
|
78 |
|
|
static const struct token t_show[] = { |
79 |
|
|
{NOTOKEN, "", NONE, NULL}, |
80 |
|
|
{KEYWORD, "interfaces", SHOW_IFACE, t_show_iface}, |
81 |
|
|
{KEYWORD, "database", SHOW_DB, t_show_db}, |
82 |
|
|
{KEYWORD, "neighbor", SHOW_NBR, t_show_nbr}, |
83 |
|
|
{KEYWORD, "rib", SHOW_RIB, t_show_rib}, |
84 |
|
|
{KEYWORD, "fib", SHOW_FIB, t_show_fib}, |
85 |
|
|
{KEYWORD, "summary", SHOW_SUM, NULL}, |
86 |
|
|
{ENDTOKEN, "", NONE, NULL} |
87 |
|
|
}; |
88 |
|
|
|
89 |
|
|
static const struct token t_show_iface[] = { |
90 |
|
|
{NOTOKEN, "", NONE, NULL}, |
91 |
|
|
{KEYWORD, "detail", SHOW_IFACE_DTAIL, NULL}, |
92 |
|
|
{IFNAME, "", SHOW_IFACE_DTAIL, NULL}, |
93 |
|
|
{ENDTOKEN, "", NONE, NULL} |
94 |
|
|
}; |
95 |
|
|
|
96 |
|
|
static const struct token t_show_db[] = { |
97 |
|
|
{NOTOKEN, "", NONE, NULL}, |
98 |
|
|
{KEYWORD, "area", SHOW_DBBYAREA, t_show_area}, |
99 |
|
|
{KEYWORD, "asbr", SHOW_DBASBR, NULL}, |
100 |
|
|
{KEYWORD, "external", SHOW_DBEXT, NULL}, |
101 |
|
|
{KEYWORD, "link", SHOW_DBLINK, NULL}, |
102 |
|
|
{KEYWORD, "network", SHOW_DBNET, NULL}, |
103 |
|
|
{KEYWORD, "router", SHOW_DBRTR, NULL}, |
104 |
|
|
{KEYWORD, "intra", SHOW_DBINTRA, NULL}, |
105 |
|
|
{KEYWORD, "self-originated", SHOW_DBSELF, NULL}, |
106 |
|
|
{KEYWORD, "summary", SHOW_DBSUM, NULL}, |
107 |
|
|
{ENDTOKEN, "", NONE, NULL} |
108 |
|
|
}; |
109 |
|
|
|
110 |
|
|
static const struct token t_show_area[] = { |
111 |
|
|
{ADDRESS, "", NONE, NULL}, |
112 |
|
|
{ENDTOKEN, "", NONE, NULL} |
113 |
|
|
}; |
114 |
|
|
|
115 |
|
|
static const struct token t_show_nbr[] = { |
116 |
|
|
{NOTOKEN, "", NONE, NULL}, |
117 |
|
|
{KEYWORD, "detail", SHOW_NBR_DTAIL, NULL}, |
118 |
|
|
{ENDTOKEN, "", NONE, NULL} |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
static const struct token t_show_rib[] = { |
122 |
|
|
{NOTOKEN, "", NONE, NULL}, |
123 |
|
|
{KEYWORD, "detail", SHOW_RIB_DTAIL, NULL}, |
124 |
|
|
{ENDTOKEN, "", NONE, NULL} |
125 |
|
|
}; |
126 |
|
|
|
127 |
|
|
static const struct token t_show_fib[] = { |
128 |
|
|
{NOTOKEN, "", NONE, NULL}, |
129 |
|
|
{FLAG, "connected", F_CONNECTED, t_show_fib}, |
130 |
|
|
{FLAG, "static", F_STATIC, t_show_fib}, |
131 |
|
|
{FLAG, "ospf", F_OSPFD_INSERTED, t_show_fib}, |
132 |
|
|
{ADDRESS, "", NONE, NULL}, |
133 |
|
|
{ENDTOKEN, "", NONE, NULL} |
134 |
|
|
}; |
135 |
|
|
|
136 |
|
|
static const struct token t_log[] = { |
137 |
|
|
{KEYWORD, "verbose", LOG_VERBOSE, NULL}, |
138 |
|
|
{KEYWORD, "brief", LOG_BRIEF, NULL}, |
139 |
|
|
{ENDTOKEN, "", NONE, NULL} |
140 |
|
|
}; |
141 |
|
|
|
142 |
|
|
static const struct token *match_token(const char *, const struct token *, |
143 |
|
|
struct parse_result *); |
144 |
|
|
static void show_valid_args(const struct token *); |
145 |
|
|
|
146 |
|
|
struct parse_result * |
147 |
|
|
parse(int argc, char *argv[]) |
148 |
|
|
{ |
149 |
|
|
static struct parse_result res; |
150 |
|
|
const struct token *table = t_main; |
151 |
|
|
const struct token *match; |
152 |
|
|
|
153 |
|
|
bzero(&res, sizeof(res)); |
154 |
|
|
|
155 |
|
|
while (argc >= 0) { |
156 |
|
|
if ((match = match_token(argv[0], table, &res)) == NULL) { |
157 |
|
|
fprintf(stderr, "valid commands/args:\n"); |
158 |
|
|
show_valid_args(table); |
159 |
|
|
return (NULL); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
argc--; |
163 |
|
|
argv++; |
164 |
|
|
|
165 |
|
|
if (match->type == NOTOKEN || match->next == NULL) |
166 |
|
|
break; |
167 |
|
|
|
168 |
|
|
table = match->next; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
if (argc > 0) { |
172 |
|
|
fprintf(stderr, "superfluous argument: %s\n", argv[0]); |
173 |
|
|
return (NULL); |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
return (&res); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
static const struct token * |
180 |
|
|
match_token(const char *word, const struct token *table, |
181 |
|
|
struct parse_result *res) |
182 |
|
|
{ |
183 |
|
|
u_int i, match; |
184 |
|
|
const struct token *t = NULL; |
185 |
|
|
|
186 |
|
|
match = 0; |
187 |
|
|
|
188 |
|
|
for (i = 0; table[i].type != ENDTOKEN; i++) { |
189 |
|
|
switch (table[i].type) { |
190 |
|
|
case NOTOKEN: |
191 |
|
|
if (word == NULL || strlen(word) == 0) { |
192 |
|
|
match++; |
193 |
|
|
t = &table[i]; |
194 |
|
|
} |
195 |
|
|
break; |
196 |
|
|
case KEYWORD: |
197 |
|
|
if (word != NULL && strncmp(word, table[i].keyword, |
198 |
|
|
strlen(word)) == 0) { |
199 |
|
|
match++; |
200 |
|
|
t = &table[i]; |
201 |
|
|
if (t->value) |
202 |
|
|
res->action = t->value; |
203 |
|
|
} |
204 |
|
|
break; |
205 |
|
|
case FLAG: |
206 |
|
|
if (word != NULL && strncmp(word, table[i].keyword, |
207 |
|
|
strlen(word)) == 0) { |
208 |
|
|
match++; |
209 |
|
|
t = &table[i]; |
210 |
|
|
res->flags |= t->value; |
211 |
|
|
} |
212 |
|
|
break; |
213 |
|
|
case ADDRESS: |
214 |
|
|
if (parse_addr(word, &res->addr)) { |
215 |
|
|
match++; |
216 |
|
|
t = &table[i]; |
217 |
|
|
if (t->value) |
218 |
|
|
res->action = t->value; |
219 |
|
|
} |
220 |
|
|
break; |
221 |
|
|
case PREFIX: |
222 |
|
|
if (parse_prefix(word, &res->addr, &res->prefixlen)) { |
223 |
|
|
match++; |
224 |
|
|
t = &table[i]; |
225 |
|
|
if (t->value) |
226 |
|
|
res->action = t->value; |
227 |
|
|
} |
228 |
|
|
break; |
229 |
|
|
case IFNAME: |
230 |
|
|
if (!match && word != NULL && strlen(word) > 0) { |
231 |
|
|
if (strlcpy(res->ifname, word, |
232 |
|
|
sizeof(res->ifname)) >= |
233 |
|
|
sizeof(res->ifname)) |
234 |
|
|
err(1, "interface name too long"); |
235 |
|
|
match++; |
236 |
|
|
t = &table[i]; |
237 |
|
|
if (t->value) |
238 |
|
|
res->action = t->value; |
239 |
|
|
} |
240 |
|
|
break; |
241 |
|
|
|
242 |
|
|
case ENDTOKEN: |
243 |
|
|
break; |
244 |
|
|
} |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
if (match != 1) { |
248 |
|
|
if (word == NULL) |
249 |
|
|
fprintf(stderr, "missing argument:\n"); |
250 |
|
|
else if (match > 1) |
251 |
|
|
fprintf(stderr, "ambiguous argument: %s\n", word); |
252 |
|
|
else if (match < 1) |
253 |
|
|
fprintf(stderr, "unknown argument: %s\n", word); |
254 |
|
|
return (NULL); |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
return (t); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
static void |
261 |
|
|
show_valid_args(const struct token *table) |
262 |
|
|
{ |
263 |
|
|
int i; |
264 |
|
|
|
265 |
|
|
for (i = 0; table[i].type != ENDTOKEN; i++) { |
266 |
|
|
switch (table[i].type) { |
267 |
|
|
case NOTOKEN: |
268 |
|
|
fprintf(stderr, " <cr>\n"); |
269 |
|
|
break; |
270 |
|
|
case KEYWORD: |
271 |
|
|
case FLAG: |
272 |
|
|
fprintf(stderr, " %s\n", table[i].keyword); |
273 |
|
|
break; |
274 |
|
|
case ADDRESS: |
275 |
|
|
fprintf(stderr, " <address>\n"); |
276 |
|
|
break; |
277 |
|
|
case PREFIX: |
278 |
|
|
fprintf(stderr, " <address>[/<len>]\n"); |
279 |
|
|
break; |
280 |
|
|
case IFNAME: |
281 |
|
|
fprintf(stderr, " <interface>\n"); |
282 |
|
|
break; |
283 |
|
|
case ENDTOKEN: |
284 |
|
|
break; |
285 |
|
|
} |
286 |
|
|
} |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
/* XXX shared with parse.y should be merged */ |
290 |
|
|
int |
291 |
|
|
parse_addr(const char *word, struct in6_addr *addr) |
292 |
|
|
{ |
293 |
|
|
struct addrinfo hints, *r; |
294 |
|
|
|
295 |
|
|
if (word == NULL) |
296 |
|
|
return (0); |
297 |
|
|
|
298 |
|
|
bzero(addr, sizeof(struct in6_addr)); |
299 |
|
|
bzero(&hints, sizeof(hints)); |
300 |
|
|
hints.ai_family = AF_INET6; |
301 |
|
|
hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
302 |
|
|
hints.ai_flags = AI_NUMERICHOST; |
303 |
|
|
if (getaddrinfo(word, "0", &hints, &r) == 0) { |
304 |
|
|
*addr = ((struct sockaddr_in6 *)r->ai_addr)->sin6_addr; |
305 |
|
|
/* XXX address scope !!! */ |
306 |
|
|
/* ((struct sockaddr_in6 *)r->ai_addr)->sin6_scope_id */ |
307 |
|
|
freeaddrinfo(r); |
308 |
|
|
return (1); |
309 |
|
|
} |
310 |
|
|
return (0); |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
/* XXX shared with parse.y should be merged */ |
314 |
|
|
int |
315 |
|
|
parse_prefix(const char *word, struct in6_addr *addr, u_int8_t *prefixlen) |
316 |
|
|
{ |
317 |
|
|
char *p, *ps; |
318 |
|
|
const char *errstr; |
319 |
|
|
int mask; |
320 |
|
|
|
321 |
|
|
if (word == NULL) |
322 |
|
|
return (0); |
323 |
|
|
|
324 |
|
|
if ((p = strrchr(word, '/')) != NULL) { |
325 |
|
|
mask = strtonum(p + 1, 0, 128, &errstr); |
326 |
|
|
if (errstr) |
327 |
|
|
errx(1, "invalid netmask: %s", errstr); |
328 |
|
|
|
329 |
|
|
if ((ps = malloc(strlen(word) - strlen(p) + 1)) == NULL) |
330 |
|
|
err(1, "parse_prefix: malloc"); |
331 |
|
|
strlcpy(ps, word, strlen(word) - strlen(p) + 1); |
332 |
|
|
|
333 |
|
|
if (parse_addr(ps, addr) == 0) { |
334 |
|
|
free(ps); |
335 |
|
|
return (0); |
336 |
|
|
} |
337 |
|
|
free(ps); |
338 |
|
|
|
339 |
|
|
inet6applymask(addr, addr, mask); |
340 |
|
|
*prefixlen = mask; |
341 |
|
|
return (1); |
342 |
|
|
} |
343 |
|
|
*prefixlen = 128; |
344 |
|
|
return (parse_addr(word, addr)); |
345 |
|
|
} |