blob: 9aef6b0225a2cbf87b9dd5d1c64568a4917c18ca [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 Lindstrom7d68fbf2001-06-05 19:29:20 +000036RCSID("$OpenBSD: session.c,v 1.76 2001/05/19 00:36:40 djm 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"
48#include "channels.h"
49#include "nchan.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100051#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100052#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "pathnames.h"
54#include "log.h"
55#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000056#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "serverloop.h"
58#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000059#include "session.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060
Damien Miller91606b12000-06-28 08:22:29 +100061#ifdef WITH_IRIX_PROJECT
62#include <proj.h>
63#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000064#ifdef WITH_IRIX_JOBS
65#include <sys/resource.h>
Kevin Stevesef4eea92001-02-05 12:42:17 +000066#endif
Ben Lindstrom49a79c02000-11-17 03:47:20 +000067#ifdef WITH_IRIX_AUDIT
68#include <sat.h>
69#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100070
Damien Miller37023962000-07-11 17:31:38 +100071#if defined(HAVE_USERSEC_H)
72#include <usersec.h>
73#endif
74
Damien Millerbac2d8a2000-09-05 16:13:06 +110075#ifdef HAVE_CYGWIN
76#include <windows.h>
77#include <sys/cygwin.h>
78#define is_winnt (GetVersion() < 0x80000000)
79#endif
80
Damien Millerd17b8d52000-08-09 14:42:28 +100081/* AIX limits */
82#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100083# define S_UFSIZE_HARD S_UFSIZE "_hard"
84# define S_UCPU_HARD S_UCPU "_hard"
85# define S_UDATA_HARD S_UDATA "_hard"
86# define S_USTACK_HARD S_USTACK "_hard"
87# define S_URSS_HARD S_URSS "_hard"
88# define S_UCORE_HARD S_UCORE "_hard"
89# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100090#endif
91
Damien Miller168a7002001-03-17 10:29:50 +110092#ifdef _AIX
93# include <uinfo.h>
94#endif
95
Damien Millerb38eff82000-04-01 11:09:21 +100096/* types */
97
98#define TTYSZ 64
99typedef struct Session Session;
100struct Session {
101 int used;
102 int self;
103 struct passwd *pw;
104 pid_t pid;
105 /* tty */
106 char *term;
107 int ptyfd, ttyfd, ptymaster;
108 int row, col, xpixel, ypixel;
109 char tty[TTYSZ];
110 /* X11 */
111 char *display;
112 int screen;
113 char *auth_proto;
114 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000115 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000116 /* proto 2 */
117 int chanid;
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000118 int is_subsystem;
Damien Millerb38eff82000-04-01 11:09:21 +1000119};
120
121/* func */
122
123Session *session_new(void);
124void session_set_fds(Session *s, int fdin, int fdout, int fderr);
125void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000126void session_proctitle(Session *s);
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000127void do_exec_pty(Session *s, const char *command);
128void do_exec_no_pty(Session *s, const char *command);
Damien Miller69b69aa2000-10-28 14:19:58 +1100129void do_login(Session *s, const char *command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000130#ifdef LOGIN_NEEDS_UTMPX
131void do_pre_login(Session *s);
132#endif
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000133void do_child(Session *s, const char *command);
Damien Millercf205e82001-04-16 18:29:15 +1000134void do_motd(void);
135int check_quietlogin(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000136
Ben Lindstromb31783d2001-03-22 02:02:12 +0000137void do_authenticated1(Authctxt *authctxt);
138void do_authenticated2(Authctxt *authctxt);
139
Damien Millerb38eff82000-04-01 11:09:21 +1000140/* import */
141extern ServerOptions options;
142extern char *__progname;
143extern int log_stderr;
144extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000145extern u_int utmp_len;
Damien Miller37023962000-07-11 17:31:38 +1000146extern int startup_pipe;
Ben Lindstrom005dd222001-04-18 15:29:33 +0000147extern void destroy_sensitive_data(void);
Damien Miller37023962000-07-11 17:31:38 +1000148
Damien Millerb38eff82000-04-01 11:09:21 +1000149/* Local Xauthority file. */
150static char *xauthfile;
151
Damien Miller7b28dc52000-09-05 13:34:53 +1100152/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100154
Damien Millerb38eff82000-04-01 11:09:21 +1000155/* data */
156#define MAX_SESSIONS 10
157Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100158
Damien Millerd2c208a2000-05-17 22:00:02 +1000159#ifdef WITH_AIXAUTHENTICATE
160/* AIX's lastlogin message, set in auth1.c */
161char *aixloginmsg;
162#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000163
Damien Millerad833b32000-08-23 10:46:23 +1000164#ifdef HAVE_LOGIN_CAP
165static login_cap_t *lc;
166#endif
167
Ben Lindstromb31783d2001-03-22 02:02:12 +0000168void
169do_authenticated(Authctxt *authctxt)
170{
171 /*
172 * Cancel the alarm we set to limit the time taken for
173 * authentication.
174 */
175 alarm(0);
176 if (startup_pipe != -1) {
177 close(startup_pipe);
178 startup_pipe = -1;
179 }
180#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
181 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
182 error("unable to get login class");
183 return;
184 }
Ben Lindstrom005dd222001-04-18 15:29:33 +0000185#ifdef BSD_AUTH
186 if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
187 packet_disconnect("Approval failure for %s",
188 authctxt->pw->pw_name);
189 }
190#endif
Ben Lindstromb31783d2001-03-22 02:02:12 +0000191#endif
192 /* setup the channel layer */
193 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
194 channel_permit_all_opens();
195
196 if (compat20)
197 do_authenticated2(authctxt);
198 else
199 do_authenticated1(authctxt);
200}
201
Damien Millerb38eff82000-04-01 11:09:21 +1000202/*
203 * Remove local Xauthority file.
204 */
205void
206xauthfile_cleanup_proc(void *ignore)
207{
208 debug("xauthfile_cleanup_proc called");
209
210 if (xauthfile != NULL) {
211 char *p;
212 unlink(xauthfile);
213 p = strrchr(xauthfile, '/');
214 if (p != NULL) {
215 *p = '\0';
216 rmdir(xauthfile);
217 }
218 xfree(xauthfile);
219 xauthfile = NULL;
220 }
221}
222
223/*
224 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
225 * dropped connection).
226 */
Damien Miller4af51302000-04-16 11:18:38 +1000227void
Damien Millerb38eff82000-04-01 11:09:21 +1000228pty_cleanup_proc(void *session)
229{
230 Session *s=session;
231 if (s == NULL)
232 fatal("pty_cleanup_proc: no session");
233 debug("pty_cleanup_proc: %s", s->tty);
234
235 if (s->pid != 0) {
236 /* Record that the user has logged out. */
237 record_logout(s->pid, s->tty);
238 }
239
240 /* Release the pseudo-tty. */
241 pty_release(s->tty);
242}
243
244/*
245 * Prepares for an interactive session. This is called after the user has
246 * been successfully authenticated. During this message exchange, pseudo
247 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
248 * are requested, etc.
249 */
Damien Miller4af51302000-04-16 11:18:38 +1000250void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000251do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000252{
253 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000254 char *command;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000255 int success, type, fd, n_bytes, plen, screen_flag, have_pty = 0;
256 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000257 u_int proto_len, data_len, dlen;
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +0000258 struct stat st;
Damien Millerb38eff82000-04-01 11:09:21 +1000259
260 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000261 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000262
Damien Millerb38eff82000-04-01 11:09:21 +1000263 /*
264 * We stay in this loop until the client requests to execute a shell
265 * or a command.
266 */
267 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000268 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000269
270 /* Get a packet from the client. */
271 type = packet_read(&plen);
272
273 /* Process the packet. */
274 switch (type) {
275 case SSH_CMSG_REQUEST_COMPRESSION:
276 packet_integrity_check(plen, 4, type);
277 compression_level = packet_get_int();
278 if (compression_level < 1 || compression_level > 9) {
279 packet_send_debug("Received illegal compression level %d.",
280 compression_level);
281 break;
282 }
283 /* Enable compression after we have responded with SUCCESS. */
284 enable_compression_after_reply = 1;
285 success = 1;
286 break;
287
288 case SSH_CMSG_REQUEST_PTY:
289 if (no_pty_flag) {
290 debug("Allocating a pty not permitted for this authentication.");
291 break;
292 }
293 if (have_pty)
294 packet_disconnect("Protocol error: you already have a pty.");
295
296 debug("Allocating pty.");
297
298 /* Allocate a pty and open it. */
299 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
300 sizeof(s->tty))) {
301 error("Failed to allocate pty.");
302 break;
303 }
304 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000305 pty_setowner(s->pw, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000306
307 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
308 s->term = packet_get_string(&dlen);
309 packet_integrity_check(dlen, strlen(s->term), type);
310 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
311 /* Remaining bytes */
312 n_bytes = plen - (4 + dlen + 4 * 4);
313
314 if (strcmp(s->term, "") == 0) {
315 xfree(s->term);
316 s->term = NULL;
317 }
318 /* Get window size from the packet. */
319 s->row = packet_get_int();
320 s->col = packet_get_int();
321 s->xpixel = packet_get_int();
322 s->ypixel = packet_get_int();
323 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
324
325 /* Get tty modes from the packet. */
326 tty_parse_modes(s->ttyfd, &n_bytes);
327 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
328
Damien Millere247cc42000-05-07 12:03:14 +1000329 session_proctitle(s);
330
Damien Millerb38eff82000-04-01 11:09:21 +1000331 /* Indicate that we now have a pty. */
332 success = 1;
333 have_pty = 1;
334 break;
335
336 case SSH_CMSG_X11_REQUEST_FORWARDING:
337 if (!options.x11_forwarding) {
338 packet_send_debug("X11 forwarding disabled in server configuration file.");
339 break;
340 }
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +0000341 if (!options.xauth_location ||
342 (stat(options.xauth_location, &st) == -1)) {
Damien Miller0c043c12000-06-07 21:22:38 +1000343 packet_send_debug("No xauth program; cannot forward with spoofing.");
344 break;
345 }
Damien Millerb38eff82000-04-01 11:09:21 +1000346 if (no_x11_forwarding_flag) {
347 packet_send_debug("X11 forwarding not permitted for this authentication.");
348 break;
349 }
350 debug("Received request for X11 forwarding with auth spoofing.");
351 if (s->display != NULL)
352 packet_disconnect("Protocol error: X11 display already set.");
353
354 s->auth_proto = packet_get_string(&proto_len);
355 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000356
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000357 screen_flag = packet_get_protocol_flags() &
358 SSH_PROTOFLAG_SCREEN_NUMBER;
359 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
360
361 if (packet_remaining() == 4) {
362 if (!screen_flag)
363 debug2("Buggy client: "
364 "X11 screen flag missing");
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000365 packet_integrity_check(plen,
366 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000367 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000368 } else {
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000369 packet_integrity_check(plen,
370 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000371 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000372 }
Damien Millerb38eff82000-04-01 11:09:21 +1000373 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
374
375 if (s->display == NULL)
376 break;
377
378 /* Setup to always have a local .Xauthority. */
379 xauthfile = xmalloc(MAXPATHLEN);
380 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000381 temporarily_use_uid(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000382 if (mkdtemp(xauthfile) == NULL) {
383 restore_uid();
384 error("private X11 dir: mkdtemp %s failed: %s",
385 xauthfile, strerror(errno));
386 xfree(xauthfile);
387 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000388 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000389 break;
390 }
391 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100392 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
393 if (fd >= 0)
394 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000395 restore_uid();
396 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
397 success = 1;
398 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000399
400 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
401 if (no_agent_forwarding_flag || compat13) {
402 debug("Authentication agent forwarding not permitted for this authentication.");
403 break;
404 }
405 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000406 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000407 break;
408
409 case SSH_CMSG_PORT_FORWARD_REQUEST:
410 if (no_port_forwarding_flag) {
411 debug("Port forwarding not permitted for this authentication.");
412 break;
413 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100414 if (!options.allow_tcp_forwarding) {
415 debug("Port forwarding not permitted.");
416 break;
417 }
Damien Millerb38eff82000-04-01 11:09:21 +1000418 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000419 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000420 success = 1;
421 break;
422
423 case SSH_CMSG_MAX_PACKET_SIZE:
424 if (packet_set_maxsize(packet_get_int()) > 0)
425 success = 1;
426 break;
427
428 case SSH_CMSG_EXEC_SHELL:
429 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000430 if (type == SSH_CMSG_EXEC_CMD) {
431 command = packet_get_string(&dlen);
432 debug("Exec command '%.500s'", command);
433 packet_integrity_check(plen, 4 + dlen, type);
434 } else {
435 command = NULL;
436 packet_integrity_check(plen, 0, type);
437 }
438 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100439 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000440 command = forced_command;
441 debug("Forced command '%.500s'", forced_command);
442 }
443 if (have_pty)
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000444 do_exec_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000445 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000446 do_exec_no_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000447
448 if (command != NULL)
449 xfree(command);
450 /* Cleanup user's local Xauthority file. */
451 if (xauthfile)
452 xauthfile_cleanup_proc(NULL);
453 return;
454
455 default:
456 /*
457 * Any unknown messages in this phase are ignored,
458 * and a failure message is returned.
459 */
460 log("Unknown packet type received after authentication: %d", type);
461 }
462 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
463 packet_send();
464 packet_write_wait();
465
466 /* Enable compression now that we have replied if appropriate. */
467 if (enable_compression_after_reply) {
468 enable_compression_after_reply = 0;
469 packet_start_compression(compression_level);
470 }
471 }
472}
473
474/*
475 * This is called to fork and execute a command when we have no tty. This
476 * will call do_child from the child, and server_loop from the parent after
477 * setting up file descriptors and such.
478 */
Damien Miller4af51302000-04-16 11:18:38 +1000479void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000480do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000481{
482 int pid;
483
484#ifdef USE_PIPES
485 int pin[2], pout[2], perr[2];
486 /* Allocate pipes for communicating with the program. */
487 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
488 packet_disconnect("Could not create pipes: %.100s",
489 strerror(errno));
490#else /* USE_PIPES */
491 int inout[2], err[2];
492 /* Uses socket pairs to communicate with the program. */
493 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
494 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
495 packet_disconnect("Could not create socket pairs: %.100s",
496 strerror(errno));
497#endif /* USE_PIPES */
498 if (s == NULL)
499 fatal("do_exec_no_pty: no session");
500
Damien Millere247cc42000-05-07 12:03:14 +1000501 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000502
Damien Millerc5946332001-02-28 11:46:11 +1100503#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000504 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000505#endif /* USE_PAM */
506
Damien Millerb38eff82000-04-01 11:09:21 +1000507 /* Fork the child. */
508 if ((pid = fork()) == 0) {
509 /* Child. Reinitialize the log since the pid has changed. */
510 log_init(__progname, options.log_level, options.log_facility, log_stderr);
511
512 /*
513 * Create a new session and process group since the 4.4BSD
514 * setlogin() affects the entire process group.
515 */
516 if (setsid() < 0)
517 error("setsid failed: %.100s", strerror(errno));
518
519#ifdef USE_PIPES
520 /*
521 * Redirect stdin. We close the parent side of the socket
522 * pair, and make the child side the standard input.
523 */
524 close(pin[1]);
525 if (dup2(pin[0], 0) < 0)
526 perror("dup2 stdin");
527 close(pin[0]);
528
529 /* Redirect stdout. */
530 close(pout[0]);
531 if (dup2(pout[1], 1) < 0)
532 perror("dup2 stdout");
533 close(pout[1]);
534
535 /* Redirect stderr. */
536 close(perr[0]);
537 if (dup2(perr[1], 2) < 0)
538 perror("dup2 stderr");
539 close(perr[1]);
540#else /* USE_PIPES */
541 /*
542 * Redirect stdin, stdout, and stderr. Stdin and stdout will
543 * use the same socket, as some programs (particularly rdist)
544 * seem to depend on it.
545 */
546 close(inout[1]);
547 close(err[1]);
548 if (dup2(inout[0], 0) < 0) /* stdin */
549 perror("dup2 stdin");
550 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
551 perror("dup2 stdout");
552 if (dup2(err[0], 2) < 0) /* stderr */
553 perror("dup2 stderr");
554#endif /* USE_PIPES */
555
556 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000557 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000558 /* NOTREACHED */
559 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100560#ifdef HAVE_CYGWIN
561 if (is_winnt)
562 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
563#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000564 if (pid < 0)
565 packet_disconnect("fork failed: %.100s", strerror(errno));
566 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000567 /* Set interactive/non-interactive mode. */
568 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000569#ifdef USE_PIPES
570 /* We are the parent. Close the child sides of the pipes. */
571 close(pin[0]);
572 close(pout[1]);
573 close(perr[1]);
574
Damien Millerefb4afe2000-04-12 18:45:05 +1000575 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000576 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000577 } else {
578 /* Enter the interactive session. */
579 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000580 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000581 }
Damien Millerb38eff82000-04-01 11:09:21 +1000582#else /* USE_PIPES */
583 /* We are the parent. Close the child sides of the socket pairs. */
584 close(inout[0]);
585 close(err[0]);
586
587 /*
588 * Enter the interactive session. Note: server_loop must be able to
589 * handle the case that fdin and fdout are the same.
590 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000591 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000592 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000593 } else {
594 server_loop(pid, inout[1], inout[1], err[1]);
595 /* server_loop has closed inout[1] and err[1]. */
596 }
Damien Millerb38eff82000-04-01 11:09:21 +1000597#endif /* USE_PIPES */
598}
599
600/*
601 * This is called to fork and execute a command when we have a tty. This
602 * will call do_child from the child, and server_loop from the parent after
603 * setting up file descriptors, controlling tty, updating wtmp, utmp,
604 * lastlog, and other such operations.
605 */
Damien Miller4af51302000-04-16 11:18:38 +1000606void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000607do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000608{
Damien Millerb38eff82000-04-01 11:09:21 +1000609 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000610 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000611
612 if (s == NULL)
613 fatal("do_exec_pty: no session");
614 ptyfd = s->ptyfd;
615 ttyfd = s->ttyfd;
616
Damien Millerc5946332001-02-28 11:46:11 +1100617#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000618 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000619 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100620#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000621
Damien Millerb38eff82000-04-01 11:09:21 +1000622 /* Fork the child. */
623 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000624 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000625 log_init(__progname, options.log_level, options.log_facility, log_stderr);
626
627 /* Close the master side of the pseudo tty. */
628 close(ptyfd);
629
630 /* Make the pseudo tty our controlling tty. */
631 pty_make_controlling_tty(&ttyfd, s->tty);
632
633 /* Redirect stdin from the pseudo tty. */
634 if (dup2(ttyfd, fileno(stdin)) < 0)
635 error("dup2 stdin failed: %.100s", strerror(errno));
636
637 /* Redirect stdout to the pseudo tty. */
638 if (dup2(ttyfd, fileno(stdout)) < 0)
639 error("dup2 stdin failed: %.100s", strerror(errno));
640
641 /* Redirect stderr to the pseudo tty. */
642 if (dup2(ttyfd, fileno(stderr)) < 0)
643 error("dup2 stdin failed: %.100s", strerror(errno));
644
645 /* Close the extra descriptor for the pseudo tty. */
646 close(ttyfd);
647
Damien Miller942da032000-08-18 13:59:06 +1000648 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000649#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100650 if (!(options.use_login && command == NULL))
651 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000652# ifdef LOGIN_NEEDS_UTMPX
653 else
654 do_pre_login(s);
655# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000656#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000657
Damien Millerb38eff82000-04-01 11:09:21 +1000658 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000659 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000660 /* NOTREACHED */
661 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100662#ifdef HAVE_CYGWIN
663 if (is_winnt)
664 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
665#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000666 if (pid < 0)
667 packet_disconnect("fork failed: %.100s", strerror(errno));
668 s->pid = pid;
669
670 /* Parent. Close the slave side of the pseudo tty. */
671 close(ttyfd);
672
673 /*
674 * Create another descriptor of the pty master side for use as the
675 * standard input. We could use the original descriptor, but this
676 * simplifies code in server_loop. The descriptor is bidirectional.
677 */
678 fdout = dup(ptyfd);
679 if (fdout < 0)
680 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
681
682 /* we keep a reference to the pty master */
683 ptymaster = dup(ptyfd);
684 if (ptymaster < 0)
685 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
686 s->ptymaster = ptymaster;
687
688 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000689 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000690 if (compat20) {
691 session_set_fds(s, ptyfd, fdout, -1);
692 } else {
693 server_loop(pid, ptyfd, fdout, -1);
694 /* server_loop _has_ closed ptyfd and fdout. */
695 session_pty_cleanup(s);
696 }
Damien Millerb38eff82000-04-01 11:09:21 +1000697}
698
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000699#ifdef LOGIN_NEEDS_UTMPX
700void
701do_pre_login(Session *s)
702{
703 socklen_t fromlen;
704 struct sockaddr_storage from;
705 pid_t pid = getpid();
706
707 /*
708 * Get IP address of client. If the connection is not a socket, let
709 * the address be 0.0.0.0.
710 */
711 memset(&from, 0, sizeof(from));
712 if (packet_connection_is_on_socket()) {
713 fromlen = sizeof(from);
714 if (getpeername(packet_get_connection_in(),
715 (struct sockaddr *) & from, &fromlen) < 0) {
716 debug("getpeername: %.100s", strerror(errno));
717 fatal_cleanup();
718 }
719 }
720
721 record_utmp_only(pid, s->tty, s->pw->pw_name,
722 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
723 (struct sockaddr *)&from);
724}
725#endif
726
Damien Miller942da032000-08-18 13:59:06 +1000727/* administrative, login(1)-like work */
728void
Damien Miller69b69aa2000-10-28 14:19:58 +1100729do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000730{
Damien Miller942da032000-08-18 13:59:06 +1000731 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100732 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000733 socklen_t fromlen;
734 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000735 time_t last_login_time;
736 struct passwd * pw = s->pw;
737 pid_t pid = getpid();
738
739 /*
740 * Get IP address of client. If the connection is not a socket, let
741 * the address be 0.0.0.0.
742 */
743 memset(&from, 0, sizeof(from));
744 if (packet_connection_is_on_socket()) {
745 fromlen = sizeof(from);
746 if (getpeername(packet_get_connection_in(),
747 (struct sockaddr *) & from, &fromlen) < 0) {
748 debug("getpeername: %.100s", strerror(errno));
749 fatal_cleanup();
750 }
751 }
752
Damien Miller7b28dc52000-09-05 13:34:53 +1100753 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000754 if (options.print_lastlog) {
755 hostname[0] = '\0';
756 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
757 hostname, sizeof(hostname));
758 }
Damien Millere4340be2000-09-16 13:29:08 +1100759
Damien Miller942da032000-08-18 13:59:06 +1000760 /* Record that there was a login on that tty from the remote host. */
761 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000762 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
763 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000764
Kevin Steves092f2ef2000-10-14 13:36:13 +0000765#ifdef USE_PAM
766 /*
767 * If password change is needed, do it now.
768 * This needs to occur before the ~/.hushlogin check.
769 */
Damien Miller646aa602001-02-15 11:51:32 +1100770 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000771 print_pam_messages();
772 do_pam_chauthtok();
773 }
774#endif
775
Damien Millercf205e82001-04-16 18:29:15 +1000776 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000777 return;
778
779#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100780 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000781 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000782#endif /* USE_PAM */
783#ifdef WITH_AIXAUTHENTICATE
784 if (aixloginmsg && *aixloginmsg)
785 printf("%s\n", aixloginmsg);
786#endif /* WITH_AIXAUTHENTICATE */
787
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000788 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000789 time_string = ctime(&last_login_time);
790 if (strchr(time_string, '\n'))
791 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000792 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000793 printf("Last login: %s\r\n", time_string);
794 else
Damien Millere4340be2000-09-16 13:29:08 +1100795 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000796 }
Damien Millercf205e82001-04-16 18:29:15 +1000797
798 do_motd();
799}
800
801/*
802 * Display the message of the day.
803 */
804void
805do_motd(void)
806{
807 FILE *f;
808 char buf[256];
809
Damien Miller942da032000-08-18 13:59:06 +1000810 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000811#ifdef HAVE_LOGIN_CAP
812 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
813 "/etc/motd"), "r");
814#else
Damien Miller942da032000-08-18 13:59:06 +1000815 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000816#endif
Damien Miller942da032000-08-18 13:59:06 +1000817 if (f) {
818 while (fgets(buf, sizeof(buf), f))
819 fputs(buf, stdout);
820 fclose(f);
821 }
822 }
823}
824
Damien Millercf205e82001-04-16 18:29:15 +1000825
826/*
827 * Check for quiet login, either .hushlogin or command given.
828 */
829int
830check_quietlogin(Session *s, const char *command)
831{
832 char buf[256];
833 struct passwd * pw = s->pw;
834 struct stat st;
835
836 /* Return 1 if .hushlogin exists or a command given. */
837 if (command != NULL)
838 return 1;
839 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
840#ifdef HAVE_LOGIN_CAP
841 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
842 return 1;
843#else
844 if (stat(buf, &st) >= 0)
845 return 1;
846#endif
847 return 0;
848}
849
Damien Millerb38eff82000-04-01 11:09:21 +1000850/*
851 * Sets the value of the given variable in the environment. If the variable
852 * already exists, its value is overriden.
853 */
Damien Miller4af51302000-04-16 11:18:38 +1000854void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000855child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000856 const char *value)
857{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000858 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000859 char **env;
860
861 /*
862 * Find the slot where the value should be stored. If the variable
863 * already exists, we reuse the slot; otherwise we append a new slot
864 * at the end of the array, expanding if necessary.
865 */
866 env = *envp;
867 namelen = strlen(name);
868 for (i = 0; env[i]; i++)
869 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
870 break;
871 if (env[i]) {
872 /* Reuse the slot. */
873 xfree(env[i]);
874 } else {
875 /* New variable. Expand if necessary. */
876 if (i >= (*envsizep) - 1) {
877 (*envsizep) += 50;
878 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
879 }
880 /* Need to set the NULL pointer at end of array beyond the new slot. */
881 env[i + 1] = NULL;
882 }
883
884 /* Allocate space and format the variable in the appropriate slot. */
885 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
886 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
887}
888
889/*
890 * Reads environment variables from the given file and adds/overrides them
891 * into the environment. If the file does not exist, this does nothing.
892 * Otherwise, it must consist of empty lines, comments (line starts with '#')
893 * and assignments of the form name=value. No other forms are allowed.
894 */
Damien Miller4af51302000-04-16 11:18:38 +1000895void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000896read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000897 const char *filename)
898{
899 FILE *f;
900 char buf[4096];
901 char *cp, *value;
902
903 f = fopen(filename, "r");
904 if (!f)
905 return;
906
907 while (fgets(buf, sizeof(buf), f)) {
908 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
909 ;
910 if (!*cp || *cp == '#' || *cp == '\n')
911 continue;
912 if (strchr(cp, '\n'))
913 *strchr(cp, '\n') = '\0';
914 value = strchr(cp, '=');
915 if (value == NULL) {
916 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
917 continue;
918 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000919 /*
920 * Replace the equals sign by nul, and advance value to
921 * the value string.
922 */
Damien Millerb38eff82000-04-01 11:09:21 +1000923 *value = '\0';
924 value++;
925 child_set_env(env, envsize, cp, value);
926 }
927 fclose(f);
928}
929
930#ifdef USE_PAM
931/*
932 * Sets any environment variables which have been specified by PAM
933 */
934void do_pam_environment(char ***env, int *envsize)
935{
936 char *equals, var_name[512], var_val[512];
937 char **pam_env;
938 int i;
939
940 if ((pam_env = fetch_pam_environment()) == NULL)
941 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000942
Damien Millerb38eff82000-04-01 11:09:21 +1000943 for(i = 0; pam_env[i] != NULL; i++) {
944 if ((equals = strstr(pam_env[i], "=")) == NULL)
945 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000946
Damien Millerb38eff82000-04-01 11:09:21 +1000947 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
948 memset(var_name, '\0', sizeof(var_name));
949 memset(var_val, '\0', sizeof(var_val));
950
951 strncpy(var_name, pam_env[i], equals - pam_env[i]);
952 strcpy(var_val, equals + 1);
953
Damien Millercb5e44a2000-09-29 12:12:36 +1100954 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000955
956 child_set_env(env, envsize, var_name, var_val);
957 }
958 }
959}
960#endif /* USE_PAM */
961
Damien Millercb5e44a2000-09-29 12:12:36 +1100962#ifdef HAVE_CYGWIN
963void copy_environment(char ***env, int *envsize)
964{
965 char *equals, var_name[512], var_val[512];
966 int i;
967
968 for(i = 0; environ[i] != NULL; i++) {
969 if ((equals = strstr(environ[i], "=")) == NULL)
970 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000971
Damien Millercb5e44a2000-09-29 12:12:36 +1100972 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
973 memset(var_name, '\0', sizeof(var_name));
974 memset(var_val, '\0', sizeof(var_val));
975
976 strncpy(var_name, environ[i], equals - environ[i]);
977 strcpy(var_val, equals + 1);
978
979 debug3("Copy environment: %s=%s", var_name, var_val);
980
981 child_set_env(env, envsize, var_name, var_val);
982 }
983 }
984}
985#endif
986
Damien Miller5fc85652000-07-09 23:53:07 +1000987#if defined(HAVE_GETUSERATTR)
988/*
989 * AIX-specific login initialisation
990 */
991void set_limit(char *user, char *soft, char *hard, int resource, int mult)
992{
993 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000994 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000995
996 getrlimit(resource, &rlim);
997
Damien Miller65964d62000-07-11 09:16:22 +1000998 slim = 0;
999 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
1000 if (slim < 0) {
1001 rlim.rlim_cur = RLIM_INFINITY;
1002 } else if (slim != 0) {
1003 /* See the wackiness below */
1004 if (rlim.rlim_cur == slim * mult)
1005 slim = 0;
1006 else
1007 rlim.rlim_cur = slim * mult;
1008 }
1009 }
Damien Miller5fc85652000-07-09 23:53:07 +10001010
Damien Miller65964d62000-07-11 09:16:22 +10001011 hlim = 0;
1012 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
1013 if (hlim < 0) {
1014 rlim.rlim_max = RLIM_INFINITY;
1015 } else if (hlim != 0) {
1016 rlim.rlim_max = hlim * mult;
1017 }
1018 }
Damien Miller5fc85652000-07-09 23:53:07 +10001019
Damien Miller65964d62000-07-11 09:16:22 +10001020 /*
1021 * XXX For cpu and fsize the soft limit is set to the hard limit
1022 * if the hard limit is left at its default value and the soft limit
1023 * is changed from its default value, either by requesting it
1024 * (slim == 0) or by setting it to the current default. At least
1025 * that's how rlogind does it. If you're confused you're not alone.
1026 * Bug or feature? AIX 4.3.1.2
1027 */
1028 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
1029 && hlim == 0 && slim != 0)
1030 rlim.rlim_max = rlim.rlim_cur;
1031 /* A specified hard limit limits the soft limit */
1032 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
1033 rlim.rlim_cur = rlim.rlim_max;
1034 /* A soft limit can increase a hard limit */
1035 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +10001036 rlim.rlim_max = rlim.rlim_cur;
1037
1038 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +10001039 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +10001040}
1041
1042void set_limits_from_userattr(char *user)
1043{
1044 int mask;
1045 char buf[16];
1046
1047 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
1048 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
1049 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1050 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1051 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1052 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1053#if defined(S_UNOFILE)
1054 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1055#endif
1056
1057 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1058 /* Convert decimal to octal */
1059 (void) snprintf(buf, sizeof(buf), "%d", mask);
1060 if (sscanf(buf, "%o", &mask) == 1)
1061 umask(mask);
1062 }
1063}
1064#endif /* defined(HAVE_GETUSERATTR) */
1065
Damien Millerb38eff82000-04-01 11:09:21 +10001066/*
1067 * Performs common processing for the child, such as setting up the
1068 * environment, closing extra file descriptors, setting the user and group
1069 * ids, and executing the command or shell.
1070 */
Damien Miller4af51302000-04-16 11:18:38 +10001071void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001072do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001073{
Damien Miller7b28dc52000-09-05 13:34:53 +11001074 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001075 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001076 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001077 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001078 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001079 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001080 char **env;
1081 extern char **environ;
1082 struct stat st;
1083 char *argv[10];
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001084 int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
Damien Miller91606b12000-06-28 08:22:29 +10001085#ifdef WITH_IRIX_PROJECT
1086 prid_t projid;
1087#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001088#ifdef WITH_IRIX_JOBS
1089 jid_t jid = 0;
1090#else
1091#ifdef WITH_IRIX_ARRAY
1092 int jid = 0;
1093#endif /* WITH_IRIX_ARRAY */
1094#endif /* WITH_IRIX_JOBS */
1095
Ben Lindstrom005dd222001-04-18 15:29:33 +00001096 /* remove hostkey from the child's memory */
1097 destroy_sensitive_data();
1098
Damien Millerd3a18572000-06-07 19:55:44 +10001099 /* login(1) is only called if we execute the login shell */
1100 if (options.use_login && command != NULL)
1101 options.use_login = 0;
1102
Damien Miller364a9bd2001-04-16 18:37:05 +10001103#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001104 if (!options.use_login) {
1105# ifdef HAVE_LOGIN_CAP
1106 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1107 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1108 _PATH_NOLOGIN), "r");
1109# else /* HAVE_LOGIN_CAP */
1110 if (pw->pw_uid)
1111 f = fopen(_PATH_NOLOGIN, "r");
1112# endif /* HAVE_LOGIN_CAP */
1113 if (f) {
1114 /* /etc/nologin exists. Print its contents and exit. */
1115 while (fgets(buf, sizeof(buf), f))
1116 fputs(buf, stderr);
1117 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001118 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001119 }
Damien Millerb38eff82000-04-01 11:09:21 +10001120 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001121#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001122
Damien Millerad833b32000-08-23 10:46:23 +10001123 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001124 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1125 switch, so we let login(1) to this for us. */
1126 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001127#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001128 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001129 if (!check_quietlogin(s, command))
1130 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001131#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001132#ifdef HAVE_CYGWIN
1133 if (is_winnt) {
1134#else
Damien Millerb38eff82000-04-01 11:09:21 +10001135 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001136#endif
Damien Millerad833b32000-08-23 10:46:23 +10001137# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001138 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001139# endif /* HAVE_GETUSERATTR */
1140# ifdef HAVE_LOGIN_CAP
1141 if (setusercontext(lc, pw, pw->pw_uid,
1142 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1143 perror("unable to set user context");
1144 exit(1);
1145 }
1146# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001147#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1148 /* Sets login uid for accounting */
1149 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1150 error("setluid: %s", strerror(errno));
1151#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1152
Damien Millerad833b32000-08-23 10:46:23 +10001153 if (setlogin(pw->pw_name) < 0)
1154 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001155 if (setgid(pw->pw_gid) < 0) {
1156 perror("setgid");
1157 exit(1);
1158 }
1159 /* Initialize the group list. */
1160 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1161 perror("initgroups");
1162 exit(1);
1163 }
1164 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001165# ifdef USE_PAM
1166 /*
1167 * PAM credentials may take the form of
1168 * supplementary groups. These will have been
1169 * wiped by the above initgroups() call.
1170 * Reestablish them here.
1171 */
1172 do_pam_setcred(0);
1173# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001174# ifdef WITH_IRIX_JOBS
1175 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1176 if (jid == -1) {
1177 fatal("Failed to create job container: %.100s",
1178 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001179 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001180# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001181# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001182 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001183 if (jid == 0) {
1184 if (newarraysess() != 0)
1185 fatal("Failed to set up new array session: %.100s",
1186 strerror(errno));
1187 }
Damien Millerad833b32000-08-23 10:46:23 +10001188# endif /* WITH_IRIX_ARRAY */
1189# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001190 /* initialize irix project info */
1191 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1192 debug("Failed to get project id, using projid 0");
1193 projid = 0;
1194 }
Damien Miller91606b12000-06-28 08:22:29 +10001195 if (setprid(projid))
1196 fatal("Failed to initialize project %d for %s: %.100s",
1197 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001198# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001199#ifdef WITH_IRIX_AUDIT
1200 if (sysconf(_SC_AUDIT)) {
1201 debug("Setting sat id to %d", (int) pw->pw_uid);
1202 if (satsetid(pw->pw_uid))
1203 debug("error setting satid: %.100s", strerror(errno));
1204 }
1205#endif /* WITH_IRIX_AUDIT */
1206
Damien Miller168a7002001-03-17 10:29:50 +11001207#ifdef _AIX
1208 /*
1209 * AIX has a "usrinfo" area where logname and
1210 * other stuff is stored - a few applications
1211 * actually use this and die if it's not set
1212 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001213 if (s->ttyfd == -1)
1214 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001215 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001216 2 * strlen(pw->pw_name));
1217 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001218 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001219 if (usrinfo(SETUINFO, cp, i) == -1)
1220 fatal("Couldn't set usrinfo: %s",
1221 strerror(errno));
1222 debug3("AIX/UsrInfo: set len %d", i);
1223 xfree(cp);
1224#endif
1225
Damien Millerb38eff82000-04-01 11:09:21 +10001226 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001227 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001228# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001229 }
Damien Millerad833b32000-08-23 10:46:23 +10001230#endif /* HAVE_OSF_SIA */
1231
Damien Millerbac2d8a2000-09-05 16:13:06 +11001232#ifdef HAVE_CYGWIN
1233 if (is_winnt)
1234#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001235 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001236 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001237 }
1238 /*
1239 * Get the shell from the password data. An empty shell field is
1240 * legal, and means /bin/sh.
1241 */
1242 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001243#ifdef HAVE_LOGIN_CAP
1244 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1245#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001246
1247#ifdef AFS
1248 /* Try to get AFS tokens for the local cell. */
1249 if (k_hasafs()) {
1250 char cell[64];
1251
1252 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1253 krb_afslog(cell, 0);
1254
1255 krb_afslog(0, 0);
1256 }
1257#endif /* AFS */
1258
1259 /* Initialize the environment. */
1260 envsize = 100;
1261 env = xmalloc(envsize * sizeof(char *));
1262 env[0] = NULL;
1263
Damien Millerbac2d8a2000-09-05 16:13:06 +11001264#ifdef HAVE_CYGWIN
1265 /*
1266 * The Windows environment contains some setting which are
1267 * important for a running system. They must not be dropped.
1268 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001269 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001270#endif
1271
Damien Millerb38eff82000-04-01 11:09:21 +10001272 if (!options.use_login) {
1273 /* Set basic environment. */
1274 child_set_env(&env, &envsize, "USER", pw->pw_name);
1275 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1276 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001277#ifdef HAVE_LOGIN_CAP
1278 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1279 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001280#else /* HAVE_LOGIN_CAP */
1281# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001282 /*
1283 * There's no standard path on Windows. The path contains
1284 * important components pointing to the system directories,
1285 * needed for loading shared libraries. So the path better
1286 * remains intact here.
1287 */
Damien Millerb38eff82000-04-01 11:09:21 +10001288 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001289# endif /* HAVE_CYGWIN */
1290#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001291
1292 snprintf(buf, sizeof buf, "%.200s/%.50s",
1293 _PATH_MAILDIR, pw->pw_name);
1294 child_set_env(&env, &envsize, "MAIL", buf);
1295
1296 /* Normal systems set SHELL by default. */
1297 child_set_env(&env, &envsize, "SHELL", shell);
1298 }
1299 if (getenv("TZ"))
1300 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1301
1302 /* Set custom environment options from RSA authentication. */
1303 while (custom_environment) {
1304 struct envstring *ce = custom_environment;
1305 char *s = ce->s;
1306 int i;
1307 for (i = 0; s[i] != '=' && s[i]; i++);
1308 if (s[i] == '=') {
1309 s[i] = 0;
1310 child_set_env(&env, &envsize, s, s + i + 1);
1311 }
1312 custom_environment = ce->next;
1313 xfree(ce->s);
1314 xfree(ce);
1315 }
1316
1317 snprintf(buf, sizeof buf, "%.50s %d %d",
1318 get_remote_ipaddr(), get_remote_port(), get_local_port());
1319 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1320
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001321 if (s->ttyfd != -1)
1322 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1323 if (s->term)
1324 child_set_env(&env, &envsize, "TERM", s->term);
1325 if (s->display)
1326 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001327 if (original_command)
1328 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1329 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001330
1331#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001332 if ((cp = getenv("AUTHSTATE")) != NULL)
1333 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1334 if ((cp = getenv("KRB5CCNAME")) != NULL)
1335 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1336 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001337#endif
1338
1339#ifdef KRB4
1340 {
1341 extern char *ticket;
1342
1343 if (ticket)
1344 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1345 }
1346#endif /* KRB4 */
1347
1348#ifdef USE_PAM
1349 /* Pull in any environment variables that may have been set by PAM. */
1350 do_pam_environment(&env, &envsize);
1351#endif /* USE_PAM */
1352
Damien Millerb38eff82000-04-01 11:09:21 +10001353 if (xauthfile)
1354 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1355 if (auth_get_socket_name() != NULL)
1356 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1357 auth_get_socket_name());
1358
1359 /* read $HOME/.ssh/environment. */
1360 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001361 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1362 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001363 read_environment_file(&env, &envsize, buf);
1364 }
1365 if (debug_flag) {
1366 /* dump the environment */
1367 fprintf(stderr, "Environment:\n");
1368 for (i = 0; env[i]; i++)
1369 fprintf(stderr, " %.200s\n", env[i]);
1370 }
Damien Millerad833b32000-08-23 10:46:23 +10001371 /* we have to stash the hostname before we close our socket. */
1372 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001373 hostname = get_remote_name_or_ip(utmp_len,
1374 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001375 /*
1376 * Close the connection descriptors; note that this is the child, and
1377 * the server will still have the socket open, and it is important
1378 * that we do not shutdown it. Note that the descriptors cannot be
1379 * closed before building the environment, as we call
1380 * get_remote_ipaddr there.
1381 */
1382 if (packet_get_connection_in() == packet_get_connection_out())
1383 close(packet_get_connection_in());
1384 else {
1385 close(packet_get_connection_in());
1386 close(packet_get_connection_out());
1387 }
1388 /*
1389 * Close all descriptors related to channels. They will still remain
1390 * open in the parent.
1391 */
1392 /* XXX better use close-on-exec? -markus */
1393 channel_close_all();
1394
1395 /*
1396 * Close any extra file descriptors. Note that there may still be
1397 * descriptors left by system functions. They will be closed later.
1398 */
1399 endpwent();
1400
1401 /*
1402 * Close any extra open file descriptors so that we don\'t have them
1403 * hanging around in clients. Note that we want to do this after
1404 * initgroups, because at least on Solaris 2.3 it leaves file
1405 * descriptors open.
1406 */
1407 for (i = 3; i < 64; i++)
1408 close(i);
1409
1410 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001411 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001412 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1413 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001414#ifdef HAVE_LOGIN_CAP
1415 if (login_getcapbool(lc, "requirehome", 0))
1416 exit(1);
1417#endif
1418 }
Damien Millerb38eff82000-04-01 11:09:21 +10001419
1420 /*
1421 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1422 * xauth are run in the proper environment.
1423 */
1424 environ = env;
1425
1426 /*
1427 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1428 * in this order).
1429 */
1430 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001431 /* ignore _PATH_SSH_USER_RC for subsystems */
1432 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001433 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1434 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001435 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001436 fprintf(stderr, "Running %s\n", cmd);
1437 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001438 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001439 if (do_xauth)
1440 fprintf(f, "%s %s\n", s->auth_proto,
1441 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001442 pclose(f);
1443 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001444 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001445 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001446 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001447 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001448 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1449 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001450 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001451 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001452 if (do_xauth)
1453 fprintf(f, "%s %s\n", s->auth_proto,
1454 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001455 pclose(f);
1456 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001457 fprintf(stderr, "Could not run %s\n",
1458 _PATH_SSH_SYSTEM_RC);
1459 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001460 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001461 char *screen = strchr(s->display, ':');
1462
1463 if (debug_flag) {
1464 fprintf(stderr,
1465 "Running %.100s add "
1466 "%.100s %.100s %.100s\n",
1467 options.xauth_location, s->display,
1468 s->auth_proto, s->auth_data);
1469 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001470 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001471 "Adding %.*s/unix%s %s %s\n",
1472 (int)(screen - s->display),
1473 s->display, screen,
1474 s->auth_proto, s->auth_data);
1475 }
1476 snprintf(cmd, sizeof cmd, "%s -q -",
1477 options.xauth_location);
1478 f = popen(cmd, "w");
1479 if (f) {
1480 fprintf(f, "add %s %s %s\n", s->display,
1481 s->auth_proto, s->auth_data);
1482 if (screen != NULL)
1483 fprintf(f, "add %.*s/unix%s %s %s\n",
1484 (int)(screen - s->display),
1485 s->display, screen,
1486 s->auth_proto,
1487 s->auth_data);
1488 pclose(f);
1489 } else {
1490 fprintf(stderr, "Could not run %s\n",
1491 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001492 }
1493 }
Damien Millerb38eff82000-04-01 11:09:21 +10001494 /* Get the last component of the shell name. */
1495 cp = strrchr(shell, '/');
1496 if (cp)
1497 cp++;
1498 else
1499 cp = shell;
1500 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001501
1502 /* restore SIGPIPE for child */
1503 signal(SIGPIPE, SIG_DFL);
1504
Damien Millerb38eff82000-04-01 11:09:21 +10001505 /*
1506 * If we have no command, execute the shell. In this case, the shell
1507 * name to be passed in argv[0] is preceded by '-' to indicate that
1508 * this is a login shell.
1509 */
1510 if (!command) {
1511 if (!options.use_login) {
1512 char buf[256];
1513
1514 /*
1515 * Check for mail if we have a tty and it was enabled
1516 * in server options.
1517 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001518 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001519 char *mailbox;
1520 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001521
Damien Millerb38eff82000-04-01 11:09:21 +10001522 mailbox = getenv("MAIL");
1523 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001524 if (stat(mailbox, &mailstat) != 0 ||
1525 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001526 printf("No mail.\n");
1527 else if (mailstat.st_mtime < mailstat.st_atime)
1528 printf("You have mail.\n");
1529 else
1530 printf("You have new mail.\n");
1531 }
1532 }
1533 /* Start the shell. Set initial character to '-'. */
1534 buf[0] = '-';
1535 strncpy(buf + 1, cp, sizeof(buf) - 1);
1536 buf[sizeof(buf) - 1] = 0;
1537
1538 /* Execute the shell. */
1539 argv[0] = buf;
1540 argv[1] = NULL;
1541 execve(shell, argv, env);
1542
1543 /* Executing the shell failed. */
1544 perror(shell);
1545 exit(1);
1546
1547 } else {
1548 /* Launch login(1). */
1549
Damien Millerad833b32000-08-23 10:46:23 +10001550 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001551#ifdef LOGIN_NEEDS_TERM
1552 s->term? s->term : "unknown",
1553#endif
Damien Miller942da032000-08-18 13:59:06 +10001554 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001555
1556 /* Login couldn't be executed, die. */
1557
1558 perror("login");
1559 exit(1);
1560 }
1561 }
1562 /*
1563 * Execute the command using the user's shell. This uses the -c
1564 * option to execute the command.
1565 */
1566 argv[0] = (char *) cp;
1567 argv[1] = "-c";
1568 argv[2] = (char *) command;
1569 argv[3] = NULL;
1570 execve(shell, argv, env);
1571 perror(shell);
1572 exit(1);
1573}
1574
1575Session *
1576session_new(void)
1577{
1578 int i;
1579 static int did_init = 0;
1580 if (!did_init) {
1581 debug("session_new: init");
1582 for(i = 0; i < MAX_SESSIONS; i++) {
1583 sessions[i].used = 0;
1584 sessions[i].self = i;
1585 }
1586 did_init = 1;
1587 }
1588 for(i = 0; i < MAX_SESSIONS; i++) {
1589 Session *s = &sessions[i];
1590 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001591 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001592 s->chanid = -1;
1593 s->ptyfd = -1;
1594 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001595 s->used = 1;
Damien Miller15b29522000-10-14 12:33:48 +11001596 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001597 return s;
1598 }
1599 }
1600 return NULL;
1601}
1602
1603void
1604session_dump(void)
1605{
1606 int i;
1607 for(i = 0; i < MAX_SESSIONS; i++) {
1608 Session *s = &sessions[i];
1609 debug("dump: used %d session %d %p channel %d pid %d",
1610 s->used,
1611 s->self,
1612 s,
1613 s->chanid,
1614 s->pid);
1615 }
1616}
1617
Damien Millerefb4afe2000-04-12 18:45:05 +10001618int
1619session_open(int chanid)
1620{
1621 Session *s = session_new();
1622 debug("session_open: channel %d", chanid);
1623 if (s == NULL) {
1624 error("no more sessions");
1625 return 0;
1626 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001627 s->pw = auth_get_user();
1628 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001629 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001630 debug("session_open: session %d: link with channel %d", s->self, chanid);
1631 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001632 return 1;
1633}
1634
1635Session *
1636session_by_channel(int id)
1637{
1638 int i;
1639 for(i = 0; i < MAX_SESSIONS; i++) {
1640 Session *s = &sessions[i];
1641 if (s->used && s->chanid == id) {
1642 debug("session_by_channel: session %d channel %d", i, id);
1643 return s;
1644 }
1645 }
1646 debug("session_by_channel: unknown channel %d", id);
1647 session_dump();
1648 return NULL;
1649}
1650
1651Session *
1652session_by_pid(pid_t pid)
1653{
1654 int i;
1655 debug("session_by_pid: pid %d", pid);
1656 for(i = 0; i < MAX_SESSIONS; i++) {
1657 Session *s = &sessions[i];
1658 if (s->used && s->pid == pid)
1659 return s;
1660 }
1661 error("session_by_pid: unknown pid %d", pid);
1662 session_dump();
1663 return NULL;
1664}
1665
1666int
1667session_window_change_req(Session *s)
1668{
1669 s->col = packet_get_int();
1670 s->row = packet_get_int();
1671 s->xpixel = packet_get_int();
1672 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001673 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001674 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1675 return 1;
1676}
1677
1678int
1679session_pty_req(Session *s)
1680{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001681 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001682 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001683
Damien Millerf6d9e222000-06-18 14:50:44 +10001684 if (no_pty_flag)
1685 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001686 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001687 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001688 s->term = packet_get_string(&len);
1689 s->col = packet_get_int();
1690 s->row = packet_get_int();
1691 s->xpixel = packet_get_int();
1692 s->ypixel = packet_get_int();
1693
1694 if (strcmp(s->term, "") == 0) {
1695 xfree(s->term);
1696 s->term = NULL;
1697 }
1698 /* Allocate a pty and open it. */
1699 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1700 xfree(s->term);
1701 s->term = NULL;
1702 s->ptyfd = -1;
1703 s->ttyfd = -1;
1704 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001705 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001706 }
1707 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1708 /*
1709 * Add a cleanup function to clear the utmp entry and record logout
1710 * time in case we call fatal() (e.g., the connection gets closed).
1711 */
1712 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1713 pty_setowner(s->pw, s->tty);
1714 /* Get window size from the packet. */
1715 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1716
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001717 /* Get tty modes from the packet. */
1718 tty_parse_modes(s->ttyfd, &n_bytes);
1719 packet_done();
1720
Damien Millere247cc42000-05-07 12:03:14 +10001721 session_proctitle(s);
1722
Damien Millerefb4afe2000-04-12 18:45:05 +10001723 return 1;
1724}
1725
Damien Millerbd483e72000-04-30 10:00:53 +10001726int
1727session_subsystem_req(Session *s)
1728{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001729 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001730 int success = 0;
1731 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001732 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001733
1734 packet_done();
1735 log("subsystem request for %s", subsys);
1736
Damien Millerf6d9e222000-06-18 14:50:44 +10001737 for (i = 0; i < options.num_subsystems; i++) {
1738 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1739 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001740 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001741 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001742 success = 1;
1743 }
1744 }
1745
1746 if (!success)
1747 log("subsystem request for %s failed, subsystem not found", subsys);
1748
Damien Millerbd483e72000-04-30 10:00:53 +10001749 xfree(subsys);
1750 return success;
1751}
1752
1753int
1754session_x11_req(Session *s)
1755{
Damien Miller7b28dc52000-09-05 13:34:53 +11001756 int fd;
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +00001757 struct stat st;
Damien Miller37023962000-07-11 17:31:38 +10001758 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001759 debug("X11 forwarding disabled in user configuration file.");
1760 return 0;
1761 }
Damien Millerbd483e72000-04-30 10:00:53 +10001762 if (!options.x11_forwarding) {
1763 debug("X11 forwarding disabled in server configuration file.");
1764 return 0;
1765 }
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +00001766 if (!options.xauth_location ||
1767 (stat(options.xauth_location, &st) == -1)) {
1768 packet_send_debug("No xauth program; cannot forward with spoofing.");
1769 return 0;
1770 }
Damien Millerbd483e72000-04-30 10:00:53 +10001771 if (xauthfile != NULL) {
1772 debug("X11 fwd already started.");
1773 return 0;
1774 }
1775
1776 debug("Received request for X11 forwarding with auth spoofing.");
1777 if (s->display != NULL)
1778 packet_disconnect("Protocol error: X11 display already set.");
1779
1780 s->single_connection = packet_get_char();
1781 s->auth_proto = packet_get_string(NULL);
1782 s->auth_data = packet_get_string(NULL);
1783 s->screen = packet_get_int();
1784 packet_done();
1785
1786 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1787 if (s->display == NULL) {
1788 xfree(s->auth_proto);
1789 xfree(s->auth_data);
1790 return 0;
1791 }
1792 xauthfile = xmalloc(MAXPATHLEN);
1793 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001794 temporarily_use_uid(s->pw);
Damien Millerbd483e72000-04-30 10:00:53 +10001795 if (mkdtemp(xauthfile) == NULL) {
1796 restore_uid();
1797 error("private X11 dir: mkdtemp %s failed: %s",
1798 xauthfile, strerror(errno));
1799 xfree(xauthfile);
1800 xauthfile = NULL;
1801 xfree(s->auth_proto);
1802 xfree(s->auth_data);
1803 /* XXXX remove listening channels */
1804 return 0;
1805 }
1806 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001807 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1808 if (fd >= 0)
1809 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001810 restore_uid();
1811 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1812 return 1;
1813}
1814
Damien Millerf6d9e222000-06-18 14:50:44 +10001815int
1816session_shell_req(Session *s)
1817{
1818 /* if forced_command == NULL, the shell is execed */
1819 char *shell = forced_command;
1820 packet_done();
Damien Millerf6d9e222000-06-18 14:50:44 +10001821 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001822 do_exec_no_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001823 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001824 do_exec_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001825 return 1;
1826}
1827
1828int
1829session_exec_req(Session *s)
1830{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001831 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001832 char *command = packet_get_string(&len);
1833 packet_done();
1834 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001835 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001836 command = forced_command;
1837 debug("Forced command '%.500s'", forced_command);
1838 }
Damien Millerf6d9e222000-06-18 14:50:44 +10001839 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001840 do_exec_no_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001841 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001842 do_exec_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001843 if (forced_command == NULL)
1844 xfree(command);
1845 return 1;
1846}
1847
Damien Miller0bc1bd82000-11-13 22:57:25 +11001848int
1849session_auth_agent_req(Session *s)
1850{
1851 static int called = 0;
1852 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001853 if (no_agent_forwarding_flag) {
1854 debug("session_auth_agent_req: no_agent_forwarding_flag");
1855 return 0;
1856 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001857 if (called) {
1858 return 0;
1859 } else {
1860 called = 1;
1861 return auth_input_request_forwarding(s->pw);
1862 }
1863}
1864
Damien Millerefb4afe2000-04-12 18:45:05 +10001865void
1866session_input_channel_req(int id, void *arg)
1867{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001868 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001869 int reply;
1870 int success = 0;
1871 char *rtype;
1872 Session *s;
1873 Channel *c;
1874
1875 rtype = packet_get_string(&len);
1876 reply = packet_get_char();
1877
1878 s = session_by_channel(id);
1879 if (s == NULL)
1880 fatal("session_input_channel_req: channel %d: no session", id);
1881 c = channel_lookup(id);
1882 if (c == NULL)
1883 fatal("session_input_channel_req: channel %d: bad channel", id);
1884
1885 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1886 s->self, id, rtype, reply);
1887
1888 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001889 * a session is in LARVAL state until a shell, a command
1890 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001891 */
1892 if (c->type == SSH_CHANNEL_LARVAL) {
1893 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001894 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001895 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001896 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001897 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001898 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001899 } else if (strcmp(rtype, "x11-req") == 0) {
1900 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001901 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1902 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001903 } else if (strcmp(rtype, "subsystem") == 0) {
1904 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001905 }
1906 }
1907 if (strcmp(rtype, "window-change") == 0) {
1908 success = session_window_change_req(s);
1909 }
1910
1911 if (reply) {
1912 packet_start(success ?
1913 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1914 packet_put_int(c->remote_id);
1915 packet_send();
1916 }
1917 xfree(rtype);
1918}
1919
1920void
1921session_set_fds(Session *s, int fdin, int fdout, int fderr)
1922{
1923 if (!compat20)
1924 fatal("session_set_fds: called for proto != 2.0");
1925 /*
1926 * now that have a child and a pipe to the child,
1927 * we can activate our channel and register the fd's
1928 */
1929 if (s->chanid == -1)
1930 fatal("no channel for session %d", s->self);
1931 channel_set_fds(s->chanid,
1932 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001933 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1934 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001935}
1936
Damien Millerb38eff82000-04-01 11:09:21 +10001937void
1938session_pty_cleanup(Session *s)
1939{
1940 if (s == NULL || s->ttyfd == -1)
1941 return;
1942
Kevin Steves43cdef32001-02-11 14:12:08 +00001943 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001944
1945 /* Cancel the cleanup function. */
1946 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1947
1948 /* Record that the user has logged out. */
1949 record_logout(s->pid, s->tty);
1950
1951 /* Release the pseudo-tty. */
1952 pty_release(s->tty);
1953
1954 /*
1955 * Close the server side of the socket pairs. We must do this after
1956 * the pty cleanup, so that another process doesn't get this pty
1957 * while we're still cleaning up.
1958 */
1959 if (close(s->ptymaster) < 0)
1960 error("close(s->ptymaster): %s", strerror(errno));
1961}
Damien Millerefb4afe2000-04-12 18:45:05 +10001962
1963void
1964session_exit_message(Session *s, int status)
1965{
1966 Channel *c;
1967 if (s == NULL)
1968 fatal("session_close: no session");
1969 c = channel_lookup(s->chanid);
1970 if (c == NULL)
1971 fatal("session_close: session %d: no channel %d",
1972 s->self, s->chanid);
1973 debug("session_exit_message: session %d channel %d pid %d",
1974 s->self, s->chanid, s->pid);
1975
1976 if (WIFEXITED(status)) {
1977 channel_request_start(s->chanid,
1978 "exit-status", 0);
1979 packet_put_int(WEXITSTATUS(status));
1980 packet_send();
1981 } else if (WIFSIGNALED(status)) {
1982 channel_request_start(s->chanid,
1983 "exit-signal", 0);
1984 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001985#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001986 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001987#else /* WCOREDUMP */
1988 packet_put_char(0);
1989#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001990 packet_put_cstring("");
1991 packet_put_cstring("");
1992 packet_send();
1993 } else {
1994 /* Some weird exit cause. Just exit. */
1995 packet_disconnect("wait returned status %04x.", status);
1996 }
1997
1998 /* disconnect channel */
1999 debug("session_exit_message: release channel %d", s->chanid);
2000 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10002001 /*
2002 * emulate a write failure with 'chan_write_failed', nobody will be
2003 * interested in data we write.
2004 * Note that we must not call 'chan_read_failed', since there could
2005 * be some more data waiting in the pipe.
2006 */
Damien Millerbd483e72000-04-30 10:00:53 +10002007 if (c->ostate != CHAN_OUTPUT_CLOSED)
2008 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002009 s->chanid = -1;
2010}
2011
2012void
2013session_free(Session *s)
2014{
2015 debug("session_free: session %d pid %d", s->self, s->pid);
2016 if (s->term)
2017 xfree(s->term);
2018 if (s->display)
2019 xfree(s->display);
2020 if (s->auth_data)
2021 xfree(s->auth_data);
2022 if (s->auth_proto)
2023 xfree(s->auth_proto);
2024 s->used = 0;
2025}
2026
2027void
2028session_close(Session *s)
2029{
2030 session_pty_cleanup(s);
2031 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10002032 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002033}
2034
2035void
2036session_close_by_pid(pid_t pid, int status)
2037{
2038 Session *s = session_by_pid(pid);
2039 if (s == NULL) {
2040 debug("session_close_by_pid: no session for pid %d", s->pid);
2041 return;
2042 }
2043 if (s->chanid != -1)
2044 session_exit_message(s, status);
2045 session_close(s);
2046}
2047
2048/*
2049 * this is called when a channel dies before
2050 * the session 'child' itself dies
2051 */
2052void
2053session_close_by_channel(int id, void *arg)
2054{
2055 Session *s = session_by_channel(id);
2056 if (s == NULL) {
2057 debug("session_close_by_channel: no session for channel %d", id);
2058 return;
2059 }
2060 /* disconnect channel */
2061 channel_cancel_cleanup(s->chanid);
2062 s->chanid = -1;
2063
2064 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
2065 if (s->pid == 0) {
2066 /* close session immediately */
2067 session_close(s);
2068 } else {
2069 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10002070 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10002071 error("session_close_by_channel: kill %d: %s",
2072 s->pid, strerror(errno));
2073 }
2074}
2075
Damien Millere247cc42000-05-07 12:03:14 +10002076char *
2077session_tty_list(void)
2078{
2079 static char buf[1024];
2080 int i;
2081 buf[0] = '\0';
2082 for(i = 0; i < MAX_SESSIONS; i++) {
2083 Session *s = &sessions[i];
2084 if (s->used && s->ttyfd != -1) {
2085 if (buf[0] != '\0')
2086 strlcat(buf, ",", sizeof buf);
2087 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2088 }
2089 }
2090 if (buf[0] == '\0')
2091 strlcpy(buf, "notty", sizeof buf);
2092 return buf;
2093}
2094
2095void
2096session_proctitle(Session *s)
2097{
2098 if (s->pw == NULL)
2099 error("no user for session %d", s->self);
2100 else
2101 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2102}
2103
Damien Millerefb4afe2000-04-12 18:45:05 +10002104void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002105do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002106{
Ben Lindstromb31783d2001-03-22 02:02:12 +00002107
Damien Millerefb4afe2000-04-12 18:45:05 +10002108 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002109 if (xauthfile)
2110 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002111}