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