xref: /freebsd/crypto/openssh/session.c (revision ca3176e7c8c53ffd5e822c8bad53fa885faf7ae0)
1a04a10f8SKris Kennaway /*
2a04a10f8SKris Kennaway  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3a04a10f8SKris Kennaway  *                    All rights reserved
4c2d3a559SKris Kennaway  *
5c2d3a559SKris Kennaway  * As far as I am concerned, the code I have written for this software
6c2d3a559SKris Kennaway  * can be used freely for any purpose.  Any derived versions of this
7c2d3a559SKris Kennaway  * software must be clearly marked as such, and if the derived work is
8c2d3a559SKris Kennaway  * incompatible with the protocol description in the RFC file, it must be
9c2d3a559SKris Kennaway  * called by a name other than "ssh" or "Secure Shell".
10c2d3a559SKris Kennaway  *
11a04a10f8SKris Kennaway  * SSH2 support by Markus Friedl.
12a04a10f8SKris Kennaway  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
13e8aafc91SKris Kennaway  *
14c2d3a559SKris Kennaway  * Redistribution and use in source and binary forms, with or without
15c2d3a559SKris Kennaway  * modification, are permitted provided that the following conditions
16c2d3a559SKris Kennaway  * are met:
17c2d3a559SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
18c2d3a559SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
19c2d3a559SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
20c2d3a559SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
21c2d3a559SKris Kennaway  *    documentation and/or other materials provided with the distribution.
22c2d3a559SKris Kennaway  *
23c2d3a559SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24c2d3a559SKris Kennaway  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25c2d3a559SKris Kennaway  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26c2d3a559SKris Kennaway  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27c2d3a559SKris Kennaway  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28c2d3a559SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29c2d3a559SKris Kennaway  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30c2d3a559SKris Kennaway  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31c2d3a559SKris Kennaway  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32c2d3a559SKris Kennaway  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33a04a10f8SKris Kennaway  */
34a04a10f8SKris Kennaway 
35a04a10f8SKris Kennaway #include "includes.h"
36ca3176e7SBrian Feldman RCSID("$OpenBSD: session.c,v 1.74 2001/04/17 19:34:25 markus Exp $");
37c2d3a559SKris Kennaway RCSID("$FreeBSD$");
38a04a10f8SKris Kennaway 
39a04a10f8SKris Kennaway #include "ssh.h"
40ca3176e7SBrian Feldman #include "ssh1.h"
41ca3176e7SBrian Feldman #include "ssh2.h"
42ca3176e7SBrian Feldman #include "xmalloc.h"
43ca3176e7SBrian Feldman #include "sshpty.h"
44a04a10f8SKris Kennaway #include "packet.h"
45a04a10f8SKris Kennaway #include "buffer.h"
46a04a10f8SKris Kennaway #include "mpaux.h"
47a04a10f8SKris Kennaway #include "uidswap.h"
48a04a10f8SKris Kennaway #include "compat.h"
49a04a10f8SKris Kennaway #include "channels.h"
50a04a10f8SKris Kennaway #include "nchan.h"
51a04a10f8SKris Kennaway #include "bufaux.h"
52a04a10f8SKris Kennaway #include "auth.h"
53c2d3a559SKris Kennaway #include "auth-options.h"
54ca3176e7SBrian Feldman #include "pathnames.h"
55ca3176e7SBrian Feldman #include "log.h"
56ca3176e7SBrian Feldman #include "servconf.h"
57ca3176e7SBrian Feldman #include "sshlogin.h"
58ca3176e7SBrian Feldman #include "serverloop.h"
59ca3176e7SBrian Feldman #include "canohost.h"
60ca3176e7SBrian Feldman #include "session.h"
61a04a10f8SKris Kennaway 
62e8aafc91SKris Kennaway #ifdef __FreeBSD__
63e8aafc91SKris Kennaway #define _PATH_CHPASS "/usr/bin/passwd"
64e8aafc91SKris Kennaway #endif /* __FreeBSD__ */
65e8aafc91SKris Kennaway 
66c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
67e8aafc91SKris Kennaway #include <login_cap.h>
68c2d3a559SKris Kennaway #endif
69e8aafc91SKris Kennaway 
70b787acb5SKris Kennaway #ifdef KRB5
71b787acb5SKris Kennaway extern krb5_context ssh_context;
72b787acb5SKris Kennaway #endif
73b787acb5SKris Kennaway 
74a04a10f8SKris Kennaway /* types */
75a04a10f8SKris Kennaway 
76a04a10f8SKris Kennaway #define TTYSZ 64
77a04a10f8SKris Kennaway typedef struct Session Session;
78a04a10f8SKris Kennaway struct Session {
79a04a10f8SKris Kennaway 	int	used;
80a04a10f8SKris Kennaway 	int	self;
81a04a10f8SKris Kennaway 	struct	passwd *pw;
82a04a10f8SKris Kennaway 	pid_t	pid;
83a04a10f8SKris Kennaway 	/* tty */
84a04a10f8SKris Kennaway 	char	*term;
85a04a10f8SKris Kennaway 	int	ptyfd, ttyfd, ptymaster;
86a04a10f8SKris Kennaway 	int	row, col, xpixel, ypixel;
87a04a10f8SKris Kennaway 	char	tty[TTYSZ];
88a04a10f8SKris Kennaway 	/* X11 */
89a04a10f8SKris Kennaway 	char	*display;
90a04a10f8SKris Kennaway 	int	screen;
91a04a10f8SKris Kennaway 	char	*auth_proto;
92a04a10f8SKris Kennaway 	char	*auth_data;
93a04a10f8SKris Kennaway 	int	single_connection;
94a04a10f8SKris Kennaway 	/* proto 2 */
95a04a10f8SKris Kennaway 	int	chanid;
96ca3176e7SBrian Feldman 	int	is_subsystem;
97a04a10f8SKris Kennaway };
98a04a10f8SKris Kennaway 
99a04a10f8SKris Kennaway /* func */
100a04a10f8SKris Kennaway 
101a04a10f8SKris Kennaway Session *session_new(void);
102a04a10f8SKris Kennaway void	session_set_fds(Session *s, int fdin, int fdout, int fderr);
103a04a10f8SKris Kennaway void	session_pty_cleanup(Session *s);
104a04a10f8SKris Kennaway void	session_proctitle(Session *s);
105ca3176e7SBrian Feldman void	do_exec_pty(Session *s, const char *command);
106ca3176e7SBrian Feldman void	do_exec_no_pty(Session *s, const char *command);
107ca3176e7SBrian Feldman void	do_login(Session *s, const char *command);
108ca3176e7SBrian Feldman void	do_child(Session *s, const char *command);
109ca3176e7SBrian Feldman void	do_motd(void);
110ca3176e7SBrian Feldman int	check_quietlogin(Session *s, const char *command);
111a04a10f8SKris Kennaway 
112ca3176e7SBrian Feldman void	do_authenticated1(Authctxt *authctxt);
113ca3176e7SBrian Feldman void	do_authenticated2(Authctxt *authctxt);
114a04a10f8SKris Kennaway 
115a04a10f8SKris Kennaway /* import */
116a04a10f8SKris Kennaway extern ServerOptions options;
117a04a10f8SKris Kennaway extern char *__progname;
118a04a10f8SKris Kennaway extern int log_stderr;
119a04a10f8SKris Kennaway extern int debug_flag;
120ca3176e7SBrian Feldman extern u_int utmp_len;
121c2d3a559SKris Kennaway extern int startup_pipe;
122ca3176e7SBrian Feldman extern void destroy_sensitive_data(void);
123a04a10f8SKris Kennaway 
124a04a10f8SKris Kennaway /* Local Xauthority file. */
125a04a10f8SKris Kennaway static char *xauthfile;
126a04a10f8SKris Kennaway 
127c2d3a559SKris Kennaway /* original command from peer. */
128c2d3a559SKris Kennaway char *original_command = NULL;
129c2d3a559SKris Kennaway 
130a04a10f8SKris Kennaway /* data */
131a04a10f8SKris Kennaway #define MAX_SESSIONS 10
132a04a10f8SKris Kennaway Session	sessions[MAX_SESSIONS];
133a04a10f8SKris Kennaway 
134c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
135c2d3a559SKris Kennaway static login_cap_t *lc;
136c2d3a559SKris Kennaway #endif
137a04a10f8SKris Kennaway 
138ca3176e7SBrian Feldman void
139ca3176e7SBrian Feldman do_authenticated(Authctxt *authctxt)
140ca3176e7SBrian Feldman {
141ca3176e7SBrian Feldman 	/*
142ca3176e7SBrian Feldman 	 * Cancel the alarm we set to limit the time taken for
143ca3176e7SBrian Feldman 	 * authentication.
144ca3176e7SBrian Feldman 	 */
145ca3176e7SBrian Feldman 	alarm(0);
146ca3176e7SBrian Feldman 	if (startup_pipe != -1) {
147ca3176e7SBrian Feldman 		close(startup_pipe);
148ca3176e7SBrian Feldman 		startup_pipe = -1;
149ca3176e7SBrian Feldman 	}
150ca3176e7SBrian Feldman #ifdef HAVE_LOGIN_CAP
151ca3176e7SBrian Feldman 	if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
152ca3176e7SBrian Feldman 		error("unable to get login class");
153ca3176e7SBrian Feldman 		return;
154ca3176e7SBrian Feldman 	}
155ca3176e7SBrian Feldman #ifdef BSD_AUTH
156ca3176e7SBrian Feldman 	if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
157ca3176e7SBrian Feldman 		packet_disconnect("Approval failure for %s",
158ca3176e7SBrian Feldman 		    authctxt->pw->pw_name);
159ca3176e7SBrian Feldman 	}
160ca3176e7SBrian Feldman #endif
161ca3176e7SBrian Feldman #endif
162ca3176e7SBrian Feldman 	/* setup the channel layer */
163ca3176e7SBrian Feldman 	if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
164ca3176e7SBrian Feldman 		channel_permit_all_opens();
165ca3176e7SBrian Feldman 
166ca3176e7SBrian Feldman 	if (compat20)
167ca3176e7SBrian Feldman 		do_authenticated2(authctxt);
168ca3176e7SBrian Feldman 	else
169ca3176e7SBrian Feldman 		do_authenticated1(authctxt);
170ca3176e7SBrian Feldman }
171ca3176e7SBrian Feldman 
172a04a10f8SKris Kennaway /*
173a04a10f8SKris Kennaway  * Remove local Xauthority file.
174a04a10f8SKris Kennaway  */
175a04a10f8SKris Kennaway void
176a04a10f8SKris Kennaway xauthfile_cleanup_proc(void *ignore)
177a04a10f8SKris Kennaway {
178a04a10f8SKris Kennaway 	debug("xauthfile_cleanup_proc called");
179a04a10f8SKris Kennaway 
180a04a10f8SKris Kennaway 	if (xauthfile != NULL) {
181a04a10f8SKris Kennaway 		char *p;
182a04a10f8SKris Kennaway 		unlink(xauthfile);
183a04a10f8SKris Kennaway 		p = strrchr(xauthfile, '/');
184a04a10f8SKris Kennaway 		if (p != NULL) {
185a04a10f8SKris Kennaway 			*p = '\0';
186a04a10f8SKris Kennaway 			rmdir(xauthfile);
187a04a10f8SKris Kennaway 		}
188a04a10f8SKris Kennaway 		xfree(xauthfile);
189a04a10f8SKris Kennaway 		xauthfile = NULL;
190a04a10f8SKris Kennaway 	}
191a04a10f8SKris Kennaway }
192a04a10f8SKris Kennaway 
193a04a10f8SKris Kennaway /*
194a04a10f8SKris Kennaway  * Function to perform cleanup if we get aborted abnormally (e.g., due to a
195a04a10f8SKris Kennaway  * dropped connection).
196a04a10f8SKris Kennaway  */
197a04a10f8SKris Kennaway void
198a04a10f8SKris Kennaway pty_cleanup_proc(void *session)
199a04a10f8SKris Kennaway {
200a04a10f8SKris Kennaway 	Session *s=session;
201a04a10f8SKris Kennaway 	if (s == NULL)
202a04a10f8SKris Kennaway 		fatal("pty_cleanup_proc: no session");
203a04a10f8SKris Kennaway 	debug("pty_cleanup_proc: %s", s->tty);
204a04a10f8SKris Kennaway 
205a04a10f8SKris Kennaway 	if (s->pid != 0) {
206a04a10f8SKris Kennaway 		/* Record that the user has logged out. */
207a04a10f8SKris Kennaway 		record_logout(s->pid, s->tty);
208a04a10f8SKris Kennaway 	}
209a04a10f8SKris Kennaway 
210a04a10f8SKris Kennaway 	/* Release the pseudo-tty. */
211a04a10f8SKris Kennaway 	pty_release(s->tty);
212a04a10f8SKris Kennaway }
213a04a10f8SKris Kennaway 
214a04a10f8SKris Kennaway /*
215a04a10f8SKris Kennaway  * Prepares for an interactive session.  This is called after the user has
216a04a10f8SKris Kennaway  * been successfully authenticated.  During this message exchange, pseudo
217a04a10f8SKris Kennaway  * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
218a04a10f8SKris Kennaway  * are requested, etc.
219a04a10f8SKris Kennaway  */
220a04a10f8SKris Kennaway void
221ca3176e7SBrian Feldman do_authenticated1(Authctxt *authctxt)
222a04a10f8SKris Kennaway {
223a04a10f8SKris Kennaway 	Session *s;
224a04a10f8SKris Kennaway 	char *command;
225ca3176e7SBrian Feldman 	int success, type, fd, n_bytes, plen, screen_flag, have_pty = 0;
226ca3176e7SBrian Feldman 	int compression_level = 0, enable_compression_after_reply = 0;
227ca3176e7SBrian Feldman 	u_int proto_len, data_len, dlen;
228a04a10f8SKris Kennaway 
229a04a10f8SKris Kennaway 	s = session_new();
230ca3176e7SBrian Feldman 	s->pw = authctxt->pw;
231c2d3a559SKris Kennaway 
232a04a10f8SKris Kennaway 	/*
233a04a10f8SKris Kennaway 	 * We stay in this loop until the client requests to execute a shell
234a04a10f8SKris Kennaway 	 * or a command.
235a04a10f8SKris Kennaway 	 */
236a04a10f8SKris Kennaway 	for (;;) {
237ca3176e7SBrian Feldman 		success = 0;
238a04a10f8SKris Kennaway 
239a04a10f8SKris Kennaway 		/* Get a packet from the client. */
240a04a10f8SKris Kennaway 		type = packet_read(&plen);
241a04a10f8SKris Kennaway 
242a04a10f8SKris Kennaway 		/* Process the packet. */
243a04a10f8SKris Kennaway 		switch (type) {
244a04a10f8SKris Kennaway 		case SSH_CMSG_REQUEST_COMPRESSION:
245a04a10f8SKris Kennaway 			packet_integrity_check(plen, 4, type);
246a04a10f8SKris Kennaway 			compression_level = packet_get_int();
247a04a10f8SKris Kennaway 			if (compression_level < 1 || compression_level > 9) {
248a04a10f8SKris Kennaway 				packet_send_debug("Received illegal compression level %d.",
249a04a10f8SKris Kennaway 				     compression_level);
250a04a10f8SKris Kennaway 				break;
251a04a10f8SKris Kennaway 			}
252a04a10f8SKris Kennaway 			/* Enable compression after we have responded with SUCCESS. */
253a04a10f8SKris Kennaway 			enable_compression_after_reply = 1;
254a04a10f8SKris Kennaway 			success = 1;
255a04a10f8SKris Kennaway 			break;
256a04a10f8SKris Kennaway 
257a04a10f8SKris Kennaway 		case SSH_CMSG_REQUEST_PTY:
258a04a10f8SKris Kennaway 			if (no_pty_flag) {
259a04a10f8SKris Kennaway 				debug("Allocating a pty not permitted for this authentication.");
260a04a10f8SKris Kennaway 				break;
261a04a10f8SKris Kennaway 			}
262a04a10f8SKris Kennaway 			if (have_pty)
263a04a10f8SKris Kennaway 				packet_disconnect("Protocol error: you already have a pty.");
264a04a10f8SKris Kennaway 
265a04a10f8SKris Kennaway 			debug("Allocating pty.");
266a04a10f8SKris Kennaway 
267a04a10f8SKris Kennaway 			/* Allocate a pty and open it. */
268a04a10f8SKris Kennaway 			if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
269a04a10f8SKris Kennaway 			    sizeof(s->tty))) {
270a04a10f8SKris Kennaway 				error("Failed to allocate pty.");
271a04a10f8SKris Kennaway 				break;
272a04a10f8SKris Kennaway 			}
273a04a10f8SKris Kennaway 			fatal_add_cleanup(pty_cleanup_proc, (void *)s);
274ca3176e7SBrian Feldman 			pty_setowner(s->pw, s->tty);
275a04a10f8SKris Kennaway 
276a04a10f8SKris Kennaway 			/* Get TERM from the packet.  Note that the value may be of arbitrary length. */
277a04a10f8SKris Kennaway 			s->term = packet_get_string(&dlen);
278a04a10f8SKris Kennaway 			packet_integrity_check(dlen, strlen(s->term), type);
279a04a10f8SKris Kennaway 			/* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
280a04a10f8SKris Kennaway 			/* Remaining bytes */
281a04a10f8SKris Kennaway 			n_bytes = plen - (4 + dlen + 4 * 4);
282a04a10f8SKris Kennaway 
283a04a10f8SKris Kennaway 			if (strcmp(s->term, "") == 0) {
284a04a10f8SKris Kennaway 				xfree(s->term);
285a04a10f8SKris Kennaway 				s->term = NULL;
286a04a10f8SKris Kennaway 			}
287a04a10f8SKris Kennaway 			/* Get window size from the packet. */
288a04a10f8SKris Kennaway 			s->row = packet_get_int();
289a04a10f8SKris Kennaway 			s->col = packet_get_int();
290a04a10f8SKris Kennaway 			s->xpixel = packet_get_int();
291a04a10f8SKris Kennaway 			s->ypixel = packet_get_int();
292a04a10f8SKris Kennaway 			pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
293a04a10f8SKris Kennaway 
294a04a10f8SKris Kennaway 			/* Get tty modes from the packet. */
295a04a10f8SKris Kennaway 			tty_parse_modes(s->ttyfd, &n_bytes);
296a04a10f8SKris Kennaway 			packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
297a04a10f8SKris Kennaway 
298a04a10f8SKris Kennaway 			session_proctitle(s);
299a04a10f8SKris Kennaway 
300a04a10f8SKris Kennaway 			/* Indicate that we now have a pty. */
301a04a10f8SKris Kennaway 			success = 1;
302a04a10f8SKris Kennaway 			have_pty = 1;
303a04a10f8SKris Kennaway 			break;
304a04a10f8SKris Kennaway 
305a04a10f8SKris Kennaway 		case SSH_CMSG_X11_REQUEST_FORWARDING:
306a04a10f8SKris Kennaway 			if (!options.x11_forwarding) {
307a04a10f8SKris Kennaway 				packet_send_debug("X11 forwarding disabled in server configuration file.");
308a04a10f8SKris Kennaway 				break;
309a04a10f8SKris Kennaway 			}
310c2d3a559SKris Kennaway 			if (!options.xauth_location) {
311c2d3a559SKris Kennaway 				packet_send_debug("No xauth program; cannot forward with spoofing.");
312c2d3a559SKris Kennaway 				break;
313c2d3a559SKris Kennaway 			}
314a04a10f8SKris Kennaway 			if (no_x11_forwarding_flag) {
315a04a10f8SKris Kennaway 				packet_send_debug("X11 forwarding not permitted for this authentication.");
316a04a10f8SKris Kennaway 				break;
317a04a10f8SKris Kennaway 			}
318a04a10f8SKris Kennaway 			debug("Received request for X11 forwarding with auth spoofing.");
319a04a10f8SKris Kennaway 			if (s->display != NULL)
320a04a10f8SKris Kennaway 				packet_disconnect("Protocol error: X11 display already set.");
321a04a10f8SKris Kennaway 
322a04a10f8SKris Kennaway 			s->auth_proto = packet_get_string(&proto_len);
323a04a10f8SKris Kennaway 			s->auth_data = packet_get_string(&data_len);
324a04a10f8SKris Kennaway 
325ca3176e7SBrian Feldman 			screen_flag = packet_get_protocol_flags() &
326ca3176e7SBrian Feldman 			    SSH_PROTOFLAG_SCREEN_NUMBER;
327ca3176e7SBrian Feldman 			debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
328ca3176e7SBrian Feldman 
329ca3176e7SBrian Feldman 			if (packet_remaining() == 4) {
330ca3176e7SBrian Feldman 				if (!screen_flag)
331ca3176e7SBrian Feldman 					debug2("Buggy client: "
332ca3176e7SBrian Feldman 					    "X11 screen flag missing");
333ca3176e7SBrian Feldman 				packet_integrity_check(plen,
334ca3176e7SBrian Feldman 				    4 + proto_len + 4 + data_len + 4, type);
335a04a10f8SKris Kennaway 				s->screen = packet_get_int();
336ca3176e7SBrian Feldman 			} else {
337ca3176e7SBrian Feldman 				packet_integrity_check(plen,
338ca3176e7SBrian Feldman 				    4 + proto_len + 4 + data_len, type);
339a04a10f8SKris Kennaway 				s->screen = 0;
340ca3176e7SBrian Feldman 			}
341a04a10f8SKris Kennaway 			s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
342a04a10f8SKris Kennaway 
343a04a10f8SKris Kennaway 			if (s->display == NULL)
344a04a10f8SKris Kennaway 				break;
345a04a10f8SKris Kennaway 
346a04a10f8SKris Kennaway 			/* Setup to always have a local .Xauthority. */
347a04a10f8SKris Kennaway 			xauthfile = xmalloc(MAXPATHLEN);
348a04a10f8SKris Kennaway 			strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
349ca3176e7SBrian Feldman 			temporarily_use_uid(s->pw);
350a04a10f8SKris Kennaway 			if (mkdtemp(xauthfile) == NULL) {
351a04a10f8SKris Kennaway 				restore_uid();
352a04a10f8SKris Kennaway 				error("private X11 dir: mkdtemp %s failed: %s",
353a04a10f8SKris Kennaway 				    xauthfile, strerror(errno));
354a04a10f8SKris Kennaway 				xfree(xauthfile);
355a04a10f8SKris Kennaway 				xauthfile = NULL;
356a04a10f8SKris Kennaway 				/* XXXX remove listening channels */
357a04a10f8SKris Kennaway 				break;
358a04a10f8SKris Kennaway 			}
359a04a10f8SKris Kennaway 			strlcat(xauthfile, "/cookies", MAXPATHLEN);
360c2d3a559SKris Kennaway 			fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
361c2d3a559SKris Kennaway 			if (fd >= 0)
362c2d3a559SKris Kennaway 				close(fd);
363a04a10f8SKris Kennaway 			restore_uid();
364a04a10f8SKris Kennaway 			fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
365a04a10f8SKris Kennaway 			success = 1;
366a04a10f8SKris Kennaway 			break;
367a04a10f8SKris Kennaway 
368a04a10f8SKris Kennaway 		case SSH_CMSG_AGENT_REQUEST_FORWARDING:
369a04a10f8SKris Kennaway 			if (no_agent_forwarding_flag || compat13) {
370a04a10f8SKris Kennaway 				debug("Authentication agent forwarding not permitted for this authentication.");
371a04a10f8SKris Kennaway 				break;
372a04a10f8SKris Kennaway 			}
373a04a10f8SKris Kennaway 			debug("Received authentication agent forwarding request.");
374ca3176e7SBrian Feldman 			success = auth_input_request_forwarding(s->pw);
375a04a10f8SKris Kennaway 			break;
376a04a10f8SKris Kennaway 
377a04a10f8SKris Kennaway 		case SSH_CMSG_PORT_FORWARD_REQUEST:
378a04a10f8SKris Kennaway 			if (no_port_forwarding_flag) {
379a04a10f8SKris Kennaway 				debug("Port forwarding not permitted for this authentication.");
380a04a10f8SKris Kennaway 				break;
381a04a10f8SKris Kennaway 			}
38209958426SBrian Feldman 			if (!options.allow_tcp_forwarding) {
38309958426SBrian Feldman 				debug("Port forwarding not permitted.");
38409958426SBrian Feldman 				break;
38509958426SBrian Feldman 			}
386a04a10f8SKris Kennaway 			debug("Received TCP/IP port forwarding request.");
387ca3176e7SBrian Feldman 			channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
388a04a10f8SKris Kennaway 			success = 1;
389a04a10f8SKris Kennaway 			break;
390a04a10f8SKris Kennaway 
391a04a10f8SKris Kennaway 		case SSH_CMSG_MAX_PACKET_SIZE:
392a04a10f8SKris Kennaway 			if (packet_set_maxsize(packet_get_int()) > 0)
393a04a10f8SKris Kennaway 				success = 1;
394a04a10f8SKris Kennaway 			break;
395a04a10f8SKris Kennaway 
396a04a10f8SKris Kennaway 		case SSH_CMSG_EXEC_SHELL:
397a04a10f8SKris Kennaway 		case SSH_CMSG_EXEC_CMD:
398a04a10f8SKris Kennaway 			if (type == SSH_CMSG_EXEC_CMD) {
399a04a10f8SKris Kennaway 				command = packet_get_string(&dlen);
400a04a10f8SKris Kennaway 				debug("Exec command '%.500s'", command);
401a04a10f8SKris Kennaway 				packet_integrity_check(plen, 4 + dlen, type);
402a04a10f8SKris Kennaway 			} else {
403a04a10f8SKris Kennaway 				command = NULL;
404a04a10f8SKris Kennaway 				packet_integrity_check(plen, 0, type);
405a04a10f8SKris Kennaway 			}
406a04a10f8SKris Kennaway 			if (forced_command != NULL) {
407c2d3a559SKris Kennaway 				original_command = command;
408a04a10f8SKris Kennaway 				command = forced_command;
409a04a10f8SKris Kennaway 				debug("Forced command '%.500s'", forced_command);
410a04a10f8SKris Kennaway 			}
411a04a10f8SKris Kennaway 			if (have_pty)
412ca3176e7SBrian Feldman 				do_exec_pty(s, command);
413a04a10f8SKris Kennaway 			else
414ca3176e7SBrian Feldman 				do_exec_no_pty(s, command);
415a04a10f8SKris Kennaway 
416a04a10f8SKris Kennaway 			if (command != NULL)
417a04a10f8SKris Kennaway 				xfree(command);
418a04a10f8SKris Kennaway 			/* Cleanup user's local Xauthority file. */
419a04a10f8SKris Kennaway 			if (xauthfile)
420a04a10f8SKris Kennaway 				xauthfile_cleanup_proc(NULL);
421a04a10f8SKris Kennaway 			return;
422a04a10f8SKris Kennaway 
423a04a10f8SKris Kennaway 		default:
424a04a10f8SKris Kennaway 			/*
425a04a10f8SKris Kennaway 			 * Any unknown messages in this phase are ignored,
426a04a10f8SKris Kennaway 			 * and a failure message is returned.
427a04a10f8SKris Kennaway 			 */
428a04a10f8SKris Kennaway 			log("Unknown packet type received after authentication: %d", type);
429a04a10f8SKris Kennaway 		}
430a04a10f8SKris Kennaway 		packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
431a04a10f8SKris Kennaway 		packet_send();
432a04a10f8SKris Kennaway 		packet_write_wait();
433a04a10f8SKris Kennaway 
434a04a10f8SKris Kennaway 		/* Enable compression now that we have replied if appropriate. */
435a04a10f8SKris Kennaway 		if (enable_compression_after_reply) {
436a04a10f8SKris Kennaway 			enable_compression_after_reply = 0;
437a04a10f8SKris Kennaway 			packet_start_compression(compression_level);
438a04a10f8SKris Kennaway 		}
439a04a10f8SKris Kennaway 	}
440a04a10f8SKris Kennaway }
441a04a10f8SKris Kennaway 
442a04a10f8SKris Kennaway /*
443a04a10f8SKris Kennaway  * This is called to fork and execute a command when we have no tty.  This
444a04a10f8SKris Kennaway  * will call do_child from the child, and server_loop from the parent after
445a04a10f8SKris Kennaway  * setting up file descriptors and such.
446a04a10f8SKris Kennaway  */
447a04a10f8SKris Kennaway void
448ca3176e7SBrian Feldman do_exec_no_pty(Session *s, const char *command)
449a04a10f8SKris Kennaway {
450a04a10f8SKris Kennaway 	int pid;
451a04a10f8SKris Kennaway 
452a04a10f8SKris Kennaway #ifdef USE_PIPES
453a04a10f8SKris Kennaway 	int pin[2], pout[2], perr[2];
454a04a10f8SKris Kennaway 	/* Allocate pipes for communicating with the program. */
455a04a10f8SKris Kennaway 	if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
456a04a10f8SKris Kennaway 		packet_disconnect("Could not create pipes: %.100s",
457a04a10f8SKris Kennaway 				  strerror(errno));
458a04a10f8SKris Kennaway #else /* USE_PIPES */
459a04a10f8SKris Kennaway 	int inout[2], err[2];
460a04a10f8SKris Kennaway 	/* Uses socket pairs to communicate with the program. */
461a04a10f8SKris Kennaway 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
462a04a10f8SKris Kennaway 	    socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
463a04a10f8SKris Kennaway 		packet_disconnect("Could not create socket pairs: %.100s",
464a04a10f8SKris Kennaway 				  strerror(errno));
465a04a10f8SKris Kennaway #endif /* USE_PIPES */
466a04a10f8SKris Kennaway 	if (s == NULL)
467a04a10f8SKris Kennaway 		fatal("do_exec_no_pty: no session");
468a04a10f8SKris Kennaway 
469a04a10f8SKris Kennaway 	session_proctitle(s);
470a04a10f8SKris Kennaway 
47109958426SBrian Feldman #ifdef USE_PAM
47209958426SBrian Feldman 	do_pam_setcred();
47309958426SBrian Feldman #endif /* USE_PAM */
47409958426SBrian Feldman 
475a04a10f8SKris Kennaway 	/* Fork the child. */
476a04a10f8SKris Kennaway 	if ((pid = fork()) == 0) {
477a04a10f8SKris Kennaway 		/* Child.  Reinitialize the log since the pid has changed. */
478a04a10f8SKris Kennaway 		log_init(__progname, options.log_level, options.log_facility, log_stderr);
479a04a10f8SKris Kennaway 
480a04a10f8SKris Kennaway 		/*
481a04a10f8SKris Kennaway 		 * Create a new session and process group since the 4.4BSD
482a04a10f8SKris Kennaway 		 * setlogin() affects the entire process group.
483a04a10f8SKris Kennaway 		 */
484a04a10f8SKris Kennaway 		if (setsid() < 0)
485a04a10f8SKris Kennaway 			error("setsid failed: %.100s", strerror(errno));
486a04a10f8SKris Kennaway 
487a04a10f8SKris Kennaway #ifdef USE_PIPES
488a04a10f8SKris Kennaway 		/*
489a04a10f8SKris Kennaway 		 * Redirect stdin.  We close the parent side of the socket
490a04a10f8SKris Kennaway 		 * pair, and make the child side the standard input.
491a04a10f8SKris Kennaway 		 */
492a04a10f8SKris Kennaway 		close(pin[1]);
493a04a10f8SKris Kennaway 		if (dup2(pin[0], 0) < 0)
494a04a10f8SKris Kennaway 			perror("dup2 stdin");
495a04a10f8SKris Kennaway 		close(pin[0]);
496a04a10f8SKris Kennaway 
497a04a10f8SKris Kennaway 		/* Redirect stdout. */
498a04a10f8SKris Kennaway 		close(pout[0]);
499a04a10f8SKris Kennaway 		if (dup2(pout[1], 1) < 0)
500a04a10f8SKris Kennaway 			perror("dup2 stdout");
501a04a10f8SKris Kennaway 		close(pout[1]);
502a04a10f8SKris Kennaway 
503a04a10f8SKris Kennaway 		/* Redirect stderr. */
504a04a10f8SKris Kennaway 		close(perr[0]);
505a04a10f8SKris Kennaway 		if (dup2(perr[1], 2) < 0)
506a04a10f8SKris Kennaway 			perror("dup2 stderr");
507a04a10f8SKris Kennaway 		close(perr[1]);
508a04a10f8SKris Kennaway #else /* USE_PIPES */
509a04a10f8SKris Kennaway 		/*
510a04a10f8SKris Kennaway 		 * Redirect stdin, stdout, and stderr.  Stdin and stdout will
511a04a10f8SKris Kennaway 		 * use the same socket, as some programs (particularly rdist)
512a04a10f8SKris Kennaway 		 * seem to depend on it.
513a04a10f8SKris Kennaway 		 */
514a04a10f8SKris Kennaway 		close(inout[1]);
515a04a10f8SKris Kennaway 		close(err[1]);
516a04a10f8SKris Kennaway 		if (dup2(inout[0], 0) < 0)	/* stdin */
517a04a10f8SKris Kennaway 			perror("dup2 stdin");
518a04a10f8SKris Kennaway 		if (dup2(inout[0], 1) < 0)	/* stdout.  Note: same socket as stdin. */
519a04a10f8SKris Kennaway 			perror("dup2 stdout");
520a04a10f8SKris Kennaway 		if (dup2(err[0], 2) < 0)	/* stderr */
521a04a10f8SKris Kennaway 			perror("dup2 stderr");
522a04a10f8SKris Kennaway #endif /* USE_PIPES */
523a04a10f8SKris Kennaway 
524a04a10f8SKris Kennaway 		/* Do processing for the child (exec command etc). */
525ca3176e7SBrian Feldman 		do_child(s, command);
526a04a10f8SKris Kennaway 		/* NOTREACHED */
527a04a10f8SKris Kennaway 	}
528a04a10f8SKris Kennaway 	if (pid < 0)
529a04a10f8SKris Kennaway 		packet_disconnect("fork failed: %.100s", strerror(errno));
530a04a10f8SKris Kennaway 	s->pid = pid;
531ca3176e7SBrian Feldman 	/* Set interactive/non-interactive mode. */
532ca3176e7SBrian Feldman 	packet_set_interactive(s->display != NULL);
533a04a10f8SKris Kennaway #ifdef USE_PIPES
534a04a10f8SKris Kennaway 	/* We are the parent.  Close the child sides of the pipes. */
535a04a10f8SKris Kennaway 	close(pin[0]);
536a04a10f8SKris Kennaway 	close(pout[1]);
537a04a10f8SKris Kennaway 	close(perr[1]);
538a04a10f8SKris Kennaway 
539a04a10f8SKris Kennaway 	if (compat20) {
540ca3176e7SBrian Feldman 		session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
541a04a10f8SKris Kennaway 	} else {
542a04a10f8SKris Kennaway 		/* Enter the interactive session. */
543a04a10f8SKris Kennaway 		server_loop(pid, pin[1], pout[0], perr[0]);
544ca3176e7SBrian Feldman 		/* server_loop has closed pin[1], pout[0], and perr[0]. */
545a04a10f8SKris Kennaway 	}
546a04a10f8SKris Kennaway #else /* USE_PIPES */
547a04a10f8SKris Kennaway 	/* We are the parent.  Close the child sides of the socket pairs. */
548a04a10f8SKris Kennaway 	close(inout[0]);
549a04a10f8SKris Kennaway 	close(err[0]);
550a04a10f8SKris Kennaway 
551a04a10f8SKris Kennaway 	/*
552a04a10f8SKris Kennaway 	 * Enter the interactive session.  Note: server_loop must be able to
553a04a10f8SKris Kennaway 	 * handle the case that fdin and fdout are the same.
554a04a10f8SKris Kennaway 	 */
555a04a10f8SKris Kennaway 	if (compat20) {
556ca3176e7SBrian Feldman 		session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
557a04a10f8SKris Kennaway 	} else {
558a04a10f8SKris Kennaway 		server_loop(pid, inout[1], inout[1], err[1]);
559a04a10f8SKris Kennaway 		/* server_loop has closed inout[1] and err[1]. */
560a04a10f8SKris Kennaway 	}
561a04a10f8SKris Kennaway #endif /* USE_PIPES */
562a04a10f8SKris Kennaway }
563a04a10f8SKris Kennaway 
564a04a10f8SKris Kennaway /*
565a04a10f8SKris Kennaway  * This is called to fork and execute a command when we have a tty.  This
566a04a10f8SKris Kennaway  * will call do_child from the child, and server_loop from the parent after
567a04a10f8SKris Kennaway  * setting up file descriptors, controlling tty, updating wtmp, utmp,
568a04a10f8SKris Kennaway  * lastlog, and other such operations.
569a04a10f8SKris Kennaway  */
570a04a10f8SKris Kennaway void
571ca3176e7SBrian Feldman do_exec_pty(Session *s, const char *command)
572a04a10f8SKris Kennaway {
573a04a10f8SKris Kennaway 	int fdout, ptyfd, ttyfd, ptymaster;
574a04a10f8SKris Kennaway 	pid_t pid;
575a04a10f8SKris Kennaway 
576a04a10f8SKris Kennaway 	if (s == NULL)
577a04a10f8SKris Kennaway 		fatal("do_exec_pty: no session");
578a04a10f8SKris Kennaway 	ptyfd = s->ptyfd;
579a04a10f8SKris Kennaway 	ttyfd = s->ttyfd;
580a04a10f8SKris Kennaway 
58109958426SBrian Feldman #ifdef USE_PAM
582ca3176e7SBrian Feldman 	do_pam_session(s->pw->pw_name, s->tty);
58309958426SBrian Feldman 	do_pam_setcred();
58409958426SBrian Feldman #endif /* USE_PAM */
58509958426SBrian Feldman 
586a04a10f8SKris Kennaway 	/* Fork the child. */
587a04a10f8SKris Kennaway 	if ((pid = fork()) == 0) {
588c2d3a559SKris Kennaway 		/* Child.  Reinitialize the log because the pid has changed. */
589a04a10f8SKris Kennaway 		log_init(__progname, options.log_level, options.log_facility, log_stderr);
590a04a10f8SKris Kennaway 
591a04a10f8SKris Kennaway 		/* Close the master side of the pseudo tty. */
592a04a10f8SKris Kennaway 		close(ptyfd);
593a04a10f8SKris Kennaway 
594a04a10f8SKris Kennaway 		/* Make the pseudo tty our controlling tty. */
595a04a10f8SKris Kennaway 		pty_make_controlling_tty(&ttyfd, s->tty);
596a04a10f8SKris Kennaway 
597a04a10f8SKris Kennaway 		/* Redirect stdin from the pseudo tty. */
598a04a10f8SKris Kennaway 		if (dup2(ttyfd, fileno(stdin)) < 0)
599a04a10f8SKris Kennaway 			error("dup2 stdin failed: %.100s", strerror(errno));
600a04a10f8SKris Kennaway 
601a04a10f8SKris Kennaway 		/* Redirect stdout to the pseudo tty. */
602a04a10f8SKris Kennaway 		if (dup2(ttyfd, fileno(stdout)) < 0)
603a04a10f8SKris Kennaway 			error("dup2 stdin failed: %.100s", strerror(errno));
604a04a10f8SKris Kennaway 
605a04a10f8SKris Kennaway 		/* Redirect stderr to the pseudo tty. */
606a04a10f8SKris Kennaway 		if (dup2(ttyfd, fileno(stderr)) < 0)
607a04a10f8SKris Kennaway 			error("dup2 stdin failed: %.100s", strerror(errno));
608a04a10f8SKris Kennaway 
609a04a10f8SKris Kennaway 		/* Close the extra descriptor for the pseudo tty. */
610a04a10f8SKris Kennaway 		close(ttyfd);
611a04a10f8SKris Kennaway 
612c2d3a559SKris Kennaway 		/* record login, etc. similar to login(1) */
613ca3176e7SBrian Feldman 		if (!(options.use_login && command == NULL))
614ca3176e7SBrian Feldman 			do_login(s, command);
615e8aafc91SKris Kennaway 
616a04a10f8SKris Kennaway 		/* Do common processing for the child, such as execing the command. */
617ca3176e7SBrian Feldman 		do_child(s, command);
618a04a10f8SKris Kennaway 		/* NOTREACHED */
619a04a10f8SKris Kennaway 	}
620a04a10f8SKris Kennaway 	if (pid < 0)
621a04a10f8SKris Kennaway 		packet_disconnect("fork failed: %.100s", strerror(errno));
622a04a10f8SKris Kennaway 	s->pid = pid;
623a04a10f8SKris Kennaway 
624a04a10f8SKris Kennaway 	/* Parent.  Close the slave side of the pseudo tty. */
625a04a10f8SKris Kennaway 	close(ttyfd);
626a04a10f8SKris Kennaway 
627a04a10f8SKris Kennaway 	/*
628a04a10f8SKris Kennaway 	 * Create another descriptor of the pty master side for use as the
629a04a10f8SKris Kennaway 	 * standard input.  We could use the original descriptor, but this
630a04a10f8SKris Kennaway 	 * simplifies code in server_loop.  The descriptor is bidirectional.
631a04a10f8SKris Kennaway 	 */
632a04a10f8SKris Kennaway 	fdout = dup(ptyfd);
633a04a10f8SKris Kennaway 	if (fdout < 0)
634a04a10f8SKris Kennaway 		packet_disconnect("dup #1 failed: %.100s", strerror(errno));
635a04a10f8SKris Kennaway 
636a04a10f8SKris Kennaway 	/* we keep a reference to the pty master */
637a04a10f8SKris Kennaway 	ptymaster = dup(ptyfd);
638a04a10f8SKris Kennaway 	if (ptymaster < 0)
639a04a10f8SKris Kennaway 		packet_disconnect("dup #2 failed: %.100s", strerror(errno));
640a04a10f8SKris Kennaway 	s->ptymaster = ptymaster;
641a04a10f8SKris Kennaway 
642a04a10f8SKris Kennaway 	/* Enter interactive session. */
643ca3176e7SBrian Feldman 	packet_set_interactive(1);
644a04a10f8SKris Kennaway 	if (compat20) {
645a04a10f8SKris Kennaway 		session_set_fds(s, ptyfd, fdout, -1);
646a04a10f8SKris Kennaway 	} else {
647a04a10f8SKris Kennaway 		server_loop(pid, ptyfd, fdout, -1);
648a04a10f8SKris Kennaway 		/* server_loop _has_ closed ptyfd and fdout. */
649a04a10f8SKris Kennaway 		session_pty_cleanup(s);
650a04a10f8SKris Kennaway 	}
651a04a10f8SKris Kennaway }
652a04a10f8SKris Kennaway 
653c2d3a559SKris Kennaway /* administrative, login(1)-like work */
654ca3176e7SBrian Feldman void
655c2d3a559SKris Kennaway do_login(Session *s, const char *command)
656c2d3a559SKris Kennaway {
657c2d3a559SKris Kennaway 	FILE *f;
658c2d3a559SKris Kennaway 	char *time_string, *newcommand;
659c2d3a559SKris Kennaway 	char buf[256];
660c2d3a559SKris Kennaway 	char hostname[MAXHOSTNAMELEN];
661c2d3a559SKris Kennaway 	socklen_t fromlen;
662c2d3a559SKris Kennaway 	struct sockaddr_storage from;
663c2d3a559SKris Kennaway 	time_t last_login_time;
664c2d3a559SKris Kennaway 	struct passwd * pw = s->pw;
665c2d3a559SKris Kennaway 	pid_t pid = getpid();
666c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
667c2d3a559SKris Kennaway 	char *fname;
668c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
669c2d3a559SKris Kennaway #ifdef __FreeBSD__
670c2d3a559SKris Kennaway #define DEFAULT_WARN  (2L * 7L * 86400L)  /* Two weeks */
671c2d3a559SKris Kennaway 	struct timeval tv;
672c2d3a559SKris Kennaway 	time_t warntime = DEFAULT_WARN;
673c2d3a559SKris Kennaway #endif /* __FreeBSD__ */
674c2d3a559SKris Kennaway 
675c2d3a559SKris Kennaway 	/*
676c2d3a559SKris Kennaway 	 * Get IP address of client. If the connection is not a socket, let
677c2d3a559SKris Kennaway 	 * the address be 0.0.0.0.
678c2d3a559SKris Kennaway 	 */
679c2d3a559SKris Kennaway 	memset(&from, 0, sizeof(from));
680c2d3a559SKris Kennaway 	if (packet_connection_is_on_socket()) {
681c2d3a559SKris Kennaway 		fromlen = sizeof(from);
682c2d3a559SKris Kennaway 		if (getpeername(packet_get_connection_in(),
683c2d3a559SKris Kennaway 		     (struct sockaddr *) & from, &fromlen) < 0) {
684c2d3a559SKris Kennaway 			debug("getpeername: %.100s", strerror(errno));
685c2d3a559SKris Kennaway 			fatal_cleanup();
686c2d3a559SKris Kennaway 		}
687c2d3a559SKris Kennaway 	}
688c2d3a559SKris Kennaway 
689c2d3a559SKris Kennaway 	/* Get the time and hostname when the user last logged in. */
690ca3176e7SBrian Feldman 	if (options.print_lastlog) {
691c2d3a559SKris Kennaway 		hostname[0] = '\0';
692c2d3a559SKris Kennaway 		last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
693c2d3a559SKris Kennaway 		    hostname, sizeof(hostname));
694ca3176e7SBrian Feldman 	}
695c2d3a559SKris Kennaway 
696c2d3a559SKris Kennaway 	/* Record that there was a login on that tty from the remote host. */
697c2d3a559SKris Kennaway 	record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
698ca3176e7SBrian Feldman 	    get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
699ca3176e7SBrian Feldman 	    (struct sockaddr *)&from);
700c2d3a559SKris Kennaway 
70109958426SBrian Feldman #ifdef USE_PAM
70209958426SBrian Feldman 	/*
70309958426SBrian Feldman 	 * If password change is needed, do it now.
70409958426SBrian Feldman 	 * This needs to occur before the ~/.hushlogin check.
70509958426SBrian Feldman 	 */
70609958426SBrian Feldman 	if (pam_password_change_required()) {
70709958426SBrian Feldman 		print_pam_messages();
70809958426SBrian Feldman 		do_pam_chauthtok();
70909958426SBrian Feldman 	}
71009958426SBrian Feldman #endif
71109958426SBrian Feldman 
71209958426SBrian Feldman #ifdef USE_PAM
713ca3176e7SBrian Feldman 	if (!check_quietlogin(s, command) && !pam_password_change_required())
71409958426SBrian Feldman 		print_pam_messages();
71509958426SBrian Feldman #endif /* USE_PAM */
716c2d3a559SKris Kennaway 
717c2d3a559SKris Kennaway #ifdef __FreeBSD__
718c2d3a559SKris Kennaway 	if (pw->pw_change || pw->pw_expire)
719c2d3a559SKris Kennaway 		(void)gettimeofday(&tv, NULL);
720c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
721c2d3a559SKris Kennaway 	warntime = login_getcaptime(lc, "warnpassword",
722c2d3a559SKris Kennaway 				    DEFAULT_WARN, DEFAULT_WARN);
723c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
724c2d3a559SKris Kennaway 	/*
725c2d3a559SKris Kennaway 	 * If the password change time is set and has passed, give the
726c2d3a559SKris Kennaway 	 * user a password expiry notice and chance to change it.
727c2d3a559SKris Kennaway 	 */
728c2d3a559SKris Kennaway 	if (pw->pw_change != 0) {
729c2d3a559SKris Kennaway 		if (tv.tv_sec >= pw->pw_change) {
730c2d3a559SKris Kennaway 			(void)printf(
731c2d3a559SKris Kennaway 			    "Sorry -- your password has expired.\n");
732c2d3a559SKris Kennaway 			log("%s Password expired - forcing change",
733c2d3a559SKris Kennaway 			    pw->pw_name);
73409958426SBrian Feldman 			if (newcommand != NULL)
73509958426SBrian Feldman 				xfree(newcommand);
73609958426SBrian Feldman 			newcommand = xstrdup(_PATH_CHPASS);
737c2d3a559SKris Kennaway 		} else if (pw->pw_change - tv.tv_sec < warntime &&
738ca3176e7SBrian Feldman 			   !check_quietlogin(s, command))
739c2d3a559SKris Kennaway 			(void)printf(
740c2d3a559SKris Kennaway 			    "Warning: your password expires on %s",
741c2d3a559SKris Kennaway 			     ctime(&pw->pw_change));
742c2d3a559SKris Kennaway 	}
743c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
744c2d3a559SKris Kennaway 	warntime = login_getcaptime(lc, "warnexpire",
745c2d3a559SKris Kennaway 				    DEFAULT_WARN, DEFAULT_WARN);
746c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
74709958426SBrian Feldman #ifndef USE_PAM
748c2d3a559SKris Kennaway 	if (pw->pw_expire) {
749c2d3a559SKris Kennaway 		if (tv.tv_sec >= pw->pw_expire) {
750c2d3a559SKris Kennaway 			(void)printf(
751c2d3a559SKris Kennaway 			    "Sorry -- your account has expired.\n");
752c2d3a559SKris Kennaway 			log(
753c2d3a559SKris Kennaway 	   "LOGIN %.200s REFUSED (EXPIRED) FROM %.200s ON TTY %.200s",
754ca3176e7SBrian Feldman 				pw->pw_name, get_remote_name_or_ip(utmp_len,
755ca3176e7SBrian Feldman 				options.reverse_mapping_check), s->tty);
756c2d3a559SKris Kennaway 			exit(254);
757c2d3a559SKris Kennaway 		} else if (pw->pw_expire - tv.tv_sec < warntime &&
758ca3176e7SBrian Feldman 			   !check_quietlogin(s, command))
759c2d3a559SKris Kennaway 			(void)printf(
760c2d3a559SKris Kennaway 			    "Warning: your account expires on %s",
761c2d3a559SKris Kennaway 			     ctime(&pw->pw_expire));
762c2d3a559SKris Kennaway 	}
76309958426SBrian Feldman #endif /* !USE_PAM */
764c2d3a559SKris Kennaway #endif /* __FreeBSD__ */
765c2d3a559SKris Kennaway 
766c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
767c2d3a559SKris Kennaway 	if (!auth_ttyok(lc, s->tty)) {
768c2d3a559SKris Kennaway 		(void)printf("Permission denied.\n");
769c2d3a559SKris Kennaway 		log(
770c2d3a559SKris Kennaway 	       "LOGIN %.200s REFUSED (TTY) FROM %.200s ON TTY %.200s",
771ca3176e7SBrian Feldman 		    pw->pw_name, get_remote_name_or_ip(utmp_len,
772ca3176e7SBrian Feldman 			options.reverse_mapping_check), s->tty);
773c2d3a559SKris Kennaway 		exit(254);
774c2d3a559SKris Kennaway 	}
775c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
776c2d3a559SKris Kennaway 
777c2d3a559SKris Kennaway 	/*
778c2d3a559SKris Kennaway 	 * If the user has logged in before, display the time of last
779c2d3a559SKris Kennaway 	 * login. However, don't display anything extra if a command
780c2d3a559SKris Kennaway 	 * has been specified (so that ssh can be used to execute
781c2d3a559SKris Kennaway 	 * commands on a remote machine without users knowing they
782c2d3a559SKris Kennaway 	 * are going to another machine). Login(1) will do this for
783c2d3a559SKris Kennaway 	 * us as well, so check if login(1) is used
784c2d3a559SKris Kennaway 	 */
785ca3176e7SBrian Feldman 	if (command == NULL && options.print_lastlog &&
786ca3176e7SBrian Feldman 	    last_login_time != 0 && !check_quietlogin(s, command) &&
787c2d3a559SKris Kennaway 	    !options.use_login) {
788c2d3a559SKris Kennaway 		time_string = ctime(&last_login_time);
789c2d3a559SKris Kennaway 		/* Remove the trailing newline. */
790c2d3a559SKris Kennaway 		if (strchr(time_string, '\n'))
791c2d3a559SKris Kennaway 			*strchr(time_string, '\n') = 0;
79209958426SBrian Feldman 		if (strcmp(hostname, "") == 0)
793c2d3a559SKris Kennaway 			printf("Last login: %s\r\n", time_string);
794c2d3a559SKris Kennaway 		else
795c2d3a559SKris Kennaway 			printf("Last login: %s from %s\r\n", time_string, hostname);
796c2d3a559SKris Kennaway 	}
797c2d3a559SKris Kennaway 
798c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
799ca3176e7SBrian Feldman 	if (command == NULL && !check_quietlogin(s, command) &&
800ca3176e7SBrian Feldman 	    !options.use_login) {
801c2d3a559SKris Kennaway 		fname = login_getcapstr(lc, "copyright", NULL, NULL);
802c2d3a559SKris Kennaway 		if (fname != NULL && (f = fopen(fname, "r")) != NULL) {
803c2d3a559SKris Kennaway 			while (fgets(buf, sizeof(buf), f) != NULL)
804c2d3a559SKris Kennaway 				fputs(buf, stdout);
805c2d3a559SKris Kennaway 				fclose(f);
806c2d3a559SKris Kennaway 		} else
807c2d3a559SKris Kennaway 			(void)printf("%s\n\t%s %s\n",
808c2d3a559SKris Kennaway 		"Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
809c2d3a559SKris Kennaway 		"The Regents of the University of California. ",
810c2d3a559SKris Kennaway 		"All rights reserved.");
811c2d3a559SKris Kennaway 	}
812c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
813c2d3a559SKris Kennaway 
814c2d3a559SKris Kennaway 	/*
815c2d3a559SKris Kennaway 	 * Print /etc/motd unless a command was specified or printing
816c2d3a559SKris Kennaway 	 * it was disabled in server options or login(1) will be
817c2d3a559SKris Kennaway 	 * used.  Note that some machines appear to print it in
818c2d3a559SKris Kennaway 	 * /etc/profile or similar.
819c2d3a559SKris Kennaway 	 */
820ca3176e7SBrian Feldman 	if (command == NULL && !check_quietlogin(s, command) && !options.use_login)
821ca3176e7SBrian Feldman 		do_motd();
822ca3176e7SBrian Feldman }
823ca3176e7SBrian Feldman 
824ca3176e7SBrian Feldman /*
825ca3176e7SBrian Feldman  * Display the message of the day.
826ca3176e7SBrian Feldman  */
827ca3176e7SBrian Feldman void
828ca3176e7SBrian Feldman do_motd(void)
829ca3176e7SBrian Feldman {
830ca3176e7SBrian Feldman 	FILE *f;
831ca3176e7SBrian Feldman 	char buf[256];
832ca3176e7SBrian Feldman 
833ca3176e7SBrian Feldman 	if (options.print_motd) {
834c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
835c2d3a559SKris Kennaway 		f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
836c2d3a559SKris Kennaway 		    "/etc/motd"), "r");
837c2d3a559SKris Kennaway #else /* !HAVE_LOGIN_CAP */
838c2d3a559SKris Kennaway 		f = fopen("/etc/motd", "r");
839c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
840c2d3a559SKris Kennaway 		if (f) {
841c2d3a559SKris Kennaway 			while (fgets(buf, sizeof(buf), f))
842c2d3a559SKris Kennaway 				fputs(buf, stdout);
843c2d3a559SKris Kennaway 			fclose(f);
844c2d3a559SKris Kennaway 		}
845c2d3a559SKris Kennaway 	}
846ca3176e7SBrian Feldman }
847c2d3a559SKris Kennaway 
848ca3176e7SBrian Feldman /*
849ca3176e7SBrian Feldman  * Check for quiet login, either .hushlogin or command given.
850ca3176e7SBrian Feldman  */
851ca3176e7SBrian Feldman int
852ca3176e7SBrian Feldman check_quietlogin(Session *s, const char *command)
853ca3176e7SBrian Feldman {
854ca3176e7SBrian Feldman 	char buf[256];
855ca3176e7SBrian Feldman 	struct passwd * pw = s->pw;
856ca3176e7SBrian Feldman 	struct stat st;
857ca3176e7SBrian Feldman 
858ca3176e7SBrian Feldman 	/* Return 1 if .hushlogin exists or a command given. */
859ca3176e7SBrian Feldman 	if (command != NULL)
860ca3176e7SBrian Feldman 		return 1;
861ca3176e7SBrian Feldman 	snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
862c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
863ca3176e7SBrian Feldman 	if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
864ca3176e7SBrian Feldman 		return 1;
865ca3176e7SBrian Feldman #else
866ca3176e7SBrian Feldman 	if (stat(buf, &st) >= 0)
867ca3176e7SBrian Feldman 		return 1;
868ca3176e7SBrian Feldman #endif
869ca3176e7SBrian Feldman 	return 0;
870c2d3a559SKris Kennaway }
871c2d3a559SKris Kennaway 
872a04a10f8SKris Kennaway /*
873a04a10f8SKris Kennaway  * Sets the value of the given variable in the environment.  If the variable
874a04a10f8SKris Kennaway  * already exists, its value is overriden.
875a04a10f8SKris Kennaway  */
876a04a10f8SKris Kennaway void
877ca3176e7SBrian Feldman child_set_env(char ***envp, u_int *envsizep, const char *name,
878a04a10f8SKris Kennaway 	      const char *value)
879a04a10f8SKris Kennaway {
880ca3176e7SBrian Feldman 	u_int i, namelen;
881a04a10f8SKris Kennaway 	char **env;
882a04a10f8SKris Kennaway 
883a04a10f8SKris Kennaway 	/*
884a04a10f8SKris Kennaway 	 * Find the slot where the value should be stored.  If the variable
885a04a10f8SKris Kennaway 	 * already exists, we reuse the slot; otherwise we append a new slot
886a04a10f8SKris Kennaway 	 * at the end of the array, expanding if necessary.
887a04a10f8SKris Kennaway 	 */
888a04a10f8SKris Kennaway 	env = *envp;
889a04a10f8SKris Kennaway 	namelen = strlen(name);
890a04a10f8SKris Kennaway 	for (i = 0; env[i]; i++)
891a04a10f8SKris Kennaway 		if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
892a04a10f8SKris Kennaway 			break;
893a04a10f8SKris Kennaway 	if (env[i]) {
894a04a10f8SKris Kennaway 		/* Reuse the slot. */
895a04a10f8SKris Kennaway 		xfree(env[i]);
896a04a10f8SKris Kennaway 	} else {
897a04a10f8SKris Kennaway 		/* New variable.  Expand if necessary. */
898a04a10f8SKris Kennaway 		if (i >= (*envsizep) - 1) {
899a04a10f8SKris Kennaway 			(*envsizep) += 50;
900a04a10f8SKris Kennaway 			env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
901a04a10f8SKris Kennaway 		}
902a04a10f8SKris Kennaway 		/* Need to set the NULL pointer at end of array beyond the new slot. */
903a04a10f8SKris Kennaway 		env[i + 1] = NULL;
904a04a10f8SKris Kennaway 	}
905a04a10f8SKris Kennaway 
906a04a10f8SKris Kennaway 	/* Allocate space and format the variable in the appropriate slot. */
907a04a10f8SKris Kennaway 	env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
908a04a10f8SKris Kennaway 	snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
909a04a10f8SKris Kennaway }
910a04a10f8SKris Kennaway 
911a04a10f8SKris Kennaway /*
912a04a10f8SKris Kennaway  * Reads environment variables from the given file and adds/overrides them
913a04a10f8SKris Kennaway  * into the environment.  If the file does not exist, this does nothing.
914a04a10f8SKris Kennaway  * Otherwise, it must consist of empty lines, comments (line starts with '#')
915a04a10f8SKris Kennaway  * and assignments of the form name=value.  No other forms are allowed.
916a04a10f8SKris Kennaway  */
917a04a10f8SKris Kennaway void
918ca3176e7SBrian Feldman read_environment_file(char ***env, u_int *envsize,
919a04a10f8SKris Kennaway 		      const char *filename)
920a04a10f8SKris Kennaway {
921a04a10f8SKris Kennaway 	FILE *f;
922a04a10f8SKris Kennaway 	char buf[4096];
923a04a10f8SKris Kennaway 	char *cp, *value;
924a04a10f8SKris Kennaway 
925a04a10f8SKris Kennaway 	f = fopen(filename, "r");
926a04a10f8SKris Kennaway 	if (!f)
927a04a10f8SKris Kennaway 		return;
928a04a10f8SKris Kennaway 
929a04a10f8SKris Kennaway 	while (fgets(buf, sizeof(buf), f)) {
930a04a10f8SKris Kennaway 		for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
931a04a10f8SKris Kennaway 			;
932a04a10f8SKris Kennaway 		if (!*cp || *cp == '#' || *cp == '\n')
933a04a10f8SKris Kennaway 			continue;
934a04a10f8SKris Kennaway 		if (strchr(cp, '\n'))
935a04a10f8SKris Kennaway 			*strchr(cp, '\n') = '\0';
936a04a10f8SKris Kennaway 		value = strchr(cp, '=');
937a04a10f8SKris Kennaway 		if (value == NULL) {
938a04a10f8SKris Kennaway 			fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
939a04a10f8SKris Kennaway 			continue;
940a04a10f8SKris Kennaway 		}
941db1cb46cSKris Kennaway 		/*
942db1cb46cSKris Kennaway 		 * Replace the equals sign by nul, and advance value to
943db1cb46cSKris Kennaway 		 * the value string.
944db1cb46cSKris Kennaway 		 */
945a04a10f8SKris Kennaway 		*value = '\0';
946a04a10f8SKris Kennaway 		value++;
947a04a10f8SKris Kennaway 		child_set_env(env, envsize, cp, value);
948a04a10f8SKris Kennaway 	}
949a04a10f8SKris Kennaway 	fclose(f);
950a04a10f8SKris Kennaway }
951a04a10f8SKris Kennaway 
95209958426SBrian Feldman #ifdef USE_PAM
95309958426SBrian Feldman /*
95409958426SBrian Feldman  * Sets any environment variables which have been specified by PAM
95509958426SBrian Feldman  */
95609958426SBrian Feldman void do_pam_environment(char ***env, int *envsize)
95709958426SBrian Feldman {
95809958426SBrian Feldman 	char *equals, var_name[512], var_val[512];
95909958426SBrian Feldman 	char **pam_env;
96009958426SBrian Feldman 	int i;
96109958426SBrian Feldman 
96209958426SBrian Feldman 	if ((pam_env = fetch_pam_environment()) == NULL)
96309958426SBrian Feldman 		return;
96409958426SBrian Feldman 
96509958426SBrian Feldman 	for(i = 0; pam_env[i] != NULL; i++) {
96609958426SBrian Feldman 		if ((equals = strstr(pam_env[i], "=")) == NULL)
96709958426SBrian Feldman 			continue;
96809958426SBrian Feldman 
96909958426SBrian Feldman 		if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
97009958426SBrian Feldman 			memset(var_name, '\0', sizeof(var_name));
97109958426SBrian Feldman 			memset(var_val, '\0', sizeof(var_val));
97209958426SBrian Feldman 
97309958426SBrian Feldman 			strncpy(var_name, pam_env[i], equals - pam_env[i]);
97409958426SBrian Feldman 			strcpy(var_val, equals + 1);
97509958426SBrian Feldman 
97609958426SBrian Feldman 			child_set_env(env, envsize, var_name, var_val);
97709958426SBrian Feldman 		}
97809958426SBrian Feldman 	}
97909958426SBrian Feldman }
98009958426SBrian Feldman #endif /* USE_PAM */
98109958426SBrian Feldman 
98209958426SBrian Feldman 
983a04a10f8SKris Kennaway /*
984a04a10f8SKris Kennaway  * Performs common processing for the child, such as setting up the
985a04a10f8SKris Kennaway  * environment, closing extra file descriptors, setting the user and group
986a04a10f8SKris Kennaway  * ids, and executing the command or shell.
987a04a10f8SKris Kennaway  */
988a04a10f8SKris Kennaway void
989ca3176e7SBrian Feldman do_child(Session *s, const char *command)
990a04a10f8SKris Kennaway {
991c2d3a559SKris Kennaway 	const char *shell, *hostname = NULL, *cp = NULL;
992ca3176e7SBrian Feldman 	struct passwd * pw = s->pw;
993a04a10f8SKris Kennaway 	char buf[256];
994c2d3a559SKris Kennaway 	char cmd[1024];
995c2d3a559SKris Kennaway 	FILE *f = NULL;
996ca3176e7SBrian Feldman 	u_int envsize, i;
997ca3176e7SBrian Feldman 	char **env;
998a04a10f8SKris Kennaway 	extern char **environ;
999a04a10f8SKris Kennaway 	struct stat st;
1000a04a10f8SKris Kennaway 	char *argv[10];
1001ca3176e7SBrian Feldman 	int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
1002ca3176e7SBrian Feldman 
1003ca3176e7SBrian Feldman 	/* remove hostkey from the child's memory */
1004ca3176e7SBrian Feldman 	destroy_sensitive_data();
1005a04a10f8SKris Kennaway 
100695e2a710SKris Kennaway 	/* login(1) is only called if we execute the login shell */
100795e2a710SKris Kennaway 	if (options.use_login && command != NULL)
100895e2a710SKris Kennaway 		options.use_login = 0;
100995e2a710SKris Kennaway 
101009958426SBrian Feldman #ifndef USE_PAM
1011c2d3a559SKris Kennaway 	if (!options.use_login) {
1012c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
1013ca3176e7SBrian Feldman 	if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1014c2d3a559SKris Kennaway 		f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1015c2d3a559SKris Kennaway 		    _PATH_NOLOGIN), "r");
1016c2d3a559SKris Kennaway #else
1017c2d3a559SKris Kennaway 		if (pw->pw_uid)
1018c2d3a559SKris Kennaway 			f = fopen(_PATH_NOLOGIN, "r");
1019c2d3a559SKris Kennaway #endif
1020a04a10f8SKris Kennaway 		if (f) {
1021a04a10f8SKris Kennaway 			/* /etc/nologin exists.  Print its contents and exit. */
1022a04a10f8SKris Kennaway 			while (fgets(buf, sizeof(buf), f))
1023a04a10f8SKris Kennaway 				fputs(buf, stderr);
1024a04a10f8SKris Kennaway 			fclose(f);
1025a04a10f8SKris Kennaway 			exit(254);
1026a04a10f8SKris Kennaway 		}
1027c2d3a559SKris Kennaway 	}
102809958426SBrian Feldman #endif /* !USE_PAM */
1029c2d3a559SKris Kennaway 	/* Set login name, uid, gid, and groups. */
1030a04a10f8SKris Kennaway 	/* Login(1) does this as well, and it needs uid 0 for the "-h"
1031a04a10f8SKris Kennaway 	   switch, so we let login(1) to this for us. */
1032a04a10f8SKris Kennaway 	if (!options.use_login) {
1033c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
1034e8aafc91SKris Kennaway 		char **tmpenv;
1035e8aafc91SKris Kennaway 
1036e8aafc91SKris Kennaway 		/* Initialize temp environment */
1037e8aafc91SKris Kennaway 		envsize = 64;
1038e8aafc91SKris Kennaway 		env = xmalloc(envsize * sizeof(char *));
1039e8aafc91SKris Kennaway 		env[0] = NULL;
1040e8aafc91SKris Kennaway 
1041e8aafc91SKris Kennaway 		child_set_env(&env, &envsize, "PATH",
1042e8aafc91SKris Kennaway 			      (pw->pw_uid == 0) ?
1043e8aafc91SKris Kennaway 			      _PATH_STDPATH : _PATH_DEFPATH);
1044e8aafc91SKris Kennaway 
1045e8aafc91SKris Kennaway 		snprintf(buf, sizeof buf, "%.200s/%.50s",
1046e8aafc91SKris Kennaway 			 _PATH_MAILDIR, pw->pw_name);
1047e8aafc91SKris Kennaway 		child_set_env(&env, &envsize, "MAIL", buf);
1048e8aafc91SKris Kennaway 
1049e8aafc91SKris Kennaway 		if (getenv("TZ"))
1050e8aafc91SKris Kennaway 			child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1051e8aafc91SKris Kennaway 
1052e8aafc91SKris Kennaway 		/* Save parent environment */
1053e8aafc91SKris Kennaway 		tmpenv = environ;
1054e8aafc91SKris Kennaway 		environ = env;
1055e8aafc91SKris Kennaway 
1056e8aafc91SKris Kennaway 		if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETALL) < 0)
1057e8aafc91SKris Kennaway 			fatal("setusercontext failed: %s", strerror(errno));
1058e8aafc91SKris Kennaway 
1059e8aafc91SKris Kennaway 		/* Restore parent environment */
1060e8aafc91SKris Kennaway 		env = environ;
1061e8aafc91SKris Kennaway 		environ = tmpenv;
1062e8aafc91SKris Kennaway 
1063e8aafc91SKris Kennaway 		for (envsize = 0; env[envsize] != NULL; ++envsize)
1064e8aafc91SKris Kennaway 			;
1065e8aafc91SKris Kennaway 		envsize = (envsize < 100) ? 100 : envsize + 16;
1066e8aafc91SKris Kennaway 		env = xrealloc(env, envsize * sizeof(char *));
1067e8aafc91SKris Kennaway 
1068c2d3a559SKris Kennaway #endif /* !HAVE_LOGIN_CAP */
1069a04a10f8SKris Kennaway 		if (getuid() == 0 || geteuid() == 0) {
1070c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
1071c2d3a559SKris Kennaway 			if (setusercontext(lc, pw, pw->pw_uid,
1072c2d3a559SKris Kennaway 			    (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1073c2d3a559SKris Kennaway 				perror("unable to set user context");
1074c2d3a559SKris Kennaway 				exit(1);
1075c2d3a559SKris Kennaway 			}
1076c2d3a559SKris Kennaway #else
1077c2d3a559SKris Kennaway 			if (setlogin(pw->pw_name) < 0)
1078c2d3a559SKris Kennaway 				error("setlogin failed: %s", strerror(errno));
1079a04a10f8SKris Kennaway 			if (setgid(pw->pw_gid) < 0) {
1080a04a10f8SKris Kennaway 				perror("setgid");
1081a04a10f8SKris Kennaway 				exit(1);
1082a04a10f8SKris Kennaway 			}
1083a04a10f8SKris Kennaway 			/* Initialize the group list. */
1084a04a10f8SKris Kennaway 			if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1085a04a10f8SKris Kennaway 				perror("initgroups");
1086a04a10f8SKris Kennaway 				exit(1);
1087a04a10f8SKris Kennaway 			}
1088a04a10f8SKris Kennaway 			endgrent();
1089a04a10f8SKris Kennaway 
1090a04a10f8SKris Kennaway 			/* Permanently switch to the desired uid. */
1091ca3176e7SBrian Feldman 			permanently_set_uid(pw);
1092c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
1093a04a10f8SKris Kennaway 		}
1094a04a10f8SKris Kennaway 		if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1095c2d3a559SKris Kennaway 			fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1096a04a10f8SKris Kennaway 	}
1097a04a10f8SKris Kennaway 	/*
1098a04a10f8SKris Kennaway 	 * Get the shell from the password data.  An empty shell field is
1099a04a10f8SKris Kennaway 	 * legal, and means /bin/sh.
1100a04a10f8SKris Kennaway 	 */
1101a04a10f8SKris Kennaway 	shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1102c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
1103c2d3a559SKris Kennaway 	shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1104c2d3a559SKris Kennaway #endif
1105a04a10f8SKris Kennaway 
1106a04a10f8SKris Kennaway #ifdef AFS
1107a04a10f8SKris Kennaway 	/* Try to get AFS tokens for the local cell. */
1108a04a10f8SKris Kennaway 	if (k_hasafs()) {
1109a04a10f8SKris Kennaway 		char cell[64];
1110a04a10f8SKris Kennaway 
1111a04a10f8SKris Kennaway 		if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1112a04a10f8SKris Kennaway 			krb_afslog(cell, 0);
1113a04a10f8SKris Kennaway 
1114a04a10f8SKris Kennaway 		krb_afslog(0, 0);
1115a04a10f8SKris Kennaway 	}
1116a04a10f8SKris Kennaway #endif /* AFS */
1117a04a10f8SKris Kennaway 
1118a04a10f8SKris Kennaway 	/* Initialize the environment. */
1119e8aafc91SKris Kennaway 	if (env == NULL) {
1120a04a10f8SKris Kennaway 		envsize = 100;
1121a04a10f8SKris Kennaway 		env = xmalloc(envsize * sizeof(char *));
1122a04a10f8SKris Kennaway 		env[0] = NULL;
1123e8aafc91SKris Kennaway 	}
1124a04a10f8SKris Kennaway 
1125a04a10f8SKris Kennaway 	if (!options.use_login) {
1126a04a10f8SKris Kennaway 		/* Set basic environment. */
1127a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "USER", pw->pw_name);
1128a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1129a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1130c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
1131c2d3a559SKris Kennaway 		(void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1132c2d3a559SKris Kennaway 		child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1133c2d3a559SKris Kennaway #else
1134a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1135c2d3a559SKris Kennaway #endif
1136a04a10f8SKris Kennaway 
1137a04a10f8SKris Kennaway 		snprintf(buf, sizeof buf, "%.200s/%.50s",
1138a04a10f8SKris Kennaway 			 _PATH_MAILDIR, pw->pw_name);
1139a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "MAIL", buf);
1140a04a10f8SKris Kennaway 
1141a04a10f8SKris Kennaway 		/* Normal systems set SHELL by default. */
1142a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "SHELL", shell);
1143a04a10f8SKris Kennaway 	}
1144a04a10f8SKris Kennaway 	if (getenv("TZ"))
1145a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1146a04a10f8SKris Kennaway 
1147a04a10f8SKris Kennaway 	/* Set custom environment options from RSA authentication. */
1148a04a10f8SKris Kennaway 	while (custom_environment) {
1149a04a10f8SKris Kennaway 		struct envstring *ce = custom_environment;
1150a04a10f8SKris Kennaway 		char *s = ce->s;
1151a04a10f8SKris Kennaway 		int i;
1152a04a10f8SKris Kennaway 		for (i = 0; s[i] != '=' && s[i]; i++);
1153a04a10f8SKris Kennaway 		if (s[i] == '=') {
1154a04a10f8SKris Kennaway 			s[i] = 0;
1155a04a10f8SKris Kennaway 			child_set_env(&env, &envsize, s, s + i + 1);
1156a04a10f8SKris Kennaway 		}
1157a04a10f8SKris Kennaway 		custom_environment = ce->next;
1158a04a10f8SKris Kennaway 		xfree(ce->s);
1159a04a10f8SKris Kennaway 		xfree(ce);
1160a04a10f8SKris Kennaway 	}
1161a04a10f8SKris Kennaway 
1162a04a10f8SKris Kennaway 	snprintf(buf, sizeof buf, "%.50s %d %d",
1163a04a10f8SKris Kennaway 		 get_remote_ipaddr(), get_remote_port(), get_local_port());
1164a04a10f8SKris Kennaway 	child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1165a04a10f8SKris Kennaway 
1166ca3176e7SBrian Feldman 	if (s->ttyfd != -1)
1167ca3176e7SBrian Feldman 		child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1168ca3176e7SBrian Feldman 	if (s->term)
1169ca3176e7SBrian Feldman 		child_set_env(&env, &envsize, "TERM", s->term);
1170ca3176e7SBrian Feldman 	if (s->display)
1171ca3176e7SBrian Feldman 		child_set_env(&env, &envsize, "DISPLAY", s->display);
1172c2d3a559SKris Kennaway 	if (original_command)
1173c2d3a559SKris Kennaway 		child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1174c2d3a559SKris Kennaway 		    original_command);
1175a04a10f8SKris Kennaway 
1176a04a10f8SKris Kennaway #ifdef KRB4
1177a04a10f8SKris Kennaway 	{
1178a04a10f8SKris Kennaway 		extern char *ticket;
1179a04a10f8SKris Kennaway 
1180a04a10f8SKris Kennaway 		if (ticket)
1181a04a10f8SKris Kennaway 			child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1182a04a10f8SKris Kennaway 	}
1183a04a10f8SKris Kennaway #endif /* KRB4 */
1184e8aafc91SKris Kennaway #ifdef KRB5
1185e8aafc91SKris Kennaway {
1186e8aafc91SKris Kennaway 	  extern krb5_ccache mem_ccache;
1187e8aafc91SKris Kennaway 
1188e8aafc91SKris Kennaway 	   if (mem_ccache) {
1189e8aafc91SKris Kennaway 	     krb5_error_code problem;
1190e8aafc91SKris Kennaway 	      krb5_ccache ccache;
1191e8aafc91SKris Kennaway #ifdef AFS
1192e8aafc91SKris Kennaway 	      if (k_hasafs())
1193e8aafc91SKris Kennaway 		krb5_afslog(ssh_context, mem_ccache, NULL, NULL);
1194e8aafc91SKris Kennaway #endif /* AFS */
1195e8aafc91SKris Kennaway 
1196e8aafc91SKris Kennaway 	      problem = krb5_cc_default(ssh_context, &ccache);
1197e8aafc91SKris Kennaway 	      if (problem) {}
1198e8aafc91SKris Kennaway 	      else {
1199e8aafc91SKris Kennaway 		problem = krb5_cc_copy_cache(ssh_context, mem_ccache, ccache);
1200e8aafc91SKris Kennaway 		 if (problem) {}
1201e8aafc91SKris Kennaway 	      }
1202e8aafc91SKris Kennaway 
1203e8aafc91SKris Kennaway 	      krb5_cc_close(ssh_context, ccache);
1204e8aafc91SKris Kennaway 	   }
1205e8aafc91SKris Kennaway 
1206e8aafc91SKris Kennaway 	   krb5_cleanup_proc(NULL);
1207e8aafc91SKris Kennaway 	}
1208e8aafc91SKris Kennaway #endif /* KRB5 */
1209a04a10f8SKris Kennaway 
121009958426SBrian Feldman #ifdef USE_PAM
121109958426SBrian Feldman 	/* Pull in any environment variables that may have been set by PAM. */
121209958426SBrian Feldman 	do_pam_environment(&env, &envsize);
121309958426SBrian Feldman #endif /* USE_PAM */
121409958426SBrian Feldman 
1215a04a10f8SKris Kennaway 	if (xauthfile)
1216a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1217a04a10f8SKris Kennaway 	if (auth_get_socket_name() != NULL)
1218a04a10f8SKris Kennaway 		child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1219a04a10f8SKris Kennaway 			      auth_get_socket_name());
1220a04a10f8SKris Kennaway 
1221a04a10f8SKris Kennaway 	/* read $HOME/.ssh/environment. */
1222a04a10f8SKris Kennaway 	if (!options.use_login) {
1223db1cb46cSKris Kennaway 		snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1224db1cb46cSKris Kennaway 		    pw->pw_dir);
1225a04a10f8SKris Kennaway 		read_environment_file(&env, &envsize, buf);
1226a04a10f8SKris Kennaway 	}
1227a04a10f8SKris Kennaway 	if (debug_flag) {
1228a04a10f8SKris Kennaway 		/* dump the environment */
1229a04a10f8SKris Kennaway 		fprintf(stderr, "Environment:\n");
1230a04a10f8SKris Kennaway 		for (i = 0; env[i]; i++)
1231a04a10f8SKris Kennaway 			fprintf(stderr, "  %.200s\n", env[i]);
1232a04a10f8SKris Kennaway 	}
1233c2d3a559SKris Kennaway 	/* we have to stash the hostname before we close our socket. */
1234c2d3a559SKris Kennaway 	if (options.use_login)
1235ca3176e7SBrian Feldman 		hostname = get_remote_name_or_ip(utmp_len,
1236ca3176e7SBrian Feldman 		    options.reverse_mapping_check);
1237a04a10f8SKris Kennaway 	/*
1238a04a10f8SKris Kennaway 	 * Close the connection descriptors; note that this is the child, and
1239a04a10f8SKris Kennaway 	 * the server will still have the socket open, and it is important
1240a04a10f8SKris Kennaway 	 * that we do not shutdown it.  Note that the descriptors cannot be
1241a04a10f8SKris Kennaway 	 * closed before building the environment, as we call
1242a04a10f8SKris Kennaway 	 * get_remote_ipaddr there.
1243a04a10f8SKris Kennaway 	 */
1244a04a10f8SKris Kennaway 	if (packet_get_connection_in() == packet_get_connection_out())
1245a04a10f8SKris Kennaway 		close(packet_get_connection_in());
1246a04a10f8SKris Kennaway 	else {
1247a04a10f8SKris Kennaway 		close(packet_get_connection_in());
1248a04a10f8SKris Kennaway 		close(packet_get_connection_out());
1249a04a10f8SKris Kennaway 	}
1250a04a10f8SKris Kennaway 	/*
1251a04a10f8SKris Kennaway 	 * Close all descriptors related to channels.  They will still remain
1252a04a10f8SKris Kennaway 	 * open in the parent.
1253a04a10f8SKris Kennaway 	 */
1254a04a10f8SKris Kennaway 	/* XXX better use close-on-exec? -markus */
1255a04a10f8SKris Kennaway 	channel_close_all();
1256a04a10f8SKris Kennaway 
1257a04a10f8SKris Kennaway 	/*
1258a04a10f8SKris Kennaway 	 * Close any extra file descriptors.  Note that there may still be
1259a04a10f8SKris Kennaway 	 * descriptors left by system functions.  They will be closed later.
1260a04a10f8SKris Kennaway 	 */
1261a04a10f8SKris Kennaway 	endpwent();
1262a04a10f8SKris Kennaway 
1263a04a10f8SKris Kennaway 	/*
126446c9472cSBrian Feldman 	 * Restore any signal handlers set by sshd previously that should
126546c9472cSBrian Feldman 	 * be restored to their initial state.
126646c9472cSBrian Feldman 	 */
126746c9472cSBrian Feldman 	signal(SIGPIPE, SIG_DFL);
126846c9472cSBrian Feldman 
1269a04a10f8SKris Kennaway 	/* Change current directory to the user\'s home directory. */
1270e8aafc91SKris Kennaway 	if (
1271e8aafc91SKris Kennaway #ifdef __FreeBSD__
1272e8aafc91SKris Kennaway 		!*pw->pw_dir ||
1273e8aafc91SKris Kennaway #endif /* __FreeBSD__ */
1274e8aafc91SKris Kennaway 		chdir(pw->pw_dir) < 0
1275e8aafc91SKris Kennaway 	   ) {
1276c2d3a559SKris Kennaway #ifdef HAVE_LOGIN_CAP
1277e8aafc91SKris Kennaway 		if (login_getcapbool(lc, "requirehome", 0)) {
1278e8aafc91SKris Kennaway 			(void)printf("Home directory not available\n");
1279e8aafc91SKris Kennaway 			log("LOGIN %.200s REFUSED (HOMEDIR) ON TTY %.200s",
1280e8aafc91SKris Kennaway 				pw->pw_name, ttyname);
1281e8aafc91SKris Kennaway 			exit(254);
1282e8aafc91SKris Kennaway 		}
1283c2d3a559SKris Kennaway #endif /* HAVE_LOGIN_CAP */
1284e8aafc91SKris Kennaway #ifdef __FreeBSD__
1285e8aafc91SKris Kennaway 		if (chdir("/") < 0) {
1286e8aafc91SKris Kennaway 			(void)printf("Cannot find root directory\n");
1287e8aafc91SKris Kennaway 			log("LOGIN %.200s REFUSED (ROOTDIR) ON TTY %.200s",
1288e8aafc91SKris Kennaway 				pw->pw_name, ttyname);
1289e8aafc91SKris Kennaway 			exit(254);
1290e8aafc91SKris Kennaway 		}
1291ca3176e7SBrian Feldman 		if (!check_quietlogin(s, command) || *pw->pw_dir)
1292e8aafc91SKris Kennaway 			(void)printf(
1293e8aafc91SKris Kennaway 		       "No home directory.\nLogging in with home = \"/\".\n");
1294e8aafc91SKris Kennaway 
1295e8aafc91SKris Kennaway #else /* !__FreeBSD__ */
1296e8aafc91SKris Kennaway 
1297a04a10f8SKris Kennaway 		fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1298a04a10f8SKris Kennaway 			pw->pw_dir, strerror(errno));
1299e8aafc91SKris Kennaway #endif /* __FreeBSD__ */
1300e8aafc91SKris Kennaway 	}
1301ca3176e7SBrian Feldman 
1302ca3176e7SBrian Feldman 	/*
1303ca3176e7SBrian Feldman 	 * Close any extra open file descriptors so that we don\'t have them
1304ca3176e7SBrian Feldman 	 * hanging around in clients.  Note that we want to do this after
1305ca3176e7SBrian Feldman 	 * initgroups, because at least on Solaris 2.3 it leaves file
1306ca3176e7SBrian Feldman 	 * descriptors open.
1307ca3176e7SBrian Feldman 	 */
1308ca3176e7SBrian Feldman 	for (i = 3; i < getdtablesize(); i++)
1309ca3176e7SBrian Feldman 		close(i);
1310a04a10f8SKris Kennaway 
1311a04a10f8SKris Kennaway 	/*
1312a04a10f8SKris Kennaway 	 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1313a04a10f8SKris Kennaway 	 * xauth are run in the proper environment.
1314a04a10f8SKris Kennaway 	 */
1315a04a10f8SKris Kennaway 	environ = env;
1316a04a10f8SKris Kennaway 
1317a04a10f8SKris Kennaway 	/*
1318a04a10f8SKris Kennaway 	 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1319a04a10f8SKris Kennaway 	 * in this order).
1320a04a10f8SKris Kennaway 	 */
1321a04a10f8SKris Kennaway 	if (!options.use_login) {
1322ca3176e7SBrian Feldman 		/* ignore _PATH_SSH_USER_RC for subsystems */
1323ca3176e7SBrian Feldman 		if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1324a04a10f8SKris Kennaway 			if (debug_flag)
1325ca3176e7SBrian Feldman 				fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1326ca3176e7SBrian Feldman 				    _PATH_SSH_USER_RC);
1327ca3176e7SBrian Feldman 			f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
1328a04a10f8SKris Kennaway 			if (f) {
1329ca3176e7SBrian Feldman 				if (do_xauth)
1330ca3176e7SBrian Feldman 					fprintf(f, "%s %s\n", s->auth_proto,
1331ca3176e7SBrian Feldman 					    s->auth_data);
1332a04a10f8SKris Kennaway 				pclose(f);
1333a04a10f8SKris Kennaway 			} else
1334ca3176e7SBrian Feldman 				fprintf(stderr, "Could not run %s\n",
1335ca3176e7SBrian Feldman 				    _PATH_SSH_USER_RC);
1336ca3176e7SBrian Feldman 		} else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1337a04a10f8SKris Kennaway 			if (debug_flag)
1338ca3176e7SBrian Feldman 				fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1339ca3176e7SBrian Feldman 				    _PATH_SSH_SYSTEM_RC);
1340ca3176e7SBrian Feldman 			f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1341a04a10f8SKris Kennaway 			if (f) {
1342ca3176e7SBrian Feldman 				if (do_xauth)
1343ca3176e7SBrian Feldman 					fprintf(f, "%s %s\n", s->auth_proto,
1344ca3176e7SBrian Feldman 					    s->auth_data);
1345a04a10f8SKris Kennaway 				pclose(f);
1346a04a10f8SKris Kennaway 			} else
1347ca3176e7SBrian Feldman 				fprintf(stderr, "Could not run %s\n",
1348ca3176e7SBrian Feldman 				    _PATH_SSH_SYSTEM_RC);
1349ca3176e7SBrian Feldman 		} else if (do_xauth && options.xauth_location != NULL) {
1350a04a10f8SKris Kennaway 			/* Add authority data to .Xauthority if appropriate. */
1351ca3176e7SBrian Feldman 			char *screen = strchr(s->display, ':');
1352ca3176e7SBrian Feldman 
1353db1cb46cSKris Kennaway 			if (debug_flag) {
1354db1cb46cSKris Kennaway 				fprintf(stderr,
1355ca3176e7SBrian Feldman 				    "Running %.100s add "
1356ca3176e7SBrian Feldman 				    "%.100s %.100s %.100s\n",
1357ca3176e7SBrian Feldman 				    options.xauth_location, s->display,
1358ca3176e7SBrian Feldman 				    s->auth_proto, s->auth_data);
1359db1cb46cSKris Kennaway 				if (screen != NULL)
1360db1cb46cSKris Kennaway 					fprintf(stderr,
1361db1cb46cSKris Kennaway 					    "Adding %.*s/unix%s %s %s\n",
1362ca3176e7SBrian Feldman 					    (int)(screen - s->display),
1363ca3176e7SBrian Feldman 					    s->display, screen,
1364ca3176e7SBrian Feldman 					    s->auth_proto, s->auth_data);
1365db1cb46cSKris Kennaway 			}
1366c2d3a559SKris Kennaway 			snprintf(cmd, sizeof cmd, "%s -q -",
1367c2d3a559SKris Kennaway 			    options.xauth_location);
1368c2d3a559SKris Kennaway 			f = popen(cmd, "w");
1369a04a10f8SKris Kennaway 			if (f) {
1370ca3176e7SBrian Feldman 				fprintf(f, "add %s %s %s\n", s->display,
1371ca3176e7SBrian Feldman 				    s->auth_proto, s->auth_data);
1372db1cb46cSKris Kennaway 				if (screen != NULL)
1373db1cb46cSKris Kennaway 					fprintf(f, "add %.*s/unix%s %s %s\n",
1374ca3176e7SBrian Feldman 					    (int)(screen - s->display),
1375ca3176e7SBrian Feldman 					    s->display, screen,
1376ca3176e7SBrian Feldman 					    s->auth_proto,
1377ca3176e7SBrian Feldman 					    s->auth_data);
1378a04a10f8SKris Kennaway 				pclose(f);
1379c2d3a559SKris Kennaway 			} else {
1380c2d3a559SKris Kennaway 				fprintf(stderr, "Could not run %s\n",
1381c2d3a559SKris Kennaway 				    cmd);
1382a04a10f8SKris Kennaway 			}
1383a04a10f8SKris Kennaway 		}
1384a04a10f8SKris Kennaway 		/* Get the last component of the shell name. */
1385a04a10f8SKris Kennaway 		cp = strrchr(shell, '/');
1386a04a10f8SKris Kennaway 		if (cp)
1387a04a10f8SKris Kennaway 			cp++;
1388a04a10f8SKris Kennaway 		else
1389a04a10f8SKris Kennaway 			cp = shell;
1390a04a10f8SKris Kennaway 	}
1391ca3176e7SBrian Feldman 
1392ca3176e7SBrian Feldman 	/* restore SIGPIPE for child */
1393ca3176e7SBrian Feldman 	signal(SIGPIPE,  SIG_DFL);
1394ca3176e7SBrian Feldman 
1395a04a10f8SKris Kennaway 	/*
1396a04a10f8SKris Kennaway 	 * If we have no command, execute the shell.  In this case, the shell
1397a04a10f8SKris Kennaway 	 * name to be passed in argv[0] is preceded by '-' to indicate that
1398a04a10f8SKris Kennaway 	 * this is a login shell.
1399a04a10f8SKris Kennaway 	 */
1400a04a10f8SKris Kennaway 	if (!command) {
1401a04a10f8SKris Kennaway 		if (!options.use_login) {
1402a04a10f8SKris Kennaway 			char buf[256];
1403a04a10f8SKris Kennaway 
1404a04a10f8SKris Kennaway 			/*
1405a04a10f8SKris Kennaway 			 * Check for mail if we have a tty and it was enabled
1406a04a10f8SKris Kennaway 			 * in server options.
1407a04a10f8SKris Kennaway 			 */
1408ca3176e7SBrian Feldman 			if (s->ttyfd != -1 && options.check_mail) {
1409a04a10f8SKris Kennaway 				char *mailbox;
1410a04a10f8SKris Kennaway 				struct stat mailstat;
1411ca3176e7SBrian Feldman 
1412a04a10f8SKris Kennaway 				mailbox = getenv("MAIL");
1413a04a10f8SKris Kennaway 				if (mailbox != NULL) {
1414db1cb46cSKris Kennaway 					if (stat(mailbox, &mailstat) != 0 ||
1415db1cb46cSKris Kennaway 					    mailstat.st_size == 0)
1416e8aafc91SKris Kennaway #ifdef __FreeBSD__
1417e8aafc91SKris Kennaway 						;
1418e8aafc91SKris Kennaway #else /* !__FreeBSD__ */
1419a04a10f8SKris Kennaway 						printf("No mail.\n");
1420e8aafc91SKris Kennaway #endif /* __FreeBSD__ */
1421a04a10f8SKris Kennaway 					else if (mailstat.st_mtime < mailstat.st_atime)
1422a04a10f8SKris Kennaway 						printf("You have mail.\n");
1423a04a10f8SKris Kennaway 					else
1424a04a10f8SKris Kennaway 						printf("You have new mail.\n");
1425a04a10f8SKris Kennaway 				}
1426a04a10f8SKris Kennaway 			}
1427a04a10f8SKris Kennaway 			/* Start the shell.  Set initial character to '-'. */
1428a04a10f8SKris Kennaway 			buf[0] = '-';
1429a04a10f8SKris Kennaway 			strncpy(buf + 1, cp, sizeof(buf) - 1);
1430a04a10f8SKris Kennaway 			buf[sizeof(buf) - 1] = 0;
1431a04a10f8SKris Kennaway 
1432a04a10f8SKris Kennaway 			/* Execute the shell. */
1433a04a10f8SKris Kennaway 			argv[0] = buf;
1434a04a10f8SKris Kennaway 			argv[1] = NULL;
1435a04a10f8SKris Kennaway 			execve(shell, argv, env);
1436a04a10f8SKris Kennaway 
1437a04a10f8SKris Kennaway 			/* Executing the shell failed. */
1438a04a10f8SKris Kennaway 			perror(shell);
1439a04a10f8SKris Kennaway 			exit(1);
1440a04a10f8SKris Kennaway 
1441a04a10f8SKris Kennaway 		} else {
1442a04a10f8SKris Kennaway 			/* Launch login(1). */
1443a04a10f8SKris Kennaway 
1444c2d3a559SKris Kennaway 			execl("/usr/bin/login", "login", "-h", hostname,
1445a04a10f8SKris Kennaway 			     "-p", "-f", "--", pw->pw_name, NULL);
1446a04a10f8SKris Kennaway 
1447a04a10f8SKris Kennaway 			/* Login couldn't be executed, die. */
1448a04a10f8SKris Kennaway 
1449a04a10f8SKris Kennaway 			perror("login");
1450a04a10f8SKris Kennaway 			exit(1);
1451a04a10f8SKris Kennaway 		}
1452a04a10f8SKris Kennaway 	}
1453a04a10f8SKris Kennaway 	/*
1454a04a10f8SKris Kennaway 	 * Execute the command using the user's shell.  This uses the -c
1455a04a10f8SKris Kennaway 	 * option to execute the command.
1456a04a10f8SKris Kennaway 	 */
1457a04a10f8SKris Kennaway 	argv[0] = (char *) cp;
1458a04a10f8SKris Kennaway 	argv[1] = "-c";
1459a04a10f8SKris Kennaway 	argv[2] = (char *) command;
1460a04a10f8SKris Kennaway 	argv[3] = NULL;
1461a04a10f8SKris Kennaway 	execve(shell, argv, env);
1462a04a10f8SKris Kennaway 	perror(shell);
1463a04a10f8SKris Kennaway 	exit(1);
1464a04a10f8SKris Kennaway }
1465a04a10f8SKris Kennaway 
1466a04a10f8SKris Kennaway Session *
1467a04a10f8SKris Kennaway session_new(void)
1468a04a10f8SKris Kennaway {
1469a04a10f8SKris Kennaway 	int i;
1470a04a10f8SKris Kennaway 	static int did_init = 0;
1471a04a10f8SKris Kennaway 	if (!did_init) {
1472a04a10f8SKris Kennaway 		debug("session_new: init");
1473a04a10f8SKris Kennaway 		for(i = 0; i < MAX_SESSIONS; i++) {
1474a04a10f8SKris Kennaway 			sessions[i].used = 0;
1475a04a10f8SKris Kennaway 			sessions[i].self = i;
1476a04a10f8SKris Kennaway 		}
1477a04a10f8SKris Kennaway 		did_init = 1;
1478a04a10f8SKris Kennaway 	}
1479a04a10f8SKris Kennaway 	for(i = 0; i < MAX_SESSIONS; i++) {
1480a04a10f8SKris Kennaway 		Session *s = &sessions[i];
1481a04a10f8SKris Kennaway 		if (! s->used) {
1482ca3176e7SBrian Feldman 			memset(s, 0, sizeof(*s));
1483a04a10f8SKris Kennaway 			s->chanid = -1;
1484a04a10f8SKris Kennaway 			s->ptyfd = -1;
1485a04a10f8SKris Kennaway 			s->ttyfd = -1;
1486a04a10f8SKris Kennaway 			s->used = 1;
1487a04a10f8SKris Kennaway 			debug("session_new: session %d", i);
1488a04a10f8SKris Kennaway 			return s;
1489a04a10f8SKris Kennaway 		}
1490a04a10f8SKris Kennaway 	}
1491a04a10f8SKris Kennaway 	return NULL;
1492a04a10f8SKris Kennaway }
1493a04a10f8SKris Kennaway 
1494a04a10f8SKris Kennaway void
1495a04a10f8SKris Kennaway session_dump(void)
1496a04a10f8SKris Kennaway {
1497a04a10f8SKris Kennaway 	int i;
1498a04a10f8SKris Kennaway 	for(i = 0; i < MAX_SESSIONS; i++) {
1499a04a10f8SKris Kennaway 		Session *s = &sessions[i];
1500a04a10f8SKris Kennaway 		debug("dump: used %d session %d %p channel %d pid %d",
1501a04a10f8SKris Kennaway 		    s->used,
1502a04a10f8SKris Kennaway 		    s->self,
1503a04a10f8SKris Kennaway 		    s,
1504a04a10f8SKris Kennaway 		    s->chanid,
1505a04a10f8SKris Kennaway 		    s->pid);
1506a04a10f8SKris Kennaway 	}
1507a04a10f8SKris Kennaway }
1508a04a10f8SKris Kennaway 
1509a04a10f8SKris Kennaway int
1510a04a10f8SKris Kennaway session_open(int chanid)
1511a04a10f8SKris Kennaway {
1512a04a10f8SKris Kennaway 	Session *s = session_new();
1513a04a10f8SKris Kennaway 	debug("session_open: channel %d", chanid);
1514a04a10f8SKris Kennaway 	if (s == NULL) {
1515a04a10f8SKris Kennaway 		error("no more sessions");
1516a04a10f8SKris Kennaway 		return 0;
1517a04a10f8SKris Kennaway 	}
1518a04a10f8SKris Kennaway 	s->pw = auth_get_user();
1519a04a10f8SKris Kennaway 	if (s->pw == NULL)
1520ca3176e7SBrian Feldman 		fatal("no user for session %d", s->self);
1521a04a10f8SKris Kennaway 	debug("session_open: session %d: link with channel %d", s->self, chanid);
1522a04a10f8SKris Kennaway 	s->chanid = chanid;
1523a04a10f8SKris Kennaway 	return 1;
1524a04a10f8SKris Kennaway }
1525a04a10f8SKris Kennaway 
1526a04a10f8SKris Kennaway Session *
1527a04a10f8SKris Kennaway session_by_channel(int id)
1528a04a10f8SKris Kennaway {
1529a04a10f8SKris Kennaway 	int i;
1530a04a10f8SKris Kennaway 	for(i = 0; i < MAX_SESSIONS; i++) {
1531a04a10f8SKris Kennaway 		Session *s = &sessions[i];
1532a04a10f8SKris Kennaway 		if (s->used && s->chanid == id) {
1533a04a10f8SKris Kennaway 			debug("session_by_channel: session %d channel %d", i, id);
1534a04a10f8SKris Kennaway 			return s;
1535a04a10f8SKris Kennaway 		}
1536a04a10f8SKris Kennaway 	}
1537a04a10f8SKris Kennaway 	debug("session_by_channel: unknown channel %d", id);
1538a04a10f8SKris Kennaway 	session_dump();
1539a04a10f8SKris Kennaway 	return NULL;
1540a04a10f8SKris Kennaway }
1541a04a10f8SKris Kennaway 
1542a04a10f8SKris Kennaway Session *
1543a04a10f8SKris Kennaway session_by_pid(pid_t pid)
1544a04a10f8SKris Kennaway {
1545a04a10f8SKris Kennaway 	int i;
1546a04a10f8SKris Kennaway 	debug("session_by_pid: pid %d", pid);
1547a04a10f8SKris Kennaway 	for(i = 0; i < MAX_SESSIONS; i++) {
1548a04a10f8SKris Kennaway 		Session *s = &sessions[i];
1549a04a10f8SKris Kennaway 		if (s->used && s->pid == pid)
1550a04a10f8SKris Kennaway 			return s;
1551a04a10f8SKris Kennaway 	}
1552a04a10f8SKris Kennaway 	error("session_by_pid: unknown pid %d", pid);
1553a04a10f8SKris Kennaway 	session_dump();
1554a04a10f8SKris Kennaway 	return NULL;
1555a04a10f8SKris Kennaway }
1556a04a10f8SKris Kennaway 
1557a04a10f8SKris Kennaway int
1558a04a10f8SKris Kennaway session_window_change_req(Session *s)
1559a04a10f8SKris Kennaway {
1560a04a10f8SKris Kennaway 	s->col = packet_get_int();
1561a04a10f8SKris Kennaway 	s->row = packet_get_int();
1562a04a10f8SKris Kennaway 	s->xpixel = packet_get_int();
1563a04a10f8SKris Kennaway 	s->ypixel = packet_get_int();
1564a04a10f8SKris Kennaway 	packet_done();
1565a04a10f8SKris Kennaway 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1566a04a10f8SKris Kennaway 	return 1;
1567a04a10f8SKris Kennaway }
1568a04a10f8SKris Kennaway 
1569a04a10f8SKris Kennaway int
1570a04a10f8SKris Kennaway session_pty_req(Session *s)
1571a04a10f8SKris Kennaway {
1572ca3176e7SBrian Feldman 	u_int len;
1573ca3176e7SBrian Feldman 	int n_bytes;
1574a04a10f8SKris Kennaway 
1575c2d3a559SKris Kennaway 	if (no_pty_flag)
1576c2d3a559SKris Kennaway 		return 0;
1577a04a10f8SKris Kennaway 	if (s->ttyfd != -1)
1578a04a10f8SKris Kennaway 		return 0;
1579a04a10f8SKris Kennaway 	s->term = packet_get_string(&len);
1580a04a10f8SKris Kennaway 	s->col = packet_get_int();
1581a04a10f8SKris Kennaway 	s->row = packet_get_int();
1582a04a10f8SKris Kennaway 	s->xpixel = packet_get_int();
1583a04a10f8SKris Kennaway 	s->ypixel = packet_get_int();
1584a04a10f8SKris Kennaway 
1585a04a10f8SKris Kennaway 	if (strcmp(s->term, "") == 0) {
1586a04a10f8SKris Kennaway 		xfree(s->term);
1587a04a10f8SKris Kennaway 		s->term = NULL;
1588a04a10f8SKris Kennaway 	}
1589a04a10f8SKris Kennaway 	/* Allocate a pty and open it. */
1590a04a10f8SKris Kennaway 	if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1591a04a10f8SKris Kennaway 		xfree(s->term);
1592a04a10f8SKris Kennaway 		s->term = NULL;
1593a04a10f8SKris Kennaway 		s->ptyfd = -1;
1594a04a10f8SKris Kennaway 		s->ttyfd = -1;
1595a04a10f8SKris Kennaway 		error("session_pty_req: session %d alloc failed", s->self);
1596a04a10f8SKris Kennaway 		return 0;
1597a04a10f8SKris Kennaway 	}
1598a04a10f8SKris Kennaway 	debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1599a04a10f8SKris Kennaway 	/*
1600a04a10f8SKris Kennaway 	 * Add a cleanup function to clear the utmp entry and record logout
1601a04a10f8SKris Kennaway 	 * time in case we call fatal() (e.g., the connection gets closed).
1602a04a10f8SKris Kennaway 	 */
1603a04a10f8SKris Kennaway 	fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1604a04a10f8SKris Kennaway 	pty_setowner(s->pw, s->tty);
1605a04a10f8SKris Kennaway 	/* Get window size from the packet. */
1606a04a10f8SKris Kennaway 	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1607a04a10f8SKris Kennaway 
1608ca3176e7SBrian Feldman 	/* Get tty modes from the packet. */
1609ca3176e7SBrian Feldman 	tty_parse_modes(s->ttyfd, &n_bytes);
1610ca3176e7SBrian Feldman 	packet_done();
1611ca3176e7SBrian Feldman 
1612a04a10f8SKris Kennaway 	session_proctitle(s);
1613a04a10f8SKris Kennaway 
1614a04a10f8SKris Kennaway 	return 1;
1615a04a10f8SKris Kennaway }
1616a04a10f8SKris Kennaway 
1617a04a10f8SKris Kennaway int
1618a04a10f8SKris Kennaway session_subsystem_req(Session *s)
1619a04a10f8SKris Kennaway {
1620ca3176e7SBrian Feldman 	u_int len;
1621a04a10f8SKris Kennaway 	int success = 0;
1622a04a10f8SKris Kennaway 	char *subsys = packet_get_string(&len);
1623c2d3a559SKris Kennaway 	int i;
1624a04a10f8SKris Kennaway 
1625a04a10f8SKris Kennaway 	packet_done();
1626a04a10f8SKris Kennaway 	log("subsystem request for %s", subsys);
1627a04a10f8SKris Kennaway 
1628c2d3a559SKris Kennaway 	for (i = 0; i < options.num_subsystems; i++) {
1629c2d3a559SKris Kennaway 		if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1630c2d3a559SKris Kennaway 			debug("subsystem: exec() %s", options.subsystem_command[i]);
1631ca3176e7SBrian Feldman 			s->is_subsystem = 1;
1632ca3176e7SBrian Feldman 			do_exec_no_pty(s, options.subsystem_command[i]);
1633c2d3a559SKris Kennaway 			success = 1;
1634c2d3a559SKris Kennaway 		}
1635c2d3a559SKris Kennaway 	}
1636c2d3a559SKris Kennaway 
1637c2d3a559SKris Kennaway 	if (!success)
1638c2d3a559SKris Kennaway 		log("subsystem request for %s failed, subsystem not found", subsys);
1639c2d3a559SKris Kennaway 
1640a04a10f8SKris Kennaway 	xfree(subsys);
1641a04a10f8SKris Kennaway 	return success;
1642a04a10f8SKris Kennaway }
1643a04a10f8SKris Kennaway 
1644a04a10f8SKris Kennaway int
1645a04a10f8SKris Kennaway session_x11_req(Session *s)
1646a04a10f8SKris Kennaway {
1647c2d3a559SKris Kennaway 	int fd;
1648c2d3a559SKris Kennaway 	if (no_x11_forwarding_flag) {
1649c2d3a559SKris Kennaway 		debug("X11 forwarding disabled in user configuration file.");
1650c2d3a559SKris Kennaway 		return 0;
1651c2d3a559SKris Kennaway 	}
1652a04a10f8SKris Kennaway 	if (!options.x11_forwarding) {
1653a04a10f8SKris Kennaway 		debug("X11 forwarding disabled in server configuration file.");
1654a04a10f8SKris Kennaway 		return 0;
1655a04a10f8SKris Kennaway 	}
1656a04a10f8SKris Kennaway 	if (xauthfile != NULL) {
1657a04a10f8SKris Kennaway 		debug("X11 fwd already started.");
1658a04a10f8SKris Kennaway 		return 0;
1659a04a10f8SKris Kennaway 	}
1660a04a10f8SKris Kennaway 
1661a04a10f8SKris Kennaway 	debug("Received request for X11 forwarding with auth spoofing.");
1662a04a10f8SKris Kennaway 	if (s->display != NULL)
1663a04a10f8SKris Kennaway 		packet_disconnect("Protocol error: X11 display already set.");
1664a04a10f8SKris Kennaway 
1665a04a10f8SKris Kennaway 	s->single_connection = packet_get_char();
1666a04a10f8SKris Kennaway 	s->auth_proto = packet_get_string(NULL);
1667a04a10f8SKris Kennaway 	s->auth_data = packet_get_string(NULL);
1668a04a10f8SKris Kennaway 	s->screen = packet_get_int();
1669a04a10f8SKris Kennaway 	packet_done();
1670a04a10f8SKris Kennaway 
1671a04a10f8SKris Kennaway 	s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1672a04a10f8SKris Kennaway 	if (s->display == NULL) {
1673a04a10f8SKris Kennaway 		xfree(s->auth_proto);
1674a04a10f8SKris Kennaway 		xfree(s->auth_data);
1675a04a10f8SKris Kennaway 		return 0;
1676a04a10f8SKris Kennaway 	}
1677a04a10f8SKris Kennaway 	xauthfile = xmalloc(MAXPATHLEN);
1678a04a10f8SKris Kennaway 	strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1679ca3176e7SBrian Feldman 	temporarily_use_uid(s->pw);
1680a04a10f8SKris Kennaway 	if (mkdtemp(xauthfile) == NULL) {
1681a04a10f8SKris Kennaway 		restore_uid();
1682a04a10f8SKris Kennaway 		error("private X11 dir: mkdtemp %s failed: %s",
1683a04a10f8SKris Kennaway 		    xauthfile, strerror(errno));
1684a04a10f8SKris Kennaway 		xfree(xauthfile);
1685a04a10f8SKris Kennaway 		xauthfile = NULL;
1686a04a10f8SKris Kennaway 		xfree(s->auth_proto);
1687a04a10f8SKris Kennaway 		xfree(s->auth_data);
1688a04a10f8SKris Kennaway 		/* XXXX remove listening channels */
1689a04a10f8SKris Kennaway 		return 0;
1690a04a10f8SKris Kennaway 	}
1691a04a10f8SKris Kennaway 	strlcat(xauthfile, "/cookies", MAXPATHLEN);
1692c2d3a559SKris Kennaway 	fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1693c2d3a559SKris Kennaway 	if (fd >= 0)
1694c2d3a559SKris Kennaway 		close(fd);
1695a04a10f8SKris Kennaway 	restore_uid();
1696a04a10f8SKris Kennaway 	fatal_add_cleanup(xauthfile_cleanup_proc, s);
1697a04a10f8SKris Kennaway 	return 1;
1698a04a10f8SKris Kennaway }
1699a04a10f8SKris Kennaway 
1700c2d3a559SKris Kennaway int
1701c2d3a559SKris Kennaway session_shell_req(Session *s)
1702c2d3a559SKris Kennaway {
1703c2d3a559SKris Kennaway 	/* if forced_command == NULL, the shell is execed */
1704c2d3a559SKris Kennaway 	char *shell = forced_command;
1705c2d3a559SKris Kennaway 	packet_done();
1706c2d3a559SKris Kennaway 	if (s->ttyfd == -1)
1707ca3176e7SBrian Feldman 		do_exec_no_pty(s, shell);
1708c2d3a559SKris Kennaway 	else
1709ca3176e7SBrian Feldman 		do_exec_pty(s, shell);
1710c2d3a559SKris Kennaway 	return 1;
1711c2d3a559SKris Kennaway }
1712c2d3a559SKris Kennaway 
1713c2d3a559SKris Kennaway int
1714c2d3a559SKris Kennaway session_exec_req(Session *s)
1715c2d3a559SKris Kennaway {
1716ca3176e7SBrian Feldman 	u_int len;
1717c2d3a559SKris Kennaway 	char *command = packet_get_string(&len);
1718c2d3a559SKris Kennaway 	packet_done();
1719c2d3a559SKris Kennaway 	if (forced_command) {
1720c2d3a559SKris Kennaway 		original_command = command;
1721c2d3a559SKris Kennaway 		command = forced_command;
1722c2d3a559SKris Kennaway 		debug("Forced command '%.500s'", forced_command);
1723c2d3a559SKris Kennaway 	}
1724c2d3a559SKris Kennaway 	if (s->ttyfd == -1)
1725ca3176e7SBrian Feldman 		do_exec_no_pty(s, command);
1726c2d3a559SKris Kennaway 	else
1727ca3176e7SBrian Feldman 		do_exec_pty(s, command);
1728c2d3a559SKris Kennaway 	if (forced_command == NULL)
1729c2d3a559SKris Kennaway 		xfree(command);
1730c2d3a559SKris Kennaway 	return 1;
1731c2d3a559SKris Kennaway }
1732c2d3a559SKris Kennaway 
1733ca3176e7SBrian Feldman int
1734ca3176e7SBrian Feldman session_auth_agent_req(Session *s)
1735ca3176e7SBrian Feldman {
1736ca3176e7SBrian Feldman 	static int called = 0;
1737ca3176e7SBrian Feldman 	packet_done();
1738ca3176e7SBrian Feldman 	if (no_agent_forwarding_flag) {
1739ca3176e7SBrian Feldman 		debug("session_auth_agent_req: no_agent_forwarding_flag");
1740ca3176e7SBrian Feldman 		return 0;
1741ca3176e7SBrian Feldman 	}
1742ca3176e7SBrian Feldman 	if (called) {
1743ca3176e7SBrian Feldman 		return 0;
1744ca3176e7SBrian Feldman 	} else {
1745ca3176e7SBrian Feldman 		called = 1;
1746ca3176e7SBrian Feldman 		return auth_input_request_forwarding(s->pw);
1747ca3176e7SBrian Feldman 	}
1748ca3176e7SBrian Feldman }
1749ca3176e7SBrian Feldman 
1750a04a10f8SKris Kennaway void
1751a04a10f8SKris Kennaway session_input_channel_req(int id, void *arg)
1752a04a10f8SKris Kennaway {
1753ca3176e7SBrian Feldman 	u_int len;
1754a04a10f8SKris Kennaway 	int reply;
1755a04a10f8SKris Kennaway 	int success = 0;
1756a04a10f8SKris Kennaway 	char *rtype;
1757a04a10f8SKris Kennaway 	Session *s;
1758a04a10f8SKris Kennaway 	Channel *c;
1759a04a10f8SKris Kennaway 
1760a04a10f8SKris Kennaway 	rtype = packet_get_string(&len);
1761a04a10f8SKris Kennaway 	reply = packet_get_char();
1762a04a10f8SKris Kennaway 
1763a04a10f8SKris Kennaway 	s = session_by_channel(id);
1764a04a10f8SKris Kennaway 	if (s == NULL)
1765a04a10f8SKris Kennaway 		fatal("session_input_channel_req: channel %d: no session", id);
1766a04a10f8SKris Kennaway 	c = channel_lookup(id);
1767a04a10f8SKris Kennaway 	if (c == NULL)
1768a04a10f8SKris Kennaway 		fatal("session_input_channel_req: channel %d: bad channel", id);
1769a04a10f8SKris Kennaway 
1770a04a10f8SKris Kennaway 	debug("session_input_channel_req: session %d channel %d request %s reply %d",
1771a04a10f8SKris Kennaway 	    s->self, id, rtype, reply);
1772a04a10f8SKris Kennaway 
1773a04a10f8SKris Kennaway 	/*
1774ca3176e7SBrian Feldman 	 * a session is in LARVAL state until a shell, a command
1775ca3176e7SBrian Feldman 	 * or a subsystem is executed
1776a04a10f8SKris Kennaway 	 */
1777a04a10f8SKris Kennaway 	if (c->type == SSH_CHANNEL_LARVAL) {
1778a04a10f8SKris Kennaway 		if (strcmp(rtype, "shell") == 0) {
1779c2d3a559SKris Kennaway 			success = session_shell_req(s);
1780a04a10f8SKris Kennaway 		} else if (strcmp(rtype, "exec") == 0) {
1781c2d3a559SKris Kennaway 			success = session_exec_req(s);
1782a04a10f8SKris Kennaway 		} else if (strcmp(rtype, "pty-req") == 0) {
1783a04a10f8SKris Kennaway 			success =  session_pty_req(s);
1784a04a10f8SKris Kennaway 		} else if (strcmp(rtype, "x11-req") == 0) {
1785a04a10f8SKris Kennaway 			success = session_x11_req(s);
1786ca3176e7SBrian Feldman 		} else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1787ca3176e7SBrian Feldman 			success = session_auth_agent_req(s);
1788a04a10f8SKris Kennaway 		} else if (strcmp(rtype, "subsystem") == 0) {
1789a04a10f8SKris Kennaway 			success = session_subsystem_req(s);
1790a04a10f8SKris Kennaway 		}
1791a04a10f8SKris Kennaway 	}
1792a04a10f8SKris Kennaway 	if (strcmp(rtype, "window-change") == 0) {
1793a04a10f8SKris Kennaway 		success = session_window_change_req(s);
1794a04a10f8SKris Kennaway 	}
1795a04a10f8SKris Kennaway 
1796a04a10f8SKris Kennaway 	if (reply) {
1797a04a10f8SKris Kennaway 		packet_start(success ?
1798a04a10f8SKris Kennaway 		    SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1799a04a10f8SKris Kennaway 		packet_put_int(c->remote_id);
1800a04a10f8SKris Kennaway 		packet_send();
1801a04a10f8SKris Kennaway 	}
1802a04a10f8SKris Kennaway 	xfree(rtype);
1803a04a10f8SKris Kennaway }
1804a04a10f8SKris Kennaway 
1805a04a10f8SKris Kennaway void
1806a04a10f8SKris Kennaway session_set_fds(Session *s, int fdin, int fdout, int fderr)
1807a04a10f8SKris Kennaway {
1808a04a10f8SKris Kennaway 	if (!compat20)
1809a04a10f8SKris Kennaway 		fatal("session_set_fds: called for proto != 2.0");
1810a04a10f8SKris Kennaway 	/*
1811a04a10f8SKris Kennaway 	 * now that have a child and a pipe to the child,
1812a04a10f8SKris Kennaway 	 * we can activate our channel and register the fd's
1813a04a10f8SKris Kennaway 	 */
1814a04a10f8SKris Kennaway 	if (s->chanid == -1)
1815a04a10f8SKris Kennaway 		fatal("no channel for session %d", s->self);
1816a04a10f8SKris Kennaway 	channel_set_fds(s->chanid,
1817a04a10f8SKris Kennaway 	    fdout, fdin, fderr,
181809958426SBrian Feldman 	    fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
181909958426SBrian Feldman 	    1);
1820a04a10f8SKris Kennaway }
1821a04a10f8SKris Kennaway 
1822a04a10f8SKris Kennaway void
1823a04a10f8SKris Kennaway session_pty_cleanup(Session *s)
1824a04a10f8SKris Kennaway {
1825a04a10f8SKris Kennaway 	if (s == NULL || s->ttyfd == -1)
1826a04a10f8SKris Kennaway 		return;
1827a04a10f8SKris Kennaway 
1828ca3176e7SBrian Feldman 	debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1829a04a10f8SKris Kennaway 
1830a04a10f8SKris Kennaway 	/* Cancel the cleanup function. */
1831a04a10f8SKris Kennaway 	fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1832a04a10f8SKris Kennaway 
1833a04a10f8SKris Kennaway 	/* Record that the user has logged out. */
1834a04a10f8SKris Kennaway 	record_logout(s->pid, s->tty);
1835a04a10f8SKris Kennaway 
1836a04a10f8SKris Kennaway 	/* Release the pseudo-tty. */
1837a04a10f8SKris Kennaway 	pty_release(s->tty);
1838a04a10f8SKris Kennaway 
1839a04a10f8SKris Kennaway 	/*
1840a04a10f8SKris Kennaway 	 * Close the server side of the socket pairs.  We must do this after
1841a04a10f8SKris Kennaway 	 * the pty cleanup, so that another process doesn't get this pty
1842a04a10f8SKris Kennaway 	 * while we're still cleaning up.
1843a04a10f8SKris Kennaway 	 */
1844a04a10f8SKris Kennaway 	if (close(s->ptymaster) < 0)
1845a04a10f8SKris Kennaway 		error("close(s->ptymaster): %s", strerror(errno));
1846a04a10f8SKris Kennaway }
1847a04a10f8SKris Kennaway 
1848a04a10f8SKris Kennaway void
1849a04a10f8SKris Kennaway session_exit_message(Session *s, int status)
1850a04a10f8SKris Kennaway {
1851a04a10f8SKris Kennaway 	Channel *c;
1852a04a10f8SKris Kennaway 	if (s == NULL)
1853a04a10f8SKris Kennaway 		fatal("session_close: no session");
1854a04a10f8SKris Kennaway 	c = channel_lookup(s->chanid);
1855a04a10f8SKris Kennaway 	if (c == NULL)
1856a04a10f8SKris Kennaway 		fatal("session_close: session %d: no channel %d",
1857a04a10f8SKris Kennaway 		    s->self, s->chanid);
1858a04a10f8SKris Kennaway 	debug("session_exit_message: session %d channel %d pid %d",
1859a04a10f8SKris Kennaway 	    s->self, s->chanid, s->pid);
1860a04a10f8SKris Kennaway 
1861a04a10f8SKris Kennaway 	if (WIFEXITED(status)) {
1862a04a10f8SKris Kennaway 		channel_request_start(s->chanid,
1863a04a10f8SKris Kennaway 		    "exit-status", 0);
1864a04a10f8SKris Kennaway 		packet_put_int(WEXITSTATUS(status));
1865a04a10f8SKris Kennaway 		packet_send();
1866a04a10f8SKris Kennaway 	} else if (WIFSIGNALED(status)) {
1867a04a10f8SKris Kennaway 		channel_request_start(s->chanid,
1868a04a10f8SKris Kennaway 		    "exit-signal", 0);
1869a04a10f8SKris Kennaway 		packet_put_int(WTERMSIG(status));
1870a04a10f8SKris Kennaway 		packet_put_char(WCOREDUMP(status));
1871a04a10f8SKris Kennaway 		packet_put_cstring("");
1872a04a10f8SKris Kennaway 		packet_put_cstring("");
1873a04a10f8SKris Kennaway 		packet_send();
1874a04a10f8SKris Kennaway 	} else {
1875a04a10f8SKris Kennaway 		/* Some weird exit cause.  Just exit. */
1876a04a10f8SKris Kennaway 		packet_disconnect("wait returned status %04x.", status);
1877a04a10f8SKris Kennaway 	}
1878a04a10f8SKris Kennaway 
1879a04a10f8SKris Kennaway 	/* disconnect channel */
1880a04a10f8SKris Kennaway 	debug("session_exit_message: release channel %d", s->chanid);
1881a04a10f8SKris Kennaway 	channel_cancel_cleanup(s->chanid);
1882a04a10f8SKris Kennaway 	/*
1883a04a10f8SKris Kennaway 	 * emulate a write failure with 'chan_write_failed', nobody will be
1884a04a10f8SKris Kennaway 	 * interested in data we write.
1885a04a10f8SKris Kennaway 	 * Note that we must not call 'chan_read_failed', since there could
1886a04a10f8SKris Kennaway 	 * be some more data waiting in the pipe.
1887a04a10f8SKris Kennaway 	 */
1888a04a10f8SKris Kennaway 	if (c->ostate != CHAN_OUTPUT_CLOSED)
1889a04a10f8SKris Kennaway 		chan_write_failed(c);
1890a04a10f8SKris Kennaway 	s->chanid = -1;
1891a04a10f8SKris Kennaway }
1892a04a10f8SKris Kennaway 
1893a04a10f8SKris Kennaway void
1894a04a10f8SKris Kennaway session_free(Session *s)
1895a04a10f8SKris Kennaway {
1896a04a10f8SKris Kennaway 	debug("session_free: session %d pid %d", s->self, s->pid);
1897a04a10f8SKris Kennaway 	if (s->term)
1898a04a10f8SKris Kennaway 		xfree(s->term);
1899a04a10f8SKris Kennaway 	if (s->display)
1900a04a10f8SKris Kennaway 		xfree(s->display);
1901a04a10f8SKris Kennaway 	if (s->auth_data)
1902a04a10f8SKris Kennaway 		xfree(s->auth_data);
1903a04a10f8SKris Kennaway 	if (s->auth_proto)
1904a04a10f8SKris Kennaway 		xfree(s->auth_proto);
1905a04a10f8SKris Kennaway 	s->used = 0;
1906a04a10f8SKris Kennaway }
1907a04a10f8SKris Kennaway 
1908a04a10f8SKris Kennaway void
1909a04a10f8SKris Kennaway session_close(Session *s)
1910a04a10f8SKris Kennaway {
1911a04a10f8SKris Kennaway 	session_pty_cleanup(s);
1912a04a10f8SKris Kennaway 	session_free(s);
1913a04a10f8SKris Kennaway 	session_proctitle(s);
1914a04a10f8SKris Kennaway }
1915a04a10f8SKris Kennaway 
1916a04a10f8SKris Kennaway void
1917a04a10f8SKris Kennaway session_close_by_pid(pid_t pid, int status)
1918a04a10f8SKris Kennaway {
1919a04a10f8SKris Kennaway 	Session *s = session_by_pid(pid);
1920a04a10f8SKris Kennaway 	if (s == NULL) {
1921a04a10f8SKris Kennaway 		debug("session_close_by_pid: no session for pid %d", s->pid);
1922a04a10f8SKris Kennaway 		return;
1923a04a10f8SKris Kennaway 	}
1924a04a10f8SKris Kennaway 	if (s->chanid != -1)
1925a04a10f8SKris Kennaway 		session_exit_message(s, status);
1926a04a10f8SKris Kennaway 	session_close(s);
1927a04a10f8SKris Kennaway }
1928a04a10f8SKris Kennaway 
1929a04a10f8SKris Kennaway /*
1930a04a10f8SKris Kennaway  * this is called when a channel dies before
1931a04a10f8SKris Kennaway  * the session 'child' itself dies
1932a04a10f8SKris Kennaway  */
1933a04a10f8SKris Kennaway void
1934a04a10f8SKris Kennaway session_close_by_channel(int id, void *arg)
1935a04a10f8SKris Kennaway {
1936a04a10f8SKris Kennaway 	Session *s = session_by_channel(id);
1937a04a10f8SKris Kennaway 	if (s == NULL) {
1938a04a10f8SKris Kennaway 		debug("session_close_by_channel: no session for channel %d", id);
1939a04a10f8SKris Kennaway 		return;
1940a04a10f8SKris Kennaway 	}
1941a04a10f8SKris Kennaway 	/* disconnect channel */
1942a04a10f8SKris Kennaway 	channel_cancel_cleanup(s->chanid);
1943a04a10f8SKris Kennaway 	s->chanid = -1;
1944a04a10f8SKris Kennaway 
1945a04a10f8SKris Kennaway 	debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1946a04a10f8SKris Kennaway 	if (s->pid == 0) {
1947a04a10f8SKris Kennaway 		/* close session immediately */
1948a04a10f8SKris Kennaway 		session_close(s);
1949a04a10f8SKris Kennaway 	} else {
1950a04a10f8SKris Kennaway 		/* notify child, delay session cleanup */
195109958426SBrian Feldman 		if (s->pid <= 1)
195209958426SBrian Feldman 			fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
1953a04a10f8SKris Kennaway 		if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
1954a04a10f8SKris Kennaway 			error("session_close_by_channel: kill %d: %s",
1955a04a10f8SKris Kennaway 			    s->pid, strerror(errno));
1956a04a10f8SKris Kennaway 	}
1957a04a10f8SKris Kennaway }
1958a04a10f8SKris Kennaway 
1959a04a10f8SKris Kennaway char *
1960a04a10f8SKris Kennaway session_tty_list(void)
1961a04a10f8SKris Kennaway {
1962a04a10f8SKris Kennaway 	static char buf[1024];
1963a04a10f8SKris Kennaway 	int i;
1964a04a10f8SKris Kennaway 	buf[0] = '\0';
1965a04a10f8SKris Kennaway 	for(i = 0; i < MAX_SESSIONS; i++) {
1966a04a10f8SKris Kennaway 		Session *s = &sessions[i];
1967a04a10f8SKris Kennaway 		if (s->used && s->ttyfd != -1) {
1968a04a10f8SKris Kennaway 			if (buf[0] != '\0')
1969a04a10f8SKris Kennaway 				strlcat(buf, ",", sizeof buf);
1970a04a10f8SKris Kennaway 			strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1971a04a10f8SKris Kennaway 		}
1972a04a10f8SKris Kennaway 	}
1973a04a10f8SKris Kennaway 	if (buf[0] == '\0')
1974a04a10f8SKris Kennaway 		strlcpy(buf, "notty", sizeof buf);
1975a04a10f8SKris Kennaway 	return buf;
1976a04a10f8SKris Kennaway }
1977a04a10f8SKris Kennaway 
1978a04a10f8SKris Kennaway void
1979a04a10f8SKris Kennaway session_proctitle(Session *s)
1980a04a10f8SKris Kennaway {
1981a04a10f8SKris Kennaway 	if (s->pw == NULL)
1982a04a10f8SKris Kennaway 		error("no user for session %d", s->self);
1983a04a10f8SKris Kennaway 	else
1984a04a10f8SKris Kennaway 		setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1985a04a10f8SKris Kennaway }
1986a04a10f8SKris Kennaway 
1987a04a10f8SKris Kennaway void
1988ca3176e7SBrian Feldman do_authenticated2(Authctxt *authctxt)
1989a04a10f8SKris Kennaway {
1990c2d3a559SKris Kennaway 
1991a04a10f8SKris Kennaway 	server_loop2();
1992a04a10f8SKris Kennaway 	if (xauthfile)
1993a04a10f8SKris Kennaway 		xauthfile_cleanup_proc(NULL);
1994a04a10f8SKris Kennaway }
1995