]> andersk Git - openssh.git/blobdiff - session.c
- markus@cvs.openbsd.org 2001/10/09 21:59:41
[openssh.git] / session.c
index 4873e4d74b3387afa46e5854f5cc51ba65268fe2..0d6ebdaeafe91f248a6b5f6bcd3cdb8ba1ca1d9c 100644 (file)
--- a/session.c
+++ b/session.c
@@ -33,7 +33,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: session.c,v 1.102 2001/09/16 14:46:54 markus Exp $");
+RCSID("$OpenBSD: session.c,v 1.106 2001/10/09 21:59:41 markus Exp $");
 
 #include "ssh.h"
 #include "ssh1.h"
@@ -129,6 +129,9 @@ void        do_exec_pty(Session *, const char *);
 void   do_exec_no_pty(Session *, const char *);
 void   do_exec(Session *, const char *);
 void   do_login(Session *, const char *);
+#ifdef LOGIN_NEEDS_UTMPX
+static void    do_pre_login(Session *s);
+#endif
 void   do_child(Session *, const char *);
 void   do_motd(void);
 int    check_quietlogin(Session *, const char *);
@@ -557,17 +560,13 @@ do_exec_pty(Session *s, const char *command)
                /* Make the pseudo tty our controlling tty. */
                pty_make_controlling_tty(&ttyfd, s->tty);
 
-               /* Redirect stdin from the pseudo tty. */
-               if (dup2(ttyfd, fileno(stdin)) < 0)
-                       error("dup2 stdin failed: %.100s", strerror(errno));
-
-               /* Redirect stdout to the pseudo tty. */
-               if (dup2(ttyfd, fileno(stdout)) < 0)
-                       error("dup2 stdin failed: %.100s", strerror(errno));
-
-               /* Redirect stderr to the pseudo tty. */
-               if (dup2(ttyfd, fileno(stderr)) < 0)
-                       error("dup2 stdin failed: %.100s", strerror(errno));
+               /* Redirect stdin/stdout/stderr from the pseudo tty. */
+               if (dup2(ttyfd, 0) < 0)
+                       error("dup2 stdin: %s", strerror(errno));
+               if (dup2(ttyfd, 1) < 0)
+                       error("dup2 stdout: %s", strerror(errno));
+               if (dup2(ttyfd, 2) < 0)
+                       error("dup2 stderr: %s", strerror(errno));
 
                /* Close the extra descriptor for the pseudo tty. */
                close(ttyfd);
@@ -1343,16 +1342,6 @@ do_child(Session *s, const char *command)
        for (i = 3; i < 64; i++)
                close(i);
 
-       /* Change current directory to the user\'s home directory. */
-       if (chdir(pw->pw_dir) < 0) {
-               fprintf(stderr, "Could not chdir to home directory %s: %s\n",
-                       pw->pw_dir, strerror(errno));
-#ifdef HAVE_LOGIN_CAP
-               if (login_getcapbool(lc, "requirehome", 0))
-                       exit(1);
-#endif
-       }
-
        /*
         * Must take new environment into use so that .ssh/rc, /etc/sshrc and
         * xauth are run in the proper environment.
@@ -1371,6 +1360,16 @@ do_child(Session *s, const char *command)
        }
 #endif /* AFS */
 
+       /* Change current directory to the user\'s home directory. */
+       if (chdir(pw->pw_dir) < 0) {
+               fprintf(stderr, "Could not chdir to home directory %s: %s\n",
+                   pw->pw_dir, strerror(errno));
+#ifdef HAVE_LOGIN_CAP
+               if (login_getcapbool(lc, "requirehome", 0))
+                       exit(1);
+#endif
+       }
+
        /*
         * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
         * in this order).
@@ -1673,25 +1672,33 @@ session_pty_req(Session *s)
 static int
 session_subsystem_req(Session *s)
 {
+       struct stat st;
        u_int len;
        int success = 0;
-       char *subsys = packet_get_string(&len);
+       char *cmd, *subsys = packet_get_string(&len);
        int i;
 
        packet_done();
        log("subsystem request for %s", subsys);
 
        for (i = 0; i < options.num_subsystems; i++) {
-               if(strcmp(subsys, options.subsystem_name[i]) == 0) {
-                       debug("subsystem: exec() %s", options.subsystem_command[i]);
+               if (strcmp(subsys, options.subsystem_name[i]) == 0) {
+                       cmd = options.subsystem_command[i];
+                       if (stat(cmd, &st) < 0) {
+                               error("subsystem: cannot stat %s: %s", cmd,
+                                   strerror(errno));
+                               break;
+                       }
+                       debug("subsystem: exec() %s", cmd);
                        s->is_subsystem = 1;
-                       do_exec(s, options.subsystem_command[i]);
+                       do_exec(s, cmd);
                        success = 1;
                }
        }
 
        if (!success)
-               log("subsystem request for %s failed, subsystem not found", subsys);
+               log("subsystem request for %s failed, subsystem not found",
+                   subsys);
 
        xfree(subsys);
        return success;
@@ -1942,22 +1949,6 @@ session_close_by_pid(pid_t pid, int status)
        session_close(s);
 }
 
-int
-session_have_children(void)
-{
-       int i;
-
-       for(i = 0; i < MAX_SESSIONS; i++) {
-               Session *s = &sessions[i];
-               if (s->used && s->pid != -1) {
-                       debug("session_have_children: id %d pid %d", i, s->pid);
-                       return 1;
-               }
-       }
-       debug("session_have_children: no more children");
-       return 0;
-}
-
 /*
  * this is called when a channel dies before
  * the session 'child' itself dies
@@ -1975,15 +1966,29 @@ session_close_by_channel(int id, void *arg)
        s->chanid = -1;
 
        debug("session_close_by_channel: channel %d kill %d", id, s->pid);
-       if (s->pid == 0) {
-               /* close session immediately */
-               session_close(s);
-       } else {
-               /* notify child, delay session cleanup */
-               if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
+       if (s->pid != 0) {
+               /* notify child */
+               if (kill(s->pid, SIGHUP) < 0)
                        error("session_close_by_channel: kill %d: %s",
                            s->pid, strerror(errno));
        }
+       session_close(s);
+}
+
+void
+session_close_all(void)
+{
+       int i;
+       for(i = 0; i < MAX_SESSIONS; i++) {
+               Session *s = &sessions[i];
+               if (s->used) {
+                       if (s->chanid != -1) {
+                               channel_cancel_cleanup(s->chanid);
+                               s->chanid = -1;
+                       }
+                       session_close(s);
+               }
+       }
 }
 
 static char *
This page took 0.040301 seconds and 4 git commands to generate.