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