]>
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'},
206 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
207 static char *vtysh_rl_gets(void)
210 /* If the buffer has already been allocated, return the memory
211 * to the free pool. */
217 /* Get a line from the user. Change prompt according to node. XXX. */
218 line_read
= readline(vtysh_prompt());
220 /* If the line has any text in it, save it on the history. But only if
221 * last command in history isn't the same one. */
222 if (line_read
&& *line_read
) {
224 last
= previous_history();
225 if (!last
|| strcmp(last
->line
, line_read
) != 0) {
226 add_history(line_read
);
227 append_history(1, history_file
);
234 static void log_it(const char *line
)
236 time_t t
= time(NULL
);
238 const char *user
= getenv("USER");
241 localtime_r(&t
, &tmp
);
245 strftime(tod
, sizeof(tod
), "%Y%m%d-%H:%M.%S", &tmp
);
247 fprintf(logfile
, "%s:%s %s\n", tod
, user
, line
);
252 static void vtysh_flock_config(const char *flock_file
)
256 flock_fd
= open(flock_file
, O_RDONLY
, 0644);
258 fprintf(stderr
, "Unable to create lock file: %s, %s\n",
259 flock_file
, safe_strerror(errno
));
263 while (count
< 400 && (flock(flock_fd
, LOCK_EX
| LOCK_NB
) < 0)) {
270 "Flock of %s failed, continuing this may cause issues\n",
274 static void vtysh_unflock_config(void)
276 flock(flock_fd
, LOCK_UN
);
282 if (elevuid
!= realuid
&& seteuid(elevuid
)) {
283 perror("seteuid(on)");
286 if (elevgid
!= realgid
&& setegid(elevgid
)) {
287 perror("setegid(on)");
294 if (elevuid
!= realuid
&& seteuid(realuid
)) {
295 perror("seteuid(off)");
298 if (elevgid
!= realgid
&& setegid(realgid
)) {
299 perror("setegid(off)");
304 /* VTY shell main routine. */
305 int main(int argc
, char **argv
, char **env
)
311 const char *daemon_name
= NULL
;
312 const char *inputfile
= NULL
;
315 struct cmd_rec
*next
;
317 struct cmd_rec
*tail
= NULL
;
318 int echo_command
= 0;
323 char *homedir
= NULL
;
325 char sysconfdir
[MAXPATHLEN
];
326 const char *pathspace_arg
= NULL
;
327 char pathspace
[MAXPATHLEN
] = "";
328 const char *histfile
= NULL
;
330 /* SUID: drop down to calling user & go back up when needed */
337 user_mode
= 0; /* may be set in options processing */
339 /* Preserve name of myself. */
340 progname
= ((p
= strrchr(argv
[0], '/')) ? ++p
: argv
[0]);
342 strlcpy(sysconfdir
, frr_sysconfdir
, sizeof(sysconfdir
));
345 strlcpy(vtydir
, frr_vtydir
, sizeof(vtydir
));
347 /* Option handling. */
349 opt
= getopt_long(argc
, argv
, "be:c:d:nf:H:mEhCwN:u", longopts
,
364 cr
= XMALLOC(MTYPE_TMP
, sizeof(*cr
));
374 ditch_suid
= 1; /* option disables SUID */
375 strlcpy(vtydir
, optarg
, sizeof(vtydir
));
378 ditch_suid
= 1; /* option disables SUID */
379 snprintf(sysconfdir
, sizeof(sysconfdir
), "%s/", optarg
);
382 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
384 "slashes or dots are not permitted in the --pathspace option.\n");
387 pathspace_arg
= optarg
;
388 snprintf(pathspace
, sizeof(pathspace
), "%s/", optarg
);
391 daemon_name
= optarg
;
431 if (markfile
+ writeconfig
+ dryrun
+ boot_flag
> 1) {
433 "Invalid combination of arguments. Please specify at most one of:\n\t-b, -C, -m, -w\n");
436 if (inputfile
&& (writeconfig
|| boot_flag
)) {
438 "WARNING: Combinining the -f option with -b or -w is NOT SUPPORTED since its\nresults are inconsistent!\n");
441 snprintf(vtysh_config
, sizeof(vtysh_config
), "%s%s%s", sysconfdir
,
442 pathspace
, VTYSH_CONFIG_NAME
);
443 snprintf(frr_config
, sizeof(frr_config
), "%s%s%s", sysconfdir
,
444 pathspace
, FRR_CONFIG_NAME
);
447 strlcat(vtydir
, "/", sizeof(vtydir
));
448 strlcat(vtydir
, pathspace_arg
, sizeof(vtydir
));
451 /* Initialize user input buffer. */
455 /* Signal and others. */
458 /* Make vty structure and register commands. */
467 /* Read vtysh configuration file before connecting to daemons.
468 * (file may not be readable to calling user in SUID mode) */
470 vtysh_read_config(vtysh_config
, dryrun
);
473 /* Error code library system */
480 "-f option MUST be specified with -m option\n");
483 return (vtysh_mark_file(inputfile
));
486 /* Start execution only if not in dry-run mode */
487 if (dryrun
&& !cmd
) {
489 ret
= vtysh_read_config(inputfile
, dryrun
);
491 ret
= vtysh_read_config(frr_config
, dryrun
);
497 if (dryrun
&& cmd
&& cmd
->line
) {
499 vtysh_execute("enable");
502 char *cmdnow
= cmd
->line
, *next
;
504 next
= strchr(cmdnow
, '\n');
509 printf("%s%s\n", vtysh_prompt(),
512 ret
= vtysh_execute_no_pager(cmdnow
);
514 && !(ret
== CMD_SUCCESS
515 || ret
== CMD_SUCCESS_DAEMON
516 || ret
== CMD_WARNING
))
518 } while ((cmdnow
= next
) != NULL
);
522 XFREE(MTYPE_TMP
, cr
);
527 /* Ignore error messages */
529 if (freopen("/dev/null", "w", stdout
) == NULL
) {
531 "Exiting: Failed to duplicate stdout with -n option");
536 /* SUID: go back up elevated privs */
539 /* Make sure we pass authentication before proceeding. */
542 /* Do not connect until we have passed authentication. */
543 if (vtysh_connect_all(daemon_name
) <= 0) {
544 fprintf(stderr
, "Exiting: failed to connect to any daemons.\n");
547 "Hint: if this seems wrong, try running me as a privileged user!\n");
554 /* SUID: back down, don't need privs further on */
560 "writeconfig cannot be used when running as an unprivileged user.\n");
566 vtysh_execute("enable");
567 return vtysh_write_config_integrated();
571 vtysh_flock_config(inputfile
);
572 ret
= vtysh_read_config(inputfile
, dryrun
);
573 vtysh_unflock_config();
578 * Setup history file for use by both -c and regular input
579 * If we can't find the home directory, then don't store
580 * the history information.
581 * VTYSH_HISTFILE is prefered over command line
582 * argument (-H/--histfile).
584 if (getenv("VTYSH_HISTFILE")) {
585 const char *file
= getenv("VTYSH_HISTFILE");
587 strlcpy(history_file
, file
, sizeof(history_file
));
588 } else if (histfile
) {
589 strlcpy(history_file
, histfile
, sizeof(history_file
));
591 homedir
= vtysh_get_home();
593 snprintf(history_file
, sizeof(history_file
),
594 "%s/.history_frr", homedir
);
597 if (strlen(history_file
) > 0) {
598 if (read_history(history_file
) != 0) {
601 fp
= open(history_file
, O_CREAT
| O_EXCL
,
606 read_history(history_file
);
610 if (getenv("VTYSH_LOG")) {
611 const char *logpath
= getenv("VTYSH_LOG");
613 logfile
= fopen(logpath
, "a");
615 fprintf(stderr
, "Failed to open logfile (%s): %s\n",
616 logpath
, strerror(errno
));
622 if (cmd
&& cmd
->line
) {
623 /* Enter into enable node. */
625 vtysh_execute("enable");
627 while (cmd
!= NULL
) {
630 while ((eol
= strchr(cmd
->line
, '\n')) != NULL
) {
633 add_history(cmd
->line
);
634 append_history(1, history_file
);
637 printf("%s%s\n", vtysh_prompt(),
643 ret
= vtysh_execute_no_pager(cmd
->line
);
645 && !(ret
== CMD_SUCCESS
646 || ret
== CMD_SUCCESS_DAEMON
647 || ret
== CMD_WARNING
))
653 add_history(cmd
->line
);
654 append_history(1, history_file
);
657 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
663 * Parsing logic for regular commands will be different
664 * than for those commands requiring further
665 * processing, such as cli instructions terminating
666 * with question-mark character.
668 if (!vtysh_execute_command_questionmark(cmd
->line
))
671 ret
= vtysh_execute_no_pager(cmd
->line
);
674 && !(ret
== CMD_SUCCESS
|| ret
== CMD_SUCCESS_DAEMON
675 || ret
== CMD_WARNING
))
682 XFREE(MTYPE_TMP
, cr
);
686 history_truncate_file(history_file
, 1000);
690 /* Boot startup configuration file. */
692 vtysh_flock_config(frr_config
);
693 ret
= vtysh_read_config(frr_config
, dryrun
);
694 vtysh_unflock_config();
697 "Configuration file[%s] processing failure: %d\n",
707 vtysh_readline_init();
711 /* Enter into enable node. */
713 vtysh_execute("enable");
715 /* Preparation for longjmp() in sigtstp(). */
716 sigsetjmp(jmpbuf
, 1);
719 /* Main command loop. */
720 while (vtysh_rl_gets())
721 vtysh_execute(line_read
);
725 history_truncate_file(history_file
, 1000);