blob: 2934c3a4a62e802d28a4af6c66a69afb990f41b8 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Created: Sat Mar 18 16:36:11 1995 ylo
6 * Ssh client program. This program can be used to log into a remote machine.
7 * The software supports strong authentication, encryption, and forwarding
8 * of X11, TCP/IP, and authentication connections.
9 *
10 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu> in Canada.
11 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
13#include "includes.h"
Damien Millerb1715dc2000-05-30 13:44:51 +100014RCSID("$Id: ssh.c,v 1.33 2000/05/30 03:44:54 damien Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100015
16#include <openssl/evp.h>
17#include <openssl/dsa.h>
18#include <openssl/rsa.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "xmalloc.h"
21#include "ssh.h"
22#include "packet.h"
23#include "buffer.h"
24#include "authfd.h"
25#include "readconf.h"
26#include "uidswap.h"
Damien Miller1383bd82000-04-06 12:32:37 +100027
28#include "ssh2.h"
29#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100030#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "key.h"
32#include "authfile.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
Damien Miller3f905871999-11-15 17:10:57 +110034#ifdef HAVE___PROGNAME
35extern char *__progname;
36#else /* HAVE___PROGNAME */
Damien Miller70fb6712000-05-01 20:59:50 +100037static const char *__progname = "ssh";
Damien Miller3f905871999-11-15 17:10:57 +110038#endif /* HAVE___PROGNAME */
39
Damien Miller34132e52000-01-14 15:45:46 +110040/* Flag indicating whether IPv4 or IPv6. This can be set on the command line.
41 Default value is AF_UNSPEC means both IPv4 and IPv6. */
Damien Miller7d80e342000-01-19 14:36:49 +110042#ifdef IPV4_DEFAULT
43int IPv4or6 = AF_INET;
44#else
Damien Miller34132e52000-01-14 15:45:46 +110045int IPv4or6 = AF_UNSPEC;
Damien Miller7d80e342000-01-19 14:36:49 +110046#endif
Damien Miller34132e52000-01-14 15:45:46 +110047
Damien Miller95def091999-11-25 00:26:21 +110048/* Flag indicating whether debug mode is on. This can be set on the command line. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049int debug_flag = 0;
50
Damien Miller78928792000-04-12 20:17:38 +100051/* Flag indicating whether a tty should be allocated */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052int tty_flag = 0;
53
Damien Miller1383bd82000-04-06 12:32:37 +100054/* don't exec a shell */
55int no_shell_flag = 0;
56int no_tty_flag = 0;
57
Damien Miller5428f641999-11-25 11:54:57 +110058/*
59 * Flag indicating that nothing should be read from stdin. This can be set
60 * on the command line.
61 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062int stdin_null_flag = 0;
63
Damien Miller5428f641999-11-25 11:54:57 +110064/*
65 * Flag indicating that ssh should fork after authentication. This is useful
66 * so that the pasphrase can be entered manually, and then ssh goes to the
67 * background.
68 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069int fork_after_authentication_flag = 0;
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * General data structure for command line options and options configurable
73 * in configuration files. See readconf.h.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075Options options;
76
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Name of the host we are connecting to. This is the name given on the
79 * command line, or the HostName specified for the user-supplied name in a
80 * configuration file.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082char *host;
83
84/* socket address the host resolves to */
Damien Miller34132e52000-01-14 15:45:46 +110085struct sockaddr_storage hostaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller5428f641999-11-25 11:54:57 +110087/*
88 * Flag to indicate that we have received a window change signal which has
89 * not yet been processed. This will cause a message indicating the new
90 * window size to be sent to the server a little later. This is volatile
91 * because this is updated in a signal handler.
92 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093volatile int received_window_change_signal = 0;
94
95/* Value of argv[0] (set in the main program). */
96char *av0;
97
98/* Flag indicating whether we have a valid host private key loaded. */
99int host_private_key_loaded = 0;
100
101/* Host private key. */
102RSA *host_private_key = NULL;
103
104/* Original real UID. */
105uid_t original_real_uid;
106
Damien Miller1383bd82000-04-06 12:32:37 +1000107/* command to be executed */
108Buffer command;
109
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110/* Prints a help message to the user. This function never returns. */
111
112void
113usage()
114{
Damien Miller95def091999-11-25 00:26:21 +1100115 fprintf(stderr, "Usage: %s [options] host [command]\n", av0);
116 fprintf(stderr, "Options:\n");
117 fprintf(stderr, " -l user Log in using this user name.\n");
118 fprintf(stderr, " -n Redirect input from /dev/null.\n");
Damien Millerb1715dc2000-05-30 13:44:51 +1000119 fprintf(stderr, " -A Enable authentication agent forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100120 fprintf(stderr, " -a Disable authentication agent forwarding.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121#ifdef AFS
Damien Miller95def091999-11-25 00:26:21 +1100122 fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n");
123#endif /* AFS */
Damien Millerdcb6ecd2000-05-17 22:34:22 +1000124 fprintf(stderr, " -X Enable X11 connection forwarding.\n");
Damien Miller95def091999-11-25 00:26:21 +1100125 fprintf(stderr, " -x Disable X11 connection forwarding.\n");
126 fprintf(stderr, " -i file Identity for RSA authentication (default: ~/.ssh/identity).\n");
127 fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000128 fprintf(stderr, " -T Do not allocate a tty.\n");
Damien Miller95def091999-11-25 00:26:21 +1100129 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
130 fprintf(stderr, " -V Display version number only.\n");
131 fprintf(stderr, " -P Don't allocate a privileged port.\n");
132 fprintf(stderr, " -q Quiet; don't display any warning messages.\n");
133 fprintf(stderr, " -f Fork into background after authentication.\n");
134 fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller95def091999-11-25 00:26:21 +1100136 fprintf(stderr, " -c cipher Select encryption algorithm: "
137 "``3des'', "
138 "``blowfish''\n");
139 fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n");
140 fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n");
141 fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n");
142 fprintf(stderr, " These cause %s to listen for connections on a port, and\n", av0);
143 fprintf(stderr, " forward them to the other side by connecting to host:port.\n");
144 fprintf(stderr, " -C Enable compression.\n");
Damien Miller1383bd82000-04-06 12:32:37 +1000145 fprintf(stderr, " -N Do not execute a shell or command.\n");
Damien Miller95def091999-11-25 00:26:21 +1100146 fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n");
Damien Miller34132e52000-01-14 15:45:46 +1100147 fprintf(stderr, " -4 Use IPv4 only.\n");
148 fprintf(stderr, " -6 Use IPv6 only.\n");
Damien Miller4af51302000-04-16 11:18:38 +1000149 fprintf(stderr, " -2 Force protocol version 2.\n");
Damien Miller95def091999-11-25 00:26:21 +1100150 fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n");
151 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152}
153
Damien Miller95def091999-11-25 00:26:21 +1100154/*
155 * Connects to the given host using rsh (or prints an error message and exits
156 * if rsh is not available). This function never returns.
157 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158void
Damien Miller95def091999-11-25 00:26:21 +1100159rsh_connect(char *host, char *user, Buffer * command)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160{
Damien Miller95def091999-11-25 00:26:21 +1100161 char *args[10];
162 int i;
163
164 log("Using rsh. WARNING: Connection will not be encrypted.");
165 /* Build argument list for rsh. */
166 i = 0;
167 args[i++] = _PATH_RSH;
168 /* host may have to come after user on some systems */
169 args[i++] = host;
170 if (user) {
171 args[i++] = "-l";
172 args[i++] = user;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173 }
Damien Miller95def091999-11-25 00:26:21 +1100174 if (buffer_len(command) > 0) {
175 buffer_append(command, "\0", 1);
176 args[i++] = buffer_ptr(command);
177 }
178 args[i++] = NULL;
179 if (debug_flag) {
180 for (i = 0; args[i]; i++) {
181 if (i != 0)
182 fprintf(stderr, " ");
183 fprintf(stderr, "%s", args[i]);
184 }
185 fprintf(stderr, "\n");
186 }
187 execv(_PATH_RSH, args);
188 perror(_PATH_RSH);
189 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Damien Miller1383bd82000-04-06 12:32:37 +1000192int ssh_session(void);
193int ssh_session2(void);
194
Damien Miller95def091999-11-25 00:26:21 +1100195/*
196 * Main program for the ssh client.
197 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198int
199main(int ac, char **av)
200{
Damien Miller1383bd82000-04-06 12:32:37 +1000201 int i, opt, optind, exit_status, ok;
Damien Milleraae6c611999-12-06 11:47:28 +1100202 u_short fwd_port, fwd_host_port;
Damien Miller95def091999-11-25 00:26:21 +1100203 char *optarg, *cp, buf[256];
Damien Miller95def091999-11-25 00:26:21 +1100204 struct stat st;
205 struct passwd *pw, pwcopy;
Damien Miller1383bd82000-04-06 12:32:37 +1000206 int dummy;
Damien Miller95def091999-11-25 00:26:21 +1100207 uid_t original_effective_uid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208
Damien Miller5428f641999-11-25 11:54:57 +1100209 /*
210 * Save the original real uid. It will be needed later (uid-swapping
211 * may clobber the real uid).
212 */
Damien Miller95def091999-11-25 00:26:21 +1100213 original_real_uid = getuid();
214 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller95def091999-11-25 00:26:21 +1100216 /* If we are installed setuid root be careful to not drop core. */
217 if (original_real_uid != original_effective_uid) {
218 struct rlimit rlim;
219 rlim.rlim_cur = rlim.rlim_max = 0;
220 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
221 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222 }
Damien Miller5428f641999-11-25 11:54:57 +1100223 /*
224 * Use uid-swapping to give up root privileges for the duration of
225 * option processing. We will re-instantiate the rights when we are
226 * ready to create the privileged port, and will permanently drop
227 * them when the port has been created (actually, when the connection
228 * has been made, as we may need to create the port several times).
229 */
Damien Miller95def091999-11-25 00:26:21 +1100230 temporarily_use_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Miller5428f641999-11-25 11:54:57 +1100232 /*
233 * Set our umask to something reasonable, as some files are created
234 * with the default umask. This will make them world-readable but
235 * writable only by the owner, which is ok for all files for which we
236 * don't set the modes explicitly.
237 */
Damien Miller95def091999-11-25 00:26:21 +1100238 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller95def091999-11-25 00:26:21 +1100240 /* Save our own name. */
241 av0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller95def091999-11-25 00:26:21 +1100243 /* Initialize option structure to indicate that no values have been set. */
244 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller95def091999-11-25 00:26:21 +1100246 /* Parse command-line arguments. */
247 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller95def091999-11-25 00:26:21 +1100249 /* If program name is not one of the standard names, use it as host name. */
250 if (strchr(av0, '/'))
251 cp = strrchr(av0, '/') + 1;
252 else
253 cp = av0;
254 if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 &&
255 strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0)
256 host = cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller95def091999-11-25 00:26:21 +1100258 for (optind = 1; optind < ac; optind++) {
259 if (av[optind][0] != '-') {
260 if (host)
261 break;
262 if ((cp = strchr(av[optind], '@'))) {
Damien Miller4af51302000-04-16 11:18:38 +1000263 if(cp == av[optind])
264 usage();
Damien Miller95def091999-11-25 00:26:21 +1100265 options.user = av[optind];
266 *cp = '\0';
267 host = ++cp;
268 } else
269 host = av[optind];
270 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271 }
Damien Miller95def091999-11-25 00:26:21 +1100272 opt = av[optind][1];
273 if (!opt)
274 usage();
275 if (strchr("eilcpLRo", opt)) { /* options with arguments */
276 optarg = av[optind] + 2;
277 if (strcmp(optarg, "") == 0) {
278 if (optind >= ac - 1)
279 usage();
280 optarg = av[++optind];
281 }
282 } else {
283 if (av[optind][2])
284 usage();
285 optarg = NULL;
286 }
287 switch (opt) {
Damien Miller4af51302000-04-16 11:18:38 +1000288 case '2':
289 options.protocol = SSH_PROTO_2;
290 break;
Damien Miller34132e52000-01-14 15:45:46 +1100291 case '4':
292 IPv4or6 = AF_INET;
293 break;
Damien Miller34132e52000-01-14 15:45:46 +1100294 case '6':
295 IPv4or6 = AF_INET6;
296 break;
Damien Miller95def091999-11-25 00:26:21 +1100297 case 'n':
298 stdin_null_flag = 1;
299 break;
Damien Miller95def091999-11-25 00:26:21 +1100300 case 'f':
301 fork_after_authentication_flag = 1;
302 stdin_null_flag = 1;
303 break;
Damien Miller95def091999-11-25 00:26:21 +1100304 case 'x':
305 options.forward_x11 = 0;
306 break;
Damien Miller95def091999-11-25 00:26:21 +1100307 case 'X':
308 options.forward_x11 = 1;
309 break;
Damien Miller95def091999-11-25 00:26:21 +1100310 case 'g':
311 options.gateway_ports = 1;
312 break;
Damien Miller95def091999-11-25 00:26:21 +1100313 case 'P':
314 options.use_privileged_port = 0;
315 break;
Damien Miller95def091999-11-25 00:26:21 +1100316 case 'a':
317 options.forward_agent = 0;
318 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000319 case 'A':
320 options.forward_agent = 1;
321 break;
Damien Miller95def091999-11-25 00:26:21 +1100322#ifdef AFS
323 case 'k':
324 options.kerberos_tgt_passing = 0;
325 options.afs_token_passing = 0;
326 break;
327#endif
328 case 'i':
329 if (stat(optarg, &st) < 0) {
330 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
331 optarg);
332 break;
333 }
334 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
335 fatal("Too many identity files specified (max %d)",
336 SSH_MAX_IDENTITY_FILES);
337 options.identity_files[options.num_identity_files++] =
338 xstrdup(optarg);
339 break;
Damien Miller95def091999-11-25 00:26:21 +1100340 case 't':
341 tty_flag = 1;
342 break;
Damien Miller95def091999-11-25 00:26:21 +1100343 case 'v':
344 case 'V':
Damien Miller78928792000-04-12 20:17:38 +1000345 fprintf(stderr, "SSH Version %s, protocol versions %d.%d/%d.%d.\n",
346 SSH_VERSION,
347 PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
348 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
Damien Miller1383bd82000-04-06 12:32:37 +1000349 fprintf(stderr, "Compiled with SSL (0x%8.8lx).\n", SSLeay());
Damien Miller95def091999-11-25 00:26:21 +1100350 if (opt == 'V')
351 exit(0);
352 debug_flag = 1;
353 options.log_level = SYSLOG_LEVEL_DEBUG;
354 break;
Damien Miller95def091999-11-25 00:26:21 +1100355 case 'q':
356 options.log_level = SYSLOG_LEVEL_QUIET;
357 break;
Damien Miller95def091999-11-25 00:26:21 +1100358 case 'e':
359 if (optarg[0] == '^' && optarg[2] == 0 &&
360 (unsigned char) optarg[1] >= 64 && (unsigned char) optarg[1] < 128)
361 options.escape_char = (unsigned char) optarg[1] & 31;
362 else if (strlen(optarg) == 1)
363 options.escape_char = (unsigned char) optarg[0];
364 else if (strcmp(optarg, "none") == 0)
365 options.escape_char = -2;
366 else {
367 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
368 exit(1);
369 }
370 break;
Damien Miller95def091999-11-25 00:26:21 +1100371 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000372 if (ciphers_valid(optarg)) {
373 /* SSH2 only */
374 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000375 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000376 } else {
377 /* SSH1 only */
378 options.cipher = cipher_number(optarg);
379 if (options.cipher == -1) {
380 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
381 exit(1);
382 }
Damien Miller95def091999-11-25 00:26:21 +1100383 }
384 break;
Damien Miller95def091999-11-25 00:26:21 +1100385 case 'p':
386 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100387 break;
Damien Miller95def091999-11-25 00:26:21 +1100388 case 'l':
389 options.user = optarg;
390 break;
Damien Miller95def091999-11-25 00:26:21 +1100391 case 'R':
Damien Miller34132e52000-01-14 15:45:46 +1100392 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
393 &fwd_host_port) != 3 &&
394 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
395 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100396 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
397 usage();
398 /* NOTREACHED */
399 }
400 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
401 break;
Damien Miller95def091999-11-25 00:26:21 +1100402 case 'L':
Damien Miller34132e52000-01-14 15:45:46 +1100403 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
404 &fwd_host_port) != 3 &&
405 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
406 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100407 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
408 usage();
409 /* NOTREACHED */
410 }
411 add_local_forward(&options, fwd_port, buf, fwd_host_port);
412 break;
Damien Miller95def091999-11-25 00:26:21 +1100413 case 'C':
414 options.compression = 1;
415 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000416 case 'N':
417 no_shell_flag = 1;
418 no_tty_flag = 1;
419 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000420 case 'T':
421 no_tty_flag = 1;
422 break;
Damien Miller95def091999-11-25 00:26:21 +1100423 case 'o':
424 dummy = 1;
425 if (process_config_line(&options, host ? host : "", optarg,
426 "command-line", 0, &dummy) != 0)
427 exit(1);
428 break;
Damien Miller95def091999-11-25 00:26:21 +1100429 default:
430 usage();
431 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434 /* Check that we got a host name. */
435 if (!host)
436 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Damien Miller95def091999-11-25 00:26:21 +1100438 /* Initialize the command to execute on remote host. */
439 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Damien Millereba71ba2000-04-29 23:57:08 +1000441 OpenSSL_add_all_algorithms();
Damien Miller7d6656c2000-05-20 15:22:36 +1000442
Damien Miller5428f641999-11-25 11:54:57 +1100443 /*
444 * Save the command to execute on the remote host in a buffer. There
445 * is no limit on the length of the command, except by the maximum
446 * packet size. Also sets the tty flag if there is no command.
447 */
Damien Miller95def091999-11-25 00:26:21 +1100448 if (optind == ac) {
449 /* No command specified - execute shell on a tty. */
450 tty_flag = 1;
451 } else {
452 /* A command has been specified. Store it into the
453 buffer. */
454 for (i = optind; i < ac; i++) {
455 if (i > optind)
456 buffer_append(&command, " ", 1);
457 buffer_append(&command, av[i], strlen(av[i]));
458 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460
Damien Miller95def091999-11-25 00:26:21 +1100461 /* Cannot fork to background if no command. */
462 if (fork_after_authentication_flag && buffer_len(&command) == 0)
463 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller95def091999-11-25 00:26:21 +1100465 /* Allocate a tty by default if no command specified. */
466 if (buffer_len(&command) == 0)
467 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller95def091999-11-25 00:26:21 +1100469 /* Do not allocate a tty if stdin is not a tty. */
470 if (!isatty(fileno(stdin))) {
471 if (tty_flag)
472 fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
473 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474 }
Damien Miller1383bd82000-04-06 12:32:37 +1000475 /* force */
476 if (no_tty_flag)
477 tty_flag = 0;
478
Damien Miller95def091999-11-25 00:26:21 +1100479 /* Get user data. */
480 pw = getpwuid(original_real_uid);
481 if (!pw) {
482 fprintf(stderr, "You don't exist, go away!\n");
483 exit(1);
484 }
485 /* Take a copy of the returned structure. */
486 memset(&pwcopy, 0, sizeof(pwcopy));
487 pwcopy.pw_name = xstrdup(pw->pw_name);
488 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
489 pwcopy.pw_uid = pw->pw_uid;
490 pwcopy.pw_gid = pw->pw_gid;
491 pwcopy.pw_dir = xstrdup(pw->pw_dir);
492 pwcopy.pw_shell = xstrdup(pw->pw_shell);
493 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494
Damien Miller95def091999-11-25 00:26:21 +1100495 /* Initialize "log" output. Since we are the client all output
496 actually goes to the terminal. */
497 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Damien Miller95def091999-11-25 00:26:21 +1100499 /* Read per-user configuration file. */
500 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
501 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502
Damien Miller95def091999-11-25 00:26:21 +1100503 /* Read systemwide configuration file. */
504 read_config_file(HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Miller95def091999-11-25 00:26:21 +1100506 /* Fill configuration defaults. */
507 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508
Damien Miller95def091999-11-25 00:26:21 +1100509 /* reinit */
510 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511
Damien Millereba71ba2000-04-29 23:57:08 +1000512 /* check if RSA support exists */
513 if ((options.protocol & SSH_PROTO_1) &&
514 rsa_alive() == 0) {
515 log("%s: no RSA support in libssl and libcrypto. See ssl(8).",
516 __progname);
517 log("Disabling protocol version 1");
518 options.protocol &= ~ (SSH_PROTO_1|SSH_PROTO_1_PREFERRED);
519 }
520 if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) {
521 fprintf(stderr, "%s: No protocol version available.\n",
522 __progname);
523 exit(1);
524 }
525
Damien Miller95def091999-11-25 00:26:21 +1100526 if (options.user == NULL)
527 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
Damien Miller95def091999-11-25 00:26:21 +1100529 if (options.hostname != NULL)
530 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531
Damien Miller95def091999-11-25 00:26:21 +1100532 /* Find canonic host name. */
533 if (strchr(host, '.') == 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100534 struct addrinfo hints;
535 struct addrinfo *ai = NULL;
536 int errgai;
537 memset(&hints, 0, sizeof(hints));
Damien Miller98c7ad62000-03-09 21:27:49 +1100538 hints.ai_family = IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +1100539 hints.ai_flags = AI_CANONNAME;
540 hints.ai_socktype = SOCK_STREAM;
541 errgai = getaddrinfo(host, NULL, &hints, &ai);
542 if (errgai == 0) {
543 if (ai->ai_canonname != NULL)
544 host = xstrdup(ai->ai_canonname);
545 freeaddrinfo(ai);
Damien Miller95def091999-11-25 00:26:21 +1100546 }
547 }
548 /* Disable rhosts authentication if not running as root. */
549 if (original_effective_uid != 0 || !options.use_privileged_port) {
550 options.rhosts_authentication = 0;
551 options.rhosts_rsa_authentication = 0;
552 }
Damien Miller5428f641999-11-25 11:54:57 +1100553 /*
554 * If using rsh has been selected, exec it now (without trying
555 * anything else). Note that we must release privileges first.
556 */
Damien Miller95def091999-11-25 00:26:21 +1100557 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100558 /*
559 * Restore our superuser privileges. This must be done
560 * before permanently setting the uid.
561 */
Damien Miller95def091999-11-25 00:26:21 +1100562 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
Damien Miller95def091999-11-25 00:26:21 +1100564 /* Switch to the original uid permanently. */
565 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566
Damien Miller95def091999-11-25 00:26:21 +1100567 /* Execute rsh. */
568 rsh_connect(host, options.user, &command);
569 fatal("rsh_connect returned");
570 }
571 /* Restore our superuser privileges. */
572 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573
Damien Miller5428f641999-11-25 11:54:57 +1100574 /*
575 * Open a connection to the remote host. This needs root privileges
576 * if rhosts_{rsa_}authentication is enabled.
577 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578
Damien Miller95def091999-11-25 00:26:21 +1100579 ok = ssh_connect(host, &hostaddr, options.port,
580 options.connection_attempts,
581 !options.rhosts_authentication &&
582 !options.rhosts_rsa_authentication,
583 original_real_uid,
584 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585
Damien Miller5428f641999-11-25 11:54:57 +1100586 /*
587 * If we successfully made the connection, load the host private key
588 * in case we will need it later for combined rsa-rhosts
589 * authentication. This must be done before releasing extra
590 * privileges, because the file is only readable by root.
591 */
Damien Millereba71ba2000-04-29 23:57:08 +1000592 if (ok && (options.protocol & SSH_PROTO_1)) {
593 Key k;
Damien Miller95def091999-11-25 00:26:21 +1100594 host_private_key = RSA_new();
Damien Millereba71ba2000-04-29 23:57:08 +1000595 k.type = KEY_RSA;
596 k.rsa = host_private_key;
597 if (load_private_key(HOST_KEY_FILE, "", &k, NULL))
Damien Miller95def091999-11-25 00:26:21 +1100598 host_private_key_loaded = 1;
599 }
Damien Miller5428f641999-11-25 11:54:57 +1100600 /*
601 * Get rid of any extra privileges that we may have. We will no
602 * longer need them. Also, extra privileges could make it very hard
603 * to read identity files and other non-world-readable files from the
604 * user's home directory if it happens to be on a NFS volume where
605 * root is mapped to nobody.
606 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607
Damien Miller5428f641999-11-25 11:54:57 +1100608 /*
609 * Note that some legacy systems need to postpone the following call
610 * to permanently_set_uid() until the private hostkey is destroyed
611 * with RSA_free(). Otherwise the calling user could ptrace() the
612 * process, read the private hostkey and impersonate the host.
613 * OpenBSD does not allow ptracing of setuid processes.
614 */
Damien Miller95def091999-11-25 00:26:21 +1100615 permanently_set_uid(original_real_uid);
616
Damien Miller5428f641999-11-25 11:54:57 +1100617 /*
618 * Now that we are back to our own permissions, create ~/.ssh
619 * directory if it doesn\'t already exist.
620 */
Damien Miller95def091999-11-25 00:26:21 +1100621 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
622 if (stat(buf, &st) < 0)
623 if (mkdir(buf, 0755) < 0)
624 error("Could not create directory '%.200s'.", buf);
625
626 /* Check if the connection failed, and try "rsh" if appropriate. */
627 if (!ok) {
628 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100629 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100630 host, options.port,
631 options.fallback_to_rsh ? "; reverting to insecure method" : "");
632 else
633 log("Secure connection to %.100s refused%.100s.", host,
634 options.fallback_to_rsh ? "; reverting to insecure method" : "");
635
636 if (options.fallback_to_rsh) {
637 rsh_connect(host, options.user, &command);
638 fatal("rsh_connect returned");
639 }
640 exit(1);
641 }
642 /* Expand ~ in options.identity_files. */
Damien Millereba71ba2000-04-29 23:57:08 +1000643 /* XXX mem-leaks */
Damien Miller95def091999-11-25 00:26:21 +1100644 for (i = 0; i < options.num_identity_files; i++)
645 options.identity_files[i] =
646 tilde_expand_filename(options.identity_files[i], original_real_uid);
Damien Millereba71ba2000-04-29 23:57:08 +1000647 for (i = 0; i < options.num_identity_files2; i++)
648 options.identity_files2[i] =
649 tilde_expand_filename(options.identity_files2[i], original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100650 /* Expand ~ in known host file names. */
651 options.system_hostfile = tilde_expand_filename(options.system_hostfile,
Damien Millereba71ba2000-04-29 23:57:08 +1000652 original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100653 options.user_hostfile = tilde_expand_filename(options.user_hostfile,
Damien Millereba71ba2000-04-29 23:57:08 +1000654 original_real_uid);
655 options.system_hostfile2 = tilde_expand_filename(options.system_hostfile2,
656 original_real_uid);
657 options.user_hostfile2 = tilde_expand_filename(options.user_hostfile2,
658 original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100659
660 /* Log into the remote system. This never returns if the login fails. */
661 ssh_login(host_private_key_loaded, host_private_key,
Damien Miller34132e52000-01-14 15:45:46 +1100662 host, (struct sockaddr *)&hostaddr, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100663
664 /* We no longer need the host private key. Clear it now. */
665 if (host_private_key_loaded)
666 RSA_free(host_private_key); /* Destroys contents safely */
667
Damien Miller1383bd82000-04-06 12:32:37 +1000668 exit_status = compat20 ? ssh_session2() : ssh_session();
669 packet_close();
670 return exit_status;
671}
672
Damien Millerbd483e72000-04-30 10:00:53 +1000673void
674x11_get_proto(char *proto, int proto_len, char *data, int data_len)
675{
676 char line[512];
677 FILE *f;
678 int got_data = 0, i;
679
680#ifdef XAUTH_PATH
681 /* Try to get Xauthority information for the display. */
682 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
683 XAUTH_PATH, getenv("DISPLAY"));
684 f = popen(line, "r");
685 if (f && fgets(line, sizeof(line), f) &&
686 sscanf(line, "%*s %s %s", proto, data) == 2)
687 got_data = 1;
688 if (f)
689 pclose(f);
690#endif /* XAUTH_PATH */
691 /*
692 * If we didn't get authentication data, just make up some
693 * data. The forwarding code will check the validity of the
694 * response anyway, and substitute this data. The X11
695 * server, however, will ignore this fake data and use
696 * whatever authentication mechanisms it was using otherwise
697 * for the local connection.
698 */
699 if (!got_data) {
700 u_int32_t rand = 0;
701
702 strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
703 for (i = 0; i < 16; i++) {
704 if (i % 4 == 0)
705 rand = arc4random();
706 snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
707 rand >>= 8;
708 }
709 }
710}
711
Damien Miller1383bd82000-04-06 12:32:37 +1000712int
713ssh_session(void)
714{
715 int type;
716 int i;
717 int plen;
718 int interactive = 0;
719 int have_tty = 0;
720 struct winsize ws;
721 int authfd;
722 char *cp;
723
Damien Miller95def091999-11-25 00:26:21 +1100724 /* Enable compression if requested. */
725 if (options.compression) {
726 debug("Requesting compression at level %d.", options.compression_level);
727
728 if (options.compression_level < 1 || options.compression_level > 9)
729 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
730
731 /* Send the request. */
732 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
733 packet_put_int(options.compression_level);
734 packet_send();
735 packet_write_wait();
736 type = packet_read(&plen);
737 if (type == SSH_SMSG_SUCCESS)
738 packet_start_compression(options.compression_level);
739 else if (type == SSH_SMSG_FAILURE)
740 log("Warning: Remote host refused compression.");
741 else
742 packet_disconnect("Protocol error waiting for compression response.");
743 }
744 /* Allocate a pseudo tty if appropriate. */
745 if (tty_flag) {
746 debug("Requesting pty.");
747
748 /* Start the packet. */
749 packet_start(SSH_CMSG_REQUEST_PTY);
750
751 /* Store TERM in the packet. There is no limit on the
752 length of the string. */
753 cp = getenv("TERM");
754 if (!cp)
755 cp = "";
756 packet_put_string(cp, strlen(cp));
757
758 /* Store window size in the packet. */
759 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
760 memset(&ws, 0, sizeof(ws));
761 packet_put_int(ws.ws_row);
762 packet_put_int(ws.ws_col);
763 packet_put_int(ws.ws_xpixel);
764 packet_put_int(ws.ws_ypixel);
765
766 /* Store tty modes in the packet. */
767 tty_make_modes(fileno(stdin));
768
769 /* Send the packet, and wait for it to leave. */
770 packet_send();
771 packet_write_wait();
772
773 /* Read response from the server. */
774 type = packet_read(&plen);
Damien Miller450a7a12000-03-26 13:04:51 +1000775 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100776 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000777 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000778 } else if (type == SSH_SMSG_FAILURE)
Damien Miller95def091999-11-25 00:26:21 +1100779 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
780 else
781 packet_disconnect("Protocol error waiting for pty request response.");
782 }
783 /* Request X11 forwarding if enabled and DISPLAY is set. */
784 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +1000785 char proto[512], data[512];
786 /* Get reasonable local authentication information. */
787 x11_get_proto(proto, sizeof proto, data, sizeof data);
788 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100789 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000790 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100791
792 /* Read response from the server. */
793 type = packet_read(&plen);
794 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100795 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000796 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller95def091999-11-25 00:26:21 +1100797 log("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000798 } else {
Damien Miller95def091999-11-25 00:26:21 +1100799 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000800 }
Damien Miller95def091999-11-25 00:26:21 +1100801 }
802 /* Tell the packet module whether this is an interactive session. */
803 packet_set_interactive(interactive, options.keepalives);
804
805 /* Clear agent forwarding if we don\'t have an agent. */
806 authfd = ssh_get_authentication_socket();
807 if (authfd < 0)
808 options.forward_agent = 0;
809 else
810 ssh_close_authentication_socket(authfd);
811
812 /* Request authentication agent forwarding if appropriate. */
813 if (options.forward_agent) {
814 debug("Requesting authentication agent forwarding.");
815 auth_request_forwarding();
816
817 /* Read response from the server. */
818 type = packet_read(&plen);
819 packet_integrity_check(plen, 0, type);
820 if (type != SSH_SMSG_SUCCESS)
821 log("Warning: Remote host denied authentication agent forwarding.");
822 }
823 /* Initiate local TCP/IP port forwardings. */
824 for (i = 0; i < options.num_local_forwards; i++) {
825 debug("Connections to local port %d forwarded to remote address %.200s:%d",
826 options.local_forwards[i].port,
827 options.local_forwards[i].host,
828 options.local_forwards[i].host_port);
829 channel_request_local_forwarding(options.local_forwards[i].port,
Damien Miller4af51302000-04-16 11:18:38 +1000830 options.local_forwards[i].host,
Damien Millera34a28b1999-12-14 10:47:15 +1100831 options.local_forwards[i].host_port,
832 options.gateway_ports);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000833 }
834
Damien Miller95def091999-11-25 00:26:21 +1100835 /* Initiate remote TCP/IP port forwardings. */
836 for (i = 0; i < options.num_remote_forwards; i++) {
837 debug("Connections to remote port %d forwarded to local address %.200s:%d",
838 options.remote_forwards[i].port,
839 options.remote_forwards[i].host,
840 options.remote_forwards[i].host_port);
841 channel_request_remote_forwarding(options.remote_forwards[i].port,
842 options.remote_forwards[i].host,
Damien Miller4af51302000-04-16 11:18:38 +1000843 options.remote_forwards[i].host_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845
Damien Miller5428f641999-11-25 11:54:57 +1100846 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000847 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100848 if (daemon(1, 1) < 0)
849 fatal("daemon() failed: %.200s", strerror(errno));
850
851 /*
852 * If a command was specified on the command line, execute the
853 * command now. Otherwise request the server to start a shell.
854 */
Damien Miller95def091999-11-25 00:26:21 +1100855 if (buffer_len(&command) > 0) {
856 int len = buffer_len(&command);
857 if (len > 900)
858 len = 900;
859 debug("Sending command: %.*s", len, buffer_ptr(&command));
860 packet_start(SSH_CMSG_EXEC_CMD);
861 packet_put_string(buffer_ptr(&command), buffer_len(&command));
862 packet_send();
863 packet_write_wait();
864 } else {
865 debug("Requesting shell.");
866 packet_start(SSH_CMSG_EXEC_SHELL);
867 packet_send();
868 packet_write_wait();
869 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870
Damien Miller95def091999-11-25 00:26:21 +1100871 /* Enter the interactive session. */
Damien Miller1383bd82000-04-06 12:32:37 +1000872 return client_loop(have_tty, tty_flag ? options.escape_char : -1);
873}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874
Damien Miller1383bd82000-04-06 12:32:37 +1000875void
876init_local_fwd(void)
877{
878 int i;
879 /* Initiate local TCP/IP port forwardings. */
880 for (i = 0; i < options.num_local_forwards; i++) {
881 debug("Connections to local port %d forwarded to remote address %.200s:%d",
882 options.local_forwards[i].port,
883 options.local_forwards[i].host,
884 options.local_forwards[i].host_port);
885 channel_request_local_forwarding(options.local_forwards[i].port,
Damien Miller4af51302000-04-16 11:18:38 +1000886 options.local_forwards[i].host,
Damien Miller1383bd82000-04-06 12:32:37 +1000887 options.local_forwards[i].host_port,
888 options.gateway_ports);
889 }
890}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
Damien Miller1383bd82000-04-06 12:32:37 +1000892extern void client_set_session_ident(int id);
893
894void
895client_init(int id, void *arg)
896{
897 int len;
898 debug("client_init id %d arg %d", id, (int)arg);
899
900 if (no_shell_flag)
901 goto done;
902
903 if (tty_flag) {
904 struct winsize ws;
905 char *cp;
906 cp = getenv("TERM");
907 if (!cp)
908 cp = "";
909 /* Store window size in the packet. */
910 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
911 memset(&ws, 0, sizeof(ws));
912
913 channel_request_start(id, "pty-req", 0);
914 packet_put_cstring(cp);
915 packet_put_int(ws.ws_col);
916 packet_put_int(ws.ws_row);
917 packet_put_int(ws.ws_xpixel);
918 packet_put_int(ws.ws_ypixel);
919 packet_put_cstring(""); /* XXX: encode terminal modes */
920 packet_send();
921 /* XXX wait for reply */
922 }
Damien Millerbd483e72000-04-30 10:00:53 +1000923 if (options.forward_x11 &&
924 getenv("DISPLAY") != NULL) {
925 char proto[512], data[512];
926 /* Get reasonable local authentication information. */
927 x11_get_proto(proto, sizeof proto, data, sizeof data);
928 /* Request forwarding with authentication spoofing. */
929 debug("Requesting X11 forwarding with authentication spoofing.");
930 x11_request_forwarding_with_spoofing(id, proto, data);
931 /* XXX wait for reply */
932 }
933
Damien Miller1383bd82000-04-06 12:32:37 +1000934 len = buffer_len(&command);
935 if (len > 0) {
936 if (len > 900)
937 len = 900;
938 debug("Sending command: %.*s", len, buffer_ptr(&command));
939 channel_request_start(id, "exec", 0);
940 packet_put_string(buffer_ptr(&command), len);
941 packet_send();
942 } else {
943 channel_request(id, "shell", 0);
944 }
945 /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
946done:
947 /* register different callback, etc. XXX */
948 client_set_session_ident(id);
949}
950
951int
952ssh_session2(void)
953{
954 int window, packetmax, id;
955 int in = dup(STDIN_FILENO);
956 int out = dup(STDOUT_FILENO);
957 int err = dup(STDERR_FILENO);
958
959 if (in < 0 || out < 0 || err < 0)
960 fatal("dump in/out/err failed");
961
962 /* should be pre-session */
963 init_local_fwd();
964
965 window = 32*1024;
966 if (tty_flag) {
967 packetmax = window/8;
968 } else {
969 window *= 2;
970 packetmax = window/2;
971 }
972
973 id = channel_new(
974 "session", SSH_CHANNEL_OPENING, in, out, err,
975 window, packetmax, CHAN_EXTENDED_WRITE, xstrdup("client-session"));
976
977
978 channel_open(id);
979 channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, client_init, (void *)0);
980
981 return client_loop(tty_flag, tty_flag ? options.escape_char : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982}