Annotation of src/usr.bin/ssh/session.c, Revision 1.251
1.251 ! dtucker 1: /* $OpenBSD: session.c,v 1.250 2010/01/12 01:31:05 dtucker 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;
1.251 ! dtucker 1103: char buf[1024], *nl, *def_nl = _PATH_NOLOGIN;
! 1104: struct stat sb;
1.127 markus 1105:
1.251 ! dtucker 1106: if (login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
! 1107: return;
! 1108: nl = login_getcapstr(lc, "nologin", def_nl, def_nl);
! 1109:
! 1110: if (stat(nl, &sb) == -1) {
! 1111: if (nl != def_nl)
! 1112: xfree(nl);
! 1113: return;
! 1114: }
! 1115:
! 1116: /* /etc/nologin exists. Print its contents if we can and exit. */
! 1117: logit("User %.100s not allowed because %s exists", pw->pw_name, nl);
! 1118: if ((f = fopen(nl, "r")) != NULL) {
1.127 markus 1119: while (fgets(buf, sizeof(buf), f))
1120: fputs(buf, stderr);
1121: fclose(f);
1122: }
1.251 ! dtucker 1123: exit(254);
1.127 markus 1124: }
1125:
1.226 djm 1126: /*
1127: * Chroot into a directory after checking it for safety: all path components
1128: * must be root-owned directories with strict permissions.
1129: */
1130: static void
1131: safely_chroot(const char *path, uid_t uid)
1132: {
1133: const char *cp;
1134: char component[MAXPATHLEN];
1135: struct stat st;
1136:
1137: if (*path != '/')
1138: fatal("chroot path does not begin at root");
1139: if (strlen(path) >= sizeof(component))
1140: fatal("chroot path too long");
1141:
1142: /*
1143: * Descend the path, checking that each component is a
1144: * root-owned directory with strict permissions.
1145: */
1146: for (cp = path; cp != NULL;) {
1147: if ((cp = strchr(cp, '/')) == NULL)
1148: strlcpy(component, path, sizeof(component));
1149: else {
1150: cp++;
1151: memcpy(component, path, cp - path);
1152: component[cp - path] = '\0';
1153: }
1154:
1155: debug3("%s: checking '%s'", __func__, component);
1156:
1157: if (stat(component, &st) != 0)
1158: fatal("%s: stat(\"%s\"): %s", __func__,
1159: component, strerror(errno));
1160: if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1161: fatal("bad ownership or modes for chroot "
1162: "directory %s\"%s\"",
1163: cp == NULL ? "" : "component ", component);
1164: if (!S_ISDIR(st.st_mode))
1165: fatal("chroot path %s\"%s\" is not a directory",
1166: cp == NULL ? "" : "component ", component);
1167:
1168: }
1169:
1170: if (chdir(path) == -1)
1171: fatal("Unable to chdir to chroot path \"%s\": "
1172: "%s", path, strerror(errno));
1173: if (chroot(path) == -1)
1174: fatal("chroot(\"%s\"): %s", path, strerror(errno));
1175: if (chdir("/") == -1)
1176: fatal("%s: chdir(/) after chroot: %s",
1177: __func__, strerror(errno));
1178: verbose("Changed root directory to \"%s\"", path);
1179: }
1180:
1.127 markus 1181: /* Set login name, uid, gid, and groups. */
1.130 provos 1182: void
1.127 markus 1183: do_setusercontext(struct passwd *pw)
1184: {
1.227 djm 1185: char *chroot_path, *tmp;
1186:
1.127 markus 1187: if (getuid() == 0 || geteuid() == 0) {
1.226 djm 1188: /* Prepare groups */
1.127 markus 1189: if (setusercontext(lc, pw, pw->pw_uid,
1.226 djm 1190: (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
1.127 markus 1191: perror("unable to set user context");
1192: exit(1);
1193: }
1.226 djm 1194:
1195: if (options.chroot_directory != NULL &&
1196: strcasecmp(options.chroot_directory, "none") != 0) {
1.227 djm 1197: tmp = tilde_expand_filename(options.chroot_directory,
1198: pw->pw_uid);
1199: chroot_path = percent_expand(tmp, "h", pw->pw_dir,
1200: "u", pw->pw_name, (char *)NULL);
1.226 djm 1201: safely_chroot(chroot_path, pw->pw_uid);
1.227 djm 1202: free(tmp);
1.226 djm 1203: free(chroot_path);
1204: }
1205:
1206: /* Set UID */
1207: if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETUSER) < 0) {
1208: perror("unable to set user context (setuser)");
1209: exit(1);
1210: }
1.127 markus 1211: }
1212: if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1213: fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1214: }
1215:
1.131 markus 1216: static void
1.172 markus 1217: do_pwchange(Session *s)
1218: {
1.179 dtucker 1219: fflush(NULL);
1.172 markus 1220: fprintf(stderr, "WARNING: Your password has expired.\n");
1221: if (s->ttyfd != -1) {
1.178 deraadt 1222: fprintf(stderr,
1.172 markus 1223: "You must change your password now and login again!\n");
1224: execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
1225: perror("passwd");
1226: } else {
1227: fprintf(stderr,
1228: "Password change required but no TTY available.\n");
1229: }
1230: exit(1);
1231: }
1232:
1233: static void
1.130 provos 1234: launch_login(struct passwd *pw, const char *hostname)
1235: {
1236: /* Launch login(1). */
1237:
1238: execl("/usr/bin/login", "login", "-h", hostname,
1239: "-p", "-f", "--", pw->pw_name, (char *)NULL);
1240:
1241: /* Login couldn't be executed, die. */
1242:
1243: perror("login");
1244: exit(1);
1245: }
1246:
1.172 markus 1247: static void
1248: child_close_fds(void)
1249: {
1250: int i;
1251:
1252: if (packet_get_connection_in() == packet_get_connection_out())
1253: close(packet_get_connection_in());
1254: else {
1255: close(packet_get_connection_in());
1256: close(packet_get_connection_out());
1257: }
1258: /*
1259: * Close all descriptors related to channels. They will still remain
1260: * open in the parent.
1261: */
1262: /* XXX better use close-on-exec? -markus */
1263: channel_close_all();
1264:
1265: /*
1266: * Close any extra file descriptors. Note that there may still be
1267: * descriptors left by system functions. They will be closed later.
1268: */
1269: endpwent();
1270:
1271: /*
1.188 djm 1272: * Close any extra open file descriptors so that we don't have them
1.172 markus 1273: * hanging around in clients. Note that we want to do this after
1274: * initgroups, because at least on Solaris 2.3 it leaves file
1275: * descriptors open.
1276: */
1277: for (i = 3; i < 64; i++)
1278: close(i);
1279: }
1280:
1.127 markus 1281: /*
1282: * Performs common processing for the child, such as setting up the
1283: * environment, closing extra file descriptors, setting the user and group
1284: * ids, and executing the command or shell.
1285: */
1.225 markus 1286: #define ARGV_MAX 10
1.127 markus 1287: void
1288: do_child(Session *s, const char *command)
1289: {
1290: extern char **environ;
1291: char **env;
1.225 markus 1292: char *argv[ARGV_MAX];
1.127 markus 1293: const char *shell, *shell0, *hostname = NULL;
1294: struct passwd *pw = s->pw;
1.239 djm 1295: int r = 0;
1.127 markus 1296:
1297: /* remove hostkey from the child's memory */
1298: destroy_sensitive_data();
1299:
1.172 markus 1300: /* Force a password change */
1301: if (s->authctxt->force_pwchange) {
1302: do_setusercontext(pw);
1303: child_close_fds();
1304: do_pwchange(s);
1305: exit(1);
1306: }
1307:
1.127 markus 1308: /* login(1) is only called if we execute the login shell */
1309: if (options.use_login && command != NULL)
1310: options.use_login = 0;
1311:
1312: /*
1313: * Login(1) does this as well, and it needs uid 0 for the "-h"
1314: * switch, so we let login(1) to this for us.
1315: */
1316: if (!options.use_login) {
1317: do_nologin(pw);
1318: do_setusercontext(pw);
1319: }
1320:
1321: /*
1322: * Get the shell from the password data. An empty shell field is
1323: * legal, and means /bin/sh.
1324: */
1325: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.152 markus 1326:
1327: /*
1328: * Make sure $SHELL points to the shell from the password file,
1329: * even if shell is overridden from login.conf
1330: */
1331: env = do_setup_env(s, shell);
1332:
1.127 markus 1333: shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1334:
1.26 millert 1335: /* we have to stash the hostname before we close our socket. */
1336: if (options.use_login)
1.70 stevesk 1337: hostname = get_remote_name_or_ip(utmp_len,
1.158 markus 1338: options.use_dns);
1.1 markus 1339: /*
1340: * Close the connection descriptors; note that this is the child, and
1341: * the server will still have the socket open, and it is important
1342: * that we do not shutdown it. Note that the descriptors cannot be
1343: * closed before building the environment, as we call
1344: * get_remote_ipaddr there.
1345: */
1.172 markus 1346: child_close_fds();
1.1 markus 1347:
1348: /*
1.125 deraadt 1349: * Must take new environment into use so that .ssh/rc,
1350: * /etc/ssh/sshrc and xauth are run in the proper environment.
1.1 markus 1351: */
1352: environ = env;
1.170 jakob 1353:
1354: #ifdef KRB5
1355: /*
1356: * At this point, we check to see if AFS is active and if we have
1357: * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1358: * if we can (and need to) extend the ticket into an AFS token. If
1359: * we don't do this, we run into potential problems if the user's
1360: * home directory is in AFS and it's not world-readable.
1361: */
1362:
1363: if (options.kerberos_get_afs_token && k_hasafs() &&
1.185 djm 1364: (s->authctxt->krb5_ctx != NULL)) {
1.170 jakob 1365: char cell[64];
1366:
1367: debug("Getting AFS token");
1368:
1369: k_setpag();
1370:
1371: if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1372: krb5_afslog(s->authctxt->krb5_ctx,
1373: s->authctxt->krb5_fwd_ccache, cell, NULL);
1374:
1375: krb5_afslog_home(s->authctxt->krb5_ctx,
1376: s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1377: }
1378: #endif
1.104 markus 1379:
1.188 djm 1380: /* Change current directory to the user's home directory. */
1.104 markus 1381: if (chdir(pw->pw_dir) < 0) {
1.239 djm 1382: /* Suppress missing homedir warning for chroot case */
1383: r = login_getcapbool(lc, "requirehome", 0);
1384: if (r || options.chroot_directory == NULL)
1385: fprintf(stderr, "Could not chdir to home "
1386: "directory %s: %s\n", pw->pw_dir,
1387: strerror(errno));
1388: if (r)
1.104 markus 1389: exit(1);
1390: }
1.230 djm 1391:
1392: closefrom(STDERR_FILENO + 1);
1.1 markus 1393:
1.127 markus 1394: if (!options.use_login)
1395: do_rc_files(s, shell);
1.67 markus 1396:
1397: /* restore SIGPIPE for child */
1.217 stevesk 1398: signal(SIGPIPE, SIG_DFL);
1.67 markus 1399:
1.248 djm 1400: if (s->is_subsystem == SUBSYSTEM_INT_SFTP_ERROR) {
1401: printf("This service allows sftp connections only.\n");
1402: fflush(NULL);
1403: exit(1);
1404: } else if (s->is_subsystem == SUBSYSTEM_INT_SFTP) {
1.225 markus 1405: extern int optind, optreset;
1406: int i;
1407: char *p, *args;
1408:
1.246 stevesk 1409: setproctitle("%s@%s", s->pw->pw_name, INTERNAL_SFTP_NAME);
1.243 millert 1410: args = xstrdup(command ? command : "sftp-server");
1.225 markus 1411: for (i = 0, (p = strtok(args, " ")); p; (p = strtok(NULL, " ")))
1412: if (i < ARGV_MAX - 1)
1413: argv[i++] = p;
1414: argv[i] = NULL;
1415: optind = optreset = 1;
1416: __progname = argv[0];
1.226 djm 1417: exit(sftp_server_main(i, argv, s->pw));
1.225 markus 1418: }
1.247 djm 1419:
1420: fflush(NULL);
1.225 markus 1421:
1.127 markus 1422: if (options.use_login) {
1.130 provos 1423: launch_login(pw, hostname);
1424: /* NEVERREACHED */
1.127 markus 1425: }
1426:
1427: /* Get the last component of the shell name. */
1428: if ((shell0 = strrchr(shell, '/')) != NULL)
1429: shell0++;
1430: else
1431: shell0 = shell;
1432:
1.1 markus 1433: /*
1434: * If we have no command, execute the shell. In this case, the shell
1435: * name to be passed in argv[0] is preceded by '-' to indicate that
1436: * this is a login shell.
1437: */
1438: if (!command) {
1.127 markus 1439: char argv0[256];
1.1 markus 1440:
1.127 markus 1441: /* Start the shell. Set initial character to '-'. */
1442: argv0[0] = '-';
1.1 markus 1443:
1.127 markus 1444: if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1.128 markus 1445: >= sizeof(argv0) - 1) {
1.127 markus 1446: errno = EINVAL;
1.1 markus 1447: perror(shell);
1448: exit(1);
1.127 markus 1449: }
1.1 markus 1450:
1.127 markus 1451: /* Execute the shell. */
1452: argv[0] = argv0;
1453: argv[1] = NULL;
1454: execve(shell, argv, env);
1455:
1456: /* Executing the shell failed. */
1457: perror(shell);
1458: exit(1);
1.1 markus 1459: }
1460: /*
1461: * Execute the command using the user's shell. This uses the -c
1462: * option to execute the command.
1463: */
1.127 markus 1464: argv[0] = (char *) shell0;
1.1 markus 1465: argv[1] = "-c";
1466: argv[2] = (char *) command;
1467: argv[3] = NULL;
1468: execve(shell, argv, env);
1469: perror(shell);
1470: exit(1);
1471: }
1472:
1.237 djm 1473: void
1474: session_unused(int id)
1475: {
1476: debug3("%s: session id %d unused", __func__, id);
1477: if (id >= options.max_sessions ||
1478: id >= sessions_nalloc) {
1479: fatal("%s: insane session id %d (max %d nalloc %d)",
1480: __func__, id, options.max_sessions, sessions_nalloc);
1481: }
1482: bzero(&sessions[id], sizeof(*sessions));
1483: sessions[id].self = id;
1484: sessions[id].used = 0;
1485: sessions[id].chanid = -1;
1486: sessions[id].ptyfd = -1;
1487: sessions[id].ttyfd = -1;
1488: sessions[id].ptymaster = -1;
1489: sessions[id].x11_chanids = NULL;
1490: sessions[id].next_unused = sessions_first_unused;
1491: sessions_first_unused = id;
1492: }
1493:
1.1 markus 1494: Session *
1495: session_new(void)
1496: {
1.237 djm 1497: Session *s, *tmp;
1498:
1499: if (sessions_first_unused == -1) {
1500: if (sessions_nalloc >= options.max_sessions)
1501: return NULL;
1502: debug2("%s: allocate (allocated %d max %d)",
1503: __func__, sessions_nalloc, options.max_sessions);
1504: tmp = xrealloc(sessions, sessions_nalloc + 1,
1505: sizeof(*sessions));
1506: if (tmp == NULL) {
1507: error("%s: cannot allocate %d sessions",
1508: __func__, sessions_nalloc + 1);
1509: return NULL;
1510: }
1511: sessions = tmp;
1512: session_unused(sessions_nalloc++);
1513: }
1514:
1515: if (sessions_first_unused >= sessions_nalloc ||
1516: sessions_first_unused < 0) {
1517: fatal("%s: insane first_unused %d max %d nalloc %d",
1518: __func__, sessions_first_unused, options.max_sessions,
1519: sessions_nalloc);
1520: }
1521:
1522: s = &sessions[sessions_first_unused];
1523: if (s->used) {
1524: fatal("%s: session %d already used",
1525: __func__, sessions_first_unused);
1526: }
1527: sessions_first_unused = s->next_unused;
1528: s->used = 1;
1529: s->next_unused = -1;
1530: debug("session_new: session %d", s->self);
1531:
1532: return s;
1.1 markus 1533: }
1534:
1.94 itojun 1535: static void
1.1 markus 1536: session_dump(void)
1537: {
1538: int i;
1.237 djm 1539: for (i = 0; i < sessions_nalloc; i++) {
1.1 markus 1540: Session *s = &sessions[i];
1.237 djm 1541:
1542: debug("dump: used %d next_unused %d session %d %p "
1543: "channel %d pid %ld",
1.1 markus 1544: s->used,
1.237 djm 1545: s->next_unused,
1.1 markus 1546: s->self,
1547: s,
1548: s->chanid,
1.137 mpech 1549: (long)s->pid);
1.1 markus 1550: }
1551: }
1552:
1.2 markus 1553: int
1.97 markus 1554: session_open(Authctxt *authctxt, int chanid)
1.2 markus 1555: {
1556: Session *s = session_new();
1557: debug("session_open: channel %d", chanid);
1558: if (s == NULL) {
1559: error("no more sessions");
1560: return 0;
1561: }
1.97 markus 1562: s->authctxt = authctxt;
1563: s->pw = authctxt->pw;
1.167 djm 1564: if (s->pw == NULL || !authctxt->valid)
1.54 stevesk 1565: fatal("no user for session %d", s->self);
1.2 markus 1566: debug("session_open: session %d: link with channel %d", s->self, chanid);
1567: s->chanid = chanid;
1568: return 1;
1569: }
1570:
1.130 provos 1571: Session *
1572: session_by_tty(char *tty)
1573: {
1574: int i;
1.237 djm 1575: for (i = 0; i < sessions_nalloc; i++) {
1.130 provos 1576: Session *s = &sessions[i];
1577: if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1578: debug("session_by_tty: session %d tty %s", i, tty);
1579: return s;
1580: }
1581: }
1582: debug("session_by_tty: unknown tty %.100s", tty);
1583: session_dump();
1584: return NULL;
1585: }
1586:
1.94 itojun 1587: static Session *
1.2 markus 1588: session_by_channel(int id)
1589: {
1590: int i;
1.237 djm 1591: for (i = 0; i < sessions_nalloc; i++) {
1.2 markus 1592: Session *s = &sessions[i];
1593: if (s->used && s->chanid == id) {
1.237 djm 1594: debug("session_by_channel: session %d channel %d",
1595: i, id);
1.2 markus 1596: return s;
1597: }
1598: }
1599: debug("session_by_channel: unknown channel %d", id);
1600: session_dump();
1601: return NULL;
1602: }
1603:
1.94 itojun 1604: static Session *
1.184 djm 1605: session_by_x11_channel(int id)
1606: {
1607: int i, j;
1608:
1.237 djm 1609: for (i = 0; i < sessions_nalloc; i++) {
1.184 djm 1610: Session *s = &sessions[i];
1611:
1612: if (s->x11_chanids == NULL || !s->used)
1613: continue;
1614: for (j = 0; s->x11_chanids[j] != -1; j++) {
1615: if (s->x11_chanids[j] == id) {
1616: debug("session_by_x11_channel: session %d "
1617: "channel %d", s->self, id);
1618: return s;
1619: }
1620: }
1621: }
1622: debug("session_by_x11_channel: unknown channel %d", id);
1623: session_dump();
1624: return NULL;
1625: }
1626:
1627: static Session *
1.2 markus 1628: session_by_pid(pid_t pid)
1629: {
1630: int i;
1.137 mpech 1631: debug("session_by_pid: pid %ld", (long)pid);
1.237 djm 1632: for (i = 0; i < sessions_nalloc; i++) {
1.2 markus 1633: Session *s = &sessions[i];
1634: if (s->used && s->pid == pid)
1635: return s;
1636: }
1.137 mpech 1637: error("session_by_pid: unknown pid %ld", (long)pid);
1.2 markus 1638: session_dump();
1639: return NULL;
1640: }
1641:
1.94 itojun 1642: static int
1.2 markus 1643: session_window_change_req(Session *s)
1644: {
1645: s->col = packet_get_int();
1646: s->row = packet_get_int();
1647: s->xpixel = packet_get_int();
1648: s->ypixel = packet_get_int();
1.116 markus 1649: packet_check_eom();
1.2 markus 1650: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1651: return 1;
1652: }
1653:
1.94 itojun 1654: static int
1.2 markus 1655: session_pty_req(Session *s)
1656: {
1.45 markus 1657: u_int len;
1.72 stevesk 1658: int n_bytes;
1.132 markus 1659:
1.86 markus 1660: if (no_pty_flag) {
1661: debug("Allocating a pty not permitted for this authentication.");
1.19 markus 1662: return 0;
1.86 markus 1663: }
1664: if (s->ttyfd != -1) {
1665: packet_disconnect("Protocol error: you already have a pty.");
1.3 markus 1666: return 0;
1.86 markus 1667: }
1668:
1.2 markus 1669: s->term = packet_get_string(&len);
1.86 markus 1670:
1671: if (compat20) {
1672: s->col = packet_get_int();
1673: s->row = packet_get_int();
1674: } else {
1675: s->row = packet_get_int();
1676: s->col = packet_get_int();
1677: }
1.2 markus 1678: s->xpixel = packet_get_int();
1679: s->ypixel = packet_get_int();
1680:
1681: if (strcmp(s->term, "") == 0) {
1682: xfree(s->term);
1683: s->term = NULL;
1684: }
1.86 markus 1685:
1.2 markus 1686: /* Allocate a pty and open it. */
1.86 markus 1687: debug("Allocating pty.");
1.237 djm 1688: if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty,
1689: sizeof(s->tty)))) {
1.86 markus 1690: if (s->term)
1691: xfree(s->term);
1.2 markus 1692: s->term = NULL;
1693: s->ptyfd = -1;
1694: s->ttyfd = -1;
1695: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1696: return 0;
1.2 markus 1697: }
1698: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1.86 markus 1699:
1700: /* for SSH1 the tty modes length is not given */
1701: if (!compat20)
1702: n_bytes = packet_remaining();
1703: tty_parse_modes(s->ttyfd, &n_bytes);
1704:
1.130 provos 1705: if (!use_privsep)
1706: pty_setowner(s->pw, s->tty);
1.86 markus 1707:
1708: /* Set window size from the packet. */
1.2 markus 1709: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1710:
1.116 markus 1711: packet_check_eom();
1.9 markus 1712: session_proctitle(s);
1.2 markus 1713: return 1;
1714: }
1715:
1.94 itojun 1716: static int
1.7 markus 1717: session_subsystem_req(Session *s)
1718: {
1.105 markus 1719: struct stat st;
1.45 markus 1720: u_int len;
1.7 markus 1721: int success = 0;
1.205 djm 1722: char *prog, *cmd, *subsys = packet_get_string(&len);
1.182 djm 1723: u_int i;
1.7 markus 1724:
1.116 markus 1725: packet_check_eom();
1.155 itojun 1726: logit("subsystem request for %.100s", subsys);
1.18 jakob 1727:
1728: for (i = 0; i < options.num_subsystems; i++) {
1.105 markus 1729: if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1.205 djm 1730: prog = options.subsystem_command[i];
1731: cmd = options.subsystem_args[i];
1.249 dtucker 1732: if (strcmp(INTERNAL_SFTP_NAME, prog) == 0) {
1.225 markus 1733: s->is_subsystem = SUBSYSTEM_INT_SFTP;
1.249 dtucker 1734: debug("subsystem: %s", prog);
1.225 markus 1735: } else {
1.249 dtucker 1736: if (stat(prog, &st) < 0)
1737: debug("subsystem: cannot stat %s: %s",
1738: prog, strerror(errno));
1.225 markus 1739: s->is_subsystem = SUBSYSTEM_EXT;
1.249 dtucker 1740: debug("subsystem: exec() %s", cmd);
1.105 markus 1741: }
1.237 djm 1742: success = do_exec(s, cmd) == 0;
1.122 markus 1743: break;
1.18 jakob 1744: }
1745: }
1746:
1747: if (!success)
1.155 itojun 1748: logit("subsystem request for %.100s failed, subsystem not found",
1.105 markus 1749: subsys);
1.7 markus 1750:
1751: xfree(subsys);
1752: return success;
1753: }
1754:
1.94 itojun 1755: static int
1.7 markus 1756: session_x11_req(Session *s)
1757: {
1.81 markus 1758: int success;
1.7 markus 1759:
1.184 djm 1760: if (s->auth_proto != NULL || s->auth_data != NULL) {
1761: error("session_x11_req: session %d: "
1.190 stevesk 1762: "x11 forwarding already active", s->self);
1.184 djm 1763: return 0;
1764: }
1.7 markus 1765: s->single_connection = packet_get_char();
1766: s->auth_proto = packet_get_string(NULL);
1767: s->auth_data = packet_get_string(NULL);
1768: s->screen = packet_get_int();
1.116 markus 1769: packet_check_eom();
1.7 markus 1770:
1.81 markus 1771: success = session_setup_x11fwd(s);
1772: if (!success) {
1.7 markus 1773: xfree(s->auth_proto);
1774: xfree(s->auth_data);
1.84 markus 1775: s->auth_proto = NULL;
1776: s->auth_data = NULL;
1.7 markus 1777: }
1.81 markus 1778: return success;
1.7 markus 1779: }
1780:
1.94 itojun 1781: static int
1.19 markus 1782: session_shell_req(Session *s)
1783: {
1.116 markus 1784: packet_check_eom();
1.237 djm 1785: return do_exec(s, NULL) == 0;
1.19 markus 1786: }
1787:
1.94 itojun 1788: static int
1.19 markus 1789: session_exec_req(Session *s)
1790: {
1.237 djm 1791: u_int len, success;
1792:
1.19 markus 1793: char *command = packet_get_string(&len);
1.116 markus 1794: packet_check_eom();
1.237 djm 1795: success = do_exec(s, command) == 0;
1.92 markus 1796: xfree(command);
1.237 djm 1797: return success;
1.19 markus 1798: }
1799:
1.94 itojun 1800: static int
1.157 markus 1801: session_break_req(Session *s)
1802: {
1803:
1.175 deraadt 1804: packet_get_int(); /* ignored */
1.157 markus 1805: packet_check_eom();
1806:
1.237 djm 1807: if (s->ttyfd == -1 || tcsendbreak(s->ttyfd, 0) < 0)
1.157 markus 1808: return 0;
1809: return 1;
1810: }
1811:
1812: static int
1.173 djm 1813: session_env_req(Session *s)
1814: {
1815: char *name, *val;
1816: u_int name_len, val_len, i;
1817:
1818: name = packet_get_string(&name_len);
1819: val = packet_get_string(&val_len);
1820: packet_check_eom();
1821:
1822: /* Don't set too many environment variables */
1823: if (s->num_env > 128) {
1824: debug2("Ignoring env request %s: too many env vars", name);
1825: goto fail;
1826: }
1827:
1828: for (i = 0; i < options.num_accept_env; i++) {
1829: if (match_pattern(name, options.accept_env[i])) {
1830: debug2("Setting env %d: %s=%s", s->num_env, name, val);
1.201 djm 1831: s->env = xrealloc(s->env, s->num_env + 1,
1832: sizeof(*s->env));
1.173 djm 1833: s->env[s->num_env].name = name;
1834: s->env[s->num_env].val = val;
1835: s->num_env++;
1836: return (1);
1837: }
1838: }
1839: debug2("Ignoring env request %s: disallowed name", name);
1840:
1841: fail:
1842: xfree(name);
1843: xfree(val);
1844: return (0);
1845: }
1846:
1847: static int
1.43 markus 1848: session_auth_agent_req(Session *s)
1849: {
1850: static int called = 0;
1.116 markus 1851: packet_check_eom();
1.235 pyr 1852: if (no_agent_forwarding_flag || !options.allow_agent_forwarding) {
1.44 markus 1853: debug("session_auth_agent_req: no_agent_forwarding_flag");
1854: return 0;
1855: }
1.43 markus 1856: if (called) {
1857: return 0;
1858: } else {
1859: called = 1;
1860: return auth_input_request_forwarding(s->pw);
1861: }
1862: }
1863:
1.123 markus 1864: int
1865: session_input_channel_req(Channel *c, const char *rtype)
1.2 markus 1866: {
1867: int success = 0;
1868: Session *s;
1869:
1.123 markus 1870: if ((s = session_by_channel(c->self)) == NULL) {
1.155 itojun 1871: logit("session_input_channel_req: no session %d req %.100s",
1.123 markus 1872: c->self, rtype);
1873: return 0;
1874: }
1875: debug("session_input_channel_req: session %d req %s", s->self, rtype);
1.2 markus 1876:
1877: /*
1.64 markus 1878: * a session is in LARVAL state until a shell, a command
1879: * or a subsystem is executed
1.2 markus 1880: */
1881: if (c->type == SSH_CHANNEL_LARVAL) {
1882: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1883: success = session_shell_req(s);
1.2 markus 1884: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1885: success = session_exec_req(s);
1.2 markus 1886: } else if (strcmp(rtype, "pty-req") == 0) {
1.221 stevesk 1887: success = session_pty_req(s);
1.7 markus 1888: } else if (strcmp(rtype, "x11-req") == 0) {
1889: success = session_x11_req(s);
1.43 markus 1890: } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1891: success = session_auth_agent_req(s);
1.7 markus 1892: } else if (strcmp(rtype, "subsystem") == 0) {
1893: success = session_subsystem_req(s);
1.173 djm 1894: } else if (strcmp(rtype, "env") == 0) {
1895: success = session_env_req(s);
1.2 markus 1896: }
1897: }
1898: if (strcmp(rtype, "window-change") == 0) {
1899: success = session_window_change_req(s);
1.177 djm 1900: } else if (strcmp(rtype, "break") == 0) {
1901: success = session_break_req(s);
1.2 markus 1902: }
1.177 djm 1903:
1.123 markus 1904: return success;
1.2 markus 1905: }
1906:
1907: void
1.241 dtucker 1908: session_set_fds(Session *s, int fdin, int fdout, int fderr, int is_tty)
1.2 markus 1909: {
1910: if (!compat20)
1911: fatal("session_set_fds: called for proto != 2.0");
1912: /*
1913: * now that have a child and a pipe to the child,
1914: * we can activate our channel and register the fd's
1915: */
1916: if (s->chanid == -1)
1917: fatal("no channel for session %d", s->self);
1918: channel_set_fds(s->chanid,
1919: fdout, fdin, fderr,
1.42 markus 1920: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1.241 dtucker 1921: 1, is_tty, CHAN_SES_WINDOW_DEFAULT);
1.2 markus 1922: }
1923:
1.85 markus 1924: /*
1925: * Function to perform pty cleanup. Also called if we get aborted abnormally
1926: * (e.g., due to a dropped connection).
1927: */
1.130 provos 1928: void
1.165 markus 1929: session_pty_cleanup2(Session *s)
1.1 markus 1930: {
1.85 markus 1931: if (s == NULL) {
1932: error("session_pty_cleanup: no session");
1933: return;
1934: }
1935: if (s->ttyfd == -1)
1.1 markus 1936: return;
1937:
1.54 stevesk 1938: debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1 markus 1939:
1940: /* Record that the user has logged out. */
1.85 markus 1941: if (s->pid != 0)
1942: record_logout(s->pid, s->tty);
1.1 markus 1943:
1944: /* Release the pseudo-tty. */
1.130 provos 1945: if (getuid() == 0)
1946: pty_release(s->tty);
1.1 markus 1947:
1948: /*
1949: * Close the server side of the socket pairs. We must do this after
1950: * the pty cleanup, so that another process doesn't get this pty
1951: * while we're still cleaning up.
1952: */
1.237 djm 1953: if (s->ptymaster != -1 && close(s->ptymaster) < 0)
1954: error("close(s->ptymaster/%d): %s",
1955: s->ptymaster, strerror(errno));
1.108 markus 1956:
1957: /* unlink pty from session */
1958: s->ttyfd = -1;
1.2 markus 1959: }
1960:
1.130 provos 1961: void
1.165 markus 1962: session_pty_cleanup(Session *s)
1.130 provos 1963: {
1.165 markus 1964: PRIVSEP(session_pty_cleanup2(s));
1.130 provos 1965: }
1966:
1.147 markus 1967: static char *
1968: sig2name(int sig)
1969: {
1970: #define SSH_SIG(x) if (sig == SIG ## x) return #x
1971: SSH_SIG(ABRT);
1972: SSH_SIG(ALRM);
1973: SSH_SIG(FPE);
1974: SSH_SIG(HUP);
1975: SSH_SIG(ILL);
1976: SSH_SIG(INT);
1977: SSH_SIG(KILL);
1978: SSH_SIG(PIPE);
1979: SSH_SIG(QUIT);
1980: SSH_SIG(SEGV);
1981: SSH_SIG(TERM);
1982: SSH_SIG(USR1);
1983: SSH_SIG(USR2);
1984: #undef SSH_SIG
1985: return "SIG@openssh.com";
1986: }
1987:
1.94 itojun 1988: static void
1.184 djm 1989: session_close_x11(int id)
1990: {
1991: Channel *c;
1992:
1.189 markus 1993: if ((c = channel_by_id(id)) == NULL) {
1.184 djm 1994: debug("session_close_x11: x11 channel %d missing", id);
1995: } else {
1996: /* Detach X11 listener */
1997: debug("session_close_x11: detach x11 channel %d", id);
1998: channel_cancel_cleanup(id);
1999: if (c->ostate != CHAN_OUTPUT_CLOSED)
2000: chan_mark_dead(c);
2001: }
2002: }
2003:
2004: static void
2005: session_close_single_x11(int id, void *arg)
2006: {
2007: Session *s;
2008: u_int i;
2009:
2010: debug3("session_close_single_x11: channel %d", id);
2011: channel_cancel_cleanup(id);
1.221 stevesk 2012: if ((s = session_by_x11_channel(id)) == NULL)
1.184 djm 2013: fatal("session_close_single_x11: no x11 channel %d", id);
2014: for (i = 0; s->x11_chanids[i] != -1; i++) {
2015: debug("session_close_single_x11: session %d: "
2016: "closing channel %d", s->self, s->x11_chanids[i]);
2017: /*
2018: * The channel "id" is already closing, but make sure we
2019: * close all of its siblings.
2020: */
2021: if (s->x11_chanids[i] != id)
2022: session_close_x11(s->x11_chanids[i]);
2023: }
2024: xfree(s->x11_chanids);
2025: s->x11_chanids = NULL;
2026: if (s->display) {
2027: xfree(s->display);
2028: s->display = NULL;
2029: }
2030: if (s->auth_proto) {
2031: xfree(s->auth_proto);
2032: s->auth_proto = NULL;
2033: }
2034: if (s->auth_data) {
2035: xfree(s->auth_data);
2036: s->auth_data = NULL;
2037: }
2038: if (s->auth_display) {
2039: xfree(s->auth_display);
2040: s->auth_display = NULL;
2041: }
2042: }
2043:
2044: static void
1.2 markus 2045: session_exit_message(Session *s, int status)
2046: {
2047: Channel *c;
1.124 markus 2048:
2049: if ((c = channel_lookup(s->chanid)) == NULL)
1.85 markus 2050: fatal("session_exit_message: session %d: no channel %d",
1.2 markus 2051: s->self, s->chanid);
1.137 mpech 2052: debug("session_exit_message: session %d channel %d pid %ld",
2053: s->self, s->chanid, (long)s->pid);
1.2 markus 2054:
2055: if (WIFEXITED(status)) {
1.124 markus 2056: channel_request_start(s->chanid, "exit-status", 0);
1.2 markus 2057: packet_put_int(WEXITSTATUS(status));
2058: packet_send();
2059: } else if (WIFSIGNALED(status)) {
1.124 markus 2060: channel_request_start(s->chanid, "exit-signal", 0);
1.147 markus 2061: packet_put_cstring(sig2name(WTERMSIG(status)));
1.229 markus 2062: packet_put_char(WCOREDUMP(status)? 1 : 0);
1.2 markus 2063: packet_put_cstring("");
2064: packet_put_cstring("");
2065: packet_send();
2066: } else {
2067: /* Some weird exit cause. Just exit. */
2068: packet_disconnect("wait returned status %04x.", status);
2069: }
2070:
2071: /* disconnect channel */
2072: debug("session_exit_message: release channel %d", s->chanid);
1.187 djm 2073:
2074: /*
2075: * Adjust cleanup callback attachment to send close messages when
1.199 deraadt 2076: * the channel gets EOF. The session will be then be closed
1.187 djm 2077: * by session_close_by_channel when the childs close their fds.
2078: */
2079: channel_register_cleanup(c->self, session_close_by_channel, 1);
2080:
1.5 markus 2081: /*
2082: * emulate a write failure with 'chan_write_failed', nobody will be
2083: * interested in data we write.
2084: * Note that we must not call 'chan_read_failed', since there could
2085: * be some more data waiting in the pipe.
2086: */
1.8 markus 2087: if (c->ostate != CHAN_OUTPUT_CLOSED)
2088: chan_write_failed(c);
1.2 markus 2089: }
2090:
1.130 provos 2091: void
1.85 markus 2092: session_close(Session *s)
1.2 markus 2093: {
1.182 djm 2094: u_int i;
1.173 djm 2095:
1.137 mpech 2096: debug("session_close: session %d pid %ld", s->self, (long)s->pid);
1.165 markus 2097: if (s->ttyfd != -1)
1.85 markus 2098: session_pty_cleanup(s);
1.2 markus 2099: if (s->term)
2100: xfree(s->term);
2101: if (s->display)
2102: xfree(s->display);
1.184 djm 2103: if (s->x11_chanids)
2104: xfree(s->x11_chanids);
1.118 stevesk 2105: if (s->auth_display)
2106: xfree(s->auth_display);
1.2 markus 2107: if (s->auth_data)
2108: xfree(s->auth_data);
2109: if (s->auth_proto)
2110: xfree(s->auth_proto);
1.219 djm 2111: if (s->env != NULL) {
2112: for (i = 0; i < s->num_env; i++) {
2113: xfree(s->env[i].name);
2114: xfree(s->env[i].val);
2115: }
2116: xfree(s->env);
1.173 djm 2117: }
1.9 markus 2118: session_proctitle(s);
1.237 djm 2119: session_unused(s->self);
1.2 markus 2120: }
2121:
2122: void
2123: session_close_by_pid(pid_t pid, int status)
2124: {
2125: Session *s = session_by_pid(pid);
2126: if (s == NULL) {
1.137 mpech 2127: debug("session_close_by_pid: no session for pid %ld",
2128: (long)pid);
1.2 markus 2129: return;
2130: }
2131: if (s->chanid != -1)
2132: session_exit_message(s, status);
1.187 djm 2133: if (s->ttyfd != -1)
2134: session_pty_cleanup(s);
1.197 djm 2135: s->pid = 0;
1.98 markus 2136: }
2137:
1.2 markus 2138: /*
2139: * this is called when a channel dies before
2140: * the session 'child' itself dies
2141: */
2142: void
2143: session_close_by_channel(int id, void *arg)
2144: {
2145: Session *s = session_by_channel(id);
1.187 djm 2146: u_int i;
1.184 djm 2147:
1.2 markus 2148: if (s == NULL) {
1.107 markus 2149: debug("session_close_by_channel: no session for id %d", id);
1.2 markus 2150: return;
2151: }
1.137 mpech 2152: debug("session_close_by_channel: channel %d child %ld",
2153: id, (long)s->pid);
1.107 markus 2154: if (s->pid != 0) {
2155: debug("session_close_by_channel: channel %d: has child", id);
1.108 markus 2156: /*
2157: * delay detach of session, but release pty, since
2158: * the fd's to the child are already closed
2159: */
1.165 markus 2160: if (s->ttyfd != -1)
1.108 markus 2161: session_pty_cleanup(s);
1.107 markus 2162: return;
2163: }
2164: /* detach by removing callback */
1.2 markus 2165: channel_cancel_cleanup(s->chanid);
1.187 djm 2166:
2167: /* Close any X11 listeners associated with this session */
2168: if (s->x11_chanids != NULL) {
2169: for (i = 0; s->x11_chanids[i] != -1; i++) {
2170: session_close_x11(s->x11_chanids[i]);
2171: s->x11_chanids[i] = -1;
2172: }
2173: }
2174:
1.2 markus 2175: s->chanid = -1;
1.106 markus 2176: session_close(s);
2177: }
2178:
2179: void
1.130 provos 2180: session_destroy_all(void (*closefunc)(Session *))
1.106 markus 2181: {
2182: int i;
1.237 djm 2183: for (i = 0; i < sessions_nalloc; i++) {
1.106 markus 2184: Session *s = &sessions[i];
1.130 provos 2185: if (s->used) {
2186: if (closefunc != NULL)
2187: closefunc(s);
2188: else
2189: session_close(s);
2190: }
1.2 markus 2191: }
1.9 markus 2192: }
2193:
1.94 itojun 2194: static char *
1.9 markus 2195: session_tty_list(void)
2196: {
2197: static char buf[1024];
2198: int i;
2199: buf[0] = '\0';
1.237 djm 2200: for (i = 0; i < sessions_nalloc; i++) {
1.9 markus 2201: Session *s = &sessions[i];
2202: if (s->used && s->ttyfd != -1) {
2203: if (buf[0] != '\0')
2204: strlcat(buf, ",", sizeof buf);
2205: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2206: }
2207: }
2208: if (buf[0] == '\0')
2209: strlcpy(buf, "notty", sizeof buf);
2210: return buf;
2211: }
2212:
2213: void
2214: session_proctitle(Session *s)
2215: {
2216: if (s->pw == NULL)
2217: error("no user for session %d", s->self);
2218: else
2219: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.81 markus 2220: }
2221:
2222: int
2223: session_setup_x11fwd(Session *s)
2224: {
2225: struct stat st;
1.109 stevesk 2226: char display[512], auth_display[512];
2227: char hostname[MAXHOSTNAMELEN];
1.184 djm 2228: u_int i;
1.81 markus 2229:
2230: if (no_x11_forwarding_flag) {
2231: packet_send_debug("X11 forwarding disabled in user configuration file.");
2232: return 0;
2233: }
2234: if (!options.x11_forwarding) {
2235: debug("X11 forwarding disabled in server configuration file.");
2236: return 0;
2237: }
2238: if (!options.xauth_location ||
2239: (stat(options.xauth_location, &st) == -1)) {
2240: packet_send_debug("No xauth program; cannot forward with spoofing.");
1.91 markus 2241: return 0;
2242: }
2243: if (options.use_login) {
2244: packet_send_debug("X11 forwarding disabled; "
2245: "not compatible with UseLogin=yes.");
1.81 markus 2246: return 0;
2247: }
1.87 markus 2248: if (s->display != NULL) {
1.81 markus 2249: debug("X11 display already set.");
2250: return 0;
2251: }
1.140 deraadt 2252: if (x11_create_display_inet(options.x11_display_offset,
2253: options.x11_use_localhost, s->single_connection,
1.184 djm 2254: &s->display_number, &s->x11_chanids) == -1) {
1.87 markus 2255: debug("x11_create_display_inet failed.");
1.81 markus 2256: return 0;
1.184 djm 2257: }
2258: for (i = 0; s->x11_chanids[i] != -1; i++) {
2259: channel_register_cleanup(s->x11_chanids[i],
1.187 djm 2260: session_close_single_x11, 0);
1.81 markus 2261: }
1.109 stevesk 2262:
2263: /* Set up a suitable value for the DISPLAY variable. */
2264: if (gethostname(hostname, sizeof(hostname)) < 0)
2265: fatal("gethostname: %.100s", strerror(errno));
2266: /*
2267: * auth_display must be used as the displayname when the
2268: * authorization entry is added with xauth(1). This will be
2269: * different than the DISPLAY string for localhost displays.
2270: */
1.119 stevesk 2271: if (options.x11_use_localhost) {
1.140 deraadt 2272: snprintf(display, sizeof display, "localhost:%u.%u",
1.109 stevesk 2273: s->display_number, s->screen);
1.140 deraadt 2274: snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
1.118 stevesk 2275: s->display_number, s->screen);
1.109 stevesk 2276: s->display = xstrdup(display);
1.118 stevesk 2277: s->auth_display = xstrdup(auth_display);
1.109 stevesk 2278: } else {
1.140 deraadt 2279: snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
1.109 stevesk 2280: s->display_number, s->screen);
2281: s->display = xstrdup(display);
1.118 stevesk 2282: s->auth_display = xstrdup(display);
1.109 stevesk 2283: }
2284:
1.81 markus 2285: return 1;
1.2 markus 2286: }
2287:
1.94 itojun 2288: static void
1.49 markus 2289: do_authenticated2(Authctxt *authctxt)
1.2 markus 2290: {
1.97 markus 2291: server_loop2(authctxt);
1.165 markus 2292: }
2293:
2294: void
2295: do_cleanup(Authctxt *authctxt)
2296: {
2297: static int called = 0;
2298:
2299: debug("do_cleanup");
2300:
2301: /* no cleanup if we're in the child for login shell */
2302: if (is_child)
2303: return;
2304:
2305: /* avoid double cleanup */
2306: if (called)
2307: return;
2308: called = 1;
2309:
1.218 markus 2310: if (authctxt == NULL || !authctxt->authenticated)
1.165 markus 2311: return;
2312: #ifdef KRB5
2313: if (options.kerberos_ticket_cleanup &&
2314: authctxt->krb5_ctx)
2315: krb5_cleanup_proc(authctxt);
1.161 markus 2316: #endif
1.165 markus 2317:
2318: #ifdef GSSAPI
2319: if (compat20 && options.gss_cleanup_creds)
2320: ssh_gssapi_cleanup_creds();
2321: #endif
2322:
2323: /* remove agent socket */
2324: auth_sock_cleanup_proc(authctxt->pw);
2325:
2326: /*
2327: * Cleanup ptys/utmp only if privsep is disabled,
2328: * or if running in monitor.
2329: */
2330: if (!use_privsep || mm_is_monitor())
2331: session_destroy_all(session_pty_cleanup2);
1.1 markus 2332: }