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