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