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