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