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