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