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