blob: 63ca371329d314ce12e2583eadca1356d0a58e21 [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 Millere7378562001-12-21 14:58:35 +110036RCSID("$OpenBSD: session.c,v 1.114 2001/12/20 16:37:29 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 */
Kevin Steves366298c2001-12-19 17:58:01 +0000111 int display_number;
Damien Millerb38eff82000-04-01 11:09:21 +1000112 char *display;
113 int screen;
Kevin Steves366298c2001-12-19 17:58:01 +0000114 char *auth_display[2];
Damien Millerb38eff82000-04-01 11:09:21 +1000115 char *auth_proto;
116 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000117 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000118 /* proto 2 */
119 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000120 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +1000121};
122
123/* func */
124
125Session *session_new(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000126void session_set_fds(Session *, int, int, int);
127static void session_pty_cleanup(void *);
128void session_proctitle(Session *);
129int session_setup_x11fwd(Session *);
130void do_exec_pty(Session *, const char *);
131void do_exec_no_pty(Session *, const char *);
132void do_exec(Session *, const char *);
133void do_login(Session *, const char *);
Kevin Stevesa0957d62001-09-27 19:50:26 +0000134#ifdef LOGIN_NEEDS_UTMPX
135static void do_pre_login(Session *s);
136#endif
Kevin Steves8f63caa2001-07-04 18:23:02 +0000137void do_child(Session *, const char *);
Damien Millercf205e82001-04-16 18:29:15 +1000138void do_motd(void);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000139int check_quietlogin(Session *, const char *);
Damien Millerb38eff82000-04-01 11:09:21 +1000140
Ben Lindstrombba81212001-06-25 05:01:22 +0000141static void do_authenticated1(Authctxt *);
142static void do_authenticated2(Authctxt *);
Kevin Steves8f63caa2001-07-04 18:23:02 +0000143
Ben Lindstrombba81212001-06-25 05:01:22 +0000144static void session_close(Session *);
145static int session_pty_req(Session *);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* import */
148extern ServerOptions options;
149extern char *__progname;
150extern int log_stderr;
151extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000152extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000153extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000154extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000155
Damien Miller7b28dc52000-09-05 13:34:53 +1100156/* original command from peer. */
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000157const char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100158
Damien Millerb38eff82000-04-01 11:09:21 +1000159/* data */
160#define MAX_SESSIONS 10
161Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100162
Damien Millerd2c208a2000-05-17 22:00:02 +1000163#ifdef WITH_AIXAUTHENTICATE
Damien Millerd2c208a2000-05-17 22:00:02 +1000164char *aixloginmsg;
165#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000166
Damien Millerad833b32000-08-23 10:46:23 +1000167#ifdef HAVE_LOGIN_CAP
168static login_cap_t *lc;
169#endif
170
Ben Lindstromb31783d2001-03-22 02:02:12 +0000171void
172do_authenticated(Authctxt *authctxt)
173{
174 /*
175 * Cancel the alarm we set to limit the time taken for
176 * authentication.
177 */
178 alarm(0);
179 if (startup_pipe != -1) {
180 close(startup_pipe);
181 startup_pipe = -1;
182 }
183#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
184 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
185 error("unable to get login class");
186 return;
187 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000188#ifdef BSD_AUTH
189 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
190 packet_disconnect("Approval failure for %s",
191 authctxt->pw->pw_name);
192 }
193#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000194#endif
Damien Millere49d0962001-11-13 23:46:18 +1100195#ifdef WITH_AIXAUTHENTICATE
196 /* We don't have a pty yet, so just label the line as "ssh" */
197 if (loginsuccess(authctxt->user,
198 get_canonical_hostname(options.reverse_mapping_check),
199 "ssh", &aixloginmsg) < 0)
200 aixloginmsg = NULL;
201#endif /* WITH_AIXAUTHENTICATE */
202
Ben Lindstromb31783d2001-03-22 02:02:12 +0000203 /* setup the channel layer */
204 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
205 channel_permit_all_opens();
206
207 if (compat20)
208 do_authenticated2(authctxt);
209 else
210 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000211
Ben Lindstrom2bcdf062001-06-13 04:41:41 +0000212 /* remove agent socket */
Ben Lindstrom838394c2001-06-09 01:11:59 +0000213 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000214 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromec95ed92001-07-04 04:21:14 +0000215#ifdef KRB4
216 if (options.kerberos_ticket_cleanup)
217 krb4_cleanup_proc(authctxt);
218#endif
219#ifdef KRB5
220 if (options.kerberos_ticket_cleanup)
221 krb5_cleanup_proc(authctxt);
222#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000223}
224
Damien Millerb38eff82000-04-01 11:09:21 +1000225/*
Damien Millerb38eff82000-04-01 11:09:21 +1000226 * Prepares for an interactive session. This is called after the user has
227 * been successfully authenticated. During this message exchange, pseudo
228 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
229 * are requested, etc.
230 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000231static void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000232do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000233{
234 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000235 char *command;
Ben Lindstrom49c12602001-06-13 04:37:36 +0000236 int success, type, plen, screen_flag;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000237 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000238 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000239
240 s = session_new();
Ben Lindstromec95ed92001-07-04 04:21:14 +0000241 s->authctxt = authctxt;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000242 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000243
Damien Millerb38eff82000-04-01 11:09:21 +1000244 /*
245 * We stay in this loop until the client requests to execute a shell
246 * or a command.
247 */
248 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000249 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000250
251 /* Get a packet from the client. */
252 type = packet_read(&plen);
253
254 /* Process the packet. */
255 switch (type) {
256 case SSH_CMSG_REQUEST_COMPRESSION:
257 packet_integrity_check(plen, 4, type);
258 compression_level = packet_get_int();
259 if (compression_level < 1 || compression_level > 9) {
260 packet_send_debug("Received illegal compression level %d.",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100261 compression_level);
Damien Millerb38eff82000-04-01 11:09:21 +1000262 break;
263 }
264 /* Enable compression after we have responded with SUCCESS. */
265 enable_compression_after_reply = 1;
266 success = 1;
267 break;
268
269 case SSH_CMSG_REQUEST_PTY:
Ben Lindstrom49c12602001-06-13 04:37:36 +0000270 success = session_pty_req(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000271 break;
272
273 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000274 s->auth_proto = packet_get_string(&proto_len);
275 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000276
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000277 screen_flag = packet_get_protocol_flags() &
278 SSH_PROTOFLAG_SCREEN_NUMBER;
279 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
280
281 if (packet_remaining() == 4) {
282 if (!screen_flag)
283 debug2("Buggy client: "
284 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000285 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000286 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000287 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000288 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000289 packet_done();
290 success = session_setup_x11fwd(s);
291 if (!success) {
292 xfree(s->auth_proto);
293 xfree(s->auth_data);
Ben Lindstrom88259fb2001-06-12 00:21:34 +0000294 s->auth_proto = NULL;
295 s->auth_data = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +1000296 }
Damien Millerb38eff82000-04-01 11:09:21 +1000297 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000298
299 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
300 if (no_agent_forwarding_flag || compat13) {
301 debug("Authentication agent forwarding not permitted for this authentication.");
302 break;
303 }
304 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000305 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000306 break;
307
308 case SSH_CMSG_PORT_FORWARD_REQUEST:
309 if (no_port_forwarding_flag) {
310 debug("Port forwarding not permitted for this authentication.");
311 break;
312 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100313 if (!options.allow_tcp_forwarding) {
314 debug("Port forwarding not permitted.");
315 break;
316 }
Damien Millerb38eff82000-04-01 11:09:21 +1000317 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000318 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000319 success = 1;
320 break;
321
322 case SSH_CMSG_MAX_PACKET_SIZE:
323 if (packet_set_maxsize(packet_get_int()) > 0)
324 success = 1;
325 break;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100326
Ben Lindstromec95ed92001-07-04 04:21:14 +0000327#if defined(AFS) || defined(KRB5)
328 case SSH_CMSG_HAVE_KERBEROS_TGT:
329 if (!options.kerberos_tgt_passing) {
330 verbose("Kerberos TGT passing disabled.");
331 } else {
332 char *kdata = packet_get_string(&dlen);
333 packet_integrity_check(plen, 4 + dlen, type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100334
Ben Lindstromec95ed92001-07-04 04:21:14 +0000335 /* XXX - 0x41, see creds_to_radix version */
336 if (kdata[0] != 0x41) {
337#ifdef KRB5
338 krb5_data tgt;
339 tgt.data = kdata;
340 tgt.length = dlen;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100341
Ben Lindstromec95ed92001-07-04 04:21:14 +0000342 if (auth_krb5_tgt(s->authctxt, &tgt))
343 success = 1;
344 else
345 verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user);
346#endif /* KRB5 */
347 } else {
348#ifdef AFS
349 if (auth_krb4_tgt(s->authctxt, kdata))
350 success = 1;
351 else
352 verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user);
353#endif /* AFS */
354 }
355 xfree(kdata);
356 }
357 break;
358#endif /* AFS || KRB5 */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100359
Ben Lindstromec95ed92001-07-04 04:21:14 +0000360#ifdef AFS
361 case SSH_CMSG_HAVE_AFS_TOKEN:
362 if (!options.afs_token_passing || !k_hasafs()) {
363 verbose("AFS token passing disabled.");
364 } else {
365 /* Accept AFS token. */
366 char *token = packet_get_string(&dlen);
367 packet_integrity_check(plen, 4 + dlen, type);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100368
Ben Lindstromec95ed92001-07-04 04:21:14 +0000369 if (auth_afs_token(s->authctxt, token))
370 success = 1;
371 else
372 verbose("AFS token refused for %.100s",
373 s->authctxt->user);
374 xfree(token);
375 }
376 break;
377#endif /* AFS */
Damien Millerb38eff82000-04-01 11:09:21 +1000378
379 case SSH_CMSG_EXEC_SHELL:
380 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000381 if (type == SSH_CMSG_EXEC_CMD) {
382 command = packet_get_string(&dlen);
383 debug("Exec command '%.500s'", command);
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000384 do_exec(s, command);
385 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000386 } else {
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000387 do_exec(s, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000388 }
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000389 packet_done();
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000390 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000391 return;
392
393 default:
394 /*
395 * Any unknown messages in this phase are ignored,
396 * and a failure message is returned.
397 */
398 log("Unknown packet type received after authentication: %d", type);
399 }
400 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
401 packet_send();
402 packet_write_wait();
403
404 /* Enable compression now that we have replied if appropriate. */
405 if (enable_compression_after_reply) {
406 enable_compression_after_reply = 0;
407 packet_start_compression(compression_level);
408 }
409 }
410}
411
412/*
413 * This is called to fork and execute a command when we have no tty. This
414 * will call do_child from the child, and server_loop from the parent after
415 * setting up file descriptors and such.
416 */
Damien Miller4af51302000-04-16 11:18:38 +1000417void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000418do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000419{
420 int pid;
421
422#ifdef USE_PIPES
423 int pin[2], pout[2], perr[2];
424 /* Allocate pipes for communicating with the program. */
425 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
426 packet_disconnect("Could not create pipes: %.100s",
427 strerror(errno));
428#else /* USE_PIPES */
429 int inout[2], err[2];
430 /* Uses socket pairs to communicate with the program. */
431 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
432 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
433 packet_disconnect("Could not create socket pairs: %.100s",
434 strerror(errno));
435#endif /* USE_PIPES */
436 if (s == NULL)
437 fatal("do_exec_no_pty: no session");
438
Damien Millere247cc42000-05-07 12:03:14 +1000439 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000440
Damien Millerc5946332001-02-28 11:46:11 +1100441#if defined(USE_PAM)
Damien Miller665af9c2001-06-27 09:34:15 +1000442 do_pam_session(s->pw->pw_name, NULL);
Damien Millerf9e93002001-03-27 16:12:24 +1000443 do_pam_setcred(1);
Damien Millerc3aa3dd2001-10-28 22:34:52 +1100444 if (is_pam_password_change_required())
445 packet_disconnect("Password change required but no "
446 "TTY available");
Kevin Stevesff793a22001-02-21 16:36:51 +0000447#endif /* USE_PAM */
448
Damien Millerb38eff82000-04-01 11:09:21 +1000449 /* Fork the child. */
450 if ((pid = fork()) == 0) {
451 /* Child. Reinitialize the log since the pid has changed. */
452 log_init(__progname, options.log_level, options.log_facility, log_stderr);
453
454 /*
455 * Create a new session and process group since the 4.4BSD
456 * setlogin() affects the entire process group.
457 */
458 if (setsid() < 0)
459 error("setsid failed: %.100s", strerror(errno));
460
461#ifdef USE_PIPES
462 /*
463 * Redirect stdin. We close the parent side of the socket
464 * pair, and make the child side the standard input.
465 */
466 close(pin[1]);
467 if (dup2(pin[0], 0) < 0)
468 perror("dup2 stdin");
469 close(pin[0]);
470
471 /* Redirect stdout. */
472 close(pout[0]);
473 if (dup2(pout[1], 1) < 0)
474 perror("dup2 stdout");
475 close(pout[1]);
476
477 /* Redirect stderr. */
478 close(perr[0]);
479 if (dup2(perr[1], 2) < 0)
480 perror("dup2 stderr");
481 close(perr[1]);
482#else /* USE_PIPES */
483 /*
484 * Redirect stdin, stdout, and stderr. Stdin and stdout will
485 * use the same socket, as some programs (particularly rdist)
486 * seem to depend on it.
487 */
488 close(inout[1]);
489 close(err[1]);
490 if (dup2(inout[0], 0) < 0) /* stdin */
491 perror("dup2 stdin");
492 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
493 perror("dup2 stdout");
494 if (dup2(err[0], 2) < 0) /* stderr */
495 perror("dup2 stderr");
496#endif /* USE_PIPES */
497
498 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000499 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000500 /* NOTREACHED */
501 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100502#ifdef HAVE_CYGWIN
503 if (is_winnt)
504 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
505#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000506 if (pid < 0)
507 packet_disconnect("fork failed: %.100s", strerror(errno));
508 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000509 /* Set interactive/non-interactive mode. */
510 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000511#ifdef USE_PIPES
512 /* We are the parent. Close the child sides of the pipes. */
513 close(pin[0]);
514 close(pout[1]);
515 close(perr[1]);
516
Damien Millerefb4afe2000-04-12 18:45:05 +1000517 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000518 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000519 } else {
520 /* Enter the interactive session. */
521 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000522 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000523 }
Damien Millerb38eff82000-04-01 11:09:21 +1000524#else /* USE_PIPES */
525 /* We are the parent. Close the child sides of the socket pairs. */
526 close(inout[0]);
527 close(err[0]);
528
529 /*
530 * Enter the interactive session. Note: server_loop must be able to
531 * handle the case that fdin and fdout are the same.
532 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000533 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000534 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000535 } else {
536 server_loop(pid, inout[1], inout[1], err[1]);
537 /* server_loop has closed inout[1] and err[1]. */
538 }
Damien Millerb38eff82000-04-01 11:09:21 +1000539#endif /* USE_PIPES */
540}
541
542/*
543 * This is called to fork and execute a command when we have a tty. This
544 * will call do_child from the child, and server_loop from the parent after
545 * setting up file descriptors, controlling tty, updating wtmp, utmp,
546 * lastlog, and other such operations.
547 */
Damien Miller4af51302000-04-16 11:18:38 +1000548void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000549do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000550{
Damien Millerb38eff82000-04-01 11:09:21 +1000551 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000552 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000553
554 if (s == NULL)
555 fatal("do_exec_pty: no session");
556 ptyfd = s->ptyfd;
557 ttyfd = s->ttyfd;
558
Damien Millerc5946332001-02-28 11:46:11 +1100559#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000560 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000561 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100562#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000563
Damien Millerb38eff82000-04-01 11:09:21 +1000564 /* Fork the child. */
565 if ((pid = fork()) == 0) {
Ben Lindstrombddd5512001-07-04 04:53:53 +0000566
Damien Miller942da032000-08-18 13:59:06 +1000567 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000568 log_init(__progname, options.log_level, options.log_facility, log_stderr);
Damien Millerb38eff82000-04-01 11:09:21 +1000569 /* Close the master side of the pseudo tty. */
570 close(ptyfd);
571
572 /* Make the pseudo tty our controlling tty. */
573 pty_make_controlling_tty(&ttyfd, s->tty);
574
Damien Miller9c751422001-10-10 15:02:46 +1000575 /* Redirect stdin/stdout/stderr from the pseudo tty. */
576 if (dup2(ttyfd, 0) < 0)
577 error("dup2 stdin: %s", strerror(errno));
578 if (dup2(ttyfd, 1) < 0)
579 error("dup2 stdout: %s", strerror(errno));
580 if (dup2(ttyfd, 2) < 0)
581 error("dup2 stderr: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +1000582
583 /* Close the extra descriptor for the pseudo tty. */
584 close(ttyfd);
585
Damien Miller942da032000-08-18 13:59:06 +1000586 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000587#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100588 if (!(options.use_login && command == NULL))
589 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000590# ifdef LOGIN_NEEDS_UTMPX
591 else
592 do_pre_login(s);
593# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000594#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000595
Damien Millerb38eff82000-04-01 11:09:21 +1000596 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000597 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000598 /* NOTREACHED */
599 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100600#ifdef HAVE_CYGWIN
601 if (is_winnt)
602 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
603#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000604 if (pid < 0)
605 packet_disconnect("fork failed: %.100s", strerror(errno));
606 s->pid = pid;
607
608 /* Parent. Close the slave side of the pseudo tty. */
609 close(ttyfd);
610
611 /*
612 * Create another descriptor of the pty master side for use as the
613 * standard input. We could use the original descriptor, but this
614 * simplifies code in server_loop. The descriptor is bidirectional.
615 */
616 fdout = dup(ptyfd);
617 if (fdout < 0)
618 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
619
620 /* we keep a reference to the pty master */
621 ptymaster = dup(ptyfd);
622 if (ptymaster < 0)
623 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
624 s->ptymaster = ptymaster;
625
626 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000627 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000628 if (compat20) {
629 session_set_fds(s, ptyfd, fdout, -1);
630 } else {
631 server_loop(pid, ptyfd, fdout, -1);
632 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000633 }
Damien Millerb38eff82000-04-01 11:09:21 +1000634}
635
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000636#ifdef LOGIN_NEEDS_UTMPX
Damien Miller599d8eb2001-09-15 12:25:53 +1000637static void
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000638do_pre_login(Session *s)
639{
640 socklen_t fromlen;
641 struct sockaddr_storage from;
642 pid_t pid = getpid();
643
644 /*
645 * Get IP address of client. If the connection is not a socket, let
646 * the address be 0.0.0.0.
647 */
648 memset(&from, 0, sizeof(from));
649 if (packet_connection_is_on_socket()) {
650 fromlen = sizeof(from);
651 if (getpeername(packet_get_connection_in(),
Damien Miller9f0f5c62001-12-21 14:45:46 +1100652 (struct sockaddr *) & from, &fromlen) < 0) {
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000653 debug("getpeername: %.100s", strerror(errno));
654 fatal_cleanup();
655 }
656 }
657
658 record_utmp_only(pid, s->tty, s->pw->pw_name,
659 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
660 (struct sockaddr *)&from);
661}
662#endif
663
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000664/*
665 * This is called to fork and execute a command. If another command is
666 * to be forced, execute that instead.
667 */
668void
669do_exec(Session *s, const char *command)
670{
671 if (forced_command) {
672 original_command = command;
673 command = forced_command;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000674 debug("Forced command '%.900s'", command);
675 }
676
677 if (s->ttyfd != -1)
678 do_exec_pty(s, command);
679 else
680 do_exec_no_pty(s, command);
681
Ben Lindstrom0a7ca6c2001-06-21 03:17:42 +0000682 original_command = NULL;
Ben Lindstromc85ab8a2001-06-21 03:13:10 +0000683}
684
Damien Miller942da032000-08-18 13:59:06 +1000685/* administrative, login(1)-like work */
686void
Damien Miller69b69aa2000-10-28 14:19:58 +1100687do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000688{
Damien Miller942da032000-08-18 13:59:06 +1000689 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100690 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000691 socklen_t fromlen;
692 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000693 time_t last_login_time;
694 struct passwd * pw = s->pw;
695 pid_t pid = getpid();
696
697 /*
698 * Get IP address of client. If the connection is not a socket, let
699 * the address be 0.0.0.0.
700 */
701 memset(&from, 0, sizeof(from));
702 if (packet_connection_is_on_socket()) {
703 fromlen = sizeof(from);
704 if (getpeername(packet_get_connection_in(),
705 (struct sockaddr *) & from, &fromlen) < 0) {
706 debug("getpeername: %.100s", strerror(errno));
707 fatal_cleanup();
708 }
709 }
710
Damien Miller7b28dc52000-09-05 13:34:53 +1100711 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000712 if (options.print_lastlog) {
713 hostname[0] = '\0';
714 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
715 hostname, sizeof(hostname));
716 }
Damien Millere4340be2000-09-16 13:29:08 +1100717
Damien Miller942da032000-08-18 13:59:06 +1000718 /* Record that there was a login on that tty from the remote host. */
719 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000720 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
721 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000722
Kevin Steves092f2ef2000-10-14 13:36:13 +0000723#ifdef USE_PAM
724 /*
725 * If password change is needed, do it now.
726 * This needs to occur before the ~/.hushlogin check.
727 */
Damien Miller646aa602001-02-15 11:51:32 +1100728 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000729 print_pam_messages();
730 do_pam_chauthtok();
731 }
732#endif
733
Damien Millercf205e82001-04-16 18:29:15 +1000734 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000735 return;
736
737#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100738 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000739 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000740#endif /* USE_PAM */
741#ifdef WITH_AIXAUTHENTICATE
742 if (aixloginmsg && *aixloginmsg)
743 printf("%s\n", aixloginmsg);
744#endif /* WITH_AIXAUTHENTICATE */
745
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000746 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000747 time_string = ctime(&last_login_time);
748 if (strchr(time_string, '\n'))
749 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000750 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000751 printf("Last login: %s\r\n", time_string);
752 else
Damien Millere4340be2000-09-16 13:29:08 +1100753 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000754 }
Damien Millercf205e82001-04-16 18:29:15 +1000755
756 do_motd();
757}
758
759/*
760 * Display the message of the day.
761 */
762void
763do_motd(void)
764{
765 FILE *f;
766 char buf[256];
767
Damien Miller942da032000-08-18 13:59:06 +1000768 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000769#ifdef HAVE_LOGIN_CAP
770 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
771 "/etc/motd"), "r");
772#else
Damien Miller942da032000-08-18 13:59:06 +1000773 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000774#endif
Damien Miller942da032000-08-18 13:59:06 +1000775 if (f) {
776 while (fgets(buf, sizeof(buf), f))
777 fputs(buf, stdout);
778 fclose(f);
779 }
780 }
781}
782
Kevin Steves8f63caa2001-07-04 18:23:02 +0000783
784/*
785 * Check for quiet login, either .hushlogin or command given.
786 */
787int
788check_quietlogin(Session *s, const char *command)
789{
790 char buf[256];
791 struct passwd *pw = s->pw;
792 struct stat st;
793
794 /* Return 1 if .hushlogin exists or a command given. */
795 if (command != NULL)
796 return 1;
797 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
798#ifdef HAVE_LOGIN_CAP
799 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
800 return 1;
801#else
802 if (stat(buf, &st) >= 0)
803 return 1;
804#endif
805 return 0;
806}
807
Damien Millerb38eff82000-04-01 11:09:21 +1000808/*
809 * Sets the value of the given variable in the environment. If the variable
810 * already exists, its value is overriden.
811 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000812static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000813child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100814 const char *value)
Damien Millerb38eff82000-04-01 11:09:21 +1000815{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000816 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000817 char **env;
818
819 /*
820 * Find the slot where the value should be stored. If the variable
821 * already exists, we reuse the slot; otherwise we append a new slot
822 * at the end of the array, expanding if necessary.
823 */
824 env = *envp;
825 namelen = strlen(name);
826 for (i = 0; env[i]; i++)
827 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
828 break;
829 if (env[i]) {
830 /* Reuse the slot. */
831 xfree(env[i]);
832 } else {
833 /* New variable. Expand if necessary. */
834 if (i >= (*envsizep) - 1) {
835 (*envsizep) += 50;
836 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
837 }
838 /* Need to set the NULL pointer at end of array beyond the new slot. */
839 env[i + 1] = NULL;
840 }
841
842 /* Allocate space and format the variable in the appropriate slot. */
843 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
844 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
845}
846
847/*
848 * Reads environment variables from the given file and adds/overrides them
849 * into the environment. If the file does not exist, this does nothing.
850 * Otherwise, it must consist of empty lines, comments (line starts with '#')
851 * and assignments of the form name=value. No other forms are allowed.
852 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000853static void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000854read_environment_file(char ***env, u_int *envsize,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100855 const char *filename)
Damien Millerb38eff82000-04-01 11:09:21 +1000856{
857 FILE *f;
858 char buf[4096];
859 char *cp, *value;
860
861 f = fopen(filename, "r");
862 if (!f)
863 return;
864
865 while (fgets(buf, sizeof(buf), f)) {
866 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
867 ;
868 if (!*cp || *cp == '#' || *cp == '\n')
869 continue;
870 if (strchr(cp, '\n'))
871 *strchr(cp, '\n') = '\0';
872 value = strchr(cp, '=');
873 if (value == NULL) {
874 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
875 continue;
876 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000877 /*
878 * Replace the equals sign by nul, and advance value to
879 * the value string.
880 */
Damien Millerb38eff82000-04-01 11:09:21 +1000881 *value = '\0';
882 value++;
883 child_set_env(env, envsize, cp, value);
884 }
885 fclose(f);
886}
887
888#ifdef USE_PAM
889/*
890 * Sets any environment variables which have been specified by PAM
891 */
Kevin Steves9b26f962001-06-29 17:52:17 +0000892void do_pam_environment(char ***env, u_int *envsize)
Damien Millerb38eff82000-04-01 11:09:21 +1000893{
894 char *equals, var_name[512], var_val[512];
895 char **pam_env;
896 int i;
897
898 if ((pam_env = fetch_pam_environment()) == NULL)
899 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000900
Damien Millerb38eff82000-04-01 11:09:21 +1000901 for(i = 0; pam_env[i] != NULL; i++) {
902 if ((equals = strstr(pam_env[i], "=")) == NULL)
903 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000904
Damien Millerb38eff82000-04-01 11:09:21 +1000905 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
906 memset(var_name, '\0', sizeof(var_name));
907 memset(var_val, '\0', sizeof(var_val));
908
909 strncpy(var_name, pam_env[i], equals - pam_env[i]);
910 strcpy(var_val, equals + 1);
911
Damien Millercb5e44a2000-09-29 12:12:36 +1100912 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000913
914 child_set_env(env, envsize, var_name, var_val);
915 }
916 }
917}
918#endif /* USE_PAM */
919
Damien Millercb5e44a2000-09-29 12:12:36 +1100920#ifdef HAVE_CYGWIN
Kevin Steves9b26f962001-06-29 17:52:17 +0000921void copy_environment(char ***env, u_int *envsize)
Damien Millercb5e44a2000-09-29 12:12:36 +1100922{
923 char *equals, var_name[512], var_val[512];
924 int i;
925
926 for(i = 0; environ[i] != NULL; i++) {
927 if ((equals = strstr(environ[i], "=")) == NULL)
928 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000929
Damien Millercb5e44a2000-09-29 12:12:36 +1100930 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
931 memset(var_name, '\0', sizeof(var_name));
932 memset(var_val, '\0', sizeof(var_val));
933
934 strncpy(var_name, environ[i], equals - environ[i]);
935 strcpy(var_val, equals + 1);
936
937 debug3("Copy environment: %s=%s", var_name, var_val);
938
939 child_set_env(env, envsize, var_name, var_val);
940 }
941 }
942}
943#endif
944
Damien Miller5fc85652000-07-09 23:53:07 +1000945#if defined(HAVE_GETUSERATTR)
946/*
947 * AIX-specific login initialisation
948 */
949void set_limit(char *user, char *soft, char *hard, int resource, int mult)
950{
951 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000952 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000953
954 getrlimit(resource, &rlim);
955
Damien Miller65964d62000-07-11 09:16:22 +1000956 slim = 0;
957 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
958 if (slim < 0) {
959 rlim.rlim_cur = RLIM_INFINITY;
960 } else if (slim != 0) {
961 /* See the wackiness below */
962 if (rlim.rlim_cur == slim * mult)
963 slim = 0;
964 else
965 rlim.rlim_cur = slim * mult;
966 }
967 }
Damien Miller5fc85652000-07-09 23:53:07 +1000968
Damien Miller65964d62000-07-11 09:16:22 +1000969 hlim = 0;
970 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
971 if (hlim < 0) {
972 rlim.rlim_max = RLIM_INFINITY;
973 } else if (hlim != 0) {
974 rlim.rlim_max = hlim * mult;
975 }
976 }
Damien Miller5fc85652000-07-09 23:53:07 +1000977
Damien Miller65964d62000-07-11 09:16:22 +1000978 /*
979 * XXX For cpu and fsize the soft limit is set to the hard limit
980 * if the hard limit is left at its default value and the soft limit
981 * is changed from its default value, either by requesting it
982 * (slim == 0) or by setting it to the current default. At least
983 * that's how rlogind does it. If you're confused you're not alone.
984 * Bug or feature? AIX 4.3.1.2
985 */
986 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
987 && hlim == 0 && slim != 0)
988 rlim.rlim_max = rlim.rlim_cur;
989 /* A specified hard limit limits the soft limit */
990 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
991 rlim.rlim_cur = rlim.rlim_max;
992 /* A soft limit can increase a hard limit */
993 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000994 rlim.rlim_max = rlim.rlim_cur;
995
996 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000997 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000998}
999
1000void set_limits_from_userattr(char *user)
1001{
1002 int mask;
1003 char buf[16];
1004
1005 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
1006 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
1007 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1008 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1009 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1010 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1011#if defined(S_UNOFILE)
1012 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1013#endif
1014
1015 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1016 /* Convert decimal to octal */
1017 (void) snprintf(buf, sizeof(buf), "%d", mask);
1018 if (sscanf(buf, "%o", &mask) == 1)
1019 umask(mask);
1020 }
1021}
1022#endif /* defined(HAVE_GETUSERATTR) */
1023
Damien Millerb38eff82000-04-01 11:09:21 +10001024/*
1025 * Performs common processing for the child, such as setting up the
1026 * environment, closing extra file descriptors, setting the user and group
1027 * ids, and executing the command or shell.
1028 */
Damien Miller4af51302000-04-16 11:18:38 +10001029void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001030do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001031{
Damien Miller7b28dc52000-09-05 13:34:53 +11001032 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstromec95ed92001-07-04 04:21:14 +00001033 struct passwd *pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001034 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001035 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001036 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001037 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001038 char **env;
1039 extern char **environ;
1040 struct stat st;
1041 char *argv[10];
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001042 int do_xauth;
Damien Miller91606b12000-06-28 08:22:29 +10001043#ifdef WITH_IRIX_PROJECT
1044 prid_t projid;
1045#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001046#ifdef WITH_IRIX_JOBS
1047 jid_t jid = 0;
1048#else
1049#ifdef WITH_IRIX_ARRAY
1050 int jid = 0;
1051#endif /* WITH_IRIX_ARRAY */
1052#endif /* WITH_IRIX_JOBS */
1053
Ben Lindstrom88259fb2001-06-12 00:21:34 +00001054 do_xauth =
1055 s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
1056
Ben Lindstrom005dd222001-04-18 15:29:33 +00001057 /* remove hostkey from the child's memory */
1058 destroy_sensitive_data();
1059
Damien Millerd3a18572000-06-07 19:55:44 +10001060 /* login(1) is only called if we execute the login shell */
1061 if (options.use_login && command != NULL)
1062 options.use_login = 0;
1063
Damien Millerc62f1fc2001-07-14 11:54:05 +10001064#if !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001065 if (!options.use_login) {
1066# ifdef HAVE_LOGIN_CAP
1067 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1068 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1069 _PATH_NOLOGIN), "r");
1070# else /* HAVE_LOGIN_CAP */
1071 if (pw->pw_uid)
1072 f = fopen(_PATH_NOLOGIN, "r");
1073# endif /* HAVE_LOGIN_CAP */
1074 if (f) {
1075 /* /etc/nologin exists. Print its contents and exit. */
1076 while (fgets(buf, sizeof(buf), f))
1077 fputs(buf, stderr);
1078 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001079 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001080 }
Damien Millerb38eff82000-04-01 11:09:21 +10001081 }
Damien Millerc62f1fc2001-07-14 11:54:05 +10001082#endif /* HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001083
Damien Millerad833b32000-08-23 10:46:23 +10001084 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001085 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1086 switch, so we let login(1) to this for us. */
1087 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001088#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001089 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001090 if (!check_quietlogin(s, command))
1091 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001092#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001093#ifdef HAVE_CYGWIN
1094 if (is_winnt) {
1095#else
Damien Millerb38eff82000-04-01 11:09:21 +10001096 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001097#endif
Damien Millerad833b32000-08-23 10:46:23 +10001098# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001099 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001100# endif /* HAVE_GETUSERATTR */
1101# ifdef HAVE_LOGIN_CAP
1102 if (setusercontext(lc, pw, pw->pw_uid,
1103 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1104 perror("unable to set user context");
1105 exit(1);
1106 }
1107# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001108#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1109 /* Sets login uid for accounting */
1110 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1111 error("setluid: %s", strerror(errno));
1112#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1113
Damien Millerad833b32000-08-23 10:46:23 +10001114 if (setlogin(pw->pw_name) < 0)
1115 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001116 if (setgid(pw->pw_gid) < 0) {
1117 perror("setgid");
1118 exit(1);
1119 }
1120 /* Initialize the group list. */
1121 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1122 perror("initgroups");
1123 exit(1);
1124 }
1125 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001126# ifdef USE_PAM
1127 /*
1128 * PAM credentials may take the form of
1129 * supplementary groups. These will have been
1130 * wiped by the above initgroups() call.
1131 * Reestablish them here.
1132 */
1133 do_pam_setcred(0);
1134# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001135# ifdef WITH_IRIX_JOBS
1136 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1137 if (jid == -1) {
1138 fatal("Failed to create job container: %.100s",
1139 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001140 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001141# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001142# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001143 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001144 if (jid == 0) {
1145 if (newarraysess() != 0)
1146 fatal("Failed to set up new array session: %.100s",
1147 strerror(errno));
1148 }
Damien Millerad833b32000-08-23 10:46:23 +10001149# endif /* WITH_IRIX_ARRAY */
1150# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001151 /* initialize irix project info */
1152 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1153 debug("Failed to get project id, using projid 0");
1154 projid = 0;
1155 }
Damien Miller91606b12000-06-28 08:22:29 +10001156 if (setprid(projid))
1157 fatal("Failed to initialize project %d for %s: %.100s",
1158 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001159# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001160#ifdef WITH_IRIX_AUDIT
1161 if (sysconf(_SC_AUDIT)) {
1162 debug("Setting sat id to %d", (int) pw->pw_uid);
1163 if (satsetid(pw->pw_uid))
1164 debug("error setting satid: %.100s", strerror(errno));
1165 }
1166#endif /* WITH_IRIX_AUDIT */
1167
Damien Miller168a7002001-03-17 10:29:50 +11001168#ifdef _AIX
1169 /*
1170 * AIX has a "usrinfo" area where logname and
1171 * other stuff is stored - a few applications
1172 * actually use this and die if it's not set
1173 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001174 if (s->ttyfd == -1)
1175 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001176 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001177 2 * strlen(pw->pw_name));
1178 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001179 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001180 if (usrinfo(SETUINFO, cp, i) == -1)
1181 fatal("Couldn't set usrinfo: %s",
1182 strerror(errno));
1183 debug3("AIX/UsrInfo: set len %d", i);
1184 xfree(cp);
1185#endif
1186
Damien Millerb38eff82000-04-01 11:09:21 +10001187 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001188 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001189# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001190 }
Damien Millerad833b32000-08-23 10:46:23 +10001191#endif /* HAVE_OSF_SIA */
1192
Damien Millerbac2d8a2000-09-05 16:13:06 +11001193#ifdef HAVE_CYGWIN
1194 if (is_winnt)
1195#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001196 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001197 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001198 }
1199 /*
1200 * Get the shell from the password data. An empty shell field is
1201 * legal, and means /bin/sh.
1202 */
1203 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001204#ifdef HAVE_LOGIN_CAP
1205 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1206#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001207
Damien Millerb38eff82000-04-01 11:09:21 +10001208 /* Initialize the environment. */
1209 envsize = 100;
1210 env = xmalloc(envsize * sizeof(char *));
1211 env[0] = NULL;
1212
Damien Millerbac2d8a2000-09-05 16:13:06 +11001213#ifdef HAVE_CYGWIN
1214 /*
1215 * The Windows environment contains some setting which are
1216 * important for a running system. They must not be dropped.
1217 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001218 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001219#endif
1220
Damien Millerb38eff82000-04-01 11:09:21 +10001221 if (!options.use_login) {
1222 /* Set basic environment. */
1223 child_set_env(&env, &envsize, "USER", pw->pw_name);
1224 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1225 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001226#ifdef HAVE_LOGIN_CAP
1227 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1228 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001229#else /* HAVE_LOGIN_CAP */
1230# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001231 /*
1232 * There's no standard path on Windows. The path contains
1233 * important components pointing to the system directories,
1234 * needed for loading shared libraries. So the path better
1235 * remains intact here.
1236 */
Damien Millerb38eff82000-04-01 11:09:21 +10001237 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001238# endif /* HAVE_CYGWIN */
1239#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001240
1241 snprintf(buf, sizeof buf, "%.200s/%.50s",
1242 _PATH_MAILDIR, pw->pw_name);
1243 child_set_env(&env, &envsize, "MAIL", buf);
1244
1245 /* Normal systems set SHELL by default. */
1246 child_set_env(&env, &envsize, "SHELL", shell);
1247 }
1248 if (getenv("TZ"))
1249 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1250
1251 /* Set custom environment options from RSA authentication. */
Ben Lindstrom38b951c2001-12-06 17:47:47 +00001252 if (!options.use_login) {
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 ;
1259 if (s[i] == '=') {
1260 s[i] = 0;
1261 child_set_env(&env, &envsize, s, s + i + 1);
1262 }
1263 custom_environment = ce->next;
1264 xfree(ce->s);
1265 xfree(ce);
Damien Millerb38eff82000-04-01 11:09:21 +10001266 }
Damien Millerb38eff82000-04-01 11:09:21 +10001267 }
1268
1269 snprintf(buf, sizeof buf, "%.50s %d %d",
1270 get_remote_ipaddr(), get_remote_port(), get_local_port());
1271 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1272
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001273 if (s->ttyfd != -1)
1274 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1275 if (s->term)
1276 child_set_env(&env, &envsize, "TERM", s->term);
1277 if (s->display)
1278 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001279 if (original_command)
1280 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1281 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001282
1283#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001284 if ((cp = getenv("AUTHSTATE")) != NULL)
1285 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1286 if ((cp = getenv("KRB5CCNAME")) != NULL)
1287 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1288 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001289#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001290#ifdef KRB4
Ben Lindstromec95ed92001-07-04 04:21:14 +00001291 if (s->authctxt->krb4_ticket_file)
1292 child_set_env(&env, &envsize, "KRBTKFILE",
1293 s->authctxt->krb4_ticket_file);
1294#endif
1295#ifdef KRB5
1296 if (s->authctxt->krb5_ticket_file)
1297 child_set_env(&env, &envsize, "KRB5CCNAME",
1298 s->authctxt->krb5_ticket_file);
1299#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001300#ifdef USE_PAM
1301 /* Pull in any environment variables that may have been set by PAM. */
1302 do_pam_environment(&env, &envsize);
1303#endif /* USE_PAM */
1304
Damien Millerb38eff82000-04-01 11:09:21 +10001305 if (auth_get_socket_name() != NULL)
1306 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001307 auth_get_socket_name());
Damien Millerb38eff82000-04-01 11:09:21 +10001308
1309 /* read $HOME/.ssh/environment. */
1310 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001311 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1312 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001313 read_environment_file(&env, &envsize, buf);
1314 }
1315 if (debug_flag) {
1316 /* dump the environment */
1317 fprintf(stderr, "Environment:\n");
1318 for (i = 0; env[i]; i++)
1319 fprintf(stderr, " %.200s\n", env[i]);
1320 }
Damien Millerad833b32000-08-23 10:46:23 +10001321 /* we have to stash the hostname before we close our socket. */
1322 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001323 hostname = get_remote_name_or_ip(utmp_len,
1324 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001325 /*
1326 * Close the connection descriptors; note that this is the child, and
1327 * the server will still have the socket open, and it is important
1328 * that we do not shutdown it. Note that the descriptors cannot be
1329 * closed before building the environment, as we call
1330 * get_remote_ipaddr there.
1331 */
1332 if (packet_get_connection_in() == packet_get_connection_out())
1333 close(packet_get_connection_in());
1334 else {
1335 close(packet_get_connection_in());
1336 close(packet_get_connection_out());
1337 }
1338 /*
1339 * Close all descriptors related to channels. They will still remain
1340 * open in the parent.
1341 */
1342 /* XXX better use close-on-exec? -markus */
1343 channel_close_all();
1344
1345 /*
1346 * Close any extra file descriptors. Note that there may still be
1347 * descriptors left by system functions. They will be closed later.
1348 */
1349 endpwent();
1350
1351 /*
1352 * Close any extra open file descriptors so that we don\'t have them
1353 * hanging around in clients. Note that we want to do this after
1354 * initgroups, because at least on Solaris 2.3 it leaves file
1355 * descriptors open.
1356 */
1357 for (i = 3; i < 64; i++)
1358 close(i);
1359
Damien Millerb38eff82000-04-01 11:09:21 +10001360 /*
1361 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1362 * xauth are run in the proper environment.
1363 */
1364 environ = env;
1365
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001366#ifdef AFS
1367 /* Try to get AFS tokens for the local cell. */
1368 if (k_hasafs()) {
1369 char cell[64];
Damien Miller9f0f5c62001-12-21 14:45:46 +11001370
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001371 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1372 krb_afslog(cell, 0);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001373
Ben Lindstrom37e41c92001-09-16 22:17:15 +00001374 krb_afslog(0, 0);
1375 }
1376#endif /* AFS */
1377
Damien Miller139d4cd2001-10-10 15:07:44 +10001378 /* Change current directory to the user\'s home directory. */
1379 if (chdir(pw->pw_dir) < 0) {
1380 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1381 pw->pw_dir, strerror(errno));
1382#ifdef HAVE_LOGIN_CAP
1383 if (login_getcapbool(lc, "requirehome", 0))
1384 exit(1);
1385#endif
1386 }
1387
Damien Millerb38eff82000-04-01 11:09:21 +10001388 /*
1389 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1390 * in this order).
1391 */
1392 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001393 /* ignore _PATH_SSH_USER_RC for subsystems */
1394 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001395 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1396 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001397 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001398 fprintf(stderr, "Running %s\n", cmd);
1399 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001400 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001401 if (do_xauth)
1402 fprintf(f, "%s %s\n", s->auth_proto,
1403 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001404 pclose(f);
1405 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001406 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001407 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001408 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001409 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001410 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1411 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001412 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001413 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001414 if (do_xauth)
1415 fprintf(f, "%s %s\n", s->auth_proto,
1416 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001417 pclose(f);
1418 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001419 fprintf(stderr, "Could not run %s\n",
1420 _PATH_SSH_SYSTEM_RC);
1421 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001422 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001423 if (debug_flag) {
1424 fprintf(stderr,
1425 "Running %.100s add "
1426 "%.100s %.100s %.100s\n",
Kevin Steves366298c2001-12-19 17:58:01 +00001427 options.xauth_location, s->auth_display[0],
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001428 s->auth_proto, s->auth_data);
Kevin Steves366298c2001-12-19 17:58:01 +00001429 if (s->auth_display[1])
Damien Millerb1715dc2000-05-30 13:44:51 +10001430 fprintf(stderr,
Kevin Steves366298c2001-12-19 17:58:01 +00001431 "add %.100s %.100s %.100s\n",
1432 s->auth_display[1],
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001433 s->auth_proto, s->auth_data);
1434 }
1435 snprintf(cmd, sizeof cmd, "%s -q -",
1436 options.xauth_location);
1437 f = popen(cmd, "w");
1438 if (f) {
Kevin Steves366298c2001-12-19 17:58:01 +00001439 fprintf(f, "add %s %s %s\n",
1440 s->auth_display[0], s->auth_proto,
1441 s->auth_data);
1442 if (s->auth_display[1])
1443 fprintf(f, "add %s %s %s\n",
1444 s->auth_display[1], s->auth_proto,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001445 s->auth_data);
1446 pclose(f);
1447 } else {
1448 fprintf(stderr, "Could not run %s\n",
1449 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001450 }
1451 }
Damien Millerb38eff82000-04-01 11:09:21 +10001452 /* Get the last component of the shell name. */
1453 cp = strrchr(shell, '/');
1454 if (cp)
1455 cp++;
1456 else
1457 cp = shell;
1458 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001459
1460 /* restore SIGPIPE for child */
1461 signal(SIGPIPE, SIG_DFL);
1462
Damien Millerb38eff82000-04-01 11:09:21 +10001463 /*
1464 * If we have no command, execute the shell. In this case, the shell
1465 * name to be passed in argv[0] is preceded by '-' to indicate that
1466 * this is a login shell.
1467 */
1468 if (!command) {
1469 if (!options.use_login) {
1470 char buf[256];
1471
Damien Millerb38eff82000-04-01 11:09:21 +10001472 /* Start the shell. Set initial character to '-'. */
1473 buf[0] = '-';
Ben Lindstromccd8d072001-12-07 17:26:48 +00001474 strlcpy(buf + 1, cp, sizeof(buf) - 1);
Damien Millerb38eff82000-04-01 11:09:21 +10001475
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
Damien Miller9f0f5c62001-12-21 14:45:46 +11001490 s->term? s->term : "unknown",
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001491#endif
Damien Miller9f0f5c62001-12-21 14:45:46 +11001492 "-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");
Damien Miller9f0f5c62001-12-21 14:45:46 +11001520 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001521 sessions[i].used = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001522 }
1523 did_init = 1;
1524 }
Damien Miller9f0f5c62001-12-21 14:45:46 +11001525 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001526 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;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001545 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001546 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;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001578 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001579 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);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001595 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millerefb4afe2000-04-12 18:45:05 +10001596 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);
Kevin Steves366298c2001-12-19 17:58:01 +00001944 if (s->auth_display[0])
1945 xfree(s->auth_display[0]);
1946 if (s->auth_display[1])
1947 xfree(s->auth_display[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +10001948 if (s->auth_data)
1949 xfree(s->auth_data);
1950 if (s->auth_proto)
1951 xfree(s->auth_proto);
1952 s->used = 0;
Damien Millere247cc42000-05-07 12:03:14 +10001953 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001954}
1955
1956void
1957session_close_by_pid(pid_t pid, int status)
1958{
1959 Session *s = session_by_pid(pid);
1960 if (s == NULL) {
Ben Lindstrom7a837222001-06-13 19:23:32 +00001961 debug("session_close_by_pid: no session for pid %d", pid);
Damien Millerefb4afe2000-04-12 18:45:05 +10001962 return;
1963 }
1964 if (s->chanid != -1)
1965 session_exit_message(s, status);
1966 session_close(s);
1967}
1968
1969/*
1970 * this is called when a channel dies before
1971 * the session 'child' itself dies
1972 */
1973void
1974session_close_by_channel(int id, void *arg)
1975{
1976 Session *s = session_by_channel(id);
1977 if (s == NULL) {
Damien Miller3ec27592001-10-12 11:35:04 +10001978 debug("session_close_by_channel: no session for id %d", id);
Damien Millerefb4afe2000-04-12 18:45:05 +10001979 return;
1980 }
Damien Miller3ec27592001-10-12 11:35:04 +10001981 debug("session_close_by_channel: channel %d child %d", id, s->pid);
1982 if (s->pid != 0) {
Damien Miller3ec27592001-10-12 11:35:04 +10001983 debug("session_close_by_channel: channel %d: has child", id);
Damien Miller0585d512001-10-12 11:35:50 +10001984 /*
1985 * delay detach of session, but release pty, since
1986 * the fd's to the child are already closed
1987 */
1988 if (s->ttyfd != -1) {
1989 fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1990 session_pty_cleanup(s);
1991 }
Damien Miller3ec27592001-10-12 11:35:04 +10001992 return;
1993 }
1994 /* detach by removing callback */
Damien Millerefb4afe2000-04-12 18:45:05 +10001995 channel_cancel_cleanup(s->chanid);
1996 s->chanid = -1;
Damien Miller52b77be2001-10-10 15:14:37 +10001997 session_close(s);
1998}
1999
2000void
Damien Miller3ec27592001-10-12 11:35:04 +10002001session_destroy_all(void)
Damien Miller52b77be2001-10-10 15:14:37 +10002002{
2003 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002004 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Miller52b77be2001-10-10 15:14:37 +10002005 Session *s = &sessions[i];
Damien Miller9f0f5c62001-12-21 14:45:46 +11002006 if (s->used)
Damien Miller52b77be2001-10-10 15:14:37 +10002007 session_close(s);
Damien Miller52b77be2001-10-10 15:14:37 +10002008 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002009}
2010
Ben Lindstrombba81212001-06-25 05:01:22 +00002011static char *
Damien Millere247cc42000-05-07 12:03:14 +10002012session_tty_list(void)
2013{
2014 static char buf[1024];
2015 int i;
2016 buf[0] = '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +11002017 for (i = 0; i < MAX_SESSIONS; i++) {
Damien Millere247cc42000-05-07 12:03:14 +10002018 Session *s = &sessions[i];
2019 if (s->used && s->ttyfd != -1) {
2020 if (buf[0] != '\0')
2021 strlcat(buf, ",", sizeof buf);
2022 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2023 }
2024 }
2025 if (buf[0] == '\0')
2026 strlcpy(buf, "notty", sizeof buf);
2027 return buf;
2028}
2029
2030void
2031session_proctitle(Session *s)
2032{
2033 if (s->pw == NULL)
2034 error("no user for session %d", s->self);
2035 else
2036 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2037}
2038
Ben Lindstrom768176b2001-06-09 01:29:12 +00002039int
2040session_setup_x11fwd(Session *s)
2041{
Ben Lindstrom768176b2001-06-09 01:29:12 +00002042 struct stat st;
Kevin Steves366298c2001-12-19 17:58:01 +00002043 char display[512], auth_display[512];
2044 char hostname[MAXHOSTNAMELEN];
Ben Lindstrom768176b2001-06-09 01:29:12 +00002045
2046 if (no_x11_forwarding_flag) {
2047 packet_send_debug("X11 forwarding disabled in user configuration file.");
2048 return 0;
2049 }
2050 if (!options.x11_forwarding) {
2051 debug("X11 forwarding disabled in server configuration file.");
2052 return 0;
2053 }
2054 if (!options.xauth_location ||
2055 (stat(options.xauth_location, &st) == -1)) {
2056 packet_send_debug("No xauth program; cannot forward with spoofing.");
2057 return 0;
2058 }
Ben Lindstrom699776e2001-06-21 03:14:49 +00002059 if (options.use_login) {
2060 packet_send_debug("X11 forwarding disabled; "
2061 "not compatible with UseLogin=yes.");
2062 return 0;
2063 }
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002064 if (s->display != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002065 debug("X11 display already set.");
2066 return 0;
2067 }
Kevin Steves366298c2001-12-19 17:58:01 +00002068 s->display_number = x11_create_display_inet(options.x11_display_offset,
Damien Millere7378562001-12-21 14:58:35 +11002069 options.gateway_ports, s->single_connection);
Kevin Steves366298c2001-12-19 17:58:01 +00002070 if (s->display_number == -1) {
Ben Lindstrom2bcdf062001-06-13 04:41:41 +00002071 debug("x11_create_display_inet failed.");
Ben Lindstrom768176b2001-06-09 01:29:12 +00002072 return 0;
2073 }
Kevin Steves366298c2001-12-19 17:58:01 +00002074
2075 /* Set up a suitable value for the DISPLAY variable. */
2076 if (gethostname(hostname, sizeof(hostname)) < 0)
2077 fatal("gethostname: %.100s", strerror(errno));
2078 /*
2079 * auth_display must be used as the displayname when the
2080 * authorization entry is added with xauth(1). This will be
2081 * different than the DISPLAY string for localhost displays.
2082 */
2083 s->auth_display[1] = NULL;
2084 if (!options.gateway_ports) {
2085 struct utsname uts;
2086
2087 snprintf(display, sizeof display, "localhost:%d.%d",
2088 s->display_number, s->screen);
2089 snprintf(auth_display, sizeof auth_display, "%.400s/unix:%d.%d",
2090 hostname, s->display_number, s->screen);
2091 s->display = xstrdup(display);
2092 s->auth_display[0] = xstrdup(auth_display);
2093 /*
2094 * Xlib may use gethostbyname() or uname() hostname to
2095 * look up authorization data for FamilyLocal; see:
2096 * xc/lib/xtrans/Xtrans.c:TRANS(GetHostname)
2097 * We just add authorization entries with both
2098 * hostname and nodename if they are different.
2099 */
2100 if (uname(&uts) == -1)
2101 fatal("uname: %.100s", strerror(errno));
2102 if (strcmp(hostname, uts.nodename) != 0) {
2103 snprintf(auth_display, sizeof auth_display,
2104 "%.400s/unix:%d.%d", uts.nodename,
2105 s->display_number, s->screen);
2106 s->auth_display[1] = xstrdup(auth_display);
2107 }
2108 } else {
2109#ifdef IPADDR_IN_DISPLAY
2110 struct hostent *he;
2111 struct in_addr my_addr;
2112
2113 he = gethostbyname(hostname);
2114 if (he == NULL) {
2115 error("Can't get IP address for X11 DISPLAY.");
2116 packet_send_debug("Can't get IP address for X11 DISPLAY.");
2117 return 0;
2118 }
2119 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2120 snprintf(display, sizeof display, "%.50s:%d.%d", inet_ntoa(my_addr),
2121 s->display_number, s->screen);
2122#else
2123 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
2124 s->display_number, s->screen);
2125#endif
2126 s->display = xstrdup(display);
2127 s->auth_display[0] = xstrdup(display);
2128 }
2129
Ben Lindstrom768176b2001-06-09 01:29:12 +00002130 return 1;
2131}
2132
Ben Lindstrombba81212001-06-25 05:01:22 +00002133static void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002134do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002135{
Ben Lindstrombddd5512001-07-04 04:53:53 +00002136 server_loop2(authctxt);
Damien Millerefb4afe2000-04-12 18:45:05 +10002137}