- (djm) OpenBSD CVS Sync
   - djm@cvs.openbsd.org 2004/06/17 14:52:48
     [clientloop.c clientloop.h ssh.c]
     support environment passing over shared connections; ok markus@
diff --git a/ChangeLog b/ChangeLog
index 95b583e..ae4897e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+20040618
+ - (djm) OpenBSD CVS Sync
+   - djm@cvs.openbsd.org 2004/06/17 14:52:48
+     [clientloop.c clientloop.h ssh.c]
+     support environment passing over shared connections; ok markus@
+
 20040617
  - (dtucker) [regress/scp.sh] diff -N is not portable (but needed for some
    platforms), so test if diff understands it.  Pointed out by tim@, ok djm@
@@ -1270,4 +1276,4 @@
    - (djm) Trim deprecated options from INSTALL. Mention UsePAM
    - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
 
-$Id: ChangeLog,v 1.3404 2004/06/17 07:01:21 dtucker Exp $
+$Id: ChangeLog,v 1.3405 2004/06/17 15:17:29 djm Exp $
diff --git a/clientloop.c b/clientloop.c
index 6b849a9..eb32003 100644
--- a/clientloop.c
+++ b/clientloop.c
@@ -59,7 +59,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: clientloop.c,v 1.125 2004/06/15 05:45:04 djm Exp $");
+RCSID("$OpenBSD: clientloop.c,v 1.126 2004/06/17 14:52:48 djm Exp $");
 
 #include "ssh.h"
 #include "ssh1.h"
@@ -143,6 +143,7 @@
 	Buffer cmd;
 	char *term;
 	struct termios tio;
+	char **env;
 };
 
 /*XXX*/
@@ -538,6 +539,7 @@
 {
 	struct confirm_ctx *cctx = arg;
 	Channel *c;
+	int i;
 	
 	if (cctx == NULL)
 		fatal("%s: cctx == NULL", __func__);
@@ -545,13 +547,18 @@
 		fatal("%s: no channel for id %d", __func__, id);
 
 	client_session2_setup(id, cctx->want_tty, cctx->want_subsys, 
-	    cctx->term, &cctx->tio, c->rfd, &cctx->cmd, 
+	    cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env,
 	    client_subsystem_reply);
 	
 	c->confirm_ctx = NULL;
 	buffer_free(&cctx->cmd);
-	free(cctx->term);
-	free(cctx);
+	xfree(cctx->term);
+	if (cctx->env != NULL) {
+		for (i = 0; cctx->env[i] != NULL; i++)
+			xfree(cctx->env[i]);
+		xfree(cctx->env);
+	}			
+	xfree(cctx);
 }
 
 static void
@@ -559,12 +566,12 @@
 {
 	Buffer m;
 	Channel *c;
-	int client_fd, new_fd[3], ver;
+	int client_fd, new_fd[3], ver, i;
 	socklen_t addrlen;
 	struct sockaddr_storage addr;
 	struct confirm_ctx *cctx;
 	char *cmd;
-	u_int len;
+	u_int len, env_len;
 	uid_t euid;
 	gid_t egid;
 
@@ -631,6 +638,16 @@
 	buffer_init(&cctx->cmd);
 	buffer_append(&cctx->cmd, cmd, strlen(cmd));
 
+	env_len = buffer_get_int(&m);
+	env_len = MIN(env_len, 4096);
+	debug3("%s: receiving %d env vars", __func__, env_len);
+	if (env_len != 0) {
+		cctx->env = xmalloc(sizeof(*cctx->env) * (env_len + 1));
+		for (i = 0; i < env_len; i++)
+			cctx->env[i] = buffer_get_string(&m, &len);
+		cctx->env[i] = NULL;
+	}
+
 	debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
 	    cctx->want_tty, cctx->want_subsys, cmd);
 
