blob: 9f472a22b11ac8a04d3fa32deed18ac586a3baab [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 Lindstrom92a2e382001-03-05 06:59:27 +000012 * Copyright (c) 2000 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 Lindstrom4d3f2272001-06-09 01:44:07 +000036RCSID("$OpenBSD: session.c,v 1.83 2001/06/07 22:25:02 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;
102 struct passwd *pw;
103 pid_t pid;
104 /* tty */
105 char *term;
106 int ptyfd, ttyfd, ptymaster;
107 int row, col, xpixel, ypixel;
108 char tty[TTYSZ];
109 /* X11 */
110 char *display;
111 int screen;
112 char *auth_proto;
113 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000114 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000115 /* proto 2 */
116 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000117 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +1000118};
119
120/* func */
121
122Session *session_new(void);
123void session_set_fds(Session *s, int fdin, int fdout, int fderr);
124void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000125void session_proctitle(Session *s);
Ben Lindstrom768176b2001-06-09 01:29:12 +0000126int session_setup_x11fwd(Session *s);
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000127void session_close(Session *s);
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000128void do_exec_pty(Session *s, const char *command);
129void do_exec_no_pty(Session *s, const char *command);
Damien Miller69b69aa2000-10-28 14:19:58 +1100130void do_login(Session *s, const char *command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000131#ifdef LOGIN_NEEDS_UTMPX
132void do_pre_login(Session *s);
133#endif
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000134void do_child(Session *s, const char *command);
Damien Millercf205e82001-04-16 18:29:15 +1000135void do_motd(void);
136int check_quietlogin(Session *s, const char *command);
Ben Lindstrom983c0982001-06-09 01:20:06 +0000137void xauthfile_cleanup_proc(void *pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000138
Ben Lindstromb31783d2001-03-22 02:02:12 +0000139void do_authenticated1(Authctxt *authctxt);
140void do_authenticated2(Authctxt *authctxt);
141
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 Millerb38eff82000-04-01 11:09:21 +1000151/* Local Xauthority file. */
152static char *xauthfile;
153
Damien Miller7b28dc52000-09-05 13:34:53 +1100154/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000155char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100156
Damien Millerb38eff82000-04-01 11:09:21 +1000157/* data */
158#define MAX_SESSIONS 10
159Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100160
Damien Millerd2c208a2000-05-17 22:00:02 +1000161#ifdef WITH_AIXAUTHENTICATE
162/* AIX's lastlogin message, set in auth1.c */
163char *aixloginmsg;
164#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000165
Damien Millerad833b32000-08-23 10:46:23 +1000166#ifdef HAVE_LOGIN_CAP
167static login_cap_t *lc;
168#endif
169
Ben Lindstromb31783d2001-03-22 02:02:12 +0000170void
171do_authenticated(Authctxt *authctxt)
172{
173 /*
174 * Cancel the alarm we set to limit the time taken for
175 * authentication.
176 */
177 alarm(0);
178 if (startup_pipe != -1) {
179 close(startup_pipe);
180 startup_pipe = -1;
181 }
182#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
183 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
184 error("unable to get login class");
185 return;
186 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000187#ifdef BSD_AUTH
188 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
189 packet_disconnect("Approval failure for %s",
190 authctxt->pw->pw_name);
191 }
192#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000193#endif
194 /* setup the channel layer */
195 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
196 channel_permit_all_opens();
197
198 if (compat20)
199 do_authenticated2(authctxt);
200 else
201 do_authenticated1(authctxt);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000202
203 /* remote user's local Xauthority file and agent socket */
204 if (xauthfile)
Ben Lindstrom983c0982001-06-09 01:20:06 +0000205 xauthfile_cleanup_proc(authctxt->pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000206 if (auth_get_socket_name())
Ben Lindstrom983c0982001-06-09 01:20:06 +0000207 auth_sock_cleanup_proc(authctxt->pw);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000208}
209
Damien Millerb38eff82000-04-01 11:09:21 +1000210/*
211 * Remove local Xauthority file.
212 */
213void
Ben Lindstrom983c0982001-06-09 01:20:06 +0000214xauthfile_cleanup_proc(void *_pw)
Damien Millerb38eff82000-04-01 11:09:21 +1000215{
Ben Lindstrom983c0982001-06-09 01:20:06 +0000216 struct passwd *pw = _pw;
217 char *p;
Damien Millerb38eff82000-04-01 11:09:21 +1000218
Ben Lindstrom983c0982001-06-09 01:20:06 +0000219 debug("xauthfile_cleanup_proc called");
Damien Millerb38eff82000-04-01 11:09:21 +1000220 if (xauthfile != NULL) {
Ben Lindstrom983c0982001-06-09 01:20:06 +0000221 temporarily_use_uid(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000222 unlink(xauthfile);
223 p = strrchr(xauthfile, '/');
224 if (p != NULL) {
225 *p = '\0';
226 rmdir(xauthfile);
227 }
228 xfree(xauthfile);
229 xauthfile = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +0000230 restore_uid();
Damien Millerb38eff82000-04-01 11:09:21 +1000231 }
232}
233
234/*
235 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
236 * dropped connection).
237 */
Damien Miller4af51302000-04-16 11:18:38 +1000238void
Damien Millerb38eff82000-04-01 11:09:21 +1000239pty_cleanup_proc(void *session)
240{
241 Session *s=session;
242 if (s == NULL)
243 fatal("pty_cleanup_proc: no session");
244 debug("pty_cleanup_proc: %s", s->tty);
245
246 if (s->pid != 0) {
247 /* Record that the user has logged out. */
248 record_logout(s->pid, s->tty);
249 }
250
251 /* Release the pseudo-tty. */
252 pty_release(s->tty);
253}
254
255/*
256 * Prepares for an interactive session. This is called after the user has
257 * been successfully authenticated. During this message exchange, pseudo
258 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
259 * are requested, etc.
260 */
Damien Miller4af51302000-04-16 11:18:38 +1000261void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000262do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000263{
264 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000265 char *command;
Ben Lindstrom768176b2001-06-09 01:29:12 +0000266 int success, type, n_bytes, plen, screen_flag, have_pty = 0;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000267 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000268 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000269
270 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000271 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000272
Damien Millerb38eff82000-04-01 11:09:21 +1000273 /*
274 * We stay in this loop until the client requests to execute a shell
275 * or a command.
276 */
277 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000278 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000279
280 /* Get a packet from the client. */
281 type = packet_read(&plen);
282
283 /* Process the packet. */
284 switch (type) {
285 case SSH_CMSG_REQUEST_COMPRESSION:
286 packet_integrity_check(plen, 4, type);
287 compression_level = packet_get_int();
288 if (compression_level < 1 || compression_level > 9) {
289 packet_send_debug("Received illegal compression level %d.",
290 compression_level);
291 break;
292 }
293 /* Enable compression after we have responded with SUCCESS. */
294 enable_compression_after_reply = 1;
295 success = 1;
296 break;
297
298 case SSH_CMSG_REQUEST_PTY:
299 if (no_pty_flag) {
300 debug("Allocating a pty not permitted for this authentication.");
301 break;
302 }
303 if (have_pty)
304 packet_disconnect("Protocol error: you already have a pty.");
305
306 debug("Allocating pty.");
307
308 /* Allocate a pty and open it. */
309 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
310 sizeof(s->tty))) {
311 error("Failed to allocate pty.");
312 break;
313 }
314 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000315 pty_setowner(s->pw, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000316
317 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
318 s->term = packet_get_string(&dlen);
319 packet_integrity_check(dlen, strlen(s->term), type);
320 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
321 /* Remaining bytes */
322 n_bytes = plen - (4 + dlen + 4 * 4);
323
324 if (strcmp(s->term, "") == 0) {
325 xfree(s->term);
326 s->term = NULL;
327 }
328 /* Get window size from the packet. */
329 s->row = packet_get_int();
330 s->col = packet_get_int();
331 s->xpixel = packet_get_int();
332 s->ypixel = packet_get_int();
333 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
334
335 /* Get tty modes from the packet. */
336 tty_parse_modes(s->ttyfd, &n_bytes);
337 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
338
Damien Millere247cc42000-05-07 12:03:14 +1000339 session_proctitle(s);
340
Damien Millerb38eff82000-04-01 11:09:21 +1000341 /* Indicate that we now have a pty. */
342 success = 1;
343 have_pty = 1;
344 break;
345
346 case SSH_CMSG_X11_REQUEST_FORWARDING:
Damien Millerb38eff82000-04-01 11:09:21 +1000347 s->auth_proto = packet_get_string(&proto_len);
348 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000349
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000350 screen_flag = packet_get_protocol_flags() &
351 SSH_PROTOFLAG_SCREEN_NUMBER;
352 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
353
354 if (packet_remaining() == 4) {
355 if (!screen_flag)
356 debug2("Buggy client: "
357 "X11 screen flag missing");
Damien Millerb38eff82000-04-01 11:09:21 +1000358 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000359 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000360 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000361 }
Ben Lindstrom768176b2001-06-09 01:29:12 +0000362 packet_done();
363 success = session_setup_x11fwd(s);
364 if (!success) {
365 xfree(s->auth_proto);
366 xfree(s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +1000367 }
Damien Millerb38eff82000-04-01 11:09:21 +1000368 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000369
370 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
371 if (no_agent_forwarding_flag || compat13) {
372 debug("Authentication agent forwarding not permitted for this authentication.");
373 break;
374 }
375 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000376 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000377 break;
378
379 case SSH_CMSG_PORT_FORWARD_REQUEST:
380 if (no_port_forwarding_flag) {
381 debug("Port forwarding not permitted for this authentication.");
382 break;
383 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100384 if (!options.allow_tcp_forwarding) {
385 debug("Port forwarding not permitted.");
386 break;
387 }
Damien Millerb38eff82000-04-01 11:09:21 +1000388 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000389 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000390 success = 1;
391 break;
392
393 case SSH_CMSG_MAX_PACKET_SIZE:
394 if (packet_set_maxsize(packet_get_int()) > 0)
395 success = 1;
396 break;
397
398 case SSH_CMSG_EXEC_SHELL:
399 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000400 if (type == SSH_CMSG_EXEC_CMD) {
401 command = packet_get_string(&dlen);
402 debug("Exec command '%.500s'", command);
403 packet_integrity_check(plen, 4 + dlen, type);
404 } else {
405 command = NULL;
406 packet_integrity_check(plen, 0, type);
407 }
408 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100409 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000410 command = forced_command;
411 debug("Forced command '%.500s'", forced_command);
412 }
413 if (have_pty)
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000414 do_exec_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000415 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000416 do_exec_no_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000417 if (command != NULL)
418 xfree(command);
Ben Lindstromcb3929d2001-06-09 01:34:15 +0000419 session_close(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000420 return;
421
422 default:
423 /*
424 * Any unknown messages in this phase are ignored,
425 * and a failure message is returned.
426 */
427 log("Unknown packet type received after authentication: %d", type);
428 }
429 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
430 packet_send();
431 packet_write_wait();
432
433 /* Enable compression now that we have replied if appropriate. */
434 if (enable_compression_after_reply) {
435 enable_compression_after_reply = 0;
436 packet_start_compression(compression_level);
437 }
438 }
439}
440
441/*
442 * This is called to fork and execute a command when we have no tty. This
443 * will call do_child from the child, and server_loop from the parent after
444 * setting up file descriptors and such.
445 */
Damien Miller4af51302000-04-16 11:18:38 +1000446void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000447do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000448{
449 int pid;
450
451#ifdef USE_PIPES
452 int pin[2], pout[2], perr[2];
453 /* Allocate pipes for communicating with the program. */
454 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
455 packet_disconnect("Could not create pipes: %.100s",
456 strerror(errno));
457#else /* USE_PIPES */
458 int inout[2], err[2];
459 /* Uses socket pairs to communicate with the program. */
460 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
461 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
462 packet_disconnect("Could not create socket pairs: %.100s",
463 strerror(errno));
464#endif /* USE_PIPES */
465 if (s == NULL)
466 fatal("do_exec_no_pty: no session");
467
Damien Millere247cc42000-05-07 12:03:14 +1000468 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000469
Damien Millerc5946332001-02-28 11:46:11 +1100470#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000471 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000472#endif /* USE_PAM */
473
Damien Millerb38eff82000-04-01 11:09:21 +1000474 /* Fork the child. */
475 if ((pid = fork()) == 0) {
476 /* Child. Reinitialize the log since the pid has changed. */
477 log_init(__progname, options.log_level, options.log_facility, log_stderr);
478
479 /*
480 * Create a new session and process group since the 4.4BSD
481 * setlogin() affects the entire process group.
482 */
483 if (setsid() < 0)
484 error("setsid failed: %.100s", strerror(errno));
485
486#ifdef USE_PIPES
487 /*
488 * Redirect stdin. We close the parent side of the socket
489 * pair, and make the child side the standard input.
490 */
491 close(pin[1]);
492 if (dup2(pin[0], 0) < 0)
493 perror("dup2 stdin");
494 close(pin[0]);
495
496 /* Redirect stdout. */
497 close(pout[0]);
498 if (dup2(pout[1], 1) < 0)
499 perror("dup2 stdout");
500 close(pout[1]);
501
502 /* Redirect stderr. */
503 close(perr[0]);
504 if (dup2(perr[1], 2) < 0)
505 perror("dup2 stderr");
506 close(perr[1]);
507#else /* USE_PIPES */
508 /*
509 * Redirect stdin, stdout, and stderr. Stdin and stdout will
510 * use the same socket, as some programs (particularly rdist)
511 * seem to depend on it.
512 */
513 close(inout[1]);
514 close(err[1]);
515 if (dup2(inout[0], 0) < 0) /* stdin */
516 perror("dup2 stdin");
517 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
518 perror("dup2 stdout");
519 if (dup2(err[0], 2) < 0) /* stderr */
520 perror("dup2 stderr");
521#endif /* USE_PIPES */
522
523 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000524 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000525 /* NOTREACHED */
526 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100527#ifdef HAVE_CYGWIN
528 if (is_winnt)
529 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
530#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000531 if (pid < 0)
532 packet_disconnect("fork failed: %.100s", strerror(errno));
533 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000534 /* Set interactive/non-interactive mode. */
535 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000536#ifdef USE_PIPES
537 /* We are the parent. Close the child sides of the pipes. */
538 close(pin[0]);
539 close(pout[1]);
540 close(perr[1]);
541
Damien Millerefb4afe2000-04-12 18:45:05 +1000542 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000543 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000544 } else {
545 /* Enter the interactive session. */
546 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000547 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000548 }
Damien Millerb38eff82000-04-01 11:09:21 +1000549#else /* USE_PIPES */
550 /* We are the parent. Close the child sides of the socket pairs. */
551 close(inout[0]);
552 close(err[0]);
553
554 /*
555 * Enter the interactive session. Note: server_loop must be able to
556 * handle the case that fdin and fdout are the same.
557 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000558 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000559 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000560 } else {
561 server_loop(pid, inout[1], inout[1], err[1]);
562 /* server_loop has closed inout[1] and err[1]. */
563 }
Damien Millerb38eff82000-04-01 11:09:21 +1000564#endif /* USE_PIPES */
565}
566
567/*
568 * This is called to fork and execute a command when we have a tty. This
569 * will call do_child from the child, and server_loop from the parent after
570 * setting up file descriptors, controlling tty, updating wtmp, utmp,
571 * lastlog, and other such operations.
572 */
Damien Miller4af51302000-04-16 11:18:38 +1000573void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000574do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000575{
Damien Millerb38eff82000-04-01 11:09:21 +1000576 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000577 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000578
579 if (s == NULL)
580 fatal("do_exec_pty: no session");
581 ptyfd = s->ptyfd;
582 ttyfd = s->ttyfd;
583
Damien Millerc5946332001-02-28 11:46:11 +1100584#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000585 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000586 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100587#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000588
Damien Millerb38eff82000-04-01 11:09:21 +1000589 /* Fork the child. */
590 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000591 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000592 log_init(__progname, options.log_level, options.log_facility, log_stderr);
593
594 /* Close the master side of the pseudo tty. */
595 close(ptyfd);
596
597 /* Make the pseudo tty our controlling tty. */
598 pty_make_controlling_tty(&ttyfd, s->tty);
599
600 /* Redirect stdin from the pseudo tty. */
601 if (dup2(ttyfd, fileno(stdin)) < 0)
602 error("dup2 stdin failed: %.100s", strerror(errno));
603
604 /* Redirect stdout to the pseudo tty. */
605 if (dup2(ttyfd, fileno(stdout)) < 0)
606 error("dup2 stdin failed: %.100s", strerror(errno));
607
608 /* Redirect stderr to the pseudo tty. */
609 if (dup2(ttyfd, fileno(stderr)) < 0)
610 error("dup2 stdin failed: %.100s", strerror(errno));
611
612 /* Close the extra descriptor for the pseudo tty. */
613 close(ttyfd);
614
Damien Miller942da032000-08-18 13:59:06 +1000615 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000616#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100617 if (!(options.use_login && command == NULL))
618 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000619# ifdef LOGIN_NEEDS_UTMPX
620 else
621 do_pre_login(s);
622# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000623#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000624
Damien Millerb38eff82000-04-01 11:09:21 +1000625 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000626 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000627 /* NOTREACHED */
628 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100629#ifdef HAVE_CYGWIN
630 if (is_winnt)
631 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
632#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000633 if (pid < 0)
634 packet_disconnect("fork failed: %.100s", strerror(errno));
635 s->pid = pid;
636
637 /* Parent. Close the slave side of the pseudo tty. */
638 close(ttyfd);
639
640 /*
641 * Create another descriptor of the pty master side for use as the
642 * standard input. We could use the original descriptor, but this
643 * simplifies code in server_loop. The descriptor is bidirectional.
644 */
645 fdout = dup(ptyfd);
646 if (fdout < 0)
647 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
648
649 /* we keep a reference to the pty master */
650 ptymaster = dup(ptyfd);
651 if (ptymaster < 0)
652 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
653 s->ptymaster = ptymaster;
654
655 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000656 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000657 if (compat20) {
658 session_set_fds(s, ptyfd, fdout, -1);
659 } else {
660 server_loop(pid, ptyfd, fdout, -1);
661 /* server_loop _has_ closed ptyfd and fdout. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000662 }
Damien Millerb38eff82000-04-01 11:09:21 +1000663}
664
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000665#ifdef LOGIN_NEEDS_UTMPX
666void
667do_pre_login(Session *s)
668{
669 socklen_t fromlen;
670 struct sockaddr_storage from;
671 pid_t pid = getpid();
672
673 /*
674 * Get IP address of client. If the connection is not a socket, let
675 * the address be 0.0.0.0.
676 */
677 memset(&from, 0, sizeof(from));
678 if (packet_connection_is_on_socket()) {
679 fromlen = sizeof(from);
680 if (getpeername(packet_get_connection_in(),
681 (struct sockaddr *) & from, &fromlen) < 0) {
682 debug("getpeername: %.100s", strerror(errno));
683 fatal_cleanup();
684 }
685 }
686
687 record_utmp_only(pid, s->tty, s->pw->pw_name,
688 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
689 (struct sockaddr *)&from);
690}
691#endif
692
Damien Miller942da032000-08-18 13:59:06 +1000693/* administrative, login(1)-like work */
694void
Damien Miller69b69aa2000-10-28 14:19:58 +1100695do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000696{
Damien Miller942da032000-08-18 13:59:06 +1000697 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100698 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000699 socklen_t fromlen;
700 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000701 time_t last_login_time;
702 struct passwd * pw = s->pw;
703 pid_t pid = getpid();
704
705 /*
706 * Get IP address of client. If the connection is not a socket, let
707 * the address be 0.0.0.0.
708 */
709 memset(&from, 0, sizeof(from));
710 if (packet_connection_is_on_socket()) {
711 fromlen = sizeof(from);
712 if (getpeername(packet_get_connection_in(),
713 (struct sockaddr *) & from, &fromlen) < 0) {
714 debug("getpeername: %.100s", strerror(errno));
715 fatal_cleanup();
716 }
717 }
718
Damien Miller7b28dc52000-09-05 13:34:53 +1100719 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000720 if (options.print_lastlog) {
721 hostname[0] = '\0';
722 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
723 hostname, sizeof(hostname));
724 }
Damien Millere4340be2000-09-16 13:29:08 +1100725
Damien Miller942da032000-08-18 13:59:06 +1000726 /* Record that there was a login on that tty from the remote host. */
727 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000728 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
729 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000730
Kevin Steves092f2ef2000-10-14 13:36:13 +0000731#ifdef USE_PAM
732 /*
733 * If password change is needed, do it now.
734 * This needs to occur before the ~/.hushlogin check.
735 */
Damien Miller646aa602001-02-15 11:51:32 +1100736 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000737 print_pam_messages();
738 do_pam_chauthtok();
739 }
740#endif
741
Damien Millercf205e82001-04-16 18:29:15 +1000742 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000743 return;
744
745#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100746 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000747 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000748#endif /* USE_PAM */
749#ifdef WITH_AIXAUTHENTICATE
750 if (aixloginmsg && *aixloginmsg)
751 printf("%s\n", aixloginmsg);
752#endif /* WITH_AIXAUTHENTICATE */
753
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000754 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000755 time_string = ctime(&last_login_time);
756 if (strchr(time_string, '\n'))
757 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000758 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000759 printf("Last login: %s\r\n", time_string);
760 else
Damien Millere4340be2000-09-16 13:29:08 +1100761 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000762 }
Damien Millercf205e82001-04-16 18:29:15 +1000763
764 do_motd();
765}
766
767/*
768 * Display the message of the day.
769 */
770void
771do_motd(void)
772{
773 FILE *f;
774 char buf[256];
775
Damien Miller942da032000-08-18 13:59:06 +1000776 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000777#ifdef HAVE_LOGIN_CAP
778 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
779 "/etc/motd"), "r");
780#else
Damien Miller942da032000-08-18 13:59:06 +1000781 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000782#endif
Damien Miller942da032000-08-18 13:59:06 +1000783 if (f) {
784 while (fgets(buf, sizeof(buf), f))
785 fputs(buf, stdout);
786 fclose(f);
787 }
788 }
789}
790
Damien Millercf205e82001-04-16 18:29:15 +1000791
792/*
793 * Check for quiet login, either .hushlogin or command given.
794 */
795int
796check_quietlogin(Session *s, const char *command)
797{
798 char buf[256];
799 struct passwd * pw = s->pw;
800 struct stat st;
801
802 /* Return 1 if .hushlogin exists or a command given. */
803 if (command != NULL)
804 return 1;
805 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
806#ifdef HAVE_LOGIN_CAP
807 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
808 return 1;
809#else
810 if (stat(buf, &st) >= 0)
811 return 1;
812#endif
813 return 0;
814}
815
Damien Millerb38eff82000-04-01 11:09:21 +1000816/*
817 * Sets the value of the given variable in the environment. If the variable
818 * already exists, its value is overriden.
819 */
Damien Miller4af51302000-04-16 11:18:38 +1000820void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000821child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000822 const char *value)
823{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000824 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000825 char **env;
826
827 /*
828 * Find the slot where the value should be stored. If the variable
829 * already exists, we reuse the slot; otherwise we append a new slot
830 * at the end of the array, expanding if necessary.
831 */
832 env = *envp;
833 namelen = strlen(name);
834 for (i = 0; env[i]; i++)
835 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
836 break;
837 if (env[i]) {
838 /* Reuse the slot. */
839 xfree(env[i]);
840 } else {
841 /* New variable. Expand if necessary. */
842 if (i >= (*envsizep) - 1) {
843 (*envsizep) += 50;
844 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
845 }
846 /* Need to set the NULL pointer at end of array beyond the new slot. */
847 env[i + 1] = NULL;
848 }
849
850 /* Allocate space and format the variable in the appropriate slot. */
851 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
852 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
853}
854
855/*
856 * Reads environment variables from the given file and adds/overrides them
857 * into the environment. If the file does not exist, this does nothing.
858 * Otherwise, it must consist of empty lines, comments (line starts with '#')
859 * and assignments of the form name=value. No other forms are allowed.
860 */
Damien Miller4af51302000-04-16 11:18:38 +1000861void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000862read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000863 const char *filename)
864{
865 FILE *f;
866 char buf[4096];
867 char *cp, *value;
868
869 f = fopen(filename, "r");
870 if (!f)
871 return;
872
873 while (fgets(buf, sizeof(buf), f)) {
874 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
875 ;
876 if (!*cp || *cp == '#' || *cp == '\n')
877 continue;
878 if (strchr(cp, '\n'))
879 *strchr(cp, '\n') = '\0';
880 value = strchr(cp, '=');
881 if (value == NULL) {
882 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
883 continue;
884 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000885 /*
886 * Replace the equals sign by nul, and advance value to
887 * the value string.
888 */
Damien Millerb38eff82000-04-01 11:09:21 +1000889 *value = '\0';
890 value++;
891 child_set_env(env, envsize, cp, value);
892 }
893 fclose(f);
894}
895
896#ifdef USE_PAM
897/*
898 * Sets any environment variables which have been specified by PAM
899 */
900void do_pam_environment(char ***env, int *envsize)
901{
902 char *equals, var_name[512], var_val[512];
903 char **pam_env;
904 int i;
905
906 if ((pam_env = fetch_pam_environment()) == NULL)
907 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000908
Damien Millerb38eff82000-04-01 11:09:21 +1000909 for(i = 0; pam_env[i] != NULL; i++) {
910 if ((equals = strstr(pam_env[i], "=")) == NULL)
911 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000912
Damien Millerb38eff82000-04-01 11:09:21 +1000913 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
914 memset(var_name, '\0', sizeof(var_name));
915 memset(var_val, '\0', sizeof(var_val));
916
917 strncpy(var_name, pam_env[i], equals - pam_env[i]);
918 strcpy(var_val, equals + 1);
919
Damien Millercb5e44a2000-09-29 12:12:36 +1100920 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000921
922 child_set_env(env, envsize, var_name, var_val);
923 }
924 }
925}
926#endif /* USE_PAM */
927
Damien Millercb5e44a2000-09-29 12:12:36 +1100928#ifdef HAVE_CYGWIN
929void copy_environment(char ***env, int *envsize)
930{
931 char *equals, var_name[512], var_val[512];
932 int i;
933
934 for(i = 0; environ[i] != NULL; i++) {
935 if ((equals = strstr(environ[i], "=")) == NULL)
936 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000937
Damien Millercb5e44a2000-09-29 12:12:36 +1100938 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
939 memset(var_name, '\0', sizeof(var_name));
940 memset(var_val, '\0', sizeof(var_val));
941
942 strncpy(var_name, environ[i], equals - environ[i]);
943 strcpy(var_val, equals + 1);
944
945 debug3("Copy environment: %s=%s", var_name, var_val);
946
947 child_set_env(env, envsize, var_name, var_val);
948 }
949 }
950}
951#endif
952
Damien Miller5fc85652000-07-09 23:53:07 +1000953#if defined(HAVE_GETUSERATTR)
954/*
955 * AIX-specific login initialisation
956 */
957void set_limit(char *user, char *soft, char *hard, int resource, int mult)
958{
959 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000960 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000961
962 getrlimit(resource, &rlim);
963
Damien Miller65964d62000-07-11 09:16:22 +1000964 slim = 0;
965 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
966 if (slim < 0) {
967 rlim.rlim_cur = RLIM_INFINITY;
968 } else if (slim != 0) {
969 /* See the wackiness below */
970 if (rlim.rlim_cur == slim * mult)
971 slim = 0;
972 else
973 rlim.rlim_cur = slim * mult;
974 }
975 }
Damien Miller5fc85652000-07-09 23:53:07 +1000976
Damien Miller65964d62000-07-11 09:16:22 +1000977 hlim = 0;
978 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
979 if (hlim < 0) {
980 rlim.rlim_max = RLIM_INFINITY;
981 } else if (hlim != 0) {
982 rlim.rlim_max = hlim * mult;
983 }
984 }
Damien Miller5fc85652000-07-09 23:53:07 +1000985
Damien Miller65964d62000-07-11 09:16:22 +1000986 /*
987 * XXX For cpu and fsize the soft limit is set to the hard limit
988 * if the hard limit is left at its default value and the soft limit
989 * is changed from its default value, either by requesting it
990 * (slim == 0) or by setting it to the current default. At least
991 * that's how rlogind does it. If you're confused you're not alone.
992 * Bug or feature? AIX 4.3.1.2
993 */
994 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
995 && hlim == 0 && slim != 0)
996 rlim.rlim_max = rlim.rlim_cur;
997 /* A specified hard limit limits the soft limit */
998 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
999 rlim.rlim_cur = rlim.rlim_max;
1000 /* A soft limit can increase a hard limit */
1001 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +10001002 rlim.rlim_max = rlim.rlim_cur;
1003
1004 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +10001005 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +10001006}
1007
1008void set_limits_from_userattr(char *user)
1009{
1010 int mask;
1011 char buf[16];
1012
1013 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
1014 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
1015 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1016 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1017 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1018 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1019#if defined(S_UNOFILE)
1020 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1021#endif
1022
1023 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1024 /* Convert decimal to octal */
1025 (void) snprintf(buf, sizeof(buf), "%d", mask);
1026 if (sscanf(buf, "%o", &mask) == 1)
1027 umask(mask);
1028 }
1029}
1030#endif /* defined(HAVE_GETUSERATTR) */
1031
Damien Millerb38eff82000-04-01 11:09:21 +10001032/*
1033 * Performs common processing for the child, such as setting up the
1034 * environment, closing extra file descriptors, setting the user and group
1035 * ids, and executing the command or shell.
1036 */
Damien Miller4af51302000-04-16 11:18:38 +10001037void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001038do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001039{
Damien Miller7b28dc52000-09-05 13:34:53 +11001040 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001041 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001042 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001043 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001044 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001045 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001046 char **env;
1047 extern char **environ;
1048 struct stat st;
1049 char *argv[10];
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001050 int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
Damien Miller91606b12000-06-28 08:22:29 +10001051#ifdef WITH_IRIX_PROJECT
1052 prid_t projid;
1053#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001054#ifdef WITH_IRIX_JOBS
1055 jid_t jid = 0;
1056#else
1057#ifdef WITH_IRIX_ARRAY
1058 int jid = 0;
1059#endif /* WITH_IRIX_ARRAY */
1060#endif /* WITH_IRIX_JOBS */
1061
Ben Lindstrom005dd222001-04-18 15:29:33 +00001062 /* remove hostkey from the child's memory */
1063 destroy_sensitive_data();
1064
Damien Millerd3a18572000-06-07 19:55:44 +10001065 /* login(1) is only called if we execute the login shell */
1066 if (options.use_login && command != NULL)
1067 options.use_login = 0;
1068
Damien Miller364a9bd2001-04-16 18:37:05 +10001069#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001070 if (!options.use_login) {
1071# ifdef HAVE_LOGIN_CAP
1072 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1073 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1074 _PATH_NOLOGIN), "r");
1075# else /* HAVE_LOGIN_CAP */
1076 if (pw->pw_uid)
1077 f = fopen(_PATH_NOLOGIN, "r");
1078# endif /* HAVE_LOGIN_CAP */
1079 if (f) {
1080 /* /etc/nologin exists. Print its contents and exit. */
1081 while (fgets(buf, sizeof(buf), f))
1082 fputs(buf, stderr);
1083 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001084 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001085 }
Damien Millerb38eff82000-04-01 11:09:21 +10001086 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001087#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001088
Damien Millerad833b32000-08-23 10:46:23 +10001089 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001090 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1091 switch, so we let login(1) to this for us. */
1092 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001093#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001094 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001095 if (!check_quietlogin(s, command))
1096 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001097#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001098#ifdef HAVE_CYGWIN
1099 if (is_winnt) {
1100#else
Damien Millerb38eff82000-04-01 11:09:21 +10001101 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001102#endif
Damien Millerad833b32000-08-23 10:46:23 +10001103# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001104 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001105# endif /* HAVE_GETUSERATTR */
1106# ifdef HAVE_LOGIN_CAP
1107 if (setusercontext(lc, pw, pw->pw_uid,
1108 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1109 perror("unable to set user context");
1110 exit(1);
1111 }
1112# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001113#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1114 /* Sets login uid for accounting */
1115 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1116 error("setluid: %s", strerror(errno));
1117#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1118
Damien Millerad833b32000-08-23 10:46:23 +10001119 if (setlogin(pw->pw_name) < 0)
1120 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001121 if (setgid(pw->pw_gid) < 0) {
1122 perror("setgid");
1123 exit(1);
1124 }
1125 /* Initialize the group list. */
1126 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1127 perror("initgroups");
1128 exit(1);
1129 }
1130 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001131# ifdef USE_PAM
1132 /*
1133 * PAM credentials may take the form of
1134 * supplementary groups. These will have been
1135 * wiped by the above initgroups() call.
1136 * Reestablish them here.
1137 */
1138 do_pam_setcred(0);
1139# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001140# ifdef WITH_IRIX_JOBS
1141 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1142 if (jid == -1) {
1143 fatal("Failed to create job container: %.100s",
1144 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001145 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001146# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001147# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001148 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001149 if (jid == 0) {
1150 if (newarraysess() != 0)
1151 fatal("Failed to set up new array session: %.100s",
1152 strerror(errno));
1153 }
Damien Millerad833b32000-08-23 10:46:23 +10001154# endif /* WITH_IRIX_ARRAY */
1155# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001156 /* initialize irix project info */
1157 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1158 debug("Failed to get project id, using projid 0");
1159 projid = 0;
1160 }
Damien Miller91606b12000-06-28 08:22:29 +10001161 if (setprid(projid))
1162 fatal("Failed to initialize project %d for %s: %.100s",
1163 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001164# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001165#ifdef WITH_IRIX_AUDIT
1166 if (sysconf(_SC_AUDIT)) {
1167 debug("Setting sat id to %d", (int) pw->pw_uid);
1168 if (satsetid(pw->pw_uid))
1169 debug("error setting satid: %.100s", strerror(errno));
1170 }
1171#endif /* WITH_IRIX_AUDIT */
1172
Damien Miller168a7002001-03-17 10:29:50 +11001173#ifdef _AIX
1174 /*
1175 * AIX has a "usrinfo" area where logname and
1176 * other stuff is stored - a few applications
1177 * actually use this and die if it's not set
1178 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001179 if (s->ttyfd == -1)
1180 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001181 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001182 2 * strlen(pw->pw_name));
1183 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001184 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001185 if (usrinfo(SETUINFO, cp, i) == -1)
1186 fatal("Couldn't set usrinfo: %s",
1187 strerror(errno));
1188 debug3("AIX/UsrInfo: set len %d", i);
1189 xfree(cp);
1190#endif
1191
Damien Millerb38eff82000-04-01 11:09:21 +10001192 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001193 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001194# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001195 }
Damien Millerad833b32000-08-23 10:46:23 +10001196#endif /* HAVE_OSF_SIA */
1197
Damien Millerbac2d8a2000-09-05 16:13:06 +11001198#ifdef HAVE_CYGWIN
1199 if (is_winnt)
1200#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001201 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001202 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001203 }
1204 /*
1205 * Get the shell from the password data. An empty shell field is
1206 * legal, and means /bin/sh.
1207 */
1208 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001209#ifdef HAVE_LOGIN_CAP
1210 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1211#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001212
1213#ifdef AFS
1214 /* Try to get AFS tokens for the local cell. */
1215 if (k_hasafs()) {
1216 char cell[64];
1217
1218 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1219 krb_afslog(cell, 0);
1220
1221 krb_afslog(0, 0);
1222 }
1223#endif /* AFS */
1224
1225 /* Initialize the environment. */
1226 envsize = 100;
1227 env = xmalloc(envsize * sizeof(char *));
1228 env[0] = NULL;
1229
Damien Millerbac2d8a2000-09-05 16:13:06 +11001230#ifdef HAVE_CYGWIN
1231 /*
1232 * The Windows environment contains some setting which are
1233 * important for a running system. They must not be dropped.
1234 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001235 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001236#endif
1237
Damien Millerb38eff82000-04-01 11:09:21 +10001238 if (!options.use_login) {
1239 /* Set basic environment. */
1240 child_set_env(&env, &envsize, "USER", pw->pw_name);
1241 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1242 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001243#ifdef HAVE_LOGIN_CAP
1244 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1245 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001246#else /* HAVE_LOGIN_CAP */
1247# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001248 /*
1249 * There's no standard path on Windows. The path contains
1250 * important components pointing to the system directories,
1251 * needed for loading shared libraries. So the path better
1252 * remains intact here.
1253 */
Damien Millerb38eff82000-04-01 11:09:21 +10001254 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001255# endif /* HAVE_CYGWIN */
1256#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001257
1258 snprintf(buf, sizeof buf, "%.200s/%.50s",
1259 _PATH_MAILDIR, pw->pw_name);
1260 child_set_env(&env, &envsize, "MAIL", buf);
1261
1262 /* Normal systems set SHELL by default. */
1263 child_set_env(&env, &envsize, "SHELL", shell);
1264 }
1265 if (getenv("TZ"))
1266 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1267
1268 /* Set custom environment options from RSA authentication. */
1269 while (custom_environment) {
1270 struct envstring *ce = custom_environment;
1271 char *s = ce->s;
1272 int i;
1273 for (i = 0; s[i] != '=' && s[i]; i++);
1274 if (s[i] == '=') {
1275 s[i] = 0;
1276 child_set_env(&env, &envsize, s, s + i + 1);
1277 }
1278 custom_environment = ce->next;
1279 xfree(ce->s);
1280 xfree(ce);
1281 }
1282
1283 snprintf(buf, sizeof buf, "%.50s %d %d",
1284 get_remote_ipaddr(), get_remote_port(), get_local_port());
1285 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1286
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001287 if (s->ttyfd != -1)
1288 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1289 if (s->term)
1290 child_set_env(&env, &envsize, "TERM", s->term);
1291 if (s->display)
1292 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001293 if (original_command)
1294 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1295 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001296
1297#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001298 if ((cp = getenv("AUTHSTATE")) != NULL)
1299 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1300 if ((cp = getenv("KRB5CCNAME")) != NULL)
1301 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1302 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001303#endif
1304
1305#ifdef KRB4
1306 {
1307 extern char *ticket;
1308
1309 if (ticket)
1310 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1311 }
1312#endif /* KRB4 */
1313
1314#ifdef USE_PAM
1315 /* Pull in any environment variables that may have been set by PAM. */
1316 do_pam_environment(&env, &envsize);
1317#endif /* USE_PAM */
1318
Damien Millerb38eff82000-04-01 11:09:21 +10001319 if (xauthfile)
1320 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1321 if (auth_get_socket_name() != NULL)
1322 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1323 auth_get_socket_name());
1324
1325 /* read $HOME/.ssh/environment. */
1326 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001327 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1328 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001329 read_environment_file(&env, &envsize, buf);
1330 }
1331 if (debug_flag) {
1332 /* dump the environment */
1333 fprintf(stderr, "Environment:\n");
1334 for (i = 0; env[i]; i++)
1335 fprintf(stderr, " %.200s\n", env[i]);
1336 }
Damien Millerad833b32000-08-23 10:46:23 +10001337 /* we have to stash the hostname before we close our socket. */
1338 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001339 hostname = get_remote_name_or_ip(utmp_len,
1340 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001341 /*
1342 * Close the connection descriptors; note that this is the child, and
1343 * the server will still have the socket open, and it is important
1344 * that we do not shutdown it. Note that the descriptors cannot be
1345 * closed before building the environment, as we call
1346 * get_remote_ipaddr there.
1347 */
1348 if (packet_get_connection_in() == packet_get_connection_out())
1349 close(packet_get_connection_in());
1350 else {
1351 close(packet_get_connection_in());
1352 close(packet_get_connection_out());
1353 }
1354 /*
1355 * Close all descriptors related to channels. They will still remain
1356 * open in the parent.
1357 */
1358 /* XXX better use close-on-exec? -markus */
1359 channel_close_all();
1360
1361 /*
1362 * Close any extra file descriptors. Note that there may still be
1363 * descriptors left by system functions. They will be closed later.
1364 */
1365 endpwent();
1366
1367 /*
1368 * Close any extra open file descriptors so that we don\'t have them
1369 * hanging around in clients. Note that we want to do this after
1370 * initgroups, because at least on Solaris 2.3 it leaves file
1371 * descriptors open.
1372 */
1373 for (i = 3; i < 64; i++)
1374 close(i);
1375
1376 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001377 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001378 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1379 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001380#ifdef HAVE_LOGIN_CAP
1381 if (login_getcapbool(lc, "requirehome", 0))
1382 exit(1);
1383#endif
1384 }
Damien Millerb38eff82000-04-01 11:09:21 +10001385
1386 /*
1387 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1388 * xauth are run in the proper environment.
1389 */
1390 environ = env;
1391
1392 /*
1393 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1394 * in this order).
1395 */
1396 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001397 /* ignore _PATH_SSH_USER_RC for subsystems */
1398 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001399 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1400 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001401 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001402 fprintf(stderr, "Running %s\n", cmd);
1403 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001404 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001405 if (do_xauth)
1406 fprintf(f, "%s %s\n", s->auth_proto,
1407 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001408 pclose(f);
1409 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001410 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001411 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001412 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001413 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001414 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1415 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001416 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001417 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001418 if (do_xauth)
1419 fprintf(f, "%s %s\n", s->auth_proto,
1420 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001421 pclose(f);
1422 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001423 fprintf(stderr, "Could not run %s\n",
1424 _PATH_SSH_SYSTEM_RC);
1425 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001426 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001427 char *screen = strchr(s->display, ':');
1428
1429 if (debug_flag) {
1430 fprintf(stderr,
1431 "Running %.100s add "
1432 "%.100s %.100s %.100s\n",
1433 options.xauth_location, s->display,
1434 s->auth_proto, s->auth_data);
1435 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001436 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001437 "Adding %.*s/unix%s %s %s\n",
1438 (int)(screen - s->display),
1439 s->display, screen,
1440 s->auth_proto, s->auth_data);
1441 }
1442 snprintf(cmd, sizeof cmd, "%s -q -",
1443 options.xauth_location);
1444 f = popen(cmd, "w");
1445 if (f) {
1446 fprintf(f, "add %s %s %s\n", s->display,
1447 s->auth_proto, s->auth_data);
1448 if (screen != NULL)
1449 fprintf(f, "add %.*s/unix%s %s %s\n",
1450 (int)(screen - s->display),
1451 s->display, screen,
1452 s->auth_proto,
1453 s->auth_data);
1454 pclose(f);
1455 } else {
1456 fprintf(stderr, "Could not run %s\n",
1457 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001458 }
1459 }
Damien Millerb38eff82000-04-01 11:09:21 +10001460 /* Get the last component of the shell name. */
1461 cp = strrchr(shell, '/');
1462 if (cp)
1463 cp++;
1464 else
1465 cp = shell;
1466 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001467
1468 /* restore SIGPIPE for child */
1469 signal(SIGPIPE, SIG_DFL);
1470
Damien Millerb38eff82000-04-01 11:09:21 +10001471 /*
1472 * If we have no command, execute the shell. In this case, the shell
1473 * name to be passed in argv[0] is preceded by '-' to indicate that
1474 * this is a login shell.
1475 */
1476 if (!command) {
1477 if (!options.use_login) {
1478 char buf[256];
1479
1480 /*
1481 * Check for mail if we have a tty and it was enabled
1482 * in server options.
1483 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001484 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001485 char *mailbox;
1486 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001487
Damien Millerb38eff82000-04-01 11:09:21 +10001488 mailbox = getenv("MAIL");
1489 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001490 if (stat(mailbox, &mailstat) != 0 ||
1491 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001492 printf("No mail.\n");
1493 else if (mailstat.st_mtime < mailstat.st_atime)
1494 printf("You have mail.\n");
1495 else
1496 printf("You have new mail.\n");
1497 }
1498 }
1499 /* Start the shell. Set initial character to '-'. */
1500 buf[0] = '-';
1501 strncpy(buf + 1, cp, sizeof(buf) - 1);
1502 buf[sizeof(buf) - 1] = 0;
1503
1504 /* Execute the shell. */
1505 argv[0] = buf;
1506 argv[1] = NULL;
1507 execve(shell, argv, env);
1508
1509 /* Executing the shell failed. */
1510 perror(shell);
1511 exit(1);
1512
1513 } else {
1514 /* Launch login(1). */
1515
Damien Millerad833b32000-08-23 10:46:23 +10001516 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001517#ifdef LOGIN_NEEDS_TERM
1518 s->term? s->term : "unknown",
1519#endif
Damien Miller942da032000-08-18 13:59:06 +10001520 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001521
1522 /* Login couldn't be executed, die. */
1523
1524 perror("login");
1525 exit(1);
1526 }
1527 }
1528 /*
1529 * Execute the command using the user's shell. This uses the -c
1530 * option to execute the command.
1531 */
1532 argv[0] = (char *) cp;
1533 argv[1] = "-c";
1534 argv[2] = (char *) command;
1535 argv[3] = NULL;
1536 execve(shell, argv, env);
1537 perror(shell);
1538 exit(1);
1539}
1540
1541Session *
1542session_new(void)
1543{
1544 int i;
1545 static int did_init = 0;
1546 if (!did_init) {
1547 debug("session_new: init");
1548 for(i = 0; i < MAX_SESSIONS; i++) {
1549 sessions[i].used = 0;
1550 sessions[i].self = i;
1551 }
1552 did_init = 1;
1553 }
1554 for(i = 0; i < MAX_SESSIONS; i++) {
1555 Session *s = &sessions[i];
1556 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001557 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001558 s->chanid = -1;
1559 s->ptyfd = -1;
1560 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001561 s->used = 1;
Damien Miller15b29522000-10-14 12:33:48 +11001562 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001563 return s;
1564 }
1565 }
1566 return NULL;
1567}
1568
1569void
1570session_dump(void)
1571{
1572 int i;
1573 for(i = 0; i < MAX_SESSIONS; i++) {
1574 Session *s = &sessions[i];
1575 debug("dump: used %d session %d %p channel %d pid %d",
1576 s->used,
1577 s->self,
1578 s,
1579 s->chanid,
1580 s->pid);
1581 }
1582}
1583
Damien Millerefb4afe2000-04-12 18:45:05 +10001584int
1585session_open(int chanid)
1586{
1587 Session *s = session_new();
1588 debug("session_open: channel %d", chanid);
1589 if (s == NULL) {
1590 error("no more sessions");
1591 return 0;
1592 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001593 s->pw = auth_get_user();
1594 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001595 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001596 debug("session_open: session %d: link with channel %d", s->self, chanid);
1597 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001598 return 1;
1599}
1600
1601Session *
1602session_by_channel(int id)
1603{
1604 int i;
1605 for(i = 0; i < MAX_SESSIONS; i++) {
1606 Session *s = &sessions[i];
1607 if (s->used && s->chanid == id) {
1608 debug("session_by_channel: session %d channel %d", i, id);
1609 return s;
1610 }
1611 }
1612 debug("session_by_channel: unknown channel %d", id);
1613 session_dump();
1614 return NULL;
1615}
1616
1617Session *
1618session_by_pid(pid_t pid)
1619{
1620 int i;
1621 debug("session_by_pid: pid %d", pid);
1622 for(i = 0; i < MAX_SESSIONS; i++) {
1623 Session *s = &sessions[i];
1624 if (s->used && s->pid == pid)
1625 return s;
1626 }
1627 error("session_by_pid: unknown pid %d", pid);
1628 session_dump();
1629 return NULL;
1630}
1631
1632int
1633session_window_change_req(Session *s)
1634{
1635 s->col = packet_get_int();
1636 s->row = packet_get_int();
1637 s->xpixel = packet_get_int();
1638 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001639 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001640 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1641 return 1;
1642}
1643
1644int
1645session_pty_req(Session *s)
1646{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001647 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001648 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001649
Damien Millerf6d9e222000-06-18 14:50:44 +10001650 if (no_pty_flag)
1651 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001652 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001653 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001654 s->term = packet_get_string(&len);
1655 s->col = packet_get_int();
1656 s->row = packet_get_int();
1657 s->xpixel = packet_get_int();
1658 s->ypixel = packet_get_int();
1659
1660 if (strcmp(s->term, "") == 0) {
1661 xfree(s->term);
1662 s->term = NULL;
1663 }
1664 /* Allocate a pty and open it. */
1665 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1666 xfree(s->term);
1667 s->term = NULL;
1668 s->ptyfd = -1;
1669 s->ttyfd = -1;
1670 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001671 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001672 }
1673 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1674 /*
1675 * Add a cleanup function to clear the utmp entry and record logout
1676 * time in case we call fatal() (e.g., the connection gets closed).
1677 */
1678 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1679 pty_setowner(s->pw, s->tty);
1680 /* Get window size from the packet. */
1681 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1682
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001683 /* Get tty modes from the packet. */
1684 tty_parse_modes(s->ttyfd, &n_bytes);
1685 packet_done();
1686
Damien Millere247cc42000-05-07 12:03:14 +10001687 session_proctitle(s);
1688
Damien Millerefb4afe2000-04-12 18:45:05 +10001689 return 1;
1690}
1691
Damien Millerbd483e72000-04-30 10:00:53 +10001692int
1693session_subsystem_req(Session *s)
1694{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001695 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001696 int success = 0;
1697 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001698 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001699
1700 packet_done();
1701 log("subsystem request for %s", subsys);
1702
Damien Millerf6d9e222000-06-18 14:50:44 +10001703 for (i = 0; i < options.num_subsystems; i++) {
1704 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1705 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001706 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001707 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001708 success = 1;
1709 }
1710 }
1711
1712 if (!success)
1713 log("subsystem request for %s failed, subsystem not found", subsys);
1714
Damien Millerbd483e72000-04-30 10:00:53 +10001715 xfree(subsys);
1716 return success;
1717}
1718
1719int
1720session_x11_req(Session *s)
1721{
Ben Lindstrom768176b2001-06-09 01:29:12 +00001722 int success;
Damien Millerbd483e72000-04-30 10:00:53 +10001723
1724 s->single_connection = packet_get_char();
1725 s->auth_proto = packet_get_string(NULL);
1726 s->auth_data = packet_get_string(NULL);
1727 s->screen = packet_get_int();
1728 packet_done();
1729
Ben Lindstrom768176b2001-06-09 01:29:12 +00001730 success = session_setup_x11fwd(s);
1731 if (!success) {
Damien Millerbd483e72000-04-30 10:00:53 +10001732 xfree(s->auth_proto);
1733 xfree(s->auth_data);
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
Damien Millerf6d9e222000-06-18 14:50:44 +10001738int
1739session_shell_req(Session *s)
1740{
1741 /* if forced_command == NULL, the shell is execed */
1742 char *shell = forced_command;
1743 packet_done();
Damien Millerf6d9e222000-06-18 14:50:44 +10001744 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001745 do_exec_no_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001746 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001747 do_exec_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001748 return 1;
1749}
1750
1751int
1752session_exec_req(Session *s)
1753{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001754 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001755 char *command = packet_get_string(&len);
1756 packet_done();
1757 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001758 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001759 command = forced_command;
1760 debug("Forced command '%.500s'", forced_command);
1761 }
Damien Millerf6d9e222000-06-18 14:50:44 +10001762 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001763 do_exec_no_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001764 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001765 do_exec_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001766 if (forced_command == NULL)
1767 xfree(command);
1768 return 1;
1769}
1770
Damien Miller0bc1bd82000-11-13 22:57:25 +11001771int
1772session_auth_agent_req(Session *s)
1773{
1774 static int called = 0;
1775 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001776 if (no_agent_forwarding_flag) {
1777 debug("session_auth_agent_req: no_agent_forwarding_flag");
1778 return 0;
1779 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001780 if (called) {
1781 return 0;
1782 } else {
1783 called = 1;
1784 return auth_input_request_forwarding(s->pw);
1785 }
1786}
1787
Damien Millerefb4afe2000-04-12 18:45:05 +10001788void
1789session_input_channel_req(int id, void *arg)
1790{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001791 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001792 int reply;
1793 int success = 0;
1794 char *rtype;
1795 Session *s;
1796 Channel *c;
1797
1798 rtype = packet_get_string(&len);
1799 reply = packet_get_char();
1800
1801 s = session_by_channel(id);
1802 if (s == NULL)
1803 fatal("session_input_channel_req: channel %d: no session", id);
1804 c = channel_lookup(id);
1805 if (c == NULL)
1806 fatal("session_input_channel_req: channel %d: bad channel", id);
1807
1808 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1809 s->self, id, rtype, reply);
1810
1811 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001812 * a session is in LARVAL state until a shell, a command
1813 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001814 */
1815 if (c->type == SSH_CHANNEL_LARVAL) {
1816 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001817 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001818 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001819 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001820 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001821 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001822 } else if (strcmp(rtype, "x11-req") == 0) {
1823 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001824 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1825 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001826 } else if (strcmp(rtype, "subsystem") == 0) {
1827 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001828 }
1829 }
1830 if (strcmp(rtype, "window-change") == 0) {
1831 success = session_window_change_req(s);
1832 }
1833
1834 if (reply) {
1835 packet_start(success ?
1836 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1837 packet_put_int(c->remote_id);
1838 packet_send();
1839 }
1840 xfree(rtype);
1841}
1842
1843void
1844session_set_fds(Session *s, int fdin, int fdout, int fderr)
1845{
1846 if (!compat20)
1847 fatal("session_set_fds: called for proto != 2.0");
1848 /*
1849 * now that have a child and a pipe to the child,
1850 * we can activate our channel and register the fd's
1851 */
1852 if (s->chanid == -1)
1853 fatal("no channel for session %d", s->self);
1854 channel_set_fds(s->chanid,
1855 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001856 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1857 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001858}
1859
Damien Millerb38eff82000-04-01 11:09:21 +10001860void
1861session_pty_cleanup(Session *s)
1862{
1863 if (s == NULL || s->ttyfd == -1)
1864 return;
1865
Kevin Steves43cdef32001-02-11 14:12:08 +00001866 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001867
1868 /* Cancel the cleanup function. */
1869 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1870
1871 /* Record that the user has logged out. */
1872 record_logout(s->pid, s->tty);
1873
1874 /* Release the pseudo-tty. */
1875 pty_release(s->tty);
1876
1877 /*
1878 * Close the server side of the socket pairs. We must do this after
1879 * the pty cleanup, so that another process doesn't get this pty
1880 * while we're still cleaning up.
1881 */
1882 if (close(s->ptymaster) < 0)
1883 error("close(s->ptymaster): %s", strerror(errno));
1884}
Damien Millerefb4afe2000-04-12 18:45:05 +10001885
1886void
1887session_exit_message(Session *s, int status)
1888{
1889 Channel *c;
1890 if (s == NULL)
1891 fatal("session_close: no session");
1892 c = channel_lookup(s->chanid);
1893 if (c == NULL)
1894 fatal("session_close: session %d: no channel %d",
1895 s->self, s->chanid);
1896 debug("session_exit_message: session %d channel %d pid %d",
1897 s->self, s->chanid, s->pid);
1898
1899 if (WIFEXITED(status)) {
1900 channel_request_start(s->chanid,
1901 "exit-status", 0);
1902 packet_put_int(WEXITSTATUS(status));
1903 packet_send();
1904 } else if (WIFSIGNALED(status)) {
1905 channel_request_start(s->chanid,
1906 "exit-signal", 0);
1907 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001908#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001909 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001910#else /* WCOREDUMP */
1911 packet_put_char(0);
1912#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001913 packet_put_cstring("");
1914 packet_put_cstring("");
1915 packet_send();
1916 } else {
1917 /* Some weird exit cause. Just exit. */
1918 packet_disconnect("wait returned status %04x.", status);
1919 }
1920
1921 /* disconnect channel */
1922 debug("session_exit_message: release channel %d", s->chanid);
1923 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001924 /*
1925 * emulate a write failure with 'chan_write_failed', nobody will be
1926 * interested in data we write.
1927 * Note that we must not call 'chan_read_failed', since there could
1928 * be some more data waiting in the pipe.
1929 */
Damien Millerbd483e72000-04-30 10:00:53 +10001930 if (c->ostate != CHAN_OUTPUT_CLOSED)
1931 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001932 s->chanid = -1;
1933}
1934
1935void
1936session_free(Session *s)
1937{
1938 debug("session_free: session %d pid %d", s->self, s->pid);
1939 if (s->term)
1940 xfree(s->term);
1941 if (s->display)
1942 xfree(s->display);
1943 if (s->auth_data)
1944 xfree(s->auth_data);
1945 if (s->auth_proto)
1946 xfree(s->auth_proto);
1947 s->used = 0;
1948}
1949
1950void
1951session_close(Session *s)
1952{
1953 session_pty_cleanup(s);
1954 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001955 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001956}
1957
1958void
1959session_close_by_pid(pid_t pid, int status)
1960{
1961 Session *s = session_by_pid(pid);
1962 if (s == NULL) {
1963 debug("session_close_by_pid: no session for pid %d", s->pid);
1964 return;
1965 }
1966 if (s->chanid != -1)
1967 session_exit_message(s, status);
1968 session_close(s);
1969}
1970
1971/*
1972 * this is called when a channel dies before
1973 * the session 'child' itself dies
1974 */
1975void
1976session_close_by_channel(int id, void *arg)
1977{
1978 Session *s = session_by_channel(id);
1979 if (s == NULL) {
1980 debug("session_close_by_channel: no session for channel %d", id);
1981 return;
1982 }
1983 /* disconnect channel */
1984 channel_cancel_cleanup(s->chanid);
1985 s->chanid = -1;
1986
1987 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1988 if (s->pid == 0) {
1989 /* close session immediately */
1990 session_close(s);
1991 } else {
1992 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001993 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001994 error("session_close_by_channel: kill %d: %s",
1995 s->pid, strerror(errno));
1996 }
1997}
1998
Damien Millere247cc42000-05-07 12:03:14 +10001999char *
2000session_tty_list(void)
2001{
2002 static char buf[1024];
2003 int i;
2004 buf[0] = '\0';
2005 for(i = 0; i < MAX_SESSIONS; i++) {
2006 Session *s = &sessions[i];
2007 if (s->used && s->ttyfd != -1) {
2008 if (buf[0] != '\0')
2009 strlcat(buf, ",", sizeof buf);
2010 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2011 }
2012 }
2013 if (buf[0] == '\0')
2014 strlcpy(buf, "notty", sizeof buf);
2015 return buf;
2016}
2017
2018void
2019session_proctitle(Session *s)
2020{
2021 if (s->pw == NULL)
2022 error("no user for session %d", s->self);
2023 else
2024 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2025}
2026
Ben Lindstrom768176b2001-06-09 01:29:12 +00002027int
2028session_setup_x11fwd(Session *s)
2029{
2030 int fd;
2031 struct stat st;
2032
2033 if (no_x11_forwarding_flag) {
2034 packet_send_debug("X11 forwarding disabled in user configuration file.");
2035 return 0;
2036 }
2037 if (!options.x11_forwarding) {
2038 debug("X11 forwarding disabled in server configuration file.");
2039 return 0;
2040 }
2041 if (!options.xauth_location ||
2042 (stat(options.xauth_location, &st) == -1)) {
2043 packet_send_debug("No xauth program; cannot forward with spoofing.");
2044 return 0;
2045 }
Ben Lindstromcb3929d2001-06-09 01:34:15 +00002046 if (s->display != NULL || xauthfile != NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002047 debug("X11 display already set.");
2048 return 0;
2049 }
2050 xauthfile = xmalloc(MAXPATHLEN);
2051 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
2052 temporarily_use_uid(s->pw);
2053 if (mkdtemp(xauthfile) == NULL) {
Ben Lindstrom768176b2001-06-09 01:29:12 +00002054 error("private X11 dir: mkdtemp %s failed: %s",
2055 xauthfile, strerror(errno));
Ben Lindstrom4d3f2272001-06-09 01:44:07 +00002056 restore_uid();
Ben Lindstrom768176b2001-06-09 01:29:12 +00002057 xfree(xauthfile);
2058 xauthfile = NULL;
2059 return 0;
2060 }
2061 strlcat(xauthfile, "/cookies", MAXPATHLEN);
2062 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
2063 if (fd >= 0)
2064 close(fd);
2065 restore_uid();
2066 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
2067 if (s->display == NULL) {
2068 xauthfile_cleanup_proc(s->pw);
2069 return 0;
2070 }
2071 fatal_add_cleanup(xauthfile_cleanup_proc, s->pw);
2072 return 1;
2073}
2074
Damien Millerefb4afe2000-04-12 18:45:05 +10002075void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002076do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002077{
Damien Millerefb4afe2000-04-12 18:45:05 +10002078 server_loop2();
2079}