blob: 3c53f5cc1f16c7ce506018aa0efd0318dc485f29 [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
Damien Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
Ben Lindstrom92a2e382001-03-05 06:59:27 +000012 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Ben Lindstrom9c532442001-03-05 07:33:14 +000036RCSID("$OpenBSD: session.c,v 1.59 2001/03/04 01:46:30 djm Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000042#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "packet.h"
44#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100045#include "mpaux.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "uidswap.h"
47#include "compat.h"
48#include "channels.h"
49#include "nchan.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100051#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100052#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "pathnames.h"
54#include "log.h"
55#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000056#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "serverloop.h"
58#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000059#include "session.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060
Damien Miller91606b12000-06-28 08:22:29 +100061#ifdef WITH_IRIX_PROJECT
62#include <proj.h>
63#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000064#ifdef WITH_IRIX_JOBS
65#include <sys/resource.h>
Kevin Stevesef4eea92001-02-05 12:42:17 +000066#endif
Ben Lindstrom49a79c02000-11-17 03:47:20 +000067#ifdef WITH_IRIX_AUDIT
68#include <sat.h>
69#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100070
Damien Miller37023962000-07-11 17:31:38 +100071#if defined(HAVE_USERSEC_H)
72#include <usersec.h>
73#endif
74
Damien Millerbac2d8a2000-09-05 16:13:06 +110075#ifdef HAVE_CYGWIN
76#include <windows.h>
77#include <sys/cygwin.h>
78#define is_winnt (GetVersion() < 0x80000000)
79#endif
80
Damien Millerd17b8d52000-08-09 14:42:28 +100081/* AIX limits */
82#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100083# define S_UFSIZE_HARD S_UFSIZE "_hard"
84# define S_UCPU_HARD S_UCPU "_hard"
85# define S_UDATA_HARD S_UDATA "_hard"
86# define S_USTACK_HARD S_USTACK "_hard"
87# define S_URSS_HARD S_URSS "_hard"
88# define S_UCORE_HARD S_UCORE "_hard"
89# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100090#endif
91
Damien Miller168a7002001-03-17 10:29:50 +110092#ifdef _AIX
93# include <uinfo.h>
94#endif
95
Damien Millerb38eff82000-04-01 11:09:21 +100096/* types */
97
98#define TTYSZ 64
99typedef struct Session Session;
100struct Session {
101 int used;
102 int self;
Damien Millerbd483e72000-04-30 10:00:53 +1000103 int extended;
Damien Millerb38eff82000-04-01 11:09:21 +1000104 struct passwd *pw;
105 pid_t pid;
106 /* tty */
107 char *term;
108 int ptyfd, ttyfd, ptymaster;
109 int row, col, xpixel, ypixel;
110 char tty[TTYSZ];
111 /* X11 */
112 char *display;
113 int screen;
114 char *auth_proto;
115 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000116 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000117 /* proto 2 */
118 int chanid;
119};
120
121/* func */
122
123Session *session_new(void);
124void session_set_fds(Session *s, int fdin, int fdout, int fderr);
125void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000126void session_proctitle(Session *s);
Damien Millerb38eff82000-04-01 11:09:21 +1000127void do_exec_pty(Session *s, const char *command, struct passwd * pw);
128void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
Damien Miller69b69aa2000-10-28 14:19:58 +1100129void do_login(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000130
131void
132do_child(const char *command, struct passwd * pw, const char *term,
133 const char *display, const char *auth_proto,
134 const char *auth_data, const char *ttyname);
135
136/* import */
137extern ServerOptions options;
138extern char *__progname;
139extern int log_stderr;
140extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000141extern u_int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000142
Damien Miller37023962000-07-11 17:31:38 +1000143extern int startup_pipe;
144
Damien Millerb38eff82000-04-01 11:09:21 +1000145/* Local Xauthority file. */
146static char *xauthfile;
147
Damien Miller7b28dc52000-09-05 13:34:53 +1100148/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000149char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100150
Damien Millerb38eff82000-04-01 11:09:21 +1000151/* data */
152#define MAX_SESSIONS 10
153Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100154
Damien Millerd2c208a2000-05-17 22:00:02 +1000155#ifdef WITH_AIXAUTHENTICATE
156/* AIX's lastlogin message, set in auth1.c */
157char *aixloginmsg;
158#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000159
Damien Millerad833b32000-08-23 10:46:23 +1000160#ifdef HAVE_LOGIN_CAP
161static login_cap_t *lc;
162#endif
163
Damien Millerb38eff82000-04-01 11:09:21 +1000164/*
165 * Remove local Xauthority file.
166 */
167void
168xauthfile_cleanup_proc(void *ignore)
169{
170 debug("xauthfile_cleanup_proc called");
171
172 if (xauthfile != NULL) {
173 char *p;
174 unlink(xauthfile);
175 p = strrchr(xauthfile, '/');
176 if (p != NULL) {
177 *p = '\0';
178 rmdir(xauthfile);
179 }
180 xfree(xauthfile);
181 xauthfile = NULL;
182 }
183}
184
185/*
186 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
187 * dropped connection).
188 */
Damien Miller4af51302000-04-16 11:18:38 +1000189void
Damien Millerb38eff82000-04-01 11:09:21 +1000190pty_cleanup_proc(void *session)
191{
192 Session *s=session;
193 if (s == NULL)
194 fatal("pty_cleanup_proc: no session");
195 debug("pty_cleanup_proc: %s", s->tty);
196
197 if (s->pid != 0) {
198 /* Record that the user has logged out. */
199 record_logout(s->pid, s->tty);
200 }
201
202 /* Release the pseudo-tty. */
203 pty_release(s->tty);
204}
205
206/*
207 * Prepares for an interactive session. This is called after the user has
208 * been successfully authenticated. During this message exchange, pseudo
209 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
210 * are requested, etc.
211 */
Damien Miller4af51302000-04-16 11:18:38 +1000212void
Damien Millerb38eff82000-04-01 11:09:21 +1000213do_authenticated(struct passwd * pw)
214{
215 Session *s;
Damien Miller7b28dc52000-09-05 13:34:53 +1100216 int type, fd;
Damien Millerb38eff82000-04-01 11:09:21 +1000217 int compression_level = 0, enable_compression_after_reply = 0;
218 int have_pty = 0;
219 char *command;
220 int n_bytes;
221 int plen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000222 u_int proto_len, data_len, dlen;
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000223 int screen_flag;
Damien Millerb38eff82000-04-01 11:09:21 +1000224
225 /*
226 * Cancel the alarm we set to limit the time taken for
227 * authentication.
228 */
229 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +1000230 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +1000231 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +1000232 startup_pipe = -1;
233 }
Damien Millerb38eff82000-04-01 11:09:21 +1000234
235 /*
236 * Inform the channel mechanism that we are the server side and that
237 * the client may request to connect to any port at all. (The user
238 * could do it anyway, and we wouldn\'t know what is permitted except
239 * by the client telling us, so we can equally well trust the client
240 * not to request anything bogus.)
241 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100242 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
Damien Millerb38eff82000-04-01 11:09:21 +1000243 channel_permit_all_opens();
244
245 s = session_new();
Damien Millerbd483e72000-04-30 10:00:53 +1000246 s->pw = pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000247
Kevin Steves48b7cc02000-10-07 13:24:00 +0000248#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +1000249 if ((lc = login_getclass(pw->pw_class)) == NULL) {
250 error("unable to get login class");
251 return;
252 }
253#endif
254
Damien Millerb38eff82000-04-01 11:09:21 +1000255 /*
256 * We stay in this loop until the client requests to execute a shell
257 * or a command.
258 */
259 for (;;) {
260 int success = 0;
261
262 /* Get a packet from the client. */
263 type = packet_read(&plen);
264
265 /* Process the packet. */
266 switch (type) {
267 case SSH_CMSG_REQUEST_COMPRESSION:
268 packet_integrity_check(plen, 4, type);
269 compression_level = packet_get_int();
270 if (compression_level < 1 || compression_level > 9) {
271 packet_send_debug("Received illegal compression level %d.",
272 compression_level);
273 break;
274 }
275 /* Enable compression after we have responded with SUCCESS. */
276 enable_compression_after_reply = 1;
277 success = 1;
278 break;
279
280 case SSH_CMSG_REQUEST_PTY:
281 if (no_pty_flag) {
282 debug("Allocating a pty not permitted for this authentication.");
283 break;
284 }
285 if (have_pty)
286 packet_disconnect("Protocol error: you already have a pty.");
287
288 debug("Allocating pty.");
289
290 /* Allocate a pty and open it. */
291 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
292 sizeof(s->tty))) {
293 error("Failed to allocate pty.");
294 break;
295 }
296 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
297 pty_setowner(pw, s->tty);
298
299 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
300 s->term = packet_get_string(&dlen);
301 packet_integrity_check(dlen, strlen(s->term), type);
302 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
303 /* Remaining bytes */
304 n_bytes = plen - (4 + dlen + 4 * 4);
305
306 if (strcmp(s->term, "") == 0) {
307 xfree(s->term);
308 s->term = NULL;
309 }
310 /* Get window size from the packet. */
311 s->row = packet_get_int();
312 s->col = packet_get_int();
313 s->xpixel = packet_get_int();
314 s->ypixel = packet_get_int();
315 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
316
317 /* Get tty modes from the packet. */
318 tty_parse_modes(s->ttyfd, &n_bytes);
319 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
320
Damien Millere247cc42000-05-07 12:03:14 +1000321 session_proctitle(s);
322
Damien Millerb38eff82000-04-01 11:09:21 +1000323 /* Indicate that we now have a pty. */
324 success = 1;
325 have_pty = 1;
326 break;
327
328 case SSH_CMSG_X11_REQUEST_FORWARDING:
329 if (!options.x11_forwarding) {
330 packet_send_debug("X11 forwarding disabled in server configuration file.");
331 break;
332 }
Damien Miller0c043c12000-06-07 21:22:38 +1000333 if (!options.xauth_location) {
334 packet_send_debug("No xauth program; cannot forward with spoofing.");
335 break;
336 }
Damien Millerb38eff82000-04-01 11:09:21 +1000337 if (no_x11_forwarding_flag) {
338 packet_send_debug("X11 forwarding not permitted for this authentication.");
339 break;
340 }
341 debug("Received request for X11 forwarding with auth spoofing.");
342 if (s->display != NULL)
343 packet_disconnect("Protocol error: X11 display already set.");
344
345 s->auth_proto = packet_get_string(&proto_len);
346 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000347
Ben Lindstrom7603b2d2001-02-26 20:13:32 +0000348 screen_flag = packet_get_protocol_flags() &
349 SSH_PROTOFLAG_SCREEN_NUMBER;
350 debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
351
352 if (packet_remaining() == 4) {
353 if (!screen_flag)
354 debug2("Buggy client: "
355 "X11 screen flag missing");
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000356 packet_integrity_check(plen,
357 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000358 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000359 } else {
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000360 packet_integrity_check(plen,
361 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000362 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000363 }
Damien Millerb38eff82000-04-01 11:09:21 +1000364 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
365
366 if (s->display == NULL)
367 break;
368
369 /* Setup to always have a local .Xauthority. */
370 xauthfile = xmalloc(MAXPATHLEN);
371 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
372 temporarily_use_uid(pw->pw_uid);
373 if (mkdtemp(xauthfile) == NULL) {
374 restore_uid();
375 error("private X11 dir: mkdtemp %s failed: %s",
376 xauthfile, strerror(errno));
377 xfree(xauthfile);
378 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000379 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000380 break;
381 }
382 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100383 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
384 if (fd >= 0)
385 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000386 restore_uid();
387 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
388 success = 1;
389 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000390
391 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
392 if (no_agent_forwarding_flag || compat13) {
393 debug("Authentication agent forwarding not permitted for this authentication.");
394 break;
395 }
396 debug("Received authentication agent forwarding request.");
Damien Miller0c043c12000-06-07 21:22:38 +1000397 success = auth_input_request_forwarding(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000398 break;
399
400 case SSH_CMSG_PORT_FORWARD_REQUEST:
401 if (no_port_forwarding_flag) {
402 debug("Port forwarding not permitted for this authentication.");
403 break;
404 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100405 if (!options.allow_tcp_forwarding) {
406 debug("Port forwarding not permitted.");
407 break;
408 }
Damien Millerb38eff82000-04-01 11:09:21 +1000409 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000410 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000411 success = 1;
412 break;
413
414 case SSH_CMSG_MAX_PACKET_SIZE:
415 if (packet_set_maxsize(packet_get_int()) > 0)
416 success = 1;
417 break;
418
419 case SSH_CMSG_EXEC_SHELL:
420 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000421 if (type == SSH_CMSG_EXEC_CMD) {
422 command = packet_get_string(&dlen);
423 debug("Exec command '%.500s'", command);
424 packet_integrity_check(plen, 4 + dlen, type);
425 } else {
426 command = NULL;
427 packet_integrity_check(plen, 0, type);
428 }
429 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100430 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000431 command = forced_command;
432 debug("Forced command '%.500s'", forced_command);
433 }
434 if (have_pty)
435 do_exec_pty(s, command, pw);
436 else
437 do_exec_no_pty(s, command, pw);
438
439 if (command != NULL)
440 xfree(command);
441 /* Cleanup user's local Xauthority file. */
442 if (xauthfile)
443 xauthfile_cleanup_proc(NULL);
444 return;
445
446 default:
447 /*
448 * Any unknown messages in this phase are ignored,
449 * and a failure message is returned.
450 */
451 log("Unknown packet type received after authentication: %d", type);
452 }
453 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
454 packet_send();
455 packet_write_wait();
456
457 /* Enable compression now that we have replied if appropriate. */
458 if (enable_compression_after_reply) {
459 enable_compression_after_reply = 0;
460 packet_start_compression(compression_level);
461 }
462 }
463}
464
465/*
466 * This is called to fork and execute a command when we have no tty. This
467 * will call do_child from the child, and server_loop from the parent after
468 * setting up file descriptors and such.
469 */
Damien Miller4af51302000-04-16 11:18:38 +1000470void
Damien Millerb38eff82000-04-01 11:09:21 +1000471do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
472{
473 int pid;
474
475#ifdef USE_PIPES
476 int pin[2], pout[2], perr[2];
477 /* Allocate pipes for communicating with the program. */
478 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
479 packet_disconnect("Could not create pipes: %.100s",
480 strerror(errno));
481#else /* USE_PIPES */
482 int inout[2], err[2];
483 /* Uses socket pairs to communicate with the program. */
484 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
485 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
486 packet_disconnect("Could not create socket pairs: %.100s",
487 strerror(errno));
488#endif /* USE_PIPES */
489 if (s == NULL)
490 fatal("do_exec_no_pty: no session");
491
Damien Millere247cc42000-05-07 12:03:14 +1000492 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000493
Damien Millerc5946332001-02-28 11:46:11 +1100494#if defined(USE_PAM)
Kevin Stevesff793a22001-02-21 16:36:51 +0000495 do_pam_setcred();
496#endif /* USE_PAM */
497
Damien Millerb38eff82000-04-01 11:09:21 +1000498 /* Fork the child. */
499 if ((pid = fork()) == 0) {
500 /* Child. Reinitialize the log since the pid has changed. */
501 log_init(__progname, options.log_level, options.log_facility, log_stderr);
502
Damien Miller152cea22000-12-13 19:21:51 +1100503 signal(SIGPIPE, SIG_DFL);
504
Damien Millerb38eff82000-04-01 11:09:21 +1000505 /*
506 * Create a new session and process group since the 4.4BSD
507 * setlogin() affects the entire process group.
508 */
509 if (setsid() < 0)
510 error("setsid failed: %.100s", strerror(errno));
511
512#ifdef USE_PIPES
513 /*
514 * Redirect stdin. We close the parent side of the socket
515 * pair, and make the child side the standard input.
516 */
517 close(pin[1]);
518 if (dup2(pin[0], 0) < 0)
519 perror("dup2 stdin");
520 close(pin[0]);
521
522 /* Redirect stdout. */
523 close(pout[0]);
524 if (dup2(pout[1], 1) < 0)
525 perror("dup2 stdout");
526 close(pout[1]);
527
528 /* Redirect stderr. */
529 close(perr[0]);
530 if (dup2(perr[1], 2) < 0)
531 perror("dup2 stderr");
532 close(perr[1]);
533#else /* USE_PIPES */
534 /*
535 * Redirect stdin, stdout, and stderr. Stdin and stdout will
536 * use the same socket, as some programs (particularly rdist)
537 * seem to depend on it.
538 */
539 close(inout[1]);
540 close(err[1]);
541 if (dup2(inout[0], 0) < 0) /* stdin */
542 perror("dup2 stdin");
543 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
544 perror("dup2 stdout");
545 if (dup2(err[0], 2) < 0) /* stderr */
546 perror("dup2 stderr");
547#endif /* USE_PIPES */
548
549 /* Do processing for the child (exec command etc). */
550 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
551 /* NOTREACHED */
552 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100553#ifdef HAVE_CYGWIN
554 if (is_winnt)
555 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
556#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000557 if (pid < 0)
558 packet_disconnect("fork failed: %.100s", strerror(errno));
559 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000560 /* Set interactive/non-interactive mode. */
561 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000562#ifdef USE_PIPES
563 /* We are the parent. Close the child sides of the pipes. */
564 close(pin[0]);
565 close(pout[1]);
566 close(perr[1]);
567
Damien Millerefb4afe2000-04-12 18:45:05 +1000568 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000569 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000570 } else {
571 /* Enter the interactive session. */
572 server_loop(pid, pin[1], pout[0], perr[0]);
573 /* server_loop has closed pin[1], pout[1], and perr[1]. */
574 }
Damien Millerb38eff82000-04-01 11:09:21 +1000575#else /* USE_PIPES */
576 /* We are the parent. Close the child sides of the socket pairs. */
577 close(inout[0]);
578 close(err[0]);
579
580 /*
581 * Enter the interactive session. Note: server_loop must be able to
582 * handle the case that fdin and fdout are the same.
583 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000584 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000585 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000586 } else {
587 server_loop(pid, inout[1], inout[1], err[1]);
588 /* server_loop has closed inout[1] and err[1]. */
589 }
Damien Millerb38eff82000-04-01 11:09:21 +1000590#endif /* USE_PIPES */
591}
592
593/*
594 * This is called to fork and execute a command when we have a tty. This
595 * will call do_child from the child, and server_loop from the parent after
596 * setting up file descriptors, controlling tty, updating wtmp, utmp,
597 * lastlog, and other such operations.
598 */
Damien Miller4af51302000-04-16 11:18:38 +1000599void
Damien Millerb38eff82000-04-01 11:09:21 +1000600do_exec_pty(Session *s, const char *command, struct passwd * pw)
601{
Damien Millerb38eff82000-04-01 11:09:21 +1000602 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000603 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000604
605 if (s == NULL)
606 fatal("do_exec_pty: no session");
607 ptyfd = s->ptyfd;
608 ttyfd = s->ttyfd;
609
Damien Millerc5946332001-02-28 11:46:11 +1100610#if defined(USE_PAM)
Kevin Stevesff793a22001-02-21 16:36:51 +0000611 do_pam_session(pw->pw_name, s->tty);
612 do_pam_setcred();
Damien Miller5a761312001-02-27 09:28:23 +1100613#endif
Kevin Stevesff793a22001-02-21 16:36:51 +0000614
Damien Millerb38eff82000-04-01 11:09:21 +1000615 /* Fork the child. */
616 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000617 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000618 log_init(__progname, options.log_level, options.log_facility, log_stderr);
619
Damien Miller152cea22000-12-13 19:21:51 +1100620 signal(SIGPIPE, SIG_DFL);
621
Damien Millerb38eff82000-04-01 11:09:21 +1000622 /* Close the master side of the pseudo tty. */
623 close(ptyfd);
624
625 /* Make the pseudo tty our controlling tty. */
626 pty_make_controlling_tty(&ttyfd, s->tty);
627
628 /* Redirect stdin from the pseudo tty. */
629 if (dup2(ttyfd, fileno(stdin)) < 0)
630 error("dup2 stdin failed: %.100s", strerror(errno));
631
632 /* Redirect stdout to the pseudo tty. */
633 if (dup2(ttyfd, fileno(stdout)) < 0)
634 error("dup2 stdin failed: %.100s", strerror(errno));
635
636 /* Redirect stderr to the pseudo tty. */
637 if (dup2(ttyfd, fileno(stderr)) < 0)
638 error("dup2 stdin failed: %.100s", strerror(errno));
639
640 /* Close the extra descriptor for the pseudo tty. */
641 close(ttyfd);
642
Damien Miller942da032000-08-18 13:59:06 +1000643 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100644 if (!(options.use_login && command == NULL))
645 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000646
Damien Millerb38eff82000-04-01 11:09:21 +1000647 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000648 do_child(command, pw, s->term, s->display, s->auth_proto,
649 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000650 /* NOTREACHED */
651 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100652#ifdef HAVE_CYGWIN
653 if (is_winnt)
654 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
655#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000656 if (pid < 0)
657 packet_disconnect("fork failed: %.100s", strerror(errno));
658 s->pid = pid;
659
660 /* Parent. Close the slave side of the pseudo tty. */
661 close(ttyfd);
662
663 /*
664 * Create another descriptor of the pty master side for use as the
665 * standard input. We could use the original descriptor, but this
666 * simplifies code in server_loop. The descriptor is bidirectional.
667 */
668 fdout = dup(ptyfd);
669 if (fdout < 0)
670 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
671
672 /* we keep a reference to the pty master */
673 ptymaster = dup(ptyfd);
674 if (ptymaster < 0)
675 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
676 s->ptymaster = ptymaster;
677
678 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000679 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000680 if (compat20) {
681 session_set_fds(s, ptyfd, fdout, -1);
682 } else {
683 server_loop(pid, ptyfd, fdout, -1);
684 /* server_loop _has_ closed ptyfd and fdout. */
685 session_pty_cleanup(s);
686 }
Damien Millerb38eff82000-04-01 11:09:21 +1000687}
688
Damien Miller942da032000-08-18 13:59:06 +1000689const char *
690get_remote_name_or_ip(void)
691{
692 static const char *remote = "";
693 if (utmp_len > 0)
Damien Miller33804262001-02-04 23:20:18 +1100694 remote = get_canonical_hostname(options.reverse_mapping_check);
Damien Miller942da032000-08-18 13:59:06 +1000695 if (utmp_len == 0 || strlen(remote) > utmp_len)
696 remote = get_remote_ipaddr();
697 return remote;
698}
699
700/* administrative, login(1)-like work */
701void
Damien Miller69b69aa2000-10-28 14:19:58 +1100702do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000703{
704 FILE *f;
705 char *time_string;
706 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100707 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000708 socklen_t fromlen;
709 struct sockaddr_storage from;
710 struct stat st;
711 time_t last_login_time;
712 struct passwd * pw = s->pw;
713 pid_t pid = getpid();
714
715 /*
716 * Get IP address of client. If the connection is not a socket, let
717 * the address be 0.0.0.0.
718 */
719 memset(&from, 0, sizeof(from));
720 if (packet_connection_is_on_socket()) {
721 fromlen = sizeof(from);
722 if (getpeername(packet_get_connection_in(),
723 (struct sockaddr *) & from, &fromlen) < 0) {
724 debug("getpeername: %.100s", strerror(errno));
725 fatal_cleanup();
726 }
727 }
728
Damien Miller7b28dc52000-09-05 13:34:53 +1100729 /* Get the time and hostname when the user last logged in. */
Damien Millere4340be2000-09-16 13:29:08 +1100730 hostname[0] = '\0';
731 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
732 hostname, sizeof(hostname));
733
Damien Miller942da032000-08-18 13:59:06 +1000734 /* Record that there was a login on that tty from the remote host. */
735 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
736 get_remote_name_or_ip(), (struct sockaddr *)&from);
737
Kevin Steves092f2ef2000-10-14 13:36:13 +0000738#ifdef USE_PAM
739 /*
740 * If password change is needed, do it now.
741 * This needs to occur before the ~/.hushlogin check.
742 */
Damien Miller646aa602001-02-15 11:51:32 +1100743 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000744 print_pam_messages();
745 do_pam_chauthtok();
746 }
747#endif
748
Damien Miller69b69aa2000-10-28 14:19:58 +1100749 /* Done if .hushlogin exists or a command given. */
750 if (command != NULL)
751 return;
Damien Miller942da032000-08-18 13:59:06 +1000752 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000753#ifdef HAVE_LOGIN_CAP
754 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
755#else
Damien Miller942da032000-08-18 13:59:06 +1000756 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000757#endif
Damien Miller942da032000-08-18 13:59:06 +1000758 return;
759
760#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100761 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000762 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000763#endif /* USE_PAM */
764#ifdef WITH_AIXAUTHENTICATE
765 if (aixloginmsg && *aixloginmsg)
766 printf("%s\n", aixloginmsg);
767#endif /* WITH_AIXAUTHENTICATE */
768
Damien Miller942da032000-08-18 13:59:06 +1000769 if (last_login_time != 0) {
770 time_string = ctime(&last_login_time);
771 if (strchr(time_string, '\n'))
772 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000773 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000774 printf("Last login: %s\r\n", time_string);
775 else
Damien Millere4340be2000-09-16 13:29:08 +1100776 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000777 }
778 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000779#ifdef HAVE_LOGIN_CAP
780 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
781 "/etc/motd"), "r");
782#else
Damien Miller942da032000-08-18 13:59:06 +1000783 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000784#endif
Damien Miller942da032000-08-18 13:59:06 +1000785 if (f) {
786 while (fgets(buf, sizeof(buf), f))
787 fputs(buf, stdout);
788 fclose(f);
789 }
790 }
791}
792
Damien Millerb38eff82000-04-01 11:09:21 +1000793/*
794 * Sets the value of the given variable in the environment. If the variable
795 * already exists, its value is overriden.
796 */
Damien Miller4af51302000-04-16 11:18:38 +1000797void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000798child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000799 const char *value)
800{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000801 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000802 char **env;
803
804 /*
805 * Find the slot where the value should be stored. If the variable
806 * already exists, we reuse the slot; otherwise we append a new slot
807 * at the end of the array, expanding if necessary.
808 */
809 env = *envp;
810 namelen = strlen(name);
811 for (i = 0; env[i]; i++)
812 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
813 break;
814 if (env[i]) {
815 /* Reuse the slot. */
816 xfree(env[i]);
817 } else {
818 /* New variable. Expand if necessary. */
819 if (i >= (*envsizep) - 1) {
820 (*envsizep) += 50;
821 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
822 }
823 /* Need to set the NULL pointer at end of array beyond the new slot. */
824 env[i + 1] = NULL;
825 }
826
827 /* Allocate space and format the variable in the appropriate slot. */
828 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
829 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
830}
831
832/*
833 * Reads environment variables from the given file and adds/overrides them
834 * into the environment. If the file does not exist, this does nothing.
835 * Otherwise, it must consist of empty lines, comments (line starts with '#')
836 * and assignments of the form name=value. No other forms are allowed.
837 */
Damien Miller4af51302000-04-16 11:18:38 +1000838void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000839read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000840 const char *filename)
841{
842 FILE *f;
843 char buf[4096];
844 char *cp, *value;
845
846 f = fopen(filename, "r");
847 if (!f)
848 return;
849
850 while (fgets(buf, sizeof(buf), f)) {
851 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
852 ;
853 if (!*cp || *cp == '#' || *cp == '\n')
854 continue;
855 if (strchr(cp, '\n'))
856 *strchr(cp, '\n') = '\0';
857 value = strchr(cp, '=');
858 if (value == NULL) {
859 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
860 continue;
861 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000862 /*
863 * Replace the equals sign by nul, and advance value to
864 * the value string.
865 */
Damien Millerb38eff82000-04-01 11:09:21 +1000866 *value = '\0';
867 value++;
868 child_set_env(env, envsize, cp, value);
869 }
870 fclose(f);
871}
872
873#ifdef USE_PAM
874/*
875 * Sets any environment variables which have been specified by PAM
876 */
877void do_pam_environment(char ***env, int *envsize)
878{
879 char *equals, var_name[512], var_val[512];
880 char **pam_env;
881 int i;
882
883 if ((pam_env = fetch_pam_environment()) == NULL)
884 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000885
Damien Millerb38eff82000-04-01 11:09:21 +1000886 for(i = 0; pam_env[i] != NULL; i++) {
887 if ((equals = strstr(pam_env[i], "=")) == NULL)
888 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000889
Damien Millerb38eff82000-04-01 11:09:21 +1000890 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
891 memset(var_name, '\0', sizeof(var_name));
892 memset(var_val, '\0', sizeof(var_val));
893
894 strncpy(var_name, pam_env[i], equals - pam_env[i]);
895 strcpy(var_val, equals + 1);
896
Damien Millercb5e44a2000-09-29 12:12:36 +1100897 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000898
899 child_set_env(env, envsize, var_name, var_val);
900 }
901 }
902}
903#endif /* USE_PAM */
904
Damien Millercb5e44a2000-09-29 12:12:36 +1100905#ifdef HAVE_CYGWIN
906void copy_environment(char ***env, int *envsize)
907{
908 char *equals, var_name[512], var_val[512];
909 int i;
910
911 for(i = 0; environ[i] != NULL; i++) {
912 if ((equals = strstr(environ[i], "=")) == NULL)
913 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000914
Damien Millercb5e44a2000-09-29 12:12:36 +1100915 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
916 memset(var_name, '\0', sizeof(var_name));
917 memset(var_val, '\0', sizeof(var_val));
918
919 strncpy(var_name, environ[i], equals - environ[i]);
920 strcpy(var_val, equals + 1);
921
922 debug3("Copy environment: %s=%s", var_name, var_val);
923
924 child_set_env(env, envsize, var_name, var_val);
925 }
926 }
927}
928#endif
929
Damien Miller5fc85652000-07-09 23:53:07 +1000930#if defined(HAVE_GETUSERATTR)
931/*
932 * AIX-specific login initialisation
933 */
934void set_limit(char *user, char *soft, char *hard, int resource, int mult)
935{
936 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000937 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000938
939 getrlimit(resource, &rlim);
940
Damien Miller65964d62000-07-11 09:16:22 +1000941 slim = 0;
942 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
943 if (slim < 0) {
944 rlim.rlim_cur = RLIM_INFINITY;
945 } else if (slim != 0) {
946 /* See the wackiness below */
947 if (rlim.rlim_cur == slim * mult)
948 slim = 0;
949 else
950 rlim.rlim_cur = slim * mult;
951 }
952 }
Damien Miller5fc85652000-07-09 23:53:07 +1000953
Damien Miller65964d62000-07-11 09:16:22 +1000954 hlim = 0;
955 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
956 if (hlim < 0) {
957 rlim.rlim_max = RLIM_INFINITY;
958 } else if (hlim != 0) {
959 rlim.rlim_max = hlim * mult;
960 }
961 }
Damien Miller5fc85652000-07-09 23:53:07 +1000962
Damien Miller65964d62000-07-11 09:16:22 +1000963 /*
964 * XXX For cpu and fsize the soft limit is set to the hard limit
965 * if the hard limit is left at its default value and the soft limit
966 * is changed from its default value, either by requesting it
967 * (slim == 0) or by setting it to the current default. At least
968 * that's how rlogind does it. If you're confused you're not alone.
969 * Bug or feature? AIX 4.3.1.2
970 */
971 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
972 && hlim == 0 && slim != 0)
973 rlim.rlim_max = rlim.rlim_cur;
974 /* A specified hard limit limits the soft limit */
975 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
976 rlim.rlim_cur = rlim.rlim_max;
977 /* A soft limit can increase a hard limit */
978 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000979 rlim.rlim_max = rlim.rlim_cur;
980
981 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000982 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000983}
984
985void set_limits_from_userattr(char *user)
986{
987 int mask;
988 char buf[16];
989
990 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
991 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
992 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
993 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
994 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
995 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
996#if defined(S_UNOFILE)
997 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
998#endif
999
1000 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
1001 /* Convert decimal to octal */
1002 (void) snprintf(buf, sizeof(buf), "%d", mask);
1003 if (sscanf(buf, "%o", &mask) == 1)
1004 umask(mask);
1005 }
1006}
1007#endif /* defined(HAVE_GETUSERATTR) */
1008
Damien Millerb38eff82000-04-01 11:09:21 +10001009/*
1010 * Performs common processing for the child, such as setting up the
1011 * environment, closing extra file descriptors, setting the user and group
1012 * ids, and executing the command or shell.
1013 */
Damien Miller4af51302000-04-16 11:18:38 +10001014void
Damien Millerb38eff82000-04-01 11:09:21 +10001015do_child(const char *command, struct passwd * pw, const char *term,
1016 const char *display, const char *auth_proto,
1017 const char *auth_data, const char *ttyname)
1018{
Damien Miller7b28dc52000-09-05 13:34:53 +11001019 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001020 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001021 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001022 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001023 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001024 char **env;
1025 extern char **environ;
1026 struct stat st;
1027 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001028#ifdef WITH_IRIX_PROJECT
1029 prid_t projid;
1030#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001031#ifdef WITH_IRIX_JOBS
1032 jid_t jid = 0;
1033#else
1034#ifdef WITH_IRIX_ARRAY
1035 int jid = 0;
1036#endif /* WITH_IRIX_ARRAY */
1037#endif /* WITH_IRIX_JOBS */
1038
Damien Millerd3a18572000-06-07 19:55:44 +10001039 /* login(1) is only called if we execute the login shell */
1040 if (options.use_login && command != NULL)
1041 options.use_login = 0;
1042
Damien Millerb38eff82000-04-01 11:09:21 +10001043#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001044 if (!options.use_login) {
1045# ifdef HAVE_LOGIN_CAP
1046 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1047 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1048 _PATH_NOLOGIN), "r");
1049# else /* HAVE_LOGIN_CAP */
1050 if (pw->pw_uid)
1051 f = fopen(_PATH_NOLOGIN, "r");
1052# endif /* HAVE_LOGIN_CAP */
1053 if (f) {
1054 /* /etc/nologin exists. Print its contents and exit. */
1055 while (fgets(buf, sizeof(buf), f))
1056 fputs(buf, stderr);
1057 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001058 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001059 }
Damien Millerb38eff82000-04-01 11:09:21 +10001060 }
1061#endif /* USE_PAM */
1062
Damien Millerad833b32000-08-23 10:46:23 +10001063 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001064 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1065 switch, so we let login(1) to this for us. */
1066 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001067#ifdef HAVE_OSF_SIA
Damien Miller92ddb7d2001-02-14 01:25:23 +11001068 session_setup_sia(pw->pw_name, ttyname);
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001069#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001070#ifdef HAVE_CYGWIN
1071 if (is_winnt) {
1072#else
Damien Millerb38eff82000-04-01 11:09:21 +10001073 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001074#endif
Damien Millerad833b32000-08-23 10:46:23 +10001075# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001076 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001077# endif /* HAVE_GETUSERATTR */
1078# ifdef HAVE_LOGIN_CAP
1079 if (setusercontext(lc, pw, pw->pw_uid,
1080 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1081 perror("unable to set user context");
1082 exit(1);
1083 }
Damien Miller60396b02001-02-18 17:01:00 +11001084#ifdef BSD_AUTH
1085 if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
1086 error("approval failure for %s", pw->pw_name);
1087 fprintf(stderr, "Approval failure");
1088 exit(1);
1089 }
1090#endif
Damien Millerad833b32000-08-23 10:46:23 +10001091# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001092#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1093 /* Sets login uid for accounting */
1094 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1095 error("setluid: %s", strerror(errno));
1096#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1097
Damien Millerad833b32000-08-23 10:46:23 +10001098 if (setlogin(pw->pw_name) < 0)
1099 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001100 if (setgid(pw->pw_gid) < 0) {
1101 perror("setgid");
1102 exit(1);
1103 }
1104 /* Initialize the group list. */
1105 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1106 perror("initgroups");
1107 exit(1);
1108 }
1109 endgrent();
Ben Lindstrom980754c2000-11-12 00:04:24 +00001110# ifdef WITH_IRIX_JOBS
1111 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1112 if (jid == -1) {
1113 fatal("Failed to create job container: %.100s",
1114 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001115 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001116# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001117# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001118 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001119 if (jid == 0) {
1120 if (newarraysess() != 0)
1121 fatal("Failed to set up new array session: %.100s",
1122 strerror(errno));
1123 }
Damien Millerad833b32000-08-23 10:46:23 +10001124# endif /* WITH_IRIX_ARRAY */
1125# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001126 /* initialize irix project info */
1127 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1128 debug("Failed to get project id, using projid 0");
1129 projid = 0;
1130 }
Damien Miller91606b12000-06-28 08:22:29 +10001131 if (setprid(projid))
1132 fatal("Failed to initialize project %d for %s: %.100s",
1133 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001134# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001135#ifdef WITH_IRIX_AUDIT
1136 if (sysconf(_SC_AUDIT)) {
1137 debug("Setting sat id to %d", (int) pw->pw_uid);
1138 if (satsetid(pw->pw_uid))
1139 debug("error setting satid: %.100s", strerror(errno));
1140 }
1141#endif /* WITH_IRIX_AUDIT */
1142
Damien Miller168a7002001-03-17 10:29:50 +11001143#ifdef _AIX
1144 /*
1145 * AIX has a "usrinfo" area where logname and
1146 * other stuff is stored - a few applications
1147 * actually use this and die if it's not set
1148 */
1149 cp = xmalloc(22 + strlen(ttyname) +
1150 2 * strlen(pw->pw_name));
1151 i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c",
1152 pw->pw_name, 0, pw->pw_name, 0, ttyname, 0,0);
1153 if (usrinfo(SETUINFO, cp, i) == -1)
1154 fatal("Couldn't set usrinfo: %s",
1155 strerror(errno));
1156 debug3("AIX/UsrInfo: set len %d", i);
1157 xfree(cp);
1158#endif
1159
Damien Millerb38eff82000-04-01 11:09:21 +10001160 /* Permanently switch to the desired uid. */
1161 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001162# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001163 }
Damien Millerad833b32000-08-23 10:46:23 +10001164#endif /* HAVE_OSF_SIA */
1165
Damien Millerbac2d8a2000-09-05 16:13:06 +11001166#ifdef HAVE_CYGWIN
1167 if (is_winnt)
1168#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001169 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001170 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001171 }
1172 /*
1173 * Get the shell from the password data. An empty shell field is
1174 * legal, and means /bin/sh.
1175 */
1176 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001177#ifdef HAVE_LOGIN_CAP
1178 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1179#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001180
1181#ifdef AFS
1182 /* Try to get AFS tokens for the local cell. */
1183 if (k_hasafs()) {
1184 char cell[64];
1185
1186 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1187 krb_afslog(cell, 0);
1188
1189 krb_afslog(0, 0);
1190 }
1191#endif /* AFS */
1192
1193 /* Initialize the environment. */
1194 envsize = 100;
1195 env = xmalloc(envsize * sizeof(char *));
1196 env[0] = NULL;
1197
Damien Millerbac2d8a2000-09-05 16:13:06 +11001198#ifdef HAVE_CYGWIN
1199 /*
1200 * The Windows environment contains some setting which are
1201 * important for a running system. They must not be dropped.
1202 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001203 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001204#endif
1205
Damien Millerb38eff82000-04-01 11:09:21 +10001206 if (!options.use_login) {
1207 /* Set basic environment. */
1208 child_set_env(&env, &envsize, "USER", pw->pw_name);
1209 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1210 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001211#ifdef HAVE_LOGIN_CAP
1212 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1213 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001214#else /* HAVE_LOGIN_CAP */
1215# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001216 /*
1217 * There's no standard path on Windows. The path contains
1218 * important components pointing to the system directories,
1219 * needed for loading shared libraries. So the path better
1220 * remains intact here.
1221 */
Damien Millerb38eff82000-04-01 11:09:21 +10001222 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001223# endif /* HAVE_CYGWIN */
1224#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001225
1226 snprintf(buf, sizeof buf, "%.200s/%.50s",
1227 _PATH_MAILDIR, pw->pw_name);
1228 child_set_env(&env, &envsize, "MAIL", buf);
1229
1230 /* Normal systems set SHELL by default. */
1231 child_set_env(&env, &envsize, "SHELL", shell);
1232 }
1233 if (getenv("TZ"))
1234 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1235
1236 /* Set custom environment options from RSA authentication. */
1237 while (custom_environment) {
1238 struct envstring *ce = custom_environment;
1239 char *s = ce->s;
1240 int i;
1241 for (i = 0; s[i] != '=' && s[i]; i++);
1242 if (s[i] == '=') {
1243 s[i] = 0;
1244 child_set_env(&env, &envsize, s, s + i + 1);
1245 }
1246 custom_environment = ce->next;
1247 xfree(ce->s);
1248 xfree(ce);
1249 }
1250
1251 snprintf(buf, sizeof buf, "%.50s %d %d",
1252 get_remote_ipaddr(), get_remote_port(), get_local_port());
1253 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1254
1255 if (ttyname)
1256 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1257 if (term)
1258 child_set_env(&env, &envsize, "TERM", term);
1259 if (display)
1260 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001261 if (original_command)
1262 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1263 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001264
1265#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001266 if ((cp = getenv("AUTHSTATE")) != NULL)
1267 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1268 if ((cp = getenv("KRB5CCNAME")) != NULL)
1269 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1270 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001271#endif
1272
1273#ifdef KRB4
1274 {
1275 extern char *ticket;
1276
1277 if (ticket)
1278 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1279 }
1280#endif /* KRB4 */
1281
1282#ifdef USE_PAM
1283 /* Pull in any environment variables that may have been set by PAM. */
1284 do_pam_environment(&env, &envsize);
1285#endif /* USE_PAM */
1286
Damien Millerb38eff82000-04-01 11:09:21 +10001287 if (xauthfile)
1288 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1289 if (auth_get_socket_name() != NULL)
1290 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1291 auth_get_socket_name());
1292
1293 /* read $HOME/.ssh/environment. */
1294 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001295 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1296 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001297 read_environment_file(&env, &envsize, buf);
1298 }
1299 if (debug_flag) {
1300 /* dump the environment */
1301 fprintf(stderr, "Environment:\n");
1302 for (i = 0; env[i]; i++)
1303 fprintf(stderr, " %.200s\n", env[i]);
1304 }
Damien Millerad833b32000-08-23 10:46:23 +10001305 /* we have to stash the hostname before we close our socket. */
1306 if (options.use_login)
1307 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001308 /*
1309 * Close the connection descriptors; note that this is the child, and
1310 * the server will still have the socket open, and it is important
1311 * that we do not shutdown it. Note that the descriptors cannot be
1312 * closed before building the environment, as we call
1313 * get_remote_ipaddr there.
1314 */
1315 if (packet_get_connection_in() == packet_get_connection_out())
1316 close(packet_get_connection_in());
1317 else {
1318 close(packet_get_connection_in());
1319 close(packet_get_connection_out());
1320 }
1321 /*
1322 * Close all descriptors related to channels. They will still remain
1323 * open in the parent.
1324 */
1325 /* XXX better use close-on-exec? -markus */
1326 channel_close_all();
1327
1328 /*
1329 * Close any extra file descriptors. Note that there may still be
1330 * descriptors left by system functions. They will be closed later.
1331 */
1332 endpwent();
1333
1334 /*
1335 * Close any extra open file descriptors so that we don\'t have them
1336 * hanging around in clients. Note that we want to do this after
1337 * initgroups, because at least on Solaris 2.3 it leaves file
1338 * descriptors open.
1339 */
1340 for (i = 3; i < 64; i++)
1341 close(i);
1342
1343 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001344 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001345 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1346 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001347#ifdef HAVE_LOGIN_CAP
1348 if (login_getcapbool(lc, "requirehome", 0))
1349 exit(1);
1350#endif
1351 }
Damien Millerb38eff82000-04-01 11:09:21 +10001352
1353 /*
1354 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1355 * xauth are run in the proper environment.
1356 */
1357 environ = env;
1358
1359 /*
1360 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1361 * in this order).
1362 */
1363 if (!options.use_login) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001364 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001365 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001366 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001367
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001368 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001369 if (f) {
1370 if (auth_proto != NULL && auth_data != NULL)
1371 fprintf(f, "%s %s\n", auth_proto, auth_data);
1372 pclose(f);
1373 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001374 fprintf(stderr, "Could not run %s\n", _PATH_SSH_USER_RC);
1375 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001376 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001377 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001378
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001379 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001380 if (f) {
1381 if (auth_proto != NULL && auth_data != NULL)
1382 fprintf(f, "%s %s\n", auth_proto, auth_data);
1383 pclose(f);
1384 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001385 fprintf(stderr, "Could not run %s\n", _PATH_SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001386 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001387 /* Add authority data to .Xauthority if appropriate. */
1388 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001389 char *screen = strchr(display, ':');
1390 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001391 fprintf(stderr,
1392 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001393 options.xauth_location, display,
1394 auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001395#ifndef NO_X11_UNIX_SOCKETS
Damien Millerd999ae22000-05-20 12:49:31 +10001396 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001397 fprintf(stderr,
1398 "Adding %.*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 Millerd999ae22000-05-20 12:49:31 +10001402 }
Damien Miller0c043c12000-06-07 21:22:38 +10001403 snprintf(cmd, sizeof cmd, "%s -q -",
1404 options.xauth_location);
1405 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001406 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001407 fprintf(f, "add %s %s %s\n", display,
1408 auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001409#ifndef NO_X11_UNIX_SOCKETS
Kevin Stevesef4eea92001-02-05 12:42:17 +00001410 if (screen != NULL)
Damien Millerd999ae22000-05-20 12:49:31 +10001411 fprintf(f, "add %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001412 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001413 screen, auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001414#endif /* NO_X11_UNIX_SOCKETS */
Damien Millerb38eff82000-04-01 11:09:21 +10001415 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001416 } else {
1417 fprintf(stderr, "Could not run %s\n",
1418 cmd);
1419 }
Damien Millerb38eff82000-04-01 11:09:21 +10001420 }
1421 }
Damien Millerb38eff82000-04-01 11:09:21 +10001422 /* Get the last component of the shell name. */
1423 cp = strrchr(shell, '/');
1424 if (cp)
1425 cp++;
1426 else
1427 cp = shell;
1428 }
1429 /*
1430 * If we have no command, execute the shell. In this case, the shell
1431 * name to be passed in argv[0] is preceded by '-' to indicate that
1432 * this is a login shell.
1433 */
1434 if (!command) {
1435 if (!options.use_login) {
1436 char buf[256];
1437
1438 /*
1439 * Check for mail if we have a tty and it was enabled
1440 * in server options.
1441 */
1442 if (ttyname && options.check_mail) {
1443 char *mailbox;
1444 struct stat mailstat;
1445 mailbox = getenv("MAIL");
1446 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001447 if (stat(mailbox, &mailstat) != 0 ||
1448 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001449 printf("No mail.\n");
1450 else if (mailstat.st_mtime < mailstat.st_atime)
1451 printf("You have mail.\n");
1452 else
1453 printf("You have new mail.\n");
1454 }
1455 }
1456 /* Start the shell. Set initial character to '-'. */
1457 buf[0] = '-';
1458 strncpy(buf + 1, cp, sizeof(buf) - 1);
1459 buf[sizeof(buf) - 1] = 0;
1460
1461 /* Execute the shell. */
1462 argv[0] = buf;
1463 argv[1] = NULL;
1464 execve(shell, argv, env);
1465
1466 /* Executing the shell failed. */
1467 perror(shell);
1468 exit(1);
1469
1470 } else {
1471 /* Launch login(1). */
1472
Damien Millerad833b32000-08-23 10:46:23 +10001473 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001474 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001475
1476 /* Login couldn't be executed, die. */
1477
1478 perror("login");
1479 exit(1);
1480 }
1481 }
1482 /*
1483 * Execute the command using the user's shell. This uses the -c
1484 * option to execute the command.
1485 */
1486 argv[0] = (char *) cp;
1487 argv[1] = "-c";
1488 argv[2] = (char *) command;
1489 argv[3] = NULL;
1490 execve(shell, argv, env);
1491 perror(shell);
1492 exit(1);
1493}
1494
1495Session *
1496session_new(void)
1497{
1498 int i;
1499 static int did_init = 0;
1500 if (!did_init) {
1501 debug("session_new: init");
1502 for(i = 0; i < MAX_SESSIONS; i++) {
1503 sessions[i].used = 0;
1504 sessions[i].self = i;
1505 }
1506 did_init = 1;
1507 }
1508 for(i = 0; i < MAX_SESSIONS; i++) {
1509 Session *s = &sessions[i];
1510 if (! s->used) {
1511 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001512 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001513 s->chanid = -1;
1514 s->ptyfd = -1;
1515 s->ttyfd = -1;
1516 s->term = NULL;
1517 s->pw = NULL;
1518 s->display = NULL;
1519 s->screen = 0;
1520 s->auth_data = NULL;
1521 s->auth_proto = NULL;
1522 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001523 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001524 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001525 return s;
1526 }
1527 }
1528 return NULL;
1529}
1530
1531void
1532session_dump(void)
1533{
1534 int i;
1535 for(i = 0; i < MAX_SESSIONS; i++) {
1536 Session *s = &sessions[i];
1537 debug("dump: used %d session %d %p channel %d pid %d",
1538 s->used,
1539 s->self,
1540 s,
1541 s->chanid,
1542 s->pid);
1543 }
1544}
1545
Damien Millerefb4afe2000-04-12 18:45:05 +10001546int
1547session_open(int chanid)
1548{
1549 Session *s = session_new();
1550 debug("session_open: channel %d", chanid);
1551 if (s == NULL) {
1552 error("no more sessions");
1553 return 0;
1554 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001555 s->pw = auth_get_user();
1556 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001557 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001558 debug("session_open: session %d: link with channel %d", s->self, chanid);
1559 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001560 return 1;
1561}
1562
1563Session *
1564session_by_channel(int id)
1565{
1566 int i;
1567 for(i = 0; i < MAX_SESSIONS; i++) {
1568 Session *s = &sessions[i];
1569 if (s->used && s->chanid == id) {
1570 debug("session_by_channel: session %d channel %d", i, id);
1571 return s;
1572 }
1573 }
1574 debug("session_by_channel: unknown channel %d", id);
1575 session_dump();
1576 return NULL;
1577}
1578
1579Session *
1580session_by_pid(pid_t pid)
1581{
1582 int i;
1583 debug("session_by_pid: pid %d", pid);
1584 for(i = 0; i < MAX_SESSIONS; i++) {
1585 Session *s = &sessions[i];
1586 if (s->used && s->pid == pid)
1587 return s;
1588 }
1589 error("session_by_pid: unknown pid %d", pid);
1590 session_dump();
1591 return NULL;
1592}
1593
1594int
1595session_window_change_req(Session *s)
1596{
1597 s->col = packet_get_int();
1598 s->row = packet_get_int();
1599 s->xpixel = packet_get_int();
1600 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001601 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001602 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1603 return 1;
1604}
1605
1606int
1607session_pty_req(Session *s)
1608{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001609 u_int len;
Damien Miller4af51302000-04-16 11:18:38 +10001610 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001611
Damien Millerf6d9e222000-06-18 14:50:44 +10001612 if (no_pty_flag)
1613 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001614 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001615 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001616 s->term = packet_get_string(&len);
1617 s->col = packet_get_int();
1618 s->row = packet_get_int();
1619 s->xpixel = packet_get_int();
1620 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001621 term_modes = packet_get_string(&len);
1622 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001623
1624 if (strcmp(s->term, "") == 0) {
1625 xfree(s->term);
1626 s->term = NULL;
1627 }
1628 /* Allocate a pty and open it. */
1629 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1630 xfree(s->term);
1631 s->term = NULL;
1632 s->ptyfd = -1;
1633 s->ttyfd = -1;
1634 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001635 xfree(term_modes);
1636 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001637 }
1638 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1639 /*
1640 * Add a cleanup function to clear the utmp entry and record logout
1641 * time in case we call fatal() (e.g., the connection gets closed).
1642 */
1643 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1644 pty_setowner(s->pw, s->tty);
1645 /* Get window size from the packet. */
1646 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1647
Damien Millere247cc42000-05-07 12:03:14 +10001648 session_proctitle(s);
1649
Damien Miller5f056372000-04-16 12:31:48 +10001650 /* XXX parse and set terminal modes */
1651 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001652 return 1;
1653}
1654
Damien Millerbd483e72000-04-30 10:00:53 +10001655int
1656session_subsystem_req(Session *s)
1657{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001658 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001659 int success = 0;
1660 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001661 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001662
1663 packet_done();
1664 log("subsystem request for %s", subsys);
1665
Damien Millerf6d9e222000-06-18 14:50:44 +10001666 for (i = 0; i < options.num_subsystems; i++) {
1667 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1668 debug("subsystem: exec() %s", options.subsystem_command[i]);
1669 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1670 success = 1;
1671 }
1672 }
1673
1674 if (!success)
1675 log("subsystem request for %s failed, subsystem not found", subsys);
1676
Damien Millerbd483e72000-04-30 10:00:53 +10001677 xfree(subsys);
1678 return success;
1679}
1680
1681int
1682session_x11_req(Session *s)
1683{
Damien Miller7b28dc52000-09-05 13:34:53 +11001684 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001685 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001686 debug("X11 forwarding disabled in user configuration file.");
1687 return 0;
1688 }
Damien Millerbd483e72000-04-30 10:00:53 +10001689 if (!options.x11_forwarding) {
1690 debug("X11 forwarding disabled in server configuration file.");
1691 return 0;
1692 }
1693 if (xauthfile != NULL) {
1694 debug("X11 fwd already started.");
1695 return 0;
1696 }
1697
1698 debug("Received request for X11 forwarding with auth spoofing.");
1699 if (s->display != NULL)
1700 packet_disconnect("Protocol error: X11 display already set.");
1701
1702 s->single_connection = packet_get_char();
1703 s->auth_proto = packet_get_string(NULL);
1704 s->auth_data = packet_get_string(NULL);
1705 s->screen = packet_get_int();
1706 packet_done();
1707
1708 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1709 if (s->display == NULL) {
1710 xfree(s->auth_proto);
1711 xfree(s->auth_data);
1712 return 0;
1713 }
1714 xauthfile = xmalloc(MAXPATHLEN);
1715 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1716 temporarily_use_uid(s->pw->pw_uid);
1717 if (mkdtemp(xauthfile) == NULL) {
1718 restore_uid();
1719 error("private X11 dir: mkdtemp %s failed: %s",
1720 xauthfile, strerror(errno));
1721 xfree(xauthfile);
1722 xauthfile = NULL;
1723 xfree(s->auth_proto);
1724 xfree(s->auth_data);
1725 /* XXXX remove listening channels */
1726 return 0;
1727 }
1728 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001729 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1730 if (fd >= 0)
1731 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001732 restore_uid();
1733 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1734 return 1;
1735}
1736
Damien Millerf6d9e222000-06-18 14:50:44 +10001737int
1738session_shell_req(Session *s)
1739{
1740 /* if forced_command == NULL, the shell is execed */
1741 char *shell = forced_command;
1742 packet_done();
1743 s->extended = 1;
1744 if (s->ttyfd == -1)
1745 do_exec_no_pty(s, shell, s->pw);
1746 else
1747 do_exec_pty(s, shell, s->pw);
1748 return 1;
1749}
1750
1751int
1752session_exec_req(Session *s)
1753{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001754 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001755 char *command = packet_get_string(&len);
1756 packet_done();
1757 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001758 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001759 command = forced_command;
1760 debug("Forced command '%.500s'", forced_command);
1761 }
1762 s->extended = 1;
1763 if (s->ttyfd == -1)
1764 do_exec_no_pty(s, command, s->pw);
1765 else
1766 do_exec_pty(s, command, s->pw);
1767 if (forced_command == NULL)
1768 xfree(command);
1769 return 1;
1770}
1771
Damien Miller0bc1bd82000-11-13 22:57:25 +11001772int
1773session_auth_agent_req(Session *s)
1774{
1775 static int called = 0;
1776 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001777 if (no_agent_forwarding_flag) {
1778 debug("session_auth_agent_req: no_agent_forwarding_flag");
1779 return 0;
1780 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001781 if (called) {
1782 return 0;
1783 } else {
1784 called = 1;
1785 return auth_input_request_forwarding(s->pw);
1786 }
1787}
1788
Damien Millerefb4afe2000-04-12 18:45:05 +10001789void
1790session_input_channel_req(int id, void *arg)
1791{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001792 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001793 int reply;
1794 int success = 0;
1795 char *rtype;
1796 Session *s;
1797 Channel *c;
1798
1799 rtype = packet_get_string(&len);
1800 reply = packet_get_char();
1801
1802 s = session_by_channel(id);
1803 if (s == NULL)
1804 fatal("session_input_channel_req: channel %d: no session", id);
1805 c = channel_lookup(id);
1806 if (c == NULL)
1807 fatal("session_input_channel_req: channel %d: bad channel", id);
1808
1809 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1810 s->self, id, rtype, reply);
1811
1812 /*
1813 * a session is in LARVAL state until a shell
1814 * or programm is executed
1815 */
1816 if (c->type == SSH_CHANNEL_LARVAL) {
1817 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001818 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001819 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001820 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001821 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001822 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001823 } else if (strcmp(rtype, "x11-req") == 0) {
1824 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001825 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1826 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001827 } else if (strcmp(rtype, "subsystem") == 0) {
1828 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001829 }
1830 }
1831 if (strcmp(rtype, "window-change") == 0) {
1832 success = session_window_change_req(s);
1833 }
1834
1835 if (reply) {
1836 packet_start(success ?
1837 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1838 packet_put_int(c->remote_id);
1839 packet_send();
1840 }
1841 xfree(rtype);
1842}
1843
1844void
1845session_set_fds(Session *s, int fdin, int fdout, int fderr)
1846{
1847 if (!compat20)
1848 fatal("session_set_fds: called for proto != 2.0");
1849 /*
1850 * now that have a child and a pipe to the child,
1851 * we can activate our channel and register the fd's
1852 */
1853 if (s->chanid == -1)
1854 fatal("no channel for session %d", s->self);
1855 channel_set_fds(s->chanid,
1856 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001857 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1858 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001859}
1860
Damien Millerb38eff82000-04-01 11:09:21 +10001861void
1862session_pty_cleanup(Session *s)
1863{
1864 if (s == NULL || s->ttyfd == -1)
1865 return;
1866
Kevin Steves43cdef32001-02-11 14:12:08 +00001867 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001868
1869 /* Cancel the cleanup function. */
1870 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1871
1872 /* Record that the user has logged out. */
1873 record_logout(s->pid, s->tty);
1874
1875 /* Release the pseudo-tty. */
1876 pty_release(s->tty);
1877
1878 /*
1879 * Close the server side of the socket pairs. We must do this after
1880 * the pty cleanup, so that another process doesn't get this pty
1881 * while we're still cleaning up.
1882 */
1883 if (close(s->ptymaster) < 0)
1884 error("close(s->ptymaster): %s", strerror(errno));
1885}
Damien Millerefb4afe2000-04-12 18:45:05 +10001886
1887void
1888session_exit_message(Session *s, int status)
1889{
1890 Channel *c;
1891 if (s == NULL)
1892 fatal("session_close: no session");
1893 c = channel_lookup(s->chanid);
1894 if (c == NULL)
1895 fatal("session_close: session %d: no channel %d",
1896 s->self, s->chanid);
1897 debug("session_exit_message: session %d channel %d pid %d",
1898 s->self, s->chanid, s->pid);
1899
1900 if (WIFEXITED(status)) {
1901 channel_request_start(s->chanid,
1902 "exit-status", 0);
1903 packet_put_int(WEXITSTATUS(status));
1904 packet_send();
1905 } else if (WIFSIGNALED(status)) {
1906 channel_request_start(s->chanid,
1907 "exit-signal", 0);
1908 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001909#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001910 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001911#else /* WCOREDUMP */
1912 packet_put_char(0);
1913#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001914 packet_put_cstring("");
1915 packet_put_cstring("");
1916 packet_send();
1917 } else {
1918 /* Some weird exit cause. Just exit. */
1919 packet_disconnect("wait returned status %04x.", status);
1920 }
1921
1922 /* disconnect channel */
1923 debug("session_exit_message: release channel %d", s->chanid);
1924 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001925 /*
1926 * emulate a write failure with 'chan_write_failed', nobody will be
1927 * interested in data we write.
1928 * Note that we must not call 'chan_read_failed', since there could
1929 * be some more data waiting in the pipe.
1930 */
Damien Millerbd483e72000-04-30 10:00:53 +10001931 if (c->ostate != CHAN_OUTPUT_CLOSED)
1932 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001933 s->chanid = -1;
1934}
1935
1936void
1937session_free(Session *s)
1938{
1939 debug("session_free: session %d pid %d", s->self, s->pid);
1940 if (s->term)
1941 xfree(s->term);
1942 if (s->display)
1943 xfree(s->display);
1944 if (s->auth_data)
1945 xfree(s->auth_data);
1946 if (s->auth_proto)
1947 xfree(s->auth_proto);
1948 s->used = 0;
1949}
1950
1951void
1952session_close(Session *s)
1953{
1954 session_pty_cleanup(s);
1955 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001956 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001957}
1958
1959void
1960session_close_by_pid(pid_t pid, int status)
1961{
1962 Session *s = session_by_pid(pid);
1963 if (s == NULL) {
1964 debug("session_close_by_pid: no session for pid %d", s->pid);
1965 return;
1966 }
1967 if (s->chanid != -1)
1968 session_exit_message(s, status);
1969 session_close(s);
1970}
1971
1972/*
1973 * this is called when a channel dies before
1974 * the session 'child' itself dies
1975 */
1976void
1977session_close_by_channel(int id, void *arg)
1978{
1979 Session *s = session_by_channel(id);
1980 if (s == NULL) {
1981 debug("session_close_by_channel: no session for channel %d", id);
1982 return;
1983 }
1984 /* disconnect channel */
1985 channel_cancel_cleanup(s->chanid);
1986 s->chanid = -1;
1987
1988 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1989 if (s->pid == 0) {
1990 /* close session immediately */
1991 session_close(s);
1992 } else {
1993 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001994 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001995 error("session_close_by_channel: kill %d: %s",
1996 s->pid, strerror(errno));
1997 }
1998}
1999
Damien Millere247cc42000-05-07 12:03:14 +10002000char *
2001session_tty_list(void)
2002{
2003 static char buf[1024];
2004 int i;
2005 buf[0] = '\0';
2006 for(i = 0; i < MAX_SESSIONS; i++) {
2007 Session *s = &sessions[i];
2008 if (s->used && s->ttyfd != -1) {
2009 if (buf[0] != '\0')
2010 strlcat(buf, ",", sizeof buf);
2011 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2012 }
2013 }
2014 if (buf[0] == '\0')
2015 strlcpy(buf, "notty", sizeof buf);
2016 return buf;
2017}
2018
2019void
2020session_proctitle(Session *s)
2021{
2022 if (s->pw == NULL)
2023 error("no user for session %d", s->self);
2024 else
2025 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2026}
2027
Damien Millerefb4afe2000-04-12 18:45:05 +10002028void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002029do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002030{
2031 /*
2032 * Cancel the alarm we set to limit the time taken for
2033 * authentication.
2034 */
2035 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10002036 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10002037 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10002038 startup_pipe = -1;
2039 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00002040#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002041 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10002042 error("unable to get login class");
2043 return;
2044 }
2045#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10002046 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002047 if (xauthfile)
2048 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002049}