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