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