*/
#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"
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 *);
/* 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);
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.
}
#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).
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;
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
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 *