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