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