blob: 0e3e933f91d3b66fddbb7ac4794e3051b08bf35c [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"
Damien Miller139d4cd2001-10-10 15:07:44 +100036RCSID("$OpenBSD: session.c,v 1.104 2001/10/09 10:12:08 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
Damien Miller9c751422001-10-10 15:02:46 +1000563 /* Redirect stdin/stdout/stderr from the pseudo tty. */
564 if (dup2(ttyfd, 0) < 0)
565 error("dup2 stdin: %s", strerror(errno));
566 if (dup2(ttyfd, 1) < 0)
567 error("dup2 stdout: %s", strerror(errno));
568 if (dup2(ttyfd, 2) < 0)
569 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000570
571 /* Close the extra descriptor for the pseudo tty. */
572 close(ttyfd);
573
Damien Miller942da032000-08-18 13:59:06 +1000574 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000575#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100576 if (!(options.use_login && command == NULL))
577 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000578# ifdef LOGIN_NEEDS_UTMPX
579 else
580 do_pre_login(s);
581# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000582#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000583
Damien Millerb38eff82000-04-01 11:09:21 +1000584 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000585 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000586 /* NOTREACHED */
587 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100588#ifdef HAVE_CYGWIN
589 if (is_winnt)
590 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
591#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000592 if (pid < 0)
593 packet_disconnect("fork failed: %.100s", strerror(errno));
594 s->pid = pid;
595
596 /* Parent. Close the slave side of the pseudo tty. */
597 close(ttyfd);
598
599 /*
600 * Create another descriptor of the pty master side for use as the
601 * standard input. We could use the original descriptor, but this
602 * simplifies code in server_loop. The descriptor is bidirectional.
603 */
604 fdout = dup(ptyfd);
605 if (fdout < 0)
606 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
607
608 /* we keep a reference to the pty master */
609 ptymaster = dup(ptyfd);
610 if (ptymaster < 0)
611 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
612 s->ptymaster = ptymaster;
613
614 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000615 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000616 if (compat20) {
617 session_set_fds(s, ptyfd, fdout, -1);
618 } else {
619 server_loop(pid, ptyfd, fdout, -1);
620 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000621 }
Damien Millerb38eff82000-04-01 11:09:21 +1000622}
623
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000624#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000625static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000626do_pre_login(Session *s)
627{
628 socklen_t fromlen;
629 struct sockaddr_storage from;
630 pid_t pid = getpid();
631
632 /*
633 * Get IP address of client. If the connection is not a socket, let
634 * the address be 0.0.0.0.
635 */
636 memset(&from, 0, sizeof(from));
637 if (packet_connection_is_on_socket()) {
638 fromlen = sizeof(from);
639 if (getpeername(packet_get_connection_in(),
640 (struct sockaddr *) & from, &fromlen) < 0) {
641 debug("getpeername: %.100s", strerror(errno));
642 fatal_cleanup();
643 }
644 }
645
646 record_utmp_only(pid, s->tty, s->pw->pw_name,
647 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
648 (struct sockaddr *)&from);
649}
650#endif
651
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000652/*
653 * This is called to fork and execute a command. If another command is
654 * to be forced, execute that instead.
655 */
656void
657do_exec(Session *s, const char *command)
658{
659 if (forced_command) {
660 original_command = command;
661 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000662 debug("Forced command '%.900s'", command);
663 }
664
665 if (s->ttyfd != -1)
666 do_exec_pty(s, command);
667 else
668 do_exec_no_pty(s, command);
669
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000670 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000671}
672
Damien Miller942da032000-08-18 13:59:06 +1000673/* administrative, login(1)-like work */
674void
Damien Miller69b69aa2000-10-28 14:19:58 +1100675do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000676{
Damien Miller942da032000-08-18 13:59:06 +1000677 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100678 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000679 socklen_t fromlen;
680 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000681 time_t last_login_time;
682 struct passwd * pw = s->pw;
683 pid_t pid = getpid();
684
685 /*
686 * Get IP address of client. If the connection is not a socket, let
687 * the address be 0.0.0.0.
688 */
689 memset(&from, 0, sizeof(from));
690 if (packet_connection_is_on_socket()) {
691 fromlen = sizeof(from);
692 if (getpeername(packet_get_connection_in(),
693 (struct sockaddr *) & from, &fromlen) < 0) {
694 debug("getpeername: %.100s", strerror(errno));
695 fatal_cleanup();
696 }
697 }
698
Damien Miller7b28dc52000-09-05 13:34:53 +1100699 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000700 if (options.print_lastlog) {
701 hostname[0] = '\0';
702 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
703 hostname, sizeof(hostname));
704 }
Damien Millere4340be2000-09-16 13:29:08 +1100705
Damien Miller942da032000-08-18 13:59:06 +1000706 /* Record that there was a login on that tty from the remote host. */
707 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000708 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
709 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000710
Kevin Steves092f2ef2000-10-14 13:36:13 +0000711#ifdef USE_PAM
712 /*
713 * If password change is needed, do it now.
714 * This needs to occur before the ~/.hushlogin check.
715 */
Damien Miller646aa602001-02-15 11:51:32 +1100716 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000717 print_pam_messages();
718 do_pam_chauthtok();
719 }
720#endif
721
Damien Millercf205e82001-04-16 18:29:15 +1000722 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000723 return;
724
725#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100726 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000727 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000728#endif /* USE_PAM */
729#ifdef WITH_AIXAUTHENTICATE
730 if (aixloginmsg && *aixloginmsg)
731 printf("%s\n", aixloginmsg);
732#endif /* WITH_AIXAUTHENTICATE */
733
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000734 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000735 time_string = ctime(&last_login_time);
736 if (strchr(time_string, '\n'))
737 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000738 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000739 printf("Last login: %s\r\n", time_string);
740 else
Damien Millere4340be2000-09-16 13:29:08 +1100741 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000742 }
Damien Millercf205e82001-04-16 18:29:15 +1000743
744 do_motd();
745}
746
747/*
748 * Display the message of the day.
749 */
750void
751do_motd(void)
752{
753 FILE *f;
754 char buf[256];
755
Damien Miller942da032000-08-18 13:59:06 +1000756 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000757#ifdef HAVE_LOGIN_CAP
758 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
759 "/etc/motd"), "r");
760#else
Damien Miller942da032000-08-18 13:59:06 +1000761 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000762#endif
Damien Miller942da032000-08-18 13:59:06 +1000763 if (f) {
764 while (fgets(buf, sizeof(buf), f))
765 fputs(buf, stdout);
766 fclose(f);
767 }
768 }
769}
770
Kevin Steves8f63caa2001-07-04 18:23:02 +0000771
772/*
773 * Check for quiet login, either .hushlogin or command given.
774 */
775int
776check_quietlogin(Session *s, const char *command)
777{
778 char buf[256];
779 struct passwd *pw = s->pw;
780 struct stat st;
781
782 /* Return 1 if .hushlogin exists or a command given. */
783 if (command != NULL)
784 return 1;
785 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
786#ifdef HAVE_LOGIN_CAP
787 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
788 return 1;
789#else
790 if (stat(buf, &st) >= 0)
791 return 1;
792#endif
793 return 0;
794}
795
Damien Millerb38eff82000-04-01 11:09:21 +1000796/*
797 * Sets the value of the given variable in the environment. If the variable
798 * already exists, its value is overriden.
799 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000800static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000801child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000802 const char *value)
803{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000804 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000805 char **env;
806
807 /*
808 * Find the slot where the value should be stored. If the variable
809 * already exists, we reuse the slot; otherwise we append a new slot
810 * at the end of the array, expanding if necessary.
811 */
812 env = *envp;
813 namelen = strlen(name);
814 for (i = 0; env[i]; i++)
815 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
816 break;
817 if (env[i]) {
818 /* Reuse the slot. */
819 xfree(env[i]);
820 } else {
821 /* New variable. Expand if necessary. */
822 if (i >= (*envsizep) - 1) {
823 (*envsizep) += 50;
824 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
825 }
826 /* Need to set the NULL pointer at end of array beyond the new slot. */
827 env[i + 1] = NULL;
828 }
829
830 /* Allocate space and format the variable in the appropriate slot. */
831 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
832 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
833}
834
835/*
836 * Reads environment variables from the given file and adds/overrides them
837 * into the environment. If the file does not exist, this does nothing.
838 * Otherwise, it must consist of empty lines, comments (line starts with '#')
839 * and assignments of the form name=value. No other forms are allowed.
840 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000841static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000842read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000843 const char *filename)
844{
845 FILE *f;
846 char buf[4096];
847 char *cp, *value;
848
849 f = fopen(filename, "r");
850 if (!f)
851 return;
852
853 while (fgets(buf, sizeof(buf), f)) {
854 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
855 ;
856 if (!*cp || *cp == '#' || *cp == '\n')
857 continue;
858 if (strchr(cp, '\n'))
859 *strchr(cp, '\n') = '\0';
860 value = strchr(cp, '=');
861 if (value == NULL) {
862 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
863 continue;
864 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000865 /*
866 * Replace the equals sign by nul, and advance value to
867 * the value string.
868 */
Damien Millerb38eff82000-04-01 11:09:21 +1000869 *value = '\0';
870 value++;
871 child_set_env(env, envsize, cp, value);
872 }
873 fclose(f);
874}
875
876#ifdef USE_PAM
877/*
878 * Sets any environment variables which have been specified by PAM
879 */
Kevin Steves9b26f962001-06-29 17:52:17 +0000880void do_pam_environment(char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000881{
882 char *equals, var_name[512], var_val[512];
883 char **pam_env;
884 int i;
885
886 if ((pam_env = fetch_pam_environment()) == NULL)
887 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000888
Damien Millerb38eff82000-04-01 11:09:21 +1000889 for(i = 0; pam_env[i] != NULL; i++) {
890 if ((equals = strstr(pam_env[i], "=")) == NULL)
891 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000892
Damien Millerb38eff82000-04-01 11:09:21 +1000893 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
894 memset(var_name, '\0', sizeof(var_name));
895 memset(var_val, '\0', sizeof(var_val));
896
897 strncpy(var_name, pam_env[i], equals - pam_env[i]);
898 strcpy(var_val, equals + 1);
899
Damien Millercb5e44a2000-09-29 12:12:36 +1100900 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000901
902 child_set_env(env, envsize, var_name, var_val);
903 }
904 }
905}
906#endif /* USE_PAM */
907
Damien Millercb5e44a2000-09-29 12:12:36 +1100908#ifdef HAVE_CYGWIN
Kevin Steves9b26f962001-06-29 17:52:17 +0000909void copy_environment(char ***env, u_int *envsize)
Damien Millercb5e44a2000-09-29 12:12:36 +1100910{
911 char *equals, var_name[512], var_val[512];
912 int i;
913
914 for(i = 0; environ[i] != NULL; i++) {
915 if ((equals = strstr(environ[i], "=")) == NULL)
916 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000917
Damien Millercb5e44a2000-09-29 12:12:36 +1100918 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
919 memset(var_name, '\0', sizeof(var_name));
920 memset(var_val, '\0', sizeof(var_val));
921
922 strncpy(var_name, environ[i], equals - environ[i]);
923 strcpy(var_val, equals + 1);
924
925 debug3("Copy environment: %s=%s", var_name, var_val);
926
927 child_set_env(env, envsize, var_name, var_val);
928 }
929 }
930}
931#endif
932
Damien Miller5fc85652000-07-09 23:53:07 +1000933#if defined(HAVE_GETUSERATTR)
934/*
935 * AIX-specific login initialisation
936 */
937void set_limit(char *user, char *soft, char *hard, int resource, int mult)
938{
939 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000940 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000941
942 getrlimit(resource, &rlim);
943
Damien Miller65964d62000-07-11 09:16:22 +1000944 slim = 0;
945 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
946 if (slim < 0) {
947 rlim.rlim_cur = RLIM_INFINITY;
948 } else if (slim != 0) {
949 /* See the wackiness below */
950 if (rlim.rlim_cur == slim * mult)
951 slim = 0;
952 else
953 rlim.rlim_cur = slim * mult;
954 }
955 }
Damien Miller5fc85652000-07-09 23:53:07 +1000956
Damien Miller65964d62000-07-11 09:16:22 +1000957 hlim = 0;
958 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
959 if (hlim < 0) {
960 rlim.rlim_max = RLIM_INFINITY;
961 } else if (hlim != 0) {
962 rlim.rlim_max = hlim * mult;
963 }
964 }
Damien Miller5fc85652000-07-09 23:53:07 +1000965
Damien Miller65964d62000-07-11 09:16:22 +1000966 /*
967 * XXX For cpu and fsize the soft limit is set to the hard limit
968 * if the hard limit is left at its default value and the soft limit
969 * is changed from its default value, either by requesting it
970 * (slim == 0) or by setting it to the current default. At least
971 * that's how rlogind does it. If you're confused you're not alone.
972 * Bug or feature? AIX 4.3.1.2
973 */
974 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
975 && hlim == 0 && slim != 0)
976 rlim.rlim_max = rlim.rlim_cur;
977 /* A specified hard limit limits the soft limit */
978 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
979 rlim.rlim_cur = rlim.rlim_max;
980 /* A soft limit can increase a hard limit */
981 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000982 rlim.rlim_max = rlim.rlim_cur;
983
984 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000985 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000986}
987
988void set_limits_from_userattr(char *user)
989{
990 int mask;
991 char buf[16];
992
993 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
994 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
995 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
996 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
997 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
998 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
999#if defined(S_UNOFILE)
1000 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1001#endif
1002
1003 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1004 /* Convert decimal to octal */
1005 (void) snprintf(buf, sizeof(buf), "%d", mask);
1006 if (sscanf(buf, "%o", &mask) == 1)
1007 umask(mask);
1008 }
1009}
1010#endif /* defined(HAVE_GETUSERATTR) */
1011
Damien Millerb38eff82000-04-01 11:09:21 +10001012/*
1013 * Performs common processing for the child, such as setting up the
1014 * environment, closing extra file descriptors, setting the user and group
1015 * ids, and executing the command or shell.
1016 */
Damien Miller4af51302000-04-16 11:18:38 +10001017void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001018do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001019{
Damien Miller7b28dc52000-09-05 13:34:53 +11001020 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstromec95ed92001-07-04 04:21:14 +00001021 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001022 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001023 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001024 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001025 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001026 char **env;
1027 extern char **environ;
1028 struct stat st;
1029 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001030 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +10001031#ifdef WITH_IRIX_PROJECT
1032 prid_t projid;
1033#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001034#ifdef WITH_IRIX_JOBS
1035 jid_t jid = 0;
1036#else
1037#ifdef WITH_IRIX_ARRAY
1038 int jid = 0;
1039#endif /* WITH_IRIX_ARRAY */
1040#endif /* WITH_IRIX_JOBS */
1041
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001042 do_xauth =
1043 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1044
Ben Lindstrom005dd222001-04-18 15:29:33 +00001045 /* remove hostkey from the child's memory */
1046 destroy_sensitive_data();
1047
Damien Millerd3a18572000-06-07 19:55:44 +10001048 /* login(1) is only called if we execute the login shell */
1049 if (options.use_login && command != NULL)
1050 options.use_login = 0;
1051
Damien Millerc62f1fc2001-07-14 11:54:05 +10001052#if !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001053 if (!options.use_login) {
1054# ifdef HAVE_LOGIN_CAP
1055 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1056 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1057 _PATH_NOLOGIN), "r");
1058# else /* HAVE_LOGIN_CAP */
1059 if (pw->pw_uid)
1060 f = fopen(_PATH_NOLOGIN, "r");
1061# endif /* HAVE_LOGIN_CAP */
1062 if (f) {
1063 /* /etc/nologin exists. Print its contents and exit. */
1064 while (fgets(buf, sizeof(buf), f))
1065 fputs(buf, stderr);
1066 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001067 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001068 }
Damien Millerb38eff82000-04-01 11:09:21 +10001069 }
Damien Millerc62f1fc2001-07-14 11:54:05 +10001070#endif /* HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001071
Damien Millerad833b32000-08-23 10:46:23 +10001072 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001073 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1074 switch, so we let login(1) to this for us. */
1075 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001076#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001077 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001078 if (!check_quietlogin(s, command))
1079 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001080#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001081#ifdef HAVE_CYGWIN
1082 if (is_winnt) {
1083#else
Damien Millerb38eff82000-04-01 11:09:21 +10001084 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001085#endif
Damien Millerad833b32000-08-23 10:46:23 +10001086# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001087 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001088# endif /* HAVE_GETUSERATTR */
1089# ifdef HAVE_LOGIN_CAP
1090 if (setusercontext(lc, pw, pw->pw_uid,
1091 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1092 perror("unable to set user context");
1093 exit(1);
1094 }
1095# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001096#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1097 /* Sets login uid for accounting */
1098 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1099 error("setluid: %s", strerror(errno));
1100#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1101
Damien Millerad833b32000-08-23 10:46:23 +10001102 if (setlogin(pw->pw_name) < 0)
1103 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001104 if (setgid(pw->pw_gid) < 0) {
1105 perror("setgid");
1106 exit(1);
1107 }
1108 /* Initialize the group list. */
1109 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1110 perror("initgroups");
1111 exit(1);
1112 }
1113 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001114# ifdef USE_PAM
1115 /*
1116 * PAM credentials may take the form of
1117 * supplementary groups. These will have been
1118 * wiped by the above initgroups() call.
1119 * Reestablish them here.
1120 */
1121 do_pam_setcred(0);
1122# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001123# ifdef WITH_IRIX_JOBS
1124 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1125 if (jid == -1) {
1126 fatal("Failed to create job container: %.100s",
1127 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001128 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001129# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001130# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001131 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001132 if (jid == 0) {
1133 if (newarraysess() != 0)
1134 fatal("Failed to set up new array session: %.100s",
1135 strerror(errno));
1136 }
Damien Millerad833b32000-08-23 10:46:23 +10001137# endif /* WITH_IRIX_ARRAY */
1138# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001139 /* initialize irix project info */
1140 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1141 debug("Failed to get project id, using projid 0");
1142 projid = 0;
1143 }
Damien Miller91606b12000-06-28 08:22:29 +10001144 if (setprid(projid))
1145 fatal("Failed to initialize project %d for %s: %.100s",
1146 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001147# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001148#ifdef WITH_IRIX_AUDIT
1149 if (sysconf(_SC_AUDIT)) {
1150 debug("Setting sat id to %d", (int) pw->pw_uid);
1151 if (satsetid(pw->pw_uid))
1152 debug("error setting satid: %.100s", strerror(errno));
1153 }
1154#endif /* WITH_IRIX_AUDIT */
1155
Damien Miller168a7002001-03-17 10:29:50 +11001156#ifdef _AIX
1157 /*
1158 * AIX has a "usrinfo" area where logname and
1159 * other stuff is stored - a few applications
1160 * actually use this and die if it's not set
1161 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001162 if (s->ttyfd == -1)
1163 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001164 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001165 2 * strlen(pw->pw_name));
1166 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001167 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001168 if (usrinfo(SETUINFO, cp, i) == -1)
1169 fatal("Couldn't set usrinfo: %s",
1170 strerror(errno));
1171 debug3("AIX/UsrInfo: set len %d", i);
1172 xfree(cp);
1173#endif
1174
Damien Millerb38eff82000-04-01 11:09:21 +10001175 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001176 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001177# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001178 }
Damien Millerad833b32000-08-23 10:46:23 +10001179#endif /* HAVE_OSF_SIA */
1180
Damien Millerbac2d8a2000-09-05 16:13:06 +11001181#ifdef HAVE_CYGWIN
1182 if (is_winnt)
1183#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001184 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001185 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001186 }
1187 /*
1188 * Get the shell from the password data. An empty shell field is
1189 * legal, and means /bin/sh.
1190 */
1191 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001192#ifdef HAVE_LOGIN_CAP
1193 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1194#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001195
Damien Millerb38eff82000-04-01 11:09:21 +10001196 /* Initialize the environment. */
1197 envsize = 100;
1198 env = xmalloc(envsize * sizeof(char *));
1199 env[0] = NULL;
1200
Damien Millerbac2d8a2000-09-05 16:13:06 +11001201#ifdef HAVE_CYGWIN
1202 /*
1203 * The Windows environment contains some setting which are
1204 * important for a running system. They must not be dropped.
1205 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001206 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001207#endif
1208
Damien Millerb38eff82000-04-01 11:09:21 +10001209 if (!options.use_login) {
1210 /* Set basic environment. */
1211 child_set_env(&env, &envsize, "USER", pw->pw_name);
1212 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1213 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001214#ifdef HAVE_LOGIN_CAP
1215 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1216 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001217#else /* HAVE_LOGIN_CAP */
1218# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001219 /*
1220 * There's no standard path on Windows. The path contains
1221 * important components pointing to the system directories,
1222 * needed for loading shared libraries. So the path better
1223 * remains intact here.
1224 */
Damien Millerb38eff82000-04-01 11:09:21 +10001225 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001226# endif /* HAVE_CYGWIN */
1227#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001228
1229 snprintf(buf, sizeof buf, "%.200s/%.50s",
1230 _PATH_MAILDIR, pw->pw_name);
1231 child_set_env(&env, &envsize, "MAIL", buf);
1232
1233 /* Normal systems set SHELL by default. */
1234 child_set_env(&env, &envsize, "SHELL", shell);
1235 }
1236 if (getenv("TZ"))
1237 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1238
1239 /* Set custom environment options from RSA authentication. */
1240 while (custom_environment) {
1241 struct envstring *ce = custom_environment;
1242 char *s = ce->s;
1243 int i;
1244 for (i = 0; s[i] != '=' && s[i]; i++);
1245 if (s[i] == '=') {
1246 s[i] = 0;
1247 child_set_env(&env, &envsize, s, s + i + 1);
1248 }
1249 custom_environment = ce->next;
1250 xfree(ce->s);
1251 xfree(ce);
1252 }
1253
1254 snprintf(buf, sizeof buf, "%.50s %d %d",
1255 get_remote_ipaddr(), get_remote_port(), get_local_port());
1256 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1257
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001258 if (s->ttyfd != -1)
1259 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1260 if (s->term)
1261 child_set_env(&env, &envsize, "TERM", s->term);
1262 if (s->display)
1263 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001264 if (original_command)
1265 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1266 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001267
1268#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001269 if ((cp = getenv("AUTHSTATE")) != NULL)
1270 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1271 if ((cp = getenv("KRB5CCNAME")) != NULL)
1272 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1273 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001274#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001275#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +00001276 if (s->authctxt->krb4_ticket_file)
1277 child_set_env(&env, &envsize, "KRBTKFILE",
1278 s->authctxt->krb4_ticket_file);
1279#endif
1280#ifdef KRB5
1281 if (s->authctxt->krb5_ticket_file)
1282 child_set_env(&env, &envsize, "KRB5CCNAME",
1283 s->authctxt->krb5_ticket_file);
1284#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001285#ifdef USE_PAM
1286 /* Pull in any environment variables that may have been set by PAM. */
1287 do_pam_environment(&env, &envsize);
1288#endif /* USE_PAM */
1289
Damien Millerb38eff82000-04-01 11:09:21 +10001290 if (auth_get_socket_name() != NULL)
1291 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1292 auth_get_socket_name());
1293
1294 /* read $HOME/.ssh/environment. */
1295 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001296 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1297 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001298 read_environment_file(&env, &envsize, buf);
1299 }
1300 if (debug_flag) {
1301 /* dump the environment */
1302 fprintf(stderr, "Environment:\n");
1303 for (i = 0; env[i]; i++)
1304 fprintf(stderr, " %.200s\n", env[i]);
1305 }
Damien Millerad833b32000-08-23 10:46:23 +10001306 /* we have to stash the hostname before we close our socket. */
1307 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001308 hostname = get_remote_name_or_ip(utmp_len,
1309 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001310 /*
1311 * Close the connection descriptors; note that this is the child, and
1312 * the server will still have the socket open, and it is important
1313 * that we do not shutdown it. Note that the descriptors cannot be
1314 * closed before building the environment, as we call
1315 * get_remote_ipaddr there.
1316 */
1317 if (packet_get_connection_in() == packet_get_connection_out())
1318 close(packet_get_connection_in());
1319 else {
1320 close(packet_get_connection_in());
1321 close(packet_get_connection_out());
1322 }
1323 /*
1324 * Close all descriptors related to channels. They will still remain
1325 * open in the parent.
1326 */
1327 /* XXX better use close-on-exec? -markus */
1328 channel_close_all();
1329
1330 /*
1331 * Close any extra file descriptors. Note that there may still be
1332 * descriptors left by system functions. They will be closed later.
1333 */
1334 endpwent();
1335
1336 /*
1337 * Close any extra open file descriptors so that we don\'t have them
1338 * hanging around in clients. Note that we want to do this after
1339 * initgroups, because at least on Solaris 2.3 it leaves file
1340 * descriptors open.
1341 */
1342 for (i = 3; i < 64; i++)
1343 close(i);
1344
Damien Millerb38eff82000-04-01 11:09:21 +10001345 /*
1346 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1347 * xauth are run in the proper environment.
1348 */
1349 environ = env;
1350
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001351#ifdef AFS
1352 /* Try to get AFS tokens for the local cell. */
1353 if (k_hasafs()) {
1354 char cell[64];
1355
1356 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1357 krb_afslog(cell, 0);
1358
1359 krb_afslog(0, 0);
1360 }
1361#endif /* AFS */
1362
Damien Miller139d4cd2001-10-10 15:07:44 +10001363 /* Change current directory to the user\'s home directory. */
1364 if (chdir(pw->pw_dir) < 0) {
1365 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1366 pw->pw_dir, strerror(errno));
1367#ifdef HAVE_LOGIN_CAP
1368 if (login_getcapbool(lc, "requirehome", 0))
1369 exit(1);
1370#endif
1371 }
1372
Damien Millerb38eff82000-04-01 11:09:21 +10001373 /*
1374 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1375 * in this order).
1376 */
1377 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001378 /* ignore _PATH_SSH_USER_RC for subsystems */
1379 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001380 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1381 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001382 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001383 fprintf(stderr, "Running %s\n", cmd);
1384 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001385 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001386 if (do_xauth)
1387 fprintf(f, "%s %s\n", s->auth_proto,
1388 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001389 pclose(f);
1390 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001391 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001392 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001393 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001394 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001395 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1396 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001397 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001398 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001399 if (do_xauth)
1400 fprintf(f, "%s %s\n", s->auth_proto,
1401 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001402 pclose(f);
1403 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001404 fprintf(stderr, "Could not run %s\n",
1405 _PATH_SSH_SYSTEM_RC);
1406 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001407 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001408 char *screen = strchr(s->display, ':');
1409
1410 if (debug_flag) {
1411 fprintf(stderr,
1412 "Running %.100s add "
1413 "%.100s %.100s %.100s\n",
1414 options.xauth_location, s->display,
1415 s->auth_proto, s->auth_data);
1416 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001417 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001418 "Adding %.*s/unix%s %s %s\n",
1419 (int)(screen - s->display),
1420 s->display, screen,
1421 s->auth_proto, s->auth_data);
1422 }
1423 snprintf(cmd, sizeof cmd, "%s -q -",
1424 options.xauth_location);
1425 f = popen(cmd, "w");
1426 if (f) {
1427 fprintf(f, "add %s %s %s\n", s->display,
1428 s->auth_proto, s->auth_data);
1429 if (screen != NULL)
1430 fprintf(f, "add %.*s/unix%s %s %s\n",
1431 (int)(screen - s->display),
1432 s->display, screen,
1433 s->auth_proto,
1434 s->auth_data);
1435 pclose(f);
1436 } else {
1437 fprintf(stderr, "Could not run %s\n",
1438 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001439 }
1440 }
Damien Millerb38eff82000-04-01 11:09:21 +10001441 /* Get the last component of the shell name. */
1442 cp = strrchr(shell, '/');
1443 if (cp)
1444 cp++;
1445 else
1446 cp = shell;
1447 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001448
1449 /* restore SIGPIPE for child */
1450 signal(SIGPIPE, SIG_DFL);
1451
Damien Millerb38eff82000-04-01 11:09:21 +10001452 /*
1453 * If we have no command, execute the shell. In this case, the shell
1454 * name to be passed in argv[0] is preceded by '-' to indicate that
1455 * this is a login shell.
1456 */
1457 if (!command) {
1458 if (!options.use_login) {
1459 char buf[256];
1460
Damien Millerb38eff82000-04-01 11:09:21 +10001461 /* Start the shell. Set initial character to '-'. */
1462 buf[0] = '-';
1463 strncpy(buf + 1, cp, sizeof(buf) - 1);
1464 buf[sizeof(buf) - 1] = 0;
1465
1466 /* Execute the shell. */
1467 argv[0] = buf;
1468 argv[1] = NULL;
1469 execve(shell, argv, env);
1470
1471 /* Executing the shell failed. */
1472 perror(shell);
1473 exit(1);
1474
1475 } else {
1476 /* Launch login(1). */
1477
Damien Millerad833b32000-08-23 10:46:23 +10001478 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001479#ifdef LOGIN_NEEDS_TERM
1480 s->term? s->term : "unknown",
1481#endif
Damien Millerefb1edf2001-07-14 12:19:36 +10001482 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001483
1484 /* Login couldn't be executed, die. */
1485
1486 perror("login");
1487 exit(1);
1488 }
1489 }
1490 /*
1491 * Execute the command using the user's shell. This uses the -c
1492 * option to execute the command.
1493 */
1494 argv[0] = (char *) cp;
1495 argv[1] = "-c";
1496 argv[2] = (char *) command;
1497 argv[3] = NULL;
1498 execve(shell, argv, env);
1499 perror(shell);
1500 exit(1);
1501}
1502
1503Session *
1504session_new(void)
1505{
1506 int i;
1507 static int did_init = 0;
1508 if (!did_init) {
1509 debug("session_new: init");
1510 for(i = 0; i < MAX_SESSIONS; i++) {
1511 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001512 }
1513 did_init = 1;
1514 }
1515 for(i = 0; i < MAX_SESSIONS; i++) {
1516 Session *s = &sessions[i];
1517 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001518 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001519 s->chanid = -1;
1520 s->ptyfd = -1;
1521 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001522 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001523 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001524 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001525 return s;
1526 }
1527 }
1528 return NULL;
1529}
1530
Ben Lindstrombba81212001-06-25 05:01:22 +00001531static void
Damien Millerb38eff82000-04-01 11:09:21 +10001532session_dump(void)
1533{
1534 int i;
1535 for(i = 0; i < MAX_SESSIONS; i++) {
1536 Session *s = &sessions[i];
1537 debug("dump: used %d session %d %p channel %d pid %d",
1538 s->used,
1539 s->self,
1540 s,
1541 s->chanid,
1542 s->pid);
1543 }
1544}
1545
Damien Millerefb4afe2000-04-12 18:45:05 +10001546int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001547session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001548{
1549 Session *s = session_new();
1550 debug("session_open: channel %d", chanid);
1551 if (s == NULL) {
1552 error("no more sessions");
1553 return 0;
1554 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001555 s->authctxt = authctxt;
1556 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001557 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001558 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001559 debug("session_open: session %d: link with channel %d", s->self, chanid);
1560 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001561 return 1;
1562}
1563
Ben Lindstrombba81212001-06-25 05:01:22 +00001564static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001565session_by_channel(int id)
1566{
1567 int i;
1568 for(i = 0; i < MAX_SESSIONS; i++) {
1569 Session *s = &sessions[i];
1570 if (s->used && s->chanid == id) {
1571 debug("session_by_channel: session %d channel %d", i, id);
1572 return s;
1573 }
1574 }
1575 debug("session_by_channel: unknown channel %d", id);
1576 session_dump();
1577 return NULL;
1578}
1579
Ben Lindstrombba81212001-06-25 05:01:22 +00001580static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001581session_by_pid(pid_t pid)
1582{
1583 int i;
1584 debug("session_by_pid: pid %d", pid);
1585 for(i = 0; i < MAX_SESSIONS; i++) {
1586 Session *s = &sessions[i];
1587 if (s->used && s->pid == pid)
1588 return s;
1589 }
1590 error("session_by_pid: unknown pid %d", pid);
1591 session_dump();
1592 return NULL;
1593}
1594
Ben Lindstrombba81212001-06-25 05:01:22 +00001595static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001596session_window_change_req(Session *s)
1597{
1598 s->col = packet_get_int();
1599 s->row = packet_get_int();
1600 s->xpixel = packet_get_int();
1601 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001602 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001603 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1604 return 1;
1605}
1606
Ben Lindstrombba81212001-06-25 05:01:22 +00001607static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001608session_pty_req(Session *s)
1609{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001610 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001611 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001612
Ben Lindstrom49c12602001-06-13 04:37:36 +00001613 if (no_pty_flag) {
1614 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001615 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001616 }
1617 if (s->ttyfd != -1) {
1618 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001619 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001620 }
1621
Damien Millerefb4afe2000-04-12 18:45:05 +10001622 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001623
1624 if (compat20) {
1625 s->col = packet_get_int();
1626 s->row = packet_get_int();
1627 } else {
1628 s->row = packet_get_int();
1629 s->col = packet_get_int();
1630 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001631 s->xpixel = packet_get_int();
1632 s->ypixel = packet_get_int();
1633
1634 if (strcmp(s->term, "") == 0) {
1635 xfree(s->term);
1636 s->term = NULL;
1637 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001638
Damien Millerefb4afe2000-04-12 18:45:05 +10001639 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001640 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001641 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001642 if (s->term)
1643 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001644 s->term = NULL;
1645 s->ptyfd = -1;
1646 s->ttyfd = -1;
1647 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001648 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001649 }
1650 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001651
1652 /* for SSH1 the tty modes length is not given */
1653 if (!compat20)
1654 n_bytes = packet_remaining();
1655 tty_parse_modes(s->ttyfd, &n_bytes);
1656
Damien Millerefb4afe2000-04-12 18:45:05 +10001657 /*
1658 * Add a cleanup function to clear the utmp entry and record logout
1659 * time in case we call fatal() (e.g., the connection gets closed).
1660 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001661 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001662 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001663
1664 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001665 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1666
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001667 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001668 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001669 return 1;
1670}
1671
Ben Lindstrombba81212001-06-25 05:01:22 +00001672static int
Damien Millerbd483e72000-04-30 10:00:53 +10001673session_subsystem_req(Session *s)
1674{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001675 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001676 int success = 0;
1677 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001678 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001679
1680 packet_done();
1681 log("subsystem request for %s", subsys);
1682
Damien Millerf6d9e222000-06-18 14:50:44 +10001683 for (i = 0; i < options.num_subsystems; i++) {
1684 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1685 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001686 s->is_subsystem = 1;
Ben Lindstromb09f6b52001-09-14 23:12:07 +00001687 do_exec(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001688 success = 1;
1689 }
1690 }
1691
1692 if (!success)
1693 log("subsystem request for %s failed, subsystem not found", subsys);
1694
Damien Millerbd483e72000-04-30 10:00:53 +10001695 xfree(subsys);
1696 return success;
1697}
1698
Ben Lindstrombba81212001-06-25 05:01:22 +00001699static int
Damien Millerbd483e72000-04-30 10:00:53 +10001700session_x11_req(Session *s)
1701{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001702 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001703
1704 s->single_connection = packet_get_char();
1705 s->auth_proto = packet_get_string(NULL);
1706 s->auth_data = packet_get_string(NULL);
1707 s->screen = packet_get_int();
1708 packet_done();
1709
Ben Lindstrom768176b2001-06-09 01:29:12 +00001710 success = session_setup_x11fwd(s);
1711 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001712 xfree(s->auth_proto);
1713 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001714 s->auth_proto = NULL;
1715 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001716 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001717 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001718}
1719
Ben Lindstrombba81212001-06-25 05:01:22 +00001720static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001721session_shell_req(Session *s)
1722{
Damien Millerf6d9e222000-06-18 14:50:44 +10001723 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001724 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001725 return 1;
1726}
1727
Ben Lindstrombba81212001-06-25 05:01:22 +00001728static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001729session_exec_req(Session *s)
1730{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001731 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001732 char *command = packet_get_string(&len);
1733 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001734 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001735 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001736 return 1;
1737}
1738
Ben Lindstrombba81212001-06-25 05:01:22 +00001739static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001740session_auth_agent_req(Session *s)
1741{
1742 static int called = 0;
1743 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001744 if (no_agent_forwarding_flag) {
1745 debug("session_auth_agent_req: no_agent_forwarding_flag");
1746 return 0;
1747 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001748 if (called) {
1749 return 0;
1750 } else {
1751 called = 1;
1752 return auth_input_request_forwarding(s->pw);
1753 }
1754}
1755
Damien Millerefb4afe2000-04-12 18:45:05 +10001756void
1757session_input_channel_req(int id, void *arg)
1758{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001759 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001760 int reply;
1761 int success = 0;
1762 char *rtype;
1763 Session *s;
1764 Channel *c;
1765
1766 rtype = packet_get_string(&len);
1767 reply = packet_get_char();
1768
1769 s = session_by_channel(id);
1770 if (s == NULL)
1771 fatal("session_input_channel_req: channel %d: no session", id);
1772 c = channel_lookup(id);
1773 if (c == NULL)
1774 fatal("session_input_channel_req: channel %d: bad channel", id);
1775
1776 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1777 s->self, id, rtype, reply);
1778
1779 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001780 * a session is in LARVAL state until a shell, a command
1781 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001782 */
1783 if (c->type == SSH_CHANNEL_LARVAL) {
1784 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001785 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001786 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001787 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001788 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001789 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001790 } else if (strcmp(rtype, "x11-req") == 0) {
1791 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001792 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1793 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001794 } else if (strcmp(rtype, "subsystem") == 0) {
1795 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001796 }
1797 }
1798 if (strcmp(rtype, "window-change") == 0) {
1799 success = session_window_change_req(s);
1800 }
1801
1802 if (reply) {
1803 packet_start(success ?
1804 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1805 packet_put_int(c->remote_id);
1806 packet_send();
1807 }
1808 xfree(rtype);
1809}
1810
1811void
1812session_set_fds(Session *s, int fdin, int fdout, int fderr)
1813{
1814 if (!compat20)
1815 fatal("session_set_fds: called for proto != 2.0");
1816 /*
1817 * now that have a child and a pipe to the child,
1818 * we can activate our channel and register the fd's
1819 */
1820 if (s->chanid == -1)
1821 fatal("no channel for session %d", s->self);
1822 channel_set_fds(s->chanid,
1823 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001824 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1825 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001826}
1827
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001828/*
1829 * Function to perform pty cleanup. Also called if we get aborted abnormally
1830 * (e.g., due to a dropped connection).
1831 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001832static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001833session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001834{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001835 Session *s = session;
1836
1837 if (s == NULL) {
1838 error("session_pty_cleanup: no session");
1839 return;
1840 }
1841 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001842 return;
1843
Kevin Steves43cdef32001-02-11 14:12:08 +00001844 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001845
Damien Millerb38eff82000-04-01 11:09:21 +10001846 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001847 if (s->pid != 0)
1848 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001849
1850 /* Release the pseudo-tty. */
1851 pty_release(s->tty);
1852
1853 /*
1854 * Close the server side of the socket pairs. We must do this after
1855 * the pty cleanup, so that another process doesn't get this pty
1856 * while we're still cleaning up.
1857 */
1858 if (close(s->ptymaster) < 0)
1859 error("close(s->ptymaster): %s", strerror(errno));
1860}
Damien Millerefb4afe2000-04-12 18:45:05 +10001861
Ben Lindstrombba81212001-06-25 05:01:22 +00001862static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001863session_exit_message(Session *s, int status)
1864{
1865 Channel *c;
1866 if (s == NULL)
1867 fatal("session_close: no session");
1868 c = channel_lookup(s->chanid);
1869 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001870 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001871 s->self, s->chanid);
1872 debug("session_exit_message: session %d channel %d pid %d",
1873 s->self, s->chanid, s->pid);
1874
1875 if (WIFEXITED(status)) {
1876 channel_request_start(s->chanid,
1877 "exit-status", 0);
1878 packet_put_int(WEXITSTATUS(status));
1879 packet_send();
1880 } else if (WIFSIGNALED(status)) {
1881 channel_request_start(s->chanid,
1882 "exit-signal", 0);
1883 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001884#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001885 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001886#else /* WCOREDUMP */
1887 packet_put_char(0);
1888#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001889 packet_put_cstring("");
1890 packet_put_cstring("");
1891 packet_send();
1892 } else {
1893 /* Some weird exit cause. Just exit. */
1894 packet_disconnect("wait returned status %04x.", status);
1895 }
1896
1897 /* disconnect channel */
1898 debug("session_exit_message: release channel %d", s->chanid);
1899 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001900 /*
1901 * emulate a write failure with 'chan_write_failed', nobody will be
1902 * interested in data we write.
1903 * Note that we must not call 'chan_read_failed', since there could
1904 * be some more data waiting in the pipe.
1905 */
Damien Millerbd483e72000-04-30 10:00:53 +10001906 if (c->ostate != CHAN_OUTPUT_CLOSED)
1907 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001908 s->chanid = -1;
1909}
1910
Ben Lindstrombba81212001-06-25 05:01:22 +00001911static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001912session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001913{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001914 debug("session_close: session %d pid %d", s->self, s->pid);
1915 if (s->ttyfd != -1) {
1916 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1917 session_pty_cleanup(s);
1918 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001919 if (s->term)
1920 xfree(s->term);
1921 if (s->display)
1922 xfree(s->display);
1923 if (s->auth_data)
1924 xfree(s->auth_data);
1925 if (s->auth_proto)
1926 xfree(s->auth_proto);
1927 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001928 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001929}
1930
1931void
1932session_close_by_pid(pid_t pid, int status)
1933{
1934 Session *s = session_by_pid(pid);
1935 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001936 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001937 return;
1938 }
1939 if (s->chanid != -1)
1940 session_exit_message(s, status);
1941 session_close(s);
1942}
1943
Ben Lindstrom4983d5e2001-07-04 05:17:40 +00001944int
1945session_have_children(void)
1946{
1947 int i;
1948
1949 for(i = 0; i < MAX_SESSIONS; i++) {
1950 Session *s = &sessions[i];
1951 if (s->used && s->pid != -1) {
1952 debug("session_have_children: id %d pid %d", i, s->pid);
1953 return 1;
1954 }
1955 }
1956 debug("session_have_children: no more children");
1957 return 0;
1958}
1959
Damien Millerefb4afe2000-04-12 18:45:05 +10001960/*
1961 * this is called when a channel dies before
1962 * the session 'child' itself dies
1963 */
1964void
1965session_close_by_channel(int id, void *arg)
1966{
1967 Session *s = session_by_channel(id);
1968 if (s == NULL) {
1969 debug("session_close_by_channel: no session for channel %d", id);
1970 return;
1971 }
1972 /* disconnect channel */
1973 channel_cancel_cleanup(s->chanid);
1974 s->chanid = -1;
1975
1976 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1977 if (s->pid == 0) {
1978 /* close session immediately */
1979 session_close(s);
1980 } else {
1981 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001982 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001983 error("session_close_by_channel: kill %d: %s",
1984 s->pid, strerror(errno));
1985 }
1986}
1987
Ben Lindstrombba81212001-06-25 05:01:22 +00001988static char *
Damien Millere247cc42000-05-07 12:03:14 +10001989session_tty_list(void)
1990{
1991 static char buf[1024];
1992 int i;
1993 buf[0] = '\0';
1994 for(i = 0; i < MAX_SESSIONS; i++) {
1995 Session *s = &sessions[i];
1996 if (s->used && s->ttyfd != -1) {
1997 if (buf[0] != '\0')
1998 strlcat(buf, ",", sizeof buf);
1999 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2000 }
2001 }
2002 if (buf[0] == '\0')
2003 strlcpy(buf, "notty", sizeof buf);
2004 return buf;
2005}
2006
2007void
2008session_proctitle(Session *s)
2009{
2010 if (s->pw == NULL)
2011 error("no user for session %d", s->self);
2012 else
2013 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2014}
2015
Ben Lindstrom768176b2001-06-09 01:29:12 +00002016int
2017session_setup_x11fwd(Session *s)
2018{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002019 struct stat st;
2020
2021 if (no_x11_forwarding_flag) {
2022 packet_send_debug("X11 forwarding disabled in user configuration file.");
2023 return 0;
2024 }
2025 if (!options.x11_forwarding) {
2026 debug("X11 forwarding disabled in server configuration file.");
2027 return 0;
2028 }
2029 if (!options.xauth_location ||
2030 (stat(options.xauth_location, &st) == -1)) {
2031 packet_send_debug("No xauth program; cannot forward with spoofing.");
2032 return 0;
2033 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002034 if (options.use_login) {
2035 packet_send_debug("X11 forwarding disabled; "
2036 "not compatible with UseLogin=yes.");
2037 return 0;
2038 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002039 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002040 debug("X11 display already set.");
2041 return 0;
2042 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002043 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
2044 if (s->display == NULL) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002045 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002046 return 0;
2047 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002048 return 1;
2049}
2050
Ben Lindstrombba81212001-06-25 05:01:22 +00002051static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002052do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002053{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002054 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002055}