From 9b8a82496a677e1ec0509f66026f6c3ef4ee3365 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sun, 27 Aug 2017 20:57:34 +0200 Subject: [PATCH] vtysh: simplify path handling Signed-off-by: David Lamparter --- vtysh/vtysh.c | 24 ++++++------- vtysh/vtysh.h | 9 ++--- vtysh/vtysh_main.c | 86 +++++++++++++--------------------------------- 3 files changed, 37 insertions(+), 82 deletions(-) diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 85cbcae4df..b729af9823 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -2295,12 +2295,12 @@ int vtysh_write_config_integrated(void) fprintf(stdout, "Building Configuration...\n"); - backup_config_file(quagga_config); - fp = fopen(quagga_config, "w"); + backup_config_file(frr_config); + fp = fopen(frr_config, "w"); if (fp == NULL) { fprintf(stdout, "%% Error: failed to open configuration file %s: %s\n", - quagga_config, safe_strerror(errno)); + frr_config, safe_strerror(errno)); return CMD_WARNING_CONFIG_FAILED; } fd = fileno(fp); @@ -2313,7 +2313,7 @@ int vtysh_write_config_integrated(void) if (fchmod(fd, CONFIGFILE_MASK) != 0) { printf("%% Warning: can't chmod configuration file %s: %s\n", - quagga_config, safe_strerror(errno)); + frr_config, safe_strerror(errno)); err++; } @@ -2345,18 +2345,18 @@ int vtysh_write_config_integrated(void) if ((uid != (uid_t)-1 || gid != (gid_t)-1) && fchown(fd, uid, gid)) { printf("%% Warning: can't chown configuration file %s: %s\n", - quagga_config, safe_strerror(errno)); + frr_config, safe_strerror(errno)); err++; } } else { - printf("%% Warning: stat() failed on %s: %s\n", quagga_config, + printf("%% Warning: stat() failed on %s: %s\n", frr_config, safe_strerror(errno)); err++; } fclose(fp); - printf("Integrated configuration saved to %s\n", quagga_config); + printf("Integrated configuration saved to %s\n", frr_config); if (err) return CMD_WARNING; @@ -2370,7 +2370,7 @@ static bool want_config_integrated(void) switch (vtysh_write_integrated) { case WRITE_INTEGRATED_UNSPECIFIED: - if (stat(quagga_config, &s) && errno == ENOENT) + if (stat(frr_config, &s) && errno == ENOENT) return false; return true; case WRITE_INTEGRATED_NO: @@ -2712,7 +2712,7 @@ static int vtysh_connect(struct vtysh_client *vclient) if (!vclient->path[0]) snprintf(vclient->path, sizeof(vclient->path), "%s/%s.vty", - vty_sock_path, vclient->name); + vtydir, vclient->name); path = vclient->path; /* Stat socket to see if we have permission to access it. */ @@ -2806,7 +2806,7 @@ static void vtysh_update_all_insances(struct vtysh_client *head_client) return; /* ls vty_sock_dir and look for all files ending in .vty */ - dir = opendir(vty_sock_path); + dir = opendir(vtydir); if (dir) { while ((file = readdir(dir)) != NULL) { if (begins_with(file->d_name, "ospfd-") @@ -2814,7 +2814,7 @@ static void vtysh_update_all_insances(struct vtysh_client *head_client) if (n == MAXIMUM_INSTANCES) { fprintf(stderr, "Parsing %s, client limit(%d) reached!\n", - vty_sock_path, n); + vtydir, n); break; } client = (struct vtysh_client *)malloc( @@ -2823,7 +2823,7 @@ static void vtysh_update_all_insances(struct vtysh_client *head_client) client->name = "ospfd"; client->flag = VTYSH_OSPFD; snprintf(client->path, sizeof(client->path), - "%s/%s", vty_sock_path, file->d_name); + "%s/%s", vtydir, file->d_name); client->next = NULL; vtysh_client_sorted_insert(head_client, client); n++; diff --git a/vtysh/vtysh.h b/vtysh/vtysh.h index e10ab11da9..9d6ea4bda4 100644 --- a/vtysh/vtysh.h +++ b/vtysh/vtysh.h @@ -49,10 +49,6 @@ DECLARE_MGROUP(MVTYSH) #define VTYSH_NS VTYSH_ZEBRA #define VTYSH_VRF VTYSH_ZEBRA|VTYSH_PIMD -/* vtysh local configuration file. */ -#define VTYSH_DEFAULT_CONFIG "vtysh.conf" -#define FRR_DEFAULT_CONFIG "frr.conf" - enum vtysh_write_integrated { WRITE_INTEGRATED_UNSPECIFIED, WRITE_INTEGRATED_NO, @@ -61,7 +57,8 @@ enum vtysh_write_integrated { extern enum vtysh_write_integrated vtysh_write_integrated; -extern char *quagga_config; +extern char frr_config[]; +extern char vtydir[]; void vtysh_init_vty(void); void vtysh_init_cmd(void); @@ -101,6 +98,4 @@ extern int execute_flag; extern struct vty *vty; -extern const char *vty_sock_path; - #endif /* VTYSH_H */ diff --git a/vtysh/vtysh_main.c b/vtysh/vtysh_main.c index b9909b4930..0f1fe4857b 100644 --- a/vtysh/vtysh_main.c +++ b/vtysh/vtysh_main.c @@ -48,18 +48,18 @@ char *progname; static uid_t elevuid, realuid; static gid_t elevgid, realgid; +#define VTYSH_CONFIG_NAME "vtysh.conf" +#define FRR_CONFIG_NAME "frr.conf" + /* Configuration file name and directory. */ -static char vtysh_config_always[MAXPATHLEN] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; -static char quagga_config_default[MAXPATHLEN] = SYSCONFDIR FRR_DEFAULT_CONFIG; -char *quagga_config = quagga_config_default; -char history_file[MAXPATHLEN]; +static char vtysh_config[MAXPATHLEN]; +char frr_config[MAXPATHLEN]; +char vtydir[MAXPATHLEN]; +static char history_file[MAXPATHLEN]; /* Flag for indicate executing child command. */ int execute_flag = 0; -/* VTY Socket prefix */ -const char *vty_sock_path = NULL; - /* For sigsetjmp() & siglongjmp(). */ static sigjmp_buf jmpbuf; @@ -286,7 +286,6 @@ int main(int argc, char **argv, char **env) int boot_flag = 0; const char *daemon_name = NULL; const char *inputfile = NULL; - const char *vtysh_configfile_name; struct cmd_rec { char *line; struct cmd_rec *next; @@ -299,6 +298,7 @@ int main(int argc, char **argv, char **env) int ret = 0; char *homedir = NULL; int ditch_suid = 0; + char sysconfdir[MAXPATHLEN]; /* SUID: drop down to calling user & go back up when needed */ elevuid = geteuid(); @@ -310,6 +310,9 @@ int main(int argc, char **argv, char **env) /* Preserve name of myself. */ progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]); + strlcpy(sysconfdir, frr_sysconfdir, sizeof(sysconfdir)); + strlcpy(vtydir, frr_vtydir, sizeof(vtydir)); + /* Option handling. */ while (1) { opt = getopt_long(argc, argv, "be:c:d:nf:mEhCw", longopts, 0); @@ -337,56 +340,11 @@ int main(int argc, char **argv, char **env) } break; case OPTION_VTYSOCK: ditch_suid = 1; /* option disables SUID */ - vty_sock_path = optarg; + strlcpy(vtydir, optarg, sizeof(vtydir)); break; case OPTION_CONFDIR: ditch_suid = 1; /* option disables SUID */ - /* - * Overwrite location for vtysh.conf - */ - vtysh_configfile_name = - strrchr(VTYSH_DEFAULT_CONFIG, '/'); - if (vtysh_configfile_name) - /* skip '/' */ - vtysh_configfile_name++; - else - /* - * VTYSH_DEFAULT_CONFIG configured with relative - * path - * during config? Should really never happen for - * sensible config - */ - vtysh_configfile_name = - (char *)VTYSH_DEFAULT_CONFIG; - strlcpy(vtysh_config_always, optarg, - sizeof(vtysh_config_always)); - strlcat(vtysh_config_always, "/", - sizeof(vtysh_config_always)); - strlcat(vtysh_config_always, vtysh_configfile_name, - sizeof(vtysh_config_always)); - /* - * Overwrite location for frr.conf - */ - vtysh_configfile_name = - strrchr(FRR_DEFAULT_CONFIG, '/'); - if (vtysh_configfile_name) - /* skip '/' */ - vtysh_configfile_name++; - else - /* - * FRR_DEFAULT_CONFIG configured with relative - * path - * during config? Should really never happen for - * sensible config - */ - vtysh_configfile_name = - (char *)FRR_DEFAULT_CONFIG; - strlcpy(quagga_config_default, optarg, - sizeof(vtysh_config_always)); - strlcat(quagga_config_default, "/", - sizeof(vtysh_config_always)); - strlcat(quagga_config_default, vtysh_configfile_name, - sizeof(quagga_config_default)); + strlcpy(sysconfdir, optarg, sizeof(sysconfdir)); break; case 'd': daemon_name = optarg; @@ -423,9 +381,6 @@ int main(int argc, char **argv, char **env) elevgid = realgid; } - if (!vty_sock_path) - vty_sock_path = frr_vtydir; - if (markfile + writeconfig + dryrun + boot_flag > 1) { fprintf(stderr, "Invalid combination of arguments. Please specify at " @@ -438,6 +393,11 @@ int main(int argc, char **argv, char **env) "NOT SUPPORTED since its\nresults are inconsistent!\n"); } + snprintf(vtysh_config, sizeof(vtysh_config), "%s/%s", + sysconfdir, VTYSH_CONFIG_NAME); + snprintf(frr_config, sizeof(frr_config), "%s/%s", + sysconfdir, FRR_CONFIG_NAME); + /* Initialize user input buffer. */ line_read = NULL; setlinebuf(stdout); @@ -456,7 +416,7 @@ int main(int argc, char **argv, char **env) /* Read vtysh configuration file before connecting to daemons. * (file may not be readable to calling user in SUID mode) */ suid_on(); - vtysh_read_config(vtysh_config_always); + vtysh_read_config(vtysh_config); suid_off(); if (markfile) { @@ -473,7 +433,7 @@ int main(int argc, char **argv, char **env) if (inputfile) { ret = vtysh_read_config(inputfile); } else { - ret = vtysh_read_config(quagga_config_default); + ret = vtysh_read_config(frr_config); } exit(ret); @@ -640,13 +600,13 @@ int main(int argc, char **argv, char **env) /* Boot startup configuration file. */ if (boot_flag) { - vtysh_flock_config(quagga_config); - int ret = vtysh_read_config(quagga_config); + vtysh_flock_config(frr_config); + int ret = vtysh_read_config(frr_config); vtysh_unflock_config(); if (ret) { fprintf(stderr, "Configuration file[%s] processing failure: %d\n", - quagga_config, ret); + frr_config, ret); if (no_error) exit(0); else -- 2.39.5