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