Annotation of src/usr.bin/ssh/session.c, Revision 1.223
1.223 ! djm 1: /* $OpenBSD: session.c,v 1.222 2007/08/23 02:49:43 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;
727: if (strchr(cp, '\n'))
728: *strchr(cp, '\n') = '\0';
729: value = strchr(cp, '=');
730: if (value == NULL) {
1.142 deraadt 731: fprintf(stderr, "Bad line %u in %.100s\n", lineno,
732: filename);
1.1 markus 733: continue;
734: }
1.14 deraadt 735: /*
736: * Replace the equals sign by nul, and advance value to
737: * the value string.
738: */
1.1 markus 739: *value = '\0';
740: value++;
741: child_set_env(env, envsize, cp, value);
742: }
743: fclose(f);
744: }
745:
1.127 markus 746: static char **
747: do_setup_env(Session *s, const char *shell)
1.1 markus 748: {
749: char buf[256];
1.127 markus 750: u_int i, envsize;
1.154 markus 751: char **env, *laddr;
1.127 markus 752: struct passwd *pw = s->pw;
1.1 markus 753:
754: /* Initialize the environment. */
755: envsize = 100;
1.220 djm 756: env = xcalloc(envsize, sizeof(char *));
1.1 markus 757: env[0] = NULL;
758:
1.161 markus 759: #ifdef GSSAPI
1.168 djm 760: /* Allow any GSSAPI methods that we've used to alter
1.161 markus 761: * the childs environment as they see fit
762: */
763: ssh_gssapi_do_child(&env, &envsize);
764: #endif
765:
1.1 markus 766: if (!options.use_login) {
767: /* Set basic environment. */
1.173 djm 768: for (i = 0; i < s->num_env; i++)
1.178 deraadt 769: child_set_env(&env, &envsize, s->env[i].name,
1.173 djm 770: s->env[i].val);
771:
1.1 markus 772: child_set_env(&env, &envsize, "USER", pw->pw_name);
773: child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
774: child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1.145 markus 775: if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
776: child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
777: else
778: child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1.1 markus 779:
780: snprintf(buf, sizeof buf, "%.200s/%.50s",
781: _PATH_MAILDIR, pw->pw_name);
782: child_set_env(&env, &envsize, "MAIL", buf);
783:
784: /* Normal systems set SHELL by default. */
785: child_set_env(&env, &envsize, "SHELL", shell);
786: }
787: if (getenv("TZ"))
788: child_set_env(&env, &envsize, "TZ", getenv("TZ"));
789:
790: /* Set custom environment options from RSA authentication. */
1.110 markus 791: if (!options.use_login) {
792: while (custom_environment) {
793: struct envstring *ce = custom_environment;
1.143 deraadt 794: char *str = ce->s;
1.127 markus 795:
1.143 deraadt 796: for (i = 0; str[i] != '=' && str[i]; i++)
1.110 markus 797: ;
1.143 deraadt 798: if (str[i] == '=') {
799: str[i] = 0;
800: child_set_env(&env, &envsize, str, str + i + 1);
1.110 markus 801: }
802: custom_environment = ce->next;
803: xfree(ce->s);
804: xfree(ce);
1.1 markus 805: }
806: }
807:
1.149 stevesk 808: /* SSH_CLIENT deprecated */
1.1 markus 809: snprintf(buf, sizeof buf, "%.50s %d %d",
1.127 markus 810: get_remote_ipaddr(), get_remote_port(), get_local_port());
1.1 markus 811: child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1.149 stevesk 812:
1.154 markus 813: laddr = get_local_ipaddr(packet_get_connection_in());
1.149 stevesk 814: snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1.154 markus 815: get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
816: xfree(laddr);
1.149 stevesk 817: child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1.1 markus 818:
1.60 markus 819: if (s->ttyfd != -1)
820: child_set_env(&env, &envsize, "SSH_TTY", s->tty);
821: if (s->term)
822: child_set_env(&env, &envsize, "TERM", s->term);
823: if (s->display)
824: child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34 markus 825: if (original_command)
826: child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
827: original_command);
1.96 dugsong 828: #ifdef KRB5
829: if (s->authctxt->krb5_ticket_file)
830: child_set_env(&env, &envsize, "KRB5CCNAME",
1.112 deraadt 831: s->authctxt->krb5_ticket_file);
1.96 dugsong 832: #endif
1.136 markus 833: if (auth_sock_name != NULL)
1.1 markus 834: child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1.136 markus 835: auth_sock_name);
1.1 markus 836:
837: /* read $HOME/.ssh/environment. */
1.146 markus 838: if (options.permit_user_env && !options.use_login) {
1.14 deraadt 839: snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
840: pw->pw_dir);
1.1 markus 841: read_environment_file(&env, &envsize, buf);
842: }
843: if (debug_flag) {
844: /* dump the environment */
845: fprintf(stderr, "Environment:\n");
846: for (i = 0; env[i]; i++)
847: fprintf(stderr, " %.200s\n", env[i]);
848: }
1.127 markus 849: return env;
850: }
851:
852: /*
853: * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
854: * first in this order).
855: */
856: static void
857: do_rc_files(Session *s, const char *shell)
858: {
859: FILE *f = NULL;
860: char cmd[1024];
861: int do_xauth;
862: struct stat st;
863:
864: do_xauth =
865: s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
866:
867: /* ignore _PATH_SSH_USER_RC for subsystems */
868: if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
869: snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
870: shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
871: if (debug_flag)
872: fprintf(stderr, "Running %s\n", cmd);
873: f = popen(cmd, "w");
874: if (f) {
875: if (do_xauth)
876: fprintf(f, "%s %s\n", s->auth_proto,
877: s->auth_data);
878: pclose(f);
879: } else
880: fprintf(stderr, "Could not run %s\n",
881: _PATH_SSH_USER_RC);
882: } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
883: if (debug_flag)
884: fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
885: _PATH_SSH_SYSTEM_RC);
886: f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
887: if (f) {
888: if (do_xauth)
889: fprintf(f, "%s %s\n", s->auth_proto,
890: s->auth_data);
891: pclose(f);
892: } else
893: fprintf(stderr, "Could not run %s\n",
894: _PATH_SSH_SYSTEM_RC);
895: } else if (do_xauth && options.xauth_location != NULL) {
896: /* Add authority data to .Xauthority if appropriate. */
897: if (debug_flag) {
898: fprintf(stderr,
1.151 stevesk 899: "Running %.500s remove %.100s\n",
1.165 markus 900: options.xauth_location, s->auth_display);
1.151 stevesk 901: fprintf(stderr,
902: "%.500s add %.100s %.100s %.100s\n",
1.127 markus 903: options.xauth_location, s->auth_display,
904: s->auth_proto, s->auth_data);
905: }
906: snprintf(cmd, sizeof cmd, "%s -q -",
907: options.xauth_location);
908: f = popen(cmd, "w");
909: if (f) {
1.151 stevesk 910: fprintf(f, "remove %s\n",
911: s->auth_display);
1.127 markus 912: fprintf(f, "add %s %s %s\n",
913: s->auth_display, s->auth_proto,
914: s->auth_data);
915: pclose(f);
916: } else {
917: fprintf(stderr, "Could not run %s\n",
918: cmd);
919: }
920: }
921: }
922:
923: static void
924: do_nologin(struct passwd *pw)
925: {
926: FILE *f = NULL;
927: char buf[1024];
928:
929: if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
930: f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
931: _PATH_NOLOGIN), "r");
932: if (f) {
933: /* /etc/nologin exists. Print its contents and exit. */
1.155 itojun 934: logit("User %.100s not allowed because %s exists",
1.150 stevesk 935: pw->pw_name, _PATH_NOLOGIN);
1.127 markus 936: while (fgets(buf, sizeof(buf), f))
937: fputs(buf, stderr);
938: fclose(f);
939: exit(254);
940: }
941: }
942:
943: /* Set login name, uid, gid, and groups. */
1.130 provos 944: void
1.127 markus 945: do_setusercontext(struct passwd *pw)
946: {
947: if (getuid() == 0 || geteuid() == 0) {
948: if (setusercontext(lc, pw, pw->pw_uid,
949: (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
950: perror("unable to set user context");
951: exit(1);
952: }
953: }
954: if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
955: fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
956: }
957:
1.131 markus 958: static void
1.172 markus 959: do_pwchange(Session *s)
960: {
1.179 dtucker 961: fflush(NULL);
1.172 markus 962: fprintf(stderr, "WARNING: Your password has expired.\n");
963: if (s->ttyfd != -1) {
1.178 deraadt 964: fprintf(stderr,
1.172 markus 965: "You must change your password now and login again!\n");
966: execl(_PATH_PASSWD_PROG, "passwd", (char *)NULL);
967: perror("passwd");
968: } else {
969: fprintf(stderr,
970: "Password change required but no TTY available.\n");
971: }
972: exit(1);
973: }
974:
975: static void
1.130 provos 976: launch_login(struct passwd *pw, const char *hostname)
977: {
978: /* Launch login(1). */
979:
980: execl("/usr/bin/login", "login", "-h", hostname,
981: "-p", "-f", "--", pw->pw_name, (char *)NULL);
982:
983: /* Login couldn't be executed, die. */
984:
985: perror("login");
986: exit(1);
987: }
988:
1.172 markus 989: static void
990: child_close_fds(void)
991: {
992: int i;
993:
994: if (packet_get_connection_in() == packet_get_connection_out())
995: close(packet_get_connection_in());
996: else {
997: close(packet_get_connection_in());
998: close(packet_get_connection_out());
999: }
1000: /*
1001: * Close all descriptors related to channels. They will still remain
1002: * open in the parent.
1003: */
1004: /* XXX better use close-on-exec? -markus */
1005: channel_close_all();
1006:
1007: /*
1008: * Close any extra file descriptors. Note that there may still be
1009: * descriptors left by system functions. They will be closed later.
1010: */
1011: endpwent();
1012:
1013: /*
1.188 djm 1014: * Close any extra open file descriptors so that we don't have them
1.172 markus 1015: * hanging around in clients. Note that we want to do this after
1016: * initgroups, because at least on Solaris 2.3 it leaves file
1017: * descriptors open.
1018: */
1019: for (i = 3; i < 64; i++)
1020: close(i);
1021: }
1022:
1.127 markus 1023: /*
1024: * Performs common processing for the child, such as setting up the
1025: * environment, closing extra file descriptors, setting the user and group
1026: * ids, and executing the command or shell.
1027: */
1028: void
1029: do_child(Session *s, const char *command)
1030: {
1031: extern char **environ;
1032: char **env;
1033: char *argv[10];
1034: const char *shell, *shell0, *hostname = NULL;
1035: struct passwd *pw = s->pw;
1036:
1037: /* remove hostkey from the child's memory */
1038: destroy_sensitive_data();
1039:
1.172 markus 1040: /* Force a password change */
1041: if (s->authctxt->force_pwchange) {
1042: do_setusercontext(pw);
1043: child_close_fds();
1044: do_pwchange(s);
1045: exit(1);
1046: }
1047:
1.127 markus 1048: /* login(1) is only called if we execute the login shell */
1049: if (options.use_login && command != NULL)
1050: options.use_login = 0;
1051:
1052: /*
1053: * Login(1) does this as well, and it needs uid 0 for the "-h"
1054: * switch, so we let login(1) to this for us.
1055: */
1056: if (!options.use_login) {
1057: do_nologin(pw);
1058: do_setusercontext(pw);
1059: }
1060:
1061: /*
1062: * Get the shell from the password data. An empty shell field is
1063: * legal, and means /bin/sh.
1064: */
1065: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.152 markus 1066:
1067: /*
1068: * Make sure $SHELL points to the shell from the password file,
1069: * even if shell is overridden from login.conf
1070: */
1071: env = do_setup_env(s, shell);
1072:
1.127 markus 1073: shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1074:
1.26 millert 1075: /* we have to stash the hostname before we close our socket. */
1076: if (options.use_login)
1.70 stevesk 1077: hostname = get_remote_name_or_ip(utmp_len,
1.158 markus 1078: options.use_dns);
1.1 markus 1079: /*
1080: * Close the connection descriptors; note that this is the child, and
1081: * the server will still have the socket open, and it is important
1082: * that we do not shutdown it. Note that the descriptors cannot be
1083: * closed before building the environment, as we call
1084: * get_remote_ipaddr there.
1085: */
1.172 markus 1086: child_close_fds();
1.1 markus 1087:
1088: /*
1.125 deraadt 1089: * Must take new environment into use so that .ssh/rc,
1090: * /etc/ssh/sshrc and xauth are run in the proper environment.
1.1 markus 1091: */
1092: environ = env;
1.170 jakob 1093:
1094: #ifdef KRB5
1095: /*
1096: * At this point, we check to see if AFS is active and if we have
1097: * a valid Kerberos 5 TGT. If so, it seems like a good idea to see
1098: * if we can (and need to) extend the ticket into an AFS token. If
1099: * we don't do this, we run into potential problems if the user's
1100: * home directory is in AFS and it's not world-readable.
1101: */
1102:
1103: if (options.kerberos_get_afs_token && k_hasafs() &&
1.185 djm 1104: (s->authctxt->krb5_ctx != NULL)) {
1.170 jakob 1105: char cell[64];
1106:
1107: debug("Getting AFS token");
1108:
1109: k_setpag();
1110:
1111: if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
1112: krb5_afslog(s->authctxt->krb5_ctx,
1113: s->authctxt->krb5_fwd_ccache, cell, NULL);
1114:
1115: krb5_afslog_home(s->authctxt->krb5_ctx,
1116: s->authctxt->krb5_fwd_ccache, NULL, NULL, pw->pw_dir);
1117: }
1118: #endif
1.104 markus 1119:
1.188 djm 1120: /* Change current directory to the user's home directory. */
1.104 markus 1121: if (chdir(pw->pw_dir) < 0) {
1122: fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1123: pw->pw_dir, strerror(errno));
1124: if (login_getcapbool(lc, "requirehome", 0))
1125: exit(1);
1126: }
1.1 markus 1127:
1.127 markus 1128: if (!options.use_login)
1129: do_rc_files(s, shell);
1.67 markus 1130:
1131: /* restore SIGPIPE for child */
1.217 stevesk 1132: signal(SIGPIPE, SIG_DFL);
1.67 markus 1133:
1.127 markus 1134: if (options.use_login) {
1.130 provos 1135: launch_login(pw, hostname);
1136: /* NEVERREACHED */
1.127 markus 1137: }
1138:
1139: /* Get the last component of the shell name. */
1140: if ((shell0 = strrchr(shell, '/')) != NULL)
1141: shell0++;
1142: else
1143: shell0 = shell;
1144:
1.1 markus 1145: /*
1146: * If we have no command, execute the shell. In this case, the shell
1147: * name to be passed in argv[0] is preceded by '-' to indicate that
1148: * this is a login shell.
1149: */
1150: if (!command) {
1.127 markus 1151: char argv0[256];
1.1 markus 1152:
1.127 markus 1153: /* Start the shell. Set initial character to '-'. */
1154: argv0[0] = '-';
1.1 markus 1155:
1.127 markus 1156: if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1.128 markus 1157: >= sizeof(argv0) - 1) {
1.127 markus 1158: errno = EINVAL;
1.1 markus 1159: perror(shell);
1160: exit(1);
1.127 markus 1161: }
1.1 markus 1162:
1.127 markus 1163: /* Execute the shell. */
1164: argv[0] = argv0;
1165: argv[1] = NULL;
1166: execve(shell, argv, env);
1167:
1168: /* Executing the shell failed. */
1169: perror(shell);
1170: exit(1);
1.1 markus 1171: }
1172: /*
1173: * Execute the command using the user's shell. This uses the -c
1174: * option to execute the command.
1175: */
1.127 markus 1176: argv[0] = (char *) shell0;
1.1 markus 1177: argv[1] = "-c";
1178: argv[2] = (char *) command;
1179: argv[3] = NULL;
1180: execve(shell, argv, env);
1181: perror(shell);
1182: exit(1);
1183: }
1184:
1185: Session *
1186: session_new(void)
1187: {
1188: int i;
1189: static int did_init = 0;
1190: if (!did_init) {
1191: debug("session_new: init");
1.112 deraadt 1192: for (i = 0; i < MAX_SESSIONS; i++) {
1.1 markus 1193: sessions[i].used = 0;
1194: }
1195: did_init = 1;
1196: }
1.112 deraadt 1197: for (i = 0; i < MAX_SESSIONS; i++) {
1.1 markus 1198: Session *s = &sessions[i];
1199: if (! s->used) {
1.68 djm 1200: memset(s, 0, sizeof(*s));
1.1 markus 1201: s->chanid = -1;
1202: s->ptyfd = -1;
1203: s->ttyfd = -1;
1204: s->used = 1;
1.85 markus 1205: s->self = i;
1.184 djm 1206: s->x11_chanids = NULL;
1.1 markus 1207: debug("session_new: session %d", i);
1208: return s;
1209: }
1210: }
1211: return NULL;
1212: }
1213:
1.94 itojun 1214: static void
1.1 markus 1215: session_dump(void)
1216: {
1217: int i;
1.112 deraadt 1218: for (i = 0; i < MAX_SESSIONS; i++) {
1.1 markus 1219: Session *s = &sessions[i];
1.137 mpech 1220: debug("dump: used %d session %d %p channel %d pid %ld",
1.1 markus 1221: s->used,
1222: s->self,
1223: s,
1224: s->chanid,
1.137 mpech 1225: (long)s->pid);
1.1 markus 1226: }
1227: }
1228:
1.2 markus 1229: int
1.97 markus 1230: session_open(Authctxt *authctxt, int chanid)
1.2 markus 1231: {
1232: Session *s = session_new();
1233: debug("session_open: channel %d", chanid);
1234: if (s == NULL) {
1235: error("no more sessions");
1236: return 0;
1237: }
1.97 markus 1238: s->authctxt = authctxt;
1239: s->pw = authctxt->pw;
1.167 djm 1240: if (s->pw == NULL || !authctxt->valid)
1.54 stevesk 1241: fatal("no user for session %d", s->self);
1.2 markus 1242: debug("session_open: session %d: link with channel %d", s->self, chanid);
1243: s->chanid = chanid;
1244: return 1;
1245: }
1246:
1.130 provos 1247: Session *
1248: session_by_tty(char *tty)
1249: {
1250: int i;
1251: for (i = 0; i < MAX_SESSIONS; i++) {
1252: Session *s = &sessions[i];
1253: if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1254: debug("session_by_tty: session %d tty %s", i, tty);
1255: return s;
1256: }
1257: }
1258: debug("session_by_tty: unknown tty %.100s", tty);
1259: session_dump();
1260: return NULL;
1261: }
1262:
1.94 itojun 1263: static Session *
1.2 markus 1264: session_by_channel(int id)
1265: {
1266: int i;
1.112 deraadt 1267: for (i = 0; i < MAX_SESSIONS; i++) {
1.2 markus 1268: Session *s = &sessions[i];
1269: if (s->used && s->chanid == id) {
1270: debug("session_by_channel: session %d channel %d", i, id);
1271: return s;
1272: }
1273: }
1274: debug("session_by_channel: unknown channel %d", id);
1275: session_dump();
1276: return NULL;
1277: }
1278:
1.94 itojun 1279: static Session *
1.184 djm 1280: session_by_x11_channel(int id)
1281: {
1282: int i, j;
1283:
1284: for (i = 0; i < MAX_SESSIONS; i++) {
1285: Session *s = &sessions[i];
1286:
1287: if (s->x11_chanids == NULL || !s->used)
1288: continue;
1289: for (j = 0; s->x11_chanids[j] != -1; j++) {
1290: if (s->x11_chanids[j] == id) {
1291: debug("session_by_x11_channel: session %d "
1292: "channel %d", s->self, id);
1293: return s;
1294: }
1295: }
1296: }
1297: debug("session_by_x11_channel: unknown channel %d", id);
1298: session_dump();
1299: return NULL;
1300: }
1301:
1302: static Session *
1.2 markus 1303: session_by_pid(pid_t pid)
1304: {
1305: int i;
1.137 mpech 1306: debug("session_by_pid: pid %ld", (long)pid);
1.112 deraadt 1307: for (i = 0; i < MAX_SESSIONS; i++) {
1.2 markus 1308: Session *s = &sessions[i];
1309: if (s->used && s->pid == pid)
1310: return s;
1311: }
1.137 mpech 1312: error("session_by_pid: unknown pid %ld", (long)pid);
1.2 markus 1313: session_dump();
1314: return NULL;
1315: }
1316:
1.94 itojun 1317: static int
1.2 markus 1318: session_window_change_req(Session *s)
1319: {
1320: s->col = packet_get_int();
1321: s->row = packet_get_int();
1322: s->xpixel = packet_get_int();
1323: s->ypixel = packet_get_int();
1.116 markus 1324: packet_check_eom();
1.2 markus 1325: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1326: return 1;
1327: }
1328:
1.94 itojun 1329: static int
1.2 markus 1330: session_pty_req(Session *s)
1331: {
1.45 markus 1332: u_int len;
1.72 stevesk 1333: int n_bytes;
1.132 markus 1334:
1.86 markus 1335: if (no_pty_flag) {
1336: debug("Allocating a pty not permitted for this authentication.");
1.19 markus 1337: return 0;
1.86 markus 1338: }
1339: if (s->ttyfd != -1) {
1340: packet_disconnect("Protocol error: you already have a pty.");
1.3 markus 1341: return 0;
1.86 markus 1342: }
1343:
1.2 markus 1344: s->term = packet_get_string(&len);
1.86 markus 1345:
1346: if (compat20) {
1347: s->col = packet_get_int();
1348: s->row = packet_get_int();
1349: } else {
1350: s->row = packet_get_int();
1351: s->col = packet_get_int();
1352: }
1.2 markus 1353: s->xpixel = packet_get_int();
1354: s->ypixel = packet_get_int();
1355:
1356: if (strcmp(s->term, "") == 0) {
1357: xfree(s->term);
1358: s->term = NULL;
1359: }
1.86 markus 1360:
1.2 markus 1361: /* Allocate a pty and open it. */
1.86 markus 1362: debug("Allocating pty.");
1.130 provos 1363: if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
1.86 markus 1364: if (s->term)
1365: xfree(s->term);
1.2 markus 1366: s->term = NULL;
1367: s->ptyfd = -1;
1368: s->ttyfd = -1;
1369: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1370: return 0;
1.2 markus 1371: }
1372: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1.86 markus 1373:
1374: /* for SSH1 the tty modes length is not given */
1375: if (!compat20)
1376: n_bytes = packet_remaining();
1377: tty_parse_modes(s->ttyfd, &n_bytes);
1378:
1.130 provos 1379: if (!use_privsep)
1380: pty_setowner(s->pw, s->tty);
1.86 markus 1381:
1382: /* Set window size from the packet. */
1.2 markus 1383: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1384:
1.116 markus 1385: packet_check_eom();
1.9 markus 1386: session_proctitle(s);
1.2 markus 1387: return 1;
1388: }
1389:
1.94 itojun 1390: static int
1.7 markus 1391: session_subsystem_req(Session *s)
1392: {
1.105 markus 1393: struct stat st;
1.45 markus 1394: u_int len;
1.7 markus 1395: int success = 0;
1.205 djm 1396: char *prog, *cmd, *subsys = packet_get_string(&len);
1.182 djm 1397: u_int i;
1.7 markus 1398:
1.116 markus 1399: packet_check_eom();
1.155 itojun 1400: logit("subsystem request for %.100s", subsys);
1.18 jakob 1401:
1402: for (i = 0; i < options.num_subsystems; i++) {
1.105 markus 1403: if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1.205 djm 1404: prog = options.subsystem_command[i];
1405: cmd = options.subsystem_args[i];
1406: if (stat(prog, &st) < 0) {
1407: error("subsystem: cannot stat %s: %s", prog,
1.105 markus 1408: strerror(errno));
1409: break;
1410: }
1411: debug("subsystem: exec() %s", cmd);
1.64 markus 1412: s->is_subsystem = 1;
1.105 markus 1413: do_exec(s, cmd);
1.18 jakob 1414: success = 1;
1.122 markus 1415: break;
1.18 jakob 1416: }
1417: }
1418:
1419: if (!success)
1.155 itojun 1420: logit("subsystem request for %.100s failed, subsystem not found",
1.105 markus 1421: subsys);
1.7 markus 1422:
1423: xfree(subsys);
1424: return success;
1425: }
1426:
1.94 itojun 1427: static int
1.7 markus 1428: session_x11_req(Session *s)
1429: {
1.81 markus 1430: int success;
1.7 markus 1431:
1.184 djm 1432: if (s->auth_proto != NULL || s->auth_data != NULL) {
1433: error("session_x11_req: session %d: "
1.190 stevesk 1434: "x11 forwarding already active", s->self);
1.184 djm 1435: return 0;
1436: }
1.7 markus 1437: s->single_connection = packet_get_char();
1438: s->auth_proto = packet_get_string(NULL);
1439: s->auth_data = packet_get_string(NULL);
1440: s->screen = packet_get_int();
1.116 markus 1441: packet_check_eom();
1.7 markus 1442:
1.81 markus 1443: success = session_setup_x11fwd(s);
1444: if (!success) {
1.7 markus 1445: xfree(s->auth_proto);
1446: xfree(s->auth_data);
1.84 markus 1447: s->auth_proto = NULL;
1448: s->auth_data = NULL;
1.7 markus 1449: }
1.81 markus 1450: return success;
1.7 markus 1451: }
1452:
1.94 itojun 1453: static int
1.19 markus 1454: session_shell_req(Session *s)
1455: {
1.116 markus 1456: packet_check_eom();
1.90 markus 1457: do_exec(s, NULL);
1.19 markus 1458: return 1;
1459: }
1460:
1.94 itojun 1461: static int
1.19 markus 1462: session_exec_req(Session *s)
1463: {
1.45 markus 1464: u_int len;
1.19 markus 1465: char *command = packet_get_string(&len);
1.116 markus 1466: packet_check_eom();
1.90 markus 1467: do_exec(s, command);
1.92 markus 1468: xfree(command);
1.19 markus 1469: return 1;
1470: }
1471:
1.94 itojun 1472: static int
1.157 markus 1473: session_break_req(Session *s)
1474: {
1475:
1.175 deraadt 1476: packet_get_int(); /* ignored */
1.157 markus 1477: packet_check_eom();
1478:
1.160 markus 1479: if (s->ttyfd == -1 ||
1480: tcsendbreak(s->ttyfd, 0) < 0)
1.157 markus 1481: return 0;
1482: return 1;
1483: }
1484:
1485: static int
1.173 djm 1486: session_env_req(Session *s)
1487: {
1488: char *name, *val;
1489: u_int name_len, val_len, i;
1490:
1491: name = packet_get_string(&name_len);
1492: val = packet_get_string(&val_len);
1493: packet_check_eom();
1494:
1495: /* Don't set too many environment variables */
1496: if (s->num_env > 128) {
1497: debug2("Ignoring env request %s: too many env vars", name);
1498: goto fail;
1499: }
1500:
1501: for (i = 0; i < options.num_accept_env; i++) {
1502: if (match_pattern(name, options.accept_env[i])) {
1503: debug2("Setting env %d: %s=%s", s->num_env, name, val);
1.201 djm 1504: s->env = xrealloc(s->env, s->num_env + 1,
1505: sizeof(*s->env));
1.173 djm 1506: s->env[s->num_env].name = name;
1507: s->env[s->num_env].val = val;
1508: s->num_env++;
1509: return (1);
1510: }
1511: }
1512: debug2("Ignoring env request %s: disallowed name", name);
1513:
1514: fail:
1515: xfree(name);
1516: xfree(val);
1517: return (0);
1518: }
1519:
1520: static int
1.43 markus 1521: session_auth_agent_req(Session *s)
1522: {
1523: static int called = 0;
1.116 markus 1524: packet_check_eom();
1.44 markus 1525: if (no_agent_forwarding_flag) {
1526: debug("session_auth_agent_req: no_agent_forwarding_flag");
1527: return 0;
1528: }
1.43 markus 1529: if (called) {
1530: return 0;
1531: } else {
1532: called = 1;
1533: return auth_input_request_forwarding(s->pw);
1534: }
1535: }
1536:
1.123 markus 1537: int
1538: session_input_channel_req(Channel *c, const char *rtype)
1.2 markus 1539: {
1540: int success = 0;
1541: Session *s;
1542:
1.123 markus 1543: if ((s = session_by_channel(c->self)) == NULL) {
1.155 itojun 1544: logit("session_input_channel_req: no session %d req %.100s",
1.123 markus 1545: c->self, rtype);
1546: return 0;
1547: }
1548: debug("session_input_channel_req: session %d req %s", s->self, rtype);
1.2 markus 1549:
1550: /*
1.64 markus 1551: * a session is in LARVAL state until a shell, a command
1552: * or a subsystem is executed
1.2 markus 1553: */
1554: if (c->type == SSH_CHANNEL_LARVAL) {
1555: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1556: success = session_shell_req(s);
1.2 markus 1557: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1558: success = session_exec_req(s);
1.2 markus 1559: } else if (strcmp(rtype, "pty-req") == 0) {
1.221 stevesk 1560: success = session_pty_req(s);
1.7 markus 1561: } else if (strcmp(rtype, "x11-req") == 0) {
1562: success = session_x11_req(s);
1.43 markus 1563: } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1564: success = session_auth_agent_req(s);
1.7 markus 1565: } else if (strcmp(rtype, "subsystem") == 0) {
1566: success = session_subsystem_req(s);
1.173 djm 1567: } else if (strcmp(rtype, "env") == 0) {
1568: success = session_env_req(s);
1.2 markus 1569: }
1570: }
1571: if (strcmp(rtype, "window-change") == 0) {
1572: success = session_window_change_req(s);
1.177 djm 1573: } else if (strcmp(rtype, "break") == 0) {
1574: success = session_break_req(s);
1.2 markus 1575: }
1.177 djm 1576:
1.123 markus 1577: return success;
1.2 markus 1578: }
1579:
1580: void
1581: session_set_fds(Session *s, int fdin, int fdout, int fderr)
1582: {
1583: if (!compat20)
1584: fatal("session_set_fds: called for proto != 2.0");
1585: /*
1586: * now that have a child and a pipe to the child,
1587: * we can activate our channel and register the fd's
1588: */
1589: if (s->chanid == -1)
1590: fatal("no channel for session %d", s->self);
1591: channel_set_fds(s->chanid,
1592: fdout, fdin, fderr,
1.42 markus 1593: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1.126 markus 1594: 1,
1595: CHAN_SES_WINDOW_DEFAULT);
1.2 markus 1596: }
1597:
1.85 markus 1598: /*
1599: * Function to perform pty cleanup. Also called if we get aborted abnormally
1600: * (e.g., due to a dropped connection).
1601: */
1.130 provos 1602: void
1.165 markus 1603: session_pty_cleanup2(Session *s)
1.1 markus 1604: {
1.85 markus 1605: if (s == NULL) {
1606: error("session_pty_cleanup: no session");
1607: return;
1608: }
1609: if (s->ttyfd == -1)
1.1 markus 1610: return;
1611:
1.54 stevesk 1612: debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1 markus 1613:
1614: /* Record that the user has logged out. */
1.85 markus 1615: if (s->pid != 0)
1616: record_logout(s->pid, s->tty);
1.1 markus 1617:
1618: /* Release the pseudo-tty. */
1.130 provos 1619: if (getuid() == 0)
1620: pty_release(s->tty);
1.1 markus 1621:
1622: /*
1623: * Close the server side of the socket pairs. We must do this after
1624: * the pty cleanup, so that another process doesn't get this pty
1625: * while we're still cleaning up.
1626: */
1627: if (close(s->ptymaster) < 0)
1.130 provos 1628: error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
1.108 markus 1629:
1630: /* unlink pty from session */
1631: s->ttyfd = -1;
1.2 markus 1632: }
1633:
1.130 provos 1634: void
1.165 markus 1635: session_pty_cleanup(Session *s)
1.130 provos 1636: {
1.165 markus 1637: PRIVSEP(session_pty_cleanup2(s));
1.130 provos 1638: }
1639:
1.147 markus 1640: static char *
1641: sig2name(int sig)
1642: {
1643: #define SSH_SIG(x) if (sig == SIG ## x) return #x
1644: SSH_SIG(ABRT);
1645: SSH_SIG(ALRM);
1646: SSH_SIG(FPE);
1647: SSH_SIG(HUP);
1648: SSH_SIG(ILL);
1649: SSH_SIG(INT);
1650: SSH_SIG(KILL);
1651: SSH_SIG(PIPE);
1652: SSH_SIG(QUIT);
1653: SSH_SIG(SEGV);
1654: SSH_SIG(TERM);
1655: SSH_SIG(USR1);
1656: SSH_SIG(USR2);
1657: #undef SSH_SIG
1658: return "SIG@openssh.com";
1659: }
1660:
1.94 itojun 1661: static void
1.184 djm 1662: session_close_x11(int id)
1663: {
1664: Channel *c;
1665:
1.189 markus 1666: if ((c = channel_by_id(id)) == NULL) {
1.184 djm 1667: debug("session_close_x11: x11 channel %d missing", id);
1668: } else {
1669: /* Detach X11 listener */
1670: debug("session_close_x11: detach x11 channel %d", id);
1671: channel_cancel_cleanup(id);
1672: if (c->ostate != CHAN_OUTPUT_CLOSED)
1673: chan_mark_dead(c);
1674: }
1675: }
1676:
1677: static void
1678: session_close_single_x11(int id, void *arg)
1679: {
1680: Session *s;
1681: u_int i;
1682:
1683: debug3("session_close_single_x11: channel %d", id);
1684: channel_cancel_cleanup(id);
1.221 stevesk 1685: if ((s = session_by_x11_channel(id)) == NULL)
1.184 djm 1686: fatal("session_close_single_x11: no x11 channel %d", id);
1687: for (i = 0; s->x11_chanids[i] != -1; i++) {
1688: debug("session_close_single_x11: session %d: "
1689: "closing channel %d", s->self, s->x11_chanids[i]);
1690: /*
1691: * The channel "id" is already closing, but make sure we
1692: * close all of its siblings.
1693: */
1694: if (s->x11_chanids[i] != id)
1695: session_close_x11(s->x11_chanids[i]);
1696: }
1697: xfree(s->x11_chanids);
1698: s->x11_chanids = NULL;
1699: if (s->display) {
1700: xfree(s->display);
1701: s->display = NULL;
1702: }
1703: if (s->auth_proto) {
1704: xfree(s->auth_proto);
1705: s->auth_proto = NULL;
1706: }
1707: if (s->auth_data) {
1708: xfree(s->auth_data);
1709: s->auth_data = NULL;
1710: }
1711: if (s->auth_display) {
1712: xfree(s->auth_display);
1713: s->auth_display = NULL;
1714: }
1715: }
1716:
1717: static void
1.2 markus 1718: session_exit_message(Session *s, int status)
1719: {
1720: Channel *c;
1.124 markus 1721:
1722: if ((c = channel_lookup(s->chanid)) == NULL)
1.85 markus 1723: fatal("session_exit_message: session %d: no channel %d",
1.2 markus 1724: s->self, s->chanid);
1.137 mpech 1725: debug("session_exit_message: session %d channel %d pid %ld",
1726: s->self, s->chanid, (long)s->pid);
1.2 markus 1727:
1728: if (WIFEXITED(status)) {
1.124 markus 1729: channel_request_start(s->chanid, "exit-status", 0);
1.2 markus 1730: packet_put_int(WEXITSTATUS(status));
1731: packet_send();
1732: } else if (WIFSIGNALED(status)) {
1.124 markus 1733: channel_request_start(s->chanid, "exit-signal", 0);
1.147 markus 1734: packet_put_cstring(sig2name(WTERMSIG(status)));
1.2 markus 1735: packet_put_char(WCOREDUMP(status));
1736: packet_put_cstring("");
1737: packet_put_cstring("");
1738: packet_send();
1739: } else {
1740: /* Some weird exit cause. Just exit. */
1741: packet_disconnect("wait returned status %04x.", status);
1742: }
1743:
1744: /* disconnect channel */
1745: debug("session_exit_message: release channel %d", s->chanid);
1.187 djm 1746:
1747: /*
1748: * Adjust cleanup callback attachment to send close messages when
1.199 deraadt 1749: * the channel gets EOF. The session will be then be closed
1.187 djm 1750: * by session_close_by_channel when the childs close their fds.
1751: */
1752: channel_register_cleanup(c->self, session_close_by_channel, 1);
1753:
1.5 markus 1754: /*
1755: * emulate a write failure with 'chan_write_failed', nobody will be
1756: * interested in data we write.
1757: * Note that we must not call 'chan_read_failed', since there could
1758: * be some more data waiting in the pipe.
1759: */
1.8 markus 1760: if (c->ostate != CHAN_OUTPUT_CLOSED)
1761: chan_write_failed(c);
1.2 markus 1762: }
1763:
1.130 provos 1764: void
1.85 markus 1765: session_close(Session *s)
1.2 markus 1766: {
1.182 djm 1767: u_int i;
1.173 djm 1768:
1.137 mpech 1769: debug("session_close: session %d pid %ld", s->self, (long)s->pid);
1.165 markus 1770: if (s->ttyfd != -1)
1.85 markus 1771: session_pty_cleanup(s);
1.2 markus 1772: if (s->term)
1773: xfree(s->term);
1774: if (s->display)
1775: xfree(s->display);
1.184 djm 1776: if (s->x11_chanids)
1777: xfree(s->x11_chanids);
1.118 stevesk 1778: if (s->auth_display)
1779: xfree(s->auth_display);
1.2 markus 1780: if (s->auth_data)
1781: xfree(s->auth_data);
1782: if (s->auth_proto)
1783: xfree(s->auth_proto);
1784: s->used = 0;
1.219 djm 1785: if (s->env != NULL) {
1786: for (i = 0; i < s->num_env; i++) {
1787: xfree(s->env[i].name);
1788: xfree(s->env[i].val);
1789: }
1790: xfree(s->env);
1.173 djm 1791: }
1.9 markus 1792: session_proctitle(s);
1.2 markus 1793: }
1794:
1795: void
1796: session_close_by_pid(pid_t pid, int status)
1797: {
1798: Session *s = session_by_pid(pid);
1799: if (s == NULL) {
1.137 mpech 1800: debug("session_close_by_pid: no session for pid %ld",
1801: (long)pid);
1.2 markus 1802: return;
1803: }
1804: if (s->chanid != -1)
1805: session_exit_message(s, status);
1.187 djm 1806: if (s->ttyfd != -1)
1807: session_pty_cleanup(s);
1.197 djm 1808: s->pid = 0;
1.98 markus 1809: }
1810:
1.2 markus 1811: /*
1812: * this is called when a channel dies before
1813: * the session 'child' itself dies
1814: */
1815: void
1816: session_close_by_channel(int id, void *arg)
1817: {
1818: Session *s = session_by_channel(id);
1.187 djm 1819: u_int i;
1.184 djm 1820:
1.2 markus 1821: if (s == NULL) {
1.107 markus 1822: debug("session_close_by_channel: no session for id %d", id);
1.2 markus 1823: return;
1824: }
1.137 mpech 1825: debug("session_close_by_channel: channel %d child %ld",
1826: id, (long)s->pid);
1.107 markus 1827: if (s->pid != 0) {
1828: debug("session_close_by_channel: channel %d: has child", id);
1.108 markus 1829: /*
1830: * delay detach of session, but release pty, since
1831: * the fd's to the child are already closed
1832: */
1.165 markus 1833: if (s->ttyfd != -1)
1.108 markus 1834: session_pty_cleanup(s);
1.107 markus 1835: return;
1836: }
1837: /* detach by removing callback */
1.2 markus 1838: channel_cancel_cleanup(s->chanid);
1.187 djm 1839:
1840: /* Close any X11 listeners associated with this session */
1841: if (s->x11_chanids != NULL) {
1842: for (i = 0; s->x11_chanids[i] != -1; i++) {
1843: session_close_x11(s->x11_chanids[i]);
1844: s->x11_chanids[i] = -1;
1845: }
1846: }
1847:
1.2 markus 1848: s->chanid = -1;
1.106 markus 1849: session_close(s);
1850: }
1851:
1852: void
1.130 provos 1853: session_destroy_all(void (*closefunc)(Session *))
1.106 markus 1854: {
1855: int i;
1.112 deraadt 1856: for (i = 0; i < MAX_SESSIONS; i++) {
1.106 markus 1857: Session *s = &sessions[i];
1.130 provos 1858: if (s->used) {
1859: if (closefunc != NULL)
1860: closefunc(s);
1861: else
1862: session_close(s);
1863: }
1.2 markus 1864: }
1.9 markus 1865: }
1866:
1.94 itojun 1867: static char *
1.9 markus 1868: session_tty_list(void)
1869: {
1870: static char buf[1024];
1871: int i;
1872: buf[0] = '\0';
1.112 deraadt 1873: for (i = 0; i < MAX_SESSIONS; i++) {
1.9 markus 1874: Session *s = &sessions[i];
1875: if (s->used && s->ttyfd != -1) {
1876: if (buf[0] != '\0')
1877: strlcat(buf, ",", sizeof buf);
1878: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1879: }
1880: }
1881: if (buf[0] == '\0')
1882: strlcpy(buf, "notty", sizeof buf);
1883: return buf;
1884: }
1885:
1886: void
1887: session_proctitle(Session *s)
1888: {
1889: if (s->pw == NULL)
1890: error("no user for session %d", s->self);
1891: else
1892: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.81 markus 1893: }
1894:
1895: int
1896: session_setup_x11fwd(Session *s)
1897: {
1898: struct stat st;
1.109 stevesk 1899: char display[512], auth_display[512];
1900: char hostname[MAXHOSTNAMELEN];
1.184 djm 1901: u_int i;
1.81 markus 1902:
1903: if (no_x11_forwarding_flag) {
1904: packet_send_debug("X11 forwarding disabled in user configuration file.");
1905: return 0;
1906: }
1907: if (!options.x11_forwarding) {
1908: debug("X11 forwarding disabled in server configuration file.");
1909: return 0;
1910: }
1911: if (!options.xauth_location ||
1912: (stat(options.xauth_location, &st) == -1)) {
1913: packet_send_debug("No xauth program; cannot forward with spoofing.");
1.91 markus 1914: return 0;
1915: }
1916: if (options.use_login) {
1917: packet_send_debug("X11 forwarding disabled; "
1918: "not compatible with UseLogin=yes.");
1.81 markus 1919: return 0;
1920: }
1.87 markus 1921: if (s->display != NULL) {
1.81 markus 1922: debug("X11 display already set.");
1923: return 0;
1924: }
1.140 deraadt 1925: if (x11_create_display_inet(options.x11_display_offset,
1926: options.x11_use_localhost, s->single_connection,
1.184 djm 1927: &s->display_number, &s->x11_chanids) == -1) {
1.87 markus 1928: debug("x11_create_display_inet failed.");
1.81 markus 1929: return 0;
1.184 djm 1930: }
1931: for (i = 0; s->x11_chanids[i] != -1; i++) {
1932: channel_register_cleanup(s->x11_chanids[i],
1.187 djm 1933: session_close_single_x11, 0);
1.81 markus 1934: }
1.109 stevesk 1935:
1936: /* Set up a suitable value for the DISPLAY variable. */
1937: if (gethostname(hostname, sizeof(hostname)) < 0)
1938: fatal("gethostname: %.100s", strerror(errno));
1939: /*
1940: * auth_display must be used as the displayname when the
1941: * authorization entry is added with xauth(1). This will be
1942: * different than the DISPLAY string for localhost displays.
1943: */
1.119 stevesk 1944: if (options.x11_use_localhost) {
1.140 deraadt 1945: snprintf(display, sizeof display, "localhost:%u.%u",
1.109 stevesk 1946: s->display_number, s->screen);
1.140 deraadt 1947: snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
1.118 stevesk 1948: s->display_number, s->screen);
1.109 stevesk 1949: s->display = xstrdup(display);
1.118 stevesk 1950: s->auth_display = xstrdup(auth_display);
1.109 stevesk 1951: } else {
1.140 deraadt 1952: snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
1.109 stevesk 1953: s->display_number, s->screen);
1954: s->display = xstrdup(display);
1.118 stevesk 1955: s->auth_display = xstrdup(display);
1.109 stevesk 1956: }
1957:
1.81 markus 1958: return 1;
1.2 markus 1959: }
1960:
1.94 itojun 1961: static void
1.49 markus 1962: do_authenticated2(Authctxt *authctxt)
1.2 markus 1963: {
1.97 markus 1964: server_loop2(authctxt);
1.165 markus 1965: }
1966:
1967: void
1968: do_cleanup(Authctxt *authctxt)
1969: {
1970: static int called = 0;
1971:
1972: debug("do_cleanup");
1973:
1974: /* no cleanup if we're in the child for login shell */
1975: if (is_child)
1976: return;
1977:
1978: /* avoid double cleanup */
1979: if (called)
1980: return;
1981: called = 1;
1982:
1.218 markus 1983: if (authctxt == NULL || !authctxt->authenticated)
1.165 markus 1984: return;
1985: #ifdef KRB5
1986: if (options.kerberos_ticket_cleanup &&
1987: authctxt->krb5_ctx)
1988: krb5_cleanup_proc(authctxt);
1.161 markus 1989: #endif
1.165 markus 1990:
1991: #ifdef GSSAPI
1992: if (compat20 && options.gss_cleanup_creds)
1993: ssh_gssapi_cleanup_creds();
1994: #endif
1995:
1996: /* remove agent socket */
1997: auth_sock_cleanup_proc(authctxt->pw);
1998:
1999: /*
2000: * Cleanup ptys/utmp only if privsep is disabled,
2001: * or if running in monitor.
2002: */
2003: if (!use_privsep || mm_is_monitor())
2004: session_destroy_all(session_pty_cleanup2);
1.1 markus 2005: }