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