1 /* Virtual terminal interface shell.
2 * Copyright (C) 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
31 #include <readline/readline.h>
32 #include <readline/history.h>
34 #include <lib/version.h>
39 #include "memory_vty.h"
41 #include "vtysh/vtysh.h"
42 #include "vtysh/vtysh_user.h"
44 /* VTY shell program name. */
47 /* Configuration file name and directory. */
48 static char vtysh_config_always
[MAXPATHLEN
] = SYSCONFDIR VTYSH_DEFAULT_CONFIG
;
49 static char quagga_config_default
[MAXPATHLEN
] = SYSCONFDIR FRR_DEFAULT_CONFIG
;
50 char *quagga_config
= quagga_config_default
;
51 char history_file
[MAXPATHLEN
];
53 /* Flag for indicate executing child command. */
56 /* VTY Socket prefix */
57 char * vty_sock_path
= NULL
;
59 /* For sigsetjmp() & siglongjmp(). */
60 static sigjmp_buf jmpbuf
;
62 /* Flag for avoid recursive siglongjmp() call. */
63 static int jmpflag
= 0;
65 /* A static variable for holding the line. */
66 static char *line_read
;
68 /* Master of threads. */
69 struct thread_master
*master
;
74 /* SIGTSTP handler. This function care user's ^Z input. */
78 /* Execute "end" command. */
79 vtysh_execute ("end");
81 /* Initialize readline. */
85 /* Check jmpflag for duplicate siglongjmp(). */
91 /* Back to main command loop. */
92 siglongjmp (jmpbuf
, 1);
95 /* SIGINT handler. This function care user's ^Z input. */
99 /* Check this process is not child process. */
104 rl_forced_update_display ();
108 /* Signale wrapper for vtysh. We don't use sigevent because
109 * vtysh doesn't use threads. TODO */
111 vtysh_signal_set (int signo
, void (*func
)(int))
113 struct sigaction sig
;
114 struct sigaction osig
;
116 sig
.sa_handler
= func
;
117 sigemptyset (&sig
.sa_mask
);
120 sig
.sa_flags
|= SA_RESTART
;
121 #endif /* SA_RESTART */
123 sigaction (signo
, &sig
, &osig
);
126 /* Initialization of signal handles. */
128 vtysh_signal_init (void)
130 vtysh_signal_set (SIGINT
, sigint
);
131 vtysh_signal_set (SIGTSTP
, sigtstp
);
132 vtysh_signal_set (SIGPIPE
, SIG_IGN
);
135 /* Help information display. */
140 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
142 printf ("Usage : %s [OPTION...]\n\n" \
143 "Integrated shell for FRR. \n\n" \
144 "-b, --boot Execute boot startup configuration\n" \
145 "-c, --command Execute argument as command\n" \
146 "-d, --daemon Connect only to the specified daemon\n" \
147 "-f, --inputfile Execute commands from specific file and exit\n" \
148 "-E, --echo Echo prompt and command in -c mode\n" \
149 "-C, --dryrun Check configuration for validity and exit\n" \
150 " --vty_socket Override vty socket path\n" \
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" \
154 "-w, --writeconfig Write integrated config (frr.conf) and exit\n" \
155 "-h, --help Display this help and exit\n\n" \
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" \
159 "Report bugs to %s\n", progname
, FRR_BUG_ADDRESS
);
164 /* VTY shell options, we use GNU getopt library. */
165 #define OPTION_VTYSOCK 1000
166 #define OPTION_CONFDIR 1001
167 struct option longopts
[] =
169 { "boot", no_argument
, NULL
, 'b'},
170 /* For compatibility with older zebra/quagga versions */
171 { "eval", required_argument
, NULL
, 'e'},
172 { "command", required_argument
, NULL
, 'c'},
173 { "daemon", required_argument
, NULL
, 'd'},
174 { "vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
175 { "config_dir", required_argument
, NULL
, OPTION_CONFDIR
},
176 { "inputfile", required_argument
, NULL
, 'f'},
177 { "echo", no_argument
, NULL
, 'E'},
178 { "dryrun", no_argument
, NULL
, 'C'},
179 { "help", no_argument
, NULL
, 'h'},
180 { "noerror", no_argument
, NULL
, 'n'},
181 { "mark", no_argument
, NULL
, 'm'},
182 { "writeconfig", no_argument
, NULL
, 'w'},
186 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
191 /* If the buffer has already been allocated, return the memory
192 * to the free pool. */
199 /* Get a line from the user. Change prompt according to node. XXX. */
200 line_read
= readline (vtysh_prompt ());
202 /* If the line has any text in it, save it on the history. But only if
203 * last command in history isn't the same one. */
204 if (line_read
&& *line_read
)
207 last
= previous_history();
208 if (!last
|| strcmp (last
->line
, line_read
) != 0) {
209 add_history (line_read
);
210 append_history(1,history_file
);
217 static void log_it(const char *line
)
219 time_t t
= time(NULL
);
220 struct tm
*tmp
= localtime(&t
);
221 const char *user
= getenv("USER");
227 strftime(tod
, sizeof tod
, "%Y%m%d-%H:%M.%S", tmp
);
229 fprintf(logfile
, "%s:%s %s\n", tod
, user
, line
);
235 vtysh_flock_config (const char *flock_file
)
239 flock_fd
= open (flock_file
, O_RDONLY
, 0644);
242 fprintf (stderr
, "Unable to create lock file: %s, %s\n",
243 flock_file
, safe_strerror (errno
));
247 while (count
< 400 && (flock (flock_fd
, LOCK_EX
| LOCK_NB
) < 0))
254 fprintf(stderr
, "Flock of %s failed, continuing this may cause issues\n",
259 vtysh_unflock_config (void)
261 flock (flock_fd
, LOCK_UN
);
265 /* VTY shell main routine. */
267 main (int argc
, char **argv
, char **env
)
273 const char *daemon_name
= NULL
;
274 const char *inputfile
= NULL
;
275 const char *vtysh_configfile_name
;
278 struct cmd_rec
*next
;
280 struct cmd_rec
*tail
= NULL
;
281 int echo_command
= 0;
286 char *homedir
= NULL
;
288 /* check for restricted functionality if vtysh is run setuid */
289 int restricted
= (getuid() != geteuid()) || (getgid() != getegid());
291 /* Preserve name of myself. */
292 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
294 /* if logging open now */
295 if ((p
= getenv("VTYSH_LOG")) != NULL
)
296 logfile
= fopen(p
, "a");
298 /* Option handling. */
301 opt
= getopt_long (argc
, argv
, "be:c:d:nf:mEhCw", longopts
, 0);
317 cr
= XMALLOC(MTYPE_TMP
, sizeof(*cr
));
328 vty_sock_path
= optarg
;
332 * Skip option for Config Directory if setuid
336 fprintf (stderr
, "Overriding of Config Directory blocked for vtysh with setuid");
340 * Overwrite location for vtysh.conf
342 vtysh_configfile_name
= strrchr(VTYSH_DEFAULT_CONFIG
, '/');
343 if (vtysh_configfile_name
)
345 vtysh_configfile_name
++;
348 * VTYSH_DEFAULT_CONFIG configured with relative path
349 * during config? Should really never happen for
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
));
358 * Overwrite location for frr.conf
360 vtysh_configfile_name
= strrchr(FRR_DEFAULT_CONFIG
, '/');
361 if (vtysh_configfile_name
)
363 vtysh_configfile_name
++;
366 * FRR_DEFAULT_CONFIG configured with relative path
367 * during config? Should really never happen for
370 vtysh_configfile_name
= (char *) FRR_DEFAULT_CONFIG
;
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
));
377 daemon_name
= optarg
;
406 if (markfile
+ writeconfig
+ dryrun
+ boot_flag
> 1)
408 fprintf (stderr
, "Invalid combination of arguments. Please specify at "
409 "most one of:\n\t-b, -C, -m, -w\n");
412 if (inputfile
&& (writeconfig
|| boot_flag
))
414 fprintf (stderr
, "WARNING: Combinining the -f option with -b or -w is "
415 "NOT SUPPORTED since its\nresults are inconsistent!\n");
418 /* Initialize user input buffer. */
422 /* Signal and others. */
423 vtysh_signal_init ();
425 /* Make vty structure and register commands. */
429 vtysh_config_init ();
433 /* Read vtysh configuration file before connecting to daemons. */
434 vtysh_read_config(vtysh_config_always
);
440 fprintf(stderr
, "-f option MUST be specified with -m option\n");
443 return(vtysh_mark_file(inputfile
));
446 /* Start execution only if not in dry-run mode */
451 ret
= vtysh_read_config(inputfile
);
455 ret
= vtysh_read_config(quagga_config_default
);
463 vtysh_execute ("enable");
467 char *cmdnow
= cmd
->line
, *next
;
470 next
= strchr(cmdnow
, '\n');
475 printf("%s%s\n", vtysh_prompt(), cmdnow
);
477 ret
= vtysh_execute_no_pager(cmdnow
);
479 ! (ret
== CMD_SUCCESS
||
480 ret
== CMD_SUCCESS_DAEMON
||
484 while ((cmdnow
= next
) != NULL
);
488 XFREE(MTYPE_TMP
, cr
);
493 /* Ignore error messages */
496 if (freopen("/dev/null", "w", stdout
) == NULL
)
498 fprintf(stderr
, "Exiting: Failed to duplicate stdout with -n option");
503 /* Make sure we pass authentication before proceeding. */
506 /* Do not connect until we have passed authentication. */
507 if (vtysh_connect_all (daemon_name
) <= 0)
509 fprintf(stderr
, "Exiting: failed to connect to any daemons.\n");
518 vtysh_execute ("enable");
519 return vtysh_write_config_integrated ();
524 vtysh_flock_config (inputfile
);
525 ret
= vtysh_read_config(inputfile
);
526 vtysh_unflock_config ();
531 * Setup history file for use by both -c and regular input
532 * If we can't find the home directory, then don't store
533 * the history information
535 homedir
= vtysh_get_home ();
538 snprintf(history_file
, sizeof(history_file
), "%s/.history_quagga", homedir
);
539 if (read_history (history_file
) != 0)
543 fp
= open (history_file
, O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
);
547 read_history (history_file
);
554 /* Enter into enable node. */
555 vtysh_execute ("enable");
562 while ((eol
= strchr(cmd
->line
, '\n')) != NULL
)
566 add_history (cmd
->line
);
567 append_history (1, history_file
);
570 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
575 ret
= vtysh_execute_no_pager(cmd
->line
);
577 ! (ret
== CMD_SUCCESS
||
578 ret
== CMD_SUCCESS_DAEMON
||
585 add_history (cmd
->line
);
586 append_history (1, history_file
);
589 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
594 ret
= vtysh_execute_no_pager(cmd
->line
);
596 ! (ret
== CMD_SUCCESS
||
597 ret
== CMD_SUCCESS_DAEMON
||
605 XFREE(MTYPE_TMP
, cr
);
609 history_truncate_file(history_file
,1000);
613 /* Boot startup configuration file. */
616 vtysh_flock_config (quagga_config
);
617 int ret
= vtysh_read_config (quagga_config
);
618 vtysh_unflock_config ();
621 fprintf (stderr
, "Configuration file[%s] processing failure: %d\n",
634 vtysh_readline_init ();
638 /* Enter into enable node. */
639 vtysh_execute ("enable");
641 /* Preparation for longjmp() in sigtstp(). */
642 sigsetjmp (jmpbuf
, 1);
645 /* Main command loop. */
646 while (vtysh_rl_gets ())
647 vtysh_execute (line_read
);
649 history_truncate_file(history_file
,1000);