]> git.proxmox.com Git - mirror_frr.git/blame - vtysh/vtysh_main.c
*: centralize more into frr_init()
[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" \
ce2e9ec3
MW
150 "-m, --markfile Mark input file with context end\n" \
151 " --vty_socket Override vty socket path\n" \
152 " --config_dir Override config directory path\n" \
e20dc2ba 153 "-w, --writeconfig Write integrated config (frr.conf) and exit\n" \
95e735b5 154 "-h, --help Display this help and exit\n\n" \
f366ad31
AS
155 "Note that multiple commands may be executed from the command\n" \
156 "line by passing multiple -c args, or by embedding linefeed\n" \
157 "characters in one or more of the commands.\n\n" \
b2f36157 158 "Report bugs to %s\n", progname, FRR_BUG_ADDRESS);
95e735b5 159
718e3744 160 exit (status);
161}
162
163/* VTY shell options, we use GNU getopt library. */
87d79a9f 164#define OPTION_VTYSOCK 1000
ce2e9ec3 165#define OPTION_CONFDIR 1001
718e3744 166struct option longopts[] =
167{
b094d260 168 { "boot", no_argument, NULL, 'b'},
4991f6ca 169 /* For compatibility with older zebra/quagga versions */
718e3744 170 { "eval", required_argument, NULL, 'e'},
4991f6ca 171 { "command", required_argument, NULL, 'c'},
f366ad31 172 { "daemon", required_argument, NULL, 'd'},
87d79a9f 173 { "vty_socket", required_argument, NULL, OPTION_VTYSOCK},
ce2e9ec3 174 { "config_dir", required_argument, NULL, OPTION_CONFDIR},
0846286b 175 { "inputfile", required_argument, NULL, 'f'},
f366ad31 176 { "echo", no_argument, NULL, 'E'},
876b8be0 177 { "dryrun", no_argument, NULL, 'C'},
718e3744 178 { "help", no_argument, NULL, 'h'},
914131f8 179 { "noerror", no_argument, NULL, 'n'},
0846286b 180 { "mark", no_argument, NULL, 'm'},
a68f8616 181 { "writeconfig", no_argument, NULL, 'w'},
718e3744 182 { 0 }
183};
b094d260 184
718e3744 185/* Read a string, and return a pointer to it. Returns NULL on EOF. */
c0e8c16f
DS
186static char *
187vtysh_rl_gets (void)
718e3744 188{
4991f6ca 189 HIST_ENTRY *last;
718e3744 190 /* If the buffer has already been allocated, return the memory
95e735b5 191 * to the free pool. */
718e3744 192 if (line_read)
193 {
194 free (line_read);
195 line_read = NULL;
196 }
197
198 /* Get a line from the user. Change prompt according to node. XXX. */
199 line_read = readline (vtysh_prompt ());
200
4991f6ca 201 /* If the line has any text in it, save it on the history. But only if
95e735b5 202 * last command in history isn't the same one. */
718e3744 203 if (line_read && *line_read)
4991f6ca 204 {
205 using_history();
206 last = previous_history();
3f4ab7f9 207 if (!last || strcmp (last->line, line_read) != 0) {
4991f6ca 208 add_history (line_read);
3f4ab7f9
TP
209 append_history(1,history_file);
210 }
4991f6ca 211 }
718e3744 212
213 return (line_read);
214}
b094d260 215
57fb9748
SH
216static void log_it(const char *line)
217{
218 time_t t = time(NULL);
219 struct tm *tmp = localtime(&t);
f03db93b 220 const char *user = getenv("USER");
57fb9748
SH
221 char tod[64];
222
f03db93b
DL
223 if (!user)
224 user = "boot";
225
57fb9748
SH
226 strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp);
227
228 fprintf(logfile, "%s:%s %s\n", tod, user, line);
229}
230
e43716f6
DS
231static int flock_fd;
232
233static void
234vtysh_flock_config (const char *flock_file)
235{
236 int count = 0;
237
6ac014d0 238 flock_fd = open (flock_file, O_RDONLY, 0644);
e43716f6
DS
239 if (flock_fd < 0)
240 {
241 fprintf (stderr, "Unable to create lock file: %s, %s\n",
242 flock_file, safe_strerror (errno));
243 return;
244 }
245
246 while (count < 400 && (flock (flock_fd, LOCK_EX | LOCK_NB) < 0))
247 {
248 count++;
249 usleep (500000);
250 }
251
252 if (count >= 400)
253 fprintf(stderr, "Flock of %s failed, continuing this may cause issues\n",
254 flock_file);
255}
256
257static void
258vtysh_unflock_config (void)
259{
260 flock (flock_fd, LOCK_UN);
261 close (flock_fd);
262}
263
718e3744 264/* VTY shell main routine. */
265int
266main (int argc, char **argv, char **env)
267{
268 char *p;
269 int opt;
876b8be0 270 int dryrun = 0;
718e3744 271 int boot_flag = 0;
f366ad31 272 const char *daemon_name = NULL;
0846286b 273 const char *inputfile = NULL;
ff1c42fb 274 const char *vtysh_configfile_name;
f366ad31 275 struct cmd_rec {
cd272640 276 char *line;
f366ad31
AS
277 struct cmd_rec *next;
278 } *cmd = NULL;
279 struct cmd_rec *tail = NULL;
280 int echo_command = 0;
914131f8 281 int no_error = 0;
0846286b 282 int markfile = 0;
a68f8616 283 int writeconfig = 0;
3221dca8 284 int ret = 0;
fba55c8a 285 char *homedir = NULL;
718e3744 286
ce2e9ec3
MW
287 /* check for restricted functionality if vtysh is run setuid */
288 int restricted = (getuid() != geteuid()) || (getgid() != getegid());
289
718e3744 290 /* Preserve name of myself. */
291 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
292
57fb9748
SH
293 /* if logging open now */
294 if ((p = getenv("VTYSH_LOG")) != NULL)
295 logfile = fopen(p, "a");
296
718e3744 297 /* Option handling. */
298 while (1)
299 {
a68f8616 300 opt = getopt_long (argc, argv, "be:c:d:nf:mEhCw", longopts, 0);
718e3744 301
302 if (opt == EOF)
303 break;
304
305 switch (opt)
306 {
307 case 0:
308 break;
309 case 'b':
310 boot_flag = 1;
311 break;
312 case 'e':
4991f6ca 313 case 'c':
f366ad31
AS
314 {
315 struct cmd_rec *cr;
c66f9c61 316 cr = XMALLOC(MTYPE_TMP, sizeof(*cr));
f366ad31
AS
317 cr->line = optarg;
318 cr->next = NULL;
319 if (tail)
320 tail->next = cr;
321 else
322 cmd = cr;
323 tail = cr;
324 }
325 break;
87d79a9f
MW
326 case OPTION_VTYSOCK:
327 vty_sock_path = optarg;
328 break;
ce2e9ec3
MW
329 case OPTION_CONFDIR:
330 /*
331 * Skip option for Config Directory if setuid
332 */
333 if (restricted)
334 {
335 fprintf (stderr, "Overriding of Config Directory blocked for vtysh with setuid");
336 return 1;
337 }
338 /*
339 * Overwrite location for vtysh.conf
340 */
341 vtysh_configfile_name = strrchr(VTYSH_DEFAULT_CONFIG, '/');
342 if (vtysh_configfile_name)
343 /* skip '/' */
344 vtysh_configfile_name++;
345 else
346 /*
347 * VTYSH_DEFAULT_CONFIG configured with relative path
348 * during config? Should really never happen for
349 * sensible config
350 */
351 vtysh_configfile_name = (char *) VTYSH_DEFAULT_CONFIG;
352 strlcpy(vtysh_config_always, optarg, sizeof(vtysh_config_always));
353 strlcat(vtysh_config_always, "/", sizeof(vtysh_config_always));
354 strlcat(vtysh_config_always, vtysh_configfile_name,
355 sizeof(vtysh_config_always));
356 /*
e20dc2ba 357 * Overwrite location for frr.conf
ce2e9ec3 358 */
a0b4b67e 359 vtysh_configfile_name = strrchr(FRR_DEFAULT_CONFIG, '/');
ce2e9ec3
MW
360 if (vtysh_configfile_name)
361 /* skip '/' */
362 vtysh_configfile_name++;
363 else
364 /*
a0b4b67e 365 * FRR_DEFAULT_CONFIG configured with relative path
ce2e9ec3
MW
366 * during config? Should really never happen for
367 * sensible config
368 */
a0b4b67e 369 vtysh_configfile_name = (char *) FRR_DEFAULT_CONFIG;
ce2e9ec3
MW
370 strlcpy(quagga_config_default, optarg, sizeof(vtysh_config_always));
371 strlcat(quagga_config_default, "/", sizeof(vtysh_config_always));
372 strlcat(quagga_config_default, vtysh_configfile_name,
373 sizeof(quagga_config_default));
374 break;
f366ad31
AS
375 case 'd':
376 daemon_name = optarg;
377 break;
0846286b
DS
378 case 'f':
379 inputfile = optarg;
380 break;
381 case 'm':
382 markfile = 1;
383 break;
914131f8
SH
384 case 'n':
385 no_error = 1;
386 break;
f366ad31
AS
387 case 'E':
388 echo_command = 1;
718e3744 389 break;
876b8be0
PJ
390 case 'C':
391 dryrun = 1;
392 break;
a68f8616
DL
393 case 'w':
394 writeconfig = 1;
395 break;
718e3744 396 case 'h':
397 usage (0);
398 break;
718e3744 399 default:
400 usage (1);
401 break;
402 }
403 }
404
a68f8616
DL
405 if (markfile + writeconfig + dryrun + boot_flag > 1)
406 {
407 fprintf (stderr, "Invalid combination of arguments. Please specify at "
408 "most one of:\n\t-b, -C, -m, -w\n");
409 return 1;
410 }
411 if (inputfile && (writeconfig || boot_flag))
412 {
413 fprintf (stderr, "WARNING: Combinining the -f option with -b or -w is "
414 "NOT SUPPORTED since its\nresults are inconsistent!\n");
415 }
416
718e3744 417 /* Initialize user input buffer. */
418 line_read = NULL;
0fbd62a1 419 setlinebuf(stdout);
718e3744 420
421 /* Signal and others. */
e42f5a37 422 vtysh_signal_init ();
718e3744 423
424 /* Make vty structure and register commands. */
425 vtysh_init_vty ();
426 vtysh_init_cmd ();
427 vtysh_user_init ();
428 vtysh_config_init ();
429
430 vty_init_vtysh ();
431
f366ad31 432 /* Read vtysh configuration file before connecting to daemons. */
cb947ba3 433 vtysh_read_config(vtysh_config_always);
0846286b
DS
434
435 if (markfile)
436 {
437 if (!inputfile)
438 {
439 fprintf(stderr, "-f option MUST be specified with -m option\n");
440 return(1);
441 }
442 return(vtysh_mark_file(inputfile));
443 }
718e3744 444
876b8be0 445 /* Start execution only if not in dry-run mode */
cd272640 446 if (dryrun && !cmd)
0846286b
DS
447 {
448 if (inputfile)
449 {
3221dca8 450 ret = vtysh_read_config(inputfile);
0846286b 451 }
3221dca8
DS
452 else
453 {
454 ret = vtysh_read_config(quagga_config_default);
455 }
cd272640
DL
456
457 exit(ret);
458 }
459
460 if (dryrun && cmd)
461 {
462 vtysh_execute ("enable");
463 while (cmd)
464 {
465 struct cmd_rec *cr;
466 char *cmdnow = cmd->line, *next;
467 do
468 {
469 next = strchr(cmdnow, '\n');
470 if (next)
471 *next++ = '\0';
472
473 if (echo_command)
474 printf("%s%s\n", vtysh_prompt(), cmdnow);
475
476 ret = vtysh_execute_no_pager(cmdnow);
477 if (!no_error &&
478 ! (ret == CMD_SUCCESS ||
479 ret == CMD_SUCCESS_DAEMON ||
480 ret == CMD_WARNING))
481 exit(1);
482 }
483 while ((cmdnow = next) != NULL);
484
485 cr = cmd;
486 cmd = cmd->next;
487 XFREE(MTYPE_TMP, cr);
488 }
3221dca8 489 exit(ret);
0846286b
DS
490 }
491
914131f8
SH
492 /* Ignore error messages */
493 if (no_error)
7a49a5b5
DS
494 {
495 if (freopen("/dev/null", "w", stdout) == NULL)
496 {
497 fprintf(stderr, "Exiting: Failed to duplicate stdout with -n option");
498 exit(1);
499 }
500 }
914131f8 501
f366ad31
AS
502 /* Make sure we pass authentication before proceeding. */
503 vtysh_auth ();
504
505 /* Do not connect until we have passed authentication. */
506 if (vtysh_connect_all (daemon_name) <= 0)
507 {
508 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
8ca1689f
SK
509 if (no_error)
510 exit(0);
511 else
512 exit(1);
f366ad31
AS
513 }
514
a68f8616
DL
515 if (writeconfig)
516 {
9f1f8df3 517 vtysh_execute ("enable");
a68f8616
DL
518 return vtysh_write_config_integrated ();
519 }
520
0846286b
DS
521 if (inputfile)
522 {
8f6899f2 523 vtysh_flock_config (inputfile);
3221dca8 524 ret = vtysh_read_config(inputfile);
8f6899f2 525 vtysh_unflock_config ();
3221dca8 526 exit(ret);
0846286b
DS
527 }
528
fba55c8a
DS
529 /*
530 * Setup history file for use by both -c and regular input
531 * If we can't find the home directory, then don't store
532 * the history information
533 */
534 homedir = vtysh_get_home ();
535 if (homedir)
536 {
537 snprintf(history_file, sizeof(history_file), "%s/.history_quagga", homedir);
538 if (read_history (history_file) != 0)
539 {
540 int fp;
541
542 fp = open (history_file, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
543 if (fp)
544 close (fp);
545
546 read_history (history_file);
547 }
548 }
549
95e735b5 550 /* If eval mode. */
f366ad31 551 if (cmd)
718e3744 552 {
f366ad31
AS
553 /* Enter into enable node. */
554 vtysh_execute ("enable");
555
556 while (cmd != NULL)
557 {
57fb9748 558 int ret;
f366ad31
AS
559 char *eol;
560
561 while ((eol = strchr(cmd->line, '\n')) != NULL)
562 {
563 *eol = '\0';
57fb9748 564
fba55c8a
DS
565 add_history (cmd->line);
566 append_history (1, history_file);
567
f366ad31 568 if (echo_command)
57fb9748
SH
569 printf("%s%s\n", vtysh_prompt(), cmd->line);
570
571 if (logfile)
572 log_it(cmd->line);
573
574 ret = vtysh_execute_no_pager(cmd->line);
914131f8
SH
575 if (!no_error &&
576 ! (ret == CMD_SUCCESS ||
577 ret == CMD_SUCCESS_DAEMON ||
578 ret == CMD_WARNING))
57fb9748
SH
579 exit(1);
580
f366ad31
AS
581 cmd->line = eol+1;
582 }
57fb9748 583
fba55c8a
DS
584 add_history (cmd->line);
585 append_history (1, history_file);
586
f366ad31
AS
587 if (echo_command)
588 printf("%s%s\n", vtysh_prompt(), cmd->line);
57fb9748
SH
589
590 if (logfile)
591 log_it(cmd->line);
592
593 ret = vtysh_execute_no_pager(cmd->line);
914131f8
SH
594 if (!no_error &&
595 ! (ret == CMD_SUCCESS ||
596 ret == CMD_SUCCESS_DAEMON ||
597 ret == CMD_WARNING))
57fb9748 598 exit(1);
f366ad31
AS
599
600 {
601 struct cmd_rec *cr;
602 cr = cmd;
603 cmd = cmd->next;
c66f9c61 604 XFREE(MTYPE_TMP, cr);
f366ad31
AS
605 }
606 }
fba55c8a
DS
607
608 history_truncate_file(history_file,1000);
718e3744 609 exit (0);
610 }
cb947ba3 611
718e3744 612 /* Boot startup configuration file. */
613 if (boot_flag)
614 {
cb947ba3
DL
615 vtysh_flock_config (quagga_config);
616 int ret = vtysh_read_config (quagga_config);
e43716f6 617 vtysh_unflock_config ();
1db63918
DS
618 if (ret)
619 {
620 fprintf (stderr, "Configuration file[%s] processing failure: %d\n",
cb947ba3 621 quagga_config, ret);
6dbef6e7
DS
622 if (no_error)
623 exit (0);
624 else
1db63918 625 exit (ret);
e7168df4 626 }
627 else
628 exit (0);
718e3744 629 }
630
631 vtysh_pager_init ();
632
633 vtysh_readline_init ();
634
635 vty_hello (vty);
636
e7168df4 637 /* Enter into enable node. */
638 vtysh_execute ("enable");
639
718e3744 640 /* Preparation for longjmp() in sigtstp(). */
641 sigsetjmp (jmpbuf, 1);
642 jmpflag = 1;
643
644 /* Main command loop. */
645 while (vtysh_rl_gets ())
646 vtysh_execute (line_read);
647
3f4ab7f9 648 history_truncate_file(history_file,1000);
718e3744 649 printf ("\n");
650
651 /* Rest in peace. */
652 exit (0);
653}