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