blob: 3434aafc4f9ff7e9042f26c888eb8cb7493798ea [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
10 *
Damien Millerefb4afe2000-04-12 18:45:05 +100011 * SSH2 support by Markus Friedl.
12 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110013 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Millerefb4afe2000-04-12 18:45:05 +100033 */
Damien Millerb38eff82000-04-01 11:09:21 +100034
35#include "includes.h"
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +000036RCSID("$OpenBSD: session.c,v 1.56 2001/02/16 14:03:43 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
Damien Millerb38eff82000-04-01 11:09:21 +100038#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000042#include "sshpty.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "packet.h"
44#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100045#include "mpaux.h"
Damien Millerb38eff82000-04-01 11:09:21 +100046#include "uidswap.h"
47#include "compat.h"
48#include "channels.h"
49#include "nchan.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100051#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100052#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "pathnames.h"
54#include "log.h"
55#include "servconf.h"
Ben Lindstromd95c09c2001-02-18 19:13:33 +000056#include "sshlogin.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "serverloop.h"
58#include "canohost.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000059#include "session.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100060
Damien Miller91606b12000-06-28 08:22:29 +100061#ifdef WITH_IRIX_PROJECT
62#include <proj.h>
63#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000064#ifdef WITH_IRIX_JOBS
65#include <sys/resource.h>
Kevin Stevesef4eea92001-02-05 12:42:17 +000066#endif
Ben Lindstrom49a79c02000-11-17 03:47:20 +000067#ifdef WITH_IRIX_AUDIT
68#include <sat.h>
69#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100070
Damien Miller37023962000-07-11 17:31:38 +100071#if defined(HAVE_USERSEC_H)
72#include <usersec.h>
73#endif
74
Damien Millerbac2d8a2000-09-05 16:13:06 +110075#ifdef HAVE_CYGWIN
76#include <windows.h>
77#include <sys/cygwin.h>
78#define is_winnt (GetVersion() < 0x80000000)
79#endif
80
Damien Millerd17b8d52000-08-09 14:42:28 +100081/* AIX limits */
82#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100083# define S_UFSIZE_HARD S_UFSIZE "_hard"
84# define S_UCPU_HARD S_UCPU "_hard"
85# define S_UDATA_HARD S_UDATA "_hard"
86# define S_USTACK_HARD S_USTACK "_hard"
87# define S_URSS_HARD S_URSS "_hard"
88# define S_UCORE_HARD S_UCORE "_hard"
89# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100090#endif
91
Damien Millerb38eff82000-04-01 11:09:21 +100092/* types */
93
94#define TTYSZ 64
95typedef struct Session Session;
96struct Session {
97 int used;
98 int self;
Damien Millerbd483e72000-04-30 10:00:53 +100099 int extended;
Damien Millerb38eff82000-04-01 11:09:21 +1000100 struct passwd *pw;
101 pid_t pid;
102 /* tty */
103 char *term;
104 int ptyfd, ttyfd, ptymaster;
105 int row, col, xpixel, ypixel;
106 char tty[TTYSZ];
107 /* X11 */
108 char *display;
109 int screen;
110 char *auth_proto;
111 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000112 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000113 /* proto 2 */
114 int chanid;
115};
116
117/* func */
118
119Session *session_new(void);
120void session_set_fds(Session *s, int fdin, int fdout, int fderr);
121void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000122void session_proctitle(Session *s);
Damien Millerb38eff82000-04-01 11:09:21 +1000123void do_exec_pty(Session *s, const char *command, struct passwd * pw);
124void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
Damien Miller69b69aa2000-10-28 14:19:58 +1100125void do_login(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000126
127void
128do_child(const char *command, struct passwd * pw, const char *term,
129 const char *display, const char *auth_proto,
130 const char *auth_data, const char *ttyname);
131
132/* import */
133extern ServerOptions options;
134extern char *__progname;
135extern int log_stderr;
136extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000137extern u_int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000138
Damien Miller37023962000-07-11 17:31:38 +1000139extern int startup_pipe;
140
Damien Millerb38eff82000-04-01 11:09:21 +1000141/* Local Xauthority file. */
142static char *xauthfile;
143
Damien Miller7b28dc52000-09-05 13:34:53 +1100144/* original command from peer. */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000145char *original_command = NULL;
Damien Miller7b28dc52000-09-05 13:34:53 +1100146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* data */
148#define MAX_SESSIONS 10
149Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100150
Damien Millerd2c208a2000-05-17 22:00:02 +1000151#ifdef WITH_AIXAUTHENTICATE
152/* AIX's lastlogin message, set in auth1.c */
153char *aixloginmsg;
154#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000155
Damien Millerad833b32000-08-23 10:46:23 +1000156#ifdef HAVE_LOGIN_CAP
157static login_cap_t *lc;
158#endif
159
Damien Millerb38eff82000-04-01 11:09:21 +1000160/*
161 * Remove local Xauthority file.
162 */
163void
164xauthfile_cleanup_proc(void *ignore)
165{
166 debug("xauthfile_cleanup_proc called");
167
168 if (xauthfile != NULL) {
169 char *p;
170 unlink(xauthfile);
171 p = strrchr(xauthfile, '/');
172 if (p != NULL) {
173 *p = '\0';
174 rmdir(xauthfile);
175 }
176 xfree(xauthfile);
177 xauthfile = NULL;
178 }
179}
180
181/*
182 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
183 * dropped connection).
184 */
Damien Miller4af51302000-04-16 11:18:38 +1000185void
Damien Millerb38eff82000-04-01 11:09:21 +1000186pty_cleanup_proc(void *session)
187{
188 Session *s=session;
189 if (s == NULL)
190 fatal("pty_cleanup_proc: no session");
191 debug("pty_cleanup_proc: %s", s->tty);
192
193 if (s->pid != 0) {
194 /* Record that the user has logged out. */
195 record_logout(s->pid, s->tty);
196 }
197
198 /* Release the pseudo-tty. */
199 pty_release(s->tty);
200}
201
202/*
203 * Prepares for an interactive session. This is called after the user has
204 * been successfully authenticated. During this message exchange, pseudo
205 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
206 * are requested, etc.
207 */
Damien Miller4af51302000-04-16 11:18:38 +1000208void
Damien Millerb38eff82000-04-01 11:09:21 +1000209do_authenticated(struct passwd * pw)
210{
211 Session *s;
Damien Miller7b28dc52000-09-05 13:34:53 +1100212 int type, fd;
Damien Millerb38eff82000-04-01 11:09:21 +1000213 int compression_level = 0, enable_compression_after_reply = 0;
214 int have_pty = 0;
215 char *command;
216 int n_bytes;
217 int plen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000218 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000219
220 /*
221 * Cancel the alarm we set to limit the time taken for
222 * authentication.
223 */
224 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +1000225 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +1000226 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +1000227 startup_pipe = -1;
228 }
Damien Millerb38eff82000-04-01 11:09:21 +1000229
230 /*
231 * Inform the channel mechanism that we are the server side and that
232 * the client may request to connect to any port at all. (The user
233 * could do it anyway, and we wouldn\'t know what is permitted except
234 * by the client telling us, so we can equally well trust the client
235 * not to request anything bogus.)
236 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100237 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
Damien Millerb38eff82000-04-01 11:09:21 +1000238 channel_permit_all_opens();
239
240 s = session_new();
Damien Millerbd483e72000-04-30 10:00:53 +1000241 s->pw = pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000242
Kevin Steves48b7cc02000-10-07 13:24:00 +0000243#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +1000244 if ((lc = login_getclass(pw->pw_class)) == NULL) {
245 error("unable to get login class");
246 return;
247 }
248#endif
249
Damien Millerb38eff82000-04-01 11:09:21 +1000250 /*
251 * We stay in this loop until the client requests to execute a shell
252 * or a command.
253 */
254 for (;;) {
255 int success = 0;
256
257 /* Get a packet from the client. */
258 type = packet_read(&plen);
259
260 /* Process the packet. */
261 switch (type) {
262 case SSH_CMSG_REQUEST_COMPRESSION:
263 packet_integrity_check(plen, 4, type);
264 compression_level = packet_get_int();
265 if (compression_level < 1 || compression_level > 9) {
266 packet_send_debug("Received illegal compression level %d.",
267 compression_level);
268 break;
269 }
270 /* Enable compression after we have responded with SUCCESS. */
271 enable_compression_after_reply = 1;
272 success = 1;
273 break;
274
275 case SSH_CMSG_REQUEST_PTY:
276 if (no_pty_flag) {
277 debug("Allocating a pty not permitted for this authentication.");
278 break;
279 }
280 if (have_pty)
281 packet_disconnect("Protocol error: you already have a pty.");
282
283 debug("Allocating pty.");
284
285 /* Allocate a pty and open it. */
286 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
287 sizeof(s->tty))) {
288 error("Failed to allocate pty.");
289 break;
290 }
291 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
292 pty_setowner(pw, s->tty);
293
294 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
295 s->term = packet_get_string(&dlen);
296 packet_integrity_check(dlen, strlen(s->term), type);
297 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
298 /* Remaining bytes */
299 n_bytes = plen - (4 + dlen + 4 * 4);
300
301 if (strcmp(s->term, "") == 0) {
302 xfree(s->term);
303 s->term = NULL;
304 }
305 /* Get window size from the packet. */
306 s->row = packet_get_int();
307 s->col = packet_get_int();
308 s->xpixel = packet_get_int();
309 s->ypixel = packet_get_int();
310 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
311
312 /* Get tty modes from the packet. */
313 tty_parse_modes(s->ttyfd, &n_bytes);
314 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
315
Damien Millere247cc42000-05-07 12:03:14 +1000316 session_proctitle(s);
317
Damien Millerb38eff82000-04-01 11:09:21 +1000318 /* Indicate that we now have a pty. */
319 success = 1;
320 have_pty = 1;
321 break;
322
323 case SSH_CMSG_X11_REQUEST_FORWARDING:
324 if (!options.x11_forwarding) {
325 packet_send_debug("X11 forwarding disabled in server configuration file.");
326 break;
327 }
Damien Miller0c043c12000-06-07 21:22:38 +1000328 if (!options.xauth_location) {
329 packet_send_debug("No xauth program; cannot forward with spoofing.");
330 break;
331 }
Damien Millerb38eff82000-04-01 11:09:21 +1000332 if (no_x11_forwarding_flag) {
333 packet_send_debug("X11 forwarding not permitted for this authentication.");
334 break;
335 }
336 debug("Received request for X11 forwarding with auth spoofing.");
337 if (s->display != NULL)
338 packet_disconnect("Protocol error: X11 display already set.");
339
340 s->auth_proto = packet_get_string(&proto_len);
341 s->auth_data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +1000342
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000343 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER) {
344 debug2("SSH_PROTOFLAG_SCREEN_NUMBER == true");
345 packet_integrity_check(plen,
346 4 + proto_len + 4 + data_len + 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000347 s->screen = packet_get_int();
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000348 } else {
349 debug2("SSH_PROTOFLAG_SCREEN_NUMBER == false");
350 packet_integrity_check(plen,
351 4 + proto_len + 4 + data_len, type);
Damien Millerb38eff82000-04-01 11:09:21 +1000352 s->screen = 0;
Ben Lindstrom8dcdeb82001-02-16 16:02:14 +0000353 }
Damien Millerb38eff82000-04-01 11:09:21 +1000354 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
355
356 if (s->display == NULL)
357 break;
358
359 /* Setup to always have a local .Xauthority. */
360 xauthfile = xmalloc(MAXPATHLEN);
361 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
362 temporarily_use_uid(pw->pw_uid);
363 if (mkdtemp(xauthfile) == NULL) {
364 restore_uid();
365 error("private X11 dir: mkdtemp %s failed: %s",
366 xauthfile, strerror(errno));
367 xfree(xauthfile);
368 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000369 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000370 break;
371 }
372 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100373 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
374 if (fd >= 0)
375 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000376 restore_uid();
377 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
378 success = 1;
379 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000380
381 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
382 if (no_agent_forwarding_flag || compat13) {
383 debug("Authentication agent forwarding not permitted for this authentication.");
384 break;
385 }
386 debug("Received authentication agent forwarding request.");
Damien Miller0c043c12000-06-07 21:22:38 +1000387 success = auth_input_request_forwarding(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000388 break;
389
390 case SSH_CMSG_PORT_FORWARD_REQUEST:
391 if (no_port_forwarding_flag) {
392 debug("Port forwarding not permitted for this authentication.");
393 break;
394 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100395 if (!options.allow_tcp_forwarding) {
396 debug("Port forwarding not permitted.");
397 break;
398 }
Damien Millerb38eff82000-04-01 11:09:21 +1000399 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000400 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000401 success = 1;
402 break;
403
404 case SSH_CMSG_MAX_PACKET_SIZE:
405 if (packet_set_maxsize(packet_get_int()) > 0)
406 success = 1;
407 break;
408
409 case SSH_CMSG_EXEC_SHELL:
410 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000411 if (type == SSH_CMSG_EXEC_CMD) {
412 command = packet_get_string(&dlen);
413 debug("Exec command '%.500s'", command);
414 packet_integrity_check(plen, 4 + dlen, type);
415 } else {
416 command = NULL;
417 packet_integrity_check(plen, 0, type);
418 }
419 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100420 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000421 command = forced_command;
422 debug("Forced command '%.500s'", forced_command);
423 }
424 if (have_pty)
425 do_exec_pty(s, command, pw);
426 else
427 do_exec_no_pty(s, command, pw);
428
429 if (command != NULL)
430 xfree(command);
431 /* Cleanup user's local Xauthority file. */
432 if (xauthfile)
433 xauthfile_cleanup_proc(NULL);
434 return;
435
436 default:
437 /*
438 * Any unknown messages in this phase are ignored,
439 * and a failure message is returned.
440 */
441 log("Unknown packet type received after authentication: %d", type);
442 }
443 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
444 packet_send();
445 packet_write_wait();
446
447 /* Enable compression now that we have replied if appropriate. */
448 if (enable_compression_after_reply) {
449 enable_compression_after_reply = 0;
450 packet_start_compression(compression_level);
451 }
452 }
453}
454
455/*
456 * This is called to fork and execute a command when we have no tty. This
457 * will call do_child from the child, and server_loop from the parent after
458 * setting up file descriptors and such.
459 */
Damien Miller4af51302000-04-16 11:18:38 +1000460void
Damien Millerb38eff82000-04-01 11:09:21 +1000461do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
462{
463 int pid;
464
465#ifdef USE_PIPES
466 int pin[2], pout[2], perr[2];
467 /* Allocate pipes for communicating with the program. */
468 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
469 packet_disconnect("Could not create pipes: %.100s",
470 strerror(errno));
471#else /* USE_PIPES */
472 int inout[2], err[2];
473 /* Uses socket pairs to communicate with the program. */
474 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
475 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
476 packet_disconnect("Could not create socket pairs: %.100s",
477 strerror(errno));
478#endif /* USE_PIPES */
479 if (s == NULL)
480 fatal("do_exec_no_pty: no session");
481
Damien Millere247cc42000-05-07 12:03:14 +1000482 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000483
Kevin Stevesff793a22001-02-21 16:36:51 +0000484#ifdef USE_PAM
485 do_pam_setcred();
486#endif /* USE_PAM */
487
Damien Millerb38eff82000-04-01 11:09:21 +1000488 /* Fork the child. */
489 if ((pid = fork()) == 0) {
490 /* Child. Reinitialize the log since the pid has changed. */
491 log_init(__progname, options.log_level, options.log_facility, log_stderr);
492
Damien Miller152cea22000-12-13 19:21:51 +1100493 signal(SIGPIPE, SIG_DFL);
494
Damien Millerb38eff82000-04-01 11:09:21 +1000495 /*
496 * Create a new session and process group since the 4.4BSD
497 * setlogin() affects the entire process group.
498 */
499 if (setsid() < 0)
500 error("setsid failed: %.100s", strerror(errno));
501
502#ifdef USE_PIPES
503 /*
504 * Redirect stdin. We close the parent side of the socket
505 * pair, and make the child side the standard input.
506 */
507 close(pin[1]);
508 if (dup2(pin[0], 0) < 0)
509 perror("dup2 stdin");
510 close(pin[0]);
511
512 /* Redirect stdout. */
513 close(pout[0]);
514 if (dup2(pout[1], 1) < 0)
515 perror("dup2 stdout");
516 close(pout[1]);
517
518 /* Redirect stderr. */
519 close(perr[0]);
520 if (dup2(perr[1], 2) < 0)
521 perror("dup2 stderr");
522 close(perr[1]);
523#else /* USE_PIPES */
524 /*
525 * Redirect stdin, stdout, and stderr. Stdin and stdout will
526 * use the same socket, as some programs (particularly rdist)
527 * seem to depend on it.
528 */
529 close(inout[1]);
530 close(err[1]);
531 if (dup2(inout[0], 0) < 0) /* stdin */
532 perror("dup2 stdin");
533 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
534 perror("dup2 stdout");
535 if (dup2(err[0], 2) < 0) /* stderr */
536 perror("dup2 stderr");
537#endif /* USE_PIPES */
538
539 /* Do processing for the child (exec command etc). */
540 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
541 /* NOTREACHED */
542 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100543#ifdef HAVE_CYGWIN
544 if (is_winnt)
545 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
546#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000547 if (pid < 0)
548 packet_disconnect("fork failed: %.100s", strerror(errno));
549 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000550 /* Set interactive/non-interactive mode. */
551 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000552#ifdef USE_PIPES
553 /* We are the parent. Close the child sides of the pipes. */
554 close(pin[0]);
555 close(pout[1]);
556 close(perr[1]);
557
Damien Millerefb4afe2000-04-12 18:45:05 +1000558 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000559 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000560 } else {
561 /* Enter the interactive session. */
562 server_loop(pid, pin[1], pout[0], perr[0]);
563 /* server_loop has closed pin[1], pout[1], and perr[1]. */
564 }
Damien Millerb38eff82000-04-01 11:09:21 +1000565#else /* USE_PIPES */
566 /* We are the parent. Close the child sides of the socket pairs. */
567 close(inout[0]);
568 close(err[0]);
569
570 /*
571 * Enter the interactive session. Note: server_loop must be able to
572 * handle the case that fdin and fdout are the same.
573 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000574 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000575 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000576 } else {
577 server_loop(pid, inout[1], inout[1], err[1]);
578 /* server_loop has closed inout[1] and err[1]. */
579 }
Damien Millerb38eff82000-04-01 11:09:21 +1000580#endif /* USE_PIPES */
581}
582
583/*
584 * This is called to fork and execute a command when we have a tty. This
585 * will call do_child from the child, and server_loop from the parent after
586 * setting up file descriptors, controlling tty, updating wtmp, utmp,
587 * lastlog, and other such operations.
588 */
Damien Miller4af51302000-04-16 11:18:38 +1000589void
Damien Millerb38eff82000-04-01 11:09:21 +1000590do_exec_pty(Session *s, const char *command, struct passwd * pw)
591{
Damien Millerb38eff82000-04-01 11:09:21 +1000592 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000593 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000594
595 if (s == NULL)
596 fatal("do_exec_pty: no session");
597 ptyfd = s->ptyfd;
598 ttyfd = s->ttyfd;
599
Kevin Stevesff793a22001-02-21 16:36:51 +0000600#ifdef USE_PAM
601 do_pam_session(pw->pw_name, s->tty);
602 do_pam_setcred();
603#endif /* USE_PAM */
604
Damien Millerb38eff82000-04-01 11:09:21 +1000605 /* Fork the child. */
606 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000607 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000608 log_init(__progname, options.log_level, options.log_facility, log_stderr);
609
Damien Miller152cea22000-12-13 19:21:51 +1100610 signal(SIGPIPE, SIG_DFL);
611
Damien Millerb38eff82000-04-01 11:09:21 +1000612 /* Close the master side of the pseudo tty. */
613 close(ptyfd);
614
615 /* Make the pseudo tty our controlling tty. */
616 pty_make_controlling_tty(&ttyfd, s->tty);
617
618 /* Redirect stdin from the pseudo tty. */
619 if (dup2(ttyfd, fileno(stdin)) < 0)
620 error("dup2 stdin failed: %.100s", strerror(errno));
621
622 /* Redirect stdout to the pseudo tty. */
623 if (dup2(ttyfd, fileno(stdout)) < 0)
624 error("dup2 stdin failed: %.100s", strerror(errno));
625
626 /* Redirect stderr to the pseudo tty. */
627 if (dup2(ttyfd, fileno(stderr)) < 0)
628 error("dup2 stdin failed: %.100s", strerror(errno));
629
630 /* Close the extra descriptor for the pseudo tty. */
631 close(ttyfd);
632
Damien Miller942da032000-08-18 13:59:06 +1000633 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100634 if (!(options.use_login && command == NULL))
635 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000636
Damien Millerb38eff82000-04-01 11:09:21 +1000637 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000638 do_child(command, pw, s->term, s->display, s->auth_proto,
639 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000640 /* NOTREACHED */
641 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100642#ifdef HAVE_CYGWIN
643 if (is_winnt)
644 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
645#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000646 if (pid < 0)
647 packet_disconnect("fork failed: %.100s", strerror(errno));
648 s->pid = pid;
649
650 /* Parent. Close the slave side of the pseudo tty. */
651 close(ttyfd);
652
653 /*
654 * Create another descriptor of the pty master side for use as the
655 * standard input. We could use the original descriptor, but this
656 * simplifies code in server_loop. The descriptor is bidirectional.
657 */
658 fdout = dup(ptyfd);
659 if (fdout < 0)
660 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
661
662 /* we keep a reference to the pty master */
663 ptymaster = dup(ptyfd);
664 if (ptymaster < 0)
665 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
666 s->ptymaster = ptymaster;
667
668 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000669 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000670 if (compat20) {
671 session_set_fds(s, ptyfd, fdout, -1);
672 } else {
673 server_loop(pid, ptyfd, fdout, -1);
674 /* server_loop _has_ closed ptyfd and fdout. */
675 session_pty_cleanup(s);
676 }
Damien Millerb38eff82000-04-01 11:09:21 +1000677}
678
Damien Miller942da032000-08-18 13:59:06 +1000679const char *
680get_remote_name_or_ip(void)
681{
682 static const char *remote = "";
683 if (utmp_len > 0)
Damien Miller33804262001-02-04 23:20:18 +1100684 remote = get_canonical_hostname(options.reverse_mapping_check);
Damien Miller942da032000-08-18 13:59:06 +1000685 if (utmp_len == 0 || strlen(remote) > utmp_len)
686 remote = get_remote_ipaddr();
687 return remote;
688}
689
690/* administrative, login(1)-like work */
691void
Damien Miller69b69aa2000-10-28 14:19:58 +1100692do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000693{
694 FILE *f;
695 char *time_string;
696 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100697 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000698 socklen_t fromlen;
699 struct sockaddr_storage from;
700 struct stat st;
701 time_t last_login_time;
702 struct passwd * pw = s->pw;
703 pid_t pid = getpid();
704
705 /*
706 * Get IP address of client. If the connection is not a socket, let
707 * the address be 0.0.0.0.
708 */
709 memset(&from, 0, sizeof(from));
710 if (packet_connection_is_on_socket()) {
711 fromlen = sizeof(from);
712 if (getpeername(packet_get_connection_in(),
713 (struct sockaddr *) & from, &fromlen) < 0) {
714 debug("getpeername: %.100s", strerror(errno));
715 fatal_cleanup();
716 }
717 }
718
Damien Miller7b28dc52000-09-05 13:34:53 +1100719 /* Get the time and hostname when the user last logged in. */
Damien Millere4340be2000-09-16 13:29:08 +1100720 hostname[0] = '\0';
721 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
722 hostname, sizeof(hostname));
723
Damien Miller942da032000-08-18 13:59:06 +1000724 /* Record that there was a login on that tty from the remote host. */
725 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
726 get_remote_name_or_ip(), (struct sockaddr *)&from);
727
Kevin Steves092f2ef2000-10-14 13:36:13 +0000728#ifdef USE_PAM
729 /*
730 * If password change is needed, do it now.
731 * This needs to occur before the ~/.hushlogin check.
732 */
Damien Miller646aa602001-02-15 11:51:32 +1100733 if (is_pam_password_change_required()) {
Kevin Steves092f2ef2000-10-14 13:36:13 +0000734 print_pam_messages();
735 do_pam_chauthtok();
736 }
737#endif
738
Damien Miller69b69aa2000-10-28 14:19:58 +1100739 /* Done if .hushlogin exists or a command given. */
740 if (command != NULL)
741 return;
Damien Miller942da032000-08-18 13:59:06 +1000742 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000743#ifdef HAVE_LOGIN_CAP
744 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
745#else
Damien Miller942da032000-08-18 13:59:06 +1000746 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000747#endif
Damien Miller942da032000-08-18 13:59:06 +1000748 return;
749
750#ifdef USE_PAM
Damien Miller646aa602001-02-15 11:51:32 +1100751 if (!is_pam_password_change_required())
Kevin Steves092f2ef2000-10-14 13:36:13 +0000752 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000753#endif /* USE_PAM */
754#ifdef WITH_AIXAUTHENTICATE
755 if (aixloginmsg && *aixloginmsg)
756 printf("%s\n", aixloginmsg);
757#endif /* WITH_AIXAUTHENTICATE */
758
Damien Miller942da032000-08-18 13:59:06 +1000759 if (last_login_time != 0) {
760 time_string = ctime(&last_login_time);
761 if (strchr(time_string, '\n'))
762 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000763 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000764 printf("Last login: %s\r\n", time_string);
765 else
Damien Millere4340be2000-09-16 13:29:08 +1100766 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000767 }
768 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000769#ifdef HAVE_LOGIN_CAP
770 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
771 "/etc/motd"), "r");
772#else
Damien Miller942da032000-08-18 13:59:06 +1000773 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000774#endif
Damien Miller942da032000-08-18 13:59:06 +1000775 if (f) {
776 while (fgets(buf, sizeof(buf), f))
777 fputs(buf, stdout);
778 fclose(f);
779 }
780 }
781}
782
Damien Millerb38eff82000-04-01 11:09:21 +1000783/*
784 * Sets the value of the given variable in the environment. If the variable
785 * already exists, its value is overriden.
786 */
Damien Miller4af51302000-04-16 11:18:38 +1000787void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000788child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000789 const char *value)
790{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000791 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000792 char **env;
793
794 /*
795 * Find the slot where the value should be stored. If the variable
796 * already exists, we reuse the slot; otherwise we append a new slot
797 * at the end of the array, expanding if necessary.
798 */
799 env = *envp;
800 namelen = strlen(name);
801 for (i = 0; env[i]; i++)
802 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
803 break;
804 if (env[i]) {
805 /* Reuse the slot. */
806 xfree(env[i]);
807 } else {
808 /* New variable. Expand if necessary. */
809 if (i >= (*envsizep) - 1) {
810 (*envsizep) += 50;
811 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
812 }
813 /* Need to set the NULL pointer at end of array beyond the new slot. */
814 env[i + 1] = NULL;
815 }
816
817 /* Allocate space and format the variable in the appropriate slot. */
818 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
819 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
820}
821
822/*
823 * Reads environment variables from the given file and adds/overrides them
824 * into the environment. If the file does not exist, this does nothing.
825 * Otherwise, it must consist of empty lines, comments (line starts with '#')
826 * and assignments of the form name=value. No other forms are allowed.
827 */
Damien Miller4af51302000-04-16 11:18:38 +1000828void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000829read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000830 const char *filename)
831{
832 FILE *f;
833 char buf[4096];
834 char *cp, *value;
835
836 f = fopen(filename, "r");
837 if (!f)
838 return;
839
840 while (fgets(buf, sizeof(buf), f)) {
841 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
842 ;
843 if (!*cp || *cp == '#' || *cp == '\n')
844 continue;
845 if (strchr(cp, '\n'))
846 *strchr(cp, '\n') = '\0';
847 value = strchr(cp, '=');
848 if (value == NULL) {
849 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
850 continue;
851 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000852 /*
853 * Replace the equals sign by nul, and advance value to
854 * the value string.
855 */
Damien Millerb38eff82000-04-01 11:09:21 +1000856 *value = '\0';
857 value++;
858 child_set_env(env, envsize, cp, value);
859 }
860 fclose(f);
861}
862
863#ifdef USE_PAM
864/*
865 * Sets any environment variables which have been specified by PAM
866 */
867void do_pam_environment(char ***env, int *envsize)
868{
869 char *equals, var_name[512], var_val[512];
870 char **pam_env;
871 int i;
872
873 if ((pam_env = fetch_pam_environment()) == NULL)
874 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000875
Damien Millerb38eff82000-04-01 11:09:21 +1000876 for(i = 0; pam_env[i] != NULL; i++) {
877 if ((equals = strstr(pam_env[i], "=")) == NULL)
878 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000879
Damien Millerb38eff82000-04-01 11:09:21 +1000880 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
881 memset(var_name, '\0', sizeof(var_name));
882 memset(var_val, '\0', sizeof(var_val));
883
884 strncpy(var_name, pam_env[i], equals - pam_env[i]);
885 strcpy(var_val, equals + 1);
886
Damien Millercb5e44a2000-09-29 12:12:36 +1100887 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000888
889 child_set_env(env, envsize, var_name, var_val);
890 }
891 }
892}
893#endif /* USE_PAM */
894
Damien Millercb5e44a2000-09-29 12:12:36 +1100895#ifdef HAVE_CYGWIN
896void copy_environment(char ***env, int *envsize)
897{
898 char *equals, var_name[512], var_val[512];
899 int i;
900
901 for(i = 0; environ[i] != NULL; i++) {
902 if ((equals = strstr(environ[i], "=")) == NULL)
903 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000904
Damien Millercb5e44a2000-09-29 12:12:36 +1100905 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
906 memset(var_name, '\0', sizeof(var_name));
907 memset(var_val, '\0', sizeof(var_val));
908
909 strncpy(var_name, environ[i], equals - environ[i]);
910 strcpy(var_val, equals + 1);
911
912 debug3("Copy environment: %s=%s", var_name, var_val);
913
914 child_set_env(env, envsize, var_name, var_val);
915 }
916 }
917}
918#endif
919
Damien Miller5fc85652000-07-09 23:53:07 +1000920#if defined(HAVE_GETUSERATTR)
921/*
922 * AIX-specific login initialisation
923 */
924void set_limit(char *user, char *soft, char *hard, int resource, int mult)
925{
926 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000927 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000928
929 getrlimit(resource, &rlim);
930
Damien Miller65964d62000-07-11 09:16:22 +1000931 slim = 0;
932 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
933 if (slim < 0) {
934 rlim.rlim_cur = RLIM_INFINITY;
935 } else if (slim != 0) {
936 /* See the wackiness below */
937 if (rlim.rlim_cur == slim * mult)
938 slim = 0;
939 else
940 rlim.rlim_cur = slim * mult;
941 }
942 }
Damien Miller5fc85652000-07-09 23:53:07 +1000943
Damien Miller65964d62000-07-11 09:16:22 +1000944 hlim = 0;
945 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
946 if (hlim < 0) {
947 rlim.rlim_max = RLIM_INFINITY;
948 } else if (hlim != 0) {
949 rlim.rlim_max = hlim * mult;
950 }
951 }
Damien Miller5fc85652000-07-09 23:53:07 +1000952
Damien Miller65964d62000-07-11 09:16:22 +1000953 /*
954 * XXX For cpu and fsize the soft limit is set to the hard limit
955 * if the hard limit is left at its default value and the soft limit
956 * is changed from its default value, either by requesting it
957 * (slim == 0) or by setting it to the current default. At least
958 * that's how rlogind does it. If you're confused you're not alone.
959 * Bug or feature? AIX 4.3.1.2
960 */
961 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
962 && hlim == 0 && slim != 0)
963 rlim.rlim_max = rlim.rlim_cur;
964 /* A specified hard limit limits the soft limit */
965 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
966 rlim.rlim_cur = rlim.rlim_max;
967 /* A soft limit can increase a hard limit */
968 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000969 rlim.rlim_max = rlim.rlim_cur;
970
971 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000972 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000973}
974
975void set_limits_from_userattr(char *user)
976{
977 int mask;
978 char buf[16];
979
980 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
981 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
982 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
983 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
984 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
985 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
986#if defined(S_UNOFILE)
987 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
988#endif
989
990 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
991 /* Convert decimal to octal */
992 (void) snprintf(buf, sizeof(buf), "%d", mask);
993 if (sscanf(buf, "%o", &mask) == 1)
994 umask(mask);
995 }
996}
997#endif /* defined(HAVE_GETUSERATTR) */
998
Damien Millerb38eff82000-04-01 11:09:21 +1000999/*
1000 * Performs common processing for the child, such as setting up the
1001 * environment, closing extra file descriptors, setting the user and group
1002 * ids, and executing the command or shell.
1003 */
Damien Miller4af51302000-04-16 11:18:38 +10001004void
Damien Millerb38eff82000-04-01 11:09:21 +10001005do_child(const char *command, struct passwd * pw, const char *term,
1006 const char *display, const char *auth_proto,
1007 const char *auth_data, const char *ttyname)
1008{
Damien Miller7b28dc52000-09-05 13:34:53 +11001009 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001010 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001011 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001012 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001013 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001014 char **env;
1015 extern char **environ;
1016 struct stat st;
1017 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001018#ifdef WITH_IRIX_PROJECT
1019 prid_t projid;
1020#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001021#ifdef WITH_IRIX_JOBS
1022 jid_t jid = 0;
1023#else
1024#ifdef WITH_IRIX_ARRAY
1025 int jid = 0;
1026#endif /* WITH_IRIX_ARRAY */
1027#endif /* WITH_IRIX_JOBS */
1028
Damien Millerb38eff82000-04-01 11:09:21 +10001029
Damien Millerd3a18572000-06-07 19:55:44 +10001030 /* login(1) is only called if we execute the login shell */
1031 if (options.use_login && command != NULL)
1032 options.use_login = 0;
1033
Damien Millerb38eff82000-04-01 11:09:21 +10001034#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001035 if (!options.use_login) {
1036# ifdef HAVE_LOGIN_CAP
1037 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1038 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1039 _PATH_NOLOGIN), "r");
1040# else /* HAVE_LOGIN_CAP */
1041 if (pw->pw_uid)
1042 f = fopen(_PATH_NOLOGIN, "r");
1043# endif /* HAVE_LOGIN_CAP */
1044 if (f) {
1045 /* /etc/nologin exists. Print its contents and exit. */
1046 while (fgets(buf, sizeof(buf), f))
1047 fputs(buf, stderr);
1048 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001049 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001050 }
Damien Millerb38eff82000-04-01 11:09:21 +10001051 }
1052#endif /* USE_PAM */
1053
Damien Millerad833b32000-08-23 10:46:23 +10001054 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001055 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1056 switch, so we let login(1) to this for us. */
1057 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001058#ifdef HAVE_OSF_SIA
Damien Miller92ddb7d2001-02-14 01:25:23 +11001059 session_setup_sia(pw->pw_name, ttyname);
Kevin Steves7fafa5c2001-02-13 18:45:00 +00001060#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001061#ifdef HAVE_CYGWIN
1062 if (is_winnt) {
1063#else
Damien Millerb38eff82000-04-01 11:09:21 +10001064 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001065#endif
Damien Millerad833b32000-08-23 10:46:23 +10001066# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001067 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001068# endif /* HAVE_GETUSERATTR */
1069# ifdef HAVE_LOGIN_CAP
1070 if (setusercontext(lc, pw, pw->pw_uid,
1071 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1072 perror("unable to set user context");
1073 exit(1);
1074 }
Damien Miller60396b02001-02-18 17:01:00 +11001075#ifdef BSD_AUTH
1076 if (auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
1077 error("approval failure for %s", pw->pw_name);
1078 fprintf(stderr, "Approval failure");
1079 exit(1);
1080 }
1081#endif
Damien Millerad833b32000-08-23 10:46:23 +10001082# else /* HAVE_LOGIN_CAP */
Ben Lindstrome1bd29b2001-02-21 20:00:28 +00001083#if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)
1084 /* Sets login uid for accounting */
1085 if (getluid() == -1 && setluid(pw->pw_uid) == -1)
1086 error("setluid: %s", strerror(errno));
1087#endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */
1088
Damien Millerad833b32000-08-23 10:46:23 +10001089 if (setlogin(pw->pw_name) < 0)
1090 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001091 if (setgid(pw->pw_gid) < 0) {
1092 perror("setgid");
1093 exit(1);
1094 }
1095 /* Initialize the group list. */
1096 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1097 perror("initgroups");
1098 exit(1);
1099 }
1100 endgrent();
Ben Lindstrom980754c2000-11-12 00:04:24 +00001101# ifdef WITH_IRIX_JOBS
1102 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1103 if (jid == -1) {
1104 fatal("Failed to create job container: %.100s",
1105 strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001106 }
Ben Lindstrom980754c2000-11-12 00:04:24 +00001107# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001108# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001109 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001110 if (jid == 0) {
1111 if (newarraysess() != 0)
1112 fatal("Failed to set up new array session: %.100s",
1113 strerror(errno));
1114 }
Damien Millerad833b32000-08-23 10:46:23 +10001115# endif /* WITH_IRIX_ARRAY */
1116# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001117 /* initialize irix project info */
1118 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1119 debug("Failed to get project id, using projid 0");
1120 projid = 0;
1121 }
Damien Miller91606b12000-06-28 08:22:29 +10001122 if (setprid(projid))
1123 fatal("Failed to initialize project %d for %s: %.100s",
1124 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001125# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001126#ifdef WITH_IRIX_AUDIT
1127 if (sysconf(_SC_AUDIT)) {
1128 debug("Setting sat id to %d", (int) pw->pw_uid);
1129 if (satsetid(pw->pw_uid))
1130 debug("error setting satid: %.100s", strerror(errno));
1131 }
1132#endif /* WITH_IRIX_AUDIT */
1133
Damien Millerb38eff82000-04-01 11:09:21 +10001134 /* Permanently switch to the desired uid. */
1135 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001136# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001137 }
Damien Millerad833b32000-08-23 10:46:23 +10001138#endif /* HAVE_OSF_SIA */
1139
Damien Millerbac2d8a2000-09-05 16:13:06 +11001140#ifdef HAVE_CYGWIN
1141 if (is_winnt)
1142#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001143 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001144 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001145 }
1146 /*
1147 * Get the shell from the password data. An empty shell field is
1148 * legal, and means /bin/sh.
1149 */
1150 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001151#ifdef HAVE_LOGIN_CAP
1152 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1153#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001154
1155#ifdef AFS
1156 /* Try to get AFS tokens for the local cell. */
1157 if (k_hasafs()) {
1158 char cell[64];
1159
1160 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1161 krb_afslog(cell, 0);
1162
1163 krb_afslog(0, 0);
1164 }
1165#endif /* AFS */
1166
1167 /* Initialize the environment. */
1168 envsize = 100;
1169 env = xmalloc(envsize * sizeof(char *));
1170 env[0] = NULL;
1171
Damien Millerbac2d8a2000-09-05 16:13:06 +11001172#ifdef HAVE_CYGWIN
1173 /*
1174 * The Windows environment contains some setting which are
1175 * important for a running system. They must not be dropped.
1176 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001177 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001178#endif
1179
Damien Millerb38eff82000-04-01 11:09:21 +10001180 if (!options.use_login) {
1181 /* Set basic environment. */
1182 child_set_env(&env, &envsize, "USER", pw->pw_name);
1183 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1184 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001185#ifdef HAVE_LOGIN_CAP
1186 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1187 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001188#else /* HAVE_LOGIN_CAP */
1189# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001190 /*
1191 * There's no standard path on Windows. The path contains
1192 * important components pointing to the system directories,
1193 * needed for loading shared libraries. So the path better
1194 * remains intact here.
1195 */
Damien Millerb38eff82000-04-01 11:09:21 +10001196 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001197# endif /* HAVE_CYGWIN */
1198#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001199
1200 snprintf(buf, sizeof buf, "%.200s/%.50s",
1201 _PATH_MAILDIR, pw->pw_name);
1202 child_set_env(&env, &envsize, "MAIL", buf);
1203
1204 /* Normal systems set SHELL by default. */
1205 child_set_env(&env, &envsize, "SHELL", shell);
1206 }
1207 if (getenv("TZ"))
1208 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1209
1210 /* Set custom environment options from RSA authentication. */
1211 while (custom_environment) {
1212 struct envstring *ce = custom_environment;
1213 char *s = ce->s;
1214 int i;
1215 for (i = 0; s[i] != '=' && s[i]; i++);
1216 if (s[i] == '=') {
1217 s[i] = 0;
1218 child_set_env(&env, &envsize, s, s + i + 1);
1219 }
1220 custom_environment = ce->next;
1221 xfree(ce->s);
1222 xfree(ce);
1223 }
1224
1225 snprintf(buf, sizeof buf, "%.50s %d %d",
1226 get_remote_ipaddr(), get_remote_port(), get_local_port());
1227 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1228
1229 if (ttyname)
1230 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1231 if (term)
1232 child_set_env(&env, &envsize, "TERM", term);
1233 if (display)
1234 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001235 if (original_command)
1236 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1237 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001238
1239#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001240 if ((cp = getenv("AUTHSTATE")) != NULL)
1241 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1242 if ((cp = getenv("KRB5CCNAME")) != NULL)
1243 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1244 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001245#endif
1246
1247#ifdef KRB4
1248 {
1249 extern char *ticket;
1250
1251 if (ticket)
1252 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1253 }
1254#endif /* KRB4 */
1255
1256#ifdef USE_PAM
1257 /* Pull in any environment variables that may have been set by PAM. */
1258 do_pam_environment(&env, &envsize);
1259#endif /* USE_PAM */
1260
Damien Millerb38eff82000-04-01 11:09:21 +10001261 if (xauthfile)
1262 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1263 if (auth_get_socket_name() != NULL)
1264 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1265 auth_get_socket_name());
1266
1267 /* read $HOME/.ssh/environment. */
1268 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001269 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1270 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001271 read_environment_file(&env, &envsize, buf);
1272 }
1273 if (debug_flag) {
1274 /* dump the environment */
1275 fprintf(stderr, "Environment:\n");
1276 for (i = 0; env[i]; i++)
1277 fprintf(stderr, " %.200s\n", env[i]);
1278 }
Damien Millerad833b32000-08-23 10:46:23 +10001279 /* we have to stash the hostname before we close our socket. */
1280 if (options.use_login)
1281 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001282 /*
1283 * Close the connection descriptors; note that this is the child, and
1284 * the server will still have the socket open, and it is important
1285 * that we do not shutdown it. Note that the descriptors cannot be
1286 * closed before building the environment, as we call
1287 * get_remote_ipaddr there.
1288 */
1289 if (packet_get_connection_in() == packet_get_connection_out())
1290 close(packet_get_connection_in());
1291 else {
1292 close(packet_get_connection_in());
1293 close(packet_get_connection_out());
1294 }
1295 /*
1296 * Close all descriptors related to channels. They will still remain
1297 * open in the parent.
1298 */
1299 /* XXX better use close-on-exec? -markus */
1300 channel_close_all();
1301
1302 /*
1303 * Close any extra file descriptors. Note that there may still be
1304 * descriptors left by system functions. They will be closed later.
1305 */
1306 endpwent();
1307
1308 /*
1309 * Close any extra open file descriptors so that we don\'t have them
1310 * hanging around in clients. Note that we want to do this after
1311 * initgroups, because at least on Solaris 2.3 it leaves file
1312 * descriptors open.
1313 */
1314 for (i = 3; i < 64; i++)
1315 close(i);
1316
1317 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001318 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001319 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1320 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001321#ifdef HAVE_LOGIN_CAP
1322 if (login_getcapbool(lc, "requirehome", 0))
1323 exit(1);
1324#endif
1325 }
Damien Millerb38eff82000-04-01 11:09:21 +10001326
1327 /*
1328 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1329 * xauth are run in the proper environment.
1330 */
1331 environ = env;
1332
1333 /*
1334 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1335 * in this order).
1336 */
1337 if (!options.use_login) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001338 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001339 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001340 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001341
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001342 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001343 if (f) {
1344 if (auth_proto != NULL && auth_data != NULL)
1345 fprintf(f, "%s %s\n", auth_proto, auth_data);
1346 pclose(f);
1347 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001348 fprintf(stderr, "Could not run %s\n", _PATH_SSH_USER_RC);
1349 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001350 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001351 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001352
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001353 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001354 if (f) {
1355 if (auth_proto != NULL && auth_data != NULL)
1356 fprintf(f, "%s %s\n", auth_proto, auth_data);
1357 pclose(f);
1358 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001359 fprintf(stderr, "Could not run %s\n", _PATH_SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001360 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001361 /* Add authority data to .Xauthority if appropriate. */
1362 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001363 char *screen = strchr(display, ':');
1364 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001365 fprintf(stderr,
1366 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001367 options.xauth_location, display,
1368 auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001369#ifndef NO_X11_UNIX_SOCKETS
Damien Millerd999ae22000-05-20 12:49:31 +10001370 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001371 fprintf(stderr,
1372 "Adding %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001373 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001374 screen, auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001375#endif /* NO_X11_UNIX_SOCKETS */
Damien Millerd999ae22000-05-20 12:49:31 +10001376 }
Damien Miller0c043c12000-06-07 21:22:38 +10001377 snprintf(cmd, sizeof cmd, "%s -q -",
1378 options.xauth_location);
1379 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001380 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001381 fprintf(f, "add %s %s %s\n", display,
1382 auth_proto, auth_data);
Ben Lindstrom38e60932001-02-24 00:55:04 +00001383#ifndef NO_X11_UNIX_SOCKETS
Kevin Stevesef4eea92001-02-05 12:42:17 +00001384 if (screen != NULL)
Damien Millerd999ae22000-05-20 12:49:31 +10001385 fprintf(f, "add %.*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 Millerb38eff82000-04-01 11:09:21 +10001389 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001390 } else {
1391 fprintf(stderr, "Could not run %s\n",
1392 cmd);
1393 }
Damien Millerb38eff82000-04-01 11:09:21 +10001394 }
1395 }
Damien Millerb38eff82000-04-01 11:09:21 +10001396 /* Get the last component of the shell name. */
1397 cp = strrchr(shell, '/');
1398 if (cp)
1399 cp++;
1400 else
1401 cp = shell;
1402 }
1403 /*
1404 * If we have no command, execute the shell. In this case, the shell
1405 * name to be passed in argv[0] is preceded by '-' to indicate that
1406 * this is a login shell.
1407 */
1408 if (!command) {
1409 if (!options.use_login) {
1410 char buf[256];
1411
1412 /*
1413 * Check for mail if we have a tty and it was enabled
1414 * in server options.
1415 */
1416 if (ttyname && options.check_mail) {
1417 char *mailbox;
1418 struct stat mailstat;
1419 mailbox = getenv("MAIL");
1420 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001421 if (stat(mailbox, &mailstat) != 0 ||
1422 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001423 printf("No mail.\n");
1424 else if (mailstat.st_mtime < mailstat.st_atime)
1425 printf("You have mail.\n");
1426 else
1427 printf("You have new mail.\n");
1428 }
1429 }
1430 /* Start the shell. Set initial character to '-'. */
1431 buf[0] = '-';
1432 strncpy(buf + 1, cp, sizeof(buf) - 1);
1433 buf[sizeof(buf) - 1] = 0;
1434
1435 /* Execute the shell. */
1436 argv[0] = buf;
1437 argv[1] = NULL;
1438 execve(shell, argv, env);
1439
1440 /* Executing the shell failed. */
1441 perror(shell);
1442 exit(1);
1443
1444 } else {
1445 /* Launch login(1). */
1446
Damien Millerad833b32000-08-23 10:46:23 +10001447 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001448 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001449
1450 /* Login couldn't be executed, die. */
1451
1452 perror("login");
1453 exit(1);
1454 }
1455 }
1456 /*
1457 * Execute the command using the user's shell. This uses the -c
1458 * option to execute the command.
1459 */
1460 argv[0] = (char *) cp;
1461 argv[1] = "-c";
1462 argv[2] = (char *) command;
1463 argv[3] = NULL;
1464 execve(shell, argv, env);
1465 perror(shell);
1466 exit(1);
1467}
1468
1469Session *
1470session_new(void)
1471{
1472 int i;
1473 static int did_init = 0;
1474 if (!did_init) {
1475 debug("session_new: init");
1476 for(i = 0; i < MAX_SESSIONS; i++) {
1477 sessions[i].used = 0;
1478 sessions[i].self = i;
1479 }
1480 did_init = 1;
1481 }
1482 for(i = 0; i < MAX_SESSIONS; i++) {
1483 Session *s = &sessions[i];
1484 if (! s->used) {
1485 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001486 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001487 s->chanid = -1;
1488 s->ptyfd = -1;
1489 s->ttyfd = -1;
1490 s->term = NULL;
1491 s->pw = NULL;
1492 s->display = NULL;
1493 s->screen = 0;
1494 s->auth_data = NULL;
1495 s->auth_proto = NULL;
1496 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001497 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001498 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001499 return s;
1500 }
1501 }
1502 return NULL;
1503}
1504
1505void
1506session_dump(void)
1507{
1508 int i;
1509 for(i = 0; i < MAX_SESSIONS; i++) {
1510 Session *s = &sessions[i];
1511 debug("dump: used %d session %d %p channel %d pid %d",
1512 s->used,
1513 s->self,
1514 s,
1515 s->chanid,
1516 s->pid);
1517 }
1518}
1519
Damien Millerefb4afe2000-04-12 18:45:05 +10001520int
1521session_open(int chanid)
1522{
1523 Session *s = session_new();
1524 debug("session_open: channel %d", chanid);
1525 if (s == NULL) {
1526 error("no more sessions");
1527 return 0;
1528 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001529 s->pw = auth_get_user();
1530 if (s->pw == NULL)
Kevin Steves43cdef32001-02-11 14:12:08 +00001531 fatal("no user for session %d", s->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001532 debug("session_open: session %d: link with channel %d", s->self, chanid);
1533 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001534 return 1;
1535}
1536
1537Session *
1538session_by_channel(int id)
1539{
1540 int i;
1541 for(i = 0; i < MAX_SESSIONS; i++) {
1542 Session *s = &sessions[i];
1543 if (s->used && s->chanid == id) {
1544 debug("session_by_channel: session %d channel %d", i, id);
1545 return s;
1546 }
1547 }
1548 debug("session_by_channel: unknown channel %d", id);
1549 session_dump();
1550 return NULL;
1551}
1552
1553Session *
1554session_by_pid(pid_t pid)
1555{
1556 int i;
1557 debug("session_by_pid: pid %d", pid);
1558 for(i = 0; i < MAX_SESSIONS; i++) {
1559 Session *s = &sessions[i];
1560 if (s->used && s->pid == pid)
1561 return s;
1562 }
1563 error("session_by_pid: unknown pid %d", pid);
1564 session_dump();
1565 return NULL;
1566}
1567
1568int
1569session_window_change_req(Session *s)
1570{
1571 s->col = packet_get_int();
1572 s->row = packet_get_int();
1573 s->xpixel = packet_get_int();
1574 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001575 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001576 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1577 return 1;
1578}
1579
1580int
1581session_pty_req(Session *s)
1582{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001583 u_int len;
Damien Miller4af51302000-04-16 11:18:38 +10001584 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001585
Damien Millerf6d9e222000-06-18 14:50:44 +10001586 if (no_pty_flag)
1587 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001588 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001589 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001590 s->term = packet_get_string(&len);
1591 s->col = packet_get_int();
1592 s->row = packet_get_int();
1593 s->xpixel = packet_get_int();
1594 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001595 term_modes = packet_get_string(&len);
1596 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001597
1598 if (strcmp(s->term, "") == 0) {
1599 xfree(s->term);
1600 s->term = NULL;
1601 }
1602 /* Allocate a pty and open it. */
1603 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1604 xfree(s->term);
1605 s->term = NULL;
1606 s->ptyfd = -1;
1607 s->ttyfd = -1;
1608 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001609 xfree(term_modes);
1610 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001611 }
1612 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1613 /*
1614 * Add a cleanup function to clear the utmp entry and record logout
1615 * time in case we call fatal() (e.g., the connection gets closed).
1616 */
1617 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1618 pty_setowner(s->pw, s->tty);
1619 /* Get window size from the packet. */
1620 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1621
Damien Millere247cc42000-05-07 12:03:14 +10001622 session_proctitle(s);
1623
Damien Miller5f056372000-04-16 12:31:48 +10001624 /* XXX parse and set terminal modes */
1625 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001626 return 1;
1627}
1628
Damien Millerbd483e72000-04-30 10:00:53 +10001629int
1630session_subsystem_req(Session *s)
1631{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001632 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001633 int success = 0;
1634 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001635 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001636
1637 packet_done();
1638 log("subsystem request for %s", subsys);
1639
Damien Millerf6d9e222000-06-18 14:50:44 +10001640 for (i = 0; i < options.num_subsystems; i++) {
1641 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1642 debug("subsystem: exec() %s", options.subsystem_command[i]);
1643 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1644 success = 1;
1645 }
1646 }
1647
1648 if (!success)
1649 log("subsystem request for %s failed, subsystem not found", subsys);
1650
Damien Millerbd483e72000-04-30 10:00:53 +10001651 xfree(subsys);
1652 return success;
1653}
1654
1655int
1656session_x11_req(Session *s)
1657{
Damien Miller7b28dc52000-09-05 13:34:53 +11001658 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001659 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001660 debug("X11 forwarding disabled in user configuration file.");
1661 return 0;
1662 }
Damien Millerbd483e72000-04-30 10:00:53 +10001663 if (!options.x11_forwarding) {
1664 debug("X11 forwarding disabled in server configuration file.");
1665 return 0;
1666 }
1667 if (xauthfile != NULL) {
1668 debug("X11 fwd already started.");
1669 return 0;
1670 }
1671
1672 debug("Received request for X11 forwarding with auth spoofing.");
1673 if (s->display != NULL)
1674 packet_disconnect("Protocol error: X11 display already set.");
1675
1676 s->single_connection = packet_get_char();
1677 s->auth_proto = packet_get_string(NULL);
1678 s->auth_data = packet_get_string(NULL);
1679 s->screen = packet_get_int();
1680 packet_done();
1681
1682 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1683 if (s->display == NULL) {
1684 xfree(s->auth_proto);
1685 xfree(s->auth_data);
1686 return 0;
1687 }
1688 xauthfile = xmalloc(MAXPATHLEN);
1689 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1690 temporarily_use_uid(s->pw->pw_uid);
1691 if (mkdtemp(xauthfile) == NULL) {
1692 restore_uid();
1693 error("private X11 dir: mkdtemp %s failed: %s",
1694 xauthfile, strerror(errno));
1695 xfree(xauthfile);
1696 xauthfile = NULL;
1697 xfree(s->auth_proto);
1698 xfree(s->auth_data);
1699 /* XXXX remove listening channels */
1700 return 0;
1701 }
1702 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001703 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1704 if (fd >= 0)
1705 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001706 restore_uid();
1707 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1708 return 1;
1709}
1710
Damien Millerf6d9e222000-06-18 14:50:44 +10001711int
1712session_shell_req(Session *s)
1713{
1714 /* if forced_command == NULL, the shell is execed */
1715 char *shell = forced_command;
1716 packet_done();
1717 s->extended = 1;
1718 if (s->ttyfd == -1)
1719 do_exec_no_pty(s, shell, s->pw);
1720 else
1721 do_exec_pty(s, shell, s->pw);
1722 return 1;
1723}
1724
1725int
1726session_exec_req(Session *s)
1727{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001728 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001729 char *command = packet_get_string(&len);
1730 packet_done();
1731 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001732 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001733 command = forced_command;
1734 debug("Forced command '%.500s'", forced_command);
1735 }
1736 s->extended = 1;
1737 if (s->ttyfd == -1)
1738 do_exec_no_pty(s, command, s->pw);
1739 else
1740 do_exec_pty(s, command, s->pw);
1741 if (forced_command == NULL)
1742 xfree(command);
1743 return 1;
1744}
1745
Damien Miller0bc1bd82000-11-13 22:57:25 +11001746int
1747session_auth_agent_req(Session *s)
1748{
1749 static int called = 0;
1750 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001751 if (no_agent_forwarding_flag) {
1752 debug("session_auth_agent_req: no_agent_forwarding_flag");
1753 return 0;
1754 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001755 if (called) {
1756 return 0;
1757 } else {
1758 called = 1;
1759 return auth_input_request_forwarding(s->pw);
1760 }
1761}
1762
Damien Millerefb4afe2000-04-12 18:45:05 +10001763void
1764session_input_channel_req(int id, void *arg)
1765{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001766 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001767 int reply;
1768 int success = 0;
1769 char *rtype;
1770 Session *s;
1771 Channel *c;
1772
1773 rtype = packet_get_string(&len);
1774 reply = packet_get_char();
1775
1776 s = session_by_channel(id);
1777 if (s == NULL)
1778 fatal("session_input_channel_req: channel %d: no session", id);
1779 c = channel_lookup(id);
1780 if (c == NULL)
1781 fatal("session_input_channel_req: channel %d: bad channel", id);
1782
1783 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1784 s->self, id, rtype, reply);
1785
1786 /*
1787 * a session is in LARVAL state until a shell
1788 * or programm is executed
1789 */
1790 if (c->type == SSH_CHANNEL_LARVAL) {
1791 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001792 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001793 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001794 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001795 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001796 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001797 } else if (strcmp(rtype, "x11-req") == 0) {
1798 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001799 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1800 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001801 } else if (strcmp(rtype, "subsystem") == 0) {
1802 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001803 }
1804 }
1805 if (strcmp(rtype, "window-change") == 0) {
1806 success = session_window_change_req(s);
1807 }
1808
1809 if (reply) {
1810 packet_start(success ?
1811 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1812 packet_put_int(c->remote_id);
1813 packet_send();
1814 }
1815 xfree(rtype);
1816}
1817
1818void
1819session_set_fds(Session *s, int fdin, int fdout, int fderr)
1820{
1821 if (!compat20)
1822 fatal("session_set_fds: called for proto != 2.0");
1823 /*
1824 * now that have a child and a pipe to the child,
1825 * we can activate our channel and register the fd's
1826 */
1827 if (s->chanid == -1)
1828 fatal("no channel for session %d", s->self);
1829 channel_set_fds(s->chanid,
1830 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001831 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1832 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001833}
1834
Damien Millerb38eff82000-04-01 11:09:21 +10001835void
1836session_pty_cleanup(Session *s)
1837{
1838 if (s == NULL || s->ttyfd == -1)
1839 return;
1840
Kevin Steves43cdef32001-02-11 14:12:08 +00001841 debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +10001842
1843 /* Cancel the cleanup function. */
1844 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1845
1846 /* Record that the user has logged out. */
1847 record_logout(s->pid, s->tty);
1848
1849 /* Release the pseudo-tty. */
1850 pty_release(s->tty);
1851
1852 /*
1853 * Close the server side of the socket pairs. We must do this after
1854 * the pty cleanup, so that another process doesn't get this pty
1855 * while we're still cleaning up.
1856 */
1857 if (close(s->ptymaster) < 0)
1858 error("close(s->ptymaster): %s", strerror(errno));
1859}
Damien Millerefb4afe2000-04-12 18:45:05 +10001860
1861void
1862session_exit_message(Session *s, int status)
1863{
1864 Channel *c;
1865 if (s == NULL)
1866 fatal("session_close: no session");
1867 c = channel_lookup(s->chanid);
1868 if (c == NULL)
1869 fatal("session_close: session %d: no channel %d",
1870 s->self, s->chanid);
1871 debug("session_exit_message: session %d channel %d pid %d",
1872 s->self, s->chanid, s->pid);
1873
1874 if (WIFEXITED(status)) {
1875 channel_request_start(s->chanid,
1876 "exit-status", 0);
1877 packet_put_int(WEXITSTATUS(status));
1878 packet_send();
1879 } else if (WIFSIGNALED(status)) {
1880 channel_request_start(s->chanid,
1881 "exit-signal", 0);
1882 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001883#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001884 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001885#else /* WCOREDUMP */
1886 packet_put_char(0);
1887#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001888 packet_put_cstring("");
1889 packet_put_cstring("");
1890 packet_send();
1891 } else {
1892 /* Some weird exit cause. Just exit. */
1893 packet_disconnect("wait returned status %04x.", status);
1894 }
1895
1896 /* disconnect channel */
1897 debug("session_exit_message: release channel %d", s->chanid);
1898 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001899 /*
1900 * emulate a write failure with 'chan_write_failed', nobody will be
1901 * interested in data we write.
1902 * Note that we must not call 'chan_read_failed', since there could
1903 * be some more data waiting in the pipe.
1904 */
Damien Millerbd483e72000-04-30 10:00:53 +10001905 if (c->ostate != CHAN_OUTPUT_CLOSED)
1906 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001907 s->chanid = -1;
1908}
1909
1910void
1911session_free(Session *s)
1912{
1913 debug("session_free: session %d pid %d", s->self, s->pid);
1914 if (s->term)
1915 xfree(s->term);
1916 if (s->display)
1917 xfree(s->display);
1918 if (s->auth_data)
1919 xfree(s->auth_data);
1920 if (s->auth_proto)
1921 xfree(s->auth_proto);
1922 s->used = 0;
1923}
1924
1925void
1926session_close(Session *s)
1927{
1928 session_pty_cleanup(s);
1929 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001930 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001931}
1932
1933void
1934session_close_by_pid(pid_t pid, int status)
1935{
1936 Session *s = session_by_pid(pid);
1937 if (s == NULL) {
1938 debug("session_close_by_pid: no session for pid %d", s->pid);
1939 return;
1940 }
1941 if (s->chanid != -1)
1942 session_exit_message(s, status);
1943 session_close(s);
1944}
1945
1946/*
1947 * this is called when a channel dies before
1948 * the session 'child' itself dies
1949 */
1950void
1951session_close_by_channel(int id, void *arg)
1952{
1953 Session *s = session_by_channel(id);
1954 if (s == NULL) {
1955 debug("session_close_by_channel: no session for channel %d", id);
1956 return;
1957 }
1958 /* disconnect channel */
1959 channel_cancel_cleanup(s->chanid);
1960 s->chanid = -1;
1961
1962 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1963 if (s->pid == 0) {
1964 /* close session immediately */
1965 session_close(s);
1966 } else {
1967 /* notify child, delay session cleanup */
Damien Miller7a445bb2000-06-26 13:12:37 +10001968 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001969 error("session_close_by_channel: kill %d: %s",
1970 s->pid, strerror(errno));
1971 }
1972}
1973
Damien Millere247cc42000-05-07 12:03:14 +10001974char *
1975session_tty_list(void)
1976{
1977 static char buf[1024];
1978 int i;
1979 buf[0] = '\0';
1980 for(i = 0; i < MAX_SESSIONS; i++) {
1981 Session *s = &sessions[i];
1982 if (s->used && s->ttyfd != -1) {
1983 if (buf[0] != '\0')
1984 strlcat(buf, ",", sizeof buf);
1985 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1986 }
1987 }
1988 if (buf[0] == '\0')
1989 strlcpy(buf, "notty", sizeof buf);
1990 return buf;
1991}
1992
1993void
1994session_proctitle(Session *s)
1995{
1996 if (s->pw == NULL)
1997 error("no user for session %d", s->self);
1998 else
1999 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2000}
2001
Damien Millerefb4afe2000-04-12 18:45:05 +10002002void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002003do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002004{
2005 /*
2006 * Cancel the alarm we set to limit the time taken for
2007 * authentication.
2008 */
2009 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10002010 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10002011 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10002012 startup_pipe = -1;
2013 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00002014#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002015 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10002016 error("unable to get login class");
2017 return;
2018 }
2019#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10002020 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002021 if (xauthfile)
2022 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002023}