blob: 482590803d70194dff6eef723c55844ed52cbbea [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 Lindstrom44697232001-07-04 03:32:30 +000012 * Copyright (c) 2000, 2001 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 Lindstrom37e41c92001-09-16 22:17:15 +000036RCSID("$OpenBSD: session.c,v 1.102 2001/09/16 14:46:54 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;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000102 struct passwd *pw;
103 Authctxt *authctxt;
Damien Millerb38eff82000-04-01 11:09:21 +1000104 pid_t pid;
105 /* tty */
106 char *term;
107 int ptyfd, ttyfd, ptymaster;
108 int row, col, xpixel, ypixel;
109 char tty[TTYSZ];
110 /* X11 */
111 char *display;
112 int screen;
113 char *auth_proto;
114 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000115 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000116 /* proto 2 */
117 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000118 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +1000119};
120
121/* func */
122
123Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000124void session_set_fds(Session *, int, int, int);
125static void session_pty_cleanup(void *);
126void session_proctitle(Session *);
127int session_setup_x11fwd(Session *);
128void do_exec_pty(Session *, const char *);
129void do_exec_no_pty(Session *, const char *);
130void do_exec(Session *, const char *);
131void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000132#ifdef LOGIN_NEEDS_UTMPX
133static void do_pre_login(Session *s);
134#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000135void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000136void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000137int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void do_authenticated1(Authctxt *);
140static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000141
Ben Lindstrombba81212001-06-25 05:01:22 +0000142static void session_close(Session *);
143static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000144
Damien Millerb38eff82000-04-01 11:09:21 +1000145/* import */
146extern ServerOptions options;
147extern char *__progname;
148extern int log_stderr;
149extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000150extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000151extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000152extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000153
Damien Miller7b28dc52000-09-05 13:34:53 +1100154/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000155const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100156
Damien Millerb38eff82000-04-01 11:09:21 +1000157/* data */
158#define MAX_SESSIONS 10
159Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100160
Damien Millerd2c208a2000-05-17 22:00:02 +1000161#ifdef WITH_AIXAUTHENTICATE
162/* AIX's lastlogin message, set in auth1.c */
163char *aixloginmsg;
164#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000165
Damien Millerad833b32000-08-23 10:46:23 +1000166#ifdef HAVE_LOGIN_CAP
167static login_cap_t *lc;
168#endif
169
Ben Lindstromb31783d2001-03-22 02:02:12 +0000170void
171do_authenticated(Authctxt *authctxt)
172{
173 /*
174 * Cancel the alarm we set to limit the time taken for
175 * authentication.
176 */
177 alarm(0);
178 if (startup_pipe != -1) {
179 close(startup_pipe);
180 startup_pipe = -1;
181 }
182#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
183 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
184 error("unable to get login class");
185 return;
186 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000187#ifdef BSD_AUTH
188 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
189 packet_disconnect("Approval failure for %s",
190 authctxt->pw->pw_name);
191 }
192#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000193#endif
194 /* setup the channel layer */
195 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
196 channel_permit_all_opens();
197
198 if (compat20)
199 do_authenticated2(authctxt);
200 else
201 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000202
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000203 /* remove agent socket */
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 Lindstromec95ed92001-07-04 04:21:14 +0000206#ifdef KRB4
207 if (options.kerberos_ticket_cleanup)
208 krb4_cleanup_proc(authctxt);
209#endif
210#ifdef KRB5
211 if (options.kerberos_ticket_cleanup)
212 krb5_cleanup_proc(authctxt);
213#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000214}
215
Damien Millerb38eff82000-04-01 11:09:21 +1000216/*
Damien Millerb38eff82000-04-01 11:09:21 +1000217 * Prepares for an interactive session. This is called after the user has
218 * been successfully authenticated. During this message exchange, pseudo
219 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
220 * are requested, etc.
221 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000222static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000223do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000224{
225 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000226 char *command;
Ben Lindstrom49c12602001-06-13 04:37:36 +0000227 int success, type, plen, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000228 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000229 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000230
231 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000232 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000233 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000234
Damien Millerb38eff82000-04-01 11:09:21 +1000235 /*
236 * We stay in this loop until the client requests to execute a shell
237 * or a command.
238 */
239 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000240 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000241
242 /* Get a packet from the client. */
243 type = packet_read(&plen);
244
245 /* Process the packet. */
246 switch (type) {
247 case SSH_CMSG_REQUEST_COMPRESSION:
248 packet_integrity_check(plen, 4, type);
249 compression_level = packet_get_int();
250 if (compression_level < 1 || compression_level > 9) {
251 packet_send_debug("Received illegal compression level %d.",
252 compression_level);
253 break;
254 }
255 /* Enable compression after we have responded with SUCCESS. */
256 enable_compression_after_reply = 1;
257 success = 1;
258 break;
259
260 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000261 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000262 break;
263
264 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000265 s->auth_proto = packet_get_string(&proto_len);
266 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000267
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000268 screen_flag = packet_get_protocol_flags() &
269 SSH_PROTOFLAG_SCREEN_NUMBER;
270 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
271
272 if (packet_remaining() == 4) {
273 if (!screen_flag)
274 debug2("Buggy client: "
275 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000276 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000277 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000278 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000279 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000280 packet_done();
281 success = session_setup_x11fwd(s);
282 if (!success) {
283 xfree(s->auth_proto);
284 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000285 s->auth_proto = NULL;
286 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000287 }
Damien Millerb38eff82000-04-01 11:09:21 +1000288 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000289
290 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
291 if (no_agent_forwarding_flag || compat13) {
292 debug("Authentication agent forwarding not permitted for this authentication.");
293 break;
294 }
295 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000296 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000297 break;
298
299 case SSH_CMSG_PORT_FORWARD_REQUEST:
300 if (no_port_forwarding_flag) {
301 debug("Port forwarding not permitted for this authentication.");
302 break;
303 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100304 if (!options.allow_tcp_forwarding) {
305 debug("Port forwarding not permitted.");
306 break;
307 }
Damien Millerb38eff82000-04-01 11:09:21 +1000308 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000309 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000310 success = 1;
311 break;
312
313 case SSH_CMSG_MAX_PACKET_SIZE:
314 if (packet_set_maxsize(packet_get_int()) > 0)
315 success = 1;
316 break;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000317
318#if defined(AFS) || defined(KRB5)
319 case SSH_CMSG_HAVE_KERBEROS_TGT:
320 if (!options.kerberos_tgt_passing) {
321 verbose("Kerberos TGT passing disabled.");
322 } else {
323 char *kdata = packet_get_string(&dlen);
324 packet_integrity_check(plen, 4 + dlen, type);
325
326 /* XXX - 0x41, see creds_to_radix version */
327 if (kdata[0] != 0x41) {
328#ifdef KRB5
329 krb5_data tgt;
330 tgt.data = kdata;
331 tgt.length = dlen;
332
333 if (auth_krb5_tgt(s->authctxt, &tgt))
334 success = 1;
335 else
336 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
337#endif /* KRB5 */
338 } else {
339#ifdef AFS
340 if (auth_krb4_tgt(s->authctxt, kdata))
341 success = 1;
342 else
343 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
344#endif /* AFS */
345 }
346 xfree(kdata);
347 }
348 break;
349#endif /* AFS || KRB5 */
350
351#ifdef AFS
352 case SSH_CMSG_HAVE_AFS_TOKEN:
353 if (!options.afs_token_passing || !k_hasafs()) {
354 verbose("AFS token passing disabled.");
355 } else {
356 /* Accept AFS token. */
357 char *token = packet_get_string(&dlen);
358 packet_integrity_check(plen, 4 + dlen, type);
359
360 if (auth_afs_token(s->authctxt, token))
361 success = 1;
362 else
363 verbose("AFS token refused for %.100s",
364 s->authctxt->user);
365 xfree(token);
366 }
367 break;
368#endif /* AFS */
Damien Millerb38eff82000-04-01 11:09:21 +1000369
370 case SSH_CMSG_EXEC_SHELL:
371 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000372 if (type == SSH_CMSG_EXEC_CMD) {
373 command = packet_get_string(&dlen);
374 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000375 do_exec(s, command);
376 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000377 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000378 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000379 }
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000380 packet_done();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000381 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000382 return;
383
384 default:
385 /*
386 * Any unknown messages in this phase are ignored,
387 * and a failure message is returned.
388 */
389 log("Unknown packet type received after authentication: %d", type);
390 }
391 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
392 packet_send();
393 packet_write_wait();
394
395 /* Enable compression now that we have replied if appropriate. */
396 if (enable_compression_after_reply) {
397 enable_compression_after_reply = 0;
398 packet_start_compression(compression_level);
399 }
400 }
401}
402
403/*
404 * This is called to fork and execute a command when we have no tty. This
405 * will call do_child from the child, and server_loop from the parent after
406 * setting up file descriptors and such.
407 */
Damien Miller4af51302000-04-16 11:18:38 +1000408void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000409do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000410{
411 int pid;
412
413#ifdef USE_PIPES
414 int pin[2], pout[2], perr[2];
415 /* Allocate pipes for communicating with the program. */
416 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
417 packet_disconnect("Could not create pipes: %.100s",
418 strerror(errno));
419#else /* USE_PIPES */
420 int inout[2], err[2];
421 /* Uses socket pairs to communicate with the program. */
422 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
423 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
424 packet_disconnect("Could not create socket pairs: %.100s",
425 strerror(errno));
426#endif /* USE_PIPES */
427 if (s == NULL)
428 fatal("do_exec_no_pty: no session");
429
Damien Millere247cc42000-05-07 12:03:14 +1000430 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000431
Damien Millerc5946332001-02-28 11:46:11 +1100432#if defined(USE_PAM)
Damien Miller665af9c2001-06-27 09:34:15 +1000433 do_pam_session(s->pw->pw_name, NULL);
Damien Millerf9e93002001-03-27 16:12:24 +1000434 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000435#endif /* USE_PAM */
436
Damien Millerb38eff82000-04-01 11:09:21 +1000437 /* Fork the child. */
438 if ((pid = fork()) == 0) {
439 /* Child. Reinitialize the log since the pid has changed. */
440 log_init(__progname, options.log_level, options.log_facility, log_stderr);
441
442 /*
443 * Create a new session and process group since the 4.4BSD
444 * setlogin() affects the entire process group.
445 */
446 if (setsid() < 0)
447 error("setsid failed: %.100s", strerror(errno));
448
449#ifdef USE_PIPES
450 /*
451 * Redirect stdin. We close the parent side of the socket
452 * pair, and make the child side the standard input.
453 */
454 close(pin[1]);
455 if (dup2(pin[0], 0) < 0)
456 perror("dup2 stdin");
457 close(pin[0]);
458
459 /* Redirect stdout. */
460 close(pout[0]);
461 if (dup2(pout[1], 1) < 0)
462 perror("dup2 stdout");
463 close(pout[1]);
464
465 /* Redirect stderr. */
466 close(perr[0]);
467 if (dup2(perr[1], 2) < 0)
468 perror("dup2 stderr");
469 close(perr[1]);
470#else /* USE_PIPES */
471 /*
472 * Redirect stdin, stdout, and stderr. Stdin and stdout will
473 * use the same socket, as some programs (particularly rdist)
474 * seem to depend on it.
475 */
476 close(inout[1]);
477 close(err[1]);
478 if (dup2(inout[0], 0) < 0) /* stdin */
479 perror("dup2 stdin");
480 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
481 perror("dup2 stdout");
482 if (dup2(err[0], 2) < 0) /* stderr */
483 perror("dup2 stderr");
484#endif /* USE_PIPES */
485
486 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000487 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000488 /* NOTREACHED */
489 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100490#ifdef HAVE_CYGWIN
491 if (is_winnt)
492 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
493#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000494 if (pid < 0)
495 packet_disconnect("fork failed: %.100s", strerror(errno));
496 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000497 /* Set interactive/non-interactive mode. */
498 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000499#ifdef USE_PIPES
500 /* We are the parent. Close the child sides of the pipes. */
501 close(pin[0]);
502 close(pout[1]);
503 close(perr[1]);
504
Damien Millerefb4afe2000-04-12 18:45:05 +1000505 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000506 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000507 } else {
508 /* Enter the interactive session. */
509 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000510 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000511 }
Damien Millerb38eff82000-04-01 11:09:21 +1000512#else /* USE_PIPES */
513 /* We are the parent. Close the child sides of the socket pairs. */
514 close(inout[0]);
515 close(err[0]);
516
517 /*
518 * Enter the interactive session. Note: server_loop must be able to
519 * handle the case that fdin and fdout are the same.
520 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000521 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000522 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000523 } else {
524 server_loop(pid, inout[1], inout[1], err[1]);
525 /* server_loop has closed inout[1] and err[1]. */
526 }
Damien Millerb38eff82000-04-01 11:09:21 +1000527#endif /* USE_PIPES */
528}
529
530/*
531 * This is called to fork and execute a command when we have a tty. This
532 * will call do_child from the child, and server_loop from the parent after
533 * setting up file descriptors, controlling tty, updating wtmp, utmp,
534 * lastlog, and other such operations.
535 */
Damien Miller4af51302000-04-16 11:18:38 +1000536void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000537do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000538{
Damien Millerb38eff82000-04-01 11:09:21 +1000539 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000540 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000541
542 if (s == NULL)
543 fatal("do_exec_pty: no session");
544 ptyfd = s->ptyfd;
545 ttyfd = s->ttyfd;
546
Damien Millerc5946332001-02-28 11:46:11 +1100547#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000548 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000549 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100550#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000551
Damien Millerb38eff82000-04-01 11:09:21 +1000552 /* Fork the child. */
553 if ((pid = fork()) == 0) {
Ben Lindstrombddd5512001-07-04 04:53:53 +0000554
Damien Miller942da032000-08-18 13:59:06 +1000555 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000556 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000557 /* Close the master side of the pseudo tty. */
558 close(ptyfd);
559
560 /* Make the pseudo tty our controlling tty. */
561 pty_make_controlling_tty(&ttyfd, s->tty);
562
563 /* Redirect stdin from the pseudo tty. */
564 if (dup2(ttyfd, fileno(stdin)) < 0)
565 error("dup2 stdin failed: %.100s", strerror(errno));
566
567 /* Redirect stdout to the pseudo tty. */
568 if (dup2(ttyfd, fileno(stdout)) < 0)
569 error("dup2 stdin failed: %.100s", strerror(errno));
570
571 /* Redirect stderr to the pseudo tty. */
572 if (dup2(ttyfd, fileno(stderr)) < 0)
573 error("dup2 stdin failed: %.100s", strerror(errno));
574
575 /* Close the extra descriptor for the pseudo tty. */
576 close(ttyfd);
577
Damien Miller942da032000-08-18 13:59:06 +1000578 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000579#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100580 if (!(options.use_login && command == NULL))
581 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000582# ifdef LOGIN_NEEDS_UTMPX
583 else
584 do_pre_login(s);
585# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000586#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000587
Damien Millerb38eff82000-04-01 11:09:21 +1000588 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000589 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000590 /* NOTREACHED */
591 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100592#ifdef HAVE_CYGWIN
593 if (is_winnt)
594 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
595#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000596 if (pid < 0)
597 packet_disconnect("fork failed: %.100s", strerror(errno));
598 s->pid = pid;
599
600 /* Parent. Close the slave side of the pseudo tty. */
601 close(ttyfd);
602
603 /*
604 * Create another descriptor of the pty master side for use as the
605 * standard input. We could use the original descriptor, but this
606 * simplifies code in server_loop. The descriptor is bidirectional.
607 */
608 fdout = dup(ptyfd);
609 if (fdout < 0)
610 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
611
612 /* we keep a reference to the pty master */
613 ptymaster = dup(ptyfd);
614 if (ptymaster < 0)
615 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
616 s->ptymaster = ptymaster;
617
618 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000619 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000620 if (compat20) {
621 session_set_fds(s, ptyfd, fdout, -1);
622 } else {
623 server_loop(pid, ptyfd, fdout, -1);
624 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000625 }
Damien Millerb38eff82000-04-01 11:09:21 +1000626}
627
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000628#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000629static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000630do_pre_login(Session *s)
631{
632 socklen_t fromlen;
633 struct sockaddr_storage from;
634 pid_t pid = getpid();
635
636 /*
637 * Get IP address of client. If the connection is not a socket, let
638 * the address be 0.0.0.0.
639 */
640 memset(&from, 0, sizeof(from));
641 if (packet_connection_is_on_socket()) {
642 fromlen = sizeof(from);
643 if (getpeername(packet_get_connection_in(),
644 (struct sockaddr *) & from, &fromlen) < 0) {
645 debug("getpeername: %.100s", strerror(errno));
646 fatal_cleanup();
647 }
648 }
649
650 record_utmp_only(pid, s->tty, s->pw->pw_name,
651 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
652 (struct sockaddr *)&from);
653}
654#endif
655
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000656/*
657 * This is called to fork and execute a command. If another command is
658 * to be forced, execute that instead.
659 */
660void
661do_exec(Session *s, const char *command)
662{
663 if (forced_command) {
664 original_command = command;
665 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000666 debug("Forced command '%.900s'", command);
667 }
668
669 if (s->ttyfd != -1)
670 do_exec_pty(s, command);
671 else
672 do_exec_no_pty(s, command);
673
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000674 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000675}
676
Damien Miller942da032000-08-18 13:59:06 +1000677/* administrative, login(1)-like work */
678void
Damien Miller69b69aa2000-10-28 14:19:58 +1100679do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000680{
Damien Miller942da032000-08-18 13:59:06 +1000681 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100682 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000683 socklen_t fromlen;
684 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000685 time_t last_login_time;
686 struct passwd * pw = s->pw;
687 pid_t pid = getpid();
688
689 /*
690 * Get IP address of client. If the connection is not a socket, let
691 * the address be 0.0.0.0.
692 */
693 memset(&from, 0, sizeof(from));
694 if (packet_connection_is_on_socket()) {
695 fromlen = sizeof(from);
696 if (getpeername(packet_get_connection_in(),
697 (struct sockaddr *) & from, &fromlen) < 0) {
698 debug("getpeername: %.100s", strerror(errno));
699 fatal_cleanup();
700 }
701 }
702
Damien Miller7b28dc52000-09-05 13:34:53 +1100703 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000704 if (options.print_lastlog) {
705 hostname[0] = '\0';
706 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
707 hostname, sizeof(hostname));
708 }
Damien Millere4340be2000-09-16 13:29:08 +1100709
Damien Miller942da032000-08-18 13:59:06 +1000710 /* Record that there was a login on that tty from the remote host. */
711 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000712 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
713 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000714
Kevin Steves092f2ef2000-10-14 13:36:13 +0000715#ifdef USE_PAM
716 /*
717 * If password change is needed, do it now.
718 * This needs to occur before the ~/.hushlogin check.
719 */
Damien Miller646aa602001-02-15 11:51:32 +1100720 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000721 print_pam_messages();
722 do_pam_chauthtok();
723 }
724#endif
725
Damien Millercf205e82001-04-16 18:29:15 +1000726 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000727 return;
728
729#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100730 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000731 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000732#endif /* USE_PAM */
733#ifdef WITH_AIXAUTHENTICATE
734 if (aixloginmsg && *aixloginmsg)
735 printf("%s\n", aixloginmsg);
736#endif /* WITH_AIXAUTHENTICATE */
737
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000738 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000739 time_string = ctime(&last_login_time);
740 if (strchr(time_string, '\n'))
741 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000742 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000743 printf("Last login: %s\r\n", time_string);
744 else
Damien Millere4340be2000-09-16 13:29:08 +1100745 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000746 }
Damien Millercf205e82001-04-16 18:29:15 +1000747
748 do_motd();
749}
750
751/*
752 * Display the message of the day.
753 */
754void
755do_motd(void)
756{
757 FILE *f;
758 char buf[256];
759
Damien Miller942da032000-08-18 13:59:06 +1000760 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000761#ifdef HAVE_LOGIN_CAP
762 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
763 "/etc/motd"), "r");
764#else
Damien Miller942da032000-08-18 13:59:06 +1000765 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000766#endif
Damien Miller942da032000-08-18 13:59:06 +1000767 if (f) {
768 while (fgets(buf, sizeof(buf), f))
769 fputs(buf, stdout);
770 fclose(f);
771 }
772 }
773}
774
Kevin Steves8f63caa2001-07-04 18:23:02 +0000775
776/*
777 * Check for quiet login, either .hushlogin or command given.
778 */
779int
780check_quietlogin(Session *s, const char *command)
781{
782 char buf[256];
783 struct passwd *pw = s->pw;
784 struct stat st;
785
786 /* Return 1 if .hushlogin exists or a command given. */
787 if (command != NULL)
788 return 1;
789 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
790#ifdef HAVE_LOGIN_CAP
791 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
792 return 1;
793#else
794 if (stat(buf, &st) >= 0)
795 return 1;
796#endif
797 return 0;
798}
799
Damien Millerb38eff82000-04-01 11:09:21 +1000800/*
801 * Sets the value of the given variable in the environment. If the variable
802 * already exists, its value is overriden.
803 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000804static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000805child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000806 const char *value)
807{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000808 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000809 char **env;
810
811 /*
812 * Find the slot where the value should be stored. If the variable
813 * already exists, we reuse the slot; otherwise we append a new slot
814 * at the end of the array, expanding if necessary.
815 */
816 env = *envp;
817 namelen = strlen(name);
818 for (i = 0; env[i]; i++)
819 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
820 break;
821 if (env[i]) {
822 /* Reuse the slot. */
823 xfree(env[i]);
824 } else {
825 /* New variable. Expand if necessary. */
826 if (i >= (*envsizep) - 1) {
827 (*envsizep) += 50;
828 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
829 }
830 /* Need to set the NULL pointer at end of array beyond the new slot. */
831 env[i + 1] = NULL;
832 }
833
834 /* Allocate space and format the variable in the appropriate slot. */
835 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
836 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
837}
838
839/*
840 * Reads environment variables from the given file and adds/overrides them
841 * into the environment. If the file does not exist, this does nothing.
842 * Otherwise, it must consist of empty lines, comments (line starts with '#')
843 * and assignments of the form name=value. No other forms are allowed.
844 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000845static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000846read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000847 const char *filename)
848{
849 FILE *f;
850 char buf[4096];
851 char *cp, *value;
852
853 f = fopen(filename, "r");
854 if (!f)
855 return;
856
857 while (fgets(buf, sizeof(buf), f)) {
858 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
859 ;
860 if (!*cp || *cp == '#' || *cp == '\n')
861 continue;
862 if (strchr(cp, '\n'))
863 *strchr(cp, '\n') = '\0';
864 value = strchr(cp, '=');
865 if (value == NULL) {
866 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
867 continue;
868 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000869 /*
870 * Replace the equals sign by nul, and advance value to
871 * the value string.
872 */
Damien Millerb38eff82000-04-01 11:09:21 +1000873 *value = '\0';
874 value++;
875 child_set_env(env, envsize, cp, value);
876 }
877 fclose(f);
878}
879
880#ifdef USE_PAM
881/*
882 * Sets any environment variables which have been specified by PAM
883 */
Kevin Steves9b26f962001-06-29 17:52:17 +0000884void do_pam_environment(char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000885{
886 char *equals, var_name[512], var_val[512];
887 char **pam_env;
888 int i;
889
890 if ((pam_env = fetch_pam_environment()) == NULL)
891 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000892
Damien Millerb38eff82000-04-01 11:09:21 +1000893 for(i = 0; pam_env[i] != NULL; i++) {
894 if ((equals = strstr(pam_env[i], "=")) == NULL)
895 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000896
Damien Millerb38eff82000-04-01 11:09:21 +1000897 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
898 memset(var_name, '\0', sizeof(var_name));
899 memset(var_val, '\0', sizeof(var_val));
900
901 strncpy(var_name, pam_env[i], equals - pam_env[i]);
902 strcpy(var_val, equals + 1);
903
Damien Millercb5e44a2000-09-29 12:12:36 +1100904 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000905
906 child_set_env(env, envsize, var_name, var_val);
907 }
908 }
909}
910#endif /* USE_PAM */
911
Damien Millercb5e44a2000-09-29 12:12:36 +1100912#ifdef HAVE_CYGWIN
Kevin Steves9b26f962001-06-29 17:52:17 +0000913void copy_environment(char ***env, u_int *envsize)
Damien Millercb5e44a2000-09-29 12:12:36 +1100914{
915 char *equals, var_name[512], var_val[512];
916 int i;
917
918 for(i = 0; environ[i] != NULL; i++) {
919 if ((equals = strstr(environ[i], "=")) == NULL)
920 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000921
Damien Millercb5e44a2000-09-29 12:12:36 +1100922 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
923 memset(var_name, '\0', sizeof(var_name));
924 memset(var_val, '\0', sizeof(var_val));
925
926 strncpy(var_name, environ[i], equals - environ[i]);
927 strcpy(var_val, equals + 1);
928
929 debug3("Copy environment: %s=%s", var_name, var_val);
930
931 child_set_env(env, envsize, var_name, var_val);
932 }
933 }
934}
935#endif
936
Damien Miller5fc85652000-07-09 23:53:07 +1000937#if defined(HAVE_GETUSERATTR)
938/*
939 * AIX-specific login initialisation
940 */
941void set_limit(char *user, char *soft, char *hard, int resource, int mult)
942{
943 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000944 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000945
946 getrlimit(resource, &rlim);
947
Damien Miller65964d62000-07-11 09:16:22 +1000948 slim = 0;
949 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
950 if (slim < 0) {
951 rlim.rlim_cur = RLIM_INFINITY;
952 } else if (slim != 0) {
953 /* See the wackiness below */
954 if (rlim.rlim_cur == slim * mult)
955 slim = 0;
956 else
957 rlim.rlim_cur = slim * mult;
958 }
959 }
Damien Miller5fc85652000-07-09 23:53:07 +1000960
Damien Miller65964d62000-07-11 09:16:22 +1000961 hlim = 0;
962 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
963 if (hlim < 0) {
964 rlim.rlim_max = RLIM_INFINITY;
965 } else if (hlim != 0) {
966 rlim.rlim_max = hlim * mult;
967 }
968 }
Damien Miller5fc85652000-07-09 23:53:07 +1000969
Damien Miller65964d62000-07-11 09:16:22 +1000970 /*
971 * XXX For cpu and fsize the soft limit is set to the hard limit
972 * if the hard limit is left at its default value and the soft limit
973 * is changed from its default value, either by requesting it
974 * (slim == 0) or by setting it to the current default. At least
975 * that's how rlogind does it. If you're confused you're not alone.
976 * Bug or feature? AIX 4.3.1.2
977 */
978 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
979 && hlim == 0 && slim != 0)
980 rlim.rlim_max = rlim.rlim_cur;
981 /* A specified hard limit limits the soft limit */
982 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
983 rlim.rlim_cur = rlim.rlim_max;
984 /* A soft limit can increase a hard limit */
985 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000986 rlim.rlim_max = rlim.rlim_cur;
987
988 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000989 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000990}
991
992void set_limits_from_userattr(char *user)
993{
994 int mask;
995 char buf[16];
996
997 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
998 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
999 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1000 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1001 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1002 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1003#if defined(S_UNOFILE)
1004 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1005#endif
1006
1007 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1008 /* Convert decimal to octal */
1009 (void) snprintf(buf, sizeof(buf), "%d", mask);
1010 if (sscanf(buf, "%o", &mask) == 1)
1011 umask(mask);
1012 }
1013}
1014#endif /* defined(HAVE_GETUSERATTR) */
1015
Damien Millerb38eff82000-04-01 11:09:21 +10001016/*
1017 * Performs common processing for the child, such as setting up the
1018 * environment, closing extra file descriptors, setting the user and group
1019 * ids, and executing the command or shell.
1020 */
Damien Miller4af51302000-04-16 11:18:38 +10001021void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001022do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001023{
Damien Miller7b28dc52000-09-05 13:34:53 +11001024 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstromec95ed92001-07-04 04:21:14 +00001025 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001026 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001027 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001028 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001029 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001030 char **env;
1031 extern char **environ;
1032 struct stat st;
1033 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001034 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +10001035#ifdef WITH_IRIX_PROJECT
1036 prid_t projid;
1037#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001038#ifdef WITH_IRIX_JOBS
1039 jid_t jid = 0;
1040#else
1041#ifdef WITH_IRIX_ARRAY
1042 int jid = 0;
1043#endif /* WITH_IRIX_ARRAY */
1044#endif /* WITH_IRIX_JOBS */
1045
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001046 do_xauth =
1047 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1048
Ben Lindstrom005dd222001-04-18 15:29:33 +00001049 /* remove hostkey from the child's memory */
1050 destroy_sensitive_data();
1051
Damien Millerd3a18572000-06-07 19:55:44 +10001052 /* login(1) is only called if we execute the login shell */
1053 if (options.use_login && command != NULL)
1054 options.use_login = 0;
1055
Damien Millerc62f1fc2001-07-14 11:54:05 +10001056#if !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001057 if (!options.use_login) {
1058# ifdef HAVE_LOGIN_CAP
1059 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1060 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1061 _PATH_NOLOGIN), "r");
1062# else /* HAVE_LOGIN_CAP */
1063 if (pw->pw_uid)
1064 f = fopen(_PATH_NOLOGIN, "r");
1065# endif /* HAVE_LOGIN_CAP */
1066 if (f) {
1067 /* /etc/nologin exists. Print its contents and exit. */
1068 while (fgets(buf, sizeof(buf), f))
1069 fputs(buf, stderr);
1070 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001071 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001072 }
Damien Millerb38eff82000-04-01 11:09:21 +10001073 }
Damien Millerc62f1fc2001-07-14 11:54:05 +10001074#endif /* HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001075
Damien Millerad833b32000-08-23 10:46:23 +10001076 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001077 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1078 switch, so we let login(1) to this for us. */
1079 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001080#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001081 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001082 if (!check_quietlogin(s, command))
1083 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001084#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001085#ifdef HAVE_CYGWIN
1086 if (is_winnt) {
1087#else
Damien Millerb38eff82000-04-01 11:09:21 +10001088 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001089#endif
Damien Millerad833b32000-08-23 10:46:23 +10001090# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001091 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001092# endif /* HAVE_GETUSERATTR */
1093# ifdef HAVE_LOGIN_CAP
1094 if (setusercontext(lc, pw, pw->pw_uid,
1095 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1096 perror("unable to set user context");
1097 exit(1);
1098 }
1099# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001100#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1101 /* Sets login uid for accounting */
1102 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1103 error("setluid: %s", strerror(errno));
1104#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1105
Damien Millerad833b32000-08-23 10:46:23 +10001106 if (setlogin(pw->pw_name) < 0)
1107 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001108 if (setgid(pw->pw_gid) < 0) {
1109 perror("setgid");
1110 exit(1);
1111 }
1112 /* Initialize the group list. */
1113 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1114 perror("initgroups");
1115 exit(1);
1116 }
1117 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001118# ifdef USE_PAM
1119 /*
1120 * PAM credentials may take the form of
1121 * supplementary groups. These will have been
1122 * wiped by the above initgroups() call.
1123 * Reestablish them here.
1124 */
1125 do_pam_setcred(0);
1126# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001127# ifdef WITH_IRIX_JOBS
1128 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1129 if (jid == -1) {
1130 fatal("Failed to create job container: %.100s",
1131 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001132 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001133# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001134# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001135 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001136 if (jid == 0) {
1137 if (newarraysess() != 0)
1138 fatal("Failed to set up new array session: %.100s",
1139 strerror(errno));
1140 }
Damien Millerad833b32000-08-23 10:46:23 +10001141# endif /* WITH_IRIX_ARRAY */
1142# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001143 /* initialize irix project info */
1144 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1145 debug("Failed to get project id, using projid 0");
1146 projid = 0;
1147 }
Damien Miller91606b12000-06-28 08:22:29 +10001148 if (setprid(projid))
1149 fatal("Failed to initialize project %d for %s: %.100s",
1150 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001151# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001152#ifdef WITH_IRIX_AUDIT
1153 if (sysconf(_SC_AUDIT)) {
1154 debug("Setting sat id to %d", (int) pw->pw_uid);
1155 if (satsetid(pw->pw_uid))
1156 debug("error setting satid: %.100s", strerror(errno));
1157 }
1158#endif /* WITH_IRIX_AUDIT */
1159
Damien Miller168a7002001-03-17 10:29:50 +11001160#ifdef _AIX
1161 /*
1162 * AIX has a "usrinfo" area where logname and
1163 * other stuff is stored - a few applications
1164 * actually use this and die if it's not set
1165 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001166 if (s->ttyfd == -1)
1167 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001168 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001169 2 * strlen(pw->pw_name));
1170 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001171 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001172 if (usrinfo(SETUINFO, cp, i) == -1)
1173 fatal("Couldn't set usrinfo: %s",
1174 strerror(errno));
1175 debug3("AIX/UsrInfo: set len %d", i);
1176 xfree(cp);
1177#endif
1178
Damien Millerb38eff82000-04-01 11:09:21 +10001179 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001180 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001181# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001182 }
Damien Millerad833b32000-08-23 10:46:23 +10001183#endif /* HAVE_OSF_SIA */
1184
Damien Millerbac2d8a2000-09-05 16:13:06 +11001185#ifdef HAVE_CYGWIN
1186 if (is_winnt)
1187#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001188 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001189 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001190 }
1191 /*
1192 * Get the shell from the password data. An empty shell field is
1193 * legal, and means /bin/sh.
1194 */
1195 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001196#ifdef HAVE_LOGIN_CAP
1197 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1198#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001199
Damien Millerb38eff82000-04-01 11:09:21 +10001200 /* Initialize the environment. */
1201 envsize = 100;
1202 env = xmalloc(envsize * sizeof(char *));
1203 env[0] = NULL;
1204
Damien Millerbac2d8a2000-09-05 16:13:06 +11001205#ifdef HAVE_CYGWIN
1206 /*
1207 * The Windows environment contains some setting which are
1208 * important for a running system. They must not be dropped.
1209 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001210 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001211#endif
1212
Damien Millerb38eff82000-04-01 11:09:21 +10001213 if (!options.use_login) {
1214 /* Set basic environment. */
1215 child_set_env(&env, &envsize, "USER", pw->pw_name);
1216 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1217 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001218#ifdef HAVE_LOGIN_CAP
1219 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1220 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001221#else /* HAVE_LOGIN_CAP */
1222# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001223 /*
1224 * There's no standard path on Windows. The path contains
1225 * important components pointing to the system directories,
1226 * needed for loading shared libraries. So the path better
1227 * remains intact here.
1228 */
Damien Millerb38eff82000-04-01 11:09:21 +10001229 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001230# endif /* HAVE_CYGWIN */
1231#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001232
1233 snprintf(buf, sizeof buf, "%.200s/%.50s",
1234 _PATH_MAILDIR, pw->pw_name);
1235 child_set_env(&env, &envsize, "MAIL", buf);
1236
1237 /* Normal systems set SHELL by default. */
1238 child_set_env(&env, &envsize, "SHELL", shell);
1239 }
1240 if (getenv("TZ"))
1241 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1242
1243 /* Set custom environment options from RSA authentication. */
1244 while (custom_environment) {
1245 struct envstring *ce = custom_environment;
1246 char *s = ce->s;
1247 int i;
1248 for (i = 0; s[i] != '=' && s[i]; i++);
1249 if (s[i] == '=') {
1250 s[i] = 0;
1251 child_set_env(&env, &envsize, s, s + i + 1);
1252 }
1253 custom_environment = ce->next;
1254 xfree(ce->s);
1255 xfree(ce);
1256 }
1257
1258 snprintf(buf, sizeof buf, "%.50s %d %d",
1259 get_remote_ipaddr(), get_remote_port(), get_local_port());
1260 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1261
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001262 if (s->ttyfd != -1)
1263 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1264 if (s->term)
1265 child_set_env(&env, &envsize, "TERM", s->term);
1266 if (s->display)
1267 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001268 if (original_command)
1269 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1270 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001271
1272#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001273 if ((cp = getenv("AUTHSTATE")) != NULL)
1274 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1275 if ((cp = getenv("KRB5CCNAME")) != NULL)
1276 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1277 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001278#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001279#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +00001280 if (s->authctxt->krb4_ticket_file)
1281 child_set_env(&env, &envsize, "KRBTKFILE",
1282 s->authctxt->krb4_ticket_file);
1283#endif
1284#ifdef KRB5
1285 if (s->authctxt->krb5_ticket_file)
1286 child_set_env(&env, &envsize, "KRB5CCNAME",
1287 s->authctxt->krb5_ticket_file);
1288#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001289#ifdef USE_PAM
1290 /* Pull in any environment variables that may have been set by PAM. */
1291 do_pam_environment(&env, &envsize);
1292#endif /* USE_PAM */
1293
Damien Millerb38eff82000-04-01 11:09:21 +10001294 if (auth_get_socket_name() != NULL)
1295 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1296 auth_get_socket_name());
1297
1298 /* read $HOME/.ssh/environment. */
1299 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001300 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1301 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001302 read_environment_file(&env, &envsize, buf);
1303 }
1304 if (debug_flag) {
1305 /* dump the environment */
1306 fprintf(stderr, "Environment:\n");
1307 for (i = 0; env[i]; i++)
1308 fprintf(stderr, " %.200s\n", env[i]);
1309 }
Damien Millerad833b32000-08-23 10:46:23 +10001310 /* we have to stash the hostname before we close our socket. */
1311 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001312 hostname = get_remote_name_or_ip(utmp_len,
1313 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001314 /*
1315 * Close the connection descriptors; note that this is the child, and
1316 * the server will still have the socket open, and it is important
1317 * that we do not shutdown it. Note that the descriptors cannot be
1318 * closed before building the environment, as we call
1319 * get_remote_ipaddr there.
1320 */
1321 if (packet_get_connection_in() == packet_get_connection_out())
1322 close(packet_get_connection_in());
1323 else {
1324 close(packet_get_connection_in());
1325 close(packet_get_connection_out());
1326 }
1327 /*
1328 * Close all descriptors related to channels. They will still remain
1329 * open in the parent.
1330 */
1331 /* XXX better use close-on-exec? -markus */
1332 channel_close_all();
1333
1334 /*
1335 * Close any extra file descriptors. Note that there may still be
1336 * descriptors left by system functions. They will be closed later.
1337 */
1338 endpwent();
1339
1340 /*
1341 * Close any extra open file descriptors so that we don\'t have them
1342 * hanging around in clients. Note that we want to do this after
1343 * initgroups, because at least on Solaris 2.3 it leaves file
1344 * descriptors open.
1345 */
1346 for (i = 3; i < 64; i++)
1347 close(i);
1348
1349 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001350 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001351 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1352 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001353#ifdef HAVE_LOGIN_CAP
1354 if (login_getcapbool(lc, "requirehome", 0))
1355 exit(1);
1356#endif
1357 }
Damien Millerb38eff82000-04-01 11:09:21 +10001358
1359 /*
1360 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1361 * xauth are run in the proper environment.
1362 */
1363 environ = env;
1364
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001365#ifdef AFS
1366 /* Try to get AFS tokens for the local cell. */
1367 if (k_hasafs()) {
1368 char cell[64];
1369
1370 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1371 krb_afslog(cell, 0);
1372
1373 krb_afslog(0, 0);
1374 }
1375#endif /* AFS */
1376
Damien Millerb38eff82000-04-01 11:09:21 +10001377 /*
1378 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1379 * in this order).
1380 */
1381 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001382 /* ignore _PATH_SSH_USER_RC for subsystems */
1383 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001384 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1385 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001386 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001387 fprintf(stderr, "Running %s\n", cmd);
1388 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001389 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001390 if (do_xauth)
1391 fprintf(f, "%s %s\n", s->auth_proto,
1392 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001393 pclose(f);
1394 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001395 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001396 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001397 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001398 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001399 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1400 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001401 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001402 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001403 if (do_xauth)
1404 fprintf(f, "%s %s\n", s->auth_proto,
1405 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001406 pclose(f);
1407 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001408 fprintf(stderr, "Could not run %s\n",
1409 _PATH_SSH_SYSTEM_RC);
1410 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001411 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001412 char *screen = strchr(s->display, ':');
1413
1414 if (debug_flag) {
1415 fprintf(stderr,
1416 "Running %.100s add "
1417 "%.100s %.100s %.100s\n",
1418 options.xauth_location, s->display,
1419 s->auth_proto, s->auth_data);
1420 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001421 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001422 "Adding %.*s/unix%s %s %s\n",
1423 (int)(screen - s->display),
1424 s->display, screen,
1425 s->auth_proto, s->auth_data);
1426 }
1427 snprintf(cmd, sizeof cmd, "%s -q -",
1428 options.xauth_location);
1429 f = popen(cmd, "w");
1430 if (f) {
1431 fprintf(f, "add %s %s %s\n", s->display,
1432 s->auth_proto, s->auth_data);
1433 if (screen != NULL)
1434 fprintf(f, "add %.*s/unix%s %s %s\n",
1435 (int)(screen - s->display),
1436 s->display, screen,
1437 s->auth_proto,
1438 s->auth_data);
1439 pclose(f);
1440 } else {
1441 fprintf(stderr, "Could not run %s\n",
1442 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001443 }
1444 }
Damien Millerb38eff82000-04-01 11:09:21 +10001445 /* Get the last component of the shell name. */
1446 cp = strrchr(shell, '/');
1447 if (cp)
1448 cp++;
1449 else
1450 cp = shell;
1451 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001452
1453 /* restore SIGPIPE for child */
1454 signal(SIGPIPE, SIG_DFL);
1455
Damien Millerb38eff82000-04-01 11:09:21 +10001456 /*
1457 * If we have no command, execute the shell. In this case, the shell
1458 * name to be passed in argv[0] is preceded by '-' to indicate that
1459 * this is a login shell.
1460 */
1461 if (!command) {
1462 if (!options.use_login) {
1463 char buf[256];
1464
Damien Millerb38eff82000-04-01 11:09:21 +10001465 /* Start the shell. Set initial character to '-'. */
1466 buf[0] = '-';
1467 strncpy(buf + 1, cp, sizeof(buf) - 1);
1468 buf[sizeof(buf) - 1] = 0;
1469
1470 /* Execute the shell. */
1471 argv[0] = buf;
1472 argv[1] = NULL;
1473 execve(shell, argv, env);
1474
1475 /* Executing the shell failed. */
1476 perror(shell);
1477 exit(1);
1478
1479 } else {
1480 /* Launch login(1). */
1481
Damien Millerad833b32000-08-23 10:46:23 +10001482 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001483#ifdef LOGIN_NEEDS_TERM
1484 s->term? s->term : "unknown",
1485#endif
Damien Millerefb1edf2001-07-14 12:19:36 +10001486 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001487
1488 /* Login couldn't be executed, die. */
1489
1490 perror("login");
1491 exit(1);
1492 }
1493 }
1494 /*
1495 * Execute the command using the user's shell. This uses the -c
1496 * option to execute the command.
1497 */
1498 argv[0] = (char *) cp;
1499 argv[1] = "-c";
1500 argv[2] = (char *) command;
1501 argv[3] = NULL;
1502 execve(shell, argv, env);
1503 perror(shell);
1504 exit(1);
1505}
1506
1507Session *
1508session_new(void)
1509{
1510 int i;
1511 static int did_init = 0;
1512 if (!did_init) {
1513 debug("session_new: init");
1514 for(i = 0; i < MAX_SESSIONS; i++) {
1515 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001516 }
1517 did_init = 1;
1518 }
1519 for(i = 0; i < MAX_SESSIONS; i++) {
1520 Session *s = &sessions[i];
1521 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001522 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001523 s->chanid = -1;
1524 s->ptyfd = -1;
1525 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001526 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001527 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001528 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001529 return s;
1530 }
1531 }
1532 return NULL;
1533}
1534
Ben Lindstrombba81212001-06-25 05:01:22 +00001535static void
Damien Millerb38eff82000-04-01 11:09:21 +10001536session_dump(void)
1537{
1538 int i;
1539 for(i = 0; i < MAX_SESSIONS; i++) {
1540 Session *s = &sessions[i];
1541 debug("dump: used %d session %d %p channel %d pid %d",
1542 s->used,
1543 s->self,
1544 s,
1545 s->chanid,
1546 s->pid);
1547 }
1548}
1549
Damien Millerefb4afe2000-04-12 18:45:05 +10001550int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001551session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001552{
1553 Session *s = session_new();
1554 debug("session_open: channel %d", chanid);
1555 if (s == NULL) {
1556 error("no more sessions");
1557 return 0;
1558 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001559 s->authctxt = authctxt;
1560 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001561 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001562 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001563 debug("session_open: session %d: link with channel %d", s->self, chanid);
1564 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001565 return 1;
1566}
1567
Ben Lindstrombba81212001-06-25 05:01:22 +00001568static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001569session_by_channel(int id)
1570{
1571 int i;
1572 for(i = 0; i < MAX_SESSIONS; i++) {
1573 Session *s = &sessions[i];
1574 if (s->used && s->chanid == id) {
1575 debug("session_by_channel: session %d channel %d", i, id);
1576 return s;
1577 }
1578 }
1579 debug("session_by_channel: unknown channel %d", id);
1580 session_dump();
1581 return NULL;
1582}
1583
Ben Lindstrombba81212001-06-25 05:01:22 +00001584static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001585session_by_pid(pid_t pid)
1586{
1587 int i;
1588 debug("session_by_pid: pid %d", pid);
1589 for(i = 0; i < MAX_SESSIONS; i++) {
1590 Session *s = &sessions[i];
1591 if (s->used && s->pid == pid)
1592 return s;
1593 }
1594 error("session_by_pid: unknown pid %d", pid);
1595 session_dump();
1596 return NULL;
1597}
1598
Ben Lindstrombba81212001-06-25 05:01:22 +00001599static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001600session_window_change_req(Session *s)
1601{
1602 s->col = packet_get_int();
1603 s->row = packet_get_int();
1604 s->xpixel = packet_get_int();
1605 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001606 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001607 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1608 return 1;
1609}
1610
Ben Lindstrombba81212001-06-25 05:01:22 +00001611static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001612session_pty_req(Session *s)
1613{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001614 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001615 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001616
Ben Lindstrom49c12602001-06-13 04:37:36 +00001617 if (no_pty_flag) {
1618 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001619 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001620 }
1621 if (s->ttyfd != -1) {
1622 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001623 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001624 }
1625
Damien Millerefb4afe2000-04-12 18:45:05 +10001626 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001627
1628 if (compat20) {
1629 s->col = packet_get_int();
1630 s->row = packet_get_int();
1631 } else {
1632 s->row = packet_get_int();
1633 s->col = packet_get_int();
1634 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001635 s->xpixel = packet_get_int();
1636 s->ypixel = packet_get_int();
1637
1638 if (strcmp(s->term, "") == 0) {
1639 xfree(s->term);
1640 s->term = NULL;
1641 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001642
Damien Millerefb4afe2000-04-12 18:45:05 +10001643 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001644 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001645 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001646 if (s->term)
1647 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001648 s->term = NULL;
1649 s->ptyfd = -1;
1650 s->ttyfd = -1;
1651 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001652 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001653 }
1654 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001655
1656 /* for SSH1 the tty modes length is not given */
1657 if (!compat20)
1658 n_bytes = packet_remaining();
1659 tty_parse_modes(s->ttyfd, &n_bytes);
1660
Damien Millerefb4afe2000-04-12 18:45:05 +10001661 /*
1662 * Add a cleanup function to clear the utmp entry and record logout
1663 * time in case we call fatal() (e.g., the connection gets closed).
1664 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001665 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001666 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001667
1668 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001669 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1670
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001671 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001672 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001673 return 1;
1674}
1675
Ben Lindstrombba81212001-06-25 05:01:22 +00001676static int
Damien Millerbd483e72000-04-30 10:00:53 +10001677session_subsystem_req(Session *s)
1678{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001679 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001680 int success = 0;
1681 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001682 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001683
1684 packet_done();
1685 log("subsystem request for %s", subsys);
1686
Damien Millerf6d9e222000-06-18 14:50:44 +10001687 for (i = 0; i < options.num_subsystems; i++) {
1688 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1689 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001690 s->is_subsystem = 1;
Ben Lindstromb09f6b52001-09-14 23:12:07 +00001691 do_exec(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001692 success = 1;
1693 }
1694 }
1695
1696 if (!success)
1697 log("subsystem request for %s failed, subsystem not found", subsys);
1698
Damien Millerbd483e72000-04-30 10:00:53 +10001699 xfree(subsys);
1700 return success;
1701}
1702
Ben Lindstrombba81212001-06-25 05:01:22 +00001703static int
Damien Millerbd483e72000-04-30 10:00:53 +10001704session_x11_req(Session *s)
1705{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001706 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001707
1708 s->single_connection = packet_get_char();
1709 s->auth_proto = packet_get_string(NULL);
1710 s->auth_data = packet_get_string(NULL);
1711 s->screen = packet_get_int();
1712 packet_done();
1713
Ben Lindstrom768176b2001-06-09 01:29:12 +00001714 success = session_setup_x11fwd(s);
1715 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001716 xfree(s->auth_proto);
1717 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001718 s->auth_proto = NULL;
1719 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001720 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001721 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001722}
1723
Ben Lindstrombba81212001-06-25 05:01:22 +00001724static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001725session_shell_req(Session *s)
1726{
Damien Millerf6d9e222000-06-18 14:50:44 +10001727 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001728 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001729 return 1;
1730}
1731
Ben Lindstrombba81212001-06-25 05:01:22 +00001732static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001733session_exec_req(Session *s)
1734{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001735 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001736 char *command = packet_get_string(&len);
1737 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001738 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001739 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001740 return 1;
1741}
1742
Ben Lindstrombba81212001-06-25 05:01:22 +00001743static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001744session_auth_agent_req(Session *s)
1745{
1746 static int called = 0;
1747 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001748 if (no_agent_forwarding_flag) {
1749 debug("session_auth_agent_req: no_agent_forwarding_flag");
1750 return 0;
1751 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001752 if (called) {
1753 return 0;
1754 } else {
1755 called = 1;
1756 return auth_input_request_forwarding(s->pw);
1757 }
1758}
1759
Damien Millerefb4afe2000-04-12 18:45:05 +10001760void
1761session_input_channel_req(int id, void *arg)
1762{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001763 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001764 int reply;
1765 int success = 0;
1766 char *rtype;
1767 Session *s;
1768 Channel *c;
1769
1770 rtype = packet_get_string(&len);
1771 reply = packet_get_char();
1772
1773 s = session_by_channel(id);
1774 if (s == NULL)
1775 fatal("session_input_channel_req: channel %d: no session", id);
1776 c = channel_lookup(id);
1777 if (c == NULL)
1778 fatal("session_input_channel_req: channel %d: bad channel", id);
1779
1780 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1781 s->self, id, rtype, reply);
1782
1783 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001784 * a session is in LARVAL state until a shell, a command
1785 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001786 */
1787 if (c->type == SSH_CHANNEL_LARVAL) {
1788 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001789 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001790 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001791 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001792 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001793 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001794 } else if (strcmp(rtype, "x11-req") == 0) {
1795 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001796 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1797 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001798 } else if (strcmp(rtype, "subsystem") == 0) {
1799 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001800 }
1801 }
1802 if (strcmp(rtype, "window-change") == 0) {
1803 success = session_window_change_req(s);
1804 }
1805
1806 if (reply) {
1807 packet_start(success ?
1808 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1809 packet_put_int(c->remote_id);
1810 packet_send();
1811 }
1812 xfree(rtype);
1813}
1814
1815void
1816session_set_fds(Session *s, int fdin, int fdout, int fderr)
1817{
1818 if (!compat20)
1819 fatal("session_set_fds: called for proto != 2.0");
1820 /*
1821 * now that have a child and a pipe to the child,
1822 * we can activate our channel and register the fd's
1823 */
1824 if (s->chanid == -1)
1825 fatal("no channel for session %d", s->self);
1826 channel_set_fds(s->chanid,
1827 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001828 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1829 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001830}
1831
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001832/*
1833 * Function to perform pty cleanup. Also called if we get aborted abnormally
1834 * (e.g., due to a dropped connection).
1835 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001836static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001837session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001838{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001839 Session *s = session;
1840
1841 if (s == NULL) {
1842 error("session_pty_cleanup: no session");
1843 return;
1844 }
1845 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001846 return;
1847
Kevin Steves43cdef32001-02-11 14:12:08 +00001848 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001849
Damien Millerb38eff82000-04-01 11:09:21 +10001850 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001851 if (s->pid != 0)
1852 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001853
1854 /* Release the pseudo-tty. */
1855 pty_release(s->tty);
1856
1857 /*
1858 * Close the server side of the socket pairs. We must do this after
1859 * the pty cleanup, so that another process doesn't get this pty
1860 * while we're still cleaning up.
1861 */
1862 if (close(s->ptymaster) < 0)
1863 error("close(s->ptymaster): %s", strerror(errno));
1864}
Damien Millerefb4afe2000-04-12 18:45:05 +10001865
Ben Lindstrombba81212001-06-25 05:01:22 +00001866static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001867session_exit_message(Session *s, int status)
1868{
1869 Channel *c;
1870 if (s == NULL)
1871 fatal("session_close: no session");
1872 c = channel_lookup(s->chanid);
1873 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001874 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001875 s->self, s->chanid);
1876 debug("session_exit_message: session %d channel %d pid %d",
1877 s->self, s->chanid, s->pid);
1878
1879 if (WIFEXITED(status)) {
1880 channel_request_start(s->chanid,
1881 "exit-status", 0);
1882 packet_put_int(WEXITSTATUS(status));
1883 packet_send();
1884 } else if (WIFSIGNALED(status)) {
1885 channel_request_start(s->chanid,
1886 "exit-signal", 0);
1887 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001888#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001889 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001890#else /* WCOREDUMP */
1891 packet_put_char(0);
1892#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001893 packet_put_cstring("");
1894 packet_put_cstring("");
1895 packet_send();
1896 } else {
1897 /* Some weird exit cause. Just exit. */
1898 packet_disconnect("wait returned status %04x.", status);
1899 }
1900
1901 /* disconnect channel */
1902 debug("session_exit_message: release channel %d", s->chanid);
1903 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001904 /*
1905 * emulate a write failure with 'chan_write_failed', nobody will be
1906 * interested in data we write.
1907 * Note that we must not call 'chan_read_failed', since there could
1908 * be some more data waiting in the pipe.
1909 */
Damien Millerbd483e72000-04-30 10:00:53 +10001910 if (c->ostate != CHAN_OUTPUT_CLOSED)
1911 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001912 s->chanid = -1;
1913}
1914
Ben Lindstrombba81212001-06-25 05:01:22 +00001915static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001916session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001917{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001918 debug("session_close: session %d pid %d", s->self, s->pid);
1919 if (s->ttyfd != -1) {
1920 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1921 session_pty_cleanup(s);
1922 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001923 if (s->term)
1924 xfree(s->term);
1925 if (s->display)
1926 xfree(s->display);
1927 if (s->auth_data)
1928 xfree(s->auth_data);
1929 if (s->auth_proto)
1930 xfree(s->auth_proto);
1931 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001932 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001933}
1934
1935void
1936session_close_by_pid(pid_t pid, int status)
1937{
1938 Session *s = session_by_pid(pid);
1939 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001940 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001941 return;
1942 }
1943 if (s->chanid != -1)
1944 session_exit_message(s, status);
1945 session_close(s);
1946}
1947
Ben Lindstrom4983d5e2001-07-04 05:17:40 +00001948int
1949session_have_children(void)
1950{
1951 int i;
1952
1953 for(i = 0; i < MAX_SESSIONS; i++) {
1954 Session *s = &sessions[i];
1955 if (s->used && s->pid != -1) {
1956 debug("session_have_children: id %d pid %d", i, s->pid);
1957 return 1;
1958 }
1959 }
1960 debug("session_have_children: no more children");
1961 return 0;
1962}
1963
Damien Millerefb4afe2000-04-12 18:45:05 +10001964/*
1965 * this is called when a channel dies before
1966 * the session 'child' itself dies
1967 */
1968void
1969session_close_by_channel(int id, void *arg)
1970{
1971 Session *s = session_by_channel(id);
1972 if (s == NULL) {
1973 debug("session_close_by_channel: no session for channel %d", id);
1974 return;
1975 }
1976 /* disconnect channel */
1977 channel_cancel_cleanup(s->chanid);
1978 s->chanid = -1;
1979
1980 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1981 if (s->pid == 0) {
1982 /* close session immediately */
1983 session_close(s);
1984 } else {
1985 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001986 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001987 error("session_close_by_channel: kill %d: %s",
1988 s->pid, strerror(errno));
1989 }
1990}
1991
Ben Lindstrombba81212001-06-25 05:01:22 +00001992static char *
Damien Millere247cc42000-05-07 12:03:14 +10001993session_tty_list(void)
1994{
1995 static char buf[1024];
1996 int i;
1997 buf[0] = '\0';
1998 for(i = 0; i < MAX_SESSIONS; i++) {
1999 Session *s = &sessions[i];
2000 if (s->used && s->ttyfd != -1) {
2001 if (buf[0] != '\0')
2002 strlcat(buf, ",", sizeof buf);
2003 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2004 }
2005 }
2006 if (buf[0] == '\0')
2007 strlcpy(buf, "notty", sizeof buf);
2008 return buf;
2009}
2010
2011void
2012session_proctitle(Session *s)
2013{
2014 if (s->pw == NULL)
2015 error("no user for session %d", s->self);
2016 else
2017 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2018}
2019
Ben Lindstrom768176b2001-06-09 01:29:12 +00002020int
2021session_setup_x11fwd(Session *s)
2022{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002023 struct stat st;
2024
2025 if (no_x11_forwarding_flag) {
2026 packet_send_debug("X11 forwarding disabled in user configuration file.");
2027 return 0;
2028 }
2029 if (!options.x11_forwarding) {
2030 debug("X11 forwarding disabled in server configuration file.");
2031 return 0;
2032 }
2033 if (!options.xauth_location ||
2034 (stat(options.xauth_location, &st) == -1)) {
2035 packet_send_debug("No xauth program; cannot forward with spoofing.");
2036 return 0;
2037 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002038 if (options.use_login) {
2039 packet_send_debug("X11 forwarding disabled; "
2040 "not compatible with UseLogin=yes.");
2041 return 0;
2042 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002043 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002044 debug("X11 display already set.");
2045 return 0;
2046 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002047 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
2048 if (s->display == NULL) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002049 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002050 return 0;
2051 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002052 return 1;
2053}
2054
Ben Lindstrombba81212001-06-25 05:01:22 +00002055static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002056do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002057{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002058 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002059}