- markus@cvs.openbsd.org 2001/06/19 12:34:09
     [session.c]
     cleanup forced command handling, from dwd@bell-labs.com
diff --git a/ChangeLog b/ChangeLog
index e75e397..a3766b2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,9 @@
    - markus@cvs.openbsd.org 2001/06/16 08:58:34
      [misc.c]
      copy pw_expire and pw_change, too.
+   - markus@cvs.openbsd.org 2001/06/19 12:34:09
+     [session.c]
+     cleanup forced command handling, from dwd@bell-labs.com
 
 20010615
  - (stevesk) don't set SA_RESTART and set SIGCHLD to SIG_DFL
@@ -5664,4 +5667,4 @@
  - Wrote replacements for strlcpy and mkdtemp
  - Released 1.0pre1
 
-$Id: ChangeLog,v 1.1292 2001/06/21 03:11:27 mouring Exp $
+$Id: ChangeLog,v 1.1293 2001/06/21 03:13:10 mouring Exp $
diff --git a/session.c b/session.c
index 65f6bac..005f7ab 100644
--- a/session.c
+++ b/session.c
@@ -33,7 +33,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: session.c,v 1.89 2001/06/13 09:10:31 markus Exp $");
+RCSID("$OpenBSD: session.c,v 1.90 2001/06/19 12:34:09 markus Exp $");
 
 #include "ssh.h"
 #include "ssh1.h"
@@ -128,6 +128,7 @@
 void	session_close(Session *s);
 void	do_exec_pty(Session *s, const char *command);
 void	do_exec_no_pty(Session *s, const char *command);
+void	do_exec(Session *s, const char *command);
 void	do_login(Session *s, const char *command);
 #ifdef LOGIN_NEEDS_UTMPX
 void	do_pre_login(Session *s);
@@ -313,17 +314,7 @@
 				command = NULL;
 				packet_integrity_check(plen, 0, type);
 			}
-			if (forced_command != NULL) {
-				original_command = command;
-				command = forced_command;
-				debug("Forced command '%.500s'", forced_command);
-			}
-			if (s->ttyfd != -1)
-				do_exec_pty(s, command);
-			else
-				do_exec_no_pty(s, command);
-			if (command != NULL)
-				xfree(command);
+			do_exec(s, command);
 			session_close(s);
 			return;
 
@@ -598,6 +589,35 @@
 }
 #endif
 
+/*
+ * This is called to fork and execute a command.  If another command is
+ * to be forced, execute that instead.
+ */
+void
+do_exec(Session *s, const char *command)
+{
+	if (forced_command) {
+		original_command = command;
+		command = forced_command;
+		forced_command = NULL;
+		debug("Forced command '%.900s'", command);
+	}
+
+	if (s->ttyfd != -1)
+		do_exec_pty(s, command);
+	else
+		do_exec_no_pty(s, command);
+
+	if (command != NULL)
+		xfree(command);
+
+	if (original_command != NULL) {
+		xfree(original_command);
+		original_command = NULL;
+	}
+}
+
+
 /* administrative, login(1)-like work */
 void
 do_login(Session *s, const char *command)
@@ -1666,13 +1686,8 @@
 int
 session_shell_req(Session *s)
 {
-	/* if forced_command == NULL, the shell is execed */
-	char *shell = forced_command;
 	packet_done();
-	if (s->ttyfd == -1)
-		do_exec_no_pty(s, shell);
-	else
-		do_exec_pty(s, shell);
+	do_exec(s, NULL);
 	return 1;
 }
 
@@ -1682,17 +1697,7 @@
 	u_int len;
 	char *command = packet_get_string(&len);
 	packet_done();
-	if (forced_command) {
-		original_command = command;
-		command = forced_command;
-		debug("Forced command '%.500s'", forced_command);
-	}
-	if (s->ttyfd == -1)
-		do_exec_no_pty(s, command);
-	else
-		do_exec_pty(s, command);
-	if (forced_command == NULL)
-		xfree(command);
+	do_exec(s, command);
 	return 1;
 }