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