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