]> andersk Git - openssh.git/blame - session.c
missed sshconnect.c part of:
[openssh.git] / session.c
CommitLineData
7368a6c8 1/*
2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 * All rights reserved
bcbf86ec 4 *
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 *
e78a59f5 11 * SSH2 support by Markus Friedl.
12 * Copyright (c) 2000 Markus Friedl. All rights reserved.
bcbf86ec 13 *
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.
e78a59f5 33 */
7368a6c8 34
35#include "includes.h"
5ca51e19 36RCSID("$OpenBSD: session.c,v 1.55 2001/02/08 19:30:52 itojun Exp $");
7368a6c8 37
7368a6c8 38#include "ssh.h"
42f11eb2 39#include "ssh1.h"
40#include "ssh2.h"
41#include "xmalloc.h"
7368a6c8 42#include "pty.h"
43#include "packet.h"
44#include "buffer.h"
7368a6c8 45#include "mpaux.h"
7368a6c8 46#include "uidswap.h"
47#include "compat.h"
48#include "channels.h"
49#include "nchan.h"
e78a59f5 50#include "bufaux.h"
e78a59f5 51#include "auth.h"
38c295d6 52#include "auth-options.h"
42f11eb2 53#include "pathnames.h"
54#include "log.h"
55#include "servconf.h"
56#include "login.h"
57#include "serverloop.h"
58#include "canohost.h"
5ca51e19 59#include "session.h"
e78a59f5 60
3206bb3b 61#ifdef WITH_IRIX_PROJECT
62#include <proj.h>
63#endif /* WITH_IRIX_PROJECT */
d287c664 64#ifdef WITH_IRIX_JOBS
65#include <sys/resource.h>
2b87da3b 66#endif
260d427b 67#ifdef WITH_IRIX_AUDIT
68#include <sat.h>
69#endif /* WITH_IRIX_AUDIT */
3206bb3b 70
089fbbd2 71#if defined(HAVE_USERSEC_H)
72#include <usersec.h>
73#endif
74
4d33e531 75#ifdef HAVE_OSF_SIA
76# include <sia.h>
77# include <siad.h>
78#endif
79
3c62e7eb 80#ifdef HAVE_CYGWIN
81#include <windows.h>
82#include <sys/cygwin.h>
83#define is_winnt (GetVersion() < 0x80000000)
84#endif
85
3fcce26c 86/* AIX limits */
87#if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE)
2a2cb9e7 88# define S_UFSIZE_HARD S_UFSIZE "_hard"
89# define S_UCPU_HARD S_UCPU "_hard"
90# define S_UDATA_HARD S_UDATA "_hard"
91# define S_USTACK_HARD S_USTACK "_hard"
92# define S_URSS_HARD S_URSS "_hard"
93# define S_UCORE_HARD S_UCORE "_hard"
94# define S_UNOFILE_HARD S_UNOFILE "_hard"
3fcce26c 95#endif
96
2e73a022 97#ifdef HAVE_LOGIN_CAP
98#include <login_cap.h>
99#endif
100
7368a6c8 101/* types */
102
103#define TTYSZ 64
104typedef struct Session Session;
105struct Session {
106 int used;
107 int self;
0b242b12 108 int extended;
7368a6c8 109 struct passwd *pw;
110 pid_t pid;
111 /* tty */
112 char *term;
113 int ptyfd, ttyfd, ptymaster;
114 int row, col, xpixel, ypixel;
115 char tty[TTYSZ];
116 /* X11 */
117 char *display;
118 int screen;
119 char *auth_proto;
120 char *auth_data;
0b242b12 121 int single_connection;
7368a6c8 122 /* proto 2 */
123 int chanid;
124};
125
126/* func */
127
128Session *session_new(void);
129void session_set_fds(Session *s, int fdin, int fdout, int fderr);
130void session_pty_cleanup(Session *s);
1d1ffb87 131void session_proctitle(Session *s);
7368a6c8 132void do_exec_pty(Session *s, const char *command, struct passwd * pw);
133void do_exec_no_pty(Session *s, const char *command, struct passwd * pw);
a22aff1f 134void do_login(Session *s, const char *command);
7368a6c8 135
136void
137do_child(const char *command, struct passwd * pw, const char *term,
138 const char *display, const char *auth_proto,
139 const char *auth_data, const char *ttyname);
140
141/* import */
142extern ServerOptions options;
143extern char *__progname;
144extern int log_stderr;
145extern int debug_flag;
1e3b8b07 146extern u_int utmp_len;
7368a6c8 147
089fbbd2 148extern int startup_pipe;
149
7368a6c8 150/* Local Xauthority file. */
151static char *xauthfile;
152
b5e300c2 153/* original command from peer. */
2b87da3b 154char *original_command = NULL;
b5e300c2 155
7368a6c8 156/* data */
157#define MAX_SESSIONS 10
158Session sessions[MAX_SESSIONS];
c5d85828 159
c1ef8333 160#ifdef WITH_AIXAUTHENTICATE
161/* AIX's lastlogin message, set in auth1.c */
162char *aixloginmsg;
163#endif /* WITH_AIXAUTHENTICATE */
7368a6c8 164
2e73a022 165#ifdef HAVE_LOGIN_CAP
166static login_cap_t *lc;
167#endif
168
7368a6c8 169/*
170 * Remove local Xauthority file.
171 */
172void
173xauthfile_cleanup_proc(void *ignore)
174{
175 debug("xauthfile_cleanup_proc called");
176
177 if (xauthfile != NULL) {
178 char *p;
179 unlink(xauthfile);
180 p = strrchr(xauthfile, '/');
181 if (p != NULL) {
182 *p = '\0';
183 rmdir(xauthfile);
184 }
185 xfree(xauthfile);
186 xauthfile = NULL;
187 }
188}
189
190/*
191 * Function to perform cleanup if we get aborted abnormally (e.g., due to a
192 * dropped connection).
193 */
6ae2364d 194void
7368a6c8 195pty_cleanup_proc(void *session)
196{
197 Session *s=session;
198 if (s == NULL)
199 fatal("pty_cleanup_proc: no session");
200 debug("pty_cleanup_proc: %s", s->tty);
201
202 if (s->pid != 0) {
203 /* Record that the user has logged out. */
204 record_logout(s->pid, s->tty);
205 }
206
207 /* Release the pseudo-tty. */
208 pty_release(s->tty);
209}
210
211/*
212 * Prepares for an interactive session. This is called after the user has
213 * been successfully authenticated. During this message exchange, pseudo
214 * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
215 * are requested, etc.
216 */
6ae2364d 217void
7368a6c8 218do_authenticated(struct passwd * pw)
219{
220 Session *s;
b5e300c2 221 int type, fd;
7368a6c8 222 int compression_level = 0, enable_compression_after_reply = 0;
223 int have_pty = 0;
224 char *command;
225 int n_bytes;
226 int plen;
1e3b8b07 227 u_int proto_len, data_len, dlen;
7368a6c8 228
229 /*
230 * Cancel the alarm we set to limit the time taken for
231 * authentication.
232 */
233 alarm(0);
3f7a7e4a 234 if (startup_pipe != -1) {
5540ea9b 235 close(startup_pipe);
3f7a7e4a 236 startup_pipe = -1;
237 }
7368a6c8 238
239 /*
240 * Inform the channel mechanism that we are the server side and that
241 * the client may request to connect to any port at all. (The user
242 * could do it anyway, and we wouldn\'t know what is permitted except
243 * by the client telling us, so we can equally well trust the client
244 * not to request anything bogus.)
245 */
33de75a3 246 if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
7368a6c8 247 channel_permit_all_opens();
248
249 s = session_new();
0b242b12 250 s->pw = pw;
7368a6c8 251
97994d32 252#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
2e73a022 253 if ((lc = login_getclass(pw->pw_class)) == NULL) {
254 error("unable to get login class");
255 return;
256 }
257#endif
258
7368a6c8 259 /*
260 * We stay in this loop until the client requests to execute a shell
261 * or a command.
262 */
263 for (;;) {
264 int success = 0;
265
266 /* Get a packet from the client. */
267 type = packet_read(&plen);
268
269 /* Process the packet. */
270 switch (type) {
271 case SSH_CMSG_REQUEST_COMPRESSION:
272 packet_integrity_check(plen, 4, type);
273 compression_level = packet_get_int();
274 if (compression_level < 1 || compression_level > 9) {
275 packet_send_debug("Received illegal compression level %d.",
276 compression_level);
277 break;
278 }
279 /* Enable compression after we have responded with SUCCESS. */
280 enable_compression_after_reply = 1;
281 success = 1;
282 break;
283
284 case SSH_CMSG_REQUEST_PTY:
285 if (no_pty_flag) {
286 debug("Allocating a pty not permitted for this authentication.");
287 break;
288 }
289 if (have_pty)
290 packet_disconnect("Protocol error: you already have a pty.");
291
292 debug("Allocating pty.");
293
294 /* Allocate a pty and open it. */
295 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
296 sizeof(s->tty))) {
297 error("Failed to allocate pty.");
298 break;
299 }
300 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
301 pty_setowner(pw, s->tty);
302
303 /* Get TERM from the packet. Note that the value may be of arbitrary length. */
304 s->term = packet_get_string(&dlen);
305 packet_integrity_check(dlen, strlen(s->term), type);
306 /* packet_integrity_check(plen, 4 + dlen + 4*4 + n_bytes, type); */
307 /* Remaining bytes */
308 n_bytes = plen - (4 + dlen + 4 * 4);
309
310 if (strcmp(s->term, "") == 0) {
311 xfree(s->term);
312 s->term = NULL;
313 }
314 /* Get window size from the packet. */
315 s->row = packet_get_int();
316 s->col = packet_get_int();
317 s->xpixel = packet_get_int();
318 s->ypixel = packet_get_int();
319 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
320
321 /* Get tty modes from the packet. */
322 tty_parse_modes(s->ttyfd, &n_bytes);
323 packet_integrity_check(plen, 4 + dlen + 4 * 4 + n_bytes, type);
324
1d1ffb87 325 session_proctitle(s);
326
7368a6c8 327 /* Indicate that we now have a pty. */
328 success = 1;
329 have_pty = 1;
330 break;
331
332 case SSH_CMSG_X11_REQUEST_FORWARDING:
333 if (!options.x11_forwarding) {
334 packet_send_debug("X11 forwarding disabled in server configuration file.");
335 break;
336 }
6fc1e9f3 337 if (!options.xauth_location) {
338 packet_send_debug("No xauth program; cannot forward with spoofing.");
339 break;
340 }
7368a6c8 341 if (no_x11_forwarding_flag) {
342 packet_send_debug("X11 forwarding not permitted for this authentication.");
343 break;
344 }
345 debug("Received request for X11 forwarding with auth spoofing.");
346 if (s->display != NULL)
347 packet_disconnect("Protocol error: X11 display already set.");
348
349 s->auth_proto = packet_get_string(&proto_len);
350 s->auth_data = packet_get_string(&data_len);
351 packet_integrity_check(plen, 4 + proto_len + 4 + data_len + 4, type);
352
353 if (packet_get_protocol_flags() & SSH_PROTOFLAG_SCREEN_NUMBER)
354 s->screen = packet_get_int();
355 else
356 s->screen = 0;
357 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
358
359 if (s->display == NULL)
360 break;
361
362 /* Setup to always have a local .Xauthority. */
363 xauthfile = xmalloc(MAXPATHLEN);
364 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
365 temporarily_use_uid(pw->pw_uid);
366 if (mkdtemp(xauthfile) == NULL) {
367 restore_uid();
368 error("private X11 dir: mkdtemp %s failed: %s",
369 xauthfile, strerror(errno));
370 xfree(xauthfile);
371 xauthfile = NULL;
0b242b12 372 /* XXXX remove listening channels */
7368a6c8 373 break;
374 }
375 strlcat(xauthfile, "/cookies", MAXPATHLEN);
b5e300c2 376 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
377 if (fd >= 0)
378 close(fd);
7368a6c8 379 restore_uid();
380 fatal_add_cleanup(xauthfile_cleanup_proc, NULL);
381 success = 1;
382 break;
7368a6c8 383
384 case SSH_CMSG_AGENT_REQUEST_FORWARDING:
385 if (no_agent_forwarding_flag || compat13) {
386 debug("Authentication agent forwarding not permitted for this authentication.");
387 break;
388 }
389 debug("Received authentication agent forwarding request.");
6fc1e9f3 390 success = auth_input_request_forwarding(pw);
7368a6c8 391 break;
392
393 case SSH_CMSG_PORT_FORWARD_REQUEST:
394 if (no_port_forwarding_flag) {
395 debug("Port forwarding not permitted for this authentication.");
396 break;
397 }
33de75a3 398 if (!options.allow_tcp_forwarding) {
399 debug("Port forwarding not permitted.");
400 break;
401 }
7368a6c8 402 debug("Received TCP/IP port forwarding request.");
1d1ffb87 403 channel_input_port_forward_request(pw->pw_uid == 0, options.gateway_ports);
7368a6c8 404 success = 1;
405 break;
406
407 case SSH_CMSG_MAX_PACKET_SIZE:
408 if (packet_set_maxsize(packet_get_int()) > 0)
409 success = 1;
410 break;
411
412 case SSH_CMSG_EXEC_SHELL:
413 case SSH_CMSG_EXEC_CMD:
7368a6c8 414 if (type == SSH_CMSG_EXEC_CMD) {
415 command = packet_get_string(&dlen);
416 debug("Exec command '%.500s'", command);
417 packet_integrity_check(plen, 4 + dlen, type);
418 } else {
419 command = NULL;
420 packet_integrity_check(plen, 0, type);
421 }
422 if (forced_command != NULL) {
b5e300c2 423 original_command = command;
7368a6c8 424 command = forced_command;
425 debug("Forced command '%.500s'", forced_command);
426 }
427 if (have_pty)
428 do_exec_pty(s, command, pw);
429 else
430 do_exec_no_pty(s, command, pw);
431
432 if (command != NULL)
433 xfree(command);
434 /* Cleanup user's local Xauthority file. */
435 if (xauthfile)
436 xauthfile_cleanup_proc(NULL);
437 return;
438
439 default:
440 /*
441 * Any unknown messages in this phase are ignored,
442 * and a failure message is returned.
443 */
444 log("Unknown packet type received after authentication: %d", type);
445 }
446 packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
447 packet_send();
448 packet_write_wait();
449
450 /* Enable compression now that we have replied if appropriate. */
451 if (enable_compression_after_reply) {
452 enable_compression_after_reply = 0;
453 packet_start_compression(compression_level);
454 }
455 }
456}
457
458/*
459 * This is called to fork and execute a command when we have no tty. This
460 * will call do_child from the child, and server_loop from the parent after
461 * setting up file descriptors and such.
462 */
6ae2364d 463void
7368a6c8 464do_exec_no_pty(Session *s, const char *command, struct passwd * pw)
465{
466 int pid;
467
468#ifdef USE_PIPES
469 int pin[2], pout[2], perr[2];
470 /* Allocate pipes for communicating with the program. */
471 if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0)
472 packet_disconnect("Could not create pipes: %.100s",
473 strerror(errno));
474#else /* USE_PIPES */
475 int inout[2], err[2];
476 /* Uses socket pairs to communicate with the program. */
477 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 ||
478 socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0)
479 packet_disconnect("Could not create socket pairs: %.100s",
480 strerror(errno));
481#endif /* USE_PIPES */
482 if (s == NULL)
483 fatal("do_exec_no_pty: no session");
484
1d1ffb87 485 session_proctitle(s);
7368a6c8 486
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
6c935fbd 496 signal(SIGPIPE, SIG_DFL);
497
7368a6c8 498 /*
499 * Create a new session and process group since the 4.4BSD
500 * setlogin() affects the entire process group.
501 */
502 if (setsid() < 0)
503 error("setsid failed: %.100s", strerror(errno));
504
505#ifdef USE_PIPES
506 /*
507 * Redirect stdin. We close the parent side of the socket
508 * pair, and make the child side the standard input.
509 */
510 close(pin[1]);
511 if (dup2(pin[0], 0) < 0)
512 perror("dup2 stdin");
513 close(pin[0]);
514
515 /* Redirect stdout. */
516 close(pout[0]);
517 if (dup2(pout[1], 1) < 0)
518 perror("dup2 stdout");
519 close(pout[1]);
520
521 /* Redirect stderr. */
522 close(perr[0]);
523 if (dup2(perr[1], 2) < 0)
524 perror("dup2 stderr");
525 close(perr[1]);
526#else /* USE_PIPES */
527 /*
528 * Redirect stdin, stdout, and stderr. Stdin and stdout will
529 * use the same socket, as some programs (particularly rdist)
530 * seem to depend on it.
531 */
532 close(inout[1]);
533 close(err[1]);
534 if (dup2(inout[0], 0) < 0) /* stdin */
535 perror("dup2 stdin");
536 if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */
537 perror("dup2 stdout");
538 if (dup2(err[0], 2) < 0) /* stderr */
539 perror("dup2 stderr");
540#endif /* USE_PIPES */
541
542 /* Do processing for the child (exec command etc). */
543 do_child(command, pw, NULL, s->display, s->auth_proto, s->auth_data, NULL);
544 /* NOTREACHED */
545 }
3c62e7eb 546#ifdef HAVE_CYGWIN
547 if (is_winnt)
548 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
549#endif
7368a6c8 550 if (pid < 0)
551 packet_disconnect("fork failed: %.100s", strerror(errno));
552 s->pid = pid;
b5c334cc 553 /* Set interactive/non-interactive mode. */
554 packet_set_interactive(s->display != NULL);
7368a6c8 555#ifdef USE_PIPES
556 /* We are the parent. Close the child sides of the pipes. */
557 close(pin[0]);
558 close(pout[1]);
559 close(perr[1]);
560
e78a59f5 561 if (compat20) {
0b242b12 562 session_set_fds(s, pin[1], pout[0], s->extended ? perr[0] : -1);
e78a59f5 563 } else {
564 /* Enter the interactive session. */
565 server_loop(pid, pin[1], pout[0], perr[0]);
566 /* server_loop has closed pin[1], pout[1], and perr[1]. */
567 }
7368a6c8 568#else /* USE_PIPES */
569 /* We are the parent. Close the child sides of the socket pairs. */
570 close(inout[0]);
571 close(err[0]);
572
573 /*
574 * Enter the interactive session. Note: server_loop must be able to
575 * handle the case that fdin and fdout are the same.
576 */
e78a59f5 577 if (compat20) {
0b242b12 578 session_set_fds(s, inout[1], inout[1], s->extended ? err[1] : -1);
e78a59f5 579 } else {
580 server_loop(pid, inout[1], inout[1], err[1]);
581 /* server_loop has closed inout[1] and err[1]. */
582 }
7368a6c8 583#endif /* USE_PIPES */
584}
585
586/*
587 * This is called to fork and execute a command when we have a tty. This
588 * will call do_child from the child, and server_loop from the parent after
589 * setting up file descriptors, controlling tty, updating wtmp, utmp,
590 * lastlog, and other such operations.
591 */
6ae2364d 592void
7368a6c8 593do_exec_pty(Session *s, const char *command, struct passwd * pw)
594{
7368a6c8 595 int fdout, ptyfd, ttyfd, ptymaster;
7368a6c8 596 pid_t pid;
7368a6c8 597
598 if (s == NULL)
599 fatal("do_exec_pty: no session");
600 ptyfd = s->ptyfd;
601 ttyfd = s->ttyfd;
602
7368a6c8 603#ifdef USE_PAM
604 do_pam_session(pw->pw_name, s->tty);
605 do_pam_setcred();
606#endif /* USE_PAM */
607
608 /* Fork the child. */
609 if ((pid = fork()) == 0) {
c345cf9d 610 /* Child. Reinitialize the log because the pid has changed. */
7368a6c8 611 log_init(__progname, options.log_level, options.log_facility, log_stderr);
612
6c935fbd 613 signal(SIGPIPE, SIG_DFL);
614
7368a6c8 615 /* Close the master side of the pseudo tty. */
616 close(ptyfd);
617
618 /* Make the pseudo tty our controlling tty. */
619 pty_make_controlling_tty(&ttyfd, s->tty);
620
621 /* Redirect stdin from the pseudo tty. */
622 if (dup2(ttyfd, fileno(stdin)) < 0)
623 error("dup2 stdin failed: %.100s", strerror(errno));
624
625 /* Redirect stdout to the pseudo tty. */
626 if (dup2(ttyfd, fileno(stdout)) < 0)
627 error("dup2 stdin failed: %.100s", strerror(errno));
628
629 /* Redirect stderr to the pseudo tty. */
630 if (dup2(ttyfd, fileno(stderr)) < 0)
631 error("dup2 stdin failed: %.100s", strerror(errno));
632
633 /* Close the extra descriptor for the pseudo tty. */
634 close(ttyfd);
635
c345cf9d 636 /* record login, etc. similar to login(1) */
a22aff1f 637 if (!(options.use_login && command == NULL))
638 do_login(s, command);
7368a6c8 639
7368a6c8 640 /* Do common processing for the child, such as execing the command. */
71276795 641 do_child(command, pw, s->term, s->display, s->auth_proto,
642 s->auth_data, s->tty);
7368a6c8 643 /* NOTREACHED */
644 }
3c62e7eb 645#ifdef HAVE_CYGWIN
646 if (is_winnt)
647 cygwin_set_impersonation_token(INVALID_HANDLE_VALUE);
648#endif
7368a6c8 649 if (pid < 0)
650 packet_disconnect("fork failed: %.100s", strerror(errno));
651 s->pid = pid;
652
653 /* Parent. Close the slave side of the pseudo tty. */
654 close(ttyfd);
655
656 /*
657 * Create another descriptor of the pty master side for use as the
658 * standard input. We could use the original descriptor, but this
659 * simplifies code in server_loop. The descriptor is bidirectional.
660 */
661 fdout = dup(ptyfd);
662 if (fdout < 0)
663 packet_disconnect("dup #1 failed: %.100s", strerror(errno));
664
665 /* we keep a reference to the pty master */
666 ptymaster = dup(ptyfd);
667 if (ptymaster < 0)
668 packet_disconnect("dup #2 failed: %.100s", strerror(errno));
669 s->ptymaster = ptymaster;
670
671 /* Enter interactive session. */
b5c334cc 672 packet_set_interactive(1);
e78a59f5 673 if (compat20) {
674 session_set_fds(s, ptyfd, fdout, -1);
675 } else {
676 server_loop(pid, ptyfd, fdout, -1);
677 /* server_loop _has_ closed ptyfd and fdout. */
678 session_pty_cleanup(s);
679 }
7368a6c8 680}
681
c345cf9d 682const char *
683get_remote_name_or_ip(void)
684{
685 static const char *remote = "";
686 if (utmp_len > 0)
61e96248 687 remote = get_canonical_hostname(options.reverse_mapping_check);
c345cf9d 688 if (utmp_len == 0 || strlen(remote) > utmp_len)
689 remote = get_remote_ipaddr();
690 return remote;
691}
692
693/* administrative, login(1)-like work */
694void
a22aff1f 695do_login(Session *s, const char *command)
c345cf9d 696{
697 FILE *f;
698 char *time_string;
699 char buf[256];
b5e300c2 700 char hostname[MAXHOSTNAMELEN];
c345cf9d 701 socklen_t fromlen;
702 struct sockaddr_storage from;
703 struct stat st;
704 time_t last_login_time;
705 struct passwd * pw = s->pw;
706 pid_t pid = getpid();
707
708 /*
709 * Get IP address of client. If the connection is not a socket, let
710 * the address be 0.0.0.0.
711 */
712 memset(&from, 0, sizeof(from));
713 if (packet_connection_is_on_socket()) {
714 fromlen = sizeof(from);
715 if (getpeername(packet_get_connection_in(),
716 (struct sockaddr *) & from, &fromlen) < 0) {
717 debug("getpeername: %.100s", strerror(errno));
718 fatal_cleanup();
719 }
720 }
721
bcbf86ec 722 /* Get the time and hostname when the user last logged in. */
723 hostname[0] = '\0';
724 last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
725 hostname, sizeof(hostname));
726
c345cf9d 727 /* Record that there was a login on that tty from the remote host. */
728 record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
729 get_remote_name_or_ip(), (struct sockaddr *)&from);
730
2919e060 731#ifdef USE_PAM
732 /*
733 * If password change is needed, do it now.
734 * This needs to occur before the ~/.hushlogin check.
735 */
736 if (pam_password_change_required()) {
737 print_pam_messages();
738 do_pam_chauthtok();
739 }
740#endif
741
a22aff1f 742 /* Done if .hushlogin exists or a command given. */
743 if (command != NULL)
744 return;
c345cf9d 745 snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
2e73a022 746#ifdef HAVE_LOGIN_CAP
747 if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
748#else
c345cf9d 749 if (stat(buf, &st) >= 0)
2e73a022 750#endif
c345cf9d 751 return;
752
753#ifdef USE_PAM
2919e060 754 if (!pam_password_change_required())
755 print_pam_messages();
c345cf9d 756#endif /* USE_PAM */
757#ifdef WITH_AIXAUTHENTICATE
758 if (aixloginmsg && *aixloginmsg)
759 printf("%s\n", aixloginmsg);
760#endif /* WITH_AIXAUTHENTICATE */
761
c345cf9d 762 if (last_login_time != 0) {
763 time_string = ctime(&last_login_time);
764 if (strchr(time_string, '\n'))
765 *strchr(time_string, '\n') = 0;
f478712e 766 if (strcmp(hostname, "") == 0)
c345cf9d 767 printf("Last login: %s\r\n", time_string);
768 else
bcbf86ec 769 printf("Last login: %s from %s\r\n", time_string, hostname);
c345cf9d 770 }
771 if (options.print_motd) {
2e73a022 772#ifdef HAVE_LOGIN_CAP
773 f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
774 "/etc/motd"), "r");
775#else
c345cf9d 776 f = fopen("/etc/motd", "r");
2e73a022 777#endif
c345cf9d 778 if (f) {
779 while (fgets(buf, sizeof(buf), f))
780 fputs(buf, stdout);
781 fclose(f);
782 }
783 }
784}
785
7368a6c8 786/*
787 * Sets the value of the given variable in the environment. If the variable
788 * already exists, its value is overriden.
789 */
6ae2364d 790void
1e3b8b07 791child_set_env(char ***envp, u_int *envsizep, const char *name,
7368a6c8 792 const char *value)
793{
1e3b8b07 794 u_int i, namelen;
7368a6c8 795 char **env;
796
797 /*
798 * Find the slot where the value should be stored. If the variable
799 * already exists, we reuse the slot; otherwise we append a new slot
800 * at the end of the array, expanding if necessary.
801 */
802 env = *envp;
803 namelen = strlen(name);
804 for (i = 0; env[i]; i++)
805 if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
806 break;
807 if (env[i]) {
808 /* Reuse the slot. */
809 xfree(env[i]);
810 } else {
811 /* New variable. Expand if necessary. */
812 if (i >= (*envsizep) - 1) {
813 (*envsizep) += 50;
814 env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
815 }
816 /* Need to set the NULL pointer at end of array beyond the new slot. */
817 env[i + 1] = NULL;
818 }
819
820 /* Allocate space and format the variable in the appropriate slot. */
821 env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
822 snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
823}
824
825/*
826 * Reads environment variables from the given file and adds/overrides them
827 * into the environment. If the file does not exist, this does nothing.
828 * Otherwise, it must consist of empty lines, comments (line starts with '#')
829 * and assignments of the form name=value. No other forms are allowed.
830 */
6ae2364d 831void
1e3b8b07 832read_environment_file(char ***env, u_int *envsize,
7368a6c8 833 const char *filename)
834{
835 FILE *f;
836 char buf[4096];
837 char *cp, *value;
838
839 f = fopen(filename, "r");
840 if (!f)
841 return;
842
843 while (fgets(buf, sizeof(buf), f)) {
844 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
845 ;
846 if (!*cp || *cp == '#' || *cp == '\n')
847 continue;
848 if (strchr(cp, '\n'))
849 *strchr(cp, '\n') = '\0';
850 value = strchr(cp, '=');
851 if (value == NULL) {
852 fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
853 continue;
854 }
71276795 855 /*
856 * Replace the equals sign by nul, and advance value to
857 * the value string.
858 */
7368a6c8 859 *value = '\0';
860 value++;
861 child_set_env(env, envsize, cp, value);
862 }
863 fclose(f);
864}
865
866#ifdef USE_PAM
867/*
868 * Sets any environment variables which have been specified by PAM
869 */
870void do_pam_environment(char ***env, int *envsize)
871{
872 char *equals, var_name[512], var_val[512];
873 char **pam_env;
874 int i;
875
876 if ((pam_env = fetch_pam_environment()) == NULL)
877 return;
2b87da3b 878
7368a6c8 879 for(i = 0; pam_env[i] != NULL; i++) {
880 if ((equals = strstr(pam_env[i], "=")) == NULL)
881 continue;
2b87da3b 882
7368a6c8 883 if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) {
884 memset(var_name, '\0', sizeof(var_name));
885 memset(var_val, '\0', sizeof(var_val));
886
887 strncpy(var_name, pam_env[i], equals - pam_env[i]);
888 strcpy(var_val, equals + 1);
889
22d89d24 890 debug3("PAM environment: %s=%s", var_name, var_val);
7368a6c8 891
892 child_set_env(env, envsize, var_name, var_val);
893 }
894 }
895}
896#endif /* USE_PAM */
897
22d89d24 898
899#ifdef HAVE_CYGWIN
900void copy_environment(char ***env, int *envsize)
901{
902 char *equals, var_name[512], var_val[512];
903 int i;
904
905 for(i = 0; environ[i] != NULL; i++) {
906 if ((equals = strstr(environ[i], "=")) == NULL)
907 continue;
2b87da3b 908
22d89d24 909 if (strlen(environ[i]) < (sizeof(var_name) - 1)) {
910 memset(var_name, '\0', sizeof(var_name));
911 memset(var_val, '\0', sizeof(var_val));
912
913 strncpy(var_name, environ[i], equals - environ[i]);
914 strcpy(var_val, equals + 1);
915
916 debug3("Copy environment: %s=%s", var_name, var_val);
917
918 child_set_env(env, envsize, var_name, var_val);
919 }
920 }
921}
922#endif
923
5fa45897 924#if defined(HAVE_GETUSERATTR)
925/*
926 * AIX-specific login initialisation
927 */
928void set_limit(char *user, char *soft, char *hard, int resource, int mult)
929{
930 struct rlimit rlim;
56118702 931 int slim, hlim;
5fa45897 932
933 getrlimit(resource, &rlim);
934
56118702 935 slim = 0;
936 if (getuserattr(user, soft, &slim, SEC_INT) != -1) {
937 if (slim < 0) {
938 rlim.rlim_cur = RLIM_INFINITY;
939 } else if (slim != 0) {
940 /* See the wackiness below */
941 if (rlim.rlim_cur == slim * mult)
942 slim = 0;
943 else
944 rlim.rlim_cur = slim * mult;
945 }
946 }
5fa45897 947
56118702 948 hlim = 0;
949 if (getuserattr(user, hard, &hlim, SEC_INT) != -1) {
950 if (hlim < 0) {
951 rlim.rlim_max = RLIM_INFINITY;
952 } else if (hlim != 0) {
953 rlim.rlim_max = hlim * mult;
954 }
955 }
5fa45897 956
56118702 957 /*
958 * XXX For cpu and fsize the soft limit is set to the hard limit
959 * if the hard limit is left at its default value and the soft limit
960 * is changed from its default value, either by requesting it
961 * (slim == 0) or by setting it to the current default. At least
962 * that's how rlogind does it. If you're confused you're not alone.
963 * Bug or feature? AIX 4.3.1.2
964 */
965 if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu"))
966 && hlim == 0 && slim != 0)
967 rlim.rlim_max = rlim.rlim_cur;
968 /* A specified hard limit limits the soft limit */
969 else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max)
970 rlim.rlim_cur = rlim.rlim_max;
971 /* A soft limit can increase a hard limit */
972 else if (rlim.rlim_cur > rlim.rlim_max)
5fa45897 973 rlim.rlim_max = rlim.rlim_cur;
974
975 if (setrlimit(resource, &rlim) != 0)
56118702 976 error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno));
5fa45897 977}
978
979void set_limits_from_userattr(char *user)
980{
981 int mask;
982 char buf[16];
983
984 set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512);
985 set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1);
986 set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512);
987 set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512);
988 set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512);
989 set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512);
990#if defined(S_UNOFILE)
991 set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1);
992#endif
993
994 if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) {
995 /* Convert decimal to octal */
996 (void) snprintf(buf, sizeof(buf), "%d", mask);
997 if (sscanf(buf, "%o", &mask) == 1)
998 umask(mask);
999 }
1000}
1001#endif /* defined(HAVE_GETUSERATTR) */
1002
7368a6c8 1003/*
1004 * Performs common processing for the child, such as setting up the
1005 * environment, closing extra file descriptors, setting the user and group
1006 * ids, and executing the command or shell.
1007 */
6ae2364d 1008void
7368a6c8 1009do_child(const char *command, struct passwd * pw, const char *term,
1010 const char *display, const char *auth_proto,
1011 const char *auth_data, const char *ttyname)
1012{
b5e300c2 1013 const char *shell, *hostname = NULL, *cp = NULL;
7368a6c8 1014 char buf[256];
6fc1e9f3 1015 char cmd[1024];
2e73a022 1016 FILE *f = NULL;
1e3b8b07 1017 u_int envsize, i;
7368a6c8 1018 char **env;
1019 extern char **environ;
1020 struct stat st;
1021 char *argv[10];
3206bb3b 1022#ifdef WITH_IRIX_PROJECT
1023 prid_t projid;
1024#endif /* WITH_IRIX_PROJECT */
d287c664 1025#ifdef WITH_IRIX_JOBS
1026 jid_t jid = 0;
1027#else
1028#ifdef WITH_IRIX_ARRAY
1029 int jid = 0;
1030#endif /* WITH_IRIX_ARRAY */
1031#endif /* WITH_IRIX_JOBS */
1032
7368a6c8 1033
fa649821 1034 /* login(1) is only called if we execute the login shell */
1035 if (options.use_login && command != NULL)
1036 options.use_login = 0;
1037
7368a6c8 1038#ifndef USE_PAM /* pam_nologin handles this */
2e73a022 1039 if (!options.use_login) {
1040# ifdef HAVE_LOGIN_CAP
1041 if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1042 f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1043 _PATH_NOLOGIN), "r");
1044# else /* HAVE_LOGIN_CAP */
1045 if (pw->pw_uid)
1046 f = fopen(_PATH_NOLOGIN, "r");
1047# endif /* HAVE_LOGIN_CAP */
1048 if (f) {
1049 /* /etc/nologin exists. Print its contents and exit. */
1050 while (fgets(buf, sizeof(buf), f))
1051 fputs(buf, stderr);
1052 fclose(f);
7368a6c8 1053 exit(254);
2e73a022 1054 }
7368a6c8 1055 }
1056#endif /* USE_PAM */
1057
2e73a022 1058 /* Set login name, uid, gid, and groups. */
7368a6c8 1059 /* Login(1) does this as well, and it needs uid 0 for the "-h"
1060 switch, so we let login(1) to this for us. */
1061 if (!options.use_login) {
4d33e531 1062#ifdef HAVE_OSF_SIA
1063 extern char **saved_argv;
1064 extern int saved_argc;
61e96248 1065 char *host = get_canonical_hostname(options.reverse_mapping_check);
4d33e531 1066
1067 if (sia_become_user(NULL, saved_argc, saved_argv, host,
1068 pw->pw_name, ttyname, 0, NULL, NULL, SIA_BEU_SETLUID) !=
1069 SIASUCCESS) {
1070 perror("sia_become_user");
1071 exit(1);
1072 }
1073 if (setreuid(geteuid(), geteuid()) < 0) {
1074 perror("setreuid");
1075 exit(1);
1076 }
1077#else /* HAVE_OSF_SIA */
3c62e7eb 1078#ifdef HAVE_CYGWIN
1079 if (is_winnt) {
1080#else
7368a6c8 1081 if (getuid() == 0 || geteuid() == 0) {
3c62e7eb 1082#endif
2e73a022 1083# ifdef HAVE_GETUSERATTR
5fa45897 1084 set_limits_from_userattr(pw->pw_name);
2e73a022 1085# endif /* HAVE_GETUSERATTR */
1086# ifdef HAVE_LOGIN_CAP
1087 if (setusercontext(lc, pw, pw->pw_uid,
1088 (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
1089 perror("unable to set user context");
1090 exit(1);
1091 }
1092# else /* HAVE_LOGIN_CAP */
1093 if (setlogin(pw->pw_name) < 0)
1094 error("setlogin failed: %s", strerror(errno));
7368a6c8 1095 if (setgid(pw->pw_gid) < 0) {
1096 perror("setgid");
1097 exit(1);
1098 }
1099 /* Initialize the group list. */
1100 if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1101 perror("initgroups");
1102 exit(1);
1103 }
1104 endgrent();
d287c664 1105# ifdef WITH_IRIX_JOBS
1106 jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive");
1107 if (jid == -1) {
1108 fatal("Failed to create job container: %.100s",
1109 strerror(errno));
2b87da3b 1110 }
d287c664 1111# endif /* WITH_IRIX_JOBS */
2e73a022 1112# ifdef WITH_IRIX_ARRAY
3206bb3b 1113 /* initialize array session */
d287c664 1114 if (jid == 0) {
1115 if (newarraysess() != 0)
1116 fatal("Failed to set up new array session: %.100s",
1117 strerror(errno));
1118 }
2e73a022 1119# endif /* WITH_IRIX_ARRAY */
1120# ifdef WITH_IRIX_PROJECT
3206bb3b 1121 /* initialize irix project info */
1122 if ((projid = getdfltprojuser(pw->pw_name)) == -1) {
1123 debug("Failed to get project id, using projid 0");
1124 projid = 0;
1125 }
3206bb3b 1126 if (setprid(projid))
1127 fatal("Failed to initialize project %d for %s: %.100s",
1128 (int)projid, pw->pw_name, strerror(errno));
2e73a022 1129# endif /* WITH_IRIX_PROJECT */
260d427b 1130#ifdef WITH_IRIX_AUDIT
1131 if (sysconf(_SC_AUDIT)) {
1132 debug("Setting sat id to %d", (int) pw->pw_uid);
1133 if (satsetid(pw->pw_uid))
1134 debug("error setting satid: %.100s", strerror(errno));
1135 }
1136#endif /* WITH_IRIX_AUDIT */
1137
7368a6c8 1138 /* Permanently switch to the desired uid. */
1139 permanently_set_uid(pw->pw_uid);
2e73a022 1140# endif /* HAVE_LOGIN_CAP */
7368a6c8 1141 }
2e73a022 1142#endif /* HAVE_OSF_SIA */
1143
3c62e7eb 1144#ifdef HAVE_CYGWIN
1145 if (is_winnt)
1146#endif
7368a6c8 1147 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
14a9a859 1148 fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
7368a6c8 1149 }
1150 /*
1151 * Get the shell from the password data. An empty shell field is
1152 * legal, and means /bin/sh.
1153 */
1154 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
2e73a022 1155#ifdef HAVE_LOGIN_CAP
1156 shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1157#endif
7368a6c8 1158
1159#ifdef AFS
1160 /* Try to get AFS tokens for the local cell. */
1161 if (k_hasafs()) {
1162 char cell[64];
1163
1164 if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1165 krb_afslog(cell, 0);
1166
1167 krb_afslog(0, 0);
1168 }
1169#endif /* AFS */
1170
1171 /* Initialize the environment. */
1172 envsize = 100;
1173 env = xmalloc(envsize * sizeof(char *));
1174 env[0] = NULL;
1175
3c62e7eb 1176#ifdef HAVE_CYGWIN
1177 /*
1178 * The Windows environment contains some setting which are
1179 * important for a running system. They must not be dropped.
1180 */
22d89d24 1181 copy_environment(&env, &envsize);
3c62e7eb 1182#endif
1183
7368a6c8 1184 if (!options.use_login) {
1185 /* Set basic environment. */
1186 child_set_env(&env, &envsize, "USER", pw->pw_name);
1187 child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
1188 child_set_env(&env, &envsize, "HOME", pw->pw_dir);
2e73a022 1189#ifdef HAVE_LOGIN_CAP
1190 (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
1191 child_set_env(&env, &envsize, "PATH", getenv("PATH"));
22d89d24 1192#else /* HAVE_LOGIN_CAP */
1193# ifndef HAVE_CYGWIN
3c62e7eb 1194 /*
1195 * There's no standard path on Windows. The path contains
1196 * important components pointing to the system directories,
1197 * needed for loading shared libraries. So the path better
1198 * remains intact here.
1199 */
7368a6c8 1200 child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
22d89d24 1201# endif /* HAVE_CYGWIN */
1202#endif /* HAVE_LOGIN_CAP */
7368a6c8 1203
1204 snprintf(buf, sizeof buf, "%.200s/%.50s",
1205 _PATH_MAILDIR, pw->pw_name);
1206 child_set_env(&env, &envsize, "MAIL", buf);
1207
1208 /* Normal systems set SHELL by default. */
1209 child_set_env(&env, &envsize, "SHELL", shell);
1210 }
1211 if (getenv("TZ"))
1212 child_set_env(&env, &envsize, "TZ", getenv("TZ"));
1213
1214 /* Set custom environment options from RSA authentication. */
1215 while (custom_environment) {
1216 struct envstring *ce = custom_environment;
1217 char *s = ce->s;
1218 int i;
1219 for (i = 0; s[i] != '=' && s[i]; i++);
1220 if (s[i] == '=') {
1221 s[i] = 0;
1222 child_set_env(&env, &envsize, s, s + i + 1);
1223 }
1224 custom_environment = ce->next;
1225 xfree(ce->s);
1226 xfree(ce);
1227 }
1228
1229 snprintf(buf, sizeof buf, "%.50s %d %d",
1230 get_remote_ipaddr(), get_remote_port(), get_local_port());
1231 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1232
1233 if (ttyname)
1234 child_set_env(&env, &envsize, "SSH_TTY", ttyname);
1235 if (term)
1236 child_set_env(&env, &envsize, "TERM", term);
1237 if (display)
1238 child_set_env(&env, &envsize, "DISPLAY", display);
b5e300c2 1239 if (original_command)
1240 child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
1241 original_command);
7368a6c8 1242
1243#ifdef _AIX
22d89d24 1244 if ((cp = getenv("AUTHSTATE")) != NULL)
1245 child_set_env(&env, &envsize, "AUTHSTATE", cp);
1246 if ((cp = getenv("KRB5CCNAME")) != NULL)
1247 child_set_env(&env, &envsize, "KRB5CCNAME", cp);
1248 read_environment_file(&env, &envsize, "/etc/environment");
7368a6c8 1249#endif
1250
1251#ifdef KRB4
1252 {
1253 extern char *ticket;
1254
1255 if (ticket)
1256 child_set_env(&env, &envsize, "KRBTKFILE", ticket);
1257 }
1258#endif /* KRB4 */
1259
1260#ifdef USE_PAM
1261 /* Pull in any environment variables that may have been set by PAM. */
1262 do_pam_environment(&env, &envsize);
1263#endif /* USE_PAM */
1264
7368a6c8 1265 if (xauthfile)
1266 child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
1267 if (auth_get_socket_name() != NULL)
1268 child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1269 auth_get_socket_name());
1270
1271 /* read $HOME/.ssh/environment. */
1272 if (!options.use_login) {
71276795 1273 snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
1274 pw->pw_dir);
7368a6c8 1275 read_environment_file(&env, &envsize, buf);
1276 }
1277 if (debug_flag) {
1278 /* dump the environment */
1279 fprintf(stderr, "Environment:\n");
1280 for (i = 0; env[i]; i++)
1281 fprintf(stderr, " %.200s\n", env[i]);
1282 }
2e73a022 1283 /* we have to stash the hostname before we close our socket. */
1284 if (options.use_login)
1285 hostname = get_remote_name_or_ip();
7368a6c8 1286 /*
1287 * Close the connection descriptors; note that this is the child, and
1288 * the server will still have the socket open, and it is important
1289 * that we do not shutdown it. Note that the descriptors cannot be
1290 * closed before building the environment, as we call
1291 * get_remote_ipaddr there.
1292 */
1293 if (packet_get_connection_in() == packet_get_connection_out())
1294 close(packet_get_connection_in());
1295 else {
1296 close(packet_get_connection_in());
1297 close(packet_get_connection_out());
1298 }
1299 /*
1300 * Close all descriptors related to channels. They will still remain
1301 * open in the parent.
1302 */
1303 /* XXX better use close-on-exec? -markus */
1304 channel_close_all();
1305
1306 /*
1307 * Close any extra file descriptors. Note that there may still be
1308 * descriptors left by system functions. They will be closed later.
1309 */
1310 endpwent();
1311
1312 /*
1313 * Close any extra open file descriptors so that we don\'t have them
1314 * hanging around in clients. Note that we want to do this after
1315 * initgroups, because at least on Solaris 2.3 it leaves file
1316 * descriptors open.
1317 */
1318 for (i = 3; i < 64; i++)
1319 close(i);
1320
1321 /* Change current directory to the user\'s home directory. */
2e73a022 1322 if (chdir(pw->pw_dir) < 0) {
7368a6c8 1323 fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1324 pw->pw_dir, strerror(errno));
2e73a022 1325#ifdef HAVE_LOGIN_CAP
1326 if (login_getcapbool(lc, "requirehome", 0))
1327 exit(1);
1328#endif
1329 }
7368a6c8 1330
1331 /*
1332 * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1333 * xauth are run in the proper environment.
1334 */
1335 environ = env;
1336
1337 /*
1338 * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1339 * in this order).
1340 */
1341 if (!options.use_login) {
42f11eb2 1342 if (stat(_PATH_SSH_USER_RC, &st) >= 0) {
7368a6c8 1343 if (debug_flag)
42f11eb2 1344 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_USER_RC);
7368a6c8 1345
42f11eb2 1346 f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
7368a6c8 1347 if (f) {
1348 if (auth_proto != NULL && auth_data != NULL)
1349 fprintf(f, "%s %s\n", auth_proto, auth_data);
1350 pclose(f);
1351 } else
42f11eb2 1352 fprintf(stderr, "Could not run %s\n", _PATH_SSH_USER_RC);
1353 } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
7368a6c8 1354 if (debug_flag)
42f11eb2 1355 fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, _PATH_SSH_SYSTEM_RC);
7368a6c8 1356
42f11eb2 1357 f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
7368a6c8 1358 if (f) {
1359 if (auth_proto != NULL && auth_data != NULL)
1360 fprintf(f, "%s %s\n", auth_proto, auth_data);
1361 pclose(f);
1362 } else
42f11eb2 1363 fprintf(stderr, "Could not run %s\n", _PATH_SSH_SYSTEM_RC);
6fc1e9f3 1364 } else if (options.xauth_location != NULL) {
7368a6c8 1365 /* Add authority data to .Xauthority if appropriate. */
1366 if (auth_proto != NULL && auth_data != NULL) {
29611d9c 1367 char *screen = strchr(display, ':');
1368 if (debug_flag) {
71276795 1369 fprintf(stderr,
1370 "Running %.100s add %.100s %.100s %.100s\n",
6fc1e9f3 1371 options.xauth_location, display,
1372 auth_proto, auth_data);
7322ef0e 1373#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
29611d9c 1374 if (screen != NULL)
71276795 1375 fprintf(stderr,
1376 "Adding %.*s/unix%s %s %s\n",
14a9a859 1377 (int)(screen-display), display,
71276795 1378 screen, auth_proto, auth_data);
7322ef0e 1379#endif
29611d9c 1380 }
6fc1e9f3 1381 snprintf(cmd, sizeof cmd, "%s -q -",
1382 options.xauth_location);
1383 f = popen(cmd, "w");
7368a6c8 1384 if (f) {
71276795 1385 fprintf(f, "add %s %s %s\n", display,
1386 auth_proto, auth_data);
7322ef0e 1387#ifndef HAVE_CYGWIN /* Unix sockets are not supported */
2b87da3b 1388 if (screen != NULL)
29611d9c 1389 fprintf(f, "add %.*s/unix%s %s %s\n",
14a9a859 1390 (int)(screen-display), display,
71276795 1391 screen, auth_proto, auth_data);
7322ef0e 1392#endif
7368a6c8 1393 pclose(f);
6fc1e9f3 1394 } else {
1395 fprintf(stderr, "Could not run %s\n",
1396 cmd);
1397 }
7368a6c8 1398 }
1399 }
7368a6c8 1400 /* Get the last component of the shell name. */
1401 cp = strrchr(shell, '/');
1402 if (cp)
1403 cp++;
1404 else
1405 cp = shell;
1406 }
1407 /*
1408 * If we have no command, execute the shell. In this case, the shell
1409 * name to be passed in argv[0] is preceded by '-' to indicate that
1410 * this is a login shell.
1411 */
1412 if (!command) {
1413 if (!options.use_login) {
1414 char buf[256];
1415
1416 /*
1417 * Check for mail if we have a tty and it was enabled
1418 * in server options.
1419 */
1420 if (ttyname && options.check_mail) {
1421 char *mailbox;
1422 struct stat mailstat;
1423 mailbox = getenv("MAIL");
1424 if (mailbox != NULL) {
71276795 1425 if (stat(mailbox, &mailstat) != 0 ||
1426 mailstat.st_size == 0)
7368a6c8 1427 printf("No mail.\n");
1428 else if (mailstat.st_mtime < mailstat.st_atime)
1429 printf("You have mail.\n");
1430 else
1431 printf("You have new mail.\n");
1432 }
1433 }
1434 /* Start the shell. Set initial character to '-'. */
1435 buf[0] = '-';
1436 strncpy(buf + 1, cp, sizeof(buf) - 1);
1437 buf[sizeof(buf) - 1] = 0;
1438
1439 /* Execute the shell. */
1440 argv[0] = buf;
1441 argv[1] = NULL;
1442 execve(shell, argv, env);
1443
1444 /* Executing the shell failed. */
1445 perror(shell);
1446 exit(1);
1447
1448 } else {
1449 /* Launch login(1). */
1450
2e73a022 1451 execl(LOGIN_PROGRAM, "login", "-h", hostname,
c345cf9d 1452 "-p", "-f", "--", pw->pw_name, NULL);
7368a6c8 1453
1454 /* Login couldn't be executed, die. */
1455
1456 perror("login");
1457 exit(1);
1458 }
1459 }
1460 /*
1461 * Execute the command using the user's shell. This uses the -c
1462 * option to execute the command.
1463 */
1464 argv[0] = (char *) cp;
1465 argv[1] = "-c";
1466 argv[2] = (char *) command;
1467 argv[3] = NULL;
1468 execve(shell, argv, env);
1469 perror(shell);
1470 exit(1);
1471}
1472
1473Session *
1474session_new(void)
1475{
1476 int i;
1477 static int did_init = 0;
1478 if (!did_init) {
1479 debug("session_new: init");
1480 for(i = 0; i < MAX_SESSIONS; i++) {
1481 sessions[i].used = 0;
1482 sessions[i].self = i;
1483 }
1484 did_init = 1;
1485 }
1486 for(i = 0; i < MAX_SESSIONS; i++) {
1487 Session *s = &sessions[i];
1488 if (! s->used) {
1489 s->pid = 0;
0b242b12 1490 s->extended = 0;
7368a6c8 1491 s->chanid = -1;
1492 s->ptyfd = -1;
1493 s->ttyfd = -1;
1494 s->term = NULL;
1495 s->pw = NULL;
1496 s->display = NULL;
1497 s->screen = 0;
1498 s->auth_data = NULL;
1499 s->auth_proto = NULL;
1500 s->used = 1;
0b242b12 1501 s->pw = NULL;
02323c45 1502 debug("session_new: session %d", i);
7368a6c8 1503 return s;
1504 }
1505 }
1506 return NULL;
1507}
1508
1509void
1510session_dump(void)
1511{
1512 int i;
1513 for(i = 0; i < MAX_SESSIONS; i++) {
1514 Session *s = &sessions[i];
1515 debug("dump: used %d session %d %p channel %d pid %d",
1516 s->used,
1517 s->self,
1518 s,
1519 s->chanid,
1520 s->pid);
1521 }
1522}
1523
e78a59f5 1524int
1525session_open(int chanid)
1526{
1527 Session *s = session_new();
1528 debug("session_open: channel %d", chanid);
1529 if (s == NULL) {
1530 error("no more sessions");
1531 return 0;
1532 }
e78a59f5 1533 s->pw = auth_get_user();
1534 if (s->pw == NULL)
0b242b12 1535 fatal("no user for session %i", s->self);
1536 debug("session_open: session %d: link with channel %d", s->self, chanid);
1537 s->chanid = chanid;
e78a59f5 1538 return 1;
1539}
1540
1541Session *
1542session_by_channel(int id)
1543{
1544 int i;
1545 for(i = 0; i < MAX_SESSIONS; i++) {
1546 Session *s = &sessions[i];
1547 if (s->used && s->chanid == id) {
1548 debug("session_by_channel: session %d channel %d", i, id);
1549 return s;
1550 }
1551 }
1552 debug("session_by_channel: unknown channel %d", id);
1553 session_dump();
1554 return NULL;
1555}
1556
1557Session *
1558session_by_pid(pid_t pid)
1559{
1560 int i;
1561 debug("session_by_pid: pid %d", pid);
1562 for(i = 0; i < MAX_SESSIONS; i++) {
1563 Session *s = &sessions[i];
1564 if (s->used && s->pid == pid)
1565 return s;
1566 }
1567 error("session_by_pid: unknown pid %d", pid);
1568 session_dump();
1569 return NULL;
1570}
1571
1572int
1573session_window_change_req(Session *s)
1574{
1575 s->col = packet_get_int();
1576 s->row = packet_get_int();
1577 s->xpixel = packet_get_int();
1578 s->ypixel = packet_get_int();
6ae2364d 1579 packet_done();
e78a59f5 1580 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1581 return 1;
1582}
1583
1584int
1585session_pty_req(Session *s)
1586{
1e3b8b07 1587 u_int len;
6ae2364d 1588 char *term_modes; /* encoded terminal modes */
e78a59f5 1589
38c295d6 1590 if (no_pty_flag)
1591 return 0;
e78a59f5 1592 if (s->ttyfd != -1)
6ae2364d 1593 return 0;
e78a59f5 1594 s->term = packet_get_string(&len);
1595 s->col = packet_get_int();
1596 s->row = packet_get_int();
1597 s->xpixel = packet_get_int();
1598 s->ypixel = packet_get_int();
6ae2364d 1599 term_modes = packet_get_string(&len);
1600 packet_done();
e78a59f5 1601
1602 if (strcmp(s->term, "") == 0) {
1603 xfree(s->term);
1604 s->term = NULL;
1605 }
1606 /* Allocate a pty and open it. */
1607 if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1608 xfree(s->term);
1609 s->term = NULL;
1610 s->ptyfd = -1;
1611 s->ttyfd = -1;
1612 error("session_pty_req: session %d alloc failed", s->self);
6ae2364d 1613 xfree(term_modes);
1614 return 0;
e78a59f5 1615 }
1616 debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1617 /*
1618 * Add a cleanup function to clear the utmp entry and record logout
1619 * time in case we call fatal() (e.g., the connection gets closed).
1620 */
1621 fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1622 pty_setowner(s->pw, s->tty);
1623 /* Get window size from the packet. */
1624 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1625
1d1ffb87 1626 session_proctitle(s);
1627
35484284 1628 /* XXX parse and set terminal modes */
1629 xfree(term_modes);
e78a59f5 1630 return 1;
1631}
1632
0b242b12 1633int
1634session_subsystem_req(Session *s)
1635{
1e3b8b07 1636 u_int len;
0b242b12 1637 int success = 0;
1638 char *subsys = packet_get_string(&len);
38c295d6 1639 int i;
0b242b12 1640
1641 packet_done();
1642 log("subsystem request for %s", subsys);
1643
38c295d6 1644 for (i = 0; i < options.num_subsystems; i++) {
1645 if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1646 debug("subsystem: exec() %s", options.subsystem_command[i]);
1647 do_exec_no_pty(s, options.subsystem_command[i], s->pw);
1648 success = 1;
1649 }
1650 }
1651
1652 if (!success)
1653 log("subsystem request for %s failed, subsystem not found", subsys);
1654
0b242b12 1655 xfree(subsys);
1656 return success;
1657}
1658
1659int
1660session_x11_req(Session *s)
1661{
b5e300c2 1662 int fd;
089fbbd2 1663 if (no_x11_forwarding_flag) {
38c295d6 1664 debug("X11 forwarding disabled in user configuration file.");
1665 return 0;
1666 }
0b242b12 1667 if (!options.x11_forwarding) {
1668 debug("X11 forwarding disabled in server configuration file.");
1669 return 0;
1670 }
1671 if (xauthfile != NULL) {
1672 debug("X11 fwd already started.");
1673 return 0;
1674 }
1675
1676 debug("Received request for X11 forwarding with auth spoofing.");
1677 if (s->display != NULL)
1678 packet_disconnect("Protocol error: X11 display already set.");
1679
1680 s->single_connection = packet_get_char();
1681 s->auth_proto = packet_get_string(NULL);
1682 s->auth_data = packet_get_string(NULL);
1683 s->screen = packet_get_int();
1684 packet_done();
1685
1686 s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1687 if (s->display == NULL) {
1688 xfree(s->auth_proto);
1689 xfree(s->auth_data);
1690 return 0;
1691 }
1692 xauthfile = xmalloc(MAXPATHLEN);
1693 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1694 temporarily_use_uid(s->pw->pw_uid);
1695 if (mkdtemp(xauthfile) == NULL) {
1696 restore_uid();
1697 error("private X11 dir: mkdtemp %s failed: %s",
1698 xauthfile, strerror(errno));
1699 xfree(xauthfile);
1700 xauthfile = NULL;
1701 xfree(s->auth_proto);
1702 xfree(s->auth_data);
1703 /* XXXX remove listening channels */
1704 return 0;
1705 }
1706 strlcat(xauthfile, "/cookies", MAXPATHLEN);
b5e300c2 1707 fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1708 if (fd >= 0)
1709 close(fd);
0b242b12 1710 restore_uid();
1711 fatal_add_cleanup(xauthfile_cleanup_proc, s);
1712 return 1;
1713}
1714
38c295d6 1715int
1716session_shell_req(Session *s)
1717{
1718 /* if forced_command == NULL, the shell is execed */
1719 char *shell = forced_command;
1720 packet_done();
1721 s->extended = 1;
1722 if (s->ttyfd == -1)
1723 do_exec_no_pty(s, shell, s->pw);
1724 else
1725 do_exec_pty(s, shell, s->pw);
1726 return 1;
1727}
1728
1729int
1730session_exec_req(Session *s)
1731{
1e3b8b07 1732 u_int len;
38c295d6 1733 char *command = packet_get_string(&len);
1734 packet_done();
1735 if (forced_command) {
b5e300c2 1736 original_command = command;
38c295d6 1737 command = forced_command;
1738 debug("Forced command '%.500s'", forced_command);
1739 }
1740 s->extended = 1;
1741 if (s->ttyfd == -1)
1742 do_exec_no_pty(s, command, s->pw);
1743 else
1744 do_exec_pty(s, command, s->pw);
1745 if (forced_command == NULL)
1746 xfree(command);
1747 return 1;
1748}
1749
fa08c86b 1750int
1751session_auth_agent_req(Session *s)
1752{
1753 static int called = 0;
1754 packet_done();
d343d900 1755 if (no_agent_forwarding_flag) {
1756 debug("session_auth_agent_req: no_agent_forwarding_flag");
1757 return 0;
1758 }
fa08c86b 1759 if (called) {
1760 return 0;
1761 } else {
1762 called = 1;
1763 return auth_input_request_forwarding(s->pw);
1764 }
1765}
1766
e78a59f5 1767void
1768session_input_channel_req(int id, void *arg)
1769{
1e3b8b07 1770 u_int len;
e78a59f5 1771 int reply;
1772 int success = 0;
1773 char *rtype;
1774 Session *s;
1775 Channel *c;
1776
1777 rtype = packet_get_string(&len);
1778 reply = packet_get_char();
1779
1780 s = session_by_channel(id);
1781 if (s == NULL)
1782 fatal("session_input_channel_req: channel %d: no session", id);
1783 c = channel_lookup(id);
1784 if (c == NULL)
1785 fatal("session_input_channel_req: channel %d: bad channel", id);
1786
1787 debug("session_input_channel_req: session %d channel %d request %s reply %d",
1788 s->self, id, rtype, reply);
1789
1790 /*
1791 * a session is in LARVAL state until a shell
1792 * or programm is executed
1793 */
1794 if (c->type == SSH_CHANNEL_LARVAL) {
1795 if (strcmp(rtype, "shell") == 0) {
38c295d6 1796 success = session_shell_req(s);
e78a59f5 1797 } else if (strcmp(rtype, "exec") == 0) {
38c295d6 1798 success = session_exec_req(s);
e78a59f5 1799 } else if (strcmp(rtype, "pty-req") == 0) {
35484284 1800 success = session_pty_req(s);
0b242b12 1801 } else if (strcmp(rtype, "x11-req") == 0) {
1802 success = session_x11_req(s);
fa08c86b 1803 } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1804 success = session_auth_agent_req(s);
0b242b12 1805 } else if (strcmp(rtype, "subsystem") == 0) {
1806 success = session_subsystem_req(s);
e78a59f5 1807 }
1808 }
1809 if (strcmp(rtype, "window-change") == 0) {
1810 success = session_window_change_req(s);
1811 }
1812
1813 if (reply) {
1814 packet_start(success ?
1815 SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1816 packet_put_int(c->remote_id);
1817 packet_send();
1818 }
1819 xfree(rtype);
1820}
1821
1822void
1823session_set_fds(Session *s, int fdin, int fdout, int fderr)
1824{
1825 if (!compat20)
1826 fatal("session_set_fds: called for proto != 2.0");
1827 /*
1828 * now that have a child and a pipe to the child,
1829 * we can activate our channel and register the fd's
1830 */
1831 if (s->chanid == -1)
1832 fatal("no channel for session %d", s->self);
1833 channel_set_fds(s->chanid,
1834 fdout, fdin, fderr,
a22aff1f 1835 fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1836 1);
e78a59f5 1837}
1838
7368a6c8 1839void
1840session_pty_cleanup(Session *s)
1841{
1842 if (s == NULL || s->ttyfd == -1)
1843 return;
1844
1845 debug("session_pty_cleanup: session %i release %s", s->self, s->tty);
1846
1847 /* Cancel the cleanup function. */
1848 fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1849
1850 /* Record that the user has logged out. */
1851 record_logout(s->pid, s->tty);
1852
1853 /* Release the pseudo-tty. */
1854 pty_release(s->tty);
1855
1856 /*
1857 * Close the server side of the socket pairs. We must do this after
1858 * the pty cleanup, so that another process doesn't get this pty
1859 * while we're still cleaning up.
1860 */
1861 if (close(s->ptymaster) < 0)
1862 error("close(s->ptymaster): %s", strerror(errno));
1863}
e78a59f5 1864
1865void
1866session_exit_message(Session *s, int status)
1867{
1868 Channel *c;
1869 if (s == NULL)
1870 fatal("session_close: no session");
1871 c = channel_lookup(s->chanid);
1872 if (c == NULL)
1873 fatal("session_close: session %d: no channel %d",
1874 s->self, s->chanid);
1875 debug("session_exit_message: session %d channel %d pid %d",
1876 s->self, s->chanid, s->pid);
1877
1878 if (WIFEXITED(status)) {
1879 channel_request_start(s->chanid,
1880 "exit-status", 0);
1881 packet_put_int(WEXITSTATUS(status));
1882 packet_send();
1883 } else if (WIFSIGNALED(status)) {
1884 channel_request_start(s->chanid,
1885 "exit-signal", 0);
1886 packet_put_int(WTERMSIG(status));
a64009ad 1887#ifdef WCOREDUMP
e78a59f5 1888 packet_put_char(WCOREDUMP(status));
a64009ad 1889#else /* WCOREDUMP */
1890 packet_put_char(0);
1891#endif /* WCOREDUMP */
e78a59f5 1892 packet_put_cstring("");
1893 packet_put_cstring("");
1894 packet_send();
1895 } else {
1896 /* Some weird exit cause. Just exit. */
1897 packet_disconnect("wait returned status %04x.", status);
1898 }
1899
1900 /* disconnect channel */
1901 debug("session_exit_message: release channel %d", s->chanid);
1902 channel_cancel_cleanup(s->chanid);
9da5c3c9 1903 /*
1904 * emulate a write failure with 'chan_write_failed', nobody will be
1905 * interested in data we write.
1906 * Note that we must not call 'chan_read_failed', since there could
1907 * be some more data waiting in the pipe.
1908 */
0b242b12 1909 if (c->ostate != CHAN_OUTPUT_CLOSED)
1910 chan_write_failed(c);
e78a59f5 1911 s->chanid = -1;
1912}
1913
1914void
1915session_free(Session *s)
1916{
1917 debug("session_free: session %d pid %d", s->self, s->pid);
1918 if (s->term)
1919 xfree(s->term);
1920 if (s->display)
1921 xfree(s->display);
1922 if (s->auth_data)
1923 xfree(s->auth_data);
1924 if (s->auth_proto)
1925 xfree(s->auth_proto);
1926 s->used = 0;
1927}
1928
1929void
1930session_close(Session *s)
1931{
1932 session_pty_cleanup(s);
1933 session_free(s);
1d1ffb87 1934 session_proctitle(s);
e78a59f5 1935}
1936
1937void
1938session_close_by_pid(pid_t pid, int status)
1939{
1940 Session *s = session_by_pid(pid);
1941 if (s == NULL) {
1942 debug("session_close_by_pid: no session for pid %d", s->pid);
1943 return;
1944 }
1945 if (s->chanid != -1)
1946 session_exit_message(s, status);
1947 session_close(s);
1948}
1949
1950/*
1951 * this is called when a channel dies before
1952 * the session 'child' itself dies
1953 */
1954void
1955session_close_by_channel(int id, void *arg)
1956{
1957 Session *s = session_by_channel(id);
1958 if (s == NULL) {
1959 debug("session_close_by_channel: no session for channel %d", id);
1960 return;
1961 }
1962 /* disconnect channel */
1963 channel_cancel_cleanup(s->chanid);
1964 s->chanid = -1;
1965
1966 debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1967 if (s->pid == 0) {
1968 /* close session immediately */
1969 session_close(s);
1970 } else {
1971 /* notify child, delay session cleanup */
2b87da3b 1972 if (s->pid <= 1)
512ad3c0 1973 fatal("session_close_by_channel: Unsafe s->pid = %d", s->pid);
1974 if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
e78a59f5 1975 error("session_close_by_channel: kill %d: %s",
1976 s->pid, strerror(errno));
1977 }
1978}
1979
1d1ffb87 1980char *
1981session_tty_list(void)
1982{
1983 static char buf[1024];
1984 int i;
1985 buf[0] = '\0';
1986 for(i = 0; i < MAX_SESSIONS; i++) {
1987 Session *s = &sessions[i];
1988 if (s->used && s->ttyfd != -1) {
1989 if (buf[0] != '\0')
1990 strlcat(buf, ",", sizeof buf);
1991 strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1992 }
1993 }
1994 if (buf[0] == '\0')
1995 strlcpy(buf, "notty", sizeof buf);
1996 return buf;
1997}
1998
1999void
2000session_proctitle(Session *s)
2001{
2002 if (s->pw == NULL)
2003 error("no user for session %d", s->self);
2004 else
2005 setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
2006}
2007
e78a59f5 2008void
59c97189 2009do_authenticated2(Authctxt *authctxt)
e78a59f5 2010{
2e73a022 2011
e78a59f5 2012 /*
2013 * Cancel the alarm we set to limit the time taken for
2014 * authentication.
2015 */
2016 alarm(0);
3f7a7e4a 2017 if (startup_pipe != -1) {
5540ea9b 2018 close(startup_pipe);
3f7a7e4a 2019 startup_pipe = -1;
2020 }
97994d32 2021#if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD)
59c97189 2022 if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
2e73a022 2023 error("unable to get login class");
2024 return;
2025 }
2026#endif
e78a59f5 2027 server_loop2();
34bce9a5 2028 if (xauthfile)
2029 xauthfile_cleanup_proc(NULL);
e78a59f5 2030}
This page took 0.515934 seconds and 5 git commands to generate.