]>
git.proxmox.com Git - mirror_frr.git/blob - vtysh/vtysh_main.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Virtual terminal interface shell.
3 * Copyright (C) 2000 Kunihiro Ishiguro
15 /* readline carries some ancient definitions around */
16 #pragma GCC diagnostic push
17 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
18 #include <readline/readline.h>
19 #include <readline/history.h>
20 #pragma GCC diagnostic pop
23 * The append_history function only appears in newer versions
24 * of the readline library it appears like. Since we don't
25 * need this just silently ignore the code on these
28 #if !defined HAVE_APPEND_HISTORY
29 #define append_history(A, B)
32 #include <lib/version.h>
39 #include "lib_errors.h"
41 #include "vtysh/vtysh.h"
42 #include "vtysh/vtysh_user.h"
44 /* VTY shell program name. */
48 static uid_t elevuid
, realuid
;
49 static gid_t elevgid
, realgid
;
51 #define VTYSH_CONFIG_NAME "vtysh.conf"
52 #define FRR_CONFIG_NAME "frr.conf"
54 /* Configuration file name and directory. */
55 static char vtysh_config
[MAXPATHLEN
* 3];
56 char frr_config
[MAXPATHLEN
* 3];
57 char vtydir
[MAXPATHLEN
];
58 static char history_file
[MAXPATHLEN
];
60 /* Flag for indicate executing child command. */
63 /* Flag to indicate if in user/unprivileged mode. */
66 /* Master of threads. */
67 struct thread_master
*master
;
72 static void vtysh_rl_callback(char *line_read
)
76 rl_callback_handler_remove();
79 vtysh_loop_exited
= true;
83 /* If the line has any text in it, save it on the history. But only if
84 * last command in history isn't the same one.
88 last
= previous_history();
89 if (!last
|| strcmp(last
->line
, line_read
) != 0) {
90 add_history(line_read
);
91 append_history(1, history_file
);
95 vtysh_execute(line_read
);
97 if (!vtysh_loop_exited
)
98 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
103 /* SIGTSTP handler. This function care user's ^Z input. */
104 static void sigtstp(int sig
)
106 rl_callback_handler_remove();
108 /* Execute "end" command. */
109 vtysh_execute("end");
111 if (!vtysh_loop_exited
)
112 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
114 /* Initialize readline. */
117 rl_forced_update_display();
120 /* SIGINT handler. This function care user's ^Z input. */
121 static void sigint(int sig
)
123 /* Check this process is not child process. */
127 rl_forced_update_display();
131 /* Signale wrapper for vtysh. We don't use sigevent because
132 * vtysh doesn't use threads. TODO */
133 static void vtysh_signal_set(int signo
, void (*func
)(int))
135 struct sigaction sig
;
136 struct sigaction osig
;
138 sig
.sa_handler
= func
;
139 sigemptyset(&sig
.sa_mask
);
142 sig
.sa_flags
|= SA_RESTART
;
143 #endif /* SA_RESTART */
145 sigaction(signo
, &sig
, &osig
);
148 /* Initialization of signal handles. */
149 static void vtysh_signal_init(void)
151 vtysh_signal_set(SIGINT
, sigint
);
152 vtysh_signal_set(SIGTSTP
, sigtstp
);
153 vtysh_signal_set(SIGPIPE
, SIG_IGN
);
156 /* Help information display. */
157 static void usage(int status
)
160 fprintf(stderr
, "Try `%s --help' for more information.\n",
163 printf("Usage : %s [OPTION...]\n\n"
164 "Integrated shell for FRR (version " FRR_VERSION
166 "Configured with:\n " FRR_CONFIG_ARGS
168 "-b, --boot Execute boot startup configuration\n"
169 "-c, --command Execute argument as command\n"
170 "-d, --daemon Connect only to the specified daemon\n"
171 "-f, --inputfile Execute commands from specific file and exit\n"
172 "-E, --echo Echo prompt and command in -c mode\n"
173 "-C, --dryrun Check configuration for validity and exit\n"
174 "-m, --markfile Mark input file with context end\n"
175 " --vty_socket Override vty socket path\n"
176 " --config_dir Override config directory path\n"
177 "-N --pathspace Insert prefix into config & socket paths\n"
178 "-u --user Run as an unprivileged user\n"
179 "-w, --writeconfig Write integrated config (frr.conf) and exit\n"
180 "-H, --histfile Override history file\n"
181 "-h, --help Display this help and exit\n\n"
182 "Note that multiple commands may be executed from the command\n"
183 "line by passing multiple -c args, or by embedding linefeed\n"
184 "characters in one or more of the commands.\n\n"
185 "Report bugs to %s\n",
186 progname
, FRR_BUG_ADDRESS
);
191 /* VTY shell options, we use GNU getopt library. */
192 #define OPTION_VTYSOCK 1000
193 #define OPTION_CONFDIR 1001
194 struct option longopts
[] = {
195 {"boot", no_argument
, NULL
, 'b'},
196 /* For compatibility with older zebra/quagga versions */
197 {"eval", required_argument
, NULL
, 'e'},
198 {"command", required_argument
, NULL
, 'c'},
199 {"daemon", required_argument
, NULL
, 'd'},
200 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
201 {"config_dir", required_argument
, NULL
, OPTION_CONFDIR
},
202 {"inputfile", required_argument
, NULL
, 'f'},
203 {"histfile", required_argument
, NULL
, 'H'},
204 {"echo", no_argument
, NULL
, 'E'},
205 {"dryrun", no_argument
, NULL
, 'C'},
206 {"help", no_argument
, NULL
, 'h'},
207 {"noerror", no_argument
, NULL
, 'n'},
208 {"mark", no_argument
, NULL
, 'm'},
209 {"writeconfig", no_argument
, NULL
, 'w'},
210 {"pathspace", required_argument
, NULL
, 'N'},
211 {"user", no_argument
, NULL
, 'u'},
212 {"timestamp", no_argument
, NULL
, 't'},
215 bool vtysh_loop_exited
;
217 static struct thread
*vtysh_rl_read_thread
;
219 static void vtysh_rl_read(struct thread
*thread
)
221 thread_add_read(master
, vtysh_rl_read
, NULL
, STDIN_FILENO
,
222 &vtysh_rl_read_thread
);
223 rl_callback_read_char();
226 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
227 static void vtysh_rl_run(void)
229 struct thread thread
;
231 master
= thread_master_create(NULL
);
233 rl_callback_handler_install(vtysh_prompt(), vtysh_rl_callback
);
234 thread_add_read(master
, vtysh_rl_read
, NULL
, STDIN_FILENO
,
235 &vtysh_rl_read_thread
);
237 while (!vtysh_loop_exited
&& thread_fetch(master
, &thread
))
238 thread_call(&thread
);
240 if (!vtysh_loop_exited
)
241 rl_callback_handler_remove();
243 thread_master_free(master
);
246 static void log_it(const char *line
)
248 time_t t
= time(NULL
);
250 const char *user
= getenv("USER");
253 localtime_r(&t
, &tmp
);
257 strftime(tod
, sizeof(tod
), "%Y%m%d-%H:%M.%S", &tmp
);
259 fprintf(logfile
, "%s:%s %s\n", tod
, user
, line
);
264 static void vtysh_flock_config(const char *flock_file
)
268 flock_fd
= open(flock_file
, O_RDONLY
, 0644);
270 fprintf(stderr
, "Unable to create lock file: %s, %s\n",
271 flock_file
, safe_strerror(errno
));
275 while (count
< 400 && (flock(flock_fd
, LOCK_EX
| LOCK_NB
) < 0)) {
282 "Flock of %s failed, continuing this may cause issues\n",
286 static void vtysh_unflock_config(void)
288 flock(flock_fd
, LOCK_UN
);
294 if (elevuid
!= realuid
&& seteuid(elevuid
)) {
295 perror("seteuid(on)");
298 if (elevgid
!= realgid
&& setegid(elevgid
)) {
299 perror("setegid(on)");
306 if (elevuid
!= realuid
&& seteuid(realuid
)) {
307 perror("seteuid(off)");
310 if (elevgid
!= realgid
&& setegid(realgid
)) {
311 perror("setegid(off)");
316 /* VTY shell main routine. */
317 int main(int argc
, char **argv
, char **env
)
323 bool ts_flag
= false;
324 const char *daemon_name
= NULL
;
325 const char *inputfile
= NULL
;
328 struct cmd_rec
*next
;
330 struct cmd_rec
*tail
= NULL
;
331 int echo_command
= 0;
336 char *homedir
= NULL
;
338 char sysconfdir
[MAXPATHLEN
];
339 const char *pathspace_arg
= NULL
;
340 char pathspace
[MAXPATHLEN
] = "";
341 const char *histfile
= NULL
;
342 const char *histfile_env
= getenv("VTYSH_HISTFILE");
344 /* SUID: drop down to calling user & go back up when needed */
351 user_mode
= 0; /* may be set in options processing */
353 /* Preserve name of myself. */
354 progname
= ((p
= strrchr(argv
[0], '/')) ? ++p
: argv
[0]);
356 strlcpy(sysconfdir
, frr_sysconfdir
, sizeof(sysconfdir
));
359 strlcpy(vtydir
, frr_vtydir
, sizeof(vtydir
));
361 /* Option handling. */
363 opt
= getopt_long(argc
, argv
, "be:c:d:nf:H:mEhCwN:ut", longopts
,
378 cr
= XMALLOC(MTYPE_TMP
, sizeof(*cr
));
388 ditch_suid
= 1; /* option disables SUID */
389 strlcpy(vtydir
, optarg
, sizeof(vtydir
));
392 ditch_suid
= 1; /* option disables SUID */
393 snprintf(sysconfdir
, sizeof(sysconfdir
), "%s/", optarg
);
396 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
398 "slashes or dots are not permitted in the --pathspace option.\n");
401 pathspace_arg
= optarg
;
402 snprintf(pathspace
, sizeof(pathspace
), "%s/", optarg
);
405 daemon_name
= optarg
;
448 if (markfile
+ writeconfig
+ dryrun
+ boot_flag
> 1) {
450 "Invalid combination of arguments. Please specify at most one of:\n\t-b, -C, -m, -w\n");
453 if (inputfile
&& (writeconfig
|| boot_flag
)) {
455 "WARNING: Combinining the -f option with -b or -w is NOT SUPPORTED since its\nresults are inconsistent!\n");
458 snprintf(vtysh_config
, sizeof(vtysh_config
), "%s%s%s", sysconfdir
,
459 pathspace
, VTYSH_CONFIG_NAME
);
460 snprintf(frr_config
, sizeof(frr_config
), "%s%s%s", sysconfdir
,
461 pathspace
, FRR_CONFIG_NAME
);
464 strlcat(vtydir
, "/", sizeof(vtydir
));
465 strlcat(vtydir
, pathspace_arg
, sizeof(vtydir
));
468 /* Initialize user input buffer. */
471 /* Signal and others. */
474 /* Make vty structure and register commands. */
483 /* Read vtysh configuration file before connecting to daemons.
484 * (file may not be readable to calling user in SUID mode) */
486 vtysh_read_config(vtysh_config
, dryrun
);
489 /* Error code library system */
496 "-f option MUST be specified with -m option\n");
499 return (vtysh_mark_file(inputfile
));
502 /* Start execution only if not in dry-run mode */
503 if (dryrun
&& !cmd
) {
505 ret
= vtysh_read_config(inputfile
, dryrun
);
507 ret
= vtysh_read_config(frr_config
, dryrun
);
513 if (dryrun
&& cmd
&& cmd
->line
) {
515 vtysh_execute("enable");
518 char *cmdnow
= cmd
->line
, *next
;
520 next
= strchr(cmdnow
, '\n');
525 printf("%s%s\n", vtysh_prompt(),
528 ret
= vtysh_execute_no_pager(cmdnow
);
530 && !(ret
== CMD_SUCCESS
531 || ret
== CMD_SUCCESS_DAEMON
532 || ret
== CMD_WARNING
))
534 } while ((cmdnow
= next
) != NULL
);
538 XFREE(MTYPE_TMP
, cr
);
543 /* Ignore error messages */
545 if (freopen("/dev/null", "w", stdout
) == NULL
) {
547 "Exiting: Failed to duplicate stdout with -n option");
552 /* SUID: go back up elevated privs */
555 /* Make sure we pass authentication before proceeding. */
558 /* Do not connect until we have passed authentication. */
559 if (vtysh_connect_all(daemon_name
) <= 0) {
560 fprintf(stderr
, "Exiting: failed to connect to any daemons.\n");
563 "Hint: if this seems wrong, try running me as a privileged user!\n");
570 /* SUID: back down, don't need privs further on */
576 "writeconfig cannot be used when running as an unprivileged user.\n");
582 vtysh_execute("enable");
583 return vtysh_write_config_integrated();
587 vtysh_flock_config(inputfile
);
588 ret
= vtysh_read_config(inputfile
, dryrun
);
589 vtysh_unflock_config();
594 * Setup history file for use by both -c and regular input
595 * If we can't find the home directory, then don't store
596 * the history information.
597 * VTYSH_HISTFILE is preferred over command line
598 * argument (-H/--histfile).
601 strlcpy(history_file
, histfile_env
, sizeof(history_file
));
602 } else if (histfile
) {
603 strlcpy(history_file
, histfile
, sizeof(history_file
));
605 homedir
= vtysh_get_home();
607 snprintf(history_file
, sizeof(history_file
),
608 "%s/.history_frr", homedir
);
611 if (strlen(history_file
) > 0) {
612 if (read_history(history_file
) != 0) {
615 fp
= open(history_file
, O_CREAT
| O_EXCL
,
620 read_history(history_file
);
624 if (getenv("VTYSH_LOG")) {
625 const char *logpath
= getenv("VTYSH_LOG");
627 logfile
= fopen(logpath
, "a");
629 fprintf(stderr
, "Failed to open logfile (%s): %s\n",
630 logpath
, strerror(errno
));
636 if (cmd
&& cmd
->line
) {
637 /* Enter into enable node. */
639 vtysh_execute("enable");
641 vtysh_add_timestamp
= ts_flag
;
643 while (cmd
!= NULL
) {
646 while ((eol
= strchr(cmd
->line
, '\n')) != NULL
) {
649 add_history(cmd
->line
);
650 append_history(1, history_file
);
653 printf("%s%s\n", vtysh_prompt(),
659 ret
= vtysh_execute_no_pager(cmd
->line
);
661 && !(ret
== CMD_SUCCESS
662 || ret
== CMD_SUCCESS_DAEMON
663 || ret
== CMD_WARNING
))
669 add_history(cmd
->line
);
670 append_history(1, history_file
);
673 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
679 * Parsing logic for regular commands will be different
680 * than for those commands requiring further
681 * processing, such as cli instructions terminating
682 * with question-mark character.
684 if (!vtysh_execute_command_questionmark(cmd
->line
))
687 ret
= vtysh_execute_no_pager(cmd
->line
);
690 && !(ret
== CMD_SUCCESS
|| ret
== CMD_SUCCESS_DAEMON
691 || ret
== CMD_WARNING
))
698 XFREE(MTYPE_TMP
, cr
);
702 history_truncate_file(history_file
, 1000);
706 /* Boot startup configuration file. */
708 vtysh_flock_config(frr_config
);
709 ret
= vtysh_read_config(frr_config
, dryrun
);
710 vtysh_unflock_config();
713 "Configuration file[%s] processing failure: %d\n",
723 vtysh_readline_init();
727 /* Enter into enable node. */
729 vtysh_execute("enable");
731 vtysh_add_timestamp
= ts_flag
;
733 /* Main command loop. */
738 history_truncate_file(history_file
, 1000);