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