blob: d0685f86bec104d7a8b1d7efa22598fcd91e3367 [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
Damien Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000012 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Ben Lindstromb09f6b52001-09-14 23:12:07 +000036RCSID("$OpenBSD: session.c,v 1.101 2001/09/14 18:59:11 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 *);
132void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000133void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000134int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000135
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static void do_authenticated1(Authctxt *);
137static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void session_close(Session *);
140static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000141
Damien Millerb38eff82000-04-01 11:09:21 +1000142/* import */
143extern ServerOptions options;
144extern char *__progname;
145extern int log_stderr;
146extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000147extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000148extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000149extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000150
Damien Miller7b28dc52000-09-05 13:34:53 +1100151/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000152const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100153
Damien Millerb38eff82000-04-01 11:09:21 +1000154/* data */
155#define MAX_SESSIONS 10
156Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100157
Damien Millerd2c208a2000-05-17 22:00:02 +1000158#ifdef WITH_AIXAUTHENTICATE
159/* AIX's lastlogin message, set in auth1.c */
160char *aixloginmsg;
161#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000162
Damien Millerad833b32000-08-23 10:46:23 +1000163#ifdef HAVE_LOGIN_CAP
164static login_cap_t *lc;
165#endif
166
Ben Lindstromb31783d2001-03-22 02:02:12 +0000167void
168do_authenticated(Authctxt *authctxt)
169{
170 /*
171 * Cancel the alarm we set to limit the time taken for
172 * authentication.
173 */
174 alarm(0);
175 if (startup_pipe != -1) {
176 close(startup_pipe);
177 startup_pipe = -1;
178 }
179#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
180 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
181 error("unable to get login class");
182 return;
183 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000184#ifdef BSD_AUTH
185 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
186 packet_disconnect("Approval failure for %s",
187 authctxt->pw->pw_name);
188 }
189#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000190#endif
191 /* setup the channel layer */
192 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
193 channel_permit_all_opens();
194
195 if (compat20)
196 do_authenticated2(authctxt);
197 else
198 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000199
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000200 /* remove agent socket */
Ben Lindstrom838394c2001-06-09 01:11:59 +0000201 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000202 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000203#ifdef KRB4
204 if (options.kerberos_ticket_cleanup)
205 krb4_cleanup_proc(authctxt);
206#endif
207#ifdef KRB5
208 if (options.kerberos_ticket_cleanup)
209 krb5_cleanup_proc(authctxt);
210#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000211}
212
Damien Millerb38eff82000-04-01 11:09:21 +1000213/*
Damien Millerb38eff82000-04-01 11:09:21 +1000214 * Prepares for an interactive session. This is called after the user has
215 * been successfully authenticated. During this message exchange, pseudo
216 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
217 * are requested, etc.
218 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000219static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000220do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000221{
222 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000223 char *command;
Ben Lindstrom49c12602001-06-13 04:37:36 +0000224 int success, type, plen, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000225 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000226 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000227
228 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000229 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000230 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000231
Damien Millerb38eff82000-04-01 11:09:21 +1000232 /*
233 * We stay in this loop until the client requests to execute a shell
234 * or a command.
235 */
236 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000237 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000238
239 /* Get a packet from the client. */
240 type = packet_read(&plen);
241
242 /* Process the packet. */
243 switch (type) {
244 case SSH_CMSG_REQUEST_COMPRESSION:
245 packet_integrity_check(plen, 4, type);
246 compression_level = packet_get_int();
247 if (compression_level < 1 || compression_level > 9) {
248 packet_send_debug("Received illegal compression level %d.",
249 compression_level);
250 break;
251 }
252 /* Enable compression after we have responded with SUCCESS. */
253 enable_compression_after_reply = 1;
254 success = 1;
255 break;
256
257 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000258 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000259 break;
260
261 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000262 s->auth_proto = packet_get_string(&proto_len);
263 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000264
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000265 screen_flag = packet_get_protocol_flags() &
266 SSH_PROTOFLAG_SCREEN_NUMBER;
267 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
268
269 if (packet_remaining() == 4) {
270 if (!screen_flag)
271 debug2("Buggy client: "
272 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000273 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000274 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000275 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000276 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000277 packet_done();
278 success = session_setup_x11fwd(s);
279 if (!success) {
280 xfree(s->auth_proto);
281 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000282 s->auth_proto = NULL;
283 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000284 }
Damien Millerb38eff82000-04-01 11:09:21 +1000285 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000286
287 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
288 if (no_agent_forwarding_flag || compat13) {
289 debug("Authentication agent forwarding not permitted for this authentication.");
290 break;
291 }
292 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000293 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000294 break;
295
296 case SSH_CMSG_PORT_FORWARD_REQUEST:
297 if (no_port_forwarding_flag) {
298 debug("Port forwarding not permitted for this authentication.");
299 break;
300 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100301 if (!options.allow_tcp_forwarding) {
302 debug("Port forwarding not permitted.");
303 break;
304 }
Damien Millerb38eff82000-04-01 11:09:21 +1000305 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000306 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000307 success = 1;
308 break;
309
310 case SSH_CMSG_MAX_PACKET_SIZE:
311 if (packet_set_maxsize(packet_get_int()) > 0)
312 success = 1;
313 break;
Ben Lindstromec95ed92001-07-04 04:21:14 +0000314
315#if defined(AFS) || defined(KRB5)
316 case SSH_CMSG_HAVE_KERBEROS_TGT:
317 if (!options.kerberos_tgt_passing) {
318 verbose("Kerberos TGT passing disabled.");
319 } else {
320 char *kdata = packet_get_string(&dlen);
321 packet_integrity_check(plen, 4 + dlen, type);
322
323 /* XXX - 0x41, see creds_to_radix version */
324 if (kdata[0] != 0x41) {
325#ifdef KRB5
326 krb5_data tgt;
327 tgt.data = kdata;
328 tgt.length = dlen;
329
330 if (auth_krb5_tgt(s->authctxt, &tgt))
331 success = 1;
332 else
333 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
334#endif /* KRB5 */
335 } else {
336#ifdef AFS
337 if (auth_krb4_tgt(s->authctxt, kdata))
338 success = 1;
339 else
340 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
341#endif /* AFS */
342 }
343 xfree(kdata);
344 }
345 break;
346#endif /* AFS || KRB5 */
347
348#ifdef AFS
349 case SSH_CMSG_HAVE_AFS_TOKEN:
350 if (!options.afs_token_passing || !k_hasafs()) {
351 verbose("AFS token passing disabled.");
352 } else {
353 /* Accept AFS token. */
354 char *token = packet_get_string(&dlen);
355 packet_integrity_check(plen, 4 + dlen, type);
356
357 if (auth_afs_token(s->authctxt, token))
358 success = 1;
359 else
360 verbose("AFS token refused for %.100s",
361 s->authctxt->user);
362 xfree(token);
363 }
364 break;
365#endif /* AFS */
Damien Millerb38eff82000-04-01 11:09:21 +1000366
367 case SSH_CMSG_EXEC_SHELL:
368 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000369 if (type == SSH_CMSG_EXEC_CMD) {
370 command = packet_get_string(&dlen);
371 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000372 do_exec(s, command);
373 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000374 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000375 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000376 }
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000377 packet_done();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000378 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000379 return;
380
381 default:
382 /*
383 * Any unknown messages in this phase are ignored,
384 * and a failure message is returned.
385 */
386 log("Unknown packet type received after authentication: %d", type);
387 }
388 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
389 packet_send();
390 packet_write_wait();
391
392 /* Enable compression now that we have replied if appropriate. */
393 if (enable_compression_after_reply) {
394 enable_compression_after_reply = 0;
395 packet_start_compression(compression_level);
396 }
397 }
398}
399
400/*
401 * This is called to fork and execute a command when we have no tty. This
402 * will call do_child from the child, and server_loop from the parent after
403 * setting up file descriptors and such.
404 */
Damien Miller4af51302000-04-16 11:18:38 +1000405void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000406do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000407{
408 int pid;
409
410#ifdef USE_PIPES
411 int pin[2], pout[2], perr[2];
412 /* Allocate pipes for communicating with the program. */
413 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
414 packet_disconnect("Could not create pipes: %.100s",
415 strerror(errno));
416#else /* USE_PIPES */
417 int inout[2], err[2];
418 /* Uses socket pairs to communicate with the program. */
419 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
420 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
421 packet_disconnect("Could not create socket pairs: %.100s",
422 strerror(errno));
423#endif /* USE_PIPES */
424 if (s == NULL)
425 fatal("do_exec_no_pty: no session");
426
Damien Millere247cc42000-05-07 12:03:14 +1000427 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000428
Damien Millerc5946332001-02-28 11:46:11 +1100429#if defined(USE_PAM)
Damien Miller665af9c2001-06-27 09:34:15 +1000430 do_pam_session(s->pw->pw_name, NULL);
Damien Millerf9e93002001-03-27 16:12:24 +1000431 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000432#endif /* USE_PAM */
433
Damien Millerb38eff82000-04-01 11:09:21 +1000434 /* Fork the child. */
435 if ((pid = fork()) == 0) {
436 /* Child. Reinitialize the log since the pid has changed. */
437 log_init(__progname, options.log_level, options.log_facility, log_stderr);
438
439 /*
440 * Create a new session and process group since the 4.4BSD
441 * setlogin() affects the entire process group.
442 */
443 if (setsid() < 0)
444 error("setsid failed: %.100s", strerror(errno));
445
446#ifdef USE_PIPES
447 /*
448 * Redirect stdin. We close the parent side of the socket
449 * pair, and make the child side the standard input.
450 */
451 close(pin[1]);
452 if (dup2(pin[0], 0) < 0)
453 perror("dup2 stdin");
454 close(pin[0]);
455
456 /* Redirect stdout. */
457 close(pout[0]);
458 if (dup2(pout[1], 1) < 0)
459 perror("dup2 stdout");
460 close(pout[1]);
461
462 /* Redirect stderr. */
463 close(perr[0]);
464 if (dup2(perr[1], 2) < 0)
465 perror("dup2 stderr");
466 close(perr[1]);
467#else /* USE_PIPES */
468 /*
469 * Redirect stdin, stdout, and stderr. Stdin and stdout will
470 * use the same socket, as some programs (particularly rdist)
471 * seem to depend on it.
472 */
473 close(inout[1]);
474 close(err[1]);
475 if (dup2(inout[0], 0) < 0) /* stdin */
476 perror("dup2 stdin");
477 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
478 perror("dup2 stdout");
479 if (dup2(err[0], 2) < 0) /* stderr */
480 perror("dup2 stderr");
481#endif /* USE_PIPES */
482
483 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000484 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000485 /* NOTREACHED */
486 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100487#ifdef HAVE_CYGWIN
488 if (is_winnt)
489 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
490#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000491 if (pid < 0)
492 packet_disconnect("fork failed: %.100s", strerror(errno));
493 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000494 /* Set interactive/non-interactive mode. */
495 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000496#ifdef USE_PIPES
497 /* We are the parent. Close the child sides of the pipes. */
498 close(pin[0]);
499 close(pout[1]);
500 close(perr[1]);
501
Damien Millerefb4afe2000-04-12 18:45:05 +1000502 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000503 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000504 } else {
505 /* Enter the interactive session. */
506 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000507 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000508 }
Damien Millerb38eff82000-04-01 11:09:21 +1000509#else /* USE_PIPES */
510 /* We are the parent. Close the child sides of the socket pairs. */
511 close(inout[0]);
512 close(err[0]);
513
514 /*
515 * Enter the interactive session. Note: server_loop must be able to
516 * handle the case that fdin and fdout are the same.
517 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000518 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000519 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000520 } else {
521 server_loop(pid, inout[1], inout[1], err[1]);
522 /* server_loop has closed inout[1] and err[1]. */
523 }
Damien Millerb38eff82000-04-01 11:09:21 +1000524#endif /* USE_PIPES */
525}
526
527/*
528 * This is called to fork and execute a command when we have a tty. This
529 * will call do_child from the child, and server_loop from the parent after
530 * setting up file descriptors, controlling tty, updating wtmp, utmp,
531 * lastlog, and other such operations.
532 */
Damien Miller4af51302000-04-16 11:18:38 +1000533void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000534do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000535{
Damien Millerb38eff82000-04-01 11:09:21 +1000536 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000537 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000538
539 if (s == NULL)
540 fatal("do_exec_pty: no session");
541 ptyfd = s->ptyfd;
542 ttyfd = s->ttyfd;
543
Damien Millerc5946332001-02-28 11:46:11 +1100544#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000545 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000546 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100547#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000548
Damien Millerb38eff82000-04-01 11:09:21 +1000549 /* Fork the child. */
550 if ((pid = fork()) == 0) {
Ben Lindstrombddd5512001-07-04 04:53:53 +0000551
Damien Miller942da032000-08-18 13:59:06 +1000552 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000553 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000554 /* Close the master side of the pseudo tty. */
555 close(ptyfd);
556
557 /* Make the pseudo tty our controlling tty. */
558 pty_make_controlling_tty(&ttyfd, s->tty);
559
560 /* Redirect stdin from the pseudo tty. */
561 if (dup2(ttyfd, fileno(stdin)) < 0)
562 error("dup2 stdin failed: %.100s", strerror(errno));
563
564 /* Redirect stdout to the pseudo tty. */
565 if (dup2(ttyfd, fileno(stdout)) < 0)
566 error("dup2 stdin failed: %.100s", strerror(errno));
567
568 /* Redirect stderr to the pseudo tty. */
569 if (dup2(ttyfd, fileno(stderr)) < 0)
570 error("dup2 stdin failed: %.100s", strerror(errno));
571
572 /* Close the extra descriptor for the pseudo tty. */
573 close(ttyfd);
574
Damien Miller942da032000-08-18 13:59:06 +1000575 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000576#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100577 if (!(options.use_login && command == NULL))
578 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000579# ifdef LOGIN_NEEDS_UTMPX
580 else
581 do_pre_login(s);
582# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000583#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000584
Damien Millerb38eff82000-04-01 11:09:21 +1000585 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000586 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000587 /* NOTREACHED */
588 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100589#ifdef HAVE_CYGWIN
590 if (is_winnt)
591 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
592#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000593 if (pid < 0)
594 packet_disconnect("fork failed: %.100s", strerror(errno));
595 s->pid = pid;
596
597 /* Parent. Close the slave side of the pseudo tty. */
598 close(ttyfd);
599
600 /*
601 * Create another descriptor of the pty master side for use as the
602 * standard input. We could use the original descriptor, but this
603 * simplifies code in server_loop. The descriptor is bidirectional.
604 */
605 fdout = dup(ptyfd);
606 if (fdout < 0)
607 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
608
609 /* we keep a reference to the pty master */
610 ptymaster = dup(ptyfd);
611 if (ptymaster < 0)
612 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
613 s->ptymaster = ptymaster;
614
615 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000616 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000617 if (compat20) {
618 session_set_fds(s, ptyfd, fdout, -1);
619 } else {
620 server_loop(pid, ptyfd, fdout, -1);
621 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000622 }
Damien Millerb38eff82000-04-01 11:09:21 +1000623}
624
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000625#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000626static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000627do_pre_login(Session *s)
628{
629 socklen_t fromlen;
630 struct sockaddr_storage from;
631 pid_t pid = getpid();
632
633 /*
634 * Get IP address of client. If the connection is not a socket, let
635 * the address be 0.0.0.0.
636 */
637 memset(&from, 0, sizeof(from));
638 if (packet_connection_is_on_socket()) {
639 fromlen = sizeof(from);
640 if (getpeername(packet_get_connection_in(),
641 (struct sockaddr *) & from, &fromlen) < 0) {
642 debug("getpeername: %.100s", strerror(errno));
643 fatal_cleanup();
644 }
645 }
646
647 record_utmp_only(pid, s->tty, s->pw->pw_name,
648 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
649 (struct sockaddr *)&from);
650}
651#endif
652
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000653/*
654 * This is called to fork and execute a command. If another command is
655 * to be forced, execute that instead.
656 */
657void
658do_exec(Session *s, const char *command)
659{
660 if (forced_command) {
661 original_command = command;
662 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000663 debug("Forced command '%.900s'", command);
664 }
665
666 if (s->ttyfd != -1)
667 do_exec_pty(s, command);
668 else
669 do_exec_no_pty(s, command);
670
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000671 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000672}
673
Damien Miller942da032000-08-18 13:59:06 +1000674/* administrative, login(1)-like work */
675void
Damien Miller69b69aa2000-10-28 14:19:58 +1100676do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000677{
Damien Miller942da032000-08-18 13:59:06 +1000678 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100679 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000680 socklen_t fromlen;
681 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000682 time_t last_login_time;
683 struct passwd * pw = s->pw;
684 pid_t pid = getpid();
685
686 /*
687 * Get IP address of client. If the connection is not a socket, let
688 * the address be 0.0.0.0.
689 */
690 memset(&from, 0, sizeof(from));
691 if (packet_connection_is_on_socket()) {
692 fromlen = sizeof(from);
693 if (getpeername(packet_get_connection_in(),
694 (struct sockaddr *) & from, &fromlen) < 0) {
695 debug("getpeername: %.100s", strerror(errno));
696 fatal_cleanup();
697 }
698 }
699
Damien Miller7b28dc52000-09-05 13:34:53 +1100700 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000701 if (options.print_lastlog) {
702 hostname[0] = '\0';
703 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
704 hostname, sizeof(hostname));
705 }
Damien Millere4340be2000-09-16 13:29:08 +1100706
Damien Miller942da032000-08-18 13:59:06 +1000707 /* Record that there was a login on that tty from the remote host. */
708 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000709 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
710 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000711
Kevin Steves092f2ef2000-10-14 13:36:13 +0000712#ifdef USE_PAM
713 /*
714 * If password change is needed, do it now.
715 * This needs to occur before the ~/.hushlogin check.
716 */
Damien Miller646aa602001-02-15 11:51:32 +1100717 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000718 print_pam_messages();
719 do_pam_chauthtok();
720 }
721#endif
722
Damien Millercf205e82001-04-16 18:29:15 +1000723 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000724 return;
725
726#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100727 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000728 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000729#endif /* USE_PAM */
730#ifdef WITH_AIXAUTHENTICATE
731 if (aixloginmsg && *aixloginmsg)
732 printf("%s\n", aixloginmsg);
733#endif /* WITH_AIXAUTHENTICATE */
734
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000735 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000736 time_string = ctime(&last_login_time);
737 if (strchr(time_string, '\n'))
738 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000739 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000740 printf("Last login: %s\r\n", time_string);
741 else
Damien Millere4340be2000-09-16 13:29:08 +1100742 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000743 }
Damien Millercf205e82001-04-16 18:29:15 +1000744
745 do_motd();
746}
747
748/*
749 * Display the message of the day.
750 */
751void
752do_motd(void)
753{
754 FILE *f;
755 char buf[256];
756
Damien Miller942da032000-08-18 13:59:06 +1000757 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000758#ifdef HAVE_LOGIN_CAP
759 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
760 "/etc/motd"), "r");
761#else
Damien Miller942da032000-08-18 13:59:06 +1000762 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000763#endif
Damien Miller942da032000-08-18 13:59:06 +1000764 if (f) {
765 while (fgets(buf, sizeof(buf), f))
766 fputs(buf, stdout);
767 fclose(f);
768 }
769 }
770}
771
Kevin Steves8f63caa2001-07-04 18:23:02 +0000772
773/*
774 * Check for quiet login, either .hushlogin or command given.
775 */
776int
777check_quietlogin(Session *s, const char *command)
778{
779 char buf[256];
780 struct passwd *pw = s->pw;
781 struct stat st;
782
783 /* Return 1 if .hushlogin exists or a command given. */
784 if (command != NULL)
785 return 1;
786 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
787#ifdef HAVE_LOGIN_CAP
788 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
789 return 1;
790#else
791 if (stat(buf, &st) >= 0)
792 return 1;
793#endif
794 return 0;
795}
796
Damien Millerb38eff82000-04-01 11:09:21 +1000797/*
798 * Sets the value of the given variable in the environment. If the variable
799 * already exists, its value is overriden.
800 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000801static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000802child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000803 const char *value)
804{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000805 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000806 char **env;
807
808 /*
809 * Find the slot where the value should be stored. If the variable
810 * already exists, we reuse the slot; otherwise we append a new slot
811 * at the end of the array, expanding if necessary.
812 */
813 env = *envp;
814 namelen = strlen(name);
815 for (i = 0; env[i]; i++)
816 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
817 break;
818 if (env[i]) {
819 /* Reuse the slot. */
820 xfree(env[i]);
821 } else {
822 /* New variable. Expand if necessary. */
823 if (i >= (*envsizep) - 1) {
824 (*envsizep) += 50;
825 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
826 }
827 /* Need to set the NULL pointer at end of array beyond the new slot. */
828 env[i + 1] = NULL;
829 }
830
831 /* Allocate space and format the variable in the appropriate slot. */
832 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
833 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
834}
835
836/*
837 * Reads environment variables from the given file and adds/overrides them
838 * into the environment. If the file does not exist, this does nothing.
839 * Otherwise, it must consist of empty lines, comments (line starts with '#')
840 * and assignments of the form name=value. No other forms are allowed.
841 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000842static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000843read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000844 const char *filename)
845{
846 FILE *f;
847 char buf[4096];
848 char *cp, *value;
849
850 f = fopen(filename, "r");
851 if (!f)
852 return;
853
854 while (fgets(buf, sizeof(buf), f)) {
855 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
856 ;
857 if (!*cp || *cp == '#' || *cp == '\n')
858 continue;
859 if (strchr(cp, '\n'))
860 *strchr(cp, '\n') = '\0';
861 value = strchr(cp, '=');
862 if (value == NULL) {
863 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
864 continue;
865 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000866 /*
867 * Replace the equals sign by nul, and advance value to
868 * the value string.
869 */
Damien Millerb38eff82000-04-01 11:09:21 +1000870 *value = '\0';
871 value++;
872 child_set_env(env, envsize, cp, value);
873 }
874 fclose(f);
875}
876
877#ifdef USE_PAM
878/*
879 * Sets any environment variables which have been specified by PAM
880 */
Kevin Steves9b26f962001-06-29 17:52:17 +0000881void do_pam_environment(char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000882{
883 char *equals, var_name[512], var_val[512];
884 char **pam_env;
885 int i;
886
887 if ((pam_env = fetch_pam_environment()) == NULL)
888 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000889
Damien Millerb38eff82000-04-01 11:09:21 +1000890 for(i = 0; pam_env[i] != NULL; i++) {
891 if ((equals = strstr(pam_env[i], "=")) == NULL)
892 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000893
Damien Millerb38eff82000-04-01 11:09:21 +1000894 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
895 memset(var_name, '\0', sizeof(var_name));
896 memset(var_val, '\0', sizeof(var_val));
897
898 strncpy(var_name, pam_env[i], equals - pam_env[i]);
899 strcpy(var_val, equals + 1);
900
Damien Millercb5e44a2000-09-29 12:12:36 +1100901 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000902
903 child_set_env(env, envsize, var_name, var_val);
904 }
905 }
906}
907#endif /* USE_PAM */
908
Damien Millercb5e44a2000-09-29 12:12:36 +1100909#ifdef HAVE_CYGWIN
Kevin Steves9b26f962001-06-29 17:52:17 +0000910void copy_environment(char ***env, u_int *envsize)
Damien Millercb5e44a2000-09-29 12:12:36 +1100911{
912 char *equals, var_name[512], var_val[512];
913 int i;
914
915 for(i = 0; environ[i] != NULL; i++) {
916 if ((equals = strstr(environ[i], "=")) == NULL)
917 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000918
Damien Millercb5e44a2000-09-29 12:12:36 +1100919 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
920 memset(var_name, '\0', sizeof(var_name));
921 memset(var_val, '\0', sizeof(var_val));
922
923 strncpy(var_name, environ[i], equals - environ[i]);
924 strcpy(var_val, equals + 1);
925
926 debug3("Copy environment: %s=%s", var_name, var_val);
927
928 child_set_env(env, envsize, var_name, var_val);
929 }
930 }
931}
932#endif
933
Damien Miller5fc85652000-07-09 23:53:07 +1000934#if defined(HAVE_GETUSERATTR)
935/*
936 * AIX-specific login initialisation
937 */
938void set_limit(char *user, char *soft, char *hard, int resource, int mult)
939{
940 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000941 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000942
943 getrlimit(resource, &rlim);
944
Damien Miller65964d62000-07-11 09:16:22 +1000945 slim = 0;
946 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
947 if (slim < 0) {
948 rlim.rlim_cur = RLIM_INFINITY;
949 } else if (slim != 0) {
950 /* See the wackiness below */
951 if (rlim.rlim_cur == slim * mult)
952 slim = 0;
953 else
954 rlim.rlim_cur = slim * mult;
955 }
956 }
Damien Miller5fc85652000-07-09 23:53:07 +1000957
Damien Miller65964d62000-07-11 09:16:22 +1000958 hlim = 0;
959 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
960 if (hlim < 0) {
961 rlim.rlim_max = RLIM_INFINITY;
962 } else if (hlim != 0) {
963 rlim.rlim_max = hlim * mult;
964 }
965 }
Damien Miller5fc85652000-07-09 23:53:07 +1000966
Damien Miller65964d62000-07-11 09:16:22 +1000967 /*
968 * XXX For cpu and fsize the soft limit is set to the hard limit
969 * if the hard limit is left at its default value and the soft limit
970 * is changed from its default value, either by requesting it
971 * (slim == 0) or by setting it to the current default. At least
972 * that's how rlogind does it. If you're confused you're not alone.
973 * Bug or feature? AIX 4.3.1.2
974 */
975 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
976 && hlim == 0 && slim != 0)
977 rlim.rlim_max = rlim.rlim_cur;
978 /* A specified hard limit limits the soft limit */
979 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
980 rlim.rlim_cur = rlim.rlim_max;
981 /* A soft limit can increase a hard limit */
982 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000983 rlim.rlim_max = rlim.rlim_cur;
984
985 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000986 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000987}
988
989void set_limits_from_userattr(char *user)
990{
991 int mask;
992 char buf[16];
993
994 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
995 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
996 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
997 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
998 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
999 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1000#if defined(S_UNOFILE)
1001 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1002#endif
1003
1004 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1005 /* Convert decimal to octal */
1006 (void) snprintf(buf, sizeof(buf), "%d", mask);
1007 if (sscanf(buf, "%o", &mask) == 1)
1008 umask(mask);
1009 }
1010}
1011#endif /* defined(HAVE_GETUSERATTR) */
1012
Damien Millerb38eff82000-04-01 11:09:21 +10001013/*
1014 * Performs common processing for the child, such as setting up the
1015 * environment, closing extra file descriptors, setting the user and group
1016 * ids, and executing the command or shell.
1017 */
Damien Miller4af51302000-04-16 11:18:38 +10001018void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001019do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001020{
Damien Miller7b28dc52000-09-05 13:34:53 +11001021 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstromec95ed92001-07-04 04:21:14 +00001022 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001023 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001024 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001025 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001026 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001027 char **env;
1028 extern char **environ;
1029 struct stat st;
1030 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001031 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +10001032#ifdef WITH_IRIX_PROJECT
1033 prid_t projid;
1034#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001035#ifdef WITH_IRIX_JOBS
1036 jid_t jid = 0;
1037#else
1038#ifdef WITH_IRIX_ARRAY
1039 int jid = 0;
1040#endif /* WITH_IRIX_ARRAY */
1041#endif /* WITH_IRIX_JOBS */
1042
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001043 do_xauth =
1044 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1045
Ben Lindstrom005dd222001-04-18 15:29:33 +00001046 /* remove hostkey from the child's memory */
1047 destroy_sensitive_data();
1048
Damien Millerd3a18572000-06-07 19:55:44 +10001049 /* login(1) is only called if we execute the login shell */
1050 if (options.use_login && command != NULL)
1051 options.use_login = 0;
1052
Damien Millerc62f1fc2001-07-14 11:54:05 +10001053#if !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001054 if (!options.use_login) {
1055# ifdef HAVE_LOGIN_CAP
1056 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1057 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1058 _PATH_NOLOGIN), "r");
1059# else /* HAVE_LOGIN_CAP */
1060 if (pw->pw_uid)
1061 f = fopen(_PATH_NOLOGIN, "r");
1062# endif /* HAVE_LOGIN_CAP */
1063 if (f) {
1064 /* /etc/nologin exists. Print its contents and exit. */
1065 while (fgets(buf, sizeof(buf), f))
1066 fputs(buf, stderr);
1067 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001068 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001069 }
Damien Millerb38eff82000-04-01 11:09:21 +10001070 }
Damien Millerc62f1fc2001-07-14 11:54:05 +10001071#endif /* HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001072
Damien Millerad833b32000-08-23 10:46:23 +10001073 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001074 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1075 switch, so we let login(1) to this for us. */
1076 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001077#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001078 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001079 if (!check_quietlogin(s, command))
1080 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001081#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001082#ifdef HAVE_CYGWIN
1083 if (is_winnt) {
1084#else
Damien Millerb38eff82000-04-01 11:09:21 +10001085 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001086#endif
Damien Millerad833b32000-08-23 10:46:23 +10001087# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001088 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001089# endif /* HAVE_GETUSERATTR */
1090# ifdef HAVE_LOGIN_CAP
1091 if (setusercontext(lc, pw, pw->pw_uid,
1092 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1093 perror("unable to set user context");
1094 exit(1);
1095 }
1096# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001097#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1098 /* Sets login uid for accounting */
1099 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1100 error("setluid: %s", strerror(errno));
1101#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1102
Damien Millerad833b32000-08-23 10:46:23 +10001103 if (setlogin(pw->pw_name) < 0)
1104 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001105 if (setgid(pw->pw_gid) < 0) {
1106 perror("setgid");
1107 exit(1);
1108 }
1109 /* Initialize the group list. */
1110 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1111 perror("initgroups");
1112 exit(1);
1113 }
1114 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001115# ifdef USE_PAM
1116 /*
1117 * PAM credentials may take the form of
1118 * supplementary groups. These will have been
1119 * wiped by the above initgroups() call.
1120 * Reestablish them here.
1121 */
1122 do_pam_setcred(0);
1123# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001124# ifdef WITH_IRIX_JOBS
1125 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1126 if (jid == -1) {
1127 fatal("Failed to create job container: %.100s",
1128 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001129 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001130# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001131# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001132 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001133 if (jid == 0) {
1134 if (newarraysess() != 0)
1135 fatal("Failed to set up new array session: %.100s",
1136 strerror(errno));
1137 }
Damien Millerad833b32000-08-23 10:46:23 +10001138# endif /* WITH_IRIX_ARRAY */
1139# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001140 /* initialize irix project info */
1141 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1142 debug("Failed to get project id, using projid 0");
1143 projid = 0;
1144 }
Damien Miller91606b12000-06-28 08:22:29 +10001145 if (setprid(projid))
1146 fatal("Failed to initialize project %d for %s: %.100s",
1147 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001148# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001149#ifdef WITH_IRIX_AUDIT
1150 if (sysconf(_SC_AUDIT)) {
1151 debug("Setting sat id to %d", (int) pw->pw_uid);
1152 if (satsetid(pw->pw_uid))
1153 debug("error setting satid: %.100s", strerror(errno));
1154 }
1155#endif /* WITH_IRIX_AUDIT */
1156
Damien Miller168a7002001-03-17 10:29:50 +11001157#ifdef _AIX
1158 /*
1159 * AIX has a "usrinfo" area where logname and
1160 * other stuff is stored - a few applications
1161 * actually use this and die if it's not set
1162 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001163 if (s->ttyfd == -1)
1164 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001165 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001166 2 * strlen(pw->pw_name));
1167 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001168 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001169 if (usrinfo(SETUINFO, cp, i) == -1)
1170 fatal("Couldn't set usrinfo: %s",
1171 strerror(errno));
1172 debug3("AIX/UsrInfo: set len %d", i);
1173 xfree(cp);
1174#endif
1175
Damien Millerb38eff82000-04-01 11:09:21 +10001176 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001177 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001178# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001179 }
Damien Millerad833b32000-08-23 10:46:23 +10001180#endif /* HAVE_OSF_SIA */
1181
Damien Millerbac2d8a2000-09-05 16:13:06 +11001182#ifdef HAVE_CYGWIN
1183 if (is_winnt)
1184#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001185 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001186 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001187 }
1188 /*
1189 * Get the shell from the password data. An empty shell field is
1190 * legal, and means /bin/sh.
1191 */
1192 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001193#ifdef HAVE_LOGIN_CAP
1194 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1195#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001196
1197#ifdef AFS
1198 /* Try to get AFS tokens for the local cell. */
1199 if (k_hasafs()) {
1200 char cell[64];
Ben Lindstromec95ed92001-07-04 04:21:14 +00001201
Damien Millerb38eff82000-04-01 11:09:21 +10001202 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1203 krb_afslog(cell, 0);
Ben Lindstromec95ed92001-07-04 04:21:14 +00001204
Damien Millerb38eff82000-04-01 11:09:21 +10001205 krb_afslog(0, 0);
1206 }
1207#endif /* AFS */
1208
1209 /* Initialize the environment. */
1210 envsize = 100;
1211 env = xmalloc(envsize * sizeof(char *));
1212 env[0] = NULL;
1213
Damien Millerbac2d8a2000-09-05 16:13:06 +11001214#ifdef HAVE_CYGWIN
1215 /*
1216 * The Windows environment contains some setting which are
1217 * important for a running system. They must not be dropped.
1218 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001219 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001220#endif
1221
Damien Millerb38eff82000-04-01 11:09:21 +10001222 if (!options.use_login) {
1223 /* Set basic environment. */
1224 child_set_env(&env, &envsize, "USER", pw->pw_name);
1225 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1226 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001227#ifdef HAVE_LOGIN_CAP
1228 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1229 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001230#else /* HAVE_LOGIN_CAP */
1231# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001232 /*
1233 * There's no standard path on Windows. The path contains
1234 * important components pointing to the system directories,
1235 * needed for loading shared libraries. So the path better
1236 * remains intact here.
1237 */
Damien Millerb38eff82000-04-01 11:09:21 +10001238 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001239# endif /* HAVE_CYGWIN */
1240#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001241
1242 snprintf(buf, sizeof buf, "%.200s/%.50s",
1243 _PATH_MAILDIR, pw->pw_name);
1244 child_set_env(&env, &envsize, "MAIL", buf);
1245
1246 /* Normal systems set SHELL by default. */
1247 child_set_env(&env, &envsize, "SHELL", shell);
1248 }
1249 if (getenv("TZ"))
1250 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1251
1252 /* Set custom environment options from RSA authentication. */
1253 while (custom_environment) {
1254 struct envstring *ce = custom_environment;
1255 char *s = ce->s;
1256 int i;
1257 for (i = 0; s[i] != '=' && s[i]; i++);
1258 if (s[i] == '=') {
1259 s[i] = 0;
1260 child_set_env(&env, &envsize, s, s + i + 1);
1261 }
1262 custom_environment = ce->next;
1263 xfree(ce->s);
1264 xfree(ce);
1265 }
1266
1267 snprintf(buf, sizeof buf, "%.50s %d %d",
1268 get_remote_ipaddr(), get_remote_port(), get_local_port());
1269 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1270
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001271 if (s->ttyfd != -1)
1272 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1273 if (s->term)
1274 child_set_env(&env, &envsize, "TERM", s->term);
1275 if (s->display)
1276 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001277 if (original_command)
1278 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1279 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001280
1281#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001282 if ((cp = getenv("AUTHSTATE")) != NULL)
1283 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1284 if ((cp = getenv("KRB5CCNAME")) != NULL)
1285 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1286 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001287#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001288#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +00001289 if (s->authctxt->krb4_ticket_file)
1290 child_set_env(&env, &envsize, "KRBTKFILE",
1291 s->authctxt->krb4_ticket_file);
1292#endif
1293#ifdef KRB5
1294 if (s->authctxt->krb5_ticket_file)
1295 child_set_env(&env, &envsize, "KRB5CCNAME",
1296 s->authctxt->krb5_ticket_file);
1297#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001298#ifdef USE_PAM
1299 /* Pull in any environment variables that may have been set by PAM. */
1300 do_pam_environment(&env, &envsize);
1301#endif /* USE_PAM */
1302
Damien Millerb38eff82000-04-01 11:09:21 +10001303 if (auth_get_socket_name() != NULL)
1304 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1305 auth_get_socket_name());
1306
1307 /* read $HOME/.ssh/environment. */
1308 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001309 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1310 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001311 read_environment_file(&env, &envsize, buf);
1312 }
1313 if (debug_flag) {
1314 /* dump the environment */
1315 fprintf(stderr, "Environment:\n");
1316 for (i = 0; env[i]; i++)
1317 fprintf(stderr, " %.200s\n", env[i]);
1318 }
Damien Millerad833b32000-08-23 10:46:23 +10001319 /* we have to stash the hostname before we close our socket. */
1320 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001321 hostname = get_remote_name_or_ip(utmp_len,
1322 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001323 /*
1324 * Close the connection descriptors; note that this is the child, and
1325 * the server will still have the socket open, and it is important
1326 * that we do not shutdown it. Note that the descriptors cannot be
1327 * closed before building the environment, as we call
1328 * get_remote_ipaddr there.
1329 */
1330 if (packet_get_connection_in() == packet_get_connection_out())
1331 close(packet_get_connection_in());
1332 else {
1333 close(packet_get_connection_in());
1334 close(packet_get_connection_out());
1335 }
1336 /*
1337 * Close all descriptors related to channels. They will still remain
1338 * open in the parent.
1339 */
1340 /* XXX better use close-on-exec? -markus */
1341 channel_close_all();
1342
1343 /*
1344 * Close any extra file descriptors. Note that there may still be
1345 * descriptors left by system functions. They will be closed later.
1346 */
1347 endpwent();
1348
1349 /*
1350 * Close any extra open file descriptors so that we don\'t have them
1351 * hanging around in clients. Note that we want to do this after
1352 * initgroups, because at least on Solaris 2.3 it leaves file
1353 * descriptors open.
1354 */
1355 for (i = 3; i < 64; i++)
1356 close(i);
1357
1358 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001359 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001360 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1361 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001362#ifdef HAVE_LOGIN_CAP
1363 if (login_getcapbool(lc, "requirehome", 0))
1364 exit(1);
1365#endif
1366 }
Damien Millerb38eff82000-04-01 11:09:21 +10001367
1368 /*
1369 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1370 * xauth are run in the proper environment.
1371 */
1372 environ = env;
1373
1374 /*
1375 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1376 * in this order).
1377 */
1378 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001379 /* ignore _PATH_SSH_USER_RC for subsystems */
1380 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001381 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1382 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001383 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001384 fprintf(stderr, "Running %s\n", cmd);
1385 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001386 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001387 if (do_xauth)
1388 fprintf(f, "%s %s\n", s->auth_proto,
1389 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001390 pclose(f);
1391 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001392 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001393 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001394 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001395 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001396 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1397 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001398 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001399 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001400 if (do_xauth)
1401 fprintf(f, "%s %s\n", s->auth_proto,
1402 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001403 pclose(f);
1404 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001405 fprintf(stderr, "Could not run %s\n",
1406 _PATH_SSH_SYSTEM_RC);
1407 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001408 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001409 char *screen = strchr(s->display, ':');
1410
1411 if (debug_flag) {
1412 fprintf(stderr,
1413 "Running %.100s add "
1414 "%.100s %.100s %.100s\n",
1415 options.xauth_location, s->display,
1416 s->auth_proto, s->auth_data);
1417 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001418 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001419 "Adding %.*s/unix%s %s %s\n",
1420 (int)(screen - s->display),
1421 s->display, screen,
1422 s->auth_proto, s->auth_data);
1423 }
1424 snprintf(cmd, sizeof cmd, "%s -q -",
1425 options.xauth_location);
1426 f = popen(cmd, "w");
1427 if (f) {
1428 fprintf(f, "add %s %s %s\n", s->display,
1429 s->auth_proto, s->auth_data);
1430 if (screen != NULL)
1431 fprintf(f, "add %.*s/unix%s %s %s\n",
1432 (int)(screen - s->display),
1433 s->display, screen,
1434 s->auth_proto,
1435 s->auth_data);
1436 pclose(f);
1437 } else {
1438 fprintf(stderr, "Could not run %s\n",
1439 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001440 }
1441 }
Damien Millerb38eff82000-04-01 11:09:21 +10001442 /* Get the last component of the shell name. */
1443 cp = strrchr(shell, '/');
1444 if (cp)
1445 cp++;
1446 else
1447 cp = shell;
1448 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001449
1450 /* restore SIGPIPE for child */
1451 signal(SIGPIPE, SIG_DFL);
1452
Damien Millerb38eff82000-04-01 11:09:21 +10001453 /*
1454 * If we have no command, execute the shell. In this case, the shell
1455 * name to be passed in argv[0] is preceded by '-' to indicate that
1456 * this is a login shell.
1457 */
1458 if (!command) {
1459 if (!options.use_login) {
1460 char buf[256];
1461
Damien Millerb38eff82000-04-01 11:09:21 +10001462 /* Start the shell. Set initial character to '-'. */
1463 buf[0] = '-';
1464 strncpy(buf + 1, cp, sizeof(buf) - 1);
1465 buf[sizeof(buf) - 1] = 0;
1466
1467 /* Execute the shell. */
1468 argv[0] = buf;
1469 argv[1] = NULL;
1470 execve(shell, argv, env);
1471
1472 /* Executing the shell failed. */
1473 perror(shell);
1474 exit(1);
1475
1476 } else {
1477 /* Launch login(1). */
1478
Damien Millerad833b32000-08-23 10:46:23 +10001479 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001480#ifdef LOGIN_NEEDS_TERM
1481 s->term? s->term : "unknown",
1482#endif
Damien Millerefb1edf2001-07-14 12:19:36 +10001483 "-p", "-f", "--", pw->pw_name, (char *)NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001484
1485 /* Login couldn't be executed, die. */
1486
1487 perror("login");
1488 exit(1);
1489 }
1490 }
1491 /*
1492 * Execute the command using the user's shell. This uses the -c
1493 * option to execute the command.
1494 */
1495 argv[0] = (char *) cp;
1496 argv[1] = "-c";
1497 argv[2] = (char *) command;
1498 argv[3] = NULL;
1499 execve(shell, argv, env);
1500 perror(shell);
1501 exit(1);
1502}
1503
1504Session *
1505session_new(void)
1506{
1507 int i;
1508 static int did_init = 0;
1509 if (!did_init) {
1510 debug("session_new: init");
1511 for(i = 0; i < MAX_SESSIONS; i++) {
1512 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001513 }
1514 did_init = 1;
1515 }
1516 for(i = 0; i < MAX_SESSIONS; i++) {
1517 Session *s = &sessions[i];
1518 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001519 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001520 s->chanid = -1;
1521 s->ptyfd = -1;
1522 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001523 s->used = 1;
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001524 s->self = i;
Damien Miller15b29522000-10-14 12:33:48 +11001525 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001526 return s;
1527 }
1528 }
1529 return NULL;
1530}
1531
Ben Lindstrombba81212001-06-25 05:01:22 +00001532static void
Damien Millerb38eff82000-04-01 11:09:21 +10001533session_dump(void)
1534{
1535 int i;
1536 for(i = 0; i < MAX_SESSIONS; i++) {
1537 Session *s = &sessions[i];
1538 debug("dump: used %d session %d %p channel %d pid %d",
1539 s->used,
1540 s->self,
1541 s,
1542 s->chanid,
1543 s->pid);
1544 }
1545}
1546
Damien Millerefb4afe2000-04-12 18:45:05 +10001547int
Ben Lindstrombddd5512001-07-04 04:53:53 +00001548session_open(Authctxt *authctxt, int chanid)
Damien Millerefb4afe2000-04-12 18:45:05 +10001549{
1550 Session *s = session_new();
1551 debug("session_open: channel %d", chanid);
1552 if (s == NULL) {
1553 error("no more sessions");
1554 return 0;
1555 }
Ben Lindstrombddd5512001-07-04 04:53:53 +00001556 s->authctxt = authctxt;
1557 s->pw = authctxt->pw;
Damien Millerefb4afe2000-04-12 18:45:05 +10001558 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001559 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001560 debug("session_open: session %d: link with channel %d", s->self, chanid);
1561 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001562 return 1;
1563}
1564
Ben Lindstrombba81212001-06-25 05:01:22 +00001565static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001566session_by_channel(int id)
1567{
1568 int i;
1569 for(i = 0; i < MAX_SESSIONS; i++) {
1570 Session *s = &sessions[i];
1571 if (s->used && s->chanid == id) {
1572 debug("session_by_channel: session %d channel %d", i, id);
1573 return s;
1574 }
1575 }
1576 debug("session_by_channel: unknown channel %d", id);
1577 session_dump();
1578 return NULL;
1579}
1580
Ben Lindstrombba81212001-06-25 05:01:22 +00001581static Session *
Damien Millerefb4afe2000-04-12 18:45:05 +10001582session_by_pid(pid_t pid)
1583{
1584 int i;
1585 debug("session_by_pid: pid %d", pid);
1586 for(i = 0; i < MAX_SESSIONS; i++) {
1587 Session *s = &sessions[i];
1588 if (s->used && s->pid == pid)
1589 return s;
1590 }
1591 error("session_by_pid: unknown pid %d", pid);
1592 session_dump();
1593 return NULL;
1594}
1595
Ben Lindstrombba81212001-06-25 05:01:22 +00001596static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001597session_window_change_req(Session *s)
1598{
1599 s->col = packet_get_int();
1600 s->row = packet_get_int();
1601 s->xpixel = packet_get_int();
1602 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001603 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001604 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1605 return 1;
1606}
1607
Ben Lindstrombba81212001-06-25 05:01:22 +00001608static int
Damien Millerefb4afe2000-04-12 18:45:05 +10001609session_pty_req(Session *s)
1610{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001611 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001612 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001613
Ben Lindstrom49c12602001-06-13 04:37:36 +00001614 if (no_pty_flag) {
1615 debug("Allocating a pty not permitted for this authentication.");
Damien Millerf6d9e222000-06-18 14:50:44 +10001616 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001617 }
1618 if (s->ttyfd != -1) {
1619 packet_disconnect("Protocol error: you already have a pty.");
Damien Miller4af51302000-04-16 11:18:38 +10001620 return 0;
Ben Lindstrom49c12602001-06-13 04:37:36 +00001621 }
1622
Damien Millerefb4afe2000-04-12 18:45:05 +10001623 s->term = packet_get_string(&len);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001624
1625 if (compat20) {
1626 s->col = packet_get_int();
1627 s->row = packet_get_int();
1628 } else {
1629 s->row = packet_get_int();
1630 s->col = packet_get_int();
1631 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001632 s->xpixel = packet_get_int();
1633 s->ypixel = packet_get_int();
1634
1635 if (strcmp(s->term, "") == 0) {
1636 xfree(s->term);
1637 s->term = NULL;
1638 }
Ben Lindstrom49c12602001-06-13 04:37:36 +00001639
Damien Millerefb4afe2000-04-12 18:45:05 +10001640 /* Allocate a pty and open it. */
Ben Lindstrom49c12602001-06-13 04:37:36 +00001641 debug("Allocating pty.");
Damien Millerefb4afe2000-04-12 18:45:05 +10001642 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
Ben Lindstrom49c12602001-06-13 04:37:36 +00001643 if (s->term)
1644 xfree(s->term);
Damien Millerefb4afe2000-04-12 18:45:05 +10001645 s->term = NULL;
1646 s->ptyfd = -1;
1647 s->ttyfd = -1;
1648 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001649 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001650 }
1651 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001652
1653 /* for SSH1 the tty modes length is not given */
1654 if (!compat20)
1655 n_bytes = packet_remaining();
1656 tty_parse_modes(s->ttyfd, &n_bytes);
1657
Damien Millerefb4afe2000-04-12 18:45:05 +10001658 /*
1659 * Add a cleanup function to clear the utmp entry and record logout
1660 * time in case we call fatal() (e.g., the connection gets closed).
1661 */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001662 fatal_add_cleanup(session_pty_cleanup, (void *)s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001663 pty_setowner(s->pw, s->tty);
Ben Lindstrom49c12602001-06-13 04:37:36 +00001664
1665 /* Set window size from the packet. */
Damien Millerefb4afe2000-04-12 18:45:05 +10001666 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1667
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001668 packet_done();
Damien Millere247cc42000-05-07 12:03:14 +10001669 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001670 return 1;
1671}
1672
Ben Lindstrombba81212001-06-25 05:01:22 +00001673static int
Damien Millerbd483e72000-04-30 10:00:53 +10001674session_subsystem_req(Session *s)
1675{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001676 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001677 int success = 0;
1678 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001679 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001680
1681 packet_done();
1682 log("subsystem request for %s", subsys);
1683
Damien Millerf6d9e222000-06-18 14:50:44 +10001684 for (i = 0; i < options.num_subsystems; i++) {
1685 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1686 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001687 s->is_subsystem = 1;
Ben Lindstromb09f6b52001-09-14 23:12:07 +00001688 do_exec(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001689 success = 1;
1690 }
1691 }
1692
1693 if (!success)
1694 log("subsystem request for %s failed, subsystem not found", subsys);
1695
Damien Millerbd483e72000-04-30 10:00:53 +10001696 xfree(subsys);
1697 return success;
1698}
1699
Ben Lindstrombba81212001-06-25 05:01:22 +00001700static int
Damien Millerbd483e72000-04-30 10:00:53 +10001701session_x11_req(Session *s)
1702{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001703 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001704
1705 s->single_connection = packet_get_char();
1706 s->auth_proto = packet_get_string(NULL);
1707 s->auth_data = packet_get_string(NULL);
1708 s->screen = packet_get_int();
1709 packet_done();
1710
Ben Lindstrom768176b2001-06-09 01:29:12 +00001711 success = session_setup_x11fwd(s);
1712 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001713 xfree(s->auth_proto);
1714 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001715 s->auth_proto = NULL;
1716 s->auth_data = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +10001717 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00001718 return success;
Damien Millerbd483e72000-04-30 10:00:53 +10001719}
1720
Ben Lindstrombba81212001-06-25 05:01:22 +00001721static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001722session_shell_req(Session *s)
1723{
Damien Millerf6d9e222000-06-18 14:50:44 +10001724 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001725 do_exec(s, NULL);
Damien Millerf6d9e222000-06-18 14:50:44 +10001726 return 1;
1727}
1728
Ben Lindstrombba81212001-06-25 05:01:22 +00001729static int
Damien Millerf6d9e222000-06-18 14:50:44 +10001730session_exec_req(Session *s)
1731{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001732 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001733 char *command = packet_get_string(&len);
1734 packet_done();
Ben Lindstromc85ab8a2001-06-21 03:13:10 +00001735 do_exec(s, command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +00001736 xfree(command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001737 return 1;
1738}
1739
Ben Lindstrombba81212001-06-25 05:01:22 +00001740static int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001741session_auth_agent_req(Session *s)
1742{
1743 static int called = 0;
1744 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001745 if (no_agent_forwarding_flag) {
1746 debug("session_auth_agent_req: no_agent_forwarding_flag");
1747 return 0;
1748 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001749 if (called) {
1750 return 0;
1751 } else {
1752 called = 1;
1753 return auth_input_request_forwarding(s->pw);
1754 }
1755}
1756
Damien Millerefb4afe2000-04-12 18:45:05 +10001757void
1758session_input_channel_req(int id, void *arg)
1759{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001760 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001761 int reply;
1762 int success = 0;
1763 char *rtype;
1764 Session *s;
1765 Channel *c;
1766
1767 rtype = packet_get_string(&len);
1768 reply = packet_get_char();
1769
1770 s = session_by_channel(id);
1771 if (s == NULL)
1772 fatal("session_input_channel_req: channel %d: no session", id);
1773 c = channel_lookup(id);
1774 if (c == NULL)
1775 fatal("session_input_channel_req: channel %d: bad channel", id);
1776
1777 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1778 s->self, id, rtype, reply);
1779
1780 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001781 * a session is in LARVAL state until a shell, a command
1782 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001783 */
1784 if (c->type == SSH_CHANNEL_LARVAL) {
1785 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001786 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001787 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001788 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001789 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001790 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001791 } else if (strcmp(rtype, "x11-req") == 0) {
1792 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001793 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1794 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001795 } else if (strcmp(rtype, "subsystem") == 0) {
1796 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001797 }
1798 }
1799 if (strcmp(rtype, "window-change") == 0) {
1800 success = session_window_change_req(s);
1801 }
1802
1803 if (reply) {
1804 packet_start(success ?
1805 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1806 packet_put_int(c->remote_id);
1807 packet_send();
1808 }
1809 xfree(rtype);
1810}
1811
1812void
1813session_set_fds(Session *s, int fdin, int fdout, int fderr)
1814{
1815 if (!compat20)
1816 fatal("session_set_fds: called for proto != 2.0");
1817 /*
1818 * now that have a child and a pipe to the child,
1819 * we can activate our channel and register the fd's
1820 */
1821 if (s->chanid == -1)
1822 fatal("no channel for session %d", s->self);
1823 channel_set_fds(s->chanid,
1824 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001825 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1826 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001827}
1828
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001829/*
1830 * Function to perform pty cleanup. Also called if we get aborted abnormally
1831 * (e.g., due to a dropped connection).
1832 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001833static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001834session_pty_cleanup(void *session)
Damien Millerb38eff82000-04-01 11:09:21 +10001835{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001836 Session *s = session;
1837
1838 if (s == NULL) {
1839 error("session_pty_cleanup: no session");
1840 return;
1841 }
1842 if (s->ttyfd == -1)
Damien Millerb38eff82000-04-01 11:09:21 +10001843 return;
1844
Kevin Steves43cdef32001-02-11 14:12:08 +00001845 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001846
Damien Millerb38eff82000-04-01 11:09:21 +10001847 /* Record that the user has logged out. */
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001848 if (s->pid != 0)
1849 record_logout(s->pid, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001850
1851 /* Release the pseudo-tty. */
1852 pty_release(s->tty);
1853
1854 /*
1855 * Close the server side of the socket pairs. We must do this after
1856 * the pty cleanup, so that another process doesn't get this pty
1857 * while we're still cleaning up.
1858 */
1859 if (close(s->ptymaster) < 0)
1860 error("close(s->ptymaster): %s", strerror(errno));
1861}
Damien Millerefb4afe2000-04-12 18:45:05 +10001862
Ben Lindstrombba81212001-06-25 05:01:22 +00001863static void
Damien Millerefb4afe2000-04-12 18:45:05 +10001864session_exit_message(Session *s, int status)
1865{
1866 Channel *c;
1867 if (s == NULL)
1868 fatal("session_close: no session");
1869 c = channel_lookup(s->chanid);
1870 if (c == NULL)
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001871 fatal("session_exit_message: session %d: no channel %d",
Damien Millerefb4afe2000-04-12 18:45:05 +10001872 s->self, s->chanid);
1873 debug("session_exit_message: session %d channel %d pid %d",
1874 s->self, s->chanid, s->pid);
1875
1876 if (WIFEXITED(status)) {
1877 channel_request_start(s->chanid,
1878 "exit-status", 0);
1879 packet_put_int(WEXITSTATUS(status));
1880 packet_send();
1881 } else if (WIFSIGNALED(status)) {
1882 channel_request_start(s->chanid,
1883 "exit-signal", 0);
1884 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001885#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001886 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001887#else /* WCOREDUMP */
1888 packet_put_char(0);
1889#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001890 packet_put_cstring("");
1891 packet_put_cstring("");
1892 packet_send();
1893 } else {
1894 /* Some weird exit cause. Just exit. */
1895 packet_disconnect("wait returned status %04x.", status);
1896 }
1897
1898 /* disconnect channel */
1899 debug("session_exit_message: release channel %d", s->chanid);
1900 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001901 /*
1902 * emulate a write failure with 'chan_write_failed', nobody will be
1903 * interested in data we write.
1904 * Note that we must not call 'chan_read_failed', since there could
1905 * be some more data waiting in the pipe.
1906 */
Damien Millerbd483e72000-04-30 10:00:53 +10001907 if (c->ostate != CHAN_OUTPUT_CLOSED)
1908 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001909 s->chanid = -1;
1910}
1911
Ben Lindstrombba81212001-06-25 05:01:22 +00001912static void
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001913session_close(Session *s)
Damien Millerefb4afe2000-04-12 18:45:05 +10001914{
Ben Lindstrom7eaf8e42001-06-13 04:35:43 +00001915 debug("session_close: session %d pid %d", s->self, s->pid);
1916 if (s->ttyfd != -1) {
1917 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1918 session_pty_cleanup(s);
1919 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001920 if (s->term)
1921 xfree(s->term);
1922 if (s->display)
1923 xfree(s->display);
1924 if (s->auth_data)
1925 xfree(s->auth_data);
1926 if (s->auth_proto)
1927 xfree(s->auth_proto);
1928 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001929 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001930}
1931
1932void
1933session_close_by_pid(pid_t pid, int status)
1934{
1935 Session *s = session_by_pid(pid);
1936 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001937 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001938 return;
1939 }
1940 if (s->chanid != -1)
1941 session_exit_message(s, status);
1942 session_close(s);
1943}
1944
Ben Lindstrom4983d5e2001-07-04 05:17:40 +00001945int
1946session_have_children(void)
1947{
1948 int i;
1949
1950 for(i = 0; i < MAX_SESSIONS; i++) {
1951 Session *s = &sessions[i];
1952 if (s->used && s->pid != -1) {
1953 debug("session_have_children: id %d pid %d", i, s->pid);
1954 return 1;
1955 }
1956 }
1957 debug("session_have_children: no more children");
1958 return 0;
1959}
1960
Damien Millerefb4afe2000-04-12 18:45:05 +10001961/*
1962 * this is called when a channel dies before
1963 * the session 'child' itself dies
1964 */
1965void
1966session_close_by_channel(int id, void *arg)
1967{
1968 Session *s = session_by_channel(id);
1969 if (s == NULL) {
1970 debug("session_close_by_channel: no session for channel %d", id);
1971 return;
1972 }
1973 /* disconnect channel */
1974 channel_cancel_cleanup(s->chanid);
1975 s->chanid = -1;
1976
1977 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1978 if (s->pid == 0) {
1979 /* close session immediately */
1980 session_close(s);
1981 } else {
1982 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001983 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001984 error("session_close_by_channel: kill %d: %s",
1985 s->pid, strerror(errno));
1986 }
1987}
1988
Ben Lindstrombba81212001-06-25 05:01:22 +00001989static char *
Damien Millere247cc42000-05-07 12:03:14 +10001990session_tty_list(void)
1991{
1992 static char buf[1024];
1993 int i;
1994 buf[0] = '\0';
1995 for(i = 0; i < MAX_SESSIONS; i++) {
1996 Session *s = &sessions[i];
1997 if (s->used && s->ttyfd != -1) {
1998 if (buf[0] != '\0')
1999 strlcat(buf, ",", sizeof buf);
2000 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2001 }
2002 }
2003 if (buf[0] == '\0')
2004 strlcpy(buf, "notty", sizeof buf);
2005 return buf;
2006}
2007
2008void
2009session_proctitle(Session *s)
2010{
2011 if (s->pw == NULL)
2012 error("no user for session %d", s->self);
2013 else
2014 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2015}
2016
Ben Lindstrom768176b2001-06-09 01:29:12 +00002017int
2018session_setup_x11fwd(Session *s)
2019{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002020 struct stat st;
2021
2022 if (no_x11_forwarding_flag) {
2023 packet_send_debug("X11 forwarding disabled in user configuration file.");
2024 return 0;
2025 }
2026 if (!options.x11_forwarding) {
2027 debug("X11 forwarding disabled in server configuration file.");
2028 return 0;
2029 }
2030 if (!options.xauth_location ||
2031 (stat(options.xauth_location, &st) == -1)) {
2032 packet_send_debug("No xauth program; cannot forward with spoofing.");
2033 return 0;
2034 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002035 if (options.use_login) {
2036 packet_send_debug("X11 forwarding disabled; "
2037 "not compatible with UseLogin=yes.");
2038 return 0;
2039 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002040 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002041 debug("X11 display already set.");
2042 return 0;
2043 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002044 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
2045 if (s->display == NULL) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002046 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002047 return 0;
2048 }
Ben Lindstrom768176b2001-06-09 01:29:12 +00002049 return 1;
2050}
2051
Ben Lindstrombba81212001-06-25 05:01:22 +00002052static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002053do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002054{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002055 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002056}