@@ -1626,7 +1643,7 @@
 
 void
 client_session2_setup(int id, int want_tty, int want_subsystem, 
-    const char *term, struct termios *tiop, int in_fd, Buffer *cmd, 
+    const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env,
     dispatch_fn *subsys_repl)
 {
 	int len;
@@ -1654,15 +1671,14 @@
 	}
 
 	/* Transfer any environment variables from client to server */
-	if (options.num_send_env != 0) {
+	if (options.num_send_env != 0 && env != NULL) {
 		int i, j, matched;
-		extern char **environ;
 		char *name, *val;
 
 		debug("Sending environment.");
-		for (i = 0; environ && environ[i] != NULL; i++) {
+		for (i = 0; env[i] != NULL; i++) {
 			/* Split */
-			name = xstrdup(environ[i]);
+			name = xstrdup(env[i]);
 			if ((val = strchr(name, '=')) == NULL) {
 				free(name);
 				continue;
diff --git a/clientloop.h b/clientloop.h
index f1e13ac..c34d667 100644
--- a/clientloop.h
+++ b/clientloop.h
@@ -1,4 +1,4 @@
-/*	$OpenBSD: clientloop.h,v 1.9 2004/06/13 15:03:02 djm Exp $	*/
+/*	$OpenBSD: clientloop.h,v 1.10 2004/06/17 14:52:48 djm Exp $	*/
 
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -39,4 +39,4 @@
 int	 client_loop(int, int, int);
 void	 client_global_request_reply_fwd(int, u_int32_t, void *);
 void	 client_session2_setup(int, int, int, const char *, struct termios *, 
-	    int, Buffer *, dispatch_fn *);
+	    int, Buffer *, char **, dispatch_fn *);
diff --git a/ssh.c b/ssh.c
index 4badd29..9b434b9 100644
--- a/ssh.c
+++ b/ssh.c
@@ -40,7 +40,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: ssh.c,v 1.214 2004/06/13 15:03:02 djm Exp $");
+RCSID("$OpenBSD: ssh.c,v 1.215 2004/06/17 14:52:48 djm Exp $");
 
 #include <openssl/evp.h>
 #include <openssl/err.h>
@@ -1080,6 +1080,8 @@
 static void
 ssh_session2_setup(int id, void *arg)
 {
+	extern char **environ;
+
 	int interactive = tty_flag;
 	if (options.forward_x11 && getenv("DISPLAY") != NULL) {
 		char *proto, *data;
@@ -1100,7 +1102,7 @@
 	}
 
 	client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
-	    NULL, fileno(stdin), &command, &ssh_subsystem_reply);
+	    NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
 
 	packet_set_interactive(interactive);
 }
@@ -1230,9 +1232,10 @@
 control_client(const char *path)
 {
 	struct sockaddr_un addr;
-	int r, sock, exitval, addr_len;
+	int i, r, sock, exitval, addr_len;
 	Buffer m;
 	char *cp;
+	extern char **environ;
 	
 	memset(&addr, '\0', sizeof(addr));
 	addr.sun_family = AF_UNIX;
@@ -1265,8 +1268,6 @@
 		fatal("%s: wrong version", __func__);
 	control_server_pid = buffer_get_int(&m);
 
-	/* XXX: env passing */
-
 	buffer_clear(&m);
 	buffer_put_int(&m, tty_flag);
 	buffer_put_int(&m, subsystem_flag);
@@ -1275,6 +1276,13 @@
 	buffer_append(&command, "\0", 1);
 	buffer_put_cstring(&m, buffer_ptr(&command));
 
+	/* Pass environment */
+	for (i = 0; environ != NULL && environ[i] != NULL; i++)
+		;
+	buffer_put_int(&m, i);
+	for (i = 0; environ != NULL && environ[i] != NULL; i++)
+		buffer_put_cstring(&m, environ[i]);
+
 	if (ssh_msg_send(sock, /* version */0, &m) == -1)
 		fatal("%s: msg_send", __func__);