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