blob: aa7d02187da2316725d81cfb6e3aa7150f60c90c [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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>
28
29#include <readline/readline.h>
30#include <readline/history.h>
31
gdt5e4fa162004-03-16 14:38:36 +000032#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000033#include "getopt.h"
34#include "command.h"
Andrew J. Schorrf366ad32006-07-27 18:01:41 +000035#include "memory.h"
paul718e3742002-12-13 20:15:29 +000036
37#include "vtysh/vtysh.h"
38#include "vtysh/vtysh_user.h"
hassob094d262004-08-25 12:22:00 +000039
paul718e3742002-12-13 20:15:29 +000040/* VTY shell program name. */
41char *progname;
42
hasso67e29ab2004-08-26 22:21:31 +000043/* Configuration file name and directory. */
paul718e3742002-12-13 20:15:29 +000044char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG;
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +010045char history_file[MAXPATHLEN];
paul718e3742002-12-13 20:15:29 +000046
paul718e3742002-12-13 20:15:29 +000047/* Flag for indicate executing child command. */
48int execute_flag = 0;
49
50/* For sigsetjmp() & siglongjmp(). */
51static sigjmp_buf jmpbuf;
52
53/* Flag for avoid recursive siglongjmp() call. */
54static int jmpflag = 0;
55
56/* A static variable for holding the line. */
57static char *line_read;
58
59/* Master of threads. */
60struct thread_master *master;
hassob094d262004-08-25 12:22:00 +000061
Stephen Hemminger57fb9742008-07-28 12:19:04 -070062/* Command logging */
63FILE *logfile;
64
paul718e3742002-12-13 20:15:29 +000065/* SIGTSTP handler. This function care user's ^Z input. */
David Lampartera9eb9062015-03-04 07:07:01 +010066static void
paul718e3742002-12-13 20:15:29 +000067sigtstp (int sig)
68{
69 /* Execute "end" command. */
70 vtysh_execute ("end");
71
72 /* Initialize readline. */
73 rl_initialize ();
74 printf ("\n");
75
76 /* Check jmpflag for duplicate siglongjmp(). */
77 if (! jmpflag)
78 return;
79
80 jmpflag = 0;
81
82 /* Back to main command loop. */
83 siglongjmp (jmpbuf, 1);
84}
85
86/* SIGINT handler. This function care user's ^Z input. */
David Lampartera9eb9062015-03-04 07:07:01 +010087static void
paul718e3742002-12-13 20:15:29 +000088sigint (int sig)
89{
90 /* Check this process is not child process. */
91 if (! execute_flag)
92 {
93 rl_initialize ();
94 printf ("\n");
95 rl_forced_update_display ();
96 }
97}
98
hassoe42f5a32004-08-28 17:04:33 +000099/* Signale wrapper for vtysh. We don't use sigevent because
100 * vtysh doesn't use threads. TODO */
David Lamparter6769f432015-03-04 07:18:24 +0100101static void
hassoe42f5a32004-08-28 17:04:33 +0000102vtysh_signal_set (int signo, void (*func)(int))
paul718e3742002-12-13 20:15:29 +0000103{
paul718e3742002-12-13 20:15:29 +0000104 struct sigaction sig;
105 struct sigaction osig;
106
107 sig.sa_handler = func;
108 sigemptyset (&sig.sa_mask);
109 sig.sa_flags = 0;
110#ifdef SA_RESTART
111 sig.sa_flags |= SA_RESTART;
112#endif /* SA_RESTART */
113
David Lamparter6769f432015-03-04 07:18:24 +0100114 sigaction (signo, &sig, &osig);
paul718e3742002-12-13 20:15:29 +0000115}
116
117/* Initialization of signal handles. */
David Lampartera9eb9062015-03-04 07:07:01 +0100118static void
hassoe42f5a32004-08-28 17:04:33 +0000119vtysh_signal_init ()
paul718e3742002-12-13 20:15:29 +0000120{
hassoe42f5a32004-08-28 17:04:33 +0000121 vtysh_signal_set (SIGINT, sigint);
122 vtysh_signal_set (SIGTSTP, sigtstp);
123 vtysh_signal_set (SIGPIPE, SIG_IGN);
paul718e3742002-12-13 20:15:29 +0000124}
hassob094d262004-08-25 12:22:00 +0000125
paul718e3742002-12-13 20:15:29 +0000126/* Help information display. */
127static void
128usage (int status)
129{
130 if (status != 0)
131 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
132 else
hasso95e735b2004-08-26 13:08:30 +0000133 printf ("Usage : %s [OPTION...]\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000134 "Integrated shell for Quagga routing software suite. \n\n" \
hasso95e735b2004-08-26 13:08:30 +0000135 "-b, --boot Execute boot startup configuration\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000136 "-c, --command Execute argument as command\n" \
137 "-d, --daemon Connect only to the specified daemon\n" \
138 "-E, --echo Echo prompt and command in -c mode\n" \
Paul Jakma876b8be2006-10-15 23:35:57 +0000139 "-C, --dryrun Check configuration for validity and exit\n" \
hasso95e735b2004-08-26 13:08:30 +0000140 "-h, --help Display this help and exit\n\n" \
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000141 "Note that multiple commands may be executed from the command\n" \
142 "line by passing multiple -c args, or by embedding linefeed\n" \
143 "characters in one or more of the commands.\n\n" \
hasso95e735b2004-08-26 13:08:30 +0000144 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
145
paul718e3742002-12-13 20:15:29 +0000146 exit (status);
147}
148
149/* VTY shell options, we use GNU getopt library. */
150struct option longopts[] =
151{
hassob094d262004-08-25 12:22:00 +0000152 { "boot", no_argument, NULL, 'b'},
hasso4991f6c2004-04-06 11:36:17 +0000153 /* For compatibility with older zebra/quagga versions */
paul718e3742002-12-13 20:15:29 +0000154 { "eval", required_argument, NULL, 'e'},
hasso4991f6c2004-04-06 11:36:17 +0000155 { "command", required_argument, NULL, 'c'},
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000156 { "daemon", required_argument, NULL, 'd'},
157 { "echo", no_argument, NULL, 'E'},
Paul Jakma876b8be2006-10-15 23:35:57 +0000158 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +0000159 { "help", no_argument, NULL, 'h'},
Stephen Hemminger914131f2008-07-30 14:16:47 -0700160 { "noerror", no_argument, NULL, 'n'},
paul718e3742002-12-13 20:15:29 +0000161 { 0 }
162};
hassob094d262004-08-25 12:22:00 +0000163
paul718e3742002-12-13 20:15:29 +0000164/* Read a string, and return a pointer to it. Returns NULL on EOF. */
David Lampartera9eb9062015-03-04 07:07:01 +0100165static char *
paul718e3742002-12-13 20:15:29 +0000166vtysh_rl_gets ()
167{
hasso4991f6c2004-04-06 11:36:17 +0000168 HIST_ENTRY *last;
paul718e3742002-12-13 20:15:29 +0000169 /* If the buffer has already been allocated, return the memory
hasso95e735b2004-08-26 13:08:30 +0000170 * to the free pool. */
paul718e3742002-12-13 20:15:29 +0000171 if (line_read)
172 {
173 free (line_read);
174 line_read = NULL;
175 }
176
177 /* Get a line from the user. Change prompt according to node. XXX. */
178 line_read = readline (vtysh_prompt ());
179
hasso4991f6c2004-04-06 11:36:17 +0000180 /* If the line has any text in it, save it on the history. But only if
hasso95e735b2004-08-26 13:08:30 +0000181 * last command in history isn't the same one. */
paul718e3742002-12-13 20:15:29 +0000182 if (line_read && *line_read)
hasso4991f6c2004-04-06 11:36:17 +0000183 {
184 using_history();
185 last = previous_history();
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100186 if (!last || strcmp (last->line, line_read) != 0) {
hasso4991f6c2004-04-06 11:36:17 +0000187 add_history (line_read);
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100188 append_history(1,history_file);
189 }
hasso4991f6c2004-04-06 11:36:17 +0000190 }
paul718e3742002-12-13 20:15:29 +0000191
192 return (line_read);
193}
hassob094d262004-08-25 12:22:00 +0000194
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700195static void log_it(const char *line)
196{
197 time_t t = time(NULL);
198 struct tm *tmp = localtime(&t);
David Lamparter71f55f32015-03-03 09:08:05 +0100199 const char *user = getenv("USER");
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700200 char tod[64];
201
David Lamparter71f55f32015-03-03 09:08:05 +0100202 if (!user)
203 user = "boot";
204
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700205 strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp);
206
207 fprintf(logfile, "%s:%s %s\n", tod, user, line);
208}
209
paul718e3742002-12-13 20:15:29 +0000210/* VTY shell main routine. */
211int
212main (int argc, char **argv, char **env)
213{
214 char *p;
215 int opt;
Paul Jakma876b8be2006-10-15 23:35:57 +0000216 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000217 int boot_flag = 0;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000218 const char *daemon_name = NULL;
219 struct cmd_rec {
220 const char *line;
221 struct cmd_rec *next;
222 } *cmd = NULL;
223 struct cmd_rec *tail = NULL;
224 int echo_command = 0;
Stephen Hemminger914131f2008-07-30 14:16:47 -0700225 int no_error = 0;
paul718e3742002-12-13 20:15:29 +0000226
227 /* Preserve name of myself. */
228 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
229
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700230 /* if logging open now */
231 if ((p = getenv("VTYSH_LOG")) != NULL)
232 logfile = fopen(p, "a");
233
paul718e3742002-12-13 20:15:29 +0000234 /* Option handling. */
235 while (1)
236 {
Stephen Hemminger914131f2008-07-30 14:16:47 -0700237 opt = getopt_long (argc, argv, "be:c:d:nEhC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000238
239 if (opt == EOF)
240 break;
241
242 switch (opt)
243 {
244 case 0:
245 break;
246 case 'b':
247 boot_flag = 1;
248 break;
249 case 'e':
hasso4991f6c2004-04-06 11:36:17 +0000250 case 'c':
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000251 {
252 struct cmd_rec *cr;
253 cr = XMALLOC(0, sizeof(*cr));
254 cr->line = optarg;
255 cr->next = NULL;
256 if (tail)
257 tail->next = cr;
258 else
259 cmd = cr;
260 tail = cr;
261 }
262 break;
263 case 'd':
264 daemon_name = optarg;
265 break;
Stephen Hemminger914131f2008-07-30 14:16:47 -0700266 case 'n':
267 no_error = 1;
268 break;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000269 case 'E':
270 echo_command = 1;
paul718e3742002-12-13 20:15:29 +0000271 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000272 case 'C':
273 dryrun = 1;
274 break;
paul718e3742002-12-13 20:15:29 +0000275 case 'h':
276 usage (0);
277 break;
paul718e3742002-12-13 20:15:29 +0000278 default:
279 usage (1);
280 break;
281 }
282 }
283
284 /* Initialize user input buffer. */
285 line_read = NULL;
Stephen Hemminger0fbd62a2008-05-02 09:25:02 -0700286 setlinebuf(stdout);
paul718e3742002-12-13 20:15:29 +0000287
288 /* Signal and others. */
hassoe42f5a32004-08-28 17:04:33 +0000289 vtysh_signal_init ();
paul718e3742002-12-13 20:15:29 +0000290
291 /* Make vty structure and register commands. */
292 vtysh_init_vty ();
293 vtysh_init_cmd ();
294 vtysh_user_init ();
295 vtysh_config_init ();
296
297 vty_init_vtysh ();
298
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000299 /* Read vtysh configuration file before connecting to daemons. */
hassoe7168df2004-10-03 20:11:32 +0000300 vtysh_read_config (config_default);
paul718e3742002-12-13 20:15:29 +0000301
Paul Jakma876b8be2006-10-15 23:35:57 +0000302 /* Start execution only if not in dry-run mode */
303 if(dryrun)
304 return(0);
305
Stephen Hemminger914131f2008-07-30 14:16:47 -0700306 /* Ignore error messages */
307 if (no_error)
308 freopen("/dev/null", "w", stdout);
309
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000310 /* Make sure we pass authentication before proceeding. */
311 vtysh_auth ();
312
313 /* Do not connect until we have passed authentication. */
314 if (vtysh_connect_all (daemon_name) <= 0)
paul718e3742002-12-13 20:15:29 +0000315 {
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000316 fprintf(stderr, "Exiting: failed to connect to any daemons.\n");
317 exit(1);
318 }
319
320 /* If eval mode. */
321 if (cmd)
322 {
323 /* Enter into enable node. */
324 vtysh_execute ("enable");
325
326 while (cmd != NULL)
327 {
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700328 int ret;
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000329 char *eol;
330
331 while ((eol = strchr(cmd->line, '\n')) != NULL)
332 {
333 *eol = '\0';
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700334
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000335 if (echo_command)
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700336 printf("%s%s\n", vtysh_prompt(), cmd->line);
337
338 if (logfile)
339 log_it(cmd->line);
340
341 ret = vtysh_execute_no_pager(cmd->line);
Stephen Hemminger914131f2008-07-30 14:16:47 -0700342 if (!no_error &&
343 ! (ret == CMD_SUCCESS ||
344 ret == CMD_SUCCESS_DAEMON ||
345 ret == CMD_WARNING))
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700346 exit(1);
347
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000348 cmd->line = eol+1;
349 }
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700350
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000351 if (echo_command)
352 printf("%s%s\n", vtysh_prompt(), cmd->line);
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700353
354 if (logfile)
355 log_it(cmd->line);
356
357 ret = vtysh_execute_no_pager(cmd->line);
Stephen Hemminger914131f2008-07-30 14:16:47 -0700358 if (!no_error &&
359 ! (ret == CMD_SUCCESS ||
360 ret == CMD_SUCCESS_DAEMON ||
361 ret == CMD_WARNING))
Stephen Hemminger57fb9742008-07-28 12:19:04 -0700362 exit(1);
Andrew J. Schorrf366ad32006-07-27 18:01:41 +0000363
364 {
365 struct cmd_rec *cr;
366 cr = cmd;
367 cmd = cmd->next;
368 XFREE(0, cr);
369 }
370 }
paul718e3742002-12-13 20:15:29 +0000371 exit (0);
372 }
373
374 /* Boot startup configuration file. */
375 if (boot_flag)
376 {
hassoe7168df2004-10-03 20:11:32 +0000377 if (vtysh_read_config (integrate_default))
378 {
379 fprintf (stderr, "Can't open configuration file [%s]\n",
380 integrate_default);
381 exit (1);
382 }
383 else
384 exit (0);
paul718e3742002-12-13 20:15:29 +0000385 }
386
387 vtysh_pager_init ();
388
389 vtysh_readline_init ();
390
391 vty_hello (vty);
392
hassoe7168df2004-10-03 20:11:32 +0000393 /* Enter into enable node. */
394 vtysh_execute ("enable");
395
paul718e3742002-12-13 20:15:29 +0000396 /* Preparation for longjmp() in sigtstp(). */
397 sigsetjmp (jmpbuf, 1);
398 jmpflag = 1;
399
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100400 snprintf(history_file, sizeof(history_file), "%s/.history_quagga", getenv("HOME"));
401 read_history(history_file);
paul718e3742002-12-13 20:15:29 +0000402 /* Main command loop. */
403 while (vtysh_rl_gets ())
404 vtysh_execute (line_read);
405
Tomasz Pala3f4ab7f2009-06-24 22:23:11 +0100406 history_truncate_file(history_file,1000);
paul718e3742002-12-13 20:15:29 +0000407 printf ("\n");
408
409 /* Rest in peace. */
410 exit (0);
411}