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