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