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