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