blob: ce9b2002b840bae85b8b466adf8feb1f036ac122 [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 Lindstrom838394c2001-06-09 01:11:59 +000036RCSID("$OpenBSD: session.c,v 1.79 2001/06/03 14:55:39 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 Lindstromb4c961d2001-03-22 01:25:37 +0000126void do_exec_pty(Session *s, const char *command);
127void do_exec_no_pty(Session *s, const char *command);
Damien Miller69b69aa2000-10-28 14:19:58 +1100128void do_login(Session *s, const char *command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000129#ifdef LOGIN_NEEDS_UTMPX
130void do_pre_login(Session *s);
131#endif
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000132void do_child(Session *s, const char *command);
Damien Millercf205e82001-04-16 18:29:15 +1000133void do_motd(void);
134int check_quietlogin(Session *s, const char *command);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000135void xauthfile_cleanup_proc(void *ignore);
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);
Ben Lindstrom838394c2001-06-09 01:11:59 +0000200
201 /* remote user's local Xauthority file and agent socket */
202 if (xauthfile)
203 xauthfile_cleanup_proc(NULL);
204 if (auth_get_socket_name())
205 auth_sock_cleanup_proc(NULL);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000206}
207
Damien Millerb38eff82000-04-01 11:09:21 +1000208/*
209 * Remove local Xauthority file.
210 */
211void
212xauthfile_cleanup_proc(void *ignore)
213{
214 debug("xauthfile_cleanup_proc called");
215
216 if (xauthfile != NULL) {
217 char *p;
218 unlink(xauthfile);
219 p = strrchr(xauthfile, '/');
220 if (p != NULL) {
221 *p = '\0';
222 rmdir(xauthfile);
223 }
224 xfree(xauthfile);
225 xauthfile = NULL;
226 }
227}
228
229/*
230 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
231 * dropped connection).
232 */
Damien Miller4af51302000-04-16 11:18:38 +1000233void
Damien Millerb38eff82000-04-01 11:09:21 +1000234pty_cleanup_proc(void *session)
235{
236 Session *s=session;
237 if (s == NULL)
238 fatal("pty_cleanup_proc: no session");
239 debug("pty_cleanup_proc: %s", s->tty);
240
241 if (s->pid != 0) {
242 /* Record that the user has logged out. */
243 record_logout(s->pid, s->tty);
244 }
245
246 /* Release the pseudo-tty. */
247 pty_release(s->tty);
248}
249
250/*
251 * Prepares for an interactive session. This is called after the user has
252 * been successfully authenticated. During this message exchange, pseudo
253 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
254 * are requested, etc.
255 */
Damien Miller4af51302000-04-16 11:18:38 +1000256void
Ben Lindstromb31783d2001-03-22 02:02:12 +0000257do_authenticated1(Authctxt *authctxt)
Damien Millerb38eff82000-04-01 11:09:21 +1000258{
259 Session *s;
Damien Millerb38eff82000-04-01 11:09:21 +1000260 char *command;
Ben Lindstromb31783d2001-03-22 02:02:12 +0000261 int success, type, fd, n_bytes, plen, screen_flag, have_pty = 0;
262 int compression_level = 0, enable_compression_after_reply = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000263 u_int proto_len, data_len, dlen;
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +0000264 struct stat st;
Damien Millerb38eff82000-04-01 11:09:21 +1000265
266 s = session_new();
Ben Lindstromb31783d2001-03-22 02:02:12 +0000267 s->pw = authctxt->pw;
Damien Millerad833b32000-08-23 10:46:23 +1000268
Damien Millerb38eff82000-04-01 11:09:21 +1000269 /*
270 * We stay in this loop until the client requests to execute a shell
271 * or a command.
272 */
273 for (;;) {
Ben Lindstromb31783d2001-03-22 02:02:12 +0000274 success = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000275
276 /* Get a packet from the client. */
277 type = packet_read(&plen);
278
279 /* Process the packet. */
280 switch (type) {
281 case SSH_CMSG_REQUEST_COMPRESSION:
282 packet_integrity_check(plen, 4, type);
283 compression_level = packet_get_int();
284 if (compression_level < 1 || compression_level > 9) {
285 packet_send_debug("Received illegal compression level %d.",
286 compression_level);
287 break;
288 }
289 /* Enable compression after we have responded with SUCCESS. */
290 enable_compression_after_reply = 1;
291 success = 1;
292 break;
293
294 case SSH_CMSG_REQUEST_PTY:
295 if (no_pty_flag) {
296 debug("Allocating a pty not permitted for this authentication.");
297 break;
298 }
299 if (have_pty)
300 packet_disconnect("Protocol error: you already have a pty.");
301
302 debug("Allocating pty.");
303
304 /* Allocate a pty and open it. */
305 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
306 sizeof(s->tty))) {
307 error("Failed to allocate pty.");
308 break;
309 }
310 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
Ben Lindstromb31783d2001-03-22 02:02:12 +0000311 pty_setowner(s->pw, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000312
313 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
314 s->term = packet_get_string(&dlen);
315 packet_integrity_check(dlen, strlen(s->term), type);
316 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
317 /* Remaining bytes */
318 n_bytes = plen - (4 + dlen + 4 * 4);
319
320 if (strcmp(s->term, "") == 0) {
321 xfree(s->term);
322 s->term = NULL;
323 }
324 /* Get window size from the packet. */
325 s->row = packet_get_int();
326 s->col = packet_get_int();
327 s->xpixel = packet_get_int();
328 s->ypixel = packet_get_int();
329 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
330
331 /* Get tty modes from the packet. */
332 tty_parse_modes(s->ttyfd, &n_bytes);
333 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
334
Damien Millere247cc42000-05-07 12:03:14 +1000335 session_proctitle(s);
336
Damien Millerb38eff82000-04-01 11:09:21 +1000337 /* Indicate that we now have a pty. */
338 success = 1;
339 have_pty = 1;
340 break;
341
342 case SSH_CMSG_X11_REQUEST_FORWARDING:
343 if (!options.x11_forwarding) {
344 packet_send_debug("X11 forwarding disabled in server configuration file.");
345 break;
346 }
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +0000347 if (!options.xauth_location ||
348 (stat(options.xauth_location, &st) == -1)) {
Damien Miller0c043c12000-06-07 21:22:38 +1000349 packet_send_debug("No xauth program; cannot forward with spoofing.");
350 break;
351 }
Damien Millerb38eff82000-04-01 11:09:21 +1000352 if (no_x11_forwarding_flag) {
353 packet_send_debug("X11 forwarding not permitted for this authentication.");
354 break;
355 }
356 debug("Received request for X11 forwarding with auth spoofing.");
357 if (s->display != NULL)
358 packet_disconnect("Protocol error: X11 display already set.");
359
360 s->auth_proto = packet_get_string(&proto_len);
361 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000362
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000363 screen_flag = packet_get_protocol_flags() &
364 SSH_PROTOFLAG_SCREEN_NUMBER;
365 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
366
367 if (packet_remaining() == 4) {
368 if (!screen_flag)
369 debug2("Buggy client: "
370 "X11 screen flag missing");
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000371 packet_integrity_check(plen,
372 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000373 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000374 } else {
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000375 packet_integrity_check(plen,
376 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000377 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000378 }
Damien Millerb38eff82000-04-01 11:09:21 +1000379 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
380
381 if (s->display == NULL)
382 break;
383
384 /* Setup to always have a local .Xauthority. */
385 xauthfile = xmalloc(MAXPATHLEN);
386 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000387 temporarily_use_uid(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000388 if (mkdtemp(xauthfile) == NULL) {
389 restore_uid();
390 error("private X11 dir: mkdtemp %s failed: %s",
391 xauthfile, strerror(errno));
392 xfree(xauthfile);
393 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000394 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000395 break;
396 }
397 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100398 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
399 if (fd >= 0)
400 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000401 restore_uid();
402 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
403 success = 1;
404 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000405
406 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
407 if (no_agent_forwarding_flag || compat13) {
408 debug("Authentication agent forwarding not permitted for this authentication.");
409 break;
410 }
411 debug("Received authentication agent forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000412 success = auth_input_request_forwarding(s->pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000413 break;
414
415 case SSH_CMSG_PORT_FORWARD_REQUEST:
416 if (no_port_forwarding_flag) {
417 debug("Port forwarding not permitted for this authentication.");
418 break;
419 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100420 if (!options.allow_tcp_forwarding) {
421 debug("Port forwarding not permitted.");
422 break;
423 }
Damien Millerb38eff82000-04-01 11:09:21 +1000424 debug("Received TCP/IP port forwarding request.");
Ben Lindstromb31783d2001-03-22 02:02:12 +0000425 channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000426 success = 1;
427 break;
428
429 case SSH_CMSG_MAX_PACKET_SIZE:
430 if (packet_set_maxsize(packet_get_int()) > 0)
431 success = 1;
432 break;
433
434 case SSH_CMSG_EXEC_SHELL:
435 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000436 if (type == SSH_CMSG_EXEC_CMD) {
437 command = packet_get_string(&dlen);
438 debug("Exec command '%.500s'", command);
439 packet_integrity_check(plen, 4 + dlen, type);
440 } else {
441 command = NULL;
442 packet_integrity_check(plen, 0, type);
443 }
444 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100445 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000446 command = forced_command;
447 debug("Forced command '%.500s'", forced_command);
448 }
449 if (have_pty)
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000450 do_exec_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000451 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000452 do_exec_no_pty(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000453
454 if (command != NULL)
455 xfree(command);
Damien Millerb38eff82000-04-01 11:09:21 +1000456 return;
457
458 default:
459 /*
460 * Any unknown messages in this phase are ignored,
461 * and a failure message is returned.
462 */
463 log("Unknown packet type received after authentication: %d", type);
464 }
465 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
466 packet_send();
467 packet_write_wait();
468
469 /* Enable compression now that we have replied if appropriate. */
470 if (enable_compression_after_reply) {
471 enable_compression_after_reply = 0;
472 packet_start_compression(compression_level);
473 }
474 }
475}
476
477/*
478 * This is called to fork and execute a command when we have no tty. This
479 * will call do_child from the child, and server_loop from the parent after
480 * setting up file descriptors and such.
481 */
Damien Miller4af51302000-04-16 11:18:38 +1000482void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000483do_exec_no_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000484{
485 int pid;
486
487#ifdef USE_PIPES
488 int pin[2], pout[2], perr[2];
489 /* Allocate pipes for communicating with the program. */
490 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
491 packet_disconnect("Could not create pipes: %.100s",
492 strerror(errno));
493#else /* USE_PIPES */
494 int inout[2], err[2];
495 /* Uses socket pairs to communicate with the program. */
496 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
497 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
498 packet_disconnect("Could not create socket pairs: %.100s",
499 strerror(errno));
500#endif /* USE_PIPES */
501 if (s == NULL)
502 fatal("do_exec_no_pty: no session");
503
Damien Millere247cc42000-05-07 12:03:14 +1000504 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000505
Damien Millerc5946332001-02-28 11:46:11 +1100506#if defined(USE_PAM)
Damien Millerf9e93002001-03-27 16:12:24 +1000507 do_pam_setcred(1);
Kevin Stevesff793a22001-02-21 16:36:51 +0000508#endif /* USE_PAM */
509
Damien Millerb38eff82000-04-01 11:09:21 +1000510 /* Fork the child. */
511 if ((pid = fork()) == 0) {
512 /* Child. Reinitialize the log since the pid has changed. */
513 log_init(__progname, options.log_level, options.log_facility, log_stderr);
514
515 /*
516 * Create a new session and process group since the 4.4BSD
517 * setlogin() affects the entire process group.
518 */
519 if (setsid() < 0)
520 error("setsid failed: %.100s", strerror(errno));
521
522#ifdef USE_PIPES
523 /*
524 * Redirect stdin. We close the parent side of the socket
525 * pair, and make the child side the standard input.
526 */
527 close(pin[1]);
528 if (dup2(pin[0], 0) < 0)
529 perror("dup2 stdin");
530 close(pin[0]);
531
532 /* Redirect stdout. */
533 close(pout[0]);
534 if (dup2(pout[1], 1) < 0)
535 perror("dup2 stdout");
536 close(pout[1]);
537
538 /* Redirect stderr. */
539 close(perr[0]);
540 if (dup2(perr[1], 2) < 0)
541 perror("dup2 stderr");
542 close(perr[1]);
543#else /* USE_PIPES */
544 /*
545 * Redirect stdin, stdout, and stderr. Stdin and stdout will
546 * use the same socket, as some programs (particularly rdist)
547 * seem to depend on it.
548 */
549 close(inout[1]);
550 close(err[1]);
551 if (dup2(inout[0], 0) < 0) /* stdin */
552 perror("dup2 stdin");
553 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
554 perror("dup2 stdout");
555 if (dup2(err[0], 2) < 0) /* stderr */
556 perror("dup2 stderr");
557#endif /* USE_PIPES */
558
559 /* Do processing for the child (exec command etc). */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000560 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000561 /* NOTREACHED */
562 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100563#ifdef HAVE_CYGWIN
564 if (is_winnt)
565 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
566#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000567 if (pid < 0)
568 packet_disconnect("fork failed: %.100s", strerror(errno));
569 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000570 /* Set interactive/non-interactive mode. */
571 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000572#ifdef USE_PIPES
573 /* We are the parent. Close the child sides of the pipes. */
574 close(pin[0]);
575 close(pout[1]);
576 close(perr[1]);
577
Damien Millerefb4afe2000-04-12 18:45:05 +1000578 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000579 session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000580 } else {
581 /* Enter the interactive session. */
582 server_loop(pid, pin[1], pout[0], perr[0]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000583 /* server_loop has closed pin[1], pout[0], and perr[0]. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000584 }
Damien Millerb38eff82000-04-01 11:09:21 +1000585#else /* USE_PIPES */
586 /* We are the parent. Close the child sides of the socket pairs. */
587 close(inout[0]);
588 close(err[0]);
589
590 /*
591 * Enter the interactive session. Note: server_loop must be able to
592 * handle the case that fdin and fdout are the same.
593 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000594 if (compat20) {
Ben Lindstromfc9b07d2001-03-22 01:27:23 +0000595 session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]);
Damien Millerefb4afe2000-04-12 18:45:05 +1000596 } else {
597 server_loop(pid, inout[1], inout[1], err[1]);
598 /* server_loop has closed inout[1] and err[1]. */
599 }
Damien Millerb38eff82000-04-01 11:09:21 +1000600#endif /* USE_PIPES */
601}
602
603/*
604 * This is called to fork and execute a command when we have a tty. This
605 * will call do_child from the child, and server_loop from the parent after
606 * setting up file descriptors, controlling tty, updating wtmp, utmp,
607 * lastlog, and other such operations.
608 */
Damien Miller4af51302000-04-16 11:18:38 +1000609void
Ben Lindstromb4c961d2001-03-22 01:25:37 +0000610do_exec_pty(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +1000611{
Damien Millerb38eff82000-04-01 11:09:21 +1000612 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000613 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000614
615 if (s == NULL)
616 fatal("do_exec_pty: no session");
617 ptyfd = s->ptyfd;
618 ttyfd = s->ttyfd;
619
Damien Millerc5946332001-02-28 11:46:11 +1100620#if defined(USE_PAM)
Ben Lindstromb31783d2001-03-22 02:02:12 +0000621 do_pam_session(s->pw->pw_name, s->tty);
Damien Millerf9e93002001-03-27 16:12:24 +1000622 do_pam_setcred(1);
Damien Miller5a761312001-02-27 09:28:23 +1100623#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000624
Damien Millerb38eff82000-04-01 11:09:21 +1000625 /* Fork the child. */
626 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000627 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000628 log_init(__progname, options.log_level, options.log_facility, log_stderr);
629
630 /* Close the master side of the pseudo tty. */
631 close(ptyfd);
632
633 /* Make the pseudo tty our controlling tty. */
634 pty_make_controlling_tty(&ttyfd, s->tty);
635
636 /* Redirect stdin from the pseudo tty. */
637 if (dup2(ttyfd, fileno(stdin)) < 0)
638 error("dup2 stdin failed: %.100s", strerror(errno));
639
640 /* Redirect stdout to the pseudo tty. */
641 if (dup2(ttyfd, fileno(stdout)) < 0)
642 error("dup2 stdin failed: %.100s", strerror(errno));
643
644 /* Redirect stderr to the pseudo tty. */
645 if (dup2(ttyfd, fileno(stderr)) < 0)
646 error("dup2 stdin failed: %.100s", strerror(errno));
647
648 /* Close the extra descriptor for the pseudo tty. */
649 close(ttyfd);
650
Damien Miller942da032000-08-18 13:59:06 +1000651 /* record login, etc. similar to login(1) */
Damien Miller364a9bd2001-04-16 18:37:05 +1000652#ifndef HAVE_OSF_SIA
Damien Miller69b69aa2000-10-28 14:19:58 +1100653 if (!(options.use_login && command == NULL))
654 do_login(s, command);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000655# ifdef LOGIN_NEEDS_UTMPX
656 else
657 do_pre_login(s);
658# endif
Damien Miller364a9bd2001-04-16 18:37:05 +1000659#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000660
Damien Millerb38eff82000-04-01 11:09:21 +1000661 /* Do common processing for the child, such as execing the command. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +0000662 do_child(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000663 /* NOTREACHED */
664 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100665#ifdef HAVE_CYGWIN
666 if (is_winnt)
667 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
668#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000669 if (pid < 0)
670 packet_disconnect("fork failed: %.100s", strerror(errno));
671 s->pid = pid;
672
673 /* Parent. Close the slave side of the pseudo tty. */
674 close(ttyfd);
675
676 /*
677 * Create another descriptor of the pty master side for use as the
678 * standard input. We could use the original descriptor, but this
679 * simplifies code in server_loop. The descriptor is bidirectional.
680 */
681 fdout = dup(ptyfd);
682 if (fdout < 0)
683 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
684
685 /* we keep a reference to the pty master */
686 ptymaster = dup(ptyfd);
687 if (ptymaster < 0)
688 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
689 s->ptymaster = ptymaster;
690
691 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000692 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000693 if (compat20) {
694 session_set_fds(s, ptyfd, fdout, -1);
695 } else {
696 server_loop(pid, ptyfd, fdout, -1);
697 /* server_loop _has_ closed ptyfd and fdout. */
698 session_pty_cleanup(s);
699 }
Damien Millerb38eff82000-04-01 11:09:21 +1000700}
701
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000702#ifdef LOGIN_NEEDS_UTMPX
703void
704do_pre_login(Session *s)
705{
706 socklen_t fromlen;
707 struct sockaddr_storage from;
708 pid_t pid = getpid();
709
710 /*
711 * Get IP address of client. If the connection is not a socket, let
712 * the address be 0.0.0.0.
713 */
714 memset(&from, 0, sizeof(from));
715 if (packet_connection_is_on_socket()) {
716 fromlen = sizeof(from);
717 if (getpeername(packet_get_connection_in(),
718 (struct sockaddr *) & from, &fromlen) < 0) {
719 debug("getpeername: %.100s", strerror(errno));
720 fatal_cleanup();
721 }
722 }
723
724 record_utmp_only(pid, s->tty, s->pw->pw_name,
725 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
726 (struct sockaddr *)&from);
727}
728#endif
729
Damien Miller942da032000-08-18 13:59:06 +1000730/* administrative, login(1)-like work */
731void
Damien Miller69b69aa2000-10-28 14:19:58 +1100732do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000733{
Damien Miller942da032000-08-18 13:59:06 +1000734 char *time_string;
Damien Miller7b28dc52000-09-05 13:34:53 +1100735 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000736 socklen_t fromlen;
737 struct sockaddr_storage from;
Damien Miller942da032000-08-18 13:59:06 +1000738 time_t last_login_time;
739 struct passwd * pw = s->pw;
740 pid_t pid = getpid();
741
742 /*
743 * Get IP address of client. If the connection is not a socket, let
744 * the address be 0.0.0.0.
745 */
746 memset(&from, 0, sizeof(from));
747 if (packet_connection_is_on_socket()) {
748 fromlen = sizeof(from);
749 if (getpeername(packet_get_connection_in(),
750 (struct sockaddr *) & from, &fromlen) < 0) {
751 debug("getpeername: %.100s", strerror(errno));
752 fatal_cleanup();
753 }
754 }
755
Damien Miller7b28dc52000-09-05 13:34:53 +1100756 /* Get the time and hostname when the user last logged in. */
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000757 if (options.print_lastlog) {
758 hostname[0] = '\0';
759 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
760 hostname, sizeof(hostname));
761 }
Damien Millere4340be2000-09-16 13:29:08 +1100762
Damien Miller942da032000-08-18 13:59:06 +1000763 /* Record that there was a login on that tty from the remote host. */
764 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
Ben Lindstromf15a3862001-04-05 23:32:17 +0000765 get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
766 (struct sockaddr *)&from);
Damien Miller942da032000-08-18 13:59:06 +1000767
Kevin Steves092f2ef2000-10-14 13:36:13 +0000768#ifdef USE_PAM
769 /*
770 * If password change is needed, do it now.
771 * This needs to occur before the ~/.hushlogin check.
772 */
Damien Miller646aa602001-02-15 11:51:32 +1100773 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000774 print_pam_messages();
775 do_pam_chauthtok();
776 }
777#endif
778
Damien Millercf205e82001-04-16 18:29:15 +1000779 if (check_quietlogin(s, command))
Damien Miller942da032000-08-18 13:59:06 +1000780 return;
781
782#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100783 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000784 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000785#endif /* USE_PAM */
786#ifdef WITH_AIXAUTHENTICATE
787 if (aixloginmsg && *aixloginmsg)
788 printf("%s\n", aixloginmsg);
789#endif /* WITH_AIXAUTHENTICATE */
790
Ben Lindstrom7bfff362001-03-26 05:45:53 +0000791 if (options.print_lastlog && last_login_time != 0) {
Damien Miller942da032000-08-18 13:59:06 +1000792 time_string = ctime(&last_login_time);
793 if (strchr(time_string, '\n'))
794 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000795 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000796 printf("Last login: %s\r\n", time_string);
797 else
Damien Millere4340be2000-09-16 13:29:08 +1100798 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000799 }
Damien Millercf205e82001-04-16 18:29:15 +1000800
801 do_motd();
802}
803
804/*
805 * Display the message of the day.
806 */
807void
808do_motd(void)
809{
810 FILE *f;
811 char buf[256];
812
Damien Miller942da032000-08-18 13:59:06 +1000813 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000814#ifdef HAVE_LOGIN_CAP
815 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
816 "/etc/motd"), "r");
817#else
Damien Miller942da032000-08-18 13:59:06 +1000818 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000819#endif
Damien Miller942da032000-08-18 13:59:06 +1000820 if (f) {
821 while (fgets(buf, sizeof(buf), f))
822 fputs(buf, stdout);
823 fclose(f);
824 }
825 }
826}
827
Damien Millercf205e82001-04-16 18:29:15 +1000828
829/*
830 * Check for quiet login, either .hushlogin or command given.
831 */
832int
833check_quietlogin(Session *s, const char *command)
834{
835 char buf[256];
836 struct passwd * pw = s->pw;
837 struct stat st;
838
839 /* Return 1 if .hushlogin exists or a command given. */
840 if (command != NULL)
841 return 1;
842 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
843#ifdef HAVE_LOGIN_CAP
844 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
845 return 1;
846#else
847 if (stat(buf, &st) >= 0)
848 return 1;
849#endif
850 return 0;
851}
852
Damien Millerb38eff82000-04-01 11:09:21 +1000853/*
854 * Sets the value of the given variable in the environment. If the variable
855 * already exists, its value is overriden.
856 */
Damien Miller4af51302000-04-16 11:18:38 +1000857void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000858child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000859 const char *value)
860{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000861 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000862 char **env;
863
864 /*
865 * Find the slot where the value should be stored. If the variable
866 * already exists, we reuse the slot; otherwise we append a new slot
867 * at the end of the array, expanding if necessary.
868 */
869 env = *envp;
870 namelen = strlen(name);
871 for (i = 0; env[i]; i++)
872 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
873 break;
874 if (env[i]) {
875 /* Reuse the slot. */
876 xfree(env[i]);
877 } else {
878 /* New variable. Expand if necessary. */
879 if (i >= (*envsizep) - 1) {
880 (*envsizep) += 50;
881 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
882 }
883 /* Need to set the NULL pointer at end of array beyond the new slot. */
884 env[i + 1] = NULL;
885 }
886
887 /* Allocate space and format the variable in the appropriate slot. */
888 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
889 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
890}
891
892/*
893 * Reads environment variables from the given file and adds/overrides them
894 * into the environment. If the file does not exist, this does nothing.
895 * Otherwise, it must consist of empty lines, comments (line starts with '#')
896 * and assignments of the form name=value. No other forms are allowed.
897 */
Damien Miller4af51302000-04-16 11:18:38 +1000898void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000899read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000900 const char *filename)
901{
902 FILE *f;
903 char buf[4096];
904 char *cp, *value;
905
906 f = fopen(filename, "r");
907 if (!f)
908 return;
909
910 while (fgets(buf, sizeof(buf), f)) {
911 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
912 ;
913 if (!*cp || *cp == '#' || *cp == '\n')
914 continue;
915 if (strchr(cp, '\n'))
916 *strchr(cp, '\n') = '\0';
917 value = strchr(cp, '=');
918 if (value == NULL) {
919 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
920 continue;
921 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000922 /*
923 * Replace the equals sign by nul, and advance value to
924 * the value string.
925 */
Damien Millerb38eff82000-04-01 11:09:21 +1000926 *value = '\0';
927 value++;
928 child_set_env(env, envsize, cp, value);
929 }
930 fclose(f);
931}
932
933#ifdef USE_PAM
934/*
935 * Sets any environment variables which have been specified by PAM
936 */
937void do_pam_environment(char ***env, int *envsize)
938{
939 char *equals, var_name[512], var_val[512];
940 char **pam_env;
941 int i;
942
943 if ((pam_env = fetch_pam_environment()) == NULL)
944 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000945
Damien Millerb38eff82000-04-01 11:09:21 +1000946 for(i = 0; pam_env[i] != NULL; i++) {
947 if ((equals = strstr(pam_env[i], "=")) == NULL)
948 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000949
Damien Millerb38eff82000-04-01 11:09:21 +1000950 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
951 memset(var_name, '\0', sizeof(var_name));
952 memset(var_val, '\0', sizeof(var_val));
953
954 strncpy(var_name, pam_env[i], equals - pam_env[i]);
955 strcpy(var_val, equals + 1);
956
Damien Millercb5e44a2000-09-29 12:12:36 +1100957 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000958
959 child_set_env(env, envsize, var_name, var_val);
960 }
961 }
962}
963#endif /* USE_PAM */
964
Damien Millercb5e44a2000-09-29 12:12:36 +1100965#ifdef HAVE_CYGWIN
966void copy_environment(char ***env, int *envsize)
967{
968 char *equals, var_name[512], var_val[512];
969 int i;
970
971 for(i = 0; environ[i] != NULL; i++) {
972 if ((equals = strstr(environ[i], "=")) == NULL)
973 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000974
Damien Millercb5e44a2000-09-29 12:12:36 +1100975 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
976 memset(var_name, '\0', sizeof(var_name));
977 memset(var_val, '\0', sizeof(var_val));
978
979 strncpy(var_name, environ[i], equals - environ[i]);
980 strcpy(var_val, equals + 1);
981
982 debug3("Copy environment: %s=%s", var_name, var_val);
983
984 child_set_env(env, envsize, var_name, var_val);
985 }
986 }
987}
988#endif
989
Damien Miller5fc85652000-07-09 23:53:07 +1000990#if defined(HAVE_GETUSERATTR)
991/*
992 * AIX-specific login initialisation
993 */
994void set_limit(char *user, char *soft, char *hard, int resource, int mult)
995{
996 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000997 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000998
999 getrlimit(resource, &rlim);
1000
Damien Miller65964d62000-07-11 09:16:22 +10001001 slim = 0;
1002 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
1003 if (slim < 0) {
1004 rlim.rlim_cur = RLIM_INFINITY;
1005 } else if (slim != 0) {
1006 /* See the wackiness below */
1007 if (rlim.rlim_cur == slim * mult)
1008 slim = 0;
1009 else
1010 rlim.rlim_cur = slim * mult;
1011 }
1012 }
Damien Miller5fc85652000-07-09 23:53:07 +10001013
Damien Miller65964d62000-07-11 09:16:22 +10001014 hlim = 0;
1015 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
1016 if (hlim < 0) {
1017 rlim.rlim_max = RLIM_INFINITY;
1018 } else if (hlim != 0) {
1019 rlim.rlim_max = hlim * mult;
1020 }
1021 }
Damien Miller5fc85652000-07-09 23:53:07 +10001022
Damien Miller65964d62000-07-11 09:16:22 +10001023 /*
1024 * XXX For cpu and fsize the soft limit is set to the hard limit
1025 * if the hard limit is left at its default value and the soft limit
1026 * is changed from its default value, either by requesting it
1027 * (slim == 0) or by setting it to the current default. At least
1028 * that's how rlogind does it. If you're confused you're not alone.
1029 * Bug or feature? AIX 4.3.1.2
1030 */
1031 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
1032 && hlim == 0 && slim != 0)
1033 rlim.rlim_max = rlim.rlim_cur;
1034 /* A specified hard limit limits the soft limit */
1035 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
1036 rlim.rlim_cur = rlim.rlim_max;
1037 /* A soft limit can increase a hard limit */
1038 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +10001039 rlim.rlim_max = rlim.rlim_cur;
1040
1041 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +10001042 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +10001043}
1044
1045void set_limits_from_userattr(char *user)
1046{
1047 int mask;
1048 char buf[16];
1049
1050 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
1051 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
1052 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
1053 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
1054 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
1055 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
1056#if defined(S_UNOFILE)
1057 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
1058#endif
1059
1060 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1061 /* Convert decimal to octal */
1062 (void) snprintf(buf, sizeof(buf), "%d", mask);
1063 if (sscanf(buf, "%o", &mask) == 1)
1064 umask(mask);
1065 }
1066}
1067#endif /* defined(HAVE_GETUSERATTR) */
1068
Damien Millerb38eff82000-04-01 11:09:21 +10001069/*
1070 * Performs common processing for the child, such as setting up the
1071 * environment, closing extra file descriptors, setting the user and group
1072 * ids, and executing the command or shell.
1073 */
Damien Miller4af51302000-04-16 11:18:38 +10001074void
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001075do_child(Session *s, const char *command)
Damien Millerb38eff82000-04-01 11:09:21 +10001076{
Damien Miller7b28dc52000-09-05 13:34:53 +11001077 const char *shell, *hostname = NULL, *cp = NULL;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001078 struct passwd * pw = s->pw;
Damien Millerb38eff82000-04-01 11:09:21 +10001079 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001080 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001081 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001082 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001083 char **env;
1084 extern char **environ;
1085 struct stat st;
1086 char *argv[10];
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001087 int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
Damien Miller91606b12000-06-28 08:22:29 +10001088#ifdef WITH_IRIX_PROJECT
1089 prid_t projid;
1090#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001091#ifdef WITH_IRIX_JOBS
1092 jid_t jid = 0;
1093#else
1094#ifdef WITH_IRIX_ARRAY
1095 int jid = 0;
1096#endif /* WITH_IRIX_ARRAY */
1097#endif /* WITH_IRIX_JOBS */
1098
Ben Lindstrom005dd222001-04-18 15:29:33 +00001099 /* remove hostkey from the child's memory */
1100 destroy_sensitive_data();
1101
Damien Millerd3a18572000-06-07 19:55:44 +10001102 /* login(1) is only called if we execute the login shell */
1103 if (options.use_login && command != NULL)
1104 options.use_login = 0;
1105
Damien Miller364a9bd2001-04-16 18:37:05 +10001106#if !defined(USE_PAM) && !defined(HAVE_OSF_SIA)
Damien Millerad833b32000-08-23 10:46:23 +10001107 if (!options.use_login) {
1108# ifdef HAVE_LOGIN_CAP
1109 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1110 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1111 _PATH_NOLOGIN), "r");
1112# else /* HAVE_LOGIN_CAP */
1113 if (pw->pw_uid)
1114 f = fopen(_PATH_NOLOGIN, "r");
1115# endif /* HAVE_LOGIN_CAP */
1116 if (f) {
1117 /* /etc/nologin exists. Print its contents and exit. */
1118 while (fgets(buf, sizeof(buf), f))
1119 fputs(buf, stderr);
1120 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001121 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001122 }
Damien Millerb38eff82000-04-01 11:09:21 +10001123 }
Damien Miller364a9bd2001-04-16 18:37:05 +10001124#endif /* USE_PAM || HAVE_OSF_SIA */
Damien Millerb38eff82000-04-01 11:09:21 +10001125
Damien Millerad833b32000-08-23 10:46:23 +10001126 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001127 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1128 switch, so we let login(1) to this for us. */
1129 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001130#ifdef HAVE_OSF_SIA
Damien Millerb69407d2001-03-21 16:13:03 +11001131 session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);
Damien Miller364a9bd2001-04-16 18:37:05 +10001132 if (!check_quietlogin(s, command))
1133 do_motd();
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001134#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001135#ifdef HAVE_CYGWIN
1136 if (is_winnt) {
1137#else
Damien Millerb38eff82000-04-01 11:09:21 +10001138 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001139#endif
Damien Millerad833b32000-08-23 10:46:23 +10001140# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001141 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001142# endif /* HAVE_GETUSERATTR */
1143# ifdef HAVE_LOGIN_CAP
1144 if (setusercontext(lc, pw, pw->pw_uid,
1145 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1146 perror("unable to set user context");
1147 exit(1);
1148 }
1149# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001150#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1151 /* Sets login uid for accounting */
1152 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1153 error("setluid: %s", strerror(errno));
1154#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1155
Damien Millerad833b32000-08-23 10:46:23 +10001156 if (setlogin(pw->pw_name) < 0)
1157 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001158 if (setgid(pw->pw_gid) < 0) {
1159 perror("setgid");
1160 exit(1);
1161 }
1162 /* Initialize the group list. */
1163 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1164 perror("initgroups");
1165 exit(1);
1166 }
1167 endgrent();
Damien Millerf9e93002001-03-27 16:12:24 +10001168# ifdef USE_PAM
1169 /*
1170 * PAM credentials may take the form of
1171 * supplementary groups. These will have been
1172 * wiped by the above initgroups() call.
1173 * Reestablish them here.
1174 */
1175 do_pam_setcred(0);
1176# endif /* USE_PAM */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001177# ifdef WITH_IRIX_JOBS
1178 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1179 if (jid == -1) {
1180 fatal("Failed to create job container: %.100s",
1181 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001182 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001183# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001184# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001185 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001186 if (jid == 0) {
1187 if (newarraysess() != 0)
1188 fatal("Failed to set up new array session: %.100s",
1189 strerror(errno));
1190 }
Damien Millerad833b32000-08-23 10:46:23 +10001191# endif /* WITH_IRIX_ARRAY */
1192# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001193 /* initialize irix project info */
1194 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1195 debug("Failed to get project id, using projid 0");
1196 projid = 0;
1197 }
Damien Miller91606b12000-06-28 08:22:29 +10001198 if (setprid(projid))
1199 fatal("Failed to initialize project %d for %s: %.100s",
1200 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001201# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001202#ifdef WITH_IRIX_AUDIT
1203 if (sysconf(_SC_AUDIT)) {
1204 debug("Setting sat id to %d", (int) pw->pw_uid);
1205 if (satsetid(pw->pw_uid))
1206 debug("error setting satid: %.100s", strerror(errno));
1207 }
1208#endif /* WITH_IRIX_AUDIT */
1209
Damien Miller168a7002001-03-17 10:29:50 +11001210#ifdef _AIX
1211 /*
1212 * AIX has a "usrinfo" area where logname and
1213 * other stuff is stored - a few applications
1214 * actually use this and die if it's not set
1215 */
Damien Millerbebd8be2001-03-22 11:58:15 +11001216 if (s->ttyfd == -1)
1217 s->tty[0] = '\0';
Damien Millerbe081762001-03-21 11:11:57 +11001218 cp = xmalloc(22 + strlen(s->tty) +
Damien Miller168a7002001-03-17 10:29:50 +11001219 2 * strlen(pw->pw_name));
1220 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
Damien Millerbebd8be2001-03-22 11:58:15 +11001221 pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0);
Damien Miller168a7002001-03-17 10:29:50 +11001222 if (usrinfo(SETUINFO, cp, i) == -1)
1223 fatal("Couldn't set usrinfo: %s",
1224 strerror(errno));
1225 debug3("AIX/UsrInfo: set len %d", i);
1226 xfree(cp);
1227#endif
1228
Damien Millerb38eff82000-04-01 11:09:21 +10001229 /* Permanently switch to the desired uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001230 permanently_set_uid(pw);
Damien Millerad833b32000-08-23 10:46:23 +10001231# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001232 }
Damien Millerad833b32000-08-23 10:46:23 +10001233#endif /* HAVE_OSF_SIA */
1234
Damien Millerbac2d8a2000-09-05 16:13:06 +11001235#ifdef HAVE_CYGWIN
1236 if (is_winnt)
1237#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001238 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001239 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001240 }
1241 /*
1242 * Get the shell from the password data. An empty shell field is
1243 * legal, and means /bin/sh.
1244 */
1245 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001246#ifdef HAVE_LOGIN_CAP
1247 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1248#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001249
1250#ifdef AFS
1251 /* Try to get AFS tokens for the local cell. */
1252 if (k_hasafs()) {
1253 char cell[64];
1254
1255 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1256 krb_afslog(cell, 0);
1257
1258 krb_afslog(0, 0);
1259 }
1260#endif /* AFS */
1261
1262 /* Initialize the environment. */
1263 envsize = 100;
1264 env = xmalloc(envsize * sizeof(char *));
1265 env[0] = NULL;
1266
Damien Millerbac2d8a2000-09-05 16:13:06 +11001267#ifdef HAVE_CYGWIN
1268 /*
1269 * The Windows environment contains some setting which are
1270 * important for a running system. They must not be dropped.
1271 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001272 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001273#endif
1274
Damien Millerb38eff82000-04-01 11:09:21 +10001275 if (!options.use_login) {
1276 /* Set basic environment. */
1277 child_set_env(&env, &envsize, "USER", pw->pw_name);
1278 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1279 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001280#ifdef HAVE_LOGIN_CAP
1281 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1282 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001283#else /* HAVE_LOGIN_CAP */
1284# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001285 /*
1286 * There's no standard path on Windows. The path contains
1287 * important components pointing to the system directories,
1288 * needed for loading shared libraries. So the path better
1289 * remains intact here.
1290 */
Damien Millerb38eff82000-04-01 11:09:21 +10001291 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001292# endif /* HAVE_CYGWIN */
1293#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001294
1295 snprintf(buf, sizeof buf, "%.200s/%.50s",
1296 _PATH_MAILDIR, pw->pw_name);
1297 child_set_env(&env, &envsize, "MAIL", buf);
1298
1299 /* Normal systems set SHELL by default. */
1300 child_set_env(&env, &envsize, "SHELL", shell);
1301 }
1302 if (getenv("TZ"))
1303 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1304
1305 /* Set custom environment options from RSA authentication. */
1306 while (custom_environment) {
1307 struct envstring *ce = custom_environment;
1308 char *s = ce->s;
1309 int i;
1310 for (i = 0; s[i] != '=' && s[i]; i++);
1311 if (s[i] == '=') {
1312 s[i] = 0;
1313 child_set_env(&env, &envsize, s, s + i + 1);
1314 }
1315 custom_environment = ce->next;
1316 xfree(ce->s);
1317 xfree(ce);
1318 }
1319
1320 snprintf(buf, sizeof buf, "%.50s %d %d",
1321 get_remote_ipaddr(), get_remote_port(), get_local_port());
1322 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1323
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001324 if (s->ttyfd != -1)
1325 child_set_env(&env, &envsize, "SSH_TTY", s->tty);
1326 if (s->term)
1327 child_set_env(&env, &envsize, "TERM", s->term);
1328 if (s->display)
1329 child_set_env(&env, &envsize, "DISPLAY", s->display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001330 if (original_command)
1331 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1332 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001333
1334#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001335 if ((cp = getenv("AUTHSTATE")) != NULL)
1336 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1337 if ((cp = getenv("KRB5CCNAME")) != NULL)
1338 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1339 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001340#endif
1341
1342#ifdef KRB4
1343 {
1344 extern char *ticket;
1345
1346 if (ticket)
1347 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1348 }
1349#endif /* KRB4 */
1350
1351#ifdef USE_PAM
1352 /* Pull in any environment variables that may have been set by PAM. */
1353 do_pam_environment(&env, &envsize);
1354#endif /* USE_PAM */
1355
Damien Millerb38eff82000-04-01 11:09:21 +10001356 if (xauthfile)
1357 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1358 if (auth_get_socket_name() != NULL)
1359 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1360 auth_get_socket_name());
1361
1362 /* read $HOME/.ssh/environment. */
1363 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001364 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1365 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001366 read_environment_file(&env, &envsize, buf);
1367 }
1368 if (debug_flag) {
1369 /* dump the environment */
1370 fprintf(stderr, "Environment:\n");
1371 for (i = 0; env[i]; i++)
1372 fprintf(stderr, " %.200s\n", env[i]);
1373 }
Damien Millerad833b32000-08-23 10:46:23 +10001374 /* we have to stash the hostname before we close our socket. */
1375 if (options.use_login)
Ben Lindstromf15a3862001-04-05 23:32:17 +00001376 hostname = get_remote_name_or_ip(utmp_len,
1377 options.reverse_mapping_check);
Damien Millerb38eff82000-04-01 11:09:21 +10001378 /*
1379 * Close the connection descriptors; note that this is the child, and
1380 * the server will still have the socket open, and it is important
1381 * that we do not shutdown it. Note that the descriptors cannot be
1382 * closed before building the environment, as we call
1383 * get_remote_ipaddr there.
1384 */
1385 if (packet_get_connection_in() == packet_get_connection_out())
1386 close(packet_get_connection_in());
1387 else {
1388 close(packet_get_connection_in());
1389 close(packet_get_connection_out());
1390 }
1391 /*
1392 * Close all descriptors related to channels. They will still remain
1393 * open in the parent.
1394 */
1395 /* XXX better use close-on-exec? -markus */
1396 channel_close_all();
1397
1398 /*
1399 * Close any extra file descriptors. Note that there may still be
1400 * descriptors left by system functions. They will be closed later.
1401 */
1402 endpwent();
1403
1404 /*
1405 * Close any extra open file descriptors so that we don\'t have them
1406 * hanging around in clients. Note that we want to do this after
1407 * initgroups, because at least on Solaris 2.3 it leaves file
1408 * descriptors open.
1409 */
1410 for (i = 3; i < 64; i++)
1411 close(i);
1412
1413 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001414 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001415 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1416 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001417#ifdef HAVE_LOGIN_CAP
1418 if (login_getcapbool(lc, "requirehome", 0))
1419 exit(1);
1420#endif
1421 }
Damien Millerb38eff82000-04-01 11:09:21 +10001422
1423 /*
1424 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1425 * xauth are run in the proper environment.
1426 */
1427 environ = env;
1428
1429 /*
1430 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1431 * in this order).
1432 */
1433 if (!options.use_login) {
Ben Lindstrom005dd222001-04-18 15:29:33 +00001434 /* ignore _PATH_SSH_USER_RC for subsystems */
1435 if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001436 snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
1437 shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001438 if (debug_flag)
Ben Lindstrom60402fd2001-05-03 22:37:26 +00001439 fprintf(stderr, "Running %s\n", cmd);
1440 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001441 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001442 if (do_xauth)
1443 fprintf(f, "%s %s\n", s->auth_proto,
1444 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001445 pclose(f);
1446 } else
Ben Lindstrom5428bea2001-05-06 02:53:25 +00001447 fprintf(stderr, "Could not run %s\n",
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001448 _PATH_SSH_USER_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001449 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001450 if (debug_flag)
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001451 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1452 _PATH_SSH_SYSTEM_RC);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001453 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001454 if (f) {
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001455 if (do_xauth)
1456 fprintf(f, "%s %s\n", s->auth_proto,
1457 s->auth_data);
Damien Millerb38eff82000-04-01 11:09:21 +10001458 pclose(f);
1459 } else
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001460 fprintf(stderr, "Could not run %s\n",
1461 _PATH_SSH_SYSTEM_RC);
1462 } else if (do_xauth && options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001463 /* Add authority data to .Xauthority if appropriate. */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001464 char *screen = strchr(s->display, ':');
1465
1466 if (debug_flag) {
1467 fprintf(stderr,
1468 "Running %.100s add "
1469 "%.100s %.100s %.100s\n",
1470 options.xauth_location, s->display,
1471 s->auth_proto, s->auth_data);
1472 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001473 fprintf(stderr,
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001474 "Adding %.*s/unix%s %s %s\n",
1475 (int)(screen - s->display),
1476 s->display, screen,
1477 s->auth_proto, s->auth_data);
1478 }
1479 snprintf(cmd, sizeof cmd, "%s -q -",
1480 options.xauth_location);
1481 f = popen(cmd, "w");
1482 if (f) {
1483 fprintf(f, "add %s %s %s\n", s->display,
1484 s->auth_proto, s->auth_data);
1485 if (screen != NULL)
1486 fprintf(f, "add %.*s/unix%s %s %s\n",
1487 (int)(screen - s->display),
1488 s->display, screen,
1489 s->auth_proto,
1490 s->auth_data);
1491 pclose(f);
1492 } else {
1493 fprintf(stderr, "Could not run %s\n",
1494 cmd);
Damien Millerb38eff82000-04-01 11:09:21 +10001495 }
1496 }
Damien Millerb38eff82000-04-01 11:09:21 +10001497 /* Get the last component of the shell name. */
1498 cp = strrchr(shell, '/');
1499 if (cp)
1500 cp++;
1501 else
1502 cp = shell;
1503 }
Ben Lindstromde71cda2001-03-24 00:43:26 +00001504
1505 /* restore SIGPIPE for child */
1506 signal(SIGPIPE, SIG_DFL);
1507
Damien Millerb38eff82000-04-01 11:09:21 +10001508 /*
1509 * If we have no command, execute the shell. In this case, the shell
1510 * name to be passed in argv[0] is preceded by '-' to indicate that
1511 * this is a login shell.
1512 */
1513 if (!command) {
1514 if (!options.use_login) {
1515 char buf[256];
1516
1517 /*
1518 * Check for mail if we have a tty and it was enabled
1519 * in server options.
1520 */
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001521 if (s->ttyfd != -1 && options.check_mail) {
Damien Millerb38eff82000-04-01 11:09:21 +10001522 char *mailbox;
1523 struct stat mailstat;
Ben Lindstrom86fe8682001-03-17 00:32:57 +00001524
Damien Millerb38eff82000-04-01 11:09:21 +10001525 mailbox = getenv("MAIL");
1526 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001527 if (stat(mailbox, &mailstat) != 0 ||
1528 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001529 printf("No mail.\n");
1530 else if (mailstat.st_mtime < mailstat.st_atime)
1531 printf("You have mail.\n");
1532 else
1533 printf("You have new mail.\n");
1534 }
1535 }
1536 /* Start the shell. Set initial character to '-'. */
1537 buf[0] = '-';
1538 strncpy(buf + 1, cp, sizeof(buf) - 1);
1539 buf[sizeof(buf) - 1] = 0;
1540
1541 /* Execute the shell. */
1542 argv[0] = buf;
1543 argv[1] = NULL;
1544 execve(shell, argv, env);
1545
1546 /* Executing the shell failed. */
1547 perror(shell);
1548 exit(1);
1549
1550 } else {
1551 /* Launch login(1). */
1552
Damien Millerad833b32000-08-23 10:46:23 +10001553 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Ben Lindstrom97c677d2001-05-08 20:33:05 +00001554#ifdef LOGIN_NEEDS_TERM
1555 s->term? s->term : "unknown",
1556#endif
Damien Miller942da032000-08-18 13:59:06 +10001557 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001558
1559 /* Login couldn't be executed, die. */
1560
1561 perror("login");
1562 exit(1);
1563 }
1564 }
1565 /*
1566 * Execute the command using the user's shell. This uses the -c
1567 * option to execute the command.
1568 */
1569 argv[0] = (char *) cp;
1570 argv[1] = "-c";
1571 argv[2] = (char *) command;
1572 argv[3] = NULL;
1573 execve(shell, argv, env);
1574 perror(shell);
1575 exit(1);
1576}
1577
1578Session *
1579session_new(void)
1580{
1581 int i;
1582 static int did_init = 0;
1583 if (!did_init) {
1584 debug("session_new: init");
1585 for(i = 0; i < MAX_SESSIONS; i++) {
1586 sessions[i].used = 0;
1587 sessions[i].self = i;
1588 }
1589 did_init = 1;
1590 }
1591 for(i = 0; i < MAX_SESSIONS; i++) {
1592 Session *s = &sessions[i];
1593 if (! s->used) {
Ben Lindstrom60294322001-03-26 05:38:25 +00001594 memset(s, 0, sizeof(*s));
Damien Millerb38eff82000-04-01 11:09:21 +10001595 s->chanid = -1;
1596 s->ptyfd = -1;
1597 s->ttyfd = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001598 s->used = 1;
Damien Miller15b29522000-10-14 12:33:48 +11001599 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001600 return s;
1601 }
1602 }
1603 return NULL;
1604}
1605
1606void
1607session_dump(void)
1608{
1609 int i;
1610 for(i = 0; i < MAX_SESSIONS; i++) {
1611 Session *s = &sessions[i];
1612 debug("dump: used %d session %d %p channel %d pid %d",
1613 s->used,
1614 s->self,
1615 s,
1616 s->chanid,
1617 s->pid);
1618 }
1619}
1620
Damien Millerefb4afe2000-04-12 18:45:05 +10001621int
1622session_open(int chanid)
1623{
1624 Session *s = session_new();
1625 debug("session_open: channel %d", chanid);
1626 if (s == NULL) {
1627 error("no more sessions");
1628 return 0;
1629 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001630 s->pw = auth_get_user();
1631 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001632 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001633 debug("session_open: session %d: link with channel %d", s->self, chanid);
1634 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001635 return 1;
1636}
1637
1638Session *
1639session_by_channel(int id)
1640{
1641 int i;
1642 for(i = 0; i < MAX_SESSIONS; i++) {
1643 Session *s = &sessions[i];
1644 if (s->used && s->chanid == id) {
1645 debug("session_by_channel: session %d channel %d", i, id);
1646 return s;
1647 }
1648 }
1649 debug("session_by_channel: unknown channel %d", id);
1650 session_dump();
1651 return NULL;
1652}
1653
1654Session *
1655session_by_pid(pid_t pid)
1656{
1657 int i;
1658 debug("session_by_pid: pid %d", pid);
1659 for(i = 0; i < MAX_SESSIONS; i++) {
1660 Session *s = &sessions[i];
1661 if (s->used && s->pid == pid)
1662 return s;
1663 }
1664 error("session_by_pid: unknown pid %d", pid);
1665 session_dump();
1666 return NULL;
1667}
1668
1669int
1670session_window_change_req(Session *s)
1671{
1672 s->col = packet_get_int();
1673 s->row = packet_get_int();
1674 s->xpixel = packet_get_int();
1675 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001676 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001677 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1678 return 1;
1679}
1680
1681int
1682session_pty_req(Session *s)
1683{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001684 u_int len;
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001685 int n_bytes;
Damien Millerefb4afe2000-04-12 18:45:05 +10001686
Damien Millerf6d9e222000-06-18 14:50:44 +10001687 if (no_pty_flag)
1688 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001689 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001690 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001691 s->term = packet_get_string(&len);
1692 s->col = packet_get_int();
1693 s->row = packet_get_int();
1694 s->xpixel = packet_get_int();
1695 s->ypixel = packet_get_int();
1696
1697 if (strcmp(s->term, "") == 0) {
1698 xfree(s->term);
1699 s->term = NULL;
1700 }
1701 /* Allocate a pty and open it. */
1702 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1703 xfree(s->term);
1704 s->term = NULL;
1705 s->ptyfd = -1;
1706 s->ttyfd = -1;
1707 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001708 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001709 }
1710 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1711 /*
1712 * Add a cleanup function to clear the utmp entry and record logout
1713 * time in case we call fatal() (e.g., the connection gets closed).
1714 */
1715 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1716 pty_setowner(s->pw, s->tty);
1717 /* Get window size from the packet. */
1718 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1719
Ben Lindstromae8e2d32001-04-14 23:13:02 +00001720 /* Get tty modes from the packet. */
1721 tty_parse_modes(s->ttyfd, &n_bytes);
1722 packet_done();
1723
Damien Millere247cc42000-05-07 12:03:14 +10001724 session_proctitle(s);
1725
Damien Millerefb4afe2000-04-12 18:45:05 +10001726 return 1;
1727}
1728
Damien Millerbd483e72000-04-30 10:00:53 +10001729int
1730session_subsystem_req(Session *s)
1731{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001732 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001733 int success = 0;
1734 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001735 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001736
1737 packet_done();
1738 log("subsystem request for %s", subsys);
1739
Damien Millerf6d9e222000-06-18 14:50:44 +10001740 for (i = 0; i < options.num_subsystems; i++) {
1741 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1742 debug("subsystem: exec() %s", options.subsystem_command[i]);
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001743 s->is_subsystem = 1;
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001744 do_exec_no_pty(s, options.subsystem_command[i]);
Damien Millerf6d9e222000-06-18 14:50:44 +10001745 success = 1;
1746 }
1747 }
1748
1749 if (!success)
1750 log("subsystem request for %s failed, subsystem not found", subsys);
1751
Damien Millerbd483e72000-04-30 10:00:53 +10001752 xfree(subsys);
1753 return success;
1754}
1755
1756int
1757session_x11_req(Session *s)
1758{
Damien Miller7b28dc52000-09-05 13:34:53 +11001759 int fd;
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +00001760 struct stat st;
Damien Miller37023962000-07-11 17:31:38 +10001761 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001762 debug("X11 forwarding disabled in user configuration file.");
1763 return 0;
1764 }
Damien Millerbd483e72000-04-30 10:00:53 +10001765 if (!options.x11_forwarding) {
1766 debug("X11 forwarding disabled in server configuration file.");
1767 return 0;
1768 }
Ben Lindstrom7d68fbf2001-06-05 19:29:20 +00001769 if (!options.xauth_location ||
1770 (stat(options.xauth_location, &st) == -1)) {
1771 packet_send_debug("No xauth program; cannot forward with spoofing.");
1772 return 0;
1773 }
Damien Millerbd483e72000-04-30 10:00:53 +10001774 if (xauthfile != NULL) {
1775 debug("X11 fwd already started.");
1776 return 0;
1777 }
1778
1779 debug("Received request for X11 forwarding with auth spoofing.");
1780 if (s->display != NULL)
1781 packet_disconnect("Protocol error: X11 display already set.");
1782
1783 s->single_connection = packet_get_char();
1784 s->auth_proto = packet_get_string(NULL);
1785 s->auth_data = packet_get_string(NULL);
1786 s->screen = packet_get_int();
1787 packet_done();
1788
1789 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1790 if (s->display == NULL) {
1791 xfree(s->auth_proto);
1792 xfree(s->auth_data);
1793 return 0;
1794 }
1795 xauthfile = xmalloc(MAXPATHLEN);
1796 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00001797 temporarily_use_uid(s->pw);
Damien Millerbd483e72000-04-30 10:00:53 +10001798 if (mkdtemp(xauthfile) == NULL) {
1799 restore_uid();
1800 error("private X11 dir: mkdtemp %s failed: %s",
1801 xauthfile, strerror(errno));
1802 xfree(xauthfile);
1803 xauthfile = NULL;
1804 xfree(s->auth_proto);
1805 xfree(s->auth_data);
1806 /* XXXX remove listening channels */
1807 return 0;
1808 }
1809 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001810 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1811 if (fd >= 0)
1812 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001813 restore_uid();
1814 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1815 return 1;
1816}
1817
Damien Millerf6d9e222000-06-18 14:50:44 +10001818int
1819session_shell_req(Session *s)
1820{
1821 /* if forced_command == NULL, the shell is execed */
1822 char *shell = forced_command;
1823 packet_done();
Damien Millerf6d9e222000-06-18 14:50:44 +10001824 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001825 do_exec_no_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001826 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001827 do_exec_pty(s, shell);
Damien Millerf6d9e222000-06-18 14:50:44 +10001828 return 1;
1829}
1830
1831int
1832session_exec_req(Session *s)
1833{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001834 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001835 char *command = packet_get_string(&len);
1836 packet_done();
1837 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001838 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001839 command = forced_command;
1840 debug("Forced command '%.500s'", forced_command);
1841 }
Damien Millerf6d9e222000-06-18 14:50:44 +10001842 if (s->ttyfd == -1)
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001843 do_exec_no_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001844 else
Ben Lindstromb4c961d2001-03-22 01:25:37 +00001845 do_exec_pty(s, command);
Damien Millerf6d9e222000-06-18 14:50:44 +10001846 if (forced_command == NULL)
1847 xfree(command);
1848 return 1;
1849}
1850
Damien Miller0bc1bd82000-11-13 22:57:25 +11001851int
1852session_auth_agent_req(Session *s)
1853{
1854 static int called = 0;
1855 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001856 if (no_agent_forwarding_flag) {
1857 debug("session_auth_agent_req: no_agent_forwarding_flag");
1858 return 0;
1859 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001860 if (called) {
1861 return 0;
1862 } else {
1863 called = 1;
1864 return auth_input_request_forwarding(s->pw);
1865 }
1866}
1867
Damien Millerefb4afe2000-04-12 18:45:05 +10001868void
1869session_input_channel_req(int id, void *arg)
1870{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001871 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001872 int reply;
1873 int success = 0;
1874 char *rtype;
1875 Session *s;
1876 Channel *c;
1877
1878 rtype = packet_get_string(&len);
1879 reply = packet_get_char();
1880
1881 s = session_by_channel(id);
1882 if (s == NULL)
1883 fatal("session_input_channel_req: channel %d: no session", id);
1884 c = channel_lookup(id);
1885 if (c == NULL)
1886 fatal("session_input_channel_req: channel %d: bad channel", id);
1887
1888 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1889 s->self, id, rtype, reply);
1890
1891 /*
Ben Lindstromfc9b07d2001-03-22 01:27:23 +00001892 * a session is in LARVAL state until a shell, a command
1893 * or a subsystem is executed
Damien Millerefb4afe2000-04-12 18:45:05 +10001894 */
1895 if (c->type == SSH_CHANNEL_LARVAL) {
1896 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001897 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001898 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001899 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001900 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001901 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001902 } else if (strcmp(rtype, "x11-req") == 0) {
1903 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001904 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1905 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001906 } else if (strcmp(rtype, "subsystem") == 0) {
1907 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001908 }
1909 }
1910 if (strcmp(rtype, "window-change") == 0) {
1911 success = session_window_change_req(s);
1912 }
1913
1914 if (reply) {
1915 packet_start(success ?
1916 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1917 packet_put_int(c->remote_id);
1918 packet_send();
1919 }
1920 xfree(rtype);
1921}
1922
1923void
1924session_set_fds(Session *s, int fdin, int fdout, int fderr)
1925{
1926 if (!compat20)
1927 fatal("session_set_fds: called for proto != 2.0");
1928 /*
1929 * now that have a child and a pipe to the child,
1930 * we can activate our channel and register the fd's
1931 */
1932 if (s->chanid == -1)
1933 fatal("no channel for session %d", s->self);
1934 channel_set_fds(s->chanid,
1935 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001936 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1937 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001938}
1939
Damien Millerb38eff82000-04-01 11:09:21 +10001940void
1941session_pty_cleanup(Session *s)
1942{
1943 if (s == NULL || s->ttyfd == -1)
1944 return;
1945
Kevin Steves43cdef32001-02-11 14:12:08 +00001946 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001947
1948 /* Cancel the cleanup function. */
1949 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1950
1951 /* Record that the user has logged out. */
1952 record_logout(s->pid, s->tty);
1953
1954 /* Release the pseudo-tty. */
1955 pty_release(s->tty);
1956
1957 /*
1958 * Close the server side of the socket pairs. We must do this after
1959 * the pty cleanup, so that another process doesn't get this pty
1960 * while we're still cleaning up.
1961 */
1962 if (close(s->ptymaster) < 0)
1963 error("close(s->ptymaster): %s", strerror(errno));
1964}
Damien Millerefb4afe2000-04-12 18:45:05 +10001965
1966void
1967session_exit_message(Session *s, int status)
1968{
1969 Channel *c;
1970 if (s == NULL)
1971 fatal("session_close: no session");
1972 c = channel_lookup(s->chanid);
1973 if (c == NULL)
1974 fatal("session_close: session %d: no channel %d",
1975 s->self, s->chanid);
1976 debug("session_exit_message: session %d channel %d pid %d",
1977 s->self, s->chanid, s->pid);
1978
1979 if (WIFEXITED(status)) {
1980 channel_request_start(s->chanid,
1981 "exit-status", 0);
1982 packet_put_int(WEXITSTATUS(status));
1983 packet_send();
1984 } else if (WIFSIGNALED(status)) {
1985 channel_request_start(s->chanid,
1986 "exit-signal", 0);
1987 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001988#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001989 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001990#else /* WCOREDUMP */
1991 packet_put_char(0);
1992#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001993 packet_put_cstring("");
1994 packet_put_cstring("");
1995 packet_send();
1996 } else {
1997 /* Some weird exit cause. Just exit. */
1998 packet_disconnect("wait returned status %04x.", status);
1999 }
2000
2001 /* disconnect channel */
2002 debug("session_exit_message: release channel %d", s->chanid);
2003 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10002004 /*
2005 * emulate a write failure with 'chan_write_failed', nobody will be
2006 * interested in data we write.
2007 * Note that we must not call 'chan_read_failed', since there could
2008 * be some more data waiting in the pipe.
2009 */
Damien Millerbd483e72000-04-30 10:00:53 +10002010 if (c->ostate != CHAN_OUTPUT_CLOSED)
2011 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10002012 s->chanid = -1;
2013}
2014
2015void
2016session_free(Session *s)
2017{
2018 debug("session_free: session %d pid %d", s->self, s->pid);
2019 if (s->term)
2020 xfree(s->term);
2021 if (s->display)
2022 xfree(s->display);
2023 if (s->auth_data)
2024 xfree(s->auth_data);
2025 if (s->auth_proto)
2026 xfree(s->auth_proto);
2027 s->used = 0;
2028}
2029
2030void
2031session_close(Session *s)
2032{
2033 session_pty_cleanup(s);
2034 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10002035 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10002036}
2037
2038void
2039session_close_by_pid(pid_t pid, int status)
2040{
2041 Session *s = session_by_pid(pid);
2042 if (s == NULL) {
2043 debug("session_close_by_pid: no session for pid %d", s->pid);
2044 return;
2045 }
2046 if (s->chanid != -1)
2047 session_exit_message(s, status);
2048 session_close(s);
2049}
2050
2051/*
2052 * this is called when a channel dies before
2053 * the session 'child' itself dies
2054 */
2055void
2056session_close_by_channel(int id, void *arg)
2057{
2058 Session *s = session_by_channel(id);
2059 if (s == NULL) {
2060 debug("session_close_by_channel: no session for channel %d", id);
2061 return;
2062 }
2063 /* disconnect channel */
2064 channel_cancel_cleanup(s->chanid);
2065 s->chanid = -1;
2066
2067 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
2068 if (s->pid == 0) {
2069 /* close session immediately */
2070 session_close(s);
2071 } else {
2072 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10002073 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10002074 error("session_close_by_channel: kill %d: %s",
2075 s->pid, strerror(errno));
2076 }
2077}
2078
Damien Millere247cc42000-05-07 12:03:14 +10002079char *
2080session_tty_list(void)
2081{
2082 static char buf[1024];
2083 int i;
2084 buf[0] = '\0';
2085 for(i = 0; i < MAX_SESSIONS; i++) {
2086 Session *s = &sessions[i];
2087 if (s->used && s->ttyfd != -1) {
2088 if (buf[0] != '\0')
2089 strlcat(buf, ",", sizeof buf);
2090 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2091 }
2092 }
2093 if (buf[0] == '\0')
2094 strlcpy(buf, "notty", sizeof buf);
2095 return buf;
2096}
2097
2098void
2099session_proctitle(Session *s)
2100{
2101 if (s->pw == NULL)
2102 error("no user for session %d", s->self);
2103 else
2104 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2105}
2106
Damien Millerefb4afe2000-04-12 18:45:05 +10002107void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002108do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002109{
Ben Lindstromb31783d2001-03-22 02:02:12 +00002110
Damien Millerefb4afe2000-04-12 18:45:05 +10002111 server_loop2();
2112}