blob: 890e16d59c26ef0a6053b63a5195e3de35a15aa8 [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"
Damien Miller0bc1bd82000-11-13 22:57:25 +110036RCSID("$OpenBSD: session.c,v 1.43 2000/11/06 23:04:56 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100037
38#include "xmalloc.h"
39#include "ssh.h"
40#include "pty.h"
41#include "packet.h"
42#include "buffer.h"
Damien Millerb38eff82000-04-01 11:09:21 +100043#include "mpaux.h"
44#include "servconf.h"
45#include "uidswap.h"
46#include "compat.h"
47#include "channels.h"
48#include "nchan.h"
49
Damien Millerefb4afe2000-04-12 18:45:05 +100050#include "bufaux.h"
51#include "ssh2.h"
52#include "auth.h"
Damien Millerf6d9e222000-06-18 14:50:44 +100053#include "auth-options.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100054
Damien Miller91606b12000-06-28 08:22:29 +100055#ifdef WITH_IRIX_PROJECT
56#include <proj.h>
57#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000058#ifdef WITH_IRIX_JOBS
59#include <sys/resource.h>
Ben Lindstrom49a79c02000-11-17 03:47:20 +000060#endif
61#ifdef WITH_IRIX_AUDIT
62#include <sat.h>
63#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100064
Damien Miller37023962000-07-11 17:31:38 +100065#if defined(HAVE_USERSEC_H)
66#include <usersec.h>
67#endif
68
Damien Millerb8c656e2000-06-28 15:22:41 +100069#ifdef HAVE_OSF_SIA
70# include <sia.h>
71# include <siad.h>
72#endif
73
Damien Millerbac2d8a2000-09-05 16:13:06 +110074#ifdef HAVE_CYGWIN
75#include <windows.h>
76#include <sys/cygwin.h>
77#define is_winnt (GetVersion() < 0x80000000)
78#endif
79
Damien Millerd17b8d52000-08-09 14:42:28 +100080/* AIX limits */
81#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100082# define S_UFSIZE_HARD S_UFSIZE "_hard"
83# define S_UCPU_HARD S_UCPU "_hard"
84# define S_UDATA_HARD S_UDATA "_hard"
85# define S_USTACK_HARD S_USTACK "_hard"
86# define S_URSS_HARD S_URSS "_hard"
87# define S_UCORE_HARD S_UCORE "_hard"
88# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100089#endif
90
Damien Millerad833b32000-08-23 10:46:23 +100091#ifdef HAVE_LOGIN_CAP
92#include <login_cap.h>
93#endif
94
Damien Millerb38eff82000-04-01 11:09:21 +100095/* types */
96
97#define TTYSZ 64
98typedef struct Session Session;
99struct Session {
100 int used;
101 int self;
Damien Millerbd483e72000-04-30 10:00:53 +1000102 int extended;
Damien Millerb38eff82000-04-01 11:09:21 +1000103 struct passwd *pw;
104 pid_t pid;
105 /* tty */
106 char *term;
107 int ptyfd, ttyfd, ptymaster;
108 int row, col, xpixel, ypixel;
109 char tty[TTYSZ];
110 /* X11 */
111 char *display;
112 int screen;
113 char *auth_proto;
114 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000115 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000116 /* proto 2 */
117 int chanid;
118};
119
120/* func */
121
122Session *session_new(void);
123void session_set_fds(Session *s, int fdin, int fdout, int fderr);
124void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000125void session_proctitle(Session *s);
Damien Millerb38eff82000-04-01 11:09:21 +1000126void do_exec_pty(Session *s, const char *command, struct passwd * pw);
127void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
Damien Miller69b69aa2000-10-28 14:19:58 +1100128void do_login(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000129
130void
131do_child(const char *command, struct passwd * pw, const char *term,
132 const char *display, const char *auth_proto,
133 const char *auth_data, const char *ttyname);
134
135/* import */
136extern ServerOptions options;
137extern char *__progname;
138extern int log_stderr;
139extern int debug_flag;
Damien Miller942da032000-08-18 13:59:06 +1000140extern unsigned int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000141
Damien Miller37023962000-07-11 17:31:38 +1000142extern int startup_pipe;
143
Damien Millerb38eff82000-04-01 11:09:21 +1000144/* Local Xauthority file. */
145static char *xauthfile;
146
Damien Miller7b28dc52000-09-05 13:34:53 +1100147/* original command from peer. */
148char *original_command = NULL;
149
Damien Millerb38eff82000-04-01 11:09:21 +1000150/* data */
151#define MAX_SESSIONS 10
152Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100153
Damien Millerd2c208a2000-05-17 22:00:02 +1000154#ifdef WITH_AIXAUTHENTICATE
155/* AIX's lastlogin message, set in auth1.c */
156char *aixloginmsg;
157#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000158
Damien Millerad833b32000-08-23 10:46:23 +1000159#ifdef HAVE_LOGIN_CAP
160static login_cap_t *lc;
161#endif
162
Damien Millerb38eff82000-04-01 11:09:21 +1000163/*
164 * Remove local Xauthority file.
165 */
166void
167xauthfile_cleanup_proc(void *ignore)
168{
169 debug("xauthfile_cleanup_proc called");
170
171 if (xauthfile != NULL) {
172 char *p;
173 unlink(xauthfile);
174 p = strrchr(xauthfile, '/');
175 if (p != NULL) {
176 *p = '\0';
177 rmdir(xauthfile);
178 }
179 xfree(xauthfile);
180 xauthfile = NULL;
181 }
182}
183
184/*
185 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
186 * dropped connection).
187 */
Damien Miller4af51302000-04-16 11:18:38 +1000188void
Damien Millerb38eff82000-04-01 11:09:21 +1000189pty_cleanup_proc(void *session)
190{
191 Session *s=session;
192 if (s == NULL)
193 fatal("pty_cleanup_proc: no session");
194 debug("pty_cleanup_proc: %s", s->tty);
195
196 if (s->pid != 0) {
197 /* Record that the user has logged out. */
198 record_logout(s->pid, s->tty);
199 }
200
201 /* Release the pseudo-tty. */
202 pty_release(s->tty);
203}
204
205/*
206 * Prepares for an interactive session. This is called after the user has
207 * been successfully authenticated. During this message exchange, pseudo
208 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
209 * are requested, etc.
210 */
Damien Miller4af51302000-04-16 11:18:38 +1000211void
Damien Millerb38eff82000-04-01 11:09:21 +1000212do_authenticated(struct passwd * pw)
213{
214 Session *s;
Damien Miller7b28dc52000-09-05 13:34:53 +1100215 int type, fd;
Damien Millerb38eff82000-04-01 11:09:21 +1000216 int compression_level = 0, enable_compression_after_reply = 0;
217 int have_pty = 0;
218 char *command;
219 int n_bytes;
220 int plen;
221 unsigned int proto_len, data_len, dlen;
222
223 /*
224 * Cancel the alarm we set to limit the time taken for
225 * authentication.
226 */
227 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +1000228 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +1000229 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +1000230 startup_pipe = -1;
231 }
Damien Millerb38eff82000-04-01 11:09:21 +1000232
233 /*
234 * Inform the channel mechanism that we are the server side and that
235 * the client may request to connect to any port at all. (The user
236 * could do it anyway, and we wouldn\'t know what is permitted except
237 * by the client telling us, so we can equally well trust the client
238 * not to request anything bogus.)
239 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100240 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
Damien Millerb38eff82000-04-01 11:09:21 +1000241 channel_permit_all_opens();
242
243 s = session_new();
Damien Millerbd483e72000-04-30 10:00:53 +1000244 s->pw = pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000245
Kevin Steves48b7cc02000-10-07 13:24:00 +0000246#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +1000247 if ((lc = login_getclass(pw->pw_class)) == NULL) {
248 error("unable to get login class");
249 return;
250 }
251#endif
252
Damien Millerb38eff82000-04-01 11:09:21 +1000253 /*
254 * We stay in this loop until the client requests to execute a shell
255 * or a command.
256 */
257 for (;;) {
258 int success = 0;
259
260 /* Get a packet from the client. */
261 type = packet_read(&plen);
262
263 /* Process the packet. */
264 switch (type) {
265 case SSH_CMSG_REQUEST_COMPRESSION:
266 packet_integrity_check(plen, 4, type);
267 compression_level = packet_get_int();
268 if (compression_level < 1 || compression_level > 9) {
269 packet_send_debug("Received illegal compression level %d.",
270 compression_level);
271 break;
272 }
273 /* Enable compression after we have responded with SUCCESS. */
274 enable_compression_after_reply = 1;
275 success = 1;
276 break;
277
278 case SSH_CMSG_REQUEST_PTY:
279 if (no_pty_flag) {
280 debug("Allocating a pty not permitted for this authentication.");
281 break;
282 }
283 if (have_pty)
284 packet_disconnect("Protocol error: you already have a pty.");
285
286 debug("Allocating pty.");
287
288 /* Allocate a pty and open it. */
289 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
290 sizeof(s->tty))) {
291 error("Failed to allocate pty.");
292 break;
293 }
294 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
295 pty_setowner(pw, s->tty);
296
297 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
298 s->term = packet_get_string(&dlen);
299 packet_integrity_check(dlen, strlen(s->term), type);
300 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
301 /* Remaining bytes */
302 n_bytes = plen - (4 + dlen + 4 * 4);
303
304 if (strcmp(s->term, "") == 0) {
305 xfree(s->term);
306 s->term = NULL;
307 }
308 /* Get window size from the packet. */
309 s->row = packet_get_int();
310 s->col = packet_get_int();
311 s->xpixel = packet_get_int();
312 s->ypixel = packet_get_int();
313 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
314
315 /* Get tty modes from the packet. */
316 tty_parse_modes(s->ttyfd, &n_bytes);
317 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
318
Damien Millere247cc42000-05-07 12:03:14 +1000319 session_proctitle(s);
320
Damien Millerb38eff82000-04-01 11:09:21 +1000321 /* Indicate that we now have a pty. */
322 success = 1;
323 have_pty = 1;
324 break;
325
326 case SSH_CMSG_X11_REQUEST_FORWARDING:
327 if (!options.x11_forwarding) {
328 packet_send_debug("X11 forwarding disabled in server configuration file.");
329 break;
330 }
Damien Miller0c043c12000-06-07 21:22:38 +1000331 if (!options.xauth_location) {
332 packet_send_debug("No xauth program; cannot forward with spoofing.");
333 break;
334 }
Damien Millerb38eff82000-04-01 11:09:21 +1000335 if (no_x11_forwarding_flag) {
336 packet_send_debug("X11 forwarding not permitted for this authentication.");
337 break;
338 }
339 debug("Received request for X11 forwarding with auth spoofing.");
340 if (s->display != NULL)
341 packet_disconnect("Protocol error: X11 display already set.");
342
343 s->auth_proto = packet_get_string(&proto_len);
344 s->auth_data = packet_get_string(&data_len);
345 packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
346
347 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
348 s->screen = packet_get_int();
349 else
350 s->screen = 0;
351 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
352
353 if (s->display == NULL)
354 break;
355
356 /* Setup to always have a local .Xauthority. */
357 xauthfile = xmalloc(MAXPATHLEN);
358 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
359 temporarily_use_uid(pw->pw_uid);
360 if (mkdtemp(xauthfile) == NULL) {
361 restore_uid();
362 error("private X11 dir: mkdtemp %s failed: %s",
363 xauthfile, strerror(errno));
364 xfree(xauthfile);
365 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000366 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000367 break;
368 }
369 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100370 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
371 if (fd >= 0)
372 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000373 restore_uid();
374 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
375 success = 1;
376 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000377
378 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
379 if (no_agent_forwarding_flag || compat13) {
380 debug("Authentication agent forwarding not permitted for this authentication.");
381 break;
382 }
383 debug("Received authentication agent forwarding request.");
Damien Miller0c043c12000-06-07 21:22:38 +1000384 success = auth_input_request_forwarding(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000385 break;
386
387 case SSH_CMSG_PORT_FORWARD_REQUEST:
388 if (no_port_forwarding_flag) {
389 debug("Port forwarding not permitted for this authentication.");
390 break;
391 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100392 if (!options.allow_tcp_forwarding) {
393 debug("Port forwarding not permitted.");
394 break;
395 }
Damien Millerb38eff82000-04-01 11:09:21 +1000396 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000397 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000398 success = 1;
399 break;
400
401 case SSH_CMSG_MAX_PACKET_SIZE:
402 if (packet_set_maxsize(packet_get_int()) > 0)
403 success = 1;
404 break;
405
406 case SSH_CMSG_EXEC_SHELL:
407 case SSH_CMSG_EXEC_CMD:
408 /* Set interactive/non-interactive mode. */
409 packet_set_interactive(have_pty || s->display != NULL,
410 options.keepalives);
411
412 if (type == SSH_CMSG_EXEC_CMD) {
413 command = packet_get_string(&dlen);
414 debug("Exec command '%.500s'", command);
415 packet_integrity_check(plen, 4 + dlen, type);
416 } else {
417 command = NULL;
418 packet_integrity_check(plen, 0, type);
419 }
420 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100421 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000422 command = forced_command;
423 debug("Forced command '%.500s'", forced_command);
424 }
425 if (have_pty)
426 do_exec_pty(s, command, pw);
427 else
428 do_exec_no_pty(s, command, pw);
429
430 if (command != NULL)
431 xfree(command);
432 /* Cleanup user's local Xauthority file. */
433 if (xauthfile)
434 xauthfile_cleanup_proc(NULL);
435 return;
436
437 default:
438 /*
439 * Any unknown messages in this phase are ignored,
440 * and a failure message is returned.
441 */
442 log("Unknown packet type received after authentication: %d", type);
443 }
444 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
445 packet_send();
446 packet_write_wait();
447
448 /* Enable compression now that we have replied if appropriate. */
449 if (enable_compression_after_reply) {
450 enable_compression_after_reply = 0;
451 packet_start_compression(compression_level);
452 }
453 }
454}
455
456/*
457 * This is called to fork and execute a command when we have no tty. This
458 * will call do_child from the child, and server_loop from the parent after
459 * setting up file descriptors and such.
460 */
Damien Miller4af51302000-04-16 11:18:38 +1000461void
Damien Millerb38eff82000-04-01 11:09:21 +1000462do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
463{
464 int pid;
465
466#ifdef USE_PIPES
467 int pin[2], pout[2], perr[2];
468 /* Allocate pipes for communicating with the program. */
469 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
470 packet_disconnect("Could not create pipes: %.100s",
471 strerror(errno));
472#else /* USE_PIPES */
473 int inout[2], err[2];
474 /* Uses socket pairs to communicate with the program. */
475 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
476 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
477 packet_disconnect("Could not create socket pairs: %.100s",
478 strerror(errno));
479#endif /* USE_PIPES */
480 if (s == NULL)
481 fatal("do_exec_no_pty: no session");
482
Damien Millercf3888d2000-09-30 14:17:52 +1100483 signal(SIGPIPE, SIG_DFL);
484
Damien Millere247cc42000-05-07 12:03:14 +1000485 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000486
487#ifdef USE_PAM
488 do_pam_setcred();
489#endif /* USE_PAM */
490
491 /* Fork the child. */
492 if ((pid = fork()) == 0) {
493 /* Child. Reinitialize the log since the pid has changed. */
494 log_init(__progname, options.log_level, options.log_facility, log_stderr);
495
496 /*
497 * Create a new session and process group since the 4.4BSD
498 * setlogin() affects the entire process group.
499 */
500 if (setsid() < 0)
501 error("setsid failed: %.100s", strerror(errno));
502
503#ifdef USE_PIPES
504 /*
505 * Redirect stdin. We close the parent side of the socket
506 * pair, and make the child side the standard input.
507 */
508 close(pin[1]);
509 if (dup2(pin[0], 0) < 0)
510 perror("dup2 stdin");
511 close(pin[0]);
512
513 /* Redirect stdout. */
514 close(pout[0]);
515 if (dup2(pout[1], 1) < 0)
516 perror("dup2 stdout");
517 close(pout[1]);
518
519 /* Redirect stderr. */
520 close(perr[0]);
521 if (dup2(perr[1], 2) < 0)
522 perror("dup2 stderr");
523 close(perr[1]);
524#else /* USE_PIPES */
525 /*
526 * Redirect stdin, stdout, and stderr. Stdin and stdout will
527 * use the same socket, as some programs (particularly rdist)
528 * seem to depend on it.
529 */
530 close(inout[1]);
531 close(err[1]);
532 if (dup2(inout[0], 0) < 0) /* stdin */
533 perror("dup2 stdin");
534 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
535 perror("dup2 stdout");
536 if (dup2(err[0], 2) < 0) /* stderr */
537 perror("dup2 stderr");
538#endif /* USE_PIPES */
539
540 /* Do processing for the child (exec command etc). */
541 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
542 /* NOTREACHED */
543 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100544#ifdef HAVE_CYGWIN
545 if (is_winnt)
546 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
547#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000548 if (pid < 0)
549 packet_disconnect("fork failed: %.100s", strerror(errno));
550 s->pid = pid;
551#ifdef USE_PIPES
552 /* We are the parent. Close the child sides of the pipes. */
553 close(pin[0]);
554 close(pout[1]);
555 close(perr[1]);
556
Damien Millerefb4afe2000-04-12 18:45:05 +1000557 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000558 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000559 } else {
560 /* Enter the interactive session. */
561 server_loop(pid, pin[1], pout[0], perr[0]);
562 /* server_loop has closed pin[1], pout[1], and perr[1]. */
563 }
Damien Millerb38eff82000-04-01 11:09:21 +1000564#else /* USE_PIPES */
565 /* We are the parent. Close the child sides of the socket pairs. */
566 close(inout[0]);
567 close(err[0]);
568
569 /*
570 * Enter the interactive session. Note: server_loop must be able to
571 * handle the case that fdin and fdout are the same.
572 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000573 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000574 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000575 } else {
576 server_loop(pid, inout[1], inout[1], err[1]);
577 /* server_loop has closed inout[1] and err[1]. */
578 }
Damien Millerb38eff82000-04-01 11:09:21 +1000579#endif /* USE_PIPES */
580}
581
582/*
583 * This is called to fork and execute a command when we have a tty. This
584 * will call do_child from the child, and server_loop from the parent after
585 * setting up file descriptors, controlling tty, updating wtmp, utmp,
586 * lastlog, and other such operations.
587 */
Damien Miller4af51302000-04-16 11:18:38 +1000588void
Damien Millerb38eff82000-04-01 11:09:21 +1000589do_exec_pty(Session *s, const char *command, struct passwd * pw)
590{
Damien Millerb38eff82000-04-01 11:09:21 +1000591 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000592 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000593
594 if (s == NULL)
595 fatal("do_exec_pty: no session");
596 ptyfd = s->ptyfd;
597 ttyfd = s->ttyfd;
598
Damien Millerb38eff82000-04-01 11:09:21 +1000599#ifdef USE_PAM
600 do_pam_session(pw->pw_name, s->tty);
601 do_pam_setcred();
602#endif /* USE_PAM */
603
604 /* Fork the child. */
605 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000606 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000607 log_init(__progname, options.log_level, options.log_facility, log_stderr);
608
609 /* Close the master side of the pseudo tty. */
610 close(ptyfd);
611
612 /* Make the pseudo tty our controlling tty. */
613 pty_make_controlling_tty(&ttyfd, s->tty);
614
615 /* Redirect stdin from the pseudo tty. */
616 if (dup2(ttyfd, fileno(stdin)) < 0)
617 error("dup2 stdin failed: %.100s", strerror(errno));
618
619 /* Redirect stdout to the pseudo tty. */
620 if (dup2(ttyfd, fileno(stdout)) < 0)
621 error("dup2 stdin failed: %.100s", strerror(errno));
622
623 /* Redirect stderr to the pseudo tty. */
624 if (dup2(ttyfd, fileno(stderr)) < 0)
625 error("dup2 stdin failed: %.100s", strerror(errno));
626
627 /* Close the extra descriptor for the pseudo tty. */
628 close(ttyfd);
629
Damien Miller942da032000-08-18 13:59:06 +1000630 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100631 if (!(options.use_login && command == NULL))
632 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000633
Damien Millerb38eff82000-04-01 11:09:21 +1000634 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000635 do_child(command, pw, s->term, s->display, s->auth_proto,
636 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000637 /* NOTREACHED */
638 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100639#ifdef HAVE_CYGWIN
640 if (is_winnt)
641 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
642#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000643 if (pid < 0)
644 packet_disconnect("fork failed: %.100s", strerror(errno));
645 s->pid = pid;
646
647 /* Parent. Close the slave side of the pseudo tty. */
648 close(ttyfd);
649
650 /*
651 * Create another descriptor of the pty master side for use as the
652 * standard input. We could use the original descriptor, but this
653 * simplifies code in server_loop. The descriptor is bidirectional.
654 */
655 fdout = dup(ptyfd);
656 if (fdout < 0)
657 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
658
659 /* we keep a reference to the pty master */
660 ptymaster = dup(ptyfd);
661 if (ptymaster < 0)
662 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
663 s->ptymaster = ptymaster;
664
665 /* Enter interactive session. */
Damien Millerefb4afe2000-04-12 18:45:05 +1000666 if (compat20) {
667 session_set_fds(s, ptyfd, fdout, -1);
668 } else {
669 server_loop(pid, ptyfd, fdout, -1);
670 /* server_loop _has_ closed ptyfd and fdout. */
671 session_pty_cleanup(s);
672 }
Damien Millerb38eff82000-04-01 11:09:21 +1000673}
674
Damien Miller942da032000-08-18 13:59:06 +1000675const char *
676get_remote_name_or_ip(void)
677{
678 static const char *remote = "";
679 if (utmp_len > 0)
680 remote = get_canonical_hostname();
681 if (utmp_len == 0 || strlen(remote) > utmp_len)
682 remote = get_remote_ipaddr();
683 return remote;
684}
685
686/* administrative, login(1)-like work */
687void
Damien Miller69b69aa2000-10-28 14:19:58 +1100688do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000689{
690 FILE *f;
691 char *time_string;
692 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100693 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000694 socklen_t fromlen;
695 struct sockaddr_storage from;
696 struct stat st;
697 time_t last_login_time;
698 struct passwd * pw = s->pw;
699 pid_t pid = getpid();
700
701 /*
702 * Get IP address of client. If the connection is not a socket, let
703 * the address be 0.0.0.0.
704 */
705 memset(&from, 0, sizeof(from));
706 if (packet_connection_is_on_socket()) {
707 fromlen = sizeof(from);
708 if (getpeername(packet_get_connection_in(),
709 (struct sockaddr *) & from, &fromlen) < 0) {
710 debug("getpeername: %.100s", strerror(errno));
711 fatal_cleanup();
712 }
713 }
714
Damien Miller7b28dc52000-09-05 13:34:53 +1100715 /* Get the time and hostname when the user last logged in. */
Damien Millere4340be2000-09-16 13:29:08 +1100716 hostname[0] = '\0';
717 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
718 hostname, sizeof(hostname));
719
Damien Miller942da032000-08-18 13:59:06 +1000720 /* Record that there was a login on that tty from the remote host. */
721 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
722 get_remote_name_or_ip(), (struct sockaddr *)&from);
723
Kevin Steves092f2ef2000-10-14 13:36:13 +0000724#ifdef USE_PAM
725 /*
726 * If password change is needed, do it now.
727 * This needs to occur before the ~/.hushlogin check.
728 */
729 if (pam_password_change_required()) {
730 print_pam_messages();
731 do_pam_chauthtok();
732 }
733#endif
734
Damien Miller69b69aa2000-10-28 14:19:58 +1100735 /* Done if .hushlogin exists or a command given. */
736 if (command != NULL)
737 return;
Damien Miller942da032000-08-18 13:59:06 +1000738 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000739#ifdef HAVE_LOGIN_CAP
740 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
741#else
Damien Miller942da032000-08-18 13:59:06 +1000742 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000743#endif
Damien Miller942da032000-08-18 13:59:06 +1000744 return;
745
746#ifdef USE_PAM
Kevin Steves092f2ef2000-10-14 13:36:13 +0000747 if (!pam_password_change_required())
748 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000749#endif /* USE_PAM */
750#ifdef WITH_AIXAUTHENTICATE
751 if (aixloginmsg && *aixloginmsg)
752 printf("%s\n", aixloginmsg);
753#endif /* WITH_AIXAUTHENTICATE */
754
Damien Miller942da032000-08-18 13:59:06 +1000755 if (last_login_time != 0) {
756 time_string = ctime(&last_login_time);
757 if (strchr(time_string, '\n'))
758 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000759 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000760 printf("Last login: %s\r\n", time_string);
761 else
Damien Millere4340be2000-09-16 13:29:08 +1100762 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000763 }
764 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000765#ifdef HAVE_LOGIN_CAP
766 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
767 "/etc/motd"), "r");
768#else
Damien Miller942da032000-08-18 13:59:06 +1000769 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000770#endif
Damien Miller942da032000-08-18 13:59:06 +1000771 if (f) {
772 while (fgets(buf, sizeof(buf), f))
773 fputs(buf, stdout);
774 fclose(f);
775 }
776 }
777}
778
Damien Millerb38eff82000-04-01 11:09:21 +1000779/*
780 * Sets the value of the given variable in the environment. If the variable
781 * already exists, its value is overriden.
782 */
Damien Miller4af51302000-04-16 11:18:38 +1000783void
Damien Millerb38eff82000-04-01 11:09:21 +1000784child_set_env(char ***envp, unsigned int *envsizep, const char *name,
785 const char *value)
786{
787 unsigned int i, namelen;
788 char **env;
789
790 /*
791 * Find the slot where the value should be stored. If the variable
792 * already exists, we reuse the slot; otherwise we append a new slot
793 * at the end of the array, expanding if necessary.
794 */
795 env = *envp;
796 namelen = strlen(name);
797 for (i = 0; env[i]; i++)
798 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
799 break;
800 if (env[i]) {
801 /* Reuse the slot. */
802 xfree(env[i]);
803 } else {
804 /* New variable. Expand if necessary. */
805 if (i >= (*envsizep) - 1) {
806 (*envsizep) += 50;
807 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
808 }
809 /* Need to set the NULL pointer at end of array beyond the new slot. */
810 env[i + 1] = NULL;
811 }
812
813 /* Allocate space and format the variable in the appropriate slot. */
814 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
815 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
816}
817
818/*
819 * Reads environment variables from the given file and adds/overrides them
820 * into the environment. If the file does not exist, this does nothing.
821 * Otherwise, it must consist of empty lines, comments (line starts with '#')
822 * and assignments of the form name=value. No other forms are allowed.
823 */
Damien Miller4af51302000-04-16 11:18:38 +1000824void
Damien Millerb38eff82000-04-01 11:09:21 +1000825read_environment_file(char ***env, unsigned int *envsize,
826 const char *filename)
827{
828 FILE *f;
829 char buf[4096];
830 char *cp, *value;
831
832 f = fopen(filename, "r");
833 if (!f)
834 return;
835
836 while (fgets(buf, sizeof(buf), f)) {
837 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
838 ;
839 if (!*cp || *cp == '#' || *cp == '\n')
840 continue;
841 if (strchr(cp, '\n'))
842 *strchr(cp, '\n') = '\0';
843 value = strchr(cp, '=');
844 if (value == NULL) {
845 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
846 continue;
847 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000848 /*
849 * Replace the equals sign by nul, and advance value to
850 * the value string.
851 */
Damien Millerb38eff82000-04-01 11:09:21 +1000852 *value = '\0';
853 value++;
854 child_set_env(env, envsize, cp, value);
855 }
856 fclose(f);
857}
858
859#ifdef USE_PAM
860/*
861 * Sets any environment variables which have been specified by PAM
862 */
863void do_pam_environment(char ***env, int *envsize)
864{
865 char *equals, var_name[512], var_val[512];
866 char **pam_env;
867 int i;
868
869 if ((pam_env = fetch_pam_environment()) == NULL)
870 return;
871
872 for(i = 0; pam_env[i] != NULL; i++) {
873 if ((equals = strstr(pam_env[i], "=")) == NULL)
874 continue;
875
876 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
877 memset(var_name, '\0', sizeof(var_name));
878 memset(var_val, '\0', sizeof(var_val));
879
880 strncpy(var_name, pam_env[i], equals - pam_env[i]);
881 strcpy(var_val, equals + 1);
882
Damien Millercb5e44a2000-09-29 12:12:36 +1100883 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000884
885 child_set_env(env, envsize, var_name, var_val);
886 }
887 }
888}
889#endif /* USE_PAM */
890
Damien Millercb5e44a2000-09-29 12:12:36 +1100891
892#ifdef HAVE_CYGWIN
893void copy_environment(char ***env, int *envsize)
894{
895 char *equals, var_name[512], var_val[512];
896 int i;
897
898 for(i = 0; environ[i] != NULL; i++) {
899 if ((equals = strstr(environ[i], "=")) == NULL)
900 continue;
901
902 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
903 memset(var_name, '\0', sizeof(var_name));
904 memset(var_val, '\0', sizeof(var_val));
905
906 strncpy(var_name, environ[i], equals - environ[i]);
907 strcpy(var_val, equals + 1);
908
909 debug3("Copy environment: %s=%s", var_name, var_val);
910
911 child_set_env(env, envsize, var_name, var_val);
912 }
913 }
914}
915#endif
916
Damien Miller5fc85652000-07-09 23:53:07 +1000917#if defined(HAVE_GETUSERATTR)
918/*
919 * AIX-specific login initialisation
920 */
921void set_limit(char *user, char *soft, char *hard, int resource, int mult)
922{
923 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000924 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000925
926 getrlimit(resource, &rlim);
927
Damien Miller65964d62000-07-11 09:16:22 +1000928 slim = 0;
929 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
930 if (slim < 0) {
931 rlim.rlim_cur = RLIM_INFINITY;
932 } else if (slim != 0) {
933 /* See the wackiness below */
934 if (rlim.rlim_cur == slim * mult)
935 slim = 0;
936 else
937 rlim.rlim_cur = slim * mult;
938 }
939 }
Damien Miller5fc85652000-07-09 23:53:07 +1000940
Damien Miller65964d62000-07-11 09:16:22 +1000941 hlim = 0;
942 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
943 if (hlim < 0) {
944 rlim.rlim_max = RLIM_INFINITY;
945 } else if (hlim != 0) {
946 rlim.rlim_max = hlim * mult;
947 }
948 }
Damien Miller5fc85652000-07-09 23:53:07 +1000949
Damien Miller65964d62000-07-11 09:16:22 +1000950 /*
951 * XXX For cpu and fsize the soft limit is set to the hard limit
952 * if the hard limit is left at its default value and the soft limit
953 * is changed from its default value, either by requesting it
954 * (slim == 0) or by setting it to the current default. At least
955 * that's how rlogind does it. If you're confused you're not alone.
956 * Bug or feature? AIX 4.3.1.2
957 */
958 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
959 && hlim == 0 && slim != 0)
960 rlim.rlim_max = rlim.rlim_cur;
961 /* A specified hard limit limits the soft limit */
962 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
963 rlim.rlim_cur = rlim.rlim_max;
964 /* A soft limit can increase a hard limit */
965 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000966 rlim.rlim_max = rlim.rlim_cur;
967
968 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000969 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000970}
971
972void set_limits_from_userattr(char *user)
973{
974 int mask;
975 char buf[16];
976
977 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
978 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
979 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
980 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
981 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
982 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
983#if defined(S_UNOFILE)
984 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
985#endif
986
987 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
988 /* Convert decimal to octal */
989 (void) snprintf(buf, sizeof(buf), "%d", mask);
990 if (sscanf(buf, "%o", &mask) == 1)
991 umask(mask);
992 }
993}
994#endif /* defined(HAVE_GETUSERATTR) */
995
Damien Millerb38eff82000-04-01 11:09:21 +1000996/*
997 * Performs common processing for the child, such as setting up the
998 * environment, closing extra file descriptors, setting the user and group
999 * ids, and executing the command or shell.
1000 */
Damien Miller4af51302000-04-16 11:18:38 +10001001void
Damien Millerb38eff82000-04-01 11:09:21 +10001002do_child(const char *command, struct passwd * pw, const char *term,
1003 const char *display, const char *auth_proto,
1004 const char *auth_data, const char *ttyname)
1005{
Damien Miller7b28dc52000-09-05 13:34:53 +11001006 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001007 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001008 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001009 FILE *f = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001010 unsigned int envsize, i;
1011 char **env;
1012 extern char **environ;
1013 struct stat st;
1014 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001015#ifdef WITH_IRIX_PROJECT
1016 prid_t projid;
1017#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001018#ifdef WITH_IRIX_JOBS
1019 jid_t jid = 0;
1020#else
1021#ifdef WITH_IRIX_ARRAY
1022 int jid = 0;
1023#endif /* WITH_IRIX_ARRAY */
1024#endif /* WITH_IRIX_JOBS */
1025
Damien Millerb38eff82000-04-01 11:09:21 +10001026
Damien Millerd3a18572000-06-07 19:55:44 +10001027 /* login(1) is only called if we execute the login shell */
1028 if (options.use_login && command != NULL)
1029 options.use_login = 0;
1030
Damien Millerb38eff82000-04-01 11:09:21 +10001031#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001032 if (!options.use_login) {
1033# ifdef HAVE_LOGIN_CAP
1034 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1035 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1036 _PATH_NOLOGIN), "r");
1037# else /* HAVE_LOGIN_CAP */
1038 if (pw->pw_uid)
1039 f = fopen(_PATH_NOLOGIN, "r");
1040# endif /* HAVE_LOGIN_CAP */
1041 if (f) {
1042 /* /etc/nologin exists. Print its contents and exit. */
1043 while (fgets(buf, sizeof(buf), f))
1044 fputs(buf, stderr);
1045 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001046 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001047 }
Damien Millerb38eff82000-04-01 11:09:21 +10001048 }
1049#endif /* USE_PAM */
1050
Damien Millerad833b32000-08-23 10:46:23 +10001051 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001052 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1053 switch, so we let login(1) to this for us. */
1054 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001055#ifdef HAVE_OSF_SIA
1056 extern char **saved_argv;
1057 extern int saved_argc;
1058 char *host = get_canonical_hostname ();
1059
1060 if (sia_become_user(NULL, saved_argc, saved_argv, host,
1061 pw->pw_name, ttyname, 0, NULL, NULL, SIA_BEU_SETLUID) !=
1062 SIASUCCESS) {
1063 perror("sia_become_user");
1064 exit(1);
1065 }
1066 if (setreuid(geteuid(), geteuid()) < 0) {
1067 perror("setreuid");
1068 exit(1);
1069 }
1070#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001071#ifdef HAVE_CYGWIN
1072 if (is_winnt) {
1073#else
Damien Millerb38eff82000-04-01 11:09:21 +10001074 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001075#endif
Damien Millerad833b32000-08-23 10:46:23 +10001076# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001077 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001078# endif /* HAVE_GETUSERATTR */
1079# ifdef HAVE_LOGIN_CAP
1080 if (setusercontext(lc, pw, pw->pw_uid,
1081 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1082 perror("unable to set user context");
1083 exit(1);
1084 }
1085# else /* HAVE_LOGIN_CAP */
1086 if (setlogin(pw->pw_name) < 0)
1087 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001088 if (setgid(pw->pw_gid) < 0) {
1089 perror("setgid");
1090 exit(1);
1091 }
1092 /* Initialize the group list. */
1093 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1094 perror("initgroups");
1095 exit(1);
1096 }
1097 endgrent();
Ben Lindstrom980754c2000-11-12 00:04:24 +00001098# ifdef WITH_IRIX_JOBS
1099 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1100 if (jid == -1) {
1101 fatal("Failed to create job container: %.100s",
1102 strerror(errno));
1103 }
1104# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001105# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001106 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001107 if (jid == 0) {
1108 if (newarraysess() != 0)
1109 fatal("Failed to set up new array session: %.100s",
1110 strerror(errno));
1111 }
Damien Millerad833b32000-08-23 10:46:23 +10001112# endif /* WITH_IRIX_ARRAY */
1113# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001114 /* initialize irix project info */
1115 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1116 debug("Failed to get project id, using projid 0");
1117 projid = 0;
1118 }
Damien Miller91606b12000-06-28 08:22:29 +10001119 if (setprid(projid))
1120 fatal("Failed to initialize project %d for %s: %.100s",
1121 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001122# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001123#ifdef WITH_IRIX_AUDIT
1124 if (sysconf(_SC_AUDIT)) {
1125 debug("Setting sat id to %d", (int) pw->pw_uid);
1126 if (satsetid(pw->pw_uid))
1127 debug("error setting satid: %.100s", strerror(errno));
1128 }
1129#endif /* WITH_IRIX_AUDIT */
1130
Damien Millerb38eff82000-04-01 11:09:21 +10001131 /* Permanently switch to the desired uid. */
1132 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001133# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001134 }
Damien Millerad833b32000-08-23 10:46:23 +10001135#endif /* HAVE_OSF_SIA */
1136
Damien Millerbac2d8a2000-09-05 16:13:06 +11001137#ifdef HAVE_CYGWIN
1138 if (is_winnt)
1139#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001140 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001141 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001142 }
1143 /*
1144 * Get the shell from the password data. An empty shell field is
1145 * legal, and means /bin/sh.
1146 */
1147 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001148#ifdef HAVE_LOGIN_CAP
1149 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1150#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001151
1152#ifdef AFS
1153 /* Try to get AFS tokens for the local cell. */
1154 if (k_hasafs()) {
1155 char cell[64];
1156
1157 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1158 krb_afslog(cell, 0);
1159
1160 krb_afslog(0, 0);
1161 }
1162#endif /* AFS */
1163
1164 /* Initialize the environment. */
1165 envsize = 100;
1166 env = xmalloc(envsize * sizeof(char *));
1167 env[0] = NULL;
1168
Damien Millerbac2d8a2000-09-05 16:13:06 +11001169#ifdef HAVE_CYGWIN
1170 /*
1171 * The Windows environment contains some setting which are
1172 * important for a running system. They must not be dropped.
1173 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001174 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001175#endif
1176
Damien Millerb38eff82000-04-01 11:09:21 +10001177 if (!options.use_login) {
1178 /* Set basic environment. */
1179 child_set_env(&env, &envsize, "USER", pw->pw_name);
1180 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1181 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001182#ifdef HAVE_LOGIN_CAP
1183 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1184 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001185#else /* HAVE_LOGIN_CAP */
1186# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001187 /*
1188 * There's no standard path on Windows. The path contains
1189 * important components pointing to the system directories,
1190 * needed for loading shared libraries. So the path better
1191 * remains intact here.
1192 */
Damien Millerb38eff82000-04-01 11:09:21 +10001193 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001194# endif /* HAVE_CYGWIN */
1195#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001196
1197 snprintf(buf, sizeof buf, "%.200s/%.50s",
1198 _PATH_MAILDIR, pw->pw_name);
1199 child_set_env(&env, &envsize, "MAIL", buf);
1200
1201 /* Normal systems set SHELL by default. */
1202 child_set_env(&env, &envsize, "SHELL", shell);
1203 }
1204 if (getenv("TZ"))
1205 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1206
1207 /* Set custom environment options from RSA authentication. */
1208 while (custom_environment) {
1209 struct envstring *ce = custom_environment;
1210 char *s = ce->s;
1211 int i;
1212 for (i = 0; s[i] != '=' && s[i]; i++);
1213 if (s[i] == '=') {
1214 s[i] = 0;
1215 child_set_env(&env, &envsize, s, s + i + 1);
1216 }
1217 custom_environment = ce->next;
1218 xfree(ce->s);
1219 xfree(ce);
1220 }
1221
1222 snprintf(buf, sizeof buf, "%.50s %d %d",
1223 get_remote_ipaddr(), get_remote_port(), get_local_port());
1224 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1225
1226 if (ttyname)
1227 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1228 if (term)
1229 child_set_env(&env, &envsize, "TERM", term);
1230 if (display)
1231 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001232 if (original_command)
1233 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1234 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001235
1236#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001237 if ((cp = getenv("AUTHSTATE")) != NULL)
1238 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1239 if ((cp = getenv("KRB5CCNAME")) != NULL)
1240 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1241 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001242#endif
1243
1244#ifdef KRB4
1245 {
1246 extern char *ticket;
1247
1248 if (ticket)
1249 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1250 }
1251#endif /* KRB4 */
1252
1253#ifdef USE_PAM
1254 /* Pull in any environment variables that may have been set by PAM. */
1255 do_pam_environment(&env, &envsize);
1256#endif /* USE_PAM */
1257
Damien Millerb38eff82000-04-01 11:09:21 +10001258 if (xauthfile)
1259 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1260 if (auth_get_socket_name() != NULL)
1261 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1262 auth_get_socket_name());
1263
1264 /* read $HOME/.ssh/environment. */
1265 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001266 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1267 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001268 read_environment_file(&env, &envsize, buf);
1269 }
1270 if (debug_flag) {
1271 /* dump the environment */
1272 fprintf(stderr, "Environment:\n");
1273 for (i = 0; env[i]; i++)
1274 fprintf(stderr, " %.200s\n", env[i]);
1275 }
Damien Millerad833b32000-08-23 10:46:23 +10001276 /* we have to stash the hostname before we close our socket. */
1277 if (options.use_login)
1278 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001279 /*
1280 * Close the connection descriptors; note that this is the child, and
1281 * the server will still have the socket open, and it is important
1282 * that we do not shutdown it. Note that the descriptors cannot be
1283 * closed before building the environment, as we call
1284 * get_remote_ipaddr there.
1285 */
1286 if (packet_get_connection_in() == packet_get_connection_out())
1287 close(packet_get_connection_in());
1288 else {
1289 close(packet_get_connection_in());
1290 close(packet_get_connection_out());
1291 }
1292 /*
1293 * Close all descriptors related to channels. They will still remain
1294 * open in the parent.
1295 */
1296 /* XXX better use close-on-exec? -markus */
1297 channel_close_all();
1298
1299 /*
1300 * Close any extra file descriptors. Note that there may still be
1301 * descriptors left by system functions. They will be closed later.
1302 */
1303 endpwent();
1304
1305 /*
1306 * Close any extra open file descriptors so that we don\'t have them
1307 * hanging around in clients. Note that we want to do this after
1308 * initgroups, because at least on Solaris 2.3 it leaves file
1309 * descriptors open.
1310 */
1311 for (i = 3; i < 64; i++)
1312 close(i);
1313
1314 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001315 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001316 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1317 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001318#ifdef HAVE_LOGIN_CAP
1319 if (login_getcapbool(lc, "requirehome", 0))
1320 exit(1);
1321#endif
1322 }
Damien Millerb38eff82000-04-01 11:09:21 +10001323
1324 /*
1325 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1326 * xauth are run in the proper environment.
1327 */
1328 environ = env;
1329
1330 /*
1331 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1332 * in this order).
1333 */
1334 if (!options.use_login) {
1335 if (stat(SSH_USER_RC, &st) >= 0) {
1336 if (debug_flag)
Damien Miller7b413d22000-07-01 13:24:21 +10001337 fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001338
Damien Miller7b413d22000-07-01 13:24:21 +10001339 f = popen(_PATH_BSHELL " " SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001340 if (f) {
1341 if (auth_proto != NULL && auth_data != NULL)
1342 fprintf(f, "%s %s\n", auth_proto, auth_data);
1343 pclose(f);
1344 } else
1345 fprintf(stderr, "Could not run %s\n", SSH_USER_RC);
1346 } else if (stat(SSH_SYSTEM_RC, &st) >= 0) {
1347 if (debug_flag)
Damien Miller7b413d22000-07-01 13:24:21 +10001348 fprintf(stderr, "Running "_PATH_BSHELL" %s\n", SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001349
Damien Miller7b413d22000-07-01 13:24:21 +10001350 f = popen(_PATH_BSHELL " " SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001351 if (f) {
1352 if (auth_proto != NULL && auth_data != NULL)
1353 fprintf(f, "%s %s\n", auth_proto, auth_data);
1354 pclose(f);
1355 } else
1356 fprintf(stderr, "Could not run %s\n", SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001357 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001358 /* Add authority data to .Xauthority if appropriate. */
1359 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001360 char *screen = strchr(display, ':');
1361 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001362 fprintf(stderr,
1363 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001364 options.xauth_location, display,
1365 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001366#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001367 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001368 fprintf(stderr,
1369 "Adding %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001370 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001371 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001372#endif
Damien Millerd999ae22000-05-20 12:49:31 +10001373 }
Damien Miller0c043c12000-06-07 21:22:38 +10001374 snprintf(cmd, sizeof cmd, "%s -q -",
1375 options.xauth_location);
1376 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001377 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001378 fprintf(f, "add %s %s %s\n", display,
1379 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001380#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001381 if (screen != NULL)
1382 fprintf(f, "add %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001383 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001384 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001385#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001386 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001387 } else {
1388 fprintf(stderr, "Could not run %s\n",
1389 cmd);
1390 }
Damien Millerb38eff82000-04-01 11:09:21 +10001391 }
1392 }
Damien Millerb38eff82000-04-01 11:09:21 +10001393 /* Get the last component of the shell name. */
1394 cp = strrchr(shell, '/');
1395 if (cp)
1396 cp++;
1397 else
1398 cp = shell;
1399 }
1400 /*
1401 * If we have no command, execute the shell. In this case, the shell
1402 * name to be passed in argv[0] is preceded by '-' to indicate that
1403 * this is a login shell.
1404 */
1405 if (!command) {
1406 if (!options.use_login) {
1407 char buf[256];
1408
1409 /*
1410 * Check for mail if we have a tty and it was enabled
1411 * in server options.
1412 */
1413 if (ttyname && options.check_mail) {
1414 char *mailbox;
1415 struct stat mailstat;
1416 mailbox = getenv("MAIL");
1417 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001418 if (stat(mailbox, &mailstat) != 0 ||
1419 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001420 printf("No mail.\n");
1421 else if (mailstat.st_mtime < mailstat.st_atime)
1422 printf("You have mail.\n");
1423 else
1424 printf("You have new mail.\n");
1425 }
1426 }
1427 /* Start the shell. Set initial character to '-'. */
1428 buf[0] = '-';
1429 strncpy(buf + 1, cp, sizeof(buf) - 1);
1430 buf[sizeof(buf) - 1] = 0;
1431
1432 /* Execute the shell. */
1433 argv[0] = buf;
1434 argv[1] = NULL;
1435 execve(shell, argv, env);
1436
1437 /* Executing the shell failed. */
1438 perror(shell);
1439 exit(1);
1440
1441 } else {
1442 /* Launch login(1). */
1443
Damien Millerad833b32000-08-23 10:46:23 +10001444 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001445 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001446
1447 /* Login couldn't be executed, die. */
1448
1449 perror("login");
1450 exit(1);
1451 }
1452 }
1453 /*
1454 * Execute the command using the user's shell. This uses the -c
1455 * option to execute the command.
1456 */
1457 argv[0] = (char *) cp;
1458 argv[1] = "-c";
1459 argv[2] = (char *) command;
1460 argv[3] = NULL;
1461 execve(shell, argv, env);
1462 perror(shell);
1463 exit(1);
1464}
1465
1466Session *
1467session_new(void)
1468{
1469 int i;
1470 static int did_init = 0;
1471 if (!did_init) {
1472 debug("session_new: init");
1473 for(i = 0; i < MAX_SESSIONS; i++) {
1474 sessions[i].used = 0;
1475 sessions[i].self = i;
1476 }
1477 did_init = 1;
1478 }
1479 for(i = 0; i < MAX_SESSIONS; i++) {
1480 Session *s = &sessions[i];
1481 if (! s->used) {
1482 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001483 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001484 s->chanid = -1;
1485 s->ptyfd = -1;
1486 s->ttyfd = -1;
1487 s->term = NULL;
1488 s->pw = NULL;
1489 s->display = NULL;
1490 s->screen = 0;
1491 s->auth_data = NULL;
1492 s->auth_proto = NULL;
1493 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001494 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001495 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001496 return s;
1497 }
1498 }
1499 return NULL;
1500}
1501
1502void
1503session_dump(void)
1504{
1505 int i;
1506 for(i = 0; i < MAX_SESSIONS; i++) {
1507 Session *s = &sessions[i];
1508 debug("dump: used %d session %d %p channel %d pid %d",
1509 s->used,
1510 s->self,
1511 s,
1512 s->chanid,
1513 s->pid);
1514 }
1515}
1516
Damien Millerefb4afe2000-04-12 18:45:05 +10001517int
1518session_open(int chanid)
1519{
1520 Session *s = session_new();
1521 debug("session_open: channel %d", chanid);
1522 if (s == NULL) {
1523 error("no more sessions");
1524 return 0;
1525 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001526 s->pw = auth_get_user();
1527 if (s->pw == NULL)
Damien Millerbd483e72000-04-30 10:00:53 +10001528 fatal("no user for session %i", s->self);
1529 debug("session_open: session %d: link with channel %d", s->self, chanid);
1530 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001531 return 1;
1532}
1533
1534Session *
1535session_by_channel(int id)
1536{
1537 int i;
1538 for(i = 0; i < MAX_SESSIONS; i++) {
1539 Session *s = &sessions[i];
1540 if (s->used && s->chanid == id) {
1541 debug("session_by_channel: session %d channel %d", i, id);
1542 return s;
1543 }
1544 }
1545 debug("session_by_channel: unknown channel %d", id);
1546 session_dump();
1547 return NULL;
1548}
1549
1550Session *
1551session_by_pid(pid_t pid)
1552{
1553 int i;
1554 debug("session_by_pid: pid %d", pid);
1555 for(i = 0; i < MAX_SESSIONS; i++) {
1556 Session *s = &sessions[i];
1557 if (s->used && s->pid == pid)
1558 return s;
1559 }
1560 error("session_by_pid: unknown pid %d", pid);
1561 session_dump();
1562 return NULL;
1563}
1564
1565int
1566session_window_change_req(Session *s)
1567{
1568 s->col = packet_get_int();
1569 s->row = packet_get_int();
1570 s->xpixel = packet_get_int();
1571 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001572 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001573 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1574 return 1;
1575}
1576
1577int
1578session_pty_req(Session *s)
1579{
1580 unsigned int len;
Damien Miller4af51302000-04-16 11:18:38 +10001581 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001582
Damien Millerf6d9e222000-06-18 14:50:44 +10001583 if (no_pty_flag)
1584 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001585 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001586 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001587 s->term = packet_get_string(&len);
1588 s->col = packet_get_int();
1589 s->row = packet_get_int();
1590 s->xpixel = packet_get_int();
1591 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001592 term_modes = packet_get_string(&len);
1593 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001594
1595 if (strcmp(s->term, "") == 0) {
1596 xfree(s->term);
1597 s->term = NULL;
1598 }
1599 /* Allocate a pty and open it. */
1600 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1601 xfree(s->term);
1602 s->term = NULL;
1603 s->ptyfd = -1;
1604 s->ttyfd = -1;
1605 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001606 xfree(term_modes);
1607 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001608 }
1609 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1610 /*
1611 * Add a cleanup function to clear the utmp entry and record logout
1612 * time in case we call fatal() (e.g., the connection gets closed).
1613 */
1614 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1615 pty_setowner(s->pw, s->tty);
1616 /* Get window size from the packet. */
1617 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1618
Damien Millere247cc42000-05-07 12:03:14 +10001619 session_proctitle(s);
1620
Damien Miller5f056372000-04-16 12:31:48 +10001621 /* XXX parse and set terminal modes */
1622 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001623 return 1;
1624}
1625
Damien Millerbd483e72000-04-30 10:00:53 +10001626int
1627session_subsystem_req(Session *s)
1628{
1629 unsigned int len;
1630 int success = 0;
1631 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001632 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001633
1634 packet_done();
1635 log("subsystem request for %s", subsys);
1636
Damien Millerf6d9e222000-06-18 14:50:44 +10001637 for (i = 0; i < options.num_subsystems; i++) {
1638 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1639 debug("subsystem: exec() %s", options.subsystem_command[i]);
1640 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1641 success = 1;
1642 }
1643 }
1644
1645 if (!success)
1646 log("subsystem request for %s failed, subsystem not found", subsys);
1647
Damien Millerbd483e72000-04-30 10:00:53 +10001648 xfree(subsys);
1649 return success;
1650}
1651
1652int
1653session_x11_req(Session *s)
1654{
Damien Miller7b28dc52000-09-05 13:34:53 +11001655 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001656 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001657 debug("X11 forwarding disabled in user configuration file.");
1658 return 0;
1659 }
Damien Millerbd483e72000-04-30 10:00:53 +10001660 if (!options.x11_forwarding) {
1661 debug("X11 forwarding disabled in server configuration file.");
1662 return 0;
1663 }
1664 if (xauthfile != NULL) {
1665 debug("X11 fwd already started.");
1666 return 0;
1667 }
1668
1669 debug("Received request for X11 forwarding with auth spoofing.");
1670 if (s->display != NULL)
1671 packet_disconnect("Protocol error: X11 display already set.");
1672
1673 s->single_connection = packet_get_char();
1674 s->auth_proto = packet_get_string(NULL);
1675 s->auth_data = packet_get_string(NULL);
1676 s->screen = packet_get_int();
1677 packet_done();
1678
1679 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1680 if (s->display == NULL) {
1681 xfree(s->auth_proto);
1682 xfree(s->auth_data);
1683 return 0;
1684 }
1685 xauthfile = xmalloc(MAXPATHLEN);
1686 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1687 temporarily_use_uid(s->pw->pw_uid);
1688 if (mkdtemp(xauthfile) == NULL) {
1689 restore_uid();
1690 error("private X11 dir: mkdtemp %s failed: %s",
1691 xauthfile, strerror(errno));
1692 xfree(xauthfile);
1693 xauthfile = NULL;
1694 xfree(s->auth_proto);
1695 xfree(s->auth_data);
1696 /* XXXX remove listening channels */
1697 return 0;
1698 }
1699 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001700 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1701 if (fd >= 0)
1702 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001703 restore_uid();
1704 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1705 return 1;
1706}
1707
Damien Millerf6d9e222000-06-18 14:50:44 +10001708int
1709session_shell_req(Session *s)
1710{
1711 /* if forced_command == NULL, the shell is execed */
1712 char *shell = forced_command;
1713 packet_done();
1714 s->extended = 1;
1715 if (s->ttyfd == -1)
1716 do_exec_no_pty(s, shell, s->pw);
1717 else
1718 do_exec_pty(s, shell, s->pw);
1719 return 1;
1720}
1721
1722int
1723session_exec_req(Session *s)
1724{
1725 unsigned int len;
1726 char *command = packet_get_string(&len);
1727 packet_done();
1728 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001729 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001730 command = forced_command;
1731 debug("Forced command '%.500s'", forced_command);
1732 }
1733 s->extended = 1;
1734 if (s->ttyfd == -1)
1735 do_exec_no_pty(s, command, s->pw);
1736 else
1737 do_exec_pty(s, command, s->pw);
1738 if (forced_command == NULL)
1739 xfree(command);
1740 return 1;
1741}
1742
Damien Miller0bc1bd82000-11-13 22:57:25 +11001743int
1744session_auth_agent_req(Session *s)
1745{
1746 static int called = 0;
1747 packet_done();
1748 if (called) {
1749 return 0;
1750 } else {
1751 called = 1;
1752 return auth_input_request_forwarding(s->pw);
1753 }
1754}
1755
Damien Millerefb4afe2000-04-12 18:45:05 +10001756void
1757session_input_channel_req(int id, void *arg)
1758{
1759 unsigned int len;
1760 int reply;
1761 int success = 0;
1762 char *rtype;
1763 Session *s;
1764 Channel *c;
1765
1766 rtype = packet_get_string(&len);
1767 reply = packet_get_char();
1768
1769 s = session_by_channel(id);
1770 if (s == NULL)
1771 fatal("session_input_channel_req: channel %d: no session", id);
1772 c = channel_lookup(id);
1773 if (c == NULL)
1774 fatal("session_input_channel_req: channel %d: bad channel", id);
1775
1776 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1777 s->self, id, rtype, reply);
1778
1779 /*
1780 * a session is in LARVAL state until a shell
1781 * or programm is executed
1782 */
1783 if (c->type == SSH_CHANNEL_LARVAL) {
1784 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001785 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001786 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001787 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001788 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001789 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001790 } else if (strcmp(rtype, "x11-req") == 0) {
1791 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001792 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1793 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001794 } else if (strcmp(rtype, "subsystem") == 0) {
1795 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001796 }
1797 }
1798 if (strcmp(rtype, "window-change") == 0) {
1799 success = session_window_change_req(s);
1800 }
1801
1802 if (reply) {
1803 packet_start(success ?
1804 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1805 packet_put_int(c->remote_id);
1806 packet_send();
1807 }
1808 xfree(rtype);
1809}
1810
1811void
1812session_set_fds(Session *s, int fdin, int fdout, int fderr)
1813{
1814 if (!compat20)
1815 fatal("session_set_fds: called for proto != 2.0");
1816 /*
1817 * now that have a child and a pipe to the child,
1818 * we can activate our channel and register the fd's
1819 */
1820 if (s->chanid == -1)
1821 fatal("no channel for session %d", s->self);
1822 channel_set_fds(s->chanid,
1823 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001824 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1825 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001826}
1827
Damien Millerb38eff82000-04-01 11:09:21 +10001828void
1829session_pty_cleanup(Session *s)
1830{
1831 if (s == NULL || s->ttyfd == -1)
1832 return;
1833
1834 debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1835
1836 /* Cancel the cleanup function. */
1837 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1838
1839 /* Record that the user has logged out. */
1840 record_logout(s->pid, s->tty);
1841
1842 /* Release the pseudo-tty. */
1843 pty_release(s->tty);
1844
1845 /*
1846 * Close the server side of the socket pairs. We must do this after
1847 * the pty cleanup, so that another process doesn't get this pty
1848 * while we're still cleaning up.
1849 */
1850 if (close(s->ptymaster) < 0)
1851 error("close(s->ptymaster): %s", strerror(errno));
1852}
Damien Millerefb4afe2000-04-12 18:45:05 +10001853
1854void
1855session_exit_message(Session *s, int status)
1856{
1857 Channel *c;
1858 if (s == NULL)
1859 fatal("session_close: no session");
1860 c = channel_lookup(s->chanid);
1861 if (c == NULL)
1862 fatal("session_close: session %d: no channel %d",
1863 s->self, s->chanid);
1864 debug("session_exit_message: session %d channel %d pid %d",
1865 s->self, s->chanid, s->pid);
1866
1867 if (WIFEXITED(status)) {
1868 channel_request_start(s->chanid,
1869 "exit-status", 0);
1870 packet_put_int(WEXITSTATUS(status));
1871 packet_send();
1872 } else if (WIFSIGNALED(status)) {
1873 channel_request_start(s->chanid,
1874 "exit-signal", 0);
1875 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001876#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001877 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001878#else /* WCOREDUMP */
1879 packet_put_char(0);
1880#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001881 packet_put_cstring("");
1882 packet_put_cstring("");
1883 packet_send();
1884 } else {
1885 /* Some weird exit cause. Just exit. */
1886 packet_disconnect("wait returned status %04x.", status);
1887 }
1888
1889 /* disconnect channel */
1890 debug("session_exit_message: release channel %d", s->chanid);
1891 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001892 /*
1893 * emulate a write failure with 'chan_write_failed', nobody will be
1894 * interested in data we write.
1895 * Note that we must not call 'chan_read_failed', since there could
1896 * be some more data waiting in the pipe.
Damien Miller59939352000-10-15 12:21:32 +11001897 * djm - This is no longer true as we have allowed one pass through
1898 * the select loop before killing the connection
Damien Miller166fca82000-04-20 07:42:21 +10001899 */
Damien Millerbd483e72000-04-30 10:00:53 +10001900 if (c->ostate != CHAN_OUTPUT_CLOSED)
1901 chan_write_failed(c);
Damien Miller59939352000-10-15 12:21:32 +11001902 if (c->istate != CHAN_INPUT_CLOSED)
1903 chan_read_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001904 s->chanid = -1;
1905}
1906
1907void
1908session_free(Session *s)
1909{
1910 debug("session_free: session %d pid %d", s->self, s->pid);
1911 if (s->term)
1912 xfree(s->term);
1913 if (s->display)
1914 xfree(s->display);
1915 if (s->auth_data)
1916 xfree(s->auth_data);
1917 if (s->auth_proto)
1918 xfree(s->auth_proto);
1919 s->used = 0;
1920}
1921
1922void
1923session_close(Session *s)
1924{
1925 session_pty_cleanup(s);
1926 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001927 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001928}
1929
1930void
1931session_close_by_pid(pid_t pid, int status)
1932{
1933 Session *s = session_by_pid(pid);
1934 if (s == NULL) {
1935 debug("session_close_by_pid: no session for pid %d", s->pid);
1936 return;
1937 }
1938 if (s->chanid != -1)
1939 session_exit_message(s, status);
1940 session_close(s);
1941}
1942
1943/*
1944 * this is called when a channel dies before
1945 * the session 'child' itself dies
1946 */
1947void
1948session_close_by_channel(int id, void *arg)
1949{
1950 Session *s = session_by_channel(id);
1951 if (s == NULL) {
1952 debug("session_close_by_channel: no session for channel %d", id);
1953 return;
1954 }
1955 /* disconnect channel */
1956 channel_cancel_cleanup(s->chanid);
1957 s->chanid = -1;
1958
1959 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1960 if (s->pid == 0) {
1961 /* close session immediately */
1962 session_close(s);
1963 } else {
1964 /* notify child, delay session cleanup */
Damien Miller099f5052000-06-22 20:57:11 +10001965 if (s->pid <= 1)
Damien Miller7a445bb2000-06-26 13:12:37 +10001966 fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
1967 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001968 error("session_close_by_channel: kill %d: %s",
1969 s->pid, strerror(errno));
1970 }
1971}
1972
Damien Millere247cc42000-05-07 12:03:14 +10001973char *
1974session_tty_list(void)
1975{
1976 static char buf[1024];
1977 int i;
1978 buf[0] = '\0';
1979 for(i = 0; i < MAX_SESSIONS; i++) {
1980 Session *s = &sessions[i];
1981 if (s->used && s->ttyfd != -1) {
1982 if (buf[0] != '\0')
1983 strlcat(buf, ",", sizeof buf);
1984 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1985 }
1986 }
1987 if (buf[0] == '\0')
1988 strlcpy(buf, "notty", sizeof buf);
1989 return buf;
1990}
1991
1992void
1993session_proctitle(Session *s)
1994{
1995 if (s->pw == NULL)
1996 error("no user for session %d", s->self);
1997 else
1998 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1999}
2000
Damien Millerefb4afe2000-04-12 18:45:05 +10002001void
2002do_authenticated2(void)
2003{
Damien Miller87d29ed2000-08-30 09:21:22 +11002004#ifdef HAVE_LOGIN_CAP
Damien Millerad833b32000-08-23 10:46:23 +10002005 struct passwd *pw;
Damien Miller87d29ed2000-08-30 09:21:22 +11002006#endif
Damien Millerad833b32000-08-23 10:46:23 +10002007
Damien Millerefb4afe2000-04-12 18:45:05 +10002008 /*
2009 * Cancel the alarm we set to limit the time taken for
2010 * authentication.
2011 */
2012 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10002013 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10002014 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10002015 startup_pipe = -1;
2016 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00002017#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +10002018 pw = auth_get_user();
2019 if ((lc = login_getclass(pw->pw_class)) == NULL) {
2020 error("unable to get login class");
2021 return;
2022 }
2023#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10002024 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002025 if (xauthfile)
2026 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002027}