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