]>
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>
33 #include <lib/version.h>
38 #include "memory_vty.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
];
56 char frr_config
[MAXPATHLEN
];
57 char vtydir
[MAXPATHLEN
];
58 static char history_file
[MAXPATHLEN
];
60 /* Flag for indicate executing child command. */
63 /* For sigsetjmp() & siglongjmp(). */
64 static sigjmp_buf jmpbuf
;
66 /* Flag for avoid recursive siglongjmp() call. */
67 static int jmpflag
= 0;
69 /* A static variable for holding the line. */
70 static char *line_read
;
72 /* Master of threads. */
73 struct thread_master
*master
;
78 /* SIGTSTP handler. This function care user's ^Z input. */
79 static void sigtstp(int sig
)
81 /* Execute "end" command. */
84 /* Initialize readline. */
88 /* Check jmpflag for duplicate siglongjmp(). */
94 /* Back to main command loop. */
95 siglongjmp(jmpbuf
, 1);
98 /* SIGINT handler. This function care user's ^Z input. */
99 static void sigint(int sig
)
101 /* Check this process is not child process. */
105 rl_forced_update_display();
109 /* Signale wrapper for vtysh. We don't use sigevent because
110 * vtysh doesn't use threads. TODO */
111 static void vtysh_signal_set(int signo
, void (*func
)(int))
113 struct sigaction sig
;
114 struct sigaction osig
;
116 sig
.sa_handler
= func
;
117 sigemptyset(&sig
.sa_mask
);
120 sig
.sa_flags
|= SA_RESTART
;
121 #endif /* SA_RESTART */
123 sigaction(signo
, &sig
, &osig
);
126 /* Initialization of signal handles. */
127 static void vtysh_signal_init(void)
129 vtysh_signal_set(SIGINT
, sigint
);
130 vtysh_signal_set(SIGTSTP
, sigtstp
);
131 vtysh_signal_set(SIGPIPE
, SIG_IGN
);
134 /* Help information display. */
135 static void usage(int status
)
138 fprintf(stderr
, "Try `%s --help' for more information.\n",
141 printf("Usage : %s [OPTION...]\n\n"
142 "Integrated shell for FRR. \n\n"
143 "-b, --boot Execute boot startup configuration\n"
144 "-c, --command Execute argument as command\n"
145 "-d, --daemon Connect only to the specified daemon\n"
146 "-f, --inputfile Execute commands from specific file and exit\n"
147 "-E, --echo Echo prompt and command in -c mode\n"
148 "-C, --dryrun Check configuration for validity and exit\n"
149 "-m, --markfile Mark input file with context end\n"
150 " --vty_socket Override vty socket path\n"
151 " --config_dir Override config directory path\n"
152 "-N --pathspace Insert prefix into config & socket paths\n"
153 "-w, --writeconfig Write integrated config (frr.conf) and exit\n"
154 "-h, --help Display this help and exit\n\n"
155 "Note that multiple commands may be executed from the command\n"
156 "line by passing multiple -c args, or by embedding linefeed\n"
157 "characters in one or more of the commands.\n\n"
158 "Report bugs to %s\n",
159 progname
, FRR_BUG_ADDRESS
);
164 /* VTY shell options, we use GNU getopt library. */
165 #define OPTION_VTYSOCK 1000
166 #define OPTION_CONFDIR 1001
167 struct option longopts
[] = {
168 {"boot", no_argument
, NULL
, 'b'},
169 /* For compatibility with older zebra/quagga versions */
170 {"eval", required_argument
, NULL
, 'e'},
171 {"command", required_argument
, NULL
, 'c'},
172 {"daemon", required_argument
, NULL
, 'd'},
173 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
174 {"config_dir", required_argument
, NULL
, OPTION_CONFDIR
},
175 {"inputfile", required_argument
, NULL
, 'f'},
176 {"echo", no_argument
, NULL
, 'E'},
177 {"dryrun", no_argument
, NULL
, 'C'},
178 {"help", no_argument
, NULL
, 'h'},
179 {"noerror", no_argument
, NULL
, 'n'},
180 {"mark", no_argument
, NULL
, 'm'},
181 {"writeconfig", no_argument
, NULL
, 'w'},
182 {"pathspace", required_argument
, NULL
, 'N'},
185 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
186 static char *vtysh_rl_gets(void)
189 /* If the buffer has already been allocated, return the memory
190 * to the free pool. */
196 /* Get a line from the user. Change prompt according to node. XXX. */
197 line_read
= readline(vtysh_prompt());
199 /* If the line has any text in it, save it on the history. But only if
200 * last command in history isn't the same one. */
201 if (line_read
&& *line_read
) {
203 last
= previous_history();
204 if (!last
|| strcmp(last
->line
, line_read
) != 0) {
205 add_history(line_read
);
206 append_history(1, history_file
);
213 static void log_it(const char *line
)
215 time_t t
= time(NULL
);
216 struct tm
*tmp
= localtime(&t
);
217 const char *user
= getenv("USER");
223 strftime(tod
, sizeof tod
, "%Y%m%d-%H:%M.%S", tmp
);
225 fprintf(logfile
, "%s:%s %s\n", tod
, user
, line
);
230 static void vtysh_flock_config(const char *flock_file
)
234 flock_fd
= open(flock_file
, O_RDONLY
, 0644);
236 fprintf(stderr
, "Unable to create lock file: %s, %s\n",
237 flock_file
, safe_strerror(errno
));
241 while (count
< 400 && (flock(flock_fd
, LOCK_EX
| LOCK_NB
) < 0)) {
248 "Flock of %s failed, continuing this may cause issues\n",
252 static void vtysh_unflock_config(void)
254 flock(flock_fd
, LOCK_UN
);
260 if (elevuid
!= realuid
&& seteuid(elevuid
)) {
261 perror("seteuid(on)");
264 if (elevgid
!= realgid
&& setegid(elevgid
)) {
265 perror("setegid(on)");
272 if (elevuid
!= realuid
&& seteuid(realuid
)) {
273 perror("seteuid(off)");
276 if (elevgid
!= realgid
&& setegid(realgid
)) {
277 perror("setegid(off)");
282 /* VTY shell main routine. */
283 int main(int argc
, char **argv
, char **env
)
289 const char *daemon_name
= NULL
;
290 const char *inputfile
= NULL
;
293 struct cmd_rec
*next
;
295 struct cmd_rec
*tail
= NULL
;
296 int echo_command
= 0;
301 char *homedir
= NULL
;
303 char sysconfdir
[MAXPATHLEN
];
304 char pathspace
[MAXPATHLEN
] = "";
306 /* SUID: drop down to calling user & go back up when needed */
313 /* Preserve name of myself. */
314 progname
= ((p
= strrchr(argv
[0], '/')) ? ++p
: argv
[0]);
316 strlcpy(sysconfdir
, frr_sysconfdir
, sizeof(sysconfdir
));
317 strlcpy(vtydir
, frr_vtydir
, sizeof(vtydir
));
319 /* Option handling. */
321 opt
= getopt_long(argc
, argv
, "be:c:d:nf:mEhCwN:", longopts
, 0);
335 cr
= XMALLOC(MTYPE_TMP
, sizeof(*cr
));
345 ditch_suid
= 1; /* option disables SUID */
346 strlcpy(vtydir
, optarg
, sizeof(vtydir
));
349 ditch_suid
= 1; /* option disables SUID */
350 strlcpy(sysconfdir
, optarg
, sizeof(sysconfdir
));
353 if (strchr(optarg
, '/') || strchr(optarg
, '.')) {
355 "slashes or dots are not permitted in the --pathspace option.\n");
358 snprintf(pathspace
, sizeof(pathspace
), "/%s", optarg
);
361 daemon_name
= optarg
;
395 if (markfile
+ writeconfig
+ dryrun
+ boot_flag
> 1) {
397 "Invalid combination of arguments. Please specify at "
398 "most one of:\n\t-b, -C, -m, -w\n");
401 if (inputfile
&& (writeconfig
|| boot_flag
)) {
403 "WARNING: Combinining the -f option with -b or -w is "
404 "NOT SUPPORTED since its\nresults are inconsistent!\n");
407 snprintf(vtysh_config
, sizeof(vtysh_config
), "%s%s%s", sysconfdir
,
408 pathspace
, VTYSH_CONFIG_NAME
);
409 snprintf(frr_config
, sizeof(frr_config
), "%s%s%s", sysconfdir
,
410 pathspace
, FRR_CONFIG_NAME
);
411 strlcat(vtydir
, pathspace
, sizeof(vtydir
));
413 /* Initialize user input buffer. */
417 /* Signal and others. */
420 /* Make vty structure and register commands. */
428 /* Read vtysh configuration file before connecting to daemons.
429 * (file may not be readable to calling user in SUID mode) */
431 vtysh_read_config(vtysh_config
);
437 "-f option MUST be specified with -m option\n");
440 return (vtysh_mark_file(inputfile
));
443 /* Start execution only if not in dry-run mode */
444 if (dryrun
&& !cmd
) {
446 ret
= vtysh_read_config(inputfile
);
448 ret
= vtysh_read_config(frr_config
);
454 if (dryrun
&& cmd
&& cmd
->line
) {
455 vtysh_execute("enable");
458 char *cmdnow
= cmd
->line
, *next
;
460 next
= strchr(cmdnow
, '\n');
465 printf("%s%s\n", vtysh_prompt(),
468 ret
= vtysh_execute_no_pager(cmdnow
);
470 && !(ret
== CMD_SUCCESS
471 || ret
== CMD_SUCCESS_DAEMON
472 || ret
== CMD_WARNING
))
474 } while ((cmdnow
= next
) != NULL
);
478 XFREE(MTYPE_TMP
, cr
);
483 /* Ignore error messages */
485 if (freopen("/dev/null", "w", stdout
) == NULL
) {
487 "Exiting: Failed to duplicate stdout with -n option");
492 /* SUID: go back up elevated privs */
495 /* Make sure we pass authentication before proceeding. */
498 /* Do not connect until we have passed authentication. */
499 if (vtysh_connect_all(daemon_name
) <= 0) {
500 fprintf(stderr
, "Exiting: failed to connect to any daemons.\n");
507 /* SUID: back down, don't need privs further on */
511 vtysh_execute("enable");
512 return vtysh_write_config_integrated();
516 vtysh_flock_config(inputfile
);
517 ret
= vtysh_read_config(inputfile
);
518 vtysh_unflock_config();
523 * Setup history file for use by both -c and regular input
524 * If we can't find the home directory, then don't store
525 * the history information
527 homedir
= vtysh_get_home();
529 snprintf(history_file
, sizeof(history_file
),
530 "%s/.history_quagga", homedir
);
531 if (read_history(history_file
) != 0) {
534 fp
= open(history_file
, O_CREAT
| O_EXCL
,
539 read_history(history_file
);
543 if (getenv("VTYSH_LOG")) {
544 const char *logpath
= getenv("VTYSH_LOG");
546 logfile
= fopen(logpath
, "a");
548 fprintf(stderr
, "Failed to open logfile (%s): %s\n",
549 logpath
, strerror(errno
));
555 if (cmd
&& cmd
->line
) {
556 /* Enter into enable node. */
557 vtysh_execute("enable");
559 while (cmd
!= NULL
) {
563 while ((eol
= strchr(cmd
->line
, '\n')) != NULL
) {
566 add_history(cmd
->line
);
567 append_history(1, history_file
);
570 printf("%s%s\n", vtysh_prompt(),
576 ret
= vtysh_execute_no_pager(cmd
->line
);
578 && !(ret
== CMD_SUCCESS
579 || ret
== CMD_SUCCESS_DAEMON
580 || ret
== CMD_WARNING
))
586 add_history(cmd
->line
);
587 append_history(1, history_file
);
590 printf("%s%s\n", vtysh_prompt(), cmd
->line
);
595 ret
= vtysh_execute_no_pager(cmd
->line
);
597 && !(ret
== CMD_SUCCESS
|| ret
== CMD_SUCCESS_DAEMON
598 || ret
== CMD_WARNING
))
605 XFREE(MTYPE_TMP
, cr
);
609 history_truncate_file(history_file
, 1000);
613 /* Boot startup configuration file. */
615 vtysh_flock_config(frr_config
);
616 int ret
= vtysh_read_config(frr_config
);
617 vtysh_unflock_config();
620 "Configuration file[%s] processing failure: %d\n",
632 vtysh_readline_init();
636 /* Enter into enable node. */
637 vtysh_execute("enable");
639 /* Preparation for longjmp() in sigtstp(). */
640 sigsetjmp(jmpbuf
, 1);
643 /* Main command loop. */
644 while (vtysh_rl_gets())
645 vtysh_execute(line_read
);
647 history_truncate_file(history_file
, 1000);