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