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