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