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