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