blob: d0f9072f39fe93a79da7dd650b82fdb54228b79f [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
Damien Millerd2c208a2000-05-17 22:00:02 +1000162char *aixloginmsg;
163#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000164
Damien Millerad833b32000-08-23 10:46:23 +1000165#ifdef HAVE_LOGIN_CAP
166static login_cap_t *lc;
167#endif
168
Ben Lindstromb31783d2001-03-22 02:02:12 +0000169void
170do_authenticated(Authctxt *authctxt)
171{
172 /*
173 * Cancel the alarm we set to limit the time taken for
174 * authentication.
175 */
176 alarm(0);
177 if (startup_pipe != -1) {
178 close(startup_pipe);
179 startup_pipe = -1;
180 }
181#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
182 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
183 error("unable to get login class");
184 return;
185 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000186#ifdef BSD_AUTH
187 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
188 packet_disconnect("Approval failure for %s",
189 authctxt->pw->pw_name);
190 }
191#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000192#endif
Damien Millere49d0962001-11-13 23:46:18 +1100193#ifdef WITH_AIXAUTHENTICATE
194 /* We don't have a pty yet, so just label the line as "ssh" */
195 if (loginsuccess(authctxt->user,
196 get_canonical_hostname(options.reverse_mapping_check),
197 "ssh", &aixloginmsg) < 0)
198 aixloginmsg = NULL;
199#endif /* WITH_AIXAUTHENTICATE */
200
Ben Lindstromb31783d2001-03-22 02:02:12 +0000201 /* setup the channel layer */
202 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
203 channel_permit_all_opens();
204
205 if (compat20)
206 do_authenticated2(authctxt);
207 else
208 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000209
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000210 /* remove agent socket */
Ben Lindstrom838394c2001-06-09 01:11:59 +0000211 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000212 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000213#ifdef KRB4
214 if (options.kerberos_ticket_cleanup)
215 krb4_cleanup_proc(authctxt);
216#endif
217#ifdef KRB5
218 if (options.kerberos_ticket_cleanup)
219 krb5_cleanup_proc(authctxt);
220#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000221}
222
Damien Millerb38eff82000-04-01 11:09:21 +1000223/*
Damien Millerb38eff82000-04-01 11:09:21 +1000224 * Prepares for an interactive session. This is called after the user has
225 * been successfully authenticated. During this message exchange, pseudo
226 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
227 * are requested, etc.
228 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000230do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000231{
232 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000233 char *command;
Ben Lindstrom49c12602001-06-13 04:37:36 +0000234 int success, type, plen, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000235 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000236 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000237
238 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000239 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000240 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000241
Damien Millerb38eff82000-04-01 11:09:21 +1000242 /*
243 * We stay in this loop until the client requests to execute a shell
244 * or a command.
245 */
246 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000247 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000248
249 /* Get a packet from the client. */
250 type = packet_read(&plen);
251
252 /* Process the packet. */
253 switch (type) {
254 case SSH_CMSG_REQUEST_COMPRESSION:
255 packet_integrity_check(plen, 4, type);
256 compression_level = packet_get_int();
257 if (compression_level < 1 || compression_level > 9) {
258 packet_send_debug("Received illegal compression level %d.",
259 compression_level);
260 break;
261 }
262 /* Enable compression after we have responded with SUCCESS. */
263 enable_compression_after_reply = 1;
264 success = 1;
265 break;
266
267 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000268 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000269 break;
270
271 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000272 s->auth_proto = packet_get_string(&proto_len);
273 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000274
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000275 screen_flag = packet_get_protocol_flags() &
276 SSH_PROTOFLAG_SCREEN_NUMBER;
277 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
278
279 if (packet_remaining() == 4) {
280 if (!screen_flag)
281 debug2("Buggy client: "
282 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000283 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000284 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000285 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000286 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000287 packet_done();
288 success = session_setup_x11fwd(s);
289 if (!success) {
290 xfree(s->auth_proto);
291 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000292 s->auth_proto = NULL;
293 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000294 }
Damien Millerb38eff82000-04-01 11:09:21 +1000295 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000296
297 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
298 if (no_agent_forwarding_flag || compat13) {
299 debug("Authentication agent forwarding not permitted for this authentication.");
300 break;
301 }
302 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000303 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000304 break;
305
306 case SSH_CMSG_PORT_FORWARD_REQUEST:
307 if (no_port_forwarding_flag) {
308 debug("Port forwarding not permitted for this authentication.");
309 break;
310 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100311 if (!options.allow_tcp_forwarding) {
312 debug("Port forwarding not permitted.");
313 break;
314 }
Damien Millerb38eff82000-04-01 11:09:21 +1000315 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000316 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000317 success = 1;
318 break;
319
320 case SSH_CMSG_MAX_PACKET_SIZE:
321 if (packet_set_maxsize(packet_get_int()) > 0)
322 success = 1;
323 break;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000324
325#if defined(AFS) || defined(KRB5)
326 case SSH_CMSG_HAVE_KERBEROS_TGT:
327 if (!options.kerberos_tgt_passing) {
328 verbose("Kerberos TGT passing disabled.");
329 } else {
330 char *kdata = packet_get_string(&dlen);
331 packet_integrity_check(plen, 4 + dlen, type);
332
333 /* XXX - 0x41, see creds_to_radix version */
334 if (kdata[0] != 0x41) {
335#ifdef KRB5
336 krb5_data tgt;
337 tgt.data = kdata;
338 tgt.length = dlen;
339
340 if (auth_krb5_tgt(s->authctxt, &tgt))
341 success = 1;
342 else
343 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
344#endif /* KRB5 */
345 } else {
346#ifdef AFS
347 if (auth_krb4_tgt(s->authctxt, kdata))
348 success = 1;
349 else
350 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
351#endif /* AFS */
352 }
353 xfree(kdata);
354 }
355 break;
356#endif /* AFS || KRB5 */
357
358#ifdef AFS
359 case SSH_CMSG_HAVE_AFS_TOKEN:
360 if (!options.afs_token_passing || !k_hasafs()) {
361 verbose("AFS token passing disabled.");
362 } else {
363 /* Accept AFS token. */
364 char *token = packet_get_string(&dlen);
365 packet_integrity_check(plen, 4 + dlen, type);
366
367 if (auth_afs_token(s->authctxt, token))
368 success = 1;
369 else
370 verbose("AFS token refused for %.100s",
371 s->authctxt->user);
372 xfree(token);
373 }
374 break;
375#endif /* AFS */
Damien Millerb38eff82000-04-01 11:09:21 +1000376
377 case SSH_CMSG_EXEC_SHELL:
378 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000379 if (type == SSH_CMSG_EXEC_CMD) {
380 command = packet_get_string(&dlen);
381 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000382 do_exec(s, command);
383 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000384 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000385 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000386 }
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000387 packet_done();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000388 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000389 return;
390
391 default:
392 /*
393 * Any unknown messages in this phase are ignored,
394 * and a failure message is returned.
395 */
396 log("Unknown packet type received after authentication: %d", type);
397 }
398 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
399 packet_send();
400 packet_write_wait();
401
402 /* Enable compression now that we have replied if appropriate. */
403 if (enable_compression_after_reply) {
404 enable_compression_after_reply = 0;
405 packet_start_compression(compression_level);
406 }
407 }
408}
409
410/*
411 * This is called to fork and execute a command when we have no tty. This
412 * will call do_child from the child, and server_loop from the parent after
413 * setting up file descriptors and such.
414 */
Damien Miller4af51302000-04-16 11:18:38 +1000415void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000416do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000417{
418 int pid;
419
420#ifdef USE_PIPES
421 int pin[2], pout[2], perr[2];
422 /* Allocate pipes for communicating with the program. */
423 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
424 packet_disconnect("Could not create pipes: %.100s",
425 strerror(errno));
426#else /* USE_PIPES */
427 int inout[2], err[2];
428 /* Uses socket pairs to communicate with the program. */
429 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
430 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
431 packet_disconnect("Could not create socket pairs: %.100s",
432 strerror(errno));
433#endif /* USE_PIPES */
434 if (s == NULL)
435 fatal("do_exec_no_pty: no session");
436
Damien Millere247cc42000-05-07 12:03:14 +1000437 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000438
Damien Millerc5946332001-02-28 11:46:11 +1100439#if defined(USE_PAM)
Damien Miller665af9c2001-06-27 09:34:15 +1000440 do_pam_session(s->pw->pw_name, NULL);
Damien Millerf9e93002001-03-27 16:12:24 +1000441 do_pam_setcred(1);
Damien Millerc3aa3dd2001-10-28 22:34:52 +1100442 if (is_pam_password_change_required())
443 packet_disconnect("Password change required but no "
444 "TTY available");
Kevin Stevesff793a22001-02-21 16:36:51 +0000445#endif /* USE_PAM */
446
Damien Millerb38eff82000-04-01 11:09:21 +1000447 /* Fork the child. */
448 if ((pid = fork()) == 0) {
449 /* Child. Reinitialize the log since the pid has changed. */
450 log_init(__progname, options.log_level, options.log_facility, log_stderr);
451
452 /*
453 * Create a new session and process group since the 4.4BSD
454 * setlogin() affects the entire process group.
455 */
456 if (setsid() < 0)
457 error("setsid failed: %.100s", strerror(errno));
458
459#ifdef USE_PIPES
460 /*
461 * Redirect stdin. We close the parent side of the socket
462 * pair, and make the child side the standard input.
463 */
464 close(pin[1]);
465 if (dup2(pin[0], 0) < 0)
466 perror("dup2 stdin");
467 close(pin[0]);
468
469 /* Redirect stdout. */
470 close(pout[0]);
471 if (dup2(pout[1], 1) < 0)
472 perror("dup2 stdout");
473 close(pout[1]);
474
475 /* Redirect stderr. */
476 close(perr[0]);
477 if (dup2(perr[1], 2) < 0)
478 perror("dup2 stderr");
479 close(perr[1]);
480#else /* USE_PIPES */
481 /*
482 * Redirect stdin, stdout, and stderr. Stdin and stdout will
483 * use the same socket, as some programs (particularly rdist)
484 * seem to depend on it.
485 */
486 close(inout[1]);
487 close(err[1]);
488 if (dup2(inout[0], 0) < 0) /* stdin */
489 perror("dup2 stdin");
490 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
491 perror("dup2 stdout");
492 if (dup2(err[0], 2) < 0) /* stderr */
493 perror("dup2 stderr");
494#endif /* USE_PIPES */
495
496 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000497 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000498 /* NOTREACHED */
499 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100500#ifdef HAVE_CYGWIN
501 if (is_winnt)
502 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
503#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000504 if (pid < 0)
505 packet_disconnect("fork failed: %.100s", strerror(errno));
506 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000507 /* Set interactive/non-interactive mode. */
508 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000509#ifdef USE_PIPES
510 /* We are the parent. Close the child sides of the pipes. */
511 close(pin[0]);
512 close(pout[1]);
513 close(perr[1]);
514
Damien Millerefb4afe2000-04-12 18:45:05 +1000515 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000516 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000517 } else {
518 /* Enter the interactive session. */
519 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000520 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000521 }
Damien Millerb38eff82000-04-01 11:09:21 +1000522#else /* USE_PIPES */
523 /* We are the parent. Close the child sides of the socket pairs. */
524 close(inout[0]);
525 close(err[0]);
526
527 /*
528 * Enter the interactive session. Note: server_loop must be able to
529 * handle the case that fdin and fdout are the same.
530 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000531 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000532 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000533 } else {
534 server_loop(pid, inout[1], inout[1], err[1]);
535 /* server_loop has closed inout[1] and err[1]. */
536 }
Damien Millerb38eff82000-04-01 11:09:21 +1000537#endif /* USE_PIPES */
538}
539
540/*
541 * This is called to fork and execute a command when we have a tty. This
542 * will call do_child from the child, and server_loop from the parent after
543 * setting up file descriptors, controlling tty, updating wtmp, utmp,
544 * lastlog, and other such operations.
545 */
Damien Miller4af51302000-04-16 11:18:38 +1000546void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000547do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000548{
Damien Millerb38eff82000-04-01 11:09:21 +1000549 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000550 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000551
552 if (s == NULL)
553 fatal("do_exec_pty: no session");
554 ptyfd = s->ptyfd;
555 ttyfd = s->ttyfd;
556
Damien Millerc5946332001-02-28 11:46:11 +1100557#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000558 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000559 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100560#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000561
Damien Millerb38eff82000-04-01 11:09:21 +1000562 /* Fork the child. */
563 if ((pid = fork()) == 0) {
Ben Lindstrombddd5512001-07-04 04:53:53 +0000564
Damien Miller942da032000-08-18 13:59:06 +1000565 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000566 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000567 /* Close the master side of the pseudo tty. */
568 close(ptyfd);
569
570 /* Make the pseudo tty our controlling tty. */
571 pty_make_controlling_tty(&ttyfd, s->tty);
572
Damien Miller9c751422001-10-10 15:02:46 +1000573 /* Redirect stdin/stdout/stderr from the pseudo tty. */
574 if (dup2(ttyfd, 0) < 0)
575 error("dup2 stdin: %s", strerror(errno));
576 if (dup2(ttyfd, 1) < 0)
577 error("dup2 stdout: %s", strerror(errno));
578 if (dup2(ttyfd, 2) < 0)
579 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000580
581 /* Close the extra descriptor for the pseudo tty. */
582 close(ttyfd);
583
Damien Miller942da032000-08-18 13:59:06 +1000584 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000585#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100586 if (!(options.use_login && command == NULL))
587 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000588# ifdef LOGIN_NEEDS_UTMPX
589 else
590 do_pre_login(s);
591# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000592#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000593
Damien Millerb38eff82000-04-01 11:09:21 +1000594 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000595 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000596 /* NOTREACHED */
597 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100598#ifdef HAVE_CYGWIN
599 if (is_winnt)
600 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
601#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000602 if (pid < 0)
603 packet_disconnect("fork failed: %.100s", strerror(errno));
604 s->pid = pid;
605
606 /* Parent. Close the slave side of the pseudo tty. */
607 close(ttyfd);
608
609 /*
610 * Create another descriptor of the pty master side for use as the
611 * standard input. We could use the original descriptor, but this
612 * simplifies code in server_loop. The descriptor is bidirectional.
613 */
614 fdout = dup(ptyfd);
615 if (fdout < 0)
616 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
617
618 /* we keep a reference to the pty master */
619 ptymaster = dup(ptyfd);
620 if (ptymaster < 0)
621 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
622 s->ptymaster = ptymaster;
623
624 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000625 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000626 if (compat20) {
627 session_set_fds(s, ptyfd, fdout, -1);
628 } else {
629 server_loop(pid, ptyfd, fdout, -1);
630 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000631 }
Damien Millerb38eff82000-04-01 11:09:21 +1000632}
633
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000634#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000635static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000636do_pre_login(Session *s)
637{
638 socklen_t fromlen;
639 struct sockaddr_storage from;
640 pid_t pid = getpid();
641
642 /*
643 * Get IP address of client. If the connection is not a socket, let
644 * the address be 0.0.0.0.
645 */
646 memset(&from, 0, sizeof(from));
647 if (packet_connection_is_on_socket()) {
648 fromlen = sizeof(from);
649 if (getpeername(packet_get_connection_in(),
650 (struct sockaddr *) & from, &fromlen) < 0) {
651 debug("getpeername: %.100s", strerror(errno));
652 fatal_cleanup();
653 }
654 }
655
656 record_utmp_only(pid, s->tty, s->pw->pw_name,
657 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
658 (struct sockaddr *)&from);
659}
660#endif
661
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000662/*
663 * This is called to fork and execute a command. If another command is
664 * to be forced, execute that instead.
665 */
666void
667do_exec(Session *s, const char *command)
668{
669 if (forced_command) {
670 original_command = command;
671 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000672 debug("Forced command '%.900s'", command);
673 }
674
675 if (s->ttyfd != -1)
676 do_exec_pty(s, command);
677 else
678 do_exec_no_pty(s, command);
679
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000680 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000681}
682
Damien Miller942da032000-08-18 13:59:06 +1000683/* administrative, login(1)-like work */
684void
Damien Miller69b69aa2000-10-28 14:19:58 +1100685do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000686{
Damien Miller942da032000-08-18 13:59:06 +1000687 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100688 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000689 socklen_t fromlen;
690 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000691 time_t last_login_time;
692 struct passwd * pw = s->pw;
693 pid_t pid = getpid();
694
695 /*
696 * Get IP address of client. If the connection is not a socket, let
697 * the address be 0.0.0.0.
698 */
699 memset(&from, 0, sizeof(from));
700 if (packet_connection_is_on_socket()) {
701 fromlen = sizeof(from);
702 if (getpeername(packet_get_connection_in(),
703 (struct sockaddr *) & from, &fromlen) < 0) {
704 debug("getpeername: %.100s", strerror(errno));
705 fatal_cleanup();
706 }
707 }
708
Damien Miller7b28dc52000-09-05 13:34:53 +1100709 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000710 if (options.print_lastlog) {
711 hostname[0] = '\0';
712 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
713 hostname, sizeof(hostname));
714 }
Damien Millere4340be2000-09-16 13:29:08 +1100715
Damien Miller942da032000-08-18 13:59:06 +1000716 /* Record that there was a login on that tty from the remote host. */
717 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000718 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
719 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000720
Kevin Steves092f2ef2000-10-14 13:36:13 +0000721#ifdef USE_PAM
722 /*
723 * If password change is needed, do it now.
724 * This needs to occur before the ~/.hushlogin check.
725 */
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();
728 do_pam_chauthtok();
729 }
730#endif
731
Damien Millercf205e82001-04-16 18:29:15 +1000732 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000733 return;
734
735#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100736 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000737 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000738#endif /* USE_PAM */
739#ifdef WITH_AIXAUTHENTICATE
740 if (aixloginmsg && *aixloginmsg)
741 printf("%s\n", aixloginmsg);
742#endif /* WITH_AIXAUTHENTICATE */
743
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000744 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000745 time_string = ctime(&last_login_time);
746 if (strchr(time_string, '\n'))
747 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000748 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000749 printf("Last login: %s\r\n", time_string);
750 else
Damien Millere4340be2000-09-16 13:29:08 +1100751 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000752 }
Damien Millercf205e82001-04-16 18:29:15 +1000753
754 do_motd();
755}
756
757/*
758 * Display the message of the day.
759 */
760void
761do_motd(void)
762{
763 FILE *f;
764 char buf[256];
765
Damien Miller942da032000-08-18 13:59:06 +1000766 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000767#ifdef HAVE_LOGIN_CAP
768 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
769 "/etc/motd"), "r");
770#else
Damien Miller942da032000-08-18 13:59:06 +1000771 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000772#endif
Damien Miller942da032000-08-18 13:59:06 +1000773 if (f) {
774 while (fgets(buf, sizeof(buf), f))
775 fputs(buf, stdout);
776 fclose(f);
777 }
778 }
779}
780
Kevin Steves8f63caa2001-07-04 18:23:02 +0000781
782/*
783 * Check for quiet login, either .hushlogin or command given.
784 */
785int
786check_quietlogin(Session *s, const char *command)
787{
788 char buf[256];
789 struct passwd *pw = s->pw;
790 struct stat st;
791
792 /* Return 1 if .hushlogin exists or a command given. */
793 if (command != NULL)
794 return 1;
795 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
796#ifdef HAVE_LOGIN_CAP
797 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
798 return 1;
799#else
800 if (stat(buf, &st) >= 0)
801 return 1;
802#endif
803 return 0;
804}
805
Damien Millerb38eff82000-04-01 11:09:21 +1000806/*
807 * Sets the value of the given variable in the environment. If the variable
808 * already exists, its value is overriden.
809 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000810static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000811child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000812 const char *value)
813{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000814 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000815 char **env;
816
817 /*
818 * Find the slot where the value should be stored. If the variable
819 * already exists, we reuse the slot; otherwise we append a new slot
820 * at the end of the array, expanding if necessary.
821 */
822 env = *envp;
823 namelen = strlen(name);
824 for (i = 0; env[i]; i++)
825 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
826 break;
827 if (env[i]) {
828 /* Reuse the slot. */
829 xfree(env[i]);
830 } else {
831 /* New variable. Expand if necessary. */
832 if (i >= (*envsizep) - 1) {
833 (*envsizep) += 50;
834 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
835 }
836 /* Need to set the NULL pointer at end of array beyond the new slot. */
837 env[i + 1] = NULL;
838 }
839
840 /* Allocate space and format the variable in the appropriate slot. */
841 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
842 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
843}
844
845/*
846 * Reads environment variables from the given file and adds/overrides them
847 * into the environment. If the file does not exist, this does nothing.
848 * Otherwise, it must consist of empty lines, comments (line starts with '#')
849 * and assignments of the form name=value. No other forms are allowed.
850 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000851static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000852read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000853 const char *filename)
854{
855 FILE *f;
856 char buf[4096];
857 char *cp, *value;
858
859 f = fopen(filename, "r");
860 if (!f)
861 return;
862
863 while (fgets(buf, sizeof(buf), f)) {
864 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
865 ;
866 if (!*cp || *cp == '#' || *cp == '\n')
867 continue;
868 if (strchr(cp, '\n'))
869 *strchr(cp, '\n') = '\0';
870 value = strchr(cp, '=');
871 if (value == NULL) {
872 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
873 continue;
874 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000875 /*
876 * Replace the equals sign by nul, and advance value to
877 * the value string.
878 */
Damien Millerb38eff82000-04-01 11:09:21 +1000879 *value = '\0';
880 value++;
881 child_set_env(env, envsize, cp, value);
882 }
883 fclose(f);
884}
885
886#ifdef USE_PAM
887/*
888 * Sets any environment variables which have been specified by PAM
889 */
Kevin Steves9b26f962001-06-29 17:52:17 +0000890void do_pam_environment(char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000891{
892 char *equals, var_name[512], var_val[512];
893 char **pam_env;
894 int i;
895
896 if ((pam_env = fetch_pam_environment()) == NULL)
897 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000898
Damien Millerb38eff82000-04-01 11:09:21 +1000899 for(i = 0; pam_env[i] != NULL; i++) {
900 if ((equals = strstr(pam_env[i], "=")) == NULL)
901 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000902
Damien Millerb38eff82000-04-01 11:09:21 +1000903 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
904 memset(var_name, '\0', sizeof(var_name));
905 memset(var_val, '\0', sizeof(var_val));
906
907 strncpy(var_name, pam_env[i], equals - pam_env[i]);
908 strcpy(var_val, equals + 1);
909
Damien Millercb5e44a2000-09-29 12:12:36 +1100910 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000911
912 child_set_env(env, envsize, var_name, var_val);
913 }
914 }
915}
916#endif /* USE_PAM */
917
Damien Millercb5e44a2000-09-29 12:12:36 +1100918#ifdef HAVE_CYGWIN
Kevin Steves9b26f962001-06-29 17:52:17 +0000919void copy_environment(char ***env, u_int *envsize)
Damien Millercb5e44a2000-09-29 12:12:36 +1100920{
921 char *equals, var_name[512], var_val[512];
922 int i;
923
924 for(i = 0; environ[i] != NULL; i++) {
925 if ((equals = strstr(environ[i], "=")) == NULL)
926 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000927
Damien Millercb5e44a2000-09-29 12:12:36 +1100928 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
929 memset(var_name, '\0', sizeof(var_name));
930 memset(var_val, '\0', sizeof(var_val));
931
932 strncpy(var_name, environ[i], equals - environ[i]);
933 strcpy(var_val, equals + 1);
934
935 debug3("Copy environment: %s=%s", var_name, var_val);
936
937 child_set_env(env, envsize, var_name, var_val);
938 }
939 }
940}
941#endif
942
Damien Miller5fc85652000-07-09 23:53:07 +1000943#if defined(HAVE_GETUSERATTR)
944/*
945 * AIX-specific login initialisation
946 */
947void set_limit(char *user, char *soft, char *hard, int resource, int mult)
948{
949 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000950 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000951
952 getrlimit(resource, &rlim);
953
Damien Miller65964d62000-07-11 09:16:22 +1000954 slim = 0;
955 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
956 if (slim < 0) {
957 rlim.rlim_cur = RLIM_INFINITY;
958 } else if (slim != 0) {
959 /* See the wackiness below */
960 if (rlim.rlim_cur == slim * mult)
961 slim = 0;
962 else
963 rlim.rlim_cur = slim * mult;
964 }
965 }
Damien Miller5fc85652000-07-09 23:53:07 +1000966
Damien Miller65964d62000-07-11 09:16:22 +1000967 hlim = 0;
968 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
969 if (hlim < 0) {
970 rlim.rlim_max = RLIM_INFINITY;
971 } else if (hlim != 0) {
972 rlim.rlim_max = hlim * mult;
973 }
974 }
Damien Miller5fc85652000-07-09 23:53:07 +1000975
Damien Miller65964d62000-07-11 09:16:22 +1000976 /*
977 * XXX For cpu and fsize the soft limit is set to the hard limit
978 * if the hard limit is left at its default value and the soft limit
979 * is changed from its default value, either by requesting it
980 * (slim == 0) or by setting it to the current default. At least
981 * that's how rlogind does it. If you're confused you're not alone.
982 * Bug or feature? AIX 4.3.1.2
983 */
984 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
985 && hlim == 0 && slim != 0)
986 rlim.rlim_max = rlim.rlim_cur;
987 /* A specified hard limit limits the soft limit */
988 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
989 rlim.rlim_cur = rlim.rlim_max;
990 /* A soft limit can increase a hard limit */
991 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000992 rlim.rlim_max = rlim.rlim_cur;
993
994 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000995 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000996}
997
998void set_limits_from_userattr(char *user)
999{
1000 int mask;
1001 char buf[16];
1002
1003 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
1004 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
1005 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1006 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1007 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1008 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1009#if defined(S_UNOFILE)
1010 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1011#endif
1012
1013 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1014 /* Convert decimal to octal */
1015 (void) snprintf(buf, sizeof(buf), "%d", mask);
1016 if (sscanf(buf, "%o", &mask) == 1)
1017 umask(mask);
1018 }
1019}
1020#endif /* defined(HAVE_GETUSERATTR) */
1021
Damien Millerb38eff82000-04-01 11:09:21 +10001022/*
1023 * Performs common processing for the child, such as setting up the
1024 * environment, closing extra file descriptors, setting the user and group
1025 * ids, and executing the command or shell.
1026 */
Damien Miller4af51302000-04-16 11:18:38 +10001027void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001028do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001029{
Damien Miller7b28dc52000-09-05 13:34:53 +11001030 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstromec95ed92001-07-04 04:21:14 +00001031 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001032 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001033 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001034 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001035 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001036 char **env;
1037 extern char **environ;
1038 struct stat st;
1039 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001040 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +10001041#ifdef WITH_IRIX_PROJECT
1042 prid_t projid;
1043#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001044#ifdef WITH_IRIX_JOBS
1045 jid_t jid = 0;
1046#else
1047#ifdef WITH_IRIX_ARRAY
1048 int jid = 0;
1049#endif /* WITH_IRIX_ARRAY */
1050#endif /* WITH_IRIX_JOBS */
1051
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001052 do_xauth =
1053 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1054
Ben Lindstrom005dd222001-04-18 15:29:33 +00001055 /* remove hostkey from the child's memory */
1056 destroy_sensitive_data();
1057
Damien Millerd3a18572000-06-07 19:55:44 +10001058 /* login(1) is only called if we execute the login shell */
1059 if (options.use_login && command != NULL)
1060 options.use_login = 0;
1061
Damien Millerc62f1fc2001-07-14 11:54:05 +10001062#if !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001063 if (!options.use_login) {
1064# ifdef HAVE_LOGIN_CAP
1065 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1066 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1067 _PATH_NOLOGIN), "r");
1068# else /* HAVE_LOGIN_CAP */
1069 if (pw->pw_uid)
1070 f = fopen(_PATH_NOLOGIN, "r");
1071# endif /* HAVE_LOGIN_CAP */
1072 if (f) {
1073 /* /etc/nologin exists. Print its contents and exit. */
1074 while (fgets(buf, sizeof(buf), f))
1075 fputs(buf, stderr);
1076 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001077 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001078 }
Damien Millerb38eff82000-04-01 11:09:21 +10001079 }
Damien Millerc62f1fc2001-07-14 11:54:05 +10001080#endif /* HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001081
Damien Millerad833b32000-08-23 10:46:23 +10001082 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001083 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1084 switch, so we let login(1) to this for us. */
1085 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001086#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001087 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001088 if (!check_quietlogin(s, command))
1089 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001090#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001091#ifdef HAVE_CYGWIN
1092 if (is_winnt) {
1093#else
Damien Millerb38eff82000-04-01 11:09:21 +10001094 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001095#endif
Damien Millerad833b32000-08-23 10:46:23 +10001096# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001097 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001098# endif /* HAVE_GETUSERATTR */
1099# ifdef HAVE_LOGIN_CAP
1100 if (setusercontext(lc, pw, pw->pw_uid,
1101 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1102 perror("unable to set user context");
1103 exit(1);
1104 }
1105# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001106#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1107 /* Sets login uid for accounting */
1108 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1109 error("setluid: %s", strerror(errno));
1110#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1111
Damien Millerad833b32000-08-23 10:46:23 +10001112 if (setlogin(pw->pw_name) < 0)
1113 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001114 if (setgid(pw->pw_gid) < 0) {
1115 perror("setgid");
1116 exit(1);
1117 }
1118 /* Initialize the group list. */
1119 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1120 perror("initgroups");
1121 exit(1);
1122 }
1123 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001124# ifdef USE_PAM
1125 /*
1126 * PAM credentials may take the form of
1127 * supplementary groups. These will have been
1128 * wiped by the above initgroups() call.
1129 * Reestablish them here.
1130 */
1131 do_pam_setcred(0);
1132# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001133# ifdef WITH_IRIX_JOBS
1134 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1135 if (jid == -1) {
1136 fatal("Failed to create job container: %.100s",
1137 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001138 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001139# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001140# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001141 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001142 if (jid == 0) {
1143 if (newarraysess() != 0)
1144 fatal("Failed to set up new array session: %.100s",
1145 strerror(errno));
1146 }
Damien Millerad833b32000-08-23 10:46:23 +10001147# endif /* WITH_IRIX_ARRAY */
1148# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001149 /* initialize irix project info */
1150 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1151 debug("Failed to get project id, using projid 0");
1152 projid = 0;
1153 }
Damien Miller91606b12000-06-28 08:22:29 +10001154 if (setprid(projid))
1155 fatal("Failed to initialize project %d for %s: %.100s",
1156 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001157# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001158#ifdef WITH_IRIX_AUDIT
1159 if (sysconf(_SC_AUDIT)) {
1160 debug("Setting sat id to %d", (int) pw->pw_uid);
1161 if (satsetid(pw->pw_uid))
1162 debug("error setting satid: %.100s", strerror(errno));
1163 }
1164#endif /* WITH_IRIX_AUDIT */
1165
Damien Miller168a7002001-03-17 10:29:50 +11001166#ifdef _AIX
1167 /*
1168 * AIX has a "usrinfo" area where logname and
1169 * other stuff is stored - a few applications
1170 * actually use this and die if it's not set
1171 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001172 if (s->ttyfd == -1)
1173 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001174 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001175 2 * strlen(pw->pw_name));
1176 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001177 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001178 if (usrinfo(SETUINFO, cp, i) == -1)
1179 fatal("Couldn't set usrinfo: %s",
1180 strerror(errno));
1181 debug3("AIX/UsrInfo: set len %d", i);
1182 xfree(cp);
1183#endif
1184
Damien Millerb38eff82000-04-01 11:09:21 +10001185 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001186 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001187# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001188 }
Damien Millerad833b32000-08-23 10:46:23 +10001189#endif /* HAVE_OSF_SIA */
1190
Damien Millerbac2d8a2000-09-05 16:13:06 +11001191#ifdef HAVE_CYGWIN
1192 if (is_winnt)
1193#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001194 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001195 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001196 }
1197 /*
1198 * Get the shell from the password data. An empty shell field is
1199 * legal, and means /bin/sh.
1200 */
1201 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001202#ifdef HAVE_LOGIN_CAP
1203 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1204#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001205
Damien Millerb38eff82000-04-01 11:09:21 +10001206 /* Initialize the environment. */
1207 envsize = 100;
1208 env = xmalloc(envsize * sizeof(char *));
1209 env[0] = NULL;
1210
Damien Millerbac2d8a2000-09-05 16:13:06 +11001211#ifdef HAVE_CYGWIN
1212 /*
1213 * The Windows environment contains some setting which are
1214 * important for a running system. They must not be dropped.
1215 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001216 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001217#endif
1218
Damien Millerb38eff82000-04-01 11:09:21 +10001219 if (!options.use_login) {
1220 /* Set basic environment. */
1221 child_set_env(&env, &envsize, "USER", pw->pw_name);
1222 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1223 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001224#ifdef HAVE_LOGIN_CAP
1225 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1226 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001227#else /* HAVE_LOGIN_CAP */
1228# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001229 /*
1230 * There's no standard path on Windows. The path contains
1231 * important components pointing to the system directories,
1232 * needed for loading shared libraries. So the path better
1233 * remains intact here.
1234 */
Damien Millerb38eff82000-04-01 11:09:21 +10001235 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001236# endif /* HAVE_CYGWIN */
1237#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001238
1239 snprintf(buf, sizeof buf, "%.200s/%.50s",
1240 _PATH_MAILDIR, pw->pw_name);
1241 child_set_env(&env, &envsize, "MAIL", buf);
1242
1243 /* Normal systems set SHELL by default. */
1244 child_set_env(&env, &envsize, "SHELL", shell);
1245 }
1246 if (getenv("TZ"))
1247 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1248
1249 /* Set custom environment options from RSA authentication. */
1250 while (custom_environment) {
1251 struct envstring *ce = custom_environment;
1252 char *s = ce->s;
1253 int i;
1254 for (i = 0; s[i] != '=' && s[i]; i++);
1255 if (s[i] == '=') {
1256 s[i] = 0;
1257 child_set_env(&env, &envsize, s, s + i + 1);
1258 }
1259 custom_environment = ce->next;
1260 xfree(ce->s);
1261 xfree(ce);
1262 }
1263
1264 snprintf(buf, sizeof buf, "%.50s %d %d",
1265 get_remote_ipaddr(), get_remote_port(), get_local_port());
1266 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1267
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001268 if (s->ttyfd != -1)
1269 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1270 if (s->term)
1271 child_set_env(&env, &envsize, "TERM", s->term);
1272 if (s->display)
1273 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001274 if (original_command)
1275 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1276 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001277
1278#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001279 if ((cp = getenv("AUTHSTATE")) != NULL)
1280 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1281 if ((cp = getenv("KRB5CCNAME")) != NULL)
1282 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1283 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001284#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001285#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +00001286 if (s->authctxt->krb4_ticket_file)
1287 child_set_env(&env, &envsize, "KRBTKFILE",
1288 s->authctxt->krb4_ticket_file);
1289#endif
1290#ifdef KRB5
1291 if (s->authctxt->krb5_ticket_file)
1292 child_set_env(&env, &envsize, "KRB5CCNAME",
1293 s->authctxt->krb5_ticket_file);
1294#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001295#ifdef USE_PAM
1296 /* Pull in any environment variables that may have been set by PAM. */
1297 do_pam_environment(&env, &envsize);
1298#endif /* USE_PAM */
1299
Damien Millerb38eff82000-04-01 11:09:21 +10001300 if (auth_get_socket_name() != NULL)
1301 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1302 auth_get_socket_name());
1303
1304 /* read $HOME/.ssh/environment. */
1305 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001306 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1307 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001308 read_environment_file(&env, &envsize, buf);
1309 }
1310 if (debug_flag) {
1311 /* dump the environment */
1312 fprintf(stderr, "Environment:\n");
1313 for (i = 0; env[i]; i++)
1314 fprintf(stderr, " %.200s\n", env[i]);
1315 }
Damien Millerad833b32000-08-23 10:46:23 +10001316 /* we have to stash the hostname before we close our socket. */
1317 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001318 hostname = get_remote_name_or_ip(utmp_len,
1319 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001320 /*
1321 * Close the connection descriptors; note that this is the child, and
1322 * the server will still have the socket open, and it is important
1323 * that we do not shutdown it. Note that the descriptors cannot be
1324 * closed before building the environment, as we call
1325 * get_remote_ipaddr there.
1326 */
1327 if (packet_get_connection_in() == packet_get_connection_out())
1328 close(packet_get_connection_in());
1329 else {
1330 close(packet_get_connection_in());
1331 close(packet_get_connection_out());
1332 }
1333 /*
1334 * Close all descriptors related to channels. They will still remain
1335 * open in the parent.
1336 */
1337 /* XXX better use close-on-exec? -markus */
1338 channel_close_all();
1339
1340 /*
1341 * Close any extra file descriptors. Note that there may still be
1342 * descriptors left by system functions. They will be closed later.
1343 */
1344 endpwent();
1345
1346 /*
1347 * Close any extra open file descriptors so that we don\'t have them
1348 * hanging around in clients. Note that we want to do this after
1349 * initgroups, because at least on Solaris 2.3 it leaves file
1350 * descriptors open.
1351 */
1352 for (i = 3; i < 64; i++)
1353 close(i);
1354
Damien Millerb38eff82000-04-01 11:09:21 +10001355 /*
1356 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1357 * xauth are run in the proper environment.
1358 */
1359 environ = env;
1360
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001361#ifdef AFS
1362 /* Try to get AFS tokens for the local cell. */
1363 if (k_hasafs()) {
1364 char cell[64];
1365
1366 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1367 krb_afslog(cell, 0);
1368
1369 krb_afslog(0, 0);
1370 }
1371#endif /* AFS */
1372
Damien Miller139d4cd2001-10-10 15:07:44 +10001373 /* Change current directory to the user\'s home directory. */
1374 if (chdir(pw->pw_dir) < 0) {
1375 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1376 pw->pw_dir, strerror(errno));
1377#ifdef HAVE_LOGIN_CAP
1378 if (login_getcapbool(lc, "requirehome", 0))
1379 exit(1);
1380#endif
1381 }
1382
Damien Millerb38eff82000-04-01 11:09:21 +10001383 /*
1384 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1385 * in this order).
1386 */
1387 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001388 /* ignore _PATH_SSH_USER_RC for subsystems */
1389 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001390 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1391 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001392 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001393 fprintf(stderr, "Running %s\n", cmd);
1394 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001395 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001396 if (do_xauth)
1397 fprintf(f, "%s %s\n", s->auth_proto,
1398 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001399 pclose(f);
1400 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001401 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001402 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001403 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001404 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001405 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1406 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001407 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001408 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001409 if (do_xauth)
1410 fprintf(f, "%s %s\n", s->auth_proto,
1411 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001412 pclose(f);
1413 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001414 fprintf(stderr, "Could not run %s\n",
1415 _PATH_SSH_SYSTEM_RC);
1416 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001417 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001418 char *screen = strchr(s->display, ':');
1419
1420 if (debug_flag) {
1421 fprintf(stderr,
1422 "Running %.100s add "
1423 "%.100s %.100s %.100s\n",
1424 options.xauth_location, s->display,
1425 s->auth_proto, s->auth_data);
1426 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001427 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001428 "Adding %.*s/unix%s %s %s\n",
1429 (int)(screen - s->display),
1430 s->display, screen,
1431 s->auth_proto, s->auth_data);
1432 }
1433 snprintf(cmd, sizeof cmd, "%s -q -",
1434 options.xauth_location);
1435 f = popen(cmd, "w");
1436 if (f) {
1437 fprintf(f, "add %s %s %s\n", s->display,
1438 s->auth_proto, s->auth_data);
1439 if (screen != NULL)
1440 fprintf(f, "add %.*s/unix%s %s %s\n",
1441 (int)(screen - s->display),
1442 s->display, screen,
1443 s->auth_proto,
1444 s->auth_data);
1445 pclose(f);
1446 } else {
1447 fprintf(stderr, "Could not run %s\n",
1448 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001449 }
1450 }
Damien Millerb38eff82000-04-01 11:09:21 +10001451 /* Get the last component of the shell name. */
1452 cp = strrchr(shell, '/');
1453 if (cp)
1454 cp++;
1455 else
1456 cp = shell;
1457 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001458
1459 /* restore SIGPIPE for child */
1460 signal(SIGPIPE, SIG_DFL);
1461
Damien Millerb38eff82000-04-01 11:09:21 +10001462 /*
1463 * If we have no command, execute the shell. In this case, the shell
1464 * name to be passed in argv[0] is preceded by '-' to indicate that
1465 * this is a login shell.
1466 */
1467 if (!command) {
1468 if (!options.use_login) {
1469 char buf[256];
1470
Damien Millerb38eff82000-04-01 11:09:21 +10001471 /* Start the shell. Set initial character to '-'. */
1472 buf[0] = '-';
1473 strncpy(buf + 1, cp, sizeof(buf) - 1);
1474 buf[sizeof(buf) - 1] = 0;
1475
1476 /* Execute the shell. */
1477 argv[0] = buf;
1478 argv[1] = NULL;
1479 execve(shell, argv, env);
1480
1481 /* Executing the shell failed. */
1482 perror(shell);
1483 exit(1);
1484
1485 } else {
1486 /* Launch login(1). */
1487
Damien Millerad833b32000-08-23 10:46:23 +10001488 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001489#ifdef LOGIN_NEEDS_TERM
1490 s->term? s->term : "unknown",
1491#endif
Damien Millerefb1edf2001-07-14 12:19:36 +10001492 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001493
1494 /* Login couldn't be executed, die. */
1495
1496 perror("login");
1497 exit(1);
1498 }
1499 }
1500 /*
1501 * Execute the command using the user's shell. This uses the -c
1502 * option to execute the command.
1503 */
1504 argv[0] = (char *) cp;
1505 argv[1] = "-c";
1506 argv[2] = (char *) command;
1507 argv[3] = NULL;
1508 execve(shell, argv, env);
1509 perror(shell);
1510 exit(1);
1511}
1512
1513Session *
1514session_new(void)
1515{
1516 int i;
1517 static int did_init = 0;
1518 if (!did_init) {
1519 debug("session_new: init");
1520 for(i = 0; i < MAX_SESSIONS; i++) {
1521 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001522 }
1523 did_init = 1;
1524 }
1525 for(i = 0; i < MAX_SESSIONS; i++) {
1526 Session *s = &sessions[i];
1527 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001528 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001529 s->chanid = -1;
1530 s->ptyfd = -1;
1531 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001532 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001533 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001534 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001535 return s;
1536 }
1537 }
1538 return NULL;
1539}
1540
Ben Lindstrombba81212001-06-25 05:01:22 +00001541static void
Damien Millerb38eff82000-04-01 11:09:21 +10001542session_dump(void)
1543{
1544 int i;
1545 for(i = 0; i < MAX_SESSIONS; i++) {
1546 Session *s = &sessions[i];
1547 debug("dump: used %d session %d %p channel %d pid %d",
1548 s->used,
1549 s->self,
1550 s,
1551 s->chanid,
1552 s->pid);
1553 }
1554}
1555
Damien Millerefb4afe2000-04-12 18:45:05 +10001556int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001557session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001558{
1559 Session *s = session_new();
1560 debug("session_open: channel %d", chanid);
1561 if (s == NULL) {
1562 error("no more sessions");
1563 return 0;
1564 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001565 s->authctxt = authctxt;
1566 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001567 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001568 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001569 debug("session_open: session %d: link with channel %d", s->self, chanid);
1570 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001571 return 1;
1572}
1573
Ben Lindstrombba81212001-06-25 05:01:22 +00001574static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001575session_by_channel(int id)
1576{
1577 int i;
1578 for(i = 0; i < MAX_SESSIONS; i++) {
1579 Session *s = &sessions[i];
1580 if (s->used && s->chanid == id) {
1581 debug("session_by_channel: session %d channel %d", i, id);
1582 return s;
1583 }
1584 }
1585 debug("session_by_channel: unknown channel %d", id);
1586 session_dump();
1587 return NULL;
1588}
1589
Ben Lindstrombba81212001-06-25 05:01:22 +00001590static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001591session_by_pid(pid_t pid)
1592{
1593 int i;
1594 debug("session_by_pid: pid %d", pid);
1595 for(i = 0; i < MAX_SESSIONS; i++) {
1596 Session *s = &sessions[i];
1597 if (s->used && s->pid == pid)
1598 return s;
1599 }
1600 error("session_by_pid: unknown pid %d", pid);
1601 session_dump();
1602 return NULL;
1603}
1604
Ben Lindstrombba81212001-06-25 05:01:22 +00001605static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001606session_window_change_req(Session *s)
1607{
1608 s->col = packet_get_int();
1609 s->row = packet_get_int();
1610 s->xpixel = packet_get_int();
1611 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001612 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001613 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1614 return 1;
1615}
1616
Ben Lindstrombba81212001-06-25 05:01:22 +00001617static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001618session_pty_req(Session *s)
1619{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001620 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001621 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001622
Ben Lindstrom49c12602001-06-13 04:37:36 +00001623 if (no_pty_flag) {
1624 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001625 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001626 }
1627 if (s->ttyfd != -1) {
1628 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001629 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001630 }
1631
Damien Millerefb4afe2000-04-12 18:45:05 +10001632 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001633
1634 if (compat20) {
1635 s->col = packet_get_int();
1636 s->row = packet_get_int();
1637 } else {
1638 s->row = packet_get_int();
1639 s->col = packet_get_int();
1640 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001641 s->xpixel = packet_get_int();
1642 s->ypixel = packet_get_int();
1643
1644 if (strcmp(s->term, "") == 0) {
1645 xfree(s->term);
1646 s->term = NULL;
1647 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001648
Damien Millerefb4afe2000-04-12 18:45:05 +10001649 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001650 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001651 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001652 if (s->term)
1653 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001654 s->term = NULL;
1655 s->ptyfd = -1;
1656 s->ttyfd = -1;
1657 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001658 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001659 }
1660 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001661
1662 /* for SSH1 the tty modes length is not given */
1663 if (!compat20)
1664 n_bytes = packet_remaining();
1665 tty_parse_modes(s->ttyfd, &n_bytes);
1666
Damien Millerefb4afe2000-04-12 18:45:05 +10001667 /*
1668 * Add a cleanup function to clear the utmp entry and record logout
1669 * time in case we call fatal() (e.g., the connection gets closed).
1670 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001671 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001672 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001673
1674 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001675 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1676
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001677 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001678 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001679 return 1;
1680}
1681
Ben Lindstrombba81212001-06-25 05:01:22 +00001682static int
Damien Millerbd483e72000-04-30 10:00:53 +10001683session_subsystem_req(Session *s)
1684{
Damien Millerae452462001-10-10 15:08:06 +10001685 struct stat st;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001686 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001687 int success = 0;
Damien Millerae452462001-10-10 15:08:06 +10001688 char *cmd, *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001689 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001690
1691 packet_done();
1692 log("subsystem request for %s", subsys);
1693
Damien Millerf6d9e222000-06-18 14:50:44 +10001694 for (i = 0; i < options.num_subsystems; i++) {
Damien Millerae452462001-10-10 15:08:06 +10001695 if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1696 cmd = options.subsystem_command[i];
1697 if (stat(cmd, &st) < 0) {
1698 error("subsystem: cannot stat %s: %s", cmd,
1699 strerror(errno));
1700 break;
1701 }
1702 debug("subsystem: exec() %s", cmd);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001703 s->is_subsystem = 1;
Damien Millerae452462001-10-10 15:08:06 +10001704 do_exec(s, cmd);
Damien Millerf6d9e222000-06-18 14:50:44 +10001705 success = 1;
1706 }
1707 }
1708
1709 if (!success)
Damien Millerae452462001-10-10 15:08:06 +10001710 log("subsystem request for %s failed, subsystem not found",
1711 subsys);
Damien Millerf6d9e222000-06-18 14:50:44 +10001712
Damien Millerbd483e72000-04-30 10:00:53 +10001713 xfree(subsys);
1714 return success;
1715}
1716
Ben Lindstrombba81212001-06-25 05:01:22 +00001717static int
Damien Millerbd483e72000-04-30 10:00:53 +10001718session_x11_req(Session *s)
1719{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001720 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001721
1722 s->single_connection = packet_get_char();
1723 s->auth_proto = packet_get_string(NULL);
1724 s->auth_data = packet_get_string(NULL);
1725 s->screen = packet_get_int();
1726 packet_done();
1727
Ben Lindstrom768176b2001-06-09 01:29:12 +00001728 success = session_setup_x11fwd(s);
1729 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001730 xfree(s->auth_proto);
1731 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001732 s->auth_proto = NULL;
1733 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001734 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001735 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001736}
1737
Ben Lindstrombba81212001-06-25 05:01:22 +00001738static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001739session_shell_req(Session *s)
1740{
Damien Millerf6d9e222000-06-18 14:50:44 +10001741 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001742 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001743 return 1;
1744}
1745
Ben Lindstrombba81212001-06-25 05:01:22 +00001746static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001747session_exec_req(Session *s)
1748{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001749 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001750 char *command = packet_get_string(&len);
1751 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001752 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001753 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001754 return 1;
1755}
1756
Ben Lindstrombba81212001-06-25 05:01:22 +00001757static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001758session_auth_agent_req(Session *s)
1759{
1760 static int called = 0;
1761 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001762 if (no_agent_forwarding_flag) {
1763 debug("session_auth_agent_req: no_agent_forwarding_flag");
1764 return 0;
1765 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001766 if (called) {
1767 return 0;
1768 } else {
1769 called = 1;
1770 return auth_input_request_forwarding(s->pw);
1771 }
1772}
1773
Damien Millerefb4afe2000-04-12 18:45:05 +10001774void
1775session_input_channel_req(int id, void *arg)
1776{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001777 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001778 int reply;
1779 int success = 0;
1780 char *rtype;
1781 Session *s;
1782 Channel *c;
1783
1784 rtype = packet_get_string(&len);
1785 reply = packet_get_char();
1786
1787 s = session_by_channel(id);
1788 if (s == NULL)
1789 fatal("session_input_channel_req: channel %d: no session", id);
1790 c = channel_lookup(id);
1791 if (c == NULL)
1792 fatal("session_input_channel_req: channel %d: bad channel", id);
1793
1794 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1795 s->self, id, rtype, reply);
1796
1797 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001798 * a session is in LARVAL state until a shell, a command
1799 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001800 */
1801 if (c->type == SSH_CHANNEL_LARVAL) {
1802 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001803 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001804 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001805 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001806 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001807 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001808 } else if (strcmp(rtype, "x11-req") == 0) {
1809 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001810 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1811 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001812 } else if (strcmp(rtype, "subsystem") == 0) {
1813 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001814 }
1815 }
1816 if (strcmp(rtype, "window-change") == 0) {
1817 success = session_window_change_req(s);
1818 }
1819
1820 if (reply) {
1821 packet_start(success ?
1822 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1823 packet_put_int(c->remote_id);
1824 packet_send();
1825 }
1826 xfree(rtype);
1827}
1828
1829void
1830session_set_fds(Session *s, int fdin, int fdout, int fderr)
1831{
1832 if (!compat20)
1833 fatal("session_set_fds: called for proto != 2.0");
1834 /*
1835 * now that have a child and a pipe to the child,
1836 * we can activate our channel and register the fd's
1837 */
1838 if (s->chanid == -1)
1839 fatal("no channel for session %d", s->self);
1840 channel_set_fds(s->chanid,
1841 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001842 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1843 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001844}
1845
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001846/*
1847 * Function to perform pty cleanup. Also called if we get aborted abnormally
1848 * (e.g., due to a dropped connection).
1849 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001850static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001851session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001852{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001853 Session *s = session;
1854
1855 if (s == NULL) {
1856 error("session_pty_cleanup: no session");
1857 return;
1858 }
1859 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001860 return;
1861
Kevin Steves43cdef32001-02-11 14:12:08 +00001862 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001863
Damien Millerb38eff82000-04-01 11:09:21 +10001864 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001865 if (s->pid != 0)
1866 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001867
1868 /* Release the pseudo-tty. */
1869 pty_release(s->tty);
1870
1871 /*
1872 * Close the server side of the socket pairs. We must do this after
1873 * the pty cleanup, so that another process doesn't get this pty
1874 * while we're still cleaning up.
1875 */
1876 if (close(s->ptymaster) < 0)
1877 error("close(s->ptymaster): %s", strerror(errno));
Damien Miller0585d512001-10-12 11:35:50 +10001878
1879 /* unlink pty from session */
1880 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001881}
Damien Millerefb4afe2000-04-12 18:45:05 +10001882
Ben Lindstrombba81212001-06-25 05:01:22 +00001883static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001884session_exit_message(Session *s, int status)
1885{
1886 Channel *c;
1887 if (s == NULL)
1888 fatal("session_close: no session");
1889 c = channel_lookup(s->chanid);
1890 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001891 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001892 s->self, s->chanid);
1893 debug("session_exit_message: session %d channel %d pid %d",
1894 s->self, s->chanid, s->pid);
1895
1896 if (WIFEXITED(status)) {
1897 channel_request_start(s->chanid,
1898 "exit-status", 0);
1899 packet_put_int(WEXITSTATUS(status));
1900 packet_send();
1901 } else if (WIFSIGNALED(status)) {
1902 channel_request_start(s->chanid,
1903 "exit-signal", 0);
1904 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001905#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001906 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001907#else /* WCOREDUMP */
1908 packet_put_char(0);
1909#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001910 packet_put_cstring("");
1911 packet_put_cstring("");
1912 packet_send();
1913 } else {
1914 /* Some weird exit cause. Just exit. */
1915 packet_disconnect("wait returned status %04x.", status);
1916 }
1917
1918 /* disconnect channel */
1919 debug("session_exit_message: release channel %d", s->chanid);
1920 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001921 /*
1922 * emulate a write failure with 'chan_write_failed', nobody will be
1923 * interested in data we write.
1924 * Note that we must not call 'chan_read_failed', since there could
1925 * be some more data waiting in the pipe.
1926 */
Damien Millerbd483e72000-04-30 10:00:53 +10001927 if (c->ostate != CHAN_OUTPUT_CLOSED)
1928 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001929 s->chanid = -1;
1930}
1931
Ben Lindstrombba81212001-06-25 05:01:22 +00001932static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001933session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001934{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001935 debug("session_close: session %d pid %d", s->self, s->pid);
1936 if (s->ttyfd != -1) {
1937 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1938 session_pty_cleanup(s);
1939 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001940 if (s->term)
1941 xfree(s->term);
1942 if (s->display)
1943 xfree(s->display);
1944 if (s->auth_data)
1945 xfree(s->auth_data);
1946 if (s->auth_proto)
1947 xfree(s->auth_proto);
1948 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001949 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001950}
1951
1952void
1953session_close_by_pid(pid_t pid, int status)
1954{
1955 Session *s = session_by_pid(pid);
1956 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001957 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001958 return;
1959 }
1960 if (s->chanid != -1)
1961 session_exit_message(s, status);
1962 session_close(s);
1963}
1964
1965/*
1966 * this is called when a channel dies before
1967 * the session 'child' itself dies
1968 */
1969void
1970session_close_by_channel(int id, void *arg)
1971{
1972 Session *s = session_by_channel(id);
1973 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001974 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001975 return;
1976 }
Damien Miller3ec27592001-10-12 11:35:04 +10001977 debug("session_close_by_channel: channel %d child %d", id, s->pid);
1978 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10001979 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10001980 /*
1981 * delay detach of session, but release pty, since
1982 * the fd's to the child are already closed
1983 */
1984 if (s->ttyfd != -1) {
1985 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1986 session_pty_cleanup(s);
1987 }
Damien Miller3ec27592001-10-12 11:35:04 +10001988 return;
1989 }
1990 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10001991 channel_cancel_cleanup(s->chanid);
1992 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10001993 session_close(s);
1994}
1995
1996void
Damien Miller3ec27592001-10-12 11:35:04 +10001997session_destroy_all(void)
Damien Miller52b77be2001-10-10 15:14:37 +10001998{
1999 int i;
2000 for(i = 0; i < MAX_SESSIONS; i++) {
2001 Session *s = &sessions[i];
Damien Miller3ec27592001-10-12 11:35:04 +10002002 if (s->used)
Damien Miller52b77be2001-10-10 15:14:37 +10002003 session_close(s);
Damien Miller52b77be2001-10-10 15:14:37 +10002004 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002005}
2006
Ben Lindstrombba81212001-06-25 05:01:22 +00002007static char *
Damien Millere247cc42000-05-07 12:03:14 +10002008session_tty_list(void)
2009{
2010 static char buf[1024];
2011 int i;
2012 buf[0] = '\0';
2013 for(i = 0; i < MAX_SESSIONS; i++) {
2014 Session *s = &sessions[i];
2015 if (s->used && s->ttyfd != -1) {
2016 if (buf[0] != '\0')
2017 strlcat(buf, ",", sizeof buf);
2018 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2019 }
2020 }
2021 if (buf[0] == '\0')
2022 strlcpy(buf, "notty", sizeof buf);
2023 return buf;
2024}
2025
2026void
2027session_proctitle(Session *s)
2028{
2029 if (s->pw == NULL)
2030 error("no user for session %d", s->self);
2031 else
2032 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2033}
2034
Ben Lindstrom768176b2001-06-09 01:29:12 +00002035int
2036session_setup_x11fwd(Session *s)
2037{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002038 struct stat st;
2039
2040 if (no_x11_forwarding_flag) {
2041 packet_send_debug("X11 forwarding disabled in user configuration file.");
2042 return 0;
2043 }
2044 if (!options.x11_forwarding) {
2045 debug("X11 forwarding disabled in server configuration file.");
2046 return 0;
2047 }
2048 if (!options.xauth_location ||
2049 (stat(options.xauth_location, &st) == -1)) {
2050 packet_send_debug("No xauth program; cannot forward with spoofing.");
2051 return 0;
2052 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002053 if (options.use_login) {
2054 packet_send_debug("X11 forwarding disabled; "
2055 "not compatible with UseLogin=yes.");
2056 return 0;
2057 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002058 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002059 debug("X11 display already set.");
2060 return 0;
2061 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002062 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
2063 if (s->display == NULL) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002064 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002065 return 0;
2066 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002067 return 1;
2068}
2069
Ben Lindstrombba81212001-06-25 05:01:22 +00002070static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002071do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002072{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002073 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002074}