1*b75abfe5Sflorian /* $OpenBSD: parser.c,v 1.4 2018/10/15 11:30:01 florian Exp $ */
2208af722Sclaudio
3208af722Sclaudio /*
4208af722Sclaudio * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
5208af722Sclaudio * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6208af722Sclaudio *
7208af722Sclaudio * Permission to use, copy, modify, and distribute this software for any
8208af722Sclaudio * purpose with or without fee is hereby granted, provided that the above
9208af722Sclaudio * copyright notice and this permission notice appear in all copies.
10208af722Sclaudio *
11208af722Sclaudio * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12208af722Sclaudio * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13208af722Sclaudio * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14208af722Sclaudio * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15208af722Sclaudio * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16208af722Sclaudio * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17208af722Sclaudio * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18208af722Sclaudio */
19208af722Sclaudio
20208af722Sclaudio #include <sys/types.h>
21208af722Sclaudio #include <sys/queue.h>
22208af722Sclaudio #include <sys/socket.h>
23208af722Sclaudio #include <sys/uio.h>
24208af722Sclaudio #include <netinet/in.h>
25208af722Sclaudio #include <err.h>
26208af722Sclaudio #include <errno.h>
27208af722Sclaudio #include <event.h>
28208af722Sclaudio #include <limits.h>
29208af722Sclaudio #include <netdb.h>
30208af722Sclaudio #include <stdio.h>
31208af722Sclaudio #include <stdlib.h>
32208af722Sclaudio #include <string.h>
33208af722Sclaudio #include <unistd.h>
34208af722Sclaudio
35208af722Sclaudio #include "iscsid.h"
36208af722Sclaudio #include "iscsictl.h"
37208af722Sclaudio
38208af722Sclaudio enum token_type {
39208af722Sclaudio NOTOKEN,
40208af722Sclaudio ENDTOKEN,
41208af722Sclaudio KEYWORD,
42208af722Sclaudio ADDRESS,
43d9ff5473Sclaudio NAME,
44208af722Sclaudio FLAG
45208af722Sclaudio };
46208af722Sclaudio
47208af722Sclaudio struct token {
48208af722Sclaudio enum token_type type;
49208af722Sclaudio const char *keyword;
50208af722Sclaudio int value;
51208af722Sclaudio const struct token *next;
52208af722Sclaudio };
53208af722Sclaudio
54208af722Sclaudio static const struct token t_main[];
55208af722Sclaudio static const struct token t_show[];
56208af722Sclaudio static const struct token t_log[];
57208af722Sclaudio static const struct token t_discovery[];
58d9ff5473Sclaudio static const struct token t_session[];
59cf4672e7Sclaudio static const struct token t_vscsi[];
60208af722Sclaudio
61208af722Sclaudio static const struct token t_main[] = {
62208af722Sclaudio {KEYWORD, "reload", RELOAD, NULL},
63208af722Sclaudio {KEYWORD, "discover", DISCOVERY, t_discovery},
64d9ff5473Sclaudio {KEYWORD, "show", SHOW_SUM, t_show},
65208af722Sclaudio {KEYWORD, "log", NONE, t_log},
66208af722Sclaudio {ENDTOKEN, "", NONE, NULL}
67208af722Sclaudio };
68208af722Sclaudio
69208af722Sclaudio static const struct token t_show[] = {
70208af722Sclaudio {NOTOKEN, "", NONE, NULL},
71208af722Sclaudio {KEYWORD, "summary", SHOW_SUM, NULL},
72d9ff5473Sclaudio {KEYWORD, "session", SHOW_SESS, t_session},
73cf4672e7Sclaudio {KEYWORD, "vscsi", NONE, t_vscsi},
74208af722Sclaudio {ENDTOKEN, "", NONE, NULL}
75208af722Sclaudio };
76208af722Sclaudio
77208af722Sclaudio static const struct token t_log[] = {
78208af722Sclaudio {KEYWORD, "verbose", LOG_VERBOSE, NULL},
79208af722Sclaudio {KEYWORD, "brief", LOG_BRIEF, NULL},
80208af722Sclaudio {ENDTOKEN, "", NONE, NULL}
81208af722Sclaudio };
82208af722Sclaudio
83208af722Sclaudio static const struct token t_discovery[] = {
84208af722Sclaudio {ADDRESS, "", NONE, NULL},
85208af722Sclaudio {ENDTOKEN, "", NONE, NULL}
86208af722Sclaudio };
87208af722Sclaudio
88d9ff5473Sclaudio static const struct token t_session[] = {
89d9ff5473Sclaudio {NAME, "", NONE, NULL},
90d9ff5473Sclaudio {ENDTOKEN, "", NONE, NULL}
91d9ff5473Sclaudio };
92d9ff5473Sclaudio
93cf4672e7Sclaudio static const struct token t_vscsi[] = {
94cf4672e7Sclaudio {KEYWORD, "stats", SHOW_VSCSI_STATS, NULL},
95cf4672e7Sclaudio {ENDTOKEN, "", NONE, NULL}
96cf4672e7Sclaudio };
97cf4672e7Sclaudio
98208af722Sclaudio static struct parse_result res;
99208af722Sclaudio
100208af722Sclaudio struct parse_result *
parse(int argc,char * argv[])101208af722Sclaudio parse(int argc, char *argv[])
102208af722Sclaudio {
103208af722Sclaudio const struct token *table = t_main;
104208af722Sclaudio const struct token *match;
105208af722Sclaudio
106208af722Sclaudio bzero(&res, sizeof(res));
107208af722Sclaudio
108208af722Sclaudio while (argc >= 0) {
109208af722Sclaudio if ((match = match_token(argv[0], table)) == NULL) {
110208af722Sclaudio fprintf(stderr, "valid commands/args:\n");
111208af722Sclaudio show_valid_args(table);
112208af722Sclaudio return (NULL);
113208af722Sclaudio }
114208af722Sclaudio
115208af722Sclaudio argc--;
116208af722Sclaudio argv++;
117208af722Sclaudio
118208af722Sclaudio if (match->type == NOTOKEN || match->next == NULL)
119208af722Sclaudio break;
120208af722Sclaudio
121208af722Sclaudio table = match->next;
122208af722Sclaudio }
123208af722Sclaudio
124208af722Sclaudio if (argc > 0) {
125208af722Sclaudio fprintf(stderr, "superfluous argument: %s\n", argv[0]);
126208af722Sclaudio return (NULL);
127208af722Sclaudio }
128208af722Sclaudio
129208af722Sclaudio return (&res);
130208af722Sclaudio }
131208af722Sclaudio
132208af722Sclaudio const struct token *
match_token(const char * word,const struct token * table)133208af722Sclaudio match_token(const char *word, const struct token *table)
134208af722Sclaudio {
135208af722Sclaudio u_int i, match;
136208af722Sclaudio const struct token *t = NULL;
137208af722Sclaudio
138208af722Sclaudio match = 0;
139208af722Sclaudio
140208af722Sclaudio for (i = 0; table[i].type != ENDTOKEN; i++) {
141208af722Sclaudio switch (table[i].type) {
142208af722Sclaudio case NOTOKEN:
143208af722Sclaudio if (word == NULL || strlen(word) == 0) {
144208af722Sclaudio match++;
145208af722Sclaudio t = &table[i];
146208af722Sclaudio }
147208af722Sclaudio break;
148208af722Sclaudio case KEYWORD:
149208af722Sclaudio if (word != NULL && strncmp(word, table[i].keyword,
150208af722Sclaudio strlen(word)) == 0) {
151208af722Sclaudio match++;
152208af722Sclaudio t = &table[i];
153208af722Sclaudio if (t->value)
154208af722Sclaudio res.action = t->value;
155208af722Sclaudio }
156208af722Sclaudio break;
157208af722Sclaudio case FLAG:
158208af722Sclaudio if (word != NULL && strncmp(word, table[i].keyword,
159208af722Sclaudio strlen(word)) == 0) {
160208af722Sclaudio match++;
161208af722Sclaudio t = &table[i];
162208af722Sclaudio res.flags |= t->value;
163208af722Sclaudio }
164208af722Sclaudio break;
165208af722Sclaudio case ADDRESS:
166208af722Sclaudio if (!parse_addr(word, &res.addr)) {
167208af722Sclaudio match++;
168208af722Sclaudio t = &table[i];
169208af722Sclaudio if (t->value)
170208af722Sclaudio res.action = t->value;
171208af722Sclaudio }
172208af722Sclaudio break;
173d9ff5473Sclaudio case NAME:
174d9ff5473Sclaudio if (word != NULL && strlen(word) > 0) {
175d9ff5473Sclaudio if (strlcpy(res.name, word, sizeof(res.name)) >=
176d9ff5473Sclaudio sizeof(res.name))
177d9ff5473Sclaudio errx(1, "name too long");
178d9ff5473Sclaudio match++;
179d9ff5473Sclaudio t = &table[i];
180d9ff5473Sclaudio }
181d9ff5473Sclaudio break;
182208af722Sclaudio case ENDTOKEN:
183208af722Sclaudio break;
184208af722Sclaudio }
185208af722Sclaudio }
186208af722Sclaudio
187208af722Sclaudio if (match != 1) {
188208af722Sclaudio if (word == NULL)
189208af722Sclaudio fprintf(stderr, "missing argument:\n");
190208af722Sclaudio else if (match > 1)
191208af722Sclaudio fprintf(stderr, "ambiguous argument: %s\n", word);
192208af722Sclaudio else if (match < 1)
193208af722Sclaudio fprintf(stderr, "unknown argument: %s\n", word);
194208af722Sclaudio return (NULL);
195208af722Sclaudio }
196208af722Sclaudio
197208af722Sclaudio return (t);
198208af722Sclaudio }
199208af722Sclaudio
200208af722Sclaudio void
show_valid_args(const struct token * table)201208af722Sclaudio show_valid_args(const struct token *table)
202208af722Sclaudio {
203208af722Sclaudio int i;
204208af722Sclaudio
205208af722Sclaudio for (i = 0; table[i].type != ENDTOKEN; i++) {
206208af722Sclaudio switch (table[i].type) {
207208af722Sclaudio case NOTOKEN:
208208af722Sclaudio fprintf(stderr, " <cr>\n");
209208af722Sclaudio break;
210208af722Sclaudio case KEYWORD:
211208af722Sclaudio case FLAG:
212208af722Sclaudio fprintf(stderr, " %s\n", table[i].keyword);
213208af722Sclaudio break;
214208af722Sclaudio case ADDRESS:
215208af722Sclaudio fprintf(stderr, " <address>\n");
216208af722Sclaudio break;
217d9ff5473Sclaudio case NAME:
218d9ff5473Sclaudio fprintf(stderr, " <name>\n");
219d9ff5473Sclaudio break;
220208af722Sclaudio case ENDTOKEN:
221208af722Sclaudio break;
222208af722Sclaudio }
223208af722Sclaudio }
224208af722Sclaudio }
225208af722Sclaudio
226208af722Sclaudio int
parse_addr(const char * word,struct sockaddr_storage * sa)227208af722Sclaudio parse_addr(const char *word, struct sockaddr_storage *sa)
228208af722Sclaudio {
229208af722Sclaudio struct addrinfo hints, *addrs;
230208af722Sclaudio int rv;
231208af722Sclaudio
232208af722Sclaudio bzero(&hints, sizeof(hints));
233208af722Sclaudio hints.ai_family = PF_UNSPEC;
234208af722Sclaudio hints.ai_socktype = SOCK_STREAM;
235208af722Sclaudio hints.ai_protocol = IPPROTO_TCP;
236208af722Sclaudio
237208af722Sclaudio if ((rv = getaddrinfo(word, "iscsi", &hints, &addrs)) == 0) {
238208af722Sclaudio bcopy(addrs->ai_addr, sa, addrs->ai_addrlen);
239208af722Sclaudio freeaddrinfo(addrs);
240208af722Sclaudio return (0);
241208af722Sclaudio }
242208af722Sclaudio
243208af722Sclaudio errx(1, "parse_host: %s", gai_strerror(rv));
244208af722Sclaudio }
245