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