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