blob: 0d7b8fa36ef38c89123057d155dcea2f70821f16 [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 Lindstrom49c12602001-06-13 04:37:36 +000036RCSID("$OpenBSD: session.c,v 1.86 2001/06/12 16:10:38 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);
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +0000124void session_pty_cleanup(void *session);
Ben Lindstrom49c12602001-06-13 04:37:36 +0000125int session_pty_req(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000126void session_proctitle(Session *s);
Ben Lindstrom768176b2001-06-09 01:29:12 +0000127int session_setup_x11fwd(Session *s);
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000128void session_close(Session *s);
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000129void do_exec_pty(Session *s, const char *command);
130void do_exec_no_pty(Session *s, const char *command);
Damien Miller69b69aa2000-10-28 14:19:58 +1100131void do_login(Session *s, const char *command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000132#ifdef LOGIN_NEEDS_UTMPX
133void do_pre_login(Session *s);
134#endif
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000135void do_child(Session *s, const char *command);
Damien Millercf205e82001-04-16 18:29:15 +1000136void do_motd(void);
137int check_quietlogin(Session *s, const char *command);
Ben Lindstrom983c0982001-06-09 01:20:06 +0000138void xauthfile_cleanup_proc(void *pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000139
Ben Lindstromb31783d2001-03-22 02:02:12 +0000140void do_authenticated1(Authctxt *authctxt);
141void do_authenticated2(Authctxt *authctxt);
142
Damien Millerb38eff82000-04-01 11:09:21 +1000143/* import */
144extern ServerOptions options;
145extern char *__progname;
146extern int log_stderr;
147extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000148extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000149extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000150extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000151
Damien Millerb38eff82000-04-01 11:09:21 +1000152/* Local Xauthority file. */
153static char *xauthfile;
154
Damien Miller7b28dc52000-09-05 13:34:53 +1100155/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000156char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100157
Damien Millerb38eff82000-04-01 11:09:21 +1000158/* data */
159#define MAX_SESSIONS 10
160Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100161
Damien Millerd2c208a2000-05-17 22:00:02 +1000162#ifdef WITH_AIXAUTHENTICATE
163/* AIX's lastlogin message, set in auth1.c */
164char *aixloginmsg;
165#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000166
Damien Millerad833b32000-08-23 10:46:23 +1000167#ifdef HAVE_LOGIN_CAP
168static login_cap_t *lc;
169#endif
170
Ben Lindstromb31783d2001-03-22 02:02:12 +0000171void
172do_authenticated(Authctxt *authctxt)
173{
174 /*
175 * Cancel the alarm we set to limit the time taken for
176 * authentication.
177 */
178 alarm(0);
179 if (startup_pipe != -1) {
180 close(startup_pipe);
181 startup_pipe = -1;
182 }
183#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
184 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
185 error("unable to get login class");
186 return;
187 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000188#ifdef BSD_AUTH
189 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
190 packet_disconnect("Approval failure for %s",
191 authctxt->pw->pw_name);
192 }
193#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000194#endif
195 /* setup the channel layer */
196 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
197 channel_permit_all_opens();
198
199 if (compat20)
200 do_authenticated2(authctxt);
201 else
202 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000203
204 /* remote user's local Xauthority file and agent socket */
205 if (xauthfile)
Ben Lindstrom983c0982001-06-09 01:20:06 +0000206 xauthfile_cleanup_proc(authctxt->pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000207 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000208 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000209}
210
Damien Millerb38eff82000-04-01 11:09:21 +1000211/*
212 * Remove local Xauthority file.
213 */
214void
Ben Lindstrom983c0982001-06-09 01:20:06 +0000215xauthfile_cleanup_proc(void *_pw)
Damien Millerb38eff82000-04-01 11:09:21 +1000216{
Ben Lindstrom983c0982001-06-09 01:20:06 +0000217 struct passwd *pw = _pw;
218 char *p;
Damien Millerb38eff82000-04-01 11:09:21 +1000219
Ben Lindstrom983c0982001-06-09 01:20:06 +0000220 debug("xauthfile_cleanup_proc called");
Damien Millerb38eff82000-04-01 11:09:21 +1000221 if (xauthfile != NULL) {
Ben Lindstrom983c0982001-06-09 01:20:06 +0000222 temporarily_use_uid(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000223 unlink(xauthfile);
224 p = strrchr(xauthfile, '/');
225 if (p != NULL) {
226 *p = '\0';
227 rmdir(xauthfile);
228 }
229 xfree(xauthfile);
230 xauthfile = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +0000231 restore_uid();
Damien Millerb38eff82000-04-01 11:09:21 +1000232 }
233}
234
235/*
Damien Millerb38eff82000-04-01 11:09:21 +1000236 * Prepares for an interactive session. This is called after the user has
237 * been successfully authenticated. During this message exchange, pseudo
238 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
239 * are requested, etc.
240 */
Damien Miller4af51302000-04-16 11:18:38 +1000241void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000242do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000243{
244 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000245 char *command;
Ben Lindstrom49c12602001-06-13 04:37:36 +0000246 int success, type, plen, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000247 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000248 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000249
250 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000251 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000252
Damien Millerb38eff82000-04-01 11:09:21 +1000253 /*
254 * We stay in this loop until the client requests to execute a shell
255 * or a command.
256 */
257 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000258 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000259
260 /* Get a packet from the client. */
261 type = packet_read(&plen);
262
263 /* Process the packet. */
264 switch (type) {
265 case SSH_CMSG_REQUEST_COMPRESSION:
266 packet_integrity_check(plen, 4, type);
267 compression_level = packet_get_int();
268 if (compression_level < 1 || compression_level > 9) {
269 packet_send_debug("Received illegal compression level %d.",
270 compression_level);
271 break;
272 }
273 /* Enable compression after we have responded with SUCCESS. */
274 enable_compression_after_reply = 1;
275 success = 1;
276 break;
277
278 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000279 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000280 break;
281
282 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000283 s->auth_proto = packet_get_string(&proto_len);
284 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000285
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000286 screen_flag = packet_get_protocol_flags() &
287 SSH_PROTOFLAG_SCREEN_NUMBER;
288 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
289
290 if (packet_remaining() == 4) {
291 if (!screen_flag)
292 debug2("Buggy client: "
293 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000294 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000295 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000296 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000297 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000298 packet_done();
299 success = session_setup_x11fwd(s);
300 if (!success) {
301 xfree(s->auth_proto);
302 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000303 s->auth_proto = NULL;
304 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000305 }
Damien Millerb38eff82000-04-01 11:09:21 +1000306 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000307
308 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
309 if (no_agent_forwarding_flag || compat13) {
310 debug("Authentication agent forwarding not permitted for this authentication.");
311 break;
312 }
313 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000314 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000315 break;
316
317 case SSH_CMSG_PORT_FORWARD_REQUEST:
318 if (no_port_forwarding_flag) {
319 debug("Port forwarding not permitted for this authentication.");
320 break;
321 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100322 if (!options.allow_tcp_forwarding) {
323 debug("Port forwarding not permitted.");
324 break;
325 }
Damien Millerb38eff82000-04-01 11:09:21 +1000326 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000327 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000328 success = 1;
329 break;
330
331 case SSH_CMSG_MAX_PACKET_SIZE:
332 if (packet_set_maxsize(packet_get_int()) > 0)
333 success = 1;
334 break;
335
336 case SSH_CMSG_EXEC_SHELL:
337 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000338 if (type == SSH_CMSG_EXEC_CMD) {
339 command = packet_get_string(&dlen);
340 debug("Exec command '%.500s'", command);
341 packet_integrity_check(plen, 4 + dlen, type);
342 } else {
343 command = NULL;
344 packet_integrity_check(plen, 0, type);
345 }
346 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100347 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000348 command = forced_command;
349 debug("Forced command '%.500s'", forced_command);
350 }
Ben Lindstrom49c12602001-06-13 04:37:36 +0000351 if (s->ttyfd != -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000352 do_exec_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000353 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000354 do_exec_no_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000355 if (command != NULL)
356 xfree(command);
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000357 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000358 return;
359
360 default:
361 /*
362 * Any unknown messages in this phase are ignored,
363 * and a failure message is returned.
364 */
365 log("Unknown packet type received after authentication: %d", type);
366 }
367 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
368 packet_send();
369 packet_write_wait();
370
371 /* Enable compression now that we have replied if appropriate. */
372 if (enable_compression_after_reply) {
373 enable_compression_after_reply = 0;
374 packet_start_compression(compression_level);
375 }
376 }
377}
378
379/*
380 * This is called to fork and execute a command when we have no tty. This
381 * will call do_child from the child, and server_loop from the parent after
382 * setting up file descriptors and such.
383 */
Damien Miller4af51302000-04-16 11:18:38 +1000384void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000385do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000386{
387 int pid;
388
389#ifdef USE_PIPES
390 int pin[2], pout[2], perr[2];
391 /* Allocate pipes for communicating with the program. */
392 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
393 packet_disconnect("Could not create pipes: %.100s",
394 strerror(errno));
395#else /* USE_PIPES */
396 int inout[2], err[2];
397 /* Uses socket pairs to communicate with the program. */
398 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
399 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
400 packet_disconnect("Could not create socket pairs: %.100s",
401 strerror(errno));
402#endif /* USE_PIPES */
403 if (s == NULL)
404 fatal("do_exec_no_pty: no session");
405
Damien Millere247cc42000-05-07 12:03:14 +1000406 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000407
Damien Millerc5946332001-02-28 11:46:11 +1100408#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000409 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000410#endif /* USE_PAM */
411
Damien Millerb38eff82000-04-01 11:09:21 +1000412 /* Fork the child. */
413 if ((pid = fork()) == 0) {
414 /* Child. Reinitialize the log since the pid has changed. */
415 log_init(__progname, options.log_level, options.log_facility, log_stderr);
416
417 /*
418 * Create a new session and process group since the 4.4BSD
419 * setlogin() affects the entire process group.
420 */
421 if (setsid() < 0)
422 error("setsid failed: %.100s", strerror(errno));
423
424#ifdef USE_PIPES
425 /*
426 * Redirect stdin. We close the parent side of the socket
427 * pair, and make the child side the standard input.
428 */
429 close(pin[1]);
430 if (dup2(pin[0], 0) < 0)
431 perror("dup2 stdin");
432 close(pin[0]);
433
434 /* Redirect stdout. */
435 close(pout[0]);
436 if (dup2(pout[1], 1) < 0)
437 perror("dup2 stdout");
438 close(pout[1]);
439
440 /* Redirect stderr. */
441 close(perr[0]);
442 if (dup2(perr[1], 2) < 0)
443 perror("dup2 stderr");
444 close(perr[1]);
445#else /* USE_PIPES */
446 /*
447 * Redirect stdin, stdout, and stderr. Stdin and stdout will
448 * use the same socket, as some programs (particularly rdist)
449 * seem to depend on it.
450 */
451 close(inout[1]);
452 close(err[1]);
453 if (dup2(inout[0], 0) < 0) /* stdin */
454 perror("dup2 stdin");
455 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
456 perror("dup2 stdout");
457 if (dup2(err[0], 2) < 0) /* stderr */
458 perror("dup2 stderr");
459#endif /* USE_PIPES */
460
461 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000462 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000463 /* NOTREACHED */
464 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100465#ifdef HAVE_CYGWIN
466 if (is_winnt)
467 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
468#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000469 if (pid < 0)
470 packet_disconnect("fork failed: %.100s", strerror(errno));
471 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000472 /* Set interactive/non-interactive mode. */
473 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000474#ifdef USE_PIPES
475 /* We are the parent. Close the child sides of the pipes. */
476 close(pin[0]);
477 close(pout[1]);
478 close(perr[1]);
479
Damien Millerefb4afe2000-04-12 18:45:05 +1000480 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000481 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000482 } else {
483 /* Enter the interactive session. */
484 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000485 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000486 }
Damien Millerb38eff82000-04-01 11:09:21 +1000487#else /* USE_PIPES */
488 /* We are the parent. Close the child sides of the socket pairs. */
489 close(inout[0]);
490 close(err[0]);
491
492 /*
493 * Enter the interactive session. Note: server_loop must be able to
494 * handle the case that fdin and fdout are the same.
495 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000496 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000497 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000498 } else {
499 server_loop(pid, inout[1], inout[1], err[1]);
500 /* server_loop has closed inout[1] and err[1]. */
501 }
Damien Millerb38eff82000-04-01 11:09:21 +1000502#endif /* USE_PIPES */
503}
504
505/*
506 * This is called to fork and execute a command when we have a tty. This
507 * will call do_child from the child, and server_loop from the parent after
508 * setting up file descriptors, controlling tty, updating wtmp, utmp,
509 * lastlog, and other such operations.
510 */
Damien Miller4af51302000-04-16 11:18:38 +1000511void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000512do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000513{
Damien Millerb38eff82000-04-01 11:09:21 +1000514 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000515 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000516
517 if (s == NULL)
518 fatal("do_exec_pty: no session");
519 ptyfd = s->ptyfd;
520 ttyfd = s->ttyfd;
521
Damien Millerc5946332001-02-28 11:46:11 +1100522#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000523 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000524 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100525#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000526
Damien Millerb38eff82000-04-01 11:09:21 +1000527 /* Fork the child. */
528 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000529 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000530 log_init(__progname, options.log_level, options.log_facility, log_stderr);
531
532 /* Close the master side of the pseudo tty. */
533 close(ptyfd);
534
535 /* Make the pseudo tty our controlling tty. */
536 pty_make_controlling_tty(&ttyfd, s->tty);
537
538 /* Redirect stdin from the pseudo tty. */
539 if (dup2(ttyfd, fileno(stdin)) < 0)
540 error("dup2 stdin failed: %.100s", strerror(errno));
541
542 /* Redirect stdout to the pseudo tty. */
543 if (dup2(ttyfd, fileno(stdout)) < 0)
544 error("dup2 stdin failed: %.100s", strerror(errno));
545
546 /* Redirect stderr to the pseudo tty. */
547 if (dup2(ttyfd, fileno(stderr)) < 0)
548 error("dup2 stdin failed: %.100s", strerror(errno));
549
550 /* Close the extra descriptor for the pseudo tty. */
551 close(ttyfd);
552
Damien Miller942da032000-08-18 13:59:06 +1000553 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000554#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100555 if (!(options.use_login && command == NULL))
556 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000557# ifdef LOGIN_NEEDS_UTMPX
558 else
559 do_pre_login(s);
560# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000561#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000562
Damien Millerb38eff82000-04-01 11:09:21 +1000563 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000564 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000565 /* NOTREACHED */
566 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100567#ifdef HAVE_CYGWIN
568 if (is_winnt)
569 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
570#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000571 if (pid < 0)
572 packet_disconnect("fork failed: %.100s", strerror(errno));
573 s->pid = pid;
574
575 /* Parent. Close the slave side of the pseudo tty. */
576 close(ttyfd);
577
578 /*
579 * Create another descriptor of the pty master side for use as the
580 * standard input. We could use the original descriptor, but this
581 * simplifies code in server_loop. The descriptor is bidirectional.
582 */
583 fdout = dup(ptyfd);
584 if (fdout < 0)
585 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
586
587 /* we keep a reference to the pty master */
588 ptymaster = dup(ptyfd);
589 if (ptymaster < 0)
590 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
591 s->ptymaster = ptymaster;
592
593 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000594 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000595 if (compat20) {
596 session_set_fds(s, ptyfd, fdout, -1);
597 } else {
598 server_loop(pid, ptyfd, fdout, -1);
599 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000600 }
Damien Millerb38eff82000-04-01 11:09:21 +1000601}
602
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000603#ifdef LOGIN_NEEDS_UTMPX
604void
605do_pre_login(Session *s)
606{
607 socklen_t fromlen;
608 struct sockaddr_storage from;
609 pid_t pid = getpid();
610
611 /*
612 * Get IP address of client. If the connection is not a socket, let
613 * the address be 0.0.0.0.
614 */
615 memset(&from, 0, sizeof(from));
616 if (packet_connection_is_on_socket()) {
617 fromlen = sizeof(from);
618 if (getpeername(packet_get_connection_in(),
619 (struct sockaddr *) & from, &fromlen) < 0) {
620 debug("getpeername: %.100s", strerror(errno));
621 fatal_cleanup();
622 }
623 }
624
625 record_utmp_only(pid, s->tty, s->pw->pw_name,
626 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
627 (struct sockaddr *)&from);
628}
629#endif
630
Damien Miller942da032000-08-18 13:59:06 +1000631/* administrative, login(1)-like work */
632void
Damien Miller69b69aa2000-10-28 14:19:58 +1100633do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000634{
Damien Miller942da032000-08-18 13:59:06 +1000635 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100636 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000637 socklen_t fromlen;
638 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000639 time_t last_login_time;
640 struct passwd * pw = s->pw;
641 pid_t pid = getpid();
642
643 /*
644 * Get IP address of client. If the connection is not a socket, let
645 * the address be 0.0.0.0.
646 */
647 memset(&from, 0, sizeof(from));
648 if (packet_connection_is_on_socket()) {
649 fromlen = sizeof(from);
650 if (getpeername(packet_get_connection_in(),
651 (struct sockaddr *) & from, &fromlen) < 0) {
652 debug("getpeername: %.100s", strerror(errno));
653 fatal_cleanup();
654 }
655 }
656
Damien Miller7b28dc52000-09-05 13:34:53 +1100657 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000658 if (options.print_lastlog) {
659 hostname[0] = '\0';
660 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
661 hostname, sizeof(hostname));
662 }
Damien Millere4340be2000-09-16 13:29:08 +1100663
Damien Miller942da032000-08-18 13:59:06 +1000664 /* Record that there was a login on that tty from the remote host. */
665 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000666 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
667 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000668
Kevin Steves092f2ef2000-10-14 13:36:13 +0000669#ifdef USE_PAM
670 /*
671 * If password change is needed, do it now.
672 * This needs to occur before the ~/.hushlogin check.
673 */
Damien Miller646aa602001-02-15 11:51:32 +1100674 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000675 print_pam_messages();
676 do_pam_chauthtok();
677 }
678#endif
679
Damien Millercf205e82001-04-16 18:29:15 +1000680 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000681 return;
682
683#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100684 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000685 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000686#endif /* USE_PAM */
687#ifdef WITH_AIXAUTHENTICATE
688 if (aixloginmsg && *aixloginmsg)
689 printf("%s\n", aixloginmsg);
690#endif /* WITH_AIXAUTHENTICATE */
691
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000692 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000693 time_string = ctime(&last_login_time);
694 if (strchr(time_string, '\n'))
695 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000696 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000697 printf("Last login: %s\r\n", time_string);
698 else
Damien Millere4340be2000-09-16 13:29:08 +1100699 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000700 }
Damien Millercf205e82001-04-16 18:29:15 +1000701
702 do_motd();
703}
704
705/*
706 * Display the message of the day.
707 */
708void
709do_motd(void)
710{
711 FILE *f;
712 char buf[256];
713
Damien Miller942da032000-08-18 13:59:06 +1000714 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000715#ifdef HAVE_LOGIN_CAP
716 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
717 "/etc/motd"), "r");
718#else
Damien Miller942da032000-08-18 13:59:06 +1000719 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000720#endif
Damien Miller942da032000-08-18 13:59:06 +1000721 if (f) {
722 while (fgets(buf, sizeof(buf), f))
723 fputs(buf, stdout);
724 fclose(f);
725 }
726 }
727}
728
Damien Millercf205e82001-04-16 18:29:15 +1000729
730/*
731 * Check for quiet login, either .hushlogin or command given.
732 */
733int
734check_quietlogin(Session *s, const char *command)
735{
736 char buf[256];
737 struct passwd * pw = s->pw;
738 struct stat st;
739
740 /* Return 1 if .hushlogin exists or a command given. */
741 if (command != NULL)
742 return 1;
743 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
744#ifdef HAVE_LOGIN_CAP
745 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
746 return 1;
747#else
748 if (stat(buf, &st) >= 0)
749 return 1;
750#endif
751 return 0;
752}
753
Damien Millerb38eff82000-04-01 11:09:21 +1000754/*
755 * Sets the value of the given variable in the environment. If the variable
756 * already exists, its value is overriden.
757 */
Damien Miller4af51302000-04-16 11:18:38 +1000758void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000759child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000760 const char *value)
761{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000762 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000763 char **env;
764
765 /*
766 * Find the slot where the value should be stored. If the variable
767 * already exists, we reuse the slot; otherwise we append a new slot
768 * at the end of the array, expanding if necessary.
769 */
770 env = *envp;
771 namelen = strlen(name);
772 for (i = 0; env[i]; i++)
773 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
774 break;
775 if (env[i]) {
776 /* Reuse the slot. */
777 xfree(env[i]);
778 } else {
779 /* New variable. Expand if necessary. */
780 if (i >= (*envsizep) - 1) {
781 (*envsizep) += 50;
782 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
783 }
784 /* Need to set the NULL pointer at end of array beyond the new slot. */
785 env[i + 1] = NULL;
786 }
787
788 /* Allocate space and format the variable in the appropriate slot. */
789 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
790 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
791}
792
793/*
794 * Reads environment variables from the given file and adds/overrides them
795 * into the environment. If the file does not exist, this does nothing.
796 * Otherwise, it must consist of empty lines, comments (line starts with '#')
797 * and assignments of the form name=value. No other forms are allowed.
798 */
Damien Miller4af51302000-04-16 11:18:38 +1000799void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000800read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000801 const char *filename)
802{
803 FILE *f;
804 char buf[4096];
805 char *cp, *value;
806
807 f = fopen(filename, "r");
808 if (!f)
809 return;
810
811 while (fgets(buf, sizeof(buf), f)) {
812 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
813 ;
814 if (!*cp || *cp == '#' || *cp == '\n')
815 continue;
816 if (strchr(cp, '\n'))
817 *strchr(cp, '\n') = '\0';
818 value = strchr(cp, '=');
819 if (value == NULL) {
820 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
821 continue;
822 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000823 /*
824 * Replace the equals sign by nul, and advance value to
825 * the value string.
826 */
Damien Millerb38eff82000-04-01 11:09:21 +1000827 *value = '\0';
828 value++;
829 child_set_env(env, envsize, cp, value);
830 }
831 fclose(f);
832}
833
834#ifdef USE_PAM
835/*
836 * Sets any environment variables which have been specified by PAM
837 */
838void do_pam_environment(char ***env, int *envsize)
839{
840 char *equals, var_name[512], var_val[512];
841 char **pam_env;
842 int i;
843
844 if ((pam_env = fetch_pam_environment()) == NULL)
845 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000846
Damien Millerb38eff82000-04-01 11:09:21 +1000847 for(i = 0; pam_env[i] != NULL; i++) {
848 if ((equals = strstr(pam_env[i], "=")) == NULL)
849 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000850
Damien Millerb38eff82000-04-01 11:09:21 +1000851 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
852 memset(var_name, '\0', sizeof(var_name));
853 memset(var_val, '\0', sizeof(var_val));
854
855 strncpy(var_name, pam_env[i], equals - pam_env[i]);
856 strcpy(var_val, equals + 1);
857
Damien Millercb5e44a2000-09-29 12:12:36 +1100858 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000859
860 child_set_env(env, envsize, var_name, var_val);
861 }
862 }
863}
864#endif /* USE_PAM */
865
Damien Millercb5e44a2000-09-29 12:12:36 +1100866#ifdef HAVE_CYGWIN
867void copy_environment(char ***env, int *envsize)
868{
869 char *equals, var_name[512], var_val[512];
870 int i;
871
872 for(i = 0; environ[i] != NULL; i++) {
873 if ((equals = strstr(environ[i], "=")) == NULL)
874 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000875
Damien Millercb5e44a2000-09-29 12:12:36 +1100876 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
877 memset(var_name, '\0', sizeof(var_name));
878 memset(var_val, '\0', sizeof(var_val));
879
880 strncpy(var_name, environ[i], equals - environ[i]);
881 strcpy(var_val, equals + 1);
882
883 debug3("Copy environment: %s=%s", var_name, var_val);
884
885 child_set_env(env, envsize, var_name, var_val);
886 }
887 }
888}
889#endif
890
Damien Miller5fc85652000-07-09 23:53:07 +1000891#if defined(HAVE_GETUSERATTR)
892/*
893 * AIX-specific login initialisation
894 */
895void set_limit(char *user, char *soft, char *hard, int resource, int mult)
896{
897 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000898 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000899
900 getrlimit(resource, &rlim);
901
Damien Miller65964d62000-07-11 09:16:22 +1000902 slim = 0;
903 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
904 if (slim < 0) {
905 rlim.rlim_cur = RLIM_INFINITY;
906 } else if (slim != 0) {
907 /* See the wackiness below */
908 if (rlim.rlim_cur == slim * mult)
909 slim = 0;
910 else
911 rlim.rlim_cur = slim * mult;
912 }
913 }
Damien Miller5fc85652000-07-09 23:53:07 +1000914
Damien Miller65964d62000-07-11 09:16:22 +1000915 hlim = 0;
916 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
917 if (hlim < 0) {
918 rlim.rlim_max = RLIM_INFINITY;
919 } else if (hlim != 0) {
920 rlim.rlim_max = hlim * mult;
921 }
922 }
Damien Miller5fc85652000-07-09 23:53:07 +1000923
Damien Miller65964d62000-07-11 09:16:22 +1000924 /*
925 * XXX For cpu and fsize the soft limit is set to the hard limit
926 * if the hard limit is left at its default value and the soft limit
927 * is changed from its default value, either by requesting it
928 * (slim == 0) or by setting it to the current default. At least
929 * that's how rlogind does it. If you're confused you're not alone.
930 * Bug or feature? AIX 4.3.1.2
931 */
932 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
933 && hlim == 0 && slim != 0)
934 rlim.rlim_max = rlim.rlim_cur;
935 /* A specified hard limit limits the soft limit */
936 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
937 rlim.rlim_cur = rlim.rlim_max;
938 /* A soft limit can increase a hard limit */
939 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000940 rlim.rlim_max = rlim.rlim_cur;
941
942 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000943 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000944}
945
946void set_limits_from_userattr(char *user)
947{
948 int mask;
949 char buf[16];
950
951 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
952 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
953 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
954 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
955 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
956 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
957#if defined(S_UNOFILE)
958 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
959#endif
960
961 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
962 /* Convert decimal to octal */
963 (void) snprintf(buf, sizeof(buf), "%d", mask);
964 if (sscanf(buf, "%o", &mask) == 1)
965 umask(mask);
966 }
967}
968#endif /* defined(HAVE_GETUSERATTR) */
969
Damien Millerb38eff82000-04-01 11:09:21 +1000970/*
971 * Performs common processing for the child, such as setting up the
972 * environment, closing extra file descriptors, setting the user and group
973 * ids, and executing the command or shell.
974 */
Damien Miller4af51302000-04-16 11:18:38 +1000975void
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000976do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000977{
Damien Miller7b28dc52000-09-05 13:34:53 +1100978 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000979 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000980 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +1000981 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +1000982 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000983 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000984 char **env;
985 extern char **environ;
986 struct stat st;
987 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000988 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +1000989#ifdef WITH_IRIX_PROJECT
990 prid_t projid;
991#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +0000992#ifdef WITH_IRIX_JOBS
993 jid_t jid = 0;
994#else
995#ifdef WITH_IRIX_ARRAY
996 int jid = 0;
997#endif /* WITH_IRIX_ARRAY */
998#endif /* WITH_IRIX_JOBS */
999
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001000 do_xauth =
1001 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1002
Ben Lindstrom005dd222001-04-18 15:29:33 +00001003 /* remove hostkey from the child's memory */
1004 destroy_sensitive_data();
1005
Damien Millerd3a18572000-06-07 19:55:44 +10001006 /* login(1) is only called if we execute the login shell */
1007 if (options.use_login && command != NULL)
1008 options.use_login = 0;
1009
Damien Miller364a9bd2001-04-16 18:37:05 +10001010#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001011 if (!options.use_login) {
1012# ifdef HAVE_LOGIN_CAP
1013 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1014 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1015 _PATH_NOLOGIN), "r");
1016# else /* HAVE_LOGIN_CAP */
1017 if (pw->pw_uid)
1018 f = fopen(_PATH_NOLOGIN, "r");
1019# endif /* HAVE_LOGIN_CAP */
1020 if (f) {
1021 /* /etc/nologin exists. Print its contents and exit. */
1022 while (fgets(buf, sizeof(buf), f))
1023 fputs(buf, stderr);
1024 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001025 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001026 }
Damien Millerb38eff82000-04-01 11:09:21 +10001027 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001028#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001029
Damien Millerad833b32000-08-23 10:46:23 +10001030 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001031 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1032 switch, so we let login(1) to this for us. */
1033 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001034#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001035 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001036 if (!check_quietlogin(s, command))
1037 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001038#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001039#ifdef HAVE_CYGWIN
1040 if (is_winnt) {
1041#else
Damien Millerb38eff82000-04-01 11:09:21 +10001042 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001043#endif
Damien Millerad833b32000-08-23 10:46:23 +10001044# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001045 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001046# endif /* HAVE_GETUSERATTR */
1047# ifdef HAVE_LOGIN_CAP
1048 if (setusercontext(lc, pw, pw->pw_uid,
1049 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1050 perror("unable to set user context");
1051 exit(1);
1052 }
1053# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001054#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1055 /* Sets login uid for accounting */
1056 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1057 error("setluid: %s", strerror(errno));
1058#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1059
Damien Millerad833b32000-08-23 10:46:23 +10001060 if (setlogin(pw->pw_name) < 0)
1061 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001062 if (setgid(pw->pw_gid) < 0) {
1063 perror("setgid");
1064 exit(1);
1065 }
1066 /* Initialize the group list. */
1067 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1068 perror("initgroups");
1069 exit(1);
1070 }
1071 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001072# ifdef USE_PAM
1073 /*
1074 * PAM credentials may take the form of
1075 * supplementary groups. These will have been
1076 * wiped by the above initgroups() call.
1077 * Reestablish them here.
1078 */
1079 do_pam_setcred(0);
1080# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001081# ifdef WITH_IRIX_JOBS
1082 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1083 if (jid == -1) {
1084 fatal("Failed to create job container: %.100s",
1085 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001086 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001087# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001088# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001089 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001090 if (jid == 0) {
1091 if (newarraysess() != 0)
1092 fatal("Failed to set up new array session: %.100s",
1093 strerror(errno));
1094 }
Damien Millerad833b32000-08-23 10:46:23 +10001095# endif /* WITH_IRIX_ARRAY */
1096# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001097 /* initialize irix project info */
1098 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1099 debug("Failed to get project id, using projid 0");
1100 projid = 0;
1101 }
Damien Miller91606b12000-06-28 08:22:29 +10001102 if (setprid(projid))
1103 fatal("Failed to initialize project %d for %s: %.100s",
1104 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001105# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001106#ifdef WITH_IRIX_AUDIT
1107 if (sysconf(_SC_AUDIT)) {
1108 debug("Setting sat id to %d", (int) pw->pw_uid);
1109 if (satsetid(pw->pw_uid))
1110 debug("error setting satid: %.100s", strerror(errno));
1111 }
1112#endif /* WITH_IRIX_AUDIT */
1113
Damien Miller168a7002001-03-17 10:29:50 +11001114#ifdef _AIX
1115 /*
1116 * AIX has a "usrinfo" area where logname and
1117 * other stuff is stored - a few applications
1118 * actually use this and die if it's not set
1119 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001120 if (s->ttyfd == -1)
1121 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001122 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001123 2 * strlen(pw->pw_name));
1124 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001125 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001126 if (usrinfo(SETUINFO, cp, i) == -1)
1127 fatal("Couldn't set usrinfo: %s",
1128 strerror(errno));
1129 debug3("AIX/UsrInfo: set len %d", i);
1130 xfree(cp);
1131#endif
1132
Damien Millerb38eff82000-04-01 11:09:21 +10001133 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001134 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001135# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001136 }
Damien Millerad833b32000-08-23 10:46:23 +10001137#endif /* HAVE_OSF_SIA */
1138
Damien Millerbac2d8a2000-09-05 16:13:06 +11001139#ifdef HAVE_CYGWIN
1140 if (is_winnt)
1141#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001142 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001143 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001144 }
1145 /*
1146 * Get the shell from the password data. An empty shell field is
1147 * legal, and means /bin/sh.
1148 */
1149 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001150#ifdef HAVE_LOGIN_CAP
1151 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1152#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001153
1154#ifdef AFS
1155 /* Try to get AFS tokens for the local cell. */
1156 if (k_hasafs()) {
1157 char cell[64];
1158
1159 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1160 krb_afslog(cell, 0);
1161
1162 krb_afslog(0, 0);
1163 }
1164#endif /* AFS */
1165
1166 /* Initialize the environment. */
1167 envsize = 100;
1168 env = xmalloc(envsize * sizeof(char *));
1169 env[0] = NULL;
1170
Damien Millerbac2d8a2000-09-05 16:13:06 +11001171#ifdef HAVE_CYGWIN
1172 /*
1173 * The Windows environment contains some setting which are
1174 * important for a running system. They must not be dropped.
1175 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001176 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001177#endif
1178
Damien Millerb38eff82000-04-01 11:09:21 +10001179 if (!options.use_login) {
1180 /* Set basic environment. */
1181 child_set_env(&env, &envsize, "USER", pw->pw_name);
1182 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1183 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001184#ifdef HAVE_LOGIN_CAP
1185 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1186 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001187#else /* HAVE_LOGIN_CAP */
1188# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001189 /*
1190 * There's no standard path on Windows. The path contains
1191 * important components pointing to the system directories,
1192 * needed for loading shared libraries. So the path better
1193 * remains intact here.
1194 */
Damien Millerb38eff82000-04-01 11:09:21 +10001195 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001196# endif /* HAVE_CYGWIN */
1197#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001198
1199 snprintf(buf, sizeof buf, "%.200s/%.50s",
1200 _PATH_MAILDIR, pw->pw_name);
1201 child_set_env(&env, &envsize, "MAIL", buf);
1202
1203 /* Normal systems set SHELL by default. */
1204 child_set_env(&env, &envsize, "SHELL", shell);
1205 }
1206 if (getenv("TZ"))
1207 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1208
1209 /* Set custom environment options from RSA authentication. */
1210 while (custom_environment) {
1211 struct envstring *ce = custom_environment;
1212 char *s = ce->s;
1213 int i;
1214 for (i = 0; s[i] != '=' && s[i]; i++);
1215 if (s[i] == '=') {
1216 s[i] = 0;
1217 child_set_env(&env, &envsize, s, s + i + 1);
1218 }
1219 custom_environment = ce->next;
1220 xfree(ce->s);
1221 xfree(ce);
1222 }
1223
1224 snprintf(buf, sizeof buf, "%.50s %d %d",
1225 get_remote_ipaddr(), get_remote_port(), get_local_port());
1226 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1227
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001228 if (s->ttyfd != -1)
1229 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1230 if (s->term)
1231 child_set_env(&env, &envsize, "TERM", s->term);
1232 if (s->display)
1233 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001234 if (original_command)
1235 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1236 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001237
1238#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001239 if ((cp = getenv("AUTHSTATE")) != NULL)
1240 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1241 if ((cp = getenv("KRB5CCNAME")) != NULL)
1242 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1243 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001244#endif
1245
1246#ifdef KRB4
1247 {
1248 extern char *ticket;
1249
1250 if (ticket)
1251 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1252 }
1253#endif /* KRB4 */
1254
1255#ifdef USE_PAM
1256 /* Pull in any environment variables that may have been set by PAM. */
1257 do_pam_environment(&env, &envsize);
1258#endif /* USE_PAM */
1259
Damien Millerb38eff82000-04-01 11:09:21 +10001260 if (xauthfile)
1261 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1262 if (auth_get_socket_name() != NULL)
1263 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1264 auth_get_socket_name());
1265
1266 /* read $HOME/.ssh/environment. */
1267 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001268 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1269 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001270 read_environment_file(&env, &envsize, buf);
1271 }
1272 if (debug_flag) {
1273 /* dump the environment */
1274 fprintf(stderr, "Environment:\n");
1275 for (i = 0; env[i]; i++)
1276 fprintf(stderr, " %.200s\n", env[i]);
1277 }
Damien Millerad833b32000-08-23 10:46:23 +10001278 /* we have to stash the hostname before we close our socket. */
1279 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001280 hostname = get_remote_name_or_ip(utmp_len,
1281 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001282 /*
1283 * Close the connection descriptors; note that this is the child, and
1284 * the server will still have the socket open, and it is important
1285 * that we do not shutdown it. Note that the descriptors cannot be
1286 * closed before building the environment, as we call
1287 * get_remote_ipaddr there.
1288 */
1289 if (packet_get_connection_in() == packet_get_connection_out())
1290 close(packet_get_connection_in());
1291 else {
1292 close(packet_get_connection_in());
1293 close(packet_get_connection_out());
1294 }
1295 /*
1296 * Close all descriptors related to channels. They will still remain
1297 * open in the parent.
1298 */
1299 /* XXX better use close-on-exec? -markus */
1300 channel_close_all();
1301
1302 /*
1303 * Close any extra file descriptors. Note that there may still be
1304 * descriptors left by system functions. They will be closed later.
1305 */
1306 endpwent();
1307
1308 /*
1309 * Close any extra open file descriptors so that we don\'t have them
1310 * hanging around in clients. Note that we want to do this after
1311 * initgroups, because at least on Solaris 2.3 it leaves file
1312 * descriptors open.
1313 */
1314 for (i = 3; i < 64; i++)
1315 close(i);
1316
1317 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001318 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001319 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1320 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001321#ifdef HAVE_LOGIN_CAP
1322 if (login_getcapbool(lc, "requirehome", 0))
1323 exit(1);
1324#endif
1325 }
Damien Millerb38eff82000-04-01 11:09:21 +10001326
1327 /*
1328 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1329 * xauth are run in the proper environment.
1330 */
1331 environ = env;
1332
1333 /*
1334 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1335 * in this order).
1336 */
1337 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001338 /* ignore _PATH_SSH_USER_RC for subsystems */
1339 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001340 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1341 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001342 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001343 fprintf(stderr, "Running %s\n", cmd);
1344 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001345 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001346 if (do_xauth)
1347 fprintf(f, "%s %s\n", s->auth_proto,
1348 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001349 pclose(f);
1350 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001351 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001352 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001353 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001354 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001355 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1356 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001357 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001358 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001359 if (do_xauth)
1360 fprintf(f, "%s %s\n", s->auth_proto,
1361 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001362 pclose(f);
1363 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001364 fprintf(stderr, "Could not run %s\n",
1365 _PATH_SSH_SYSTEM_RC);
1366 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001367 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001368 char *screen = strchr(s->display, ':');
1369
1370 if (debug_flag) {
1371 fprintf(stderr,
1372 "Running %.100s add "
1373 "%.100s %.100s %.100s\n",
1374 options.xauth_location, s->display,
1375 s->auth_proto, s->auth_data);
1376 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001377 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001378 "Adding %.*s/unix%s %s %s\n",
1379 (int)(screen - s->display),
1380 s->display, screen,
1381 s->auth_proto, s->auth_data);
1382 }
1383 snprintf(cmd, sizeof cmd, "%s -q -",
1384 options.xauth_location);
1385 f = popen(cmd, "w");
1386 if (f) {
1387 fprintf(f, "add %s %s %s\n", s->display,
1388 s->auth_proto, s->auth_data);
1389 if (screen != NULL)
1390 fprintf(f, "add %.*s/unix%s %s %s\n",
1391 (int)(screen - s->display),
1392 s->display, screen,
1393 s->auth_proto,
1394 s->auth_data);
1395 pclose(f);
1396 } else {
1397 fprintf(stderr, "Could not run %s\n",
1398 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001399 }
1400 }
Damien Millerb38eff82000-04-01 11:09:21 +10001401 /* Get the last component of the shell name. */
1402 cp = strrchr(shell, '/');
1403 if (cp)
1404 cp++;
1405 else
1406 cp = shell;
1407 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001408
1409 /* restore SIGPIPE for child */
1410 signal(SIGPIPE, SIG_DFL);
1411
Damien Millerb38eff82000-04-01 11:09:21 +10001412 /*
1413 * If we have no command, execute the shell. In this case, the shell
1414 * name to be passed in argv[0] is preceded by '-' to indicate that
1415 * this is a login shell.
1416 */
1417 if (!command) {
1418 if (!options.use_login) {
1419 char buf[256];
1420
1421 /*
1422 * Check for mail if we have a tty and it was enabled
1423 * in server options.
1424 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001425 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001426 char *mailbox;
1427 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001428
Damien Millerb38eff82000-04-01 11:09:21 +10001429 mailbox = getenv("MAIL");
1430 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001431 if (stat(mailbox, &mailstat) != 0 ||
1432 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001433 printf("No mail.\n");
1434 else if (mailstat.st_mtime < mailstat.st_atime)
1435 printf("You have mail.\n");
1436 else
1437 printf("You have new mail.\n");
1438 }
1439 }
1440 /* Start the shell. Set initial character to '-'. */
1441 buf[0] = '-';
1442 strncpy(buf + 1, cp, sizeof(buf) - 1);
1443 buf[sizeof(buf) - 1] = 0;
1444
1445 /* Execute the shell. */
1446 argv[0] = buf;
1447 argv[1] = NULL;
1448 execve(shell, argv, env);
1449
1450 /* Executing the shell failed. */
1451 perror(shell);
1452 exit(1);
1453
1454 } else {
1455 /* Launch login(1). */
1456
Damien Millerad833b32000-08-23 10:46:23 +10001457 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001458#ifdef LOGIN_NEEDS_TERM
1459 s->term? s->term : "unknown",
1460#endif
Damien Miller942da032000-08-18 13:59:06 +10001461 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001462
1463 /* Login couldn't be executed, die. */
1464
1465 perror("login");
1466 exit(1);
1467 }
1468 }
1469 /*
1470 * Execute the command using the user's shell. This uses the -c
1471 * option to execute the command.
1472 */
1473 argv[0] = (char *) cp;
1474 argv[1] = "-c";
1475 argv[2] = (char *) command;
1476 argv[3] = NULL;
1477 execve(shell, argv, env);
1478 perror(shell);
1479 exit(1);
1480}
1481
1482Session *
1483session_new(void)
1484{
1485 int i;
1486 static int did_init = 0;
1487 if (!did_init) {
1488 debug("session_new: init");
1489 for(i = 0; i < MAX_SESSIONS; i++) {
1490 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001491 }
1492 did_init = 1;
1493 }
1494 for(i = 0; i < MAX_SESSIONS; i++) {
1495 Session *s = &sessions[i];
1496 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001497 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001498 s->chanid = -1;
1499 s->ptyfd = -1;
1500 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001501 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001502 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001503 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001504 return s;
1505 }
1506 }
1507 return NULL;
1508}
1509
1510void
1511session_dump(void)
1512{
1513 int i;
1514 for(i = 0; i < MAX_SESSIONS; i++) {
1515 Session *s = &sessions[i];
1516 debug("dump: used %d session %d %p channel %d pid %d",
1517 s->used,
1518 s->self,
1519 s,
1520 s->chanid,
1521 s->pid);
1522 }
1523}
1524
Damien Millerefb4afe2000-04-12 18:45:05 +10001525int
1526session_open(int chanid)
1527{
1528 Session *s = session_new();
1529 debug("session_open: channel %d", chanid);
1530 if (s == NULL) {
1531 error("no more sessions");
1532 return 0;
1533 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001534 s->pw = auth_get_user();
1535 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001536 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001537 debug("session_open: session %d: link with channel %d", s->self, chanid);
1538 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001539 return 1;
1540}
1541
1542Session *
1543session_by_channel(int id)
1544{
1545 int i;
1546 for(i = 0; i < MAX_SESSIONS; i++) {
1547 Session *s = &sessions[i];
1548 if (s->used && s->chanid == id) {
1549 debug("session_by_channel: session %d channel %d", i, id);
1550 return s;
1551 }
1552 }
1553 debug("session_by_channel: unknown channel %d", id);
1554 session_dump();
1555 return NULL;
1556}
1557
1558Session *
1559session_by_pid(pid_t pid)
1560{
1561 int i;
1562 debug("session_by_pid: pid %d", pid);
1563 for(i = 0; i < MAX_SESSIONS; i++) {
1564 Session *s = &sessions[i];
1565 if (s->used && s->pid == pid)
1566 return s;
1567 }
1568 error("session_by_pid: unknown pid %d", pid);
1569 session_dump();
1570 return NULL;
1571}
1572
1573int
1574session_window_change_req(Session *s)
1575{
1576 s->col = packet_get_int();
1577 s->row = packet_get_int();
1578 s->xpixel = packet_get_int();
1579 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001580 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001581 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1582 return 1;
1583}
1584
1585int
1586session_pty_req(Session *s)
1587{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001588 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001589 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001590
Ben Lindstrom49c12602001-06-13 04:37:36 +00001591 if (no_pty_flag) {
1592 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001593 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001594 }
1595 if (s->ttyfd != -1) {
1596 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001597 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001598 }
1599
Damien Millerefb4afe2000-04-12 18:45:05 +10001600 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001601
1602 if (compat20) {
1603 s->col = packet_get_int();
1604 s->row = packet_get_int();
1605 } else {
1606 s->row = packet_get_int();
1607 s->col = packet_get_int();
1608 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001609 s->xpixel = packet_get_int();
1610 s->ypixel = packet_get_int();
1611
1612 if (strcmp(s->term, "") == 0) {
1613 xfree(s->term);
1614 s->term = NULL;
1615 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001616
Damien Millerefb4afe2000-04-12 18:45:05 +10001617 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001618 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001619 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001620 if (s->term)
1621 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001622 s->term = NULL;
1623 s->ptyfd = -1;
1624 s->ttyfd = -1;
1625 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001626 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001627 }
1628 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001629
1630 /* for SSH1 the tty modes length is not given */
1631 if (!compat20)
1632 n_bytes = packet_remaining();
1633 tty_parse_modes(s->ttyfd, &n_bytes);
1634
Damien Millerefb4afe2000-04-12 18:45:05 +10001635 /*
1636 * Add a cleanup function to clear the utmp entry and record logout
1637 * time in case we call fatal() (e.g., the connection gets closed).
1638 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001639 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001640 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001641
1642 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001643 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1644
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001645 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001646 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001647 return 1;
1648}
1649
Damien Millerbd483e72000-04-30 10:00:53 +10001650int
1651session_subsystem_req(Session *s)
1652{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001653 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001654 int success = 0;
1655 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001656 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001657
1658 packet_done();
1659 log("subsystem request for %s", subsys);
1660
Damien Millerf6d9e222000-06-18 14:50:44 +10001661 for (i = 0; i < options.num_subsystems; i++) {
1662 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1663 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001664 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001665 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001666 success = 1;
1667 }
1668 }
1669
1670 if (!success)
1671 log("subsystem request for %s failed, subsystem not found", subsys);
1672
Damien Millerbd483e72000-04-30 10:00:53 +10001673 xfree(subsys);
1674 return success;
1675}
1676
1677int
1678session_x11_req(Session *s)
1679{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001680 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001681
1682 s->single_connection = packet_get_char();
1683 s->auth_proto = packet_get_string(NULL);
1684 s->auth_data = packet_get_string(NULL);
1685 s->screen = packet_get_int();
1686 packet_done();
1687
Ben Lindstrom768176b2001-06-09 01:29:12 +00001688 success = session_setup_x11fwd(s);
1689 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001690 xfree(s->auth_proto);
1691 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001692 s->auth_proto = NULL;
1693 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001694 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001695 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001696}
1697
Damien Millerf6d9e222000-06-18 14:50:44 +10001698int
1699session_shell_req(Session *s)
1700{
1701 /* if forced_command == NULL, the shell is execed */
1702 char *shell = forced_command;
1703 packet_done();
Damien Millerf6d9e222000-06-18 14:50:44 +10001704 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001705 do_exec_no_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001706 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001707 do_exec_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001708 return 1;
1709}
1710
1711int
1712session_exec_req(Session *s)
1713{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001714 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001715 char *command = packet_get_string(&len);
1716 packet_done();
1717 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001718 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001719 command = forced_command;
1720 debug("Forced command '%.500s'", forced_command);
1721 }
Damien Millerf6d9e222000-06-18 14:50:44 +10001722 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001723 do_exec_no_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001724 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001725 do_exec_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001726 if (forced_command == NULL)
1727 xfree(command);
1728 return 1;
1729}
1730
Damien Miller0bc1bd82000-11-13 22:57:25 +11001731int
1732session_auth_agent_req(Session *s)
1733{
1734 static int called = 0;
1735 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001736 if (no_agent_forwarding_flag) {
1737 debug("session_auth_agent_req: no_agent_forwarding_flag");
1738 return 0;
1739 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001740 if (called) {
1741 return 0;
1742 } else {
1743 called = 1;
1744 return auth_input_request_forwarding(s->pw);
1745 }
1746}
1747
Damien Millerefb4afe2000-04-12 18:45:05 +10001748void
1749session_input_channel_req(int id, void *arg)
1750{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001751 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001752 int reply;
1753 int success = 0;
1754 char *rtype;
1755 Session *s;
1756 Channel *c;
1757
1758 rtype = packet_get_string(&len);
1759 reply = packet_get_char();
1760
1761 s = session_by_channel(id);
1762 if (s == NULL)
1763 fatal("session_input_channel_req: channel %d: no session", id);
1764 c = channel_lookup(id);
1765 if (c == NULL)
1766 fatal("session_input_channel_req: channel %d: bad channel", id);
1767
1768 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1769 s->self, id, rtype, reply);
1770
1771 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001772 * a session is in LARVAL state until a shell, a command
1773 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001774 */
1775 if (c->type == SSH_CHANNEL_LARVAL) {
1776 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001777 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001778 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001779 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001780 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001781 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001782 } else if (strcmp(rtype, "x11-req") == 0) {
1783 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001784 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1785 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001786 } else if (strcmp(rtype, "subsystem") == 0) {
1787 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001788 }
1789 }
1790 if (strcmp(rtype, "window-change") == 0) {
1791 success = session_window_change_req(s);
1792 }
1793
1794 if (reply) {
1795 packet_start(success ?
1796 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1797 packet_put_int(c->remote_id);
1798 packet_send();
1799 }
1800 xfree(rtype);
1801}
1802
1803void
1804session_set_fds(Session *s, int fdin, int fdout, int fderr)
1805{
1806 if (!compat20)
1807 fatal("session_set_fds: called for proto != 2.0");
1808 /*
1809 * now that have a child and a pipe to the child,
1810 * we can activate our channel and register the fd's
1811 */
1812 if (s->chanid == -1)
1813 fatal("no channel for session %d", s->self);
1814 channel_set_fds(s->chanid,
1815 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001816 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1817 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001818}
1819
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001820/*
1821 * Function to perform pty cleanup. Also called if we get aborted abnormally
1822 * (e.g., due to a dropped connection).
1823 */
Damien Millerb38eff82000-04-01 11:09:21 +10001824void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001825session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001826{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001827 Session *s = session;
1828
1829 if (s == NULL) {
1830 error("session_pty_cleanup: no session");
1831 return;
1832 }
1833 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001834 return;
1835
Kevin Steves43cdef32001-02-11 14:12:08 +00001836 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001837
Damien Millerb38eff82000-04-01 11:09:21 +10001838 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001839 if (s->pid != 0)
1840 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001841
1842 /* Release the pseudo-tty. */
1843 pty_release(s->tty);
1844
1845 /*
1846 * Close the server side of the socket pairs. We must do this after
1847 * the pty cleanup, so that another process doesn't get this pty
1848 * while we're still cleaning up.
1849 */
1850 if (close(s->ptymaster) < 0)
1851 error("close(s->ptymaster): %s", strerror(errno));
1852}
Damien Millerefb4afe2000-04-12 18:45:05 +10001853
1854void
1855session_exit_message(Session *s, int status)
1856{
1857 Channel *c;
1858 if (s == NULL)
1859 fatal("session_close: no session");
1860 c = channel_lookup(s->chanid);
1861 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001862 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001863 s->self, s->chanid);
1864 debug("session_exit_message: session %d channel %d pid %d",
1865 s->self, s->chanid, s->pid);
1866
1867 if (WIFEXITED(status)) {
1868 channel_request_start(s->chanid,
1869 "exit-status", 0);
1870 packet_put_int(WEXITSTATUS(status));
1871 packet_send();
1872 } else if (WIFSIGNALED(status)) {
1873 channel_request_start(s->chanid,
1874 "exit-signal", 0);
1875 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001876#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001877 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001878#else /* WCOREDUMP */
1879 packet_put_char(0);
1880#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001881 packet_put_cstring("");
1882 packet_put_cstring("");
1883 packet_send();
1884 } else {
1885 /* Some weird exit cause. Just exit. */
1886 packet_disconnect("wait returned status %04x.", status);
1887 }
1888
1889 /* disconnect channel */
1890 debug("session_exit_message: release channel %d", s->chanid);
1891 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001892 /*
1893 * emulate a write failure with 'chan_write_failed', nobody will be
1894 * interested in data we write.
1895 * Note that we must not call 'chan_read_failed', since there could
1896 * be some more data waiting in the pipe.
1897 */
Damien Millerbd483e72000-04-30 10:00:53 +10001898 if (c->ostate != CHAN_OUTPUT_CLOSED)
1899 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001900 s->chanid = -1;
1901}
1902
1903void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001904session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001905{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001906 debug("session_close: session %d pid %d", s->self, s->pid);
1907 if (s->ttyfd != -1) {
1908 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1909 session_pty_cleanup(s);
1910 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001911 if (s->term)
1912 xfree(s->term);
1913 if (s->display)
1914 xfree(s->display);
1915 if (s->auth_data)
1916 xfree(s->auth_data);
1917 if (s->auth_proto)
1918 xfree(s->auth_proto);
1919 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001920 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001921}
1922
1923void
1924session_close_by_pid(pid_t pid, int status)
1925{
1926 Session *s = session_by_pid(pid);
1927 if (s == NULL) {
1928 debug("session_close_by_pid: no session for pid %d", s->pid);
1929 return;
1930 }
1931 if (s->chanid != -1)
1932 session_exit_message(s, status);
1933 session_close(s);
1934}
1935
1936/*
1937 * this is called when a channel dies before
1938 * the session 'child' itself dies
1939 */
1940void
1941session_close_by_channel(int id, void *arg)
1942{
1943 Session *s = session_by_channel(id);
1944 if (s == NULL) {
1945 debug("session_close_by_channel: no session for channel %d", id);
1946 return;
1947 }
1948 /* disconnect channel */
1949 channel_cancel_cleanup(s->chanid);
1950 s->chanid = -1;
1951
1952 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1953 if (s->pid == 0) {
1954 /* close session immediately */
1955 session_close(s);
1956 } else {
1957 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001958 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001959 error("session_close_by_channel: kill %d: %s",
1960 s->pid, strerror(errno));
1961 }
1962}
1963
Damien Millere247cc42000-05-07 12:03:14 +10001964char *
1965session_tty_list(void)
1966{
1967 static char buf[1024];
1968 int i;
1969 buf[0] = '\0';
1970 for(i = 0; i < MAX_SESSIONS; i++) {
1971 Session *s = &sessions[i];
1972 if (s->used && s->ttyfd != -1) {
1973 if (buf[0] != '\0')
1974 strlcat(buf, ",", sizeof buf);
1975 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1976 }
1977 }
1978 if (buf[0] == '\0')
1979 strlcpy(buf, "notty", sizeof buf);
1980 return buf;
1981}
1982
1983void
1984session_proctitle(Session *s)
1985{
1986 if (s->pw == NULL)
1987 error("no user for session %d", s->self);
1988 else
1989 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1990}
1991
Ben Lindstrom768176b2001-06-09 01:29:12 +00001992int
1993session_setup_x11fwd(Session *s)
1994{
1995 int fd;
1996 struct stat st;
1997
1998 if (no_x11_forwarding_flag) {
1999 packet_send_debug("X11 forwarding disabled in user configuration file.");
2000 return 0;
2001 }
2002 if (!options.x11_forwarding) {
2003 debug("X11 forwarding disabled in server configuration file.");
2004 return 0;
2005 }
2006 if (!options.xauth_location ||
2007 (stat(options.xauth_location, &st) == -1)) {
2008 packet_send_debug("No xauth program; cannot forward with spoofing.");
2009 return 0;
2010 }
Ben Lindstromcb3929d2001-06-09 01:34:15 +00002011 if (s->display != NULL || xauthfile != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002012 debug("X11 display already set.");
2013 return 0;
2014 }
2015 xauthfile = xmalloc(MAXPATHLEN);
2016 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
2017 temporarily_use_uid(s->pw);
2018 if (mkdtemp(xauthfile) == NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002019 error("private X11 dir: mkdtemp %s failed: %s",
2020 xauthfile, strerror(errno));
Ben Lindstrom4d3f2272001-06-09 01:44:07 +00002021 restore_uid();
Ben Lindstrom768176b2001-06-09 01:29:12 +00002022 xfree(xauthfile);
2023 xauthfile = NULL;
2024 return 0;
2025 }
2026 strlcat(xauthfile, "/cookies", MAXPATHLEN);
2027 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
2028 if (fd >= 0)
2029 close(fd);
2030 restore_uid();
2031 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
2032 if (s->display == NULL) {
2033 xauthfile_cleanup_proc(s->pw);
2034 return 0;
2035 }
2036 fatal_add_cleanup(xauthfile_cleanup_proc, s->pw);
2037 return 1;
2038}
2039
Damien Millerefb4afe2000-04-12 18:45:05 +10002040void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002041do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002042{
Damien Millerefb4afe2000-04-12 18:45:05 +10002043 server_loop2();
2044}