]> andersk Git - gssapi-openssh.git/blame - openssh/ssh.c
http://www.psc.edu/networking/projects/hpn-ssh/openssh-5.1p1-hpn13v5.diff.gz committe...
[gssapi-openssh.git] / openssh / ssh.c
CommitLineData
22616013 1/* $OpenBSD: ssh.c,v 1.318 2008/07/02 13:47:39 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.
cdd66111 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"
9108f8d9 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>
9108f8d9 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>
47686178 74#include "openbsd-compat/openssl-compat.h"
22616013 75#include "openbsd-compat/sys-queue.h"
3c0ef626 76
9108f8d9 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"
c9f39d2c 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"
c9f39d2c 98#include "sshpty.h"
99#include "match.h"
100#include "msg.h"
c9f39d2c 101#include "uidswap.h"
9108f8d9 102#include "version.h"
3c0ef626 103
104#ifdef SMARTCARD
3c0ef626 105#include "scard.h"
106#endif
107
3c0ef626 108extern char *__progname;
3c0ef626 109
22616013 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
f5799ae1 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. */
f5799ae1 154Sensitive sensitive_data;
3c0ef626 155
156/* Original real UID. */
157uid_t original_real_uid;
f5799ae1 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
700318f3 166/* # of replies received for global requests */
22616013 167static int remote_forward_confirms_received = 0;
700318f3 168
41b2f314 169/* pid of proxycommand child process */
170pid_t proxy_command_pid = 0;
171
22616013 172/* mux.c */
173extern int muxserver_sock;
174extern u_int muxclient_command;
c9f39d2c 175
3c0ef626 176/* Prints a help message to the user. This function never returns. */
177
178static void
179usage(void)
180{
99be0775 181 fprintf(stderr,
d4487008 182"usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
2c06c99b 183" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
996d5e62 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"
9108f8d9 187" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
99be0775 188 );
2c06c99b 189 exit(255);
3c0ef626 190}
191
3c0ef626 192static int ssh_session(void);
193static int ssh_session2(void);
194static void load_public_identity_files(void);
22616013 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{
f5799ae1 206 int i, opt, exit_status;
cdd66111 207 char *p, *cp, *line, buf[256];
3c0ef626 208 struct stat st;
209 struct passwd *pw;
47686178 210 int dummy, timeout_ms;
3c0ef626 211 extern int optind, optreset;
212 extern char *optarg;
665a873d 213 struct servent *sp;
996d5e62 214 Forward fwd;
3c0ef626 215
2c06c99b 216 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
217 sanitise_stdfd();
218
0fff78ff 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();
cdd66111 228
41b2f314 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) {
0fff78ff 250 logit("You don't exist, go away!");
2c06c99b 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
22616013 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;
272
9108f8d9 273 again:
22616013 274 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
275 "ACD:F:I:KL:MNO:PR:S:TVw:XY")) != -1) {
3c0ef626 276 switch (opt) {
277 case '1':
278 options.protocol = SSH_PROTO_1;
279 break;
280 case '2':
281 options.protocol = SSH_PROTO_2;
282 break;
283 case '4':
0fff78ff 284 options.address_family = AF_INET;
3c0ef626 285 break;
286 case '6':
0fff78ff 287 options.address_family = AF_INET6;
3c0ef626 288 break;
289 case 'n':
290 stdin_null_flag = 1;
291 break;
292 case 'f':
293 fork_after_authentication_flag = 1;
294 stdin_null_flag = 1;
295 break;
296 case 'x':
297 options.forward_x11 = 0;
298 break;
299 case 'X':
300 options.forward_x11 = 1;
301 break;
cdd66111 302 case 'Y':
303 options.forward_x11 = 1;
304 options.forward_x11_trusted = 1;
305 break;
3c0ef626 306 case 'g':
307 options.gateway_ports = 1;
308 break;
996d5e62 309 case 'O':
310 if (strcmp(optarg, "check") == 0)
22616013 311 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
996d5e62 312 else if (strcmp(optarg, "exit") == 0)
22616013 313 muxclient_command = SSHMUX_COMMAND_TERMINATE;
996d5e62 314 else
315 fatal("Invalid multiplex command.");
316 break;
41b2f314 317 case 'P': /* deprecated */
3c0ef626 318 options.use_privileged_port = 0;
319 break;
320 case 'a':
321 options.forward_agent = 0;
322 break;
323 case 'A':
324 options.forward_agent = 1;
325 break;
3c0ef626 326 case 'k':
cdd66111 327 options.gss_deleg_creds = 0;
3c0ef626 328 break;
d4487008 329 case 'K':
330 options.gss_authentication = 1;
331 options.gss_deleg_creds = 1;
332 break;
3c0ef626 333 case 'i':
334 if (stat(optarg, &st) < 0) {
335 fprintf(stderr, "Warning: Identity file %s "
996d5e62 336 "not accessible: %s.\n", optarg,
337 strerror(errno));
3c0ef626 338 break;
339 }
340 if (options.num_identity_files >=
341 SSH_MAX_IDENTITY_FILES)
342 fatal("Too many identity files specified "
343 "(max %d)", SSH_MAX_IDENTITY_FILES);
344 options.identity_files[options.num_identity_files++] =
345 xstrdup(optarg);
346 break;
347 case 'I':
348#ifdef SMARTCARD
349 options.smartcard_device = xstrdup(optarg);
350#else
351 fprintf(stderr, "no support for smartcards.\n");
352#endif
353 break;
354 case 't':
355 if (tty_flag)
356 force_tty_flag = 1;
357 tty_flag = 1;
358 break;
359 case 'v':
0fff78ff 360 if (debug_flag == 0) {
3c0ef626 361 debug_flag = 1;
362 options.log_level = SYSLOG_LEVEL_DEBUG1;
0fff78ff 363 } else {
364 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
365 options.log_level++;
3c0ef626 366 break;
0fff78ff 367 }
996d5e62 368 /* FALLTHROUGH */
3c0ef626 369 case 'V':
99be0775 370 fprintf(stderr, "%s, %s\n",
996d5e62 371 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
3c0ef626 372 if (opt == 'V')
373 exit(0);
374 break;
2c06c99b 375 case 'w':
376 if (options.tun_open == -1)
377 options.tun_open = SSH_TUNMODE_DEFAULT;
378 options.tun_local = a2tun(optarg, &options.tun_remote);
379 if (options.tun_local == SSH_TUNID_ERR) {
22616013 380 fprintf(stderr,
381 "Bad tun device '%s'\n", optarg);
2c06c99b 382 exit(255);
383 }
384 break;
3c0ef626 385 case 'q':
386 options.log_level = SYSLOG_LEVEL_QUIET;
387 break;
388 case 'e':
389 if (optarg[0] == '^' && optarg[2] == 0 &&
390 (u_char) optarg[1] >= 64 &&
391 (u_char) optarg[1] < 128)
392 options.escape_char = (u_char) optarg[1] & 31;
393 else if (strlen(optarg) == 1)
394 options.escape_char = (u_char) optarg[0];
395 else if (strcmp(optarg, "none") == 0)
396 options.escape_char = SSH_ESCAPECHAR_NONE;
397 else {
398 fprintf(stderr, "Bad escape character '%s'.\n",
399 optarg);
2c06c99b 400 exit(255);
3c0ef626 401 }
402 break;
403 case 'c':
404 if (ciphers_valid(optarg)) {
405 /* SSH2 only */
406 options.ciphers = xstrdup(optarg);
c9f39d2c 407 options.cipher = SSH_CIPHER_INVALID;
3c0ef626 408 } else {
409 /* SSH1 only */
410 options.cipher = cipher_number(optarg);
411 if (options.cipher == -1) {
412 fprintf(stderr,
413 "Unknown cipher type '%s'\n",
414 optarg);
2c06c99b 415 exit(255);
3c0ef626 416 }
417 if (options.cipher == SSH_CIPHER_3DES)
418 options.ciphers = "3des-cbc";
419 else if (options.cipher == SSH_CIPHER_BLOWFISH)
420 options.ciphers = "blowfish-cbc";
421 else
422 options.ciphers = (char *)-1;
423 }
424 break;
425 case 'm':
426 if (mac_valid(optarg))
427 options.macs = xstrdup(optarg);
428 else {
429 fprintf(stderr, "Unknown mac type '%s'\n",
430 optarg);
2c06c99b 431 exit(255);
3c0ef626 432 }
433 break;
c9f39d2c 434 case 'M':
665a873d 435 if (options.control_master == SSHCTL_MASTER_YES)
436 options.control_master = SSHCTL_MASTER_ASK;
437 else
438 options.control_master = SSHCTL_MASTER_YES;
c9f39d2c 439 break;
3c0ef626 440 case 'p':
441 options.port = a2port(optarg);
442 if (options.port == 0) {
443 fprintf(stderr, "Bad port '%s'\n", optarg);
2c06c99b 444 exit(255);
3c0ef626 445 }
446 break;
447 case 'l':
448 options.user = optarg;
449 break;
450
451 case 'L':
996d5e62 452 if (parse_forward(&fwd, optarg))
453 add_local_forward(&options, &fwd);
454 else {
3c0ef626 455 fprintf(stderr,
996d5e62 456 "Bad local forwarding specification '%s'\n",
3c0ef626 457 optarg);
2c06c99b 458 exit(255);
3c0ef626 459 }
996d5e62 460 break;
461
462 case 'R':
463 if (parse_forward(&fwd, optarg)) {
464 add_remote_forward(&options, &fwd);
465 } else {
3c0ef626 466 fprintf(stderr,
996d5e62 467 "Bad remote forwarding specification "
468 "'%s'\n", optarg);
2c06c99b 469 exit(255);
3c0ef626 470 }
3c0ef626 471 break;
472
473 case 'D':
996d5e62 474 cp = p = xstrdup(optarg);
475 memset(&fwd, '\0', sizeof(fwd));
476 fwd.connect_host = "socks";
477 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
478 fprintf(stderr, "Bad dynamic forwarding "
479 "specification '%.100s'\n", optarg);
2c06c99b 480 exit(255);
996d5e62 481 }
482 if (cp != NULL) {
483 fwd.listen_port = a2port(cp);
22616013 484 fwd.listen_host =
485 cleanhostname(fwd.listen_host);
996d5e62 486 } else {
487 fwd.listen_port = a2port(fwd.listen_host);
665a873d 488 fwd.listen_host = NULL;
996d5e62 489 }
490
491 if (fwd.listen_port == 0) {
3c0ef626 492 fprintf(stderr, "Bad dynamic port '%s'\n",
493 optarg);
2c06c99b 494 exit(255);
3c0ef626 495 }
996d5e62 496 add_local_forward(&options, &fwd);
497 xfree(p);
3c0ef626 498 break;
499
500 case 'C':
501 options.compression = 1;
502 break;
503 case 'N':
504 no_shell_flag = 1;
505 no_tty_flag = 1;
506 break;
3c0ef626 507 case 'o':
508 dummy = 1;
cdd66111 509 line = xstrdup(optarg);
3c0ef626 510 if (process_config_line(&options, host ? host : "",
cdd66111 511 line, "command-line", 0, &dummy) != 0)
2c06c99b 512 exit(255);
cdd66111 513 xfree(line);
3c0ef626 514 break;
c015d727 515 case 'T':
516 no_tty_flag = 1;
517 /* ensure that the user doesn't try to backdoor a */
518 /* null cipher switch on an interactive session */
519 /* so explicitly disable it no matter what */
520 options.none_switch=0;
521 break;
3c0ef626 522 case 's':
523 subsystem_flag = 1;
524 break;
c9f39d2c 525 case 'S':
526 if (options.control_path != NULL)
527 free(options.control_path);
528 options.control_path = xstrdup(optarg);
529 break;
3c0ef626 530 case 'b':
531 options.bind_address = optarg;
532 break;
533 case 'F':
534 config = optarg;
535 break;
536 default:
537 usage();
538 }
539 }
540
541 ac -= optind;
542 av += optind;
543
544 if (ac > 0 && !host && **av != '-') {
6a9b3198 545 if (strrchr(*av, '@')) {
3c0ef626 546 p = xstrdup(*av);
6a9b3198 547 cp = strrchr(p, '@');
3c0ef626 548 if (cp == NULL || cp == p)
549 usage();
550 options.user = p;
551 *cp = '\0';
552 host = ++cp;
553 } else
554 host = *av;
6a9b3198 555 if (ac > 1) {
556 optind = optreset = 1;
3c0ef626 557 goto again;
558 }
6a9b3198 559 ac--, av++;
3c0ef626 560 }
561
562 /* Check that we got a host name. */
563 if (!host)
564 usage();
565
566 SSLeay_add_all_algorithms();
567 ERR_load_crypto_strings();
3c0ef626 568
569 /* Initialize the command to execute on remote host. */
570 buffer_init(&command);
571
572 /*
573 * Save the command to execute on the remote host in a buffer. There
574 * is no limit on the length of the command, except by the maximum
575 * packet size. Also sets the tty flag if there is no command.
576 */
577 if (!ac) {
578 /* No command specified - execute shell on a tty. */
579 tty_flag = 1;
580 if (subsystem_flag) {
581 fprintf(stderr,
582 "You must specify a subsystem to invoke.\n");
583 usage();
584 }
585 } else {
586 /* A command has been specified. Store it into the buffer. */
587 for (i = 0; i < ac; i++) {
588 if (i)
589 buffer_append(&command, " ", 1);
590 buffer_append(&command, av[i], strlen(av[i]));
591 }
592 }
593
594 /* Cannot fork to background if no command. */
22616013 595 if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
596 !no_shell_flag)
597 fatal("Cannot fork into background without a command "
598 "to execute.");
3c0ef626 599
600 /* Allocate a tty by default if no command specified. */
601 if (buffer_len(&command) == 0)
602 tty_flag = 1;
603
41b2f314 604 /* Force no tty */
3c0ef626 605 if (no_tty_flag)
606 tty_flag = 0;
607 /* Do not allocate a tty if stdin is not a tty. */
665a873d 608 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
3c0ef626 609 if (tty_flag)
22616013 610 logit("Pseudo-terminal will not be allocated because "
611 "stdin is not a terminal.");
3c0ef626 612 tty_flag = 0;
613 }
614
615 /*
616 * Initialize "log" output. Since we are the client all output
617 * actually goes to stderr.
618 */
22616013 619 log_init(av[0],
620 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
3c0ef626 621 SYSLOG_FACILITY_USER, 1);
622
623 /*
624 * Read per-user configuration file. Ignore the system wide config
625 * file if the user specifies a config file on the command line.
626 */
627 if (config != NULL) {
c9f39d2c 628 if (!read_config_file(config, host, &options, 0))
3c0ef626 629 fatal("Can't open user config file %.100s: "
630 "%.100s", config, strerror(errno));
799ae497 631 } else {
3c0ef626 632 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
633 _PATH_SSH_USER_CONFFILE);
c9f39d2c 634 (void)read_config_file(buf, host, &options, 1);
3c0ef626 635
636 /* Read systemwide configuration file after use config. */
c9f39d2c 637 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
638 &options, 0);
3c0ef626 639 }
640
641 /* Fill configuration defaults. */
642 fill_default_options(&options);
643
0fff78ff 644 channel_set_af(options.address_family);
645
3c0ef626 646 /* reinit */
647 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
648
649 seed_rng();
650
651 if (options.user == NULL)
652 options.user = xstrdup(pw->pw_name);
653
22616013 654 /* Get default port if port has not been set. */
655 if (options.port == 0) {
656 sp = getservbyname(SSH_SERVICE_NAME, "tcp");
657 options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
658 }
659
660 if (options.local_command != NULL) {
661 char thishost[NI_MAXHOST];
662
663 if (gethostname(thishost, sizeof(thishost)) == -1)
664 fatal("gethostname: %s", strerror(errno));
665 snprintf(buf, sizeof(buf), "%d", options.port);
666 debug3("expanding LocalCommand: %s", options.local_command);
667 cp = options.local_command;
668 options.local_command = percent_expand(cp, "d", pw->pw_dir,
669 "h", options.hostname? options.hostname : host,
670 "l", thishost, "n", host, "r", options.user, "p", buf,
671 "u", pw->pw_name, (char *)NULL);
672 debug3("expanded LocalCommand: %s", options.local_command);
673 xfree(cp);
674 }
675
3c0ef626 676 if (options.hostname != NULL)
677 host = options.hostname;
678
0fff78ff 679 /* force lowercase for hostkey matching */
680 if (options.host_key_alias != NULL) {
681 for (p = options.host_key_alias; *p; p++)
682 if (isupper(*p))
9108f8d9 683 *p = (char)tolower(*p);
0fff78ff 684 }
685
6a9b3198 686 if (options.proxy_command != NULL &&
47686178 687 strcmp(options.proxy_command, "none") == 0) {
688 xfree(options.proxy_command);
6a9b3198 689 options.proxy_command = NULL;
47686178 690 }
665a873d 691 if (options.control_path != NULL &&
47686178 692 strcmp(options.control_path, "none") == 0) {
693 xfree(options.control_path);
665a873d 694 options.control_path = NULL;
47686178 695 }
6a9b3198 696
c9f39d2c 697 if (options.control_path != NULL) {
9108f8d9 698 char thishost[NI_MAXHOST];
699
700 if (gethostname(thishost, sizeof(thishost)) == -1)
701 fatal("gethostname: %s", strerror(errno));
665a873d 702 snprintf(buf, sizeof(buf), "%d", options.port);
703 cp = tilde_expand_filename(options.control_path,
704 original_real_uid);
47686178 705 xfree(options.control_path);
665a873d 706 options.control_path = percent_expand(cp, "p", buf, "h", host,
9108f8d9 707 "r", options.user, "l", thishost, (char *)NULL);
665a873d 708 xfree(cp);
c9f39d2c 709 }
22616013 710 if (muxclient_command != 0 && options.control_path == NULL)
665a873d 711 fatal("No ControlPath specified for \"-O\" command");
712 if (options.control_path != NULL)
22616013 713 muxclient(options.control_path);
c9f39d2c 714
47686178 715 timeout_ms = options.connection_timeout * 1000;
716
3c0ef626 717 /* Open a connection to the remote host. */
0fff78ff 718 if (ssh_connect(host, &hostaddr, options.port,
47686178 719 options.address_family, options.connection_attempts, &timeout_ms,
720 options.tcp_keep_alive,
f5799ae1 721#ifdef HAVE_CYGWIN
722 options.use_privileged_port,
723#else
724 original_effective_uid == 0 && options.use_privileged_port,
725#endif
726 options.proxy_command) != 0)
2c06c99b 727 exit(255);
3c0ef626 728
47686178 729 if (timeout_ms > 0)
730 debug3("timeout: %d ms remain after connect", timeout_ms);
731
3c0ef626 732 /*
733 * If we successfully made the connection, load the host private key
734 * in case we will need it later for combined rsa-rhosts
735 * authentication. This must be done before releasing extra
736 * privileges, because the file is only readable by root.
f5799ae1 737 * If we cannot access the private keys, load the public keys
738 * instead and try to execute the ssh-keysign helper instead.
3c0ef626 739 */
740 sensitive_data.nkeys = 0;
741 sensitive_data.keys = NULL;
f5799ae1 742 sensitive_data.external_keysign = 0;
743 if (options.rhosts_rsa_authentication ||
744 options.hostbased_authentication) {
3c0ef626 745 sensitive_data.nkeys = 3;
9108f8d9 746 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
41b2f314 747 sizeof(Key));
f5799ae1 748
749 PRIV_START;
3c0ef626 750 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
9108f8d9 751 _PATH_HOST_KEY_FILE, "", NULL, NULL);
3c0ef626 752 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
9108f8d9 753 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
3c0ef626 754 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
9108f8d9 755 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
f5799ae1 756 PRIV_END;
757
41b2f314 758 if (options.hostbased_authentication == 1 &&
759 sensitive_data.keys[0] == NULL &&
f5799ae1 760 sensitive_data.keys[1] == NULL &&
761 sensitive_data.keys[2] == NULL) {
762 sensitive_data.keys[1] = key_load_public(
763 _PATH_HOST_DSA_KEY_FILE, NULL);
764 sensitive_data.keys[2] = key_load_public(
765 _PATH_HOST_RSA_KEY_FILE, NULL);
766 sensitive_data.external_keysign = 1;
767 }
3c0ef626 768 }
769 /*
770 * Get rid of any extra privileges that we may have. We will no
771 * longer need them. Also, extra privileges could make it very hard
772 * to read identity files and other non-world-readable files from the
773 * user's home directory if it happens to be on a NFS volume where
774 * root is mapped to nobody.
775 */
c9f39d2c 776 if (original_effective_uid == 0) {
777 PRIV_START;
778 permanently_set_uid(pw);
779 }
3c0ef626 780
781 /*
782 * Now that we are back to our own permissions, create ~/.ssh
2c06c99b 783 * directory if it doesn't already exist.
3c0ef626 784 */
22616013 785 snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir,
786 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
3c0ef626 787 if (stat(buf, &st) < 0)
788 if (mkdir(buf, 0700) < 0)
789 error("Could not create directory '%.200s'.", buf);
790
3c0ef626 791 /* load options.identity_files */
792 load_public_identity_files();
793
794 /* Expand ~ in known host file names. */
795 /* XXX mem-leaks: */
796 options.system_hostfile =
797 tilde_expand_filename(options.system_hostfile, original_real_uid);
798 options.user_hostfile =
799 tilde_expand_filename(options.user_hostfile, original_real_uid);
800 options.system_hostfile2 =
801 tilde_expand_filename(options.system_hostfile2, original_real_uid);
802 options.user_hostfile2 =
803 tilde_expand_filename(options.user_hostfile2, original_real_uid);
804
805 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
806
22616013 807 /* Log into the remote system. Never returns if the login fails. */
47686178 808 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
809 pw, timeout_ms);
3c0ef626 810
811 /* We no longer need the private host keys. Clear them now. */
812 if (sensitive_data.nkeys != 0) {
813 for (i = 0; i < sensitive_data.nkeys; i++) {
814 if (sensitive_data.keys[i] != NULL) {
815 /* Destroys contents safely */
816 debug3("clear hostkey %d", i);
817 key_free(sensitive_data.keys[i]);
818 sensitive_data.keys[i] = NULL;
819 }
820 }
821 xfree(sensitive_data.keys);
822 }
823 for (i = 0; i < options.num_identity_files; i++) {
824 if (options.identity_files[i]) {
825 xfree(options.identity_files[i]);
826 options.identity_files[i] = NULL;
827 }
828 if (options.identity_keys[i]) {
829 key_free(options.identity_keys[i]);
830 options.identity_keys[i] = NULL;
831 }
832 }
833
834 exit_status = compat20 ? ssh_session2() : ssh_session();
835 packet_close();
41b2f314 836
22616013 837 if (options.control_path != NULL && muxserver_sock != -1)
c9f39d2c 838 unlink(options.control_path);
839
41b2f314 840 /*
cdd66111 841 * Send SIGHUP to proxy command if used. We don't wait() in
41b2f314 842 * case it hangs and instead rely on init to reap the child
843 */
844 if (proxy_command_pid > 1)
845 kill(proxy_command_pid, SIGHUP);
846
3c0ef626 847 return exit_status;
848}
849
22616013 850/* Callback for remote forward global requests */
851static void
852ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt)
853{
854 Forward *rfwd = (Forward *)ctxt;
855
856 debug("remote forward %s for: listen %d, connect %s:%d",
857 type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
858 rfwd->listen_port, rfwd->connect_host, rfwd->connect_port);
859 if (type == SSH2_MSG_REQUEST_FAILURE) {
860 if (options.exit_on_forward_failure)
861 fatal("Error: remote port forwarding failed for "
862 "listen port %d", rfwd->listen_port);
863 else
864 logit("Warning: remote port forwarding failed for "
865 "listen port %d", rfwd->listen_port);
866 }
867 if (++remote_forward_confirms_received == options.num_remote_forwards) {
868 debug("All remote forwarding requests processed");
869 if (fork_after_authentication_flag) {
870 fork_after_authentication_flag = 0;
871 if (daemon(1, 1) < 0)
872 fatal("daemon() failed: %.200s",
873 strerror(errno));
874 }
875 }
876}
877
3c0ef626 878static void
879ssh_init_forwarding(void)
880{
881 int success = 0;
882 int i;
883
884 /* Initiate local TCP/IP port forwardings. */
885 for (i = 0; i < options.num_local_forwards; i++) {
996d5e62 886 debug("Local connections to %.200s:%d forwarded to remote "
887 "address %.200s:%d",
dec6d9fe 888 (options.local_forwards[i].listen_host == NULL) ?
889 (options.gateway_ports ? "*" : "LOCALHOST") :
996d5e62 890 options.local_forwards[i].listen_host,
891 options.local_forwards[i].listen_port,
892 options.local_forwards[i].connect_host,
893 options.local_forwards[i].connect_port);
e9a17296 894 success += channel_setup_local_fwd_listener(
996d5e62 895 options.local_forwards[i].listen_host,
896 options.local_forwards[i].listen_port,
897 options.local_forwards[i].connect_host,
898 options.local_forwards[i].connect_port,
3c0ef626 899 options.gateway_ports);
900 }
9108f8d9 901 if (i > 0 && success != i && options.exit_on_forward_failure)
902 fatal("Could not request local forwarding.");
3c0ef626 903 if (i > 0 && success == 0)
904 error("Could not request local forwarding.");
905
906 /* Initiate remote TCP/IP port forwardings. */
907 for (i = 0; i < options.num_remote_forwards; i++) {
996d5e62 908 debug("Remote connections from %.200s:%d forwarded to "
909 "local address %.200s:%d",
665a873d 910 (options.remote_forwards[i].listen_host == NULL) ?
2c06c99b 911 "LOCALHOST" : options.remote_forwards[i].listen_host,
996d5e62 912 options.remote_forwards[i].listen_port,
913 options.remote_forwards[i].connect_host,
914 options.remote_forwards[i].connect_port);
9108f8d9 915 if (channel_request_remote_forwarding(
996d5e62 916 options.remote_forwards[i].listen_host,
917 options.remote_forwards[i].listen_port,
918 options.remote_forwards[i].connect_host,
9108f8d9 919 options.remote_forwards[i].connect_port) < 0) {
920 if (options.exit_on_forward_failure)
921 fatal("Could not request remote forwarding.");
922 else
923 logit("Warning: Could not request remote "
924 "forwarding.");
925 }
22616013 926 client_register_global_confirm(ssh_confirm_remote_forward,
927 &options.remote_forwards[i]);
3c0ef626 928 }
d4487008 929
930 /* Initiate tunnel forwarding. */
931 if (options.tun_open != SSH_TUNMODE_NO) {
932 if (client_request_tun_fwd(options.tun_open,
933 options.tun_local, options.tun_remote) == -1) {
934 if (options.exit_on_forward_failure)
935 fatal("Could not request tunnel forwarding.");
936 else
937 error("Could not request tunnel forwarding.");
938 }
939 }
3c0ef626 940}
941
942static void
943check_agent_present(void)
944{
945 if (options.forward_agent) {
2c06c99b 946 /* Clear agent forwarding if we don't have an agent. */
41b2f314 947 if (!ssh_agent_present())
3c0ef626 948 options.forward_agent = 0;
3c0ef626 949 }
950}
951
952static int
953ssh_session(void)
954{
955 int type;
3c0ef626 956 int interactive = 0;
957 int have_tty = 0;
958 struct winsize ws;
959 char *cp;
665a873d 960 const char *display;
3c0ef626 961
962 /* Enable compression if requested. */
963 if (options.compression) {
22616013 964 debug("Requesting compression at level %d.",
965 options.compression_level);
3c0ef626 966
22616013 967 if (options.compression_level < 1 ||
968 options.compression_level > 9)
969 fatal("Compression level must be from 1 (fast) to "
970 "9 (slow, best).");
3c0ef626 971
972 /* Send the request. */
973 packet_start(SSH_CMSG_REQUEST_COMPRESSION);
974 packet_put_int(options.compression_level);
975 packet_send();
976 packet_write_wait();
e9a17296 977 type = packet_read();
3c0ef626 978 if (type == SSH_SMSG_SUCCESS)
979 packet_start_compression(options.compression_level);
980 else if (type == SSH_SMSG_FAILURE)
0fff78ff 981 logit("Warning: Remote host refused compression.");
3c0ef626 982 else
22616013 983 packet_disconnect("Protocol error waiting for "
984 "compression response.");
3c0ef626 985 }
986 /* Allocate a pseudo tty if appropriate. */
987 if (tty_flag) {
988 debug("Requesting pty.");
989
990 /* Start the packet. */
991 packet_start(SSH_CMSG_REQUEST_PTY);
992
993 /* Store TERM in the packet. There is no limit on the
994 length of the string. */
995 cp = getenv("TERM");
996 if (!cp)
997 cp = "";
998 packet_put_cstring(cp);
999
1000 /* Store window size in the packet. */
1001 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1002 memset(&ws, 0, sizeof(ws));
9108f8d9 1003 packet_put_int((u_int)ws.ws_row);
1004 packet_put_int((u_int)ws.ws_col);
1005 packet_put_int((u_int)ws.ws_xpixel);
1006 packet_put_int((u_int)ws.ws_ypixel);
3c0ef626 1007
1008 /* Store tty modes in the packet. */
1009 tty_make_modes(fileno(stdin), NULL);
1010
1011 /* Send the packet, and wait for it to leave. */
1012 packet_send();
1013 packet_write_wait();
1014
1015 /* Read response from the server. */
e9a17296 1016 type = packet_read();
3c0ef626 1017 if (type == SSH_SMSG_SUCCESS) {
1018 interactive = 1;
1019 have_tty = 1;
1020 } else if (type == SSH_SMSG_FAILURE)
22616013 1021 logit("Warning: Remote host failed or refused to "
1022 "allocate a pseudo tty.");
3c0ef626 1023 else
22616013 1024 packet_disconnect("Protocol error waiting for pty "
1025 "request response.");
3c0ef626 1026 }
1027 /* Request X11 forwarding if enabled and DISPLAY is set. */
665a873d 1028 display = getenv("DISPLAY");
1029 if (options.forward_x11 && display != NULL) {
e9a17296 1030 char *proto, *data;
3c0ef626 1031 /* Get reasonable local authentication information. */
665a873d 1032 client_x11_get_proto(display, options.xauth_location,
1033 options.forward_x11_trusted, &proto, &data);
3c0ef626 1034 /* Request forwarding with authentication spoofing. */
22616013 1035 debug("Requesting X11 forwarding with authentication "
1036 "spoofing.");
665a873d 1037 x11_request_forwarding_with_spoofing(0, display, proto, data);
3c0ef626 1038
1039 /* Read response from the server. */
e9a17296 1040 type = packet_read();
3c0ef626 1041 if (type == SSH_SMSG_SUCCESS) {
1042 interactive = 1;
1043 } else if (type == SSH_SMSG_FAILURE) {
0fff78ff 1044 logit("Warning: Remote host denied X11 forwarding.");
3c0ef626 1045 } else {
22616013 1046 packet_disconnect("Protocol error waiting for X11 "
1047 "forwarding");
3c0ef626 1048 }
1049 }
1050 /* Tell the packet module whether this is an interactive session. */
1051 packet_set_interactive(interactive);
1052
1053 /* Request authentication agent forwarding if appropriate. */
1054 check_agent_present();
1055
1056 if (options.forward_agent) {
1057 debug("Requesting authentication agent forwarding.");
1058 auth_request_forwarding();
1059
1060 /* Read response from the server. */
e9a17296 1061 type = packet_read();
1062 packet_check_eom();
3c0ef626 1063 if (type != SSH_SMSG_SUCCESS)
0fff78ff 1064 logit("Warning: Remote host denied authentication agent forwarding.");
3c0ef626 1065 }
1066
1067 /* Initiate port forwardings. */
1068 ssh_init_forwarding();
1069
47686178 1070 /* Execute a local command */
1071 if (options.local_command != NULL &&
1072 options.permit_local_command)
1073 ssh_local_cmd(options.local_command);
1074
22616013 1075 /*
1076 * If requested and we are not interested in replies to remote
1077 * forwarding requests, then let ssh continue in the background.
1078 */
1079 if (fork_after_authentication_flag &&
1080 (!options.exit_on_forward_failure ||
1081 options.num_remote_forwards == 0)) {
1082 fork_after_authentication_flag = 0;
3c0ef626 1083 if (daemon(1, 1) < 0)
1084 fatal("daemon() failed: %.200s", strerror(errno));
22616013 1085 }
3c0ef626 1086
1087 /*
1088 * If a command was specified on the command line, execute the
1089 * command now. Otherwise request the server to start a shell.
1090 */
1091 if (buffer_len(&command) > 0) {
1092 int len = buffer_len(&command);
1093 if (len > 900)
1094 len = 900;
22616013 1095 debug("Sending command: %.*s", len,
1096 (u_char *)buffer_ptr(&command));
3c0ef626 1097 packet_start(SSH_CMSG_EXEC_CMD);
1098 packet_put_string(buffer_ptr(&command), buffer_len(&command));
1099 packet_send();
1100 packet_write_wait();
1101 } else {
1102 debug("Requesting shell.");
1103 packet_start(SSH_CMSG_EXEC_SHELL);
1104 packet_send();
1105 packet_write_wait();
1106 }
1107
1108 /* Enter the interactive session. */
1109 return client_loop(have_tty, tty_flag ?
1110 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1111}
1112
c9f39d2c 1113/* request pty/x11/agent/tcpfwd/shell for channel */
1114static void
1115ssh_session2_setup(int id, void *arg)
1116{
1117 extern char **environ;
665a873d 1118 const char *display;
c9f39d2c 1119 int interactive = tty_flag;
665a873d 1120
1121 display = getenv("DISPLAY");
1122 if (options.forward_x11 && display != NULL) {
e9a17296 1123 char *proto, *data;
3c0ef626 1124 /* Get reasonable local authentication information. */
665a873d 1125 client_x11_get_proto(display, options.xauth_location,
1126 options.forward_x11_trusted, &proto, &data);
3c0ef626 1127 /* Request forwarding with authentication spoofing. */
22616013 1128 debug("Requesting X11 forwarding with authentication "
1129 "spoofing.");
665a873d 1130 x11_request_forwarding_with_spoofing(id, display, proto, data);
3c0ef626 1131 interactive = 1;
1132 /* XXX wait for reply */
1133 }
1134
1135 check_agent_present();
1136 if (options.forward_agent) {
1137 debug("Requesting authentication agent forwarding.");
1138 channel_request_start(id, "auth-agent-req@openssh.com", 0);
1139 packet_send();
1140 }
1141
c9f39d2c 1142 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
22616013 1143 NULL, fileno(stdin), &command, environ);
3c0ef626 1144
3c0ef626 1145 packet_set_interactive(interactive);
1146}
1147
1148/* open new channel for a session */
1149static int
1150ssh_session2_open(void)
1151{
1152 Channel *c;
1153 int window, packetmax, in, out, err;
c015d727 1154 int sock;
1155 int socksize;
1156 int socksizelen = sizeof(int);
3c0ef626 1157
1158 if (stdin_null_flag) {
1159 in = open(_PATH_DEVNULL, O_RDONLY);
1160 } else {
1161 in = dup(STDIN_FILENO);
1162 }
1163 out = dup(STDOUT_FILENO);
1164 err = dup(STDERR_FILENO);
1165
1166 if (in < 0 || out < 0 || err < 0)
1167 fatal("dup() in/out/err failed");
1168
1169 /* enable nonblocking unless tty */
1170 if (!isatty(in))
1171 set_nonblock(in);
1172 if (!isatty(out))
1173 set_nonblock(out);
1174 if (!isatty(err))
1175 set_nonblock(err);
1176
c015d727 1177 /* we need to check to see if what they want to do about buffer */
1178 /* sizes here. In a hpn to nonhpn connection we want to limit */
1179 /* the window size to something reasonable in case the far side */
1180 /* has the large window bug. In hpn to hpn connection we want to */
1181 /* use the max window size but allow the user to override it */
1182 /* lastly if they disabled hpn then use the ssh std window size */
1183
1184 /* so why don't we just do a getsockopt() here and set the */
1185 /* ssh window to that? In the case of a autotuning receive */
1186 /* window the window would get stuck at the initial buffer */
1187 /* size generally less than 96k. Therefore we need to set the */
1188 /* maximum ssh window size to the maximum hpn buffer size */
1189 /* unless the user has specifically set the tcprcvbufpoll */
1190 /* to no. In which case we *can* just set the window to the */
1191 /* minimum of the hpn buffer size and tcp receive buffer size */
1192
1193 if (tty_flag)
1194 options.hpn_buffer_size = CHAN_SES_WINDOW_DEFAULT;
1195 else
1196 options.hpn_buffer_size = 2*1024*1024;
1197
1198 if (datafellows & SSH_BUG_LARGEWINDOW)
1199 {
1200 debug("HPN to Non-HPN Connection");
1201 }
1202 else
1203 {
1204 if (options.tcp_rcv_buf_poll <= 0)
1205 {
1206 sock = socket(AF_INET, SOCK_STREAM, 0);
1207 getsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1208 &socksize, &socksizelen);
1209 close(sock);
1210 debug("socksize %d", socksize);
1211 options.hpn_buffer_size = socksize;
1212 debug ("HPNBufferSize set to TCP RWIN: %d", options.hpn_buffer_size);
1213 }
1214 else
1215 {
1216 if (options.tcp_rcv_buf > 0)
1217 {
1218 /*create a socket but don't connect it */
1219 /* we use that the get the rcv socket size */
1220 sock = socket(AF_INET, SOCK_STREAM, 0);
1221 /* if they are using the tcp_rcv_buf option */
1222 /* attempt to set the buffer size to that */
1223 if (options.tcp_rcv_buf)
1224 setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&options.tcp_rcv_buf,
1225 sizeof(options.tcp_rcv_buf));
1226 getsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1227 &socksize, &socksizelen);
1228 close(sock);
1229 debug("socksize %d", socksize);
1230 options.hpn_buffer_size = socksize;
1231 debug ("HPNBufferSize set to user TCPRcvBuf: %d", options.hpn_buffer_size);
1232 }
1233 }
1234
1235 }
1236
1237 debug("Final hpn_buffer_size = %d", options.hpn_buffer_size);
1238
1239 window = options.hpn_buffer_size;
1240
1241 channel_set_hpn(options.hpn_disabled, options.hpn_buffer_size);
1242
3c0ef626 1243 packetmax = CHAN_SES_PACKET_DEFAULT;
e9a17296 1244 if (tty_flag) {
c015d727 1245 window = 4*CHAN_SES_PACKET_DEFAULT;
e9a17296 1246 window >>= 1;
1247 packetmax >>= 1;
3c0ef626 1248 }
1249 c = channel_new(
1250 "session", SSH_CHANNEL_OPENING, in, out, err,
1251 window, packetmax, CHAN_EXTENDED_WRITE,
0fff78ff 1252 "client-session", /*nonblock*/0);
c015d727 1253 if ((options.tcp_rcv_buf_poll > 0) && (!options.hpn_disabled)) {
1254 c->dynamic_window = 1;
1255 debug ("Enabled Dynamic Window Scaling\n");
1256 }
3c0ef626 1257 debug3("ssh_session2_open: channel_new: %d", c->self);
1258
1259 channel_send_open(c->self);
1260 if (!no_shell_flag)
22616013 1261 channel_register_open_confirm(c->self,
1262 ssh_session2_setup, NULL);
3c0ef626 1263
1264 return c->self;
1265}
1266
1267static int
1268ssh_session2(void)
1269{
1270 int id = -1;
1271
1272 /* XXX should be pre-session */
1273 ssh_init_forwarding();
1274
1275 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1276 id = ssh_session2_open();
1277
22616013 1278 /* If we don't expect to open a new session, then disallow it */
1279 if (options.control_master == SSHCTL_MASTER_NO) {
1280 debug("Requesting no-more-sessions@openssh.com");
1281 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1282 packet_put_cstring("no-more-sessions@openssh.com");
1283 packet_put_char(0);
1284 packet_send();
1285 }
1286
2c06c99b 1287 /* Execute a local command */
1288 if (options.local_command != NULL &&
1289 options.permit_local_command)
1290 ssh_local_cmd(options.local_command);
1291
d4487008 1292 /* Start listening for multiplex clients */
22616013 1293 muxserver_listen();
d4487008 1294
3c0ef626 1295 /* If requested, let ssh continue in the background. */
22616013 1296 if (fork_after_authentication_flag) {
1297 fork_after_authentication_flag = 0;
3c0ef626 1298 if (daemon(1, 1) < 0)
1299 fatal("daemon() failed: %.200s", strerror(errno));
22616013 1300 }
3c0ef626 1301
1302 return client_loop(tty_flag, tty_flag ?
1303 options.escape_char : SSH_ESCAPECHAR_NONE, id);
1304}
1305
1306static void
1307load_public_identity_files(void)
1308{
9108f8d9 1309 char *filename, *cp, thishost[NI_MAXHOST];
47686178 1310 char *pwdir = NULL, *pwname = NULL;
3c0ef626 1311 int i = 0;
700318f3 1312 Key *public;
9108f8d9 1313 struct passwd *pw;
3c0ef626 1314#ifdef SMARTCARD
700318f3 1315 Key **keys;
1316
3c0ef626 1317 if (options.smartcard_device != NULL &&
700318f3 1318 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
9108f8d9 1319 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
700318f3 1320 int count = 0;
1321 for (i = 0; keys[i] != NULL; i++) {
1322 count++;
22616013 1323 memmove(&options.identity_files[1],
1324 &options.identity_files[0],
700318f3 1325 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
22616013 1326 memmove(&options.identity_keys[1],
1327 &options.identity_keys[0],
700318f3 1328 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1329 options.num_identity_files++;
1330 options.identity_keys[0] = keys[i];
0fff78ff 1331 options.identity_files[0] = sc_get_key_label(keys[i]);
700318f3 1332 }
1333 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1334 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1335 i = count;
1336 xfree(keys);
3c0ef626 1337 }
1338#endif /* SMARTCARD */
9108f8d9 1339 if ((pw = getpwuid(original_real_uid)) == NULL)
1340 fatal("load_public_identity_files: getpwuid failed");
47686178 1341 pwname = xstrdup(pw->pw_name);
1342 pwdir = xstrdup(pw->pw_dir);
9108f8d9 1343 if (gethostname(thishost, sizeof(thishost)) == -1)
1344 fatal("load_public_identity_files: gethostname: %s",
1345 strerror(errno));
3c0ef626 1346 for (; i < options.num_identity_files; i++) {
9108f8d9 1347 cp = tilde_expand_filename(options.identity_files[i],
3c0ef626 1348 original_real_uid);
47686178 1349 filename = percent_expand(cp, "d", pwdir,
1350 "u", pwname, "l", thishost, "h", host,
9108f8d9 1351 "r", options.user, (char *)NULL);
1352 xfree(cp);
3c0ef626 1353 public = key_load_public(filename, NULL);
1354 debug("identity file %s type %d", filename,
1355 public ? public->type : -1);
1356 xfree(options.identity_files[i]);
1357 options.identity_files[i] = filename;
1358 options.identity_keys[i] = public;
1359 }
47686178 1360 bzero(pwname, strlen(pwname));
1361 xfree(pwname);
1362 bzero(pwdir, strlen(pwdir));
1363 xfree(pwdir);
3c0ef626 1364}
This page took 0.29156 seconds and 5 git commands to generate.