blob: d4053b4c947c88014da02406f7d5861da42fc2d7 [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 Lindstrom7603b2d2001-02-26 20:13:32 +000036RCSID("$OpenBSD: session.c,v 1.57 2001/02/23 15:37:45 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;
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000219 int screen_flag;
Damien Millerb38eff82000-04-01 11:09:21 +1000220
221 /*
222 * Cancel the alarm we set to limit the time taken for
223 * authentication.
224 */
225 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +1000226 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +1000227 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +1000228 startup_pipe = -1;
229 }
Damien Millerb38eff82000-04-01 11:09:21 +1000230
231 /*
232 * Inform the channel mechanism that we are the server side and that
233 * the client may request to connect to any port at all. (The user
234 * could do it anyway, and we wouldn\'t know what is permitted except
235 * by the client telling us, so we can equally well trust the client
236 * not to request anything bogus.)
237 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100238 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
Damien Millerb38eff82000-04-01 11:09:21 +1000239 channel_permit_all_opens();
240
241 s = session_new();
Damien Millerbd483e72000-04-30 10:00:53 +1000242 s->pw = pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000243
Kevin Steves48b7cc02000-10-07 13:24:00 +0000244#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +1000245 if ((lc = login_getclass(pw->pw_class)) == NULL) {
246 error("unable to get login class");
247 return;
248 }
249#endif
250
Damien Millerb38eff82000-04-01 11:09:21 +1000251 /*
252 * We stay in this loop until the client requests to execute a shell
253 * or a command.
254 */
255 for (;;) {
256 int success = 0;
257
258 /* Get a packet from the client. */
259 type = packet_read(&plen);
260
261 /* Process the packet. */
262 switch (type) {
263 case SSH_CMSG_REQUEST_COMPRESSION:
264 packet_integrity_check(plen, 4, type);
265 compression_level = packet_get_int();
266 if (compression_level < 1 || compression_level > 9) {
267 packet_send_debug("Received illegal compression level %d.",
268 compression_level);
269 break;
270 }
271 /* Enable compression after we have responded with SUCCESS. */
272 enable_compression_after_reply = 1;
273 success = 1;
274 break;
275
276 case SSH_CMSG_REQUEST_PTY:
277 if (no_pty_flag) {
278 debug("Allocating a pty not permitted for this authentication.");
279 break;
280 }
281 if (have_pty)
282 packet_disconnect("Protocol error: you already have a pty.");
283
284 debug("Allocating pty.");
285
286 /* Allocate a pty and open it. */
287 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
288 sizeof(s->tty))) {
289 error("Failed to allocate pty.");
290 break;
291 }
292 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
293 pty_setowner(pw, s->tty);
294
295 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
296 s->term = packet_get_string(&dlen);
297 packet_integrity_check(dlen, strlen(s->term), type);
298 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
299 /* Remaining bytes */
300 n_bytes = plen - (4 + dlen + 4 * 4);
301
302 if (strcmp(s->term, "") == 0) {
303 xfree(s->term);
304 s->term = NULL;
305 }
306 /* Get window size from the packet. */
307 s->row = packet_get_int();
308 s->col = packet_get_int();
309 s->xpixel = packet_get_int();
310 s->ypixel = packet_get_int();
311 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
312
313 /* Get tty modes from the packet. */
314 tty_parse_modes(s->ttyfd, &n_bytes);
315 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
316
Damien Millere247cc42000-05-07 12:03:14 +1000317 session_proctitle(s);
318
Damien Millerb38eff82000-04-01 11:09:21 +1000319 /* Indicate that we now have a pty. */
320 success = 1;
321 have_pty = 1;
322 break;
323
324 case SSH_CMSG_X11_REQUEST_FORWARDING:
325 if (!options.x11_forwarding) {
326 packet_send_debug("X11 forwarding disabled in server configuration file.");
327 break;
328 }
Damien Miller0c043c12000-06-07 21:22:38 +1000329 if (!options.xauth_location) {
330 packet_send_debug("No xauth program; cannot forward with spoofing.");
331 break;
332 }
Damien Millerb38eff82000-04-01 11:09:21 +1000333 if (no_x11_forwarding_flag) {
334 packet_send_debug("X11 forwarding not permitted for this authentication.");
335 break;
336 }
337 debug("Received request for X11 forwarding with auth spoofing.");
338 if (s->display != NULL)
339 packet_disconnect("Protocol error: X11 display already set.");
340
341 s->auth_proto = packet_get_string(&proto_len);
342 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000343
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000344 screen_flag = packet_get_protocol_flags() &
345 SSH_PROTOFLAG_SCREEN_NUMBER;
346 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
347
348 if (packet_remaining() == 4) {
349 if (!screen_flag)
350 debug2("Buggy client: "
351 "X11 screen flag missing");
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000352 packet_integrity_check(plen,
353 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000354 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000355 } else {
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000356 packet_integrity_check(plen,
357 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000358 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000359 }
Damien Millerb38eff82000-04-01 11:09:21 +1000360 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
361
362 if (s->display == NULL)
363 break;
364
365 /* Setup to always have a local .Xauthority. */
366 xauthfile = xmalloc(MAXPATHLEN);
367 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
368 temporarily_use_uid(pw->pw_uid);
369 if (mkdtemp(xauthfile) == NULL) {
370 restore_uid();
371 error("private X11 dir: mkdtemp %s failed: %s",
372 xauthfile, strerror(errno));
373 xfree(xauthfile);
374 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000375 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000376 break;
377 }
378 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100379 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
380 if (fd >= 0)
381 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000382 restore_uid();
383 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
384 success = 1;
385 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000386
387 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
388 if (no_agent_forwarding_flag || compat13) {
389 debug("Authentication agent forwarding not permitted for this authentication.");
390 break;
391 }
392 debug("Received authentication agent forwarding request.");
Damien Miller0c043c12000-06-07 21:22:38 +1000393 success = auth_input_request_forwarding(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000394 break;
395
396 case SSH_CMSG_PORT_FORWARD_REQUEST:
397 if (no_port_forwarding_flag) {
398 debug("Port forwarding not permitted for this authentication.");
399 break;
400 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100401 if (!options.allow_tcp_forwarding) {
402 debug("Port forwarding not permitted.");
403 break;
404 }
Damien Millerb38eff82000-04-01 11:09:21 +1000405 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000406 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000407 success = 1;
408 break;
409
410 case SSH_CMSG_MAX_PACKET_SIZE:
411 if (packet_set_maxsize(packet_get_int()) > 0)
412 success = 1;
413 break;
414
415 case SSH_CMSG_EXEC_SHELL:
416 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000417 if (type == SSH_CMSG_EXEC_CMD) {
418 command = packet_get_string(&dlen);
419 debug("Exec command '%.500s'", command);
420 packet_integrity_check(plen, 4 + dlen, type);
421 } else {
422 command = NULL;
423 packet_integrity_check(plen, 0, type);
424 }
425 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100426 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000427 command = forced_command;
428 debug("Forced command '%.500s'", forced_command);
429 }
430 if (have_pty)
431 do_exec_pty(s, command, pw);
432 else
433 do_exec_no_pty(s, command, pw);
434
435 if (command != NULL)
436 xfree(command);
437 /* Cleanup user's local Xauthority file. */
438 if (xauthfile)
439 xauthfile_cleanup_proc(NULL);
440 return;
441
442 default:
443 /*
444 * Any unknown messages in this phase are ignored,
445 * and a failure message is returned.
446 */
447 log("Unknown packet type received after authentication: %d", type);
448 }
449 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
450 packet_send();
451 packet_write_wait();
452
453 /* Enable compression now that we have replied if appropriate. */
454 if (enable_compression_after_reply) {
455 enable_compression_after_reply = 0;
456 packet_start_compression(compression_level);
457 }
458 }
459}
460
461/*
462 * This is called to fork and execute a command when we have no tty. This
463 * will call do_child from the child, and server_loop from the parent after
464 * setting up file descriptors and such.
465 */
Damien Miller4af51302000-04-16 11:18:38 +1000466void
Damien Millerb38eff82000-04-01 11:09:21 +1000467do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
468{
469 int pid;
470
471#ifdef USE_PIPES
472 int pin[2], pout[2], perr[2];
473 /* Allocate pipes for communicating with the program. */
474 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
475 packet_disconnect("Could not create pipes: %.100s",
476 strerror(errno));
477#else /* USE_PIPES */
478 int inout[2], err[2];
479 /* Uses socket pairs to communicate with the program. */
480 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
481 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
482 packet_disconnect("Could not create socket pairs: %.100s",
483 strerror(errno));
484#endif /* USE_PIPES */
485 if (s == NULL)
486 fatal("do_exec_no_pty: no session");
487
Damien Millere247cc42000-05-07 12:03:14 +1000488 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000489
Damien Miller5a761312001-02-27 09:28:23 +1100490#if defined(USE_PAM) && defined(PAM_SUN_CODEBASE)
491 /* Solaris-derived PAMs don't like doing this after the fork() */
Kevin Stevesff793a22001-02-21 16:36:51 +0000492 do_pam_setcred();
493#endif /* USE_PAM */
494
Damien Millerb38eff82000-04-01 11:09:21 +1000495 /* Fork the child. */
496 if ((pid = fork()) == 0) {
497 /* Child. Reinitialize the log since the pid has changed. */
498 log_init(__progname, options.log_level, options.log_facility, log_stderr);
499
Damien Miller152cea22000-12-13 19:21:51 +1100500 signal(SIGPIPE, SIG_DFL);
501
Damien Millerb38eff82000-04-01 11:09:21 +1000502 /*
503 * Create a new session and process group since the 4.4BSD
504 * setlogin() affects the entire process group.
505 */
506 if (setsid() < 0)
507 error("setsid failed: %.100s", strerror(errno));
508
509#ifdef USE_PIPES
510 /*
511 * Redirect stdin. We close the parent side of the socket
512 * pair, and make the child side the standard input.
513 */
514 close(pin[1]);
515 if (dup2(pin[0], 0) < 0)
516 perror("dup2 stdin");
517 close(pin[0]);
518
519 /* Redirect stdout. */
520 close(pout[0]);
521 if (dup2(pout[1], 1) < 0)
522 perror("dup2 stdout");
523 close(pout[1]);
524
525 /* Redirect stderr. */
526 close(perr[0]);
527 if (dup2(perr[1], 2) < 0)
528 perror("dup2 stderr");
529 close(perr[1]);
530#else /* USE_PIPES */
531 /*
532 * Redirect stdin, stdout, and stderr. Stdin and stdout will
533 * use the same socket, as some programs (particularly rdist)
534 * seem to depend on it.
535 */
536 close(inout[1]);
537 close(err[1]);
538 if (dup2(inout[0], 0) < 0) /* stdin */
539 perror("dup2 stdin");
540 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
541 perror("dup2 stdout");
542 if (dup2(err[0], 2) < 0) /* stderr */
543 perror("dup2 stderr");
544#endif /* USE_PIPES */
545
546 /* Do processing for the child (exec command etc). */
547 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
548 /* NOTREACHED */
549 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100550#ifdef HAVE_CYGWIN
551 if (is_winnt)
552 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
553#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000554 if (pid < 0)
555 packet_disconnect("fork failed: %.100s", strerror(errno));
556 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000557 /* Set interactive/non-interactive mode. */
558 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000559#ifdef USE_PIPES
560 /* We are the parent. Close the child sides of the pipes. */
561 close(pin[0]);
562 close(pout[1]);
563 close(perr[1]);
564
Damien Millerefb4afe2000-04-12 18:45:05 +1000565 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000566 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000567 } else {
568 /* Enter the interactive session. */
569 server_loop(pid, pin[1], pout[0], perr[0]);
570 /* server_loop has closed pin[1], pout[1], and perr[1]. */
571 }
Damien Millerb38eff82000-04-01 11:09:21 +1000572#else /* USE_PIPES */
573 /* We are the parent. Close the child sides of the socket pairs. */
574 close(inout[0]);
575 close(err[0]);
576
577 /*
578 * Enter the interactive session. Note: server_loop must be able to
579 * handle the case that fdin and fdout are the same.
580 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000581 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000582 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000583 } else {
584 server_loop(pid, inout[1], inout[1], err[1]);
585 /* server_loop has closed inout[1] and err[1]. */
586 }
Damien Millerb38eff82000-04-01 11:09:21 +1000587#endif /* USE_PIPES */
588}
589
590/*
591 * This is called to fork and execute a command when we have a tty. This
592 * will call do_child from the child, and server_loop from the parent after
593 * setting up file descriptors, controlling tty, updating wtmp, utmp,
594 * lastlog, and other such operations.
595 */
Damien Miller4af51302000-04-16 11:18:38 +1000596void
Damien Millerb38eff82000-04-01 11:09:21 +1000597do_exec_pty(Session *s, const char *command, struct passwd * pw)
598{
Damien Millerb38eff82000-04-01 11:09:21 +1000599 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000600 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000601
602 if (s == NULL)
603 fatal("do_exec_pty: no session");
604 ptyfd = s->ptyfd;
605 ttyfd = s->ttyfd;
606
Damien Miller5a761312001-02-27 09:28:23 +1100607#if defined(USE_PAM) && defined(PAM_SUN_CODEBASE)
608 /* Solaris-derived PAMs don't like doing this after the fork() */
Kevin Stevesff793a22001-02-21 16:36:51 +0000609 do_pam_session(pw->pw_name, s->tty);
610 do_pam_setcred();
Damien Miller5a761312001-02-27 09:28:23 +1100611#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000612
Damien Millerb38eff82000-04-01 11:09:21 +1000613 /* Fork the child. */
614 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000615 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000616 log_init(__progname, options.log_level, options.log_facility, log_stderr);
617
Damien Miller152cea22000-12-13 19:21:51 +1100618 signal(SIGPIPE, SIG_DFL);
619
Damien Millerb38eff82000-04-01 11:09:21 +1000620 /* Close the master side of the pseudo tty. */
621 close(ptyfd);
622
623 /* Make the pseudo tty our controlling tty. */
624 pty_make_controlling_tty(&ttyfd, s->tty);
625
626 /* Redirect stdin from the pseudo tty. */
627 if (dup2(ttyfd, fileno(stdin)) < 0)
628 error("dup2 stdin failed: %.100s", strerror(errno));
629
630 /* Redirect stdout to the pseudo tty. */
631 if (dup2(ttyfd, fileno(stdout)) < 0)
632 error("dup2 stdin failed: %.100s", strerror(errno));
633
634 /* Redirect stderr to the pseudo tty. */
635 if (dup2(ttyfd, fileno(stderr)) < 0)
636 error("dup2 stdin failed: %.100s", strerror(errno));
637
638 /* Close the extra descriptor for the pseudo tty. */
639 close(ttyfd);
640
Damien Miller942da032000-08-18 13:59:06 +1000641 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100642 if (!(options.use_login && command == NULL))
643 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000644
Damien Millerb38eff82000-04-01 11:09:21 +1000645 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000646 do_child(command, pw, s->term, s->display, s->auth_proto,
647 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000648 /* NOTREACHED */
649 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100650#ifdef HAVE_CYGWIN
651 if (is_winnt)
652 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
653#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000654 if (pid < 0)
655 packet_disconnect("fork failed: %.100s", strerror(errno));
656 s->pid = pid;
657
658 /* Parent. Close the slave side of the pseudo tty. */
659 close(ttyfd);
660
661 /*
662 * Create another descriptor of the pty master side for use as the
663 * standard input. We could use the original descriptor, but this
664 * simplifies code in server_loop. The descriptor is bidirectional.
665 */
666 fdout = dup(ptyfd);
667 if (fdout < 0)
668 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
669
670 /* we keep a reference to the pty master */
671 ptymaster = dup(ptyfd);
672 if (ptymaster < 0)
673 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
674 s->ptymaster = ptymaster;
675
676 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000677 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000678 if (compat20) {
679 session_set_fds(s, ptyfd, fdout, -1);
680 } else {
681 server_loop(pid, ptyfd, fdout, -1);
682 /* server_loop _has_ closed ptyfd and fdout. */
683 session_pty_cleanup(s);
684 }
Damien Millerb38eff82000-04-01 11:09:21 +1000685}
686
Damien Miller942da032000-08-18 13:59:06 +1000687const char *
688get_remote_name_or_ip(void)
689{
690 static const char *remote = "";
691 if (utmp_len > 0)
Damien Miller33804262001-02-04 23:20:18 +1100692 remote = get_canonical_hostname(options.reverse_mapping_check);
Damien Miller942da032000-08-18 13:59:06 +1000693 if (utmp_len == 0 || strlen(remote) > utmp_len)
694 remote = get_remote_ipaddr();
695 return remote;
696}
697
698/* administrative, login(1)-like work */
699void
Damien Miller69b69aa2000-10-28 14:19:58 +1100700do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000701{
702 FILE *f;
703 char *time_string;
704 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100705 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000706 socklen_t fromlen;
707 struct sockaddr_storage from;
708 struct stat st;
709 time_t last_login_time;
710 struct passwd * pw = s->pw;
711 pid_t pid = getpid();
712
713 /*
714 * Get IP address of client. If the connection is not a socket, let
715 * the address be 0.0.0.0.
716 */
717 memset(&from, 0, sizeof(from));
718 if (packet_connection_is_on_socket()) {
719 fromlen = sizeof(from);
720 if (getpeername(packet_get_connection_in(),
721 (struct sockaddr *) & from, &fromlen) < 0) {
722 debug("getpeername: %.100s", strerror(errno));
723 fatal_cleanup();
724 }
725 }
726
Damien Miller7b28dc52000-09-05 13:34:53 +1100727 /* Get the time and hostname when the user last logged in. */
Damien Millere4340be2000-09-16 13:29:08 +1100728 hostname[0] = '\0';
729 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
730 hostname, sizeof(hostname));
731
Damien Miller942da032000-08-18 13:59:06 +1000732 /* Record that there was a login on that tty from the remote host. */
733 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
734 get_remote_name_or_ip(), (struct sockaddr *)&from);
735
Kevin Steves092f2ef2000-10-14 13:36:13 +0000736#ifdef USE_PAM
737 /*
738 * If password change is needed, do it now.
739 * This needs to occur before the ~/.hushlogin check.
740 */
Damien Miller646aa602001-02-15 11:51:32 +1100741 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000742 print_pam_messages();
743 do_pam_chauthtok();
744 }
745#endif
746
Damien Miller69b69aa2000-10-28 14:19:58 +1100747 /* Done if .hushlogin exists or a command given. */
748 if (command != NULL)
749 return;
Damien Miller942da032000-08-18 13:59:06 +1000750 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000751#ifdef HAVE_LOGIN_CAP
752 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
753#else
Damien Miller942da032000-08-18 13:59:06 +1000754 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000755#endif
Damien Miller942da032000-08-18 13:59:06 +1000756 return;
757
758#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100759 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000760 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000761#endif /* USE_PAM */
762#ifdef WITH_AIXAUTHENTICATE
763 if (aixloginmsg && *aixloginmsg)
764 printf("%s\n", aixloginmsg);
765#endif /* WITH_AIXAUTHENTICATE */
766
Damien Miller942da032000-08-18 13:59:06 +1000767 if (last_login_time != 0) {
768 time_string = ctime(&last_login_time);
769 if (strchr(time_string, '\n'))
770 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000771 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000772 printf("Last login: %s\r\n", time_string);
773 else
Damien Millere4340be2000-09-16 13:29:08 +1100774 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000775 }
776 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000777#ifdef HAVE_LOGIN_CAP
778 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
779 "/etc/motd"), "r");
780#else
Damien Miller942da032000-08-18 13:59:06 +1000781 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000782#endif
Damien Miller942da032000-08-18 13:59:06 +1000783 if (f) {
784 while (fgets(buf, sizeof(buf), f))
785 fputs(buf, stdout);
786 fclose(f);
787 }
788 }
789}
790
Damien Millerb38eff82000-04-01 11:09:21 +1000791/*
792 * Sets the value of the given variable in the environment. If the variable
793 * already exists, its value is overriden.
794 */
Damien Miller4af51302000-04-16 11:18:38 +1000795void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000796child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000797 const char *value)
798{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000799 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000800 char **env;
801
802 /*
803 * Find the slot where the value should be stored. If the variable
804 * already exists, we reuse the slot; otherwise we append a new slot
805 * at the end of the array, expanding if necessary.
806 */
807 env = *envp;
808 namelen = strlen(name);
809 for (i = 0; env[i]; i++)
810 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
811 break;
812 if (env[i]) {
813 /* Reuse the slot. */
814 xfree(env[i]);
815 } else {
816 /* New variable. Expand if necessary. */
817 if (i >= (*envsizep) - 1) {
818 (*envsizep) += 50;
819 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
820 }
821 /* Need to set the NULL pointer at end of array beyond the new slot. */
822 env[i + 1] = NULL;
823 }
824
825 /* Allocate space and format the variable in the appropriate slot. */
826 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
827 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
828}
829
830/*
831 * Reads environment variables from the given file and adds/overrides them
832 * into the environment. If the file does not exist, this does nothing.
833 * Otherwise, it must consist of empty lines, comments (line starts with '#')
834 * and assignments of the form name=value. No other forms are allowed.
835 */
Damien Miller4af51302000-04-16 11:18:38 +1000836void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000837read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000838 const char *filename)
839{
840 FILE *f;
841 char buf[4096];
842 char *cp, *value;
843
844 f = fopen(filename, "r");
845 if (!f)
846 return;
847
848 while (fgets(buf, sizeof(buf), f)) {
849 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
850 ;
851 if (!*cp || *cp == '#' || *cp == '\n')
852 continue;
853 if (strchr(cp, '\n'))
854 *strchr(cp, '\n') = '\0';
855 value = strchr(cp, '=');
856 if (value == NULL) {
857 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
858 continue;
859 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000860 /*
861 * Replace the equals sign by nul, and advance value to
862 * the value string.
863 */
Damien Millerb38eff82000-04-01 11:09:21 +1000864 *value = '\0';
865 value++;
866 child_set_env(env, envsize, cp, value);
867 }
868 fclose(f);
869}
870
871#ifdef USE_PAM
872/*
873 * Sets any environment variables which have been specified by PAM
874 */
875void do_pam_environment(char ***env, int *envsize)
876{
877 char *equals, var_name[512], var_val[512];
878 char **pam_env;
879 int i;
880
881 if ((pam_env = fetch_pam_environment()) == NULL)
882 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000883
Damien Millerb38eff82000-04-01 11:09:21 +1000884 for(i = 0; pam_env[i] != NULL; i++) {
885 if ((equals = strstr(pam_env[i], "=")) == NULL)
886 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000887
Damien Millerb38eff82000-04-01 11:09:21 +1000888 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
889 memset(var_name, '\0', sizeof(var_name));
890 memset(var_val, '\0', sizeof(var_val));
891
892 strncpy(var_name, pam_env[i], equals - pam_env[i]);
893 strcpy(var_val, equals + 1);
894
Damien Millercb5e44a2000-09-29 12:12:36 +1100895 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000896
897 child_set_env(env, envsize, var_name, var_val);
898 }
899 }
900}
901#endif /* USE_PAM */
902
Damien Millercb5e44a2000-09-29 12:12:36 +1100903#ifdef HAVE_CYGWIN
904void copy_environment(char ***env, int *envsize)
905{
906 char *equals, var_name[512], var_val[512];
907 int i;
908
909 for(i = 0; environ[i] != NULL; i++) {
910 if ((equals = strstr(environ[i], "=")) == NULL)
911 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000912
Damien Millercb5e44a2000-09-29 12:12:36 +1100913 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
914 memset(var_name, '\0', sizeof(var_name));
915 memset(var_val, '\0', sizeof(var_val));
916
917 strncpy(var_name, environ[i], equals - environ[i]);
918 strcpy(var_val, equals + 1);
919
920 debug3("Copy environment: %s=%s", var_name, var_val);
921
922 child_set_env(env, envsize, var_name, var_val);
923 }
924 }
925}
926#endif
927
Damien Miller5fc85652000-07-09 23:53:07 +1000928#if defined(HAVE_GETUSERATTR)
929/*
930 * AIX-specific login initialisation
931 */
932void set_limit(char *user, char *soft, char *hard, int resource, int mult)
933{
934 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000935 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000936
937 getrlimit(resource, &rlim);
938
Damien Miller65964d62000-07-11 09:16:22 +1000939 slim = 0;
940 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
941 if (slim < 0) {
942 rlim.rlim_cur = RLIM_INFINITY;
943 } else if (slim != 0) {
944 /* See the wackiness below */
945 if (rlim.rlim_cur == slim * mult)
946 slim = 0;
947 else
948 rlim.rlim_cur = slim * mult;
949 }
950 }
Damien Miller5fc85652000-07-09 23:53:07 +1000951
Damien Miller65964d62000-07-11 09:16:22 +1000952 hlim = 0;
953 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
954 if (hlim < 0) {
955 rlim.rlim_max = RLIM_INFINITY;
956 } else if (hlim != 0) {
957 rlim.rlim_max = hlim * mult;
958 }
959 }
Damien Miller5fc85652000-07-09 23:53:07 +1000960
Damien Miller65964d62000-07-11 09:16:22 +1000961 /*
962 * XXX For cpu and fsize the soft limit is set to the hard limit
963 * if the hard limit is left at its default value and the soft limit
964 * is changed from its default value, either by requesting it
965 * (slim == 0) or by setting it to the current default. At least
966 * that's how rlogind does it. If you're confused you're not alone.
967 * Bug or feature? AIX 4.3.1.2
968 */
969 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
970 && hlim == 0 && slim != 0)
971 rlim.rlim_max = rlim.rlim_cur;
972 /* A specified hard limit limits the soft limit */
973 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
974 rlim.rlim_cur = rlim.rlim_max;
975 /* A soft limit can increase a hard limit */
976 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000977 rlim.rlim_max = rlim.rlim_cur;
978
979 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000980 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000981}
982
983void set_limits_from_userattr(char *user)
984{
985 int mask;
986 char buf[16];
987
988 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
989 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
990 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
991 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
992 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
993 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
994#if defined(S_UNOFILE)
995 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
996#endif
997
998 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
999 /* Convert decimal to octal */
1000 (void) snprintf(buf, sizeof(buf), "%d", mask);
1001 if (sscanf(buf, "%o", &mask) == 1)
1002 umask(mask);
1003 }
1004}
1005#endif /* defined(HAVE_GETUSERATTR) */
1006
Damien Millerb38eff82000-04-01 11:09:21 +10001007/*
1008 * Performs common processing for the child, such as setting up the
1009 * environment, closing extra file descriptors, setting the user and group
1010 * ids, and executing the command or shell.
1011 */
Damien Miller4af51302000-04-16 11:18:38 +10001012void
Damien Millerb38eff82000-04-01 11:09:21 +10001013do_child(const char *command, struct passwd * pw, const char *term,
1014 const char *display, const char *auth_proto,
1015 const char *auth_data, const char *ttyname)
1016{
Damien Miller7b28dc52000-09-05 13:34:53 +11001017 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001018 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001019 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001020 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001021 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001022 char **env;
1023 extern char **environ;
1024 struct stat st;
1025 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001026#ifdef WITH_IRIX_PROJECT
1027 prid_t projid;
1028#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001029#ifdef WITH_IRIX_JOBS
1030 jid_t jid = 0;
1031#else
1032#ifdef WITH_IRIX_ARRAY
1033 int jid = 0;
1034#endif /* WITH_IRIX_ARRAY */
1035#endif /* WITH_IRIX_JOBS */
1036
Damien Miller5a761312001-02-27 09:28:23 +11001037#if defined(USE_PAM) && !defined(PAM_SUN_CODEBASE)
1038 /* Solaris-derived PAMs don't like doing this after the fork() */
1039 do_pam_session(pw->pw_name, s->tty);
1040 do_pam_setcred();
1041#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001042
Damien Millerd3a18572000-06-07 19:55:44 +10001043 /* login(1) is only called if we execute the login shell */
1044 if (options.use_login && command != NULL)
1045 options.use_login = 0;
1046
Damien Millerb38eff82000-04-01 11:09:21 +10001047#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001048 if (!options.use_login) {
1049# ifdef HAVE_LOGIN_CAP
1050 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1051 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1052 _PATH_NOLOGIN), "r");
1053# else /* HAVE_LOGIN_CAP */
1054 if (pw->pw_uid)
1055 f = fopen(_PATH_NOLOGIN, "r");
1056# endif /* HAVE_LOGIN_CAP */
1057 if (f) {
1058 /* /etc/nologin exists. Print its contents and exit. */
1059 while (fgets(buf, sizeof(buf), f))
1060 fputs(buf, stderr);
1061 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001062 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001063 }
Damien Millerb38eff82000-04-01 11:09:21 +10001064 }
1065#endif /* USE_PAM */
1066
Damien Millerad833b32000-08-23 10:46:23 +10001067 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001068 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1069 switch, so we let login(1) to this for us. */
1070 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001071#ifdef HAVE_OSF_SIA
Damien Miller92ddb7d2001-02-14 01:25:23 +11001072 session_setup_sia(pw->pw_name, ttyname);
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001073#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001074#ifdef HAVE_CYGWIN
1075 if (is_winnt) {
1076#else
Damien Millerb38eff82000-04-01 11:09:21 +10001077 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001078#endif
Damien Millerad833b32000-08-23 10:46:23 +10001079# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001080 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001081# endif /* HAVE_GETUSERATTR */
1082# ifdef HAVE_LOGIN_CAP
1083 if (setusercontext(lc, pw, pw->pw_uid,
1084 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1085 perror("unable to set user context");
1086 exit(1);
1087 }
Damien Miller60396b02001-02-18 17:01:00 +11001088#ifdef BSD_AUTH
1089 if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
1090 error("approval failure for %s", pw->pw_name);
1091 fprintf(stderr, "Approval failure");
1092 exit(1);
1093 }
1094#endif
Damien Millerad833b32000-08-23 10:46:23 +10001095# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001096#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1097 /* Sets login uid for accounting */
1098 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1099 error("setluid: %s", strerror(errno));
1100#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1101
Damien Millerad833b32000-08-23 10:46:23 +10001102 if (setlogin(pw->pw_name) < 0)
1103 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001104 if (setgid(pw->pw_gid) < 0) {
1105 perror("setgid");
1106 exit(1);
1107 }
1108 /* Initialize the group list. */
1109 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1110 perror("initgroups");
1111 exit(1);
1112 }
1113 endgrent();
Ben Lindstrom980754c2000-11-12 00:04:24 +00001114# ifdef WITH_IRIX_JOBS
1115 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1116 if (jid == -1) {
1117 fatal("Failed to create job container: %.100s",
1118 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001119 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001120# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001121# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001122 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001123 if (jid == 0) {
1124 if (newarraysess() != 0)
1125 fatal("Failed to set up new array session: %.100s",
1126 strerror(errno));
1127 }
Damien Millerad833b32000-08-23 10:46:23 +10001128# endif /* WITH_IRIX_ARRAY */
1129# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001130 /* initialize irix project info */
1131 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1132 debug("Failed to get project id, using projid 0");
1133 projid = 0;
1134 }
Damien Miller91606b12000-06-28 08:22:29 +10001135 if (setprid(projid))
1136 fatal("Failed to initialize project %d for %s: %.100s",
1137 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001138# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001139#ifdef WITH_IRIX_AUDIT
1140 if (sysconf(_SC_AUDIT)) {
1141 debug("Setting sat id to %d", (int) pw->pw_uid);
1142 if (satsetid(pw->pw_uid))
1143 debug("error setting satid: %.100s", strerror(errno));
1144 }
1145#endif /* WITH_IRIX_AUDIT */
1146
Damien Millerb38eff82000-04-01 11:09:21 +10001147 /* Permanently switch to the desired uid. */
1148 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001149# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001150 }
Damien Millerad833b32000-08-23 10:46:23 +10001151#endif /* HAVE_OSF_SIA */
1152
Damien Millerbac2d8a2000-09-05 16:13:06 +11001153#ifdef HAVE_CYGWIN
1154 if (is_winnt)
1155#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001156 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001157 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001158 }
1159 /*
1160 * Get the shell from the password data. An empty shell field is
1161 * legal, and means /bin/sh.
1162 */
1163 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001164#ifdef HAVE_LOGIN_CAP
1165 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1166#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001167
1168#ifdef AFS
1169 /* Try to get AFS tokens for the local cell. */
1170 if (k_hasafs()) {
1171 char cell[64];
1172
1173 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1174 krb_afslog(cell, 0);
1175
1176 krb_afslog(0, 0);
1177 }
1178#endif /* AFS */
1179
1180 /* Initialize the environment. */
1181 envsize = 100;
1182 env = xmalloc(envsize * sizeof(char *));
1183 env[0] = NULL;
1184
Damien Millerbac2d8a2000-09-05 16:13:06 +11001185#ifdef HAVE_CYGWIN
1186 /*
1187 * The Windows environment contains some setting which are
1188 * important for a running system. They must not be dropped.
1189 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001190 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001191#endif
1192
Damien Millerb38eff82000-04-01 11:09:21 +10001193 if (!options.use_login) {
1194 /* Set basic environment. */
1195 child_set_env(&env, &envsize, "USER", pw->pw_name);
1196 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1197 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001198#ifdef HAVE_LOGIN_CAP
1199 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1200 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001201#else /* HAVE_LOGIN_CAP */
1202# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001203 /*
1204 * There's no standard path on Windows. The path contains
1205 * important components pointing to the system directories,
1206 * needed for loading shared libraries. So the path better
1207 * remains intact here.
1208 */
Damien Millerb38eff82000-04-01 11:09:21 +10001209 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001210# endif /* HAVE_CYGWIN */
1211#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001212
1213 snprintf(buf, sizeof buf, "%.200s/%.50s",
1214 _PATH_MAILDIR, pw->pw_name);
1215 child_set_env(&env, &envsize, "MAIL", buf);
1216
1217 /* Normal systems set SHELL by default. */
1218 child_set_env(&env, &envsize, "SHELL", shell);
1219 }
1220 if (getenv("TZ"))
1221 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1222
1223 /* Set custom environment options from RSA authentication. */
1224 while (custom_environment) {
1225 struct envstring *ce = custom_environment;
1226 char *s = ce->s;
1227 int i;
1228 for (i = 0; s[i] != '=' && s[i]; i++);
1229 if (s[i] == '=') {
1230 s[i] = 0;
1231 child_set_env(&env, &envsize, s, s + i + 1);
1232 }
1233 custom_environment = ce->next;
1234 xfree(ce->s);
1235 xfree(ce);
1236 }
1237
1238 snprintf(buf, sizeof buf, "%.50s %d %d",
1239 get_remote_ipaddr(), get_remote_port(), get_local_port());
1240 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1241
1242 if (ttyname)
1243 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1244 if (term)
1245 child_set_env(&env, &envsize, "TERM", term);
1246 if (display)
1247 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001248 if (original_command)
1249 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1250 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001251
1252#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001253 if ((cp = getenv("AUTHSTATE")) != NULL)
1254 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1255 if ((cp = getenv("KRB5CCNAME")) != NULL)
1256 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1257 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001258#endif
1259
1260#ifdef KRB4
1261 {
1262 extern char *ticket;
1263
1264 if (ticket)
1265 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1266 }
1267#endif /* KRB4 */
1268
1269#ifdef USE_PAM
1270 /* Pull in any environment variables that may have been set by PAM. */
1271 do_pam_environment(&env, &envsize);
1272#endif /* USE_PAM */
1273
Damien Millerb38eff82000-04-01 11:09:21 +10001274 if (xauthfile)
1275 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1276 if (auth_get_socket_name() != NULL)
1277 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1278 auth_get_socket_name());
1279
1280 /* read $HOME/.ssh/environment. */
1281 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001282 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1283 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001284 read_environment_file(&env, &envsize, buf);
1285 }
1286 if (debug_flag) {
1287 /* dump the environment */
1288 fprintf(stderr, "Environment:\n");
1289 for (i = 0; env[i]; i++)
1290 fprintf(stderr, " %.200s\n", env[i]);
1291 }
Damien Millerad833b32000-08-23 10:46:23 +10001292 /* we have to stash the hostname before we close our socket. */
1293 if (options.use_login)
1294 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001295 /*
1296 * Close the connection descriptors; note that this is the child, and
1297 * the server will still have the socket open, and it is important
1298 * that we do not shutdown it. Note that the descriptors cannot be
1299 * closed before building the environment, as we call
1300 * get_remote_ipaddr there.
1301 */
1302 if (packet_get_connection_in() == packet_get_connection_out())
1303 close(packet_get_connection_in());
1304 else {
1305 close(packet_get_connection_in());
1306 close(packet_get_connection_out());
1307 }
1308 /*
1309 * Close all descriptors related to channels. They will still remain
1310 * open in the parent.
1311 */
1312 /* XXX better use close-on-exec? -markus */
1313 channel_close_all();
1314
1315 /*
1316 * Close any extra file descriptors. Note that there may still be
1317 * descriptors left by system functions. They will be closed later.
1318 */
1319 endpwent();
1320
1321 /*
1322 * Close any extra open file descriptors so that we don\'t have them
1323 * hanging around in clients. Note that we want to do this after
1324 * initgroups, because at least on Solaris 2.3 it leaves file
1325 * descriptors open.
1326 */
1327 for (i = 3; i < 64; i++)
1328 close(i);
1329
1330 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001331 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001332 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1333 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001334#ifdef HAVE_LOGIN_CAP
1335 if (login_getcapbool(lc, "requirehome", 0))
1336 exit(1);
1337#endif
1338 }
Damien Millerb38eff82000-04-01 11:09:21 +10001339
1340 /*
1341 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1342 * xauth are run in the proper environment.
1343 */
1344 environ = env;
1345
1346 /*
1347 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1348 * in this order).
1349 */
1350 if (!options.use_login) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001351 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001352 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001353 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001354
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001355 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001356 if (f) {
1357 if (auth_proto != NULL && auth_data != NULL)
1358 fprintf(f, "%s %s\n", auth_proto, auth_data);
1359 pclose(f);
1360 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001361 fprintf(stderr, "Could not run %s\n", _PATH_SSH_USER_RC);
1362 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001363 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001364 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001365
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001366 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001367 if (f) {
1368 if (auth_proto != NULL && auth_data != NULL)
1369 fprintf(f, "%s %s\n", auth_proto, auth_data);
1370 pclose(f);
1371 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001372 fprintf(stderr, "Could not run %s\n", _PATH_SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001373 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001374 /* Add authority data to .Xauthority if appropriate. */
1375 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001376 char *screen = strchr(display, ':');
1377 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001378 fprintf(stderr,
1379 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001380 options.xauth_location, display,
1381 auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001382#ifndef NO_X11_UNIX_SOCKETS
Damien Millerd999ae22000-05-20 12:49:31 +10001383 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001384 fprintf(stderr,
1385 "Adding %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001386 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001387 screen, auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001388#endif /* NO_X11_UNIX_SOCKETS */
Damien Millerd999ae22000-05-20 12:49:31 +10001389 }
Damien Miller0c043c12000-06-07 21:22:38 +10001390 snprintf(cmd, sizeof cmd, "%s -q -",
1391 options.xauth_location);
1392 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001393 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001394 fprintf(f, "add %s %s %s\n", display,
1395 auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001396#ifndef NO_X11_UNIX_SOCKETS
Kevin Stevesef4eea92001-02-05 12:42:17 +00001397 if (screen != NULL)
Damien Millerd999ae22000-05-20 12:49:31 +10001398 fprintf(f, "add %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001399 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001400 screen, auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001401#endif /* NO_X11_UNIX_SOCKETS */
Damien Millerb38eff82000-04-01 11:09:21 +10001402 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001403 } else {
1404 fprintf(stderr, "Could not run %s\n",
1405 cmd);
1406 }
Damien Millerb38eff82000-04-01 11:09:21 +10001407 }
1408 }
Damien Millerb38eff82000-04-01 11:09:21 +10001409 /* Get the last component of the shell name. */
1410 cp = strrchr(shell, '/');
1411 if (cp)
1412 cp++;
1413 else
1414 cp = shell;
1415 }
1416 /*
1417 * If we have no command, execute the shell. In this case, the shell
1418 * name to be passed in argv[0] is preceded by '-' to indicate that
1419 * this is a login shell.
1420 */
1421 if (!command) {
1422 if (!options.use_login) {
1423 char buf[256];
1424
1425 /*
1426 * Check for mail if we have a tty and it was enabled
1427 * in server options.
1428 */
1429 if (ttyname && options.check_mail) {
1430 char *mailbox;
1431 struct stat mailstat;
1432 mailbox = getenv("MAIL");
1433 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001434 if (stat(mailbox, &mailstat) != 0 ||
1435 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001436 printf("No mail.\n");
1437 else if (mailstat.st_mtime < mailstat.st_atime)
1438 printf("You have mail.\n");
1439 else
1440 printf("You have new mail.\n");
1441 }
1442 }
1443 /* Start the shell. Set initial character to '-'. */
1444 buf[0] = '-';
1445 strncpy(buf + 1, cp, sizeof(buf) - 1);
1446 buf[sizeof(buf) - 1] = 0;
1447
1448 /* Execute the shell. */
1449 argv[0] = buf;
1450 argv[1] = NULL;
1451 execve(shell, argv, env);
1452
1453 /* Executing the shell failed. */
1454 perror(shell);
1455 exit(1);
1456
1457 } else {
1458 /* Launch login(1). */
1459
Damien Millerad833b32000-08-23 10:46:23 +10001460 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001461 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001462
1463 /* Login couldn't be executed, die. */
1464
1465 perror("login");
1466 exit(1);
1467 }
1468 }
1469 /*
1470 * Execute the command using the user's shell. This uses the -c
1471 * option to execute the command.
1472 */
1473 argv[0] = (char *) cp;
1474 argv[1] = "-c";
1475 argv[2] = (char *) command;
1476 argv[3] = NULL;
1477 execve(shell, argv, env);
1478 perror(shell);
1479 exit(1);
1480}
1481
1482Session *
1483session_new(void)
1484{
1485 int i;
1486 static int did_init = 0;
1487 if (!did_init) {
1488 debug("session_new: init");
1489 for(i = 0; i < MAX_SESSIONS; i++) {
1490 sessions[i].used = 0;
1491 sessions[i].self = i;
1492 }
1493 did_init = 1;
1494 }
1495 for(i = 0; i < MAX_SESSIONS; i++) {
1496 Session *s = &sessions[i];
1497 if (! s->used) {
1498 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001499 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001500 s->chanid = -1;
1501 s->ptyfd = -1;
1502 s->ttyfd = -1;
1503 s->term = NULL;
1504 s->pw = NULL;
1505 s->display = NULL;
1506 s->screen = 0;
1507 s->auth_data = NULL;
1508 s->auth_proto = NULL;
1509 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001510 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001511 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001512 return s;
1513 }
1514 }
1515 return NULL;
1516}
1517
1518void
1519session_dump(void)
1520{
1521 int i;
1522 for(i = 0; i < MAX_SESSIONS; i++) {
1523 Session *s = &sessions[i];
1524 debug("dump: used %d session %d %p channel %d pid %d",
1525 s->used,
1526 s->self,
1527 s,
1528 s->chanid,
1529 s->pid);
1530 }
1531}
1532
Damien Millerefb4afe2000-04-12 18:45:05 +10001533int
1534session_open(int chanid)
1535{
1536 Session *s = session_new();
1537 debug("session_open: channel %d", chanid);
1538 if (s == NULL) {
1539 error("no more sessions");
1540 return 0;
1541 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001542 s->pw = auth_get_user();
1543 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001544 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001545 debug("session_open: session %d: link with channel %d", s->self, chanid);
1546 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001547 return 1;
1548}
1549
1550Session *
1551session_by_channel(int id)
1552{
1553 int i;
1554 for(i = 0; i < MAX_SESSIONS; i++) {
1555 Session *s = &sessions[i];
1556 if (s->used && s->chanid == id) {
1557 debug("session_by_channel: session %d channel %d", i, id);
1558 return s;
1559 }
1560 }
1561 debug("session_by_channel: unknown channel %d", id);
1562 session_dump();
1563 return NULL;
1564}
1565
1566Session *
1567session_by_pid(pid_t pid)
1568{
1569 int i;
1570 debug("session_by_pid: pid %d", pid);
1571 for(i = 0; i < MAX_SESSIONS; i++) {
1572 Session *s = &sessions[i];
1573 if (s->used && s->pid == pid)
1574 return s;
1575 }
1576 error("session_by_pid: unknown pid %d", pid);
1577 session_dump();
1578 return NULL;
1579}
1580
1581int
1582session_window_change_req(Session *s)
1583{
1584 s->col = packet_get_int();
1585 s->row = packet_get_int();
1586 s->xpixel = packet_get_int();
1587 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001588 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001589 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1590 return 1;
1591}
1592
1593int
1594session_pty_req(Session *s)
1595{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001596 u_int len;
Damien Miller4af51302000-04-16 11:18:38 +10001597 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001598
Damien Millerf6d9e222000-06-18 14:50:44 +10001599 if (no_pty_flag)
1600 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001601 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001602 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001603 s->term = packet_get_string(&len);
1604 s->col = packet_get_int();
1605 s->row = packet_get_int();
1606 s->xpixel = packet_get_int();
1607 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001608 term_modes = packet_get_string(&len);
1609 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001610
1611 if (strcmp(s->term, "") == 0) {
1612 xfree(s->term);
1613 s->term = NULL;
1614 }
1615 /* Allocate a pty and open it. */
1616 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1617 xfree(s->term);
1618 s->term = NULL;
1619 s->ptyfd = -1;
1620 s->ttyfd = -1;
1621 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001622 xfree(term_modes);
1623 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001624 }
1625 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1626 /*
1627 * Add a cleanup function to clear the utmp entry and record logout
1628 * time in case we call fatal() (e.g., the connection gets closed).
1629 */
1630 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1631 pty_setowner(s->pw, s->tty);
1632 /* Get window size from the packet. */
1633 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1634
Damien Millere247cc42000-05-07 12:03:14 +10001635 session_proctitle(s);
1636
Damien Miller5f056372000-04-16 12:31:48 +10001637 /* XXX parse and set terminal modes */
1638 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001639 return 1;
1640}
1641
Damien Millerbd483e72000-04-30 10:00:53 +10001642int
1643session_subsystem_req(Session *s)
1644{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001645 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001646 int success = 0;
1647 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001648 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001649
1650 packet_done();
1651 log("subsystem request for %s", subsys);
1652
Damien Millerf6d9e222000-06-18 14:50:44 +10001653 for (i = 0; i < options.num_subsystems; i++) {
1654 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1655 debug("subsystem: exec() %s", options.subsystem_command[i]);
1656 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1657 success = 1;
1658 }
1659 }
1660
1661 if (!success)
1662 log("subsystem request for %s failed, subsystem not found", subsys);
1663
Damien Millerbd483e72000-04-30 10:00:53 +10001664 xfree(subsys);
1665 return success;
1666}
1667
1668int
1669session_x11_req(Session *s)
1670{
Damien Miller7b28dc52000-09-05 13:34:53 +11001671 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001672 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001673 debug("X11 forwarding disabled in user configuration file.");
1674 return 0;
1675 }
Damien Millerbd483e72000-04-30 10:00:53 +10001676 if (!options.x11_forwarding) {
1677 debug("X11 forwarding disabled in server configuration file.");
1678 return 0;
1679 }
1680 if (xauthfile != NULL) {
1681 debug("X11 fwd already started.");
1682 return 0;
1683 }
1684
1685 debug("Received request for X11 forwarding with auth spoofing.");
1686 if (s->display != NULL)
1687 packet_disconnect("Protocol error: X11 display already set.");
1688
1689 s->single_connection = packet_get_char();
1690 s->auth_proto = packet_get_string(NULL);
1691 s->auth_data = packet_get_string(NULL);
1692 s->screen = packet_get_int();
1693 packet_done();
1694
1695 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1696 if (s->display == NULL) {
1697 xfree(s->auth_proto);
1698 xfree(s->auth_data);
1699 return 0;
1700 }
1701 xauthfile = xmalloc(MAXPATHLEN);
1702 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1703 temporarily_use_uid(s->pw->pw_uid);
1704 if (mkdtemp(xauthfile) == NULL) {
1705 restore_uid();
1706 error("private X11 dir: mkdtemp %s failed: %s",
1707 xauthfile, strerror(errno));
1708 xfree(xauthfile);
1709 xauthfile = NULL;
1710 xfree(s->auth_proto);
1711 xfree(s->auth_data);
1712 /* XXXX remove listening channels */
1713 return 0;
1714 }
1715 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001716 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1717 if (fd >= 0)
1718 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001719 restore_uid();
1720 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1721 return 1;
1722}
1723
Damien Millerf6d9e222000-06-18 14:50:44 +10001724int
1725session_shell_req(Session *s)
1726{
1727 /* if forced_command == NULL, the shell is execed */
1728 char *shell = forced_command;
1729 packet_done();
1730 s->extended = 1;
1731 if (s->ttyfd == -1)
1732 do_exec_no_pty(s, shell, s->pw);
1733 else
1734 do_exec_pty(s, shell, s->pw);
1735 return 1;
1736}
1737
1738int
1739session_exec_req(Session *s)
1740{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001741 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001742 char *command = packet_get_string(&len);
1743 packet_done();
1744 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001745 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001746 command = forced_command;
1747 debug("Forced command '%.500s'", forced_command);
1748 }
1749 s->extended = 1;
1750 if (s->ttyfd == -1)
1751 do_exec_no_pty(s, command, s->pw);
1752 else
1753 do_exec_pty(s, command, s->pw);
1754 if (forced_command == NULL)
1755 xfree(command);
1756 return 1;
1757}
1758
Damien Miller0bc1bd82000-11-13 22:57:25 +11001759int
1760session_auth_agent_req(Session *s)
1761{
1762 static int called = 0;
1763 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001764 if (no_agent_forwarding_flag) {
1765 debug("session_auth_agent_req: no_agent_forwarding_flag");
1766 return 0;
1767 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001768 if (called) {
1769 return 0;
1770 } else {
1771 called = 1;
1772 return auth_input_request_forwarding(s->pw);
1773 }
1774}
1775
Damien Millerefb4afe2000-04-12 18:45:05 +10001776void
1777session_input_channel_req(int id, void *arg)
1778{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001779 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001780 int reply;
1781 int success = 0;
1782 char *rtype;
1783 Session *s;
1784 Channel *c;
1785
1786 rtype = packet_get_string(&len);
1787 reply = packet_get_char();
1788
1789 s = session_by_channel(id);
1790 if (s == NULL)
1791 fatal("session_input_channel_req: channel %d: no session", id);
1792 c = channel_lookup(id);
1793 if (c == NULL)
1794 fatal("session_input_channel_req: channel %d: bad channel", id);
1795
1796 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1797 s->self, id, rtype, reply);
1798
1799 /*
1800 * a session is in LARVAL state until a shell
1801 * or programm is executed
1802 */
1803 if (c->type == SSH_CHANNEL_LARVAL) {
1804 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001805 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001806 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001807 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001808 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001809 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001810 } else if (strcmp(rtype, "x11-req") == 0) {
1811 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001812 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1813 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001814 } else if (strcmp(rtype, "subsystem") == 0) {
1815 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001816 }
1817 }
1818 if (strcmp(rtype, "window-change") == 0) {
1819 success = session_window_change_req(s);
1820 }
1821
1822 if (reply) {
1823 packet_start(success ?
1824 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1825 packet_put_int(c->remote_id);
1826 packet_send();
1827 }
1828 xfree(rtype);
1829}
1830
1831void
1832session_set_fds(Session *s, int fdin, int fdout, int fderr)
1833{
1834 if (!compat20)
1835 fatal("session_set_fds: called for proto != 2.0");
1836 /*
1837 * now that have a child and a pipe to the child,
1838 * we can activate our channel and register the fd's
1839 */
1840 if (s->chanid == -1)
1841 fatal("no channel for session %d", s->self);
1842 channel_set_fds(s->chanid,
1843 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001844 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1845 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001846}
1847
Damien Millerb38eff82000-04-01 11:09:21 +10001848void
1849session_pty_cleanup(Session *s)
1850{
1851 if (s == NULL || s->ttyfd == -1)
1852 return;
1853
Kevin Steves43cdef32001-02-11 14:12:08 +00001854 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001855
1856 /* Cancel the cleanup function. */
1857 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1858
1859 /* Record that the user has logged out. */
1860 record_logout(s->pid, s->tty);
1861
1862 /* Release the pseudo-tty. */
1863 pty_release(s->tty);
1864
1865 /*
1866 * Close the server side of the socket pairs. We must do this after
1867 * the pty cleanup, so that another process doesn't get this pty
1868 * while we're still cleaning up.
1869 */
1870 if (close(s->ptymaster) < 0)
1871 error("close(s->ptymaster): %s", strerror(errno));
1872}
Damien Millerefb4afe2000-04-12 18:45:05 +10001873
1874void
1875session_exit_message(Session *s, int status)
1876{
1877 Channel *c;
1878 if (s == NULL)
1879 fatal("session_close: no session");
1880 c = channel_lookup(s->chanid);
1881 if (c == NULL)
1882 fatal("session_close: session %d: no channel %d",
1883 s->self, s->chanid);
1884 debug("session_exit_message: session %d channel %d pid %d",
1885 s->self, s->chanid, s->pid);
1886
1887 if (WIFEXITED(status)) {
1888 channel_request_start(s->chanid,
1889 "exit-status", 0);
1890 packet_put_int(WEXITSTATUS(status));
1891 packet_send();
1892 } else if (WIFSIGNALED(status)) {
1893 channel_request_start(s->chanid,
1894 "exit-signal", 0);
1895 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001896#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001897 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001898#else /* WCOREDUMP */
1899 packet_put_char(0);
1900#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001901 packet_put_cstring("");
1902 packet_put_cstring("");
1903 packet_send();
1904 } else {
1905 /* Some weird exit cause. Just exit. */
1906 packet_disconnect("wait returned status %04x.", status);
1907 }
1908
1909 /* disconnect channel */
1910 debug("session_exit_message: release channel %d", s->chanid);
1911 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001912 /*
1913 * emulate a write failure with 'chan_write_failed', nobody will be
1914 * interested in data we write.
1915 * Note that we must not call 'chan_read_failed', since there could
1916 * be some more data waiting in the pipe.
1917 */
Damien Millerbd483e72000-04-30 10:00:53 +10001918 if (c->ostate != CHAN_OUTPUT_CLOSED)
1919 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001920 s->chanid = -1;
1921}
1922
1923void
1924session_free(Session *s)
1925{
1926 debug("session_free: session %d pid %d", s->self, s->pid);
1927 if (s->term)
1928 xfree(s->term);
1929 if (s->display)
1930 xfree(s->display);
1931 if (s->auth_data)
1932 xfree(s->auth_data);
1933 if (s->auth_proto)
1934 xfree(s->auth_proto);
1935 s->used = 0;
1936}
1937
1938void
1939session_close(Session *s)
1940{
1941 session_pty_cleanup(s);
1942 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001943 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001944}
1945
1946void
1947session_close_by_pid(pid_t pid, int status)
1948{
1949 Session *s = session_by_pid(pid);
1950 if (s == NULL) {
1951 debug("session_close_by_pid: no session for pid %d", s->pid);
1952 return;
1953 }
1954 if (s->chanid != -1)
1955 session_exit_message(s, status);
1956 session_close(s);
1957}
1958
1959/*
1960 * this is called when a channel dies before
1961 * the session 'child' itself dies
1962 */
1963void
1964session_close_by_channel(int id, void *arg)
1965{
1966 Session *s = session_by_channel(id);
1967 if (s == NULL) {
1968 debug("session_close_by_channel: no session for channel %d", id);
1969 return;
1970 }
1971 /* disconnect channel */
1972 channel_cancel_cleanup(s->chanid);
1973 s->chanid = -1;
1974
1975 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1976 if (s->pid == 0) {
1977 /* close session immediately */
1978 session_close(s);
1979 } else {
1980 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001981 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001982 error("session_close_by_channel: kill %d: %s",
1983 s->pid, strerror(errno));
1984 }
1985}
1986
Damien Millere247cc42000-05-07 12:03:14 +10001987char *
1988session_tty_list(void)
1989{
1990 static char buf[1024];
1991 int i;
1992 buf[0] = '\0';
1993 for(i = 0; i < MAX_SESSIONS; i++) {
1994 Session *s = &sessions[i];
1995 if (s->used && s->ttyfd != -1) {
1996 if (buf[0] != '\0')
1997 strlcat(buf, ",", sizeof buf);
1998 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1999 }
2000 }
2001 if (buf[0] == '\0')
2002 strlcpy(buf, "notty", sizeof buf);
2003 return buf;
2004}
2005
2006void
2007session_proctitle(Session *s)
2008{
2009 if (s->pw == NULL)
2010 error("no user for session %d", s->self);
2011 else
2012 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2013}
2014
Damien Millerefb4afe2000-04-12 18:45:05 +10002015void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002016do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002017{
2018 /*
2019 * Cancel the alarm we set to limit the time taken for
2020 * authentication.
2021 */
2022 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10002023 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10002024 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10002025 startup_pipe = -1;
2026 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00002027#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002028 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10002029 error("unable to get login class");
2030 return;
2031 }
2032#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10002033 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002034 if (xauthfile)
2035 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002036}