blob: e862c6504acb8f7df4a364c40f46864800d471ee [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.
12 * 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 Lindstrom8dcdeb82001-02-16 16:02:14 +000036RCSID("$OpenBSD: session.c,v 1.56 2001/02/16 14:03:43 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"
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 Millerb38eff82000-04-01 11:09:21 +100092/* types */
93
94#define TTYSZ 64
95typedef struct Session Session;
96struct Session {
97 int used;
98 int self;
Damien Millerbd483e72000-04-30 10:00:53 +100099 int extended;
Damien Millerb38eff82000-04-01 11:09:21 +1000100 struct passwd *pw;
101 pid_t pid;
102 /* tty */
103 char *term;
104 int ptyfd, ttyfd, ptymaster;
105 int row, col, xpixel, ypixel;
106 char tty[TTYSZ];
107 /* X11 */
108 char *display;
109 int screen;
110 char *auth_proto;
111 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000112 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000113 /* proto 2 */
114 int chanid;
115};
116
117/* func */
118
119Session *session_new(void);
120void session_set_fds(Session *s, int fdin, int fdout, int fderr);
121void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000122void session_proctitle(Session *s);
Damien Millerb38eff82000-04-01 11:09:21 +1000123void do_exec_pty(Session *s, const char *command, struct passwd * pw);
124void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
Damien Miller69b69aa2000-10-28 14:19:58 +1100125void do_login(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000126
127void
128do_child(const char *command, struct passwd * pw, const char *term,
129 const char *display, const char *auth_proto,
130 const char *auth_data, const char *ttyname);
131
132/* import */
133extern ServerOptions options;
134extern char *__progname;
135extern int log_stderr;
136extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000137extern u_int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000138
Damien Miller37023962000-07-11 17:31:38 +1000139extern int startup_pipe;
140
Damien Millerb38eff82000-04-01 11:09:21 +1000141/* Local Xauthority file. */
142static char *xauthfile;
143
Damien Miller7b28dc52000-09-05 13:34:53 +1100144/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000145char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* data */
148#define MAX_SESSIONS 10
149Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100150
Damien Millerd2c208a2000-05-17 22:00:02 +1000151#ifdef WITH_AIXAUTHENTICATE
152/* AIX's lastlogin message, set in auth1.c */
153char *aixloginmsg;
154#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000155
Damien Millerad833b32000-08-23 10:46:23 +1000156#ifdef HAVE_LOGIN_CAP
157static login_cap_t *lc;
158#endif
159
Damien Millerb38eff82000-04-01 11:09:21 +1000160/*
161 * Remove local Xauthority file.
162 */
163void
164xauthfile_cleanup_proc(void *ignore)
165{
166 debug("xauthfile_cleanup_proc called");
167
168 if (xauthfile != NULL) {
169 char *p;
170 unlink(xauthfile);
171 p = strrchr(xauthfile, '/');
172 if (p != NULL) {
173 *p = '\0';
174 rmdir(xauthfile);
175 }
176 xfree(xauthfile);
177 xauthfile = NULL;
178 }
179}
180
181/*
182 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
183 * dropped connection).
184 */
Damien Miller4af51302000-04-16 11:18:38 +1000185void
Damien Millerb38eff82000-04-01 11:09:21 +1000186pty_cleanup_proc(void *session)
187{
188 Session *s=session;
189 if (s == NULL)
190 fatal("pty_cleanup_proc: no session");
191 debug("pty_cleanup_proc: %s", s->tty);
192
193 if (s->pid != 0) {
194 /* Record that the user has logged out. */
195 record_logout(s->pid, s->tty);
196 }
197
198 /* Release the pseudo-tty. */
199 pty_release(s->tty);
200}
201
202/*
203 * Prepares for an interactive session. This is called after the user has
204 * been successfully authenticated. During this message exchange, pseudo
205 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
206 * are requested, etc.
207 */
Damien Miller4af51302000-04-16 11:18:38 +1000208void
Damien Millerb38eff82000-04-01 11:09:21 +1000209do_authenticated(struct passwd * pw)
210{
211 Session *s;
Damien Miller7b28dc52000-09-05 13:34:53 +1100212 int type, fd;
Damien Millerb38eff82000-04-01 11:09:21 +1000213 int compression_level = 0, enable_compression_after_reply = 0;
214 int have_pty = 0;
215 char *command;
216 int n_bytes;
217 int plen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000218 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000219
220 /*
221 * Cancel the alarm we set to limit the time taken for
222 * authentication.
223 */
224 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +1000225 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +1000226 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +1000227 startup_pipe = -1;
228 }
Damien Millerb38eff82000-04-01 11:09:21 +1000229
230 /*
231 * Inform the channel mechanism that we are the server side and that
232 * the client may request to connect to any port at all. (The user
233 * could do it anyway, and we wouldn\'t know what is permitted except
234 * by the client telling us, so we can equally well trust the client
235 * not to request anything bogus.)
236 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100237 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
Damien Millerb38eff82000-04-01 11:09:21 +1000238 channel_permit_all_opens();
239
240 s = session_new();
Damien Millerbd483e72000-04-30 10:00:53 +1000241 s->pw = pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000242
Kevin Steves48b7cc02000-10-07 13:24:00 +0000243#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +1000244 if ((lc = login_getclass(pw->pw_class)) == NULL) {
245 error("unable to get login class");
246 return;
247 }
248#endif
249
Damien Millerb38eff82000-04-01 11:09:21 +1000250 /*
251 * We stay in this loop until the client requests to execute a shell
252 * or a command.
253 */
254 for (;;) {
255 int success = 0;
256
257 /* Get a packet from the client. */
258 type = packet_read(&plen);
259
260 /* Process the packet. */
261 switch (type) {
262 case SSH_CMSG_REQUEST_COMPRESSION:
263 packet_integrity_check(plen, 4, type);
264 compression_level = packet_get_int();
265 if (compression_level < 1 || compression_level > 9) {
266 packet_send_debug("Received illegal compression level %d.",
267 compression_level);
268 break;
269 }
270 /* Enable compression after we have responded with SUCCESS. */
271 enable_compression_after_reply = 1;
272 success = 1;
273 break;
274
275 case SSH_CMSG_REQUEST_PTY:
276 if (no_pty_flag) {
277 debug("Allocating a pty not permitted for this authentication.");
278 break;
279 }
280 if (have_pty)
281 packet_disconnect("Protocol error: you already have a pty.");
282
283 debug("Allocating pty.");
284
285 /* Allocate a pty and open it. */
286 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
287 sizeof(s->tty))) {
288 error("Failed to allocate pty.");
289 break;
290 }
291 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
292 pty_setowner(pw, s->tty);
293
294 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
295 s->term = packet_get_string(&dlen);
296 packet_integrity_check(dlen, strlen(s->term), type);
297 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
298 /* Remaining bytes */
299 n_bytes = plen - (4 + dlen + 4 * 4);
300
301 if (strcmp(s->term, "") == 0) {
302 xfree(s->term);
303 s->term = NULL;
304 }
305 /* Get window size from the packet. */
306 s->row = packet_get_int();
307 s->col = packet_get_int();
308 s->xpixel = packet_get_int();
309 s->ypixel = packet_get_int();
310 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
311
312 /* Get tty modes from the packet. */
313 tty_parse_modes(s->ttyfd, &n_bytes);
314 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
315
Damien Millere247cc42000-05-07 12:03:14 +1000316 session_proctitle(s);
317
Damien Millerb38eff82000-04-01 11:09:21 +1000318 /* Indicate that we now have a pty. */
319 success = 1;
320 have_pty = 1;
321 break;
322
323 case SSH_CMSG_X11_REQUEST_FORWARDING:
324 if (!options.x11_forwarding) {
325 packet_send_debug("X11 forwarding disabled in server configuration file.");
326 break;
327 }
Damien Miller0c043c12000-06-07 21:22:38 +1000328 if (!options.xauth_location) {
329 packet_send_debug("No xauth program; cannot forward with spoofing.");
330 break;
331 }
Damien Millerb38eff82000-04-01 11:09:21 +1000332 if (no_x11_forwarding_flag) {
333 packet_send_debug("X11 forwarding not permitted for this authentication.");
334 break;
335 }
336 debug("Received request for X11 forwarding with auth spoofing.");
337 if (s->display != NULL)
338 packet_disconnect("Protocol error: X11 display already set.");
339
340 s->auth_proto = packet_get_string(&proto_len);
341 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000342
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000343 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER) {
344 debug2("SSH_PROTOFLAG_SCREEN_NUMBER == true");
345 packet_integrity_check(plen,
346 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000347 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000348 } else {
349 debug2("SSH_PROTOFLAG_SCREEN_NUMBER == false");
350 packet_integrity_check(plen,
351 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000352 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000353 }
Damien Millerb38eff82000-04-01 11:09:21 +1000354 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
355
356 if (s->display == NULL)
357 break;
358
359 /* Setup to always have a local .Xauthority. */
360 xauthfile = xmalloc(MAXPATHLEN);
361 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
362 temporarily_use_uid(pw->pw_uid);
363 if (mkdtemp(xauthfile) == NULL) {
364 restore_uid();
365 error("private X11 dir: mkdtemp %s failed: %s",
366 xauthfile, strerror(errno));
367 xfree(xauthfile);
368 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000369 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000370 break;
371 }
372 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100373 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
374 if (fd >= 0)
375 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000376 restore_uid();
377 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
378 success = 1;
379 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000380
381 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
382 if (no_agent_forwarding_flag || compat13) {
383 debug("Authentication agent forwarding not permitted for this authentication.");
384 break;
385 }
386 debug("Received authentication agent forwarding request.");
Damien Miller0c043c12000-06-07 21:22:38 +1000387 success = auth_input_request_forwarding(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000388 break;
389
390 case SSH_CMSG_PORT_FORWARD_REQUEST:
391 if (no_port_forwarding_flag) {
392 debug("Port forwarding not permitted for this authentication.");
393 break;
394 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100395 if (!options.allow_tcp_forwarding) {
396 debug("Port forwarding not permitted.");
397 break;
398 }
Damien Millerb38eff82000-04-01 11:09:21 +1000399 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000400 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000401 success = 1;
402 break;
403
404 case SSH_CMSG_MAX_PACKET_SIZE:
405 if (packet_set_maxsize(packet_get_int()) > 0)
406 success = 1;
407 break;
408
409 case SSH_CMSG_EXEC_SHELL:
410 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000411 if (type == SSH_CMSG_EXEC_CMD) {
412 command = packet_get_string(&dlen);
413 debug("Exec command '%.500s'", command);
414 packet_integrity_check(plen, 4 + dlen, type);
415 } else {
416 command = NULL;
417 packet_integrity_check(plen, 0, type);
418 }
419 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100420 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000421 command = forced_command;
422 debug("Forced command '%.500s'", forced_command);
423 }
424 if (have_pty)
425 do_exec_pty(s, command, pw);
426 else
427 do_exec_no_pty(s, command, pw);
428
429 if (command != NULL)
430 xfree(command);
431 /* Cleanup user's local Xauthority file. */
432 if (xauthfile)
433 xauthfile_cleanup_proc(NULL);
434 return;
435
436 default:
437 /*
438 * Any unknown messages in this phase are ignored,
439 * and a failure message is returned.
440 */
441 log("Unknown packet type received after authentication: %d", type);
442 }
443 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
444 packet_send();
445 packet_write_wait();
446
447 /* Enable compression now that we have replied if appropriate. */
448 if (enable_compression_after_reply) {
449 enable_compression_after_reply = 0;
450 packet_start_compression(compression_level);
451 }
452 }
453}
454
455/*
456 * This is called to fork and execute a command when we have no tty. This
457 * will call do_child from the child, and server_loop from the parent after
458 * setting up file descriptors and such.
459 */
Damien Miller4af51302000-04-16 11:18:38 +1000460void
Damien Millerb38eff82000-04-01 11:09:21 +1000461do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
462{
463 int pid;
464
465#ifdef USE_PIPES
466 int pin[2], pout[2], perr[2];
467 /* Allocate pipes for communicating with the program. */
468 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
469 packet_disconnect("Could not create pipes: %.100s",
470 strerror(errno));
471#else /* USE_PIPES */
472 int inout[2], err[2];
473 /* Uses socket pairs to communicate with the program. */
474 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
475 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
476 packet_disconnect("Could not create socket pairs: %.100s",
477 strerror(errno));
478#endif /* USE_PIPES */
479 if (s == NULL)
480 fatal("do_exec_no_pty: no session");
481
Damien Millere247cc42000-05-07 12:03:14 +1000482 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000483
Damien Millerb38eff82000-04-01 11:09:21 +1000484 /* Fork the child. */
485 if ((pid = fork()) == 0) {
486 /* Child. Reinitialize the log since the pid has changed. */
487 log_init(__progname, options.log_level, options.log_facility, log_stderr);
488
Damien Miller152cea22000-12-13 19:21:51 +1100489 signal(SIGPIPE, SIG_DFL);
490
Damien Millerb38eff82000-04-01 11:09:21 +1000491 /*
492 * Create a new session and process group since the 4.4BSD
493 * setlogin() affects the entire process group.
494 */
495 if (setsid() < 0)
496 error("setsid failed: %.100s", strerror(errno));
497
498#ifdef USE_PIPES
499 /*
500 * Redirect stdin. We close the parent side of the socket
501 * pair, and make the child side the standard input.
502 */
503 close(pin[1]);
504 if (dup2(pin[0], 0) < 0)
505 perror("dup2 stdin");
506 close(pin[0]);
507
508 /* Redirect stdout. */
509 close(pout[0]);
510 if (dup2(pout[1], 1) < 0)
511 perror("dup2 stdout");
512 close(pout[1]);
513
514 /* Redirect stderr. */
515 close(perr[0]);
516 if (dup2(perr[1], 2) < 0)
517 perror("dup2 stderr");
518 close(perr[1]);
519#else /* USE_PIPES */
520 /*
521 * Redirect stdin, stdout, and stderr. Stdin and stdout will
522 * use the same socket, as some programs (particularly rdist)
523 * seem to depend on it.
524 */
525 close(inout[1]);
526 close(err[1]);
527 if (dup2(inout[0], 0) < 0) /* stdin */
528 perror("dup2 stdin");
529 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
530 perror("dup2 stdout");
531 if (dup2(err[0], 2) < 0) /* stderr */
532 perror("dup2 stderr");
533#endif /* USE_PIPES */
534
535 /* Do processing for the child (exec command etc). */
536 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
537 /* NOTREACHED */
538 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100539#ifdef HAVE_CYGWIN
540 if (is_winnt)
541 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
542#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000543 if (pid < 0)
544 packet_disconnect("fork failed: %.100s", strerror(errno));
545 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000546 /* Set interactive/non-interactive mode. */
547 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000548#ifdef USE_PIPES
549 /* We are the parent. Close the child sides of the pipes. */
550 close(pin[0]);
551 close(pout[1]);
552 close(perr[1]);
553
Damien Millerefb4afe2000-04-12 18:45:05 +1000554 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000555 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000556 } else {
557 /* Enter the interactive session. */
558 server_loop(pid, pin[1], pout[0], perr[0]);
559 /* server_loop has closed pin[1], pout[1], and perr[1]. */
560 }
Damien Millerb38eff82000-04-01 11:09:21 +1000561#else /* USE_PIPES */
562 /* We are the parent. Close the child sides of the socket pairs. */
563 close(inout[0]);
564 close(err[0]);
565
566 /*
567 * Enter the interactive session. Note: server_loop must be able to
568 * handle the case that fdin and fdout are the same.
569 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000570 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000571 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000572 } else {
573 server_loop(pid, inout[1], inout[1], err[1]);
574 /* server_loop has closed inout[1] and err[1]. */
575 }
Damien Millerb38eff82000-04-01 11:09:21 +1000576#endif /* USE_PIPES */
577}
578
579/*
580 * This is called to fork and execute a command when we have a tty. This
581 * will call do_child from the child, and server_loop from the parent after
582 * setting up file descriptors, controlling tty, updating wtmp, utmp,
583 * lastlog, and other such operations.
584 */
Damien Miller4af51302000-04-16 11:18:38 +1000585void
Damien Millerb38eff82000-04-01 11:09:21 +1000586do_exec_pty(Session *s, const char *command, struct passwd * pw)
587{
Damien Millerb38eff82000-04-01 11:09:21 +1000588 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000589 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000590
591 if (s == NULL)
592 fatal("do_exec_pty: no session");
593 ptyfd = s->ptyfd;
594 ttyfd = s->ttyfd;
595
Damien Millerb38eff82000-04-01 11:09:21 +1000596 /* Fork the child. */
597 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000598 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000599 log_init(__progname, options.log_level, options.log_facility, log_stderr);
600
Damien Miller152cea22000-12-13 19:21:51 +1100601 signal(SIGPIPE, SIG_DFL);
602
Damien Millerb38eff82000-04-01 11:09:21 +1000603 /* Close the master side of the pseudo tty. */
604 close(ptyfd);
605
606 /* Make the pseudo tty our controlling tty. */
607 pty_make_controlling_tty(&ttyfd, s->tty);
608
609 /* Redirect stdin from the pseudo tty. */
610 if (dup2(ttyfd, fileno(stdin)) < 0)
611 error("dup2 stdin failed: %.100s", strerror(errno));
612
613 /* Redirect stdout to the pseudo tty. */
614 if (dup2(ttyfd, fileno(stdout)) < 0)
615 error("dup2 stdin failed: %.100s", strerror(errno));
616
617 /* Redirect stderr to the pseudo tty. */
618 if (dup2(ttyfd, fileno(stderr)) < 0)
619 error("dup2 stdin failed: %.100s", strerror(errno));
620
621 /* Close the extra descriptor for the pseudo tty. */
622 close(ttyfd);
623
Damien Miller942da032000-08-18 13:59:06 +1000624 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100625 if (!(options.use_login && command == NULL))
626 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000627
Damien Millerb38eff82000-04-01 11:09:21 +1000628 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000629 do_child(command, pw, s->term, s->display, s->auth_proto,
630 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000631 /* NOTREACHED */
632 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100633#ifdef HAVE_CYGWIN
634 if (is_winnt)
635 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
636#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000637 if (pid < 0)
638 packet_disconnect("fork failed: %.100s", strerror(errno));
639 s->pid = pid;
640
641 /* Parent. Close the slave side of the pseudo tty. */
642 close(ttyfd);
643
644 /*
645 * Create another descriptor of the pty master side for use as the
646 * standard input. We could use the original descriptor, but this
647 * simplifies code in server_loop. The descriptor is bidirectional.
648 */
649 fdout = dup(ptyfd);
650 if (fdout < 0)
651 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
652
653 /* we keep a reference to the pty master */
654 ptymaster = dup(ptyfd);
655 if (ptymaster < 0)
656 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
657 s->ptymaster = ptymaster;
658
659 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000660 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000661 if (compat20) {
662 session_set_fds(s, ptyfd, fdout, -1);
663 } else {
664 server_loop(pid, ptyfd, fdout, -1);
665 /* server_loop _has_ closed ptyfd and fdout. */
666 session_pty_cleanup(s);
667 }
Damien Millerb38eff82000-04-01 11:09:21 +1000668}
669
Damien Miller942da032000-08-18 13:59:06 +1000670const char *
671get_remote_name_or_ip(void)
672{
673 static const char *remote = "";
674 if (utmp_len > 0)
Damien Miller33804262001-02-04 23:20:18 +1100675 remote = get_canonical_hostname(options.reverse_mapping_check);
Damien Miller942da032000-08-18 13:59:06 +1000676 if (utmp_len == 0 || strlen(remote) > utmp_len)
677 remote = get_remote_ipaddr();
678 return remote;
679}
680
681/* administrative, login(1)-like work */
682void
Damien Miller69b69aa2000-10-28 14:19:58 +1100683do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000684{
685 FILE *f;
686 char *time_string;
687 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100688 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000689 socklen_t fromlen;
690 struct sockaddr_storage from;
691 struct stat st;
692 time_t last_login_time;
693 struct passwd * pw = s->pw;
694 pid_t pid = getpid();
695
696 /*
697 * Get IP address of client. If the connection is not a socket, let
698 * the address be 0.0.0.0.
699 */
700 memset(&from, 0, sizeof(from));
701 if (packet_connection_is_on_socket()) {
702 fromlen = sizeof(from);
703 if (getpeername(packet_get_connection_in(),
704 (struct sockaddr *) & from, &fromlen) < 0) {
705 debug("getpeername: %.100s", strerror(errno));
706 fatal_cleanup();
707 }
708 }
709
Damien Miller7b28dc52000-09-05 13:34:53 +1100710 /* Get the time and hostname when the user last logged in. */
Damien Millere4340be2000-09-16 13:29:08 +1100711 hostname[0] = '\0';
712 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
713 hostname, sizeof(hostname));
714
Damien Miller942da032000-08-18 13:59:06 +1000715 /* Record that there was a login on that tty from the remote host. */
716 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
717 get_remote_name_or_ip(), (struct sockaddr *)&from);
718
Kevin Steves092f2ef2000-10-14 13:36:13 +0000719#ifdef USE_PAM
720 /*
721 * If password change is needed, do it now.
722 * This needs to occur before the ~/.hushlogin check.
723 */
Damien Miller646aa602001-02-15 11:51:32 +1100724 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000725 print_pam_messages();
726 do_pam_chauthtok();
727 }
728#endif
729
Damien Miller69b69aa2000-10-28 14:19:58 +1100730 /* Done if .hushlogin exists or a command given. */
731 if (command != NULL)
732 return;
Damien Miller942da032000-08-18 13:59:06 +1000733 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000734#ifdef HAVE_LOGIN_CAP
735 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
736#else
Damien Miller942da032000-08-18 13:59:06 +1000737 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000738#endif
Damien Miller942da032000-08-18 13:59:06 +1000739 return;
740
741#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100742 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000743 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000744#endif /* USE_PAM */
745#ifdef WITH_AIXAUTHENTICATE
746 if (aixloginmsg && *aixloginmsg)
747 printf("%s\n", aixloginmsg);
748#endif /* WITH_AIXAUTHENTICATE */
749
Damien Miller942da032000-08-18 13:59:06 +1000750 if (last_login_time != 0) {
751 time_string = ctime(&last_login_time);
752 if (strchr(time_string, '\n'))
753 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000754 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000755 printf("Last login: %s\r\n", time_string);
756 else
Damien Millere4340be2000-09-16 13:29:08 +1100757 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000758 }
759 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000760#ifdef HAVE_LOGIN_CAP
761 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
762 "/etc/motd"), "r");
763#else
Damien Miller942da032000-08-18 13:59:06 +1000764 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000765#endif
Damien Miller942da032000-08-18 13:59:06 +1000766 if (f) {
767 while (fgets(buf, sizeof(buf), f))
768 fputs(buf, stdout);
769 fclose(f);
770 }
771 }
772}
773
Damien Millerb38eff82000-04-01 11:09:21 +1000774/*
775 * Sets the value of the given variable in the environment. If the variable
776 * already exists, its value is overriden.
777 */
Damien Miller4af51302000-04-16 11:18:38 +1000778void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000779child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000780 const char *value)
781{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000782 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000783 char **env;
784
785 /*
786 * Find the slot where the value should be stored. If the variable
787 * already exists, we reuse the slot; otherwise we append a new slot
788 * at the end of the array, expanding if necessary.
789 */
790 env = *envp;
791 namelen = strlen(name);
792 for (i = 0; env[i]; i++)
793 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
794 break;
795 if (env[i]) {
796 /* Reuse the slot. */
797 xfree(env[i]);
798 } else {
799 /* New variable. Expand if necessary. */
800 if (i >= (*envsizep) - 1) {
801 (*envsizep) += 50;
802 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
803 }
804 /* Need to set the NULL pointer at end of array beyond the new slot. */
805 env[i + 1] = NULL;
806 }
807
808 /* Allocate space and format the variable in the appropriate slot. */
809 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
810 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
811}
812
813/*
814 * Reads environment variables from the given file and adds/overrides them
815 * into the environment. If the file does not exist, this does nothing.
816 * Otherwise, it must consist of empty lines, comments (line starts with '#')
817 * and assignments of the form name=value. No other forms are allowed.
818 */
Damien Miller4af51302000-04-16 11:18:38 +1000819void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000820read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000821 const char *filename)
822{
823 FILE *f;
824 char buf[4096];
825 char *cp, *value;
826
827 f = fopen(filename, "r");
828 if (!f)
829 return;
830
831 while (fgets(buf, sizeof(buf), f)) {
832 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
833 ;
834 if (!*cp || *cp == '#' || *cp == '\n')
835 continue;
836 if (strchr(cp, '\n'))
837 *strchr(cp, '\n') = '\0';
838 value = strchr(cp, '=');
839 if (value == NULL) {
840 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
841 continue;
842 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000843 /*
844 * Replace the equals sign by nul, and advance value to
845 * the value string.
846 */
Damien Millerb38eff82000-04-01 11:09:21 +1000847 *value = '\0';
848 value++;
849 child_set_env(env, envsize, cp, value);
850 }
851 fclose(f);
852}
853
854#ifdef USE_PAM
855/*
856 * Sets any environment variables which have been specified by PAM
857 */
858void do_pam_environment(char ***env, int *envsize)
859{
860 char *equals, var_name[512], var_val[512];
861 char **pam_env;
862 int i;
863
864 if ((pam_env = fetch_pam_environment()) == NULL)
865 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000866
Damien Millerb38eff82000-04-01 11:09:21 +1000867 for(i = 0; pam_env[i] != NULL; i++) {
868 if ((equals = strstr(pam_env[i], "=")) == NULL)
869 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000870
Damien Millerb38eff82000-04-01 11:09:21 +1000871 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
872 memset(var_name, '\0', sizeof(var_name));
873 memset(var_val, '\0', sizeof(var_val));
874
875 strncpy(var_name, pam_env[i], equals - pam_env[i]);
876 strcpy(var_val, equals + 1);
877
Damien Millercb5e44a2000-09-29 12:12:36 +1100878 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000879
880 child_set_env(env, envsize, var_name, var_val);
881 }
882 }
883}
884#endif /* USE_PAM */
885
Damien Millercb5e44a2000-09-29 12:12:36 +1100886#ifdef HAVE_CYGWIN
887void copy_environment(char ***env, int *envsize)
888{
889 char *equals, var_name[512], var_val[512];
890 int i;
891
892 for(i = 0; environ[i] != NULL; i++) {
893 if ((equals = strstr(environ[i], "=")) == NULL)
894 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000895
Damien Millercb5e44a2000-09-29 12:12:36 +1100896 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
897 memset(var_name, '\0', sizeof(var_name));
898 memset(var_val, '\0', sizeof(var_val));
899
900 strncpy(var_name, environ[i], equals - environ[i]);
901 strcpy(var_val, equals + 1);
902
903 debug3("Copy environment: %s=%s", var_name, var_val);
904
905 child_set_env(env, envsize, var_name, var_val);
906 }
907 }
908}
909#endif
910
Damien Miller5fc85652000-07-09 23:53:07 +1000911#if defined(HAVE_GETUSERATTR)
912/*
913 * AIX-specific login initialisation
914 */
915void set_limit(char *user, char *soft, char *hard, int resource, int mult)
916{
917 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000918 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000919
920 getrlimit(resource, &rlim);
921
Damien Miller65964d62000-07-11 09:16:22 +1000922 slim = 0;
923 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
924 if (slim < 0) {
925 rlim.rlim_cur = RLIM_INFINITY;
926 } else if (slim != 0) {
927 /* See the wackiness below */
928 if (rlim.rlim_cur == slim * mult)
929 slim = 0;
930 else
931 rlim.rlim_cur = slim * mult;
932 }
933 }
Damien Miller5fc85652000-07-09 23:53:07 +1000934
Damien Miller65964d62000-07-11 09:16:22 +1000935 hlim = 0;
936 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
937 if (hlim < 0) {
938 rlim.rlim_max = RLIM_INFINITY;
939 } else if (hlim != 0) {
940 rlim.rlim_max = hlim * mult;
941 }
942 }
Damien Miller5fc85652000-07-09 23:53:07 +1000943
Damien Miller65964d62000-07-11 09:16:22 +1000944 /*
945 * XXX For cpu and fsize the soft limit is set to the hard limit
946 * if the hard limit is left at its default value and the soft limit
947 * is changed from its default value, either by requesting it
948 * (slim == 0) or by setting it to the current default. At least
949 * that's how rlogind does it. If you're confused you're not alone.
950 * Bug or feature? AIX 4.3.1.2
951 */
952 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
953 && hlim == 0 && slim != 0)
954 rlim.rlim_max = rlim.rlim_cur;
955 /* A specified hard limit limits the soft limit */
956 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
957 rlim.rlim_cur = rlim.rlim_max;
958 /* A soft limit can increase a hard limit */
959 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000960 rlim.rlim_max = rlim.rlim_cur;
961
962 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000963 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000964}
965
966void set_limits_from_userattr(char *user)
967{
968 int mask;
969 char buf[16];
970
971 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
972 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
973 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
974 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
975 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
976 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
977#if defined(S_UNOFILE)
978 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
979#endif
980
981 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
982 /* Convert decimal to octal */
983 (void) snprintf(buf, sizeof(buf), "%d", mask);
984 if (sscanf(buf, "%o", &mask) == 1)
985 umask(mask);
986 }
987}
988#endif /* defined(HAVE_GETUSERATTR) */
989
Damien Millerb38eff82000-04-01 11:09:21 +1000990/*
991 * Performs common processing for the child, such as setting up the
992 * environment, closing extra file descriptors, setting the user and group
993 * ids, and executing the command or shell.
994 */
Damien Miller4af51302000-04-16 11:18:38 +1000995void
Damien Millerb38eff82000-04-01 11:09:21 +1000996do_child(const char *command, struct passwd * pw, const char *term,
997 const char *display, const char *auth_proto,
998 const char *auth_data, const char *ttyname)
999{
Damien Miller7b28dc52000-09-05 13:34:53 +11001000 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001001 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001002 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001003 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001004 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001005 char **env;
1006 extern char **environ;
1007 struct stat st;
1008 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001009#ifdef WITH_IRIX_PROJECT
1010 prid_t projid;
1011#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001012#ifdef WITH_IRIX_JOBS
1013 jid_t jid = 0;
1014#else
1015#ifdef WITH_IRIX_ARRAY
1016 int jid = 0;
1017#endif /* WITH_IRIX_ARRAY */
1018#endif /* WITH_IRIX_JOBS */
1019
Damien Millere8b5b042001-02-15 11:32:15 +11001020#ifdef USE_PAM
1021 do_pam_session(pw->pw_name, ttyname);
1022 do_pam_setcred();
1023#endif /* USE_PAM */
Damien Millerb38eff82000-04-01 11:09:21 +10001024
Damien Millerd3a18572000-06-07 19:55:44 +10001025 /* login(1) is only called if we execute the login shell */
1026 if (options.use_login && command != NULL)
1027 options.use_login = 0;
1028
Damien Millerb38eff82000-04-01 11:09:21 +10001029#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001030 if (!options.use_login) {
1031# ifdef HAVE_LOGIN_CAP
1032 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1033 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1034 _PATH_NOLOGIN), "r");
1035# else /* HAVE_LOGIN_CAP */
1036 if (pw->pw_uid)
1037 f = fopen(_PATH_NOLOGIN, "r");
1038# endif /* HAVE_LOGIN_CAP */
1039 if (f) {
1040 /* /etc/nologin exists. Print its contents and exit. */
1041 while (fgets(buf, sizeof(buf), f))
1042 fputs(buf, stderr);
1043 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001044 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001045 }
Damien Millerb38eff82000-04-01 11:09:21 +10001046 }
1047#endif /* USE_PAM */
1048
Damien Millerad833b32000-08-23 10:46:23 +10001049 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001050 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1051 switch, so we let login(1) to this for us. */
1052 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001053#ifdef HAVE_OSF_SIA
Damien Miller92ddb7d2001-02-14 01:25:23 +11001054 session_setup_sia(pw->pw_name, ttyname);
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001055#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001056#ifdef HAVE_CYGWIN
1057 if (is_winnt) {
1058#else
Damien Millerb38eff82000-04-01 11:09:21 +10001059 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001060#endif
Damien Millerad833b32000-08-23 10:46:23 +10001061# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001062 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001063# endif /* HAVE_GETUSERATTR */
1064# ifdef HAVE_LOGIN_CAP
1065 if (setusercontext(lc, pw, pw->pw_uid,
1066 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1067 perror("unable to set user context");
1068 exit(1);
1069 }
Damien Miller60396b02001-02-18 17:01:00 +11001070#ifdef BSD_AUTH
1071 if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
1072 error("approval failure for %s", pw->pw_name);
1073 fprintf(stderr, "Approval failure");
1074 exit(1);
1075 }
1076#endif
Damien Millerad833b32000-08-23 10:46:23 +10001077# else /* HAVE_LOGIN_CAP */
1078 if (setlogin(pw->pw_name) < 0)
1079 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001080 if (setgid(pw->pw_gid) < 0) {
1081 perror("setgid");
1082 exit(1);
1083 }
1084 /* Initialize the group list. */
1085 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1086 perror("initgroups");
1087 exit(1);
1088 }
1089 endgrent();
Ben Lindstrom980754c2000-11-12 00:04:24 +00001090# ifdef WITH_IRIX_JOBS
1091 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1092 if (jid == -1) {
1093 fatal("Failed to create job container: %.100s",
1094 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001095 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001096# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001097# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001098 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001099 if (jid == 0) {
1100 if (newarraysess() != 0)
1101 fatal("Failed to set up new array session: %.100s",
1102 strerror(errno));
1103 }
Damien Millerad833b32000-08-23 10:46:23 +10001104# endif /* WITH_IRIX_ARRAY */
1105# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001106 /* initialize irix project info */
1107 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1108 debug("Failed to get project id, using projid 0");
1109 projid = 0;
1110 }
Damien Miller91606b12000-06-28 08:22:29 +10001111 if (setprid(projid))
1112 fatal("Failed to initialize project %d for %s: %.100s",
1113 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001114# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001115#ifdef WITH_IRIX_AUDIT
1116 if (sysconf(_SC_AUDIT)) {
1117 debug("Setting sat id to %d", (int) pw->pw_uid);
1118 if (satsetid(pw->pw_uid))
1119 debug("error setting satid: %.100s", strerror(errno));
1120 }
1121#endif /* WITH_IRIX_AUDIT */
1122
Damien Millerb38eff82000-04-01 11:09:21 +10001123 /* Permanently switch to the desired uid. */
1124 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001125# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001126 }
Damien Millerad833b32000-08-23 10:46:23 +10001127#endif /* HAVE_OSF_SIA */
1128
Damien Miller217f5672001-02-16 12:12:41 +11001129#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1130 /* Sets login uid for accounting */
1131 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1132 error("setluid: %s", strerror(errno));
1133#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1134
Damien Millerbac2d8a2000-09-05 16:13:06 +11001135#ifdef HAVE_CYGWIN
1136 if (is_winnt)
1137#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001138 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001139 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001140 }
1141 /*
1142 * Get the shell from the password data. An empty shell field is
1143 * legal, and means /bin/sh.
1144 */
1145 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001146#ifdef HAVE_LOGIN_CAP
1147 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1148#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001149
1150#ifdef AFS
1151 /* Try to get AFS tokens for the local cell. */
1152 if (k_hasafs()) {
1153 char cell[64];
1154
1155 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1156 krb_afslog(cell, 0);
1157
1158 krb_afslog(0, 0);
1159 }
1160#endif /* AFS */
1161
1162 /* Initialize the environment. */
1163 envsize = 100;
1164 env = xmalloc(envsize * sizeof(char *));
1165 env[0] = NULL;
1166
Damien Millerbac2d8a2000-09-05 16:13:06 +11001167#ifdef HAVE_CYGWIN
1168 /*
1169 * The Windows environment contains some setting which are
1170 * important for a running system. They must not be dropped.
1171 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001172 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001173#endif
1174
Damien Millerb38eff82000-04-01 11:09:21 +10001175 if (!options.use_login) {
1176 /* Set basic environment. */
1177 child_set_env(&env, &envsize, "USER", pw->pw_name);
1178 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1179 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001180#ifdef HAVE_LOGIN_CAP
1181 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1182 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001183#else /* HAVE_LOGIN_CAP */
1184# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001185 /*
1186 * There's no standard path on Windows. The path contains
1187 * important components pointing to the system directories,
1188 * needed for loading shared libraries. So the path better
1189 * remains intact here.
1190 */
Damien Millerb38eff82000-04-01 11:09:21 +10001191 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001192# endif /* HAVE_CYGWIN */
1193#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001194
1195 snprintf(buf, sizeof buf, "%.200s/%.50s",
1196 _PATH_MAILDIR, pw->pw_name);
1197 child_set_env(&env, &envsize, "MAIL", buf);
1198
1199 /* Normal systems set SHELL by default. */
1200 child_set_env(&env, &envsize, "SHELL", shell);
1201 }
1202 if (getenv("TZ"))
1203 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1204
1205 /* Set custom environment options from RSA authentication. */
1206 while (custom_environment) {
1207 struct envstring *ce = custom_environment;
1208 char *s = ce->s;
1209 int i;
1210 for (i = 0; s[i] != '=' && s[i]; i++);
1211 if (s[i] == '=') {
1212 s[i] = 0;
1213 child_set_env(&env, &envsize, s, s + i + 1);
1214 }
1215 custom_environment = ce->next;
1216 xfree(ce->s);
1217 xfree(ce);
1218 }
1219
1220 snprintf(buf, sizeof buf, "%.50s %d %d",
1221 get_remote_ipaddr(), get_remote_port(), get_local_port());
1222 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1223
1224 if (ttyname)
1225 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1226 if (term)
1227 child_set_env(&env, &envsize, "TERM", term);
1228 if (display)
1229 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001230 if (original_command)
1231 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1232 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001233
1234#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001235 if ((cp = getenv("AUTHSTATE")) != NULL)
1236 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1237 if ((cp = getenv("KRB5CCNAME")) != NULL)
1238 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1239 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001240#endif
1241
1242#ifdef KRB4
1243 {
1244 extern char *ticket;
1245
1246 if (ticket)
1247 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1248 }
1249#endif /* KRB4 */
1250
1251#ifdef USE_PAM
1252 /* Pull in any environment variables that may have been set by PAM. */
1253 do_pam_environment(&env, &envsize);
1254#endif /* USE_PAM */
1255
Damien Millerb38eff82000-04-01 11:09:21 +10001256 if (xauthfile)
1257 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1258 if (auth_get_socket_name() != NULL)
1259 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1260 auth_get_socket_name());
1261
1262 /* read $HOME/.ssh/environment. */
1263 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001264 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1265 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001266 read_environment_file(&env, &envsize, buf);
1267 }
1268 if (debug_flag) {
1269 /* dump the environment */
1270 fprintf(stderr, "Environment:\n");
1271 for (i = 0; env[i]; i++)
1272 fprintf(stderr, " %.200s\n", env[i]);
1273 }
Damien Millerad833b32000-08-23 10:46:23 +10001274 /* we have to stash the hostname before we close our socket. */
1275 if (options.use_login)
1276 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001277 /*
1278 * Close the connection descriptors; note that this is the child, and
1279 * the server will still have the socket open, and it is important
1280 * that we do not shutdown it. Note that the descriptors cannot be
1281 * closed before building the environment, as we call
1282 * get_remote_ipaddr there.
1283 */
1284 if (packet_get_connection_in() == packet_get_connection_out())
1285 close(packet_get_connection_in());
1286 else {
1287 close(packet_get_connection_in());
1288 close(packet_get_connection_out());
1289 }
1290 /*
1291 * Close all descriptors related to channels. They will still remain
1292 * open in the parent.
1293 */
1294 /* XXX better use close-on-exec? -markus */
1295 channel_close_all();
1296
1297 /*
1298 * Close any extra file descriptors. Note that there may still be
1299 * descriptors left by system functions. They will be closed later.
1300 */
1301 endpwent();
1302
1303 /*
1304 * Close any extra open file descriptors so that we don\'t have them
1305 * hanging around in clients. Note that we want to do this after
1306 * initgroups, because at least on Solaris 2.3 it leaves file
1307 * descriptors open.
1308 */
1309 for (i = 3; i < 64; i++)
1310 close(i);
1311
1312 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001313 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001314 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1315 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001316#ifdef HAVE_LOGIN_CAP
1317 if (login_getcapbool(lc, "requirehome", 0))
1318 exit(1);
1319#endif
1320 }
Damien Millerb38eff82000-04-01 11:09:21 +10001321
1322 /*
1323 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1324 * xauth are run in the proper environment.
1325 */
1326 environ = env;
1327
1328 /*
1329 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1330 * in this order).
1331 */
1332 if (!options.use_login) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001333 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001334 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001335 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001336
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001337 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001338 if (f) {
1339 if (auth_proto != NULL && auth_data != NULL)
1340 fprintf(f, "%s %s\n", auth_proto, auth_data);
1341 pclose(f);
1342 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001343 fprintf(stderr, "Could not run %s\n", _PATH_SSH_USER_RC);
1344 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001345 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001346 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001347
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001348 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001349 if (f) {
1350 if (auth_proto != NULL && auth_data != NULL)
1351 fprintf(f, "%s %s\n", auth_proto, auth_data);
1352 pclose(f);
1353 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001354 fprintf(stderr, "Could not run %s\n", _PATH_SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001355 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001356 /* Add authority data to .Xauthority if appropriate. */
1357 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001358 char *screen = strchr(display, ':');
1359 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001360 fprintf(stderr,
1361 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001362 options.xauth_location, display,
1363 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001364#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001365 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001366 fprintf(stderr,
1367 "Adding %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001368 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001369 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001370#endif
Damien Millerd999ae22000-05-20 12:49:31 +10001371 }
Damien Miller0c043c12000-06-07 21:22:38 +10001372 snprintf(cmd, sizeof cmd, "%s -q -",
1373 options.xauth_location);
1374 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001375 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001376 fprintf(f, "add %s %s %s\n", display,
1377 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001378#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001379 if (screen != NULL)
Damien Millerd999ae22000-05-20 12:49:31 +10001380 fprintf(f, "add %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001381 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001382 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001383#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001384 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001385 } else {
1386 fprintf(stderr, "Could not run %s\n",
1387 cmd);
1388 }
Damien Millerb38eff82000-04-01 11:09:21 +10001389 }
1390 }
Damien Millerb38eff82000-04-01 11:09:21 +10001391 /* Get the last component of the shell name. */
1392 cp = strrchr(shell, '/');
1393 if (cp)
1394 cp++;
1395 else
1396 cp = shell;
1397 }
1398 /*
1399 * If we have no command, execute the shell. In this case, the shell
1400 * name to be passed in argv[0] is preceded by '-' to indicate that
1401 * this is a login shell.
1402 */
1403 if (!command) {
1404 if (!options.use_login) {
1405 char buf[256];
1406
1407 /*
1408 * Check for mail if we have a tty and it was enabled
1409 * in server options.
1410 */
1411 if (ttyname && options.check_mail) {
1412 char *mailbox;
1413 struct stat mailstat;
1414 mailbox = getenv("MAIL");
1415 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001416 if (stat(mailbox, &mailstat) != 0 ||
1417 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001418 printf("No mail.\n");
1419 else if (mailstat.st_mtime < mailstat.st_atime)
1420 printf("You have mail.\n");
1421 else
1422 printf("You have new mail.\n");
1423 }
1424 }
1425 /* Start the shell. Set initial character to '-'. */
1426 buf[0] = '-';
1427 strncpy(buf + 1, cp, sizeof(buf) - 1);
1428 buf[sizeof(buf) - 1] = 0;
1429
1430 /* Execute the shell. */
1431 argv[0] = buf;
1432 argv[1] = NULL;
1433 execve(shell, argv, env);
1434
1435 /* Executing the shell failed. */
1436 perror(shell);
1437 exit(1);
1438
1439 } else {
1440 /* Launch login(1). */
1441
Damien Millerad833b32000-08-23 10:46:23 +10001442 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001443 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001444
1445 /* Login couldn't be executed, die. */
1446
1447 perror("login");
1448 exit(1);
1449 }
1450 }
1451 /*
1452 * Execute the command using the user's shell. This uses the -c
1453 * option to execute the command.
1454 */
1455 argv[0] = (char *) cp;
1456 argv[1] = "-c";
1457 argv[2] = (char *) command;
1458 argv[3] = NULL;
1459 execve(shell, argv, env);
1460 perror(shell);
1461 exit(1);
1462}
1463
1464Session *
1465session_new(void)
1466{
1467 int i;
1468 static int did_init = 0;
1469 if (!did_init) {
1470 debug("session_new: init");
1471 for(i = 0; i < MAX_SESSIONS; i++) {
1472 sessions[i].used = 0;
1473 sessions[i].self = i;
1474 }
1475 did_init = 1;
1476 }
1477 for(i = 0; i < MAX_SESSIONS; i++) {
1478 Session *s = &sessions[i];
1479 if (! s->used) {
1480 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001481 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001482 s->chanid = -1;
1483 s->ptyfd = -1;
1484 s->ttyfd = -1;
1485 s->term = NULL;
1486 s->pw = NULL;
1487 s->display = NULL;
1488 s->screen = 0;
1489 s->auth_data = NULL;
1490 s->auth_proto = NULL;
1491 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001492 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001493 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001494 return s;
1495 }
1496 }
1497 return NULL;
1498}
1499
1500void
1501session_dump(void)
1502{
1503 int i;
1504 for(i = 0; i < MAX_SESSIONS; i++) {
1505 Session *s = &sessions[i];
1506 debug("dump: used %d session %d %p channel %d pid %d",
1507 s->used,
1508 s->self,
1509 s,
1510 s->chanid,
1511 s->pid);
1512 }
1513}
1514
Damien Millerefb4afe2000-04-12 18:45:05 +10001515int
1516session_open(int chanid)
1517{
1518 Session *s = session_new();
1519 debug("session_open: channel %d", chanid);
1520 if (s == NULL) {
1521 error("no more sessions");
1522 return 0;
1523 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001524 s->pw = auth_get_user();
1525 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001526 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001527 debug("session_open: session %d: link with channel %d", s->self, chanid);
1528 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001529 return 1;
1530}
1531
1532Session *
1533session_by_channel(int id)
1534{
1535 int i;
1536 for(i = 0; i < MAX_SESSIONS; i++) {
1537 Session *s = &sessions[i];
1538 if (s->used && s->chanid == id) {
1539 debug("session_by_channel: session %d channel %d", i, id);
1540 return s;
1541 }
1542 }
1543 debug("session_by_channel: unknown channel %d", id);
1544 session_dump();
1545 return NULL;
1546}
1547
1548Session *
1549session_by_pid(pid_t pid)
1550{
1551 int i;
1552 debug("session_by_pid: pid %d", pid);
1553 for(i = 0; i < MAX_SESSIONS; i++) {
1554 Session *s = &sessions[i];
1555 if (s->used && s->pid == pid)
1556 return s;
1557 }
1558 error("session_by_pid: unknown pid %d", pid);
1559 session_dump();
1560 return NULL;
1561}
1562
1563int
1564session_window_change_req(Session *s)
1565{
1566 s->col = packet_get_int();
1567 s->row = packet_get_int();
1568 s->xpixel = packet_get_int();
1569 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001570 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001571 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1572 return 1;
1573}
1574
1575int
1576session_pty_req(Session *s)
1577{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001578 u_int len;
Damien Miller4af51302000-04-16 11:18:38 +10001579 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001580
Damien Millerf6d9e222000-06-18 14:50:44 +10001581 if (no_pty_flag)
1582 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001583 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001584 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001585 s->term = packet_get_string(&len);
1586 s->col = packet_get_int();
1587 s->row = packet_get_int();
1588 s->xpixel = packet_get_int();
1589 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001590 term_modes = packet_get_string(&len);
1591 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001592
1593 if (strcmp(s->term, "") == 0) {
1594 xfree(s->term);
1595 s->term = NULL;
1596 }
1597 /* Allocate a pty and open it. */
1598 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1599 xfree(s->term);
1600 s->term = NULL;
1601 s->ptyfd = -1;
1602 s->ttyfd = -1;
1603 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001604 xfree(term_modes);
1605 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001606 }
1607 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1608 /*
1609 * Add a cleanup function to clear the utmp entry and record logout
1610 * time in case we call fatal() (e.g., the connection gets closed).
1611 */
1612 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1613 pty_setowner(s->pw, s->tty);
1614 /* Get window size from the packet. */
1615 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1616
Damien Millere247cc42000-05-07 12:03:14 +10001617 session_proctitle(s);
1618
Damien Miller5f056372000-04-16 12:31:48 +10001619 /* XXX parse and set terminal modes */
1620 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001621 return 1;
1622}
1623
Damien Millerbd483e72000-04-30 10:00:53 +10001624int
1625session_subsystem_req(Session *s)
1626{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001627 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001628 int success = 0;
1629 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001630 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001631
1632 packet_done();
1633 log("subsystem request for %s", subsys);
1634
Damien Millerf6d9e222000-06-18 14:50:44 +10001635 for (i = 0; i < options.num_subsystems; i++) {
1636 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1637 debug("subsystem: exec() %s", options.subsystem_command[i]);
1638 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1639 success = 1;
1640 }
1641 }
1642
1643 if (!success)
1644 log("subsystem request for %s failed, subsystem not found", subsys);
1645
Damien Millerbd483e72000-04-30 10:00:53 +10001646 xfree(subsys);
1647 return success;
1648}
1649
1650int
1651session_x11_req(Session *s)
1652{
Damien Miller7b28dc52000-09-05 13:34:53 +11001653 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001654 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001655 debug("X11 forwarding disabled in user configuration file.");
1656 return 0;
1657 }
Damien Millerbd483e72000-04-30 10:00:53 +10001658 if (!options.x11_forwarding) {
1659 debug("X11 forwarding disabled in server configuration file.");
1660 return 0;
1661 }
1662 if (xauthfile != NULL) {
1663 debug("X11 fwd already started.");
1664 return 0;
1665 }
1666
1667 debug("Received request for X11 forwarding with auth spoofing.");
1668 if (s->display != NULL)
1669 packet_disconnect("Protocol error: X11 display already set.");
1670
1671 s->single_connection = packet_get_char();
1672 s->auth_proto = packet_get_string(NULL);
1673 s->auth_data = packet_get_string(NULL);
1674 s->screen = packet_get_int();
1675 packet_done();
1676
1677 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1678 if (s->display == NULL) {
1679 xfree(s->auth_proto);
1680 xfree(s->auth_data);
1681 return 0;
1682 }
1683 xauthfile = xmalloc(MAXPATHLEN);
1684 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1685 temporarily_use_uid(s->pw->pw_uid);
1686 if (mkdtemp(xauthfile) == NULL) {
1687 restore_uid();
1688 error("private X11 dir: mkdtemp %s failed: %s",
1689 xauthfile, strerror(errno));
1690 xfree(xauthfile);
1691 xauthfile = NULL;
1692 xfree(s->auth_proto);
1693 xfree(s->auth_data);
1694 /* XXXX remove listening channels */
1695 return 0;
1696 }
1697 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001698 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1699 if (fd >= 0)
1700 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001701 restore_uid();
1702 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1703 return 1;
1704}
1705
Damien Millerf6d9e222000-06-18 14:50:44 +10001706int
1707session_shell_req(Session *s)
1708{
1709 /* if forced_command == NULL, the shell is execed */
1710 char *shell = forced_command;
1711 packet_done();
1712 s->extended = 1;
1713 if (s->ttyfd == -1)
1714 do_exec_no_pty(s, shell, s->pw);
1715 else
1716 do_exec_pty(s, shell, s->pw);
1717 return 1;
1718}
1719
1720int
1721session_exec_req(Session *s)
1722{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001723 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001724 char *command = packet_get_string(&len);
1725 packet_done();
1726 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001727 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001728 command = forced_command;
1729 debug("Forced command '%.500s'", forced_command);
1730 }
1731 s->extended = 1;
1732 if (s->ttyfd == -1)
1733 do_exec_no_pty(s, command, s->pw);
1734 else
1735 do_exec_pty(s, command, s->pw);
1736 if (forced_command == NULL)
1737 xfree(command);
1738 return 1;
1739}
1740
Damien Miller0bc1bd82000-11-13 22:57:25 +11001741int
1742session_auth_agent_req(Session *s)
1743{
1744 static int called = 0;
1745 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001746 if (no_agent_forwarding_flag) {
1747 debug("session_auth_agent_req: no_agent_forwarding_flag");
1748 return 0;
1749 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001750 if (called) {
1751 return 0;
1752 } else {
1753 called = 1;
1754 return auth_input_request_forwarding(s->pw);
1755 }
1756}
1757
Damien Millerefb4afe2000-04-12 18:45:05 +10001758void
1759session_input_channel_req(int id, void *arg)
1760{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001761 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001762 int reply;
1763 int success = 0;
1764 char *rtype;
1765 Session *s;
1766 Channel *c;
1767
1768 rtype = packet_get_string(&len);
1769 reply = packet_get_char();
1770
1771 s = session_by_channel(id);
1772 if (s == NULL)
1773 fatal("session_input_channel_req: channel %d: no session", id);
1774 c = channel_lookup(id);
1775 if (c == NULL)
1776 fatal("session_input_channel_req: channel %d: bad channel", id);
1777
1778 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1779 s->self, id, rtype, reply);
1780
1781 /*
1782 * a session is in LARVAL state until a shell
1783 * or programm is executed
1784 */
1785 if (c->type == SSH_CHANNEL_LARVAL) {
1786 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001787 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001788 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001789 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001790 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001791 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001792 } else if (strcmp(rtype, "x11-req") == 0) {
1793 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001794 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1795 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001796 } else if (strcmp(rtype, "subsystem") == 0) {
1797 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001798 }
1799 }
1800 if (strcmp(rtype, "window-change") == 0) {
1801 success = session_window_change_req(s);
1802 }
1803
1804 if (reply) {
1805 packet_start(success ?
1806 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1807 packet_put_int(c->remote_id);
1808 packet_send();
1809 }
1810 xfree(rtype);
1811}
1812
1813void
1814session_set_fds(Session *s, int fdin, int fdout, int fderr)
1815{
1816 if (!compat20)
1817 fatal("session_set_fds: called for proto != 2.0");
1818 /*
1819 * now that have a child and a pipe to the child,
1820 * we can activate our channel and register the fd's
1821 */
1822 if (s->chanid == -1)
1823 fatal("no channel for session %d", s->self);
1824 channel_set_fds(s->chanid,
1825 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001826 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1827 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001828}
1829
Damien Millerb38eff82000-04-01 11:09:21 +10001830void
1831session_pty_cleanup(Session *s)
1832{
1833 if (s == NULL || s->ttyfd == -1)
1834 return;
1835
Kevin Steves43cdef32001-02-11 14:12:08 +00001836 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001837
1838 /* Cancel the cleanup function. */
1839 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1840
1841 /* Record that the user has logged out. */
1842 record_logout(s->pid, s->tty);
1843
1844 /* Release the pseudo-tty. */
1845 pty_release(s->tty);
1846
1847 /*
1848 * Close the server side of the socket pairs. We must do this after
1849 * the pty cleanup, so that another process doesn't get this pty
1850 * while we're still cleaning up.
1851 */
1852 if (close(s->ptymaster) < 0)
1853 error("close(s->ptymaster): %s", strerror(errno));
1854}
Damien Millerefb4afe2000-04-12 18:45:05 +10001855
1856void
1857session_exit_message(Session *s, int status)
1858{
1859 Channel *c;
1860 if (s == NULL)
1861 fatal("session_close: no session");
1862 c = channel_lookup(s->chanid);
1863 if (c == NULL)
1864 fatal("session_close: session %d: no channel %d",
1865 s->self, s->chanid);
1866 debug("session_exit_message: session %d channel %d pid %d",
1867 s->self, s->chanid, s->pid);
1868
1869 if (WIFEXITED(status)) {
1870 channel_request_start(s->chanid,
1871 "exit-status", 0);
1872 packet_put_int(WEXITSTATUS(status));
1873 packet_send();
1874 } else if (WIFSIGNALED(status)) {
1875 channel_request_start(s->chanid,
1876 "exit-signal", 0);
1877 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001878#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001879 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001880#else /* WCOREDUMP */
1881 packet_put_char(0);
1882#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001883 packet_put_cstring("");
1884 packet_put_cstring("");
1885 packet_send();
1886 } else {
1887 /* Some weird exit cause. Just exit. */
1888 packet_disconnect("wait returned status %04x.", status);
1889 }
1890
1891 /* disconnect channel */
1892 debug("session_exit_message: release channel %d", s->chanid);
1893 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001894 /*
1895 * emulate a write failure with 'chan_write_failed', nobody will be
1896 * interested in data we write.
1897 * Note that we must not call 'chan_read_failed', since there could
1898 * be some more data waiting in the pipe.
1899 */
Damien Millerbd483e72000-04-30 10:00:53 +10001900 if (c->ostate != CHAN_OUTPUT_CLOSED)
1901 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001902 s->chanid = -1;
1903}
1904
1905void
1906session_free(Session *s)
1907{
1908 debug("session_free: session %d pid %d", s->self, s->pid);
1909 if (s->term)
1910 xfree(s->term);
1911 if (s->display)
1912 xfree(s->display);
1913 if (s->auth_data)
1914 xfree(s->auth_data);
1915 if (s->auth_proto)
1916 xfree(s->auth_proto);
1917 s->used = 0;
1918}
1919
1920void
1921session_close(Session *s)
1922{
1923 session_pty_cleanup(s);
1924 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001925 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001926}
1927
1928void
1929session_close_by_pid(pid_t pid, int status)
1930{
1931 Session *s = session_by_pid(pid);
1932 if (s == NULL) {
1933 debug("session_close_by_pid: no session for pid %d", s->pid);
1934 return;
1935 }
1936 if (s->chanid != -1)
1937 session_exit_message(s, status);
1938 session_close(s);
1939}
1940
1941/*
1942 * this is called when a channel dies before
1943 * the session 'child' itself dies
1944 */
1945void
1946session_close_by_channel(int id, void *arg)
1947{
1948 Session *s = session_by_channel(id);
1949 if (s == NULL) {
1950 debug("session_close_by_channel: no session for channel %d", id);
1951 return;
1952 }
1953 /* disconnect channel */
1954 channel_cancel_cleanup(s->chanid);
1955 s->chanid = -1;
1956
1957 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1958 if (s->pid == 0) {
1959 /* close session immediately */
1960 session_close(s);
1961 } else {
1962 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001963 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001964 error("session_close_by_channel: kill %d: %s",
1965 s->pid, strerror(errno));
1966 }
1967}
1968
Damien Millere247cc42000-05-07 12:03:14 +10001969char *
1970session_tty_list(void)
1971{
1972 static char buf[1024];
1973 int i;
1974 buf[0] = '\0';
1975 for(i = 0; i < MAX_SESSIONS; i++) {
1976 Session *s = &sessions[i];
1977 if (s->used && s->ttyfd != -1) {
1978 if (buf[0] != '\0')
1979 strlcat(buf, ",", sizeof buf);
1980 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1981 }
1982 }
1983 if (buf[0] == '\0')
1984 strlcpy(buf, "notty", sizeof buf);
1985 return buf;
1986}
1987
1988void
1989session_proctitle(Session *s)
1990{
1991 if (s->pw == NULL)
1992 error("no user for session %d", s->self);
1993 else
1994 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1995}
1996
Damien Millerefb4afe2000-04-12 18:45:05 +10001997void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00001998do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10001999{
2000 /*
2001 * Cancel the alarm we set to limit the time taken for
2002 * authentication.
2003 */
2004 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10002005 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10002006 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10002007 startup_pipe = -1;
2008 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00002009#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002010 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10002011 error("unable to get login class");
2012 return;
2013 }
2014#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10002015 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002016 if (xauthfile)
2017 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002018}