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