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