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