]> andersk Git - openssh.git/blob - clientloop.c
- stevesk@cvs.openbsd.org 2006/07/25 02:59:21
[openssh.git] / clientloop.c
1 /* $OpenBSD: clientloop.c,v 1.171 2006/07/25 02:59:21 stevesk Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * The main loop for the interactive session (client side).
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  *
14  *
15  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  *
38  * SSH2 support added by Markus Friedl.
39  * Copyright (c) 1999, 2000, 2001 Markus Friedl.  All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  */
61
62 #include "includes.h"
63
64 #include <sys/types.h>
65 #include <sys/ioctl.h>
66 #ifdef HAVE_SYS_STAT_H
67 # include <sys/stat.h>
68 #endif
69 #ifdef HAVE_SYS_TIME_H
70 # include <sys/time.h>
71 #endif
72 #include <sys/socket.h>
73
74 #include <ctype.h>
75 #include <errno.h>
76 #ifdef HAVE_PATHS_H
77 #include <paths.h>
78 #endif
79 #include <signal.h>
80 #include <string.h>
81 #include <termios.h>
82 #include <unistd.h>
83
84 #include "ssh.h"
85 #include "ssh1.h"
86 #include "ssh2.h"
87 #include "xmalloc.h"
88 #include "packet.h"
89 #include "buffer.h"
90 #include "compat.h"
91 #include "channels.h"
92 #include "dispatch.h"
93 #include "buffer.h"
94 #include "bufaux.h"
95 #include "key.h"
96 #include "kex.h"
97 #include "log.h"
98 #include "readconf.h"
99 #include "clientloop.h"
100 #include "sshconnect.h"
101 #include "authfd.h"
102 #include "atomicio.h"
103 #include "sshpty.h"
104 #include "misc.h"
105 #include "monitor_fdpass.h"
106 #include "match.h"
107 #include "msg.h"
108
109 /* import options */
110 extern Options options;
111
112 /* Flag indicating that stdin should be redirected from /dev/null. */
113 extern int stdin_null_flag;
114
115 /* Flag indicating that no shell has been requested */
116 extern int no_shell_flag;
117
118 /* Control socket */
119 extern int control_fd;
120
121 /*
122  * Name of the host we are connecting to.  This is the name given on the
123  * command line, or the HostName specified for the user-supplied name in a
124  * configuration file.
125  */
126 extern char *host;
127
128 /*
129  * Flag to indicate that we have received a window change signal which has
130  * not yet been processed.  This will cause a message indicating the new
131  * window size to be sent to the server a little later.  This is volatile
132  * because this is updated in a signal handler.
133  */
134 static volatile sig_atomic_t received_window_change_signal = 0;
135 static volatile sig_atomic_t received_signal = 0;
136
137 /* Flag indicating whether the user's terminal is in non-blocking mode. */
138 static int in_non_blocking_mode = 0;
139
140 /* Common data for the client loop code. */
141 static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
142 static int escape_char;         /* Escape character. */
143 static int escape_pending;      /* Last character was the escape character */
144 static int last_was_cr;         /* Last character was a newline. */
145 static int exit_status;         /* Used to store the exit status of the command. */
146 static int stdin_eof;           /* EOF has been encountered on standard error. */
147 static Buffer stdin_buffer;     /* Buffer for stdin data. */
148 static Buffer stdout_buffer;    /* Buffer for stdout data. */
149 static Buffer stderr_buffer;    /* Buffer for stderr data. */
150 static u_long stdin_bytes, stdout_bytes, stderr_bytes;
151 static u_int buffer_high;/* Soft max buffer size. */
152 static int connection_in;       /* Connection to server (input). */
153 static int connection_out;      /* Connection to server (output). */
154 static int need_rekeying;       /* Set to non-zero if rekeying is requested. */
155 static int session_closed = 0;  /* In SSH2: login session closed. */
156 static int server_alive_timeouts = 0;
157
158 static void client_init_dispatch(void);
159 int     session_ident = -1;
160
161 struct confirm_ctx {
162         int want_tty;
163         int want_subsys;
164         int want_x_fwd;
165         int want_agent_fwd;
166         Buffer cmd;
167         char *term;
168         struct termios tio;
169         char **env;
170 };
171
172 /*XXX*/
173 extern Kex *xxx_kex;
174
175 void ssh_process_session2_setup(int, int, int, Buffer *);
176
177 /* Restores stdin to blocking mode. */
178
179 static void
180 leave_non_blocking(void)
181 {
182         if (in_non_blocking_mode) {
183                 unset_nonblock(fileno(stdin));
184                 in_non_blocking_mode = 0;
185         }
186 }
187
188 /* Puts stdin terminal in non-blocking mode. */
189
190 static void
191 enter_non_blocking(void)
192 {
193         in_non_blocking_mode = 1;
194         set_nonblock(fileno(stdin));
195 }
196
197 /*
198  * Signal handler for the window change signal (SIGWINCH).  This just sets a
199  * flag indicating that the window has changed.
200  */
201 /*ARGSUSED */
202 static void
203 window_change_handler(int sig)
204 {
205         received_window_change_signal = 1;
206         signal(SIGWINCH, window_change_handler);
207 }
208
209 /*
210  * Signal handler for signals that cause the program to terminate.  These
211  * signals must be trapped to restore terminal modes.
212  */
213 /*ARGSUSED */
214 static void
215 signal_handler(int sig)
216 {
217         received_signal = sig;
218         quit_pending = 1;
219 }
220
221 /*
222  * Returns current time in seconds from Jan 1, 1970 with the maximum
223  * available resolution.
224  */
225
226 static double
227 get_current_time(void)
228 {
229         struct timeval tv;
230         gettimeofday(&tv, NULL);
231         return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
232 }
233
234 #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
235 void
236 client_x11_get_proto(const char *display, const char *xauth_path,
237     u_int trusted, char **_proto, char **_data)
238 {
239         char cmd[1024];
240         char line[512];
241         char xdisplay[512];
242         static char proto[512], data[512];
243         FILE *f;
244         int got_data = 0, generated = 0, do_unlink = 0, i;
245         char *xauthdir, *xauthfile;
246         struct stat st;
247
248         xauthdir = xauthfile = NULL;
249         *_proto = proto;
250         *_data = data;
251         proto[0] = data[0] = '\0';
252
253         if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) {
254                 debug("No xauth program.");
255         } else {
256                 if (display == NULL) {
257                         debug("x11_get_proto: DISPLAY not set");
258                         return;
259                 }
260                 /*
261                  * Handle FamilyLocal case where $DISPLAY does
262                  * not match an authorization entry.  For this we
263                  * just try "xauth list unix:displaynum.screennum".
264                  * XXX: "localhost" match to determine FamilyLocal
265                  *      is not perfect.
266                  */
267                 if (strncmp(display, "localhost:", 10) == 0) {
268                         snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
269                             display + 10);
270                         display = xdisplay;
271                 }
272                 if (trusted == 0) {
273                         xauthdir = xmalloc(MAXPATHLEN);
274                         xauthfile = xmalloc(MAXPATHLEN);
275                         strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
276                         if (mkdtemp(xauthdir) != NULL) {
277                                 do_unlink = 1;
278                                 snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
279                                     xauthdir);
280                                 snprintf(cmd, sizeof(cmd),
281                                     "%s -f %s generate %s " SSH_X11_PROTO
282                                     " untrusted timeout 1200 2>" _PATH_DEVNULL,
283                                     xauth_path, xauthfile, display);
284                                 debug2("x11_get_proto: %s", cmd);
285                                 if (system(cmd) == 0)
286                                         generated = 1;
287                         }
288                 }
289                 snprintf(cmd, sizeof(cmd),
290                     "%s %s%s list %s 2>" _PATH_DEVNULL,
291                     xauth_path,
292                     generated ? "-f " : "" ,
293                     generated ? xauthfile : "",
294                     display);
295                 debug2("x11_get_proto: %s", cmd);
296                 f = popen(cmd, "r");
297                 if (f && fgets(line, sizeof(line), f) &&
298                     sscanf(line, "%*s %511s %511s", proto, data) == 2)
299                         got_data = 1;
300                 if (f)
301                         pclose(f);
302         }
303
304         if (do_unlink) {
305                 unlink(xauthfile);
306                 rmdir(xauthdir);
307         }
308         if (xauthdir)
309                 xfree(xauthdir);
310         if (xauthfile)
311                 xfree(xauthfile);
312
313         /*
314          * If we didn't get authentication data, just make up some
315          * data.  The forwarding code will check the validity of the
316          * response anyway, and substitute this data.  The X11
317          * server, however, will ignore this fake data and use
318          * whatever authentication mechanisms it was using otherwise
319          * for the local connection.
320          */
321         if (!got_data) {
322                 u_int32_t rnd = 0;
323
324                 logit("Warning: No xauth data; "
325                     "using fake authentication data for X11 forwarding.");
326                 strlcpy(proto, SSH_X11_PROTO, sizeof proto);
327                 for (i = 0; i < 16; i++) {
328                         if (i % 4 == 0)
329                                 rnd = arc4random();
330                         snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
331                             rnd & 0xff);
332                         rnd >>= 8;
333                 }
334         }
335 }
336
337 /*
338  * This is called when the interactive is entered.  This checks if there is
339  * an EOF coming on stdin.  We must check this explicitly, as select() does
340  * not appear to wake up when redirecting from /dev/null.
341  */
342
343 static void
344 client_check_initial_eof_on_stdin(void)
345 {
346         int len;
347         char buf[1];
348
349         /*
350          * If standard input is to be "redirected from /dev/null", we simply
351          * mark that we have seen an EOF and send an EOF message to the
352          * server. Otherwise, we try to read a single character; it appears
353          * that for some files, such /dev/null, select() never wakes up for
354          * read for this descriptor, which means that we never get EOF.  This
355          * way we will get the EOF if stdin comes from /dev/null or similar.
356          */
357         if (stdin_null_flag) {
358                 /* Fake EOF on stdin. */
359                 debug("Sending eof.");
360                 stdin_eof = 1;
361                 packet_start(SSH_CMSG_EOF);
362                 packet_send();
363         } else {
364                 enter_non_blocking();
365
366                 /* Check for immediate EOF on stdin. */
367                 len = read(fileno(stdin), buf, 1);
368                 if (len == 0) {
369                         /* EOF.  Record that we have seen it and send EOF to server. */
370                         debug("Sending eof.");
371                         stdin_eof = 1;
372                         packet_start(SSH_CMSG_EOF);
373                         packet_send();
374                 } else if (len > 0) {
375                         /*
376                          * Got data.  We must store the data in the buffer,
377                          * and also process it as an escape character if
378                          * appropriate.
379                          */
380                         if ((u_char) buf[0] == escape_char)
381                                 escape_pending = 1;
382                         else
383                                 buffer_append(&stdin_buffer, buf, 1);
384                 }
385                 leave_non_blocking();
386         }
387 }
388
389
390 /*
391  * Make packets from buffered stdin data, and buffer them for sending to the
392  * connection.
393  */
394
395 static void
396 client_make_packets_from_stdin_data(void)
397 {
398         u_int len;
399
400         /* Send buffered stdin data to the server. */
401         while (buffer_len(&stdin_buffer) > 0 &&
402             packet_not_very_much_data_to_write()) {
403                 len = buffer_len(&stdin_buffer);
404                 /* Keep the packets at reasonable size. */
405                 if (len > packet_get_maxsize())
406                         len = packet_get_maxsize();
407                 packet_start(SSH_CMSG_STDIN_DATA);
408                 packet_put_string(buffer_ptr(&stdin_buffer), len);
409                 packet_send();
410                 buffer_consume(&stdin_buffer, len);
411                 stdin_bytes += len;
412                 /* If we have a pending EOF, send it now. */
413                 if (stdin_eof && buffer_len(&stdin_buffer) == 0) {
414                         packet_start(SSH_CMSG_EOF);
415                         packet_send();
416                 }
417         }
418 }
419
420 /*
421  * Checks if the client window has changed, and sends a packet about it to
422  * the server if so.  The actual change is detected elsewhere (by a software
423  * interrupt on Unix); this just checks the flag and sends a message if
424  * appropriate.
425  */
426
427 static void
428 client_check_window_change(void)
429 {
430         struct winsize ws;
431
432         if (! received_window_change_signal)
433                 return;
434         /** XXX race */
435         received_window_change_signal = 0;
436
437         debug2("client_check_window_change: changed");
438
439         if (compat20) {
440                 channel_send_window_changes();
441         } else {
442                 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
443                         return;
444                 packet_start(SSH_CMSG_WINDOW_SIZE);
445                 packet_put_int((u_int)ws.ws_row);
446                 packet_put_int((u_int)ws.ws_col);
447                 packet_put_int((u_int)ws.ws_xpixel);
448                 packet_put_int((u_int)ws.ws_ypixel);
449                 packet_send();
450         }
451 }
452
453 static void
454 client_global_request_reply(int type, u_int32_t seq, void *ctxt)
455 {
456         server_alive_timeouts = 0;
457         client_global_request_reply_fwd(type, seq, ctxt);
458 }
459
460 static void
461 server_alive_check(void)
462 {
463         if (++server_alive_timeouts > options.server_alive_count_max)
464                 packet_disconnect("Timeout, server not responding.");
465         packet_start(SSH2_MSG_GLOBAL_REQUEST);
466         packet_put_cstring("keepalive@openssh.com");
467         packet_put_char(1);     /* boolean: want reply */
468         packet_send();
469 }
470
471 /*
472  * Waits until the client can do something (some data becomes available on
473  * one of the file descriptors).
474  */
475 static void
476 client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
477     int *maxfdp, u_int *nallocp, int rekeying)
478 {
479         struct timeval tv, *tvp;
480         int ret;
481
482         /* Add any selections by the channel mechanism. */
483         channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, rekeying);
484
485         if (!compat20) {
486                 /* Read from the connection, unless our buffers are full. */
487                 if (buffer_len(&stdout_buffer) < buffer_high &&
488                     buffer_len(&stderr_buffer) < buffer_high &&
489                     channel_not_very_much_buffered_data())
490                         FD_SET(connection_in, *readsetp);
491                 /*
492                  * Read from stdin, unless we have seen EOF or have very much
493                  * buffered data to send to the server.
494                  */
495                 if (!stdin_eof && packet_not_very_much_data_to_write())
496                         FD_SET(fileno(stdin), *readsetp);
497
498                 /* Select stdout/stderr if have data in buffer. */
499                 if (buffer_len(&stdout_buffer) > 0)
500                         FD_SET(fileno(stdout), *writesetp);
501                 if (buffer_len(&stderr_buffer) > 0)
502                         FD_SET(fileno(stderr), *writesetp);
503         } else {
504                 /* channel_prepare_select could have closed the last channel */
505                 if (session_closed && !channel_still_open() &&
506                     !packet_have_data_to_write()) {
507                         /* clear mask since we did not call select() */
508                         memset(*readsetp, 0, *nallocp);
509                         memset(*writesetp, 0, *nallocp);
510                         return;
511                 } else {
512                         FD_SET(connection_in, *readsetp);
513                 }
514         }
515
516         /* Select server connection if have data to write to the server. */
517         if (packet_have_data_to_write())
518                 FD_SET(connection_out, *writesetp);
519
520         if (control_fd != -1)
521                 FD_SET(control_fd, *readsetp);
522
523         /*
524          * Wait for something to happen.  This will suspend the process until
525          * some selected descriptor can be read, written, or has some other
526          * event pending.
527          */
528
529         if (options.server_alive_interval == 0 || !compat20)
530                 tvp = NULL;
531         else {
532                 tv.tv_sec = options.server_alive_interval;
533                 tv.tv_usec = 0;
534                 tvp = &tv;
535         }
536         ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
537         if (ret < 0) {
538                 char buf[100];
539
540                 /*
541                  * We have to clear the select masks, because we return.
542                  * We have to return, because the mainloop checks for the flags
543                  * set by the signal handlers.
544                  */
545                 memset(*readsetp, 0, *nallocp);
546                 memset(*writesetp, 0, *nallocp);
547
548                 if (errno == EINTR)
549                         return;
550                 /* Note: we might still have data in the buffers. */
551                 snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
552                 buffer_append(&stderr_buffer, buf, strlen(buf));
553                 quit_pending = 1;
554         } else if (ret == 0)
555                 server_alive_check();
556 }
557
558 static void
559 client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
560 {
561         /* Flush stdout and stderr buffers. */
562         if (buffer_len(bout) > 0)
563                 atomicio(vwrite, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
564         if (buffer_len(berr) > 0)
565                 atomicio(vwrite, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
566
567         leave_raw_mode();
568
569         /*
570          * Free (and clear) the buffer to reduce the amount of data that gets
571          * written to swap.
572          */
573         buffer_free(bin);
574         buffer_free(bout);
575         buffer_free(berr);
576
577         /* Send the suspend signal to the program itself. */
578         kill(getpid(), SIGTSTP);
579
580         /* Reset window sizes in case they have changed */
581         received_window_change_signal = 1;
582
583         /* OK, we have been continued by the user. Reinitialize buffers. */
584         buffer_init(bin);
585         buffer_init(bout);
586         buffer_init(berr);
587
588         enter_raw_mode();
589 }
590
591 static void
592 client_process_net_input(fd_set *readset)
593 {
594         int len;
595         char buf[8192];
596
597         /*
598          * Read input from the server, and add any such data to the buffer of
599          * the packet subsystem.
600          */
601         if (FD_ISSET(connection_in, readset)) {
602                 /* Read as much as possible. */
603                 len = read(connection_in, buf, sizeof(buf));
604                 if (len == 0) {
605                         /* Received EOF.  The remote host has closed the connection. */
606                         snprintf(buf, sizeof buf, "Connection to %.300s closed by remote host.\r\n",
607                                  host);
608                         buffer_append(&stderr_buffer, buf, strlen(buf));
609                         quit_pending = 1;
610                         return;
611                 }
612                 /*
613                  * There is a kernel bug on Solaris that causes select to
614                  * sometimes wake up even though there is no data available.
615                  */
616                 if (len < 0 && (errno == EAGAIN || errno == EINTR))
617                         len = 0;
618
619                 if (len < 0) {
620                         /* An error has encountered.  Perhaps there is a network problem. */
621                         snprintf(buf, sizeof buf, "Read from remote host %.300s: %.100s\r\n",
622                                  host, strerror(errno));
623                         buffer_append(&stderr_buffer, buf, strlen(buf));
624                         quit_pending = 1;
625                         return;
626                 }
627                 packet_process_incoming(buf, len);
628         }
629 }
630
631 static void
632 client_subsystem_reply(int type, u_int32_t seq, void *ctxt)
633 {
634         int id;
635         Channel *c;
636
637         id = packet_get_int();
638         packet_check_eom();
639
640         if ((c = channel_lookup(id)) == NULL) {
641                 error("%s: no channel for id %d", __func__, id);
642                 return;
643         }
644
645         if (type == SSH2_MSG_CHANNEL_SUCCESS)
646                 debug2("Request suceeded on channel %d", id);
647         else if (type == SSH2_MSG_CHANNEL_FAILURE) {
648                 error("Request failed on channel %d", id);
649                 channel_free(c);
650         }
651 }
652
653 static void
654 client_extra_session2_setup(int id, void *arg)
655 {
656         struct confirm_ctx *cctx = arg;
657         const char *display;
658         Channel *c;
659         int i;
660
661         if (cctx == NULL)
662                 fatal("%s: cctx == NULL", __func__);
663         if ((c = channel_lookup(id)) == NULL)
664                 fatal("%s: no channel for id %d", __func__, id);
665
666         display = getenv("DISPLAY");
667         if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
668                 char *proto, *data;
669                 /* Get reasonable local authentication information. */
670                 client_x11_get_proto(display, options.xauth_location,
671                     options.forward_x11_trusted, &proto, &data);
672                 /* Request forwarding with authentication spoofing. */
673                 debug("Requesting X11 forwarding with authentication spoofing.");
674                 x11_request_forwarding_with_spoofing(id, display, proto, data);
675                 /* XXX wait for reply */
676         }
677
678         if (cctx->want_agent_fwd && options.forward_agent) {
679                 debug("Requesting authentication agent forwarding.");
680                 channel_request_start(id, "auth-agent-req@openssh.com", 0);
681                 packet_send();
682         }
683
684         client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
685             cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env,
686             client_subsystem_reply);
687
688         c->confirm_ctx = NULL;
689         buffer_free(&cctx->cmd);
690         xfree(cctx->term);
691         if (cctx->env != NULL) {
692                 for (i = 0; cctx->env[i] != NULL; i++)
693                         xfree(cctx->env[i]);
694                 xfree(cctx->env);
695         }
696         xfree(cctx);
697 }
698
699 static void
700 client_process_control(fd_set *readset)
701 {
702         Buffer m;
703         Channel *c;
704         int client_fd, new_fd[3], ver, allowed;
705         socklen_t addrlen;
706         struct sockaddr_storage addr;
707         struct confirm_ctx *cctx;
708         char *cmd;
709         u_int i, len, env_len, command, flags;
710         uid_t euid;
711         gid_t egid;
712
713         /*
714          * Accept connection on control socket
715          */
716         if (control_fd == -1 || !FD_ISSET(control_fd, readset))
717                 return;
718
719         memset(&addr, 0, sizeof(addr));
720         addrlen = sizeof(addr);
721         if ((client_fd = accept(control_fd,
722             (struct sockaddr*)&addr, &addrlen)) == -1) {
723                 error("%s accept: %s", __func__, strerror(errno));
724                 return;
725         }
726
727         if (getpeereid(client_fd, &euid, &egid) < 0) {
728                 error("%s getpeereid failed: %s", __func__, strerror(errno));
729                 close(client_fd);
730                 return;
731         }
732         if ((euid != 0) && (getuid() != euid)) {
733                 error("control mode uid mismatch: peer euid %u != uid %u",
734                     (u_int) euid, (u_int) getuid());
735                 close(client_fd);
736                 return;
737         }
738
739         unset_nonblock(client_fd);
740
741         /* Read command */
742         buffer_init(&m);
743         if (ssh_msg_recv(client_fd, &m) == -1) {
744                 error("%s: client msg_recv failed", __func__);
745                 close(client_fd);
746                 buffer_free(&m);
747                 return;
748         }
749         if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
750                 error("%s: wrong client version %d", __func__, ver);
751                 buffer_free(&m);
752                 close(client_fd);
753                 return;
754         }
755
756         allowed = 1;
757         command = buffer_get_int(&m);
758         flags = buffer_get_int(&m);
759
760         buffer_clear(&m);
761
762         switch (command) {
763         case SSHMUX_COMMAND_OPEN:
764                 if (options.control_master == SSHCTL_MASTER_ASK ||
765                     options.control_master == SSHCTL_MASTER_AUTO_ASK)
766                         allowed = ask_permission("Allow shared connection "
767                             "to %s? ", host);
768                 /* continue below */
769                 break;
770         case SSHMUX_COMMAND_TERMINATE:
771                 if (options.control_master == SSHCTL_MASTER_ASK ||
772                     options.control_master == SSHCTL_MASTER_AUTO_ASK)
773                         allowed = ask_permission("Terminate shared connection "
774                             "to %s? ", host);
775                 if (allowed)
776                         quit_pending = 1;
777                 /* FALLTHROUGH */
778         case SSHMUX_COMMAND_ALIVE_CHECK:
779                 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
780                 buffer_clear(&m);
781                 buffer_put_int(&m, allowed);
782                 buffer_put_int(&m, getpid());
783                 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
784                         error("%s: client msg_send failed", __func__);
785                         close(client_fd);
786                         buffer_free(&m);
787                         return;
788                 }
789                 buffer_free(&m);
790                 close(client_fd);
791                 return;
792         default:
793                 error("Unsupported command %d", command);
794                 buffer_free(&m);
795                 close(client_fd);
796                 return;
797         }
798
799         /* Reply for SSHMUX_COMMAND_OPEN */
800         buffer_clear(&m);
801         buffer_put_int(&m, allowed);
802         buffer_put_int(&m, getpid());
803         if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
804                 error("%s: client msg_send failed", __func__);
805                 close(client_fd);
806                 buffer_free(&m);
807                 return;
808         }
809
810         if (!allowed) {
811                 error("Refused control connection");
812                 close(client_fd);
813                 buffer_free(&m);
814                 return;
815         }
816
817         buffer_clear(&m);
818         if (ssh_msg_recv(client_fd, &m) == -1) {
819                 error("%s: client msg_recv failed", __func__);
820                 close(client_fd);
821                 buffer_free(&m);
822                 return;
823         }
824         if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
825                 error("%s: wrong client version %d", __func__, ver);
826                 buffer_free(&m);
827                 close(client_fd);
828                 return;
829         }
830
831         cctx = xcalloc(1, sizeof(*cctx));
832         cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
833         cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
834         cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
835         cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
836         cctx->term = buffer_get_string(&m, &len);
837
838         cmd = buffer_get_string(&m, &len);
839         buffer_init(&cctx->cmd);
840         buffer_append(&cctx->cmd, cmd, strlen(cmd));
841
842         env_len = buffer_get_int(&m);
843         env_len = MIN(env_len, 4096);
844         debug3("%s: receiving %d env vars", __func__, env_len);
845         if (env_len != 0) {
846                 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
847                 for (i = 0; i < env_len; i++)
848                         cctx->env[i] = buffer_get_string(&m, &len);
849                 cctx->env[i] = NULL;
850         }
851
852         debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
853             cctx->want_tty, cctx->want_subsys, cmd);
854         xfree(cmd);
855
856         /* Gather fds from client */
857         new_fd[0] = mm_receive_fd(client_fd);
858         new_fd[1] = mm_receive_fd(client_fd);
859         new_fd[2] = mm_receive_fd(client_fd);
860
861         debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
862             new_fd[0], new_fd[1], new_fd[2]);
863
864         /* Try to pick up ttymodes from client before it goes raw */
865         if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
866                 error("%s: tcgetattr: %s", __func__, strerror(errno));
867
868         /* This roundtrip is just for synchronisation of ttymodes */
869         buffer_clear(&m);
870         if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
871                 error("%s: client msg_send failed", __func__);
872                 close(client_fd);
873                 close(new_fd[0]);
874                 close(new_fd[1]);
875                 close(new_fd[2]);
876                 buffer_free(&m);
877                 xfree(cctx->term);
878                 if (env_len != 0) {
879                         for (i = 0; i < env_len; i++)
880                                 xfree(cctx->env[i]);
881                         xfree(cctx->env);
882                 }
883                 return;
884         }
885         buffer_free(&m);
886
887         /* enable nonblocking unless tty */
888         if (!isatty(new_fd[0]))
889                 set_nonblock(new_fd[0]);
890         if (!isatty(new_fd[1]))
891                 set_nonblock(new_fd[1]);
892         if (!isatty(new_fd[2]))
893                 set_nonblock(new_fd[2]);
894
895         set_nonblock(client_fd);
896
897         c = channel_new("session", SSH_CHANNEL_OPENING,
898             new_fd[0], new_fd[1], new_fd[2],
899             CHAN_SES_WINDOW_DEFAULT, CHAN_SES_PACKET_DEFAULT,
900             CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
901
902         /* XXX */
903         c->ctl_fd = client_fd;
904
905         debug3("%s: channel_new: %d", __func__, c->self);
906
907         channel_send_open(c->self);
908         channel_register_confirm(c->self, client_extra_session2_setup, cctx);
909 }
910
911 static void
912 process_cmdline(void)
913 {
914         void (*handler)(int);
915         char *s, *cmd, *cancel_host;
916         int delete = 0;
917         int local = 0;
918         u_short cancel_port;
919         Forward fwd;
920
921         leave_raw_mode();
922         handler = signal(SIGINT, SIG_IGN);
923         cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
924         if (s == NULL)
925                 goto out;
926         while (*s && isspace(*s))
927                 s++;
928         if (*s == '-')
929                 s++;    /* Skip cmdline '-', if any */
930         if (*s == '\0')
931                 goto out;
932
933         if (*s == 'h' || *s == 'H' || *s == '?') {
934                 logit("Commands:");
935                 logit("      -L[bind_address:]port:host:hostport    "
936                     "Request local forward");
937                 logit("      -R[bind_address:]port:host:hostport    "
938                     "Request remote forward");
939                 logit("      -KR[bind_address:]port                 "
940                     "Cancel remote forward");
941                 if (!options.permit_local_command)
942                         goto out;
943                 logit("      !args                                  "
944                     "Execute local command");
945                 goto out;
946         }
947
948         if (*s == '!' && options.permit_local_command) {
949                 s++;
950                 ssh_local_cmd(s);
951                 goto out;
952         }
953
954         if (*s == 'K') {
955                 delete = 1;
956                 s++;
957         }
958         if (*s != 'L' && *s != 'R') {
959                 logit("Invalid command.");
960                 goto out;
961         }
962         if (*s == 'L')
963                 local = 1;
964         if (local && delete) {
965                 logit("Not supported.");
966                 goto out;
967         }
968         if ((!local || delete) && !compat20) {
969                 logit("Not supported for SSH protocol version 1.");
970                 goto out;
971         }
972
973         s++;
974         while (*s && isspace(*s))
975                 s++;
976
977         if (delete) {
978                 cancel_port = 0;
979                 cancel_host = hpdelim(&s);      /* may be NULL */
980                 if (s != NULL) {
981                         cancel_port = a2port(s);
982                         cancel_host = cleanhostname(cancel_host);
983                 } else {
984                         cancel_port = a2port(cancel_host);
985                         cancel_host = NULL;
986                 }
987                 if (cancel_port == 0) {
988                         logit("Bad forwarding close port");
989                         goto out;
990                 }
991                 channel_request_rforward_cancel(cancel_host, cancel_port);
992         } else {
993                 if (!parse_forward(&fwd, s)) {
994                         logit("Bad forwarding specification.");
995                         goto out;
996                 }
997                 if (local) {
998                         if (channel_setup_local_fwd_listener(fwd.listen_host,
999                             fwd.listen_port, fwd.connect_host,
1000                             fwd.connect_port, options.gateway_ports) < 0) {
1001                                 logit("Port forwarding failed.");
1002                                 goto out;
1003                         }
1004                 } else {
1005                         if (channel_request_remote_forwarding(fwd.listen_host,
1006                             fwd.listen_port, fwd.connect_host,
1007                             fwd.connect_port) < 0) {
1008                                 logit("Port forwarding failed.");
1009                                 goto out;
1010                         }
1011                 }
1012
1013                 logit("Forwarding port.");
1014         }
1015
1016 out:
1017         signal(SIGINT, handler);
1018         enter_raw_mode();
1019         if (cmd)
1020                 xfree(cmd);
1021 }
1022
1023 /* process the characters one by one */
1024 static int
1025 process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
1026 {
1027         char string[1024];
1028         pid_t pid;
1029         int bytes = 0;
1030         u_int i;
1031         u_char ch;
1032         char *s;
1033
1034         if (len <= 0)
1035                 return (0);
1036
1037         for (i = 0; i < (u_int)len; i++) {
1038                 /* Get one character at a time. */
1039                 ch = buf[i];
1040
1041                 if (escape_pending) {
1042                         /* We have previously seen an escape character. */
1043                         /* Clear the flag now. */
1044                         escape_pending = 0;
1045
1046                         /* Process the escaped character. */
1047                         switch (ch) {
1048                         case '.':
1049                                 /* Terminate the connection. */
1050                                 snprintf(string, sizeof string, "%c.\r\n", escape_char);
1051                                 buffer_append(berr, string, strlen(string));
1052
1053                                 quit_pending = 1;
1054                                 return -1;
1055
1056                         case 'Z' - 64:
1057                                 /* Suspend the program. */
1058                                 /* Print a message to that effect to the user. */
1059                                 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
1060                                 buffer_append(berr, string, strlen(string));
1061
1062                                 /* Restore terminal modes and suspend. */
1063                                 client_suspend_self(bin, bout, berr);
1064
1065                                 /* We have been continued. */
1066                                 continue;
1067
1068                         case 'B':
1069                                 if (compat20) {
1070                                         snprintf(string, sizeof string,
1071                                             "%cB\r\n", escape_char);
1072                                         buffer_append(berr, string,
1073                                             strlen(string));
1074                                         channel_request_start(session_ident,
1075                                             "break", 0);
1076                                         packet_put_int(1000);
1077                                         packet_send();
1078                                 }
1079                                 continue;
1080
1081                         case 'R':
1082                                 if (compat20) {
1083                                         if (datafellows & SSH_BUG_NOREKEY)
1084                                                 logit("Server does not support re-keying");
1085                                         else
1086                                                 need_rekeying = 1;
1087                                 }
1088                                 continue;
1089
1090                         case '&':
1091                                 /*
1092                                  * Detach the program (continue to serve connections,
1093                                  * but put in background and no more new connections).
1094                                  */
1095                                 /* Restore tty modes. */
1096                                 leave_raw_mode();
1097
1098                                 /* Stop listening for new connections. */
1099                                 channel_stop_listening();
1100
1101                                 snprintf(string, sizeof string,
1102                                     "%c& [backgrounded]\n", escape_char);
1103                                 buffer_append(berr, string, strlen(string));
1104
1105                                 /* Fork into background. */
1106                                 pid = fork();
1107                                 if (pid < 0) {
1108                                         error("fork: %.100s", strerror(errno));
1109                                         continue;
1110                                 }
1111                                 if (pid != 0) { /* This is the parent. */
1112                                         /* The parent just exits. */
1113                                         exit(0);
1114                                 }
1115                                 /* The child continues serving connections. */
1116                                 if (compat20) {
1117                                         buffer_append(bin, "\004", 1);
1118                                         /* fake EOF on stdin */
1119                                         return -1;
1120                                 } else if (!stdin_eof) {
1121                                         /*
1122                                          * Sending SSH_CMSG_EOF alone does not always appear
1123                                          * to be enough.  So we try to send an EOF character
1124                                          * first.
1125                                          */
1126                                         packet_start(SSH_CMSG_STDIN_DATA);
1127                                         packet_put_string("\004", 1);
1128                                         packet_send();
1129                                         /* Close stdin. */
1130                                         stdin_eof = 1;
1131                                         if (buffer_len(bin) == 0) {
1132                                                 packet_start(SSH_CMSG_EOF);
1133                                                 packet_send();
1134                                         }
1135                                 }
1136                                 continue;
1137
1138                         case '?':
1139                                 snprintf(string, sizeof string,
1140 "%c?\r\n\
1141 Supported escape sequences:\r\n\
1142 %c.  - terminate connection\r\n\
1143 %cB  - send a BREAK to the remote system\r\n\
1144 %cC  - open a command line\r\n\
1145 %cR  - Request rekey (SSH protocol 2 only)\r\n\
1146 %c^Z - suspend ssh\r\n\
1147 %c#  - list forwarded connections\r\n\
1148 %c&  - background ssh (when waiting for connections to terminate)\r\n\
1149 %c?  - this message\r\n\
1150 %c%c  - send the escape character by typing it twice\r\n\
1151 (Note that escapes are only recognized immediately after newline.)\r\n",
1152                                     escape_char, escape_char, escape_char, escape_char,
1153                                     escape_char, escape_char, escape_char, escape_char,
1154                                     escape_char, escape_char, escape_char);
1155                                 buffer_append(berr, string, strlen(string));
1156                                 continue;
1157
1158                         case '#':
1159                                 snprintf(string, sizeof string, "%c#\r\n", escape_char);
1160                                 buffer_append(berr, string, strlen(string));
1161                                 s = channel_open_message();
1162                                 buffer_append(berr, s, strlen(s));
1163                                 xfree(s);
1164                                 continue;
1165
1166                         case 'C':
1167                                 process_cmdline();
1168                                 continue;
1169
1170                         default:
1171                                 if (ch != escape_char) {
1172                                         buffer_put_char(bin, escape_char);
1173                                         bytes++;
1174                                 }
1175                                 /* Escaped characters fall through here */
1176                                 break;
1177                         }
1178                 } else {
1179                         /*
1180                          * The previous character was not an escape char. Check if this
1181                          * is an escape.
1182                          */
1183                         if (last_was_cr && ch == escape_char) {
1184                                 /* It is. Set the flag and continue to next character. */
1185                                 escape_pending = 1;
1186                                 continue;
1187                         }
1188                 }
1189
1190                 /*
1191                  * Normal character.  Record whether it was a newline,
1192                  * and append it to the buffer.
1193                  */
1194                 last_was_cr = (ch == '\r' || ch == '\n');
1195                 buffer_put_char(bin, ch);
1196                 bytes++;
1197         }
1198         return bytes;
1199 }
1200
1201 static void
1202 client_process_input(fd_set *readset)
1203 {
1204         int len;
1205         char buf[8192];
1206
1207         /* Read input from stdin. */
1208         if (FD_ISSET(fileno(stdin), readset)) {
1209                 /* Read as much as possible. */
1210                 len = read(fileno(stdin), buf, sizeof(buf));
1211                 if (len < 0 && (errno == EAGAIN || errno == EINTR))
1212                         return;         /* we'll try again later */
1213                 if (len <= 0) {
1214                         /*
1215                          * Received EOF or error.  They are treated
1216                          * similarly, except that an error message is printed
1217                          * if it was an error condition.
1218                          */
1219                         if (len < 0) {
1220                                 snprintf(buf, sizeof buf, "read: %.100s\r\n", strerror(errno));
1221                                 buffer_append(&stderr_buffer, buf, strlen(buf));
1222                         }
1223                         /* Mark that we have seen EOF. */
1224                         stdin_eof = 1;
1225                         /*
1226                          * Send an EOF message to the server unless there is
1227                          * data in the buffer.  If there is data in the
1228                          * buffer, no message will be sent now.  Code
1229                          * elsewhere will send the EOF when the buffer
1230                          * becomes empty if stdin_eof is set.
1231                          */
1232                         if (buffer_len(&stdin_buffer) == 0) {
1233                                 packet_start(SSH_CMSG_EOF);
1234                                 packet_send();
1235                         }
1236                 } else if (escape_char == SSH_ESCAPECHAR_NONE) {
1237                         /*
1238                          * Normal successful read, and no escape character.
1239                          * Just append the data to buffer.
1240                          */
1241                         buffer_append(&stdin_buffer, buf, len);
1242                 } else {
1243                         /*
1244                          * Normal, successful read.  But we have an escape character
1245                          * and have to process the characters one by one.
1246                          */
1247                         if (process_escapes(&stdin_buffer, &stdout_buffer,
1248                             &stderr_buffer, buf, len) == -1)
1249                                 return;
1250                 }
1251         }
1252 }
1253
1254 static void
1255 client_process_output(fd_set *writeset)
1256 {
1257         int len;
1258         char buf[100];
1259
1260         /* Write buffered output to stdout. */
1261         if (FD_ISSET(fileno(stdout), writeset)) {
1262                 /* Write as much data as possible. */
1263                 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1264                     buffer_len(&stdout_buffer));
1265                 if (len <= 0) {
1266                         if (errno == EINTR || errno == EAGAIN)
1267                                 len = 0;
1268                         else {
1269                                 /*
1270                                  * An error or EOF was encountered.  Put an
1271                                  * error message to stderr buffer.
1272                                  */
1273                                 snprintf(buf, sizeof buf, "write stdout: %.50s\r\n", strerror(errno));
1274                                 buffer_append(&stderr_buffer, buf, strlen(buf));
1275                                 quit_pending = 1;
1276                                 return;
1277                         }
1278                 }
1279                 /* Consume printed data from the buffer. */
1280                 buffer_consume(&stdout_buffer, len);
1281                 stdout_bytes += len;
1282         }
1283         /* Write buffered output to stderr. */
1284         if (FD_ISSET(fileno(stderr), writeset)) {
1285                 /* Write as much data as possible. */
1286                 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1287                     buffer_len(&stderr_buffer));
1288                 if (len <= 0) {
1289                         if (errno == EINTR || errno == EAGAIN)
1290                                 len = 0;
1291                         else {
1292                                 /* EOF or error, but can't even print error message. */
1293                                 quit_pending = 1;
1294                                 return;
1295                         }
1296                 }
1297                 /* Consume printed characters from the buffer. */
1298                 buffer_consume(&stderr_buffer, len);
1299                 stderr_bytes += len;
1300         }
1301 }
1302
1303 /*
1304  * Get packets from the connection input buffer, and process them as long as
1305  * there are packets available.
1306  *
1307  * Any unknown packets received during the actual
1308  * session cause the session to terminate.  This is
1309  * intended to make debugging easier since no
1310  * confirmations are sent.  Any compatible protocol
1311  * extensions must be negotiated during the
1312  * preparatory phase.
1313  */
1314
1315 static void
1316 client_process_buffered_input_packets(void)
1317 {
1318         dispatch_run(DISPATCH_NONBLOCK, &quit_pending, compat20 ? xxx_kex : NULL);
1319 }
1320
1321 /* scan buf[] for '~' before sending data to the peer */
1322
1323 static int
1324 simple_escape_filter(Channel *c, char *buf, int len)
1325 {
1326         /* XXX we assume c->extended is writeable */
1327         return process_escapes(&c->input, &c->output, &c->extended, buf, len);
1328 }
1329
1330 static void
1331 client_channel_closed(int id, void *arg)
1332 {
1333         channel_cancel_cleanup(id);
1334         session_closed = 1;
1335         leave_raw_mode();
1336 }
1337
1338 /*
1339  * Implements the interactive session with the server.  This is called after
1340  * the user has been authenticated, and a command has been started on the
1341  * remote host.  If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1342  * used as an escape character for terminating or suspending the session.
1343  */
1344
1345 int
1346 client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1347 {
1348         fd_set *readset = NULL, *writeset = NULL;
1349         double start_time, total_time;
1350         int max_fd = 0, max_fd2 = 0, len, rekeying = 0;
1351         u_int nalloc = 0;
1352         char buf[100];
1353
1354         debug("Entering interactive session.");
1355
1356         start_time = get_current_time();
1357
1358         /* Initialize variables. */
1359         escape_pending = 0;
1360         last_was_cr = 1;
1361         exit_status = -1;
1362         stdin_eof = 0;
1363         buffer_high = 64 * 1024;
1364         connection_in = packet_get_connection_in();
1365         connection_out = packet_get_connection_out();
1366         max_fd = MAX(connection_in, connection_out);
1367         if (control_fd != -1)
1368                 max_fd = MAX(max_fd, control_fd);
1369
1370         if (!compat20) {
1371                 /* enable nonblocking unless tty */
1372                 if (!isatty(fileno(stdin)))
1373                         set_nonblock(fileno(stdin));
1374                 if (!isatty(fileno(stdout)))
1375                         set_nonblock(fileno(stdout));
1376                 if (!isatty(fileno(stderr)))
1377                         set_nonblock(fileno(stderr));
1378                 max_fd = MAX(max_fd, fileno(stdin));
1379                 max_fd = MAX(max_fd, fileno(stdout));
1380                 max_fd = MAX(max_fd, fileno(stderr));
1381         }
1382         stdin_bytes = 0;
1383         stdout_bytes = 0;
1384         stderr_bytes = 0;
1385         quit_pending = 0;
1386         escape_char = escape_char_arg;
1387
1388         /* Initialize buffers. */
1389         buffer_init(&stdin_buffer);
1390         buffer_init(&stdout_buffer);
1391         buffer_init(&stderr_buffer);
1392
1393         client_init_dispatch();
1394
1395         /*
1396          * Set signal handlers, (e.g. to restore non-blocking mode)
1397          * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1398          */
1399         if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1400                 signal(SIGHUP, signal_handler);
1401         if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1402                 signal(SIGINT, signal_handler);
1403         if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1404                 signal(SIGQUIT, signal_handler);
1405         if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1406                 signal(SIGTERM, signal_handler);
1407         signal(SIGWINCH, window_change_handler);
1408
1409         if (have_pty)
1410                 enter_raw_mode();
1411
1412         if (compat20) {
1413                 session_ident = ssh2_chan_id;
1414                 if (escape_char != SSH_ESCAPECHAR_NONE)
1415                         channel_register_filter(session_ident,
1416                             simple_escape_filter, NULL);
1417                 if (session_ident != -1)
1418                         channel_register_cleanup(session_ident,
1419                             client_channel_closed, 0);
1420         } else {
1421                 /* Check if we should immediately send eof on stdin. */
1422                 client_check_initial_eof_on_stdin();
1423         }
1424
1425         /* Main loop of the client for the interactive session mode. */
1426         while (!quit_pending) {
1427
1428                 /* Process buffered packets sent by the server. */
1429                 client_process_buffered_input_packets();
1430
1431                 if (compat20 && session_closed && !channel_still_open())
1432                         break;
1433
1434                 rekeying = (xxx_kex != NULL && !xxx_kex->done);
1435
1436                 if (rekeying) {
1437                         debug("rekeying in progress");
1438                 } else {
1439                         /*
1440                          * Make packets of buffered stdin data, and buffer
1441                          * them for sending to the server.
1442                          */
1443                         if (!compat20)
1444                                 client_make_packets_from_stdin_data();
1445
1446                         /*
1447                          * Make packets from buffered channel data, and
1448                          * enqueue them for sending to the server.
1449                          */
1450                         if (packet_not_very_much_data_to_write())
1451                                 channel_output_poll();
1452
1453                         /*
1454                          * Check if the window size has changed, and buffer a
1455                          * message about it to the server if so.
1456                          */
1457                         client_check_window_change();
1458
1459                         if (quit_pending)
1460                                 break;
1461                 }
1462                 /*
1463                  * Wait until we have something to do (something becomes
1464                  * available on one of the descriptors).
1465                  */
1466                 max_fd2 = max_fd;
1467                 client_wait_until_can_do_something(&readset, &writeset,
1468                     &max_fd2, &nalloc, rekeying);
1469
1470                 if (quit_pending)
1471                         break;
1472
1473                 /* Do channel operations unless rekeying in progress. */
1474                 if (!rekeying) {
1475                         channel_after_select(readset, writeset);
1476                         if (need_rekeying || packet_need_rekeying()) {
1477                                 debug("need rekeying");
1478                                 xxx_kex->done = 0;
1479                                 kex_send_kexinit(xxx_kex);
1480                                 need_rekeying = 0;
1481                         }
1482                 }
1483
1484                 /* Buffer input from the connection.  */
1485                 client_process_net_input(readset);
1486
1487                 /* Accept control connections.  */
1488                 client_process_control(readset);
1489
1490                 if (quit_pending)
1491                         break;
1492
1493                 if (!compat20) {
1494                         /* Buffer data from stdin */
1495                         client_process_input(readset);
1496                         /*
1497                          * Process output to stdout and stderr.  Output to
1498                          * the connection is processed elsewhere (above).
1499                          */
1500                         client_process_output(writeset);
1501                 }
1502
1503                 /* Send as much buffered packet data as possible to the sender. */
1504                 if (FD_ISSET(connection_out, writeset))
1505                         packet_write_poll();
1506         }
1507         if (readset)
1508                 xfree(readset);
1509         if (writeset)
1510                 xfree(writeset);
1511
1512         /* Terminate the session. */
1513
1514         /* Stop watching for window change. */
1515         signal(SIGWINCH, SIG_DFL);
1516
1517         channel_free_all();
1518
1519         if (have_pty)
1520                 leave_raw_mode();
1521
1522         /* restore blocking io */
1523         if (!isatty(fileno(stdin)))
1524                 unset_nonblock(fileno(stdin));
1525         if (!isatty(fileno(stdout)))
1526                 unset_nonblock(fileno(stdout));
1527         if (!isatty(fileno(stderr)))
1528                 unset_nonblock(fileno(stderr));
1529
1530         /*
1531          * If there was no shell or command requested, there will be no remote
1532          * exit status to be returned.  In that case, clear error code if the
1533          * connection was deliberately terminated at this end.
1534          */
1535         if (no_shell_flag && received_signal == SIGTERM) {
1536                 received_signal = 0;
1537                 exit_status = 0;
1538         }
1539
1540         if (received_signal)
1541                 fatal("Killed by signal %d.", (int) received_signal);
1542
1543         /*
1544          * In interactive mode (with pseudo tty) display a message indicating
1545          * that the connection has been closed.
1546          */
1547         if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1548                 snprintf(buf, sizeof buf, "Connection to %.64s closed.\r\n", host);
1549                 buffer_append(&stderr_buffer, buf, strlen(buf));
1550         }
1551
1552         /* Output any buffered data for stdout. */
1553         while (buffer_len(&stdout_buffer) > 0) {
1554                 len = write(fileno(stdout), buffer_ptr(&stdout_buffer),
1555                     buffer_len(&stdout_buffer));
1556                 if (len <= 0) {
1557                         error("Write failed flushing stdout buffer.");
1558                         break;
1559                 }
1560                 buffer_consume(&stdout_buffer, len);
1561                 stdout_bytes += len;
1562         }
1563
1564         /* Output any buffered data for stderr. */
1565         while (buffer_len(&stderr_buffer) > 0) {
1566                 len = write(fileno(stderr), buffer_ptr(&stderr_buffer),
1567                     buffer_len(&stderr_buffer));
1568                 if (len <= 0) {
1569                         error("Write failed flushing stderr buffer.");
1570                         break;
1571                 }
1572                 buffer_consume(&stderr_buffer, len);
1573                 stderr_bytes += len;
1574         }
1575
1576         /* Clear and free any buffers. */
1577         memset(buf, 0, sizeof(buf));
1578         buffer_free(&stdin_buffer);
1579         buffer_free(&stdout_buffer);
1580         buffer_free(&stderr_buffer);
1581
1582         /* Report bytes transferred, and transfer rates. */
1583         total_time = get_current_time() - start_time;
1584         debug("Transferred: stdin %lu, stdout %lu, stderr %lu bytes in %.1f seconds",
1585             stdin_bytes, stdout_bytes, stderr_bytes, total_time);
1586         if (total_time > 0)
1587                 debug("Bytes per second: stdin %.1f, stdout %.1f, stderr %.1f",
1588                     stdin_bytes / total_time, stdout_bytes / total_time,
1589                     stderr_bytes / total_time);
1590
1591         /* Return the exit status of the program. */
1592         debug("Exit status %d", exit_status);
1593         return exit_status;
1594 }
1595
1596 /*********/
1597
1598 static void
1599 client_input_stdout_data(int type, u_int32_t seq, void *ctxt)
1600 {
1601         u_int data_len;
1602         char *data = packet_get_string(&data_len);
1603         packet_check_eom();
1604         buffer_append(&stdout_buffer, data, data_len);
1605         memset(data, 0, data_len);
1606         xfree(data);
1607 }
1608 static void
1609 client_input_stderr_data(int type, u_int32_t seq, void *ctxt)
1610 {
1611         u_int data_len;
1612         char *data = packet_get_string(&data_len);
1613         packet_check_eom();
1614         buffer_append(&stderr_buffer, data, data_len);
1615         memset(data, 0, data_len);
1616         xfree(data);
1617 }
1618 static void
1619 client_input_exit_status(int type, u_int32_t seq, void *ctxt)
1620 {
1621         exit_status = packet_get_int();
1622         packet_check_eom();
1623         /* Acknowledge the exit. */
1624         packet_start(SSH_CMSG_EXIT_CONFIRMATION);
1625         packet_send();
1626         /*
1627          * Must wait for packet to be sent since we are
1628          * exiting the loop.
1629          */
1630         packet_write_wait();
1631         /* Flag that we want to exit. */
1632         quit_pending = 1;
1633 }
1634 static void
1635 client_input_agent_open(int type, u_int32_t seq, void *ctxt)
1636 {
1637         Channel *c = NULL;
1638         int remote_id, sock;
1639
1640         /* Read the remote channel number from the message. */
1641         remote_id = packet_get_int();
1642         packet_check_eom();
1643
1644         /*
1645          * Get a connection to the local authentication agent (this may again
1646          * get forwarded).
1647          */
1648         sock = ssh_get_authentication_socket();
1649
1650         /*
1651          * If we could not connect the agent, send an error message back to
1652          * the server. This should never happen unless the agent dies,
1653          * because authentication forwarding is only enabled if we have an
1654          * agent.
1655          */
1656         if (sock >= 0) {
1657                 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
1658                     -1, 0, 0, 0, "authentication agent connection", 1);
1659                 c->remote_id = remote_id;
1660                 c->force_drain = 1;
1661         }
1662         if (c == NULL) {
1663                 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1664                 packet_put_int(remote_id);
1665         } else {
1666                 /* Send a confirmation to the remote host. */
1667                 debug("Forwarding authentication connection.");
1668                 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1669                 packet_put_int(remote_id);
1670                 packet_put_int(c->self);
1671         }
1672         packet_send();
1673 }
1674
1675 static Channel *
1676 client_request_forwarded_tcpip(const char *request_type, int rchan)
1677 {
1678         Channel *c = NULL;
1679         char *listen_address, *originator_address;
1680         int listen_port, originator_port;
1681         int sock;
1682
1683         /* Get rest of the packet */
1684         listen_address = packet_get_string(NULL);
1685         listen_port = packet_get_int();
1686         originator_address = packet_get_string(NULL);
1687         originator_port = packet_get_int();
1688         packet_check_eom();
1689
1690         debug("client_request_forwarded_tcpip: listen %s port %d, originator %s port %d",
1691             listen_address, listen_port, originator_address, originator_port);
1692
1693         sock = channel_connect_by_listen_address(listen_port);
1694         if (sock < 0) {
1695                 xfree(originator_address);
1696                 xfree(listen_address);
1697                 return NULL;
1698         }
1699         c = channel_new("forwarded-tcpip",
1700             SSH_CHANNEL_CONNECTING, sock, sock, -1,
1701             CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1702             originator_address, 1);
1703         xfree(originator_address);
1704         xfree(listen_address);
1705         return c;
1706 }
1707
1708 static Channel *
1709 client_request_x11(const char *request_type, int rchan)
1710 {
1711         Channel *c = NULL;
1712         char *originator;
1713         int originator_port;
1714         int sock;
1715
1716         if (!options.forward_x11) {
1717                 error("Warning: ssh server tried X11 forwarding.");
1718                 error("Warning: this is probably a break-in attempt by a malicious server.");
1719                 return NULL;
1720         }
1721         originator = packet_get_string(NULL);
1722         if (datafellows & SSH_BUG_X11FWD) {
1723                 debug2("buggy server: x11 request w/o originator_port");
1724                 originator_port = 0;
1725         } else {
1726                 originator_port = packet_get_int();
1727         }
1728         packet_check_eom();
1729         /* XXX check permission */
1730         debug("client_request_x11: request from %s %d", originator,
1731             originator_port);
1732         xfree(originator);
1733         sock = x11_connect_display();
1734         if (sock < 0)
1735                 return NULL;
1736         c = channel_new("x11",
1737             SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1738             CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1739         c->force_drain = 1;
1740         return c;
1741 }
1742
1743 static Channel *
1744 client_request_agent(const char *request_type, int rchan)
1745 {
1746         Channel *c = NULL;
1747         int sock;
1748
1749         if (!options.forward_agent) {
1750                 error("Warning: ssh server tried agent forwarding.");
1751                 error("Warning: this is probably a break-in attempt by a malicious server.");
1752                 return NULL;
1753         }
1754         sock =  ssh_get_authentication_socket();
1755         if (sock < 0)
1756                 return NULL;
1757         c = channel_new("authentication agent connection",
1758             SSH_CHANNEL_OPEN, sock, sock, -1,
1759             CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
1760             "authentication agent connection", 1);
1761         c->force_drain = 1;
1762         return c;
1763 }
1764
1765 /* XXXX move to generic input handler */
1766 static void
1767 client_input_channel_open(int type, u_int32_t seq, void *ctxt)
1768 {
1769         Channel *c = NULL;
1770         char *ctype;
1771         int rchan;
1772         u_int rmaxpack, rwindow, len;
1773
1774         ctype = packet_get_string(&len);
1775         rchan = packet_get_int();
1776         rwindow = packet_get_int();
1777         rmaxpack = packet_get_int();
1778
1779         debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1780             ctype, rchan, rwindow, rmaxpack);
1781
1782         if (strcmp(ctype, "forwarded-tcpip") == 0) {
1783                 c = client_request_forwarded_tcpip(ctype, rchan);
1784         } else if (strcmp(ctype, "x11") == 0) {
1785                 c = client_request_x11(ctype, rchan);
1786         } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1787                 c = client_request_agent(ctype, rchan);
1788         }
1789 /* XXX duplicate : */
1790         if (c != NULL) {
1791                 debug("confirm %s", ctype);
1792                 c->remote_id = rchan;
1793                 c->remote_window = rwindow;
1794                 c->remote_maxpacket = rmaxpack;
1795                 if (c->type != SSH_CHANNEL_CONNECTING) {
1796                         packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1797                         packet_put_int(c->remote_id);
1798                         packet_put_int(c->self);
1799                         packet_put_int(c->local_window);
1800                         packet_put_int(c->local_maxpacket);
1801                         packet_send();
1802                 }
1803         } else {
1804                 debug("failure %s", ctype);
1805                 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1806                 packet_put_int(rchan);
1807                 packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1808                 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1809                         packet_put_cstring("open failed");
1810                         packet_put_cstring("");
1811                 }
1812                 packet_send();
1813         }
1814         xfree(ctype);
1815 }
1816 static void
1817 client_input_channel_req(int type, u_int32_t seq, void *ctxt)
1818 {
1819         Channel *c = NULL;
1820         int exitval, id, reply, success = 0;
1821         char *rtype;
1822
1823         id = packet_get_int();
1824         rtype = packet_get_string(NULL);
1825         reply = packet_get_char();
1826
1827         debug("client_input_channel_req: channel %d rtype %s reply %d",
1828             id, rtype, reply);
1829
1830         if (id == -1) {
1831                 error("client_input_channel_req: request for channel -1");
1832         } else if ((c = channel_lookup(id)) == NULL) {
1833                 error("client_input_channel_req: channel %d: unknown channel", id);
1834         } else if (strcmp(rtype, "exit-status") == 0) {
1835                 exitval = packet_get_int();
1836                 if (id == session_ident) {
1837                         success = 1;
1838                         exit_status = exitval;
1839                 } else if (c->ctl_fd == -1) {
1840                         error("client_input_channel_req: unexpected channel %d",
1841                             session_ident);
1842                 } else {
1843                         atomicio(vwrite, c->ctl_fd, &exitval, sizeof(exitval));
1844                         success = 1;
1845                 }
1846                 packet_check_eom();
1847         }
1848         if (reply) {
1849                 packet_start(success ?
1850                     SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1851                 packet_put_int(id);
1852                 packet_send();
1853         }
1854         xfree(rtype);
1855 }
1856 static void
1857 client_input_global_request(int type, u_int32_t seq, void *ctxt)
1858 {
1859         char *rtype;
1860         int want_reply;
1861         int success = 0;
1862
1863         rtype = packet_get_string(NULL);
1864         want_reply = packet_get_char();
1865         debug("client_input_global_request: rtype %s want_reply %d",
1866             rtype, want_reply);
1867         if (want_reply) {
1868                 packet_start(success ?
1869                     SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
1870                 packet_send();
1871                 packet_write_wait();
1872         }
1873         xfree(rtype);
1874 }
1875
1876 void
1877 client_session2_setup(int id, int want_tty, int want_subsystem,
1878     const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env,
1879     dispatch_fn *subsys_repl)
1880 {
1881         int len;
1882         Channel *c = NULL;
1883
1884         debug2("%s: id %d", __func__, id);
1885
1886         if ((c = channel_lookup(id)) == NULL)
1887                 fatal("client_session2_setup: channel %d: unknown channel", id);
1888
1889         if (want_tty) {
1890                 struct winsize ws;
1891                 struct termios tio;
1892
1893                 /* Store window size in the packet. */
1894                 if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
1895                         memset(&ws, 0, sizeof(ws));
1896
1897                 channel_request_start(id, "pty-req", 0);
1898                 packet_put_cstring(term != NULL ? term : "");
1899                 packet_put_int((u_int)ws.ws_col);
1900                 packet_put_int((u_int)ws.ws_row);
1901                 packet_put_int((u_int)ws.ws_xpixel);
1902                 packet_put_int((u_int)ws.ws_ypixel);
1903                 tio = get_saved_tio();
1904                 tty_make_modes(-1, tiop != NULL ? tiop : &tio);
1905                 packet_send();
1906                 /* XXX wait for reply */
1907                 c->client_tty = 1;
1908         }
1909
1910         /* Transfer any environment variables from client to server */
1911         if (options.num_send_env != 0 && env != NULL) {
1912                 int i, j, matched;
1913                 char *name, *val;
1914
1915                 debug("Sending environment.");
1916                 for (i = 0; env[i] != NULL; i++) {
1917                         /* Split */
1918                         name = xstrdup(env[i]);
1919                         if ((val = strchr(name, '=')) == NULL) {
1920                                 xfree(name);
1921                                 continue;
1922                         }
1923                         *val++ = '\0';
1924
1925                         matched = 0;
1926                         for (j = 0; j < options.num_send_env; j++) {
1927                                 if (match_pattern(name, options.send_env[j])) {
1928                                         matched = 1;
1929                                         break;
1930                                 }
1931                         }
1932                         if (!matched) {
1933                                 debug3("Ignored env %s", name);
1934                                 xfree(name);
1935                                 continue;
1936                         }
1937
1938                         debug("Sending env %s = %s", name, val);
1939                         channel_request_start(id, "env", 0);
1940                         packet_put_cstring(name);
1941                         packet_put_cstring(val);
1942                         packet_send();
1943                         xfree(name);
1944                 }
1945         }
1946
1947         len = buffer_len(cmd);
1948         if (len > 0) {
1949                 if (len > 900)
1950                         len = 900;
1951                 if (want_subsystem) {
1952                         debug("Sending subsystem: %.*s", len, (u_char*)buffer_ptr(cmd));
1953                         channel_request_start(id, "subsystem", subsys_repl != NULL);
1954                         if (subsys_repl != NULL) {
1955                                 /* register callback for reply */
1956                                 /* XXX we assume that client_loop has already been called */
1957                                 dispatch_set(SSH2_MSG_CHANNEL_FAILURE, subsys_repl);
1958                                 dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, subsys_repl);
1959                         }
1960                 } else {
1961                         debug("Sending command: %.*s", len, (u_char*)buffer_ptr(cmd));
1962                         channel_request_start(id, "exec", 0);
1963                 }
1964                 packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
1965                 packet_send();
1966         } else {
1967                 channel_request_start(id, "shell", 0);
1968                 packet_send();
1969         }
1970 }
1971
1972 static void
1973 client_init_dispatch_20(void)
1974 {
1975         dispatch_init(&dispatch_protocol_error);
1976
1977         dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
1978         dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
1979         dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
1980         dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1981         dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1982         dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
1983         dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1984         dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
1985         dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1986         dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
1987
1988         /* rekeying */
1989         dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1990
1991         /* global request reply messages */
1992         dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
1993         dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
1994 }
1995 static void
1996 client_init_dispatch_13(void)
1997 {
1998         dispatch_init(NULL);
1999         dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close);
2000         dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation);
2001         dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data);
2002         dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2003         dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2004         dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open);
2005         dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status);
2006         dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data);
2007         dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data);
2008
2009         dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ?
2010             &client_input_agent_open : &deny_input_open);
2011         dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ?
2012             &x11_input_open : &deny_input_open);
2013 }
2014 static void
2015 client_init_dispatch_15(void)
2016 {
2017         client_init_dispatch_13();
2018         dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof);
2019         dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose);
2020 }
2021 static void
2022 client_init_dispatch(void)
2023 {
2024         if (compat20)
2025                 client_init_dispatch_20();
2026         else if (compat13)
2027                 client_init_dispatch_13();
2028         else
2029                 client_init_dispatch_15();
2030 }
2031
2032 /* client specific fatal cleanup */
2033 void
2034 cleanup_exit(int i)
2035 {
2036         leave_raw_mode();
2037         leave_non_blocking();
2038         if (options.control_path != NULL && control_fd != -1)
2039                 unlink(options.control_path);
2040         _exit(i);
2041 }
This page took 0.222129 seconds and 5 git commands to generate.