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