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