]>
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 /* Master of threads. */
82 struct thread_master
*master
;
87 static void vtysh_rl_callback(char *line_read
)
91 rl_callback_handler_remove();
94 vtysh_loop_exited
= true;
98 /* If the line has any text in it, save it on the history. But only if
99 * last command in history isn't the same one.
103 last
= previous_history();
104 if (!last
|| strcmp(last
->line
, line_read
) != 0) {
105 add_history(line_read
);
106 append_history(1, history_file
);
110 vtysh_execute(line_read
);
112 if (!vtysh_loop_exited
)
113 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
118 /* SIGTSTP handler. This function care user's ^Z input. */
119 static void sigtstp(int sig
)
121 rl_callback_handler_remove();
123 /* Execute "end" command. */
124 vtysh_execute("end");
126 if (!vtysh_loop_exited
)
127 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
129 /* Initialize readline. */
132 rl_forced_update_display();
135 /* SIGINT handler. This function care user's ^Z input. */
136 static void sigint(int sig
)
138 /* Check this process is not child process. */
142 rl_forced_update_display();
146 /* Signale wrapper for vtysh. We don't use sigevent because
147 * vtysh doesn't use threads. TODO */
148 static void vtysh_signal_set(int signo
, void (*func
)(int))
150 struct sigaction sig
;
151 struct sigaction osig
;
153 sig
.sa_handler
= func
;
154 sigemptyset(&sig
.sa_mask
);
157 sig
.sa_flags
|= SA_RESTART
;
158 #endif /* SA_RESTART */
160 sigaction(signo
, &sig
, &osig
);
163 /* Initialization of signal handles. */
164 static void vtysh_signal_init(void)
166 vtysh_signal_set(SIGINT
, sigint
);
167 vtysh_signal_set(SIGTSTP
, sigtstp
);
168 vtysh_signal_set(SIGPIPE
, SIG_IGN
);
171 /* Help information display. */
172 static void usage(int status
)
175 fprintf(stderr
, "Try `%s --help' for more information.\n",
178 printf("Usage : %s [OPTION...]\n\n"
179 "Integrated shell for FRR (version " FRR_VERSION
181 "Configured with:\n " FRR_CONFIG_ARGS
183 "-b, --boot Execute boot startup configuration\n"
184 "-c, --command Execute argument as command\n"
185 "-d, --daemon Connect only to the specified daemon\n"
186 "-f, --inputfile Execute commands from specific file and exit\n"
187 "-E, --echo Echo prompt and command in -c mode\n"
188 "-C, --dryrun Check configuration for validity and exit\n"
189 "-m, --markfile Mark input file with context end\n"
190 " --vty_socket Override vty socket path\n"
191 " --config_dir Override config directory path\n"
192 "-N --pathspace Insert prefix into config & socket paths\n"
193 "-u --user Run as an unprivileged user\n"
194 "-w, --writeconfig Write integrated config (frr.conf) and exit\n"
195 "-H, --histfile Override history file\n"
196 "-h, --help Display this help and exit\n\n"
197 "Note that multiple commands may be executed from the command\n"
198 "line by passing multiple -c args, or by embedding linefeed\n"
199 "characters in one or more of the commands.\n\n"
200 "Report bugs to %s\n",
201 progname
, FRR_BUG_ADDRESS
);
206 /* VTY shell options, we use GNU getopt library. */
207 #define OPTION_VTYSOCK 1000
208 #define OPTION_CONFDIR 1001
209 struct option longopts
[] = {
210 {"boot", no_argument
, NULL
, 'b'},
211 /* For compatibility with older zebra/quagga versions */
212 {"eval", required_argument
, NULL
, 'e'},
213 {"command", required_argument
, NULL
, 'c'},
214 {"daemon", required_argument
, NULL
, 'd'},
215 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
216 {"config_dir", required_argument
, NULL
, OPTION_CONFDIR
},
217 {"inputfile", required_argument
, NULL
, 'f'},
218 {"histfile", required_argument
, NULL
, 'H'},
219 {"echo", no_argument
, NULL
, 'E'},
220 {"dryrun", no_argument
, NULL
, 'C'},
221 {"help", no_argument
, NULL
, 'h'},
222 {"noerror", no_argument
, NULL
, 'n'},
223 {"mark", no_argument
, NULL
, 'm'},
224 {"writeconfig", no_argument
, NULL
, 'w'},
225 {"pathspace", required_argument
, NULL
, 'N'},
226 {"user", no_argument
, NULL
, 'u'},
227 {"timestamp", no_argument
, NULL
, 't'},
230 bool vtysh_loop_exited
;
232 static struct thread
*vtysh_rl_read_thread
;
234 static void vtysh_rl_read(struct thread
*thread
)
236 thread_add_read(master
, vtysh_rl_read
, NULL
, STDIN_FILENO
,
237 &vtysh_rl_read_thread
);
238 rl_callback_read_char();
241 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
242 static void vtysh_rl_run(void)
244 struct thread thread
;
246 master
= thread_master_create(NULL
);
248 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
249 thread_add_read(master
, vtysh_rl_read
, NULL
, STDIN_FILENO
,
250 &vtysh_rl_read_thread
);
252 while (!vtysh_loop_exited
&& thread_fetch(master
, &thread
))
253 thread_call(&thread
);
255 if (!vtysh_loop_exited
)
256 rl_callback_handler_remove();
258 thread_master_free(master
);
261 static void log_it(const char *line
)
263 time_t t
= time(NULL
);
265 const char *user
= getenv("USER");
268 localtime_r(&t
, &tmp
);
272 strftime(tod
, sizeof(tod
), "%Y%m%d-%H:%M.%S", &tmp
);
274 fprintf(logfile
, "%s:%s %s\n", tod
, user
, line
);
279 static void vtysh_flock_config(const char *flock_file
)
283 flock_fd
= open(flock_file
, O_RDONLY
, 0644);
285 fprintf(stderr
, "Unable to create lock file: %s, %s\n",
286 flock_file
, safe_strerror(errno
));
290 while (count
< 400 && (flock(flock_fd
, LOCK_EX
| LOCK_NB
) < 0)) {
297 "Flock of %s failed, continuing this may cause issues\n",
301 static void vtysh_unflock_config(void)
303 flock(flock_fd
, LOCK_UN
);
309 if (elevuid
!= realuid
&& seteuid(elevuid
)) {
310 perror("seteuid(on)");
313 if (elevgid
!= realgid
&& setegid(elevgid
)) {
314 perror("setegid(on)");
321 if (elevuid
!= realuid
&& seteuid(realuid
)) {
322 perror("seteuid(off)");
325 if (elevgid
!= realgid
&& setegid(realgid
)) {
326 perror("setegid(off)");
331 /* VTY shell main routine. */
332 int main(int argc
, char **argv
, char **env
)
338 bool ts_flag
= false;
339 const char *daemon_name
= NULL
;
340 const char *inputfile
= NULL
;
343 struct cmd_rec
*next
;
345 struct cmd_rec
*tail
= NULL
;
346 int echo_command
= 0;
351 char *homedir
= NULL
;
353 char sysconfdir
[MAXPATHLEN
];
354 const char *pathspace_arg
= NULL
;
355 char pathspace
[MAXPATHLEN
] = "";
356 const char *histfile
= NULL
;
357 const char *histfile_env
= getenv("VTYSH_HISTFILE");
359 /* SUID: drop down to calling user & go back up when needed */
366 user_mode
= 0; /* may be set in options processing */
368 /* Preserve name of myself. */
369 progname
= ((p
= strrchr(argv
[0], '/')) ? ++p
: argv
[0]);
371 strlcpy(sysconfdir
, frr_sysconfdir
, sizeof(sysconfdir
));
374 strlcpy(vtydir
, frr_vtydir
, sizeof(vtydir
));
376 /* Option handling. */
378 opt
= getopt_long(argc
, argv
, "be:c:d:nf:H:mEhCwN:ut", longopts
,
393 cr
= XMALLOC(MTYPE_TMP
, sizeof(*cr
));
403 ditch_suid
= 1; /* option disables SUID */
404 strlcpy(vtydir
, optarg
, sizeof(vtydir
));
407 ditch_suid
= 1; /* option disables SUID */
408 snprintf(sysconfdir
, sizeof(sysconfdir
), "%s/", optarg
);
411 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
413 "slashes or dots are not permitted in the --pathspace option.\n");
416 pathspace_arg
= optarg
;
417 snprintf(pathspace
, sizeof(pathspace
), "%s/", optarg
);
420 daemon_name
= optarg
;
463 if (markfile
+ writeconfig
+ dryrun
+ boot_flag
> 1) {
465 "Invalid combination of arguments. Please specify at most one of:\n\t-b, -C, -m, -w\n");
468 if (inputfile
&& (writeconfig
|| boot_flag
)) {
470 "WARNING: Combinining the -f option with -b or -w is NOT SUPPORTED since its\nresults are inconsistent!\n");
473 snprintf(vtysh_config
, sizeof(vtysh_config
), "%s%s%s", sysconfdir
,
474 pathspace
, VTYSH_CONFIG_NAME
);
475 snprintf(frr_config
, sizeof(frr_config
), "%s%s%s", sysconfdir
,
476 pathspace
, FRR_CONFIG_NAME
);
479 strlcat(vtydir
, "/", sizeof(vtydir
));
480 strlcat(vtydir
, pathspace_arg
, sizeof(vtydir
));
483 /* Initialize user input buffer. */
486 /* Signal and others. */
489 /* Make vty structure and register commands. */
498 /* Read vtysh configuration file before connecting to daemons.
499 * (file may not be readable to calling user in SUID mode) */
501 vtysh_read_config(vtysh_config
, dryrun
);
504 /* Error code library system */
511 "-f option MUST be specified with -m option\n");
514 return (vtysh_mark_file(inputfile
));
517 /* Start execution only if not in dry-run mode */
518 if (dryrun
&& !cmd
) {
520 ret
= vtysh_read_config(inputfile
, dryrun
);
522 ret
= vtysh_read_config(frr_config
, dryrun
);
528 if (dryrun
&& cmd
&& cmd
->line
) {
530 vtysh_execute("enable");
533 char *cmdnow
= cmd
->line
, *next
;
535 next
= strchr(cmdnow
, '\n');
540 printf("%s%s\n", vtysh_prompt(),
543 ret
= vtysh_execute_no_pager(cmdnow
);
545 && !(ret
== CMD_SUCCESS
546 || ret
== CMD_SUCCESS_DAEMON
547 || ret
== CMD_WARNING
))
549 } while ((cmdnow
= next
) != NULL
);
553 XFREE(MTYPE_TMP
, cr
);
558 /* Ignore error messages */
560 if (freopen("/dev/null", "w", stdout
) == NULL
) {
562 "Exiting: Failed to duplicate stdout with -n option");
567 /* SUID: go back up elevated privs */
570 /* Make sure we pass authentication before proceeding. */
573 /* Do not connect until we have passed authentication. */
574 if (vtysh_connect_all(daemon_name
) <= 0) {
575 fprintf(stderr
, "Exiting: failed to connect to any daemons.\n");
578 "Hint: if this seems wrong, try running me as a privileged user!\n");
585 /* SUID: back down, don't need privs further on */
591 "writeconfig cannot be used when running as an unprivileged user.\n");
597 vtysh_execute("enable");
598 return vtysh_write_config_integrated();
602 vtysh_flock_config(inputfile
);
603 ret
= vtysh_read_config(inputfile
, dryrun
);
604 vtysh_unflock_config();
609 * Setup history file for use by both -c and regular input
610 * If we can't find the home directory, then don't store
611 * the history information.
612 * VTYSH_HISTFILE is preferred over command line
613 * argument (-H/--histfile).
616 strlcpy(history_file
, histfile_env
, sizeof(history_file
));
617 } else if (histfile
) {
618 strlcpy(history_file
, histfile
, sizeof(history_file
));
620 homedir
= vtysh_get_home();
622 snprintf(history_file
, sizeof(history_file
),
623 "%s/.history_frr", homedir
);
626 if (strlen(history_file
) > 0) {
627 if (read_history(history_file
) != 0) {
630 fp
= open(history_file
, O_CREAT
| O_EXCL
,
635 read_history(history_file
);
639 if (getenv("VTYSH_LOG")) {
640 const char *logpath
= getenv("VTYSH_LOG");
642 logfile
= fopen(logpath
, "a");
644 fprintf(stderr
, "Failed to open logfile (%s): %s\n",
645 logpath
, strerror(errno
));
651 if (cmd
&& cmd
->line
) {
652 /* Enter into enable node. */
654 vtysh_execute("enable");
656 vtysh_add_timestamp
= ts_flag
;
658 while (cmd
!= NULL
) {
661 while ((eol
= strchr(cmd
->line
, '\n')) != NULL
) {
664 add_history(cmd
->line
);
665 append_history(1, history_file
);
668 printf("%s%s\n", vtysh_prompt(),
674 ret
= vtysh_execute_no_pager(cmd
->line
);
676 && !(ret
== CMD_SUCCESS
677 || ret
== CMD_SUCCESS_DAEMON
678 || ret
== CMD_WARNING
))
684 add_history(cmd
->line
);
685 append_history(1, history_file
);
688 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
694 * Parsing logic for regular commands will be different
695 * than for those commands requiring further
696 * processing, such as cli instructions terminating
697 * with question-mark character.
699 if (!vtysh_execute_command_questionmark(cmd
->line
))
702 ret
= vtysh_execute_no_pager(cmd
->line
);
705 && !(ret
== CMD_SUCCESS
|| ret
== CMD_SUCCESS_DAEMON
706 || ret
== CMD_WARNING
))
713 XFREE(MTYPE_TMP
, cr
);
717 history_truncate_file(history_file
, 1000);
721 /* Boot startup configuration file. */
723 vtysh_flock_config(frr_config
);
724 ret
= vtysh_read_config(frr_config
, dryrun
);
725 vtysh_unflock_config();
728 "Configuration file[%s] processing failure: %d\n",
738 vtysh_readline_init();
742 /* Enter into enable node. */
744 vtysh_execute("enable");
746 vtysh_add_timestamp
= ts_flag
;
748 /* Main command loop. */
753 history_truncate_file(history_file
, 1000);