From a6bdf313f239cabfef445bc3658b79aec8a40c37 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Sat, 1 May 2021 09:00:03 -0700 Subject: [PATCH] Unset DISPLAY in environment. Without a DISPLAY var, ssh won't try to forward X11 when making an ssh connection. This patch also makes use of setenv() and unsetenv() if they are available. --- clientserver.c | 46 ++++++++++++++++++++++++++++++++++++++++------ configure.ac | 3 ++- main.c | 22 ++++++++++++++++++++-- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/clientserver.c b/clientserver.c index 48c15a60..14e3c544 100644 --- a/clientserver.c +++ b/clientserver.c @@ -380,7 +380,7 @@ int start_inband_exchange(int f_in, int f_out, const char *user, int argc, char return 0; } -#ifdef HAVE_PUTENV +#if defined HAVE_SETENV || defined HAVE_PUTENV static int read_arg_from_pipe(int fd, char *buf, int limit) { char *bp = buf, *eob = buf + limit - 1; @@ -405,23 +405,57 @@ static int read_arg_from_pipe(int fd, char *buf, int limit) static void set_env_str(const char *var, const char *str) { +#ifdef HAVE_SETENV + if (setenv(var, str, 1) < 0) + out_of_memory("set_env_str"); +#else #ifdef HAVE_PUTENV char *mem; if (asprintf(&mem, "%s=%s", var, str) < 0) out_of_memory("set_env_str"); putenv(mem); +#else + (void)var; + (void)str; +#endif #endif } +#if defined HAVE_SETENV || defined HAVE_PUTENV + +static void set_envN_str(const char *var, int num, const char *str) +{ +#ifdef HAVE_SETENV + char buf[128]; + (void)snprintf(buf, sizeof buf, "%s%d", var, num); + if (setenv(buf, str, 1) < 0) + out_of_memory("set_env_str"); +#else #ifdef HAVE_PUTENV + char *mem; + if (asprintf(&mem, "%s%d=%s", var, num, str) < 0) + out_of_memory("set_envN_str"); + putenv(mem); +#endif +#endif +} + void set_env_num(const char *var, long num) { +#ifdef HAVE_SETENV + char val[64]; + (void)snprintf(val, sizeof val, "%ld", num); + if (setenv(var, val, 1) < 0) + out_of_memory("set_env_str"); +#else +#ifdef HAVE_PUTENV char *mem; if (asprintf(&mem, "%s=%ld", var, num) < 0) out_of_memory("set_env_num"); putenv(mem); -} #endif +#endif +} /* Used for "early exec", "pre-xfer exec", and the "name converter" script. */ static pid_t start_pre_exec(const char *cmd, int *arg_fd_ptr, int *error_fd_ptr) @@ -451,15 +485,13 @@ static pid_t start_pre_exec(const char *cmd, int *arg_fd_ptr, int *error_fd_ptr) set_env_str("RSYNC_REQUEST", buf); for (j = 0; ; j++) { - char *p; len = read_arg_from_pipe(arg_fd, buf, BIGPATHBUFLEN); if (len <= 0) { if (!len) break; _exit(1); } - if (asprintf(&p, "RSYNC_ARG%d=%s", j, buf) >= 0) - putenv(p); + set_envN_str("RSYNC_ARG", j, buf); } dup2(arg_fd, STDIN_FILENO); @@ -490,6 +522,8 @@ static pid_t start_pre_exec(const char *cmd, int *arg_fd_ptr, int *error_fd_ptr) return pid; } +#endif + static void write_pre_exec_args(int write_fd, char *request, char **early_argv, char **argv, int exec_type) { int j = 0; @@ -809,7 +843,7 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char log_init(1); -#ifdef HAVE_PUTENV +#if defined HAVE_SETENV || defined HAVE_PUTENV if ((*lp_early_exec(module_id) || *lp_prexfer_exec(module_id) || *lp_postxfer_exec(module_id) || *lp_name_converter(module_id)) && !getenv("RSYNC_NO_XFER_EXEC")) { diff --git a/configure.ac b/configure.ac index fb0f4c0b..5a8d6d06 100644 --- a/configure.ac +++ b/configure.ac @@ -905,7 +905,8 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd chown chmod lchmod mknod mkfifo \ setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \ seteuid strerror putenv iconv_open locale_charset nl_langinfo getxattr \ extattr_get_link sigaction sigprocmask setattrlist getgrouplist \ - initgroups utimensat posix_fallocate attropen setvbuf nanosleep usleep) + initgroups utimensat posix_fallocate attropen setvbuf nanosleep usleep \ + setenv unsetenv) dnl cygwin iconv.h defines iconv_open as libiconv_open if test x"$ac_cv_func_iconv_open" != x"yes"; then diff --git a/main.c b/main.c index 66e5f780..15303e5d 100644 --- a/main.c +++ b/main.c @@ -1567,6 +1567,8 @@ static int start_client(int argc, char *argv[]) #ifdef HAVE_PUTENV if (daemon_connection) set_env_num("RSYNC_PORT", env_port); +#else + (void)env_port; #endif pid = do_cmd(shell_cmd, shell_machine, shell_user, remote_argv, remote_argc, &f_in, &f_out); @@ -1639,7 +1641,6 @@ void remember_children(UNUSED(int val)) #endif } - /** * This routine catches signals and tries to send them to gdb. * @@ -1663,7 +1664,6 @@ const char *get_panic_action(void) return "xterm -display :0 -T Panic -n Panic -e gdb /proc/%d/exe %d"; } - /** * Handle a fatal signal by launching a debugger, controlled by $RSYNC_PANIC_ACTION. * @@ -1687,6 +1687,22 @@ static void rsync_panic_handler(UNUSED(int whatsig)) } #endif +static void unset_env_var(const char *var) +{ +#ifdef HAVE_UNSETENV + unsetenv(var); +#else +#ifdef HAVE_PUTENV + char *mem; + if (asprintf(&mem, "%s=", var) < 0) + out_of_memory("unset_env_var"); + putenv(mem); +#else + (void)var; +#endif +#endif +} + int main(int argc,char *argv[]) { @@ -1724,6 +1740,8 @@ int main(int argc,char *argv[]) our_gid = MY_GID(); am_root = our_uid == ROOT_UID; + unset_env_var("DISPLAY"); + memset(&stats, 0, sizeof(stats)); /* Even a non-daemon runs needs the default config values to be set, e.g. -- 2.47.2