]>
git.proxmox.com Git - mirror_frr.git/blob - vtysh/vtysh_main.c
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 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
30 /* readline carries some ancient definitions around */
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
33 #include <readline/readline.h>
34 #include <readline/history.h>
35 #pragma GCC diagnostic pop
38 * The append_history function only appears in newer versions
39 * of the readline library it appears like. Since we don't
40 * need this just silently ignore the code on these
43 #if !defined HAVE_APPEND_HISTORY
44 #define append_history(A, B)
47 #include <lib/version.h>
54 #include "lib_errors.h"
56 #include "vtysh/vtysh.h"
57 #include "vtysh/vtysh_user.h"
59 /* VTY shell program name. */
63 static uid_t elevuid
, realuid
;
64 static gid_t elevgid
, realgid
;
66 #define VTYSH_CONFIG_NAME "vtysh.conf"
67 #define FRR_CONFIG_NAME "frr.conf"
69 /* Configuration file name and directory. */
70 static char vtysh_config
[MAXPATHLEN
* 3];
71 char frr_config
[MAXPATHLEN
* 3];
72 char vtydir
[MAXPATHLEN
];
73 static char history_file
[MAXPATHLEN
];
75 /* Flag for indicate executing child command. */
78 /* Flag to indicate if in user/unprivileged mode. */
81 /* For sigsetjmp() & siglongjmp(). */
82 static sigjmp_buf jmpbuf
;
84 /* Flag for avoid recursive siglongjmp() call. */
85 static int jmpflag
= 0;
87 /* Master of threads. */
88 struct thread_master
*master
;
93 /* SIGTSTP handler. This function care user's ^Z input. */
94 static void sigtstp(int sig
)
96 /* Execute "end" command. */
99 /* Initialize readline. */
103 /* Check jmpflag for duplicate siglongjmp(). */
109 /* Back to main command loop. */
110 siglongjmp(jmpbuf
, 1);
113 /* SIGINT handler. This function care user's ^Z input. */
114 static void sigint(int sig
)
116 /* Check this process is not child process. */
120 rl_forced_update_display();
124 /* Signale wrapper for vtysh. We don't use sigevent because
125 * vtysh doesn't use threads. TODO */
126 static void vtysh_signal_set(int signo
, void (*func
)(int))
128 struct sigaction sig
;
129 struct sigaction osig
;
131 sig
.sa_handler
= func
;
132 sigemptyset(&sig
.sa_mask
);
135 sig
.sa_flags
|= SA_RESTART
;
136 #endif /* SA_RESTART */
138 sigaction(signo
, &sig
, &osig
);
141 /* Initialization of signal handles. */
142 static void vtysh_signal_init(void)
144 vtysh_signal_set(SIGINT
, sigint
);
145 vtysh_signal_set(SIGTSTP
, sigtstp
);
146 vtysh_signal_set(SIGPIPE
, SIG_IGN
);
149 /* Help information display. */
150 static void usage(int status
)
153 fprintf(stderr
, "Try `%s --help' for more information.\n",
156 printf("Usage : %s [OPTION...]\n\n"
157 "Integrated shell for FRR (version " FRR_VERSION
159 "Configured with:\n " FRR_CONFIG_ARGS
161 "-b, --boot Execute boot startup configuration\n"
162 "-c, --command Execute argument as command\n"
163 "-d, --daemon Connect only to the specified daemon\n"
164 "-f, --inputfile Execute commands from specific file and exit\n"
165 "-E, --echo Echo prompt and command in -c mode\n"
166 "-C, --dryrun Check configuration for validity and exit\n"
167 "-m, --markfile Mark input file with context end\n"
168 " --vty_socket Override vty socket path\n"
169 " --config_dir Override config directory path\n"
170 "-N --pathspace Insert prefix into config & socket paths\n"
171 "-u --user Run as an unprivileged user\n"
172 "-w, --writeconfig Write integrated config (frr.conf) and exit\n"
173 "-H, --histfile Override history file\n"
174 "-h, --help Display this help and exit\n\n"
175 "Note that multiple commands may be executed from the command\n"
176 "line by passing multiple -c args, or by embedding linefeed\n"
177 "characters in one or more of the commands.\n\n"
178 "Report bugs to %s\n",
179 progname
, FRR_BUG_ADDRESS
);
184 /* VTY shell options, we use GNU getopt library. */
185 #define OPTION_VTYSOCK 1000
186 #define OPTION_CONFDIR 1001
187 struct option longopts
[] = {
188 {"boot", no_argument
, NULL
, 'b'},
189 /* For compatibility with older zebra/quagga versions */
190 {"eval", required_argument
, NULL
, 'e'},
191 {"command", required_argument
, NULL
, 'c'},
192 {"daemon", required_argument
, NULL
, 'd'},
193 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
194 {"config_dir", required_argument
, NULL
, OPTION_CONFDIR
},
195 {"inputfile", required_argument
, NULL
, 'f'},
196 {"histfile", required_argument
, NULL
, 'H'},
197 {"echo", no_argument
, NULL
, 'E'},
198 {"dryrun", no_argument
, NULL
, 'C'},
199 {"help", no_argument
, NULL
, 'h'},
200 {"noerror", no_argument
, NULL
, 'n'},
201 {"mark", no_argument
, NULL
, 'm'},
202 {"writeconfig", no_argument
, NULL
, 'w'},
203 {"pathspace", required_argument
, NULL
, 'N'},
204 {"user", no_argument
, NULL
, 'u'},
205 {"timestamp", no_argument
, NULL
, 't'},
208 bool vtysh_loop_exited
;
210 static void vtysh_rl_callback(char *line_read
)
214 rl_callback_handler_remove();
217 vtysh_loop_exited
= true;
221 /* If the line has any text in it, save it on the history. But only if
222 * last command in history isn't the same one. */
225 last
= previous_history();
226 if (!last
|| strcmp(last
->line
, line_read
) != 0) {
227 add_history(line_read
);
228 append_history(1, history_file
);
232 vtysh_execute(line_read
);
234 if (!vtysh_loop_exited
)
235 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
238 static struct thread
*vtysh_rl_read_thread
;
240 static void vtysh_rl_read(struct thread
*thread
)
242 thread_add_read(master
, vtysh_rl_read
, NULL
, STDIN_FILENO
,
243 &vtysh_rl_read_thread
);
244 rl_callback_read_char();
247 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
248 static void vtysh_rl_run(void)
250 struct thread thread
;
252 master
= thread_master_create(NULL
);
254 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
255 thread_add_read(master
, vtysh_rl_read
, NULL
, STDIN_FILENO
,
256 &vtysh_rl_read_thread
);
258 while (!vtysh_loop_exited
&& thread_fetch(master
, &thread
))
259 thread_call(&thread
);
261 if (!vtysh_loop_exited
)
262 rl_callback_handler_remove();
264 thread_master_free(master
);
267 static void log_it(const char *line
)
269 time_t t
= time(NULL
);
271 const char *user
= getenv("USER");
274 localtime_r(&t
, &tmp
);
278 strftime(tod
, sizeof(tod
), "%Y%m%d-%H:%M.%S", &tmp
);
280 fprintf(logfile
, "%s:%s %s\n", tod
, user
, line
);
285 static void vtysh_flock_config(const char *flock_file
)
289 flock_fd
= open(flock_file
, O_RDONLY
, 0644);
291 fprintf(stderr
, "Unable to create lock file: %s, %s\n",
292 flock_file
, safe_strerror(errno
));
296 while (count
< 400 && (flock(flock_fd
, LOCK_EX
| LOCK_NB
) < 0)) {
303 "Flock of %s failed, continuing this may cause issues\n",
307 static void vtysh_unflock_config(void)
309 flock(flock_fd
, LOCK_UN
);
315 if (elevuid
!= realuid
&& seteuid(elevuid
)) {
316 perror("seteuid(on)");
319 if (elevgid
!= realgid
&& setegid(elevgid
)) {
320 perror("setegid(on)");
327 if (elevuid
!= realuid
&& seteuid(realuid
)) {
328 perror("seteuid(off)");
331 if (elevgid
!= realgid
&& setegid(realgid
)) {
332 perror("setegid(off)");
337 /* VTY shell main routine. */
338 int main(int argc
, char **argv
, char **env
)
344 bool ts_flag
= false;
345 const char *daemon_name
= NULL
;
346 const char *inputfile
= NULL
;
349 struct cmd_rec
*next
;
351 struct cmd_rec
*tail
= NULL
;
352 int echo_command
= 0;
357 char *homedir
= NULL
;
359 char sysconfdir
[MAXPATHLEN
];
360 const char *pathspace_arg
= NULL
;
361 char pathspace
[MAXPATHLEN
] = "";
362 const char *histfile
= NULL
;
364 /* SUID: drop down to calling user & go back up when needed */
371 user_mode
= 0; /* may be set in options processing */
373 /* Preserve name of myself. */
374 progname
= ((p
= strrchr(argv
[0], '/')) ? ++p
: argv
[0]);
376 strlcpy(sysconfdir
, frr_sysconfdir
, sizeof(sysconfdir
));
379 strlcpy(vtydir
, frr_vtydir
, sizeof(vtydir
));
381 /* Option handling. */
383 opt
= getopt_long(argc
, argv
, "be:c:d:nf:H:mEhCwN:ut", longopts
,
398 cr
= XMALLOC(MTYPE_TMP
, sizeof(*cr
));
408 ditch_suid
= 1; /* option disables SUID */
409 strlcpy(vtydir
, optarg
, sizeof(vtydir
));
412 ditch_suid
= 1; /* option disables SUID */
413 snprintf(sysconfdir
, sizeof(sysconfdir
), "%s/", optarg
);
416 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
418 "slashes or dots are not permitted in the --pathspace option.\n");
421 pathspace_arg
= optarg
;
422 snprintf(pathspace
, sizeof(pathspace
), "%s/", optarg
);
425 daemon_name
= optarg
;
468 if (markfile
+ writeconfig
+ dryrun
+ boot_flag
> 1) {
470 "Invalid combination of arguments. Please specify at most one of:\n\t-b, -C, -m, -w\n");
473 if (inputfile
&& (writeconfig
|| boot_flag
)) {
475 "WARNING: Combinining the -f option with -b or -w is NOT SUPPORTED since its\nresults are inconsistent!\n");
478 snprintf(vtysh_config
, sizeof(vtysh_config
), "%s%s%s", sysconfdir
,
479 pathspace
, VTYSH_CONFIG_NAME
);
480 snprintf(frr_config
, sizeof(frr_config
), "%s%s%s", sysconfdir
,
481 pathspace
, FRR_CONFIG_NAME
);
484 strlcat(vtydir
, "/", sizeof(vtydir
));
485 strlcat(vtydir
, pathspace_arg
, sizeof(vtydir
));
488 /* Initialize user input buffer. */
491 /* Signal and others. */
494 /* Make vty structure and register commands. */
503 /* Read vtysh configuration file before connecting to daemons.
504 * (file may not be readable to calling user in SUID mode) */
506 vtysh_read_config(vtysh_config
, dryrun
);
509 /* Error code library system */
516 "-f option MUST be specified with -m option\n");
519 return (vtysh_mark_file(inputfile
));
522 /* Start execution only if not in dry-run mode */
523 if (dryrun
&& !cmd
) {
525 ret
= vtysh_read_config(inputfile
, dryrun
);
527 ret
= vtysh_read_config(frr_config
, dryrun
);
533 if (dryrun
&& cmd
&& cmd
->line
) {
535 vtysh_execute("enable");
538 char *cmdnow
= cmd
->line
, *next
;
540 next
= strchr(cmdnow
, '\n');
545 printf("%s%s\n", vtysh_prompt(),
548 ret
= vtysh_execute_no_pager(cmdnow
);
550 && !(ret
== CMD_SUCCESS
551 || ret
== CMD_SUCCESS_DAEMON
552 || ret
== CMD_WARNING
))
554 } while ((cmdnow
= next
) != NULL
);
558 XFREE(MTYPE_TMP
, cr
);
563 /* Ignore error messages */
565 if (freopen("/dev/null", "w", stdout
) == NULL
) {
567 "Exiting: Failed to duplicate stdout with -n option");
572 /* SUID: go back up elevated privs */
575 /* Make sure we pass authentication before proceeding. */
578 /* Do not connect until we have passed authentication. */
579 if (vtysh_connect_all(daemon_name
) <= 0) {
580 fprintf(stderr
, "Exiting: failed to connect to any daemons.\n");
583 "Hint: if this seems wrong, try running me as a privileged user!\n");
590 /* SUID: back down, don't need privs further on */
596 "writeconfig cannot be used when running as an unprivileged user.\n");
602 vtysh_execute("enable");
603 return vtysh_write_config_integrated();
607 vtysh_flock_config(inputfile
);
608 ret
= vtysh_read_config(inputfile
, dryrun
);
609 vtysh_unflock_config();
614 * Setup history file for use by both -c and regular input
615 * If we can't find the home directory, then don't store
616 * the history information.
617 * VTYSH_HISTFILE is preferred over command line
618 * argument (-H/--histfile).
620 if (getenv("VTYSH_HISTFILE")) {
621 const char *file
= getenv("VTYSH_HISTFILE");
623 strlcpy(history_file
, file
, sizeof(history_file
));
624 } else if (histfile
) {
625 strlcpy(history_file
, histfile
, sizeof(history_file
));
627 homedir
= vtysh_get_home();
629 snprintf(history_file
, sizeof(history_file
),
630 "%s/.history_frr", homedir
);
633 if (strlen(history_file
) > 0) {
634 if (read_history(history_file
) != 0) {
637 fp
= open(history_file
, O_CREAT
| O_EXCL
,
642 read_history(history_file
);
646 if (getenv("VTYSH_LOG")) {
647 const char *logpath
= getenv("VTYSH_LOG");
649 logfile
= fopen(logpath
, "a");
651 fprintf(stderr
, "Failed to open logfile (%s): %s\n",
652 logpath
, strerror(errno
));
658 if (cmd
&& cmd
->line
) {
659 /* Enter into enable node. */
661 vtysh_execute("enable");
663 vtysh_add_timestamp
= ts_flag
;
665 while (cmd
!= NULL
) {
668 while ((eol
= strchr(cmd
->line
, '\n')) != NULL
) {
671 add_history(cmd
->line
);
672 append_history(1, history_file
);
675 printf("%s%s\n", vtysh_prompt(),
681 ret
= vtysh_execute_no_pager(cmd
->line
);
683 && !(ret
== CMD_SUCCESS
684 || ret
== CMD_SUCCESS_DAEMON
685 || ret
== CMD_WARNING
))
691 add_history(cmd
->line
);
692 append_history(1, history_file
);
695 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
701 * Parsing logic for regular commands will be different
702 * than for those commands requiring further
703 * processing, such as cli instructions terminating
704 * with question-mark character.
706 if (!vtysh_execute_command_questionmark(cmd
->line
))
709 ret
= vtysh_execute_no_pager(cmd
->line
);
712 && !(ret
== CMD_SUCCESS
|| ret
== CMD_SUCCESS_DAEMON
713 || ret
== CMD_WARNING
))
720 XFREE(MTYPE_TMP
, cr
);
724 history_truncate_file(history_file
, 1000);
728 /* Boot startup configuration file. */
730 vtysh_flock_config(frr_config
);
731 ret
= vtysh_read_config(frr_config
, dryrun
);
732 vtysh_unflock_config();
735 "Configuration file[%s] processing failure: %d\n",
745 vtysh_readline_init();
749 /* Enter into enable node. */
751 vtysh_execute("enable");
753 vtysh_add_timestamp
= ts_flag
;
755 /* Preparation for longjmp() in sigtstp(). */
756 sigsetjmp(jmpbuf
, 1);
759 /* Main command loop. */
764 history_truncate_file(history_file
, 1000);