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