Unset DISPLAY in environment.
authorWayne Davison <wayne@opencoder.net>
Sat, 1 May 2021 16:00:03 +0000 (09:00 -0700)
committerWayne Davison <wayne@opencoder.net>
Sat, 1 May 2021 16:14:51 +0000 (09:14 -0700)
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
configure.ac
main.c

index 48c15a60130b20a05b19cbf76d7cae453952382c..14e3c544a99ab67cf564271efd24737b2c438e4f 100644 (file)
@@ -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")) {
index fb0f4c0b7a20f87c789c81a5e3f2440b187b38a8..5a8d6d069f6f3c0325b062ef036a593abef01d39 100644 (file)
@@ -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 66e5f780ac208d791e61f06c92694264299becf8..15303e5d2a792e552ad961a8ea9ec12869f41459 100644 (file)
--- 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.