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