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