blob: 58e4d7bd9b066dcabdde930e61b2ed2058533d9b [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 Miller994cf142000-07-21 10:19:44 +100014RCSID("$OpenBSD: ssh.c,v 1.58 2000/07/16 08:27:22 markus 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"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "readconf.h"
25#include "uidswap.h"
Damien Miller1383bd82000-04-06 12:32:37 +100026
27#include "ssh2.h"
28#include "compat.h"
Damien Millerb38eff82000-04-01 11:09:21 +100029#include "channels.h"
Damien Millereba71ba2000-04-29 23:57:08 +100030#include "key.h"
Damien Miller994cf142000-07-21 10:19:44 +100031#include "authfd.h"
Damien Millereba71ba2000-04-29 23:57:08 +100032#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 Millerf9b625c2000-07-09 22:42:32 +1000209 init_rng();
210
Damien Miller5428f641999-11-25 11:54:57 +1100211 /*
212 * Save the original real uid. It will be needed later (uid-swapping
213 * may clobber the real uid).
214 */
Damien Miller95def091999-11-25 00:26:21 +1100215 original_real_uid = getuid();
216 original_effective_uid = geteuid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* If we are installed setuid root be careful to not drop core. */
219 if (original_real_uid != original_effective_uid) {
220 struct rlimit rlim;
221 rlim.rlim_cur = rlim.rlim_max = 0;
222 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
223 fatal("setrlimit failed: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224 }
Damien Miller5428f641999-11-25 11:54:57 +1100225 /*
226 * Use uid-swapping to give up root privileges for the duration of
227 * option processing. We will re-instantiate the rights when we are
228 * ready to create the privileged port, and will permanently drop
229 * them when the port has been created (actually, when the connection
230 * has been made, as we may need to create the port several times).
231 */
Damien Miller95def091999-11-25 00:26:21 +1100232 temporarily_use_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Miller5428f641999-11-25 11:54:57 +1100234 /*
235 * Set our umask to something reasonable, as some files are created
236 * with the default umask. This will make them world-readable but
237 * writable only by the owner, which is ok for all files for which we
238 * don't set the modes explicitly.
239 */
Damien Miller95def091999-11-25 00:26:21 +1100240 umask(022);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
Damien Miller95def091999-11-25 00:26:21 +1100242 /* Save our own name. */
243 av0 = av[0];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller95def091999-11-25 00:26:21 +1100245 /* Initialize option structure to indicate that no values have been set. */
246 initialize_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller95def091999-11-25 00:26:21 +1100248 /* Parse command-line arguments. */
249 host = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Miller95def091999-11-25 00:26:21 +1100251 /* If program name is not one of the standard names, use it as host name. */
252 if (strchr(av0, '/'))
253 cp = strrchr(av0, '/') + 1;
254 else
255 cp = av0;
256 if (strcmp(cp, "rsh") != 0 && strcmp(cp, "ssh") != 0 &&
257 strcmp(cp, "rlogin") != 0 && strcmp(cp, "slogin") != 0)
258 host = cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Miller95def091999-11-25 00:26:21 +1100260 for (optind = 1; optind < ac; optind++) {
261 if (av[optind][0] != '-') {
262 if (host)
263 break;
264 if ((cp = strchr(av[optind], '@'))) {
Damien Miller4af51302000-04-16 11:18:38 +1000265 if(cp == av[optind])
266 usage();
Damien Miller95def091999-11-25 00:26:21 +1100267 options.user = av[optind];
268 *cp = '\0';
269 host = ++cp;
270 } else
271 host = av[optind];
272 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273 }
Damien Miller95def091999-11-25 00:26:21 +1100274 opt = av[optind][1];
275 if (!opt)
276 usage();
277 if (strchr("eilcpLRo", opt)) { /* options with arguments */
278 optarg = av[optind] + 2;
279 if (strcmp(optarg, "") == 0) {
280 if (optind >= ac - 1)
281 usage();
282 optarg = av[++optind];
283 }
284 } else {
285 if (av[optind][2])
286 usage();
287 optarg = NULL;
288 }
289 switch (opt) {
Damien Miller4af51302000-04-16 11:18:38 +1000290 case '2':
291 options.protocol = SSH_PROTO_2;
292 break;
Damien Miller34132e52000-01-14 15:45:46 +1100293 case '4':
294 IPv4or6 = AF_INET;
295 break;
Damien Miller34132e52000-01-14 15:45:46 +1100296 case '6':
297 IPv4or6 = AF_INET6;
298 break;
Damien Miller95def091999-11-25 00:26:21 +1100299 case 'n':
300 stdin_null_flag = 1;
301 break;
Damien Miller95def091999-11-25 00:26:21 +1100302 case 'f':
303 fork_after_authentication_flag = 1;
304 stdin_null_flag = 1;
305 break;
Damien Miller95def091999-11-25 00:26:21 +1100306 case 'x':
307 options.forward_x11 = 0;
308 break;
Damien Miller95def091999-11-25 00:26:21 +1100309 case 'X':
310 options.forward_x11 = 1;
311 break;
Damien Miller95def091999-11-25 00:26:21 +1100312 case 'g':
313 options.gateway_ports = 1;
314 break;
Damien Miller95def091999-11-25 00:26:21 +1100315 case 'P':
316 options.use_privileged_port = 0;
317 break;
Damien Miller95def091999-11-25 00:26:21 +1100318 case 'a':
319 options.forward_agent = 0;
320 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000321 case 'A':
322 options.forward_agent = 1;
323 break;
Damien Miller95def091999-11-25 00:26:21 +1100324#ifdef AFS
325 case 'k':
326 options.kerberos_tgt_passing = 0;
327 options.afs_token_passing = 0;
328 break;
329#endif
330 case 'i':
331 if (stat(optarg, &st) < 0) {
332 fprintf(stderr, "Warning: Identity file %s does not exist.\n",
333 optarg);
334 break;
335 }
336 if (options.num_identity_files >= SSH_MAX_IDENTITY_FILES)
337 fatal("Too many identity files specified (max %d)",
338 SSH_MAX_IDENTITY_FILES);
339 options.identity_files[options.num_identity_files++] =
340 xstrdup(optarg);
341 break;
Damien Miller95def091999-11-25 00:26:21 +1100342 case 't':
343 tty_flag = 1;
344 break;
Damien Miller95def091999-11-25 00:26:21 +1100345 case 'v':
346 case 'V':
Damien Miller78928792000-04-12 20:17:38 +1000347 fprintf(stderr, "SSH Version %s, protocol versions %d.%d/%d.%d.\n",
348 SSH_VERSION,
349 PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
350 PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
Damien Miller1383bd82000-04-06 12:32:37 +1000351 fprintf(stderr, "Compiled with SSL (0x%8.8lx).\n", SSLeay());
Damien Miller95def091999-11-25 00:26:21 +1100352 if (opt == 'V')
353 exit(0);
354 debug_flag = 1;
355 options.log_level = SYSLOG_LEVEL_DEBUG;
356 break;
Damien Miller95def091999-11-25 00:26:21 +1100357 case 'q':
358 options.log_level = SYSLOG_LEVEL_QUIET;
359 break;
Damien Miller95def091999-11-25 00:26:21 +1100360 case 'e':
361 if (optarg[0] == '^' && optarg[2] == 0 &&
362 (unsigned char) optarg[1] >= 64 && (unsigned char) optarg[1] < 128)
363 options.escape_char = (unsigned char) optarg[1] & 31;
364 else if (strlen(optarg) == 1)
365 options.escape_char = (unsigned char) optarg[0];
366 else if (strcmp(optarg, "none") == 0)
367 options.escape_char = -2;
368 else {
369 fprintf(stderr, "Bad escape character '%s'.\n", optarg);
370 exit(1);
371 }
372 break;
Damien Miller95def091999-11-25 00:26:21 +1100373 case 'c':
Damien Millereba71ba2000-04-29 23:57:08 +1000374 if (ciphers_valid(optarg)) {
375 /* SSH2 only */
376 options.ciphers = xstrdup(optarg);
Damien Miller30c3d422000-05-09 11:02:59 +1000377 options.cipher = SSH_CIPHER_ILLEGAL;
Damien Millereba71ba2000-04-29 23:57:08 +1000378 } else {
379 /* SSH1 only */
380 options.cipher = cipher_number(optarg);
381 if (options.cipher == -1) {
382 fprintf(stderr, "Unknown cipher type '%s'\n", optarg);
383 exit(1);
384 }
Damien Miller95def091999-11-25 00:26:21 +1100385 }
386 break;
Damien Miller95def091999-11-25 00:26:21 +1100387 case 'p':
388 options.port = atoi(optarg);
Damien Miller95def091999-11-25 00:26:21 +1100389 break;
Damien Miller95def091999-11-25 00:26:21 +1100390 case 'l':
391 options.user = optarg;
392 break;
Damien Miller95def091999-11-25 00:26:21 +1100393 case 'R':
Damien Miller34132e52000-01-14 15:45:46 +1100394 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
395 &fwd_host_port) != 3 &&
396 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
397 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100398 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
399 usage();
400 /* NOTREACHED */
401 }
402 add_remote_forward(&options, fwd_port, buf, fwd_host_port);
403 break;
Damien Miller95def091999-11-25 00:26:21 +1100404 case 'L':
Damien Miller34132e52000-01-14 15:45:46 +1100405 if (sscanf(optarg, "%hu/%255[^/]/%hu", &fwd_port, buf,
406 &fwd_host_port) != 3 &&
407 sscanf(optarg, "%hu:%255[^:]:%hu", &fwd_port, buf,
408 &fwd_host_port) != 3) {
Damien Miller95def091999-11-25 00:26:21 +1100409 fprintf(stderr, "Bad forwarding specification '%s'.\n", optarg);
410 usage();
411 /* NOTREACHED */
412 }
413 add_local_forward(&options, fwd_port, buf, fwd_host_port);
414 break;
Damien Miller95def091999-11-25 00:26:21 +1100415 case 'C':
416 options.compression = 1;
417 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000418 case 'N':
419 no_shell_flag = 1;
420 no_tty_flag = 1;
421 break;
Damien Miller1383bd82000-04-06 12:32:37 +1000422 case 'T':
423 no_tty_flag = 1;
424 break;
Damien Miller95def091999-11-25 00:26:21 +1100425 case 'o':
426 dummy = 1;
427 if (process_config_line(&options, host ? host : "", optarg,
428 "command-line", 0, &dummy) != 0)
429 exit(1);
430 break;
Damien Miller95def091999-11-25 00:26:21 +1100431 default:
432 usage();
433 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435
Damien Miller95def091999-11-25 00:26:21 +1100436 /* Check that we got a host name. */
437 if (!host)
438 usage();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439
Damien Miller95def091999-11-25 00:26:21 +1100440 /* Initialize the command to execute on remote host. */
441 buffer_init(&command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
Damien Millerd3a18572000-06-07 19:55:44 +1000443 SSLeay_add_all_algorithms();
Damien Miller7d6656c2000-05-20 15:22:36 +1000444
Damien Miller5428f641999-11-25 11:54:57 +1100445 /*
446 * Save the command to execute on the remote host in a buffer. There
447 * is no limit on the length of the command, except by the maximum
448 * packet size. Also sets the tty flag if there is no command.
449 */
Damien Miller95def091999-11-25 00:26:21 +1100450 if (optind == ac) {
451 /* No command specified - execute shell on a tty. */
452 tty_flag = 1;
453 } else {
454 /* A command has been specified. Store it into the
455 buffer. */
456 for (i = optind; i < ac; i++) {
457 if (i > optind)
458 buffer_append(&command, " ", 1);
459 buffer_append(&command, av[i], strlen(av[i]));
460 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462
Damien Miller95def091999-11-25 00:26:21 +1100463 /* Cannot fork to background if no command. */
464 if (fork_after_authentication_flag && buffer_len(&command) == 0)
465 fatal("Cannot fork into background without a command to execute.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
Damien Miller95def091999-11-25 00:26:21 +1100467 /* Allocate a tty by default if no command specified. */
468 if (buffer_len(&command) == 0)
469 tty_flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
Damien Miller95def091999-11-25 00:26:21 +1100471 /* Do not allocate a tty if stdin is not a tty. */
472 if (!isatty(fileno(stdin))) {
473 if (tty_flag)
474 fprintf(stderr, "Pseudo-terminal will not be allocated because stdin is not a terminal.\n");
475 tty_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476 }
Damien Miller1383bd82000-04-06 12:32:37 +1000477 /* force */
478 if (no_tty_flag)
479 tty_flag = 0;
480
Damien Miller95def091999-11-25 00:26:21 +1100481 /* Get user data. */
482 pw = getpwuid(original_real_uid);
483 if (!pw) {
484 fprintf(stderr, "You don't exist, go away!\n");
485 exit(1);
486 }
487 /* Take a copy of the returned structure. */
488 memset(&pwcopy, 0, sizeof(pwcopy));
489 pwcopy.pw_name = xstrdup(pw->pw_name);
490 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
491 pwcopy.pw_uid = pw->pw_uid;
492 pwcopy.pw_gid = pw->pw_gid;
493 pwcopy.pw_dir = xstrdup(pw->pw_dir);
494 pwcopy.pw_shell = xstrdup(pw->pw_shell);
495 pw = &pwcopy;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
Damien Miller95def091999-11-25 00:26:21 +1100497 /* Initialize "log" output. Since we are the client all output
498 actually goes to the terminal. */
499 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller95def091999-11-25 00:26:21 +1100501 /* Read per-user configuration file. */
502 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_CONFFILE);
503 read_config_file(buf, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504
Damien Miller95def091999-11-25 00:26:21 +1100505 /* Read systemwide configuration file. */
506 read_config_file(HOST_CONFIG_FILE, host, &options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Damien Miller95def091999-11-25 00:26:21 +1100508 /* Fill configuration defaults. */
509 fill_default_options(&options);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510
Damien Miller95def091999-11-25 00:26:21 +1100511 /* reinit */
512 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Millereba71ba2000-04-29 23:57:08 +1000514 /* check if RSA support exists */
515 if ((options.protocol & SSH_PROTO_1) &&
516 rsa_alive() == 0) {
517 log("%s: no RSA support in libssl and libcrypto. See ssl(8).",
518 __progname);
519 log("Disabling protocol version 1");
520 options.protocol &= ~ (SSH_PROTO_1|SSH_PROTO_1_PREFERRED);
521 }
522 if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) {
523 fprintf(stderr, "%s: No protocol version available.\n",
524 __progname);
525 exit(1);
526 }
527
Damien Miller95def091999-11-25 00:26:21 +1100528 if (options.user == NULL)
529 options.user = xstrdup(pw->pw_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530
Damien Miller95def091999-11-25 00:26:21 +1100531 if (options.hostname != NULL)
532 host = options.hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller95def091999-11-25 00:26:21 +1100534 /* Find canonic host name. */
535 if (strchr(host, '.') == 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100536 struct addrinfo hints;
537 struct addrinfo *ai = NULL;
538 int errgai;
539 memset(&hints, 0, sizeof(hints));
Damien Miller98c7ad62000-03-09 21:27:49 +1100540 hints.ai_family = IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +1100541 hints.ai_flags = AI_CANONNAME;
542 hints.ai_socktype = SOCK_STREAM;
543 errgai = getaddrinfo(host, NULL, &hints, &ai);
544 if (errgai == 0) {
545 if (ai->ai_canonname != NULL)
546 host = xstrdup(ai->ai_canonname);
547 freeaddrinfo(ai);
Damien Miller95def091999-11-25 00:26:21 +1100548 }
549 }
550 /* Disable rhosts authentication if not running as root. */
551 if (original_effective_uid != 0 || !options.use_privileged_port) {
552 options.rhosts_authentication = 0;
553 options.rhosts_rsa_authentication = 0;
554 }
Damien Miller5428f641999-11-25 11:54:57 +1100555 /*
556 * If using rsh has been selected, exec it now (without trying
557 * anything else). Note that we must release privileges first.
558 */
Damien Miller95def091999-11-25 00:26:21 +1100559 if (options.use_rsh) {
Damien Miller5428f641999-11-25 11:54:57 +1100560 /*
561 * Restore our superuser privileges. This must be done
562 * before permanently setting the uid.
563 */
Damien Miller95def091999-11-25 00:26:21 +1100564 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
Damien Miller95def091999-11-25 00:26:21 +1100566 /* Switch to the original uid permanently. */
567 permanently_set_uid(original_real_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568
Damien Miller95def091999-11-25 00:26:21 +1100569 /* Execute rsh. */
570 rsh_connect(host, options.user, &command);
571 fatal("rsh_connect returned");
572 }
573 /* Restore our superuser privileges. */
574 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575
Damien Miller5428f641999-11-25 11:54:57 +1100576 /*
577 * Open a connection to the remote host. This needs root privileges
578 * if rhosts_{rsa_}authentication is enabled.
579 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580
Damien Miller95def091999-11-25 00:26:21 +1100581 ok = ssh_connect(host, &hostaddr, options.port,
582 options.connection_attempts,
583 !options.rhosts_authentication &&
584 !options.rhosts_rsa_authentication,
585 original_real_uid,
586 options.proxy_command);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587
Damien Miller5428f641999-11-25 11:54:57 +1100588 /*
589 * If we successfully made the connection, load the host private key
590 * in case we will need it later for combined rsa-rhosts
591 * authentication. This must be done before releasing extra
592 * privileges, because the file is only readable by root.
593 */
Damien Millereba71ba2000-04-29 23:57:08 +1000594 if (ok && (options.protocol & SSH_PROTO_1)) {
595 Key k;
Damien Miller95def091999-11-25 00:26:21 +1100596 host_private_key = RSA_new();
Damien Millereba71ba2000-04-29 23:57:08 +1000597 k.type = KEY_RSA;
598 k.rsa = host_private_key;
599 if (load_private_key(HOST_KEY_FILE, "", &k, NULL))
Damien Miller95def091999-11-25 00:26:21 +1100600 host_private_key_loaded = 1;
601 }
Damien Miller5428f641999-11-25 11:54:57 +1100602 /*
603 * Get rid of any extra privileges that we may have. We will no
604 * longer need them. Also, extra privileges could make it very hard
605 * to read identity files and other non-world-readable files from the
606 * user's home directory if it happens to be on a NFS volume where
607 * root is mapped to nobody.
608 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609
Damien Miller5428f641999-11-25 11:54:57 +1100610 /*
611 * Note that some legacy systems need to postpone the following call
612 * to permanently_set_uid() until the private hostkey is destroyed
613 * with RSA_free(). Otherwise the calling user could ptrace() the
614 * process, read the private hostkey and impersonate the host.
615 * OpenBSD does not allow ptracing of setuid processes.
616 */
Damien Miller95def091999-11-25 00:26:21 +1100617 permanently_set_uid(original_real_uid);
618
Damien Miller5428f641999-11-25 11:54:57 +1100619 /*
620 * Now that we are back to our own permissions, create ~/.ssh
621 * directory if it doesn\'t already exist.
622 */
Damien Miller95def091999-11-25 00:26:21 +1100623 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, SSH_USER_DIR);
624 if (stat(buf, &st) < 0)
Damien Millerbe484b52000-07-15 14:14:16 +1000625 if (mkdir(buf, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100626 error("Could not create directory '%.200s'.", buf);
627
628 /* Check if the connection failed, and try "rsh" if appropriate. */
629 if (!ok) {
630 if (options.port != 0)
Damien Milleraae6c611999-12-06 11:47:28 +1100631 log("Secure connection to %.100s on port %hu refused%.100s.",
Damien Miller95def091999-11-25 00:26:21 +1100632 host, options.port,
633 options.fallback_to_rsh ? "; reverting to insecure method" : "");
634 else
635 log("Secure connection to %.100s refused%.100s.", host,
636 options.fallback_to_rsh ? "; reverting to insecure method" : "");
637
638 if (options.fallback_to_rsh) {
639 rsh_connect(host, options.user, &command);
640 fatal("rsh_connect returned");
641 }
642 exit(1);
643 }
644 /* Expand ~ in options.identity_files. */
Damien Millereba71ba2000-04-29 23:57:08 +1000645 /* XXX mem-leaks */
Damien Miller95def091999-11-25 00:26:21 +1100646 for (i = 0; i < options.num_identity_files; i++)
647 options.identity_files[i] =
648 tilde_expand_filename(options.identity_files[i], original_real_uid);
Damien Millereba71ba2000-04-29 23:57:08 +1000649 for (i = 0; i < options.num_identity_files2; i++)
650 options.identity_files2[i] =
651 tilde_expand_filename(options.identity_files2[i], original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100652 /* Expand ~ in known host file names. */
653 options.system_hostfile = tilde_expand_filename(options.system_hostfile,
Damien Millereba71ba2000-04-29 23:57:08 +1000654 original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100655 options.user_hostfile = tilde_expand_filename(options.user_hostfile,
Damien Millereba71ba2000-04-29 23:57:08 +1000656 original_real_uid);
657 options.system_hostfile2 = tilde_expand_filename(options.system_hostfile2,
658 original_real_uid);
659 options.user_hostfile2 = tilde_expand_filename(options.user_hostfile2,
660 original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100661
662 /* Log into the remote system. This never returns if the login fails. */
663 ssh_login(host_private_key_loaded, host_private_key,
Damien Miller34132e52000-01-14 15:45:46 +1100664 host, (struct sockaddr *)&hostaddr, original_real_uid);
Damien Miller95def091999-11-25 00:26:21 +1100665
666 /* We no longer need the host private key. Clear it now. */
667 if (host_private_key_loaded)
668 RSA_free(host_private_key); /* Destroys contents safely */
669
Damien Miller1383bd82000-04-06 12:32:37 +1000670 exit_status = compat20 ? ssh_session2() : ssh_session();
671 packet_close();
672 return exit_status;
673}
674
Damien Millerbd483e72000-04-30 10:00:53 +1000675void
676x11_get_proto(char *proto, int proto_len, char *data, int data_len)
677{
678 char line[512];
679 FILE *f;
680 int got_data = 0, i;
681
Damien Millerd3a18572000-06-07 19:55:44 +1000682 if (options.xauth_location) {
683 /* Try to get Xauthority information for the display. */
684 snprintf(line, sizeof line, "%.100s list %.200s 2>/dev/null",
685 options.xauth_location, getenv("DISPLAY"));
686 f = popen(line, "r");
687 if (f && fgets(line, sizeof(line), f) &&
688 sscanf(line, "%*s %s %s", proto, data) == 2)
689 got_data = 1;
690 if (f)
691 pclose(f);
692 }
Damien Millerbd483e72000-04-30 10:00:53 +1000693 /*
694 * If we didn't get authentication data, just make up some
695 * data. The forwarding code will check the validity of the
696 * response anyway, and substitute this data. The X11
697 * server, however, will ignore this fake data and use
698 * whatever authentication mechanisms it was using otherwise
699 * for the local connection.
700 */
701 if (!got_data) {
702 u_int32_t rand = 0;
703
704 strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
705 for (i = 0; i < 16; i++) {
706 if (i % 4 == 0)
707 rand = arc4random();
708 snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
709 rand >>= 8;
710 }
711 }
712}
713
Damien Miller1383bd82000-04-06 12:32:37 +1000714int
715ssh_session(void)
716{
717 int type;
718 int i;
719 int plen;
720 int interactive = 0;
721 int have_tty = 0;
722 struct winsize ws;
723 int authfd;
724 char *cp;
725
Damien Miller95def091999-11-25 00:26:21 +1100726 /* Enable compression if requested. */
727 if (options.compression) {
728 debug("Requesting compression at level %d.", options.compression_level);
729
730 if (options.compression_level < 1 || options.compression_level > 9)
731 fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
732
733 /* Send the request. */
734 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
735 packet_put_int(options.compression_level);
736 packet_send();
737 packet_write_wait();
738 type = packet_read(&plen);
739 if (type == SSH_SMSG_SUCCESS)
740 packet_start_compression(options.compression_level);
741 else if (type == SSH_SMSG_FAILURE)
742 log("Warning: Remote host refused compression.");
743 else
744 packet_disconnect("Protocol error waiting for compression response.");
745 }
746 /* Allocate a pseudo tty if appropriate. */
747 if (tty_flag) {
748 debug("Requesting pty.");
749
750 /* Start the packet. */
751 packet_start(SSH_CMSG_REQUEST_PTY);
752
753 /* Store TERM in the packet. There is no limit on the
754 length of the string. */
755 cp = getenv("TERM");
756 if (!cp)
757 cp = "";
758 packet_put_string(cp, strlen(cp));
759
760 /* Store window size in the packet. */
761 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
762 memset(&ws, 0, sizeof(ws));
763 packet_put_int(ws.ws_row);
764 packet_put_int(ws.ws_col);
765 packet_put_int(ws.ws_xpixel);
766 packet_put_int(ws.ws_ypixel);
767
768 /* Store tty modes in the packet. */
769 tty_make_modes(fileno(stdin));
770
771 /* Send the packet, and wait for it to leave. */
772 packet_send();
773 packet_write_wait();
774
775 /* Read response from the server. */
776 type = packet_read(&plen);
Damien Miller450a7a12000-03-26 13:04:51 +1000777 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100778 interactive = 1;
Damien Miller1383bd82000-04-06 12:32:37 +1000779 have_tty = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000780 } else if (type == SSH_SMSG_FAILURE)
Damien Miller95def091999-11-25 00:26:21 +1100781 log("Warning: Remote host failed or refused to allocate a pseudo tty.");
782 else
783 packet_disconnect("Protocol error waiting for pty request response.");
784 }
785 /* Request X11 forwarding if enabled and DISPLAY is set. */
786 if (options.forward_x11 && getenv("DISPLAY") != NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +1000787 char proto[512], data[512];
788 /* Get reasonable local authentication information. */
789 x11_get_proto(proto, sizeof proto, data, sizeof data);
790 /* Request forwarding with authentication spoofing. */
Damien Miller95def091999-11-25 00:26:21 +1100791 debug("Requesting X11 forwarding with authentication spoofing.");
Damien Millerbd483e72000-04-30 10:00:53 +1000792 x11_request_forwarding_with_spoofing(0, proto, data);
Damien Miller95def091999-11-25 00:26:21 +1100793
794 /* Read response from the server. */
795 type = packet_read(&plen);
796 if (type == SSH_SMSG_SUCCESS) {
Damien Miller95def091999-11-25 00:26:21 +1100797 interactive = 1;
Damien Millerbd483e72000-04-30 10:00:53 +1000798 } else if (type == SSH_SMSG_FAILURE) {
Damien Miller95def091999-11-25 00:26:21 +1100799 log("Warning: Remote host denied X11 forwarding.");
Damien Millerbd483e72000-04-30 10:00:53 +1000800 } else {
Damien Miller95def091999-11-25 00:26:21 +1100801 packet_disconnect("Protocol error waiting for X11 forwarding");
Damien Millerbd483e72000-04-30 10:00:53 +1000802 }
Damien Miller95def091999-11-25 00:26:21 +1100803 }
804 /* Tell the packet module whether this is an interactive session. */
805 packet_set_interactive(interactive, options.keepalives);
806
807 /* Clear agent forwarding if we don\'t have an agent. */
808 authfd = ssh_get_authentication_socket();
809 if (authfd < 0)
810 options.forward_agent = 0;
811 else
812 ssh_close_authentication_socket(authfd);
813
814 /* Request authentication agent forwarding if appropriate. */
815 if (options.forward_agent) {
816 debug("Requesting authentication agent forwarding.");
817 auth_request_forwarding();
818
819 /* Read response from the server. */
820 type = packet_read(&plen);
821 packet_integrity_check(plen, 0, type);
822 if (type != SSH_SMSG_SUCCESS)
823 log("Warning: Remote host denied authentication agent forwarding.");
824 }
825 /* Initiate local TCP/IP port forwardings. */
826 for (i = 0; i < options.num_local_forwards; i++) {
827 debug("Connections to local port %d forwarded to remote address %.200s:%d",
828 options.local_forwards[i].port,
829 options.local_forwards[i].host,
830 options.local_forwards[i].host_port);
831 channel_request_local_forwarding(options.local_forwards[i].port,
Damien Miller4af51302000-04-16 11:18:38 +1000832 options.local_forwards[i].host,
Damien Millera34a28b1999-12-14 10:47:15 +1100833 options.local_forwards[i].host_port,
834 options.gateway_ports);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835 }
836
Damien Miller95def091999-11-25 00:26:21 +1100837 /* Initiate remote TCP/IP port forwardings. */
838 for (i = 0; i < options.num_remote_forwards; i++) {
839 debug("Connections to remote port %d forwarded to local address %.200s:%d",
840 options.remote_forwards[i].port,
841 options.remote_forwards[i].host,
842 options.remote_forwards[i].host_port);
843 channel_request_remote_forwarding(options.remote_forwards[i].port,
844 options.remote_forwards[i].host,
Damien Miller4af51302000-04-16 11:18:38 +1000845 options.remote_forwards[i].host_port);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847
Damien Miller5428f641999-11-25 11:54:57 +1100848 /* If requested, let ssh continue in the background. */
Damien Miller4af51302000-04-16 11:18:38 +1000849 if (fork_after_authentication_flag)
Damien Miller5428f641999-11-25 11:54:57 +1100850 if (daemon(1, 1) < 0)
851 fatal("daemon() failed: %.200s", strerror(errno));
852
853 /*
854 * If a command was specified on the command line, execute the
855 * command now. Otherwise request the server to start a shell.
856 */
Damien Miller95def091999-11-25 00:26:21 +1100857 if (buffer_len(&command) > 0) {
858 int len = buffer_len(&command);
859 if (len > 900)
860 len = 900;
861 debug("Sending command: %.*s", len, buffer_ptr(&command));
862 packet_start(SSH_CMSG_EXEC_CMD);
863 packet_put_string(buffer_ptr(&command), buffer_len(&command));
864 packet_send();
865 packet_write_wait();
866 } else {
867 debug("Requesting shell.");
868 packet_start(SSH_CMSG_EXEC_SHELL);
869 packet_send();
870 packet_write_wait();
871 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Damien Miller95def091999-11-25 00:26:21 +1100873 /* Enter the interactive session. */
Damien Miller1383bd82000-04-06 12:32:37 +1000874 return client_loop(have_tty, tty_flag ? options.escape_char : -1);
875}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876
Damien Miller1383bd82000-04-06 12:32:37 +1000877void
878init_local_fwd(void)
879{
880 int i;
881 /* Initiate local TCP/IP port forwardings. */
882 for (i = 0; i < options.num_local_forwards; i++) {
883 debug("Connections to local port %d forwarded to remote address %.200s:%d",
884 options.local_forwards[i].port,
885 options.local_forwards[i].host,
886 options.local_forwards[i].host_port);
887 channel_request_local_forwarding(options.local_forwards[i].port,
Damien Miller4af51302000-04-16 11:18:38 +1000888 options.local_forwards[i].host,
Damien Miller1383bd82000-04-06 12:32:37 +1000889 options.local_forwards[i].host_port,
890 options.gateway_ports);
891 }
892}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller1383bd82000-04-06 12:32:37 +1000894extern void client_set_session_ident(int id);
895
896void
897client_init(int id, void *arg)
898{
899 int len;
900 debug("client_init id %d arg %d", id, (int)arg);
901
902 if (no_shell_flag)
903 goto done;
904
905 if (tty_flag) {
906 struct winsize ws;
907 char *cp;
908 cp = getenv("TERM");
909 if (!cp)
910 cp = "";
911 /* Store window size in the packet. */
912 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
913 memset(&ws, 0, sizeof(ws));
914
915 channel_request_start(id, "pty-req", 0);
916 packet_put_cstring(cp);
917 packet_put_int(ws.ws_col);
918 packet_put_int(ws.ws_row);
919 packet_put_int(ws.ws_xpixel);
920 packet_put_int(ws.ws_ypixel);
921 packet_put_cstring(""); /* XXX: encode terminal modes */
922 packet_send();
923 /* XXX wait for reply */
924 }
Damien Millerbd483e72000-04-30 10:00:53 +1000925 if (options.forward_x11 &&
926 getenv("DISPLAY") != NULL) {
927 char proto[512], data[512];
928 /* Get reasonable local authentication information. */
929 x11_get_proto(proto, sizeof proto, data, sizeof data);
930 /* Request forwarding with authentication spoofing. */
931 debug("Requesting X11 forwarding with authentication spoofing.");
932 x11_request_forwarding_with_spoofing(id, proto, data);
933 /* XXX wait for reply */
934 }
935
Damien Miller1383bd82000-04-06 12:32:37 +1000936 len = buffer_len(&command);
937 if (len > 0) {
938 if (len > 900)
939 len = 900;
940 debug("Sending command: %.*s", len, buffer_ptr(&command));
941 channel_request_start(id, "exec", 0);
942 packet_put_string(buffer_ptr(&command), len);
943 packet_send();
944 } else {
945 channel_request(id, "shell", 0);
946 }
947 /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
948done:
949 /* register different callback, etc. XXX */
950 client_set_session_ident(id);
951}
952
953int
954ssh_session2(void)
955{
956 int window, packetmax, id;
957 int in = dup(STDIN_FILENO);
958 int out = dup(STDOUT_FILENO);
959 int err = dup(STDERR_FILENO);
960
961 if (in < 0 || out < 0 || err < 0)
962 fatal("dump in/out/err failed");
963
964 /* should be pre-session */
965 init_local_fwd();
966
967 window = 32*1024;
968 if (tty_flag) {
969 packetmax = window/8;
970 } else {
971 window *= 2;
972 packetmax = window/2;
973 }
974
975 id = channel_new(
976 "session", SSH_CHANNEL_OPENING, in, out, err,
977 window, packetmax, CHAN_EXTENDED_WRITE, xstrdup("client-session"));
978
979
980 channel_open(id);
981 channel_register_callback(id, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, client_init, (void *)0);
982
983 return client_loop(tty_flag, tty_flag ? options.escape_char : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984}