]> andersk Git - openssh.git/blame - ssh.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / ssh.c
CommitLineData
a858eae9 1/* $OpenBSD: ssh.c,v 1.332 2010/01/26 01:28:35 djm Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 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 *
bcbf86ec 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.
d73a67d7 17 * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
bcbf86ec 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.
5260325f 41 */
8efc0c15 42
43#include "includes.h"
876faccd 44
4095f623 45#include <sys/types.h>
46#ifdef HAVE_SYS_STAT_H
47# include <sys/stat.h>
48#endif
876faccd 49#include <sys/resource.h>
bd7c69ea 50#include <sys/ioctl.h>
0f63ab10 51#include <sys/param.h>
5b04a8bf 52#include <sys/socket.h>
a75f5360 53
b6438382 54#include <ctype.h>
dd984467 55#include <errno.h>
d3221cca 56#include <fcntl.h>
28cb0a43 57#include <netdb.h>
b5b88c19 58#ifdef HAVE_PATHS_H
a75f5360 59#include <paths.h>
b5b88c19 60#endif
b1842393 61#include <pwd.h>
ada68823 62#include <signal.h>
24436b92 63#include <stdarg.h>
bcaab305 64#include <stddef.h>
cf851879 65#include <stdio.h>
ffa517a8 66#include <stdlib.h>
00146caa 67#include <string.h>
5188ba17 68#include <unistd.h>
8efc0c15 69
5e837c7b 70#include <netinet/in.h>
71#include <arpa/inet.h>
72
a306f2dd 73#include <openssl/evp.h>
fa08c86b 74#include <openssl/err.h>
f5f25d17 75#include "openbsd-compat/openssl-compat.h"
3593bdc0 76#include "openbsd-compat/sys-queue.h"
a306f2dd 77
31652869 78#include "xmalloc.h"
8efc0c15 79#include "ssh.h"
42f11eb2 80#include "ssh1.h"
81#include "ssh2.h"
82#include "compat.h"
83#include "cipher.h"
8efc0c15 84#include "packet.h"
85#include "buffer.h"
a5efa1bb 86#include "channels.h"
a306f2dd 87#include "key.h"
4c8722d9 88#include "authfd.h"
a306f2dd 89#include "authfile.h"
42f11eb2 90#include "pathnames.h"
5e96b616 91#include "dispatch.h"
b5c334cc 92#include "clientloop.h"
42f11eb2 93#include "log.h"
94#include "readconf.h"
95#include "sshconnect.h"
42f11eb2 96#include "misc.h"
b2552997 97#include "kex.h"
98#include "mac.h"
07d80252 99#include "sshpty.h"
61a2c1da 100#include "match.h"
5e96b616 101#include "msg.h"
6213295d 102#include "uidswap.h"
1cb94277 103#include "roaming.h"
28015df4 104#include "version.h"
8efc0c15 105
383546c4 106#ifdef SMARTCARD
383546c4 107#include "scard.h"
dd2495cb 108#endif
383546c4 109
f601d847 110extern char *__progname;
f601d847 111
e8097dc9 112/* Flag indicating whether debug mode is on. May be set on the command line. */
8efc0c15 113int debug_flag = 0;
114
a8be9f80 115/* Flag indicating whether a tty should be allocated */
8efc0c15 116int tty_flag = 0;
8abcdba4 117int no_tty_flag = 0;
118int force_tty_flag = 0;
8efc0c15 119
8ce64345 120/* don't exec a shell */
121int no_shell_flag = 0;
8ce64345 122
aa3378df 123/*
124 * Flag indicating that nothing should be read from stdin. This can be set
125 * on the command line.
126 */
8efc0c15 127int stdin_null_flag = 0;
128
aa3378df 129/*
130 * Flag indicating that ssh should fork after authentication. This is useful
c242fc96 131 * so that the passphrase can be entered manually, and then ssh goes to the
aa3378df 132 * background.
133 */
8efc0c15 134int fork_after_authentication_flag = 0;
135
e6780883 136/* forward stdio to remote host and port */
137char *stdio_forward_host = NULL;
138int stdio_forward_port = 0;
139
aa3378df 140/*
141 * General data structure for command line options and options configurable
142 * in configuration files. See readconf.h.
143 */
8efc0c15 144Options options;
145
e591b98a 146/* optional user configfile */
147char *config = NULL;
148
aa3378df 149/*
150 * Name of the host we are connecting to. This is the name given on the
151 * command line, or the HostName specified for the user-supplied name in a
152 * configuration file.
153 */
8efc0c15 154char *host;
155
156/* socket address the host resolves to */
48e671d5 157struct sockaddr_storage hostaddr;
8efc0c15 158
8002af61 159/* Private host keys. */
39c00dc2 160Sensitive sensitive_data;
8efc0c15 161
162/* Original real UID. */
163uid_t original_real_uid;
3fb156df 164uid_t original_effective_uid;
8efc0c15 165
8ce64345 166/* command to be executed */
167Buffer command;
168
ae810de7 169/* Should we execute a command or invoke a subsystem? */
170int subsystem_flag = 0;
171
e0ae8728 172/* # of replies received for global requests */
72becb62 173static int remote_forward_confirms_received = 0;
e0ae8728 174
6939bbd4 175/* pid of proxycommand child process */
176pid_t proxy_command_pid = 0;
177
3bcced4c 178/* mux.c */
179extern int muxserver_sock;
180extern u_int muxclient_command;
5e96b616 181
8efc0c15 182/* Prints a help message to the user. This function never returns. */
183
396c147e 184static void
5ca51e19 185usage(void)
8efc0c15 186{
182ccbba 187 fprintf(stderr,
f3a4d0d0 188"usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n"
80e29ee6 189" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
8cf98c65 190" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
3867aa0a 191" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
8cf98c65 192" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
e6780883 193" [-W host:port] [-w local_tun[:remote_tun]]\n"
194" [user@]hostname [command]\n"
182ccbba 195 );
3aa43b24 196 exit(255);
8efc0c15 197}
198
396c147e 199static int ssh_session(void);
200static int ssh_session2(void);
201static void load_public_identity_files(void);
3bcced4c 202
203/* from muxclient.c */
204void muxclient(const char *);
205void muxserver_listen(void);
8ce64345 206
5260325f 207/*
208 * Main program for the ssh client.
209 */
8efc0c15 210int
211main(int ac, char **av)
212{
0f63ab10 213 int i, r, opt, exit_status, use_syslog;
214 char *p, *cp, *line, *argv0, buf[MAXPATHLEN];
5260325f 215 struct stat st;
3b1a83df 216 struct passwd *pw;
71300a43 217 int dummy, timeout_ms;
57dade33 218 extern int optind, optreset;
1812a662 219 extern char *optarg;
c1cbe68a 220 struct servent *sp;
3867aa0a 221 Forward fwd;
5260325f 222
fd6168c1 223 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
224 sanitise_stdfd();
225
fda04d7d 226 __progname = ssh_get_progname(av[0]);
264dce47 227 init_rng();
228
aa3378df 229 /*
230 * Save the original real uid. It will be needed later (uid-swapping
231 * may clobber the real uid).
232 */
5260325f 233 original_real_uid = getuid();
234 original_effective_uid = geteuid();
aff51935 235
9f324470 236 /*
237 * Use uid-swapping to give up root privileges for the duration of
238 * option processing. We will re-instantiate the rights when we are
239 * ready to create the privileged port, and will permanently drop
240 * them when the port has been created (actually, when the connection
241 * has been made, as we may need to create the port several times).
242 */
243 PRIV_END;
5260325f 244
7322ef0e 245#ifdef HAVE_SETRLIMIT
5260325f 246 /* If we are installed setuid root be careful to not drop core. */
247 if (original_real_uid != original_effective_uid) {
248 struct rlimit rlim;
249 rlim.rlim_cur = rlim.rlim_max = 0;
250 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
251 fatal("setrlimit failed: %.100s", strerror(errno));
8efc0c15 252 }
3c62e7eb 253#endif
63bd8c36 254 /* Get user data. */
255 pw = getpwuid(original_real_uid);
256 if (!pw) {
bbe88b6d 257 logit("You don't exist, go away!");
3aa43b24 258 exit(255);
63bd8c36 259 }
260 /* Take a copy of the returned structure. */
261 pw = pwcopy(pw);
262
aa3378df 263 /*
264 * Set our umask to something reasonable, as some files are created
265 * with the default umask. This will make them world-readable but
266 * writable only by the owner, which is ok for all files for which we
267 * don't set the modes explicitly.
268 */
5260325f 269 umask(022);
270
e8097dc9 271 /*
272 * Initialize option structure to indicate that no values have been
273 * set.
274 */
5260325f 275 initialize_options(&options);
276
277 /* Parse command-line arguments. */
278 host = NULL;
bf793210 279 use_syslog = 0;
f0956980 280 argv0 = av[0];
5260325f 281
16a5525d 282 again:
e8097dc9 283 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
e6780883 284 "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) {
5260325f 285 switch (opt) {
1f3bf5aa 286 case '1':
287 options.protocol = SSH_PROTO_1;
288 break;
6ae2364d 289 case '2':
290 options.protocol = SSH_PROTO_2;
291 break;
48e671d5 292 case '4':
1572b90f 293 options.address_family = AF_INET;
48e671d5 294 break;
48e671d5 295 case '6':
1572b90f 296 options.address_family = AF_INET6;
48e671d5 297 break;
5260325f 298 case 'n':
299 stdin_null_flag = 1;
300 break;
5260325f 301 case 'f':
302 fork_after_authentication_flag = 1;
303 stdin_null_flag = 1;
304 break;
5260325f 305 case 'x':
306 options.forward_x11 = 0;
307 break;
5260325f 308 case 'X':
309 options.forward_x11 = 1;
310 break;
bf793210 311 case 'y':
312 use_syslog = 1;
313 break;
d73a67d7 314 case 'Y':
315 options.forward_x11 = 1;
316 options.forward_x11_trusted = 1;
317 break;
5260325f 318 case 'g':
319 options.gateway_ports = 1;
320 break;
f8c6db83 321 case 'O':
a858eae9 322 if (stdio_forward_host != NULL)
323 fatal("Cannot specify multiplexing "
324 "command with -W");
325 else if (muxclient_command != 0)
326 fatal("Multiplexing command already specified");
f8c6db83 327 if (strcmp(optarg, "check") == 0)
3bcced4c 328 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
f8c6db83 329 else if (strcmp(optarg, "exit") == 0)
3bcced4c 330 muxclient_command = SSHMUX_COMMAND_TERMINATE;
f8c6db83 331 else
332 fatal("Invalid multiplex command.");
333 break;
e59404d1 334 case 'P': /* deprecated */
5260325f 335 options.use_privileged_port = 0;
336 break;
5260325f 337 case 'a':
338 options.forward_agent = 0;
339 break;
71276795 340 case 'A':
341 options.forward_agent = 1;
342 break;
5260325f 343 case 'k':
f7926e97 344 options.gss_deleg_creds = 0;
5260325f 345 break;
b2f4d5cc 346 case 'K':
347 options.gss_authentication = 1;
348 options.gss_deleg_creds = 1;
349 break;
5260325f 350 case 'i':
351 if (stat(optarg, &st) < 0) {
f5a1a01a 352 fprintf(stderr, "Warning: Identity file %s "
ec304d66 353 "not accessible: %s.\n", optarg,
354 strerror(errno));
5260325f 355 break;
356 }
f5a1a01a 357 if (options.num_identity_files >=
358 SSH_MAX_IDENTITY_FILES)
359 fatal("Too many identity files specified "
360 "(max %d)", SSH_MAX_IDENTITY_FILES);
361 options.identity_files[options.num_identity_files++] =
362 xstrdup(optarg);
5260325f 363 break;
383546c4 364 case 'I':
365#ifdef SMARTCARD
9ff6f66f 366 options.smartcard_device = xstrdup(optarg);
dd2495cb 367#else
383546c4 368 fprintf(stderr, "no support for smartcards.\n");
dd2495cb 369#endif
383546c4 370 break;
5260325f 371 case 't':
8abcdba4 372 if (tty_flag)
373 force_tty_flag = 1;
5260325f 374 tty_flag = 1;
375 break;
5260325f 376 case 'v':
e053cd2c 377 if (debug_flag == 0) {
bcbf86ec 378 debug_flag = 1;
379 options.log_level = SYSLOG_LEVEL_DEBUG1;
e053cd2c 380 } else {
381 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
382 options.log_level++;
bcbf86ec 383 break;
e053cd2c 384 }
59d4718a 385 /* FALLTHROUGH */
5260325f 386 case 'V':
85ac7a84 387 fprintf(stderr, "%s, %s\n",
4526e8c2 388 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
5260325f 389 if (opt == 'V')
390 exit(0);
5260325f 391 break;
d20f3c9e 392 case 'w':
a4f24bf8 393 if (options.tun_open == -1)
394 options.tun_open = SSH_TUNMODE_DEFAULT;
d20f3c9e 395 options.tun_local = a2tun(optarg, &options.tun_remote);
a4f24bf8 396 if (options.tun_local == SSH_TUNID_ERR) {
e8097dc9 397 fprintf(stderr,
398 "Bad tun device '%s'\n", optarg);
3aa43b24 399 exit(255);
d20f3c9e 400 }
401 break;
e6780883 402 case 'W':
a858eae9 403 if (stdio_forward_host != NULL)
404 fatal("stdio forward already specified");
405 if (muxclient_command != 0)
406 fatal("Cannot specify stdio forward with -O");
e6780883 407 if (parse_forward(&fwd, optarg, 1, 0)) {
408 stdio_forward_host = fwd.listen_host;
409 stdio_forward_port = fwd.listen_port;
410 xfree(fwd.connect_host);
411 } else {
412 fprintf(stderr,
413 "Bad stdio forwarding specification '%s'\n",
414 optarg);
415 exit(255);
416 }
417 no_tty_flag = 1;
418 no_shell_flag = 1;
419 options.clear_forwardings = 1;
420 options.exit_on_forward_failure = 1;
421 break;
5260325f 422 case 'q':
423 options.log_level = SYSLOG_LEVEL_QUIET;
424 break;
5260325f 425 case 'e':
426 if (optarg[0] == '^' && optarg[2] == 0 &&
f5a1a01a 427 (u_char) optarg[1] >= 64 &&
428 (u_char) optarg[1] < 128)
1e3b8b07 429 options.escape_char = (u_char) optarg[1] & 31;
5260325f 430 else if (strlen(optarg) == 1)
1e3b8b07 431 options.escape_char = (u_char) optarg[0];
5260325f 432 else if (strcmp(optarg, "none") == 0)
4bf9c10e 433 options.escape_char = SSH_ESCAPECHAR_NONE;
5260325f 434 else {
f5a1a01a 435 fprintf(stderr, "Bad escape character '%s'.\n",
436 optarg);
3aa43b24 437 exit(255);
5260325f 438 }
439 break;
5260325f 440 case 'c':
a306f2dd 441 if (ciphers_valid(optarg)) {
442 /* SSH2 only */
443 options.ciphers = xstrdup(optarg);
d77347cc 444 options.cipher = SSH_CIPHER_INVALID;
a306f2dd 445 } else {
446 /* SSH1 only */
c523303b 447 options.cipher = cipher_number(optarg);
448 if (options.cipher == -1) {
f5a1a01a 449 fprintf(stderr,
450 "Unknown cipher type '%s'\n",
451 optarg);
3aa43b24 452 exit(255);
a306f2dd 453 }
f5a1a01a 454 if (options.cipher == SSH_CIPHER_3DES)
c523303b 455 options.ciphers = "3des-cbc";
f5a1a01a 456 else if (options.cipher == SSH_CIPHER_BLOWFISH)
c523303b 457 options.ciphers = "blowfish-cbc";
f5a1a01a 458 else
c523303b 459 options.ciphers = (char *)-1;
5260325f 460 }
461 break;
b2552997 462 case 'm':
463 if (mac_valid(optarg))
464 options.macs = xstrdup(optarg);
465 else {
f5a1a01a 466 fprintf(stderr, "Unknown mac type '%s'\n",
467 optarg);
3aa43b24 468 exit(255);
b2552997 469 }
470 break;
5e96b616 471 case 'M':
9dfd96d6 472 if (options.control_master == SSHCTL_MASTER_YES)
473 options.control_master = SSHCTL_MASTER_ASK;
474 else
475 options.control_master = SSHCTL_MASTER_YES;
5e96b616 476 break;
5260325f 477 case 'p':
2d2a2c65 478 options.port = a2port(optarg);
5134115d 479 if (options.port <= 0) {
6031af8d 480 fprintf(stderr, "Bad port '%s'\n", optarg);
3aa43b24 481 exit(255);
6031af8d 482 }
5260325f 483 break;
5260325f 484 case 'l':
485 options.user = optarg;
486 break;
d2e3df16 487
488 case 'L':
17525a70 489 if (parse_forward(&fwd, optarg, 0, 0))
3867aa0a 490 add_local_forward(&options, &fwd);
491 else {
f5a1a01a 492 fprintf(stderr,
3867aa0a 493 "Bad local forwarding specification '%s'\n",
f5a1a01a 494 optarg);
3aa43b24 495 exit(255);
5260325f 496 }
3867aa0a 497 break;
498
499 case 'R':
17525a70 500 if (parse_forward(&fwd, optarg, 0, 1)) {
3867aa0a 501 add_remote_forward(&options, &fwd);
502 } else {
f5a1a01a 503 fprintf(stderr,
3867aa0a 504 "Bad remote forwarding specification "
505 "'%s'\n", optarg);
3aa43b24 506 exit(255);
5260325f 507 }
5260325f 508 break;
0490e609 509
510 case 'D':
17525a70 511 if (parse_forward(&fwd, optarg, 1, 0)) {
30573fea 512 add_local_forward(&options, &fwd);
3867aa0a 513 } else {
30573fea 514 fprintf(stderr,
515 "Bad dynamic forwarding specification "
516 "'%s'\n", optarg);
3aa43b24 517 exit(255);
6031af8d 518 }
0490e609 519 break;
520
5260325f 521 case 'C':
522 options.compression = 1;
523 break;
8ce64345 524 case 'N':
525 no_shell_flag = 1;
526 no_tty_flag = 1;
527 break;
8ce64345 528 case 'T':
529 no_tty_flag = 1;
530 break;
5260325f 531 case 'o':
532 dummy = 1;
1aafd17a 533 line = xstrdup(optarg);
f5a1a01a 534 if (process_config_line(&options, host ? host : "",
1aafd17a 535 line, "command-line", 0, &dummy) != 0)
3aa43b24 536 exit(255);
1aafd17a 537 xfree(line);
5260325f 538 break;
ae810de7 539 case 's':
540 subsystem_flag = 1;
541 break;
5e96b616 542 case 'S':
543 if (options.control_path != NULL)
544 free(options.control_path);
545 options.control_path = xstrdup(optarg);
5e96b616 546 break;
3435f5a6 547 case 'b':
548 options.bind_address = optarg;
549 break;
e591b98a 550 case 'F':
551 config = optarg;
552 break;
5260325f 553 default:
554 usage();
555 }
556 }
557
f5a1a01a 558 ac -= optind;
559 av += optind;
560
c3773c6e 561 if (ac > 0 && !host) {
15748b4d 562 if (strrchr(*av, '@')) {
f5a1a01a 563 p = xstrdup(*av);
15748b4d 564 cp = strrchr(p, '@');
f5a1a01a 565 if (cp == NULL || cp == p)
566 usage();
567 options.user = p;
568 *cp = '\0';
569 host = ++cp;
570 } else
571 host = *av;
978ebf99 572 if (ac > 1) {
573 optind = optreset = 1;
f5a1a01a 574 goto again;
575 }
978ebf99 576 ac--, av++;
f5a1a01a 577 }
578
5260325f 579 /* Check that we got a host name. */
580 if (!host)
8efc0c15 581 usage();
5260325f 582
22d89d24 583 SSLeay_add_all_algorithms();
fa08c86b 584 ERR_load_crypto_strings();
22d89d24 585
5260325f 586 /* Initialize the command to execute on remote host. */
587 buffer_init(&command);
588
aa3378df 589 /*
590 * Save the command to execute on the remote host in a buffer. There
591 * is no limit on the length of the command, except by the maximum
592 * packet size. Also sets the tty flag if there is no command.
593 */
f5a1a01a 594 if (!ac) {
5260325f 595 /* No command specified - execute shell on a tty. */
596 tty_flag = 1;
ae810de7 597 if (subsystem_flag) {
f5a1a01a 598 fprintf(stderr,
599 "You must specify a subsystem to invoke.\n");
ae810de7 600 usage();
601 }
5260325f 602 } else {
f5a1a01a 603 /* A command has been specified. Store it into the buffer. */
604 for (i = 0; i < ac; i++) {
605 if (i)
5260325f 606 buffer_append(&command, " ", 1);
607 buffer_append(&command, av[i], strlen(av[i]));
608 }
8efc0c15 609 }
5260325f 610
611 /* Cannot fork to background if no command. */
e8097dc9 612 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
613 !no_shell_flag)
614 fatal("Cannot fork into background without a command "
615 "to execute.");
5260325f 616
617 /* Allocate a tty by default if no command specified. */
618 if (buffer_len(&command) == 0)
619 tty_flag = 1;
620
343288b8 621 /* Force no tty */
0b6fbf03 622 if (no_tty_flag)
623 tty_flag = 0;
5260325f 624 /* Do not allocate a tty if stdin is not a tty. */
7697ac2b 625 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
5260325f 626 if (tty_flag)
e8097dc9 627 logit("Pseudo-terminal will not be allocated because "
628 "stdin is not a terminal.");
5260325f 629 tty_flag = 0;
630 }
8ce64345 631
20244695 632 /*
633 * Initialize "log" output. Since we are the client all output
634 * actually goes to stderr.
635 */
f0956980 636 log_init(argv0,
e8097dc9 637 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
bf793210 638 SYSLOG_FACILITY_USER, !use_syslog);
5260325f 639
e591b98a 640 /*
641 * Read per-user configuration file. Ignore the system wide config
642 * file if the user specifies a config file on the command line.
643 */
644 if (config != NULL) {
f7f14143 645 if (!read_config_file(config, host, &options, 0))
93111dfa 646 fatal("Can't open user config file %.100s: "
647 "%.100s", config, strerror(errno));
4f6e2ba9 648 } else {
0f63ab10 649 r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir,
e591b98a 650 _PATH_SSH_USER_CONFFILE);
0f63ab10 651 if (r > 0 && (size_t)r < sizeof(buf))
652 (void)read_config_file(buf, host, &options, 1);
e591b98a 653
f67792f2 654 /* Read systemwide configuration file after use config. */
f2107e97 655 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
9f6cab4b 656 &options, 0);
e591b98a 657 }
5260325f 658
659 /* Fill configuration defaults. */
660 fill_default_options(&options);
661
1572b90f 662 channel_set_af(options.address_family);
663
5260325f 664 /* reinit */
f0956980 665 log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
5260325f 666
e339aa53 667 seed_rng();
668
5260325f 669 if (options.user == NULL)
670 options.user = xstrdup(pw->pw_name);
671
e9d0b573 672 /* Get default port if port has not been set. */
673 if (options.port == 0) {
674 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
675 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
676 }
677
678 if (options.local_command != NULL) {
679 char thishost[NI_MAXHOST];
680
681 if (gethostname(thishost, sizeof(thishost)) == -1)
682 fatal("gethostname: %s", strerror(errno));
683 snprintf(buf, sizeof(buf), "%d", options.port);
684 debug3("expanding LocalCommand: %s", options.local_command);
685 cp = options.local_command;
686 options.local_command = percent_expand(cp, "d", pw->pw_dir,
687 "h", options.hostname? options.hostname : host,
688 "l", thishost, "n", host, "r", options.user, "p", buf,
689 "u", pw->pw_name, (char *)NULL);
690 debug3("expanded LocalCommand: %s", options.local_command);
691 xfree(cp);
692 }
693
5260325f 694 if (options.hostname != NULL)
695 host = options.hostname;
696
03c82656 697 /* force lowercase for hostkey matching */
698 if (options.host_key_alias != NULL) {
699 for (p = options.host_key_alias; *p; p++)
700 if (isupper(*p))
9555d258 701 *p = (char)tolower(*p);
03c82656 702 }
703
f78bde70 704 if (options.proxy_command != NULL &&
d0420e01 705 strcmp(options.proxy_command, "none") == 0) {
706 xfree(options.proxy_command);
f78bde70 707 options.proxy_command = NULL;
d0420e01 708 }
699255b5 709 if (options.control_path != NULL &&
d0420e01 710 strcmp(options.control_path, "none") == 0) {
711 xfree(options.control_path);
699255b5 712 options.control_path = NULL;
d0420e01 713 }
f78bde70 714
5e96b616 715 if (options.control_path != NULL) {
8cffe22a 716 char thishost[NI_MAXHOST];
f7b8224d 717
8cffe22a 718 if (gethostname(thishost, sizeof(thishost)) == -1)
f7b8224d 719 fatal("gethostname: %s", strerror(errno));
a980cbd7 720 snprintf(buf, sizeof(buf), "%d", options.port);
721 cp = tilde_expand_filename(options.control_path,
722 original_real_uid);
d0420e01 723 xfree(options.control_path);
a980cbd7 724 options.control_path = percent_expand(cp, "p", buf, "h", host,
8cffe22a 725 "r", options.user, "l", thishost, (char *)NULL);
a980cbd7 726 xfree(cp);
5e96b616 727 }
3bcced4c 728 if (muxclient_command != 0 && options.control_path == NULL)
19186c3d 729 fatal("No ControlPath specified for \"-O\" command");
9dfd96d6 730 if (options.control_path != NULL)
3bcced4c 731 muxclient(options.control_path);
5e96b616 732
71300a43 733 timeout_ms = options.connection_timeout * 1000;
734
6ffc9c88 735 /* Open a connection to the remote host. */
1572b90f 736 if (ssh_connect(host, &hostaddr, options.port,
71300a43 737 options.address_family, options.connection_attempts, &timeout_ms,
738 options.tcp_keep_alive,
d514c907 739#ifdef HAVE_CYGWIN
740 options.use_privileged_port,
741#else
3fb156df 742 original_effective_uid == 0 && options.use_privileged_port,
d514c907 743#endif
de60473e 744 options.proxy_command) != 0)
3aa43b24 745 exit(255);
5260325f 746
71300a43 747 if (timeout_ms > 0)
748 debug3("timeout: %d ms remain after connect", timeout_ms);
749
aa3378df 750 /*
751 * If we successfully made the connection, load the host private key
752 * in case we will need it later for combined rsa-rhosts
753 * authentication. This must be done before releasing extra
754 * privileges, because the file is only readable by root.
78660ed4 755 * If we cannot access the private keys, load the public keys
756 * instead and try to execute the ssh-keysign helper instead.
aa3378df 757 */
8002af61 758 sensitive_data.nkeys = 0;
759 sensitive_data.keys = NULL;
39c00dc2 760 sensitive_data.external_keysign = 0;
b34bec32 761 if (options.rhosts_rsa_authentication ||
762 options.hostbased_authentication) {
8002af61 763 sensitive_data.nkeys = 3;
19e79961 764 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
343288b8 765 sizeof(Key));
3fb156df 766
767 PRIV_START;
8002af61 768 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
fc231518 769 _PATH_HOST_KEY_FILE, "", NULL, NULL);
8002af61 770 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
fc231518 771 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
8002af61 772 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
fc231518 773 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
3fb156df 774 PRIV_END;
39c00dc2 775
60cd0a97 776 if (options.hostbased_authentication == 1 &&
777 sensitive_data.keys[0] == NULL &&
39c00dc2 778 sensitive_data.keys[1] == NULL &&
779 sensitive_data.keys[2] == NULL) {
780 sensitive_data.keys[1] = key_load_public(
781 _PATH_HOST_DSA_KEY_FILE, NULL);
782 sensitive_data.keys[2] = key_load_public(
783 _PATH_HOST_RSA_KEY_FILE, NULL);
784 sensitive_data.external_keysign = 1;
785 }
5260325f 786 }
aa3378df 787 /*
788 * Get rid of any extra privileges that we may have. We will no
789 * longer need them. Also, extra privileges could make it very hard
790 * to read identity files and other non-world-readable files from the
791 * user's home directory if it happens to be on a NFS volume where
792 * root is mapped to nobody.
793 */
6213295d 794 if (original_effective_uid == 0) {
795 PRIV_START;
796 permanently_set_uid(pw);
797 }
5260325f 798
aa3378df 799 /*
800 * Now that we are back to our own permissions, create ~/.ssh
7b9b0103 801 * directory if it doesn't already exist.
aa3378df 802 */
0f63ab10 803 r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
e8097dc9 804 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
0f63ab10 805 if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0)
704b1659 806 if (mkdir(buf, 0700) < 0)
5260325f 807 error("Could not create directory '%.200s'.", buf);
808
fee56204 809 /* load options.identity_files */
810 load_public_identity_files();
811
812 /* Expand ~ in known host file names. */
813 /* XXX mem-leaks: */
fa08c86b 814 options.system_hostfile =
815 tilde_expand_filename(options.system_hostfile, original_real_uid);
816 options.user_hostfile =
817 tilde_expand_filename(options.user_hostfile, original_real_uid);
818 options.system_hostfile2 =
819 tilde_expand_filename(options.system_hostfile2, original_real_uid);
820 options.user_hostfile2 =
821 tilde_expand_filename(options.user_hostfile2, original_real_uid);
5260325f 822
67b75437 823 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
824
e8097dc9 825 /* Log into the remote system. Never returns if the login fails. */
71300a43 826 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
827 pw, timeout_ms);
8002af61 828
829 /* We no longer need the private host keys. Clear them now. */
830 if (sensitive_data.nkeys != 0) {
831 for (i = 0; i < sensitive_data.nkeys; i++) {
832 if (sensitive_data.keys[i] != NULL) {
833 /* Destroys contents safely */
834 debug3("clear hostkey %d", i);
835 key_free(sensitive_data.keys[i]);
836 sensitive_data.keys[i] = NULL;
837 }
838 }
839 xfree(sensitive_data.keys);
840 }
05b7537a 841 for (i = 0; i < options.num_identity_files; i++) {
842 if (options.identity_files[i]) {
843 xfree(options.identity_files[i]);
844 options.identity_files[i] = NULL;
845 }
846 if (options.identity_keys[i]) {
847 key_free(options.identity_keys[i]);
848 options.identity_keys[i] = NULL;
849 }
850 }
5260325f 851
8ce64345 852 exit_status = compat20 ? ssh_session2() : ssh_session();
853 packet_close();
6939bbd4 854
3bcced4c 855 if (options.control_path != NULL && muxserver_sock != -1)
5e96b616 856 unlink(options.control_path);
857
6939bbd4 858 /*
aff51935 859 * Send SIGHUP to proxy command if used. We don't wait() in
6939bbd4 860 * case it hangs and instead rely on init to reap the child
861 */
862 if (proxy_command_pid > 1)
863 kill(proxy_command_pid, SIGHUP);
864
8ce64345 865 return exit_status;
866}
867
72becb62 868/* Callback for remote forward global requests */
869static void
870ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
871{
872 Forward *rfwd = (Forward *)ctxt;
873
17525a70 874 /* XXX verbose() on failure? */
72becb62 875 debug("remote forward %s for: listen %d, connect %s:%d",
876 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
877 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
17525a70 878 if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) {
879 logit("Allocated port %u for remote forward to %s:%d",
880 packet_get_int(),
881 rfwd->connect_host, rfwd->connect_port);
882 }
883
72becb62 884 if (type == SSH2_MSG_REQUEST_FAILURE) {
885 if (options.exit_on_forward_failure)
886 fatal("Error: remote port forwarding failed for "
887 "listen port %d", rfwd->listen_port);
888 else
889 logit("Warning: remote port forwarding failed for "
890 "listen port %d", rfwd->listen_port);
891 }
6c777090 892 if (++remote_forward_confirms_received == options.num_remote_forwards) {
72becb62 893 debug("All remote forwarding requests processed");
6c777090 894 if (fork_after_authentication_flag) {
895 fork_after_authentication_flag = 0;
896 if (daemon(1, 1) < 0)
897 fatal("daemon() failed: %.200s",
898 strerror(errno));
899 }
900 }
72becb62 901}
902
e6780883 903static void
904client_cleanup_stdio_fwd(int id, void *arg)
905{
906 debug("stdio forwarding: done");
907 cleanup_exit(0);
908}
909
910static int
911client_setup_stdio_fwd(const char *host_to_connect, u_short port_to_connect)
912{
913 Channel *c;
a858eae9 914 int in, out;
e6780883 915
916 debug3("client_setup_stdio_fwd %s:%d", host_to_connect,
917 port_to_connect);
a858eae9 918
919 in = dup(STDIN_FILENO);
920 out = dup(STDOUT_FILENO);
921 if (in < 0 || out < 0)
922 fatal("channel_connect_stdio_fwd: dup() in/out failed");
923
924 if ((c = channel_connect_stdio_fwd(host_to_connect, port_to_connect,
925 in, out)) == NULL)
e6780883 926 return 0;
927 channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0);
928 return 1;
929}
930
396c147e 931static void
fa08c86b 932ssh_init_forwarding(void)
933{
02a024dd 934 int success = 0;
fa08c86b 935 int i;
02a024dd 936
e6780883 937 if (stdio_forward_host != NULL) {
938 if (!compat20) {
939 fatal("stdio forwarding require Protocol 2");
940 }
941 if (!client_setup_stdio_fwd(stdio_forward_host,
942 stdio_forward_port))
943 fatal("Failed to connect in stdio forward mode.");
944 }
945
fa08c86b 946 /* Initiate local TCP/IP port forwardings. */
947 for (i = 0; i < options.num_local_forwards; i++) {
3867aa0a 948 debug("Local connections to %.200s:%d forwarded to remote "
949 "address %.200s:%d",
f8cc7664 950 (options.local_forwards[i].listen_host == NULL) ?
951 (options.gateway_ports ? "*" : "LOCALHOST") :
3867aa0a 952 options.local_forwards[i].listen_host,
953 options.local_forwards[i].listen_port,
954 options.local_forwards[i].connect_host,
955 options.local_forwards[i].connect_port);
5641aefa 956 success += channel_setup_local_fwd_listener(
3867aa0a 957 options.local_forwards[i].listen_host,
958 options.local_forwards[i].listen_port,
959 options.local_forwards[i].connect_host,
960 options.local_forwards[i].connect_port,
fa08c86b 961 options.gateway_ports);
962 }
42ea6f5e 963 if (i > 0 && success != i && options.exit_on_forward_failure)
964 fatal("Could not request local forwarding.");
02a024dd 965 if (i > 0 && success == 0)
966 error("Could not request local forwarding.");
fa08c86b 967
968 /* Initiate remote TCP/IP port forwardings. */
969 for (i = 0; i < options.num_remote_forwards; i++) {
3867aa0a 970 debug("Remote connections from %.200s:%d forwarded to "
971 "local address %.200s:%d",
56964485 972 (options.remote_forwards[i].listen_host == NULL) ?
aa9bc1de 973 "LOCALHOST" : options.remote_forwards[i].listen_host,
3867aa0a 974 options.remote_forwards[i].listen_port,
975 options.remote_forwards[i].connect_host,
976 options.remote_forwards[i].connect_port);
42ea6f5e 977 if (channel_request_remote_forwarding(
3867aa0a 978 options.remote_forwards[i].listen_host,
979 options.remote_forwards[i].listen_port,
980 options.remote_forwards[i].connect_host,
42ea6f5e 981 options.remote_forwards[i].connect_port) < 0) {
982 if (options.exit_on_forward_failure)
983 fatal("Could not request remote forwarding.");
984 else
985 logit("Warning: Could not request remote "
986 "forwarding.");
987 }
72becb62 988 client_register_global_confirm(ssh_confirm_remote_forward,
989 &options.remote_forwards[i]);
fa08c86b 990 }
79303c5a 991
992 /* Initiate tunnel forwarding. */
993 if (options.tun_open != SSH_TUNMODE_NO) {
994 if (client_request_tun_fwd(options.tun_open,
995 options.tun_local, options.tun_remote) == -1) {
996 if (options.exit_on_forward_failure)
997 fatal("Could not request tunnel forwarding.");
998 else
999 error("Could not request tunnel forwarding.");
1000 }
1001 }
fa08c86b 1002}
1003
396c147e 1004static void
fa08c86b 1005check_agent_present(void)
1006{
1007 if (options.forward_agent) {
7b9b0103 1008 /* Clear agent forwarding if we don't have an agent. */
8b10e20e 1009 if (!ssh_agent_present())
fa08c86b 1010 options.forward_agent = 0;
fa08c86b 1011 }
1012}
1013
396c147e 1014static int
8ce64345 1015ssh_session(void)
1016{
1017 int type;
8ce64345 1018 int interactive = 0;
1019 int have_tty = 0;
1020 struct winsize ws;
8ce64345 1021 char *cp;
9bf083eb 1022 const char *display;
8ce64345 1023
5260325f 1024 /* Enable compression if requested. */
1025 if (options.compression) {
e8097dc9 1026 debug("Requesting compression at level %d.",
1027 options.compression_level);
5260325f 1028
e8097dc9 1029 if (options.compression_level < 1 ||
1030 options.compression_level > 9)
1031 fatal("Compression level must be from 1 (fast) to "
1032 "9 (slow, best).");
5260325f 1033
1034 /* Send the request. */
1035 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
1036 packet_put_int(options.compression_level);
1037 packet_send();
1038 packet_write_wait();
54a5250f 1039 type = packet_read();
5260325f 1040 if (type == SSH_SMSG_SUCCESS)
1041 packet_start_compression(options.compression_level);
1042 else if (type == SSH_SMSG_FAILURE)
bbe88b6d 1043 logit("Warning: Remote host refused compression.");
5260325f 1044 else
e8097dc9 1045 packet_disconnect("Protocol error waiting for "
1046 "compression response.");
5260325f 1047 }
1048 /* Allocate a pseudo tty if appropriate. */
1049 if (tty_flag) {
1050 debug("Requesting pty.");
1051
1052 /* Start the packet. */
1053 packet_start(SSH_CMSG_REQUEST_PTY);
1054
1055 /* Store TERM in the packet. There is no limit on the
1056 length of the string. */
1057 cp = getenv("TERM");
1058 if (!cp)
1059 cp = "";
449c5ba5 1060 packet_put_cstring(cp);
5260325f 1061
1062 /* Store window size in the packet. */
1063 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1064 memset(&ws, 0, sizeof(ws));
148de80c 1065 packet_put_int((u_int)ws.ws_row);
1066 packet_put_int((u_int)ws.ws_col);
1067 packet_put_int((u_int)ws.ws_xpixel);
1068 packet_put_int((u_int)ws.ws_ypixel);
5260325f 1069
1070 /* Store tty modes in the packet. */
30dcc918 1071 tty_make_modes(fileno(stdin), NULL);
5260325f 1072
1073 /* Send the packet, and wait for it to leave. */
1074 packet_send();
1075 packet_write_wait();
1076
1077 /* Read response from the server. */
54a5250f 1078 type = packet_read();
4fe2af09 1079 if (type == SSH_SMSG_SUCCESS) {
5260325f 1080 interactive = 1;
8ce64345 1081 have_tty = 1;
4fe2af09 1082 } else if (type == SSH_SMSG_FAILURE)
e8097dc9 1083 logit("Warning: Remote host failed or refused to "
1084 "allocate a pseudo tty.");
5260325f 1085 else
e8097dc9 1086 packet_disconnect("Protocol error waiting for pty "
1087 "request response.");
5260325f 1088 }
1089 /* Request X11 forwarding if enabled and DISPLAY is set. */
9bf083eb 1090 display = getenv("DISPLAY");
1091 if (options.forward_x11 && display != NULL) {
b48eeb07 1092 char *proto, *data;
0b242b12 1093 /* Get reasonable local authentication information. */
9bf083eb 1094 client_x11_get_proto(display, options.xauth_location,
1095 options.forward_x11_trusted, &proto, &data);
0b242b12 1096 /* Request forwarding with authentication spoofing. */
e8097dc9 1097 debug("Requesting X11 forwarding with authentication "
1098 "spoofing.");
9bf083eb 1099 x11_request_forwarding_with_spoofing(0, display, proto, data);
5260325f 1100
1101 /* Read response from the server. */
54a5250f 1102 type = packet_read();
5260325f 1103 if (type == SSH_SMSG_SUCCESS) {
5260325f 1104 interactive = 1;
0b242b12 1105 } else if (type == SSH_SMSG_FAILURE) {
bbe88b6d 1106 logit("Warning: Remote host denied X11 forwarding.");
0b242b12 1107 } else {
e8097dc9 1108 packet_disconnect("Protocol error waiting for X11 "
1109 "forwarding");
0b242b12 1110 }
5260325f 1111 }
1112 /* Tell the packet module whether this is an interactive session. */
b5c334cc 1113 packet_set_interactive(interactive);
5260325f 1114
1115 /* Request authentication agent forwarding if appropriate. */
fa08c86b 1116 check_agent_present();
1117
5260325f 1118 if (options.forward_agent) {
1119 debug("Requesting authentication agent forwarding.");
1120 auth_request_forwarding();
1121
1122 /* Read response from the server. */
54a5250f 1123 type = packet_read();
95500969 1124 packet_check_eom();
5260325f 1125 if (type != SSH_SMSG_SUCCESS)
bbe88b6d 1126 logit("Warning: Remote host denied authentication agent forwarding.");
5260325f 1127 }
8efc0c15 1128
fa08c86b 1129 /* Initiate port forwardings. */
1130 ssh_init_forwarding();
5260325f 1131
c80e7e5d 1132 /* Execute a local command */
1133 if (options.local_command != NULL &&
1134 options.permit_local_command)
1135 ssh_local_cmd(options.local_command);
1136
6c777090 1137 /*
1138 * If requested and we are not interested in replies to remote
1139 * forwarding requests, then let ssh continue in the background.
1140 */
1141 if (fork_after_authentication_flag &&
1142 (!options.exit_on_forward_failure ||
1143 options.num_remote_forwards == 0)) {
1144 fork_after_authentication_flag = 0;
aa3378df 1145 if (daemon(1, 1) < 0)
1146 fatal("daemon() failed: %.200s", strerror(errno));
6c777090 1147 }
aa3378df 1148
1149 /*
1150 * If a command was specified on the command line, execute the
1151 * command now. Otherwise request the server to start a shell.
1152 */
5260325f 1153 if (buffer_len(&command) > 0) {
1154 int len = buffer_len(&command);
1155 if (len > 900)
1156 len = 900;
e8097dc9 1157 debug("Sending command: %.*s", len,
1158 (u_char *)buffer_ptr(&command));
5260325f 1159 packet_start(SSH_CMSG_EXEC_CMD);
1160 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1161 packet_send();
1162 packet_write_wait();
1163 } else {
1164 debug("Requesting shell.");
1165 packet_start(SSH_CMSG_EXEC_SHELL);
1166 packet_send();
1167 packet_write_wait();
1168 }
1169
1170 /* Enter the interactive session. */
4bf9c10e 1171 return client_loop(have_tty, tty_flag ?
1172 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
8ce64345 1173}
5260325f 1174
5e96b616 1175/* request pty/x11/agent/tcpfwd/shell for channel */
1176static void
1177ssh_session2_setup(int id, void *arg)
1178{
1786be35 1179 extern char **environ;
9bf083eb 1180 const char *display;
5e96b616 1181 int interactive = tty_flag;
9bf083eb 1182
56964485 1183 display = getenv("DISPLAY");
9bf083eb 1184 if (options.forward_x11 && display != NULL) {
b48eeb07 1185 char *proto, *data;
0b242b12 1186 /* Get reasonable local authentication information. */
9bf083eb 1187 client_x11_get_proto(display, options.xauth_location,
1188 options.forward_x11_trusted, &proto, &data);
0b242b12 1189 /* Request forwarding with authentication spoofing. */
e8097dc9 1190 debug("Requesting X11 forwarding with authentication "
1191 "spoofing.");
9bf083eb 1192 x11_request_forwarding_with_spoofing(id, display, proto, data);
b5c334cc 1193 interactive = 1;
0b242b12 1194 /* XXX wait for reply */
1195 }
1196
fa08c86b 1197 check_agent_present();
1198 if (options.forward_agent) {
1199 debug("Requesting authentication agent forwarding.");
1200 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1201 packet_send();
1202 }
1203
5e96b616 1204 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
95d3c124 1205 NULL, fileno(stdin), &command, environ);
1f3bf5aa 1206
b5c334cc 1207 packet_set_interactive(interactive);
8ce64345 1208}
1209
bcdb96c2 1210/* open new channel for a session */
396c147e 1211static int
bcdb96c2 1212ssh_session2_open(void)
8ce64345 1213{
719fc62f 1214 Channel *c;
1215 int window, packetmax, in, out, err;
2e73a022 1216
14a9a859 1217 if (stdin_null_flag) {
5ca51e19 1218 in = open(_PATH_DEVNULL, O_RDONLY);
14a9a859 1219 } else {
1220 in = dup(STDIN_FILENO);
1221 }
2e73a022 1222 out = dup(STDOUT_FILENO);
1223 err = dup(STDERR_FILENO);
8ce64345 1224
1225 if (in < 0 || out < 0 || err < 0)
14a9a859 1226 fatal("dup() in/out/err failed");
8ce64345 1227
a22aff1f 1228 /* enable nonblocking unless tty */
1229 if (!isatty(in))
1230 set_nonblock(in);
1231 if (!isatty(out))
1232 set_nonblock(out);
1233 if (!isatty(err))
1234 set_nonblock(err);
1235
bcbf86ec 1236 window = CHAN_SES_WINDOW_DEFAULT;
1237 packetmax = CHAN_SES_PACKET_DEFAULT;
ea9700ba 1238 if (tty_flag) {
1239 window >>= 1;
1240 packetmax >>= 1;
8ce64345 1241 }
719fc62f 1242 c = channel_new(
8ce64345 1243 "session", SSH_CHANNEL_OPENING, in, out, err,
bcbf86ec 1244 window, packetmax, CHAN_EXTENDED_WRITE,
0d942eff 1245 "client-session", /*nonblock*/0);
8ce64345 1246
bcdb96c2 1247 debug3("ssh_session2_open: channel_new: %d", c->self);
eb0dd41f 1248
36e1f6a1 1249 channel_send_open(c->self);
bcdb96c2 1250 if (!no_shell_flag)
3593bdc0 1251 channel_register_open_confirm(c->self,
1252 ssh_session2_setup, NULL);
5260325f 1253
719fc62f 1254 return c->self;
eb0dd41f 1255}
1256
396c147e 1257static int
eb0dd41f 1258ssh_session2(void)
1259{
bcdb96c2 1260 int id = -1;
eb0dd41f 1261
1262 /* XXX should be pre-session */
1263 ssh_init_forwarding();
1264
bcdb96c2 1265 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1266 id = ssh_session2_open();
eb0dd41f 1267
3f0444ca 1268 /* If we don't expect to open a new session, then disallow it */
72bd2fca 1269 if (options.control_master == SSHCTL_MASTER_NO &&
1270 (datafellows & SSH_NEW_OPENSSH)) {
3f0444ca 1271 debug("Requesting no-more-sessions@openssh.com");
1272 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1273 packet_put_cstring("no-more-sessions@openssh.com");
1274 packet_put_char(0);
1275 packet_send();
1276 }
1277
d20f3c9e 1278 /* Execute a local command */
1279 if (options.local_command != NULL &&
1280 options.permit_local_command)
1281 ssh_local_cmd(options.local_command);
1282
79303c5a 1283 /* Start listening for multiplex clients */
3bcced4c 1284 muxserver_listen();
79303c5a 1285
eb0dd41f 1286 /* If requested, let ssh continue in the background. */
6c777090 1287 if (fork_after_authentication_flag) {
1288 fork_after_authentication_flag = 0;
eb0dd41f 1289 if (daemon(1, 1) < 0)
1290 fatal("daemon() failed: %.200s", strerror(errno));
6c777090 1291 }
eb0dd41f 1292
1cb94277 1293 if (options.use_roaming)
1294 request_roaming();
1295
4bf9c10e 1296 return client_loop(tty_flag, tty_flag ?
1297 options.escape_char : SSH_ESCAPECHAR_NONE, id);
8efc0c15 1298}
fa08c86b 1299
396c147e 1300static void
fee56204 1301load_public_identity_files(void)
1302{
8cffe22a 1303 char *filename, *cp, thishost[NI_MAXHOST];
a7082e17 1304 char *pwdir = NULL, *pwname = NULL;
2e23cde0 1305 int i = 0;
0659cace 1306 Key *public;
8cffe22a 1307 struct passwd *pw;
383546c4 1308#ifdef SMARTCARD
0659cace 1309 Key **keys;
1310
9ff6f66f 1311 if (options.smartcard_device != NULL &&
0659cace 1312 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
d4f40d92 1313 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
0659cace 1314 int count = 0;
1315 for (i = 0; keys[i] != NULL; i++) {
1316 count++;
e8097dc9 1317 memmove(&options.identity_files[1],
1318 &options.identity_files[0],
0659cace 1319 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
e8097dc9 1320 memmove(&options.identity_keys[1],
1321 &options.identity_keys[0],
0659cace 1322 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1323 options.num_identity_files++;
1324 options.identity_keys[0] = keys[i];
244e796f 1325 options.identity_files[0] = sc_get_key_label(keys[i]);
0659cace 1326 }
1c2deed1 1327 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1328 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
0659cace 1329 i = count;
1330 xfree(keys);
383546c4 1331 }
3e984472 1332#endif /* SMARTCARD */
8cffe22a 1333 if ((pw = getpwuid(original_real_uid)) == NULL)
1334 fatal("load_public_identity_files: getpwuid failed");
42f4b33f 1335 pwname = xstrdup(pw->pw_name);
1336 pwdir = xstrdup(pw->pw_dir);
8cffe22a 1337 if (gethostname(thishost, sizeof(thishost)) == -1)
1338 fatal("load_public_identity_files: gethostname: %s",
1339 strerror(errno));
2e23cde0 1340 for (; i < options.num_identity_files; i++) {
8cffe22a 1341 cp = tilde_expand_filename(options.identity_files[i],
2e23cde0 1342 original_real_uid);
a7082e17 1343 filename = percent_expand(cp, "d", pwdir,
1344 "u", pwname, "l", thishost, "h", host,
8cffe22a 1345 "r", options.user, (char *)NULL);
1346 xfree(cp);
2e23cde0 1347 public = key_load_public(filename, NULL);
1348 debug("identity file %s type %d", filename,
1349 public ? public->type : -1);
1350 xfree(options.identity_files[i]);
1351 options.identity_files[i] = filename;
1352 options.identity_keys[i] = public;
1353 }
a7082e17 1354 bzero(pwname, strlen(pwname));
42f4b33f 1355 xfree(pwname);
a7082e17 1356 bzero(pwdir, strlen(pwdir));
42f4b33f 1357 xfree(pwdir);
fee56204 1358}
This page took 0.5954 seconds and 5 git commands to generate.