Annotation of src/usr.bin/ssh/session.c, Revision 1.164
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.164 ! markus 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.161 markus 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.156 markus 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;
329:
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.155 itojun 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: }
1.163 markus 575:
576: #ifdef GSSAPI
577: if (options.gss_authentication) {
578: temporarily_use_uid(s->pw);
579: ssh_gssapi_storecreds();
580: restore_uid();
581: }
582: #endif
1.90 markus 583:
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.158 markus 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.161 markus 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.164 ! markus 698: char **env;
! 699: u_int envsize;
1.45 markus 700: u_int i, namelen;
1.1 markus 701:
702: /*
703: * Find the slot where the value should be stored. If the variable
704: * already exists, we reuse the slot; otherwise we append a new slot
705: * at the end of the array, expanding if necessary.
706: */
707: env = *envp;
708: namelen = strlen(name);
709: for (i = 0; env[i]; i++)
710: if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=')
711: break;
712: if (env[i]) {
713: /* Reuse the slot. */
714: xfree(env[i]);
715: } else {
716: /* New variable. Expand if necessary. */
1.164 ! markus 717: envsize = *envsizep;
! 718: if (i >= envsize - 1) {
! 719: if (envsize >= 1000)
! 720: fatal("child_set_env: too many env vars");
! 721: envsize += 50;
! 722: env = (*envp) = xrealloc(env, envsize * sizeof(char *));
! 723: *envsizep = envsize;
1.1 markus 724: }
725: /* Need to set the NULL pointer at end of array beyond the new slot. */
726: env[i + 1] = NULL;
727: }
728:
729: /* Allocate space and format the variable in the appropriate slot. */
730: env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1);
731: snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value);
732: }
733:
734: /*
735: * Reads environment variables from the given file and adds/overrides them
736: * into the environment. If the file does not exist, this does nothing.
737: * Otherwise, it must consist of empty lines, comments (line starts with '#')
738: * and assignments of the form name=value. No other forms are allowed.
739: */
1.94 itojun 740: static void
1.45 markus 741: read_environment_file(char ***env, u_int *envsize,
1.112 deraadt 742: const char *filename)
1.1 markus 743: {
744: FILE *f;
745: char buf[4096];
746: char *cp, *value;
1.142 deraadt 747: u_int lineno = 0;
1.1 markus 748:
749: f = fopen(filename, "r");
750: if (!f)
751: return;
752:
753: while (fgets(buf, sizeof(buf), f)) {
1.142 deraadt 754: if (++lineno > 1000)
755: fatal("Too many lines in environment file %s", filename);
1.1 markus 756: for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
757: ;
758: if (!*cp || *cp == '#' || *cp == '\n')
759: continue;
760: if (strchr(cp, '\n'))
761: *strchr(cp, '\n') = '\0';
762: value = strchr(cp, '=');
763: if (value == NULL) {
1.142 deraadt 764: fprintf(stderr, "Bad line %u in %.100s\n", lineno,
765: filename);
1.1 markus 766: continue;
767: }
1.14 deraadt 768: /*
769: * Replace the equals sign by nul, and advance value to
770: * the value string.
771: */
1.1 markus 772: *value = '\0';
773: value++;
774: child_set_env(env, envsize, cp, value);
775: }
776: fclose(f);
777: }
778:
1.127 markus 779: static char **
780: do_setup_env(Session *s, const char *shell)
1.1 markus 781: {
782: char buf[256];
1.127 markus 783: u_int i, envsize;
1.154 markus 784: char **env, *laddr;
1.127 markus 785: struct passwd *pw = s->pw;
1.1 markus 786:
787: /* Initialize the environment. */
788: envsize = 100;
789: env = xmalloc(envsize * sizeof(char *));
790: env[0] = NULL;
791:
1.161 markus 792: #ifdef GSSAPI
793: /* Allow any GSSAPI methods that we've used to alter
794: * the childs environment as they see fit
795: */
796: ssh_gssapi_do_child(&env, &envsize);
797: #endif
798:
1.1 markus 799: if (!options.use_login) {
800: /* Set basic environment. */
801: child_set_env(&env, &envsize, "USER", pw->pw_name);
802: child_set_env(&env, &envsize, "LOGNAME", pw->pw_name);
803: child_set_env(&env, &envsize, "HOME", pw->pw_dir);
1.28 millert 804: #ifdef HAVE_LOGIN_CAP
1.145 markus 805: if (setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH) < 0)
806: child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
807: else
808: child_set_env(&env, &envsize, "PATH", getenv("PATH"));
1.28 millert 809: #else
1.29 millert 810: child_set_env(&env, &envsize, "PATH", _PATH_STDPATH);
1.28 millert 811: #endif
1.1 markus 812:
813: snprintf(buf, sizeof buf, "%.200s/%.50s",
814: _PATH_MAILDIR, pw->pw_name);
815: child_set_env(&env, &envsize, "MAIL", buf);
816:
817: /* Normal systems set SHELL by default. */
818: child_set_env(&env, &envsize, "SHELL", shell);
819: }
820: if (getenv("TZ"))
821: child_set_env(&env, &envsize, "TZ", getenv("TZ"));
822:
823: /* Set custom environment options from RSA authentication. */
1.110 markus 824: if (!options.use_login) {
825: while (custom_environment) {
826: struct envstring *ce = custom_environment;
1.143 deraadt 827: char *str = ce->s;
1.127 markus 828:
1.143 deraadt 829: for (i = 0; str[i] != '=' && str[i]; i++)
1.110 markus 830: ;
1.143 deraadt 831: if (str[i] == '=') {
832: str[i] = 0;
833: child_set_env(&env, &envsize, str, str + i + 1);
1.110 markus 834: }
835: custom_environment = ce->next;
836: xfree(ce->s);
837: xfree(ce);
1.1 markus 838: }
839: }
840:
1.149 stevesk 841: /* SSH_CLIENT deprecated */
1.1 markus 842: snprintf(buf, sizeof buf, "%.50s %d %d",
1.127 markus 843: get_remote_ipaddr(), get_remote_port(), get_local_port());
1.1 markus 844: child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1.149 stevesk 845:
1.154 markus 846: laddr = get_local_ipaddr(packet_get_connection_in());
1.149 stevesk 847: snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1.154 markus 848: get_remote_ipaddr(), get_remote_port(), laddr, get_local_port());
849: xfree(laddr);
1.149 stevesk 850: child_set_env(&env, &envsize, "SSH_CONNECTION", buf);
1.1 markus 851:
1.60 markus 852: if (s->ttyfd != -1)
853: child_set_env(&env, &envsize, "SSH_TTY", s->tty);
854: if (s->term)
855: child_set_env(&env, &envsize, "TERM", s->term);
856: if (s->display)
857: child_set_env(&env, &envsize, "DISPLAY", s->display);
1.34 markus 858: if (original_command)
859: child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND",
860: original_command);
1.96 dugsong 861: #ifdef KRB5
862: if (s->authctxt->krb5_ticket_file)
863: child_set_env(&env, &envsize, "KRB5CCNAME",
1.112 deraadt 864: s->authctxt->krb5_ticket_file);
1.96 dugsong 865: #endif
1.136 markus 866: if (auth_sock_name != NULL)
1.1 markus 867: child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,
1.136 markus 868: auth_sock_name);
1.1 markus 869:
870: /* read $HOME/.ssh/environment. */
1.146 markus 871: if (options.permit_user_env && !options.use_login) {
1.14 deraadt 872: snprintf(buf, sizeof buf, "%.200s/.ssh/environment",
873: pw->pw_dir);
1.1 markus 874: read_environment_file(&env, &envsize, buf);
875: }
876: if (debug_flag) {
877: /* dump the environment */
878: fprintf(stderr, "Environment:\n");
879: for (i = 0; env[i]; i++)
880: fprintf(stderr, " %.200s\n", env[i]);
881: }
1.127 markus 882: return env;
883: }
884:
885: /*
886: * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found
887: * first in this order).
888: */
889: static void
890: do_rc_files(Session *s, const char *shell)
891: {
892: FILE *f = NULL;
893: char cmd[1024];
894: int do_xauth;
895: struct stat st;
896:
897: do_xauth =
898: s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;
899:
900: /* ignore _PATH_SSH_USER_RC for subsystems */
901: if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {
902: snprintf(cmd, sizeof cmd, "%s -c '%s %s'",
903: shell, _PATH_BSHELL, _PATH_SSH_USER_RC);
904: if (debug_flag)
905: fprintf(stderr, "Running %s\n", cmd);
906: f = popen(cmd, "w");
907: if (f) {
908: if (do_xauth)
909: fprintf(f, "%s %s\n", s->auth_proto,
910: s->auth_data);
911: pclose(f);
912: } else
913: fprintf(stderr, "Could not run %s\n",
914: _PATH_SSH_USER_RC);
915: } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {
916: if (debug_flag)
917: fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,
918: _PATH_SSH_SYSTEM_RC);
919: f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");
920: if (f) {
921: if (do_xauth)
922: fprintf(f, "%s %s\n", s->auth_proto,
923: s->auth_data);
924: pclose(f);
925: } else
926: fprintf(stderr, "Could not run %s\n",
927: _PATH_SSH_SYSTEM_RC);
928: } else if (do_xauth && options.xauth_location != NULL) {
929: /* Add authority data to .Xauthority if appropriate. */
930: if (debug_flag) {
931: fprintf(stderr,
1.151 stevesk 932: "Running %.500s remove %.100s\n",
933: options.xauth_location, s->auth_display);
934: fprintf(stderr,
935: "%.500s add %.100s %.100s %.100s\n",
1.127 markus 936: options.xauth_location, s->auth_display,
937: s->auth_proto, s->auth_data);
938: }
939: snprintf(cmd, sizeof cmd, "%s -q -",
940: options.xauth_location);
941: f = popen(cmd, "w");
942: if (f) {
1.151 stevesk 943: fprintf(f, "remove %s\n",
944: s->auth_display);
1.127 markus 945: fprintf(f, "add %s %s %s\n",
946: s->auth_display, s->auth_proto,
947: s->auth_data);
948: pclose(f);
949: } else {
950: fprintf(stderr, "Could not run %s\n",
951: cmd);
952: }
953: }
954: }
955:
956: static void
957: do_nologin(struct passwd *pw)
958: {
959: FILE *f = NULL;
960: char buf[1024];
961:
962: #ifdef HAVE_LOGIN_CAP
963: if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)
964: f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,
965: _PATH_NOLOGIN), "r");
966: #else
967: if (pw->pw_uid)
968: f = fopen(_PATH_NOLOGIN, "r");
969: #endif
970: if (f) {
971: /* /etc/nologin exists. Print its contents and exit. */
1.155 itojun 972: logit("User %.100s not allowed because %s exists",
1.150 stevesk 973: pw->pw_name, _PATH_NOLOGIN);
1.127 markus 974: while (fgets(buf, sizeof(buf), f))
975: fputs(buf, stderr);
976: fclose(f);
977: exit(254);
978: }
979: }
980:
981: /* Set login name, uid, gid, and groups. */
1.130 provos 982: void
1.127 markus 983: do_setusercontext(struct passwd *pw)
984: {
985: if (getuid() == 0 || geteuid() == 0) {
986: #ifdef HAVE_LOGIN_CAP
987: if (setusercontext(lc, pw, pw->pw_uid,
988: (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {
989: perror("unable to set user context");
990: exit(1);
991: }
992: #else
993: if (setlogin(pw->pw_name) < 0)
994: error("setlogin failed: %s", strerror(errno));
995: if (setgid(pw->pw_gid) < 0) {
996: perror("setgid");
997: exit(1);
998: }
999: /* Initialize the group list. */
1000: if (initgroups(pw->pw_name, pw->pw_gid) < 0) {
1001: perror("initgroups");
1002: exit(1);
1003: }
1004: endgrent();
1005:
1006: /* Permanently switch to the desired uid. */
1007: permanently_set_uid(pw);
1008: #endif
1009: }
1010: if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
1011: fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
1012: }
1013:
1.131 markus 1014: static void
1.130 provos 1015: launch_login(struct passwd *pw, const char *hostname)
1016: {
1017: /* Launch login(1). */
1018:
1019: execl("/usr/bin/login", "login", "-h", hostname,
1020: "-p", "-f", "--", pw->pw_name, (char *)NULL);
1021:
1022: /* Login couldn't be executed, die. */
1023:
1024: perror("login");
1025: exit(1);
1026: }
1027:
1.127 markus 1028: /*
1029: * Performs common processing for the child, such as setting up the
1030: * environment, closing extra file descriptors, setting the user and group
1031: * ids, and executing the command or shell.
1032: */
1033: void
1034: do_child(Session *s, const char *command)
1035: {
1036: extern char **environ;
1037: char **env;
1038: char *argv[10];
1039: const char *shell, *shell0, *hostname = NULL;
1040: struct passwd *pw = s->pw;
1041: u_int i;
1042:
1043: /* remove hostkey from the child's memory */
1044: destroy_sensitive_data();
1045:
1046: /* login(1) is only called if we execute the login shell */
1047: if (options.use_login && command != NULL)
1048: options.use_login = 0;
1049:
1050: /*
1051: * Login(1) does this as well, and it needs uid 0 for the "-h"
1052: * switch, so we let login(1) to this for us.
1053: */
1054: if (!options.use_login) {
1055: do_nologin(pw);
1056: do_setusercontext(pw);
1057: }
1058:
1059: /*
1060: * Get the shell from the password data. An empty shell field is
1061: * legal, and means /bin/sh.
1062: */
1063: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1.152 markus 1064:
1065: /*
1066: * Make sure $SHELL points to the shell from the password file,
1067: * even if shell is overridden from login.conf
1068: */
1069: env = do_setup_env(s, shell);
1070:
1.127 markus 1071: #ifdef HAVE_LOGIN_CAP
1072: shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);
1073: #endif
1074:
1.26 millert 1075: /* we have to stash the hostname before we close our socket. */
1076: if (options.use_login)
1.70 stevesk 1077: hostname = get_remote_name_or_ip(utmp_len,
1.158 markus 1078: options.use_dns);
1.1 markus 1079: /*
1080: * Close the connection descriptors; note that this is the child, and
1081: * the server will still have the socket open, and it is important
1082: * that we do not shutdown it. Note that the descriptors cannot be
1083: * closed before building the environment, as we call
1084: * get_remote_ipaddr there.
1085: */
1086: if (packet_get_connection_in() == packet_get_connection_out())
1087: close(packet_get_connection_in());
1088: else {
1089: close(packet_get_connection_in());
1090: close(packet_get_connection_out());
1091: }
1092: /*
1093: * Close all descriptors related to channels. They will still remain
1094: * open in the parent.
1095: */
1096: /* XXX better use close-on-exec? -markus */
1097: channel_close_all();
1098:
1099: /*
1100: * Close any extra file descriptors. Note that there may still be
1101: * descriptors left by system functions. They will be closed later.
1102: */
1103: endpwent();
1104:
1105: /*
1106: * Close any extra open file descriptors so that we don\'t have them
1107: * hanging around in clients. Note that we want to do this after
1108: * initgroups, because at least on Solaris 2.3 it leaves file
1109: * descriptors open.
1110: */
1111: for (i = 3; i < 64; i++)
1112: close(i);
1113:
1114: /*
1.125 deraadt 1115: * Must take new environment into use so that .ssh/rc,
1116: * /etc/ssh/sshrc and xauth are run in the proper environment.
1.1 markus 1117: */
1118: environ = env;
1.104 markus 1119:
1120: /* Change current directory to the user\'s home directory. */
1121: if (chdir(pw->pw_dir) < 0) {
1122: fprintf(stderr, "Could not chdir to home directory %s: %s\n",
1123: pw->pw_dir, strerror(errno));
1124: #ifdef HAVE_LOGIN_CAP
1125: if (login_getcapbool(lc, "requirehome", 0))
1126: exit(1);
1127: #endif
1128: }
1.1 markus 1129:
1.127 markus 1130: if (!options.use_login)
1131: do_rc_files(s, shell);
1.67 markus 1132:
1133: /* restore SIGPIPE for child */
1134: signal(SIGPIPE, SIG_DFL);
1135:
1.127 markus 1136: if (options.use_login) {
1.130 provos 1137: launch_login(pw, hostname);
1138: /* NEVERREACHED */
1.127 markus 1139: }
1140:
1141: /* Get the last component of the shell name. */
1142: if ((shell0 = strrchr(shell, '/')) != NULL)
1143: shell0++;
1144: else
1145: shell0 = shell;
1146:
1.1 markus 1147: /*
1148: * If we have no command, execute the shell. In this case, the shell
1149: * name to be passed in argv[0] is preceded by '-' to indicate that
1150: * this is a login shell.
1151: */
1152: if (!command) {
1.127 markus 1153: char argv0[256];
1.1 markus 1154:
1.127 markus 1155: /* Start the shell. Set initial character to '-'. */
1156: argv0[0] = '-';
1.1 markus 1157:
1.127 markus 1158: if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)
1.128 markus 1159: >= sizeof(argv0) - 1) {
1.127 markus 1160: errno = EINVAL;
1.1 markus 1161: perror(shell);
1162: exit(1);
1.127 markus 1163: }
1.1 markus 1164:
1.127 markus 1165: /* Execute the shell. */
1166: argv[0] = argv0;
1167: argv[1] = NULL;
1168: execve(shell, argv, env);
1169:
1170: /* Executing the shell failed. */
1171: perror(shell);
1172: exit(1);
1.1 markus 1173: }
1174: /*
1175: * Execute the command using the user's shell. This uses the -c
1176: * option to execute the command.
1177: */
1.127 markus 1178: argv[0] = (char *) shell0;
1.1 markus 1179: argv[1] = "-c";
1180: argv[2] = (char *) command;
1181: argv[3] = NULL;
1182: execve(shell, argv, env);
1183: perror(shell);
1184: exit(1);
1185: }
1186:
1187: Session *
1188: session_new(void)
1189: {
1190: int i;
1191: static int did_init = 0;
1192: if (!did_init) {
1193: debug("session_new: init");
1.112 deraadt 1194: for (i = 0; i < MAX_SESSIONS; i++) {
1.1 markus 1195: sessions[i].used = 0;
1196: }
1197: did_init = 1;
1198: }
1.112 deraadt 1199: for (i = 0; i < MAX_SESSIONS; i++) {
1.1 markus 1200: Session *s = &sessions[i];
1201: if (! s->used) {
1.68 djm 1202: memset(s, 0, sizeof(*s));
1.1 markus 1203: s->chanid = -1;
1204: s->ptyfd = -1;
1205: s->ttyfd = -1;
1206: s->used = 1;
1.85 markus 1207: s->self = i;
1.1 markus 1208: debug("session_new: session %d", i);
1209: return s;
1210: }
1211: }
1212: return NULL;
1213: }
1214:
1.94 itojun 1215: static void
1.1 markus 1216: session_dump(void)
1217: {
1218: int i;
1.112 deraadt 1219: for (i = 0; i < MAX_SESSIONS; i++) {
1.1 markus 1220: Session *s = &sessions[i];
1.137 mpech 1221: debug("dump: used %d session %d %p channel %d pid %ld",
1.1 markus 1222: s->used,
1223: s->self,
1224: s,
1225: s->chanid,
1.137 mpech 1226: (long)s->pid);
1.1 markus 1227: }
1228: }
1229:
1.2 markus 1230: int
1.97 markus 1231: session_open(Authctxt *authctxt, int chanid)
1.2 markus 1232: {
1233: Session *s = session_new();
1234: debug("session_open: channel %d", chanid);
1235: if (s == NULL) {
1236: error("no more sessions");
1237: return 0;
1238: }
1.97 markus 1239: s->authctxt = authctxt;
1240: s->pw = authctxt->pw;
1.7 markus 1241: if (s->pw == NULL)
1.54 stevesk 1242: fatal("no user for session %d", s->self);
1.2 markus 1243: debug("session_open: session %d: link with channel %d", s->self, chanid);
1244: s->chanid = chanid;
1245: return 1;
1246: }
1247:
1.130 provos 1248: Session *
1249: session_by_tty(char *tty)
1250: {
1251: int i;
1252: for (i = 0; i < MAX_SESSIONS; i++) {
1253: Session *s = &sessions[i];
1254: if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1255: debug("session_by_tty: session %d tty %s", i, tty);
1256: return s;
1257: }
1258: }
1259: debug("session_by_tty: unknown tty %.100s", tty);
1260: session_dump();
1261: return NULL;
1262: }
1263:
1.94 itojun 1264: static Session *
1.2 markus 1265: session_by_channel(int id)
1266: {
1267: int i;
1.112 deraadt 1268: for (i = 0; i < MAX_SESSIONS; i++) {
1.2 markus 1269: Session *s = &sessions[i];
1270: if (s->used && s->chanid == id) {
1271: debug("session_by_channel: session %d channel %d", i, id);
1272: return s;
1273: }
1274: }
1275: debug("session_by_channel: unknown channel %d", id);
1276: session_dump();
1277: return NULL;
1278: }
1279:
1.94 itojun 1280: static Session *
1.2 markus 1281: session_by_pid(pid_t pid)
1282: {
1283: int i;
1.137 mpech 1284: debug("session_by_pid: pid %ld", (long)pid);
1.112 deraadt 1285: for (i = 0; i < MAX_SESSIONS; i++) {
1.2 markus 1286: Session *s = &sessions[i];
1287: if (s->used && s->pid == pid)
1288: return s;
1289: }
1.137 mpech 1290: error("session_by_pid: unknown pid %ld", (long)pid);
1.2 markus 1291: session_dump();
1292: return NULL;
1293: }
1294:
1.94 itojun 1295: static int
1.2 markus 1296: session_window_change_req(Session *s)
1297: {
1298: s->col = packet_get_int();
1299: s->row = packet_get_int();
1300: s->xpixel = packet_get_int();
1301: s->ypixel = packet_get_int();
1.116 markus 1302: packet_check_eom();
1.2 markus 1303: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1304: return 1;
1305: }
1306:
1.94 itojun 1307: static int
1.2 markus 1308: session_pty_req(Session *s)
1309: {
1.45 markus 1310: u_int len;
1.72 stevesk 1311: int n_bytes;
1.132 markus 1312:
1.86 markus 1313: if (no_pty_flag) {
1314: debug("Allocating a pty not permitted for this authentication.");
1.19 markus 1315: return 0;
1.86 markus 1316: }
1317: if (s->ttyfd != -1) {
1318: packet_disconnect("Protocol error: you already have a pty.");
1.3 markus 1319: return 0;
1.134 markus 1320: }
1321: /* Get the time and hostname when the user last logged in. */
1322: if (options.print_lastlog) {
1323: s->hostname[0] = '\0';
1324: s->last_login_time = get_last_login_time(s->pw->pw_uid,
1325: s->pw->pw_name, s->hostname, sizeof(s->hostname));
1.86 markus 1326: }
1327:
1.2 markus 1328: s->term = packet_get_string(&len);
1.86 markus 1329:
1330: if (compat20) {
1331: s->col = packet_get_int();
1332: s->row = packet_get_int();
1333: } else {
1334: s->row = packet_get_int();
1335: s->col = packet_get_int();
1336: }
1.2 markus 1337: s->xpixel = packet_get_int();
1338: s->ypixel = packet_get_int();
1339:
1340: if (strcmp(s->term, "") == 0) {
1341: xfree(s->term);
1342: s->term = NULL;
1343: }
1.86 markus 1344:
1.2 markus 1345: /* Allocate a pty and open it. */
1.86 markus 1346: debug("Allocating pty.");
1.130 provos 1347: if (!PRIVSEP(pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)))) {
1.86 markus 1348: if (s->term)
1349: xfree(s->term);
1.2 markus 1350: s->term = NULL;
1351: s->ptyfd = -1;
1352: s->ttyfd = -1;
1353: error("session_pty_req: session %d alloc failed", s->self);
1.3 markus 1354: return 0;
1.2 markus 1355: }
1356: debug("session_pty_req: session %d alloc %s", s->self, s->tty);
1.86 markus 1357:
1358: /* for SSH1 the tty modes length is not given */
1359: if (!compat20)
1360: n_bytes = packet_remaining();
1361: tty_parse_modes(s->ttyfd, &n_bytes);
1362:
1.2 markus 1363: /*
1364: * Add a cleanup function to clear the utmp entry and record logout
1365: * time in case we call fatal() (e.g., the connection gets closed).
1366: */
1.85 markus 1367: fatal_add_cleanup(session_pty_cleanup, (void *)s);
1.130 provos 1368: if (!use_privsep)
1369: pty_setowner(s->pw, s->tty);
1.86 markus 1370:
1371: /* Set window size from the packet. */
1.2 markus 1372: pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1373:
1.116 markus 1374: packet_check_eom();
1.9 markus 1375: session_proctitle(s);
1.2 markus 1376: return 1;
1377: }
1378:
1.94 itojun 1379: static int
1.7 markus 1380: session_subsystem_req(Session *s)
1381: {
1.105 markus 1382: struct stat st;
1.45 markus 1383: u_int len;
1.7 markus 1384: int success = 0;
1.105 markus 1385: char *cmd, *subsys = packet_get_string(&len);
1.18 jakob 1386: int i;
1.7 markus 1387:
1.116 markus 1388: packet_check_eom();
1.155 itojun 1389: logit("subsystem request for %.100s", subsys);
1.18 jakob 1390:
1391: for (i = 0; i < options.num_subsystems; i++) {
1.105 markus 1392: if (strcmp(subsys, options.subsystem_name[i]) == 0) {
1393: cmd = options.subsystem_command[i];
1394: if (stat(cmd, &st) < 0) {
1395: error("subsystem: cannot stat %s: %s", cmd,
1396: strerror(errno));
1397: break;
1398: }
1399: debug("subsystem: exec() %s", cmd);
1.64 markus 1400: s->is_subsystem = 1;
1.105 markus 1401: do_exec(s, cmd);
1.18 jakob 1402: success = 1;
1.122 markus 1403: break;
1.18 jakob 1404: }
1405: }
1406:
1407: if (!success)
1.155 itojun 1408: logit("subsystem request for %.100s failed, subsystem not found",
1.105 markus 1409: subsys);
1.7 markus 1410:
1411: xfree(subsys);
1412: return success;
1413: }
1414:
1.94 itojun 1415: static int
1.7 markus 1416: session_x11_req(Session *s)
1417: {
1.81 markus 1418: int success;
1.7 markus 1419:
1420: s->single_connection = packet_get_char();
1421: s->auth_proto = packet_get_string(NULL);
1422: s->auth_data = packet_get_string(NULL);
1423: s->screen = packet_get_int();
1.116 markus 1424: packet_check_eom();
1.7 markus 1425:
1.81 markus 1426: success = session_setup_x11fwd(s);
1427: if (!success) {
1.7 markus 1428: xfree(s->auth_proto);
1429: xfree(s->auth_data);
1.84 markus 1430: s->auth_proto = NULL;
1431: s->auth_data = NULL;
1.7 markus 1432: }
1.81 markus 1433: return success;
1.7 markus 1434: }
1435:
1.94 itojun 1436: static int
1.19 markus 1437: session_shell_req(Session *s)
1438: {
1.116 markus 1439: packet_check_eom();
1.90 markus 1440: do_exec(s, NULL);
1.19 markus 1441: return 1;
1442: }
1443:
1.94 itojun 1444: static int
1.19 markus 1445: session_exec_req(Session *s)
1446: {
1.45 markus 1447: u_int len;
1.19 markus 1448: char *command = packet_get_string(&len);
1.116 markus 1449: packet_check_eom();
1.90 markus 1450: do_exec(s, command);
1.92 markus 1451: xfree(command);
1.19 markus 1452: return 1;
1453: }
1454:
1.94 itojun 1455: static int
1.157 markus 1456: session_break_req(Session *s)
1457: {
1458: u_int break_length;
1459:
1.160 markus 1460: break_length = packet_get_int(); /* ignored */
1.157 markus 1461: packet_check_eom();
1462:
1.160 markus 1463: if (s->ttyfd == -1 ||
1464: tcsendbreak(s->ttyfd, 0) < 0)
1.157 markus 1465: return 0;
1466: return 1;
1467: }
1468:
1469: static int
1.43 markus 1470: session_auth_agent_req(Session *s)
1471: {
1472: static int called = 0;
1.116 markus 1473: packet_check_eom();
1.44 markus 1474: if (no_agent_forwarding_flag) {
1475: debug("session_auth_agent_req: no_agent_forwarding_flag");
1476: return 0;
1477: }
1.43 markus 1478: if (called) {
1479: return 0;
1480: } else {
1481: called = 1;
1482: return auth_input_request_forwarding(s->pw);
1483: }
1484: }
1485:
1.123 markus 1486: int
1487: session_input_channel_req(Channel *c, const char *rtype)
1.2 markus 1488: {
1489: int success = 0;
1490: Session *s;
1491:
1.123 markus 1492: if ((s = session_by_channel(c->self)) == NULL) {
1.155 itojun 1493: logit("session_input_channel_req: no session %d req %.100s",
1.123 markus 1494: c->self, rtype);
1495: return 0;
1496: }
1497: debug("session_input_channel_req: session %d req %s", s->self, rtype);
1.2 markus 1498:
1499: /*
1.64 markus 1500: * a session is in LARVAL state until a shell, a command
1501: * or a subsystem is executed
1.2 markus 1502: */
1503: if (c->type == SSH_CHANNEL_LARVAL) {
1504: if (strcmp(rtype, "shell") == 0) {
1.19 markus 1505: success = session_shell_req(s);
1.2 markus 1506: } else if (strcmp(rtype, "exec") == 0) {
1.19 markus 1507: success = session_exec_req(s);
1.2 markus 1508: } else if (strcmp(rtype, "pty-req") == 0) {
1.3 markus 1509: success = session_pty_req(s);
1.7 markus 1510: } else if (strcmp(rtype, "x11-req") == 0) {
1511: success = session_x11_req(s);
1.43 markus 1512: } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) {
1513: success = session_auth_agent_req(s);
1.7 markus 1514: } else if (strcmp(rtype, "subsystem") == 0) {
1515: success = session_subsystem_req(s);
1.157 markus 1516: } else if (strcmp(rtype, "break") == 0) {
1517: success = session_break_req(s);
1.2 markus 1518: }
1519: }
1520: if (strcmp(rtype, "window-change") == 0) {
1521: success = session_window_change_req(s);
1522: }
1.123 markus 1523: return success;
1.2 markus 1524: }
1525:
1526: void
1527: session_set_fds(Session *s, int fdin, int fdout, int fderr)
1528: {
1529: if (!compat20)
1530: fatal("session_set_fds: called for proto != 2.0");
1531: /*
1532: * now that have a child and a pipe to the child,
1533: * we can activate our channel and register the fd's
1534: */
1535: if (s->chanid == -1)
1536: fatal("no channel for session %d", s->self);
1537: channel_set_fds(s->chanid,
1538: fdout, fdin, fderr,
1.42 markus 1539: fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ,
1.126 markus 1540: 1,
1541: CHAN_SES_WINDOW_DEFAULT);
1.2 markus 1542: }
1543:
1.85 markus 1544: /*
1545: * Function to perform pty cleanup. Also called if we get aborted abnormally
1546: * (e.g., due to a dropped connection).
1547: */
1.130 provos 1548: void
1549: session_pty_cleanup2(void *session)
1.1 markus 1550: {
1.85 markus 1551: Session *s = session;
1552:
1553: if (s == NULL) {
1554: error("session_pty_cleanup: no session");
1555: return;
1556: }
1557: if (s->ttyfd == -1)
1.1 markus 1558: return;
1559:
1.54 stevesk 1560: debug("session_pty_cleanup: session %d release %s", s->self, s->tty);
1.1 markus 1561:
1562: /* Record that the user has logged out. */
1.85 markus 1563: if (s->pid != 0)
1564: record_logout(s->pid, s->tty);
1.1 markus 1565:
1566: /* Release the pseudo-tty. */
1.130 provos 1567: if (getuid() == 0)
1568: pty_release(s->tty);
1.1 markus 1569:
1570: /*
1571: * Close the server side of the socket pairs. We must do this after
1572: * the pty cleanup, so that another process doesn't get this pty
1573: * while we're still cleaning up.
1574: */
1575: if (close(s->ptymaster) < 0)
1.130 provos 1576: error("close(s->ptymaster/%d): %s", s->ptymaster, strerror(errno));
1.108 markus 1577:
1578: /* unlink pty from session */
1579: s->ttyfd = -1;
1.2 markus 1580: }
1581:
1.130 provos 1582: void
1583: session_pty_cleanup(void *session)
1584: {
1585: PRIVSEP(session_pty_cleanup2(session));
1586: }
1587:
1.147 markus 1588: static char *
1589: sig2name(int sig)
1590: {
1591: #define SSH_SIG(x) if (sig == SIG ## x) return #x
1592: SSH_SIG(ABRT);
1593: SSH_SIG(ALRM);
1594: SSH_SIG(FPE);
1595: SSH_SIG(HUP);
1596: SSH_SIG(ILL);
1597: SSH_SIG(INT);
1598: SSH_SIG(KILL);
1599: SSH_SIG(PIPE);
1600: SSH_SIG(QUIT);
1601: SSH_SIG(SEGV);
1602: SSH_SIG(TERM);
1603: SSH_SIG(USR1);
1604: SSH_SIG(USR2);
1605: #undef SSH_SIG
1606: return "SIG@openssh.com";
1607: }
1608:
1.94 itojun 1609: static void
1.2 markus 1610: session_exit_message(Session *s, int status)
1611: {
1612: Channel *c;
1.124 markus 1613:
1614: if ((c = channel_lookup(s->chanid)) == NULL)
1.85 markus 1615: fatal("session_exit_message: session %d: no channel %d",
1.2 markus 1616: s->self, s->chanid);
1.137 mpech 1617: debug("session_exit_message: session %d channel %d pid %ld",
1618: s->self, s->chanid, (long)s->pid);
1.2 markus 1619:
1620: if (WIFEXITED(status)) {
1.124 markus 1621: channel_request_start(s->chanid, "exit-status", 0);
1.2 markus 1622: packet_put_int(WEXITSTATUS(status));
1623: packet_send();
1624: } else if (WIFSIGNALED(status)) {
1.124 markus 1625: channel_request_start(s->chanid, "exit-signal", 0);
1.147 markus 1626: packet_put_cstring(sig2name(WTERMSIG(status)));
1.2 markus 1627: packet_put_char(WCOREDUMP(status));
1628: packet_put_cstring("");
1629: packet_put_cstring("");
1630: packet_send();
1631: } else {
1632: /* Some weird exit cause. Just exit. */
1633: packet_disconnect("wait returned status %04x.", status);
1634: }
1635:
1636: /* disconnect channel */
1637: debug("session_exit_message: release channel %d", s->chanid);
1638: channel_cancel_cleanup(s->chanid);
1.5 markus 1639: /*
1640: * emulate a write failure with 'chan_write_failed', nobody will be
1641: * interested in data we write.
1642: * Note that we must not call 'chan_read_failed', since there could
1643: * be some more data waiting in the pipe.
1644: */
1.8 markus 1645: if (c->ostate != CHAN_OUTPUT_CLOSED)
1646: chan_write_failed(c);
1.2 markus 1647: s->chanid = -1;
1648: }
1649:
1.130 provos 1650: void
1.85 markus 1651: session_close(Session *s)
1.2 markus 1652: {
1.137 mpech 1653: debug("session_close: session %d pid %ld", s->self, (long)s->pid);
1.85 markus 1654: if (s->ttyfd != -1) {
1655: fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1656: session_pty_cleanup(s);
1657: }
1.2 markus 1658: if (s->term)
1659: xfree(s->term);
1660: if (s->display)
1661: xfree(s->display);
1.118 stevesk 1662: if (s->auth_display)
1663: xfree(s->auth_display);
1.2 markus 1664: if (s->auth_data)
1665: xfree(s->auth_data);
1666: if (s->auth_proto)
1667: xfree(s->auth_proto);
1668: s->used = 0;
1.9 markus 1669: session_proctitle(s);
1.2 markus 1670: }
1671:
1672: void
1673: session_close_by_pid(pid_t pid, int status)
1674: {
1675: Session *s = session_by_pid(pid);
1676: if (s == NULL) {
1.137 mpech 1677: debug("session_close_by_pid: no session for pid %ld",
1678: (long)pid);
1.2 markus 1679: return;
1680: }
1681: if (s->chanid != -1)
1682: session_exit_message(s, status);
1683: session_close(s);
1.98 markus 1684: }
1685:
1.2 markus 1686: /*
1687: * this is called when a channel dies before
1688: * the session 'child' itself dies
1689: */
1690: void
1691: session_close_by_channel(int id, void *arg)
1692: {
1693: Session *s = session_by_channel(id);
1694: if (s == NULL) {
1.107 markus 1695: debug("session_close_by_channel: no session for id %d", id);
1.2 markus 1696: return;
1697: }
1.137 mpech 1698: debug("session_close_by_channel: channel %d child %ld",
1699: id, (long)s->pid);
1.107 markus 1700: if (s->pid != 0) {
1701: debug("session_close_by_channel: channel %d: has child", id);
1.108 markus 1702: /*
1703: * delay detach of session, but release pty, since
1704: * the fd's to the child are already closed
1705: */
1706: if (s->ttyfd != -1) {
1707: fatal_remove_cleanup(session_pty_cleanup, (void *)s);
1708: session_pty_cleanup(s);
1709: }
1.107 markus 1710: return;
1711: }
1712: /* detach by removing callback */
1.2 markus 1713: channel_cancel_cleanup(s->chanid);
1714: s->chanid = -1;
1.106 markus 1715: session_close(s);
1716: }
1717:
1718: void
1.130 provos 1719: session_destroy_all(void (*closefunc)(Session *))
1.106 markus 1720: {
1721: int i;
1.112 deraadt 1722: for (i = 0; i < MAX_SESSIONS; i++) {
1.106 markus 1723: Session *s = &sessions[i];
1.130 provos 1724: if (s->used) {
1725: if (closefunc != NULL)
1726: closefunc(s);
1727: else
1728: session_close(s);
1729: }
1.2 markus 1730: }
1.9 markus 1731: }
1732:
1.94 itojun 1733: static char *
1.9 markus 1734: session_tty_list(void)
1735: {
1736: static char buf[1024];
1737: int i;
1738: buf[0] = '\0';
1.112 deraadt 1739: for (i = 0; i < MAX_SESSIONS; i++) {
1.9 markus 1740: Session *s = &sessions[i];
1741: if (s->used && s->ttyfd != -1) {
1742: if (buf[0] != '\0')
1743: strlcat(buf, ",", sizeof buf);
1744: strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
1745: }
1746: }
1747: if (buf[0] == '\0')
1748: strlcpy(buf, "notty", sizeof buf);
1749: return buf;
1750: }
1751:
1752: void
1753: session_proctitle(Session *s)
1754: {
1755: if (s->pw == NULL)
1756: error("no user for session %d", s->self);
1757: else
1758: setproctitle("%s@%s", s->pw->pw_name, session_tty_list());
1.81 markus 1759: }
1760:
1761: int
1762: session_setup_x11fwd(Session *s)
1763: {
1764: struct stat st;
1.109 stevesk 1765: char display[512], auth_display[512];
1766: char hostname[MAXHOSTNAMELEN];
1.81 markus 1767:
1768: if (no_x11_forwarding_flag) {
1769: packet_send_debug("X11 forwarding disabled in user configuration file.");
1770: return 0;
1771: }
1772: if (!options.x11_forwarding) {
1773: debug("X11 forwarding disabled in server configuration file.");
1774: return 0;
1775: }
1776: if (!options.xauth_location ||
1777: (stat(options.xauth_location, &st) == -1)) {
1778: packet_send_debug("No xauth program; cannot forward with spoofing.");
1.91 markus 1779: return 0;
1780: }
1781: if (options.use_login) {
1782: packet_send_debug("X11 forwarding disabled; "
1783: "not compatible with UseLogin=yes.");
1.81 markus 1784: return 0;
1785: }
1.87 markus 1786: if (s->display != NULL) {
1.81 markus 1787: debug("X11 display already set.");
1788: return 0;
1789: }
1.140 deraadt 1790: if (x11_create_display_inet(options.x11_display_offset,
1791: options.x11_use_localhost, s->single_connection,
1792: &s->display_number) == -1) {
1.87 markus 1793: debug("x11_create_display_inet failed.");
1.81 markus 1794: return 0;
1795: }
1.109 stevesk 1796:
1797: /* Set up a suitable value for the DISPLAY variable. */
1798: if (gethostname(hostname, sizeof(hostname)) < 0)
1799: fatal("gethostname: %.100s", strerror(errno));
1800: /*
1801: * auth_display must be used as the displayname when the
1802: * authorization entry is added with xauth(1). This will be
1803: * different than the DISPLAY string for localhost displays.
1804: */
1.119 stevesk 1805: if (options.x11_use_localhost) {
1.140 deraadt 1806: snprintf(display, sizeof display, "localhost:%u.%u",
1.109 stevesk 1807: s->display_number, s->screen);
1.140 deraadt 1808: snprintf(auth_display, sizeof auth_display, "unix:%u.%u",
1.118 stevesk 1809: s->display_number, s->screen);
1.109 stevesk 1810: s->display = xstrdup(display);
1.118 stevesk 1811: s->auth_display = xstrdup(auth_display);
1.109 stevesk 1812: } else {
1.140 deraadt 1813: snprintf(display, sizeof display, "%.400s:%u.%u", hostname,
1.109 stevesk 1814: s->display_number, s->screen);
1815: s->display = xstrdup(display);
1.118 stevesk 1816: s->auth_display = xstrdup(display);
1.109 stevesk 1817: }
1818:
1.81 markus 1819: return 1;
1.2 markus 1820: }
1821:
1.94 itojun 1822: static void
1.49 markus 1823: do_authenticated2(Authctxt *authctxt)
1.2 markus 1824: {
1.97 markus 1825: server_loop2(authctxt);
1.161 markus 1826: #if defined(GSSAPI)
1827: if (options.gss_cleanup_creds)
1828: ssh_gssapi_cleanup_creds(NULL);
1829: #endif
1.1 markus 1830: }