Annotation of src/usr.bin/ssh/session.c, Revision 1.237
1.237 ! djm 1: /* $OpenBSD: session.c,v 1.236 2008/05/08 12:02:23 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.37 deraadt 5: *
6: * As far as I am concerned, the code I have written for this software
7: * can be used freely for any purpose. Any derived versions of this
8: * software must be clearly marked as such, and if the derived work is
9: * incompatible with the protocol description in the RFC file, it must be
10: * called by a name other than "ssh" or "Secure Shell".
11: *
1.2 markus 12: * SSH2 support by Markus Friedl.
1.95 markus 13: * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
1.37 deraadt 14: *
15: * Redistribution and use in source and binary forms, with or without
16: * modification, are permitted provided that the following conditions
17: * are met:
18: * 1. Redistributions of source code must retain the above copyright
19: * notice, this list of conditions and the following disclaimer.
20: * 2. Redistributions in binary form must reproduce the above copyright
21: * notice, this list of conditions and the following disclaimer in the
22: * documentation and/or other materials provided with the distribution.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.2 markus 34: */
1.1 markus 35:
1.193 stevesk 36: #include <sys/types.h>
37: #include <sys/wait.h>
1.194 stevesk 38: #include <sys/un.h>
1.196 stevesk 39: #include <sys/stat.h>
1.207 stevesk 40: #include <sys/socket.h>
1.213 stevesk 41: #include <sys/param.h>
1.236 djm 42: #include <sys/queue.h>
1.192 stevesk 43:
1.209 stevesk 44: #include <errno.h>
1.204 stevesk 45: #include <grp.h>
1.223 djm 46: #include <login_cap.h>
1.192 stevesk 47: #include <paths.h>
1.206 stevesk 48: #include <pwd.h>
1.195 stevesk 49: #include <signal.h>
1.215 stevesk 50: #include <stdio.h>
1.214 stevesk 51: #include <stdlib.h>
1.212 stevesk 52: #include <string.h>
1.211 stevesk 53: #include <unistd.h>
1.1 markus 54:
1.216 deraadt 55: #include "xmalloc.h"
1.51 markus 56: #include "ssh.h"
57: #include "ssh1.h"
58: #include "ssh2.h"
1.59 djm 59: #include "sshpty.h"
1.1 markus 60: #include "packet.h"
61: #include "buffer.h"
1.173 djm 62: #include "match.h"
1.1 markus 63: #include "uidswap.h"
64: #include "compat.h"
1.78 markus 65: #include "channels.h"
1.216 deraadt 66: #include "key.h"
67: #include "cipher.h"
68: #include "kex.h"
69: #include "hostfile.h"
1.2 markus 70: #include "auth.h"
1.19 markus 71: #include "auth-options.h"
1.50 markus 72: #include "pathnames.h"
1.51 markus 73: #include "log.h"
74: #include "servconf.h"
1.59 djm 75: #include "sshlogin.h"
1.51 markus 76: #include "serverloop.h"
77: #include "canohost.h"
1.226 djm 78: #include "misc.h"
1.55 itojun 79: #include "session.h"
1.216 deraadt 80: #ifdef GSSAPI
81: #include "ssh-gss.h"
82: #endif
1.130 provos 83: #include "monitor_wrap.h"
1.225 markus 84: #include "sftp.h"
1.171 markus 85:
86: #ifdef KRB5
87: #include <kafs.h>
1.161 markus 88: #endif
89:
1.1 markus 90: /* func */
91:
92: Session *session_new(void);
1.94 itojun 93: void session_set_fds(Session *, int, int, int);
1.165 markus 94: void session_pty_cleanup(Session *);
1.94 itojun 95: void session_proctitle(Session *);
96: int session_setup_x11fwd(Session *);
1.237 ! djm 97: int do_exec_pty(Session *, const char *);
! 98: int do_exec_no_pty(Session *, const char *);
! 99: int do_exec(Session *, const char *);
1.94 itojun 100: void do_login(Session *, const char *);
101: void do_child(Session *, const char *);
1.73 djm 102: void do_motd(void);
1.94 itojun 103: int check_quietlogin(Session *, const char *);
1.1 markus 104:
1.94 itojun 105: static void do_authenticated1(Authctxt *);
106: static void do_authenticated2(Authctxt *);
107:
108: static int session_pty_req(Session *);
1.65 markus 109:
1.1 markus 110: /* import */
111: extern ServerOptions options;
112: extern char *__progname;
113: extern int log_stderr;
114: extern int debug_flag;
1.45 markus 115: extern u_int utmp_len;
1.21 markus 116: extern int startup_pipe;
1.74 markus 117: extern void destroy_sensitive_data(void);
1.179 dtucker 118: extern Buffer loginmsg;
1.21 markus 119:
1.34 markus 120: /* original command from peer. */
1.92 markus 121: const char *original_command = NULL;
1.34 markus 122:
1.1 markus 123: /* data */
1.237 ! djm 124: static int sessions_first_unused = -1;
! 125: static int sessions_nalloc = 0;
! 126: static Session *sessions = NULL;
1.1 markus 127:
1.225 markus 128: #define SUBSYSTEM_NONE 0
129: #define SUBSYSTEM_EXT 1
130: #define SUBSYSTEM_INT_SFTP 2
131:
1.129 provos 132: login_cap_t *lc;
1.28 millert 133:
1.165 markus 134: static int is_child = 0;
135:
1.136 markus 136: /* Name and directory of socket for authentication agent forwarding. */
137: static char *auth_sock_name = NULL;
138: static char *auth_sock_dir = NULL;
139:
140: /* removes the agent forwarding socket */
141:
142: static void
1.165 markus 143: auth_sock_cleanup_proc(struct passwd *pw)
1.136 markus 144: {
145: if (auth_sock_name != NULL) {
146: temporarily_use_uid(pw);
147: unlink(auth_sock_name);
148: rmdir(auth_sock_dir);
149: auth_sock_name = NULL;
150: restore_uid();
151: }
152: }
153:
154: static int
155: auth_input_request_forwarding(struct passwd * pw)
156: {
157: Channel *nc;
1.237 ! djm 158: int sock = -1;
1.136 markus 159: struct sockaddr_un sunaddr;
160:
161: if (auth_sock_name != NULL) {
162: error("authentication forwarding requested twice.");
163: return 0;
164: }
165:
166: /* Temporarily drop privileged uid for mkdir/bind. */
167: temporarily_use_uid(pw);
168:
169: /* Allocate a buffer for the socket name, and format the name. */
1.237 ! djm 170: auth_sock_dir = xstrdup("/tmp/ssh-XXXXXXXXXX");
1.136 markus 171:
172: /* Create private directory for socket */
173: if (mkdtemp(auth_sock_dir) == NULL) {
174: packet_send_debug("Agent forwarding disabled: "
175: "mkdtemp() failed: %.100s", strerror(errno));
176: restore_uid();
177: xfree(auth_sock_dir);
178: auth_sock_dir = NULL;
1.237 ! djm 179: goto authsock_err;
1.136 markus 180: }
1.237 ! djm 181:
! 182: xasprintf(&auth_sock_name, "%s/agent.%ld",
! 183: auth_sock_dir, (long) getpid());
1.136 markus 184:
185: /* Create the socket. */
186: sock = socket(AF_UNIX, SOCK_STREAM, 0);
1.237 ! djm 187: if (sock < 0) {
! 188: error("socket: %.100s", strerror(errno));
! 189: restore_uid();
! 190: goto authsock_err;
! 191: }
1.136 markus 192:
193: /* Bind it to the name. */
194: memset(&sunaddr, 0, sizeof(sunaddr));
195: sunaddr.sun_family = AF_UNIX;
196: strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
197:
1.237 ! djm 198: if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
! 199: error("bind: %.100s", strerror(errno));
! 200: restore_uid();
! 201: goto authsock_err;
! 202: }
1.136 markus 203:
204: /* Restore the privileged uid. */
205: restore_uid();
206:
207: /* Start listening on the socket. */
1.237 ! djm 208: if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
! 209: error("listen: %.100s", strerror(errno));
! 210: goto authsock_err;
! 211: }
1.136 markus 212:
213: /* Allocate a channel for the authentication agent socket. */
214: nc = channel_new("auth socket",
215: SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
216: CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1.156 markus 217: 0, "auth socket", 1);
1.136 markus 218: strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
219: return 1;
1.237 ! djm 220:
! 221: authsock_err:
! 222: if (auth_sock_name != NULL)
! 223: xfree(auth_sock_name);
! 224: if (auth_sock_dir != NULL) {
! 225: rmdir(auth_sock_dir);
! 226: xfree(auth_sock_dir);
! 227: }
! 228: if (sock != -1)
! 229: close(sock);
! 230: auth_sock_name = NULL;
! 231: auth_sock_dir = NULL;
! 232: return 0;
1.136 markus 233: }
234:
1.179 dtucker 235: static void
236: display_loginmsg(void)
237: {
1.183 djm 238: if (buffer_len(&loginmsg) > 0) {
239: buffer_append(&loginmsg, "\0", 1);
240: printf("%s", (char *)buffer_ptr(&loginmsg));
241: buffer_clear(&loginmsg);
242: }
1.179 dtucker 243: }
1.136 markus 244:
1.65 markus 245: void
246: do_authenticated(Authctxt *authctxt)
247: {
1.153 markus 248: setproctitle("%s", authctxt->pw->pw_name);
249:
1.65 markus 250: /* setup the channel layer */
251: if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
252: channel_permit_all_opens();
253:
254: if (compat20)
255: do_authenticated2(authctxt);
256: else
257: do_authenticated1(authctxt);
1.79 markus 258:
1.165 markus 259: do_cleanup(authctxt);
1.65 markus 260: }
261:
1.1 markus 262: /*
263: * Prepares for an interactive session. This is called after the user has
264: * been successfully authenticated. During this message exchange, pseudo
265: * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
266: * are requested, etc.
267: */
1.94 itojun 268: static void
1.65 markus 269: do_authenticated1(Authctxt *authctxt)
1.1 markus 270: {
271: Session *s;
1.65 markus 272: char *command;
1.117 markus 273: int success, type, screen_flag;
1.139 deraadt 274: int enable_compression_after_reply = 0;
275: u_int proto_len, data_len, dlen, compression_level = 0;
1.1 markus 276:
1.61 markus 277: s = session_new();
1.181 markus 278: if (s == NULL) {
279: error("no more sessions");
280: return;
281: }
1.96 dugsong 282: s->authctxt = authctxt;
1.65 markus 283: s->pw = authctxt->pw;
1.28 millert 284:
1.1 markus 285: /*
286: * We stay in this loop until the client requests to execute a shell
287: * or a command.
288: */
289: for (;;) {
1.65 markus 290: success = 0;
1.1 markus 291:
292: /* Get a packet from the client. */
1.117 markus 293: type = packet_read();
1.1 markus 294:
295: /* Process the packet. */
296: switch (type) {
297: case SSH_CMSG_REQUEST_COMPRESSION:
298: compression_level = packet_get_int();
1.116 markus 299: packet_check_eom();
1.1 markus 300: if (compression_level < 1 || compression_level > 9) {
1.180 markus 301: packet_send_debug("Received invalid compression level %d.",
1.112 deraadt 302: compression_level);
1.138 markus 303: break;
304: }
1.186 markus 305: if (options.compression == COMP_NONE) {
1.138 markus 306: debug2("compression disabled");
1.1 markus 307: break;
308: }
309: /* Enable compression after we have responded with SUCCESS. */
310: enable_compression_after_reply = 1;
311: success = 1;
312: break;
313:
314: case SSH_CMSG_REQUEST_PTY:
1.86 markus 315: success = session_pty_req(s);
1.1 markus 316: break;
317:
318: case SSH_CMSG_X11_REQUEST_FORWARDING:
319: s->auth_proto = packet_get_string(&proto_len);
320: s->auth_data = packet_get_string(&data_len);
321:
1.57 markus 322: screen_flag = packet_get_protocol_flags() &
323: SSH_PROTOFLAG_SCREEN_NUMBER;
324: debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag);
325:
326: if (packet_remaining() == 4) {
327: if (!screen_flag)
328: debug2("Buggy client: "
329: "X11 screen flag missing");
1.1 markus 330: s->screen = packet_get_int();
1.56 markus 331: } else {
1.1 markus 332: s->screen = 0;
1.56 markus 333: }
1.116 markus 334: packet_check_eom();
1.81 markus 335: success = session_setup_x11fwd(s);
336: if (!success) {
337: xfree(s->auth_proto);
338: xfree(s->auth_data);
1.84 markus 339: s->auth_proto = NULL;
340: s->auth_data = NULL;
1.1 markus 341: }
342: break;
343:
344: case SSH_CMSG_AGENT_REQUEST_FORWARDING:
1.235 pyr 345: if (!options.allow_agent_forwarding ||
346: no_agent_forwarding_flag || compat13) {
1.1 markus 347: debug("Authentication agent forwarding not permitted for this authentication.");
348: break;
349: }
350: debug("Received authentication agent forwarding request.");
1.65 markus 351: success = auth_input_request_forwarding(s->pw);
1.1 markus 352: break;
353:
354: case SSH_CMSG_PORT_FORWARD_REQUEST:
355: if (no_port_forwarding_flag) {
356: debug("Port forwarding not permitted for this authentication.");
1.39 markus 357: break;
358: }
359: if (!options.allow_tcp_forwarding) {
360: debug("Port forwarding not permitted.");
1.1 markus 361: break;
362: }
363: debug("Received TCP/IP port forwarding request.");
1.208 markus 364: if (channel_input_port_forward_request(s->pw->pw_uid == 0,
365: options.gateway_ports) < 0) {
366: debug("Port forwarding failed.");
367: break;
368: }
1.1 markus 369: success = 1;
370: break;
371:
372: case SSH_CMSG_MAX_PACKET_SIZE:
373: if (packet_set_maxsize(packet_get_int()) > 0)
374: success = 1;
375: break;
376:
377: case SSH_CMSG_EXEC_SHELL:
378: case SSH_CMSG_EXEC_CMD:
379: if (type == SSH_CMSG_EXEC_CMD) {
380: command = packet_get_string(&dlen);
381: debug("Exec command '%.500s'", command);
1.237 ! djm 382: if (do_exec(s, command) != 0)
! 383: packet_disconnect(
! 384: "command execution failed");
1.92 markus 385: xfree(command);
1.1 markus 386: } else {
1.237 ! djm 387: if (do_exec(s, NULL) != 0)
! 388: packet_disconnect(
! 389: "shell execution failed");
1.1 markus 390: }
1.116 markus 391: packet_check_eom();
1.82 markus 392: session_close(s);
1.1 markus 393: return;
394:
395: default:
396: /*
397: * Any unknown messages in this phase are ignored,
398: * and a failure message is returned.
399: */
1.155 itojun 400: logit("Unknown packet type received after authentication: %d", type);
1.1 markus 401: }
402: packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE);
403: packet_send();
404: packet_write_wait();
405:
406: /* Enable compression now that we have replied if appropriate. */
407: if (enable_compression_after_reply) {
408: enable_compression_after_reply = 0;
409: packet_start_compression(compression_level);
410: }
411: }
412: }
413:
414: /*
415: * This is called to fork and execute a command when we have no tty. This
416: * will call do_child from the child, and server_loop from the parent after
417: * setting up file descriptors and such.
418: */
1.237 ! djm 419: int
1.63 markus 420: do_exec_no_pty(Session *s, const char *command)
1.1 markus 421: {
1.137 mpech 422: pid_t pid;
1.237 ! djm 423: int inout[2], err[2];
1.1 markus 424:
425: /* Uses socket pairs to communicate with the program. */
1.237 ! djm 426: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0) {
! 427: error("%s: socketpair #1: %.100s", __func__, strerror(errno));
! 428: return -1;
! 429: }
! 430: if (socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) {
! 431: error("%s: socketpair #2: %.100s", __func__, strerror(errno));
! 432: close(inout[0]);
! 433: close(inout[1]);
! 434: return -1;
! 435: }
! 436:
1.1 markus 437: if (s == NULL)
438: fatal("do_exec_no_pty: no session");
439:
1.9 markus 440: session_proctitle(s);
1.1 markus 441:
442: /* Fork the child. */
1.237 ! djm 443: switch ((pid = fork())) {
! 444: case -1:
! 445: error("%s: fork: %.100s", __func__, strerror(errno));
! 446: close(inout[0]);
! 447: close(inout[1]);
! 448: close(err[0]);
! 449: close(err[1]);
! 450: return -1;
! 451: case 0:
1.165 markus 452: is_child = 1;
1.144 markus 453:
1.1 markus 454: /* Child. Reinitialize the log since the pid has changed. */
1.237 ! djm 455: log_init(__progname, options.log_level,
! 456: options.log_facility, log_stderr);
1.1 markus 457:
458: /*
459: * Create a new session and process group since the 4.4BSD
460: * setlogin() affects the entire process group.
461: */
462: if (setsid() < 0)
463: error("setsid failed: %.100s", strerror(errno));
464:
465: /*
466: * Redirect stdin, stdout, and stderr. Stdin and stdout will
467: * use the same socket, as some programs (particularly rdist)
468: * seem to depend on it.
469: */
470: close(inout[1]);
471: close(err[1]);
472: if (dup2(inout[0], 0) < 0) /* stdin */
473: perror("dup2 stdin");
1.237 ! djm 474: if (dup2(inout[0], 1) < 0) /* stdout (same as stdin) */
1.1 markus 475: perror("dup2 stdout");
476: if (dup2(err[0], 2) < 0) /* stderr */
477: perror("dup2 stderr");
478:
479: /* Do processing for the child (exec command etc). */
1.60 markus 480: do_child(s, command);
1.1 markus 481: /* NOTREACHED */
1.237 ! djm 482: default:
! 483: break;
1.1 markus 484: }
1.237 ! djm 485:
1.1 markus 486: s->pid = pid;
1.47 markus 487: /* Set interactive/non-interactive mode. */
1.48 markus 488: packet_set_interactive(s->display != NULL);
1.1 markus 489:
490: /* We are the parent. Close the child sides of the socket pairs. */
491: close(inout[0]);
492: close(err[0]);
493:
494: /*
495: * Enter the interactive session. Note: server_loop must be able to
496: * handle the case that fdin and fdout are the same.
497: */
1.2 markus 498: if (compat20) {
1.237 ! djm 499: session_set_fds(s, inout[1], inout[1],
! 500: s->is_subsystem ? -1 : err[1]);
! 501: if (s->is_subsystem)
! 502: close(err[1]);
1.2 markus 503: } else {
504: server_loop(pid, inout[1], inout[1], err[1]);
505: /* server_loop has closed inout[1] and err[1]. */
506: }
1.237 ! djm 507: return 0;
1.1 markus 508: }
509:
510: /*
511: * This is called to fork and execute a command when we have a tty. This
512: * will call do_child from the child, and server_loop from the parent after
513: * setting up file descriptors, controlling tty, updating wtmp, utmp,
514: * lastlog, and other such operations.
515: */
1.237 ! djm 516: int
1.63 markus 517: do_exec_pty(Session *s, const char *command)
1.1 markus 518: {
519: int fdout, ptyfd, ttyfd, ptymaster;
520: pid_t pid;
521:
522: if (s == NULL)
523: fatal("do_exec_pty: no session");
524: ptyfd = s->ptyfd;
525: ttyfd = s->ttyfd;
526:
1.237 ! djm 527: /*
! 528: * Create another descriptor of the pty master side for use as the
! 529: * standard input. We could use the original descriptor, but this
! 530: * simplifies code in server_loop. The descriptor is bidirectional.
! 531: * Do this before forking (and cleanup in the child) so as to
! 532: * detect and gracefully fail out-of-fd conditions.
! 533: */
! 534: if ((fdout = dup(ptyfd)) < 0) {
! 535: error("%s: dup #1: %s", __func__, strerror(errno));
! 536: close(ttyfd);
! 537: close(ptyfd);
! 538: return -1;
! 539: }
! 540: /* we keep a reference to the pty master */
! 541: if ((ptymaster = dup(ptyfd)) < 0) {
! 542: error("%s: dup #2: %s", __func__, strerror(errno));
! 543: close(ttyfd);
! 544: close(ptyfd);
! 545: close(fdout);
! 546: return -1;
! 547: }
! 548:
1.1 markus 549: /* Fork the child. */
1.237 ! djm 550: switch ((pid = fork())) {
! 551: case -1:
! 552: error("%s: fork: %.100s", __func__, strerror(errno));
! 553: close(fdout);
! 554: close(ptymaster);
! 555: close(ttyfd);
! 556: close(ptyfd);
! 557: return -1;
! 558: case 0:
1.165 markus 559: is_child = 1;
1.97 markus 560:
1.237 ! djm 561: close(fdout);
! 562: close(ptymaster);
! 563:
1.24 markus 564: /* Child. Reinitialize the log because the pid has changed. */
1.237 ! djm 565: log_init(__progname, options.log_level,
! 566: options.log_facility, log_stderr);
1.1 markus 567: /* Close the master side of the pseudo tty. */
568: close(ptyfd);
569:
570: /* Make the pseudo tty our controlling tty. */
571: pty_make_controlling_tty(&ttyfd, s->tty);
572:
1.103 markus 573: /* Redirect stdin/stdout/stderr from the pseudo tty. */
574: if (dup2(ttyfd, 0) < 0)
575: error("dup2 stdin: %s", strerror(errno));
576: if (dup2(ttyfd, 1) < 0)
577: error("dup2 stdout: %s", strerror(errno));
578: if (dup2(ttyfd, 2) < 0)
579: error("dup2 stderr: %s", strerror(errno));
1.1 markus 580:
581: /* Close the extra descriptor for the pseudo tty. */
582: close(ttyfd);
583:
1.24 markus 584: /* record login, etc. similar to login(1) */
1.41 markus 585: if (!(options.use_login && command == NULL))
586: do_login(s, command);
1.228 djm 587:
1.237 ! djm 588: /*
! 589: * Do common processing for the child, such as execing
! 590: * the command.
! 591: */
1.60 markus 592: do_child(s, command);
1.1 markus 593: /* NOTREACHED */
1.237 ! djm 594: default:
! 595: break;
1.1 markus 596: }
597: s->pid = pid;
598:
599: /* Parent. Close the slave side of the pseudo tty. */
600: close(ttyfd);
601:
1.237 ! djm 602: /* Enter interactive session. */
1.1 markus 603: s->ptymaster = ptymaster;
1.47 markus 604: packet_set_interactive(1);
1.2 markus 605: if (compat20) {
606: session_set_fds(s, ptyfd, fdout, -1);
607: } else {
608: server_loop(pid, ptyfd, fdout, -1);
609: /* server_loop _has_ closed ptyfd and fdout. */
1.24 markus 610: }
1.237 ! djm 611: return 0;
1.24 markus 612: }
613:
1.90 markus 614: /*
615: * This is called to fork and execute a command. If another command is
616: * to be forced, execute that instead.
617: */
1.237 ! djm 618: int
1.90 markus 619: do_exec(Session *s, const char *command)
620: {
1.237 ! djm 621: int ret;
! 622:
1.210 dtucker 623: if (options.adm_forced_command) {
624: original_command = command;
625: command = options.adm_forced_command;
1.226 djm 626: if (strcmp(INTERNAL_SFTP_NAME, command) == 0)
627: s->is_subsystem = SUBSYSTEM_INT_SFTP;
628: else if (s->is_subsystem)
1.225 markus 629: s->is_subsystem = SUBSYSTEM_EXT;
1.210 dtucker 630: debug("Forced command (config) '%.900s'", command);
631: } else if (forced_command) {
1.90 markus 632: original_command = command;
633: command = forced_command;
1.226 djm 634: if (strcmp(INTERNAL_SFTP_NAME, command) == 0)
635: s->is_subsystem = SUBSYSTEM_INT_SFTP;
636: else if (s->is_subsystem)
1.225 markus 637: s->is_subsystem = SUBSYSTEM_EXT;
1.210 dtucker 638: debug("Forced command (key option) '%.900s'", command);
1.90 markus 639: }
1.163 markus 640:
641: #ifdef GSSAPI
642: if (options.gss_authentication) {
643: temporarily_use_uid(s->pw);
644: ssh_gssapi_storecreds();
645: restore_uid();
646: }
647: #endif
1.90 markus 648: if (s->ttyfd != -1)
1.237 ! djm 649: ret = do_exec_pty(s, command);
1.90 markus 650: else
1.237 ! djm 651: ret = do_exec_no_pty(s, command);
1.90 markus 652:
1.92 markus 653: original_command = NULL;
1.179 dtucker 654:
655: /*
656: * Clear loginmsg: it's the child's responsibility to display
657: * it to the user, otherwise multiple sessions may accumulate
658: * multiple copies of the login messages.
659: */
660: buffer_clear(&loginmsg);
1.237 ! djm 661:
! 662: return ret;
1.90 markus 663: }
664:
665:
1.24 markus 666: /* administrative, login(1)-like work */
667: void
1.41 markus 668: do_login(Session *s, const char *command)
1.24 markus 669: {
670: socklen_t fromlen;
671: struct sockaddr_storage from;
672: struct passwd * pw = s->pw;
673: pid_t pid = getpid();
674:
675: /*
676: * Get IP address of client. If the connection is not a socket, let
677: * the address be 0.0.0.0.
678: */
679: memset(&from, 0, sizeof(from));
1.148 stevesk 680: fromlen = sizeof(from);
1.24 markus 681: if (packet_connection_is_on_socket()) {
682: if (getpeername(packet_get_connection_in(),
1.200 deraadt 683: (struct sockaddr *)&from, &fromlen) < 0) {
1.24 markus 684: debug("getpeername: %.100s", strerror(errno));
1.165 markus 685: cleanup_exit(255);
1.24 markus 686: }
687: }
1.25 markus 688:
1.24 markus 689: /* Record that there was a login on that tty from the remote host. */
1.133 markus 690: if (!use_privsep)
691: record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
692: get_remote_name_or_ip(utmp_len,
1.158 markus 693: options.use_dns),
1.148 stevesk 694: (struct sockaddr *)&from, fromlen);
1.24 markus 695:
1.73 djm 696: if (check_quietlogin(s, command))
1.24 markus 697: return;
1.73 djm 698:
1.179 dtucker 699: display_loginmsg();
1.73 djm 700:
701: do_motd();
702: }
703:
704: /*
705: * Display the message of the day.
706: */
707: void
708: do_motd(void)
709: {
710: FILE *f;
711: char buf[256];
712:
1.24 markus 713: if (options.print_motd) {
1.28 millert 714: f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
715: "/etc/motd"), "r");
1.24 markus 716: if (f) {
717: while (fgets(buf, sizeof(buf), f))
718: fputs(buf, stdout);
719: fclose(f);
720: }
1.2 markus 721: }
1.73 djm 722: }
723:
724:
725: /*
726: * Check for quiet login, either .hushlogin or command given.
727: */
728: int
729: check_quietlogin(Session *s, const char *command)
730: {
731: char buf[256];
1.96 dugsong 732: struct passwd *pw = s->pw;
1.73 djm 733: struct stat st;
734:
735: /* Return 1 if .hushlogin exists or a command given. */
736: if (command != NULL)
737: return 1;
738: snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
739: if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
740: return 1;
741: return 0;
1.1 markus 742: }
743:
744: /*
745: * Sets the value of the given variable in the environment. If the variable
746: * already exists, its value is overriden.
747: */
1.161 markus 748: void
1.45 markus 749: child_set_env(char ***envp, u_int *envsizep, const char *name,
1.112 deraadt 750: const char *value)
1.1 markus 751: {
1.164 markus 752: char **env;
753: u_int envsize;
1.45 markus 754: u_int i, namelen;
1.1 markus 755:
756: /*
757: * Find the slot where the value should be stored. If the variable
758: * already exists, we reuse the slot; otherwise we append a new slot
759: * at the end of the array, expanding if necessary.
760: */
761: env = *envp;
762: namelen = strlen(name);
763: for (i = 0; env[i]; i++)
764: if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
765: break;
766: if (env[i]) {
767: /* Reuse the slot. */
768: xfree(env[i]);
769: } else {
770: /* New variable. Expand if necessary. */
1.164 markus 771: envsize = *envsizep;
772: if (i >= envsize - 1) {
773: if (envsize >= 1000)
774: fatal("child_set_env: too many env vars");
775: envsize += 50;
1.201 djm 776: env = (*envp) = xrealloc(env, envsize, sizeof(char *));
1.164 markus 777: *envsizep = envsize;
1.1 markus 778: }
779: /* Need to set the NULL pointer at end of array beyond the new slot. */
780: env[i + 1] = NULL;
781: }
782:
783: /* Allocate space and format the variable in the appropriate slot. */
784: env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
785: snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
786: }
787:
788: /*
789: * Reads environment variables from the given file and adds/overrides them
790: * into the environment. If the file does not exist, this does nothing.
791: * Otherwise, it must consist of empty lines, comments (line starts with '#')
792: * and assignments of the form name=value. No other forms are allowed.
793: */
1.94 itojun 794: static void
1.45 markus 795: read_environment_file(char ***env, u_int *envsize,
1.112 deraadt 796: const char *filename)
1.1 markus 797: {
798: FILE *f;
799: char buf[4096];
800: char *cp, *value;
1.142 deraadt 801: u_int lineno = 0;
1.1 markus 802:
803: f = fopen(filename, "r");
804: if (!f)
805: return;
806:
807: while (fgets(buf, sizeof(buf), f)) {
1.142 deraadt 808: if (++lineno > 1000)
809: fatal("Too many lines in environment file %s", filename);
1.1 markus 810: for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
811: ;
812: if (!*cp || *cp == '#' || *cp == '\n')
813: continue;
1.224 gilles 814:
815: cp[strcspn(cp, "\n")] = '\0';
816:
1.1 markus 817: value = strchr(cp, '=');
818: if (value == NULL) {
1.142 deraadt 819: fprintf(stderr, "Bad line %u in %.100s\n", lineno,
820: filename);
1.1 markus 821: continue;
822: }
1.14 deraadt 823: /*
824: * Replace the equals sign by nul, and advance value to
825: * the value string.
826: */
1.1 markus 827: *value = '\0';
828: value++;
829: child_set_env(env, envsize, cp, value);
830: }
831: fclose(f);
832: }
833:
1.127 markus 834: static char **
835: do_setup_env(Session *s, const char *shell)
1.1 markus 836: {
837: char buf[256];
1.127 markus 838: u_int i, envsize;
1.154 markus 839: char **env, *laddr;
1.127 markus 840: struct passwd *pw = s->pw;
1.1 markus 841:
842: /* Initialize the environment. */
843: envsize = 100;
1.220 djm 844: env = xcalloc(envsize, sizeof(char *));
1.1 markus 845: env[0] = NULL;
846:
1.161 markus 847: #ifdef GSSAPI
1.168 djm 848: /* Allow any GSSAPI methods that we've used to alter
1.161 markus 849: * the childs environment as they see fit
850: */
851: ssh_gssapi_do_child(&env, &envsize);
852: #endif
853:
1.1 markus 854: if (!options.use_login) {
855: /* Set basic environment. */
1.173 djm 856: for (i = 0; i < s->num_env; i++)
1.178 deraadt 857: child_set_env(&env, &envsize, s->env[i].name,
1.173 djm 858: s->env[i].val);
859:
1.1 markus 860: child_set_env(&env, &envsize, "USER", pw->pw_name);
861: child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
862: child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1.145 markus 863: if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
864: child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
865: else
866: child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1.1 markus 867:
868: snprintf(buf, sizeof buf, "%.200s/%.50s",
869: _PATH_MAILDIR, pw->pw_name);
870: child_set_env(&env, &envsize, "MAIL", buf);
871:
872: /* Normal systems set SHELL by default. */
873: child_set_env(&env, &envsize, "SHELL", shell);
874: }
875: if (getenv("TZ"))
876: child_set_env(&env, &envsize, "TZ", getenv("TZ"));
877:
878: /* Set custom environment options from RSA authentication. */
1.110 markus 879: if (!options.use_login) {
880: while (custom_environment) {
881: struct envstring *ce = custom_environment;
1.143 deraadt 882: char *str = ce->s;
1.127 markus 883:
1.143 deraadt 884: for (i = 0; str[i] != '=' && str[i]; i++)
1.110 markus 885: ;
1.143 deraadt 886: if (str[i] == '=') {
887: str[i] = 0;
888: child_set_env(&env, &envsize, str, str + i + 1);
1.110 markus 889: }
890: custom_environment = ce->next;
891: xfree(ce->s);
892: xfree(ce);
1.1 markus 893: }
894: }
895:
1.149 stevesk 896: /* SSH_CLIENT deprecated */
1.1 markus 897: snprintf(buf, sizeof buf, "%.50s %d %d",
1.127 markus 898: get_remote_ipaddr(), get_remote_port(), get_local_port());
1.1 markus 899: child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1.149 stevesk 900:
1.154 markus 901: laddr = get_local_ipaddr(packet_get_connection_in());
1.149 stevesk 902: snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1.154 markus 903: get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
904: xfree(laddr);
1.149 stevesk 905: child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1.1 markus 906:
1.60 markus 907: if (s->ttyfd != -1)
908: child_set_env(&env, &envsize, "SSH_TTY", s->tty);
909: if (s->term)
910: child_set_env(&env, &envsize, "TERM", s->term);
911: if (s->display)
912: child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34 markus 913: if (original_command)
914: child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
915: original_command);
1.96 dugsong 916: #ifdef KRB5
917: if (s->authctxt->krb5_ticket_file)
918: child_set_env(&env, &envsize, "KRB5CCNAME",
1.112 deraadt 919: s->authctxt->krb5_ticket_file);
1.96 dugsong 920: #endif
1.136 markus 921: if (auth_sock_name != NULL)
1.1 markus 922: child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1.136 markus 923: auth_sock_name);
1.1 markus 924:
925: /* read $HOME/.ssh/environment. */
1.146 markus 926: if (options.permit_user_env && !options.use_login) {
1.14 deraadt 927: snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
928: pw->pw_dir);
1.1 markus 929: read_environment_file(&env, &envsize, buf);
930: }
931: if (debug_flag) {
932: /* dump the environment */
933: fprintf(stderr, "Environment:\n");
934: for (i = 0; env[i]; i++)
935: fprintf(stderr, " %.200s\n", env[i]);
936: }
1.127 markus 937: return env;
938: }
939:
940: /*
941: * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
942: * first in this order).
943: */
944: static void
945: do_rc_files(Session *s, const char *shell)
946: {
947: FILE *f = NULL;
948: char cmd[1024];
949: int do_xauth;
950: struct stat st;
951:
952: do_xauth =
953: s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
954:
1.231 djm 955: /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
1.232 djm 956: if (!s->is_subsystem && options.adm_forced_command == NULL &&
1.234 djm 957: !no_user_rc && stat(_PATH_SSH_USER_RC, &st) >= 0) {
1.127 markus 958: snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
959: shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
960: if (debug_flag)
961: fprintf(stderr, "Running %s\n", cmd);
962: f = popen(cmd, "w");
963: if (f) {
964: if (do_xauth)
965: fprintf(f, "%s %s\n", s->auth_proto,
966: s->auth_data);
967: pclose(f);
968: } else
969: fprintf(stderr, "Could not run %s\n",
970: _PATH_SSH_USER_RC);
971: } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
972: if (debug_flag)
973: fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
974: _PATH_SSH_SYSTEM_RC);
975: f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
976: if (f) {
977: if (do_xauth)
978: fprintf(f, "%s %s\n", s->auth_proto,
979: s->auth_data);
980: pclose(f);
981: } else
982: fprintf(stderr, "Could not run %s\n",
983: _PATH_SSH_SYSTEM_RC);
984: } else if (do_xauth && options.xauth_location != NULL) {
985: /* Add authority data to .Xauthority if appropriate. */
986: if (debug_flag) {
987: fprintf(stderr,
1.151 stevesk 988: "Running %.500s remove %.100s\n",
1.165 markus 989: options.xauth_location, s->auth_display);
1.151 stevesk 990: fprintf(stderr,
991: "%.500s add %.100s %.100s %.100s\n",
1.127 markus 992: options.xauth_location, s->auth_display,
993: s->auth_proto, s->auth_data);
994: }
995: snprintf(cmd, sizeof cmd, "%s -q -",
996: options.xauth_location);
997: f = popen(cmd, "w");
998: if (f) {
1.151 stevesk 999: fprintf(f, "remove %s\n",
1000: s->auth_display);
1.127 markus 1001: fprintf(f, "add %s %s %s\n",
1002: s->auth_display, s->auth_proto,
1003: s->auth_data);
1004: pclose(f);
1005: } else {
1006: fprintf(stderr, "Could not run %s\n",
1007: cmd);
1008: }
1009: }
1010: }
1011:
1012: static void
1013: do_nologin(struct passwd *pw)
1014: {
1015: FILE *f = NULL;
1016: char buf[1024];
1017:
1018: if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
1019: f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
1020: _PATH_NOLOGIN), "r");
1021: if (f) {
1022: /* /etc/nologin exists. Print its contents and exit. */
1.155 itojun 1023: logit("User %.100s not allowed because %s exists",
1.150 stevesk 1024: pw->pw_name, _PATH_NOLOGIN);
1.127 markus 1025: while (fgets(buf, sizeof(buf), f))
1026: fputs(buf, stderr);
1027: fclose(f);
1028: exit(254);
1029: }
1030: }
1031:
1.226 djm 1032: /*
1033: * Chroot into a directory after checking it for safety: all path components
1034: * must be root-owned directories with strict permissions.
1035: */
1036: static void
1037: safely_chroot(const char *path, uid_t uid)
1038: {
1039: const char *cp;
1040: char component[MAXPATHLEN];
1041: struct stat st;
1042:
1043: if (*path != '/')
1044: fatal("chroot path does not begin at root");
1045: if (strlen(path) >= sizeof(component))
1046: fatal("chroot path too long");
1047:
1048: /*
1049: * Descend the path, checking that each component is a
1050: * root-owned directory with strict permissions.
1051: */
1052: for (cp = path; cp != NULL;) {
1053: if ((cp = strchr(cp, '/')) == NULL)
1054: strlcpy(component, path, sizeof(component));
1055: else {
1056: cp++;
1057: memcpy(component, path, cp - path);
1058: component[cp - path] = '\0';
1059: }
1060:
1061: debug3("%s: checking '%s'", __func__, component);
1062:
1063: if (stat(component, &st) != 0)
1064: fatal("%s: stat(\"%s\"): %s", __func__,
1065: component, strerror(errno));
1066: if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1067: fatal("bad ownership or modes for chroot "
1068: "directory %s\"%s\"",
1069: cp == NULL ? "" : "component ", component);
1070: if (!S_ISDIR(st.st_mode))
1071: fatal("chroot path %s\"%s\" is not a directory",
1072: cp == NULL ? "" : "component ", component);
1073:
1074: }
1075:
1076: if (chdir(path) == -1)
1077: fatal("Unable to chdir to chroot path \"%s\": "
1078: "%s", path, strerror(errno));
1079: if (chroot(path) == -1)
1080: fatal("chroot(\"%s\"): %s", path, strerror(errno));
1081: if (chdir("/") == -1)
1082: fatal("%s: chdir(/) after chroot: %s",
1083: __func__, strerror(errno));
1084: verbose("Changed root directory to \"%s\"", path);
1085: }
1086:
1.127 markus 1087: /* Set login name, uid, gid, and groups. */
1.130 provos 1088: void
1.127 markus 1089: do_setusercontext(struct passwd *pw)
1090: {
1.227 djm 1091: char *chroot_path, *tmp;
1092:
1.127 markus 1093: if (getuid() == 0 || geteuid() == 0) {
1.226 djm 1094: /* Prepare groups */
1.127 markus 1095: if (setusercontext(lc, pw, pw->pw_uid,
1.226 djm 1096: (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
1.127 markus 1097: perror("unable to set user context");
1098: exit(1);
1099: }
1.226 djm 1100:
1101: if (options.chroot_directory != NULL &&
1102: strcasecmp(options.chroot_directory, "none") != 0) {
1.227 djm 1103: tmp = tilde_expand_filename(options.chroot_directory,
1104: pw->pw_uid);
1105: chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1106: "u", pw->pw_name, (char *)NULL);
1.226 djm 1107: safely_chroot(chroot_path, pw->pw_uid);
1.227 djm 1108: free(tmp);
1.226 djm 1109: free(chroot_path);
1110: }
1111:
1112: /* Set UID */
1113: if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1114: perror("unable to set user context (setuser)");
1115: exit(1);
1116: }
1.127 markus 1117: }
1118: if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1119: fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1120: }
1121:
1.131 markus 1122: static void
1.172 markus 1123: do_pwchange(Session *s)
1124: {
1.179 dtucker 1125: fflush(NULL);
1.172 markus 1126: fprintf(stderr, "WARNING: Your password has expired.\n");
1127: if (s->ttyfd != -1) {
1.178 deraadt 1128: fprintf(stderr,
1.172 markus 1129: "You must change your password now and login again!\n");
1130: execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
1131: perror("passwd");
1132: } else {
1133: fprintf(stderr,
1134: "Password change required but no TTY available.\n");
1135: }
1136: exit(1);
1137: }
1138:
1139: static void
1.130 provos 1140: launch_login(struct passwd *pw, const char *hostname)
1141: {
1142: /* Launch login(1). */
1143:
1144: execl("/usr/bin/login", "login", "-h", hostname,
1145: "-p", "-f", "--", pw->pw_name, (char *)NULL);
1146:
1147: /* Login couldn't be executed, die. */
1148:
1149: perror("login");
1150: exit(1);
1151: }
1152:
1.172 markus 1153: static void
1154: child_close_fds(void)
1155: {
1156: int i;
1157:
1158: if (packet_get_connection_in() == packet_get_connection_out())
1159: close(packet_get_connection_in());
1160: else {
1161: close(packet_get_connection_in());
1162: close(packet_get_connection_out());
1163: }
1164: /*
1165: * Close all descriptors related to channels. They will still remain
1166: * open in the parent.
1167: */
1168: /* XXX better use close-on-exec? -markus */
1169: channel_close_all();
1170:
1171: /*
1172: * Close any extra file descriptors. Note that there may still be
1173: * descriptors left by system functions. They will be closed later.
1174: */
1175: endpwent();
1176:
1177: /*
1.188 djm 1178: * Close any extra open file descriptors so that we don't have them
1.172 markus 1179: * hanging around in clients. Note that we want to do this after
1180: * initgroups, because at least on Solaris 2.3 it leaves file
1181: * descriptors open.
1182: */
1183: for (i = 3; i < 64; i++)
1184: close(i);
1185: }
1186:
1.127 markus 1187: /*
1188: * Performs common processing for the child, such as setting up the
1189: * environment, closing extra file descriptors, setting the user and group
1190: * ids, and executing the command or shell.
1191: */
1.225 markus 1192: #define ARGV_MAX 10
1.127 markus 1193: void
1194: do_child(Session *s, const char *command)
1195: {
1196: extern char **environ;
1197: char **env;
1.225 markus 1198: char *argv[ARGV_MAX];
1.127 markus 1199: const char *shell, *shell0, *hostname = NULL;
1200: struct passwd *pw = s->pw;
1201:
1202: /* remove hostkey from the child's memory */
1203: destroy_sensitive_data();
1204:
1.172 markus 1205: /* Force a password change */
1206: if (s->authctxt->force_pwchange) {
1207: do_setusercontext(pw);
1208: child_close_fds();
1209: do_pwchange(s);
1210: exit(1);
1211: }
1212:
1.127 markus 1213: /* login(1) is only called if we execute the login shell */
1214: if (options.use_login && command != NULL)
1215: options.use_login = 0;
1216:
1217: /*
1218: * Login(1) does this as well, and it needs uid 0 for the "-h"
1219: * switch, so we let login(1) to this for us.
1220: */
1221: if (!options.use_login) {
1222: do_nologin(pw);
1223: do_setusercontext(pw);
1224: }
1225:
1226: /*
1227: * Get the shell from the password data. An empty shell field is
1228: * legal, and means /bin/sh.
1229: */
1230: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.152 markus 1231:
1232: /*
1233: * Make sure $SHELL points to the shell from the password file,
1234: * even if shell is overridden from login.conf
1235: */
1236: env = do_setup_env(s, shell);
1237:
1.127 markus 1238: shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1239:
1.26 millert 1240: /* we have to stash the hostname before we close our socket. */
1241: if (options.use_login)
1.70 stevesk 1242: hostname = get_remote_name_or_ip(utmp_len,
1.158 markus 1243: options.use_dns);
1.1 markus 1244: /*
1245: * Close the connection descriptors; note that this is the child, and
1246: * the server will still have the socket open, and it is important
1247: * that we do not shutdown it. Note that the descriptors cannot be
1248: * closed before building the environment, as we call
1249: * get_remote_ipaddr there.
1250: */
1.172 markus 1251: child_close_fds();
1.1 markus 1252:
1253: /*
1.125 deraadt 1254: * Must take new environment into use so that .ssh/rc,
1255: * /etc/ssh/sshrc and xauth are run in the proper environment.
1.1 markus 1256: */
1257: environ = env;
1.170 jakob 1258:
1259: #ifdef KRB5
1260: /*
1261: * At this point, we check to see if AFS is active and if we have
1262: * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1263: * if we can (and need to) extend the ticket into an AFS token. If
1264: * we don't do this, we run into potential problems if the user's
1265: * home directory is in AFS and it's not world-readable.
1266: */
1267:
1268: if (options.kerberos_get_afs_token && k_hasafs() &&
1.185 djm 1269: (s->authctxt->krb5_ctx != NULL)) {
1.170 jakob 1270: char cell[64];
1271:
1272: debug("Getting AFS token");
1273:
1274: k_setpag();
1275:
1276: if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1277: krb5_afslog(s->authctxt->krb5_ctx,
1278: s->authctxt->krb5_fwd_ccache, cell, NULL);
1279:
1280: krb5_afslog_home(s->authctxt->krb5_ctx,
1281: s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1282: }
1283: #endif
1.104 markus 1284:
1.188 djm 1285: /* Change current directory to the user's home directory. */
1.104 markus 1286: if (chdir(pw->pw_dir) < 0) {
1287: fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1288: pw->pw_dir, strerror(errno));
1289: if (login_getcapbool(lc, "requirehome", 0))
1290: exit(1);
1291: }
1.230 djm 1292:
1293: closefrom(STDERR_FILENO + 1);
1.1 markus 1294:
1.127 markus 1295: if (!options.use_login)
1296: do_rc_files(s, shell);
1.67 markus 1297:
1298: /* restore SIGPIPE for child */
1.217 stevesk 1299: signal(SIGPIPE, SIG_DFL);
1.67 markus 1300:
1.225 markus 1301: if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
1302: extern int optind, optreset;
1303: int i;
1304: char *p, *args;
1305:
1306: setproctitle("%s@internal-sftp-server", s->pw->pw_name);
1307: args = strdup(command ? command : "sftp-server");
1308: for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1309: if (i < ARGV_MAX - 1)
1310: argv[i++] = p;
1311: argv[i] = NULL;
1312: optind = optreset = 1;
1313: __progname = argv[0];
1.226 djm 1314: exit(sftp_server_main(i, argv, s->pw));
1.225 markus 1315: }
1316:
1.127 markus 1317: if (options.use_login) {
1.130 provos 1318: launch_login(pw, hostname);
1319: /* NEVERREACHED */
1.127 markus 1320: }
1321:
1322: /* Get the last component of the shell name. */
1323: if ((shell0 = strrchr(shell, '/')) != NULL)
1324: shell0++;
1325: else
1326: shell0 = shell;
1327:
1.1 markus 1328: /*
1329: * If we have no command, execute the shell. In this case, the shell
1330: * name to be passed in argv[0] is preceded by '-' to indicate that
1331: * this is a login shell.
1332: */
1333: if (!command) {
1.127 markus 1334: char argv0[256];
1.1 markus 1335:
1.127 markus 1336: /* Start the shell. Set initial character to '-'. */
1337: argv0[0] = '-';
1.1 markus 1338:
1.127 markus 1339: if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1.128 markus 1340: >= sizeof(argv0) - 1) {
1.127 markus 1341: errno = EINVAL;
1.1 markus 1342: perror(shell);
1343: exit(1);
1.127 markus 1344: }
1.1 markus 1345:
1.127 markus 1346: /* Execute the shell. */
1347: argv[0] = argv0;
1348: argv[1] = NULL;
1349: execve(shell, argv, env);
1350:
1351: /* Executing the shell failed. */
1352: perror(shell);
1353: exit(1);
1.1 markus 1354: }
1355: /*
1356: * Execute the command using the user's shell. This uses the -c
1357: * option to execute the command.
1358: */
1.127 markus 1359: argv[0] = (char *) shell0;
1.1 markus 1360: argv[1] = "-c";
1361: argv[2] = (char *) command;
1362: argv[3] = NULL;
1363: execve(shell, argv, env);
1364: perror(shell);
1365: exit(1);
1366: }
1367:
1.237 ! djm 1368: void
! 1369: session_unused(int id)
! 1370: {
! 1371: debug3("%s: session id %d unused", __func__, id);
! 1372: if (id >= options.max_sessions ||
! 1373: id >= sessions_nalloc) {
! 1374: fatal("%s: insane session id %d (max %d nalloc %d)",
! 1375: __func__, id, options.max_sessions, sessions_nalloc);
! 1376: }
! 1377: bzero(&sessions[id], sizeof(*sessions));
! 1378: sessions[id].self = id;
! 1379: sessions[id].used = 0;
! 1380: sessions[id].chanid = -1;
! 1381: sessions[id].ptyfd = -1;
! 1382: sessions[id].ttyfd = -1;
! 1383: sessions[id].ptymaster = -1;
! 1384: sessions[id].x11_chanids = NULL;
! 1385: sessions[id].next_unused = sessions_first_unused;
! 1386: sessions_first_unused = id;
! 1387: }
! 1388:
1.1 markus 1389: Session *
1390: session_new(void)
1391: {
1.237 ! djm 1392: Session *s, *tmp;
! 1393:
! 1394: if (sessions_first_unused == -1) {
! 1395: if (sessions_nalloc >= options.max_sessions)
! 1396: return NULL;
! 1397: debug2("%s: allocate (allocated %d max %d)",
! 1398: __func__, sessions_nalloc, options.max_sessions);
! 1399: tmp = xrealloc(sessions, sessions_nalloc + 1,
! 1400: sizeof(*sessions));
! 1401: if (tmp == NULL) {
! 1402: error("%s: cannot allocate %d sessions",
! 1403: __func__, sessions_nalloc + 1);
! 1404: return NULL;
! 1405: }
! 1406: sessions = tmp;
! 1407: session_unused(sessions_nalloc++);
! 1408: }
! 1409:
! 1410: if (sessions_first_unused >= sessions_nalloc ||
! 1411: sessions_first_unused < 0) {
! 1412: fatal("%s: insane first_unused %d max %d nalloc %d",
! 1413: __func__, sessions_first_unused, options.max_sessions,
! 1414: sessions_nalloc);
! 1415: }
! 1416:
! 1417: s = &sessions[sessions_first_unused];
! 1418: if (s->used) {
! 1419: fatal("%s: session %d already used",
! 1420: __func__, sessions_first_unused);
! 1421: }
! 1422: sessions_first_unused = s->next_unused;
! 1423: s->used = 1;
! 1424: s->next_unused = -1;
! 1425: debug("session_new: session %d", s->self);
! 1426:
! 1427: return s;
1.1 markus 1428: }
1429:
1.94 itojun 1430: static void
1.1 markus 1431: session_dump(void)
1432: {
1433: int i;
1.237 ! djm 1434: for (i = 0; i < sessions_nalloc; i++) {
1.1 markus 1435: Session *s = &sessions[i];
1.237 ! djm 1436:
! 1437: debug("dump: used %d next_unused %d session %d %p "
! 1438: "channel %d pid %ld",
1.1 markus 1439: s->used,
1.237 ! djm 1440: s->next_unused,
1.1 markus 1441: s->self,
1442: s,
1443: s->chanid,
1.137 mpech 1444: (long)s->pid);
1.1 markus 1445: }
1446: }
1447:
1.2 markus 1448: int
1.97 markus 1449: session_open(Authctxt *authctxt, int chanid)
1.2 markus 1450: {
1451: Session *s = session_new();
1452: debug("session_open: channel %d", chanid);
1453: if (s == NULL) {
1454: error("no more sessions");
1455: return 0;
1456: }
1.97 markus 1457: s->authctxt = authctxt;
1458: s->pw = authctxt->pw;
1.167 djm 1459: if (s->pw == NULL || !authctxt->valid)
1.54 stevesk 1460: fatal("no user for session %d", s->self);
1.2 markus 1461: debug("session_open: session %d: link with channel %d", s->self, chanid);
1462: s->chanid = chanid;
1463: return 1;
1464: }
1465:
1.130 provos 1466: Session *
1467: session_by_tty(char *tty)
1468: {
1469: int i;
1.237 ! djm 1470: for (i = 0; i < sessions_nalloc; i++) {
1.130 provos 1471: Session *s = &sessions[i];
1472: if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1473: debug("session_by_tty: session %d tty %s", i, tty);
1474: return s;
1475: }
1476: }
1477: debug("session_by_tty: unknown tty %.100s", tty);
1478: session_dump();
1479: return NULL;
1480: }
1481:
1.94 itojun 1482: static Session *
1.2 markus 1483: session_by_channel(int id)
1484: {
1485: int i;
1.237 ! djm 1486: for (i = 0; i < sessions_nalloc; i++) {
1.2 markus 1487: Session *s = &sessions[i];
1488: if (s->used && s->chanid == id) {
1.237 ! djm 1489: debug("session_by_channel: session %d channel %d",
! 1490: i, id);
1.2 markus 1491: return s;
1492: }
1493: }
1494: debug("session_by_channel: unknown channel %d", id);
1495: session_dump();
1496: return NULL;
1497: }
1498:
1.94 itojun 1499: static Session *
1.184 djm 1500: session_by_x11_channel(int id)
1501: {
1502: int i, j;
1503:
1.237 ! djm 1504: for (i = 0; i < sessions_nalloc; i++) {
1.184 djm 1505: Session *s = &sessions[i];
1506:
1507: if (s->x11_chanids == NULL || !s->used)
1508: continue;
1509: for (j = 0; s->x11_chanids[j] != -1; j++) {
1510: if (s->x11_chanids[j] == id) {
1511: debug("session_by_x11_channel: session %d "
1512: "channel %d", s->self, id);
1513: return s;
1514: }
1515: }
1516: }
1517: debug("session_by_x11_channel: unknown channel %d", id);
1518: session_dump();
1519: return NULL;
1520: }
1521:
1522: static Session *
1.2 markus 1523: session_by_pid(pid_t pid)
1524: {
1525: int i;
1.137 mpech 1526: debug("session_by_pid: pid %ld", (long)pid);
1.237 ! djm 1527: for (i = 0; i < sessions_nalloc; i++) {
1.2 markus 1528: Session *s = &sessions[i];
1529: if (s->used && s->pid == pid)
1530: return s;
1531: }
1.137 mpech 1532: error("session_by_pid: unknown pid %ld", (long)pid);
1.2 markus 1533: session_dump();
1534: return NULL;
1535: }
1536:
1.94 itojun 1537: static int
1.2 markus 1538: session_window_change_req(Session *s)
1539: {
1540: s->col = packet_get_int();
1541: s->row = packet_get_int();
1542: s->xpixel = packet_get_int();
1543: s->ypixel = packet_get_int();
1.116 markus 1544: packet_check_eom();
1.2 markus 1545: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1546: return 1;
1547: }
1548:
1.94 itojun 1549: static int
1.2 markus 1550: session_pty_req(Session *s)
1551: {
1.45 markus 1552: u_int len;
1.72 stevesk 1553: int n_bytes;
1.132 markus 1554:
1.86 markus 1555: if (no_pty_flag) {
1556: debug("Allocating a pty not permitted for this authentication.");
1.19 markus 1557: return 0;
1.86 markus 1558: }
1559: if (s->ttyfd != -1) {
1560: packet_disconnect("Protocol error: you already have a pty.");
1.3 markus 1561: return 0;
1.86 markus 1562: }
1563:
1.2 markus 1564: s->term = packet_get_string(&len);
1.86 markus 1565:
1566: if (compat20) {
1567: s->col = packet_get_int();
1568: s->row = packet_get_int();
1569: } else {
1570: s->row = packet_get_int();
1571: s->col = packet_get_int();
1572: }
1.2 markus 1573: s->xpixel = packet_get_int();
1574: s->ypixel = packet_get_int();
1575:
1576: if (strcmp(s->term, "") == 0) {
1577: xfree(s->term);
1578: s->term = NULL;
1579: }
1.86 markus 1580:
1.2 markus 1581: /* Allocate a pty and open it. */
1.86 markus 1582: debug("Allocating pty.");
1.237 ! djm 1583: if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
! 1584: sizeof(s->tty)))) {
1.86 markus 1585: if (s->term)
1586: xfree(s->term);
1.2 markus 1587: s->term = NULL;
1588: s->ptyfd = -1;
1589: s->ttyfd = -1;
1590: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1591: return 0;
1.2 markus 1592: }
1593: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1.86 markus 1594:
1595: /* for SSH1 the tty modes length is not given */
1596: if (!compat20)
1597: n_bytes = packet_remaining();
1598: tty_parse_modes(s->ttyfd, &n_bytes);
1599:
1.130 provos 1600: if (!use_privsep)
1601: pty_setowner(s->pw, s->tty);
1.86 markus 1602:
1603: /* Set window size from the packet. */
1.2 markus 1604: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1605:
1.116 markus 1606: packet_check_eom();
1.9 markus 1607: session_proctitle(s);
1.2 markus 1608: return 1;
1609: }
1610:
1.94 itojun 1611: static int
1.7 markus 1612: session_subsystem_req(Session *s)
1613: {
1.105 markus 1614: struct stat st;
1.45 markus 1615: u_int len;
1.7 markus 1616: int success = 0;
1.205 djm 1617: char *prog, *cmd, *subsys = packet_get_string(&len);
1.182 djm 1618: u_int i;
1.7 markus 1619:
1.116 markus 1620: packet_check_eom();
1.155 itojun 1621: logit("subsystem request for %.100s", subsys);
1.18 jakob 1622:
1623: for (i = 0; i < options.num_subsystems; i++) {
1.105 markus 1624: if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1.205 djm 1625: prog = options.subsystem_command[i];
1626: cmd = options.subsystem_args[i];
1.226 djm 1627: if (!strcmp(INTERNAL_SFTP_NAME, prog)) {
1.225 markus 1628: s->is_subsystem = SUBSYSTEM_INT_SFTP;
1629: } else if (stat(prog, &st) < 0) {
1.205 djm 1630: error("subsystem: cannot stat %s: %s", prog,
1.105 markus 1631: strerror(errno));
1632: break;
1.225 markus 1633: } else {
1634: s->is_subsystem = SUBSYSTEM_EXT;
1.105 markus 1635: }
1636: debug("subsystem: exec() %s", cmd);
1.237 ! djm 1637: success = do_exec(s, cmd) == 0;
1.122 markus 1638: break;
1.18 jakob 1639: }
1640: }
1641:
1642: if (!success)
1.155 itojun 1643: logit("subsystem request for %.100s failed, subsystem not found",
1.105 markus 1644: subsys);
1.7 markus 1645:
1646: xfree(subsys);
1647: return success;
1648: }
1649:
1.94 itojun 1650: static int
1.7 markus 1651: session_x11_req(Session *s)
1652: {
1.81 markus 1653: int success;
1.7 markus 1654:
1.184 djm 1655: if (s->auth_proto != NULL || s->auth_data != NULL) {
1656: error("session_x11_req: session %d: "
1.190 stevesk 1657: "x11 forwarding already active", s->self);
1.184 djm 1658: return 0;
1659: }
1.7 markus 1660: s->single_connection = packet_get_char();
1661: s->auth_proto = packet_get_string(NULL);
1662: s->auth_data = packet_get_string(NULL);
1663: s->screen = packet_get_int();
1.116 markus 1664: packet_check_eom();
1.7 markus 1665:
1.81 markus 1666: success = session_setup_x11fwd(s);
1667: if (!success) {
1.7 markus 1668: xfree(s->auth_proto);
1669: xfree(s->auth_data);
1.84 markus 1670: s->auth_proto = NULL;
1671: s->auth_data = NULL;
1.7 markus 1672: }
1.81 markus 1673: return success;
1.7 markus 1674: }
1675:
1.94 itojun 1676: static int
1.19 markus 1677: session_shell_req(Session *s)
1678: {
1.116 markus 1679: packet_check_eom();
1.237 ! djm 1680: return do_exec(s, NULL) == 0;
1.19 markus 1681: }
1682:
1.94 itojun 1683: static int
1.19 markus 1684: session_exec_req(Session *s)
1685: {
1.237 ! djm 1686: u_int len, success;
! 1687:
1.19 markus 1688: char *command = packet_get_string(&len);
1.116 markus 1689: packet_check_eom();
1.237 ! djm 1690: success = do_exec(s, command) == 0;
1.92 markus 1691: xfree(command);
1.237 ! djm 1692: return success;
1.19 markus 1693: }
1694:
1.94 itojun 1695: static int
1.157 markus 1696: session_break_req(Session *s)
1697: {
1698:
1.175 deraadt 1699: packet_get_int(); /* ignored */
1.157 markus 1700: packet_check_eom();
1701:
1.237 ! djm 1702: if (s->ttyfd == -1 || tcsendbreak(s->ttyfd, 0) < 0)
1.157 markus 1703: return 0;
1704: return 1;
1705: }
1706:
1707: static int
1.173 djm 1708: session_env_req(Session *s)
1709: {
1710: char *name, *val;
1711: u_int name_len, val_len, i;
1712:
1713: name = packet_get_string(&name_len);
1714: val = packet_get_string(&val_len);
1715: packet_check_eom();
1716:
1717: /* Don't set too many environment variables */
1718: if (s->num_env > 128) {
1719: debug2("Ignoring env request %s: too many env vars", name);
1720: goto fail;
1721: }
1722:
1723: for (i = 0; i < options.num_accept_env; i++) {
1724: if (match_pattern(name, options.accept_env[i])) {
1725: debug2("Setting env %d: %s=%s", s->num_env, name, val);
1.201 djm 1726: s->env = xrealloc(s->env, s->num_env + 1,
1727: sizeof(*s->env));
1.173 djm 1728: s->env[s->num_env].name = name;
1729: s->env[s->num_env].val = val;
1730: s->num_env++;
1731: return (1);
1732: }
1733: }
1734: debug2("Ignoring env request %s: disallowed name", name);
1735:
1736: fail:
1737: xfree(name);
1738: xfree(val);
1739: return (0);
1740: }
1741:
1742: static int
1.43 markus 1743: session_auth_agent_req(Session *s)
1744: {
1745: static int called = 0;
1.116 markus 1746: packet_check_eom();
1.235 pyr 1747: if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
1.44 markus 1748: debug("session_auth_agent_req: no_agent_forwarding_flag");
1749: return 0;
1750: }
1.43 markus 1751: if (called) {
1752: return 0;
1753: } else {
1754: called = 1;
1755: return auth_input_request_forwarding(s->pw);
1756: }
1757: }
1758:
1.123 markus 1759: int
1760: session_input_channel_req(Channel *c, const char *rtype)
1.2 markus 1761: {
1762: int success = 0;
1763: Session *s;
1764:
1.123 markus 1765: if ((s = session_by_channel(c->self)) == NULL) {
1.155 itojun 1766: logit("session_input_channel_req: no session %d req %.100s",
1.123 markus 1767: c->self, rtype);
1768: return 0;
1769: }
1770: debug("session_input_channel_req: session %d req %s", s->self, rtype);
1.2 markus 1771:
1772: /*
1.64 markus 1773: * a session is in LARVAL state until a shell, a command
1774: * or a subsystem is executed
1.2 markus 1775: */
1776: if (c->type == SSH_CHANNEL_LARVAL) {
1777: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1778: success = session_shell_req(s);
1.2 markus 1779: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1780: success = session_exec_req(s);
1.2 markus 1781: } else if (strcmp(rtype, "pty-req") == 0) {
1.221 stevesk 1782: success = session_pty_req(s);
1.7 markus 1783: } else if (strcmp(rtype, "x11-req") == 0) {
1784: success = session_x11_req(s);
1.43 markus 1785: } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1786: success = session_auth_agent_req(s);
1.7 markus 1787: } else if (strcmp(rtype, "subsystem") == 0) {
1788: success = session_subsystem_req(s);
1.173 djm 1789: } else if (strcmp(rtype, "env") == 0) {
1790: success = session_env_req(s);
1.2 markus 1791: }
1792: }
1793: if (strcmp(rtype, "window-change") == 0) {
1794: success = session_window_change_req(s);
1.177 djm 1795: } else if (strcmp(rtype, "break") == 0) {
1796: success = session_break_req(s);
1.2 markus 1797: }
1.177 djm 1798:
1.123 markus 1799: return success;
1.2 markus 1800: }
1801:
1802: void
1803: session_set_fds(Session *s, int fdin, int fdout, int fderr)
1804: {
1805: if (!compat20)
1806: fatal("session_set_fds: called for proto != 2.0");
1807: /*
1808: * now that have a child and a pipe to the child,
1809: * we can activate our channel and register the fd's
1810: */
1811: if (s->chanid == -1)
1812: fatal("no channel for session %d", s->self);
1813: channel_set_fds(s->chanid,
1814: fdout, fdin, fderr,
1.42 markus 1815: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1.126 markus 1816: 1,
1817: CHAN_SES_WINDOW_DEFAULT);
1.2 markus 1818: }
1819:
1.85 markus 1820: /*
1821: * Function to perform pty cleanup. Also called if we get aborted abnormally
1822: * (e.g., due to a dropped connection).
1823: */
1.130 provos 1824: void
1.165 markus 1825: session_pty_cleanup2(Session *s)
1.1 markus 1826: {
1.85 markus 1827: if (s == NULL) {
1828: error("session_pty_cleanup: no session");
1829: return;
1830: }
1831: if (s->ttyfd == -1)
1.1 markus 1832: return;
1833:
1.54 stevesk 1834: debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1 markus 1835:
1836: /* Record that the user has logged out. */
1.85 markus 1837: if (s->pid != 0)
1838: record_logout(s->pid, s->tty);
1.1 markus 1839:
1840: /* Release the pseudo-tty. */
1.130 provos 1841: if (getuid() == 0)
1842: pty_release(s->tty);
1.1 markus 1843:
1844: /*
1845: * Close the server side of the socket pairs. We must do this after
1846: * the pty cleanup, so that another process doesn't get this pty
1847: * while we're still cleaning up.
1848: */
1.237 ! djm 1849: if (s->ptymaster != -1 && close(s->ptymaster) < 0)
! 1850: error("close(s->ptymaster/%d): %s",
! 1851: s->ptymaster, strerror(errno));
1.108 markus 1852:
1853: /* unlink pty from session */
1854: s->ttyfd = -1;
1.2 markus 1855: }
1856:
1.130 provos 1857: void
1.165 markus 1858: session_pty_cleanup(Session *s)
1.130 provos 1859: {
1.165 markus 1860: PRIVSEP(session_pty_cleanup2(s));
1.130 provos 1861: }
1862:
1.147 markus 1863: static char *
1864: sig2name(int sig)
1865: {
1866: #define SSH_SIG(x) if (sig == SIG ## x) return #x
1867: SSH_SIG(ABRT);
1868: SSH_SIG(ALRM);
1869: SSH_SIG(FPE);
1870: SSH_SIG(HUP);
1871: SSH_SIG(ILL);
1872: SSH_SIG(INT);
1873: SSH_SIG(KILL);
1874: SSH_SIG(PIPE);
1875: SSH_SIG(QUIT);
1876: SSH_SIG(SEGV);
1877: SSH_SIG(TERM);
1878: SSH_SIG(USR1);
1879: SSH_SIG(USR2);
1880: #undef SSH_SIG
1881: return "SIG@openssh.com";
1882: }
1883:
1.94 itojun 1884: static void
1.184 djm 1885: session_close_x11(int id)
1886: {
1887: Channel *c;
1888:
1.189 markus 1889: if ((c = channel_by_id(id)) == NULL) {
1.184 djm 1890: debug("session_close_x11: x11 channel %d missing", id);
1891: } else {
1892: /* Detach X11 listener */
1893: debug("session_close_x11: detach x11 channel %d", id);
1894: channel_cancel_cleanup(id);
1895: if (c->ostate != CHAN_OUTPUT_CLOSED)
1896: chan_mark_dead(c);
1897: }
1898: }
1899:
1900: static void
1901: session_close_single_x11(int id, void *arg)
1902: {
1903: Session *s;
1904: u_int i;
1905:
1906: debug3("session_close_single_x11: channel %d", id);
1907: channel_cancel_cleanup(id);
1.221 stevesk 1908: if ((s = session_by_x11_channel(id)) == NULL)
1.184 djm 1909: fatal("session_close_single_x11: no x11 channel %d", id);
1910: for (i = 0; s->x11_chanids[i] != -1; i++) {
1911: debug("session_close_single_x11: session %d: "
1912: "closing channel %d", s->self, s->x11_chanids[i]);
1913: /*
1914: * The channel "id" is already closing, but make sure we
1915: * close all of its siblings.
1916: */
1917: if (s->x11_chanids[i] != id)
1918: session_close_x11(s->x11_chanids[i]);
1919: }
1920: xfree(s->x11_chanids);
1921: s->x11_chanids = NULL;
1922: if (s->display) {
1923: xfree(s->display);
1924: s->display = NULL;
1925: }
1926: if (s->auth_proto) {
1927: xfree(s->auth_proto);
1928: s->auth_proto = NULL;
1929: }
1930: if (s->auth_data) {
1931: xfree(s->auth_data);
1932: s->auth_data = NULL;
1933: }
1934: if (s->auth_display) {
1935: xfree(s->auth_display);
1936: s->auth_display = NULL;
1937: }
1938: }
1939:
1940: static void
1.2 markus 1941: session_exit_message(Session *s, int status)
1942: {
1943: Channel *c;
1.124 markus 1944:
1945: if ((c = channel_lookup(s->chanid)) == NULL)
1.85 markus 1946: fatal("session_exit_message: session %d: no channel %d",
1.2 markus 1947: s->self, s->chanid);
1.137 mpech 1948: debug("session_exit_message: session %d channel %d pid %ld",
1949: s->self, s->chanid, (long)s->pid);
1.2 markus 1950:
1951: if (WIFEXITED(status)) {
1.124 markus 1952: channel_request_start(s->chanid, "exit-status", 0);
1.2 markus 1953: packet_put_int(WEXITSTATUS(status));
1954: packet_send();
1955: } else if (WIFSIGNALED(status)) {
1.124 markus 1956: channel_request_start(s->chanid, "exit-signal", 0);
1.147 markus 1957: packet_put_cstring(sig2name(WTERMSIG(status)));
1.229 markus 1958: packet_put_char(WCOREDUMP(status)? 1 : 0);
1.2 markus 1959: packet_put_cstring("");
1960: packet_put_cstring("");
1961: packet_send();
1962: } else {
1963: /* Some weird exit cause. Just exit. */
1964: packet_disconnect("wait returned status %04x.", status);
1965: }
1966:
1967: /* disconnect channel */
1968: debug("session_exit_message: release channel %d", s->chanid);
1.187 djm 1969:
1970: /*
1971: * Adjust cleanup callback attachment to send close messages when
1.199 deraadt 1972: * the channel gets EOF. The session will be then be closed
1.187 djm 1973: * by session_close_by_channel when the childs close their fds.
1974: */
1975: channel_register_cleanup(c->self, session_close_by_channel, 1);
1976:
1.5 markus 1977: /*
1978: * emulate a write failure with 'chan_write_failed', nobody will be
1979: * interested in data we write.
1980: * Note that we must not call 'chan_read_failed', since there could
1981: * be some more data waiting in the pipe.
1982: */
1.8 markus 1983: if (c->ostate != CHAN_OUTPUT_CLOSED)
1984: chan_write_failed(c);
1.2 markus 1985: }
1986:
1.130 provos 1987: void
1.85 markus 1988: session_close(Session *s)
1.2 markus 1989: {
1.182 djm 1990: u_int i;
1.173 djm 1991:
1.137 mpech 1992: debug("session_close: session %d pid %ld", s->self, (long)s->pid);
1.165 markus 1993: if (s->ttyfd != -1)
1.85 markus 1994: session_pty_cleanup(s);
1.2 markus 1995: if (s->term)
1996: xfree(s->term);
1997: if (s->display)
1998: xfree(s->display);
1.184 djm 1999: if (s->x11_chanids)
2000: xfree(s->x11_chanids);
1.118 stevesk 2001: if (s->auth_display)
2002: xfree(s->auth_display);
1.2 markus 2003: if (s->auth_data)
2004: xfree(s->auth_data);
2005: if (s->auth_proto)
2006: xfree(s->auth_proto);
1.219 djm 2007: if (s->env != NULL) {
2008: for (i = 0; i < s->num_env; i++) {
2009: xfree(s->env[i].name);
2010: xfree(s->env[i].val);
2011: }
2012: xfree(s->env);
1.173 djm 2013: }
1.9 markus 2014: session_proctitle(s);
1.237 ! djm 2015: session_unused(s->self);
1.2 markus 2016: }
2017:
2018: void
2019: session_close_by_pid(pid_t pid, int status)
2020: {
2021: Session *s = session_by_pid(pid);
2022: if (s == NULL) {
1.137 mpech 2023: debug("session_close_by_pid: no session for pid %ld",
2024: (long)pid);
1.2 markus 2025: return;
2026: }
2027: if (s->chanid != -1)
2028: session_exit_message(s, status);
1.187 djm 2029: if (s->ttyfd != -1)
2030: session_pty_cleanup(s);
1.197 djm 2031: s->pid = 0;
1.98 markus 2032: }
2033:
1.2 markus 2034: /*
2035: * this is called when a channel dies before
2036: * the session 'child' itself dies
2037: */
2038: void
2039: session_close_by_channel(int id, void *arg)
2040: {
2041: Session *s = session_by_channel(id);
1.187 djm 2042: u_int i;
1.184 djm 2043:
1.2 markus 2044: if (s == NULL) {
1.107 markus 2045: debug("session_close_by_channel: no session for id %d", id);
1.2 markus 2046: return;
2047: }
1.137 mpech 2048: debug("session_close_by_channel: channel %d child %ld",
2049: id, (long)s->pid);
1.107 markus 2050: if (s->pid != 0) {
2051: debug("session_close_by_channel: channel %d: has child", id);
1.108 markus 2052: /*
2053: * delay detach of session, but release pty, since
2054: * the fd's to the child are already closed
2055: */
1.165 markus 2056: if (s->ttyfd != -1)
1.108 markus 2057: session_pty_cleanup(s);
1.107 markus 2058: return;
2059: }
2060: /* detach by removing callback */
1.2 markus 2061: channel_cancel_cleanup(s->chanid);
1.187 djm 2062:
2063: /* Close any X11 listeners associated with this session */
2064: if (s->x11_chanids != NULL) {
2065: for (i = 0; s->x11_chanids[i] != -1; i++) {
2066: session_close_x11(s->x11_chanids[i]);
2067: s->x11_chanids[i] = -1;
2068: }
2069: }
2070:
1.2 markus 2071: s->chanid = -1;
1.106 markus 2072: session_close(s);
2073: }
2074:
2075: void
1.130 provos 2076: session_destroy_all(void (*closefunc)(Session *))
1.106 markus 2077: {
2078: int i;
1.237 ! djm 2079: for (i = 0; i < sessions_nalloc; i++) {
1.106 markus 2080: Session *s = &sessions[i];
1.130 provos 2081: if (s->used) {
2082: if (closefunc != NULL)
2083: closefunc(s);
2084: else
2085: session_close(s);
2086: }
1.2 markus 2087: }
1.9 markus 2088: }
2089:
1.94 itojun 2090: static char *
1.9 markus 2091: session_tty_list(void)
2092: {
2093: static char buf[1024];
2094: int i;
2095: buf[0] = '\0';
1.237 ! djm 2096: for (i = 0; i < sessions_nalloc; i++) {
1.9 markus 2097: Session *s = &sessions[i];
2098: if (s->used && s->ttyfd != -1) {
2099: if (buf[0] != '\0')
2100: strlcat(buf, ",", sizeof buf);
2101: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2102: }
2103: }
2104: if (buf[0] == '\0')
2105: strlcpy(buf, "notty", sizeof buf);
2106: return buf;
2107: }
2108:
2109: void
2110: session_proctitle(Session *s)
2111: {
2112: if (s->pw == NULL)
2113: error("no user for session %d", s->self);
2114: else
2115: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.81 markus 2116: }
2117:
2118: int
2119: session_setup_x11fwd(Session *s)
2120: {
2121: struct stat st;
1.109 stevesk 2122: char display[512], auth_display[512];
2123: char hostname[MAXHOSTNAMELEN];
1.184 djm 2124: u_int i;
1.81 markus 2125:
2126: if (no_x11_forwarding_flag) {
2127: packet_send_debug("X11 forwarding disabled in user configuration file.");
2128: return 0;
2129: }
2130: if (!options.x11_forwarding) {
2131: debug("X11 forwarding disabled in server configuration file.");
2132: return 0;
2133: }
2134: if (!options.xauth_location ||
2135: (stat(options.xauth_location, &st) == -1)) {
2136: packet_send_debug("No xauth program; cannot forward with spoofing.");
1.91 markus 2137: return 0;
2138: }
2139: if (options.use_login) {
2140: packet_send_debug("X11 forwarding disabled; "
2141: "not compatible with UseLogin=yes.");
1.81 markus 2142: return 0;
2143: }
1.87 markus 2144: if (s->display != NULL) {
1.81 markus 2145: debug("X11 display already set.");
2146: return 0;
2147: }
1.140 deraadt 2148: if (x11_create_display_inet(options.x11_display_offset,
2149: options.x11_use_localhost, s->single_connection,
1.184 djm 2150: &s->display_number, &s->x11_chanids) == -1) {
1.87 markus 2151: debug("x11_create_display_inet failed.");
1.81 markus 2152: return 0;
1.184 djm 2153: }
2154: for (i = 0; s->x11_chanids[i] != -1; i++) {
2155: channel_register_cleanup(s->x11_chanids[i],
1.187 djm 2156: session_close_single_x11, 0);
1.81 markus 2157: }
1.109 stevesk 2158:
2159: /* Set up a suitable value for the DISPLAY variable. */
2160: if (gethostname(hostname, sizeof(hostname)) < 0)
2161: fatal("gethostname: %.100s", strerror(errno));
2162: /*
2163: * auth_display must be used as the displayname when the
2164: * authorization entry is added with xauth(1). This will be
2165: * different than the DISPLAY string for localhost displays.
2166: */
1.119 stevesk 2167: if (options.x11_use_localhost) {
1.140 deraadt 2168: snprintf(display, sizeof display, "localhost:%u.%u",
1.109 stevesk 2169: s->display_number, s->screen);
1.140 deraadt 2170: snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
1.118 stevesk 2171: s->display_number, s->screen);
1.109 stevesk 2172: s->display = xstrdup(display);
1.118 stevesk 2173: s->auth_display = xstrdup(auth_display);
1.109 stevesk 2174: } else {
1.140 deraadt 2175: snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
1.109 stevesk 2176: s->display_number, s->screen);
2177: s->display = xstrdup(display);
1.118 stevesk 2178: s->auth_display = xstrdup(display);
1.109 stevesk 2179: }
2180:
1.81 markus 2181: return 1;
1.2 markus 2182: }
2183:
1.94 itojun 2184: static void
1.49 markus 2185: do_authenticated2(Authctxt *authctxt)
1.2 markus 2186: {
1.97 markus 2187: server_loop2(authctxt);
1.165 markus 2188: }
2189:
2190: void
2191: do_cleanup(Authctxt *authctxt)
2192: {
2193: static int called = 0;
2194:
2195: debug("do_cleanup");
2196:
2197: /* no cleanup if we're in the child for login shell */
2198: if (is_child)
2199: return;
2200:
2201: /* avoid double cleanup */
2202: if (called)
2203: return;
2204: called = 1;
2205:
1.218 markus 2206: if (authctxt == NULL || !authctxt->authenticated)
1.165 markus 2207: return;
2208: #ifdef KRB5
2209: if (options.kerberos_ticket_cleanup &&
2210: authctxt->krb5_ctx)
2211: krb5_cleanup_proc(authctxt);
1.161 markus 2212: #endif
1.165 markus 2213:
2214: #ifdef GSSAPI
2215: if (compat20 && options.gss_cleanup_creds)
2216: ssh_gssapi_cleanup_creds();
2217: #endif
2218:
2219: /* remove agent socket */
2220: auth_sock_cleanup_proc(authctxt->pw);
2221:
2222: /*
2223: * Cleanup ptys/utmp only if privsep is disabled,
2224: * or if running in monitor.
2225: */
2226: if (!use_privsep || mm_is_monitor())
2227: session_destroy_all(session_pty_cleanup2);
1.1 markus 2228: }