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