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