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