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