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