]> andersk Git - gssapi-openssh.git/blame - openssh/ssh.c
Initial revision
[gssapi-openssh.git] / openssh / ssh.c
CommitLineData
5262cbfb 1/* $OpenBSD: ssh.c,v 1.324 2009/02/12 03:00:56 djm Exp $ */
3c0ef626 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 * 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 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
16 * Copyright (c) 1999 Niels Provos. All rights reserved.
540d72c3 17 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
3c0ef626 18 *
19 * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20 * in Canada (German citizen).
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43#include "includes.h"
30460aeb 44
45#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
49#include <sys/resource.h>
50#include <sys/ioctl.h>
51#include <sys/socket.h>
30460aeb 52
53#include <ctype.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <netdb.h>
57#ifdef HAVE_PATHS_H
58#include <paths.h>
59#endif
60#include <pwd.h>
61#include <signal.h>
62#include <stdarg.h>
63#include <stddef.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68
69#include <netinet/in.h>
70#include <arpa/inet.h>
3c0ef626 71
72#include <openssl/evp.h>
73#include <openssl/err.h>
e74dc197 74#include "openbsd-compat/openssl-compat.h"
5156b1a1 75#include "openbsd-compat/sys-queue.h"
3c0ef626 76
30460aeb 77#include "xmalloc.h"
3c0ef626 78#include "ssh.h"
79#include "ssh1.h"
80#include "ssh2.h"
81#include "compat.h"
82#include "cipher.h"
3c0ef626 83#include "packet.h"
84#include "buffer.h"
3c0ef626 85#include "channels.h"
86#include "key.h"
87#include "authfd.h"
88#include "authfile.h"
89#include "pathnames.h"
7e82606e 90#include "dispatch.h"
3c0ef626 91#include "clientloop.h"
92#include "log.h"
93#include "readconf.h"
94#include "sshconnect.h"
3c0ef626 95#include "misc.h"
96#include "kex.h"
97#include "mac.h"
7e82606e 98#include "sshpty.h"
99#include "match.h"
100#include "msg.h"
7e82606e 101#include "uidswap.h"
30460aeb 102#include "version.h"
3c0ef626 103
104#ifdef SMARTCARD
3c0ef626 105#include "scard.h"
106#endif
107
3c0ef626 108extern char *__progname;
3c0ef626 109
5156b1a1 110/* Flag indicating whether debug mode is on. May be set on the command line. */
3c0ef626 111int debug_flag = 0;
112
113/* Flag indicating whether a tty should be allocated */
114int tty_flag = 0;
115int no_tty_flag = 0;
116int force_tty_flag = 0;
117
118/* don't exec a shell */
119int no_shell_flag = 0;
120
121/*
122 * Flag indicating that nothing should be read from stdin. This can be set
123 * on the command line.
124 */
125int stdin_null_flag = 0;
126
127/*
128 * Flag indicating that ssh should fork after authentication. This is useful
44a053a3 129 * so that the passphrase can be entered manually, and then ssh goes to the
3c0ef626 130 * background.
131 */
132int fork_after_authentication_flag = 0;
133
134/*
135 * General data structure for command line options and options configurable
136 * in configuration files. See readconf.h.
137 */
138Options options;
139
140/* optional user configfile */
141char *config = NULL;
142
143/*
144 * Name of the host we are connecting to. This is the name given on the
145 * command line, or the HostName specified for the user-supplied name in a
146 * configuration file.
147 */
148char *host;
149
150/* socket address the host resolves to */
151struct sockaddr_storage hostaddr;
152
153/* Private host keys. */
44a053a3 154Sensitive sensitive_data;
3c0ef626 155
156/* Original real UID. */
157uid_t original_real_uid;
44a053a3 158uid_t original_effective_uid;
3c0ef626 159
160/* command to be executed */
161Buffer command;
162
163/* Should we execute a command or invoke a subsystem? */
164int subsystem_flag = 0;
165
c6046bb0 166/* # of replies received for global requests */
5156b1a1 167static int remote_forward_confirms_received = 0;
c6046bb0 168
d03f4262 169/* pid of proxycommand child process */
170pid_t proxy_command_pid = 0;
171
5156b1a1 172/* mux.c */
173extern int muxserver_sock;
174extern u_int muxclient_command;
7e82606e 175
3c0ef626 176/* Prints a help message to the user. This function never returns. */
177
178static void
179usage(void)
180{
12a403af 181 fprintf(stderr,
5262cbfb 182"usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
08822d99 183" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
dfddba3d 184" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
185" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
186" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
30460aeb 187" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
12a403af 188 );
08822d99 189 exit(255);
3c0ef626 190}
191
3c0ef626 192static int ssh_session(void);
193static int ssh_session2(void);
194static void load_public_identity_files(void);
5156b1a1 195
196/* from muxclient.c */
197void muxclient(const char *);
198void muxserver_listen(void);
3c0ef626 199
200/*
201 * Main program for the ssh client.
202 */
203int
204main(int ac, char **av)
205{
5262cbfb 206 int i, opt, exit_status, use_syslog;
540d72c3 207 char *p, *cp, *line, buf[256];
3c0ef626 208 struct stat st;
209 struct passwd *pw;
e74dc197 210 int dummy, timeout_ms;
3c0ef626 211 extern int optind, optreset;
212 extern char *optarg;
2ce0bfe4 213 struct servent *sp;
dfddba3d 214 Forward fwd;
3c0ef626 215
08822d99 216 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
217 sanitise_stdfd();
218
7cac2b65 219 __progname = ssh_get_progname(av[0]);
3c0ef626 220 init_rng();
221
222 /*
223 * Save the original real uid. It will be needed later (uid-swapping
224 * may clobber the real uid).
225 */
226 original_real_uid = getuid();
227 original_effective_uid = geteuid();
540d72c3 228
d03f4262 229 /*
230 * Use uid-swapping to give up root privileges for the duration of
231 * option processing. We will re-instantiate the rights when we are
232 * ready to create the privileged port, and will permanently drop
233 * them when the port has been created (actually, when the connection
234 * has been made, as we may need to create the port several times).
235 */
236 PRIV_END;
3c0ef626 237
238#ifdef HAVE_SETRLIMIT
239 /* If we are installed setuid root be careful to not drop core. */
240 if (original_real_uid != original_effective_uid) {
241 struct rlimit rlim;
242 rlim.rlim_cur = rlim.rlim_max = 0;
243 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
244 fatal("setrlimit failed: %.100s", strerror(errno));
245 }
246#endif
247 /* Get user data. */
248 pw = getpwuid(original_real_uid);
249 if (!pw) {
7cac2b65 250 logit("You don't exist, go away!");
08822d99 251 exit(255);
3c0ef626 252 }
253 /* Take a copy of the returned structure. */
254 pw = pwcopy(pw);
255
3c0ef626 256 /*
257 * Set our umask to something reasonable, as some files are created
258 * with the default umask. This will make them world-readable but
259 * writable only by the owner, which is ok for all files for which we
260 * don't set the modes explicitly.
261 */
262 umask(022);
263
5156b1a1 264 /*
265 * Initialize option structure to indicate that no values have been
266 * set.
267 */
3c0ef626 268 initialize_options(&options);
269
270 /* Parse command-line arguments. */
271 host = NULL;
5262cbfb 272 use_syslog = 0;
473db5ab 273
30460aeb 274 again:
5156b1a1 275 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
5262cbfb 276 "ACD:F:I:KL:MNO:PR:S:TVw:XYy")) != -1) {
3c0ef626 277 switch (opt) {
278 case '1':
279 options.protocol = SSH_PROTO_1;
280 break;
281 case '2':
282 options.protocol = SSH_PROTO_2;
283 break;
284 case '4':
7cac2b65 285 options.address_family = AF_INET;
3c0ef626 286 break;
287 case '6':
7cac2b65 288 options.address_family = AF_INET6;
3c0ef626 289 break;
290 case 'n':
291 stdin_null_flag = 1;
292 break;
293 case 'f':
294 fork_after_authentication_flag = 1;
295 stdin_null_flag = 1;
296 break;
297 case 'x':
298 options.forward_x11 = 0;
299 break;
300 case 'X':
301 options.forward_x11 = 1;
302 break;
5262cbfb 303 case 'y':
304 use_syslog = 1;
305 break;
540d72c3 306 case 'Y':
307 options.forward_x11 = 1;
308 options.forward_x11_trusted = 1;
309 break;
3c0ef626 310 case 'g':
311 options.gateway_ports = 1;
312 break;
dfddba3d 313 case 'O':
314 if (strcmp(optarg, "check") == 0)
5156b1a1 315 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
dfddba3d 316 else if (strcmp(optarg, "exit") == 0)
5156b1a1 317 muxclient_command = SSHMUX_COMMAND_TERMINATE;
dfddba3d 318 else
319 fatal("Invalid multiplex command.");
320 break;
d03f4262 321 case 'P': /* deprecated */
3c0ef626 322 options.use_privileged_port = 0;
323 break;
324 case 'a':
325 options.forward_agent = 0;
326 break;
327 case 'A':
328 options.forward_agent = 1;
329 break;
3c0ef626 330 case 'k':
540d72c3 331 options.gss_deleg_creds = 0;
3c0ef626 332 break;
fa0f0f45 333 case 'K':
334 options.gss_authentication = 1;
335 options.gss_deleg_creds = 1;
336 break;
3c0ef626 337 case 'i':
338 if (stat(optarg, &st) < 0) {
339 fprintf(stderr, "Warning: Identity file %s "
dfddba3d 340 "not accessible: %s.\n", optarg,
341 strerror(errno));
3c0ef626 342 break;
343 }
344 if (options.num_identity_files >=
345 SSH_MAX_IDENTITY_FILES)
346 fatal("Too many identity files specified "
347 "(max %d)", SSH_MAX_IDENTITY_FILES);
348 options.identity_files[options.num_identity_files++] =
349 xstrdup(optarg);
350 break;
351 case 'I':
352#ifdef SMARTCARD
353 options.smartcard_device = xstrdup(optarg);
354#else
355 fprintf(stderr, "no support for smartcards.\n");
356#endif
357 break;
358 case 't':
359 if (tty_flag)
360 force_tty_flag = 1;
361 tty_flag = 1;
362 break;
363 case 'v':
7cac2b65 364 if (debug_flag == 0) {
3c0ef626 365 debug_flag = 1;
366 options.log_level = SYSLOG_LEVEL_DEBUG1;
7cac2b65 367 } else {
368 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
369 options.log_level++;
3c0ef626 370 break;
7cac2b65 371 }
dfddba3d 372 /* FALLTHROUGH */
3c0ef626 373 case 'V':
12a403af 374 fprintf(stderr, "%s, %s\n",
dfddba3d 375 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
3c0ef626 376 if (opt == 'V')
377 exit(0);
378 break;
08822d99 379 case 'w':
380 if (options.tun_open == -1)
381 options.tun_open = SSH_TUNMODE_DEFAULT;
382 options.tun_local = a2tun(optarg, &options.tun_remote);
383 if (options.tun_local == SSH_TUNID_ERR) {
5156b1a1 384 fprintf(stderr,
385 "Bad tun device '%s'\n", optarg);
08822d99 386 exit(255);
387 }
388 break;
3c0ef626 389 case 'q':
390 options.log_level = SYSLOG_LEVEL_QUIET;
391 break;
392 case 'e':
393 if (optarg[0] == '^' && optarg[2] == 0 &&
394 (u_char) optarg[1] >= 64 &&
395 (u_char) optarg[1] < 128)
396 options.escape_char = (u_char) optarg[1] & 31;
397 else if (strlen(optarg) == 1)
398 options.escape_char = (u_char) optarg[0];
399 else if (strcmp(optarg, "none") == 0)
400 options.escape_char = SSH_ESCAPECHAR_NONE;
401 else {
402 fprintf(stderr, "Bad escape character '%s'.\n",
403 optarg);
08822d99 404 exit(255);
3c0ef626 405 }
406 break;
407 case 'c':
408 if (ciphers_valid(optarg)) {
409 /* SSH2 only */
410 options.ciphers = xstrdup(optarg);
7e82606e 411 options.cipher = SSH_CIPHER_INVALID;
3c0ef626 412 } else {
413 /* SSH1 only */
414 options.cipher = cipher_number(optarg);
415 if (options.cipher == -1) {
416 fprintf(stderr,
417 "Unknown cipher type '%s'\n",
418 optarg);
08822d99 419 exit(255);
3c0ef626 420 }
421 if (options.cipher == SSH_CIPHER_3DES)
422 options.ciphers = "3des-cbc";
423 else if (options.cipher == SSH_CIPHER_BLOWFISH)
424 options.ciphers = "blowfish-cbc";
425 else
426 options.ciphers = (char *)-1;
427 }
428 break;
429 case 'm':
430 if (mac_valid(optarg))
431 options.macs = xstrdup(optarg);
432 else {
433 fprintf(stderr, "Unknown mac type '%s'\n",
434 optarg);
08822d99 435 exit(255);
3c0ef626 436 }
437 break;
7e82606e 438 case 'M':
2ce0bfe4 439 if (options.control_master == SSHCTL_MASTER_YES)
440 options.control_master = SSHCTL_MASTER_ASK;
441 else
442 options.control_master = SSHCTL_MASTER_YES;
7e82606e 443 break;
3c0ef626 444 case 'p':
445 options.port = a2port(optarg);
5262cbfb 446 if (options.port <= 0) {
3c0ef626 447 fprintf(stderr, "Bad port '%s'\n", optarg);
08822d99 448 exit(255);
3c0ef626 449 }
450 break;
451 case 'l':
452 options.user = optarg;
453 break;
454
455 case 'L':
5262cbfb 456 if (parse_forward(&fwd, optarg, 0, 0))
dfddba3d 457 add_local_forward(&options, &fwd);
458 else {
3c0ef626 459 fprintf(stderr,
dfddba3d 460 "Bad local forwarding specification '%s'\n",
3c0ef626 461 optarg);
08822d99 462 exit(255);
3c0ef626 463 }
dfddba3d 464 break;
465
466 case 'R':
5262cbfb 467 if (parse_forward(&fwd, optarg, 0, 1)) {
dfddba3d 468 add_remote_forward(&options, &fwd);
469 } else {
3c0ef626 470 fprintf(stderr,
dfddba3d 471 "Bad remote forwarding specification "
472 "'%s'\n", optarg);
08822d99 473 exit(255);
3c0ef626 474 }
3c0ef626 475 break;
476
477 case 'D':
5262cbfb 478 if (parse_forward(&fwd, optarg, 1, 0)) {
479 add_local_forward(&options, &fwd);
dfddba3d 480 } else {
5262cbfb 481 fprintf(stderr,
482 "Bad dynamic forwarding specification "
483 "'%s'\n", optarg);
08822d99 484 exit(255);
3c0ef626 485 }
3c0ef626 486 break;
487
488 case 'C':
489 options.compression = 1;
490 break;
491 case 'N':
492 no_shell_flag = 1;
493 no_tty_flag = 1;
3c0ef626 494 break;
495 case 'o':
496 dummy = 1;
540d72c3 497 line = xstrdup(optarg);
3c0ef626 498 if (process_config_line(&options, host ? host : "",
540d72c3 499 line, "command-line", 0, &dummy) != 0)
08822d99 500 exit(255);
540d72c3 501 xfree(line);
3c0ef626 502 break;
d3057ca4 503 case 'T':
504 no_tty_flag = 1;
505 /* ensure that the user doesn't try to backdoor a */
506 /* null cipher switch on an interactive session */
507 /* so explicitly disable it no matter what */
508 options.none_switch=0;
509 break;
3c0ef626 510 case 's':
511 subsystem_flag = 1;
512 break;
7e82606e 513 case 'S':
514 if (options.control_path != NULL)
515 free(options.control_path);
516 options.control_path = xstrdup(optarg);
517 break;
3c0ef626 518 case 'b':
519 options.bind_address = optarg;
3f48cae5 520 break;
3f750d99 521 case 'F':
522 config = optarg;
523 break;
3c0ef626 524 default:
525 usage();
526 }
527 }
528
529 ac -= optind;
530 av += optind;
531
532 if (ac > 0 && !host && **av != '-') {
bfe49944 533 if (strrchr(*av, '@')) {
3c0ef626 534 p = xstrdup(*av);
bfe49944 535 cp = strrchr(p, '@');
3c0ef626 536 if (cp == NULL || cp == p)
537 usage();
538 options.user = p;
539 *cp = '\0';
540 host = ++cp;
541 } else
542 host = *av;
bfe49944 543 if (ac > 1) {
544 optind = optreset = 1;
3c0ef626 545 goto again;
546 }
bfe49944 547 ac--, av++;
3c0ef626 548 }
549
550 /* Check that we got a host name. */
551 if (!host)
552 usage();
553
554 SSLeay_add_all_algorithms();
555 ERR_load_crypto_strings();
3c0ef626 556
557 /* Initialize the command to execute on remote host. */
558 buffer_init(&command);
559
560 /*
561 * Save the command to execute on the remote host in a buffer. There
562 * is no limit on the length of the command, except by the maximum
563 * packet size. Also sets the tty flag if there is no command.
564 */
565 if (!ac) {
566 /* No command specified - execute shell on a tty. */
567 tty_flag = 1;
568 if (subsystem_flag) {
569 fprintf(stderr,
570 "You must specify a subsystem to invoke.\n");
571 usage();
572 }
573 } else {
574 /* A command has been specified. Store it into the buffer. */
575 for (i = 0; i < ac; i++) {
576 if (i)
577 buffer_append(&command, " ", 1);
578 buffer_append(&command, av[i], strlen(av[i]));
579 }
580 }
581
582 /* Cannot fork to background if no command. */
5156b1a1 583 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
584 !no_shell_flag)
585 fatal("Cannot fork into background without a command "
586 "to execute.");
3c0ef626 587
588 /* Allocate a tty by default if no command specified. */
589 if (buffer_len(&command) == 0)
590 tty_flag = 1;
591
d03f4262 592 /* Force no tty */
3c0ef626 593 if (no_tty_flag)
594 tty_flag = 0;
595 /* Do not allocate a tty if stdin is not a tty. */
2ce0bfe4 596 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
3c0ef626 597 if (tty_flag)
5156b1a1 598 logit("Pseudo-terminal will not be allocated because "
599 "stdin is not a terminal.");
3c0ef626 600 tty_flag = 0;
601 }
602
603 /*
604 * Initialize "log" output. Since we are the client all output
605 * actually goes to stderr.
606 */
5156b1a1 607 log_init(av[0],
608 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
5262cbfb 609 SYSLOG_FACILITY_USER, !use_syslog);
3c0ef626 610
611 /*
612 * Read per-user configuration file. Ignore the system wide config
613 * file if the user specifies a config file on the command line.
614 */
615 if (config != NULL) {
7e82606e 616 if (!read_config_file(config, host, &options, 0))
3c0ef626 617 fatal("Can't open user config file %.100s: "
618 "%.100s", config, strerror(errno));
0b90ac93 619 } else {
c6046bb0 620 /*
621 * Since the config file parsing code aborts if it sees
622 * options it doesn't recognize, allow users to put
623 * options specific to compile-time add-ons in alternate
624 * config files so their primary config file will
625 * interoperate SSH versions that don't support those
626 * options.
627 */
628#ifdef GSSAPI
629 snprintf(buf, sizeof buf, "%.100s/%.100s.gssapi", pw->pw_dir,
630 _PATH_SSH_USER_CONFFILE);
7e82606e 631 (void)read_config_file(buf, host, &options, 1);
c6046bb0 632#ifdef GSI
633 snprintf(buf, sizeof buf, "%.100s/%.100s.gsi", pw->pw_dir,
634 _PATH_SSH_USER_CONFFILE);
7e82606e 635 (void)read_config_file(buf, host, &options, 1);
c6046bb0 636#endif
7cac2b65 637#if defined(KRB5)
c6046bb0 638 snprintf(buf, sizeof buf, "%.100s/%.100s.krb", pw->pw_dir,
639 _PATH_SSH_USER_CONFFILE);
7e82606e 640 (void)read_config_file(buf, host, &options, 1);
c6046bb0 641#endif
c6046bb0 642#endif
3c0ef626 643 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
644 _PATH_SSH_USER_CONFFILE);
7e82606e 645 (void)read_config_file(buf, host, &options, 1);
3c0ef626 646
647 /* Read systemwide configuration file after use config. */
7e82606e 648 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
649 &options, 0);
3c0ef626 650 }
651
652 /* Fill configuration defaults. */
653 fill_default_options(&options);
654
7cac2b65 655 channel_set_af(options.address_family);
656
3c0ef626 657 /* reinit */
5262cbfb 658 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
3c0ef626 659
660 seed_rng();
661
7cac2b65 662 if (options.user == NULL) {
663 options.user = xstrdup(pw->pw_name);
664 options.implicit = 1;
665 }
73a68d83 666 else options.implicit = 0;
3c0ef626 667
5156b1a1 668 /* Get default port if port has not been set. */
669 if (options.port == 0) {
670 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
671 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
672 }
673
674 if (options.local_command != NULL) {
675 char thishost[NI_MAXHOST];
676
677 if (gethostname(thishost, sizeof(thishost)) == -1)
678 fatal("gethostname: %s", strerror(errno));
679 snprintf(buf, sizeof(buf), "%d", options.port);
680 debug3("expanding LocalCommand: %s", options.local_command);
681 cp = options.local_command;
682 options.local_command = percent_expand(cp, "d", pw->pw_dir,
683 "h", options.hostname? options.hostname : host,
684 "l", thishost, "n", host, "r", options.user, "p", buf,
685 "u", pw->pw_name, (char *)NULL);
686 debug3("expanded LocalCommand: %s", options.local_command);
687 xfree(cp);
688 }
689
3c0ef626 690 if (options.hostname != NULL)
691 host = options.hostname;
692
7cac2b65 693 /* force lowercase for hostkey matching */
694 if (options.host_key_alias != NULL) {
695 for (p = options.host_key_alias; *p; p++)
696 if (isupper(*p))
30460aeb 697 *p = (char)tolower(*p);
7cac2b65 698 }
699
bfe49944 700 if (options.proxy_command != NULL &&
e74dc197 701 strcmp(options.proxy_command, "none") == 0) {
702 xfree(options.proxy_command);
bfe49944 703 options.proxy_command = NULL;
e74dc197 704 }
2ce0bfe4 705 if (options.control_path != NULL &&
e74dc197 706 strcmp(options.control_path, "none") == 0) {
707 xfree(options.control_path);
2ce0bfe4 708 options.control_path = NULL;
e74dc197 709 }
bfe49944 710
7e82606e 711 if (options.control_path != NULL) {
30460aeb 712 char thishost[NI_MAXHOST];
713
714 if (gethostname(thishost, sizeof(thishost)) == -1)
715 fatal("gethostname: %s", strerror(errno));
2ce0bfe4 716 snprintf(buf, sizeof(buf), "%d", options.port);
717 cp = tilde_expand_filename(options.control_path,
718 original_real_uid);
e74dc197 719 xfree(options.control_path);
2ce0bfe4 720 options.control_path = percent_expand(cp, "p", buf, "h", host,
30460aeb 721 "r", options.user, "l", thishost, (char *)NULL);
2ce0bfe4 722 xfree(cp);
7e82606e 723 }
5156b1a1 724 if (muxclient_command != 0 && options.control_path == NULL)
2ce0bfe4 725 fatal("No ControlPath specified for \"-O\" command");
726 if (options.control_path != NULL)
5156b1a1 727 muxclient(options.control_path);
7e82606e 728
e74dc197 729 timeout_ms = options.connection_timeout * 1000;
730
3c0ef626 731 /* Open a connection to the remote host. */
7cac2b65 732 if (ssh_connect(host, &hostaddr, options.port,
e74dc197 733 options.address_family, options.connection_attempts, &timeout_ms,
734 options.tcp_keep_alive,
44a053a3 735#ifdef HAVE_CYGWIN
736 options.use_privileged_port,
737#else
738 original_effective_uid == 0 && options.use_privileged_port,
739#endif
740 options.proxy_command) != 0)
08822d99 741 exit(255);
3c0ef626 742
e74dc197 743 if (timeout_ms > 0)
744 debug3("timeout: %d ms remain after connect", timeout_ms);
745
3c0ef626 746 /*
747 * If we successfully made the connection, load the host private key
748 * in case we will need it later for combined rsa-rhosts
749 * authentication. This must be done before releasing extra
750 * privileges, because the file is only readable by root.
44a053a3 751 * If we cannot access the private keys, load the public keys
752 * instead and try to execute the ssh-keysign helper instead.
3c0ef626 753 */
754 sensitive_data.nkeys = 0;
755 sensitive_data.keys = NULL;
44a053a3 756 sensitive_data.external_keysign = 0;
757 if (options.rhosts_rsa_authentication ||
758 options.hostbased_authentication) {
3c0ef626 759 sensitive_data.nkeys = 3;
30460aeb 760 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
d03f4262 761 sizeof(Key));
44a053a3 762
763 PRIV_START;
3c0ef626 764 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
30460aeb 765 _PATH_HOST_KEY_FILE, "", NULL, NULL);
3c0ef626 766 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
30460aeb 767 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
3c0ef626 768 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
30460aeb 769 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
44a053a3 770 PRIV_END;
771
d03f4262 772 if (options.hostbased_authentication == 1 &&
773 sensitive_data.keys[0] == NULL &&
44a053a3 774 sensitive_data.keys[1] == NULL &&
775 sensitive_data.keys[2] == NULL) {
776 sensitive_data.keys[1] = key_load_public(
777 _PATH_HOST_DSA_KEY_FILE, NULL);
778 sensitive_data.keys[2] = key_load_public(
779 _PATH_HOST_RSA_KEY_FILE, NULL);
780 sensitive_data.external_keysign = 1;
781 }
3c0ef626 782 }
783 /*
784 * Get rid of any extra privileges that we may have. We will no
785 * longer need them. Also, extra privileges could make it very hard
786 * to read identity files and other non-world-readable files from the
787 * user's home directory if it happens to be on a NFS volume where
788 * root is mapped to nobody.
789 */
7e82606e 790 if (original_effective_uid == 0) {
791 PRIV_START;
792 permanently_set_uid(pw);
793 }
3c0ef626 794
795 /*
796 * Now that we are back to our own permissions, create ~/.ssh
08822d99 797 * directory if it doesn't already exist.
3c0ef626 798 */
5156b1a1 799 snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir,
800 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
3c0ef626 801 if (stat(buf, &st) < 0)
802 if (mkdir(buf, 0700) < 0)
803 error("Could not create directory '%.200s'.", buf);
804
3c0ef626 805 /* load options.identity_files */
806 load_public_identity_files();
807
808 /* Expand ~ in known host file names. */
809 /* XXX mem-leaks: */
810 options.system_hostfile =
811 tilde_expand_filename(options.system_hostfile, original_real_uid);
812 options.user_hostfile =
813 tilde_expand_filename(options.user_hostfile, original_real_uid);
814 options.system_hostfile2 =
815 tilde_expand_filename(options.system_hostfile2, original_real_uid);
816 options.user_hostfile2 =
817 tilde_expand_filename(options.user_hostfile2, original_real_uid);
818
819 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
820
5156b1a1 821 /* Log into the remote system. Never returns if the login fails. */
e74dc197 822 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
823 pw, timeout_ms);
3c0ef626 824
825 /* We no longer need the private host keys. Clear them now. */
826 if (sensitive_data.nkeys != 0) {
827 for (i = 0; i < sensitive_data.nkeys; i++) {
828 if (sensitive_data.keys[i] != NULL) {
829 /* Destroys contents safely */
830 debug3("clear hostkey %d", i);
831 key_free(sensitive_data.keys[i]);
832 sensitive_data.keys[i] = NULL;
833 }
834 }
835 xfree(sensitive_data.keys);
836 }
837 for (i = 0; i < options.num_identity_files; i++) {
838 if (options.identity_files[i]) {
839 xfree(options.identity_files[i]);
840 options.identity_files[i] = NULL;
841 }
842 if (options.identity_keys[i]) {
843 key_free(options.identity_keys[i]);
844 options.identity_keys[i] = NULL;
845 }
846 }
847
848 exit_status = compat20 ? ssh_session2() : ssh_session();
849 packet_close();
d03f4262 850
5156b1a1 851 if (options.control_path != NULL && muxserver_sock != -1)
7e82606e 852 unlink(options.control_path);
853
d03f4262 854 /*
540d72c3 855 * Send SIGHUP to proxy command if used. We don't wait() in
d03f4262 856 * case it hangs and instead rely on init to reap the child
857 */
858 if (proxy_command_pid > 1)
859 kill(proxy_command_pid, SIGHUP);
860
3c0ef626 861 return exit_status;
862}
863
5156b1a1 864/* Callback for remote forward global requests */
865static void
866ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
867{
868 Forward *rfwd = (Forward *)ctxt;
869
5262cbfb 870 /* XXX verbose() on failure? */
5156b1a1 871 debug("remote forward %s for: listen %d, connect %s:%d",
872 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
873 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
5262cbfb 874 if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) {
875 logit("Allocated port %u for remote forward to %s:%d",
876 packet_get_int(),
877 rfwd->connect_host, rfwd->connect_port);
878 }
879
5156b1a1 880 if (type == SSH2_MSG_REQUEST_FAILURE) {
881 if (options.exit_on_forward_failure)
882 fatal("Error: remote port forwarding failed for "
883 "listen port %d", rfwd->listen_port);
884 else
885 logit("Warning: remote port forwarding failed for "
886 "listen port %d", rfwd->listen_port);
887 }
888 if (++remote_forward_confirms_received == options.num_remote_forwards) {
889 debug("All remote forwarding requests processed");
890 if (fork_after_authentication_flag) {
891 fork_after_authentication_flag = 0;
892 if (daemon(1, 1) < 0)
893 fatal("daemon() failed: %.200s",
894 strerror(errno));
895 }
896 }
897}
898
3c0ef626 899static void
900ssh_init_forwarding(void)
901{
902 int success = 0;
903 int i;
904
905 /* Initiate local TCP/IP port forwardings. */
906 for (i = 0; i < options.num_local_forwards; i++) {
dfddba3d 907 debug("Local connections to %.200s:%d forwarded to remote "
908 "address %.200s:%d",
8b32eddc 909 (options.local_forwards[i].listen_host == NULL) ?
910 (options.gateway_ports ? "*" : "LOCALHOST") :
dfddba3d 911 options.local_forwards[i].listen_host,
912 options.local_forwards[i].listen_port,
913 options.local_forwards[i].connect_host,
914 options.local_forwards[i].connect_port);
c6046bb0 915 success += channel_setup_local_fwd_listener(
dfddba3d 916 options.local_forwards[i].listen_host,
917 options.local_forwards[i].listen_port,
918 options.local_forwards[i].connect_host,
919 options.local_forwards[i].connect_port,
d3057ca4 920 options.gateway_ports);
3c0ef626 921 }
30460aeb 922 if (i > 0 && success != i && options.exit_on_forward_failure)
923 fatal("Could not request local forwarding.");
3c0ef626 924 if (i > 0 && success == 0)
925 error("Could not request local forwarding.");
926
927 /* Initiate remote TCP/IP port forwardings. */
928 for (i = 0; i < options.num_remote_forwards; i++) {
dfddba3d 929 debug("Remote connections from %.200s:%d forwarded to "
930 "local address %.200s:%d",
2ce0bfe4 931 (options.remote_forwards[i].listen_host == NULL) ?
08822d99 932 "LOCALHOST" : options.remote_forwards[i].listen_host,
dfddba3d 933 options.remote_forwards[i].listen_port,
934 options.remote_forwards[i].connect_host,
935 options.remote_forwards[i].connect_port);
30460aeb 936 if (channel_request_remote_forwarding(
dfddba3d 937 options.remote_forwards[i].listen_host,
938 options.remote_forwards[i].listen_port,
939 options.remote_forwards[i].connect_host,
30460aeb 940 options.remote_forwards[i].connect_port) < 0) {
941 if (options.exit_on_forward_failure)
942 fatal("Could not request remote forwarding.");
943 else
944 logit("Warning: Could not request remote "
945 "forwarding.");
946 }
5156b1a1 947 client_register_global_confirm(ssh_confirm_remote_forward,
948 &options.remote_forwards[i]);
3c0ef626 949 }
fa0f0f45 950
951 /* Initiate tunnel forwarding. */
952 if (options.tun_open != SSH_TUNMODE_NO) {
953 if (client_request_tun_fwd(options.tun_open,
954 options.tun_local, options.tun_remote) == -1) {
955 if (options.exit_on_forward_failure)
956 fatal("Could not request tunnel forwarding.");
957 else
958 error("Could not request tunnel forwarding.");
959 }
960 }
3c0ef626 961}
962
963static void
964check_agent_present(void)
965{
966 if (options.forward_agent) {
08822d99 967 /* Clear agent forwarding if we don't have an agent. */
d03f4262 968 if (!ssh_agent_present())
3c0ef626 969 options.forward_agent = 0;
3c0ef626 970 }
971}
972
973static int
974ssh_session(void)
975{
976 int type;
3c0ef626 977 int interactive = 0;
978 int have_tty = 0;
979 struct winsize ws;
980 char *cp;
2ce0bfe4 981 const char *display;
3c0ef626 982
983 /* Enable compression if requested. */
984 if (options.compression) {
5156b1a1 985 debug("Requesting compression at level %d.",
986 options.compression_level);
3c0ef626 987
5156b1a1 988 if (options.compression_level < 1 ||
989 options.compression_level > 9)
990 fatal("Compression level must be from 1 (fast) to "
991 "9 (slow, best).");
3c0ef626 992
993 /* Send the request. */
994 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
995 packet_put_int(options.compression_level);
996 packet_send();
997 packet_write_wait();
c6046bb0 998 type = packet_read();
3c0ef626 999 if (type == SSH_SMSG_SUCCESS)
1000 packet_start_compression(options.compression_level);
1001 else if (type == SSH_SMSG_FAILURE)
7cac2b65 1002 logit("Warning: Remote host refused compression.");
3c0ef626 1003 else
5156b1a1 1004 packet_disconnect("Protocol error waiting for "
1005 "compression response.");
3c0ef626 1006 }
1007 /* Allocate a pseudo tty if appropriate. */
1008 if (tty_flag) {
1009 debug("Requesting pty.");
1010
1011 /* Start the packet. */
1012 packet_start(SSH_CMSG_REQUEST_PTY);
1013
1014 /* Store TERM in the packet. There is no limit on the
1015 length of the string. */
1016 cp = getenv("TERM");
1017 if (!cp)
1018 cp = "";
1019 packet_put_cstring(cp);
1020
1021 /* Store window size in the packet. */
1022 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1023 memset(&ws, 0, sizeof(ws));
30460aeb 1024 packet_put_int((u_int)ws.ws_row);
1025 packet_put_int((u_int)ws.ws_col);
1026 packet_put_int((u_int)ws.ws_xpixel);
1027 packet_put_int((u_int)ws.ws_ypixel);
3c0ef626 1028
1029 /* Store tty modes in the packet. */
1030 tty_make_modes(fileno(stdin), NULL);
1031
1032 /* Send the packet, and wait for it to leave. */
1033 packet_send();
1034 packet_write_wait();
1035
1036 /* Read response from the server. */
c6046bb0 1037 type = packet_read();
3c0ef626 1038 if (type == SSH_SMSG_SUCCESS) {
1039 interactive = 1;
1040 have_tty = 1;
1041 } else if (type == SSH_SMSG_FAILURE)
5156b1a1 1042 logit("Warning: Remote host failed or refused to "
1043 "allocate a pseudo tty.");
3c0ef626 1044 else
5156b1a1 1045 packet_disconnect("Protocol error waiting for pty "
1046 "request response.");
3c0ef626 1047 }
1048 /* Request X11 forwarding if enabled and DISPLAY is set. */
2ce0bfe4 1049 display = getenv("DISPLAY");
1050 if (options.forward_x11 && display != NULL) {
c6046bb0 1051 char *proto, *data;
3c0ef626 1052 /* Get reasonable local authentication information. */
2ce0bfe4 1053 client_x11_get_proto(display, options.xauth_location,
1054 options.forward_x11_trusted, &proto, &data);
3c0ef626 1055 /* Request forwarding with authentication spoofing. */
5156b1a1 1056 debug("Requesting X11 forwarding with authentication "
1057 "spoofing.");
2ce0bfe4 1058 x11_request_forwarding_with_spoofing(0, display, proto, data);
3c0ef626 1059
1060 /* Read response from the server. */
c6046bb0 1061 type = packet_read();
3c0ef626 1062 if (type == SSH_SMSG_SUCCESS) {
1063 interactive = 1;
1064 } else if (type == SSH_SMSG_FAILURE) {
7cac2b65 1065 logit("Warning: Remote host denied X11 forwarding.");
3c0ef626 1066 } else {
5156b1a1 1067 packet_disconnect("Protocol error waiting for X11 "
1068 "forwarding");
3c0ef626 1069 }
1070 }
1071 /* Tell the packet module whether this is an interactive session. */
1072 packet_set_interactive(interactive);
1073
1074 /* Request authentication agent forwarding if appropriate. */
1075 check_agent_present();
1076
1077 if (options.forward_agent) {
1078 debug("Requesting authentication agent forwarding.");
1079 auth_request_forwarding();
1080
1081 /* Read response from the server. */
c6046bb0 1082 type = packet_read();
1083 packet_check_eom();
3c0ef626 1084 if (type != SSH_SMSG_SUCCESS)
7cac2b65 1085 logit("Warning: Remote host denied authentication agent forwarding.");
3c0ef626 1086 }
1087
1088 /* Initiate port forwardings. */
1089 ssh_init_forwarding();
1090
e74dc197 1091 /* Execute a local command */
1092 if (options.local_command != NULL &&
1093 options.permit_local_command)
1094 ssh_local_cmd(options.local_command);
1095
5156b1a1 1096 /*
1097 * If requested and we are not interested in replies to remote
1098 * forwarding requests, then let ssh continue in the background.
1099 */
1100 if (fork_after_authentication_flag &&
1101 (!options.exit_on_forward_failure ||
1102 options.num_remote_forwards == 0)) {
1103 fork_after_authentication_flag = 0;
3c0ef626 1104 if (daemon(1, 1) < 0)
1105 fatal("daemon() failed: %.200s", strerror(errno));
5156b1a1 1106 }
3c0ef626 1107
1108 /*
1109 * If a command was specified on the command line, execute the
1110 * command now. Otherwise request the server to start a shell.
1111 */
1112 if (buffer_len(&command) > 0) {
1113 int len = buffer_len(&command);
1114 if (len > 900)
1115 len = 900;
5156b1a1 1116 debug("Sending command: %.*s", len,
1117 (u_char *)buffer_ptr(&command));
3c0ef626 1118 packet_start(SSH_CMSG_EXEC_CMD);
1119 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1120 packet_send();
1121 packet_write_wait();
1122 } else {
1123 debug("Requesting shell.");
1124 packet_start(SSH_CMSG_EXEC_SHELL);
1125 packet_send();
1126 packet_write_wait();
1127 }
1128
1129 /* Enter the interactive session. */
1130 return client_loop(have_tty, tty_flag ?
1131 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1132}
1133
7e82606e 1134/* request pty/x11/agent/tcpfwd/shell for channel */
1135static void
1136ssh_session2_setup(int id, void *arg)
1137{
1138 extern char **environ;
2ce0bfe4 1139 const char *display;
7e82606e 1140 int interactive = tty_flag;
2ce0bfe4 1141
1142 display = getenv("DISPLAY");
1143 if (options.forward_x11 && display != NULL) {
c6046bb0 1144 char *proto, *data;
3c0ef626 1145 /* Get reasonable local authentication information. */
2ce0bfe4 1146 client_x11_get_proto(display, options.xauth_location,
1147 options.forward_x11_trusted, &proto, &data);
3c0ef626 1148 /* Request forwarding with authentication spoofing. */
5156b1a1 1149 debug("Requesting X11 forwarding with authentication "
1150 "spoofing.");
2ce0bfe4 1151 x11_request_forwarding_with_spoofing(id, display, proto, data);
3c0ef626 1152 interactive = 1;
1153 /* XXX wait for reply */
1154 }
1155
1156 check_agent_present();
1157 if (options.forward_agent) {
1158 debug("Requesting authentication agent forwarding.");
1159 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1160 packet_send();
1161 }
1162
7e82606e 1163 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
5156b1a1 1164 NULL, fileno(stdin), &command, environ);
3c0ef626 1165
3c0ef626 1166 packet_set_interactive(interactive);
1167}
1168
1169/* open new channel for a session */
1170static int
1171ssh_session2_open(void)
1172{
1173 Channel *c;
1174 int window, packetmax, in, out, err;
f713db99 1175 int sock;
1176 int socksize;
1177 int socksizelen = sizeof(int);
3c0ef626 1178
1179 if (stdin_null_flag) {
1180 in = open(_PATH_DEVNULL, O_RDONLY);
1181 } else {
1182 in = dup(STDIN_FILENO);
1183 }
1184 out = dup(STDOUT_FILENO);
1185 err = dup(STDERR_FILENO);
1186
1187 if (in < 0 || out < 0 || err < 0)
1188 fatal("dup() in/out/err failed");
1189
1190 /* enable nonblocking unless tty */
1191 if (!isatty(in))
1192 set_nonblock(in);
1193 if (!isatty(out))
1194 set_nonblock(out);
1195 if (!isatty(err))
1196 set_nonblock(err);
1197
f713db99 1198 /* we need to check to see if what they want to do about buffer */
1199 /* sizes here. In a hpn to nonhpn connection we want to limit */
1200 /* the window size to something reasonable in case the far side */
1201 /* has the large window bug. In hpn to hpn connection we want to */
1202 /* use the max window size but allow the user to override it */
1203 /* lastly if they disabled hpn then use the ssh std window size */
1204
1205 /* so why don't we just do a getsockopt() here and set the */
1206 /* ssh window to that? In the case of a autotuning receive */
1207 /* window the window would get stuck at the initial buffer */
1208 /* size generally less than 96k. Therefore we need to set the */
1209 /* maximum ssh window size to the maximum hpn buffer size */
6df46d40 1210 /* unless the user has specifically set the tcprcvbufpoll */
f713db99 1211 /* to no. In which case we *can* just set the window to the */
1212 /* minimum of the hpn buffer size and tcp receive buffer size */
1213
d3057ca4 1214 if (tty_flag)
f713db99 1215 options.hpn_buffer_size = CHAN_SES_WINDOW_DEFAULT;
d3057ca4 1216 else
1217 options.hpn_buffer_size = 2*1024*1024;
1218
1219 if (datafellows & SSH_BUG_LARGEWINDOW)
f713db99 1220 {
1221 debug("HPN to Non-HPN Connection");
f713db99 1222 }
1223 else
1224 {
f713db99 1225 if (options.tcp_rcv_buf_poll <= 0)
1226 {
d3057ca4 1227 sock = socket(AF_INET, SOCK_STREAM, 0);
1228 getsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1229 &socksize, &socksizelen);
1230 close(sock);
1231 debug("socksize %d", socksize);
1232 options.hpn_buffer_size = socksize;
1233 debug ("HPNBufferSize set to TCP RWIN: %d", options.hpn_buffer_size);
f713db99 1234 }
1235 else
1236 {
1237 if (options.tcp_rcv_buf > 0)
d3057ca4 1238 {
1239 /*create a socket but don't connect it */
1240 /* we use that the get the rcv socket size */
1241 sock = socket(AF_INET, SOCK_STREAM, 0);
1242 /* if they are using the tcp_rcv_buf option */
1243 /* attempt to set the buffer size to that */
1244 if (options.tcp_rcv_buf)
1245 setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&options.tcp_rcv_buf,
1246 sizeof(options.tcp_rcv_buf));
1247 getsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1248 &socksize, &socksizelen);
1249 close(sock);
1250 debug("socksize %d", socksize);
1251 options.hpn_buffer_size = socksize;
1252 debug ("HPNBufferSize set to user TCPRcvBuf: %d", options.hpn_buffer_size);
1253 }
f713db99 1254 }
1255
1256 }
1257
1258 debug("Final hpn_buffer_size = %d", options.hpn_buffer_size);
1259
1260 window = options.hpn_buffer_size;
1261
d3057ca4 1262 channel_set_hpn(options.hpn_disabled, options.hpn_buffer_size);
1263
3c0ef626 1264 packetmax = CHAN_SES_PACKET_DEFAULT;
c6046bb0 1265 if (tty_flag) {
473db5ab 1266 window = 4*CHAN_SES_PACKET_DEFAULT;
c6046bb0 1267 window >>= 1;
1268 packetmax >>= 1;
3c0ef626 1269 }
1270 c = channel_new(
1271 "session", SSH_CHANNEL_OPENING, in, out, err,
1272 window, packetmax, CHAN_EXTENDED_WRITE,
7cac2b65 1273 "client-session", /*nonblock*/0);
a7213e65 1274 if ((options.tcp_rcv_buf_poll > 0) && (!options.hpn_disabled)) {
473db5ab 1275 c->dynamic_window = 1;
a7213e65 1276 debug ("Enabled Dynamic Window Scaling\n");
473db5ab 1277 }
3c0ef626 1278 debug3("ssh_session2_open: channel_new: %d", c->self);
1279
1280 channel_send_open(c->self);
1281 if (!no_shell_flag)
5156b1a1 1282 channel_register_open_confirm(c->self,
1283 ssh_session2_setup, NULL);
3c0ef626 1284
1285 return c->self;
1286}
1287
1288static int
1289ssh_session2(void)
1290{
1291 int id = -1;
1292
1293 /* XXX should be pre-session */
1294 ssh_init_forwarding();
1295
1296 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1297 id = ssh_session2_open();
1298
5156b1a1 1299 /* If we don't expect to open a new session, then disallow it */
5262cbfb 1300 if (options.control_master == SSHCTL_MASTER_NO &&
1301 (datafellows & SSH_NEW_OPENSSH)) {
5156b1a1 1302 debug("Requesting no-more-sessions@openssh.com");
1303 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1304 packet_put_cstring("no-more-sessions@openssh.com");
1305 packet_put_char(0);
1306 packet_send();
1307 }
1308
08822d99 1309 /* Execute a local command */
1310 if (options.local_command != NULL &&
1311 options.permit_local_command)
1312 ssh_local_cmd(options.local_command);
1313
fa0f0f45 1314 /* Start listening for multiplex clients */
5156b1a1 1315 muxserver_listen();
fa0f0f45 1316
3c0ef626 1317 /* If requested, let ssh continue in the background. */
5156b1a1 1318 if (fork_after_authentication_flag) {
1319 fork_after_authentication_flag = 0;
3c0ef626 1320 if (daemon(1, 1) < 0)
1321 fatal("daemon() failed: %.200s", strerror(errno));
5156b1a1 1322 }
3c0ef626 1323
1324 return client_loop(tty_flag, tty_flag ?
1325 options.escape_char : SSH_ESCAPECHAR_NONE, id);
1326}
1327
1328static void
1329load_public_identity_files(void)
1330{
30460aeb 1331 char *filename, *cp, thishost[NI_MAXHOST];
e74dc197 1332 char *pwdir = NULL, *pwname = NULL;
3c0ef626 1333 int i = 0;
c6046bb0 1334 Key *public;
30460aeb 1335 struct passwd *pw;
3c0ef626 1336#ifdef SMARTCARD
c6046bb0 1337 Key **keys;
1338
3c0ef626 1339 if (options.smartcard_device != NULL &&
c6046bb0 1340 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
30460aeb 1341 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
c6046bb0 1342 int count = 0;
1343 for (i = 0; keys[i] != NULL; i++) {
1344 count++;
5156b1a1 1345 memmove(&options.identity_files[1],
1346 &options.identity_files[0],
c6046bb0 1347 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
5156b1a1 1348 memmove(&options.identity_keys[1],
1349 &options.identity_keys[0],
c6046bb0 1350 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1351 options.num_identity_files++;
1352 options.identity_keys[0] = keys[i];
7cac2b65 1353 options.identity_files[0] = sc_get_key_label(keys[i]);
c6046bb0 1354 }
1355 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1356 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1357 i = count;
1358 xfree(keys);
3c0ef626 1359 }
1360#endif /* SMARTCARD */
30460aeb 1361 if ((pw = getpwuid(original_real_uid)) == NULL)
1362 fatal("load_public_identity_files: getpwuid failed");
e74dc197 1363 pwname = xstrdup(pw->pw_name);
1364 pwdir = xstrdup(pw->pw_dir);
30460aeb 1365 if (gethostname(thishost, sizeof(thishost)) == -1)
1366 fatal("load_public_identity_files: gethostname: %s",
1367 strerror(errno));
3c0ef626 1368 for (; i < options.num_identity_files; i++) {
30460aeb 1369 cp = tilde_expand_filename(options.identity_files[i],
3c0ef626 1370 original_real_uid);
e74dc197 1371 filename = percent_expand(cp, "d", pwdir,
1372 "u", pwname, "l", thishost, "h", host,
30460aeb 1373 "r", options.user, (char *)NULL);
1374 xfree(cp);
3c0ef626 1375 public = key_load_public(filename, NULL);
1376 debug("identity file %s type %d", filename,
1377 public ? public->type : -1);
1378 xfree(options.identity_files[i]);
1379 options.identity_files[i] = filename;
1380 options.identity_keys[i] = public;
1381 }
e74dc197 1382 bzero(pwname, strlen(pwname));
1383 xfree(pwname);
1384 bzero(pwdir, strlen(pwdir));
1385 xfree(pwdir);
3c0ef626 1386}
This page took 0.414927 seconds and 5 git commands to generate.