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