blob: c65c7e6639eedef3a838c0b6918852eadd9406ea [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
Damien Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
Ben Lindstrom92a2e382001-03-05 06:59:27 +000012 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Ben Lindstrom983c0982001-06-09 01:20:06 +000036RCSID("$OpenBSD: session.c,v 1.80 2001/06/04 21:59:43 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000042#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "packet.h"
44#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100045#include "mpaux.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "uidswap.h"
47#include "compat.h"
Ben Lindstromc7637672001-06-09 00:36:26 +000048#include "channels.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100051#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "pathnames.h"
53#include "log.h"
54#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000055#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "serverloop.h"
57#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000058#include "session.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100059
Damien Miller91606b12000-06-28 08:22:29 +100060#ifdef WITH_IRIX_PROJECT
61#include <proj.h>
62#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000063#ifdef WITH_IRIX_JOBS
64#include <sys/resource.h>
Kevin Stevesef4eea92001-02-05 12:42:17 +000065#endif
Ben Lindstrom49a79c02000-11-17 03:47:20 +000066#ifdef WITH_IRIX_AUDIT
67#include <sat.h>
68#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100069
Damien Miller37023962000-07-11 17:31:38 +100070#if defined(HAVE_USERSEC_H)
71#include <usersec.h>
72#endif
73
Damien Millerbac2d8a2000-09-05 16:13:06 +110074#ifdef HAVE_CYGWIN
75#include <windows.h>
76#include <sys/cygwin.h>
77#define is_winnt (GetVersion() < 0x80000000)
78#endif
79
Damien Millerd17b8d52000-08-09 14:42:28 +100080/* AIX limits */
81#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100082# define S_UFSIZE_HARD S_UFSIZE "_hard"
83# define S_UCPU_HARD S_UCPU "_hard"
84# define S_UDATA_HARD S_UDATA "_hard"
85# define S_USTACK_HARD S_USTACK "_hard"
86# define S_URSS_HARD S_URSS "_hard"
87# define S_UCORE_HARD S_UCORE "_hard"
88# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100089#endif
90
Damien Miller168a7002001-03-17 10:29:50 +110091#ifdef _AIX
92# include <uinfo.h>
93#endif
94
Damien Millerb38eff82000-04-01 11:09:21 +100095/* types */
96
97#define TTYSZ 64
98typedef struct Session Session;
99struct Session {
100 int used;
101 int self;
102 struct passwd *pw;
103 pid_t pid;
104 /* tty */
105 char *term;
106 int ptyfd, ttyfd, ptymaster;
107 int row, col, xpixel, ypixel;
108 char tty[TTYSZ];
109 /* X11 */
110 char *display;
111 int screen;
112 char *auth_proto;
113 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000114 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000115 /* proto 2 */
116 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000117 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +1000118};
119
120/* func */
121
122Session *session_new(void);
123void session_set_fds(Session *s, int fdin, int fdout, int fderr);
124void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000125void session_proctitle(Session *s);
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000126void do_exec_pty(Session *s, const char *command);
127void do_exec_no_pty(Session *s, const char *command);
Damien Miller69b69aa2000-10-28 14:19:58 +1100128void do_login(Session *s, const char *command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000129#ifdef LOGIN_NEEDS_UTMPX
130void do_pre_login(Session *s);
131#endif
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000132void do_child(Session *s, const char *command);
Damien Millercf205e82001-04-16 18:29:15 +1000133void do_motd(void);
134int check_quietlogin(Session *s, const char *command);
Ben Lindstrom983c0982001-06-09 01:20:06 +0000135void xauthfile_cleanup_proc(void *pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000136
Ben Lindstromb31783d2001-03-22 02:02:12 +0000137void do_authenticated1(Authctxt *authctxt);
138void do_authenticated2(Authctxt *authctxt);
139
Damien Millerb38eff82000-04-01 11:09:21 +1000140/* import */
141extern ServerOptions options;
142extern char *__progname;
143extern int log_stderr;
144extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000145extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000146extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000147extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000148
Damien Millerb38eff82000-04-01 11:09:21 +1000149/* Local Xauthority file. */
150static char *xauthfile;
151
Damien Miller7b28dc52000-09-05 13:34:53 +1100152/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100154
Damien Millerb38eff82000-04-01 11:09:21 +1000155/* data */
156#define MAX_SESSIONS 10
157Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100158
Damien Millerd2c208a2000-05-17 22:00:02 +1000159#ifdef WITH_AIXAUTHENTICATE
160/* AIX's lastlogin message, set in auth1.c */
161char *aixloginmsg;
162#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000163
Damien Millerad833b32000-08-23 10:46:23 +1000164#ifdef HAVE_LOGIN_CAP
165static login_cap_t *lc;
166#endif
167
Ben Lindstromb31783d2001-03-22 02:02:12 +0000168void
169do_authenticated(Authctxt *authctxt)
170{
171 /*
172 * Cancel the alarm we set to limit the time taken for
173 * authentication.
174 */
175 alarm(0);
176 if (startup_pipe != -1) {
177 close(startup_pipe);
178 startup_pipe = -1;
179 }
180#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
181 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
182 error("unable to get login class");
183 return;
184 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000185#ifdef BSD_AUTH
186 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
187 packet_disconnect("Approval failure for %s",
188 authctxt->pw->pw_name);
189 }
190#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000191#endif
192 /* setup the channel layer */
193 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
194 channel_permit_all_opens();
195
196 if (compat20)
197 do_authenticated2(authctxt);
198 else
199 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000200
201 /* remote user's local Xauthority file and agent socket */
202 if (xauthfile)
Ben Lindstrom983c0982001-06-09 01:20:06 +0000203 xauthfile_cleanup_proc(authctxt->pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000204 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000205 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000206}
207
Damien Millerb38eff82000-04-01 11:09:21 +1000208/*
209 * Remove local Xauthority file.
210 */
211void
Ben Lindstrom983c0982001-06-09 01:20:06 +0000212xauthfile_cleanup_proc(void *_pw)
Damien Millerb38eff82000-04-01 11:09:21 +1000213{
Ben Lindstrom983c0982001-06-09 01:20:06 +0000214 struct passwd *pw = _pw;
215 char *p;
Damien Millerb38eff82000-04-01 11:09:21 +1000216
Ben Lindstrom983c0982001-06-09 01:20:06 +0000217 debug("xauthfile_cleanup_proc called");
Damien Millerb38eff82000-04-01 11:09:21 +1000218 if (xauthfile != NULL) {
Ben Lindstrom983c0982001-06-09 01:20:06 +0000219 temporarily_use_uid(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000220 unlink(xauthfile);
221 p = strrchr(xauthfile, '/');
222 if (p != NULL) {
223 *p = '\0';
224 rmdir(xauthfile);
225 }
226 xfree(xauthfile);
227 xauthfile = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +0000228 restore_uid();
Damien Millerb38eff82000-04-01 11:09:21 +1000229 }
230}
231
232/*
233 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
234 * dropped connection).
235 */
Damien Miller4af51302000-04-16 11:18:38 +1000236void
Damien Millerb38eff82000-04-01 11:09:21 +1000237pty_cleanup_proc(void *session)
238{
239 Session *s=session;
240 if (s == NULL)
241 fatal("pty_cleanup_proc: no session");
242 debug("pty_cleanup_proc: %s", s->tty);
243
244 if (s->pid != 0) {
245 /* Record that the user has logged out. */
246 record_logout(s->pid, s->tty);
247 }
248
249 /* Release the pseudo-tty. */
250 pty_release(s->tty);
251}
252
253/*
254 * Prepares for an interactive session. This is called after the user has
255 * been successfully authenticated. During this message exchange, pseudo
256 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
257 * are requested, etc.
258 */
Damien Miller4af51302000-04-16 11:18:38 +1000259void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000260do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000261{
262 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000263 char *command;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000264 int success, type, fd, n_bytes, plen, screen_flag, have_pty = 0;
265 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000266 u_int proto_len, data_len, dlen;
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +0000267 struct stat st;
Damien Millerb38eff82000-04-01 11:09:21 +1000268
269 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000270 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000271
Damien Millerb38eff82000-04-01 11:09:21 +1000272 /*
273 * We stay in this loop until the client requests to execute a shell
274 * or a command.
275 */
276 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000277 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000278
279 /* Get a packet from the client. */
280 type = packet_read(&plen);
281
282 /* Process the packet. */
283 switch (type) {
284 case SSH_CMSG_REQUEST_COMPRESSION:
285 packet_integrity_check(plen, 4, type);
286 compression_level = packet_get_int();
287 if (compression_level < 1 || compression_level > 9) {
288 packet_send_debug("Received illegal compression level %d.",
289 compression_level);
290 break;
291 }
292 /* Enable compression after we have responded with SUCCESS. */
293 enable_compression_after_reply = 1;
294 success = 1;
295 break;
296
297 case SSH_CMSG_REQUEST_PTY:
298 if (no_pty_flag) {
299 debug("Allocating a pty not permitted for this authentication.");
300 break;
301 }
302 if (have_pty)
303 packet_disconnect("Protocol error: you already have a pty.");
304
305 debug("Allocating pty.");
306
307 /* Allocate a pty and open it. */
308 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
309 sizeof(s->tty))) {
310 error("Failed to allocate pty.");
311 break;
312 }
313 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000314 pty_setowner(s->pw, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000315
316 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
317 s->term = packet_get_string(&dlen);
318 packet_integrity_check(dlen, strlen(s->term), type);
319 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
320 /* Remaining bytes */
321 n_bytes = plen - (4 + dlen + 4 * 4);
322
323 if (strcmp(s->term, "") == 0) {
324 xfree(s->term);
325 s->term = NULL;
326 }
327 /* Get window size from the packet. */
328 s->row = packet_get_int();
329 s->col = packet_get_int();
330 s->xpixel = packet_get_int();
331 s->ypixel = packet_get_int();
332 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
333
334 /* Get tty modes from the packet. */
335 tty_parse_modes(s->ttyfd, &n_bytes);
336 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
337
Damien Millere247cc42000-05-07 12:03:14 +1000338 session_proctitle(s);
339
Damien Millerb38eff82000-04-01 11:09:21 +1000340 /* Indicate that we now have a pty. */
341 success = 1;
342 have_pty = 1;
343 break;
344
345 case SSH_CMSG_X11_REQUEST_FORWARDING:
346 if (!options.x11_forwarding) {
347 packet_send_debug("X11 forwarding disabled in server configuration file.");
348 break;
349 }
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +0000350 if (!options.xauth_location ||
351 (stat(options.xauth_location, &st) == -1)) {
Damien Miller0c043c12000-06-07 21:22:38 +1000352 packet_send_debug("No xauth program; cannot forward with spoofing.");
353 break;
354 }
Damien Millerb38eff82000-04-01 11:09:21 +1000355 if (no_x11_forwarding_flag) {
356 packet_send_debug("X11 forwarding not permitted for this authentication.");
357 break;
358 }
359 debug("Received request for X11 forwarding with auth spoofing.");
360 if (s->display != NULL)
361 packet_disconnect("Protocol error: X11 display already set.");
362
363 s->auth_proto = packet_get_string(&proto_len);
364 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000365
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000366 screen_flag = packet_get_protocol_flags() &
367 SSH_PROTOFLAG_SCREEN_NUMBER;
368 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
369
370 if (packet_remaining() == 4) {
371 if (!screen_flag)
372 debug2("Buggy client: "
373 "X11 screen flag missing");
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000374 packet_integrity_check(plen,
375 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000376 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000377 } else {
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000378 packet_integrity_check(plen,
379 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000380 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000381 }
Damien Millerb38eff82000-04-01 11:09:21 +1000382 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
383
384 if (s->display == NULL)
385 break;
386
387 /* Setup to always have a local .Xauthority. */
388 xauthfile = xmalloc(MAXPATHLEN);
389 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000390 temporarily_use_uid(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000391 if (mkdtemp(xauthfile) == NULL) {
392 restore_uid();
393 error("private X11 dir: mkdtemp %s failed: %s",
394 xauthfile, strerror(errno));
395 xfree(xauthfile);
396 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000397 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000398 break;
399 }
400 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100401 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
402 if (fd >= 0)
403 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000404 restore_uid();
Ben Lindstrom983c0982001-06-09 01:20:06 +0000405 fatal_add_cleanup(xauthfile_cleanup_proc, s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000406 success = 1;
407 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000408
409 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
410 if (no_agent_forwarding_flag || compat13) {
411 debug("Authentication agent forwarding not permitted for this authentication.");
412 break;
413 }
414 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000415 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000416 break;
417
418 case SSH_CMSG_PORT_FORWARD_REQUEST:
419 if (no_port_forwarding_flag) {
420 debug("Port forwarding not permitted for this authentication.");
421 break;
422 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100423 if (!options.allow_tcp_forwarding) {
424 debug("Port forwarding not permitted.");
425 break;
426 }
Damien Millerb38eff82000-04-01 11:09:21 +1000427 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000428 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000429 success = 1;
430 break;
431
432 case SSH_CMSG_MAX_PACKET_SIZE:
433 if (packet_set_maxsize(packet_get_int()) > 0)
434 success = 1;
435 break;
436
437 case SSH_CMSG_EXEC_SHELL:
438 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000439 if (type == SSH_CMSG_EXEC_CMD) {
440 command = packet_get_string(&dlen);
441 debug("Exec command '%.500s'", command);
442 packet_integrity_check(plen, 4 + dlen, type);
443 } else {
444 command = NULL;
445 packet_integrity_check(plen, 0, type);
446 }
447 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100448 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000449 command = forced_command;
450 debug("Forced command '%.500s'", forced_command);
451 }
452 if (have_pty)
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000453 do_exec_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000454 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000455 do_exec_no_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000456
457 if (command != NULL)
458 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000459 return;
460
461 default:
462 /*
463 * Any unknown messages in this phase are ignored,
464 * and a failure message is returned.
465 */
466 log("Unknown packet type received after authentication: %d", type);
467 }
468 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
469 packet_send();
470 packet_write_wait();
471
472 /* Enable compression now that we have replied if appropriate. */
473 if (enable_compression_after_reply) {
474 enable_compression_after_reply = 0;
475 packet_start_compression(compression_level);
476 }
477 }
478}
479
480/*
481 * This is called to fork and execute a command when we have no tty. This
482 * will call do_child from the child, and server_loop from the parent after
483 * setting up file descriptors and such.
484 */
Damien Miller4af51302000-04-16 11:18:38 +1000485void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000486do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000487{
488 int pid;
489
490#ifdef USE_PIPES
491 int pin[2], pout[2], perr[2];
492 /* Allocate pipes for communicating with the program. */
493 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
494 packet_disconnect("Could not create pipes: %.100s",
495 strerror(errno));
496#else /* USE_PIPES */
497 int inout[2], err[2];
498 /* Uses socket pairs to communicate with the program. */
499 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
500 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
501 packet_disconnect("Could not create socket pairs: %.100s",
502 strerror(errno));
503#endif /* USE_PIPES */
504 if (s == NULL)
505 fatal("do_exec_no_pty: no session");
506
Damien Millere247cc42000-05-07 12:03:14 +1000507 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000508
Damien Millerc5946332001-02-28 11:46:11 +1100509#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000510 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000511#endif /* USE_PAM */
512
Damien Millerb38eff82000-04-01 11:09:21 +1000513 /* Fork the child. */
514 if ((pid = fork()) == 0) {
515 /* Child. Reinitialize the log since the pid has changed. */
516 log_init(__progname, options.log_level, options.log_facility, log_stderr);
517
518 /*
519 * Create a new session and process group since the 4.4BSD
520 * setlogin() affects the entire process group.
521 */
522 if (setsid() < 0)
523 error("setsid failed: %.100s", strerror(errno));
524
525#ifdef USE_PIPES
526 /*
527 * Redirect stdin. We close the parent side of the socket
528 * pair, and make the child side the standard input.
529 */
530 close(pin[1]);
531 if (dup2(pin[0], 0) < 0)
532 perror("dup2 stdin");
533 close(pin[0]);
534
535 /* Redirect stdout. */
536 close(pout[0]);
537 if (dup2(pout[1], 1) < 0)
538 perror("dup2 stdout");
539 close(pout[1]);
540
541 /* Redirect stderr. */
542 close(perr[0]);
543 if (dup2(perr[1], 2) < 0)
544 perror("dup2 stderr");
545 close(perr[1]);
546#else /* USE_PIPES */
547 /*
548 * Redirect stdin, stdout, and stderr. Stdin and stdout will
549 * use the same socket, as some programs (particularly rdist)
550 * seem to depend on it.
551 */
552 close(inout[1]);
553 close(err[1]);
554 if (dup2(inout[0], 0) < 0) /* stdin */
555 perror("dup2 stdin");
556 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
557 perror("dup2 stdout");
558 if (dup2(err[0], 2) < 0) /* stderr */
559 perror("dup2 stderr");
560#endif /* USE_PIPES */
561
562 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000563 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000564 /* NOTREACHED */
565 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100566#ifdef HAVE_CYGWIN
567 if (is_winnt)
568 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
569#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000570 if (pid < 0)
571 packet_disconnect("fork failed: %.100s", strerror(errno));
572 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000573 /* Set interactive/non-interactive mode. */
574 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000575#ifdef USE_PIPES
576 /* We are the parent. Close the child sides of the pipes. */
577 close(pin[0]);
578 close(pout[1]);
579 close(perr[1]);
580
Damien Millerefb4afe2000-04-12 18:45:05 +1000581 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000582 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000583 } else {
584 /* Enter the interactive session. */
585 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000586 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000587 }
Damien Millerb38eff82000-04-01 11:09:21 +1000588#else /* USE_PIPES */
589 /* We are the parent. Close the child sides of the socket pairs. */
590 close(inout[0]);
591 close(err[0]);
592
593 /*
594 * Enter the interactive session. Note: server_loop must be able to
595 * handle the case that fdin and fdout are the same.
596 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000597 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000598 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000599 } else {
600 server_loop(pid, inout[1], inout[1], err[1]);
601 /* server_loop has closed inout[1] and err[1]. */
602 }
Damien Millerb38eff82000-04-01 11:09:21 +1000603#endif /* USE_PIPES */
604}
605
606/*
607 * This is called to fork and execute a command when we have a tty. This
608 * will call do_child from the child, and server_loop from the parent after
609 * setting up file descriptors, controlling tty, updating wtmp, utmp,
610 * lastlog, and other such operations.
611 */
Damien Miller4af51302000-04-16 11:18:38 +1000612void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000613do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000614{
Damien Millerb38eff82000-04-01 11:09:21 +1000615 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000616 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000617
618 if (s == NULL)
619 fatal("do_exec_pty: no session");
620 ptyfd = s->ptyfd;
621 ttyfd = s->ttyfd;
622
Damien Millerc5946332001-02-28 11:46:11 +1100623#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000624 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000625 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100626#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000627
Damien Millerb38eff82000-04-01 11:09:21 +1000628 /* Fork the child. */
629 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000630 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000631 log_init(__progname, options.log_level, options.log_facility, log_stderr);
632
633 /* Close the master side of the pseudo tty. */
634 close(ptyfd);
635
636 /* Make the pseudo tty our controlling tty. */
637 pty_make_controlling_tty(&ttyfd, s->tty);
638
639 /* Redirect stdin from the pseudo tty. */
640 if (dup2(ttyfd, fileno(stdin)) < 0)
641 error("dup2 stdin failed: %.100s", strerror(errno));
642
643 /* Redirect stdout to the pseudo tty. */
644 if (dup2(ttyfd, fileno(stdout)) < 0)
645 error("dup2 stdin failed: %.100s", strerror(errno));
646
647 /* Redirect stderr to the pseudo tty. */
648 if (dup2(ttyfd, fileno(stderr)) < 0)
649 error("dup2 stdin failed: %.100s", strerror(errno));
650
651 /* Close the extra descriptor for the pseudo tty. */
652 close(ttyfd);
653
Damien Miller942da032000-08-18 13:59:06 +1000654 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000655#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100656 if (!(options.use_login && command == NULL))
657 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000658# ifdef LOGIN_NEEDS_UTMPX
659 else
660 do_pre_login(s);
661# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000662#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000663
Damien Millerb38eff82000-04-01 11:09:21 +1000664 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000665 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000666 /* NOTREACHED */
667 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100668#ifdef HAVE_CYGWIN
669 if (is_winnt)
670 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
671#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000672 if (pid < 0)
673 packet_disconnect("fork failed: %.100s", strerror(errno));
674 s->pid = pid;
675
676 /* Parent. Close the slave side of the pseudo tty. */
677 close(ttyfd);
678
679 /*
680 * Create another descriptor of the pty master side for use as the
681 * standard input. We could use the original descriptor, but this
682 * simplifies code in server_loop. The descriptor is bidirectional.
683 */
684 fdout = dup(ptyfd);
685 if (fdout < 0)
686 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
687
688 /* we keep a reference to the pty master */
689 ptymaster = dup(ptyfd);
690 if (ptymaster < 0)
691 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
692 s->ptymaster = ptymaster;
693
694 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000695 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000696 if (compat20) {
697 session_set_fds(s, ptyfd, fdout, -1);
698 } else {
699 server_loop(pid, ptyfd, fdout, -1);
700 /* server_loop _has_ closed ptyfd and fdout. */
701 session_pty_cleanup(s);
702 }
Damien Millerb38eff82000-04-01 11:09:21 +1000703}
704
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000705#ifdef LOGIN_NEEDS_UTMPX
706void
707do_pre_login(Session *s)
708{
709 socklen_t fromlen;
710 struct sockaddr_storage from;
711 pid_t pid = getpid();
712
713 /*
714 * Get IP address of client. If the connection is not a socket, let
715 * the address be 0.0.0.0.
716 */
717 memset(&from, 0, sizeof(from));
718 if (packet_connection_is_on_socket()) {
719 fromlen = sizeof(from);
720 if (getpeername(packet_get_connection_in(),
721 (struct sockaddr *) & from, &fromlen) < 0) {
722 debug("getpeername: %.100s", strerror(errno));
723 fatal_cleanup();
724 }
725 }
726
727 record_utmp_only(pid, s->tty, s->pw->pw_name,
728 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
729 (struct sockaddr *)&from);
730}
731#endif
732
Damien Miller942da032000-08-18 13:59:06 +1000733/* administrative, login(1)-like work */
734void
Damien Miller69b69aa2000-10-28 14:19:58 +1100735do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000736{
Damien Miller942da032000-08-18 13:59:06 +1000737 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100738 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000739 socklen_t fromlen;
740 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000741 time_t last_login_time;
742 struct passwd * pw = s->pw;
743 pid_t pid = getpid();
744
745 /*
746 * Get IP address of client. If the connection is not a socket, let
747 * the address be 0.0.0.0.
748 */
749 memset(&from, 0, sizeof(from));
750 if (packet_connection_is_on_socket()) {
751 fromlen = sizeof(from);
752 if (getpeername(packet_get_connection_in(),
753 (struct sockaddr *) & from, &fromlen) < 0) {
754 debug("getpeername: %.100s", strerror(errno));
755 fatal_cleanup();
756 }
757 }
758
Damien Miller7b28dc52000-09-05 13:34:53 +1100759 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000760 if (options.print_lastlog) {
761 hostname[0] = '\0';
762 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
763 hostname, sizeof(hostname));
764 }
Damien Millere4340be2000-09-16 13:29:08 +1100765
Damien Miller942da032000-08-18 13:59:06 +1000766 /* Record that there was a login on that tty from the remote host. */
767 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000768 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
769 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000770
Kevin Steves092f2ef2000-10-14 13:36:13 +0000771#ifdef USE_PAM
772 /*
773 * If password change is needed, do it now.
774 * This needs to occur before the ~/.hushlogin check.
775 */
Damien Miller646aa602001-02-15 11:51:32 +1100776 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000777 print_pam_messages();
778 do_pam_chauthtok();
779 }
780#endif
781
Damien Millercf205e82001-04-16 18:29:15 +1000782 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000783 return;
784
785#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100786 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000787 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000788#endif /* USE_PAM */
789#ifdef WITH_AIXAUTHENTICATE
790 if (aixloginmsg && *aixloginmsg)
791 printf("%s\n", aixloginmsg);
792#endif /* WITH_AIXAUTHENTICATE */
793
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000794 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000795 time_string = ctime(&last_login_time);
796 if (strchr(time_string, '\n'))
797 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000798 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000799 printf("Last login: %s\r\n", time_string);
800 else
Damien Millere4340be2000-09-16 13:29:08 +1100801 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000802 }
Damien Millercf205e82001-04-16 18:29:15 +1000803
804 do_motd();
805}
806
807/*
808 * Display the message of the day.
809 */
810void
811do_motd(void)
812{
813 FILE *f;
814 char buf[256];
815
Damien Miller942da032000-08-18 13:59:06 +1000816 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000817#ifdef HAVE_LOGIN_CAP
818 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
819 "/etc/motd"), "r");
820#else
Damien Miller942da032000-08-18 13:59:06 +1000821 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000822#endif
Damien Miller942da032000-08-18 13:59:06 +1000823 if (f) {
824 while (fgets(buf, sizeof(buf), f))
825 fputs(buf, stdout);
826 fclose(f);
827 }
828 }
829}
830
Damien Millercf205e82001-04-16 18:29:15 +1000831
832/*
833 * Check for quiet login, either .hushlogin or command given.
834 */
835int
836check_quietlogin(Session *s, const char *command)
837{
838 char buf[256];
839 struct passwd * pw = s->pw;
840 struct stat st;
841
842 /* Return 1 if .hushlogin exists or a command given. */
843 if (command != NULL)
844 return 1;
845 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
846#ifdef HAVE_LOGIN_CAP
847 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
848 return 1;
849#else
850 if (stat(buf, &st) >= 0)
851 return 1;
852#endif
853 return 0;
854}
855
Damien Millerb38eff82000-04-01 11:09:21 +1000856/*
857 * Sets the value of the given variable in the environment. If the variable
858 * already exists, its value is overriden.
859 */
Damien Miller4af51302000-04-16 11:18:38 +1000860void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000861child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000862 const char *value)
863{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000864 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000865 char **env;
866
867 /*
868 * Find the slot where the value should be stored. If the variable
869 * already exists, we reuse the slot; otherwise we append a new slot
870 * at the end of the array, expanding if necessary.
871 */
872 env = *envp;
873 namelen = strlen(name);
874 for (i = 0; env[i]; i++)
875 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
876 break;
877 if (env[i]) {
878 /* Reuse the slot. */
879 xfree(env[i]);
880 } else {
881 /* New variable. Expand if necessary. */
882 if (i >= (*envsizep) - 1) {
883 (*envsizep) += 50;
884 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
885 }
886 /* Need to set the NULL pointer at end of array beyond the new slot. */
887 env[i + 1] = NULL;
888 }
889
890 /* Allocate space and format the variable in the appropriate slot. */
891 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
892 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
893}
894
895/*
896 * Reads environment variables from the given file and adds/overrides them
897 * into the environment. If the file does not exist, this does nothing.
898 * Otherwise, it must consist of empty lines, comments (line starts with '#')
899 * and assignments of the form name=value. No other forms are allowed.
900 */
Damien Miller4af51302000-04-16 11:18:38 +1000901void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000902read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000903 const char *filename)
904{
905 FILE *f;
906 char buf[4096];
907 char *cp, *value;
908
909 f = fopen(filename, "r");
910 if (!f)
911 return;
912
913 while (fgets(buf, sizeof(buf), f)) {
914 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
915 ;
916 if (!*cp || *cp == '#' || *cp == '\n')
917 continue;
918 if (strchr(cp, '\n'))
919 *strchr(cp, '\n') = '\0';
920 value = strchr(cp, '=');
921 if (value == NULL) {
922 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
923 continue;
924 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000925 /*
926 * Replace the equals sign by nul, and advance value to
927 * the value string.
928 */
Damien Millerb38eff82000-04-01 11:09:21 +1000929 *value = '\0';
930 value++;
931 child_set_env(env, envsize, cp, value);
932 }
933 fclose(f);
934}
935
936#ifdef USE_PAM
937/*
938 * Sets any environment variables which have been specified by PAM
939 */
940void do_pam_environment(char ***env, int *envsize)
941{
942 char *equals, var_name[512], var_val[512];
943 char **pam_env;
944 int i;
945
946 if ((pam_env = fetch_pam_environment()) == NULL)
947 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000948
Damien Millerb38eff82000-04-01 11:09:21 +1000949 for(i = 0; pam_env[i] != NULL; i++) {
950 if ((equals = strstr(pam_env[i], "=")) == NULL)
951 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000952
Damien Millerb38eff82000-04-01 11:09:21 +1000953 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
954 memset(var_name, '\0', sizeof(var_name));
955 memset(var_val, '\0', sizeof(var_val));
956
957 strncpy(var_name, pam_env[i], equals - pam_env[i]);
958 strcpy(var_val, equals + 1);
959
Damien Millercb5e44a2000-09-29 12:12:36 +1100960 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000961
962 child_set_env(env, envsize, var_name, var_val);
963 }
964 }
965}
966#endif /* USE_PAM */
967
Damien Millercb5e44a2000-09-29 12:12:36 +1100968#ifdef HAVE_CYGWIN
969void copy_environment(char ***env, int *envsize)
970{
971 char *equals, var_name[512], var_val[512];
972 int i;
973
974 for(i = 0; environ[i] != NULL; i++) {
975 if ((equals = strstr(environ[i], "=")) == NULL)
976 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000977
Damien Millercb5e44a2000-09-29 12:12:36 +1100978 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
979 memset(var_name, '\0', sizeof(var_name));
980 memset(var_val, '\0', sizeof(var_val));
981
982 strncpy(var_name, environ[i], equals - environ[i]);
983 strcpy(var_val, equals + 1);
984
985 debug3("Copy environment: %s=%s", var_name, var_val);
986
987 child_set_env(env, envsize, var_name, var_val);
988 }
989 }
990}
991#endif
992
Damien Miller5fc85652000-07-09 23:53:07 +1000993#if defined(HAVE_GETUSERATTR)
994/*
995 * AIX-specific login initialisation
996 */
997void set_limit(char *user, char *soft, char *hard, int resource, int mult)
998{
999 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +10001000 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +10001001
1002 getrlimit(resource, &rlim);
1003
Damien Miller65964d62000-07-11 09:16:22 +10001004 slim = 0;
1005 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
1006 if (slim < 0) {
1007 rlim.rlim_cur = RLIM_INFINITY;
1008 } else if (slim != 0) {
1009 /* See the wackiness below */
1010 if (rlim.rlim_cur == slim * mult)
1011 slim = 0;
1012 else
1013 rlim.rlim_cur = slim * mult;
1014 }
1015 }
Damien Miller5fc85652000-07-09 23:53:07 +10001016
Damien Miller65964d62000-07-11 09:16:22 +10001017 hlim = 0;
1018 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
1019 if (hlim < 0) {
1020 rlim.rlim_max = RLIM_INFINITY;
1021 } else if (hlim != 0) {
1022 rlim.rlim_max = hlim * mult;
1023 }
1024 }
Damien Miller5fc85652000-07-09 23:53:07 +10001025
Damien Miller65964d62000-07-11 09:16:22 +10001026 /*
1027 * XXX For cpu and fsize the soft limit is set to the hard limit
1028 * if the hard limit is left at its default value and the soft limit
1029 * is changed from its default value, either by requesting it
1030 * (slim == 0) or by setting it to the current default. At least
1031 * that's how rlogind does it. If you're confused you're not alone.
1032 * Bug or feature? AIX 4.3.1.2
1033 */
1034 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
1035 && hlim == 0 && slim != 0)
1036 rlim.rlim_max = rlim.rlim_cur;
1037 /* A specified hard limit limits the soft limit */
1038 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
1039 rlim.rlim_cur = rlim.rlim_max;
1040 /* A soft limit can increase a hard limit */
1041 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +10001042 rlim.rlim_max = rlim.rlim_cur;
1043
1044 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +10001045 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +10001046}
1047
1048void set_limits_from_userattr(char *user)
1049{
1050 int mask;
1051 char buf[16];
1052
1053 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
1054 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
1055 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1056 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1057 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1058 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1059#if defined(S_UNOFILE)
1060 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1061#endif
1062
1063 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1064 /* Convert decimal to octal */
1065 (void) snprintf(buf, sizeof(buf), "%d", mask);
1066 if (sscanf(buf, "%o", &mask) == 1)
1067 umask(mask);
1068 }
1069}
1070#endif /* defined(HAVE_GETUSERATTR) */
1071
Damien Millerb38eff82000-04-01 11:09:21 +10001072/*
1073 * Performs common processing for the child, such as setting up the
1074 * environment, closing extra file descriptors, setting the user and group
1075 * ids, and executing the command or shell.
1076 */
Damien Miller4af51302000-04-16 11:18:38 +10001077void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001078do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001079{
Damien Miller7b28dc52000-09-05 13:34:53 +11001080 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001081 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001082 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001083 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001084 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001085 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001086 char **env;
1087 extern char **environ;
1088 struct stat st;
1089 char *argv[10];
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001090 int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
Damien Miller91606b12000-06-28 08:22:29 +10001091#ifdef WITH_IRIX_PROJECT
1092 prid_t projid;
1093#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001094#ifdef WITH_IRIX_JOBS
1095 jid_t jid = 0;
1096#else
1097#ifdef WITH_IRIX_ARRAY
1098 int jid = 0;
1099#endif /* WITH_IRIX_ARRAY */
1100#endif /* WITH_IRIX_JOBS */
1101
Ben Lindstrom005dd222001-04-18 15:29:33 +00001102 /* remove hostkey from the child's memory */
1103 destroy_sensitive_data();
1104
Damien Millerd3a18572000-06-07 19:55:44 +10001105 /* login(1) is only called if we execute the login shell */
1106 if (options.use_login && command != NULL)
1107 options.use_login = 0;
1108
Damien Miller364a9bd2001-04-16 18:37:05 +10001109#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001110 if (!options.use_login) {
1111# ifdef HAVE_LOGIN_CAP
1112 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1113 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1114 _PATH_NOLOGIN), "r");
1115# else /* HAVE_LOGIN_CAP */
1116 if (pw->pw_uid)
1117 f = fopen(_PATH_NOLOGIN, "r");
1118# endif /* HAVE_LOGIN_CAP */
1119 if (f) {
1120 /* /etc/nologin exists. Print its contents and exit. */
1121 while (fgets(buf, sizeof(buf), f))
1122 fputs(buf, stderr);
1123 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001124 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001125 }
Damien Millerb38eff82000-04-01 11:09:21 +10001126 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001127#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001128
Damien Millerad833b32000-08-23 10:46:23 +10001129 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001130 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1131 switch, so we let login(1) to this for us. */
1132 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001133#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001134 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001135 if (!check_quietlogin(s, command))
1136 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001137#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001138#ifdef HAVE_CYGWIN
1139 if (is_winnt) {
1140#else
Damien Millerb38eff82000-04-01 11:09:21 +10001141 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001142#endif
Damien Millerad833b32000-08-23 10:46:23 +10001143# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001144 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001145# endif /* HAVE_GETUSERATTR */
1146# ifdef HAVE_LOGIN_CAP
1147 if (setusercontext(lc, pw, pw->pw_uid,
1148 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1149 perror("unable to set user context");
1150 exit(1);
1151 }
1152# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001153#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1154 /* Sets login uid for accounting */
1155 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1156 error("setluid: %s", strerror(errno));
1157#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1158
Damien Millerad833b32000-08-23 10:46:23 +10001159 if (setlogin(pw->pw_name) < 0)
1160 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001161 if (setgid(pw->pw_gid) < 0) {
1162 perror("setgid");
1163 exit(1);
1164 }
1165 /* Initialize the group list. */
1166 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1167 perror("initgroups");
1168 exit(1);
1169 }
1170 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001171# ifdef USE_PAM
1172 /*
1173 * PAM credentials may take the form of
1174 * supplementary groups. These will have been
1175 * wiped by the above initgroups() call.
1176 * Reestablish them here.
1177 */
1178 do_pam_setcred(0);
1179# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001180# ifdef WITH_IRIX_JOBS
1181 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1182 if (jid == -1) {
1183 fatal("Failed to create job container: %.100s",
1184 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001185 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001186# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001187# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001188 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001189 if (jid == 0) {
1190 if (newarraysess() != 0)
1191 fatal("Failed to set up new array session: %.100s",
1192 strerror(errno));
1193 }
Damien Millerad833b32000-08-23 10:46:23 +10001194# endif /* WITH_IRIX_ARRAY */
1195# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001196 /* initialize irix project info */
1197 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1198 debug("Failed to get project id, using projid 0");
1199 projid = 0;
1200 }
Damien Miller91606b12000-06-28 08:22:29 +10001201 if (setprid(projid))
1202 fatal("Failed to initialize project %d for %s: %.100s",
1203 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001204# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001205#ifdef WITH_IRIX_AUDIT
1206 if (sysconf(_SC_AUDIT)) {
1207 debug("Setting sat id to %d", (int) pw->pw_uid);
1208 if (satsetid(pw->pw_uid))
1209 debug("error setting satid: %.100s", strerror(errno));
1210 }
1211#endif /* WITH_IRIX_AUDIT */
1212
Damien Miller168a7002001-03-17 10:29:50 +11001213#ifdef _AIX
1214 /*
1215 * AIX has a "usrinfo" area where logname and
1216 * other stuff is stored - a few applications
1217 * actually use this and die if it's not set
1218 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001219 if (s->ttyfd == -1)
1220 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001221 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001222 2 * strlen(pw->pw_name));
1223 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001224 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001225 if (usrinfo(SETUINFO, cp, i) == -1)
1226 fatal("Couldn't set usrinfo: %s",
1227 strerror(errno));
1228 debug3("AIX/UsrInfo: set len %d", i);
1229 xfree(cp);
1230#endif
1231
Damien Millerb38eff82000-04-01 11:09:21 +10001232 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001233 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001234# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001235 }
Damien Millerad833b32000-08-23 10:46:23 +10001236#endif /* HAVE_OSF_SIA */
1237
Damien Millerbac2d8a2000-09-05 16:13:06 +11001238#ifdef HAVE_CYGWIN
1239 if (is_winnt)
1240#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001241 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001242 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001243 }
1244 /*
1245 * Get the shell from the password data. An empty shell field is
1246 * legal, and means /bin/sh.
1247 */
1248 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001249#ifdef HAVE_LOGIN_CAP
1250 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1251#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001252
1253#ifdef AFS
1254 /* Try to get AFS tokens for the local cell. */
1255 if (k_hasafs()) {
1256 char cell[64];
1257
1258 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1259 krb_afslog(cell, 0);
1260
1261 krb_afslog(0, 0);
1262 }
1263#endif /* AFS */
1264
1265 /* Initialize the environment. */
1266 envsize = 100;
1267 env = xmalloc(envsize * sizeof(char *));
1268 env[0] = NULL;
1269
Damien Millerbac2d8a2000-09-05 16:13:06 +11001270#ifdef HAVE_CYGWIN
1271 /*
1272 * The Windows environment contains some setting which are
1273 * important for a running system. They must not be dropped.
1274 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001275 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001276#endif
1277
Damien Millerb38eff82000-04-01 11:09:21 +10001278 if (!options.use_login) {
1279 /* Set basic environment. */
1280 child_set_env(&env, &envsize, "USER", pw->pw_name);
1281 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1282 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001283#ifdef HAVE_LOGIN_CAP
1284 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1285 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001286#else /* HAVE_LOGIN_CAP */
1287# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001288 /*
1289 * There's no standard path on Windows. The path contains
1290 * important components pointing to the system directories,
1291 * needed for loading shared libraries. So the path better
1292 * remains intact here.
1293 */
Damien Millerb38eff82000-04-01 11:09:21 +10001294 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001295# endif /* HAVE_CYGWIN */
1296#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001297
1298 snprintf(buf, sizeof buf, "%.200s/%.50s",
1299 _PATH_MAILDIR, pw->pw_name);
1300 child_set_env(&env, &envsize, "MAIL", buf);
1301
1302 /* Normal systems set SHELL by default. */
1303 child_set_env(&env, &envsize, "SHELL", shell);
1304 }
1305 if (getenv("TZ"))
1306 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1307
1308 /* Set custom environment options from RSA authentication. */
1309 while (custom_environment) {
1310 struct envstring *ce = custom_environment;
1311 char *s = ce->s;
1312 int i;
1313 for (i = 0; s[i] != '=' && s[i]; i++);
1314 if (s[i] == '=') {
1315 s[i] = 0;
1316 child_set_env(&env, &envsize, s, s + i + 1);
1317 }
1318 custom_environment = ce->next;
1319 xfree(ce->s);
1320 xfree(ce);
1321 }
1322
1323 snprintf(buf, sizeof buf, "%.50s %d %d",
1324 get_remote_ipaddr(), get_remote_port(), get_local_port());
1325 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1326
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001327 if (s->ttyfd != -1)
1328 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1329 if (s->term)
1330 child_set_env(&env, &envsize, "TERM", s->term);
1331 if (s->display)
1332 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001333 if (original_command)
1334 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1335 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001336
1337#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001338 if ((cp = getenv("AUTHSTATE")) != NULL)
1339 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1340 if ((cp = getenv("KRB5CCNAME")) != NULL)
1341 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1342 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001343#endif
1344
1345#ifdef KRB4
1346 {
1347 extern char *ticket;
1348
1349 if (ticket)
1350 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1351 }
1352#endif /* KRB4 */
1353
1354#ifdef USE_PAM
1355 /* Pull in any environment variables that may have been set by PAM. */
1356 do_pam_environment(&env, &envsize);
1357#endif /* USE_PAM */
1358
Damien Millerb38eff82000-04-01 11:09:21 +10001359 if (xauthfile)
1360 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1361 if (auth_get_socket_name() != NULL)
1362 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1363 auth_get_socket_name());
1364
1365 /* read $HOME/.ssh/environment. */
1366 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001367 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1368 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001369 read_environment_file(&env, &envsize, buf);
1370 }
1371 if (debug_flag) {
1372 /* dump the environment */
1373 fprintf(stderr, "Environment:\n");
1374 for (i = 0; env[i]; i++)
1375 fprintf(stderr, " %.200s\n", env[i]);
1376 }
Damien Millerad833b32000-08-23 10:46:23 +10001377 /* we have to stash the hostname before we close our socket. */
1378 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001379 hostname = get_remote_name_or_ip(utmp_len,
1380 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001381 /*
1382 * Close the connection descriptors; note that this is the child, and
1383 * the server will still have the socket open, and it is important
1384 * that we do not shutdown it. Note that the descriptors cannot be
1385 * closed before building the environment, as we call
1386 * get_remote_ipaddr there.
1387 */
1388 if (packet_get_connection_in() == packet_get_connection_out())
1389 close(packet_get_connection_in());
1390 else {
1391 close(packet_get_connection_in());
1392 close(packet_get_connection_out());
1393 }
1394 /*
1395 * Close all descriptors related to channels. They will still remain
1396 * open in the parent.
1397 */
1398 /* XXX better use close-on-exec? -markus */
1399 channel_close_all();
1400
1401 /*
1402 * Close any extra file descriptors. Note that there may still be
1403 * descriptors left by system functions. They will be closed later.
1404 */
1405 endpwent();
1406
1407 /*
1408 * Close any extra open file descriptors so that we don\'t have them
1409 * hanging around in clients. Note that we want to do this after
1410 * initgroups, because at least on Solaris 2.3 it leaves file
1411 * descriptors open.
1412 */
1413 for (i = 3; i < 64; i++)
1414 close(i);
1415
1416 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001417 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001418 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1419 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001420#ifdef HAVE_LOGIN_CAP
1421 if (login_getcapbool(lc, "requirehome", 0))
1422 exit(1);
1423#endif
1424 }
Damien Millerb38eff82000-04-01 11:09:21 +10001425
1426 /*
1427 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1428 * xauth are run in the proper environment.
1429 */
1430 environ = env;
1431
1432 /*
1433 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1434 * in this order).
1435 */
1436 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001437 /* ignore _PATH_SSH_USER_RC for subsystems */
1438 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001439 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1440 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001441 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001442 fprintf(stderr, "Running %s\n", cmd);
1443 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001444 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001445 if (do_xauth)
1446 fprintf(f, "%s %s\n", s->auth_proto,
1447 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001448 pclose(f);
1449 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001450 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001451 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001452 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001453 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001454 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1455 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001456 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001457 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001458 if (do_xauth)
1459 fprintf(f, "%s %s\n", s->auth_proto,
1460 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001461 pclose(f);
1462 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001463 fprintf(stderr, "Could not run %s\n",
1464 _PATH_SSH_SYSTEM_RC);
1465 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001466 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001467 char *screen = strchr(s->display, ':');
1468
1469 if (debug_flag) {
1470 fprintf(stderr,
1471 "Running %.100s add "
1472 "%.100s %.100s %.100s\n",
1473 options.xauth_location, s->display,
1474 s->auth_proto, s->auth_data);
1475 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001476 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001477 "Adding %.*s/unix%s %s %s\n",
1478 (int)(screen - s->display),
1479 s->display, screen,
1480 s->auth_proto, s->auth_data);
1481 }
1482 snprintf(cmd, sizeof cmd, "%s -q -",
1483 options.xauth_location);
1484 f = popen(cmd, "w");
1485 if (f) {
1486 fprintf(f, "add %s %s %s\n", s->display,
1487 s->auth_proto, s->auth_data);
1488 if (screen != NULL)
1489 fprintf(f, "add %.*s/unix%s %s %s\n",
1490 (int)(screen - s->display),
1491 s->display, screen,
1492 s->auth_proto,
1493 s->auth_data);
1494 pclose(f);
1495 } else {
1496 fprintf(stderr, "Could not run %s\n",
1497 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001498 }
1499 }
Damien Millerb38eff82000-04-01 11:09:21 +10001500 /* Get the last component of the shell name. */
1501 cp = strrchr(shell, '/');
1502 if (cp)
1503 cp++;
1504 else
1505 cp = shell;
1506 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001507
1508 /* restore SIGPIPE for child */
1509 signal(SIGPIPE, SIG_DFL);
1510
Damien Millerb38eff82000-04-01 11:09:21 +10001511 /*
1512 * If we have no command, execute the shell. In this case, the shell
1513 * name to be passed in argv[0] is preceded by '-' to indicate that
1514 * this is a login shell.
1515 */
1516 if (!command) {
1517 if (!options.use_login) {
1518 char buf[256];
1519
1520 /*
1521 * Check for mail if we have a tty and it was enabled
1522 * in server options.
1523 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001524 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001525 char *mailbox;
1526 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001527
Damien Millerb38eff82000-04-01 11:09:21 +10001528 mailbox = getenv("MAIL");
1529 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001530 if (stat(mailbox, &mailstat) != 0 ||
1531 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001532 printf("No mail.\n");
1533 else if (mailstat.st_mtime < mailstat.st_atime)
1534 printf("You have mail.\n");
1535 else
1536 printf("You have new mail.\n");
1537 }
1538 }
1539 /* Start the shell. Set initial character to '-'. */
1540 buf[0] = '-';
1541 strncpy(buf + 1, cp, sizeof(buf) - 1);
1542 buf[sizeof(buf) - 1] = 0;
1543
1544 /* Execute the shell. */
1545 argv[0] = buf;
1546 argv[1] = NULL;
1547 execve(shell, argv, env);
1548
1549 /* Executing the shell failed. */
1550 perror(shell);
1551 exit(1);
1552
1553 } else {
1554 /* Launch login(1). */
1555
Damien Millerad833b32000-08-23 10:46:23 +10001556 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001557#ifdef LOGIN_NEEDS_TERM
1558 s->term? s->term : "unknown",
1559#endif
Damien Miller942da032000-08-18 13:59:06 +10001560 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001561
1562 /* Login couldn't be executed, die. */
1563
1564 perror("login");
1565 exit(1);
1566 }
1567 }
1568 /*
1569 * Execute the command using the user's shell. This uses the -c
1570 * option to execute the command.
1571 */
1572 argv[0] = (char *) cp;
1573 argv[1] = "-c";
1574 argv[2] = (char *) command;
1575 argv[3] = NULL;
1576 execve(shell, argv, env);
1577 perror(shell);
1578 exit(1);
1579}
1580
1581Session *
1582session_new(void)
1583{
1584 int i;
1585 static int did_init = 0;
1586 if (!did_init) {
1587 debug("session_new: init");
1588 for(i = 0; i < MAX_SESSIONS; i++) {
1589 sessions[i].used = 0;
1590 sessions[i].self = i;
1591 }
1592 did_init = 1;
1593 }
1594 for(i = 0; i < MAX_SESSIONS; i++) {
1595 Session *s = &sessions[i];
1596 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001597 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001598 s->chanid = -1;
1599 s->ptyfd = -1;
1600 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001601 s->used = 1;
Damien Miller15b29522000-10-14 12:33:48 +11001602 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001603 return s;
1604 }
1605 }
1606 return NULL;
1607}
1608
1609void
1610session_dump(void)
1611{
1612 int i;
1613 for(i = 0; i < MAX_SESSIONS; i++) {
1614 Session *s = &sessions[i];
1615 debug("dump: used %d session %d %p channel %d pid %d",
1616 s->used,
1617 s->self,
1618 s,
1619 s->chanid,
1620 s->pid);
1621 }
1622}
1623
Damien Millerefb4afe2000-04-12 18:45:05 +10001624int
1625session_open(int chanid)
1626{
1627 Session *s = session_new();
1628 debug("session_open: channel %d", chanid);
1629 if (s == NULL) {
1630 error("no more sessions");
1631 return 0;
1632 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001633 s->pw = auth_get_user();
1634 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001635 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001636 debug("session_open: session %d: link with channel %d", s->self, chanid);
1637 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001638 return 1;
1639}
1640
1641Session *
1642session_by_channel(int id)
1643{
1644 int i;
1645 for(i = 0; i < MAX_SESSIONS; i++) {
1646 Session *s = &sessions[i];
1647 if (s->used && s->chanid == id) {
1648 debug("session_by_channel: session %d channel %d", i, id);
1649 return s;
1650 }
1651 }
1652 debug("session_by_channel: unknown channel %d", id);
1653 session_dump();
1654 return NULL;
1655}
1656
1657Session *
1658session_by_pid(pid_t pid)
1659{
1660 int i;
1661 debug("session_by_pid: pid %d", pid);
1662 for(i = 0; i < MAX_SESSIONS; i++) {
1663 Session *s = &sessions[i];
1664 if (s->used && s->pid == pid)
1665 return s;
1666 }
1667 error("session_by_pid: unknown pid %d", pid);
1668 session_dump();
1669 return NULL;
1670}
1671
1672int
1673session_window_change_req(Session *s)
1674{
1675 s->col = packet_get_int();
1676 s->row = packet_get_int();
1677 s->xpixel = packet_get_int();
1678 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001679 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001680 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1681 return 1;
1682}
1683
1684int
1685session_pty_req(Session *s)
1686{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001687 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001688 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001689
Damien Millerf6d9e222000-06-18 14:50:44 +10001690 if (no_pty_flag)
1691 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001692 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001693 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001694 s->term = packet_get_string(&len);
1695 s->col = packet_get_int();
1696 s->row = packet_get_int();
1697 s->xpixel = packet_get_int();
1698 s->ypixel = packet_get_int();
1699
1700 if (strcmp(s->term, "") == 0) {
1701 xfree(s->term);
1702 s->term = NULL;
1703 }
1704 /* Allocate a pty and open it. */
1705 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1706 xfree(s->term);
1707 s->term = NULL;
1708 s->ptyfd = -1;
1709 s->ttyfd = -1;
1710 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001711 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001712 }
1713 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1714 /*
1715 * Add a cleanup function to clear the utmp entry and record logout
1716 * time in case we call fatal() (e.g., the connection gets closed).
1717 */
1718 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1719 pty_setowner(s->pw, s->tty);
1720 /* Get window size from the packet. */
1721 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1722
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001723 /* Get tty modes from the packet. */
1724 tty_parse_modes(s->ttyfd, &n_bytes);
1725 packet_done();
1726
Damien Millere247cc42000-05-07 12:03:14 +10001727 session_proctitle(s);
1728
Damien Millerefb4afe2000-04-12 18:45:05 +10001729 return 1;
1730}
1731
Damien Millerbd483e72000-04-30 10:00:53 +10001732int
1733session_subsystem_req(Session *s)
1734{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001735 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001736 int success = 0;
1737 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001738 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001739
1740 packet_done();
1741 log("subsystem request for %s", subsys);
1742
Damien Millerf6d9e222000-06-18 14:50:44 +10001743 for (i = 0; i < options.num_subsystems; i++) {
1744 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1745 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001746 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001747 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001748 success = 1;
1749 }
1750 }
1751
1752 if (!success)
1753 log("subsystem request for %s failed, subsystem not found", subsys);
1754
Damien Millerbd483e72000-04-30 10:00:53 +10001755 xfree(subsys);
1756 return success;
1757}
1758
1759int
1760session_x11_req(Session *s)
1761{
Damien Miller7b28dc52000-09-05 13:34:53 +11001762 int fd;
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +00001763 struct stat st;
Damien Miller37023962000-07-11 17:31:38 +10001764 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001765 debug("X11 forwarding disabled in user configuration file.");
1766 return 0;
1767 }
Damien Millerbd483e72000-04-30 10:00:53 +10001768 if (!options.x11_forwarding) {
1769 debug("X11 forwarding disabled in server configuration file.");
1770 return 0;
1771 }
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +00001772 if (!options.xauth_location ||
1773 (stat(options.xauth_location, &st) == -1)) {
1774 packet_send_debug("No xauth program; cannot forward with spoofing.");
1775 return 0;
1776 }
Damien Millerbd483e72000-04-30 10:00:53 +10001777 if (xauthfile != NULL) {
1778 debug("X11 fwd already started.");
1779 return 0;
1780 }
1781
1782 debug("Received request for X11 forwarding with auth spoofing.");
1783 if (s->display != NULL)
1784 packet_disconnect("Protocol error: X11 display already set.");
1785
1786 s->single_connection = packet_get_char();
1787 s->auth_proto = packet_get_string(NULL);
1788 s->auth_data = packet_get_string(NULL);
1789 s->screen = packet_get_int();
1790 packet_done();
1791
1792 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1793 if (s->display == NULL) {
1794 xfree(s->auth_proto);
1795 xfree(s->auth_data);
1796 return 0;
1797 }
1798 xauthfile = xmalloc(MAXPATHLEN);
1799 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001800 temporarily_use_uid(s->pw);
Damien Millerbd483e72000-04-30 10:00:53 +10001801 if (mkdtemp(xauthfile) == NULL) {
1802 restore_uid();
1803 error("private X11 dir: mkdtemp %s failed: %s",
1804 xauthfile, strerror(errno));
1805 xfree(xauthfile);
1806 xauthfile = NULL;
1807 xfree(s->auth_proto);
1808 xfree(s->auth_data);
1809 /* XXXX remove listening channels */
1810 return 0;
1811 }
1812 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001813 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1814 if (fd >= 0)
1815 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001816 restore_uid();
Ben Lindstrom983c0982001-06-09 01:20:06 +00001817 fatal_add_cleanup(xauthfile_cleanup_proc, s->pw);
Damien Millerbd483e72000-04-30 10:00:53 +10001818 return 1;
1819}
1820
Damien Millerf6d9e222000-06-18 14:50:44 +10001821int
1822session_shell_req(Session *s)
1823{
1824 /* if forced_command == NULL, the shell is execed */
1825 char *shell = forced_command;
1826 packet_done();
Damien Millerf6d9e222000-06-18 14:50:44 +10001827 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001828 do_exec_no_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001829 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001830 do_exec_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001831 return 1;
1832}
1833
1834int
1835session_exec_req(Session *s)
1836{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001837 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001838 char *command = packet_get_string(&len);
1839 packet_done();
1840 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001841 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001842 command = forced_command;
1843 debug("Forced command '%.500s'", forced_command);
1844 }
Damien Millerf6d9e222000-06-18 14:50:44 +10001845 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001846 do_exec_no_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001847 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001848 do_exec_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001849 if (forced_command == NULL)
1850 xfree(command);
1851 return 1;
1852}
1853
Damien Miller0bc1bd82000-11-13 22:57:25 +11001854int
1855session_auth_agent_req(Session *s)
1856{
1857 static int called = 0;
1858 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001859 if (no_agent_forwarding_flag) {
1860 debug("session_auth_agent_req: no_agent_forwarding_flag");
1861 return 0;
1862 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001863 if (called) {
1864 return 0;
1865 } else {
1866 called = 1;
1867 return auth_input_request_forwarding(s->pw);
1868 }
1869}
1870
Damien Millerefb4afe2000-04-12 18:45:05 +10001871void
1872session_input_channel_req(int id, void *arg)
1873{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001874 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001875 int reply;
1876 int success = 0;
1877 char *rtype;
1878 Session *s;
1879 Channel *c;
1880
1881 rtype = packet_get_string(&len);
1882 reply = packet_get_char();
1883
1884 s = session_by_channel(id);
1885 if (s == NULL)
1886 fatal("session_input_channel_req: channel %d: no session", id);
1887 c = channel_lookup(id);
1888 if (c == NULL)
1889 fatal("session_input_channel_req: channel %d: bad channel", id);
1890
1891 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1892 s->self, id, rtype, reply);
1893
1894 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001895 * a session is in LARVAL state until a shell, a command
1896 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001897 */
1898 if (c->type == SSH_CHANNEL_LARVAL) {
1899 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001900 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001901 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001902 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001903 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001904 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001905 } else if (strcmp(rtype, "x11-req") == 0) {
1906 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001907 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1908 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001909 } else if (strcmp(rtype, "subsystem") == 0) {
1910 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001911 }
1912 }
1913 if (strcmp(rtype, "window-change") == 0) {
1914 success = session_window_change_req(s);
1915 }
1916
1917 if (reply) {
1918 packet_start(success ?
1919 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1920 packet_put_int(c->remote_id);
1921 packet_send();
1922 }
1923 xfree(rtype);
1924}
1925
1926void
1927session_set_fds(Session *s, int fdin, int fdout, int fderr)
1928{
1929 if (!compat20)
1930 fatal("session_set_fds: called for proto != 2.0");
1931 /*
1932 * now that have a child and a pipe to the child,
1933 * we can activate our channel and register the fd's
1934 */
1935 if (s->chanid == -1)
1936 fatal("no channel for session %d", s->self);
1937 channel_set_fds(s->chanid,
1938 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001939 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1940 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001941}
1942
Damien Millerb38eff82000-04-01 11:09:21 +10001943void
1944session_pty_cleanup(Session *s)
1945{
1946 if (s == NULL || s->ttyfd == -1)
1947 return;
1948
Kevin Steves43cdef32001-02-11 14:12:08 +00001949 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001950
1951 /* Cancel the cleanup function. */
1952 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1953
1954 /* Record that the user has logged out. */
1955 record_logout(s->pid, s->tty);
1956
1957 /* Release the pseudo-tty. */
1958 pty_release(s->tty);
1959
1960 /*
1961 * Close the server side of the socket pairs. We must do this after
1962 * the pty cleanup, so that another process doesn't get this pty
1963 * while we're still cleaning up.
1964 */
1965 if (close(s->ptymaster) < 0)
1966 error("close(s->ptymaster): %s", strerror(errno));
1967}
Damien Millerefb4afe2000-04-12 18:45:05 +10001968
1969void
1970session_exit_message(Session *s, int status)
1971{
1972 Channel *c;
1973 if (s == NULL)
1974 fatal("session_close: no session");
1975 c = channel_lookup(s->chanid);
1976 if (c == NULL)
1977 fatal("session_close: session %d: no channel %d",
1978 s->self, s->chanid);
1979 debug("session_exit_message: session %d channel %d pid %d",
1980 s->self, s->chanid, s->pid);
1981
1982 if (WIFEXITED(status)) {
1983 channel_request_start(s->chanid,
1984 "exit-status", 0);
1985 packet_put_int(WEXITSTATUS(status));
1986 packet_send();
1987 } else if (WIFSIGNALED(status)) {
1988 channel_request_start(s->chanid,
1989 "exit-signal", 0);
1990 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001991#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001992 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001993#else /* WCOREDUMP */
1994 packet_put_char(0);
1995#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001996 packet_put_cstring("");
1997 packet_put_cstring("");
1998 packet_send();
1999 } else {
2000 /* Some weird exit cause. Just exit. */
2001 packet_disconnect("wait returned status %04x.", status);
2002 }
2003
2004 /* disconnect channel */
2005 debug("session_exit_message: release channel %d", s->chanid);
2006 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10002007 /*
2008 * emulate a write failure with 'chan_write_failed', nobody will be
2009 * interested in data we write.
2010 * Note that we must not call 'chan_read_failed', since there could
2011 * be some more data waiting in the pipe.
2012 */
Damien Millerbd483e72000-04-30 10:00:53 +10002013 if (c->ostate != CHAN_OUTPUT_CLOSED)
2014 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002015 s->chanid = -1;
2016}
2017
2018void
2019session_free(Session *s)
2020{
2021 debug("session_free: session %d pid %d", s->self, s->pid);
2022 if (s->term)
2023 xfree(s->term);
2024 if (s->display)
2025 xfree(s->display);
2026 if (s->auth_data)
2027 xfree(s->auth_data);
2028 if (s->auth_proto)
2029 xfree(s->auth_proto);
2030 s->used = 0;
2031}
2032
2033void
2034session_close(Session *s)
2035{
2036 session_pty_cleanup(s);
2037 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10002038 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002039}
2040
2041void
2042session_close_by_pid(pid_t pid, int status)
2043{
2044 Session *s = session_by_pid(pid);
2045 if (s == NULL) {
2046 debug("session_close_by_pid: no session for pid %d", s->pid);
2047 return;
2048 }
2049 if (s->chanid != -1)
2050 session_exit_message(s, status);
2051 session_close(s);
2052}
2053
2054/*
2055 * this is called when a channel dies before
2056 * the session 'child' itself dies
2057 */
2058void
2059session_close_by_channel(int id, void *arg)
2060{
2061 Session *s = session_by_channel(id);
2062 if (s == NULL) {
2063 debug("session_close_by_channel: no session for channel %d", id);
2064 return;
2065 }
2066 /* disconnect channel */
2067 channel_cancel_cleanup(s->chanid);
2068 s->chanid = -1;
2069
2070 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
2071 if (s->pid == 0) {
2072 /* close session immediately */
2073 session_close(s);
2074 } else {
2075 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10002076 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10002077 error("session_close_by_channel: kill %d: %s",
2078 s->pid, strerror(errno));
2079 }
2080}
2081
Damien Millere247cc42000-05-07 12:03:14 +10002082char *
2083session_tty_list(void)
2084{
2085 static char buf[1024];
2086 int i;
2087 buf[0] = '\0';
2088 for(i = 0; i < MAX_SESSIONS; i++) {
2089 Session *s = &sessions[i];
2090 if (s->used && s->ttyfd != -1) {
2091 if (buf[0] != '\0')
2092 strlcat(buf, ",", sizeof buf);
2093 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2094 }
2095 }
2096 if (buf[0] == '\0')
2097 strlcpy(buf, "notty", sizeof buf);
2098 return buf;
2099}
2100
2101void
2102session_proctitle(Session *s)
2103{
2104 if (s->pw == NULL)
2105 error("no user for session %d", s->self);
2106 else
2107 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2108}
2109
Damien Millerefb4afe2000-04-12 18:45:05 +10002110void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002111do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002112{
Ben Lindstromb31783d2001-03-22 02:02:12 +00002113
Damien Millerefb4afe2000-04-12 18:45:05 +10002114 server_loop2();
2115}