blob: 10cefdb4421beac6970779c1028c0507c4da88ec [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 Miller0585d512001-10-12 11:35:50 +100036RCSID("$OpenBSD: session.c,v 1.108 2001/10/11 13:45:21 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);
Damien Millerc3aa3dd2001-10-28 22:34:52 +1100435 if (is_pam_password_change_required())
436 packet_disconnect("Password change required but no "
437 "TTY available");
Kevin Stevesff793a22001-02-21 16:36:51 +0000438#endif /* USE_PAM */
439
Damien Millerb38eff82000-04-01 11:09:21 +1000440 /* Fork the child. */
441 if ((pid = fork()) == 0) {
442 /* Child. Reinitialize the log since the pid has changed. */
443 log_init(__progname, options.log_level, options.log_facility, log_stderr);
444
445 /*
446 * Create a new session and process group since the 4.4BSD
447 * setlogin() affects the entire process group.
448 */
449 if (setsid() < 0)
450 error("setsid failed: %.100s", strerror(errno));
451
452#ifdef USE_PIPES
453 /*
454 * Redirect stdin. We close the parent side of the socket
455 * pair, and make the child side the standard input.
456 */
457 close(pin[1]);
458 if (dup2(pin[0], 0) < 0)
459 perror("dup2 stdin");
460 close(pin[0]);
461
462 /* Redirect stdout. */
463 close(pout[0]);
464 if (dup2(pout[1], 1) < 0)
465 perror("dup2 stdout");
466 close(pout[1]);
467
468 /* Redirect stderr. */
469 close(perr[0]);
470 if (dup2(perr[1], 2) < 0)
471 perror("dup2 stderr");
472 close(perr[1]);
473#else /* USE_PIPES */
474 /*
475 * Redirect stdin, stdout, and stderr. Stdin and stdout will
476 * use the same socket, as some programs (particularly rdist)
477 * seem to depend on it.
478 */
479 close(inout[1]);
480 close(err[1]);
481 if (dup2(inout[0], 0) < 0) /* stdin */
482 perror("dup2 stdin");
483 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
484 perror("dup2 stdout");
485 if (dup2(err[0], 2) < 0) /* stderr */
486 perror("dup2 stderr");
487#endif /* USE_PIPES */
488
489 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000490 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000491 /* NOTREACHED */
492 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100493#ifdef HAVE_CYGWIN
494 if (is_winnt)
495 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
496#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000497 if (pid < 0)
498 packet_disconnect("fork failed: %.100s", strerror(errno));
499 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000500 /* Set interactive/non-interactive mode. */
501 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000502#ifdef USE_PIPES
503 /* We are the parent. Close the child sides of the pipes. */
504 close(pin[0]);
505 close(pout[1]);
506 close(perr[1]);
507
Damien Millerefb4afe2000-04-12 18:45:05 +1000508 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000509 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000510 } else {
511 /* Enter the interactive session. */
512 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000513 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000514 }
Damien Millerb38eff82000-04-01 11:09:21 +1000515#else /* USE_PIPES */
516 /* We are the parent. Close the child sides of the socket pairs. */
517 close(inout[0]);
518 close(err[0]);
519
520 /*
521 * Enter the interactive session. Note: server_loop must be able to
522 * handle the case that fdin and fdout are the same.
523 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000524 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000525 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000526 } else {
527 server_loop(pid, inout[1], inout[1], err[1]);
528 /* server_loop has closed inout[1] and err[1]. */
529 }
Damien Millerb38eff82000-04-01 11:09:21 +1000530#endif /* USE_PIPES */
531}
532
533/*
534 * This is called to fork and execute a command when we have a tty. This
535 * will call do_child from the child, and server_loop from the parent after
536 * setting up file descriptors, controlling tty, updating wtmp, utmp,
537 * lastlog, and other such operations.
538 */
Damien Miller4af51302000-04-16 11:18:38 +1000539void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000540do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000541{
Damien Millerb38eff82000-04-01 11:09:21 +1000542 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000543 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000544
545 if (s == NULL)
546 fatal("do_exec_pty: no session");
547 ptyfd = s->ptyfd;
548 ttyfd = s->ttyfd;
549
Damien Millerc5946332001-02-28 11:46:11 +1100550#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000551 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000552 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100553#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000554
Damien Millerb38eff82000-04-01 11:09:21 +1000555 /* Fork the child. */
556 if ((pid = fork()) == 0) {
Ben Lindstrombddd5512001-07-04 04:53:53 +0000557
Damien Miller942da032000-08-18 13:59:06 +1000558 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000559 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000560 /* Close the master side of the pseudo tty. */
561 close(ptyfd);
562
563 /* Make the pseudo tty our controlling tty. */
564 pty_make_controlling_tty(&ttyfd, s->tty);
565
Damien Miller9c751422001-10-10 15:02:46 +1000566 /* Redirect stdin/stdout/stderr from the pseudo tty. */
567 if (dup2(ttyfd, 0) < 0)
568 error("dup2 stdin: %s", strerror(errno));
569 if (dup2(ttyfd, 1) < 0)
570 error("dup2 stdout: %s", strerror(errno));
571 if (dup2(ttyfd, 2) < 0)
572 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000573
574 /* Close the extra descriptor for the pseudo tty. */
575 close(ttyfd);
576
Damien Miller942da032000-08-18 13:59:06 +1000577 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000578#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100579 if (!(options.use_login && command == NULL))
580 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000581# ifdef LOGIN_NEEDS_UTMPX
582 else
583 do_pre_login(s);
584# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000585#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000586
Damien Millerb38eff82000-04-01 11:09:21 +1000587 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000588 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000589 /* NOTREACHED */
590 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100591#ifdef HAVE_CYGWIN
592 if (is_winnt)
593 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
594#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000595 if (pid < 0)
596 packet_disconnect("fork failed: %.100s", strerror(errno));
597 s->pid = pid;
598
599 /* Parent. Close the slave side of the pseudo tty. */
600 close(ttyfd);
601
602 /*
603 * Create another descriptor of the pty master side for use as the
604 * standard input. We could use the original descriptor, but this
605 * simplifies code in server_loop. The descriptor is bidirectional.
606 */
607 fdout = dup(ptyfd);
608 if (fdout < 0)
609 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
610
611 /* we keep a reference to the pty master */
612 ptymaster = dup(ptyfd);
613 if (ptymaster < 0)
614 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
615 s->ptymaster = ptymaster;
616
617 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000618 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000619 if (compat20) {
620 session_set_fds(s, ptyfd, fdout, -1);
621 } else {
622 server_loop(pid, ptyfd, fdout, -1);
623 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000624 }
Damien Millerb38eff82000-04-01 11:09:21 +1000625}
626
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000627#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000628static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000629do_pre_login(Session *s)
630{
631 socklen_t fromlen;
632 struct sockaddr_storage from;
633 pid_t pid = getpid();
634
635 /*
636 * Get IP address of client. If the connection is not a socket, let
637 * the address be 0.0.0.0.
638 */
639 memset(&from, 0, sizeof(from));
640 if (packet_connection_is_on_socket()) {
641 fromlen = sizeof(from);
642 if (getpeername(packet_get_connection_in(),
643 (struct sockaddr *) & from, &fromlen) < 0) {
644 debug("getpeername: %.100s", strerror(errno));
645 fatal_cleanup();
646 }
647 }
648
649 record_utmp_only(pid, s->tty, s->pw->pw_name,
650 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
651 (struct sockaddr *)&from);
652}
653#endif
654
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000655/*
656 * This is called to fork and execute a command. If another command is
657 * to be forced, execute that instead.
658 */
659void
660do_exec(Session *s, const char *command)
661{
662 if (forced_command) {
663 original_command = command;
664 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000665 debug("Forced command '%.900s'", command);
666 }
667
668 if (s->ttyfd != -1)
669 do_exec_pty(s, command);
670 else
671 do_exec_no_pty(s, command);
672
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000673 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000674}
675
Damien Miller942da032000-08-18 13:59:06 +1000676/* administrative, login(1)-like work */
677void
Damien Miller69b69aa2000-10-28 14:19:58 +1100678do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000679{
Damien Miller942da032000-08-18 13:59:06 +1000680 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100681 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000682 socklen_t fromlen;
683 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000684 time_t last_login_time;
685 struct passwd * pw = s->pw;
686 pid_t pid = getpid();
687
688 /*
689 * Get IP address of client. If the connection is not a socket, let
690 * the address be 0.0.0.0.
691 */
692 memset(&from, 0, sizeof(from));
693 if (packet_connection_is_on_socket()) {
694 fromlen = sizeof(from);
695 if (getpeername(packet_get_connection_in(),
696 (struct sockaddr *) & from, &fromlen) < 0) {
697 debug("getpeername: %.100s", strerror(errno));
698 fatal_cleanup();
699 }
700 }
701
Damien Miller7b28dc52000-09-05 13:34:53 +1100702 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000703 if (options.print_lastlog) {
704 hostname[0] = '\0';
705 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
706 hostname, sizeof(hostname));
707 }
Damien Millere4340be2000-09-16 13:29:08 +1100708
Damien Miller942da032000-08-18 13:59:06 +1000709 /* Record that there was a login on that tty from the remote host. */
710 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000711 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
712 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000713
Kevin Steves092f2ef2000-10-14 13:36:13 +0000714#ifdef USE_PAM
715 /*
716 * If password change is needed, do it now.
717 * This needs to occur before the ~/.hushlogin check.
718 */
Damien Miller646aa602001-02-15 11:51:32 +1100719 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000720 print_pam_messages();
721 do_pam_chauthtok();
722 }
723#endif
724
Damien Millercf205e82001-04-16 18:29:15 +1000725 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000726 return;
727
728#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100729 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000730 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000731#endif /* USE_PAM */
732#ifdef WITH_AIXAUTHENTICATE
733 if (aixloginmsg && *aixloginmsg)
734 printf("%s\n", aixloginmsg);
735#endif /* WITH_AIXAUTHENTICATE */
736
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000737 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000738 time_string = ctime(&last_login_time);
739 if (strchr(time_string, '\n'))
740 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000741 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000742 printf("Last login: %s\r\n", time_string);
743 else
Damien Millere4340be2000-09-16 13:29:08 +1100744 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000745 }
Damien Millercf205e82001-04-16 18:29:15 +1000746
747 do_motd();
748}
749
750/*
751 * Display the message of the day.
752 */
753void
754do_motd(void)
755{
756 FILE *f;
757 char buf[256];
758
Damien Miller942da032000-08-18 13:59:06 +1000759 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000760#ifdef HAVE_LOGIN_CAP
761 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
762 "/etc/motd"), "r");
763#else
Damien Miller942da032000-08-18 13:59:06 +1000764 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000765#endif
Damien Miller942da032000-08-18 13:59:06 +1000766 if (f) {
767 while (fgets(buf, sizeof(buf), f))
768 fputs(buf, stdout);
769 fclose(f);
770 }
771 }
772}
773
Kevin Steves8f63caa2001-07-04 18:23:02 +0000774
775/*
776 * Check for quiet login, either .hushlogin or command given.
777 */
778int
779check_quietlogin(Session *s, const char *command)
780{
781 char buf[256];
782 struct passwd *pw = s->pw;
783 struct stat st;
784
785 /* Return 1 if .hushlogin exists or a command given. */
786 if (command != NULL)
787 return 1;
788 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
789#ifdef HAVE_LOGIN_CAP
790 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
791 return 1;
792#else
793 if (stat(buf, &st) >= 0)
794 return 1;
795#endif
796 return 0;
797}
798
Damien Millerb38eff82000-04-01 11:09:21 +1000799/*
800 * Sets the value of the given variable in the environment. If the variable
801 * already exists, its value is overriden.
802 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000803static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000804child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000805 const char *value)
806{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000807 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000808 char **env;
809
810 /*
811 * Find the slot where the value should be stored. If the variable
812 * already exists, we reuse the slot; otherwise we append a new slot
813 * at the end of the array, expanding if necessary.
814 */
815 env = *envp;
816 namelen = strlen(name);
817 for (i = 0; env[i]; i++)
818 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
819 break;
820 if (env[i]) {
821 /* Reuse the slot. */
822 xfree(env[i]);
823 } else {
824 /* New variable. Expand if necessary. */
825 if (i >= (*envsizep) - 1) {
826 (*envsizep) += 50;
827 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
828 }
829 /* Need to set the NULL pointer at end of array beyond the new slot. */
830 env[i + 1] = NULL;
831 }
832
833 /* Allocate space and format the variable in the appropriate slot. */
834 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
835 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
836}
837
838/*
839 * Reads environment variables from the given file and adds/overrides them
840 * into the environment. If the file does not exist, this does nothing.
841 * Otherwise, it must consist of empty lines, comments (line starts with '#')
842 * and assignments of the form name=value. No other forms are allowed.
843 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000844static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000845read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000846 const char *filename)
847{
848 FILE *f;
849 char buf[4096];
850 char *cp, *value;
851
852 f = fopen(filename, "r");
853 if (!f)
854 return;
855
856 while (fgets(buf, sizeof(buf), f)) {
857 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
858 ;
859 if (!*cp || *cp == '#' || *cp == '\n')
860 continue;
861 if (strchr(cp, '\n'))
862 *strchr(cp, '\n') = '\0';
863 value = strchr(cp, '=');
864 if (value == NULL) {
865 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
866 continue;
867 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000868 /*
869 * Replace the equals sign by nul, and advance value to
870 * the value string.
871 */
Damien Millerb38eff82000-04-01 11:09:21 +1000872 *value = '\0';
873 value++;
874 child_set_env(env, envsize, cp, value);
875 }
876 fclose(f);
877}
878
879#ifdef USE_PAM
880/*
881 * Sets any environment variables which have been specified by PAM
882 */
Kevin Steves9b26f962001-06-29 17:52:17 +0000883void do_pam_environment(char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000884{
885 char *equals, var_name[512], var_val[512];
886 char **pam_env;
887 int i;
888
889 if ((pam_env = fetch_pam_environment()) == NULL)
890 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000891
Damien Millerb38eff82000-04-01 11:09:21 +1000892 for(i = 0; pam_env[i] != NULL; i++) {
893 if ((equals = strstr(pam_env[i], "=")) == NULL)
894 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000895
Damien Millerb38eff82000-04-01 11:09:21 +1000896 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
897 memset(var_name, '\0', sizeof(var_name));
898 memset(var_val, '\0', sizeof(var_val));
899
900 strncpy(var_name, pam_env[i], equals - pam_env[i]);
901 strcpy(var_val, equals + 1);
902
Damien Millercb5e44a2000-09-29 12:12:36 +1100903 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000904
905 child_set_env(env, envsize, var_name, var_val);
906 }
907 }
908}
909#endif /* USE_PAM */
910
Damien Millercb5e44a2000-09-29 12:12:36 +1100911#ifdef HAVE_CYGWIN
Kevin Steves9b26f962001-06-29 17:52:17 +0000912void copy_environment(char ***env, u_int *envsize)
Damien Millercb5e44a2000-09-29 12:12:36 +1100913{
914 char *equals, var_name[512], var_val[512];
915 int i;
916
917 for(i = 0; environ[i] != NULL; i++) {
918 if ((equals = strstr(environ[i], "=")) == NULL)
919 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000920
Damien Millercb5e44a2000-09-29 12:12:36 +1100921 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
922 memset(var_name, '\0', sizeof(var_name));
923 memset(var_val, '\0', sizeof(var_val));
924
925 strncpy(var_name, environ[i], equals - environ[i]);
926 strcpy(var_val, equals + 1);
927
928 debug3("Copy environment: %s=%s", var_name, var_val);
929
930 child_set_env(env, envsize, var_name, var_val);
931 }
932 }
933}
934#endif
935
Damien Miller5fc85652000-07-09 23:53:07 +1000936#if defined(HAVE_GETUSERATTR)
937/*
938 * AIX-specific login initialisation
939 */
940void set_limit(char *user, char *soft, char *hard, int resource, int mult)
941{
942 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000943 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000944
945 getrlimit(resource, &rlim);
946
Damien Miller65964d62000-07-11 09:16:22 +1000947 slim = 0;
948 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
949 if (slim < 0) {
950 rlim.rlim_cur = RLIM_INFINITY;
951 } else if (slim != 0) {
952 /* See the wackiness below */
953 if (rlim.rlim_cur == slim * mult)
954 slim = 0;
955 else
956 rlim.rlim_cur = slim * mult;
957 }
958 }
Damien Miller5fc85652000-07-09 23:53:07 +1000959
Damien Miller65964d62000-07-11 09:16:22 +1000960 hlim = 0;
961 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
962 if (hlim < 0) {
963 rlim.rlim_max = RLIM_INFINITY;
964 } else if (hlim != 0) {
965 rlim.rlim_max = hlim * mult;
966 }
967 }
Damien Miller5fc85652000-07-09 23:53:07 +1000968
Damien Miller65964d62000-07-11 09:16:22 +1000969 /*
970 * XXX For cpu and fsize the soft limit is set to the hard limit
971 * if the hard limit is left at its default value and the soft limit
972 * is changed from its default value, either by requesting it
973 * (slim == 0) or by setting it to the current default. At least
974 * that's how rlogind does it. If you're confused you're not alone.
975 * Bug or feature? AIX 4.3.1.2
976 */
977 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
978 && hlim == 0 && slim != 0)
979 rlim.rlim_max = rlim.rlim_cur;
980 /* A specified hard limit limits the soft limit */
981 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
982 rlim.rlim_cur = rlim.rlim_max;
983 /* A soft limit can increase a hard limit */
984 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000985 rlim.rlim_max = rlim.rlim_cur;
986
987 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000988 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000989}
990
991void set_limits_from_userattr(char *user)
992{
993 int mask;
994 char buf[16];
995
996 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
997 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
998 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
999 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1000 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1001 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1002#if defined(S_UNOFILE)
1003 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1004#endif
1005
1006 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1007 /* Convert decimal to octal */
1008 (void) snprintf(buf, sizeof(buf), "%d", mask);
1009 if (sscanf(buf, "%o", &mask) == 1)
1010 umask(mask);
1011 }
1012}
1013#endif /* defined(HAVE_GETUSERATTR) */
1014
Damien Millerb38eff82000-04-01 11:09:21 +10001015/*
1016 * Performs common processing for the child, such as setting up the
1017 * environment, closing extra file descriptors, setting the user and group
1018 * ids, and executing the command or shell.
1019 */
Damien Miller4af51302000-04-16 11:18:38 +10001020void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001021do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001022{
Damien Miller7b28dc52000-09-05 13:34:53 +11001023 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstromec95ed92001-07-04 04:21:14 +00001024 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001025 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001026 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001027 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001028 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001029 char **env;
1030 extern char **environ;
1031 struct stat st;
1032 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001033 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +10001034#ifdef WITH_IRIX_PROJECT
1035 prid_t projid;
1036#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001037#ifdef WITH_IRIX_JOBS
1038 jid_t jid = 0;
1039#else
1040#ifdef WITH_IRIX_ARRAY
1041 int jid = 0;
1042#endif /* WITH_IRIX_ARRAY */
1043#endif /* WITH_IRIX_JOBS */
1044
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001045 do_xauth =
1046 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1047
Ben Lindstrom005dd222001-04-18 15:29:33 +00001048 /* remove hostkey from the child's memory */
1049 destroy_sensitive_data();
1050
Damien Millerd3a18572000-06-07 19:55:44 +10001051 /* login(1) is only called if we execute the login shell */
1052 if (options.use_login && command != NULL)
1053 options.use_login = 0;
1054
Damien Millerc62f1fc2001-07-14 11:54:05 +10001055#if !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001056 if (!options.use_login) {
1057# ifdef HAVE_LOGIN_CAP
1058 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1059 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1060 _PATH_NOLOGIN), "r");
1061# else /* HAVE_LOGIN_CAP */
1062 if (pw->pw_uid)
1063 f = fopen(_PATH_NOLOGIN, "r");
1064# endif /* HAVE_LOGIN_CAP */
1065 if (f) {
1066 /* /etc/nologin exists. Print its contents and exit. */
1067 while (fgets(buf, sizeof(buf), f))
1068 fputs(buf, stderr);
1069 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001070 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001071 }
Damien Millerb38eff82000-04-01 11:09:21 +10001072 }
Damien Millerc62f1fc2001-07-14 11:54:05 +10001073#endif /* HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001074
Damien Millerad833b32000-08-23 10:46:23 +10001075 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001076 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1077 switch, so we let login(1) to this for us. */
1078 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001079#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001080 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001081 if (!check_quietlogin(s, command))
1082 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001083#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001084#ifdef HAVE_CYGWIN
1085 if (is_winnt) {
1086#else
Damien Millerb38eff82000-04-01 11:09:21 +10001087 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001088#endif
Damien Millerad833b32000-08-23 10:46:23 +10001089# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001090 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001091# endif /* HAVE_GETUSERATTR */
1092# ifdef HAVE_LOGIN_CAP
1093 if (setusercontext(lc, pw, pw->pw_uid,
1094 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1095 perror("unable to set user context");
1096 exit(1);
1097 }
1098# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001099#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1100 /* Sets login uid for accounting */
1101 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1102 error("setluid: %s", strerror(errno));
1103#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1104
Damien Millerad833b32000-08-23 10:46:23 +10001105 if (setlogin(pw->pw_name) < 0)
1106 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001107 if (setgid(pw->pw_gid) < 0) {
1108 perror("setgid");
1109 exit(1);
1110 }
1111 /* Initialize the group list. */
1112 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1113 perror("initgroups");
1114 exit(1);
1115 }
1116 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001117# ifdef USE_PAM
1118 /*
1119 * PAM credentials may take the form of
1120 * supplementary groups. These will have been
1121 * wiped by the above initgroups() call.
1122 * Reestablish them here.
1123 */
1124 do_pam_setcred(0);
1125# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001126# ifdef WITH_IRIX_JOBS
1127 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1128 if (jid == -1) {
1129 fatal("Failed to create job container: %.100s",
1130 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001131 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001132# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001133# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001134 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001135 if (jid == 0) {
1136 if (newarraysess() != 0)
1137 fatal("Failed to set up new array session: %.100s",
1138 strerror(errno));
1139 }
Damien Millerad833b32000-08-23 10:46:23 +10001140# endif /* WITH_IRIX_ARRAY */
1141# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001142 /* initialize irix project info */
1143 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1144 debug("Failed to get project id, using projid 0");
1145 projid = 0;
1146 }
Damien Miller91606b12000-06-28 08:22:29 +10001147 if (setprid(projid))
1148 fatal("Failed to initialize project %d for %s: %.100s",
1149 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001150# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001151#ifdef WITH_IRIX_AUDIT
1152 if (sysconf(_SC_AUDIT)) {
1153 debug("Setting sat id to %d", (int) pw->pw_uid);
1154 if (satsetid(pw->pw_uid))
1155 debug("error setting satid: %.100s", strerror(errno));
1156 }
1157#endif /* WITH_IRIX_AUDIT */
1158
Damien Miller168a7002001-03-17 10:29:50 +11001159#ifdef _AIX
1160 /*
1161 * AIX has a "usrinfo" area where logname and
1162 * other stuff is stored - a few applications
1163 * actually use this and die if it's not set
1164 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001165 if (s->ttyfd == -1)
1166 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001167 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001168 2 * strlen(pw->pw_name));
1169 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001170 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001171 if (usrinfo(SETUINFO, cp, i) == -1)
1172 fatal("Couldn't set usrinfo: %s",
1173 strerror(errno));
1174 debug3("AIX/UsrInfo: set len %d", i);
1175 xfree(cp);
1176#endif
1177
Damien Millerb38eff82000-04-01 11:09:21 +10001178 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001179 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001180# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001181 }
Damien Millerad833b32000-08-23 10:46:23 +10001182#endif /* HAVE_OSF_SIA */
1183
Damien Millerbac2d8a2000-09-05 16:13:06 +11001184#ifdef HAVE_CYGWIN
1185 if (is_winnt)
1186#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001187 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001188 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001189 }
1190 /*
1191 * Get the shell from the password data. An empty shell field is
1192 * legal, and means /bin/sh.
1193 */
1194 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001195#ifdef HAVE_LOGIN_CAP
1196 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1197#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001198
Damien Millerb38eff82000-04-01 11:09:21 +10001199 /* Initialize the environment. */
1200 envsize = 100;
1201 env = xmalloc(envsize * sizeof(char *));
1202 env[0] = NULL;
1203
Damien Millerbac2d8a2000-09-05 16:13:06 +11001204#ifdef HAVE_CYGWIN
1205 /*
1206 * The Windows environment contains some setting which are
1207 * important for a running system. They must not be dropped.
1208 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001209 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001210#endif
1211
Damien Millerb38eff82000-04-01 11:09:21 +10001212 if (!options.use_login) {
1213 /* Set basic environment. */
1214 child_set_env(&env, &envsize, "USER", pw->pw_name);
1215 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1216 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001217#ifdef HAVE_LOGIN_CAP
1218 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1219 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001220#else /* HAVE_LOGIN_CAP */
1221# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001222 /*
1223 * There's no standard path on Windows. The path contains
1224 * important components pointing to the system directories,
1225 * needed for loading shared libraries. So the path better
1226 * remains intact here.
1227 */
Damien Millerb38eff82000-04-01 11:09:21 +10001228 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001229# endif /* HAVE_CYGWIN */
1230#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001231
1232 snprintf(buf, sizeof buf, "%.200s/%.50s",
1233 _PATH_MAILDIR, pw->pw_name);
1234 child_set_env(&env, &envsize, "MAIL", buf);
1235
1236 /* Normal systems set SHELL by default. */
1237 child_set_env(&env, &envsize, "SHELL", shell);
1238 }
1239 if (getenv("TZ"))
1240 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1241
1242 /* Set custom environment options from RSA authentication. */
1243 while (custom_environment) {
1244 struct envstring *ce = custom_environment;
1245 char *s = ce->s;
1246 int i;
1247 for (i = 0; s[i] != '=' && s[i]; i++);
1248 if (s[i] == '=') {
1249 s[i] = 0;
1250 child_set_env(&env, &envsize, s, s + i + 1);
1251 }
1252 custom_environment = ce->next;
1253 xfree(ce->s);
1254 xfree(ce);
1255 }
1256
1257 snprintf(buf, sizeof buf, "%.50s %d %d",
1258 get_remote_ipaddr(), get_remote_port(), get_local_port());
1259 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1260
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001261 if (s->ttyfd != -1)
1262 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1263 if (s->term)
1264 child_set_env(&env, &envsize, "TERM", s->term);
1265 if (s->display)
1266 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001267 if (original_command)
1268 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1269 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001270
1271#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001272 if ((cp = getenv("AUTHSTATE")) != NULL)
1273 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1274 if ((cp = getenv("KRB5CCNAME")) != NULL)
1275 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1276 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001277#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001278#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +00001279 if (s->authctxt->krb4_ticket_file)
1280 child_set_env(&env, &envsize, "KRBTKFILE",
1281 s->authctxt->krb4_ticket_file);
1282#endif
1283#ifdef KRB5
1284 if (s->authctxt->krb5_ticket_file)
1285 child_set_env(&env, &envsize, "KRB5CCNAME",
1286 s->authctxt->krb5_ticket_file);
1287#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001288#ifdef USE_PAM
1289 /* Pull in any environment variables that may have been set by PAM. */
1290 do_pam_environment(&env, &envsize);
1291#endif /* USE_PAM */
1292
Damien Millerb38eff82000-04-01 11:09:21 +10001293 if (auth_get_socket_name() != NULL)
1294 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1295 auth_get_socket_name());
1296
1297 /* read $HOME/.ssh/environment. */
1298 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001299 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1300 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001301 read_environment_file(&env, &envsize, buf);
1302 }
1303 if (debug_flag) {
1304 /* dump the environment */
1305 fprintf(stderr, "Environment:\n");
1306 for (i = 0; env[i]; i++)
1307 fprintf(stderr, " %.200s\n", env[i]);
1308 }
Damien Millerad833b32000-08-23 10:46:23 +10001309 /* we have to stash the hostname before we close our socket. */
1310 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001311 hostname = get_remote_name_or_ip(utmp_len,
1312 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001313 /*
1314 * Close the connection descriptors; note that this is the child, and
1315 * the server will still have the socket open, and it is important
1316 * that we do not shutdown it. Note that the descriptors cannot be
1317 * closed before building the environment, as we call
1318 * get_remote_ipaddr there.
1319 */
1320 if (packet_get_connection_in() == packet_get_connection_out())
1321 close(packet_get_connection_in());
1322 else {
1323 close(packet_get_connection_in());
1324 close(packet_get_connection_out());
1325 }
1326 /*
1327 * Close all descriptors related to channels. They will still remain
1328 * open in the parent.
1329 */
1330 /* XXX better use close-on-exec? -markus */
1331 channel_close_all();
1332
1333 /*
1334 * Close any extra file descriptors. Note that there may still be
1335 * descriptors left by system functions. They will be closed later.
1336 */
1337 endpwent();
1338
1339 /*
1340 * Close any extra open file descriptors so that we don\'t have them
1341 * hanging around in clients. Note that we want to do this after
1342 * initgroups, because at least on Solaris 2.3 it leaves file
1343 * descriptors open.
1344 */
1345 for (i = 3; i < 64; i++)
1346 close(i);
1347
Damien Millerb38eff82000-04-01 11:09:21 +10001348 /*
1349 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1350 * xauth are run in the proper environment.
1351 */
1352 environ = env;
1353
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001354#ifdef AFS
1355 /* Try to get AFS tokens for the local cell. */
1356 if (k_hasafs()) {
1357 char cell[64];
1358
1359 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1360 krb_afslog(cell, 0);
1361
1362 krb_afslog(0, 0);
1363 }
1364#endif /* AFS */
1365
Damien Miller139d4cd2001-10-10 15:07:44 +10001366 /* Change current directory to the user\'s home directory. */
1367 if (chdir(pw->pw_dir) < 0) {
1368 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1369 pw->pw_dir, strerror(errno));
1370#ifdef HAVE_LOGIN_CAP
1371 if (login_getcapbool(lc, "requirehome", 0))
1372 exit(1);
1373#endif
1374 }
1375
Damien Millerb38eff82000-04-01 11:09:21 +10001376 /*
1377 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1378 * in this order).
1379 */
1380 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001381 /* ignore _PATH_SSH_USER_RC for subsystems */
1382 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001383 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1384 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001385 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001386 fprintf(stderr, "Running %s\n", cmd);
1387 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001388 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001389 if (do_xauth)
1390 fprintf(f, "%s %s\n", s->auth_proto,
1391 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001392 pclose(f);
1393 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001394 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001395 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001396 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001397 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001398 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1399 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001400 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001401 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001402 if (do_xauth)
1403 fprintf(f, "%s %s\n", s->auth_proto,
1404 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001405 pclose(f);
1406 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001407 fprintf(stderr, "Could not run %s\n",
1408 _PATH_SSH_SYSTEM_RC);
1409 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001410 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001411 char *screen = strchr(s->display, ':');
1412
1413 if (debug_flag) {
1414 fprintf(stderr,
1415 "Running %.100s add "
1416 "%.100s %.100s %.100s\n",
1417 options.xauth_location, s->display,
1418 s->auth_proto, s->auth_data);
1419 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001420 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001421 "Adding %.*s/unix%s %s %s\n",
1422 (int)(screen - s->display),
1423 s->display, screen,
1424 s->auth_proto, s->auth_data);
1425 }
1426 snprintf(cmd, sizeof cmd, "%s -q -",
1427 options.xauth_location);
1428 f = popen(cmd, "w");
1429 if (f) {
1430 fprintf(f, "add %s %s %s\n", s->display,
1431 s->auth_proto, s->auth_data);
1432 if (screen != NULL)
1433 fprintf(f, "add %.*s/unix%s %s %s\n",
1434 (int)(screen - s->display),
1435 s->display, screen,
1436 s->auth_proto,
1437 s->auth_data);
1438 pclose(f);
1439 } else {
1440 fprintf(stderr, "Could not run %s\n",
1441 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001442 }
1443 }
Damien Millerb38eff82000-04-01 11:09:21 +10001444 /* Get the last component of the shell name. */
1445 cp = strrchr(shell, '/');
1446 if (cp)
1447 cp++;
1448 else
1449 cp = shell;
1450 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001451
1452 /* restore SIGPIPE for child */
1453 signal(SIGPIPE, SIG_DFL);
1454
Damien Millerb38eff82000-04-01 11:09:21 +10001455 /*
1456 * If we have no command, execute the shell. In this case, the shell
1457 * name to be passed in argv[0] is preceded by '-' to indicate that
1458 * this is a login shell.
1459 */
1460 if (!command) {
1461 if (!options.use_login) {
1462 char buf[256];
1463
Damien Millerb38eff82000-04-01 11:09:21 +10001464 /* Start the shell. Set initial character to '-'. */
1465 buf[0] = '-';
1466 strncpy(buf + 1, cp, sizeof(buf) - 1);
1467 buf[sizeof(buf) - 1] = 0;
1468
1469 /* Execute the shell. */
1470 argv[0] = buf;
1471 argv[1] = NULL;
1472 execve(shell, argv, env);
1473
1474 /* Executing the shell failed. */
1475 perror(shell);
1476 exit(1);
1477
1478 } else {
1479 /* Launch login(1). */
1480
Damien Millerad833b32000-08-23 10:46:23 +10001481 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001482#ifdef LOGIN_NEEDS_TERM
1483 s->term? s->term : "unknown",
1484#endif
Damien Millerefb1edf2001-07-14 12:19:36 +10001485 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001486
1487 /* Login couldn't be executed, die. */
1488
1489 perror("login");
1490 exit(1);
1491 }
1492 }
1493 /*
1494 * Execute the command using the user's shell. This uses the -c
1495 * option to execute the command.
1496 */
1497 argv[0] = (char *) cp;
1498 argv[1] = "-c";
1499 argv[2] = (char *) command;
1500 argv[3] = NULL;
1501 execve(shell, argv, env);
1502 perror(shell);
1503 exit(1);
1504}
1505
1506Session *
1507session_new(void)
1508{
1509 int i;
1510 static int did_init = 0;
1511 if (!did_init) {
1512 debug("session_new: init");
1513 for(i = 0; i < MAX_SESSIONS; i++) {
1514 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001515 }
1516 did_init = 1;
1517 }
1518 for(i = 0; i < MAX_SESSIONS; i++) {
1519 Session *s = &sessions[i];
1520 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001521 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001522 s->chanid = -1;
1523 s->ptyfd = -1;
1524 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001525 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001526 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001527 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001528 return s;
1529 }
1530 }
1531 return NULL;
1532}
1533
Ben Lindstrombba81212001-06-25 05:01:22 +00001534static void
Damien Millerb38eff82000-04-01 11:09:21 +10001535session_dump(void)
1536{
1537 int i;
1538 for(i = 0; i < MAX_SESSIONS; i++) {
1539 Session *s = &sessions[i];
1540 debug("dump: used %d session %d %p channel %d pid %d",
1541 s->used,
1542 s->self,
1543 s,
1544 s->chanid,
1545 s->pid);
1546 }
1547}
1548
Damien Millerefb4afe2000-04-12 18:45:05 +10001549int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001550session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001551{
1552 Session *s = session_new();
1553 debug("session_open: channel %d", chanid);
1554 if (s == NULL) {
1555 error("no more sessions");
1556 return 0;
1557 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001558 s->authctxt = authctxt;
1559 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001560 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001561 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001562 debug("session_open: session %d: link with channel %d", s->self, chanid);
1563 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001564 return 1;
1565}
1566
Ben Lindstrombba81212001-06-25 05:01:22 +00001567static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001568session_by_channel(int id)
1569{
1570 int i;
1571 for(i = 0; i < MAX_SESSIONS; i++) {
1572 Session *s = &sessions[i];
1573 if (s->used && s->chanid == id) {
1574 debug("session_by_channel: session %d channel %d", i, id);
1575 return s;
1576 }
1577 }
1578 debug("session_by_channel: unknown channel %d", id);
1579 session_dump();
1580 return NULL;
1581}
1582
Ben Lindstrombba81212001-06-25 05:01:22 +00001583static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001584session_by_pid(pid_t pid)
1585{
1586 int i;
1587 debug("session_by_pid: pid %d", pid);
1588 for(i = 0; i < MAX_SESSIONS; i++) {
1589 Session *s = &sessions[i];
1590 if (s->used && s->pid == pid)
1591 return s;
1592 }
1593 error("session_by_pid: unknown pid %d", pid);
1594 session_dump();
1595 return NULL;
1596}
1597
Ben Lindstrombba81212001-06-25 05:01:22 +00001598static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001599session_window_change_req(Session *s)
1600{
1601 s->col = packet_get_int();
1602 s->row = packet_get_int();
1603 s->xpixel = packet_get_int();
1604 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001605 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001606 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1607 return 1;
1608}
1609
Ben Lindstrombba81212001-06-25 05:01:22 +00001610static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001611session_pty_req(Session *s)
1612{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001613 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001614 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001615
Ben Lindstrom49c12602001-06-13 04:37:36 +00001616 if (no_pty_flag) {
1617 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001618 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001619 }
1620 if (s->ttyfd != -1) {
1621 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001622 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001623 }
1624
Damien Millerefb4afe2000-04-12 18:45:05 +10001625 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001626
1627 if (compat20) {
1628 s->col = packet_get_int();
1629 s->row = packet_get_int();
1630 } else {
1631 s->row = packet_get_int();
1632 s->col = packet_get_int();
1633 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001634 s->xpixel = packet_get_int();
1635 s->ypixel = packet_get_int();
1636
1637 if (strcmp(s->term, "") == 0) {
1638 xfree(s->term);
1639 s->term = NULL;
1640 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001641
Damien Millerefb4afe2000-04-12 18:45:05 +10001642 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001643 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001644 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001645 if (s->term)
1646 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001647 s->term = NULL;
1648 s->ptyfd = -1;
1649 s->ttyfd = -1;
1650 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001651 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001652 }
1653 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001654
1655 /* for SSH1 the tty modes length is not given */
1656 if (!compat20)
1657 n_bytes = packet_remaining();
1658 tty_parse_modes(s->ttyfd, &n_bytes);
1659
Damien Millerefb4afe2000-04-12 18:45:05 +10001660 /*
1661 * Add a cleanup function to clear the utmp entry and record logout
1662 * time in case we call fatal() (e.g., the connection gets closed).
1663 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001664 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001665 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001666
1667 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001668 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1669
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001670 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001671 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001672 return 1;
1673}
1674
Ben Lindstrombba81212001-06-25 05:01:22 +00001675static int
Damien Millerbd483e72000-04-30 10:00:53 +10001676session_subsystem_req(Session *s)
1677{
Damien Millerae452462001-10-10 15:08:06 +10001678 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001679 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001680 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001681 char *cmd, *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++) {
Damien Millerae452462001-10-10 15:08:06 +10001688 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1689 cmd = options.subsystem_command[i];
1690 if (stat(cmd, &st) < 0) {
1691 error("subsystem: cannot stat %s: %s", cmd,
1692 strerror(errno));
1693 break;
1694 }
1695 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001696 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001697 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001698 success = 1;
1699 }
1700 }
1701
1702 if (!success)
Damien Millerae452462001-10-10 15:08:06 +10001703 log("subsystem request for %s failed, subsystem not found",
1704 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001705
Damien Millerbd483e72000-04-30 10:00:53 +10001706 xfree(subsys);
1707 return success;
1708}
1709
Ben Lindstrombba81212001-06-25 05:01:22 +00001710static int
Damien Millerbd483e72000-04-30 10:00:53 +10001711session_x11_req(Session *s)
1712{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001713 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001714
1715 s->single_connection = packet_get_char();
1716 s->auth_proto = packet_get_string(NULL);
1717 s->auth_data = packet_get_string(NULL);
1718 s->screen = packet_get_int();
1719 packet_done();
1720
Ben Lindstrom768176b2001-06-09 01:29:12 +00001721 success = session_setup_x11fwd(s);
1722 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001723 xfree(s->auth_proto);
1724 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001725 s->auth_proto = NULL;
1726 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001727 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001728 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001729}
1730
Ben Lindstrombba81212001-06-25 05:01:22 +00001731static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001732session_shell_req(Session *s)
1733{
Damien Millerf6d9e222000-06-18 14:50:44 +10001734 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001735 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001736 return 1;
1737}
1738
Ben Lindstrombba81212001-06-25 05:01:22 +00001739static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001740session_exec_req(Session *s)
1741{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001742 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001743 char *command = packet_get_string(&len);
1744 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001745 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001746 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001747 return 1;
1748}
1749
Ben Lindstrombba81212001-06-25 05:01:22 +00001750static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001751session_auth_agent_req(Session *s)
1752{
1753 static int called = 0;
1754 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001755 if (no_agent_forwarding_flag) {
1756 debug("session_auth_agent_req: no_agent_forwarding_flag");
1757 return 0;
1758 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001759 if (called) {
1760 return 0;
1761 } else {
1762 called = 1;
1763 return auth_input_request_forwarding(s->pw);
1764 }
1765}
1766
Damien Millerefb4afe2000-04-12 18:45:05 +10001767void
1768session_input_channel_req(int id, void *arg)
1769{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001770 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001771 int reply;
1772 int success = 0;
1773 char *rtype;
1774 Session *s;
1775 Channel *c;
1776
1777 rtype = packet_get_string(&len);
1778 reply = packet_get_char();
1779
1780 s = session_by_channel(id);
1781 if (s == NULL)
1782 fatal("session_input_channel_req: channel %d: no session", id);
1783 c = channel_lookup(id);
1784 if (c == NULL)
1785 fatal("session_input_channel_req: channel %d: bad channel", id);
1786
1787 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1788 s->self, id, rtype, reply);
1789
1790 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001791 * a session is in LARVAL state until a shell, a command
1792 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001793 */
1794 if (c->type == SSH_CHANNEL_LARVAL) {
1795 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001796 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001797 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001798 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001799 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001800 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001801 } else if (strcmp(rtype, "x11-req") == 0) {
1802 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001803 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1804 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001805 } else if (strcmp(rtype, "subsystem") == 0) {
1806 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001807 }
1808 }
1809 if (strcmp(rtype, "window-change") == 0) {
1810 success = session_window_change_req(s);
1811 }
1812
1813 if (reply) {
1814 packet_start(success ?
1815 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1816 packet_put_int(c->remote_id);
1817 packet_send();
1818 }
1819 xfree(rtype);
1820}
1821
1822void
1823session_set_fds(Session *s, int fdin, int fdout, int fderr)
1824{
1825 if (!compat20)
1826 fatal("session_set_fds: called for proto != 2.0");
1827 /*
1828 * now that have a child and a pipe to the child,
1829 * we can activate our channel and register the fd's
1830 */
1831 if (s->chanid == -1)
1832 fatal("no channel for session %d", s->self);
1833 channel_set_fds(s->chanid,
1834 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001835 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1836 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001837}
1838
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001839/*
1840 * Function to perform pty cleanup. Also called if we get aborted abnormally
1841 * (e.g., due to a dropped connection).
1842 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001843static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001844session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001845{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001846 Session *s = session;
1847
1848 if (s == NULL) {
1849 error("session_pty_cleanup: no session");
1850 return;
1851 }
1852 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001853 return;
1854
Kevin Steves43cdef32001-02-11 14:12:08 +00001855 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001856
Damien Millerb38eff82000-04-01 11:09:21 +10001857 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001858 if (s->pid != 0)
1859 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001860
1861 /* Release the pseudo-tty. */
1862 pty_release(s->tty);
1863
1864 /*
1865 * Close the server side of the socket pairs. We must do this after
1866 * the pty cleanup, so that another process doesn't get this pty
1867 * while we're still cleaning up.
1868 */
1869 if (close(s->ptymaster) < 0)
1870 error("close(s->ptymaster): %s", strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001871
1872 /* unlink pty from session */
1873 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001874}
Damien Millerefb4afe2000-04-12 18:45:05 +10001875
Ben Lindstrombba81212001-06-25 05:01:22 +00001876static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001877session_exit_message(Session *s, int status)
1878{
1879 Channel *c;
1880 if (s == NULL)
1881 fatal("session_close: no session");
1882 c = channel_lookup(s->chanid);
1883 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001884 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001885 s->self, s->chanid);
1886 debug("session_exit_message: session %d channel %d pid %d",
1887 s->self, s->chanid, s->pid);
1888
1889 if (WIFEXITED(status)) {
1890 channel_request_start(s->chanid,
1891 "exit-status", 0);
1892 packet_put_int(WEXITSTATUS(status));
1893 packet_send();
1894 } else if (WIFSIGNALED(status)) {
1895 channel_request_start(s->chanid,
1896 "exit-signal", 0);
1897 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001898#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001899 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001900#else /* WCOREDUMP */
1901 packet_put_char(0);
1902#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001903 packet_put_cstring("");
1904 packet_put_cstring("");
1905 packet_send();
1906 } else {
1907 /* Some weird exit cause. Just exit. */
1908 packet_disconnect("wait returned status %04x.", status);
1909 }
1910
1911 /* disconnect channel */
1912 debug("session_exit_message: release channel %d", s->chanid);
1913 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001914 /*
1915 * emulate a write failure with 'chan_write_failed', nobody will be
1916 * interested in data we write.
1917 * Note that we must not call 'chan_read_failed', since there could
1918 * be some more data waiting in the pipe.
1919 */
Damien Millerbd483e72000-04-30 10:00:53 +10001920 if (c->ostate != CHAN_OUTPUT_CLOSED)
1921 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001922 s->chanid = -1;
1923}
1924
Ben Lindstrombba81212001-06-25 05:01:22 +00001925static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001926session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001927{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001928 debug("session_close: session %d pid %d", s->self, s->pid);
1929 if (s->ttyfd != -1) {
1930 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1931 session_pty_cleanup(s);
1932 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001933 if (s->term)
1934 xfree(s->term);
1935 if (s->display)
1936 xfree(s->display);
1937 if (s->auth_data)
1938 xfree(s->auth_data);
1939 if (s->auth_proto)
1940 xfree(s->auth_proto);
1941 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001942 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001943}
1944
1945void
1946session_close_by_pid(pid_t pid, int status)
1947{
1948 Session *s = session_by_pid(pid);
1949 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001950 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001951 return;
1952 }
1953 if (s->chanid != -1)
1954 session_exit_message(s, status);
1955 session_close(s);
1956}
1957
1958/*
1959 * this is called when a channel dies before
1960 * the session 'child' itself dies
1961 */
1962void
1963session_close_by_channel(int id, void *arg)
1964{
1965 Session *s = session_by_channel(id);
1966 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001967 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001968 return;
1969 }
Damien Miller3ec27592001-10-12 11:35:04 +10001970 debug("session_close_by_channel: channel %d child %d", id, s->pid);
1971 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10001972 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10001973 /*
1974 * delay detach of session, but release pty, since
1975 * the fd's to the child are already closed
1976 */
1977 if (s->ttyfd != -1) {
1978 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1979 session_pty_cleanup(s);
1980 }
Damien Miller3ec27592001-10-12 11:35:04 +10001981 return;
1982 }
1983 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10001984 channel_cancel_cleanup(s->chanid);
1985 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10001986 session_close(s);
1987}
1988
1989void
Damien Miller3ec27592001-10-12 11:35:04 +10001990session_destroy_all(void)
Damien Miller52b77be2001-10-10 15:14:37 +10001991{
1992 int i;
1993 for(i = 0; i < MAX_SESSIONS; i++) {
1994 Session *s = &sessions[i];
Damien Miller3ec27592001-10-12 11:35:04 +10001995 if (s->used)
Damien Miller52b77be2001-10-10 15:14:37 +10001996 session_close(s);
Damien Miller52b77be2001-10-10 15:14:37 +10001997 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001998}
1999
Ben Lindstrombba81212001-06-25 05:01:22 +00002000static char *
Damien Millere247cc42000-05-07 12:03:14 +10002001session_tty_list(void)
2002{
2003 static char buf[1024];
2004 int i;
2005 buf[0] = '\0';
2006 for(i = 0; i < MAX_SESSIONS; i++) {
2007 Session *s = &sessions[i];
2008 if (s->used && s->ttyfd != -1) {
2009 if (buf[0] != '\0')
2010 strlcat(buf, ",", sizeof buf);
2011 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2012 }
2013 }
2014 if (buf[0] == '\0')
2015 strlcpy(buf, "notty", sizeof buf);
2016 return buf;
2017}
2018
2019void
2020session_proctitle(Session *s)
2021{
2022 if (s->pw == NULL)
2023 error("no user for session %d", s->self);
2024 else
2025 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2026}
2027
Ben Lindstrom768176b2001-06-09 01:29:12 +00002028int
2029session_setup_x11fwd(Session *s)
2030{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002031 struct stat st;
2032
2033 if (no_x11_forwarding_flag) {
2034 packet_send_debug("X11 forwarding disabled in user configuration file.");
2035 return 0;
2036 }
2037 if (!options.x11_forwarding) {
2038 debug("X11 forwarding disabled in server configuration file.");
2039 return 0;
2040 }
2041 if (!options.xauth_location ||
2042 (stat(options.xauth_location, &st) == -1)) {
2043 packet_send_debug("No xauth program; cannot forward with spoofing.");
2044 return 0;
2045 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002046 if (options.use_login) {
2047 packet_send_debug("X11 forwarding disabled; "
2048 "not compatible with UseLogin=yes.");
2049 return 0;
2050 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002051 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002052 debug("X11 display already set.");
2053 return 0;
2054 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002055 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
2056 if (s->display == NULL) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002057 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002058 return 0;
2059 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002060 return 1;
2061}
2062
Ben Lindstrombba81212001-06-25 05:01:22 +00002063static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002064do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002065{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002066 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002067}