Debian Codegraph @

src/cli/cli_tool.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2015 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <popt.h>
25
26 #include "common/common.h"
27 #include "cli/cli_tool.h"
28 #include "authselect.h"
29
30 bool enable_trace;
31 bool enable_warning;
32 bool enable_debug;
33
34 void
35 print_debug(void *pvt,
36 enum authselect_debug level,
37 const char *file,
38 unsigned long line,
39 const char *function,
40 const char *msg)
41 {
42 const char *category = "unknown";
43
44 switch (level) {
45 case AUTHSELECT_INFO:
46 if (!enable_trace) {
47 return;
48 }
49 category = "info";
50 break;
51 case AUTHSELECT_WARNING:
52 if (!enable_warning) {
53 return;
54 }
55 category = "warn";
56 break;
57 case AUTHSELECT_ERROR:
58 category = "error";
59 break;
60 }
61
62 if (!enable_debug) {
63 fprintf(stderr, "[%s] %s\n", category, msg);
64 return;
65 }
66
67 fprintf(stderr, "[%s] [%s] %s\n", category, function, msg);
68 }
69
70 static void cli_tool_print_common_opts(int min_len)
71 {
72 fprintf(stderr, _("Common options:\n"));
73 fprintf(stderr, " %-*s\t %s\n", min_len, "--debug",
74 _("Print error messages"));
75 fprintf(stderr, " %-*s\t %s\n", min_len, "--trace",
76 _("Print trace messages"));
77 fprintf(stderr, " %-*s\t %s\n", min_len, "--warn",
78 _("Print warning messages"));
79 fprintf(stderr, "\n");
80 fprintf(stderr, _("Help options:\n"));
81 fprintf(stderr, " %-*s\t %s\n", min_len, "-?, --help",
82 _("Show this for a command"));
83 fprintf(stderr, " %-*s\t %s\n", min_len, "--usage",
84 _("Show brief usage message for a command"));
85 }
86
87 static struct poptOption *cli_tool_common_opts_table(void)
88 {
89 static struct poptOption options[] = {
90 {"debug", '\0', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, NULL, 'd', NULL, NULL },
91 {"trace", '\0', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, NULL, 't', NULL, NULL },
92 {"warn", '\0', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, NULL, 'w', NULL, NULL },
93 POPT_TABLEEND
94 };
95
96 options[0].descrip = _("Print error messages");
97 options[1].descrip = _("Print trace messages");
98 options[2].descrip = _("Print warning messages");
99
100 return options;
101 }
102
103 static void cli_tool_common_opts(int *argc, const char **argv)
104 {
105 poptContext pc;
106 struct poptOption *options;
107 int orig_argc = *argc;
108 int opt;
109
110 options = cli_tool_common_opts_table();
111
112 pc = poptGetContext(argv[0], orig_argc, argv, options, 0);
113 while ((opt = poptGetNextOpt(pc)) != -1) {
114 switch (opt) {
115 case 'd':
116 enable_debug = true;
117 break;
118 case 't':
119 enable_trace = true;
120 break;
121 case 'w':
122 enable_warning = true;
123 break;
124 default:
125 break;
126 }
127 }
128
129 set_debug_fn(print_debug, NULL);
130 authselect_set_debug_fn(print_debug, NULL);
131
132 /* Strip common options from arguments. We will discard_const here,
133 * since it is not worth the trouble to convert it back and forth. */
134 *argc = poptStrippedArgv(pc, orig_argc, (char **)argv);
135
136 poptFreeContext(pc);
137 }
138
139 static bool cli_tool_is_delimiter(struct cli_route_cmd *command)
140 {
141 if (command->command != NULL && command->command[0] == '\0') {
142 return true;
143 }
144
145 return false;
146 }
147
148 static size_t cli_tool_max_length(struct cli_route_cmd *commands)
149 {
150 size_t max = 0;
151 size_t len;
152 int i;
153
154 for (i = 0; commands[i].command != NULL; i++) {
155 if (cli_tool_is_delimiter(&commands[i])) {
156 continue;
157 }
158
159 len = strlen(commands[i].command);
160 if (max < len) {
161 max = len;
162 }
163 }
164
165 return max;
166 }
167
168 void cli_tool_usage(const char *tool_name, struct cli_route_cmd *commands)
169 {
170 int min_len;
171 int i;
172
173 fprintf(stderr, _("Usage:\n%s COMMAND COMMAND-ARGS\n\n"), tool_name);
174 fprintf(stderr, _("Available commands:\n"));
175
176 min_len = cli_tool_max_length(commands);
177
178 for (i = 0; commands[i].command != NULL; i++) {
179 if (commands[i].flags & CLI_CMD_HIDDEN) {
180 continue;
181 }
182
183 if (cli_tool_is_delimiter(&commands[i])) {
184 fprintf(stderr, "\n%s\n", commands[i].description);
185 continue;
186 }
187
188 if (commands[i].description == NULL) {
189 fprintf(stderr, "- %40s\n", commands[i].command);
190 } else {
191 fprintf(stderr, "- %-*s\t %s\n",
192 min_len, commands[i].command, commands[i].description);
193 }
194 }
195
196 fprintf(stderr, _("\n"));
197 cli_tool_print_common_opts(min_len);
198 }
199
200 static bool
201 cli_tool_check_root_access(uint32_t flags,
202 const char *command,
203 int argc,
204 const char **argv)
205 {
206 uid_t uid;
207 int i;
208
209 if (!(flags & CLI_CMD_REQUIRE_ROOT)) {
210 return true;
211 }
212
213 /* If help was requested, we allow the access. */
214 for (i = 0; i < argc; i++) {
215 if (strcmp(argv[i], "-?") == 0) {
216 return true;
217 }
218
219 if (strcmp(argv[i], "--help") == 0) {
220 return true;
221 }
222
223 if (strcmp(argv[i], "--usage") == 0) {
224 return true;
225 }
226 }
227
228 uid = getuid();
229 if (uid != 0) {
230 fprintf(stderr, _("Authselect command '%s' can only be run as root!\n"),
231 command);
232 return false;
233 }
234
235 return true;
236 }
237
238 errno_t cli_tool_route(int argc, const char **argv,
239 struct cli_route_cmd *commands)
240 {
241 struct cli_cmdline cmdline;
242 const char *cmd;
243 bool bret;
244 int i;
245
246 if (commands == NULL) {
247 ERROR("Bug: commands can't be NULL!\n");
248 return EINVAL;
249 }
250
251 if (argc < 2) {
252 cli_tool_usage(argv[0], commands);
253 return EINVAL;
254 }
255
256 cmd = argv[1];
257 for (i = 0; commands[i].command != NULL; i++) {
258 if (cli_tool_is_delimiter(&commands[i])) {
259 continue;
260 }
261
262 if (strcmp(commands[i].command, cmd) == 0) {
263 bret = cli_tool_check_root_access(commands[i].flags,
264 commands[i].command,
265 argc, argv);
266 if (!bret) {
267 return EACCES;
268 }
269
270 cmdline.exec = argv[0];
271 cmdline.command = argv[1];
272 cmdline.argc = argc - 2;
273 cmdline.argv = argv + 2;
274
275 return commands[i].fn(&cmdline);
276 }
277 }
278
279 cli_tool_usage(argv[0], commands);
280
281 return EINVAL;
282 }
283
284 static struct poptOption *nonnull_popt_table(struct poptOption *options)
285 {
286 static struct poptOption empty[] = {
287 POPT_TABLEEND
288 };
289
290 if (options == NULL) {
291 return empty;
292 }
293
294 return options;
295 }
296
297 errno_t cli_tool_popt_ex(struct cli_cmdline *cmdline,
298 struct poptOption *options,
299 enum cli_tool_opt require_option,
300 cli_popt_fn popt_fn,
301 void *popt_fn_pvt,
302 const char *fopt_name,
303 const char *fopt_help,
304 char **_fopt,
305 bool allow_more_free_opts,
306 bool *_opt_set)
307 {
308 struct poptOption opts_table[] = {
309 {NULL, '\0', POPT_ARG_INCLUDE_TABLE, nonnull_popt_table(options), \
310 0, _("Command options:"), NULL },
311 {NULL, '\0', POPT_ARG_INCLUDE_TABLE, cli_tool_common_opts_table(), \
312 0, _("Common options:"), NULL },
313 POPT_AUTOHELP
314 POPT_TABLEEND
315 };
316 const char *fopt;
317 char *help;
318 poptContext pc;
319 bool opt_set;
320 int ret;
321
322 /* Set output parameter _fopt to NULL value if present. */
323 if (_fopt != NULL) {
324 *_fopt = NULL;
325 }
326
327 /* Create help option string. We always need to append command name since
328 * we use POPT_CONTEXT_KEEP_FIRST. */
329 if (fopt_name == NULL) {
330 help = format("%s %s %s", cmdline->exec,
331 cmdline->command, _("[OPTIONS...]"));
332 } else {
333 help = format("%s %s %s %s", cmdline->exec,
334 cmdline->command, fopt_name, _("[OPTIONS...]"));
335 }
336 if (help == NULL) {
337 ERROR("Out of memory!");
338 return ENOMEM;
339 }
340
341 /* Create popt context. This function is supposed to be called on
342 * command argv which does not contain executable (argv[0]), therefore
343 * we need to use KEEP_FIRST that ensures argv[0] is also processed. */
344 pc = poptGetContext(cmdline->exec, cmdline->argc, cmdline->argv,
345 opts_table, POPT_CONTEXT_KEEP_FIRST);
346
347 poptSetOtherOptionHelp(pc, help);
348
349 /* Parse options. Invoke custom function if provided. If no parsing
350 * function is provided, print error on unknown option. */
351 while ((ret = poptGetNextOpt(pc)) != -1) {
352 if (popt_fn != NULL) {
353 ret = popt_fn(pc, ret, popt_fn_pvt);
354 if (ret != EOK) {
355 goto done;
356 }
357 } else {
358 fprintf(stderr, _("Invalid option %s: %s\n\n"),
359 poptBadOption(pc, 0), poptStrerror(ret));
360 poptPrintHelp(pc, stderr, 0);
361 ret = EINVAL;
362 goto done;
363 }
364 }
365
366 /* Parse free option which is always required if requested. */
367 fopt = poptGetArg(pc);
368 if (_fopt != NULL) {
369 if (fopt == NULL) {
370 fprintf(stderr, _("Missing option: %s\n\n"), fopt_help);
371 poptPrintHelp(pc, stderr, 0);
372 ret = EINVAL;
373 goto done;
374 }
375
376 if (!allow_more_free_opts) {
377 /* No more arguments expected.
378 * If something follows it is an error. */
379 if (poptGetArg(pc)) {
380 fprintf(stderr, _("Only one free argument is expected!\n\n"));
381 poptPrintHelp(pc, stderr, 0);
382 ret = EINVAL;
383 goto done;
384 }
385 }
386
387 *_fopt = strdup(fopt);
388 if (*_fopt == NULL) {
389 ERROR("Out of memory!");
390 ret = ENOMEM;
391 goto done;
392 }
393 } else if (_fopt == NULL && fopt != NULL) {
394 /* Unexpected free argument. */
395 fprintf(stderr, _("Unexpected parameter: %s\n\n"), fopt);
396 poptPrintHelp(pc, stderr, 0);
397 ret = EINVAL;
398 goto done;
399 }
400
401 opt_set = true;
402 if ((_fopt != NULL && cmdline->argc < 2) || cmdline->argc < 1) {
403 opt_set = false;
404
405 /* If at least one option is required and not provided, print error. */
406 if (require_option == CLI_TOOL_OPT_REQUIRED) {
407 fprintf(stderr, _("At least one option is required!\n\n"));
408 poptPrintHelp(pc, stderr, 0);
409 ret = EINVAL;
410 goto done;
411 }
412 }
413
414 if (_opt_set != NULL) {
415 *_opt_set = opt_set;
416 }
417
418 ret = EOK;
419
420 done:
421 poptFreeContext(pc);
422 free(help);
423 if (ret != EOK && _fopt != NULL) {
424 free(*_fopt);
425 *_fopt = NULL;
426 }
427
428 return ret;
429 }
430
431 errno_t cli_tool_popt(struct cli_cmdline *cmdline,
432 struct poptOption *options,
433 enum cli_tool_opt require_option,
434 cli_popt_fn popt_fn,
435 void *popt_fn_pvt)
436 {
437 return cli_tool_popt_ex(cmdline, options, require_option,
438 popt_fn, popt_fn_pvt, NULL, NULL,
439 NULL, false, NULL);
440 }
441
442 int cli_tool_main(int argc, const char **argv,
443 struct cli_route_cmd *commands,
444 void *pvt)
445 {
446 errno_t ret;
447
448 cli_tool_common_opts(&argc, argv);
449
450 ret = cli_tool_route(argc, argv, commands);
451
452 switch (ret) {
453 case EOK:
454 return 0;
455 case ENOENT:
456 return 2;
457 case EBADF:
458 return 3;
459 case EEXIST:
460 return 4;
461 case EACCES:
462 return 5;
463 case ENODEV:
464 return 6;
465 }
466
467 /* Generic error. */
468 return 1;
469 }