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