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