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