Annotation of src/usr.bin/ssh/session.c, Revision 1.75
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.75 ! markus 36: RCSID("$OpenBSD: session.c,v 1.74 2001/04/17 19:34:25 markus Exp $");
1.1 markus 37:
1.51 markus 38: #include "ssh.h"
39: #include "ssh1.h"
40: #include "ssh2.h"
1.1 markus 41: #include "xmalloc.h"
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.75 ! markus 1056: snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
! 1057: shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
1.1 markus 1058: if (debug_flag)
1.75 ! markus 1059: fprintf(stderr, "Running %s\n", cmd);
! 1060: f = popen(cmd, "w");
1.1 markus 1061: if (f) {
1.60 markus 1062: if (do_xauth)
1063: fprintf(f, "%s %s\n", s->auth_proto,
1064: s->auth_data);
1.1 markus 1065: pclose(f);
1066: } else
1.60 markus 1067: fprintf(stderr, "Could not run %s\n",
1068: _PATH_SSH_USER_RC);
1.50 markus 1069: } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
1.1 markus 1070: if (debug_flag)
1.60 markus 1071: fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
1072: _PATH_SSH_SYSTEM_RC);
1.50 markus 1073: f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
1.1 markus 1074: if (f) {
1.60 markus 1075: if (do_xauth)
1076: fprintf(f, "%s %s\n", s->auth_proto,
1077: s->auth_data);
1.1 markus 1078: pclose(f);
1079: } else
1.60 markus 1080: fprintf(stderr, "Could not run %s\n",
1081: _PATH_SSH_SYSTEM_RC);
1082: } else if (do_xauth && options.xauth_location != NULL) {
1.1 markus 1083: /* Add authority data to .Xauthority if appropriate. */
1.60 markus 1084: char *screen = strchr(s->display, ':');
1085:
1086: if (debug_flag) {
1087: fprintf(stderr,
1088: "Running %.100s add "
1089: "%.100s %.100s %.100s\n",
1090: options.xauth_location, s->display,
1091: s->auth_proto, s->auth_data);
1092: if (screen != NULL)
1.14 deraadt 1093: fprintf(stderr,
1.60 markus 1094: "Adding %.*s/unix%s %s %s\n",
1095: (int)(screen - s->display),
1096: s->display, screen,
1097: s->auth_proto, s->auth_data);
1098: }
1099: snprintf(cmd, sizeof cmd, "%s -q -",
1100: options.xauth_location);
1101: f = popen(cmd, "w");
1102: if (f) {
1103: fprintf(f, "add %s %s %s\n", s->display,
1104: s->auth_proto, s->auth_data);
1105: if (screen != NULL)
1106: fprintf(f, "add %.*s/unix%s %s %s\n",
1107: (int)(screen - s->display),
1108: s->display, screen,
1109: s->auth_proto,
1110: s->auth_data);
1111: pclose(f);
1112: } else {
1113: fprintf(stderr, "Could not run %s\n",
1114: cmd);
1.1 markus 1115: }
1116: }
1117: /* Get the last component of the shell name. */
1118: cp = strrchr(shell, '/');
1119: if (cp)
1120: cp++;
1121: else
1122: cp = shell;
1123: }
1.67 markus 1124:
1125: /* restore SIGPIPE for child */
1126: signal(SIGPIPE, SIG_DFL);
1127:
1.1 markus 1128: /*
1129: * If we have no command, execute the shell. In this case, the shell
1130: * name to be passed in argv[0] is preceded by '-' to indicate that
1131: * this is a login shell.
1132: */
1133: if (!command) {
1134: if (!options.use_login) {
1135: char buf[256];
1136:
1137: /*
1138: * Check for mail if we have a tty and it was enabled
1139: * in server options.
1140: */
1.60 markus 1141: if (s->ttyfd != -1 && options.check_mail) {
1.1 markus 1142: char *mailbox;
1143: struct stat mailstat;
1.60 markus 1144:
1.1 markus 1145: mailbox = getenv("MAIL");
1146: if (mailbox != NULL) {
1.14 deraadt 1147: if (stat(mailbox, &mailstat) != 0 ||
1148: mailstat.st_size == 0)
1.1 markus 1149: printf("No mail.\n");
1150: else if (mailstat.st_mtime < mailstat.st_atime)
1151: printf("You have mail.\n");
1152: else
1153: printf("You have new mail.\n");
1154: }
1155: }
1156: /* Start the shell. Set initial character to '-'. */
1157: buf[0] = '-';
1158: strncpy(buf + 1, cp, sizeof(buf) - 1);
1159: buf[sizeof(buf) - 1] = 0;
1160:
1161: /* Execute the shell. */
1162: argv[0] = buf;
1163: argv[1] = NULL;
1164: execve(shell, argv, env);
1165:
1166: /* Executing the shell failed. */
1167: perror(shell);
1168: exit(1);
1169:
1170: } else {
1171: /* Launch login(1). */
1172:
1.26 millert 1173: execl("/usr/bin/login", "login", "-h", hostname,
1.25 markus 1174: "-p", "-f", "--", pw->pw_name, NULL);
1.1 markus 1175:
1176: /* Login couldn't be executed, die. */
1177:
1178: perror("login");
1179: exit(1);
1180: }
1181: }
1182: /*
1183: * Execute the command using the user's shell. This uses the -c
1184: * option to execute the command.
1185: */
1186: argv[0] = (char *) cp;
1187: argv[1] = "-c";
1188: argv[2] = (char *) command;
1189: argv[3] = NULL;
1190: execve(shell, argv, env);
1191: perror(shell);
1192: exit(1);
1193: }
1194:
1195: Session *
1196: session_new(void)
1197: {
1198: int i;
1199: static int did_init = 0;
1200: if (!did_init) {
1201: debug("session_new: init");
1202: for(i = 0; i < MAX_SESSIONS; i++) {
1203: sessions[i].used = 0;
1204: sessions[i].self = i;
1205: }
1206: did_init = 1;
1207: }
1208: for(i = 0; i < MAX_SESSIONS; i++) {
1209: Session *s = &sessions[i];
1210: if (! s->used) {
1.68 djm 1211: memset(s, 0, sizeof(*s));
1.1 markus 1212: s->chanid = -1;
1213: s->ptyfd = -1;
1214: s->ttyfd = -1;
1215: s->used = 1;
1216: debug("session_new: session %d", i);
1217: return s;
1218: }
1219: }
1220: return NULL;
1221: }
1222:
1223: void
1224: session_dump(void)
1225: {
1226: int i;
1227: for(i = 0; i < MAX_SESSIONS; i++) {
1228: Session *s = &sessions[i];
1229: debug("dump: used %d session %d %p channel %d pid %d",
1230: s->used,
1231: s->self,
1232: s,
1233: s->chanid,
1234: s->pid);
1235: }
1236: }
1237:
1.2 markus 1238: int
1239: session_open(int chanid)
1240: {
1241: Session *s = session_new();
1242: debug("session_open: channel %d", chanid);
1243: if (s == NULL) {
1244: error("no more sessions");
1245: return 0;
1246: }
1.7 markus 1247: s->pw = auth_get_user();
1248: if (s->pw == NULL)
1.54 stevesk 1249: fatal("no user for session %d", s->self);
1.2 markus 1250: debug("session_open: session %d: link with channel %d", s->self, chanid);
1251: s->chanid = chanid;
1252: return 1;
1253: }
1254:
1255: Session *
1256: session_by_channel(int id)
1257: {
1258: int i;
1259: for(i = 0; i < MAX_SESSIONS; i++) {
1260: Session *s = &sessions[i];
1261: if (s->used && s->chanid == id) {
1262: debug("session_by_channel: session %d channel %d", i, id);
1263: return s;
1264: }
1265: }
1266: debug("session_by_channel: unknown channel %d", id);
1267: session_dump();
1268: return NULL;
1269: }
1270:
1271: Session *
1272: session_by_pid(pid_t pid)
1273: {
1274: int i;
1275: debug("session_by_pid: pid %d", pid);
1276: for(i = 0; i < MAX_SESSIONS; i++) {
1277: Session *s = &sessions[i];
1278: if (s->used && s->pid == pid)
1279: return s;
1280: }
1281: error("session_by_pid: unknown pid %d", pid);
1282: session_dump();
1283: return NULL;
1284: }
1285:
1286: int
1287: session_window_change_req(Session *s)
1288: {
1289: s->col = packet_get_int();
1290: s->row = packet_get_int();
1291: s->xpixel = packet_get_int();
1292: s->ypixel = packet_get_int();
1.3 markus 1293: packet_done();
1.2 markus 1294: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1295: return 1;
1296: }
1297:
1298: int
1299: session_pty_req(Session *s)
1300: {
1.45 markus 1301: u_int len;
1.72 stevesk 1302: int n_bytes;
1.2 markus 1303:
1.19 markus 1304: if (no_pty_flag)
1305: return 0;
1.2 markus 1306: if (s->ttyfd != -1)
1.3 markus 1307: return 0;
1.2 markus 1308: s->term = packet_get_string(&len);
1309: s->col = packet_get_int();
1310: s->row = packet_get_int();
1311: s->xpixel = packet_get_int();
1312: s->ypixel = packet_get_int();
1313:
1314: if (strcmp(s->term, "") == 0) {
1315: xfree(s->term);
1316: s->term = NULL;
1317: }
1318: /* Allocate a pty and open it. */
1319: if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) {
1320: xfree(s->term);
1321: s->term = NULL;
1322: s->ptyfd = -1;
1323: s->ttyfd = -1;
1324: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1325: return 0;
1.2 markus 1326: }
1327: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1328: /*
1329: * Add a cleanup function to clear the utmp entry and record logout
1330: * time in case we call fatal() (e.g., the connection gets closed).
1331: */
1332: fatal_add_cleanup(pty_cleanup_proc, (void *)s);
1333: pty_setowner(s->pw, s->tty);
1334: /* Get window size from the packet. */
1335: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1336:
1.72 stevesk 1337: /* Get tty modes from the packet. */
1338: tty_parse_modes(s->ttyfd, &n_bytes);
1339: packet_done();
1340:
1.9 markus 1341: session_proctitle(s);
1342:
1.2 markus 1343: return 1;
1344: }
1345:
1.7 markus 1346: int
1347: session_subsystem_req(Session *s)
1348: {
1.45 markus 1349: u_int len;
1.7 markus 1350: int success = 0;
1351: char *subsys = packet_get_string(&len);
1.18 jakob 1352: int i;
1.7 markus 1353:
1354: packet_done();
1355: log("subsystem request for %s", subsys);
1.18 jakob 1356:
1357: for (i = 0; i < options.num_subsystems; i++) {
1358: if(strcmp(subsys, options.subsystem_name[i]) == 0) {
1359: debug("subsystem: exec() %s", options.subsystem_command[i]);
1.64 markus 1360: s->is_subsystem = 1;
1.63 markus 1361: do_exec_no_pty(s, options.subsystem_command[i]);
1.18 jakob 1362: success = 1;
1363: }
1364: }
1365:
1366: if (!success)
1367: log("subsystem request for %s failed, subsystem not found", subsys);
1.7 markus 1368:
1369: xfree(subsys);
1370: return success;
1371: }
1372:
1373: int
1374: session_x11_req(Session *s)
1375: {
1.33 markus 1376: int fd;
1.22 deraadt 1377: if (no_x11_forwarding_flag) {
1.19 markus 1378: debug("X11 forwarding disabled in user configuration file.");
1379: return 0;
1380: }
1.7 markus 1381: if (!options.x11_forwarding) {
1382: debug("X11 forwarding disabled in server configuration file.");
1383: return 0;
1384: }
1385: if (xauthfile != NULL) {
1386: debug("X11 fwd already started.");
1387: return 0;
1388: }
1389:
1390: debug("Received request for X11 forwarding with auth spoofing.");
1391: if (s->display != NULL)
1392: packet_disconnect("Protocol error: X11 display already set.");
1393:
1394: s->single_connection = packet_get_char();
1395: s->auth_proto = packet_get_string(NULL);
1396: s->auth_data = packet_get_string(NULL);
1397: s->screen = packet_get_int();
1398: packet_done();
1399:
1400: s->display = x11_create_display_inet(s->screen, options.x11_display_offset);
1401: if (s->display == NULL) {
1402: xfree(s->auth_proto);
1403: xfree(s->auth_data);
1404: return 0;
1405: }
1406: xauthfile = xmalloc(MAXPATHLEN);
1407: strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1.71 markus 1408: temporarily_use_uid(s->pw);
1.7 markus 1409: if (mkdtemp(xauthfile) == NULL) {
1410: restore_uid();
1411: error("private X11 dir: mkdtemp %s failed: %s",
1412: xauthfile, strerror(errno));
1413: xfree(xauthfile);
1414: xauthfile = NULL;
1415: xfree(s->auth_proto);
1416: xfree(s->auth_data);
1417: /* XXXX remove listening channels */
1418: return 0;
1419: }
1420: strlcat(xauthfile, "/cookies", MAXPATHLEN);
1.33 markus 1421: fd = open(xauthfile, O_RDWR|O_CREAT|O_EXCL, 0600);
1422: if (fd >= 0)
1423: close(fd);
1.7 markus 1424: restore_uid();
1425: fatal_add_cleanup(xauthfile_cleanup_proc, s);
1426: return 1;
1427: }
1428:
1.19 markus 1429: int
1430: session_shell_req(Session *s)
1431: {
1432: /* if forced_command == NULL, the shell is execed */
1433: char *shell = forced_command;
1434: packet_done();
1435: if (s->ttyfd == -1)
1.63 markus 1436: do_exec_no_pty(s, shell);
1.19 markus 1437: else
1.63 markus 1438: do_exec_pty(s, shell);
1.19 markus 1439: return 1;
1440: }
1441:
1442: int
1443: session_exec_req(Session *s)
1444: {
1.45 markus 1445: u_int len;
1.19 markus 1446: char *command = packet_get_string(&len);
1447: packet_done();
1448: if (forced_command) {
1.34 markus 1449: original_command = command;
1.19 markus 1450: command = forced_command;
1451: debug("Forced command '%.500s'", forced_command);
1452: }
1453: if (s->ttyfd == -1)
1.63 markus 1454: do_exec_no_pty(s, command);
1.19 markus 1455: else
1.63 markus 1456: do_exec_pty(s, command);
1.19 markus 1457: if (forced_command == NULL)
1458: xfree(command);
1459: return 1;
1460: }
1461:
1.43 markus 1462: int
1463: session_auth_agent_req(Session *s)
1464: {
1465: static int called = 0;
1466: packet_done();
1.44 markus 1467: if (no_agent_forwarding_flag) {
1468: debug("session_auth_agent_req: no_agent_forwarding_flag");
1469: return 0;
1470: }
1.43 markus 1471: if (called) {
1472: return 0;
1473: } else {
1474: called = 1;
1475: return auth_input_request_forwarding(s->pw);
1476: }
1477: }
1478:
1.2 markus 1479: void
1480: session_input_channel_req(int id, void *arg)
1481: {
1.45 markus 1482: u_int len;
1.2 markus 1483: int reply;
1484: int success = 0;
1485: char *rtype;
1486: Session *s;
1487: Channel *c;
1488:
1489: rtype = packet_get_string(&len);
1490: reply = packet_get_char();
1491:
1492: s = session_by_channel(id);
1493: if (s == NULL)
1494: fatal("session_input_channel_req: channel %d: no session", id);
1495: c = channel_lookup(id);
1496: if (c == NULL)
1497: fatal("session_input_channel_req: channel %d: bad channel", id);
1498:
1499: debug("session_input_channel_req: session %d channel %d request %s reply %d",
1500: s->self, id, rtype, reply);
1501:
1502: /*
1.64 markus 1503: * a session is in LARVAL state until a shell, a command
1504: * or a subsystem is executed
1.2 markus 1505: */
1506: if (c->type == SSH_CHANNEL_LARVAL) {
1507: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1508: success = session_shell_req(s);
1.2 markus 1509: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1510: success = session_exec_req(s);
1.2 markus 1511: } else if (strcmp(rtype, "pty-req") == 0) {
1.3 markus 1512: success = session_pty_req(s);
1.7 markus 1513: } else if (strcmp(rtype, "x11-req") == 0) {
1514: success = session_x11_req(s);
1.43 markus 1515: } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1516: success = session_auth_agent_req(s);
1.7 markus 1517: } else if (strcmp(rtype, "subsystem") == 0) {
1518: success = session_subsystem_req(s);
1.2 markus 1519: }
1520: }
1521: if (strcmp(rtype, "window-change") == 0) {
1522: success = session_window_change_req(s);
1523: }
1524:
1525: if (reply) {
1526: packet_start(success ?
1527: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1528: packet_put_int(c->remote_id);
1529: packet_send();
1530: }
1531: xfree(rtype);
1532: }
1533:
1534: void
1535: session_set_fds(Session *s, int fdin, int fdout, int fderr)
1536: {
1537: if (!compat20)
1538: fatal("session_set_fds: called for proto != 2.0");
1539: /*
1540: * now that have a child and a pipe to the child,
1541: * we can activate our channel and register the fd's
1542: */
1543: if (s->chanid == -1)
1544: fatal("no channel for session %d", s->self);
1545: channel_set_fds(s->chanid,
1546: fdout, fdin, fderr,
1.42 markus 1547: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1548: 1);
1.2 markus 1549: }
1550:
1.1 markus 1551: void
1552: session_pty_cleanup(Session *s)
1553: {
1554: if (s == NULL || s->ttyfd == -1)
1555: return;
1556:
1.54 stevesk 1557: debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1 markus 1558:
1559: /* Cancel the cleanup function. */
1560: fatal_remove_cleanup(pty_cleanup_proc, (void *)s);
1561:
1562: /* Record that the user has logged out. */
1563: record_logout(s->pid, s->tty);
1564:
1565: /* Release the pseudo-tty. */
1566: pty_release(s->tty);
1567:
1568: /*
1569: * Close the server side of the socket pairs. We must do this after
1570: * the pty cleanup, so that another process doesn't get this pty
1571: * while we're still cleaning up.
1572: */
1573: if (close(s->ptymaster) < 0)
1574: error("close(s->ptymaster): %s", strerror(errno));
1.2 markus 1575: }
1576:
1577: void
1578: session_exit_message(Session *s, int status)
1579: {
1580: Channel *c;
1581: if (s == NULL)
1582: fatal("session_close: no session");
1583: c = channel_lookup(s->chanid);
1584: if (c == NULL)
1585: fatal("session_close: session %d: no channel %d",
1586: s->self, s->chanid);
1587: debug("session_exit_message: session %d channel %d pid %d",
1588: s->self, s->chanid, s->pid);
1589:
1590: if (WIFEXITED(status)) {
1591: channel_request_start(s->chanid,
1592: "exit-status", 0);
1593: packet_put_int(WEXITSTATUS(status));
1594: packet_send();
1595: } else if (WIFSIGNALED(status)) {
1596: channel_request_start(s->chanid,
1597: "exit-signal", 0);
1598: packet_put_int(WTERMSIG(status));
1599: packet_put_char(WCOREDUMP(status));
1600: packet_put_cstring("");
1601: packet_put_cstring("");
1602: packet_send();
1603: } else {
1604: /* Some weird exit cause. Just exit. */
1605: packet_disconnect("wait returned status %04x.", status);
1606: }
1607:
1608: /* disconnect channel */
1609: debug("session_exit_message: release channel %d", s->chanid);
1610: channel_cancel_cleanup(s->chanid);
1.5 markus 1611: /*
1612: * emulate a write failure with 'chan_write_failed', nobody will be
1613: * interested in data we write.
1614: * Note that we must not call 'chan_read_failed', since there could
1615: * be some more data waiting in the pipe.
1616: */
1.8 markus 1617: if (c->ostate != CHAN_OUTPUT_CLOSED)
1618: chan_write_failed(c);
1.2 markus 1619: s->chanid = -1;
1620: }
1621:
1622: void
1623: session_free(Session *s)
1624: {
1625: debug("session_free: session %d pid %d", s->self, s->pid);
1626: if (s->term)
1627: xfree(s->term);
1628: if (s->display)
1629: xfree(s->display);
1630: if (s->auth_data)
1631: xfree(s->auth_data);
1632: if (s->auth_proto)
1633: xfree(s->auth_proto);
1634: s->used = 0;
1635: }
1636:
1637: void
1638: session_close(Session *s)
1639: {
1640: session_pty_cleanup(s);
1641: session_free(s);
1.9 markus 1642: session_proctitle(s);
1.2 markus 1643: }
1644:
1645: void
1646: session_close_by_pid(pid_t pid, int status)
1647: {
1648: Session *s = session_by_pid(pid);
1649: if (s == NULL) {
1650: debug("session_close_by_pid: no session for pid %d", s->pid);
1651: return;
1652: }
1653: if (s->chanid != -1)
1654: session_exit_message(s, status);
1655: session_close(s);
1656: }
1657:
1658: /*
1659: * this is called when a channel dies before
1660: * the session 'child' itself dies
1661: */
1662: void
1663: session_close_by_channel(int id, void *arg)
1664: {
1665: Session *s = session_by_channel(id);
1666: if (s == NULL) {
1667: debug("session_close_by_channel: no session for channel %d", id);
1668: return;
1669: }
1670: /* disconnect channel */
1671: channel_cancel_cleanup(s->chanid);
1672: s->chanid = -1;
1673:
1674: debug("session_close_by_channel: channel %d kill %d", id, s->pid);
1675: if (s->pid == 0) {
1676: /* close session immediately */
1677: session_close(s);
1678: } else {
1679: /* notify child, delay session cleanup */
1680: if (kill(s->pid, (s->ttyfd == -1) ? SIGTERM : SIGHUP) < 0)
1681: error("session_close_by_channel: kill %d: %s",
1682: s->pid, strerror(errno));
1683: }
1.9 markus 1684: }
1685:
1686: char *
1687: session_tty_list(void)
1688: {
1689: static char buf[1024];
1690: int i;
1691: buf[0] = '\0';
1692: for(i = 0; i < MAX_SESSIONS; i++) {
1693: Session *s = &sessions[i];
1694: if (s->used && s->ttyfd != -1) {
1695: if (buf[0] != '\0')
1696: strlcat(buf, ",", sizeof buf);
1697: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1698: }
1699: }
1700: if (buf[0] == '\0')
1701: strlcpy(buf, "notty", sizeof buf);
1702: return buf;
1703: }
1704:
1705: void
1706: session_proctitle(Session *s)
1707: {
1708: if (s->pw == NULL)
1709: error("no user for session %d", s->self);
1710: else
1711: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.2 markus 1712: }
1713:
1714: void
1.49 markus 1715: do_authenticated2(Authctxt *authctxt)
1.2 markus 1716: {
1.65 markus 1717:
1.2 markus 1718: server_loop2();
1.7 markus 1719: if (xauthfile)
1720: xauthfile_cleanup_proc(NULL);
1.1 markus 1721: }