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