]> git.proxmox.com Git - mirror_frr.git/blob - vtysh/vtysh_main.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / vtysh / vtysh_main.c
1 /* Virtual terminal interface shell.
2 * Copyright (C) 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include <sys/un.h>
24 #include <setjmp.h>
25 #include <sys/wait.h>
26 #include <pwd.h>
27 #include <sys/file.h>
28 #include <unistd.h>
29
30 /* readline carries some ancient definitions around */
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
33 #include <readline/readline.h>
34 #include <readline/history.h>
35 #pragma GCC diagnostic pop
36
37 /*
38 * The append_history function only appears in newer versions
39 * of the readline library it appears like. Since we don't
40 * need this just silently ignore the code on these
41 * ancient platforms.
42 */
43 #if !defined HAVE_APPEND_HISTORY
44 #define append_history(A, B)
45 #endif
46
47 #include <lib/version.h>
48 #include "getopt.h"
49 #include "command.h"
50 #include "memory.h"
51 #include "linklist.h"
52 #include "libfrr.h"
53 #include "ferr.h"
54 #include "lib_errors.h"
55
56 #include "vtysh/vtysh.h"
57 #include "vtysh/vtysh_user.h"
58
59 /* VTY shell program name. */
60 char *progname;
61
62 /* SUID mode */
63 static uid_t elevuid, realuid;
64 static gid_t elevgid, realgid;
65
66 #define VTYSH_CONFIG_NAME "vtysh.conf"
67 #define FRR_CONFIG_NAME "frr.conf"
68
69 /* Configuration file name and directory. */
70 static char vtysh_config[MAXPATHLEN * 3];
71 char frr_config[MAXPATHLEN * 3];
72 char vtydir[MAXPATHLEN];
73 static char history_file[MAXPATHLEN];
74
75 /* Flag for indicate executing child command. */
76 int execute_flag = 0;
77
78 /* Flag to indicate if in user/unprivileged mode. */
79 int user_mode;
80
81 /* Master of threads. */
82 struct thread_master *master;
83
84 /* Command logging */
85 FILE *logfile;
86
87 static void vtysh_rl_callback(char *line_read)
88 {
89 HIST_ENTRY *last;
90
91 rl_callback_handler_remove();
92
93 if (!line_read) {
94 vtysh_loop_exited = true;
95 return;
96 }
97
98 /* If the line has any text in it, save it on the history. But only if
99 * last command in history isn't the same one.
100 */
101 if (*line_read) {
102 using_history();
103 last = previous_history();
104 if (!last || strcmp(last->line, line_read) != 0) {
105 add_history(line_read);
106 append_history(1, history_file);
107 }
108 }
109
110 vtysh_execute(line_read);
111
112 if (!vtysh_loop_exited)
113 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback);
114
115 free(line_read);
116 }
117
118 /* SIGTSTP handler. This function care user's ^Z input. */
119 static void sigtstp(int sig)
120 {
121 rl_callback_handler_remove();
122
123 /* Execute "end" command. */
124 vtysh_execute("end");
125
126 if (!vtysh_loop_exited)
127 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback);
128
129 /* Initialize readline. */
130 rl_initialize();
131 printf("\n");
132 rl_forced_update_display();
133 }
134
135 /* SIGINT handler. This function care user's ^Z input. */
136 static void sigint(int sig)
137 {
138 /* Check this process is not child process. */
139 if (!execute_flag) {
140 rl_initialize();
141 printf("\n");
142 rl_forced_update_display();
143 }
144 }
145
146 /* Signale wrapper for vtysh. We don't use sigevent because
147 * vtysh doesn't use threads. TODO */
148 static void vtysh_signal_set(int signo, void (*func)(int))
149 {
150 struct sigaction sig;
151 struct sigaction osig;
152
153 sig.sa_handler = func;
154 sigemptyset(&sig.sa_mask);
155 sig.sa_flags = 0;
156 #ifdef SA_RESTART
157 sig.sa_flags |= SA_RESTART;
158 #endif /* SA_RESTART */
159
160 sigaction(signo, &sig, &osig);
161 }
162
163 /* Initialization of signal handles. */
164 static void vtysh_signal_init(void)
165 {
166 vtysh_signal_set(SIGINT, sigint);
167 vtysh_signal_set(SIGTSTP, sigtstp);
168 vtysh_signal_set(SIGPIPE, SIG_IGN);
169 }
170
171 /* Help information display. */
172 static void usage(int status)
173 {
174 if (status != 0)
175 fprintf(stderr, "Try `%s --help' for more information.\n",
176 progname);
177 else
178 printf("Usage : %s [OPTION...]\n\n"
179 "Integrated shell for FRR (version " FRR_VERSION
180 "). \n"
181 "Configured with:\n " FRR_CONFIG_ARGS
182 "\n\n"
183 "-b, --boot Execute boot startup configuration\n"
184 "-c, --command Execute argument as command\n"
185 "-d, --daemon Connect only to the specified daemon\n"
186 "-f, --inputfile Execute commands from specific file and exit\n"
187 "-E, --echo Echo prompt and command in -c mode\n"
188 "-C, --dryrun Check configuration for validity and exit\n"
189 "-m, --markfile Mark input file with context end\n"
190 " --vty_socket Override vty socket path\n"
191 " --config_dir Override config directory path\n"
192 "-N --pathspace Insert prefix into config & socket paths\n"
193 "-u --user Run as an unprivileged user\n"
194 "-w, --writeconfig Write integrated config (frr.conf) and exit\n"
195 "-H, --histfile Override history file\n"
196 "-h, --help Display this help and exit\n\n"
197 "Note that multiple commands may be executed from the command\n"
198 "line by passing multiple -c args, or by embedding linefeed\n"
199 "characters in one or more of the commands.\n\n"
200 "Report bugs to %s\n",
201 progname, FRR_BUG_ADDRESS);
202
203 exit(status);
204 }
205
206 /* VTY shell options, we use GNU getopt library. */
207 #define OPTION_VTYSOCK 1000
208 #define OPTION_CONFDIR 1001
209 struct option longopts[] = {
210 {"boot", no_argument, NULL, 'b'},
211 /* For compatibility with older zebra/quagga versions */
212 {"eval", required_argument, NULL, 'e'},
213 {"command", required_argument, NULL, 'c'},
214 {"daemon", required_argument, NULL, 'd'},
215 {"vty_socket", required_argument, NULL, OPTION_VTYSOCK},
216 {"config_dir", required_argument, NULL, OPTION_CONFDIR},
217 {"inputfile", required_argument, NULL, 'f'},
218 {"histfile", required_argument, NULL, 'H'},
219 {"echo", no_argument, NULL, 'E'},
220 {"dryrun", no_argument, NULL, 'C'},
221 {"help", no_argument, NULL, 'h'},
222 {"noerror", no_argument, NULL, 'n'},
223 {"mark", no_argument, NULL, 'm'},
224 {"writeconfig", no_argument, NULL, 'w'},
225 {"pathspace", required_argument, NULL, 'N'},
226 {"user", no_argument, NULL, 'u'},
227 {"timestamp", no_argument, NULL, 't'},
228 {0}};
229
230 bool vtysh_loop_exited;
231
232 static struct thread *vtysh_rl_read_thread;
233
234 static void vtysh_rl_read(struct thread *thread)
235 {
236 thread_add_read(master, vtysh_rl_read, NULL, STDIN_FILENO,
237 &vtysh_rl_read_thread);
238 rl_callback_read_char();
239 }
240
241 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
242 static void vtysh_rl_run(void)
243 {
244 struct thread thread;
245
246 master = thread_master_create(NULL);
247
248 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback);
249 thread_add_read(master, vtysh_rl_read, NULL, STDIN_FILENO,
250 &vtysh_rl_read_thread);
251
252 while (!vtysh_loop_exited && thread_fetch(master, &thread))
253 thread_call(&thread);
254
255 if (!vtysh_loop_exited)
256 rl_callback_handler_remove();
257
258 thread_master_free(master);
259 }
260
261 static void log_it(const char *line)
262 {
263 time_t t = time(NULL);
264 struct tm tmp;
265 const char *user = getenv("USER");
266 char tod[64];
267
268 localtime_r(&t, &tmp);
269 if (!user)
270 user = "boot";
271
272 strftime(tod, sizeof(tod), "%Y%m%d-%H:%M.%S", &tmp);
273
274 fprintf(logfile, "%s:%s %s\n", tod, user, line);
275 }
276
277 static int flock_fd;
278
279 static void vtysh_flock_config(const char *flock_file)
280 {
281 int count = 0;
282
283 flock_fd = open(flock_file, O_RDONLY, 0644);
284 if (flock_fd < 0) {
285 fprintf(stderr, "Unable to create lock file: %s, %s\n",
286 flock_file, safe_strerror(errno));
287 return;
288 }
289
290 while (count < 400 && (flock(flock_fd, LOCK_EX | LOCK_NB) < 0)) {
291 count++;
292 usleep(500000);
293 }
294
295 if (count >= 400)
296 fprintf(stderr,
297 "Flock of %s failed, continuing this may cause issues\n",
298 flock_file);
299 }
300
301 static void vtysh_unflock_config(void)
302 {
303 flock(flock_fd, LOCK_UN);
304 close(flock_fd);
305 }
306
307 void suid_on(void)
308 {
309 if (elevuid != realuid && seteuid(elevuid)) {
310 perror("seteuid(on)");
311 exit(1);
312 }
313 if (elevgid != realgid && setegid(elevgid)) {
314 perror("setegid(on)");
315 exit(1);
316 }
317 }
318
319 void suid_off(void)
320 {
321 if (elevuid != realuid && seteuid(realuid)) {
322 perror("seteuid(off)");
323 exit(1);
324 }
325 if (elevgid != realgid && setegid(realgid)) {
326 perror("setegid(off)");
327 exit(1);
328 }
329 }
330
331 /* VTY shell main routine. */
332 int main(int argc, char **argv, char **env)
333 {
334 char *p;
335 int opt;
336 int dryrun = 0;
337 int boot_flag = 0;
338 bool ts_flag = false;
339 const char *daemon_name = NULL;
340 const char *inputfile = NULL;
341 struct cmd_rec {
342 char *line;
343 struct cmd_rec *next;
344 } *cmd = NULL;
345 struct cmd_rec *tail = NULL;
346 int echo_command = 0;
347 int no_error = 0;
348 int markfile = 0;
349 int writeconfig = 0;
350 int ret = 0;
351 char *homedir = NULL;
352 int ditch_suid = 0;
353 char sysconfdir[MAXPATHLEN];
354 const char *pathspace_arg = NULL;
355 char pathspace[MAXPATHLEN] = "";
356 const char *histfile = NULL;
357 const char *histfile_env = getenv("VTYSH_HISTFILE");
358
359 /* SUID: drop down to calling user & go back up when needed */
360 elevuid = geteuid();
361 elevgid = getegid();
362 realuid = getuid();
363 realgid = getgid();
364 suid_off();
365
366 user_mode = 0; /* may be set in options processing */
367
368 /* Preserve name of myself. */
369 progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
370
371 strlcpy(sysconfdir, frr_sysconfdir, sizeof(sysconfdir));
372
373 frr_init_vtydir();
374 strlcpy(vtydir, frr_vtydir, sizeof(vtydir));
375
376 /* Option handling. */
377 while (1) {
378 opt = getopt_long(argc, argv, "be:c:d:nf:H:mEhCwN:ut", longopts,
379 0);
380
381 if (opt == EOF)
382 break;
383
384 switch (opt) {
385 case 0:
386 break;
387 case 'b':
388 boot_flag = 1;
389 break;
390 case 'e':
391 case 'c': {
392 struct cmd_rec *cr;
393 cr = XMALLOC(MTYPE_TMP, sizeof(*cr));
394 cr->line = optarg;
395 cr->next = NULL;
396 if (tail)
397 tail->next = cr;
398 else
399 cmd = cr;
400 tail = cr;
401 } break;
402 case OPTION_VTYSOCK:
403 ditch_suid = 1; /* option disables SUID */
404 strlcpy(vtydir, optarg, sizeof(vtydir));
405 break;
406 case OPTION_CONFDIR:
407 ditch_suid = 1; /* option disables SUID */
408 snprintf(sysconfdir, sizeof(sysconfdir), "%s/", optarg);
409 break;
410 case 'N':
411 if (strchr(optarg, '/') || strchr(optarg, '.')) {
412 fprintf(stderr,
413 "slashes or dots are not permitted in the --pathspace option.\n");
414 exit(1);
415 }
416 pathspace_arg = optarg;
417 snprintf(pathspace, sizeof(pathspace), "%s/", optarg);
418 break;
419 case 'd':
420 daemon_name = optarg;
421 break;
422 case 'f':
423 inputfile = optarg;
424 break;
425 case 'm':
426 markfile = 1;
427 break;
428 case 'n':
429 no_error = 1;
430 break;
431 case 'E':
432 echo_command = 1;
433 break;
434 case 'C':
435 dryrun = 1;
436 break;
437 case 'u':
438 user_mode = 1;
439 break;
440 case 't':
441 ts_flag = true;
442 break;
443 case 'w':
444 writeconfig = 1;
445 break;
446 case 'h':
447 usage(0);
448 break;
449 case 'H':
450 histfile = optarg;
451 break;
452 default:
453 usage(1);
454 break;
455 }
456 }
457
458 if (ditch_suid) {
459 elevuid = realuid;
460 elevgid = realgid;
461 }
462
463 if (markfile + writeconfig + dryrun + boot_flag > 1) {
464 fprintf(stderr,
465 "Invalid combination of arguments. Please specify at most one of:\n\t-b, -C, -m, -w\n");
466 return 1;
467 }
468 if (inputfile && (writeconfig || boot_flag)) {
469 fprintf(stderr,
470 "WARNING: Combinining the -f option with -b or -w is NOT SUPPORTED since its\nresults are inconsistent!\n");
471 }
472
473 snprintf(vtysh_config, sizeof(vtysh_config), "%s%s%s", sysconfdir,
474 pathspace, VTYSH_CONFIG_NAME);
475 snprintf(frr_config, sizeof(frr_config), "%s%s%s", sysconfdir,
476 pathspace, FRR_CONFIG_NAME);
477
478 if (pathspace_arg) {
479 strlcat(vtydir, "/", sizeof(vtydir));
480 strlcat(vtydir, pathspace_arg, sizeof(vtydir));
481 }
482
483 /* Initialize user input buffer. */
484 setlinebuf(stdout);
485
486 /* Signal and others. */
487 vtysh_signal_init();
488
489 /* Make vty structure and register commands. */
490 vtysh_init_vty();
491 vtysh_init_cmd();
492 vtysh_user_init();
493 vtysh_config_init();
494
495 vty_init_vtysh();
496
497 if (!user_mode) {
498 /* Read vtysh configuration file before connecting to daemons.
499 * (file may not be readable to calling user in SUID mode) */
500 suid_on();
501 vtysh_read_config(vtysh_config, dryrun);
502 suid_off();
503 }
504 /* Error code library system */
505 log_ref_init();
506 lib_error_init();
507
508 if (markfile) {
509 if (!inputfile) {
510 fprintf(stderr,
511 "-f option MUST be specified with -m option\n");
512 return 1;
513 }
514 return (vtysh_mark_file(inputfile));
515 }
516
517 /* Start execution only if not in dry-run mode */
518 if (dryrun && !cmd) {
519 if (inputfile) {
520 ret = vtysh_read_config(inputfile, dryrun);
521 } else {
522 ret = vtysh_read_config(frr_config, dryrun);
523 }
524
525 exit(ret);
526 }
527
528 if (dryrun && cmd && cmd->line) {
529 if (!user_mode)
530 vtysh_execute("enable");
531 while (cmd) {
532 struct cmd_rec *cr;
533 char *cmdnow = cmd->line, *next;
534 do {
535 next = strchr(cmdnow, '\n');
536 if (next)
537 *next++ = '\0';
538
539 if (echo_command)
540 printf("%s%s\n", vtysh_prompt(),
541 cmdnow);
542
543 ret = vtysh_execute_no_pager(cmdnow);
544 if (!no_error
545 && !(ret == CMD_SUCCESS
546 || ret == CMD_SUCCESS_DAEMON
547 || ret == CMD_WARNING))
548 exit(1);
549 } while ((cmdnow = next) != NULL);
550
551 cr = cmd;
552 cmd = cmd->next;
553 XFREE(MTYPE_TMP, cr);
554 }
555 exit(ret);
556 }
557
558 /* Ignore error messages */
559 if (no_error) {
560 if (freopen("/dev/null", "w", stdout) == NULL) {
561 fprintf(stderr,
562 "Exiting: Failed to duplicate stdout with -n option");
563 exit(1);
564 }
565 }
566
567 /* SUID: go back up elevated privs */
568 suid_on();
569
570 /* Make sure we pass authentication before proceeding. */
571 vtysh_auth();
572
573 /* Do not connect until we have passed authentication. */
574 if (vtysh_connect_all(daemon_name) <= 0) {
575 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
576 if (geteuid() != 0)
577 fprintf(stderr,
578 "Hint: if this seems wrong, try running me as a privileged user!\n");
579 if (no_error)
580 exit(0);
581 else
582 exit(1);
583 }
584
585 /* SUID: back down, don't need privs further on */
586 suid_off();
587
588 if (writeconfig) {
589 if (user_mode) {
590 fprintf(stderr,
591 "writeconfig cannot be used when running as an unprivileged user.\n");
592 if (no_error)
593 exit(0);
594 else
595 exit(1);
596 }
597 vtysh_execute("enable");
598 return vtysh_write_config_integrated();
599 }
600
601 if (inputfile) {
602 vtysh_flock_config(inputfile);
603 ret = vtysh_read_config(inputfile, dryrun);
604 vtysh_unflock_config();
605 exit(ret);
606 }
607
608 /*
609 * Setup history file for use by both -c and regular input
610 * If we can't find the home directory, then don't store
611 * the history information.
612 * VTYSH_HISTFILE is preferred over command line
613 * argument (-H/--histfile).
614 */
615 if (histfile_env) {
616 strlcpy(history_file, histfile_env, sizeof(history_file));
617 } else if (histfile) {
618 strlcpy(history_file, histfile, sizeof(history_file));
619 } else {
620 homedir = vtysh_get_home();
621 if (homedir)
622 snprintf(history_file, sizeof(history_file),
623 "%s/.history_frr", homedir);
624 }
625
626 if (strlen(history_file) > 0) {
627 if (read_history(history_file) != 0) {
628 int fp;
629
630 fp = open(history_file, O_CREAT | O_EXCL,
631 S_IRUSR | S_IWUSR);
632 if (fp != -1)
633 close(fp);
634
635 read_history(history_file);
636 }
637 }
638
639 if (getenv("VTYSH_LOG")) {
640 const char *logpath = getenv("VTYSH_LOG");
641
642 logfile = fopen(logpath, "a");
643 if (!logfile) {
644 fprintf(stderr, "Failed to open logfile (%s): %s\n",
645 logpath, strerror(errno));
646 exit(1);
647 }
648 }
649
650 /* If eval mode. */
651 if (cmd && cmd->line) {
652 /* Enter into enable node. */
653 if (!user_mode)
654 vtysh_execute("enable");
655
656 vtysh_add_timestamp = ts_flag;
657
658 while (cmd != NULL) {
659 char *eol;
660
661 while ((eol = strchr(cmd->line, '\n')) != NULL) {
662 *eol = '\0';
663
664 add_history(cmd->line);
665 append_history(1, history_file);
666
667 if (echo_command)
668 printf("%s%s\n", vtysh_prompt(),
669 cmd->line);
670
671 if (logfile)
672 log_it(cmd->line);
673
674 ret = vtysh_execute_no_pager(cmd->line);
675 if (!no_error
676 && !(ret == CMD_SUCCESS
677 || ret == CMD_SUCCESS_DAEMON
678 || ret == CMD_WARNING))
679 exit(1);
680
681 cmd->line = eol + 1;
682 }
683
684 add_history(cmd->line);
685 append_history(1, history_file);
686
687 if (echo_command)
688 printf("%s%s\n", vtysh_prompt(), cmd->line);
689
690 if (logfile)
691 log_it(cmd->line);
692
693 /*
694 * Parsing logic for regular commands will be different
695 * than for those commands requiring further
696 * processing, such as cli instructions terminating
697 * with question-mark character.
698 */
699 if (!vtysh_execute_command_questionmark(cmd->line))
700 ret = CMD_SUCCESS;
701 else
702 ret = vtysh_execute_no_pager(cmd->line);
703
704 if (!no_error
705 && !(ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON
706 || ret == CMD_WARNING))
707 exit(1);
708
709 {
710 struct cmd_rec *cr;
711 cr = cmd;
712 cmd = cmd->next;
713 XFREE(MTYPE_TMP, cr);
714 }
715 }
716
717 history_truncate_file(history_file, 1000);
718 exit(0);
719 }
720
721 /* Boot startup configuration file. */
722 if (boot_flag) {
723 vtysh_flock_config(frr_config);
724 ret = vtysh_read_config(frr_config, dryrun);
725 vtysh_unflock_config();
726 if (ret) {
727 fprintf(stderr,
728 "Configuration file[%s] processing failure: %d\n",
729 frr_config, ret);
730 if (no_error)
731 exit(0);
732 else
733 exit(ret);
734 } else
735 exit(0);
736 }
737
738 vtysh_readline_init();
739
740 vty_hello(vty);
741
742 /* Enter into enable node. */
743 if (!user_mode)
744 vtysh_execute("enable");
745
746 vtysh_add_timestamp = ts_flag;
747
748 /* Main command loop. */
749 vtysh_rl_run();
750
751 vtysh_uninit();
752
753 history_truncate_file(history_file, 1000);
754 printf("\n");
755
756 /* Rest in peace. */
757 exit(0);
758 }