Annotation of src/usr.bin/ssh/session.c, Revision 1.288
1.288 ! deraadt 1: /* $OpenBSD: session.c,v 1.287 2017/05/31 08:09:45 markus 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.286 djm 242: if (no_port_forwarding_flag || options.disable_forwarding ||
1.261 djm 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.285 otto 508: do_login(s, command);
1.228 djm 509:
1.237 djm 510: /*
511: * Do common processing for the child, such as execing
512: * the command.
513: */
1.60 markus 514: do_child(s, command);
1.1 markus 515: /* NOTREACHED */
1.237 djm 516: default:
517: break;
1.1 markus 518: }
519: s->pid = pid;
520:
521: /* Parent. Close the slave side of the pseudo tty. */
522: close(ttyfd);
523:
1.237 djm 524: /* Enter interactive session. */
1.1 markus 525: s->ptymaster = ptymaster;
1.257 djm 526: packet_set_interactive(1,
527: options.ip_qos_interactive, options.ip_qos_bulk);
1.283 markus 528: session_set_fds(s, ptyfd, fdout, -1, 1, 1);
1.237 djm 529: return 0;
1.24 markus 530: }
531:
1.90 markus 532: /*
533: * This is called to fork and execute a command. If another command is
534: * to be forced, execute that instead.
535: */
1.237 djm 536: int
1.90 markus 537: do_exec(Session *s, const char *command)
538: {
1.281 djm 539: struct ssh *ssh = active_state; /* XXX */
1.237 djm 540: int ret;
1.280 djm 541: const char *forced = NULL, *tty = NULL;
542: char session_type[1024];
1.237 djm 543:
1.210 dtucker 544: if (options.adm_forced_command) {
545: original_command = command;
546: command = options.adm_forced_command;
1.267 djm 547: forced = "(config)";
1.210 dtucker 548: } else if (forced_command) {
1.90 markus 549: original_command = command;
550: command = forced_command;
1.267 djm 551: forced = "(key-option)";
552: }
553: if (forced != NULL) {
1.248 djm 554: if (IS_INTERNAL_SFTP(command)) {
555: s->is_subsystem = s->is_subsystem ?
556: SUBSYSTEM_INT_SFTP : SUBSYSTEM_INT_SFTP_ERROR;
557: } else if (s->is_subsystem)
1.225 markus 558: s->is_subsystem = SUBSYSTEM_EXT;
1.267 djm 559: snprintf(session_type, sizeof(session_type),
560: "forced-command %s '%.900s'", forced, command);
561: } else if (s->is_subsystem) {
562: snprintf(session_type, sizeof(session_type),
563: "subsystem '%.900s'", s->subsys);
564: } else if (command == NULL) {
565: snprintf(session_type, sizeof(session_type), "shell");
566: } else {
567: /* NB. we don't log unforced commands to preserve privacy */
568: snprintf(session_type, sizeof(session_type), "command");
1.90 markus 569: }
1.163 markus 570:
1.267 djm 571: if (s->ttyfd != -1) {
572: tty = s->tty;
573: if (strncmp(tty, "/dev/", 5) == 0)
574: tty += 5;
575: }
576:
1.280 djm 577: verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
1.267 djm 578: session_type,
579: tty == NULL ? "" : " on ",
580: tty == NULL ? "" : tty,
581: s->pw->pw_name,
1.281 djm 582: ssh_remote_ipaddr(ssh),
583: ssh_remote_port(ssh),
1.280 djm 584: s->self);
1.267 djm 585:
1.163 markus 586: #ifdef GSSAPI
587: if (options.gss_authentication) {
588: temporarily_use_uid(s->pw);
589: ssh_gssapi_storecreds();
590: restore_uid();
591: }
592: #endif
1.90 markus 593: if (s->ttyfd != -1)
1.237 djm 594: ret = do_exec_pty(s, command);
1.90 markus 595: else
1.237 djm 596: ret = do_exec_no_pty(s, command);
1.90 markus 597:
1.92 markus 598: original_command = NULL;
1.179 dtucker 599:
600: /*
601: * Clear loginmsg: it's the child's responsibility to display
602: * it to the user, otherwise multiple sessions may accumulate
603: * multiple copies of the login messages.
604: */
605: buffer_clear(&loginmsg);
1.237 djm 606:
607: return ret;
1.90 markus 608: }
609:
610:
1.24 markus 611: /* administrative, login(1)-like work */
612: void
1.41 markus 613: do_login(Session *s, const char *command)
1.24 markus 614: {
1.281 djm 615: struct ssh *ssh = active_state; /* XXX */
1.24 markus 616: socklen_t fromlen;
617: struct sockaddr_storage from;
618: struct passwd * pw = s->pw;
619: pid_t pid = getpid();
620:
621: /*
622: * Get IP address of client. If the connection is not a socket, let
623: * the address be 0.0.0.0.
624: */
625: memset(&from, 0, sizeof(from));
1.148 stevesk 626: fromlen = sizeof(from);
1.24 markus 627: if (packet_connection_is_on_socket()) {
628: if (getpeername(packet_get_connection_in(),
1.200 deraadt 629: (struct sockaddr *)&from, &fromlen) < 0) {
1.24 markus 630: debug("getpeername: %.100s", strerror(errno));
1.165 markus 631: cleanup_exit(255);
1.24 markus 632: }
633: }
1.25 markus 634:
1.24 markus 635: /* Record that there was a login on that tty from the remote host. */
1.133 markus 636: if (!use_privsep)
637: record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
1.281 djm 638: session_get_remote_name_or_ip(ssh, utmp_len,
1.158 markus 639: options.use_dns),
1.148 stevesk 640: (struct sockaddr *)&from, fromlen);
1.24 markus 641:
1.73 djm 642: if (check_quietlogin(s, command))
1.24 markus 643: return;
1.73 djm 644:
1.179 dtucker 645: display_loginmsg();
1.73 djm 646:
647: do_motd();
648: }
649:
650: /*
651: * Display the message of the day.
652: */
653: void
654: do_motd(void)
655: {
656: FILE *f;
657: char buf[256];
658:
1.24 markus 659: if (options.print_motd) {
1.28 millert 660: f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
661: "/etc/motd"), "r");
1.24 markus 662: if (f) {
663: while (fgets(buf, sizeof(buf), f))
664: fputs(buf, stdout);
665: fclose(f);
666: }
1.2 markus 667: }
1.73 djm 668: }
669:
670:
671: /*
672: * Check for quiet login, either .hushlogin or command given.
673: */
674: int
675: check_quietlogin(Session *s, const char *command)
676: {
677: char buf[256];
1.96 dugsong 678: struct passwd *pw = s->pw;
1.73 djm 679: struct stat st;
680:
681: /* Return 1 if .hushlogin exists or a command given. */
682: if (command != NULL)
683: return 1;
684: snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
685: if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
686: return 1;
687: return 0;
1.1 markus 688: }
689:
690: /*
691: * Sets the value of the given variable in the environment. If the variable
1.244 tobias 692: * already exists, its value is overridden.
1.1 markus 693: */
1.161 markus 694: void
1.45 markus 695: child_set_env(char ***envp, u_int *envsizep, const char *name,
1.112 deraadt 696: const char *value)
1.1 markus 697: {
1.164 markus 698: char **env;
699: u_int envsize;
1.45 markus 700: u_int i, namelen;
1.1 markus 701:
1.271 djm 702: if (strchr(name, '=') != NULL) {
703: error("Invalid environment variable \"%.100s\"", name);
704: return;
705: }
706:
1.1 markus 707: /*
708: * Find the slot where the value should be stored. If the variable
709: * already exists, we reuse the slot; otherwise we append a new slot
710: * at the end of the array, expanding if necessary.
711: */
712: env = *envp;
713: namelen = strlen(name);
714: for (i = 0; env[i]; i++)
715: if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
716: break;
717: if (env[i]) {
718: /* Reuse the slot. */
1.265 djm 719: free(env[i]);
1.1 markus 720: } else {
721: /* New variable. Expand if necessary. */
1.164 markus 722: envsize = *envsizep;
723: if (i >= envsize - 1) {
724: if (envsize >= 1000)
725: fatal("child_set_env: too many env vars");
726: envsize += 50;
1.278 deraadt 727: env = (*envp) = xreallocarray(env, envsize, sizeof(char *));
1.164 markus 728: *envsizep = envsize;
1.1 markus 729: }
730: /* Need to set the NULL pointer at end of array beyond the new slot. */
731: env[i + 1] = NULL;
732: }
733:
734: /* Allocate space and format the variable in the appropriate slot. */
735: env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
736: snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
737: }
738:
739: /*
740: * Reads environment variables from the given file and adds/overrides them
741: * into the environment. If the file does not exist, this does nothing.
742: * Otherwise, it must consist of empty lines, comments (line starts with '#')
743: * and assignments of the form name=value. No other forms are allowed.
744: */
1.94 itojun 745: static void
1.45 markus 746: read_environment_file(char ***env, u_int *envsize,
1.112 deraadt 747: const char *filename)
1.1 markus 748: {
749: FILE *f;
750: char buf[4096];
751: char *cp, *value;
1.142 deraadt 752: u_int lineno = 0;
1.1 markus 753:
754: f = fopen(filename, "r");
755: if (!f)
756: return;
757:
758: while (fgets(buf, sizeof(buf), f)) {
1.142 deraadt 759: if (++lineno > 1000)
760: fatal("Too many lines in environment file %s", filename);
1.1 markus 761: for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
762: ;
763: if (!*cp || *cp == '#' || *cp == '\n')
764: continue;
1.224 gilles 765:
766: cp[strcspn(cp, "\n")] = '\0';
767:
1.1 markus 768: value = strchr(cp, '=');
769: if (value == NULL) {
1.142 deraadt 770: fprintf(stderr, "Bad line %u in %.100s\n", lineno,
771: filename);
1.1 markus 772: continue;
773: }
1.14 deraadt 774: /*
775: * Replace the equals sign by nul, and advance value to
776: * the value string.
777: */
1.1 markus 778: *value = '\0';
779: value++;
780: child_set_env(env, envsize, cp, value);
781: }
782: fclose(f);
783: }
784:
1.127 markus 785: static char **
786: do_setup_env(Session *s, const char *shell)
1.1 markus 787: {
1.281 djm 788: struct ssh *ssh = active_state; /* XXX */
1.1 markus 789: char buf[256];
1.127 markus 790: u_int i, envsize;
1.154 markus 791: char **env, *laddr;
1.127 markus 792: struct passwd *pw = s->pw;
1.1 markus 793:
794: /* Initialize the environment. */
795: envsize = 100;
1.220 djm 796: env = xcalloc(envsize, sizeof(char *));
1.1 markus 797: env[0] = NULL;
798:
1.161 markus 799: #ifdef GSSAPI
1.168 djm 800: /* Allow any GSSAPI methods that we've used to alter
1.161 markus 801: * the childs environment as they see fit
802: */
803: ssh_gssapi_do_child(&env, &envsize);
804: #endif
805:
1.284 djm 806: /* Set basic environment. */
807: for (i = 0; i < s->num_env; i++)
808: child_set_env(&env, &envsize, s->env[i].name, s->env[i].val);
809:
810: child_set_env(&env, &envsize, "USER", pw->pw_name);
811: child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
812: child_set_env(&env, &envsize, "HOME", pw->pw_dir);
813: if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
814: child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
815: else
816: child_set_env(&env, &envsize, "PATH", getenv("PATH"));
817:
818: snprintf(buf, sizeof buf, "%.200s/%.50s", _PATH_MAILDIR, pw->pw_name);
819: child_set_env(&env, &envsize, "MAIL", buf);
820:
821: /* Normal systems set SHELL by default. */
822: child_set_env(&env, &envsize, "SHELL", shell);
1.1 markus 823:
824: if (getenv("TZ"))
825: child_set_env(&env, &envsize, "TZ", getenv("TZ"));
826:
827: /* Set custom environment options from RSA authentication. */
1.284 djm 828: while (custom_environment) {
829: struct envstring *ce = custom_environment;
830: char *str = ce->s;
831:
832: for (i = 0; str[i] != '=' && str[i]; i++)
833: ;
834: if (str[i] == '=') {
835: str[i] = 0;
836: child_set_env(&env, &envsize, str, str + i + 1);
837: }
838: custom_environment = ce->next;
839: free(ce->s);
840: free(ce);
1.1 markus 841: }
842:
1.149 stevesk 843: /* SSH_CLIENT deprecated */
1.1 markus 844: snprintf(buf, sizeof buf, "%.50s %d %d",
1.281 djm 845: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
846: ssh_local_port(ssh));
1.1 markus 847: child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1.149 stevesk 848:
1.154 markus 849: laddr = get_local_ipaddr(packet_get_connection_in());
1.149 stevesk 850: snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1.281 djm 851: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
852: laddr, ssh_local_port(ssh));
1.265 djm 853: free(laddr);
1.149 stevesk 854: child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1.1 markus 855:
1.60 markus 856: if (s->ttyfd != -1)
857: child_set_env(&env, &envsize, "SSH_TTY", s->tty);
858: if (s->term)
859: child_set_env(&env, &envsize, "TERM", s->term);
860: if (s->display)
861: child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34 markus 862: if (original_command)
863: child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
864: original_command);
1.96 dugsong 865: #ifdef KRB5
866: if (s->authctxt->krb5_ticket_file)
867: child_set_env(&env, &envsize, "KRB5CCNAME",
1.112 deraadt 868: s->authctxt->krb5_ticket_file);
1.96 dugsong 869: #endif
1.136 markus 870: if (auth_sock_name != NULL)
1.1 markus 871: child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1.136 markus 872: auth_sock_name);
1.1 markus 873:
874: /* read $HOME/.ssh/environment. */
1.284 djm 875: if (options.permit_user_env) {
1.14 deraadt 876: snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
877: pw->pw_dir);
1.1 markus 878: read_environment_file(&env, &envsize, buf);
879: }
880: if (debug_flag) {
881: /* dump the environment */
882: fprintf(stderr, "Environment:\n");
883: for (i = 0; env[i]; i++)
884: fprintf(stderr, " %.200s\n", env[i]);
885: }
1.127 markus 886: return env;
887: }
888:
889: /*
890: * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
891: * first in this order).
892: */
893: static void
894: do_rc_files(Session *s, const char *shell)
895: {
896: FILE *f = NULL;
897: char cmd[1024];
898: int do_xauth;
899: struct stat st;
900:
901: do_xauth =
902: s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
903:
1.231 djm 904: /* ignore _PATH_SSH_USER_RC for subsystems and admin forced commands */
1.232 djm 905: if (!s->is_subsystem && options.adm_forced_command == NULL &&
1.273 djm 906: !no_user_rc && options.permit_user_rc &&
907: stat(_PATH_SSH_USER_RC, &st) >= 0) {
1.127 markus 908: snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
909: shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
910: if (debug_flag)
911: fprintf(stderr, "Running %s\n", cmd);
912: f = popen(cmd, "w");
913: if (f) {
914: if (do_xauth)
915: fprintf(f, "%s %s\n", s->auth_proto,
916: s->auth_data);
917: pclose(f);
918: } else
919: fprintf(stderr, "Could not run %s\n",
920: _PATH_SSH_USER_RC);
921: } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
922: if (debug_flag)
923: fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
924: _PATH_SSH_SYSTEM_RC);
925: f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
926: if (f) {
927: if (do_xauth)
928: fprintf(f, "%s %s\n", s->auth_proto,
929: s->auth_data);
930: pclose(f);
931: } else
932: fprintf(stderr, "Could not run %s\n",
933: _PATH_SSH_SYSTEM_RC);
934: } else if (do_xauth && options.xauth_location != NULL) {
935: /* Add authority data to .Xauthority if appropriate. */
936: if (debug_flag) {
937: fprintf(stderr,
1.151 stevesk 938: "Running %.500s remove %.100s\n",
1.165 markus 939: options.xauth_location, s->auth_display);
1.151 stevesk 940: fprintf(stderr,
941: "%.500s add %.100s %.100s %.100s\n",
1.127 markus 942: options.xauth_location, s->auth_display,
943: s->auth_proto, s->auth_data);
944: }
945: snprintf(cmd, sizeof cmd, "%s -q -",
946: options.xauth_location);
947: f = popen(cmd, "w");
948: if (f) {
1.151 stevesk 949: fprintf(f, "remove %s\n",
950: s->auth_display);
1.127 markus 951: fprintf(f, "add %s %s %s\n",
952: s->auth_display, s->auth_proto,
953: s->auth_data);
954: pclose(f);
955: } else {
956: fprintf(stderr, "Could not run %s\n",
957: cmd);
958: }
959: }
960: }
961:
962: static void
963: do_nologin(struct passwd *pw)
964: {
965: FILE *f = NULL;
1.251 dtucker 966: char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
967: struct stat sb;
1.127 markus 968:
1.260 guenther 969: if (login_getcapbool(lc, "ignorenologin", 0) || pw->pw_uid == 0)
1.251 dtucker 970: return;
971: nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
972:
973: if (stat(nl, &sb) == -1) {
974: if (nl != def_nl)
1.265 djm 975: free(nl);
1.251 dtucker 976: return;
977: }
978:
979: /* /etc/nologin exists. Print its contents if we can and exit. */
980: logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
981: if ((f = fopen(nl, "r")) != NULL) {
1.127 markus 982: while (fgets(buf, sizeof(buf), f))
983: fputs(buf, stderr);
984: fclose(f);
985: }
1.251 dtucker 986: exit(254);
1.127 markus 987: }
988:
1.226 djm 989: /*
990: * Chroot into a directory after checking it for safety: all path components
991: * must be root-owned directories with strict permissions.
992: */
993: static void
994: safely_chroot(const char *path, uid_t uid)
995: {
996: const char *cp;
1.277 deraadt 997: char component[PATH_MAX];
1.226 djm 998: struct stat st;
999:
1000: if (*path != '/')
1001: fatal("chroot path does not begin at root");
1002: if (strlen(path) >= sizeof(component))
1003: fatal("chroot path too long");
1004:
1005: /*
1006: * Descend the path, checking that each component is a
1007: * root-owned directory with strict permissions.
1008: */
1009: for (cp = path; cp != NULL;) {
1010: if ((cp = strchr(cp, '/')) == NULL)
1011: strlcpy(component, path, sizeof(component));
1012: else {
1013: cp++;
1014: memcpy(component, path, cp - path);
1015: component[cp - path] = '\0';
1016: }
1017:
1018: debug3("%s: checking '%s'", __func__, component);
1019:
1020: if (stat(component, &st) != 0)
1021: fatal("%s: stat(\"%s\"): %s", __func__,
1022: component, strerror(errno));
1023: if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1024: fatal("bad ownership or modes for chroot "
1025: "directory %s\"%s\"",
1026: cp == NULL ? "" : "component ", component);
1027: if (!S_ISDIR(st.st_mode))
1028: fatal("chroot path %s\"%s\" is not a directory",
1029: cp == NULL ? "" : "component ", component);
1030:
1031: }
1032:
1033: if (chdir(path) == -1)
1034: fatal("Unable to chdir to chroot path \"%s\": "
1035: "%s", path, strerror(errno));
1036: if (chroot(path) == -1)
1037: fatal("chroot(\"%s\"): %s", path, strerror(errno));
1038: if (chdir("/") == -1)
1039: fatal("%s: chdir(/) after chroot: %s",
1040: __func__, strerror(errno));
1041: verbose("Changed root directory to \"%s\"", path);
1042: }
1043:
1.127 markus 1044: /* Set login name, uid, gid, and groups. */
1.130 provos 1045: void
1.127 markus 1046: do_setusercontext(struct passwd *pw)
1047: {
1.227 djm 1048: char *chroot_path, *tmp;
1049:
1.127 markus 1050: if (getuid() == 0 || geteuid() == 0) {
1.226 djm 1051: /* Prepare groups */
1.127 markus 1052: if (setusercontext(lc, pw, pw->pw_uid,
1.226 djm 1053: (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
1.127 markus 1054: perror("unable to set user context");
1055: exit(1);
1056: }
1.226 djm 1057:
1.279 djm 1058: if (!in_chroot && options.chroot_directory != NULL &&
1.226 djm 1059: strcasecmp(options.chroot_directory, "none") != 0) {
1.227 djm 1060: tmp = tilde_expand_filename(options.chroot_directory,
1061: pw->pw_uid);
1062: chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1063: "u", pw->pw_name, (char *)NULL);
1.226 djm 1064: safely_chroot(chroot_path, pw->pw_uid);
1.227 djm 1065: free(tmp);
1.226 djm 1066: free(chroot_path);
1.264 djm 1067: /* Make sure we don't attempt to chroot again */
1068: free(options.chroot_directory);
1069: options.chroot_directory = NULL;
1.279 djm 1070: in_chroot = 1;
1.226 djm 1071: }
1072:
1073: /* Set UID */
1074: if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1075: perror("unable to set user context (setuser)");
1076: exit(1);
1077: }
1.264 djm 1078: } else if (options.chroot_directory != NULL &&
1079: strcasecmp(options.chroot_directory, "none") != 0) {
1080: fatal("server lacks privileges to chroot to ChrootDirectory");
1.263 dtucker 1081: }
1.264 djm 1082:
1.127 markus 1083: if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1084: fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1085: }
1086:
1.131 markus 1087: static void
1.172 markus 1088: do_pwchange(Session *s)
1089: {
1.179 dtucker 1090: fflush(NULL);
1.172 markus 1091: fprintf(stderr, "WARNING: Your password has expired.\n");
1092: if (s->ttyfd != -1) {
1.178 deraadt 1093: fprintf(stderr,
1.172 markus 1094: "You must change your password now and login again!\n");
1095: execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
1096: perror("passwd");
1097: } else {
1098: fprintf(stderr,
1099: "Password change required but no TTY available.\n");
1100: }
1101: exit(1);
1102: }
1103:
1104: static void
1105: child_close_fds(void)
1106: {
1.276 djm 1107: extern int auth_sock;
1.266 markus 1108:
1.276 djm 1109: if (auth_sock != -1) {
1110: close(auth_sock);
1111: auth_sock = -1;
1.266 markus 1112: }
1113:
1.172 markus 1114: if (packet_get_connection_in() == packet_get_connection_out())
1115: close(packet_get_connection_in());
1116: else {
1117: close(packet_get_connection_in());
1118: close(packet_get_connection_out());
1119: }
1120: /*
1121: * Close all descriptors related to channels. They will still remain
1122: * open in the parent.
1123: */
1124: /* XXX better use close-on-exec? -markus */
1125: channel_close_all();
1126:
1127: /*
1128: * Close any extra file descriptors. Note that there may still be
1129: * descriptors left by system functions. They will be closed later.
1130: */
1131: endpwent();
1132:
1133: /*
1.188 djm 1134: * Close any extra open file descriptors so that we don't have them
1.172 markus 1135: * hanging around in clients. Note that we want to do this after
1136: * initgroups, because at least on Solaris 2.3 it leaves file
1137: * descriptors open.
1138: */
1.258 djm 1139: closefrom(STDERR_FILENO + 1);
1.172 markus 1140: }
1141:
1.127 markus 1142: /*
1143: * Performs common processing for the child, such as setting up the
1144: * environment, closing extra file descriptors, setting the user and group
1145: * ids, and executing the command or shell.
1146: */
1.225 markus 1147: #define ARGV_MAX 10
1.127 markus 1148: void
1149: do_child(Session *s, const char *command)
1150: {
1151: extern char **environ;
1152: char **env;
1.225 markus 1153: char *argv[ARGV_MAX];
1.284 djm 1154: const char *shell, *shell0;
1.127 markus 1155: struct passwd *pw = s->pw;
1.239 djm 1156: int r = 0;
1.127 markus 1157:
1158: /* remove hostkey from the child's memory */
1159: destroy_sensitive_data();
1.287 markus 1160: packet_clear_keys();
1.127 markus 1161:
1.172 markus 1162: /* Force a password change */
1163: if (s->authctxt->force_pwchange) {
1164: do_setusercontext(pw);
1165: child_close_fds();
1166: do_pwchange(s);
1167: exit(1);
1168: }
1169:
1.127 markus 1170: /*
1171: * Login(1) does this as well, and it needs uid 0 for the "-h"
1172: * switch, so we let login(1) to this for us.
1173: */
1.284 djm 1174: do_nologin(pw);
1175: do_setusercontext(pw);
1.127 markus 1176:
1177: /*
1178: * Get the shell from the password data. An empty shell field is
1179: * legal, and means /bin/sh.
1180: */
1181: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.152 markus 1182:
1183: /*
1184: * Make sure $SHELL points to the shell from the password file,
1185: * even if shell is overridden from login.conf
1186: */
1187: env = do_setup_env(s, shell);
1188:
1.127 markus 1189: shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1190:
1.1 markus 1191: /*
1192: * Close the connection descriptors; note that this is the child, and
1193: * the server will still have the socket open, and it is important
1194: * that we do not shutdown it. Note that the descriptors cannot be
1195: * closed before building the environment, as we call
1.281 djm 1196: * ssh_remote_ipaddr there.
1.1 markus 1197: */
1.172 markus 1198: child_close_fds();
1.1 markus 1199:
1200: /*
1.125 deraadt 1201: * Must take new environment into use so that .ssh/rc,
1202: * /etc/ssh/sshrc and xauth are run in the proper environment.
1.1 markus 1203: */
1204: environ = env;
1.170 jakob 1205:
1206: #ifdef KRB5
1207: /*
1208: * At this point, we check to see if AFS is active and if we have
1209: * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1210: * if we can (and need to) extend the ticket into an AFS token. If
1211: * we don't do this, we run into potential problems if the user's
1212: * home directory is in AFS and it's not world-readable.
1213: */
1214:
1215: if (options.kerberos_get_afs_token && k_hasafs() &&
1.185 djm 1216: (s->authctxt->krb5_ctx != NULL)) {
1.170 jakob 1217: char cell[64];
1218:
1219: debug("Getting AFS token");
1220:
1221: k_setpag();
1222:
1223: if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1224: krb5_afslog(s->authctxt->krb5_ctx,
1225: s->authctxt->krb5_fwd_ccache, cell, NULL);
1226:
1227: krb5_afslog_home(s->authctxt->krb5_ctx,
1228: s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1229: }
1230: #endif
1.104 markus 1231:
1.188 djm 1232: /* Change current directory to the user's home directory. */
1.104 markus 1233: if (chdir(pw->pw_dir) < 0) {
1.239 djm 1234: /* Suppress missing homedir warning for chroot case */
1235: r = login_getcapbool(lc, "requirehome", 0);
1.279 djm 1236: if (r || !in_chroot) {
1.239 djm 1237: fprintf(stderr, "Could not chdir to home "
1238: "directory %s: %s\n", pw->pw_dir,
1239: strerror(errno));
1.279 djm 1240: }
1.239 djm 1241: if (r)
1.104 markus 1242: exit(1);
1243: }
1.230 djm 1244:
1245: closefrom(STDERR_FILENO + 1);
1.1 markus 1246:
1.284 djm 1247: do_rc_files(s, shell);
1.67 markus 1248:
1249: /* restore SIGPIPE for child */
1.217 stevesk 1250: signal(SIGPIPE, SIG_DFL);
1.67 markus 1251:
1.248 djm 1252: if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1253: printf("This service allows sftp connections only.\n");
1254: fflush(NULL);
1255: exit(1);
1256: } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
1.225 markus 1257: extern int optind, optreset;
1258: int i;
1259: char *p, *args;
1260:
1.246 stevesk 1261: setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
1.243 millert 1262: args = xstrdup(command ? command : "sftp-server");
1.225 markus 1263: for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1264: if (i < ARGV_MAX - 1)
1265: argv[i++] = p;
1266: argv[i] = NULL;
1267: optind = optreset = 1;
1268: __progname = argv[0];
1.226 djm 1269: exit(sftp_server_main(i, argv, s->pw));
1.225 markus 1270: }
1.247 djm 1271:
1272: fflush(NULL);
1.225 markus 1273:
1.127 markus 1274: /* Get the last component of the shell name. */
1275: if ((shell0 = strrchr(shell, '/')) != NULL)
1276: shell0++;
1277: else
1278: shell0 = shell;
1279:
1.1 markus 1280: /*
1281: * If we have no command, execute the shell. In this case, the shell
1282: * name to be passed in argv[0] is preceded by '-' to indicate that
1283: * this is a login shell.
1284: */
1285: if (!command) {
1.127 markus 1286: char argv0[256];
1.1 markus 1287:
1.127 markus 1288: /* Start the shell. Set initial character to '-'. */
1289: argv0[0] = '-';
1.1 markus 1290:
1.127 markus 1291: if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1.128 markus 1292: >= sizeof(argv0) - 1) {
1.127 markus 1293: errno = EINVAL;
1.1 markus 1294: perror(shell);
1295: exit(1);
1.127 markus 1296: }
1.1 markus 1297:
1.127 markus 1298: /* Execute the shell. */
1299: argv[0] = argv0;
1300: argv[1] = NULL;
1301: execve(shell, argv, env);
1302:
1303: /* Executing the shell failed. */
1304: perror(shell);
1305: exit(1);
1.1 markus 1306: }
1307: /*
1308: * Execute the command using the user's shell. This uses the -c
1309: * option to execute the command.
1310: */
1.127 markus 1311: argv[0] = (char *) shell0;
1.1 markus 1312: argv[1] = "-c";
1313: argv[2] = (char *) command;
1314: argv[3] = NULL;
1315: execve(shell, argv, env);
1316: perror(shell);
1317: exit(1);
1318: }
1319:
1.237 djm 1320: void
1321: session_unused(int id)
1322: {
1323: debug3("%s: session id %d unused", __func__, id);
1324: if (id >= options.max_sessions ||
1325: id >= sessions_nalloc) {
1326: fatal("%s: insane session id %d (max %d nalloc %d)",
1327: __func__, id, options.max_sessions, sessions_nalloc);
1328: }
1.270 tedu 1329: memset(&sessions[id], 0, sizeof(*sessions));
1.237 djm 1330: sessions[id].self = id;
1331: sessions[id].used = 0;
1332: sessions[id].chanid = -1;
1333: sessions[id].ptyfd = -1;
1334: sessions[id].ttyfd = -1;
1335: sessions[id].ptymaster = -1;
1336: sessions[id].x11_chanids = NULL;
1337: sessions[id].next_unused = sessions_first_unused;
1338: sessions_first_unused = id;
1339: }
1340:
1.1 markus 1341: Session *
1342: session_new(void)
1343: {
1.237 djm 1344: Session *s, *tmp;
1345:
1346: if (sessions_first_unused == -1) {
1347: if (sessions_nalloc >= options.max_sessions)
1348: return NULL;
1349: debug2("%s: allocate (allocated %d max %d)",
1350: __func__, sessions_nalloc, options.max_sessions);
1.288 ! deraadt 1351: tmp = xrecallocarray(sessions, sessions_nalloc,
! 1352: sessions_nalloc + 1, sizeof(*sessions));
1.237 djm 1353: if (tmp == NULL) {
1354: error("%s: cannot allocate %d sessions",
1355: __func__, sessions_nalloc + 1);
1356: return NULL;
1357: }
1358: sessions = tmp;
1359: session_unused(sessions_nalloc++);
1360: }
1361:
1362: if (sessions_first_unused >= sessions_nalloc ||
1363: sessions_first_unused < 0) {
1364: fatal("%s: insane first_unused %d max %d nalloc %d",
1365: __func__, sessions_first_unused, options.max_sessions,
1366: sessions_nalloc);
1367: }
1368:
1369: s = &sessions[sessions_first_unused];
1370: if (s->used) {
1371: fatal("%s: session %d already used",
1372: __func__, sessions_first_unused);
1373: }
1374: sessions_first_unused = s->next_unused;
1375: s->used = 1;
1376: s->next_unused = -1;
1377: debug("session_new: session %d", s->self);
1378:
1379: return s;
1.1 markus 1380: }
1381:
1.94 itojun 1382: static void
1.1 markus 1383: session_dump(void)
1384: {
1385: int i;
1.237 djm 1386: for (i = 0; i < sessions_nalloc; i++) {
1.1 markus 1387: Session *s = &sessions[i];
1.237 djm 1388:
1389: debug("dump: used %d next_unused %d session %d %p "
1390: "channel %d pid %ld",
1.1 markus 1391: s->used,
1.237 djm 1392: s->next_unused,
1.1 markus 1393: s->self,
1394: s,
1395: s->chanid,
1.137 mpech 1396: (long)s->pid);
1.1 markus 1397: }
1398: }
1399:
1.2 markus 1400: int
1.97 markus 1401: session_open(Authctxt *authctxt, int chanid)
1.2 markus 1402: {
1403: Session *s = session_new();
1404: debug("session_open: channel %d", chanid);
1405: if (s == NULL) {
1406: error("no more sessions");
1407: return 0;
1408: }
1.97 markus 1409: s->authctxt = authctxt;
1410: s->pw = authctxt->pw;
1.167 djm 1411: if (s->pw == NULL || !authctxt->valid)
1.54 stevesk 1412: fatal("no user for session %d", s->self);
1.2 markus 1413: debug("session_open: session %d: link with channel %d", s->self, chanid);
1414: s->chanid = chanid;
1415: return 1;
1416: }
1417:
1.130 provos 1418: Session *
1419: session_by_tty(char *tty)
1420: {
1421: int i;
1.237 djm 1422: for (i = 0; i < sessions_nalloc; i++) {
1.130 provos 1423: Session *s = &sessions[i];
1424: if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1425: debug("session_by_tty: session %d tty %s", i, tty);
1426: return s;
1427: }
1428: }
1429: debug("session_by_tty: unknown tty %.100s", tty);
1430: session_dump();
1431: return NULL;
1432: }
1433:
1.94 itojun 1434: static Session *
1.2 markus 1435: session_by_channel(int id)
1436: {
1437: int i;
1.237 djm 1438: for (i = 0; i < sessions_nalloc; i++) {
1.2 markus 1439: Session *s = &sessions[i];
1440: if (s->used && s->chanid == id) {
1.237 djm 1441: debug("session_by_channel: session %d channel %d",
1442: i, id);
1.2 markus 1443: return s;
1444: }
1445: }
1446: debug("session_by_channel: unknown channel %d", id);
1447: session_dump();
1448: return NULL;
1449: }
1450:
1.94 itojun 1451: static Session *
1.184 djm 1452: session_by_x11_channel(int id)
1453: {
1454: int i, j;
1455:
1.237 djm 1456: for (i = 0; i < sessions_nalloc; i++) {
1.184 djm 1457: Session *s = &sessions[i];
1458:
1459: if (s->x11_chanids == NULL || !s->used)
1460: continue;
1461: for (j = 0; s->x11_chanids[j] != -1; j++) {
1462: if (s->x11_chanids[j] == id) {
1463: debug("session_by_x11_channel: session %d "
1464: "channel %d", s->self, id);
1465: return s;
1466: }
1467: }
1468: }
1469: debug("session_by_x11_channel: unknown channel %d", id);
1470: session_dump();
1471: return NULL;
1472: }
1473:
1474: static Session *
1.2 markus 1475: session_by_pid(pid_t pid)
1476: {
1477: int i;
1.137 mpech 1478: debug("session_by_pid: pid %ld", (long)pid);
1.237 djm 1479: for (i = 0; i < sessions_nalloc; i++) {
1.2 markus 1480: Session *s = &sessions[i];
1481: if (s->used && s->pid == pid)
1482: return s;
1483: }
1.137 mpech 1484: error("session_by_pid: unknown pid %ld", (long)pid);
1.2 markus 1485: session_dump();
1486: return NULL;
1487: }
1488:
1.94 itojun 1489: static int
1.2 markus 1490: session_window_change_req(Session *s)
1491: {
1492: s->col = packet_get_int();
1493: s->row = packet_get_int();
1494: s->xpixel = packet_get_int();
1495: s->ypixel = packet_get_int();
1.116 markus 1496: packet_check_eom();
1.2 markus 1497: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1498: return 1;
1499: }
1500:
1.94 itojun 1501: static int
1.2 markus 1502: session_pty_req(Session *s)
1503: {
1.45 markus 1504: u_int len;
1.72 stevesk 1505: int n_bytes;
1.132 markus 1506:
1.268 djm 1507: if (no_pty_flag || !options.permit_tty) {
1.86 markus 1508: debug("Allocating a pty not permitted for this authentication.");
1.19 markus 1509: return 0;
1.86 markus 1510: }
1511: if (s->ttyfd != -1) {
1512: packet_disconnect("Protocol error: you already have a pty.");
1.3 markus 1513: return 0;
1.86 markus 1514: }
1515:
1.2 markus 1516: s->term = packet_get_string(&len);
1.283 markus 1517: s->col = packet_get_int();
1518: s->row = packet_get_int();
1.2 markus 1519: s->xpixel = packet_get_int();
1520: s->ypixel = packet_get_int();
1521:
1522: if (strcmp(s->term, "") == 0) {
1.265 djm 1523: free(s->term);
1.2 markus 1524: s->term = NULL;
1525: }
1.86 markus 1526:
1.2 markus 1527: /* Allocate a pty and open it. */
1.86 markus 1528: debug("Allocating pty.");
1.237 djm 1529: if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1530: sizeof(s->tty)))) {
1.265 djm 1531: free(s->term);
1.2 markus 1532: s->term = NULL;
1533: s->ptyfd = -1;
1534: s->ttyfd = -1;
1535: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1536: return 0;
1.2 markus 1537: }
1538: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1.86 markus 1539:
1.283 markus 1540: n_bytes = packet_remaining();
1.86 markus 1541: tty_parse_modes(s->ttyfd, &n_bytes);
1542:
1.130 provos 1543: if (!use_privsep)
1544: pty_setowner(s->pw, s->tty);
1.86 markus 1545:
1546: /* Set window size from the packet. */
1.2 markus 1547: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1548:
1.116 markus 1549: packet_check_eom();
1.9 markus 1550: session_proctitle(s);
1.2 markus 1551: return 1;
1552: }
1553:
1.94 itojun 1554: static int
1.7 markus 1555: session_subsystem_req(Session *s)
1556: {
1.105 markus 1557: struct stat st;
1.45 markus 1558: u_int len;
1.7 markus 1559: int success = 0;
1.267 djm 1560: char *prog, *cmd;
1.182 djm 1561: u_int i;
1.7 markus 1562:
1.267 djm 1563: s->subsys = packet_get_string(&len);
1.116 markus 1564: packet_check_eom();
1.267 djm 1565: debug2("subsystem request for %.100s by user %s", s->subsys,
1.255 djm 1566: s->pw->pw_name);
1.18 jakob 1567:
1568: for (i = 0; i < options.num_subsystems; i++) {
1.267 djm 1569: if (strcmp(s->subsys, options.subsystem_name[i]) == 0) {
1.205 djm 1570: prog = options.subsystem_command[i];
1571: cmd = options.subsystem_args[i];
1.249 dtucker 1572: if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
1.225 markus 1573: s->is_subsystem = SUBSYSTEM_INT_SFTP;
1.249 dtucker 1574: debug("subsystem: %s", prog);
1.225 markus 1575: } else {
1.249 dtucker 1576: if (stat(prog, &st) < 0)
1577: debug("subsystem: cannot stat %s: %s",
1578: prog, strerror(errno));
1.225 markus 1579: s->is_subsystem = SUBSYSTEM_EXT;
1.249 dtucker 1580: debug("subsystem: exec() %s", cmd);
1.105 markus 1581: }
1.237 djm 1582: success = do_exec(s, cmd) == 0;
1.122 markus 1583: break;
1.18 jakob 1584: }
1585: }
1586:
1587: if (!success)
1.267 djm 1588: logit("subsystem request for %.100s by user %s failed, "
1589: "subsystem not found", s->subsys, s->pw->pw_name);
1.7 markus 1590:
1591: return success;
1592: }
1593:
1.94 itojun 1594: static int
1.7 markus 1595: session_x11_req(Session *s)
1596: {
1.81 markus 1597: int success;
1.7 markus 1598:
1.184 djm 1599: if (s->auth_proto != NULL || s->auth_data != NULL) {
1600: error("session_x11_req: session %d: "
1.190 stevesk 1601: "x11 forwarding already active", s->self);
1.184 djm 1602: return 0;
1603: }
1.7 markus 1604: s->single_connection = packet_get_char();
1605: s->auth_proto = packet_get_string(NULL);
1606: s->auth_data = packet_get_string(NULL);
1607: s->screen = packet_get_int();
1.116 markus 1608: packet_check_eom();
1.7 markus 1609:
1.282 djm 1610: if (xauth_valid_string(s->auth_proto) &&
1611: xauth_valid_string(s->auth_data))
1612: success = session_setup_x11fwd(s);
1613: else {
1614: success = 0;
1615: error("Invalid X11 forwarding data");
1616: }
1.81 markus 1617: if (!success) {
1.265 djm 1618: free(s->auth_proto);
1619: free(s->auth_data);
1.84 markus 1620: s->auth_proto = NULL;
1621: s->auth_data = NULL;
1.7 markus 1622: }
1.81 markus 1623: return success;
1.7 markus 1624: }
1625:
1.94 itojun 1626: static int
1.19 markus 1627: session_shell_req(Session *s)
1628: {
1.116 markus 1629: packet_check_eom();
1.237 djm 1630: return do_exec(s, NULL) == 0;
1.19 markus 1631: }
1632:
1.94 itojun 1633: static int
1.19 markus 1634: session_exec_req(Session *s)
1635: {
1.237 djm 1636: u_int len, success;
1637:
1.19 markus 1638: char *command = packet_get_string(&len);
1.116 markus 1639: packet_check_eom();
1.237 djm 1640: success = do_exec(s, command) == 0;
1.265 djm 1641: free(command);
1.237 djm 1642: return success;
1.19 markus 1643: }
1644:
1.94 itojun 1645: static int
1.157 markus 1646: session_break_req(Session *s)
1647: {
1648:
1.175 deraadt 1649: packet_get_int(); /* ignored */
1.157 markus 1650: packet_check_eom();
1651:
1.259 djm 1652: if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
1.157 markus 1653: return 0;
1654: return 1;
1655: }
1656:
1657: static int
1.173 djm 1658: session_env_req(Session *s)
1659: {
1660: char *name, *val;
1661: u_int name_len, val_len, i;
1662:
1.271 djm 1663: name = packet_get_cstring(&name_len);
1664: val = packet_get_cstring(&val_len);
1.173 djm 1665: packet_check_eom();
1666:
1667: /* Don't set too many environment variables */
1668: if (s->num_env > 128) {
1669: debug2("Ignoring env request %s: too many env vars", name);
1670: goto fail;
1671: }
1672:
1673: for (i = 0; i < options.num_accept_env; i++) {
1674: if (match_pattern(name, options.accept_env[i])) {
1675: debug2("Setting env %d: %s=%s", s->num_env, name, val);
1.288 ! deraadt 1676: s->env = xrecallocarray(s->env, s->num_env,
! 1677: s->num_env + 1, sizeof(*s->env));
1.173 djm 1678: s->env[s->num_env].name = name;
1679: s->env[s->num_env].val = val;
1680: s->num_env++;
1681: return (1);
1682: }
1683: }
1684: debug2("Ignoring env request %s: disallowed name", name);
1685:
1686: fail:
1.265 djm 1687: free(name);
1688: free(val);
1.173 djm 1689: return (0);
1690: }
1691:
1692: static int
1.43 markus 1693: session_auth_agent_req(Session *s)
1694: {
1695: static int called = 0;
1.116 markus 1696: packet_check_eom();
1.235 pyr 1697: if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
1.44 markus 1698: debug("session_auth_agent_req: no_agent_forwarding_flag");
1699: return 0;
1700: }
1.43 markus 1701: if (called) {
1702: return 0;
1703: } else {
1704: called = 1;
1705: return auth_input_request_forwarding(s->pw);
1706: }
1707: }
1708:
1.123 markus 1709: int
1710: session_input_channel_req(Channel *c, const char *rtype)
1.2 markus 1711: {
1712: int success = 0;
1713: Session *s;
1714:
1.123 markus 1715: if ((s = session_by_channel(c->self)) == NULL) {
1.155 itojun 1716: logit("session_input_channel_req: no session %d req %.100s",
1.123 markus 1717: c->self, rtype);
1718: return 0;
1719: }
1720: debug("session_input_channel_req: session %d req %s", s->self, rtype);
1.2 markus 1721:
1722: /*
1.64 markus 1723: * a session is in LARVAL state until a shell, a command
1724: * or a subsystem is executed
1.2 markus 1725: */
1726: if (c->type == SSH_CHANNEL_LARVAL) {
1727: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1728: success = session_shell_req(s);
1.2 markus 1729: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1730: success = session_exec_req(s);
1.2 markus 1731: } else if (strcmp(rtype, "pty-req") == 0) {
1.221 stevesk 1732: success = session_pty_req(s);
1.7 markus 1733: } else if (strcmp(rtype, "x11-req") == 0) {
1734: success = session_x11_req(s);
1.43 markus 1735: } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1736: success = session_auth_agent_req(s);
1.7 markus 1737: } else if (strcmp(rtype, "subsystem") == 0) {
1738: success = session_subsystem_req(s);
1.173 djm 1739: } else if (strcmp(rtype, "env") == 0) {
1740: success = session_env_req(s);
1.2 markus 1741: }
1742: }
1743: if (strcmp(rtype, "window-change") == 0) {
1744: success = session_window_change_req(s);
1.177 djm 1745: } else if (strcmp(rtype, "break") == 0) {
1746: success = session_break_req(s);
1.2 markus 1747: }
1.177 djm 1748:
1.123 markus 1749: return success;
1.2 markus 1750: }
1751:
1752: void
1.256 djm 1753: session_set_fds(Session *s, int fdin, int fdout, int fderr, int ignore_fderr,
1754: int is_tty)
1.2 markus 1755: {
1756: /*
1757: * now that have a child and a pipe to the child,
1758: * we can activate our channel and register the fd's
1759: */
1760: if (s->chanid == -1)
1761: fatal("no channel for session %d", s->self);
1762: channel_set_fds(s->chanid,
1763: fdout, fdin, fderr,
1.256 djm 1764: ignore_fderr ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1.241 dtucker 1765: 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
1.2 markus 1766: }
1767:
1.85 markus 1768: /*
1769: * Function to perform pty cleanup. Also called if we get aborted abnormally
1770: * (e.g., due to a dropped connection).
1771: */
1.130 provos 1772: void
1.165 markus 1773: session_pty_cleanup2(Session *s)
1.1 markus 1774: {
1.85 markus 1775: if (s == NULL) {
1776: error("session_pty_cleanup: no session");
1777: return;
1778: }
1779: if (s->ttyfd == -1)
1.1 markus 1780: return;
1781:
1.54 stevesk 1782: debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1 markus 1783:
1784: /* Record that the user has logged out. */
1.85 markus 1785: if (s->pid != 0)
1786: record_logout(s->pid, s->tty);
1.1 markus 1787:
1788: /* Release the pseudo-tty. */
1.130 provos 1789: if (getuid() == 0)
1790: pty_release(s->tty);
1.1 markus 1791:
1792: /*
1793: * Close the server side of the socket pairs. We must do this after
1794: * the pty cleanup, so that another process doesn't get this pty
1795: * while we're still cleaning up.
1796: */
1.237 djm 1797: if (s->ptymaster != -1 && close(s->ptymaster) < 0)
1798: error("close(s->ptymaster/%d): %s",
1799: s->ptymaster, strerror(errno));
1.108 markus 1800:
1801: /* unlink pty from session */
1802: s->ttyfd = -1;
1.2 markus 1803: }
1804:
1.130 provos 1805: void
1.165 markus 1806: session_pty_cleanup(Session *s)
1.130 provos 1807: {
1.165 markus 1808: PRIVSEP(session_pty_cleanup2(s));
1.130 provos 1809: }
1810:
1.147 markus 1811: static char *
1812: sig2name(int sig)
1813: {
1814: #define SSH_SIG(x) if (sig == SIG ## x) return #x
1815: SSH_SIG(ABRT);
1816: SSH_SIG(ALRM);
1817: SSH_SIG(FPE);
1818: SSH_SIG(HUP);
1819: SSH_SIG(ILL);
1820: SSH_SIG(INT);
1821: SSH_SIG(KILL);
1822: SSH_SIG(PIPE);
1823: SSH_SIG(QUIT);
1824: SSH_SIG(SEGV);
1825: SSH_SIG(TERM);
1826: SSH_SIG(USR1);
1827: SSH_SIG(USR2);
1828: #undef SSH_SIG
1829: return "SIG@openssh.com";
1830: }
1831:
1.94 itojun 1832: static void
1.184 djm 1833: session_close_x11(int id)
1834: {
1835: Channel *c;
1836:
1.189 markus 1837: if ((c = channel_by_id(id)) == NULL) {
1.184 djm 1838: debug("session_close_x11: x11 channel %d missing", id);
1839: } else {
1840: /* Detach X11 listener */
1841: debug("session_close_x11: detach x11 channel %d", id);
1842: channel_cancel_cleanup(id);
1843: if (c->ostate != CHAN_OUTPUT_CLOSED)
1844: chan_mark_dead(c);
1845: }
1846: }
1847:
1848: static void
1849: session_close_single_x11(int id, void *arg)
1850: {
1851: Session *s;
1852: u_int i;
1853:
1854: debug3("session_close_single_x11: channel %d", id);
1855: channel_cancel_cleanup(id);
1.221 stevesk 1856: if ((s = session_by_x11_channel(id)) == NULL)
1.184 djm 1857: fatal("session_close_single_x11: no x11 channel %d", id);
1858: for (i = 0; s->x11_chanids[i] != -1; i++) {
1859: debug("session_close_single_x11: session %d: "
1860: "closing channel %d", s->self, s->x11_chanids[i]);
1861: /*
1862: * The channel "id" is already closing, but make sure we
1863: * close all of its siblings.
1864: */
1865: if (s->x11_chanids[i] != id)
1866: session_close_x11(s->x11_chanids[i]);
1867: }
1.265 djm 1868: free(s->x11_chanids);
1.184 djm 1869: s->x11_chanids = NULL;
1.265 djm 1870: free(s->display);
1871: s->display = NULL;
1872: free(s->auth_proto);
1873: s->auth_proto = NULL;
1874: free(s->auth_data);
1875: s->auth_data = NULL;
1876: free(s->auth_display);
1877: s->auth_display = NULL;
1.184 djm 1878: }
1879:
1880: static void
1.2 markus 1881: session_exit_message(Session *s, int status)
1882: {
1883: Channel *c;
1.124 markus 1884:
1885: if ((c = channel_lookup(s->chanid)) == NULL)
1.85 markus 1886: fatal("session_exit_message: session %d: no channel %d",
1.2 markus 1887: s->self, s->chanid);
1.137 mpech 1888: debug("session_exit_message: session %d channel %d pid %ld",
1889: s->self, s->chanid, (long)s->pid);
1.2 markus 1890:
1891: if (WIFEXITED(status)) {
1.124 markus 1892: channel_request_start(s->chanid, "exit-status", 0);
1.2 markus 1893: packet_put_int(WEXITSTATUS(status));
1894: packet_send();
1895: } else if (WIFSIGNALED(status)) {
1.124 markus 1896: channel_request_start(s->chanid, "exit-signal", 0);
1.147 markus 1897: packet_put_cstring(sig2name(WTERMSIG(status)));
1.229 markus 1898: packet_put_char(WCOREDUMP(status)? 1 : 0);
1.2 markus 1899: packet_put_cstring("");
1900: packet_put_cstring("");
1901: packet_send();
1902: } else {
1903: /* Some weird exit cause. Just exit. */
1904: packet_disconnect("wait returned status %04x.", status);
1905: }
1906:
1907: /* disconnect channel */
1908: debug("session_exit_message: release channel %d", s->chanid);
1.187 djm 1909:
1910: /*
1911: * Adjust cleanup callback attachment to send close messages when
1.199 deraadt 1912: * the channel gets EOF. The session will be then be closed
1.187 djm 1913: * by session_close_by_channel when the childs close their fds.
1914: */
1915: channel_register_cleanup(c->self, session_close_by_channel, 1);
1916:
1.5 markus 1917: /*
1918: * emulate a write failure with 'chan_write_failed', nobody will be
1919: * interested in data we write.
1920: * Note that we must not call 'chan_read_failed', since there could
1921: * be some more data waiting in the pipe.
1922: */
1.8 markus 1923: if (c->ostate != CHAN_OUTPUT_CLOSED)
1924: chan_write_failed(c);
1.2 markus 1925: }
1926:
1.130 provos 1927: void
1.85 markus 1928: session_close(Session *s)
1.2 markus 1929: {
1.281 djm 1930: struct ssh *ssh = active_state; /* XXX */
1.182 djm 1931: u_int i;
1.173 djm 1932:
1.280 djm 1933: verbose("Close session: user %s from %.200s port %d id %d",
1934: s->pw->pw_name,
1.281 djm 1935: ssh_remote_ipaddr(ssh),
1936: ssh_remote_port(ssh),
1.280 djm 1937: s->self);
1938:
1.165 markus 1939: if (s->ttyfd != -1)
1.85 markus 1940: session_pty_cleanup(s);
1.265 djm 1941: free(s->term);
1942: free(s->display);
1943: free(s->x11_chanids);
1944: free(s->auth_display);
1945: free(s->auth_data);
1946: free(s->auth_proto);
1.267 djm 1947: free(s->subsys);
1.219 djm 1948: if (s->env != NULL) {
1949: for (i = 0; i < s->num_env; i++) {
1.265 djm 1950: free(s->env[i].name);
1951: free(s->env[i].val);
1.219 djm 1952: }
1.265 djm 1953: free(s->env);
1.173 djm 1954: }
1.9 markus 1955: session_proctitle(s);
1.237 djm 1956: session_unused(s->self);
1.2 markus 1957: }
1958:
1959: void
1960: session_close_by_pid(pid_t pid, int status)
1961: {
1962: Session *s = session_by_pid(pid);
1963: if (s == NULL) {
1.137 mpech 1964: debug("session_close_by_pid: no session for pid %ld",
1965: (long)pid);
1.2 markus 1966: return;
1967: }
1968: if (s->chanid != -1)
1969: session_exit_message(s, status);
1.187 djm 1970: if (s->ttyfd != -1)
1971: session_pty_cleanup(s);
1.197 djm 1972: s->pid = 0;
1.98 markus 1973: }
1974:
1.2 markus 1975: /*
1976: * this is called when a channel dies before
1977: * the session 'child' itself dies
1978: */
1979: void
1980: session_close_by_channel(int id, void *arg)
1981: {
1982: Session *s = session_by_channel(id);
1.187 djm 1983: u_int i;
1.184 djm 1984:
1.2 markus 1985: if (s == NULL) {
1.107 markus 1986: debug("session_close_by_channel: no session for id %d", id);
1.2 markus 1987: return;
1988: }
1.137 mpech 1989: debug("session_close_by_channel: channel %d child %ld",
1990: id, (long)s->pid);
1.107 markus 1991: if (s->pid != 0) {
1992: debug("session_close_by_channel: channel %d: has child", id);
1.108 markus 1993: /*
1994: * delay detach of session, but release pty, since
1995: * the fd's to the child are already closed
1996: */
1.165 markus 1997: if (s->ttyfd != -1)
1.108 markus 1998: session_pty_cleanup(s);
1.107 markus 1999: return;
2000: }
2001: /* detach by removing callback */
1.2 markus 2002: channel_cancel_cleanup(s->chanid);
1.187 djm 2003:
2004: /* Close any X11 listeners associated with this session */
2005: if (s->x11_chanids != NULL) {
2006: for (i = 0; s->x11_chanids[i] != -1; i++) {
2007: session_close_x11(s->x11_chanids[i]);
2008: s->x11_chanids[i] = -1;
2009: }
2010: }
2011:
1.2 markus 2012: s->chanid = -1;
1.106 markus 2013: session_close(s);
2014: }
2015:
2016: void
1.130 provos 2017: session_destroy_all(void (*closefunc)(Session *))
1.106 markus 2018: {
2019: int i;
1.237 djm 2020: for (i = 0; i < sessions_nalloc; i++) {
1.106 markus 2021: Session *s = &sessions[i];
1.130 provos 2022: if (s->used) {
2023: if (closefunc != NULL)
2024: closefunc(s);
2025: else
2026: session_close(s);
2027: }
1.2 markus 2028: }
1.9 markus 2029: }
2030:
1.94 itojun 2031: static char *
1.9 markus 2032: session_tty_list(void)
2033: {
2034: static char buf[1024];
2035: int i;
2036: buf[0] = '\0';
1.237 djm 2037: for (i = 0; i < sessions_nalloc; i++) {
1.9 markus 2038: Session *s = &sessions[i];
2039: if (s->used && s->ttyfd != -1) {
2040: if (buf[0] != '\0')
2041: strlcat(buf, ",", sizeof buf);
2042: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2043: }
2044: }
2045: if (buf[0] == '\0')
2046: strlcpy(buf, "notty", sizeof buf);
2047: return buf;
2048: }
2049:
2050: void
2051: session_proctitle(Session *s)
2052: {
2053: if (s->pw == NULL)
2054: error("no user for session %d", s->self);
2055: else
2056: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.81 markus 2057: }
2058:
2059: int
2060: session_setup_x11fwd(Session *s)
2061: {
2062: struct stat st;
1.109 stevesk 2063: char display[512], auth_display[512];
1.272 djm 2064: char hostname[NI_MAXHOST];
1.184 djm 2065: u_int i;
1.81 markus 2066:
2067: if (no_x11_forwarding_flag) {
2068: packet_send_debug("X11 forwarding disabled in user configuration file.");
2069: return 0;
2070: }
2071: if (!options.x11_forwarding) {
2072: debug("X11 forwarding disabled in server configuration file.");
2073: return 0;
2074: }
1.275 djm 2075: if (options.xauth_location == NULL ||
1.81 markus 2076: (stat(options.xauth_location, &st) == -1)) {
2077: packet_send_debug("No xauth program; cannot forward with spoofing.");
2078: return 0;
2079: }
1.87 markus 2080: if (s->display != NULL) {
1.81 markus 2081: debug("X11 display already set.");
2082: return 0;
2083: }
1.140 deraadt 2084: if (x11_create_display_inet(options.x11_display_offset,
2085: options.x11_use_localhost, s->single_connection,
1.184 djm 2086: &s->display_number, &s->x11_chanids) == -1) {
1.87 markus 2087: debug("x11_create_display_inet failed.");
1.81 markus 2088: return 0;
1.184 djm 2089: }
2090: for (i = 0; s->x11_chanids[i] != -1; i++) {
2091: channel_register_cleanup(s->x11_chanids[i],
1.187 djm 2092: session_close_single_x11, 0);
1.81 markus 2093: }
1.109 stevesk 2094:
2095: /* Set up a suitable value for the DISPLAY variable. */
2096: if (gethostname(hostname, sizeof(hostname)) < 0)
2097: fatal("gethostname: %.100s", strerror(errno));
2098: /*
2099: * auth_display must be used as the displayname when the
2100: * authorization entry is added with xauth(1). This will be
2101: * different than the DISPLAY string for localhost displays.
2102: */
1.119 stevesk 2103: if (options.x11_use_localhost) {
1.140 deraadt 2104: snprintf(display, sizeof display, "localhost:%u.%u",
1.109 stevesk 2105: s->display_number, s->screen);
1.140 deraadt 2106: snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
1.118 stevesk 2107: s->display_number, s->screen);
1.109 stevesk 2108: s->display = xstrdup(display);
1.118 stevesk 2109: s->auth_display = xstrdup(auth_display);
1.109 stevesk 2110: } else {
1.140 deraadt 2111: snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
1.109 stevesk 2112: s->display_number, s->screen);
2113: s->display = xstrdup(display);
1.118 stevesk 2114: s->auth_display = xstrdup(display);
1.109 stevesk 2115: }
2116:
1.81 markus 2117: return 1;
1.2 markus 2118: }
2119:
1.94 itojun 2120: static void
1.49 markus 2121: do_authenticated2(Authctxt *authctxt)
1.2 markus 2122: {
1.97 markus 2123: server_loop2(authctxt);
1.165 markus 2124: }
2125:
2126: void
2127: do_cleanup(Authctxt *authctxt)
2128: {
2129: static int called = 0;
2130:
2131: debug("do_cleanup");
2132:
2133: /* no cleanup if we're in the child for login shell */
2134: if (is_child)
2135: return;
2136:
2137: /* avoid double cleanup */
2138: if (called)
2139: return;
2140: called = 1;
2141:
1.218 markus 2142: if (authctxt == NULL || !authctxt->authenticated)
1.165 markus 2143: return;
2144: #ifdef KRB5
2145: if (options.kerberos_ticket_cleanup &&
2146: authctxt->krb5_ctx)
2147: krb5_cleanup_proc(authctxt);
1.161 markus 2148: #endif
1.165 markus 2149:
2150: #ifdef GSSAPI
1.283 markus 2151: if (options.gss_cleanup_creds)
1.165 markus 2152: ssh_gssapi_cleanup_creds();
2153: #endif
2154:
2155: /* remove agent socket */
2156: auth_sock_cleanup_proc(authctxt->pw);
2157:
2158: /*
2159: * Cleanup ptys/utmp only if privsep is disabled,
2160: * or if running in monitor.
2161: */
2162: if (!use_privsep || mm_is_monitor())
2163: session_destroy_all(session_pty_cleanup2);
1.1 markus 2164: }
1.281 djm 2165:
2166: /* Return a name for the remote host that fits inside utmp_size */
2167:
2168: const char *
2169: session_get_remote_name_or_ip(struct ssh *ssh, u_int utmp_size, int use_dns)
2170: {
2171: const char *remote = "";
2172:
2173: if (utmp_size > 0)
2174: remote = auth_get_canonical_hostname(ssh, use_dns);
2175: if (utmp_size == 0 || strlen(remote) > utmp_size)
2176: remote = ssh_remote_ipaddr(ssh);
2177: return remote;
2178: }
2179: