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