1 /* $OpenBSD: parser.c,v 1.14 2019/05/26 09:27:09 remi 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 AREA 45 }; 46 47 struct token { 48 enum token_type type; 49 const char *keyword; 50 int value; 51 const struct token *next; 52 }; 53 54 static const struct token t_main[]; 55 static const struct token t_fib[]; 56 static const struct token t_show[]; 57 static const struct token t_show_iface[]; 58 static const struct token t_show_db[]; 59 static const struct token t_show_area[]; 60 static const struct token t_show_nbr[]; 61 static const struct token t_show_rib[]; 62 static const struct token t_show_fib[]; 63 static const struct token t_log[]; 64 65 static const struct token t_main[] = { 66 {KEYWORD, "reload", RELOAD, NULL}, 67 {KEYWORD, "fib", FIB, t_fib}, 68 {KEYWORD, "show", SHOW, t_show}, 69 {KEYWORD, "log", NONE, t_log}, 70 {ENDTOKEN, "", NONE, NULL} 71 }; 72 73 static const struct token t_fib[] = { 74 { KEYWORD, "couple", FIB_COUPLE, NULL}, 75 { KEYWORD, "decouple", FIB_DECOUPLE, NULL}, 76 { ENDTOKEN, "", NONE, NULL} 77 }; 78 79 static const struct token t_show[] = { 80 {NOTOKEN, "", NONE, NULL}, 81 {KEYWORD, "interfaces", SHOW_IFACE, t_show_iface}, 82 {KEYWORD, "database", SHOW_DB, t_show_db}, 83 {KEYWORD, "neighbor", SHOW_NBR, t_show_nbr}, 84 {KEYWORD, "rib", SHOW_RIB, t_show_rib}, 85 {KEYWORD, "fib", SHOW_FIB, t_show_fib}, 86 {KEYWORD, "summary", SHOW_SUM, NULL}, 87 {ENDTOKEN, "", NONE, NULL} 88 }; 89 90 static const struct token t_show_iface[] = { 91 {NOTOKEN, "", NONE, NULL}, 92 {KEYWORD, "detail", SHOW_IFACE_DTAIL, NULL}, 93 {IFNAME, "", SHOW_IFACE_DTAIL, NULL}, 94 {ENDTOKEN, "", NONE, NULL} 95 }; 96 97 static const struct token t_show_db[] = { 98 {NOTOKEN, "", NONE, NULL}, 99 {KEYWORD, "area", SHOW_DBBYAREA, t_show_area}, 100 {KEYWORD, "asbr", SHOW_DBASBR, NULL}, 101 {KEYWORD, "external", SHOW_DBEXT, NULL}, 102 {KEYWORD, "link", SHOW_DBLINK, NULL}, 103 {KEYWORD, "network", SHOW_DBNET, NULL}, 104 {KEYWORD, "router", SHOW_DBRTR, NULL}, 105 {KEYWORD, "intra", SHOW_DBINTRA, NULL}, 106 {KEYWORD, "self-originated", SHOW_DBSELF, NULL}, 107 {KEYWORD, "summary", SHOW_DBSUM, NULL}, 108 {ENDTOKEN, "", NONE, NULL} 109 }; 110 111 static const struct token t_show_area[] = { 112 {AREA, "", NONE, NULL}, 113 {ENDTOKEN, "", NONE, NULL} 114 }; 115 116 static const struct token t_show_nbr[] = { 117 {NOTOKEN, "", NONE, NULL}, 118 {KEYWORD, "detail", SHOW_NBR_DTAIL, NULL}, 119 {ENDTOKEN, "", NONE, NULL} 120 }; 121 122 static const struct token t_show_rib[] = { 123 {NOTOKEN, "", NONE, NULL}, 124 {KEYWORD, "detail", SHOW_RIB_DTAIL, NULL}, 125 {ENDTOKEN, "", NONE, NULL} 126 }; 127 128 static const struct token t_show_fib[] = { 129 {NOTOKEN, "", NONE, NULL}, 130 {FLAG, "connected", F_CONNECTED, t_show_fib}, 131 {FLAG, "static", F_STATIC, t_show_fib}, 132 {FLAG, "ospf", F_OSPFD_INSERTED, t_show_fib}, 133 {ADDRESS, "", NONE, NULL}, 134 {ENDTOKEN, "", NONE, NULL} 135 }; 136 137 static const struct token t_log[] = { 138 {KEYWORD, "verbose", LOG_VERBOSE, NULL}, 139 {KEYWORD, "brief", LOG_BRIEF, NULL}, 140 {ENDTOKEN, "", NONE, NULL} 141 }; 142 143 static const struct token *match_token(const char *, const struct token *, 144 struct parse_result *); 145 static void show_valid_args(const struct token *); 146 147 struct parse_result * 148 parse(int argc, char *argv[]) 149 { 150 static struct parse_result res; 151 const struct token *table = t_main; 152 const struct token *match; 153 154 bzero(&res, sizeof(res)); 155 156 while (argc >= 0) { 157 if ((match = match_token(argv[0], table, &res)) == NULL) { 158 fprintf(stderr, "valid commands/args:\n"); 159 show_valid_args(table); 160 return (NULL); 161 } 162 163 argc--; 164 argv++; 165 166 if (match->type == NOTOKEN || match->next == NULL) 167 break; 168 169 table = match->next; 170 } 171 172 if (argc > 0) { 173 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 174 return (NULL); 175 } 176 177 return (&res); 178 } 179 180 static const struct token * 181 match_token(const char *word, const struct token *table, 182 struct parse_result *res) 183 { 184 u_int i, match; 185 const struct token *t = NULL; 186 187 match = 0; 188 189 for (i = 0; table[i].type != ENDTOKEN; i++) { 190 switch (table[i].type) { 191 case NOTOKEN: 192 if (word == NULL || strlen(word) == 0) { 193 match++; 194 t = &table[i]; 195 } 196 break; 197 case KEYWORD: 198 if (word != NULL && strncmp(word, table[i].keyword, 199 strlen(word)) == 0) { 200 match++; 201 t = &table[i]; 202 if (t->value) 203 res->action = t->value; 204 } 205 break; 206 case FLAG: 207 if (word != NULL && strncmp(word, table[i].keyword, 208 strlen(word)) == 0) { 209 match++; 210 t = &table[i]; 211 res->flags |= t->value; 212 } 213 break; 214 case ADDRESS: 215 if (parse_addr(word, &res->addr)) { 216 match++; 217 t = &table[i]; 218 if (t->value) 219 res->action = t->value; 220 } 221 break; 222 case AREA: 223 if (parse_area(word, &res->area)) { 224 match++; 225 t = &table[i]; 226 if (t->value) 227 res->action = t->value; 228 } 229 break; 230 case PREFIX: 231 if (parse_prefix(word, &res->addr, &res->prefixlen)) { 232 match++; 233 t = &table[i]; 234 if (t->value) 235 res->action = t->value; 236 } 237 break; 238 case IFNAME: 239 if (!match && word != NULL && strlen(word) > 0) { 240 if (strlcpy(res->ifname, word, 241 sizeof(res->ifname)) >= 242 sizeof(res->ifname)) 243 err(1, "interface name too long"); 244 match++; 245 t = &table[i]; 246 if (t->value) 247 res->action = t->value; 248 } 249 break; 250 251 case ENDTOKEN: 252 break; 253 } 254 } 255 256 if (match != 1) { 257 if (word == NULL) 258 fprintf(stderr, "missing argument:\n"); 259 else if (match > 1) 260 fprintf(stderr, "ambiguous argument: %s\n", word); 261 else if (match < 1) 262 fprintf(stderr, "unknown argument: %s\n", word); 263 return (NULL); 264 } 265 266 return (t); 267 } 268 269 static void 270 show_valid_args(const struct token *table) 271 { 272 int i; 273 274 for (i = 0; table[i].type != ENDTOKEN; i++) { 275 switch (table[i].type) { 276 case NOTOKEN: 277 fprintf(stderr, " <cr>\n"); 278 break; 279 case KEYWORD: 280 case FLAG: 281 fprintf(stderr, " %s\n", table[i].keyword); 282 break; 283 case ADDRESS: 284 fprintf(stderr, " <address>\n"); 285 break; 286 case AREA: 287 fprintf(stderr, " <area>\n"); 288 break; 289 case PREFIX: 290 fprintf(stderr, " <address>[/<len>]\n"); 291 break; 292 case IFNAME: 293 fprintf(stderr, " <interface>\n"); 294 break; 295 case ENDTOKEN: 296 break; 297 } 298 } 299 } 300 301 /* XXX shared with parse.y should be merged */ 302 int 303 parse_addr(const char *word, struct in6_addr *addr) 304 { 305 struct addrinfo hints, *r; 306 307 if (word == NULL) 308 return (0); 309 310 bzero(addr, sizeof(struct in6_addr)); 311 bzero(&hints, sizeof(hints)); 312 hints.ai_family = AF_INET6; 313 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 314 hints.ai_flags = AI_NUMERICHOST; 315 if (getaddrinfo(word, "0", &hints, &r) == 0) { 316 *addr = ((struct sockaddr_in6 *)r->ai_addr)->sin6_addr; 317 /* XXX address scope !!! */ 318 /* ((struct sockaddr_in6 *)r->ai_addr)->sin6_scope_id */ 319 freeaddrinfo(r); 320 return (1); 321 } 322 return (0); 323 } 324 325 int 326 parse_area(const char *word, struct in_addr *addr) 327 { 328 struct in_addr ina; 329 const char *errstr; 330 331 if (word == NULL) 332 return (0); 333 334 bzero(addr, sizeof(struct in_addr)); 335 bzero(&ina, sizeof(ina)); 336 337 if (inet_pton(AF_INET, word, &ina)) { 338 addr->s_addr = ina.s_addr; 339 return (1); 340 } 341 342 ina.s_addr = htonl(strtonum(word, 0, 0xffffffff, &errstr)); 343 if (errstr == NULL) { 344 addr->s_addr = ina.s_addr; 345 return (1); 346 } 347 return (0); 348 } 349 350 /* XXX shared with parse.y should be merged */ 351 int 352 parse_prefix(const char *word, struct in6_addr *addr, u_int8_t *prefixlen) 353 { 354 char *p, *ps; 355 const char *errstr; 356 int mask; 357 358 if (word == NULL) 359 return (0); 360 361 if ((p = strrchr(word, '/')) != NULL) { 362 mask = strtonum(p + 1, 0, 128, &errstr); 363 if (errstr) 364 errx(1, "invalid netmask: %s", errstr); 365 366 if ((ps = malloc(strlen(word) - strlen(p) + 1)) == NULL) 367 err(1, "parse_prefix: malloc"); 368 strlcpy(ps, word, strlen(word) - strlen(p) + 1); 369 370 if (parse_addr(ps, addr) == 0) { 371 free(ps); 372 return (0); 373 } 374 free(ps); 375 376 inet6applymask(addr, addr, mask); 377 *prefixlen = mask; 378 return (1); 379 } 380 *prefixlen = 128; 381 return (parse_addr(word, addr)); 382 } 383