]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh_main.c
grammar_sandbox: count ambiguous commands
[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 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include <sys/un.h>
25#include <setjmp.h>
26#include <sys/wait.h>
27#include <pwd.h>
e43716f6
DS
28#include <sys/file.h>
29#include <unistd.h>
718e3744 30
31#include <readline/readline.h>
32#include <readline/history.h>
33
5e4fa164 34#include <lib/version.h>
718e3744 35#include "getopt.h"
36#include "command.h"
f366ad31 37#include "memory.h"
4201dd11 38#include "linklist.h"
fc7948fa 39#include "memory_vty.h"
718e3744 40
41#include "vtysh/vtysh.h"
42#include "vtysh/vtysh_user.h"
b094d260 43
718e3744 44/* VTY shell program name. */
45char *progname;
46
67e29abc 47/* Configuration file name and directory. */
ce2e9ec3 48static char vtysh_config_always[MAXPATHLEN] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
a0b4b67e 49static char quagga_config_default[MAXPATHLEN] = SYSCONFDIR FRR_DEFAULT_CONFIG;
cb947ba3 50char *quagga_config = quagga_config_default;
3f4ab7f9 51char history_file[MAXPATHLEN];
718e3744 52
718e3744 53/* Flag for indicate executing child command. */
54int execute_flag = 0;
55
87d79a9f
MW
56/* VTY Socket prefix */
57char * vty_sock_path = NULL;
58
718e3744 59/* For sigsetjmp() & siglongjmp(). */
60static sigjmp_buf jmpbuf;
61
62/* Flag for avoid recursive siglongjmp() call. */
63static int jmpflag = 0;
64
65/* A static variable for holding the line. */
66static char *line_read;
67
68/* Master of threads. */
69struct thread_master *master;
b094d260 70
57fb9748
SH
71/* Command logging */
72FILE *logfile;
73
718e3744 74/* SIGTSTP handler. This function care user's ^Z input. */
c0e8c16f 75static void
718e3744 76sigtstp (int sig)
77{
78 /* Execute "end" command. */
79 vtysh_execute ("end");
80
81 /* Initialize readline. */
82 rl_initialize ();
83 printf ("\n");
84
85 /* Check jmpflag for duplicate siglongjmp(). */
86 if (! jmpflag)
87 return;
88
89 jmpflag = 0;
90
91 /* Back to main command loop. */
92 siglongjmp (jmpbuf, 1);
93}
94
95/* SIGINT handler. This function care user's ^Z input. */
c0e8c16f 96static void
718e3744 97sigint (int sig)
98{
99 /* Check this process is not child process. */
100 if (! execute_flag)
101 {
102 rl_initialize ();
103 printf ("\n");
104 rl_forced_update_display ();
105 }
106}
107
e42f5a37 108/* Signale wrapper for vtysh. We don't use sigevent because
109 * vtysh doesn't use threads. TODO */
7f720f54 110static void
e42f5a37 111vtysh_signal_set (int signo, void (*func)(int))
718e3744 112{
718e3744 113 struct sigaction sig;
114 struct sigaction osig;
115
116 sig.sa_handler = func;
117 sigemptyset (&sig.sa_mask);
118 sig.sa_flags = 0;
119#ifdef SA_RESTART
120 sig.sa_flags |= SA_RESTART;
121#endif /* SA_RESTART */
122
7f720f54 123 sigaction (signo, &sig, &osig);
718e3744 124}
125
126/* Initialization of signal handles. */
c0e8c16f
DS
127static void
128vtysh_signal_init (void)
718e3744 129{
e42f5a37 130 vtysh_signal_set (SIGINT, sigint);
131 vtysh_signal_set (SIGTSTP, sigtstp);
132 vtysh_signal_set (SIGPIPE, SIG_IGN);
718e3744 133}
b094d260 134
718e3744 135/* Help information display. */
136static void
137usage (int status)
138{
139 if (status != 0)
140 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
141 else
95e735b5 142 printf ("Usage : %s [OPTION...]\n\n" \
0dca233e 143 "Integrated shell for FRR. \n\n" \
95e735b5 144 "-b, --boot Execute boot startup configuration\n" \
f366ad31
AS
145 "-c, --command Execute argument as command\n" \
146 "-d, --daemon Connect only to the specified daemon\n" \
0846286b 147 "-f, --inputfile Execute commands from specific file and exit\n" \
f366ad31 148 "-E, --echo Echo prompt and command in -c mode\n" \
876b8be0 149 "-C, --dryrun Check configuration for validity and exit\n" \
87d79a9f 150 " --vty_socket Override vty socket path\n" \
ce2e9ec3
MW
151 "-m, --markfile Mark input file with context end\n" \
152 " --vty_socket Override vty socket path\n" \
153 " --config_dir Override config directory path\n" \
0dca233e 154 "-w, --writeconfig Write integrated config (Frr.conf) and exit\n" \
95e735b5 155 "-h, --help Display this help and exit\n\n" \
f366ad31
AS
156 "Note that multiple commands may be executed from the command\n" \
157 "line by passing multiple -c args, or by embedding linefeed\n" \
158 "characters in one or more of the commands.\n\n" \
b2f36157 159 "Report bugs to %s\n", progname, FRR_BUG_ADDRESS);
95e735b5 160
718e3744 161 exit (status);
162}
163
164/* VTY shell options, we use GNU getopt library. */
87d79a9f 165#define OPTION_VTYSOCK 1000
ce2e9ec3 166#define OPTION_CONFDIR 1001
718e3744 167struct option longopts[] =
168{
b094d260 169 { "boot", no_argument, NULL, 'b'},
4991f6ca 170 /* For compatibility with older zebra/quagga versions */
718e3744 171 { "eval", required_argument, NULL, 'e'},
4991f6ca 172 { "command", required_argument, NULL, 'c'},
f366ad31 173 { "daemon", required_argument, NULL, 'd'},
87d79a9f 174 { "vty_socket", required_argument, NULL, OPTION_VTYSOCK},
ce2e9ec3 175 { "config_dir", required_argument, NULL, OPTION_CONFDIR},
0846286b 176 { "inputfile", required_argument, NULL, 'f'},
f366ad31 177 { "echo", no_argument, NULL, 'E'},
876b8be0 178 { "dryrun", no_argument, NULL, 'C'},
718e3744 179 { "help", no_argument, NULL, 'h'},
914131f8 180 { "noerror", no_argument, NULL, 'n'},
0846286b 181 { "mark", no_argument, NULL, 'm'},
a68f8616 182 { "writeconfig", no_argument, NULL, 'w'},
718e3744 183 { 0 }
184};
b094d260 185
718e3744 186/* Read a string, and return a pointer to it. Returns NULL on EOF. */
c0e8c16f
DS
187static char *
188vtysh_rl_gets (void)
718e3744 189{
4991f6ca 190 HIST_ENTRY *last;
718e3744 191 /* If the buffer has already been allocated, return the memory
95e735b5 192 * to the free pool. */
718e3744 193 if (line_read)
194 {
195 free (line_read);
196 line_read = NULL;
197 }
198
199 /* Get a line from the user. Change prompt according to node. XXX. */
200 line_read = readline (vtysh_prompt ());
201
4991f6ca 202 /* If the line has any text in it, save it on the history. But only if
95e735b5 203 * last command in history isn't the same one. */
718e3744 204 if (line_read && *line_read)
4991f6ca 205 {
206 using_history();
207 last = previous_history();
3f4ab7f9 208 if (!last || strcmp (last->line, line_read) != 0) {
4991f6ca 209 add_history (line_read);
3f4ab7f9
TP
210 append_history(1,history_file);
211 }
4991f6ca 212 }
718e3744 213
214 return (line_read);
215}
b094d260 216
57fb9748
SH
217static void log_it(const char *line)
218{
219 time_t t = time(NULL);
220 struct tm *tmp = localtime(&t);
f03db93b 221 const char *user = getenv("USER");
57fb9748
SH
222 char tod[64];
223
f03db93b
DL
224 if (!user)
225 user = "boot";
226
57fb9748
SH
227 strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp);
228
229 fprintf(logfile, "%s:%s %s\n", tod, user, line);
230}
231
e43716f6
DS
232static int flock_fd;
233
234static void
235vtysh_flock_config (const char *flock_file)
236{
237 int count = 0;
238
6ac014d0 239 flock_fd = open (flock_file, O_RDONLY, 0644);
e43716f6
DS
240 if (flock_fd < 0)
241 {
242 fprintf (stderr, "Unable to create lock file: %s, %s\n",
243 flock_file, safe_strerror (errno));
244 return;
245 }
246
247 while (count < 400 && (flock (flock_fd, LOCK_EX | LOCK_NB) < 0))
248 {
249 count++;
250 usleep (500000);
251 }
252
253 if (count >= 400)
254 fprintf(stderr, "Flock of %s failed, continuing this may cause issues\n",
255 flock_file);
256}
257
258static void
259vtysh_unflock_config (void)
260{
261 flock (flock_fd, LOCK_UN);
262 close (flock_fd);
263}
264
718e3744 265/* VTY shell main routine. */
266int
267main (int argc, char **argv, char **env)
268{
269 char *p;
270 int opt;
876b8be0 271 int dryrun = 0;
718e3744 272 int boot_flag = 0;
f366ad31 273 const char *daemon_name = NULL;
0846286b 274 const char *inputfile = NULL;
ff1c42fb 275 const char *vtysh_configfile_name;
f366ad31
AS
276 struct cmd_rec {
277 const char *line;
278 struct cmd_rec *next;
279 } *cmd = NULL;
280 struct cmd_rec *tail = NULL;
281 int echo_command = 0;
914131f8 282 int no_error = 0;
0846286b 283 int markfile = 0;
a68f8616 284 int writeconfig = 0;
3221dca8 285 int ret = 0;
fba55c8a 286 char *homedir = NULL;
718e3744 287
ce2e9ec3
MW
288 /* check for restricted functionality if vtysh is run setuid */
289 int restricted = (getuid() != geteuid()) || (getgid() != getegid());
290
718e3744 291 /* Preserve name of myself. */
292 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
293
57fb9748
SH
294 /* if logging open now */
295 if ((p = getenv("VTYSH_LOG")) != NULL)
296 logfile = fopen(p, "a");
297
718e3744 298 /* Option handling. */
299 while (1)
300 {
a68f8616 301 opt = getopt_long (argc, argv, "be:c:d:nf:mEhCw", longopts, 0);
718e3744 302
303 if (opt == EOF)
304 break;
305
306 switch (opt)
307 {
308 case 0:
309 break;
310 case 'b':
311 boot_flag = 1;
312 break;
313 case 'e':
4991f6ca 314 case 'c':
f366ad31
AS
315 {
316 struct cmd_rec *cr;
c66f9c61 317 cr = XMALLOC(MTYPE_TMP, sizeof(*cr));
f366ad31
AS
318 cr->line = optarg;
319 cr->next = NULL;
320 if (tail)
321 tail->next = cr;
322 else
323 cmd = cr;
324 tail = cr;
325 }
326 break;
87d79a9f
MW
327 case OPTION_VTYSOCK:
328 vty_sock_path = optarg;
329 break;
ce2e9ec3
MW
330 case OPTION_CONFDIR:
331 /*
332 * Skip option for Config Directory if setuid
333 */
334 if (restricted)
335 {
336 fprintf (stderr, "Overriding of Config Directory blocked for vtysh with setuid");
337 return 1;
338 }
339 /*
340 * Overwrite location for vtysh.conf
341 */
342 vtysh_configfile_name = strrchr(VTYSH_DEFAULT_CONFIG, '/');
343 if (vtysh_configfile_name)
344 /* skip '/' */
345 vtysh_configfile_name++;
346 else
347 /*
348 * VTYSH_DEFAULT_CONFIG configured with relative path
349 * during config? Should really never happen for
350 * sensible config
351 */
352 vtysh_configfile_name = (char *) VTYSH_DEFAULT_CONFIG;
353 strlcpy(vtysh_config_always, optarg, sizeof(vtysh_config_always));
354 strlcat(vtysh_config_always, "/", sizeof(vtysh_config_always));
355 strlcat(vtysh_config_always, vtysh_configfile_name,
356 sizeof(vtysh_config_always));
357 /*
0dca233e 358 * Overwrite location for Frr.conf
ce2e9ec3 359 */
a0b4b67e 360 vtysh_configfile_name = strrchr(FRR_DEFAULT_CONFIG, '/');
ce2e9ec3
MW
361 if (vtysh_configfile_name)
362 /* skip '/' */
363 vtysh_configfile_name++;
364 else
365 /*
a0b4b67e 366 * FRR_DEFAULT_CONFIG configured with relative path
ce2e9ec3
MW
367 * during config? Should really never happen for
368 * sensible config
369 */
a0b4b67e 370 vtysh_configfile_name = (char *) FRR_DEFAULT_CONFIG;
ce2e9ec3
MW
371 strlcpy(quagga_config_default, optarg, sizeof(vtysh_config_always));
372 strlcat(quagga_config_default, "/", sizeof(vtysh_config_always));
373 strlcat(quagga_config_default, vtysh_configfile_name,
374 sizeof(quagga_config_default));
375 break;
f366ad31
AS
376 case 'd':
377 daemon_name = optarg;
378 break;
0846286b
DS
379 case 'f':
380 inputfile = optarg;
381 break;
382 case 'm':
383 markfile = 1;
384 break;
914131f8
SH
385 case 'n':
386 no_error = 1;
387 break;
f366ad31
AS
388 case 'E':
389 echo_command = 1;
718e3744 390 break;
876b8be0
PJ
391 case 'C':
392 dryrun = 1;
393 break;
a68f8616
DL
394 case 'w':
395 writeconfig = 1;
396 break;
718e3744 397 case 'h':
398 usage (0);
399 break;
718e3744 400 default:
401 usage (1);
402 break;
403 }
404 }
405
a68f8616
DL
406 if (markfile + writeconfig + dryrun + boot_flag > 1)
407 {
408 fprintf (stderr, "Invalid combination of arguments. Please specify at "
409 "most one of:\n\t-b, -C, -m, -w\n");
410 return 1;
411 }
412 if (inputfile && (writeconfig || boot_flag))
413 {
414 fprintf (stderr, "WARNING: Combinining the -f option with -b or -w is "
415 "NOT SUPPORTED since its\nresults are inconsistent!\n");
416 }
417
718e3744 418 /* Initialize user input buffer. */
419 line_read = NULL;
0fbd62a1 420 setlinebuf(stdout);
718e3744 421
422 /* Signal and others. */
e42f5a37 423 vtysh_signal_init ();
718e3744 424
425 /* Make vty structure and register commands. */
426 vtysh_init_vty ();
427 vtysh_init_cmd ();
428 vtysh_user_init ();
429 vtysh_config_init ();
430
431 vty_init_vtysh ();
432
f366ad31 433 /* Read vtysh configuration file before connecting to daemons. */
cb947ba3 434 vtysh_read_config(vtysh_config_always);
0846286b
DS
435
436 if (markfile)
437 {
438 if (!inputfile)
439 {
440 fprintf(stderr, "-f option MUST be specified with -m option\n");
441 return(1);
442 }
443 return(vtysh_mark_file(inputfile));
444 }
718e3744 445
876b8be0
PJ
446 /* Start execution only if not in dry-run mode */
447 if(dryrun)
0846286b
DS
448 {
449 if (inputfile)
450 {
3221dca8 451 ret = vtysh_read_config(inputfile);
0846286b 452 }
3221dca8
DS
453 else
454 {
455 ret = vtysh_read_config(quagga_config_default);
456 }
457 exit(ret);
0846286b
DS
458 }
459
914131f8
SH
460 /* Ignore error messages */
461 if (no_error)
7a49a5b5
DS
462 {
463 if (freopen("/dev/null", "w", stdout) == NULL)
464 {
465 fprintf(stderr, "Exiting: Failed to duplicate stdout with -n option");
466 exit(1);
467 }
468 }
914131f8 469
f366ad31
AS
470 /* Make sure we pass authentication before proceeding. */
471 vtysh_auth ();
472
473 /* Do not connect until we have passed authentication. */
474 if (vtysh_connect_all (daemon_name) <= 0)
475 {
476 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
8ca1689f
SK
477 if (no_error)
478 exit(0);
479 else
480 exit(1);
f366ad31
AS
481 }
482
a68f8616
DL
483 if (writeconfig)
484 {
9f1f8df3 485 vtysh_execute ("enable");
a68f8616
DL
486 return vtysh_write_config_integrated ();
487 }
488
0846286b
DS
489 if (inputfile)
490 {
8f6899f2 491 vtysh_flock_config (inputfile);
3221dca8 492 ret = vtysh_read_config(inputfile);
8f6899f2 493 vtysh_unflock_config ();
3221dca8 494 exit(ret);
0846286b
DS
495 }
496
fba55c8a
DS
497 /*
498 * Setup history file for use by both -c and regular input
499 * If we can't find the home directory, then don't store
500 * the history information
501 */
502 homedir = vtysh_get_home ();
503 if (homedir)
504 {
505 snprintf(history_file, sizeof(history_file), "%s/.history_quagga", homedir);
506 if (read_history (history_file) != 0)
507 {
508 int fp;
509
510 fp = open (history_file, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
511 if (fp)
512 close (fp);
513
514 read_history (history_file);
515 }
516 }
517
95e735b5 518 /* If eval mode. */
f366ad31 519 if (cmd)
718e3744 520 {
f366ad31
AS
521 /* Enter into enable node. */
522 vtysh_execute ("enable");
523
524 while (cmd != NULL)
525 {
57fb9748 526 int ret;
f366ad31
AS
527 char *eol;
528
529 while ((eol = strchr(cmd->line, '\n')) != NULL)
530 {
531 *eol = '\0';
57fb9748 532
fba55c8a
DS
533 add_history (cmd->line);
534 append_history (1, history_file);
535
f366ad31 536 if (echo_command)
57fb9748
SH
537 printf("%s%s\n", vtysh_prompt(), cmd->line);
538
539 if (logfile)
540 log_it(cmd->line);
541
542 ret = vtysh_execute_no_pager(cmd->line);
914131f8
SH
543 if (!no_error &&
544 ! (ret == CMD_SUCCESS ||
545 ret == CMD_SUCCESS_DAEMON ||
546 ret == CMD_WARNING))
57fb9748
SH
547 exit(1);
548
f366ad31
AS
549 cmd->line = eol+1;
550 }
57fb9748 551
fba55c8a
DS
552 add_history (cmd->line);
553 append_history (1, history_file);
554
f366ad31
AS
555 if (echo_command)
556 printf("%s%s\n", vtysh_prompt(), cmd->line);
57fb9748
SH
557
558 if (logfile)
559 log_it(cmd->line);
560
561 ret = vtysh_execute_no_pager(cmd->line);
914131f8
SH
562 if (!no_error &&
563 ! (ret == CMD_SUCCESS ||
564 ret == CMD_SUCCESS_DAEMON ||
565 ret == CMD_WARNING))
57fb9748 566 exit(1);
f366ad31
AS
567
568 {
569 struct cmd_rec *cr;
570 cr = cmd;
571 cmd = cmd->next;
c66f9c61 572 XFREE(MTYPE_TMP, cr);
f366ad31
AS
573 }
574 }
fba55c8a
DS
575
576 history_truncate_file(history_file,1000);
718e3744 577 exit (0);
578 }
cb947ba3 579
718e3744 580 /* Boot startup configuration file. */
581 if (boot_flag)
582 {
cb947ba3
DL
583 vtysh_flock_config (quagga_config);
584 int ret = vtysh_read_config (quagga_config);
e43716f6 585 vtysh_unflock_config ();
1db63918
DS
586 if (ret)
587 {
588 fprintf (stderr, "Configuration file[%s] processing failure: %d\n",
cb947ba3 589 quagga_config, ret);
6dbef6e7
DS
590 if (no_error)
591 exit (0);
592 else
1db63918 593 exit (ret);
e7168df4 594 }
595 else
596 exit (0);
718e3744 597 }
598
599 vtysh_pager_init ();
600
601 vtysh_readline_init ();
602
603 vty_hello (vty);
604
e7168df4 605 /* Enter into enable node. */
606 vtysh_execute ("enable");
607
718e3744 608 /* Preparation for longjmp() in sigtstp(). */
609 sigsetjmp (jmpbuf, 1);
610 jmpflag = 1;
611
612 /* Main command loop. */
613 while (vtysh_rl_gets ())
614 vtysh_execute (line_read);
615
3f4ab7f9 616 history_truncate_file(history_file,1000);
718e3744 617 printf ("\n");
618
619 /* Rest in peace. */
620 exit (0);
621}