blob: b6ab8873197e10da66115794f47e26bface65570 [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 Lindstrom226cfa02001-01-22 05:34:40 +000036RCSID("$OpenBSD: session.c,v 1.51 2001/01/21 19:05:56 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"
Damien Millerb38eff82000-04-01 11:09:21 +100042#include "pty.h"
43#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"
56#include "login.h"
57#include "serverloop.h"
58#include "canohost.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100059
Damien Miller91606b12000-06-28 08:22:29 +100060#ifdef WITH_IRIX_PROJECT
61#include <proj.h>
62#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +000063#ifdef WITH_IRIX_JOBS
64#include <sys/resource.h>
Ben Lindstrom49a79c02000-11-17 03:47:20 +000065#endif
66#ifdef WITH_IRIX_AUDIT
67#include <sat.h>
68#endif /* WITH_IRIX_AUDIT */
Damien Miller91606b12000-06-28 08:22:29 +100069
Damien Miller37023962000-07-11 17:31:38 +100070#if defined(HAVE_USERSEC_H)
71#include <usersec.h>
72#endif
73
Damien Millerb8c656e2000-06-28 15:22:41 +100074#ifdef HAVE_OSF_SIA
75# include <sia.h>
76# include <siad.h>
77#endif
78
Damien Millerbac2d8a2000-09-05 16:13:06 +110079#ifdef HAVE_CYGWIN
80#include <windows.h>
81#include <sys/cygwin.h>
82#define is_winnt (GetVersion() < 0x80000000)
83#endif
84
Damien Millerd17b8d52000-08-09 14:42:28 +100085/* AIX limits */
86#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
Damien Miller0da2eaa2000-08-15 11:32:59 +100087# define S_UFSIZE_HARD S_UFSIZE "_hard"
88# define S_UCPU_HARD S_UCPU "_hard"
89# define S_UDATA_HARD S_UDATA "_hard"
90# define S_USTACK_HARD S_USTACK "_hard"
91# define S_URSS_HARD S_URSS "_hard"
92# define S_UCORE_HARD S_UCORE "_hard"
93# define S_UNOFILE_HARD S_UNOFILE "_hard"
Damien Millerd17b8d52000-08-09 14:42:28 +100094#endif
95
Damien Millerad833b32000-08-23 10:46:23 +100096#ifdef HAVE_LOGIN_CAP
97#include <login_cap.h>
98#endif
99
Damien Millerb38eff82000-04-01 11:09:21 +1000100/* types */
101
102#define TTYSZ 64
103typedef struct Session Session;
104struct Session {
105 int used;
106 int self;
Damien Millerbd483e72000-04-30 10:00:53 +1000107 int extended;
Damien Millerb38eff82000-04-01 11:09:21 +1000108 struct passwd *pw;
109 pid_t pid;
110 /* tty */
111 char *term;
112 int ptyfd, ttyfd, ptymaster;
113 int row, col, xpixel, ypixel;
114 char tty[TTYSZ];
115 /* X11 */
116 char *display;
117 int screen;
118 char *auth_proto;
119 char *auth_data;
Damien Millerbd483e72000-04-30 10:00:53 +1000120 int single_connection;
Damien Millerb38eff82000-04-01 11:09:21 +1000121 /* proto 2 */
122 int chanid;
123};
124
125/* func */
126
127Session *session_new(void);
128void session_set_fds(Session *s, int fdin, int fdout, int fderr);
129void session_pty_cleanup(Session *s);
Damien Millere247cc42000-05-07 12:03:14 +1000130void session_proctitle(Session *s);
Damien Millerb38eff82000-04-01 11:09:21 +1000131void do_exec_pty(Session *s, const char *command, struct passwd * pw);
132void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
Damien Miller69b69aa2000-10-28 14:19:58 +1100133void do_login(Session *s, const char *command);
Damien Millerb38eff82000-04-01 11:09:21 +1000134
135void
136do_child(const char *command, struct passwd * pw, const char *term,
137 const char *display, const char *auth_proto,
138 const char *auth_data, const char *ttyname);
139
140/* import */
141extern ServerOptions options;
142extern char *__progname;
143extern int log_stderr;
144extern int debug_flag;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000145extern u_int utmp_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000146
Damien Miller37023962000-07-11 17:31:38 +1000147extern int startup_pipe;
148
Damien Millerb38eff82000-04-01 11:09:21 +1000149/* Local Xauthority file. */
150static char *xauthfile;
151
Damien Miller7b28dc52000-09-05 13:34:53 +1100152/* original command from peer. */
153char *original_command = NULL;
154
Damien Millerb38eff82000-04-01 11:09:21 +1000155/* data */
156#define MAX_SESSIONS 10
157Session sessions[MAX_SESSIONS];
Damien Miller15e7d4b2000-09-29 10:57:35 +1100158
Damien Millerd2c208a2000-05-17 22:00:02 +1000159#ifdef WITH_AIXAUTHENTICATE
160/* AIX's lastlogin message, set in auth1.c */
161char *aixloginmsg;
162#endif /* WITH_AIXAUTHENTICATE */
Damien Millerb38eff82000-04-01 11:09:21 +1000163
Damien Millerad833b32000-08-23 10:46:23 +1000164#ifdef HAVE_LOGIN_CAP
165static login_cap_t *lc;
166#endif
167
Damien Millerb38eff82000-04-01 11:09:21 +1000168/*
169 * Remove local Xauthority file.
170 */
171void
172xauthfile_cleanup_proc(void *ignore)
173{
174 debug("xauthfile_cleanup_proc called");
175
176 if (xauthfile != NULL) {
177 char *p;
178 unlink(xauthfile);
179 p = strrchr(xauthfile, '/');
180 if (p != NULL) {
181 *p = '\0';
182 rmdir(xauthfile);
183 }
184 xfree(xauthfile);
185 xauthfile = NULL;
186 }
187}
188
189/*
190 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
191 * dropped connection).
192 */
Damien Miller4af51302000-04-16 11:18:38 +1000193void
Damien Millerb38eff82000-04-01 11:09:21 +1000194pty_cleanup_proc(void *session)
195{
196 Session *s=session;
197 if (s == NULL)
198 fatal("pty_cleanup_proc: no session");
199 debug("pty_cleanup_proc: %s", s->tty);
200
201 if (s->pid != 0) {
202 /* Record that the user has logged out. */
203 record_logout(s->pid, s->tty);
204 }
205
206 /* Release the pseudo-tty. */
207 pty_release(s->tty);
208}
209
210/*
211 * Prepares for an interactive session. This is called after the user has
212 * been successfully authenticated. During this message exchange, pseudo
213 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
214 * are requested, etc.
215 */
Damien Miller4af51302000-04-16 11:18:38 +1000216void
Damien Millerb38eff82000-04-01 11:09:21 +1000217do_authenticated(struct passwd * pw)
218{
219 Session *s;
Damien Miller7b28dc52000-09-05 13:34:53 +1100220 int type, fd;
Damien Millerb38eff82000-04-01 11:09:21 +1000221 int compression_level = 0, enable_compression_after_reply = 0;
222 int have_pty = 0;
223 char *command;
224 int n_bytes;
225 int plen;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000226 u_int proto_len, data_len, dlen;
Damien Millerb38eff82000-04-01 11:09:21 +1000227
228 /*
229 * Cancel the alarm we set to limit the time taken for
230 * authentication.
231 */
232 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +1000233 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +1000234 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +1000235 startup_pipe = -1;
236 }
Damien Millerb38eff82000-04-01 11:09:21 +1000237
238 /*
239 * Inform the channel mechanism that we are the server side and that
240 * the client may request to connect to any port at all. (The user
241 * could do it anyway, and we wouldn\'t know what is permitted except
242 * by the client telling us, so we can equally well trust the client
243 * not to request anything bogus.)
244 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100245 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
Damien Millerb38eff82000-04-01 11:09:21 +1000246 channel_permit_all_opens();
247
248 s = session_new();
Damien Millerbd483e72000-04-30 10:00:53 +1000249 s->pw = pw;
Damien Millerb38eff82000-04-01 11:09:21 +1000250
Kevin Steves48b7cc02000-10-07 13:24:00 +0000251#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Damien Millerad833b32000-08-23 10:46:23 +1000252 if ((lc = login_getclass(pw->pw_class)) == NULL) {
253 error("unable to get login class");
254 return;
255 }
256#endif
257
Damien Millerb38eff82000-04-01 11:09:21 +1000258 /*
259 * We stay in this loop until the client requests to execute a shell
260 * or a command.
261 */
262 for (;;) {
263 int success = 0;
264
265 /* Get a packet from the client. */
266 type = packet_read(&plen);
267
268 /* Process the packet. */
269 switch (type) {
270 case SSH_CMSG_REQUEST_COMPRESSION:
271 packet_integrity_check(plen, 4, type);
272 compression_level = packet_get_int();
273 if (compression_level < 1 || compression_level > 9) {
274 packet_send_debug("Received illegal compression level %d.",
275 compression_level);
276 break;
277 }
278 /* Enable compression after we have responded with SUCCESS. */
279 enable_compression_after_reply = 1;
280 success = 1;
281 break;
282
283 case SSH_CMSG_REQUEST_PTY:
284 if (no_pty_flag) {
285 debug("Allocating a pty not permitted for this authentication.");
286 break;
287 }
288 if (have_pty)
289 packet_disconnect("Protocol error: you already have a pty.");
290
291 debug("Allocating pty.");
292
293 /* Allocate a pty and open it. */
294 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
295 sizeof(s->tty))) {
296 error("Failed to allocate pty.");
297 break;
298 }
299 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
300 pty_setowner(pw, s->tty);
301
302 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
303 s->term = packet_get_string(&dlen);
304 packet_integrity_check(dlen, strlen(s->term), type);
305 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
306 /* Remaining bytes */
307 n_bytes = plen - (4 + dlen + 4 * 4);
308
309 if (strcmp(s->term, "") == 0) {
310 xfree(s->term);
311 s->term = NULL;
312 }
313 /* Get window size from the packet. */
314 s->row = packet_get_int();
315 s->col = packet_get_int();
316 s->xpixel = packet_get_int();
317 s->ypixel = packet_get_int();
318 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
319
320 /* Get tty modes from the packet. */
321 tty_parse_modes(s->ttyfd, &n_bytes);
322 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
323
Damien Millere247cc42000-05-07 12:03:14 +1000324 session_proctitle(s);
325
Damien Millerb38eff82000-04-01 11:09:21 +1000326 /* Indicate that we now have a pty. */
327 success = 1;
328 have_pty = 1;
329 break;
330
331 case SSH_CMSG_X11_REQUEST_FORWARDING:
332 if (!options.x11_forwarding) {
333 packet_send_debug("X11 forwarding disabled in server configuration file.");
334 break;
335 }
Damien Miller0c043c12000-06-07 21:22:38 +1000336 if (!options.xauth_location) {
337 packet_send_debug("No xauth program; cannot forward with spoofing.");
338 break;
339 }
Damien Millerb38eff82000-04-01 11:09:21 +1000340 if (no_x11_forwarding_flag) {
341 packet_send_debug("X11 forwarding not permitted for this authentication.");
342 break;
343 }
344 debug("Received request for X11 forwarding with auth spoofing.");
345 if (s->display != NULL)
346 packet_disconnect("Protocol error: X11 display already set.");
347
348 s->auth_proto = packet_get_string(&proto_len);
349 s->auth_data = packet_get_string(&data_len);
350 packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
351
352 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
353 s->screen = packet_get_int();
354 else
355 s->screen = 0;
356 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
357
358 if (s->display == NULL)
359 break;
360
361 /* Setup to always have a local .Xauthority. */
362 xauthfile = xmalloc(MAXPATHLEN);
363 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
364 temporarily_use_uid(pw->pw_uid);
365 if (mkdtemp(xauthfile) == NULL) {
366 restore_uid();
367 error("private X11 dir: mkdtemp %s failed: %s",
368 xauthfile, strerror(errno));
369 xfree(xauthfile);
370 xauthfile = NULL;
Damien Millerbd483e72000-04-30 10:00:53 +1000371 /* XXXX remove listening channels */
Damien Millerb38eff82000-04-01 11:09:21 +1000372 break;
373 }
374 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +1100375 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
376 if (fd >= 0)
377 close(fd);
Damien Millerb38eff82000-04-01 11:09:21 +1000378 restore_uid();
379 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
380 success = 1;
381 break;
Damien Millerb38eff82000-04-01 11:09:21 +1000382
383 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
384 if (no_agent_forwarding_flag || compat13) {
385 debug("Authentication agent forwarding not permitted for this authentication.");
386 break;
387 }
388 debug("Received authentication agent forwarding request.");
Damien Miller0c043c12000-06-07 21:22:38 +1000389 success = auth_input_request_forwarding(pw);
Damien Millerb38eff82000-04-01 11:09:21 +1000390 break;
391
392 case SSH_CMSG_PORT_FORWARD_REQUEST:
393 if (no_port_forwarding_flag) {
394 debug("Port forwarding not permitted for this authentication.");
395 break;
396 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100397 if (!options.allow_tcp_forwarding) {
398 debug("Port forwarding not permitted.");
399 break;
400 }
Damien Millerb38eff82000-04-01 11:09:21 +1000401 debug("Received TCP/IP port forwarding request.");
Damien Millere247cc42000-05-07 12:03:14 +1000402 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
Damien Millerb38eff82000-04-01 11:09:21 +1000403 success = 1;
404 break;
405
406 case SSH_CMSG_MAX_PACKET_SIZE:
407 if (packet_set_maxsize(packet_get_int()) > 0)
408 success = 1;
409 break;
410
411 case SSH_CMSG_EXEC_SHELL:
412 case SSH_CMSG_EXEC_CMD:
Damien Millerb38eff82000-04-01 11:09:21 +1000413 if (type == SSH_CMSG_EXEC_CMD) {
414 command = packet_get_string(&dlen);
415 debug("Exec command '%.500s'", command);
416 packet_integrity_check(plen, 4 + dlen, type);
417 } else {
418 command = NULL;
419 packet_integrity_check(plen, 0, type);
420 }
421 if (forced_command != NULL) {
Damien Miller7b28dc52000-09-05 13:34:53 +1100422 original_command = command;
Damien Millerb38eff82000-04-01 11:09:21 +1000423 command = forced_command;
424 debug("Forced command '%.500s'", forced_command);
425 }
426 if (have_pty)
427 do_exec_pty(s, command, pw);
428 else
429 do_exec_no_pty(s, command, pw);
430
431 if (command != NULL)
432 xfree(command);
433 /* Cleanup user's local Xauthority file. */
434 if (xauthfile)
435 xauthfile_cleanup_proc(NULL);
436 return;
437
438 default:
439 /*
440 * Any unknown messages in this phase are ignored,
441 * and a failure message is returned.
442 */
443 log("Unknown packet type received after authentication: %d", type);
444 }
445 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
446 packet_send();
447 packet_write_wait();
448
449 /* Enable compression now that we have replied if appropriate. */
450 if (enable_compression_after_reply) {
451 enable_compression_after_reply = 0;
452 packet_start_compression(compression_level);
453 }
454 }
455}
456
457/*
458 * This is called to fork and execute a command when we have no tty. This
459 * will call do_child from the child, and server_loop from the parent after
460 * setting up file descriptors and such.
461 */
Damien Miller4af51302000-04-16 11:18:38 +1000462void
Damien Millerb38eff82000-04-01 11:09:21 +1000463do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
464{
465 int pid;
466
467#ifdef USE_PIPES
468 int pin[2], pout[2], perr[2];
469 /* Allocate pipes for communicating with the program. */
470 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
471 packet_disconnect("Could not create pipes: %.100s",
472 strerror(errno));
473#else /* USE_PIPES */
474 int inout[2], err[2];
475 /* Uses socket pairs to communicate with the program. */
476 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
477 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
478 packet_disconnect("Could not create socket pairs: %.100s",
479 strerror(errno));
480#endif /* USE_PIPES */
481 if (s == NULL)
482 fatal("do_exec_no_pty: no session");
483
Damien Millere247cc42000-05-07 12:03:14 +1000484 session_proctitle(s);
Damien Millerb38eff82000-04-01 11:09:21 +1000485
486#ifdef USE_PAM
487 do_pam_setcred();
488#endif /* USE_PAM */
489
490 /* Fork the child. */
491 if ((pid = fork()) == 0) {
492 /* Child. Reinitialize the log since the pid has changed. */
493 log_init(__progname, options.log_level, options.log_facility, log_stderr);
494
Damien Miller152cea22000-12-13 19:21:51 +1100495 signal(SIGPIPE, SIG_DFL);
496
Damien Millerb38eff82000-04-01 11:09:21 +1000497 /*
498 * Create a new session and process group since the 4.4BSD
499 * setlogin() affects the entire process group.
500 */
501 if (setsid() < 0)
502 error("setsid failed: %.100s", strerror(errno));
503
504#ifdef USE_PIPES
505 /*
506 * Redirect stdin. We close the parent side of the socket
507 * pair, and make the child side the standard input.
508 */
509 close(pin[1]);
510 if (dup2(pin[0], 0) < 0)
511 perror("dup2 stdin");
512 close(pin[0]);
513
514 /* Redirect stdout. */
515 close(pout[0]);
516 if (dup2(pout[1], 1) < 0)
517 perror("dup2 stdout");
518 close(pout[1]);
519
520 /* Redirect stderr. */
521 close(perr[0]);
522 if (dup2(perr[1], 2) < 0)
523 perror("dup2 stderr");
524 close(perr[1]);
525#else /* USE_PIPES */
526 /*
527 * Redirect stdin, stdout, and stderr. Stdin and stdout will
528 * use the same socket, as some programs (particularly rdist)
529 * seem to depend on it.
530 */
531 close(inout[1]);
532 close(err[1]);
533 if (dup2(inout[0], 0) < 0) /* stdin */
534 perror("dup2 stdin");
535 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
536 perror("dup2 stdout");
537 if (dup2(err[0], 2) < 0) /* stderr */
538 perror("dup2 stderr");
539#endif /* USE_PIPES */
540
541 /* Do processing for the child (exec command etc). */
542 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
543 /* NOTREACHED */
544 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100545#ifdef HAVE_CYGWIN
546 if (is_winnt)
547 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
548#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000549 if (pid < 0)
550 packet_disconnect("fork failed: %.100s", strerror(errno));
551 s->pid = pid;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000552 /* Set interactive/non-interactive mode. */
553 packet_set_interactive(s->display != NULL);
Damien Millerb38eff82000-04-01 11:09:21 +1000554#ifdef USE_PIPES
555 /* We are the parent. Close the child sides of the pipes. */
556 close(pin[0]);
557 close(pout[1]);
558 close(perr[1]);
559
Damien Millerefb4afe2000-04-12 18:45:05 +1000560 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000561 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000562 } else {
563 /* Enter the interactive session. */
564 server_loop(pid, pin[1], pout[0], perr[0]);
565 /* server_loop has closed pin[1], pout[1], and perr[1]. */
566 }
Damien Millerb38eff82000-04-01 11:09:21 +1000567#else /* USE_PIPES */
568 /* We are the parent. Close the child sides of the socket pairs. */
569 close(inout[0]);
570 close(err[0]);
571
572 /*
573 * Enter the interactive session. Note: server_loop must be able to
574 * handle the case that fdin and fdout are the same.
575 */
Damien Millerefb4afe2000-04-12 18:45:05 +1000576 if (compat20) {
Damien Millerbd483e72000-04-30 10:00:53 +1000577 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000578 } else {
579 server_loop(pid, inout[1], inout[1], err[1]);
580 /* server_loop has closed inout[1] and err[1]. */
581 }
Damien Millerb38eff82000-04-01 11:09:21 +1000582#endif /* USE_PIPES */
583}
584
585/*
586 * This is called to fork and execute a command when we have a tty. This
587 * will call do_child from the child, and server_loop from the parent after
588 * setting up file descriptors, controlling tty, updating wtmp, utmp,
589 * lastlog, and other such operations.
590 */
Damien Miller4af51302000-04-16 11:18:38 +1000591void
Damien Millerb38eff82000-04-01 11:09:21 +1000592do_exec_pty(Session *s, const char *command, struct passwd * pw)
593{
Damien Millerb38eff82000-04-01 11:09:21 +1000594 int fdout, ptyfd, ttyfd, ptymaster;
Damien Millerb38eff82000-04-01 11:09:21 +1000595 pid_t pid;
Damien Millerb38eff82000-04-01 11:09:21 +1000596
597 if (s == NULL)
598 fatal("do_exec_pty: no session");
599 ptyfd = s->ptyfd;
600 ttyfd = s->ttyfd;
601
Damien Millerb38eff82000-04-01 11:09:21 +1000602#ifdef USE_PAM
603 do_pam_session(pw->pw_name, s->tty);
604 do_pam_setcred();
605#endif /* USE_PAM */
606
607 /* Fork the child. */
608 if ((pid = fork()) == 0) {
Damien Miller942da032000-08-18 13:59:06 +1000609 /* Child. Reinitialize the log because the pid has changed. */
Damien Millerb38eff82000-04-01 11:09:21 +1000610 log_init(__progname, options.log_level, options.log_facility, log_stderr);
611
Damien Miller152cea22000-12-13 19:21:51 +1100612 signal(SIGPIPE, SIG_DFL);
613
Damien Millerb38eff82000-04-01 11:09:21 +1000614 /* Close the master side of the pseudo tty. */
615 close(ptyfd);
616
617 /* Make the pseudo tty our controlling tty. */
618 pty_make_controlling_tty(&ttyfd, s->tty);
619
620 /* Redirect stdin from the pseudo tty. */
621 if (dup2(ttyfd, fileno(stdin)) < 0)
622 error("dup2 stdin failed: %.100s", strerror(errno));
623
624 /* Redirect stdout to the pseudo tty. */
625 if (dup2(ttyfd, fileno(stdout)) < 0)
626 error("dup2 stdin failed: %.100s", strerror(errno));
627
628 /* Redirect stderr to the pseudo tty. */
629 if (dup2(ttyfd, fileno(stderr)) < 0)
630 error("dup2 stdin failed: %.100s", strerror(errno));
631
632 /* Close the extra descriptor for the pseudo tty. */
633 close(ttyfd);
634
Damien Miller942da032000-08-18 13:59:06 +1000635 /* record login, etc. similar to login(1) */
Damien Miller69b69aa2000-10-28 14:19:58 +1100636 if (!(options.use_login && command == NULL))
637 do_login(s, command);
Damien Millerb38eff82000-04-01 11:09:21 +1000638
Damien Millerb38eff82000-04-01 11:09:21 +1000639 /* Do common processing for the child, such as execing the command. */
Damien Millerb1715dc2000-05-30 13:44:51 +1000640 do_child(command, pw, s->term, s->display, s->auth_proto,
641 s->auth_data, s->tty);
Damien Millerb38eff82000-04-01 11:09:21 +1000642 /* NOTREACHED */
643 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100644#ifdef HAVE_CYGWIN
645 if (is_winnt)
646 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
647#endif
Damien Millerb38eff82000-04-01 11:09:21 +1000648 if (pid < 0)
649 packet_disconnect("fork failed: %.100s", strerror(errno));
650 s->pid = pid;
651
652 /* Parent. Close the slave side of the pseudo tty. */
653 close(ttyfd);
654
655 /*
656 * Create another descriptor of the pty master side for use as the
657 * standard input. We could use the original descriptor, but this
658 * simplifies code in server_loop. The descriptor is bidirectional.
659 */
660 fdout = dup(ptyfd);
661 if (fdout < 0)
662 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
663
664 /* we keep a reference to the pty master */
665 ptymaster = dup(ptyfd);
666 if (ptymaster < 0)
667 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
668 s->ptymaster = ptymaster;
669
670 /* Enter interactive session. */
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000671 packet_set_interactive(1);
Damien Millerefb4afe2000-04-12 18:45:05 +1000672 if (compat20) {
673 session_set_fds(s, ptyfd, fdout, -1);
674 } else {
675 server_loop(pid, ptyfd, fdout, -1);
676 /* server_loop _has_ closed ptyfd and fdout. */
677 session_pty_cleanup(s);
678 }
Damien Millerb38eff82000-04-01 11:09:21 +1000679}
680
Damien Miller942da032000-08-18 13:59:06 +1000681const char *
682get_remote_name_or_ip(void)
683{
684 static const char *remote = "";
685 if (utmp_len > 0)
686 remote = get_canonical_hostname();
687 if (utmp_len == 0 || strlen(remote) > utmp_len)
688 remote = get_remote_ipaddr();
689 return remote;
690}
691
692/* administrative, login(1)-like work */
693void
Damien Miller69b69aa2000-10-28 14:19:58 +1100694do_login(Session *s, const char *command)
Damien Miller942da032000-08-18 13:59:06 +1000695{
696 FILE *f;
697 char *time_string;
698 char buf[256];
Damien Miller7b28dc52000-09-05 13:34:53 +1100699 char hostname[MAXHOSTNAMELEN];
Damien Miller942da032000-08-18 13:59:06 +1000700 socklen_t fromlen;
701 struct sockaddr_storage from;
702 struct stat st;
703 time_t last_login_time;
704 struct passwd * pw = s->pw;
705 pid_t pid = getpid();
706
707 /*
708 * Get IP address of client. If the connection is not a socket, let
709 * the address be 0.0.0.0.
710 */
711 memset(&from, 0, sizeof(from));
712 if (packet_connection_is_on_socket()) {
713 fromlen = sizeof(from);
714 if (getpeername(packet_get_connection_in(),
715 (struct sockaddr *) & from, &fromlen) < 0) {
716 debug("getpeername: %.100s", strerror(errno));
717 fatal_cleanup();
718 }
719 }
720
Damien Miller7b28dc52000-09-05 13:34:53 +1100721 /* Get the time and hostname when the user last logged in. */
Damien Millere4340be2000-09-16 13:29:08 +1100722 hostname[0] = '\0';
723 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
724 hostname, sizeof(hostname));
725
Damien Miller942da032000-08-18 13:59:06 +1000726 /* Record that there was a login on that tty from the remote host. */
727 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
728 get_remote_name_or_ip(), (struct sockaddr *)&from);
729
Kevin Steves092f2ef2000-10-14 13:36:13 +0000730#ifdef USE_PAM
731 /*
732 * If password change is needed, do it now.
733 * This needs to occur before the ~/.hushlogin check.
734 */
735 if (pam_password_change_required()) {
736 print_pam_messages();
737 do_pam_chauthtok();
738 }
739#endif
740
Damien Miller69b69aa2000-10-28 14:19:58 +1100741 /* Done if .hushlogin exists or a command given. */
742 if (command != NULL)
743 return;
Damien Miller942da032000-08-18 13:59:06 +1000744 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +1000745#ifdef HAVE_LOGIN_CAP
746 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
747#else
Damien Miller942da032000-08-18 13:59:06 +1000748 if (stat(buf, &st) >= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000749#endif
Damien Miller942da032000-08-18 13:59:06 +1000750 return;
751
752#ifdef USE_PAM
Kevin Steves092f2ef2000-10-14 13:36:13 +0000753 if (!pam_password_change_required())
754 print_pam_messages();
Damien Miller942da032000-08-18 13:59:06 +1000755#endif /* USE_PAM */
756#ifdef WITH_AIXAUTHENTICATE
757 if (aixloginmsg && *aixloginmsg)
758 printf("%s\n", aixloginmsg);
759#endif /* WITH_AIXAUTHENTICATE */
760
Damien Miller942da032000-08-18 13:59:06 +1000761 if (last_login_time != 0) {
762 time_string = ctime(&last_login_time);
763 if (strchr(time_string, '\n'))
764 *strchr(time_string, '\n') = 0;
Kevin Stevesc368a3c2000-10-14 16:10:06 +0000765 if (strcmp(hostname, "") == 0)
Damien Miller942da032000-08-18 13:59:06 +1000766 printf("Last login: %s\r\n", time_string);
767 else
Damien Millere4340be2000-09-16 13:29:08 +1100768 printf("Last login: %s from %s\r\n", time_string, hostname);
Damien Miller942da032000-08-18 13:59:06 +1000769 }
770 if (options.print_motd) {
Damien Millerad833b32000-08-23 10:46:23 +1000771#ifdef HAVE_LOGIN_CAP
772 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
773 "/etc/motd"), "r");
774#else
Damien Miller942da032000-08-18 13:59:06 +1000775 f = fopen("/etc/motd", "r");
Damien Millerad833b32000-08-23 10:46:23 +1000776#endif
Damien Miller942da032000-08-18 13:59:06 +1000777 if (f) {
778 while (fgets(buf, sizeof(buf), f))
779 fputs(buf, stdout);
780 fclose(f);
781 }
782 }
783}
784
Damien Millerb38eff82000-04-01 11:09:21 +1000785/*
786 * Sets the value of the given variable in the environment. If the variable
787 * already exists, its value is overriden.
788 */
Damien Miller4af51302000-04-16 11:18:38 +1000789void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000790child_set_env(char ***envp, u_int *envsizep, const char *name,
Damien Millerb38eff82000-04-01 11:09:21 +1000791 const char *value)
792{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000793 u_int i, namelen;
Damien Millerb38eff82000-04-01 11:09:21 +1000794 char **env;
795
796 /*
797 * Find the slot where the value should be stored. If the variable
798 * already exists, we reuse the slot; otherwise we append a new slot
799 * at the end of the array, expanding if necessary.
800 */
801 env = *envp;
802 namelen = strlen(name);
803 for (i = 0; env[i]; i++)
804 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
805 break;
806 if (env[i]) {
807 /* Reuse the slot. */
808 xfree(env[i]);
809 } else {
810 /* New variable. Expand if necessary. */
811 if (i >= (*envsizep) - 1) {
812 (*envsizep) += 50;
813 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
814 }
815 /* Need to set the NULL pointer at end of array beyond the new slot. */
816 env[i + 1] = NULL;
817 }
818
819 /* Allocate space and format the variable in the appropriate slot. */
820 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
821 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
822}
823
824/*
825 * Reads environment variables from the given file and adds/overrides them
826 * into the environment. If the file does not exist, this does nothing.
827 * Otherwise, it must consist of empty lines, comments (line starts with '#')
828 * and assignments of the form name=value. No other forms are allowed.
829 */
Damien Miller4af51302000-04-16 11:18:38 +1000830void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000831read_environment_file(char ***env, u_int *envsize,
Damien Millerb38eff82000-04-01 11:09:21 +1000832 const char *filename)
833{
834 FILE *f;
835 char buf[4096];
836 char *cp, *value;
837
838 f = fopen(filename, "r");
839 if (!f)
840 return;
841
842 while (fgets(buf, sizeof(buf), f)) {
843 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
844 ;
845 if (!*cp || *cp == '#' || *cp == '\n')
846 continue;
847 if (strchr(cp, '\n'))
848 *strchr(cp, '\n') = '\0';
849 value = strchr(cp, '=');
850 if (value == NULL) {
851 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
852 continue;
853 }
Damien Millerb1715dc2000-05-30 13:44:51 +1000854 /*
855 * Replace the equals sign by nul, and advance value to
856 * the value string.
857 */
Damien Millerb38eff82000-04-01 11:09:21 +1000858 *value = '\0';
859 value++;
860 child_set_env(env, envsize, cp, value);
861 }
862 fclose(f);
863}
864
865#ifdef USE_PAM
866/*
867 * Sets any environment variables which have been specified by PAM
868 */
869void do_pam_environment(char ***env, int *envsize)
870{
871 char *equals, var_name[512], var_val[512];
872 char **pam_env;
873 int i;
874
875 if ((pam_env = fetch_pam_environment()) == NULL)
876 return;
877
878 for(i = 0; pam_env[i] != NULL; i++) {
879 if ((equals = strstr(pam_env[i], "=")) == NULL)
880 continue;
881
882 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
883 memset(var_name, '\0', sizeof(var_name));
884 memset(var_val, '\0', sizeof(var_val));
885
886 strncpy(var_name, pam_env[i], equals - pam_env[i]);
887 strcpy(var_val, equals + 1);
888
Damien Millercb5e44a2000-09-29 12:12:36 +1100889 debug3("PAM environment: %s=%s", var_name, var_val);
Damien Millerb38eff82000-04-01 11:09:21 +1000890
891 child_set_env(env, envsize, var_name, var_val);
892 }
893 }
894}
895#endif /* USE_PAM */
896
Damien Millercb5e44a2000-09-29 12:12:36 +1100897
898#ifdef HAVE_CYGWIN
899void copy_environment(char ***env, int *envsize)
900{
901 char *equals, var_name[512], var_val[512];
902 int i;
903
904 for(i = 0; environ[i] != NULL; i++) {
905 if ((equals = strstr(environ[i], "=")) == NULL)
906 continue;
907
908 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
909 memset(var_name, '\0', sizeof(var_name));
910 memset(var_val, '\0', sizeof(var_val));
911
912 strncpy(var_name, environ[i], equals - environ[i]);
913 strcpy(var_val, equals + 1);
914
915 debug3("Copy environment: %s=%s", var_name, var_val);
916
917 child_set_env(env, envsize, var_name, var_val);
918 }
919 }
920}
921#endif
922
Damien Miller5fc85652000-07-09 23:53:07 +1000923#if defined(HAVE_GETUSERATTR)
924/*
925 * AIX-specific login initialisation
926 */
927void set_limit(char *user, char *soft, char *hard, int resource, int mult)
928{
929 struct rlimit rlim;
Damien Miller65964d62000-07-11 09:16:22 +1000930 int slim, hlim;
Damien Miller5fc85652000-07-09 23:53:07 +1000931
932 getrlimit(resource, &rlim);
933
Damien Miller65964d62000-07-11 09:16:22 +1000934 slim = 0;
935 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
936 if (slim < 0) {
937 rlim.rlim_cur = RLIM_INFINITY;
938 } else if (slim != 0) {
939 /* See the wackiness below */
940 if (rlim.rlim_cur == slim * mult)
941 slim = 0;
942 else
943 rlim.rlim_cur = slim * mult;
944 }
945 }
Damien Miller5fc85652000-07-09 23:53:07 +1000946
Damien Miller65964d62000-07-11 09:16:22 +1000947 hlim = 0;
948 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
949 if (hlim < 0) {
950 rlim.rlim_max = RLIM_INFINITY;
951 } else if (hlim != 0) {
952 rlim.rlim_max = hlim * mult;
953 }
954 }
Damien Miller5fc85652000-07-09 23:53:07 +1000955
Damien Miller65964d62000-07-11 09:16:22 +1000956 /*
957 * XXX For cpu and fsize the soft limit is set to the hard limit
958 * if the hard limit is left at its default value and the soft limit
959 * is changed from its default value, either by requesting it
960 * (slim == 0) or by setting it to the current default. At least
961 * that's how rlogind does it. If you're confused you're not alone.
962 * Bug or feature? AIX 4.3.1.2
963 */
964 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
965 && hlim == 0 && slim != 0)
966 rlim.rlim_max = rlim.rlim_cur;
967 /* A specified hard limit limits the soft limit */
968 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
969 rlim.rlim_cur = rlim.rlim_max;
970 /* A soft limit can increase a hard limit */
971 else if (rlim.rlim_cur > rlim.rlim_max)
Damien Miller5fc85652000-07-09 23:53:07 +1000972 rlim.rlim_max = rlim.rlim_cur;
973
974 if (setrlimit(resource, &rlim) != 0)
Damien Miller65964d62000-07-11 09:16:22 +1000975 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
Damien Miller5fc85652000-07-09 23:53:07 +1000976}
977
978void set_limits_from_userattr(char *user)
979{
980 int mask;
981 char buf[16];
982
983 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
984 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
985 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
986 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
987 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
988 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
989#if defined(S_UNOFILE)
990 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
991#endif
992
993 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
994 /* Convert decimal to octal */
995 (void) snprintf(buf, sizeof(buf), "%d", mask);
996 if (sscanf(buf, "%o", &mask) == 1)
997 umask(mask);
998 }
999}
1000#endif /* defined(HAVE_GETUSERATTR) */
1001
Damien Millerb38eff82000-04-01 11:09:21 +10001002/*
1003 * Performs common processing for the child, such as setting up the
1004 * environment, closing extra file descriptors, setting the user and group
1005 * ids, and executing the command or shell.
1006 */
Damien Miller4af51302000-04-16 11:18:38 +10001007void
Damien Millerb38eff82000-04-01 11:09:21 +10001008do_child(const char *command, struct passwd * pw, const char *term,
1009 const char *display, const char *auth_proto,
1010 const char *auth_data, const char *ttyname)
1011{
Damien Miller7b28dc52000-09-05 13:34:53 +11001012 const char *shell, *hostname = NULL, *cp = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001013 char buf[256];
Damien Miller0c043c12000-06-07 21:22:38 +10001014 char cmd[1024];
Damien Millerad833b32000-08-23 10:46:23 +10001015 FILE *f = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001016 u_int envsize, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001017 char **env;
1018 extern char **environ;
1019 struct stat st;
1020 char *argv[10];
Damien Miller91606b12000-06-28 08:22:29 +10001021#ifdef WITH_IRIX_PROJECT
1022 prid_t projid;
1023#endif /* WITH_IRIX_PROJECT */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001024#ifdef WITH_IRIX_JOBS
1025 jid_t jid = 0;
1026#else
1027#ifdef WITH_IRIX_ARRAY
1028 int jid = 0;
1029#endif /* WITH_IRIX_ARRAY */
1030#endif /* WITH_IRIX_JOBS */
1031
Damien Millerb38eff82000-04-01 11:09:21 +10001032
Damien Millerd3a18572000-06-07 19:55:44 +10001033 /* login(1) is only called if we execute the login shell */
1034 if (options.use_login && command != NULL)
1035 options.use_login = 0;
1036
Damien Millerb38eff82000-04-01 11:09:21 +10001037#ifndef USE_PAM /* pam_nologin handles this */
Damien Millerad833b32000-08-23 10:46:23 +10001038 if (!options.use_login) {
1039# ifdef HAVE_LOGIN_CAP
1040 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1041 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1042 _PATH_NOLOGIN), "r");
1043# else /* HAVE_LOGIN_CAP */
1044 if (pw->pw_uid)
1045 f = fopen(_PATH_NOLOGIN, "r");
1046# endif /* HAVE_LOGIN_CAP */
1047 if (f) {
1048 /* /etc/nologin exists. Print its contents and exit. */
1049 while (fgets(buf, sizeof(buf), f))
1050 fputs(buf, stderr);
1051 fclose(f);
Damien Millerb38eff82000-04-01 11:09:21 +10001052 exit(254);
Damien Millerad833b32000-08-23 10:46:23 +10001053 }
Damien Millerb38eff82000-04-01 11:09:21 +10001054 }
1055#endif /* USE_PAM */
1056
Damien Millerad833b32000-08-23 10:46:23 +10001057 /* Set login name, uid, gid, and groups. */
Damien Millerb38eff82000-04-01 11:09:21 +10001058 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1059 switch, so we let login(1) to this for us. */
1060 if (!options.use_login) {
Damien Millerb8c656e2000-06-28 15:22:41 +10001061#ifdef HAVE_OSF_SIA
1062 extern char **saved_argv;
1063 extern int saved_argc;
1064 char *host = get_canonical_hostname ();
1065
1066 if (sia_become_user(NULL, saved_argc, saved_argv, host,
1067 pw->pw_name, ttyname, 0, NULL, NULL, SIA_BEU_SETLUID) !=
1068 SIASUCCESS) {
1069 perror("sia_become_user");
1070 exit(1);
1071 }
1072 if (setreuid(geteuid(), geteuid()) < 0) {
1073 perror("setreuid");
1074 exit(1);
1075 }
1076#else /* HAVE_OSF_SIA */
Damien Millerbac2d8a2000-09-05 16:13:06 +11001077#ifdef HAVE_CYGWIN
1078 if (is_winnt) {
1079#else
Damien Millerb38eff82000-04-01 11:09:21 +10001080 if (getuid() == 0 || geteuid() == 0) {
Damien Millerbac2d8a2000-09-05 16:13:06 +11001081#endif
Damien Millerad833b32000-08-23 10:46:23 +10001082# ifdef HAVE_GETUSERATTR
Damien Miller5fc85652000-07-09 23:53:07 +10001083 set_limits_from_userattr(pw->pw_name);
Damien Millerad833b32000-08-23 10:46:23 +10001084# endif /* HAVE_GETUSERATTR */
1085# ifdef HAVE_LOGIN_CAP
1086 if (setusercontext(lc, pw, pw->pw_uid,
1087 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1088 perror("unable to set user context");
1089 exit(1);
1090 }
1091# else /* HAVE_LOGIN_CAP */
1092 if (setlogin(pw->pw_name) < 0)
1093 error("setlogin failed: %s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001094 if (setgid(pw->pw_gid) < 0) {
1095 perror("setgid");
1096 exit(1);
1097 }
1098 /* Initialize the group list. */
1099 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1100 perror("initgroups");
1101 exit(1);
1102 }
1103 endgrent();
Ben Lindstrom980754c2000-11-12 00:04:24 +00001104# ifdef WITH_IRIX_JOBS
1105 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1106 if (jid == -1) {
1107 fatal("Failed to create job container: %.100s",
1108 strerror(errno));
1109 }
1110# endif /* WITH_IRIX_JOBS */
Damien Millerad833b32000-08-23 10:46:23 +10001111# ifdef WITH_IRIX_ARRAY
Damien Miller91606b12000-06-28 08:22:29 +10001112 /* initialize array session */
Ben Lindstrom980754c2000-11-12 00:04:24 +00001113 if (jid == 0) {
1114 if (newarraysess() != 0)
1115 fatal("Failed to set up new array session: %.100s",
1116 strerror(errno));
1117 }
Damien Millerad833b32000-08-23 10:46:23 +10001118# endif /* WITH_IRIX_ARRAY */
1119# ifdef WITH_IRIX_PROJECT
Damien Miller91606b12000-06-28 08:22:29 +10001120 /* initialize irix project info */
1121 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1122 debug("Failed to get project id, using projid 0");
1123 projid = 0;
1124 }
Damien Miller91606b12000-06-28 08:22:29 +10001125 if (setprid(projid))
1126 fatal("Failed to initialize project %d for %s: %.100s",
1127 (int)projid, pw->pw_name, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001128# endif /* WITH_IRIX_PROJECT */
Ben Lindstrom49a79c02000-11-17 03:47:20 +00001129#ifdef WITH_IRIX_AUDIT
1130 if (sysconf(_SC_AUDIT)) {
1131 debug("Setting sat id to %d", (int) pw->pw_uid);
1132 if (satsetid(pw->pw_uid))
1133 debug("error setting satid: %.100s", strerror(errno));
1134 }
1135#endif /* WITH_IRIX_AUDIT */
1136
Damien Millerb38eff82000-04-01 11:09:21 +10001137 /* Permanently switch to the desired uid. */
1138 permanently_set_uid(pw->pw_uid);
Damien Millerad833b32000-08-23 10:46:23 +10001139# endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001140 }
Damien Millerad833b32000-08-23 10:46:23 +10001141#endif /* HAVE_OSF_SIA */
1142
Damien Millerbac2d8a2000-09-05 16:13:06 +11001143#ifdef HAVE_CYGWIN
1144 if (is_winnt)
1145#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001146 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Damien Millercaf6dd62000-08-29 11:33:50 +11001147 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
Damien Millerb38eff82000-04-01 11:09:21 +10001148 }
1149 /*
1150 * Get the shell from the password data. An empty shell field is
1151 * legal, and means /bin/sh.
1152 */
1153 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
Damien Millerad833b32000-08-23 10:46:23 +10001154#ifdef HAVE_LOGIN_CAP
1155 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1156#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001157
1158#ifdef AFS
1159 /* Try to get AFS tokens for the local cell. */
1160 if (k_hasafs()) {
1161 char cell[64];
1162
1163 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1164 krb_afslog(cell, 0);
1165
1166 krb_afslog(0, 0);
1167 }
1168#endif /* AFS */
1169
1170 /* Initialize the environment. */
1171 envsize = 100;
1172 env = xmalloc(envsize * sizeof(char *));
1173 env[0] = NULL;
1174
Damien Millerbac2d8a2000-09-05 16:13:06 +11001175#ifdef HAVE_CYGWIN
1176 /*
1177 * The Windows environment contains some setting which are
1178 * important for a running system. They must not be dropped.
1179 */
Damien Millercb5e44a2000-09-29 12:12:36 +11001180 copy_environment(&env, &envsize);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001181#endif
1182
Damien Millerb38eff82000-04-01 11:09:21 +10001183 if (!options.use_login) {
1184 /* Set basic environment. */
1185 child_set_env(&env, &envsize, "USER", pw->pw_name);
1186 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1187 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
Damien Millerad833b32000-08-23 10:46:23 +10001188#ifdef HAVE_LOGIN_CAP
1189 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1190 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
Damien Millercb5e44a2000-09-29 12:12:36 +11001191#else /* HAVE_LOGIN_CAP */
1192# ifndef HAVE_CYGWIN
Damien Millerbac2d8a2000-09-05 16:13:06 +11001193 /*
1194 * There's no standard path on Windows. The path contains
1195 * important components pointing to the system directories,
1196 * needed for loading shared libraries. So the path better
1197 * remains intact here.
1198 */
Damien Millerb38eff82000-04-01 11:09:21 +10001199 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
Damien Millercb5e44a2000-09-29 12:12:36 +11001200# endif /* HAVE_CYGWIN */
1201#endif /* HAVE_LOGIN_CAP */
Damien Millerb38eff82000-04-01 11:09:21 +10001202
1203 snprintf(buf, sizeof buf, "%.200s/%.50s",
1204 _PATH_MAILDIR, pw->pw_name);
1205 child_set_env(&env, &envsize, "MAIL", buf);
1206
1207 /* Normal systems set SHELL by default. */
1208 child_set_env(&env, &envsize, "SHELL", shell);
1209 }
1210 if (getenv("TZ"))
1211 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1212
1213 /* Set custom environment options from RSA authentication. */
1214 while (custom_environment) {
1215 struct envstring *ce = custom_environment;
1216 char *s = ce->s;
1217 int i;
1218 for (i = 0; s[i] != '=' && s[i]; i++);
1219 if (s[i] == '=') {
1220 s[i] = 0;
1221 child_set_env(&env, &envsize, s, s + i + 1);
1222 }
1223 custom_environment = ce->next;
1224 xfree(ce->s);
1225 xfree(ce);
1226 }
1227
1228 snprintf(buf, sizeof buf, "%.50s %d %d",
1229 get_remote_ipaddr(), get_remote_port(), get_local_port());
1230 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1231
1232 if (ttyname)
1233 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1234 if (term)
1235 child_set_env(&env, &envsize, "TERM", term);
1236 if (display)
1237 child_set_env(&env, &envsize, "DISPLAY", display);
Damien Miller7b28dc52000-09-05 13:34:53 +11001238 if (original_command)
1239 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1240 original_command);
Damien Millerb38eff82000-04-01 11:09:21 +10001241
1242#ifdef _AIX
Damien Millercb5e44a2000-09-29 12:12:36 +11001243 if ((cp = getenv("AUTHSTATE")) != NULL)
1244 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1245 if ((cp = getenv("KRB5CCNAME")) != NULL)
1246 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1247 read_environment_file(&env, &envsize, "/etc/environment");
Damien Millerb38eff82000-04-01 11:09:21 +10001248#endif
1249
1250#ifdef KRB4
1251 {
1252 extern char *ticket;
1253
1254 if (ticket)
1255 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1256 }
1257#endif /* KRB4 */
1258
1259#ifdef USE_PAM
1260 /* Pull in any environment variables that may have been set by PAM. */
1261 do_pam_environment(&env, &envsize);
1262#endif /* USE_PAM */
1263
Damien Millerb38eff82000-04-01 11:09:21 +10001264 if (xauthfile)
1265 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1266 if (auth_get_socket_name() != NULL)
1267 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1268 auth_get_socket_name());
1269
1270 /* read $HOME/.ssh/environment. */
1271 if (!options.use_login) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001272 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1273 pw->pw_dir);
Damien Millerb38eff82000-04-01 11:09:21 +10001274 read_environment_file(&env, &envsize, buf);
1275 }
1276 if (debug_flag) {
1277 /* dump the environment */
1278 fprintf(stderr, "Environment:\n");
1279 for (i = 0; env[i]; i++)
1280 fprintf(stderr, " %.200s\n", env[i]);
1281 }
Damien Millerad833b32000-08-23 10:46:23 +10001282 /* we have to stash the hostname before we close our socket. */
1283 if (options.use_login)
1284 hostname = get_remote_name_or_ip();
Damien Millerb38eff82000-04-01 11:09:21 +10001285 /*
1286 * Close the connection descriptors; note that this is the child, and
1287 * the server will still have the socket open, and it is important
1288 * that we do not shutdown it. Note that the descriptors cannot be
1289 * closed before building the environment, as we call
1290 * get_remote_ipaddr there.
1291 */
1292 if (packet_get_connection_in() == packet_get_connection_out())
1293 close(packet_get_connection_in());
1294 else {
1295 close(packet_get_connection_in());
1296 close(packet_get_connection_out());
1297 }
1298 /*
1299 * Close all descriptors related to channels. They will still remain
1300 * open in the parent.
1301 */
1302 /* XXX better use close-on-exec? -markus */
1303 channel_close_all();
1304
1305 /*
1306 * Close any extra file descriptors. Note that there may still be
1307 * descriptors left by system functions. They will be closed later.
1308 */
1309 endpwent();
1310
1311 /*
1312 * Close any extra open file descriptors so that we don\'t have them
1313 * hanging around in clients. Note that we want to do this after
1314 * initgroups, because at least on Solaris 2.3 it leaves file
1315 * descriptors open.
1316 */
1317 for (i = 3; i < 64; i++)
1318 close(i);
1319
1320 /* Change current directory to the user\'s home directory. */
Damien Millerad833b32000-08-23 10:46:23 +10001321 if (chdir(pw->pw_dir) < 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001322 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1323 pw->pw_dir, strerror(errno));
Damien Millerad833b32000-08-23 10:46:23 +10001324#ifdef HAVE_LOGIN_CAP
1325 if (login_getcapbool(lc, "requirehome", 0))
1326 exit(1);
1327#endif
1328 }
Damien Millerb38eff82000-04-01 11:09:21 +10001329
1330 /*
1331 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1332 * xauth are run in the proper environment.
1333 */
1334 environ = env;
1335
1336 /*
1337 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1338 * in this order).
1339 */
1340 if (!options.use_login) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001341 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001342 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001343 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_USER_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001344
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001345 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001346 if (f) {
1347 if (auth_proto != NULL && auth_data != NULL)
1348 fprintf(f, "%s %s\n", auth_proto, auth_data);
1349 pclose(f);
1350 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001351 fprintf(stderr, "Could not run %s\n", _PATH_SSH_USER_RC);
1352 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001353 if (debug_flag)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001354 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_SYSTEM_RC);
Damien Millerb38eff82000-04-01 11:09:21 +10001355
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001356 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001357 if (f) {
1358 if (auth_proto != NULL && auth_data != NULL)
1359 fprintf(f, "%s %s\n", auth_proto, auth_data);
1360 pclose(f);
1361 } else
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001362 fprintf(stderr, "Could not run %s\n", _PATH_SSH_SYSTEM_RC);
Damien Miller0c043c12000-06-07 21:22:38 +10001363 } else if (options.xauth_location != NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +10001364 /* Add authority data to .Xauthority if appropriate. */
1365 if (auth_proto != NULL && auth_data != NULL) {
Damien Millerd999ae22000-05-20 12:49:31 +10001366 char *screen = strchr(display, ':');
1367 if (debug_flag) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001368 fprintf(stderr,
1369 "Running %.100s add %.100s %.100s %.100s\n",
Damien Miller0c043c12000-06-07 21:22:38 +10001370 options.xauth_location, display,
1371 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001372#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001373 if (screen != NULL)
Damien Millerb1715dc2000-05-30 13:44:51 +10001374 fprintf(stderr,
1375 "Adding %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001376 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001377 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001378#endif
Damien Millerd999ae22000-05-20 12:49:31 +10001379 }
Damien Miller0c043c12000-06-07 21:22:38 +10001380 snprintf(cmd, sizeof cmd, "%s -q -",
1381 options.xauth_location);
1382 f = popen(cmd, "w");
Damien Millerb38eff82000-04-01 11:09:21 +10001383 if (f) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001384 fprintf(f, "add %s %s %s\n", display,
1385 auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001386#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
Damien Millerd999ae22000-05-20 12:49:31 +10001387 if (screen != NULL)
1388 fprintf(f, "add %.*s/unix%s %s %s\n",
Damien Millercaf6dd62000-08-29 11:33:50 +11001389 (int)(screen-display), display,
Damien Millerb1715dc2000-05-30 13:44:51 +10001390 screen, auth_proto, auth_data);
Damien Miller05dd7952000-10-01 00:42:48 +11001391#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001392 pclose(f);
Damien Miller0c043c12000-06-07 21:22:38 +10001393 } else {
1394 fprintf(stderr, "Could not run %s\n",
1395 cmd);
1396 }
Damien Millerb38eff82000-04-01 11:09:21 +10001397 }
1398 }
Damien Millerb38eff82000-04-01 11:09:21 +10001399 /* Get the last component of the shell name. */
1400 cp = strrchr(shell, '/');
1401 if (cp)
1402 cp++;
1403 else
1404 cp = shell;
1405 }
1406 /*
1407 * If we have no command, execute the shell. In this case, the shell
1408 * name to be passed in argv[0] is preceded by '-' to indicate that
1409 * this is a login shell.
1410 */
1411 if (!command) {
1412 if (!options.use_login) {
1413 char buf[256];
1414
1415 /*
1416 * Check for mail if we have a tty and it was enabled
1417 * in server options.
1418 */
1419 if (ttyname && options.check_mail) {
1420 char *mailbox;
1421 struct stat mailstat;
1422 mailbox = getenv("MAIL");
1423 if (mailbox != NULL) {
Damien Millerb1715dc2000-05-30 13:44:51 +10001424 if (stat(mailbox, &mailstat) != 0 ||
1425 mailstat.st_size == 0)
Damien Millerb38eff82000-04-01 11:09:21 +10001426 printf("No mail.\n");
1427 else if (mailstat.st_mtime < mailstat.st_atime)
1428 printf("You have mail.\n");
1429 else
1430 printf("You have new mail.\n");
1431 }
1432 }
1433 /* Start the shell. Set initial character to '-'. */
1434 buf[0] = '-';
1435 strncpy(buf + 1, cp, sizeof(buf) - 1);
1436 buf[sizeof(buf) - 1] = 0;
1437
1438 /* Execute the shell. */
1439 argv[0] = buf;
1440 argv[1] = NULL;
1441 execve(shell, argv, env);
1442
1443 /* Executing the shell failed. */
1444 perror(shell);
1445 exit(1);
1446
1447 } else {
1448 /* Launch login(1). */
1449
Damien Millerad833b32000-08-23 10:46:23 +10001450 execl(LOGIN_PROGRAM, "login", "-h", hostname,
Damien Miller942da032000-08-18 13:59:06 +10001451 "-p", "-f", "--", pw->pw_name, NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001452
1453 /* Login couldn't be executed, die. */
1454
1455 perror("login");
1456 exit(1);
1457 }
1458 }
1459 /*
1460 * Execute the command using the user's shell. This uses the -c
1461 * option to execute the command.
1462 */
1463 argv[0] = (char *) cp;
1464 argv[1] = "-c";
1465 argv[2] = (char *) command;
1466 argv[3] = NULL;
1467 execve(shell, argv, env);
1468 perror(shell);
1469 exit(1);
1470}
1471
1472Session *
1473session_new(void)
1474{
1475 int i;
1476 static int did_init = 0;
1477 if (!did_init) {
1478 debug("session_new: init");
1479 for(i = 0; i < MAX_SESSIONS; i++) {
1480 sessions[i].used = 0;
1481 sessions[i].self = i;
1482 }
1483 did_init = 1;
1484 }
1485 for(i = 0; i < MAX_SESSIONS; i++) {
1486 Session *s = &sessions[i];
1487 if (! s->used) {
1488 s->pid = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10001489 s->extended = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001490 s->chanid = -1;
1491 s->ptyfd = -1;
1492 s->ttyfd = -1;
1493 s->term = NULL;
1494 s->pw = NULL;
1495 s->display = NULL;
1496 s->screen = 0;
1497 s->auth_data = NULL;
1498 s->auth_proto = NULL;
1499 s->used = 1;
Damien Millerbd483e72000-04-30 10:00:53 +10001500 s->pw = NULL;
Damien Miller15b29522000-10-14 12:33:48 +11001501 debug("session_new: session %d", i);
Damien Millerb38eff82000-04-01 11:09:21 +10001502 return s;
1503 }
1504 }
1505 return NULL;
1506}
1507
1508void
1509session_dump(void)
1510{
1511 int i;
1512 for(i = 0; i < MAX_SESSIONS; i++) {
1513 Session *s = &sessions[i];
1514 debug("dump: used %d session %d %p channel %d pid %d",
1515 s->used,
1516 s->self,
1517 s,
1518 s->chanid,
1519 s->pid);
1520 }
1521}
1522
Damien Millerefb4afe2000-04-12 18:45:05 +10001523int
1524session_open(int chanid)
1525{
1526 Session *s = session_new();
1527 debug("session_open: channel %d", chanid);
1528 if (s == NULL) {
1529 error("no more sessions");
1530 return 0;
1531 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001532 s->pw = auth_get_user();
1533 if (s->pw == NULL)
Damien Millerbd483e72000-04-30 10:00:53 +10001534 fatal("no user for session %i", s->self);
1535 debug("session_open: session %d: link with channel %d", s->self, chanid);
1536 s->chanid = chanid;
Damien Millerefb4afe2000-04-12 18:45:05 +10001537 return 1;
1538}
1539
1540Session *
1541session_by_channel(int id)
1542{
1543 int i;
1544 for(i = 0; i < MAX_SESSIONS; i++) {
1545 Session *s = &sessions[i];
1546 if (s->used && s->chanid == id) {
1547 debug("session_by_channel: session %d channel %d", i, id);
1548 return s;
1549 }
1550 }
1551 debug("session_by_channel: unknown channel %d", id);
1552 session_dump();
1553 return NULL;
1554}
1555
1556Session *
1557session_by_pid(pid_t pid)
1558{
1559 int i;
1560 debug("session_by_pid: pid %d", pid);
1561 for(i = 0; i < MAX_SESSIONS; i++) {
1562 Session *s = &sessions[i];
1563 if (s->used && s->pid == pid)
1564 return s;
1565 }
1566 error("session_by_pid: unknown pid %d", pid);
1567 session_dump();
1568 return NULL;
1569}
1570
1571int
1572session_window_change_req(Session *s)
1573{
1574 s->col = packet_get_int();
1575 s->row = packet_get_int();
1576 s->xpixel = packet_get_int();
1577 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001578 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001579 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1580 return 1;
1581}
1582
1583int
1584session_pty_req(Session *s)
1585{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001586 u_int len;
Damien Miller4af51302000-04-16 11:18:38 +10001587 char *term_modes; /* encoded terminal modes */
Damien Millerefb4afe2000-04-12 18:45:05 +10001588
Damien Millerf6d9e222000-06-18 14:50:44 +10001589 if (no_pty_flag)
1590 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001591 if (s->ttyfd != -1)
Damien Miller4af51302000-04-16 11:18:38 +10001592 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001593 s->term = packet_get_string(&len);
1594 s->col = packet_get_int();
1595 s->row = packet_get_int();
1596 s->xpixel = packet_get_int();
1597 s->ypixel = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001598 term_modes = packet_get_string(&len);
1599 packet_done();
Damien Millerefb4afe2000-04-12 18:45:05 +10001600
1601 if (strcmp(s->term, "") == 0) {
1602 xfree(s->term);
1603 s->term = NULL;
1604 }
1605 /* Allocate a pty and open it. */
1606 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1607 xfree(s->term);
1608 s->term = NULL;
1609 s->ptyfd = -1;
1610 s->ttyfd = -1;
1611 error("session_pty_req: session %d alloc failed", s->self);
Damien Miller4af51302000-04-16 11:18:38 +10001612 xfree(term_modes);
1613 return 0;
Damien Millerefb4afe2000-04-12 18:45:05 +10001614 }
1615 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1616 /*
1617 * Add a cleanup function to clear the utmp entry and record logout
1618 * time in case we call fatal() (e.g., the connection gets closed).
1619 */
1620 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1621 pty_setowner(s->pw, s->tty);
1622 /* Get window size from the packet. */
1623 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1624
Damien Millere247cc42000-05-07 12:03:14 +10001625 session_proctitle(s);
1626
Damien Miller5f056372000-04-16 12:31:48 +10001627 /* XXX parse and set terminal modes */
1628 xfree(term_modes);
Damien Millerefb4afe2000-04-12 18:45:05 +10001629 return 1;
1630}
1631
Damien Millerbd483e72000-04-30 10:00:53 +10001632int
1633session_subsystem_req(Session *s)
1634{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001635 u_int len;
Damien Millerbd483e72000-04-30 10:00:53 +10001636 int success = 0;
1637 char *subsys = packet_get_string(&len);
Damien Millerf6d9e222000-06-18 14:50:44 +10001638 int i;
Damien Millerbd483e72000-04-30 10:00:53 +10001639
1640 packet_done();
1641 log("subsystem request for %s", subsys);
1642
Damien Millerf6d9e222000-06-18 14:50:44 +10001643 for (i = 0; i < options.num_subsystems; i++) {
1644 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1645 debug("subsystem: exec() %s", options.subsystem_command[i]);
1646 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1647 success = 1;
1648 }
1649 }
1650
1651 if (!success)
1652 log("subsystem request for %s failed, subsystem not found", subsys);
1653
Damien Millerbd483e72000-04-30 10:00:53 +10001654 xfree(subsys);
1655 return success;
1656}
1657
1658int
1659session_x11_req(Session *s)
1660{
Damien Miller7b28dc52000-09-05 13:34:53 +11001661 int fd;
Damien Miller37023962000-07-11 17:31:38 +10001662 if (no_x11_forwarding_flag) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001663 debug("X11 forwarding disabled in user configuration file.");
1664 return 0;
1665 }
Damien Millerbd483e72000-04-30 10:00:53 +10001666 if (!options.x11_forwarding) {
1667 debug("X11 forwarding disabled in server configuration file.");
1668 return 0;
1669 }
1670 if (xauthfile != NULL) {
1671 debug("X11 fwd already started.");
1672 return 0;
1673 }
1674
1675 debug("Received request for X11 forwarding with auth spoofing.");
1676 if (s->display != NULL)
1677 packet_disconnect("Protocol error: X11 display already set.");
1678
1679 s->single_connection = packet_get_char();
1680 s->auth_proto = packet_get_string(NULL);
1681 s->auth_data = packet_get_string(NULL);
1682 s->screen = packet_get_int();
1683 packet_done();
1684
1685 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1686 if (s->display == NULL) {
1687 xfree(s->auth_proto);
1688 xfree(s->auth_data);
1689 return 0;
1690 }
1691 xauthfile = xmalloc(MAXPATHLEN);
1692 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1693 temporarily_use_uid(s->pw->pw_uid);
1694 if (mkdtemp(xauthfile) == NULL) {
1695 restore_uid();
1696 error("private X11 dir: mkdtemp %s failed: %s",
1697 xauthfile, strerror(errno));
1698 xfree(xauthfile);
1699 xauthfile = NULL;
1700 xfree(s->auth_proto);
1701 xfree(s->auth_data);
1702 /* XXXX remove listening channels */
1703 return 0;
1704 }
1705 strlcat(xauthfile, "/cookies", MAXPATHLEN);
Damien Miller7b28dc52000-09-05 13:34:53 +11001706 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1707 if (fd >= 0)
1708 close(fd);
Damien Millerbd483e72000-04-30 10:00:53 +10001709 restore_uid();
1710 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1711 return 1;
1712}
1713
Damien Millerf6d9e222000-06-18 14:50:44 +10001714int
1715session_shell_req(Session *s)
1716{
1717 /* if forced_command == NULL, the shell is execed */
1718 char *shell = forced_command;
1719 packet_done();
1720 s->extended = 1;
1721 if (s->ttyfd == -1)
1722 do_exec_no_pty(s, shell, s->pw);
1723 else
1724 do_exec_pty(s, shell, s->pw);
1725 return 1;
1726}
1727
1728int
1729session_exec_req(Session *s)
1730{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001731 u_int len;
Damien Millerf6d9e222000-06-18 14:50:44 +10001732 char *command = packet_get_string(&len);
1733 packet_done();
1734 if (forced_command) {
Damien Miller7b28dc52000-09-05 13:34:53 +11001735 original_command = command;
Damien Millerf6d9e222000-06-18 14:50:44 +10001736 command = forced_command;
1737 debug("Forced command '%.500s'", forced_command);
1738 }
1739 s->extended = 1;
1740 if (s->ttyfd == -1)
1741 do_exec_no_pty(s, command, s->pw);
1742 else
1743 do_exec_pty(s, command, s->pw);
1744 if (forced_command == NULL)
1745 xfree(command);
1746 return 1;
1747}
1748
Damien Miller0bc1bd82000-11-13 22:57:25 +11001749int
1750session_auth_agent_req(Session *s)
1751{
1752 static int called = 0;
1753 packet_done();
Ben Lindstrom14920292000-11-21 21:24:55 +00001754 if (no_agent_forwarding_flag) {
1755 debug("session_auth_agent_req: no_agent_forwarding_flag");
1756 return 0;
1757 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001758 if (called) {
1759 return 0;
1760 } else {
1761 called = 1;
1762 return auth_input_request_forwarding(s->pw);
1763 }
1764}
1765
Damien Millerefb4afe2000-04-12 18:45:05 +10001766void
1767session_input_channel_req(int id, void *arg)
1768{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001769 u_int len;
Damien Millerefb4afe2000-04-12 18:45:05 +10001770 int reply;
1771 int success = 0;
1772 char *rtype;
1773 Session *s;
1774 Channel *c;
1775
1776 rtype = packet_get_string(&len);
1777 reply = packet_get_char();
1778
1779 s = session_by_channel(id);
1780 if (s == NULL)
1781 fatal("session_input_channel_req: channel %d: no session", id);
1782 c = channel_lookup(id);
1783 if (c == NULL)
1784 fatal("session_input_channel_req: channel %d: bad channel", id);
1785
1786 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1787 s->self, id, rtype, reply);
1788
1789 /*
1790 * a session is in LARVAL state until a shell
1791 * or programm is executed
1792 */
1793 if (c->type == SSH_CHANNEL_LARVAL) {
1794 if (strcmp(rtype, "shell") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001795 success = session_shell_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001796 } else if (strcmp(rtype, "exec") == 0) {
Damien Millerf6d9e222000-06-18 14:50:44 +10001797 success = session_exec_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001798 } else if (strcmp(rtype, "pty-req") == 0) {
Damien Miller5f056372000-04-16 12:31:48 +10001799 success = session_pty_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001800 } else if (strcmp(rtype, "x11-req") == 0) {
1801 success = session_x11_req(s);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001802 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1803 success = session_auth_agent_req(s);
Damien Millerbd483e72000-04-30 10:00:53 +10001804 } else if (strcmp(rtype, "subsystem") == 0) {
1805 success = session_subsystem_req(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001806 }
1807 }
1808 if (strcmp(rtype, "window-change") == 0) {
1809 success = session_window_change_req(s);
1810 }
1811
1812 if (reply) {
1813 packet_start(success ?
1814 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1815 packet_put_int(c->remote_id);
1816 packet_send();
1817 }
1818 xfree(rtype);
1819}
1820
1821void
1822session_set_fds(Session *s, int fdin, int fdout, int fderr)
1823{
1824 if (!compat20)
1825 fatal("session_set_fds: called for proto != 2.0");
1826 /*
1827 * now that have a child and a pipe to the child,
1828 * we can activate our channel and register the fd's
1829 */
1830 if (s->chanid == -1)
1831 fatal("no channel for session %d", s->self);
1832 channel_set_fds(s->chanid,
1833 fdout, fdin, fderr,
Damien Miller69b69aa2000-10-28 14:19:58 +11001834 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1835 1);
Damien Millerefb4afe2000-04-12 18:45:05 +10001836}
1837
Damien Millerb38eff82000-04-01 11:09:21 +10001838void
1839session_pty_cleanup(Session *s)
1840{
1841 if (s == NULL || s->ttyfd == -1)
1842 return;
1843
1844 debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1845
1846 /* Cancel the cleanup function. */
1847 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1848
1849 /* Record that the user has logged out. */
1850 record_logout(s->pid, s->tty);
1851
1852 /* Release the pseudo-tty. */
1853 pty_release(s->tty);
1854
1855 /*
1856 * Close the server side of the socket pairs. We must do this after
1857 * the pty cleanup, so that another process doesn't get this pty
1858 * while we're still cleaning up.
1859 */
1860 if (close(s->ptymaster) < 0)
1861 error("close(s->ptymaster): %s", strerror(errno));
1862}
Damien Millerefb4afe2000-04-12 18:45:05 +10001863
1864void
1865session_exit_message(Session *s, int status)
1866{
1867 Channel *c;
1868 if (s == NULL)
1869 fatal("session_close: no session");
1870 c = channel_lookup(s->chanid);
1871 if (c == NULL)
1872 fatal("session_close: session %d: no channel %d",
1873 s->self, s->chanid);
1874 debug("session_exit_message: session %d channel %d pid %d",
1875 s->self, s->chanid, s->pid);
1876
1877 if (WIFEXITED(status)) {
1878 channel_request_start(s->chanid,
1879 "exit-status", 0);
1880 packet_put_int(WEXITSTATUS(status));
1881 packet_send();
1882 } else if (WIFSIGNALED(status)) {
1883 channel_request_start(s->chanid,
1884 "exit-signal", 0);
1885 packet_put_int(WTERMSIG(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001886#ifdef WCOREDUMP
Damien Millerefb4afe2000-04-12 18:45:05 +10001887 packet_put_char(WCOREDUMP(status));
Damien Millerf3c6cf12000-05-17 22:08:29 +10001888#else /* WCOREDUMP */
1889 packet_put_char(0);
1890#endif /* WCOREDUMP */
Damien Millerefb4afe2000-04-12 18:45:05 +10001891 packet_put_cstring("");
1892 packet_put_cstring("");
1893 packet_send();
1894 } else {
1895 /* Some weird exit cause. Just exit. */
1896 packet_disconnect("wait returned status %04x.", status);
1897 }
1898
1899 /* disconnect channel */
1900 debug("session_exit_message: release channel %d", s->chanid);
1901 channel_cancel_cleanup(s->chanid);
Damien Miller166fca82000-04-20 07:42:21 +10001902 /*
1903 * emulate a write failure with 'chan_write_failed', nobody will be
1904 * interested in data we write.
1905 * Note that we must not call 'chan_read_failed', since there could
1906 * be some more data waiting in the pipe.
1907 */
Damien Millerbd483e72000-04-30 10:00:53 +10001908 if (c->ostate != CHAN_OUTPUT_CLOSED)
1909 chan_write_failed(c);
Damien Millerefb4afe2000-04-12 18:45:05 +10001910 s->chanid = -1;
1911}
1912
1913void
1914session_free(Session *s)
1915{
1916 debug("session_free: session %d pid %d", s->self, s->pid);
1917 if (s->term)
1918 xfree(s->term);
1919 if (s->display)
1920 xfree(s->display);
1921 if (s->auth_data)
1922 xfree(s->auth_data);
1923 if (s->auth_proto)
1924 xfree(s->auth_proto);
1925 s->used = 0;
1926}
1927
1928void
1929session_close(Session *s)
1930{
1931 session_pty_cleanup(s);
1932 session_free(s);
Damien Millere247cc42000-05-07 12:03:14 +10001933 session_proctitle(s);
Damien Millerefb4afe2000-04-12 18:45:05 +10001934}
1935
1936void
1937session_close_by_pid(pid_t pid, int status)
1938{
1939 Session *s = session_by_pid(pid);
1940 if (s == NULL) {
1941 debug("session_close_by_pid: no session for pid %d", s->pid);
1942 return;
1943 }
1944 if (s->chanid != -1)
1945 session_exit_message(s, status);
1946 session_close(s);
1947}
1948
1949/*
1950 * this is called when a channel dies before
1951 * the session 'child' itself dies
1952 */
1953void
1954session_close_by_channel(int id, void *arg)
1955{
1956 Session *s = session_by_channel(id);
1957 if (s == NULL) {
1958 debug("session_close_by_channel: no session for channel %d", id);
1959 return;
1960 }
1961 /* disconnect channel */
1962 channel_cancel_cleanup(s->chanid);
1963 s->chanid = -1;
1964
1965 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1966 if (s->pid == 0) {
1967 /* close session immediately */
1968 session_close(s);
1969 } else {
1970 /* notify child, delay session cleanup */
Damien Miller099f5052000-06-22 20:57:11 +10001971 if (s->pid <= 1)
Damien Miller7a445bb2000-06-26 13:12:37 +10001972 fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
1973 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
Damien Millerefb4afe2000-04-12 18:45:05 +10001974 error("session_close_by_channel: kill %d: %s",
1975 s->pid, strerror(errno));
1976 }
1977}
1978
Damien Millere247cc42000-05-07 12:03:14 +10001979char *
1980session_tty_list(void)
1981{
1982 static char buf[1024];
1983 int i;
1984 buf[0] = '\0';
1985 for(i = 0; i < MAX_SESSIONS; i++) {
1986 Session *s = &sessions[i];
1987 if (s->used && s->ttyfd != -1) {
1988 if (buf[0] != '\0')
1989 strlcat(buf, ",", sizeof buf);
1990 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1991 }
1992 }
1993 if (buf[0] == '\0')
1994 strlcpy(buf, "notty", sizeof buf);
1995 return buf;
1996}
1997
1998void
1999session_proctitle(Session *s)
2000{
2001 if (s->pw == NULL)
2002 error("no user for session %d", s->self);
2003 else
2004 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2005}
2006
Damien Millerefb4afe2000-04-12 18:45:05 +10002007void
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002008do_authenticated2(Authctxt *authctxt)
Damien Millerefb4afe2000-04-12 18:45:05 +10002009{
Damien Millerad833b32000-08-23 10:46:23 +10002010
Damien Millerefb4afe2000-04-12 18:45:05 +10002011 /*
2012 * Cancel the alarm we set to limit the time taken for
2013 * authentication.
2014 */
2015 alarm(0);
Damien Miller182ee6e2000-07-12 09:45:27 +10002016 if (startup_pipe != -1) {
Damien Miller4d97ba22000-07-11 18:15:50 +10002017 close(startup_pipe);
Damien Miller182ee6e2000-07-12 09:45:27 +10002018 startup_pipe = -1;
2019 }
Kevin Steves48b7cc02000-10-07 13:24:00 +00002020#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +00002021 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10002022 error("unable to get login class");
2023 return;
2024 }
2025#endif
Damien Millerefb4afe2000-04-12 18:45:05 +10002026 server_loop2();
Damien Miller1b26ab22000-04-30 10:12:49 +10002027 if (xauthfile)
2028 xauthfile_cleanup_proc(NULL);
Damien Millerefb4afe2000-04-12 18:45:05 +10002029}