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