Annotation of src/usr.bin/ssh/session.c, Revision 1.74
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.74 ! markus 36: RCSID("$OpenBSD: session.c,v 1.73 2001/04/16 08:19:31 djm 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.73 djm 96: void do_motd(void);
97: int check_quietlogin(Session *s, const char *command);
1.1 markus 98:
1.65 markus 99: void do_authenticated1(Authctxt *authctxt);
100: void do_authenticated2(Authctxt *authctxt);
101:
1.1 markus 102: /* import */
103: extern ServerOptions options;
104: extern char *__progname;
105: extern int log_stderr;
106: extern int debug_flag;
1.45 markus 107: extern u_int utmp_len;
1.21 markus 108: extern int startup_pipe;
1.74 ! markus 109: extern void destroy_sensitive_data(void);
1.21 markus 110:
1.1 markus 111: /* Local Xauthority file. */
112: static char *xauthfile;
113:
1.34 markus 114: /* original command from peer. */
1.53 stevesk 115: char *original_command = NULL;
1.34 markus 116:
1.1 markus 117: /* data */
118: #define MAX_SESSIONS 10
119: Session sessions[MAX_SESSIONS];
120:
1.28 millert 121: #ifdef HAVE_LOGIN_CAP
122: static login_cap_t *lc;
123: #endif
124:
1.65 markus 125: void
126: do_authenticated(Authctxt *authctxt)
127: {
128: /*
129: * Cancel the alarm we set to limit the time taken for
130: * authentication.
131: */
132: alarm(0);
133: if (startup_pipe != -1) {
134: close(startup_pipe);
135: startup_pipe = -1;
136: }
137: #ifdef HAVE_LOGIN_CAP
138: if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) {
139: error("unable to get login class");
140: return;
141: }
1.74 ! markus 142: #ifdef BSD_AUTH
! 143: if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) {
! 144: packet_disconnect("Approval failure for %s",
! 145: authctxt->pw->pw_name);
! 146: }
! 147: #endif
1.65 markus 148: #endif
149: /* setup the channel layer */
150: if (!no_port_forwarding_flag && options.allow_tcp_forwarding)
151: channel_permit_all_opens();
152:
153: if (compat20)
154: do_authenticated2(authctxt);
155: else
156: do_authenticated1(authctxt);
157: }
158:
1.1 markus 159: /*
160: * Remove local Xauthority file.
161: */
162: void
163: xauthfile_cleanup_proc(void *ignore)
164: {
165: debug("xauthfile_cleanup_proc called");
166:
167: if (xauthfile != NULL) {
168: char *p;
169: unlink(xauthfile);
170: p = strrchr(xauthfile, '/');
171: if (p != NULL) {
172: *p = '\0';
173: rmdir(xauthfile);
174: }
175: xfree(xauthfile);
176: xauthfile = NULL;
177: }
178: }
179:
180: /*
181: * Function to perform cleanup if we get aborted abnormally (e.g., due to a
182: * dropped connection).
183: */
1.4 markus 184: void
1.1 markus 185: pty_cleanup_proc(void *session)
186: {
187: Session *s=session;
188: if (s == NULL)
189: fatal("pty_cleanup_proc: no session");
190: debug("pty_cleanup_proc: %s", s->tty);
191:
192: if (s->pid != 0) {
193: /* Record that the user has logged out. */
194: record_logout(s->pid, s->tty);
195: }
196:
197: /* Release the pseudo-tty. */
198: pty_release(s->tty);
199: }
200:
201: /*
202: * Prepares for an interactive session. This is called after the user has
203: * been successfully authenticated. During this message exchange, pseudo
204: * terminals are allocated, X11, TCP/IP, and authentication agent forwardings
205: * are requested, etc.
206: */
1.4 markus 207: void
1.65 markus 208: do_authenticated1(Authctxt *authctxt)
1.1 markus 209: {
210: Session *s;
1.65 markus 211: char *command;
212: int success, type, fd, n_bytes, plen, screen_flag, have_pty = 0;
1.1 markus 213: int compression_level = 0, enable_compression_after_reply = 0;
1.45 markus 214: u_int proto_len, data_len, dlen;
1.1 markus 215:
1.61 markus 216: s = session_new();
1.65 markus 217: s->pw = authctxt->pw;
1.28 millert 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 (;;) {
1.65 markus 224: success = 0;
1.1 markus 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);
1.65 markus 261: pty_setowner(s->pw, s->tty);
1.1 markus 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);
1.71 markus 336: temporarily_use_uid(s->pw);
1.1 markus 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.65 markus 361: success = auth_input_request_forwarding(s->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.65 markus 374: channel_input_port_forward_request(s->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)
1.63 markus 399: do_exec_pty(s, command);
1.1 markus 400: else
1.63 markus 401: do_exec_no_pty(s, command);
1.1 markus 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.63 markus 435: do_exec_no_pty(Session *s, const char *command)
1.1 markus 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). */
1.60 markus 508: do_child(s, command);
1.1 markus 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.64 markus 523: session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]);
1.2 markus 524: } else {
525: /* Enter the interactive session. */
526: server_loop(pid, pin[1], pout[0], perr[0]);
1.64 markus 527: /* server_loop has closed pin[1], pout[0], and perr[0]. */
1.2 markus 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.64 markus 539: session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[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.63 markus 554: do_exec_pty(Session *s, const char *command)
1.1 markus 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.60 markus 595: do_child(s, command);
1.1 markus 596: /* NOTREACHED */
597: }
598: if (pid < 0)
599: packet_disconnect("fork failed: %.100s", strerror(errno));
600: s->pid = pid;
601:
602: /* Parent. Close the slave side of the pseudo tty. */
603: close(ttyfd);
604:
605: /*
606: * Create another descriptor of the pty master side for use as the
607: * standard input. We could use the original descriptor, but this
608: * simplifies code in server_loop. The descriptor is bidirectional.
609: */
610: fdout = dup(ptyfd);
611: if (fdout < 0)
612: packet_disconnect("dup #1 failed: %.100s", strerror(errno));
613:
614: /* we keep a reference to the pty master */
615: ptymaster = dup(ptyfd);
616: if (ptymaster < 0)
617: packet_disconnect("dup #2 failed: %.100s", strerror(errno));
618: s->ptymaster = ptymaster;
619:
620: /* Enter interactive session. */
1.47 markus 621: packet_set_interactive(1);
1.2 markus 622: if (compat20) {
623: session_set_fds(s, ptyfd, fdout, -1);
624: } else {
625: server_loop(pid, ptyfd, fdout, -1);
626: /* server_loop _has_ closed ptyfd and fdout. */
627: session_pty_cleanup(s);
1.24 markus 628: }
629: }
630:
631: /* administrative, login(1)-like work */
632: void
1.41 markus 633: do_login(Session *s, const char *command)
1.24 markus 634: {
635: char *time_string;
1.35 markus 636: char hostname[MAXHOSTNAMELEN];
1.24 markus 637: socklen_t fromlen;
638: struct sockaddr_storage from;
639: time_t last_login_time;
640: struct passwd * pw = s->pw;
641: pid_t pid = getpid();
642:
643: /*
644: * Get IP address of client. If the connection is not a socket, let
645: * the address be 0.0.0.0.
646: */
647: memset(&from, 0, sizeof(from));
648: if (packet_connection_is_on_socket()) {
649: fromlen = sizeof(from);
650: if (getpeername(packet_get_connection_in(),
651: (struct sockaddr *) & from, &fromlen) < 0) {
652: debug("getpeername: %.100s", strerror(errno));
653: fatal_cleanup();
654: }
655: }
1.25 markus 656:
1.35 markus 657: /* Get the time and hostname when the user last logged in. */
1.69 stevesk 658: if (options.print_lastlog) {
659: hostname[0] = '\0';
660: last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name,
661: hostname, sizeof(hostname));
662: }
1.35 markus 663:
1.24 markus 664: /* Record that there was a login on that tty from the remote host. */
665: record_login(pid, s->tty, pw->pw_name, pw->pw_uid,
1.70 stevesk 666: get_remote_name_or_ip(utmp_len, options.reverse_mapping_check),
667: (struct sockaddr *)&from);
1.24 markus 668:
1.73 djm 669: if (check_quietlogin(s, command))
1.24 markus 670: return;
1.73 djm 671:
1.69 stevesk 672: if (options.print_lastlog && last_login_time != 0) {
1.24 markus 673: time_string = ctime(&last_login_time);
674: if (strchr(time_string, '\n'))
675: *strchr(time_string, '\n') = 0;
1.40 markus 676: if (strcmp(hostname, "") == 0)
1.24 markus 677: printf("Last login: %s\r\n", time_string);
678: else
1.36 markus 679: printf("Last login: %s from %s\r\n", time_string, hostname);
1.24 markus 680: }
1.73 djm 681:
682: do_motd();
683: }
684:
685: /*
686: * Display the message of the day.
687: */
688: void
689: do_motd(void)
690: {
691: FILE *f;
692: char buf[256];
693:
1.24 markus 694: if (options.print_motd) {
1.28 millert 695: #ifdef HAVE_LOGIN_CAP
696: f = fopen(login_getcapstr(lc, "welcome", "/etc/motd",
697: "/etc/motd"), "r");
698: #else
1.24 markus 699: f = fopen("/etc/motd", "r");
1.28 millert 700: #endif
1.24 markus 701: if (f) {
702: while (fgets(buf, sizeof(buf), f))
703: fputs(buf, stdout);
704: fclose(f);
705: }
1.2 markus 706: }
1.73 djm 707: }
708:
709:
710: /*
711: * Check for quiet login, either .hushlogin or command given.
712: */
713: int
714: check_quietlogin(Session *s, const char *command)
715: {
716: char buf[256];
717: struct passwd * pw = s->pw;
718: struct stat st;
719:
720: /* Return 1 if .hushlogin exists or a command given. */
721: if (command != NULL)
722: return 1;
723: snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir);
724: #ifdef HAVE_LOGIN_CAP
725: if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0)
726: return 1;
727: #else
728: if (stat(buf, &st) >= 0)
729: return 1;
730: #endif
731: return 0;
1.1 markus 732: }
733:
734: /*
735: * Sets the value of the given variable in the environment. If the variable
736: * already exists, its value is overriden.
737: */
1.4 markus 738: void
1.45 markus 739: child_set_env(char ***envp, u_int *envsizep, const char *name,
1.1 markus 740: const char *value)
741: {
1.45 markus 742: u_int i, namelen;
1.1 markus 743: char **env;
744:
745: /*
746: * Find the slot where the value should be stored. If the variable
747: * already exists, we reuse the slot; otherwise we append a new slot
748: * at the end of the array, expanding if necessary.
749: */
750: env = *envp;
751: namelen = strlen(name);
752: for (i = 0; env[i]; i++)
753: if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
754: break;
755: if (env[i]) {
756: /* Reuse the slot. */
757: xfree(env[i]);
758: } else {
759: /* New variable. Expand if necessary. */
760: if (i >= (*envsizep) - 1) {
761: (*envsizep) += 50;
762: env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *));
763: }
764: /* Need to set the NULL pointer at end of array beyond the new slot. */
765: env[i + 1] = NULL;
766: }
767:
768: /* Allocate space and format the variable in the appropriate slot. */
769: env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
770: snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
771: }
772:
773: /*
774: * Reads environment variables from the given file and adds/overrides them
775: * into the environment. If the file does not exist, this does nothing.
776: * Otherwise, it must consist of empty lines, comments (line starts with '#')
777: * and assignments of the form name=value. No other forms are allowed.
778: */
1.4 markus 779: void
1.45 markus 780: read_environment_file(char ***env, u_int *envsize,
1.1 markus 781: const char *filename)
782: {
783: FILE *f;
784: char buf[4096];
785: char *cp, *value;
786:
787: f = fopen(filename, "r");
788: if (!f)
789: return;
790:
791: while (fgets(buf, sizeof(buf), f)) {
792: for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
793: ;
794: if (!*cp || *cp == '#' || *cp == '\n')
795: continue;
796: if (strchr(cp, '\n'))
797: *strchr(cp, '\n') = '\0';
798: value = strchr(cp, '=');
799: if (value == NULL) {
800: fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf);
801: continue;
802: }
1.14 deraadt 803: /*
804: * Replace the equals sign by nul, and advance value to
805: * the value string.
806: */
1.1 markus 807: *value = '\0';
808: value++;
809: child_set_env(env, envsize, cp, value);
810: }
811: fclose(f);
812: }
813:
814: /*
815: * Performs common processing for the child, such as setting up the
816: * environment, closing extra file descriptors, setting the user and group
817: * ids, and executing the command or shell.
818: */
1.4 markus 819: void
1.60 markus 820: do_child(Session *s, const char *command)
1.1 markus 821: {
1.32 markus 822: const char *shell, *hostname = NULL, *cp = NULL;
1.60 markus 823: struct passwd * pw = s->pw;
1.1 markus 824: char buf[256];
1.16 markus 825: char cmd[1024];
1.27 millert 826: FILE *f = NULL;
1.45 markus 827: u_int envsize, i;
1.1 markus 828: char **env;
829: extern char **environ;
830: struct stat st;
831: char *argv[10];
1.60 markus 832: int do_xauth = s->auth_proto != NULL && s->auth_data != NULL;
1.17 markus 833:
1.74 ! markus 834: /* remove hostkey from the child's memory */
! 835: destroy_sensitive_data();
! 836:
1.17 markus 837: /* login(1) is only called if we execute the login shell */
838: if (options.use_login && command != NULL)
839: options.use_login = 0;
1.1 markus 840:
1.27 millert 841: if (!options.use_login) {
1.28 millert 842: #ifdef HAVE_LOGIN_CAP
843: if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
844: f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
845: _PATH_NOLOGIN), "r");
846: #else
1.27 millert 847: if (pw->pw_uid)
848: f = fopen(_PATH_NOLOGIN, "r");
1.28 millert 849: #endif
1.27 millert 850: if (f) {
851: /* /etc/nologin exists. Print its contents and exit. */
852: while (fgets(buf, sizeof(buf), f))
853: fputs(buf, stderr);
854: fclose(f);
1.1 markus 855: exit(254);
1.27 millert 856: }
1.1 markus 857: }
1.28 millert 858: /* Set login name, uid, gid, and groups. */
1.1 markus 859: /* Login(1) does this as well, and it needs uid 0 for the "-h"
860: switch, so we let login(1) to this for us. */
861: if (!options.use_login) {
862: if (getuid() == 0 || geteuid() == 0) {
1.28 millert 863: #ifdef HAVE_LOGIN_CAP
864: if (setusercontext(lc, pw, pw->pw_uid,
865: (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
866: perror("unable to set user context");
867: exit(1);
868: }
869: #else
870: if (setlogin(pw->pw_name) < 0)
871: error("setlogin failed: %s", strerror(errno));
1.1 markus 872: if (setgid(pw->pw_gid) < 0) {
873: perror("setgid");
874: exit(1);
875: }
876: /* Initialize the group list. */
877: if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
878: perror("initgroups");
879: exit(1);
880: }
881: endgrent();
882:
883: /* Permanently switch to the desired uid. */
1.71 markus 884: permanently_set_uid(pw);
1.28 millert 885: #endif
1.1 markus 886: }
887: if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1.31 deraadt 888: fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1.1 markus 889: }
890: /*
891: * Get the shell from the password data. An empty shell field is
892: * legal, and means /bin/sh.
893: */
894: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.28 millert 895: #ifdef HAVE_LOGIN_CAP
896: shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
897: #endif
1.1 markus 898:
899: #ifdef AFS
900: /* Try to get AFS tokens for the local cell. */
901: if (k_hasafs()) {
902: char cell[64];
903:
904: if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)
905: krb_afslog(cell, 0);
906:
907: krb_afslog(0, 0);
908: }
909: #endif /* AFS */
910:
911: /* Initialize the environment. */
912: envsize = 100;
913: env = xmalloc(envsize * sizeof(char *));
914: env[0] = NULL;
915:
916: if (!options.use_login) {
917: /* Set basic environment. */
918: child_set_env(&env, &envsize, "USER", pw->pw_name);
919: child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
920: child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1.28 millert 921: #ifdef HAVE_LOGIN_CAP
1.29 millert 922: (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH);
923: child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1.28 millert 924: #else
1.29 millert 925: child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1.28 millert 926: #endif
1.1 markus 927:
928: snprintf(buf, sizeof buf, "%.200s/%.50s",
929: _PATH_MAILDIR, pw->pw_name);
930: child_set_env(&env, &envsize, "MAIL", buf);
931:
932: /* Normal systems set SHELL by default. */
933: child_set_env(&env, &envsize, "SHELL", shell);
934: }
935: if (getenv("TZ"))
936: child_set_env(&env, &envsize, "TZ", getenv("TZ"));
937:
938: /* Set custom environment options from RSA authentication. */
939: while (custom_environment) {
940: struct envstring *ce = custom_environment;
941: char *s = ce->s;
942: int i;
943: for (i = 0; s[i] != '=' && s[i]; i++);
944: if (s[i] == '=') {
945: s[i] = 0;
946: child_set_env(&env, &envsize, s, s + i + 1);
947: }
948: custom_environment = ce->next;
949: xfree(ce->s);
950: xfree(ce);
951: }
952:
953: snprintf(buf, sizeof buf, "%.50s %d %d",
954: get_remote_ipaddr(), get_remote_port(), get_local_port());
955: child_set_env(&env, &envsize, "SSH_CLIENT", buf);
956:
1.60 markus 957: if (s->ttyfd != -1)
958: child_set_env(&env, &envsize, "SSH_TTY", s->tty);
959: if (s->term)
960: child_set_env(&env, &envsize, "TERM", s->term);
961: if (s->display)
962: child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34 markus 963: if (original_command)
964: child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
965: original_command);
1.1 markus 966:
967: #ifdef KRB4
968: {
969: extern char *ticket;
970:
971: if (ticket)
972: child_set_env(&env, &envsize, "KRBTKFILE", ticket);
973: }
974: #endif /* KRB4 */
975:
976: if (xauthfile)
977: child_set_env(&env, &envsize, "XAUTHORITY", xauthfile);
978: if (auth_get_socket_name() != NULL)
979: child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
980: auth_get_socket_name());
981:
982: /* read $HOME/.ssh/environment. */
983: if (!options.use_login) {
1.14 deraadt 984: snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
985: pw->pw_dir);
1.1 markus 986: read_environment_file(&env, &envsize, buf);
987: }
988: if (debug_flag) {
989: /* dump the environment */
990: fprintf(stderr, "Environment:\n");
991: for (i = 0; env[i]; i++)
992: fprintf(stderr, " %.200s\n", env[i]);
993: }
1.26 millert 994: /* we have to stash the hostname before we close our socket. */
995: if (options.use_login)
1.70 stevesk 996: hostname = get_remote_name_or_ip(utmp_len,
997: options.reverse_mapping_check);
1.1 markus 998: /*
999: * Close the connection descriptors; note that this is the child, and
1000: * the server will still have the socket open, and it is important
1001: * that we do not shutdown it. Note that the descriptors cannot be
1002: * closed before building the environment, as we call
1003: * get_remote_ipaddr there.
1004: */
1005: if (packet_get_connection_in() == packet_get_connection_out())
1006: close(packet_get_connection_in());
1007: else {
1008: close(packet_get_connection_in());
1009: close(packet_get_connection_out());
1010: }
1011: /*
1012: * Close all descriptors related to channels. They will still remain
1013: * open in the parent.
1014: */
1015: /* XXX better use close-on-exec? -markus */
1016: channel_close_all();
1017:
1018: /*
1019: * Close any extra file descriptors. Note that there may still be
1020: * descriptors left by system functions. They will be closed later.
1021: */
1022: endpwent();
1023:
1024: /*
1025: * Close any extra open file descriptors so that we don\'t have them
1026: * hanging around in clients. Note that we want to do this after
1027: * initgroups, because at least on Solaris 2.3 it leaves file
1028: * descriptors open.
1029: */
1030: for (i = 3; i < 64; i++)
1031: close(i);
1032:
1033: /* Change current directory to the user\'s home directory. */
1.28 millert 1034: if (chdir(pw->pw_dir) < 0) {
1.1 markus 1035: fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1036: pw->pw_dir, strerror(errno));
1.28 millert 1037: #ifdef HAVE_LOGIN_CAP
1038: if (login_getcapbool(lc, "requirehome", 0))
1039: exit(1);
1040: #endif
1041: }
1.1 markus 1042:
1043: /*
1044: * Must take new environment into use so that .ssh/rc, /etc/sshrc and
1045: * xauth are run in the proper environment.
1046: */
1047: environ = env;
1048:
1049: /*
1050: * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first
1051: * in this order).
1052: */
1053: if (!options.use_login) {
1.74 ! markus 1054: /* ignore _PATH_SSH_USER_RC for subsystems */
! 1055: if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
1.1 markus 1056: if (debug_flag)
1.60 markus 1057: fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1058: _PATH_SSH_USER_RC);
1.50 markus 1059: f = popen(_PATH_BSHELL " " _PATH_SSH_USER_RC, "w");
1.1 markus 1060: if (f) {
1.60 markus 1061: if (do_xauth)
1062: fprintf(f, "%s %s\n", s->auth_proto,
1063: s->auth_data);
1.1 markus 1064: pclose(f);
1065: } else
1.60 markus 1066: fprintf(stderr, "Could not run %s\n",
1067: _PATH_SSH_USER_RC);
1.50 markus 1068: } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1.1 markus 1069: if (debug_flag)
1.60 markus 1070: fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1071: _PATH_SSH_SYSTEM_RC);
1.50 markus 1072: f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1.1 markus 1073: if (f) {
1.60 markus 1074: if (do_xauth)
1075: fprintf(f, "%s %s\n", s->auth_proto,
1076: s->auth_data);
1.1 markus 1077: pclose(f);
1078: } else
1.60 markus 1079: fprintf(stderr, "Could not run %s\n",
1080: _PATH_SSH_SYSTEM_RC);
1081: } else if (do_xauth && options.xauth_location != NULL) {
1.1 markus 1082: /* Add authority data to .Xauthority if appropriate. */
1.60 markus 1083: char *screen = strchr(s->display, ':');
1084:
1085: if (debug_flag) {
1086: fprintf(stderr,
1087: "Running %.100s add "
1088: "%.100s %.100s %.100s\n",
1089: options.xauth_location, s->display,
1090: s->auth_proto, s->auth_data);
1091: if (screen != NULL)
1.14 deraadt 1092: fprintf(stderr,
1.60 markus 1093: "Adding %.*s/unix%s %s %s\n",
1094: (int)(screen - s->display),
1095: s->display, screen,
1096: s->auth_proto, s->auth_data);
1097: }
1098: snprintf(cmd, sizeof cmd, "%s -q -",
1099: options.xauth_location);
1100: f = popen(cmd, "w");
1101: if (f) {
1102: fprintf(f, "add %s %s %s\n", s->display,
1103: s->auth_proto, s->auth_data);
1104: if (screen != NULL)
1105: fprintf(f, "add %.*s/unix%s %s %s\n",
1106: (int)(screen - s->display),
1107: s->display, screen,
1108: s->auth_proto,
1109: s->auth_data);
1110: pclose(f);
1111: } else {
1112: fprintf(stderr, "Could not run %s\n",
1113: cmd);
1.1 markus 1114: }
1115: }
1116: /* Get the last component of the shell name. */
1117: cp = strrchr(shell, '/');
1118: if (cp)
1119: cp++;
1120: else
1121: cp = shell;
1122: }
1.67 markus 1123:
1124: /* restore SIGPIPE for child */
1125: signal(SIGPIPE, SIG_DFL);
1126:
1.1 markus 1127: /*
1128: * If we have no command, execute the shell. In this case, the shell
1129: * name to be passed in argv[0] is preceded by '-' to indicate that
1130: * this is a login shell.
1131: */
1132: if (!command) {
1133: if (!options.use_login) {
1134: char buf[256];
1135:
1136: /*
1137: * Check for mail if we have a tty and it was enabled
1138: * in server options.
1139: */
1.60 markus 1140: if (s->ttyfd != -1 && options.check_mail) {
1.1 markus 1141: char *mailbox;
1142: struct stat mailstat;
1.60 markus 1143:
1.1 markus 1144: mailbox = getenv("MAIL");
1145: if (mailbox != NULL) {
1.14 deraadt 1146: if (stat(mailbox, &mailstat) != 0 ||
1147: mailstat.st_size == 0)
1.1 markus 1148: printf("No mail.\n");
1149: else if (mailstat.st_mtime < mailstat.st_atime)
1150: printf("You have mail.\n");
1151: else
1152: printf("You have new mail.\n");
1153: }
1154: }
1155: /* Start the shell. Set initial character to '-'. */
1156: buf[0] = '-';
1157: strncpy(buf + 1, cp, sizeof(buf) - 1);
1158: buf[sizeof(buf) - 1] = 0;
1159:
1160: /* Execute the shell. */
1161: argv[0] = buf;
1162: argv[1] = NULL;
1163: execve(shell, argv, env);
1164:
1165: /* Executing the shell failed. */
1166: perror(shell);
1167: exit(1);
1168:
1169: } else {
1170: /* Launch login(1). */
1171:
1.26 millert 1172: execl("/usr/bin/login", "login", "-h", hostname,
1.25 markus 1173: "-p", "-f", "--", pw->pw_name, NULL);
1.1 markus 1174:
1175: /* Login couldn't be executed, die. */
1176:
1177: perror("login");
1178: exit(1);
1179: }
1180: }
1181: /*
1182: * Execute the command using the user's shell. This uses the -c
1183: * option to execute the command.
1184: */
1185: argv[0] = (char *) cp;
1186: argv[1] = "-c";
1187: argv[2] = (char *) command;
1188: argv[3] = NULL;
1189: execve(shell, argv, env);
1190: perror(shell);
1191: exit(1);
1192: }
1193:
1194: Session *
1195: session_new(void)
1196: {
1197: int i;
1198: static int did_init = 0;
1199: if (!did_init) {
1200: debug("session_new: init");
1201: for(i = 0; i < MAX_SESSIONS; i++) {
1202: sessions[i].used = 0;
1203: sessions[i].self = i;
1204: }
1205: did_init = 1;
1206: }
1207: for(i = 0; i < MAX_SESSIONS; i++) {
1208: Session *s = &sessions[i];
1209: if (! s->used) {
1.68 djm 1210: memset(s, 0, sizeof(*s));
1.1 markus 1211: s->chanid = -1;
1212: s->ptyfd = -1;
1213: s->ttyfd = -1;
1214: s->used = 1;
1215: debug("session_new: session %d", i);
1216: return s;
1217: }
1218: }
1219: return NULL;
1220: }
1221:
1222: void
1223: session_dump(void)
1224: {
1225: int i;
1226: for(i = 0; i < MAX_SESSIONS; i++) {
1227: Session *s = &sessions[i];
1228: debug("dump: used %d session %d %p channel %d pid %d",
1229: s->used,
1230: s->self,
1231: s,
1232: s->chanid,
1233: s->pid);
1234: }
1235: }
1236:
1.2 markus 1237: int
1238: session_open(int chanid)
1239: {
1240: Session *s = session_new();
1241: debug("session_open: channel %d", chanid);
1242: if (s == NULL) {
1243: error("no more sessions");
1244: return 0;
1245: }
1.7 markus 1246: s->pw = auth_get_user();
1247: if (s->pw == NULL)
1.54 stevesk 1248: fatal("no user for session %d", s->self);
1.2 markus 1249: debug("session_open: session %d: link with channel %d", s->self, chanid);
1250: s->chanid = chanid;
1251: return 1;
1252: }
1253:
1254: Session *
1255: session_by_channel(int id)
1256: {
1257: int i;
1258: for(i = 0; i < MAX_SESSIONS; i++) {
1259: Session *s = &sessions[i];
1260: if (s->used && s->chanid == id) {
1261: debug("session_by_channel: session %d channel %d", i, id);
1262: return s;
1263: }
1264: }
1265: debug("session_by_channel: unknown channel %d", id);
1266: session_dump();
1267: return NULL;
1268: }
1269:
1270: Session *
1271: session_by_pid(pid_t pid)
1272: {
1273: int i;
1274: debug("session_by_pid: pid %d", pid);
1275: for(i = 0; i < MAX_SESSIONS; i++) {
1276: Session *s = &sessions[i];
1277: if (s->used && s->pid == pid)
1278: return s;
1279: }
1280: error("session_by_pid: unknown pid %d", pid);
1281: session_dump();
1282: return NULL;
1283: }
1284:
1285: int
1286: session_window_change_req(Session *s)
1287: {
1288: s->col = packet_get_int();
1289: s->row = packet_get_int();
1290: s->xpixel = packet_get_int();
1291: s->ypixel = packet_get_int();
1.3 markus 1292: packet_done();
1.2 markus 1293: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1294: return 1;
1295: }
1296:
1297: int
1298: session_pty_req(Session *s)
1299: {
1.45 markus 1300: u_int len;
1.72 stevesk 1301: int n_bytes;
1.2 markus 1302:
1.19 markus 1303: if (no_pty_flag)
1304: return 0;
1.2 markus 1305: if (s->ttyfd != -1)
1.3 markus 1306: return 0;
1.2 markus 1307: s->term = packet_get_string(&len);
1308: s->col = packet_get_int();
1309: s->row = packet_get_int();
1310: s->xpixel = packet_get_int();
1311: s->ypixel = packet_get_int();
1312:
1313: if (strcmp(s->term, "") == 0) {
1314: xfree(s->term);
1315: s->term = NULL;
1316: }
1317: /* Allocate a pty and open it. */
1318: if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1319: xfree(s->term);
1320: s->term = NULL;
1321: s->ptyfd = -1;
1322: s->ttyfd = -1;
1323: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1324: return 0;
1.2 markus 1325: }
1326: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1327: /*
1328: * Add a cleanup function to clear the utmp entry and record logout
1329: * time in case we call fatal() (e.g., the connection gets closed).
1330: */
1331: fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1332: pty_setowner(s->pw, s->tty);
1333: /* Get window size from the packet. */
1334: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1335:
1.72 stevesk 1336: /* Get tty modes from the packet. */
1337: tty_parse_modes(s->ttyfd, &n_bytes);
1338: packet_done();
1339:
1.9 markus 1340: session_proctitle(s);
1341:
1.2 markus 1342: return 1;
1343: }
1344:
1.7 markus 1345: int
1346: session_subsystem_req(Session *s)
1347: {
1.45 markus 1348: u_int len;
1.7 markus 1349: int success = 0;
1350: char *subsys = packet_get_string(&len);
1.18 jakob 1351: int i;
1.7 markus 1352:
1353: packet_done();
1354: log("subsystem request for %s", subsys);
1.18 jakob 1355:
1356: for (i = 0; i < options.num_subsystems; i++) {
1357: if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1358: debug("subsystem: exec() %s", options.subsystem_command[i]);
1.64 markus 1359: s->is_subsystem = 1;
1.63 markus 1360: do_exec_no_pty(s, options.subsystem_command[i]);
1.18 jakob 1361: success = 1;
1362: }
1363: }
1364:
1365: if (!success)
1366: log("subsystem request for %s failed, subsystem not found", subsys);
1.7 markus 1367:
1368: xfree(subsys);
1369: return success;
1370: }
1371:
1372: int
1373: session_x11_req(Session *s)
1374: {
1.33 markus 1375: int fd;
1.22 deraadt 1376: if (no_x11_forwarding_flag) {
1.19 markus 1377: debug("X11 forwarding disabled in user configuration file.");
1378: return 0;
1379: }
1.7 markus 1380: if (!options.x11_forwarding) {
1381: debug("X11 forwarding disabled in server configuration file.");
1382: return 0;
1383: }
1384: if (xauthfile != NULL) {
1385: debug("X11 fwd already started.");
1386: return 0;
1387: }
1388:
1389: debug("Received request for X11 forwarding with auth spoofing.");
1390: if (s->display != NULL)
1391: packet_disconnect("Protocol error: X11 display already set.");
1392:
1393: s->single_connection = packet_get_char();
1394: s->auth_proto = packet_get_string(NULL);
1395: s->auth_data = packet_get_string(NULL);
1396: s->screen = packet_get_int();
1397: packet_done();
1398:
1399: s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1400: if (s->display == NULL) {
1401: xfree(s->auth_proto);
1402: xfree(s->auth_data);
1403: return 0;
1404: }
1405: xauthfile = xmalloc(MAXPATHLEN);
1406: strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1.71 markus 1407: temporarily_use_uid(s->pw);
1.7 markus 1408: if (mkdtemp(xauthfile) == NULL) {
1409: restore_uid();
1410: error("private X11 dir: mkdtemp %s failed: %s",
1411: xauthfile, strerror(errno));
1412: xfree(xauthfile);
1413: xauthfile = NULL;
1414: xfree(s->auth_proto);
1415: xfree(s->auth_data);
1416: /* XXXX remove listening channels */
1417: return 0;
1418: }
1419: strlcat(xauthfile, "/cookies", MAXPATHLEN);
1.33 markus 1420: fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1421: if (fd >= 0)
1422: close(fd);
1.7 markus 1423: restore_uid();
1424: fatal_add_cleanup(xauthfile_cleanup_proc, s);
1425: return 1;
1426: }
1427:
1.19 markus 1428: int
1429: session_shell_req(Session *s)
1430: {
1431: /* if forced_command == NULL, the shell is execed */
1432: char *shell = forced_command;
1433: packet_done();
1434: if (s->ttyfd == -1)
1.63 markus 1435: do_exec_no_pty(s, shell);
1.19 markus 1436: else
1.63 markus 1437: do_exec_pty(s, shell);
1.19 markus 1438: return 1;
1439: }
1440:
1441: int
1442: session_exec_req(Session *s)
1443: {
1.45 markus 1444: u_int len;
1.19 markus 1445: char *command = packet_get_string(&len);
1446: packet_done();
1447: if (forced_command) {
1.34 markus 1448: original_command = command;
1.19 markus 1449: command = forced_command;
1450: debug("Forced command '%.500s'", forced_command);
1451: }
1452: if (s->ttyfd == -1)
1.63 markus 1453: do_exec_no_pty(s, command);
1.19 markus 1454: else
1.63 markus 1455: do_exec_pty(s, command);
1.19 markus 1456: if (forced_command == NULL)
1457: xfree(command);
1458: return 1;
1459: }
1460:
1.43 markus 1461: int
1462: session_auth_agent_req(Session *s)
1463: {
1464: static int called = 0;
1465: packet_done();
1.44 markus 1466: if (no_agent_forwarding_flag) {
1467: debug("session_auth_agent_req: no_agent_forwarding_flag");
1468: return 0;
1469: }
1.43 markus 1470: if (called) {
1471: return 0;
1472: } else {
1473: called = 1;
1474: return auth_input_request_forwarding(s->pw);
1475: }
1476: }
1477:
1.2 markus 1478: void
1479: session_input_channel_req(int id, void *arg)
1480: {
1.45 markus 1481: u_int len;
1.2 markus 1482: int reply;
1483: int success = 0;
1484: char *rtype;
1485: Session *s;
1486: Channel *c;
1487:
1488: rtype = packet_get_string(&len);
1489: reply = packet_get_char();
1490:
1491: s = session_by_channel(id);
1492: if (s == NULL)
1493: fatal("session_input_channel_req: channel %d: no session", id);
1494: c = channel_lookup(id);
1495: if (c == NULL)
1496: fatal("session_input_channel_req: channel %d: bad channel", id);
1497:
1498: debug("session_input_channel_req: session %d channel %d request %s reply %d",
1499: s->self, id, rtype, reply);
1500:
1501: /*
1.64 markus 1502: * a session is in LARVAL state until a shell, a command
1503: * or a subsystem is executed
1.2 markus 1504: */
1505: if (c->type == SSH_CHANNEL_LARVAL) {
1506: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1507: success = session_shell_req(s);
1.2 markus 1508: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1509: success = session_exec_req(s);
1.2 markus 1510: } else if (strcmp(rtype, "pty-req") == 0) {
1.3 markus 1511: success = session_pty_req(s);
1.7 markus 1512: } else if (strcmp(rtype, "x11-req") == 0) {
1513: success = session_x11_req(s);
1.43 markus 1514: } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1515: success = session_auth_agent_req(s);
1.7 markus 1516: } else if (strcmp(rtype, "subsystem") == 0) {
1517: success = session_subsystem_req(s);
1.2 markus 1518: }
1519: }
1520: if (strcmp(rtype, "window-change") == 0) {
1521: success = session_window_change_req(s);
1522: }
1523:
1524: if (reply) {
1525: packet_start(success ?
1526: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1527: packet_put_int(c->remote_id);
1528: packet_send();
1529: }
1530: xfree(rtype);
1531: }
1532:
1533: void
1534: session_set_fds(Session *s, int fdin, int fdout, int fderr)
1535: {
1536: if (!compat20)
1537: fatal("session_set_fds: called for proto != 2.0");
1538: /*
1539: * now that have a child and a pipe to the child,
1540: * we can activate our channel and register the fd's
1541: */
1542: if (s->chanid == -1)
1543: fatal("no channel for session %d", s->self);
1544: channel_set_fds(s->chanid,
1545: fdout, fdin, fderr,
1.42 markus 1546: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1547: 1);
1.2 markus 1548: }
1549:
1.1 markus 1550: void
1551: session_pty_cleanup(Session *s)
1552: {
1553: if (s == NULL || s->ttyfd == -1)
1554: return;
1555:
1.54 stevesk 1556: debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1 markus 1557:
1558: /* Cancel the cleanup function. */
1559: fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1560:
1561: /* Record that the user has logged out. */
1562: record_logout(s->pid, s->tty);
1563:
1564: /* Release the pseudo-tty. */
1565: pty_release(s->tty);
1566:
1567: /*
1568: * Close the server side of the socket pairs. We must do this after
1569: * the pty cleanup, so that another process doesn't get this pty
1570: * while we're still cleaning up.
1571: */
1572: if (close(s->ptymaster) < 0)
1573: error("close(s->ptymaster): %s", strerror(errno));
1.2 markus 1574: }
1575:
1576: void
1577: session_exit_message(Session *s, int status)
1578: {
1579: Channel *c;
1580: if (s == NULL)
1581: fatal("session_close: no session");
1582: c = channel_lookup(s->chanid);
1583: if (c == NULL)
1584: fatal("session_close: session %d: no channel %d",
1585: s->self, s->chanid);
1586: debug("session_exit_message: session %d channel %d pid %d",
1587: s->self, s->chanid, s->pid);
1588:
1589: if (WIFEXITED(status)) {
1590: channel_request_start(s->chanid,
1591: "exit-status", 0);
1592: packet_put_int(WEXITSTATUS(status));
1593: packet_send();
1594: } else if (WIFSIGNALED(status)) {
1595: channel_request_start(s->chanid,
1596: "exit-signal", 0);
1597: packet_put_int(WTERMSIG(status));
1598: packet_put_char(WCOREDUMP(status));
1599: packet_put_cstring("");
1600: packet_put_cstring("");
1601: packet_send();
1602: } else {
1603: /* Some weird exit cause. Just exit. */
1604: packet_disconnect("wait returned status %04x.", status);
1605: }
1606:
1607: /* disconnect channel */
1608: debug("session_exit_message: release channel %d", s->chanid);
1609: channel_cancel_cleanup(s->chanid);
1.5 markus 1610: /*
1611: * emulate a write failure with 'chan_write_failed', nobody will be
1612: * interested in data we write.
1613: * Note that we must not call 'chan_read_failed', since there could
1614: * be some more data waiting in the pipe.
1615: */
1.8 markus 1616: if (c->ostate != CHAN_OUTPUT_CLOSED)
1617: chan_write_failed(c);
1.2 markus 1618: s->chanid = -1;
1619: }
1620:
1621: void
1622: session_free(Session *s)
1623: {
1624: debug("session_free: session %d pid %d", s->self, s->pid);
1625: if (s->term)
1626: xfree(s->term);
1627: if (s->display)
1628: xfree(s->display);
1629: if (s->auth_data)
1630: xfree(s->auth_data);
1631: if (s->auth_proto)
1632: xfree(s->auth_proto);
1633: s->used = 0;
1634: }
1635:
1636: void
1637: session_close(Session *s)
1638: {
1639: session_pty_cleanup(s);
1640: session_free(s);
1.9 markus 1641: session_proctitle(s);
1.2 markus 1642: }
1643:
1644: void
1645: session_close_by_pid(pid_t pid, int status)
1646: {
1647: Session *s = session_by_pid(pid);
1648: if (s == NULL) {
1649: debug("session_close_by_pid: no session for pid %d", s->pid);
1650: return;
1651: }
1652: if (s->chanid != -1)
1653: session_exit_message(s, status);
1654: session_close(s);
1655: }
1656:
1657: /*
1658: * this is called when a channel dies before
1659: * the session 'child' itself dies
1660: */
1661: void
1662: session_close_by_channel(int id, void *arg)
1663: {
1664: Session *s = session_by_channel(id);
1665: if (s == NULL) {
1666: debug("session_close_by_channel: no session for channel %d", id);
1667: return;
1668: }
1669: /* disconnect channel */
1670: channel_cancel_cleanup(s->chanid);
1671: s->chanid = -1;
1672:
1673: debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1674: if (s->pid == 0) {
1675: /* close session immediately */
1676: session_close(s);
1677: } else {
1678: /* notify child, delay session cleanup */
1679: if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
1680: error("session_close_by_channel: kill %d: %s",
1681: s->pid, strerror(errno));
1682: }
1.9 markus 1683: }
1684:
1685: char *
1686: session_tty_list(void)
1687: {
1688: static char buf[1024];
1689: int i;
1690: buf[0] = '\0';
1691: for(i = 0; i < MAX_SESSIONS; i++) {
1692: Session *s = &sessions[i];
1693: if (s->used && s->ttyfd != -1) {
1694: if (buf[0] != '\0')
1695: strlcat(buf, ",", sizeof buf);
1696: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1697: }
1698: }
1699: if (buf[0] == '\0')
1700: strlcpy(buf, "notty", sizeof buf);
1701: return buf;
1702: }
1703:
1704: void
1705: session_proctitle(Session *s)
1706: {
1707: if (s->pw == NULL)
1708: error("no user for session %d", s->self);
1709: else
1710: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.2 markus 1711: }
1712:
1713: void
1.49 markus 1714: do_authenticated2(Authctxt *authctxt)
1.2 markus 1715: {
1.65 markus 1716:
1.2 markus 1717: server_loop2();
1.7 markus 1718: if (xauthfile)
1719: xauthfile_cleanup_proc(NULL);
1.1 markus 1720: }