]>
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 #include <readline/readline.h>
31 #include <readline/history.h>
34 * The append_history function only appears in newer versions
35 * of the readline library it appears like. Since we don't
36 * need this just silently ignore the code on these
39 #if !defined HAVE_APPEND_HISTORY
40 #define append_history(A, B)
43 #include <lib/version.h>
50 #include "lib_errors.h"
52 #include "vtysh/vtysh.h"
53 #include "vtysh/vtysh_user.h"
55 /* VTY shell program name. */
59 static uid_t elevuid
, realuid
;
60 static gid_t elevgid
, realgid
;
62 #define VTYSH_CONFIG_NAME "vtysh.conf"
63 #define FRR_CONFIG_NAME "frr.conf"
65 /* Configuration file name and directory. */
66 static char vtysh_config
[MAXPATHLEN
* 3];
67 char frr_config
[MAXPATHLEN
* 3];
68 char vtydir
[MAXPATHLEN
];
69 static char history_file
[MAXPATHLEN
];
71 /* Flag for indicate executing child command. */
74 /* Flag to indicate if in user/unprivileged mode. */
77 /* For sigsetjmp() & siglongjmp(). */
78 static sigjmp_buf jmpbuf
;
80 /* Flag for avoid recursive siglongjmp() call. */
81 static int jmpflag
= 0;
83 /* A static variable for holding the line. */
84 static char *line_read
;
86 /* Master of threads. */
87 struct thread_master
*master
;
92 /* SIGTSTP handler. This function care user's ^Z input. */
93 static void sigtstp(int sig
)
95 /* Execute "end" command. */
98 /* Initialize readline. */
102 /* Check jmpflag for duplicate siglongjmp(). */
108 /* Back to main command loop. */
109 siglongjmp(jmpbuf
, 1);
112 /* SIGINT handler. This function care user's ^Z input. */
113 static void sigint(int sig
)
115 /* Check this process is not child process. */
119 rl_forced_update_display();
123 /* Signale wrapper for vtysh. We don't use sigevent because
124 * vtysh doesn't use threads. TODO */
125 static void vtysh_signal_set(int signo
, void (*func
)(int))
127 struct sigaction sig
;
128 struct sigaction osig
;
130 sig
.sa_handler
= func
;
131 sigemptyset(&sig
.sa_mask
);
134 sig
.sa_flags
|= SA_RESTART
;
135 #endif /* SA_RESTART */
137 sigaction(signo
, &sig
, &osig
);
140 /* Initialization of signal handles. */
141 static void vtysh_signal_init(void)
143 vtysh_signal_set(SIGINT
, sigint
);
144 vtysh_signal_set(SIGTSTP
, sigtstp
);
145 vtysh_signal_set(SIGPIPE
, SIG_IGN
);
148 /* Help information display. */
149 static void usage(int status
)
152 fprintf(stderr
, "Try `%s --help' for more information.\n",
155 printf("Usage : %s [OPTION...]\n\n"
156 "Integrated shell for FRR (version " FRR_VERSION
158 "Configured with:\n " FRR_CONFIG_ARGS
160 "-b, --boot Execute boot startup configuration\n"
161 "-c, --command Execute argument as command\n"
162 "-d, --daemon Connect only to the specified daemon\n"
163 "-f, --inputfile Execute commands from specific file and exit\n"
164 "-E, --echo Echo prompt and command in -c mode\n"
165 "-C, --dryrun Check configuration for validity and exit\n"
166 "-m, --markfile Mark input file with context end\n"
167 " --vty_socket Override vty socket path\n"
168 " --config_dir Override config directory path\n"
169 "-N --pathspace Insert prefix into config & socket paths\n"
170 "-u --user Run as an unprivileged user\n"
171 "-w, --writeconfig Write integrated config (frr.conf) and exit\n"
172 "-H, --histfile Override history file\n"
173 "-h, --help Display this help and exit\n\n"
174 "Note that multiple commands may be executed from the command\n"
175 "line by passing multiple -c args, or by embedding linefeed\n"
176 "characters in one or more of the commands.\n\n"
177 "Report bugs to %s\n",
178 progname
, FRR_BUG_ADDRESS
);
183 /* VTY shell options, we use GNU getopt library. */
184 #define OPTION_VTYSOCK 1000
185 #define OPTION_CONFDIR 1001
186 struct option longopts
[] = {
187 {"boot", no_argument
, NULL
, 'b'},
188 /* For compatibility with older zebra/quagga versions */
189 {"eval", required_argument
, NULL
, 'e'},
190 {"command", required_argument
, NULL
, 'c'},
191 {"daemon", required_argument
, NULL
, 'd'},
192 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
193 {"config_dir", required_argument
, NULL
, OPTION_CONFDIR
},
194 {"inputfile", required_argument
, NULL
, 'f'},
195 {"histfile", required_argument
, NULL
, 'H'},
196 {"echo", no_argument
, NULL
, 'E'},
197 {"dryrun", no_argument
, NULL
, 'C'},
198 {"help", no_argument
, NULL
, 'h'},
199 {"noerror", no_argument
, NULL
, 'n'},
200 {"mark", no_argument
, NULL
, 'm'},
201 {"writeconfig", no_argument
, NULL
, 'w'},
202 {"pathspace", required_argument
, NULL
, 'N'},
203 {"user", no_argument
, NULL
, 'u'},
204 {"timestamp", no_argument
, NULL
, 't'},
207 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
208 static char *vtysh_rl_gets(void)
211 /* If the buffer has already been allocated, return the memory
212 * to the free pool. */
218 /* Get a line from the user. Change prompt according to node. XXX. */
219 line_read
= readline(vtysh_prompt());
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. */
223 if (line_read
&& *line_read
) {
225 last
= previous_history();
226 if (!last
|| strcmp(last
->line
, line_read
) != 0) {
227 add_history(line_read
);
228 append_history(1, history_file
);
235 static void log_it(const char *line
)
237 time_t t
= time(NULL
);
239 const char *user
= getenv("USER");
242 localtime_r(&t
, &tmp
);
246 strftime(tod
, sizeof(tod
), "%Y%m%d-%H:%M.%S", &tmp
);
248 fprintf(logfile
, "%s:%s %s\n", tod
, user
, line
);
253 static void vtysh_flock_config(const char *flock_file
)
257 flock_fd
= open(flock_file
, O_RDONLY
, 0644);
259 fprintf(stderr
, "Unable to create lock file: %s, %s\n",
260 flock_file
, safe_strerror(errno
));
264 while (count
< 400 && (flock(flock_fd
, LOCK_EX
| LOCK_NB
) < 0)) {
271 "Flock of %s failed, continuing this may cause issues\n",
275 static void vtysh_unflock_config(void)
277 flock(flock_fd
, LOCK_UN
);
283 if (elevuid
!= realuid
&& seteuid(elevuid
)) {
284 perror("seteuid(on)");
287 if (elevgid
!= realgid
&& setegid(elevgid
)) {
288 perror("setegid(on)");
295 if (elevuid
!= realuid
&& seteuid(realuid
)) {
296 perror("seteuid(off)");
299 if (elevgid
!= realgid
&& setegid(realgid
)) {
300 perror("setegid(off)");
305 /* VTY shell main routine. */
306 int main(int argc
, char **argv
, char **env
)
312 bool ts_flag
= false;
313 const char *daemon_name
= NULL
;
314 const char *inputfile
= NULL
;
317 struct cmd_rec
*next
;
319 struct cmd_rec
*tail
= NULL
;
320 int echo_command
= 0;
325 char *homedir
= NULL
;
327 char sysconfdir
[MAXPATHLEN
];
328 const char *pathspace_arg
= NULL
;
329 char pathspace
[MAXPATHLEN
] = "";
330 const char *histfile
= NULL
;
332 /* SUID: drop down to calling user & go back up when needed */
339 user_mode
= 0; /* may be set in options processing */
341 /* Preserve name of myself. */
342 progname
= ((p
= strrchr(argv
[0], '/')) ? ++p
: argv
[0]);
344 strlcpy(sysconfdir
, frr_sysconfdir
, sizeof(sysconfdir
));
347 strlcpy(vtydir
, frr_vtydir
, sizeof(vtydir
));
349 /* Option handling. */
351 opt
= getopt_long(argc
, argv
, "be:c:d:nf:H:mEhCwN:ut", longopts
,
366 cr
= XMALLOC(MTYPE_TMP
, sizeof(*cr
));
376 ditch_suid
= 1; /* option disables SUID */
377 strlcpy(vtydir
, optarg
, sizeof(vtydir
));
380 ditch_suid
= 1; /* option disables SUID */
381 snprintf(sysconfdir
, sizeof(sysconfdir
), "%s/", optarg
);
384 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
386 "slashes or dots are not permitted in the --pathspace option.\n");
389 pathspace_arg
= optarg
;
390 snprintf(pathspace
, sizeof(pathspace
), "%s/", optarg
);
393 daemon_name
= optarg
;
436 if (markfile
+ writeconfig
+ dryrun
+ boot_flag
> 1) {
438 "Invalid combination of arguments. Please specify at most one of:\n\t-b, -C, -m, -w\n");
441 if (inputfile
&& (writeconfig
|| boot_flag
)) {
443 "WARNING: Combinining the -f option with -b or -w is NOT SUPPORTED since its\nresults are inconsistent!\n");
446 snprintf(vtysh_config
, sizeof(vtysh_config
), "%s%s%s", sysconfdir
,
447 pathspace
, VTYSH_CONFIG_NAME
);
448 snprintf(frr_config
, sizeof(frr_config
), "%s%s%s", sysconfdir
,
449 pathspace
, FRR_CONFIG_NAME
);
452 strlcat(vtydir
, "/", sizeof(vtydir
));
453 strlcat(vtydir
, pathspace_arg
, sizeof(vtydir
));
456 /* Initialize user input buffer. */
460 /* Signal and others. */
463 /* Make vty structure and register commands. */
472 /* Read vtysh configuration file before connecting to daemons.
473 * (file may not be readable to calling user in SUID mode) */
475 vtysh_read_config(vtysh_config
, dryrun
);
478 /* Error code library system */
485 "-f option MUST be specified with -m option\n");
488 return (vtysh_mark_file(inputfile
));
491 /* Start execution only if not in dry-run mode */
492 if (dryrun
&& !cmd
) {
494 ret
= vtysh_read_config(inputfile
, dryrun
);
496 ret
= vtysh_read_config(frr_config
, dryrun
);
502 if (dryrun
&& cmd
&& cmd
->line
) {
504 vtysh_execute("enable");
507 char *cmdnow
= cmd
->line
, *next
;
509 next
= strchr(cmdnow
, '\n');
514 printf("%s%s\n", vtysh_prompt(),
517 ret
= vtysh_execute_no_pager(cmdnow
);
519 && !(ret
== CMD_SUCCESS
520 || ret
== CMD_SUCCESS_DAEMON
521 || ret
== CMD_WARNING
))
523 } while ((cmdnow
= next
) != NULL
);
527 XFREE(MTYPE_TMP
, cr
);
532 /* Ignore error messages */
534 if (freopen("/dev/null", "w", stdout
) == NULL
) {
536 "Exiting: Failed to duplicate stdout with -n option");
541 /* SUID: go back up elevated privs */
544 /* Make sure we pass authentication before proceeding. */
547 /* Do not connect until we have passed authentication. */
548 if (vtysh_connect_all(daemon_name
) <= 0) {
549 fprintf(stderr
, "Exiting: failed to connect to any daemons.\n");
552 "Hint: if this seems wrong, try running me as a privileged user!\n");
559 /* SUID: back down, don't need privs further on */
565 "writeconfig cannot be used when running as an unprivileged user.\n");
571 vtysh_execute("enable");
572 return vtysh_write_config_integrated();
576 vtysh_flock_config(inputfile
);
577 ret
= vtysh_read_config(inputfile
, dryrun
);
578 vtysh_unflock_config();
583 * Setup history file for use by both -c and regular input
584 * If we can't find the home directory, then don't store
585 * the history information.
586 * VTYSH_HISTFILE is prefered over command line
587 * argument (-H/--histfile).
589 if (getenv("VTYSH_HISTFILE")) {
590 const char *file
= getenv("VTYSH_HISTFILE");
592 strlcpy(history_file
, file
, sizeof(history_file
));
593 } else if (histfile
) {
594 strlcpy(history_file
, histfile
, sizeof(history_file
));
596 homedir
= vtysh_get_home();
598 snprintf(history_file
, sizeof(history_file
),
599 "%s/.history_frr", homedir
);
602 if (strlen(history_file
) > 0) {
603 if (read_history(history_file
) != 0) {
606 fp
= open(history_file
, O_CREAT
| O_EXCL
,
611 read_history(history_file
);
615 if (getenv("VTYSH_LOG")) {
616 const char *logpath
= getenv("VTYSH_LOG");
618 logfile
= fopen(logpath
, "a");
620 fprintf(stderr
, "Failed to open logfile (%s): %s\n",
621 logpath
, strerror(errno
));
627 if (cmd
&& cmd
->line
) {
628 /* Enter into enable node. */
630 vtysh_execute("enable");
632 vtysh_add_timestamp
= ts_flag
;
634 while (cmd
!= NULL
) {
637 while ((eol
= strchr(cmd
->line
, '\n')) != NULL
) {
640 add_history(cmd
->line
);
641 append_history(1, history_file
);
644 printf("%s%s\n", vtysh_prompt(),
650 ret
= vtysh_execute_no_pager(cmd
->line
);
652 && !(ret
== CMD_SUCCESS
653 || ret
== CMD_SUCCESS_DAEMON
654 || ret
== CMD_WARNING
))
660 add_history(cmd
->line
);
661 append_history(1, history_file
);
664 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
670 * Parsing logic for regular commands will be different
671 * than for those commands requiring further
672 * processing, such as cli instructions terminating
673 * with question-mark character.
675 if (!vtysh_execute_command_questionmark(cmd
->line
))
678 ret
= vtysh_execute_no_pager(cmd
->line
);
681 && !(ret
== CMD_SUCCESS
|| ret
== CMD_SUCCESS_DAEMON
682 || ret
== CMD_WARNING
))
689 XFREE(MTYPE_TMP
, cr
);
693 history_truncate_file(history_file
, 1000);
697 /* Boot startup configuration file. */
699 vtysh_flock_config(frr_config
);
700 ret
= vtysh_read_config(frr_config
, dryrun
);
701 vtysh_unflock_config();
704 "Configuration file[%s] processing failure: %d\n",
714 vtysh_readline_init();
718 /* Enter into enable node. */
720 vtysh_execute("enable");
722 vtysh_add_timestamp
= ts_flag
;
724 /* Preparation for longjmp() in sigtstp(). */
725 sigsetjmp(jmpbuf
, 1);
728 /* Main command loop. */
729 while (vtysh_rl_gets())
730 vtysh_execute(line_read
);
734 history_truncate_file(history_file
, 1000);