1 /* $OpenBSD: parser.c,v 1.24 2010/09/04 21:31:04 tedu Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@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 <sys/queue.h> 24 25 #include <net/if.h> 26 #include <netinet/in.h> 27 #include <arpa/inet.h> 28 29 #include <err.h> 30 #include <errno.h> 31 #include <limits.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <event.h> 36 37 #include <openssl/ssl.h> 38 39 #include "relayd.h" 40 41 #include "parser.h" 42 43 enum token_type { 44 NOTOKEN, 45 ENDTOKEN, 46 HOSTID, 47 TABLEID, 48 RDRID, 49 KEYWORD 50 }; 51 52 struct token { 53 enum token_type type; 54 const char *keyword; 55 int value; 56 const struct token *next; 57 }; 58 59 static const struct token t_main[]; 60 static const struct token t_show[]; 61 static const struct token t_rdr[]; 62 static const struct token t_table[]; 63 static const struct token t_host[]; 64 static const struct token t_rdr_id[]; 65 static const struct token t_table_id[]; 66 static const struct token t_host_id[]; 67 static const struct token t_log[]; 68 69 static const struct token t_main[] = { 70 {KEYWORD, "monitor", MONITOR, NULL}, 71 {KEYWORD, "show", NONE, t_show}, 72 {KEYWORD, "poll", POLL, NULL}, 73 {KEYWORD, "reload", RELOAD, NULL}, 74 {KEYWORD, "stop", SHUTDOWN, NULL}, 75 {KEYWORD, "redirect", NONE, t_rdr}, 76 {KEYWORD, "table", NONE, t_table}, 77 {KEYWORD, "host", NONE, t_host}, 78 {KEYWORD, "log", NONE, t_log}, 79 {ENDTOKEN, "", NONE, NULL} 80 }; 81 82 static const struct token t_show[] = { 83 {KEYWORD, "summary", SHOW_SUM, NULL}, 84 {KEYWORD, "hosts", SHOW_HOSTS, NULL}, 85 {KEYWORD, "redirects", SHOW_RDRS, NULL}, 86 {KEYWORD, "relays", SHOW_RELAYS, NULL}, 87 {KEYWORD, "routers", SHOW_ROUTERS, NULL}, 88 {KEYWORD, "sessions", SHOW_SESSIONS, NULL}, 89 {ENDTOKEN, "", NONE, NULL} 90 }; 91 92 static const struct token t_rdr[] = { 93 {NOTOKEN, "", NONE, NULL}, 94 {KEYWORD, "disable", RDR_DISABLE, t_rdr_id}, 95 {KEYWORD, "enable", RDR_ENABLE, t_rdr_id}, 96 {ENDTOKEN, "", NONE, NULL} 97 }; 98 99 static const struct token t_table[] = { 100 {NOTOKEN, "", NONE, NULL}, 101 {KEYWORD, "disable", TABLE_DISABLE, t_table_id}, 102 {KEYWORD, "enable", TABLE_ENABLE, t_table_id}, 103 {ENDTOKEN, "", NONE, NULL} 104 }; 105 106 static const struct token t_host[] = { 107 {NOTOKEN, "", NONE, NULL}, 108 {KEYWORD, "disable", HOST_DISABLE, t_host_id}, 109 {KEYWORD, "enable", HOST_ENABLE, t_host_id}, 110 {ENDTOKEN, "", NONE, NULL} 111 }; 112 113 static const struct token t_rdr_id[] = { 114 {RDRID, "", NONE, NULL}, 115 {ENDTOKEN, "", NONE, NULL} 116 }; 117 118 static const struct token t_table_id[] = { 119 {TABLEID, "", NONE, NULL}, 120 {ENDTOKEN, "", NONE, NULL} 121 }; 122 123 static const struct token t_host_id[] = { 124 {HOSTID, "", NONE, NULL}, 125 {ENDTOKEN, "", NONE, NULL} 126 }; 127 128 static const struct token t_log[] = { 129 {KEYWORD, "verbose", LOG_VERBOSE, NULL}, 130 {KEYWORD, "brief", LOG_BRIEF, NULL}, 131 {ENDTOKEN, "", NONE, NULL} 132 }; 133 134 static const struct token *match_token(const char *, const struct token *, 135 struct parse_result *); 136 static void show_valid_args(const struct token *); 137 138 struct parse_result * 139 parse(int argc, char *argv[]) 140 { 141 static struct parse_result res; 142 const struct token *table = t_main; 143 const struct token *match; 144 145 bzero(&res, sizeof(res)); 146 147 while (argc >= 0) { 148 if ((match = match_token(argv[0], table, &res)) == NULL) { 149 fprintf(stderr, "valid commands/args:\n"); 150 show_valid_args(table); 151 return (NULL); 152 } 153 154 argc--; 155 argv++; 156 157 if (match->type == NOTOKEN || match->next == NULL) 158 break; 159 160 table = match->next; 161 } 162 163 if (argc > 0) { 164 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 165 return (NULL); 166 } 167 168 return (&res); 169 } 170 171 static const struct token * 172 match_token(const char *word, const struct token *table, 173 struct parse_result *res) 174 { 175 u_int i, match; 176 const struct token *t = NULL; 177 const char *errstr; 178 179 match = 0; 180 181 for (i = 0; table[i].type != ENDTOKEN; i++) { 182 switch (table[i].type) { 183 case NOTOKEN: 184 if (word == NULL || strlen(word) == 0) { 185 match++; 186 t = &table[i]; 187 } 188 break; 189 case KEYWORD: 190 if (word != NULL && strncmp(word, table[i].keyword, 191 strlen(word)) == 0) { 192 match++; 193 t = &table[i]; 194 if (t->value) 195 res->action = t->value; 196 } 197 break; 198 case HOSTID: 199 if (word == NULL) 200 break; 201 res->id.id = strtonum(word, 0, UINT_MAX, &errstr); 202 if (errstr) { 203 strlcpy(res->id.name, word, sizeof(res->id.name)); 204 res->id.id = EMPTY_ID; 205 } 206 t = &table[i]; 207 match++; 208 break; 209 case TABLEID: 210 if (word == NULL) 211 break; 212 res->id.id = strtonum(word, 0, UINT_MAX, &errstr); 213 if (errstr) { 214 strlcpy(res->id.name, word, sizeof(res->id.name)); 215 res->id.id = EMPTY_ID; 216 } 217 t = &table[i]; 218 match++; 219 break; 220 case RDRID: 221 if (word == NULL) 222 break; 223 res->id.id = strtonum(word, 0, UINT_MAX, &errstr); 224 if (errstr) { 225 strlcpy(res->id.name, word, sizeof(res->id.name)); 226 res->id.id = EMPTY_ID; 227 } 228 t = &table[i]; 229 match++; 230 break; 231 case ENDTOKEN: 232 break; 233 } 234 } 235 236 if (match != 1) { 237 if (word == NULL) 238 fprintf(stderr, "missing argument:\n"); 239 else if (match > 1) 240 fprintf(stderr, "ambiguous argument: %s\n", word); 241 else if (match < 1) 242 fprintf(stderr, "unknown argument: %s\n", word); 243 return (NULL); 244 } 245 246 return (t); 247 } 248 249 static void 250 show_valid_args(const struct token *table) 251 { 252 int i; 253 254 for (i = 0; table[i].type != ENDTOKEN; i++) { 255 switch (table[i].type) { 256 case NOTOKEN: 257 fprintf(stderr, " <cr>\n"); 258 break; 259 case KEYWORD: 260 fprintf(stderr, " %s\n", table[i].keyword); 261 break; 262 case RDRID: 263 fprintf(stderr, " <redirectid>\n"); 264 break; 265 case TABLEID: 266 fprintf(stderr, " <tableid>\n"); 267 break; 268 case HOSTID: 269 fprintf(stderr, " <hostid>\n"); 270 break; 271 case ENDTOKEN: 272 break; 273 } 274 } 275 } 276