1 /* $OpenBSD: parser.c,v 1.8 2009/08/08 00:16:49 gilles 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 #include <sys/tree.h> 25 #include <sys/param.h> 26 27 #include <net/if.h> 28 #include <netinet/in.h> 29 #include <arpa/inet.h> 30 31 #include <err.h> 32 #include <errno.h> 33 #include <limits.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <event.h> 38 39 #include <openssl/ssl.h> 40 41 #include "smtpd.h" 42 43 #include "parser.h" 44 45 enum token_type { 46 NOTOKEN, 47 ENDTOKEN, 48 KEYWORD, 49 VARIABLE 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_pause[]; 62 static const struct token t_resume[]; 63 static const struct token t_schedule[]; 64 65 static const struct token t_main[] = { 66 {KEYWORD, "show", NONE, t_show}, 67 {KEYWORD, "monitor", MONITOR, NULL}, 68 {KEYWORD, "pause", NONE, t_pause}, 69 {KEYWORD, "reload", RELOAD, NULL}, 70 {KEYWORD, "resume", NONE, t_resume}, 71 {KEYWORD, "stop", SHUTDOWN, NULL}, 72 {KEYWORD, "schedule", SCHEDULE, t_schedule}, 73 {ENDTOKEN, "", NONE, NULL} 74 }; 75 76 static const struct token t_show[] = { 77 {KEYWORD, "queue", SHOW_QUEUE, NULL}, 78 {KEYWORD, "runqueue", SHOW_RUNQUEUE, NULL}, 79 {KEYWORD, "stats", SHOW_STATS, NULL}, 80 {ENDTOKEN, "", NONE, NULL} 81 }; 82 83 static const struct token t_pause[] = { 84 {KEYWORD, "local", PAUSE_MDA, NULL}, 85 {KEYWORD, "outgoing", PAUSE_MTA, NULL}, 86 {KEYWORD, "incoming", PAUSE_SMTP, NULL}, 87 {ENDTOKEN, "", NONE, NULL} 88 }; 89 90 static const struct token t_resume[] = { 91 {KEYWORD, "local", RESUME_MDA, NULL}, 92 {KEYWORD, "outgoing", RESUME_MTA, NULL}, 93 {KEYWORD, "incoming", RESUME_SMTP, NULL}, 94 {ENDTOKEN, "", NONE, NULL} 95 }; 96 97 static const struct token t_schedule[] = { 98 {VARIABLE, "message id/uid", SCHEDULE, NULL}, 99 {ENDTOKEN, "", NONE, NULL} 100 }; 101 102 static struct parse_result res; 103 104 struct parse_result * 105 parse(int argc, char *argv[]) 106 { 107 const struct token *table = t_main; 108 const struct token *match; 109 110 bzero(&res, sizeof(res)); 111 112 while (argc >= 0) { 113 if ((match = match_token(argv[0], table)) == NULL) { 114 fprintf(stderr, "valid commands/args:\n"); 115 show_valid_args(table); 116 return (NULL); 117 } 118 119 argc--; 120 argv++; 121 122 if (match->type == NOTOKEN || match->next == NULL) 123 break; 124 125 table = match->next; 126 } 127 128 if (argc > 0) { 129 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 130 return (NULL); 131 } 132 133 return (&res); 134 } 135 136 const struct token * 137 match_token(const char *word, const struct token table[]) 138 { 139 u_int i, match; 140 const struct token *t = NULL; 141 142 match = 0; 143 144 for (i = 0; table[i].type != ENDTOKEN; i++) { 145 switch (table[i].type) { 146 case NOTOKEN: 147 if (word == NULL || strlen(word) == 0) { 148 match++; 149 t = &table[i]; 150 } 151 break; 152 case KEYWORD: 153 if (word != NULL && strncmp(word, table[i].keyword, 154 strlen(word)) == 0) { 155 match++; 156 t = &table[i]; 157 if (t->value) 158 res.action = t->value; 159 } 160 break; 161 case VARIABLE: 162 if (word != NULL && strlen(word) != 0) { 163 match++; 164 t = &table[i]; 165 if (t->value) { 166 res.action = t->value; 167 res.data = word; 168 } 169 } 170 break; 171 case ENDTOKEN: 172 break; 173 } 174 } 175 176 if (match != 1) { 177 if (word == NULL) 178 fprintf(stderr, "missing argument:\n"); 179 else if (match > 1) 180 fprintf(stderr, "ambiguous argument: %s\n", word); 181 else if (match < 1) 182 fprintf(stderr, "unknown argument: %s\n", word); 183 return (NULL); 184 } 185 186 return (t); 187 } 188 189 void 190 show_valid_args(const struct token table[]) 191 { 192 int i; 193 194 for (i = 0; table[i].type != ENDTOKEN; i++) { 195 switch (table[i].type) { 196 case NOTOKEN: 197 fprintf(stderr, " <cr>\n"); 198 break; 199 case KEYWORD: 200 fprintf(stderr, " %s\n", table[i].keyword); 201 break; 202 case VARIABLE: 203 fprintf(stderr, " %s\n", table[i].keyword); 204 break; 205 case ENDTOKEN: 206 break; 207 } 208 } 209 } 210