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