]> andersk Git - openssh.git/blame - ssh.c
- djm@cvs.openbsd.org 2008/10/09 03:50:54
[openssh.git] / ssh.c
CommitLineData
bf793210 1/* $OpenBSD: ssh.c,v 1.320 2008/10/08 23:34:03 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>
5b04a8bf 51#include <sys/socket.h>
a75f5360 52
b6438382 53#include <ctype.h>
dd984467 54#include <errno.h>
d3221cca 55#include <fcntl.h>
28cb0a43 56#include <netdb.h>
b5b88c19 57#ifdef HAVE_PATHS_H
a75f5360 58#include <paths.h>
b5b88c19 59#endif
b1842393 60#include <pwd.h>
ada68823 61#include <signal.h>
24436b92 62#include <stdarg.h>
bcaab305 63#include <stddef.h>
cf851879 64#include <stdio.h>
ffa517a8 65#include <stdlib.h>
00146caa 66#include <string.h>
5188ba17 67#include <unistd.h>
8efc0c15 68
5e837c7b 69#include <netinet/in.h>
70#include <arpa/inet.h>
71
a306f2dd 72#include <openssl/evp.h>
fa08c86b 73#include <openssl/err.h>
f5f25d17 74#include "openbsd-compat/openssl-compat.h"
3593bdc0 75#include "openbsd-compat/sys-queue.h"
a306f2dd 76
31652869 77#include "xmalloc.h"
8efc0c15 78#include "ssh.h"
42f11eb2 79#include "ssh1.h"
80#include "ssh2.h"
81#include "compat.h"
82#include "cipher.h"
8efc0c15 83#include "packet.h"
84#include "buffer.h"
a5efa1bb 85#include "channels.h"
a306f2dd 86#include "key.h"
4c8722d9 87#include "authfd.h"
a306f2dd 88#include "authfile.h"
42f11eb2 89#include "pathnames.h"
5e96b616 90#include "dispatch.h"
b5c334cc 91#include "clientloop.h"
42f11eb2 92#include "log.h"
93#include "readconf.h"
94#include "sshconnect.h"
42f11eb2 95#include "misc.h"
b2552997 96#include "kex.h"
97#include "mac.h"
07d80252 98#include "sshpty.h"
61a2c1da 99#include "match.h"
5e96b616 100#include "msg.h"
6213295d 101#include "uidswap.h"
28015df4 102#include "version.h"
8efc0c15 103
383546c4 104#ifdef SMARTCARD
383546c4 105#include "scard.h"
dd2495cb 106#endif
383546c4 107
f601d847 108extern char *__progname;
f601d847 109
e8097dc9 110/* Flag indicating whether debug mode is on. May be set on the command line. */
8efc0c15 111int debug_flag = 0;
112
a8be9f80 113/* Flag indicating whether a tty should be allocated */
8efc0c15 114int tty_flag = 0;
8abcdba4 115int no_tty_flag = 0;
116int force_tty_flag = 0;
8efc0c15 117
8ce64345 118/* don't exec a shell */
119int no_shell_flag = 0;
8ce64345 120
aa3378df 121/*
122 * Flag indicating that nothing should be read from stdin. This can be set
123 * on the command line.
124 */
8efc0c15 125int stdin_null_flag = 0;
126
aa3378df 127/*
128 * Flag indicating that ssh should fork after authentication. This is useful
c242fc96 129 * so that the passphrase can be entered manually, and then ssh goes to the
aa3378df 130 * background.
131 */
8efc0c15 132int fork_after_authentication_flag = 0;
133
aa3378df 134/*
135 * General data structure for command line options and options configurable
136 * in configuration files. See readconf.h.
137 */
8efc0c15 138Options options;
139
e591b98a 140/* optional user configfile */
141char *config = NULL;
142
aa3378df 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 */
8efc0c15 148char *host;
149
150/* socket address the host resolves to */
48e671d5 151struct sockaddr_storage hostaddr;
8efc0c15 152
8002af61 153/* Private host keys. */
39c00dc2 154Sensitive sensitive_data;
8efc0c15 155
156/* Original real UID. */
157uid_t original_real_uid;
3fb156df 158uid_t original_effective_uid;
8efc0c15 159
8ce64345 160/* command to be executed */
161Buffer command;
162
ae810de7 163/* Should we execute a command or invoke a subsystem? */
164int subsystem_flag = 0;
165
e0ae8728 166/* # of replies received for global requests */
72becb62 167static int remote_forward_confirms_received = 0;
e0ae8728 168
6939bbd4 169/* pid of proxycommand child process */
170pid_t proxy_command_pid = 0;
171
3bcced4c 172/* mux.c */
173extern int muxserver_sock;
174extern u_int muxclient_command;
5e96b616 175
8efc0c15 176/* Prints a help message to the user. This function never returns. */
177
396c147e 178static void
5ca51e19 179usage(void)
8efc0c15 180{
182ccbba 181 fprintf(stderr,
b2f4d5cc 182"usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
80e29ee6 183" [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
8cf98c65 184" [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
3867aa0a 185" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
8cf98c65 186" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
06fa4ac1 187" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
182ccbba 188 );
3aa43b24 189 exit(255);
8efc0c15 190}
191
396c147e 192static int ssh_session(void);
193static int ssh_session2(void);
194static void load_public_identity_files(void);
3bcced4c 195
196/* from muxclient.c */
197void muxclient(const char *);
198void muxserver_listen(void);
8ce64345 199
5260325f 200/*
201 * Main program for the ssh client.
202 */
8efc0c15 203int
204main(int ac, char **av)
205{
bf793210 206 int i, opt, exit_status, use_syslog;
1aafd17a 207 char *p, *cp, *line, buf[256];
5260325f 208 struct stat st;
3b1a83df 209 struct passwd *pw;
71300a43 210 int dummy, timeout_ms;
57dade33 211 extern int optind, optreset;
1812a662 212 extern char *optarg;
c1cbe68a 213 struct servent *sp;
3867aa0a 214 Forward fwd;
5260325f 215
fd6168c1 216 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
217 sanitise_stdfd();
218
fda04d7d 219 __progname = ssh_get_progname(av[0]);
264dce47 220 init_rng();
221
aa3378df 222 /*
223 * Save the original real uid. It will be needed later (uid-swapping
224 * may clobber the real uid).
225 */
5260325f 226 original_real_uid = getuid();
227 original_effective_uid = geteuid();
aff51935 228
9f324470 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;
5260325f 237
7322ef0e 238#ifdef HAVE_SETRLIMIT
5260325f 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));
8efc0c15 245 }
3c62e7eb 246#endif
63bd8c36 247 /* Get user data. */
248 pw = getpwuid(original_real_uid);
249 if (!pw) {
bbe88b6d 250 logit("You don't exist, go away!");
3aa43b24 251 exit(255);
63bd8c36 252 }
253 /* Take a copy of the returned structure. */
254 pw = pwcopy(pw);
255
aa3378df 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 */
5260325f 262 umask(022);
263
e8097dc9 264 /*
265 * Initialize option structure to indicate that no values have been
266 * set.
267 */
5260325f 268 initialize_options(&options);
269
270 /* Parse command-line arguments. */
271 host = NULL;
bf793210 272 use_syslog = 0;
5260325f 273
16a5525d 274 again:
e8097dc9 275 while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
bf793210 276 "ACD:F:I:KL:MNO:PR:S:TVw:XYy")) != -1) {
5260325f 277 switch (opt) {
1f3bf5aa 278 case '1':
279 options.protocol = SSH_PROTO_1;
280 break;
6ae2364d 281 case '2':
282 options.protocol = SSH_PROTO_2;
283 break;
48e671d5 284 case '4':
1572b90f 285 options.address_family = AF_INET;
48e671d5 286 break;
48e671d5 287 case '6':
1572b90f 288 options.address_family = AF_INET6;
48e671d5 289 break;
5260325f 290 case 'n':
291 stdin_null_flag = 1;
292 break;
5260325f 293 case 'f':
294 fork_after_authentication_flag = 1;
295 stdin_null_flag = 1;
296 break;
5260325f 297 case 'x':
298 options.forward_x11 = 0;
299 break;
5260325f 300 case 'X':
301 options.forward_x11 = 1;
302 break;
bf793210 303 case 'y':
304 use_syslog = 1;
305 break;
d73a67d7 306 case 'Y':
307 options.forward_x11 = 1;
308 options.forward_x11_trusted = 1;
309 break;
5260325f 310 case 'g':
311 options.gateway_ports = 1;
312 break;
f8c6db83 313 case 'O':
314 if (strcmp(optarg, "check") == 0)
3bcced4c 315 muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK;
f8c6db83 316 else if (strcmp(optarg, "exit") == 0)
3bcced4c 317 muxclient_command = SSHMUX_COMMAND_TERMINATE;
f8c6db83 318 else
319 fatal("Invalid multiplex command.");
320 break;
e59404d1 321 case 'P': /* deprecated */
5260325f 322 options.use_privileged_port = 0;
323 break;
5260325f 324 case 'a':
325 options.forward_agent = 0;
326 break;
71276795 327 case 'A':
328 options.forward_agent = 1;
329 break;
5260325f 330 case 'k':
f7926e97 331 options.gss_deleg_creds = 0;
5260325f 332 break;
b2f4d5cc 333 case 'K':
334 options.gss_authentication = 1;
335 options.gss_deleg_creds = 1;
336 break;
5260325f 337 case 'i':
338 if (stat(optarg, &st) < 0) {
f5a1a01a 339 fprintf(stderr, "Warning: Identity file %s "
ec304d66 340 "not accessible: %s.\n", optarg,
341 strerror(errno));
5260325f 342 break;
343 }
f5a1a01a 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);
5260325f 350 break;
383546c4 351 case 'I':
352#ifdef SMARTCARD
9ff6f66f 353 options.smartcard_device = xstrdup(optarg);
dd2495cb 354#else
383546c4 355 fprintf(stderr, "no support for smartcards.\n");
dd2495cb 356#endif
383546c4 357 break;
5260325f 358 case 't':
8abcdba4 359 if (tty_flag)
360 force_tty_flag = 1;
5260325f 361 tty_flag = 1;
362 break;
5260325f 363 case 'v':
e053cd2c 364 if (debug_flag == 0) {
bcbf86ec 365 debug_flag = 1;
366 options.log_level = SYSLOG_LEVEL_DEBUG1;
e053cd2c 367 } else {
368 if (options.log_level < SYSLOG_LEVEL_DEBUG3)
369 options.log_level++;
bcbf86ec 370 break;
e053cd2c 371 }
59d4718a 372 /* FALLTHROUGH */
5260325f 373 case 'V':
85ac7a84 374 fprintf(stderr, "%s, %s\n",
4526e8c2 375 SSH_RELEASE, SSLeay_version(SSLEAY_VERSION));
5260325f 376 if (opt == 'V')
377 exit(0);
5260325f 378 break;
d20f3c9e 379 case 'w':
a4f24bf8 380 if (options.tun_open == -1)
381 options.tun_open = SSH_TUNMODE_DEFAULT;
d20f3c9e 382 options.tun_local = a2tun(optarg, &options.tun_remote);
a4f24bf8 383 if (options.tun_local == SSH_TUNID_ERR) {
e8097dc9 384 fprintf(stderr,
385 "Bad tun device '%s'\n", optarg);
3aa43b24 386 exit(255);
d20f3c9e 387 }
388 break;
5260325f 389 case 'q':
390 options.log_level = SYSLOG_LEVEL_QUIET;
391 break;
5260325f 392 case 'e':
393 if (optarg[0] == '^' && optarg[2] == 0 &&
f5a1a01a 394 (u_char) optarg[1] >= 64 &&
395 (u_char) optarg[1] < 128)
1e3b8b07 396 options.escape_char = (u_char) optarg[1] & 31;
5260325f 397 else if (strlen(optarg) == 1)
1e3b8b07 398 options.escape_char = (u_char) optarg[0];
5260325f 399 else if (strcmp(optarg, "none") == 0)
4bf9c10e 400 options.escape_char = SSH_ESCAPECHAR_NONE;
5260325f 401 else {
f5a1a01a 402 fprintf(stderr, "Bad escape character '%s'.\n",
403 optarg);
3aa43b24 404 exit(255);
5260325f 405 }
406 break;
5260325f 407 case 'c':
a306f2dd 408 if (ciphers_valid(optarg)) {
409 /* SSH2 only */
410 options.ciphers = xstrdup(optarg);
d77347cc 411 options.cipher = SSH_CIPHER_INVALID;
a306f2dd 412 } else {
413 /* SSH1 only */
c523303b 414 options.cipher = cipher_number(optarg);
415 if (options.cipher == -1) {
f5a1a01a 416 fprintf(stderr,
417 "Unknown cipher type '%s'\n",
418 optarg);
3aa43b24 419 exit(255);
a306f2dd 420 }
f5a1a01a 421 if (options.cipher == SSH_CIPHER_3DES)
c523303b 422 options.ciphers = "3des-cbc";
f5a1a01a 423 else if (options.cipher == SSH_CIPHER_BLOWFISH)
c523303b 424 options.ciphers = "blowfish-cbc";
f5a1a01a 425 else
c523303b 426 options.ciphers = (char *)-1;
5260325f 427 }
428 break;
b2552997 429 case 'm':
430 if (mac_valid(optarg))
431 options.macs = xstrdup(optarg);
432 else {
f5a1a01a 433 fprintf(stderr, "Unknown mac type '%s'\n",
434 optarg);
3aa43b24 435 exit(255);
b2552997 436 }
437 break;
5e96b616 438 case 'M':
9dfd96d6 439 if (options.control_master == SSHCTL_MASTER_YES)
440 options.control_master = SSHCTL_MASTER_ASK;
441 else
442 options.control_master = SSHCTL_MASTER_YES;
5e96b616 443 break;
5260325f 444 case 'p':
2d2a2c65 445 options.port = a2port(optarg);
446 if (options.port == 0) {
6031af8d 447 fprintf(stderr, "Bad port '%s'\n", optarg);
3aa43b24 448 exit(255);
6031af8d 449 }
5260325f 450 break;
5260325f 451 case 'l':
452 options.user = optarg;
453 break;
d2e3df16 454
455 case 'L':
3867aa0a 456 if (parse_forward(&fwd, optarg))
457 add_local_forward(&options, &fwd);
458 else {
f5a1a01a 459 fprintf(stderr,
3867aa0a 460 "Bad local forwarding specification '%s'\n",
f5a1a01a 461 optarg);
3aa43b24 462 exit(255);
5260325f 463 }
3867aa0a 464 break;
465
466 case 'R':
467 if (parse_forward(&fwd, optarg)) {
468 add_remote_forward(&options, &fwd);
469 } else {
f5a1a01a 470 fprintf(stderr,
3867aa0a 471 "Bad remote forwarding specification "
472 "'%s'\n", optarg);
3aa43b24 473 exit(255);
5260325f 474 }
5260325f 475 break;
0490e609 476
477 case 'D':
3867aa0a 478 cp = p = xstrdup(optarg);
479 memset(&fwd, '\0', sizeof(fwd));
480 fwd.connect_host = "socks";
481 if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
482 fprintf(stderr, "Bad dynamic forwarding "
483 "specification '%.100s'\n", optarg);
3aa43b24 484 exit(255);
3867aa0a 485 }
486 if (cp != NULL) {
487 fwd.listen_port = a2port(cp);
e8097dc9 488 fwd.listen_host =
489 cleanhostname(fwd.listen_host);
3867aa0a 490 } else {
491 fwd.listen_port = a2port(fwd.listen_host);
7c840747 492 fwd.listen_host = NULL;
3867aa0a 493 }
494
495 if (fwd.listen_port == 0) {
f5a1a01a 496 fprintf(stderr, "Bad dynamic port '%s'\n",
497 optarg);
3aa43b24 498 exit(255);
6031af8d 499 }
3867aa0a 500 add_local_forward(&options, &fwd);
501 xfree(p);
0490e609 502 break;
503
5260325f 504 case 'C':
505 options.compression = 1;
506 break;
8ce64345 507 case 'N':
508 no_shell_flag = 1;
509 no_tty_flag = 1;
510 break;
8ce64345 511 case 'T':
512 no_tty_flag = 1;
513 break;
5260325f 514 case 'o':
515 dummy = 1;
1aafd17a 516 line = xstrdup(optarg);
f5a1a01a 517 if (process_config_line(&options, host ? host : "",
1aafd17a 518 line, "command-line", 0, &dummy) != 0)
3aa43b24 519 exit(255);
1aafd17a 520 xfree(line);
5260325f 521 break;
ae810de7 522 case 's':
523 subsystem_flag = 1;
524 break;
5e96b616 525 case 'S':
526 if (options.control_path != NULL)
527 free(options.control_path);
528 options.control_path = xstrdup(optarg);
5e96b616 529 break;
3435f5a6 530 case 'b':
531 options.bind_address = optarg;
532 break;
e591b98a 533 case 'F':
534 config = optarg;
535 break;
5260325f 536 default:
537 usage();
538 }
539 }
540
f5a1a01a 541 ac -= optind;
542 av += optind;
543
544 if (ac > 0 && !host && **av != '-') {
15748b4d 545 if (strrchr(*av, '@')) {
f5a1a01a 546 p = xstrdup(*av);
15748b4d 547 cp = strrchr(p, '@');
f5a1a01a 548 if (cp == NULL || cp == p)
549 usage();
550 options.user = p;
551 *cp = '\0';
552 host = ++cp;
553 } else
554 host = *av;
978ebf99 555 if (ac > 1) {
556 optind = optreset = 1;
f5a1a01a 557 goto again;
558 }
978ebf99 559 ac--, av++;
f5a1a01a 560 }
561
5260325f 562 /* Check that we got a host name. */
563 if (!host)
8efc0c15 564 usage();
5260325f 565
22d89d24 566 SSLeay_add_all_algorithms();
fa08c86b 567 ERR_load_crypto_strings();
22d89d24 568
5260325f 569 /* Initialize the command to execute on remote host. */
570 buffer_init(&command);
571
aa3378df 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 */
f5a1a01a 577 if (!ac) {
5260325f 578 /* No command specified - execute shell on a tty. */
579 tty_flag = 1;
ae810de7 580 if (subsystem_flag) {
f5a1a01a 581 fprintf(stderr,
582 "You must specify a subsystem to invoke.\n");
ae810de7 583 usage();
584 }
5260325f 585 } else {
f5a1a01a 586 /* A command has been specified. Store it into the buffer. */
587 for (i = 0; i < ac; i++) {
588 if (i)
5260325f 589 buffer_append(&command, " ", 1);
590 buffer_append(&command, av[i], strlen(av[i]));
591 }
8efc0c15 592 }
5260325f 593
594 /* Cannot fork to background if no command. */
e8097dc9 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.");
5260325f 599
600 /* Allocate a tty by default if no command specified. */
601 if (buffer_len(&command) == 0)
602 tty_flag = 1;
603
343288b8 604 /* Force no tty */
0b6fbf03 605 if (no_tty_flag)
606 tty_flag = 0;
5260325f 607 /* Do not allocate a tty if stdin is not a tty. */
7697ac2b 608 if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
5260325f 609 if (tty_flag)
e8097dc9 610 logit("Pseudo-terminal will not be allocated because "
611 "stdin is not a terminal.");
5260325f 612 tty_flag = 0;
613 }
8ce64345 614
20244695 615 /*
616 * Initialize "log" output. Since we are the client all output
617 * actually goes to stderr.
618 */
e8097dc9 619 log_init(av[0],
620 options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
bf793210 621 SYSLOG_FACILITY_USER, !use_syslog);
5260325f 622
e591b98a 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) {
f7f14143 628 if (!read_config_file(config, host, &options, 0))
93111dfa 629 fatal("Can't open user config file %.100s: "
630 "%.100s", config, strerror(errno));
4f6e2ba9 631 } else {
e591b98a 632 snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
633 _PATH_SSH_USER_CONFFILE);
9f6cab4b 634 (void)read_config_file(buf, host, &options, 1);
e591b98a 635
f67792f2 636 /* Read systemwide configuration file after use config. */
f2107e97 637 (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
9f6cab4b 638 &options, 0);
e591b98a 639 }
5260325f 640
641 /* Fill configuration defaults. */
642 fill_default_options(&options);
643
1572b90f 644 channel_set_af(options.address_family);
645
5260325f 646 /* reinit */
bf793210 647 log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, !use_syslog);
5260325f 648
e339aa53 649 seed_rng();
650
5260325f 651 if (options.user == NULL)
652 options.user = xstrdup(pw->pw_name);
653
e9d0b573 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
5260325f 676 if (options.hostname != NULL)
677 host = options.hostname;
678
03c82656 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))
9555d258 683 *p = (char)tolower(*p);
03c82656 684 }
685
f78bde70 686 if (options.proxy_command != NULL &&
d0420e01 687 strcmp(options.proxy_command, "none") == 0) {
688 xfree(options.proxy_command);
f78bde70 689 options.proxy_command = NULL;
d0420e01 690 }
699255b5 691 if (options.control_path != NULL &&
d0420e01 692 strcmp(options.control_path, "none") == 0) {
693 xfree(options.control_path);
699255b5 694 options.control_path = NULL;
d0420e01 695 }
f78bde70 696
5e96b616 697 if (options.control_path != NULL) {
8cffe22a 698 char thishost[NI_MAXHOST];
f7b8224d 699
8cffe22a 700 if (gethostname(thishost, sizeof(thishost)) == -1)
f7b8224d 701 fatal("gethostname: %s", strerror(errno));
a980cbd7 702 snprintf(buf, sizeof(buf), "%d", options.port);
703 cp = tilde_expand_filename(options.control_path,
704 original_real_uid);
d0420e01 705 xfree(options.control_path);
a980cbd7 706 options.control_path = percent_expand(cp, "p", buf, "h", host,
8cffe22a 707 "r", options.user, "l", thishost, (char *)NULL);
a980cbd7 708 xfree(cp);
5e96b616 709 }
3bcced4c 710 if (muxclient_command != 0 && options.control_path == NULL)
19186c3d 711 fatal("No ControlPath specified for \"-O\" command");
9dfd96d6 712 if (options.control_path != NULL)
3bcced4c 713 muxclient(options.control_path);
5e96b616 714
71300a43 715 timeout_ms = options.connection_timeout * 1000;
716
6ffc9c88 717 /* Open a connection to the remote host. */
1572b90f 718 if (ssh_connect(host, &hostaddr, options.port,
71300a43 719 options.address_family, options.connection_attempts, &timeout_ms,
720 options.tcp_keep_alive,
d514c907 721#ifdef HAVE_CYGWIN
722 options.use_privileged_port,
723#else
3fb156df 724 original_effective_uid == 0 && options.use_privileged_port,
d514c907 725#endif
de60473e 726 options.proxy_command) != 0)
3aa43b24 727 exit(255);
5260325f 728
71300a43 729 if (timeout_ms > 0)
730 debug3("timeout: %d ms remain after connect", timeout_ms);
731
aa3378df 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.
78660ed4 737 * If we cannot access the private keys, load the public keys
738 * instead and try to execute the ssh-keysign helper instead.
aa3378df 739 */
8002af61 740 sensitive_data.nkeys = 0;
741 sensitive_data.keys = NULL;
39c00dc2 742 sensitive_data.external_keysign = 0;
b34bec32 743 if (options.rhosts_rsa_authentication ||
744 options.hostbased_authentication) {
8002af61 745 sensitive_data.nkeys = 3;
19e79961 746 sensitive_data.keys = xcalloc(sensitive_data.nkeys,
343288b8 747 sizeof(Key));
3fb156df 748
749 PRIV_START;
8002af61 750 sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
fc231518 751 _PATH_HOST_KEY_FILE, "", NULL, NULL);
8002af61 752 sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
fc231518 753 _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL);
8002af61 754 sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
fc231518 755 _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL);
3fb156df 756 PRIV_END;
39c00dc2 757
60cd0a97 758 if (options.hostbased_authentication == 1 &&
759 sensitive_data.keys[0] == NULL &&
39c00dc2 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 }
5260325f 768 }
aa3378df 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 */
6213295d 776 if (original_effective_uid == 0) {
777 PRIV_START;
778 permanently_set_uid(pw);
779 }
5260325f 780
aa3378df 781 /*
782 * Now that we are back to our own permissions, create ~/.ssh
7b9b0103 783 * directory if it doesn't already exist.
aa3378df 784 */
e8097dc9 785 snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir,
786 strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
5260325f 787 if (stat(buf, &st) < 0)
704b1659 788 if (mkdir(buf, 0700) < 0)
5260325f 789 error("Could not create directory '%.200s'.", buf);
790
fee56204 791 /* load options.identity_files */
792 load_public_identity_files();
793
794 /* Expand ~ in known host file names. */
795 /* XXX mem-leaks: */
fa08c86b 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);
5260325f 804
67b75437 805 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
806
e8097dc9 807 /* Log into the remote system. Never returns if the login fails. */
71300a43 808 ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr,
809 pw, timeout_ms);
8002af61 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 }
05b7537a 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 }
5260325f 833
8ce64345 834 exit_status = compat20 ? ssh_session2() : ssh_session();
835 packet_close();
6939bbd4 836
3bcced4c 837 if (options.control_path != NULL && muxserver_sock != -1)
5e96b616 838 unlink(options.control_path);
839
6939bbd4 840 /*
aff51935 841 * Send SIGHUP to proxy command if used. We don't wait() in
6939bbd4 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
8ce64345 847 return exit_status;
848}
849
72becb62 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 }
6c777090 867 if (++remote_forward_confirms_received == options.num_remote_forwards) {
72becb62 868 debug("All remote forwarding requests processed");
6c777090 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 }
72becb62 876}
877
396c147e 878static void
fa08c86b 879ssh_init_forwarding(void)
880{
02a024dd 881 int success = 0;
fa08c86b 882 int i;
02a024dd 883
fa08c86b 884 /* Initiate local TCP/IP port forwardings. */
885 for (i = 0; i < options.num_local_forwards; i++) {
3867aa0a 886 debug("Local connections to %.200s:%d forwarded to remote "
887 "address %.200s:%d",
f8cc7664 888 (options.local_forwards[i].listen_host == NULL) ?
889 (options.gateway_ports ? "*" : "LOCALHOST") :
3867aa0a 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);
5641aefa 894 success += channel_setup_local_fwd_listener(
3867aa0a 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,
fa08c86b 899 options.gateway_ports);
900 }
42ea6f5e 901 if (i > 0 && success != i && options.exit_on_forward_failure)
902 fatal("Could not request local forwarding.");
02a024dd 903 if (i > 0 && success == 0)
904 error("Could not request local forwarding.");
fa08c86b 905
906 /* Initiate remote TCP/IP port forwardings. */
907 for (i = 0; i < options.num_remote_forwards; i++) {
3867aa0a 908 debug("Remote connections from %.200s:%d forwarded to "
909 "local address %.200s:%d",
56964485 910 (options.remote_forwards[i].listen_host == NULL) ?
aa9bc1de 911 "LOCALHOST" : options.remote_forwards[i].listen_host,
3867aa0a 912 options.remote_forwards[i].listen_port,
913 options.remote_forwards[i].connect_host,
914 options.remote_forwards[i].connect_port);
42ea6f5e 915 if (channel_request_remote_forwarding(
3867aa0a 916 options.remote_forwards[i].listen_host,
917 options.remote_forwards[i].listen_port,
918 options.remote_forwards[i].connect_host,
42ea6f5e 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 }
72becb62 926 client_register_global_confirm(ssh_confirm_remote_forward,
927 &options.remote_forwards[i]);
fa08c86b 928 }
79303c5a 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 }
fa08c86b 940}
941
396c147e 942static void
fa08c86b 943check_agent_present(void)
944{
945 if (options.forward_agent) {
7b9b0103 946 /* Clear agent forwarding if we don't have an agent. */
8b10e20e 947 if (!ssh_agent_present())
fa08c86b 948 options.forward_agent = 0;
fa08c86b 949 }
950}
951
396c147e 952static int
8ce64345 953ssh_session(void)
954{
955 int type;
8ce64345 956 int interactive = 0;
957 int have_tty = 0;
958 struct winsize ws;
8ce64345 959 char *cp;
9bf083eb 960 const char *display;
8ce64345 961
5260325f 962 /* Enable compression if requested. */
963 if (options.compression) {
e8097dc9 964 debug("Requesting compression at level %d.",
965 options.compression_level);
5260325f 966
e8097dc9 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).");
5260325f 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();
54a5250f 977 type = packet_read();
5260325f 978 if (type == SSH_SMSG_SUCCESS)
979 packet_start_compression(options.compression_level);
980 else if (type == SSH_SMSG_FAILURE)
bbe88b6d 981 logit("Warning: Remote host refused compression.");
5260325f 982 else
e8097dc9 983 packet_disconnect("Protocol error waiting for "
984 "compression response.");
5260325f 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 = "";
449c5ba5 998 packet_put_cstring(cp);
5260325f 999
1000 /* Store window size in the packet. */
1001 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1002 memset(&ws, 0, sizeof(ws));
148de80c 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);
5260325f 1007
1008 /* Store tty modes in the packet. */
30dcc918 1009 tty_make_modes(fileno(stdin), NULL);
5260325f 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. */
54a5250f 1016 type = packet_read();
4fe2af09 1017 if (type == SSH_SMSG_SUCCESS) {
5260325f 1018 interactive = 1;
8ce64345 1019 have_tty = 1;
4fe2af09 1020 } else if (type == SSH_SMSG_FAILURE)
e8097dc9 1021 logit("Warning: Remote host failed or refused to "
1022 "allocate a pseudo tty.");
5260325f 1023 else
e8097dc9 1024 packet_disconnect("Protocol error waiting for pty "
1025 "request response.");
5260325f 1026 }
1027 /* Request X11 forwarding if enabled and DISPLAY is set. */
9bf083eb 1028 display = getenv("DISPLAY");
1029 if (options.forward_x11 && display != NULL) {
b48eeb07 1030 char *proto, *data;
0b242b12 1031 /* Get reasonable local authentication information. */
9bf083eb 1032 client_x11_get_proto(display, options.xauth_location,
1033 options.forward_x11_trusted, &proto, &data);
0b242b12 1034 /* Request forwarding with authentication spoofing. */
e8097dc9 1035 debug("Requesting X11 forwarding with authentication "
1036 "spoofing.");
9bf083eb 1037 x11_request_forwarding_with_spoofing(0, display, proto, data);
5260325f 1038
1039 /* Read response from the server. */
54a5250f 1040 type = packet_read();
5260325f 1041 if (type == SSH_SMSG_SUCCESS) {
5260325f 1042 interactive = 1;
0b242b12 1043 } else if (type == SSH_SMSG_FAILURE) {
bbe88b6d 1044 logit("Warning: Remote host denied X11 forwarding.");
0b242b12 1045 } else {
e8097dc9 1046 packet_disconnect("Protocol error waiting for X11 "
1047 "forwarding");
0b242b12 1048 }
5260325f 1049 }
1050 /* Tell the packet module whether this is an interactive session. */
b5c334cc 1051 packet_set_interactive(interactive);
5260325f 1052
1053 /* Request authentication agent forwarding if appropriate. */
fa08c86b 1054 check_agent_present();
1055
5260325f 1056 if (options.forward_agent) {
1057 debug("Requesting authentication agent forwarding.");
1058 auth_request_forwarding();
1059
1060 /* Read response from the server. */
54a5250f 1061 type = packet_read();
95500969 1062 packet_check_eom();
5260325f 1063 if (type != SSH_SMSG_SUCCESS)
bbe88b6d 1064 logit("Warning: Remote host denied authentication agent forwarding.");
5260325f 1065 }
8efc0c15 1066
fa08c86b 1067 /* Initiate port forwardings. */
1068 ssh_init_forwarding();
5260325f 1069
c80e7e5d 1070 /* Execute a local command */
1071 if (options.local_command != NULL &&
1072 options.permit_local_command)
1073 ssh_local_cmd(options.local_command);
1074
6c777090 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;
aa3378df 1083 if (daemon(1, 1) < 0)
1084 fatal("daemon() failed: %.200s", strerror(errno));
6c777090 1085 }
aa3378df 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 */
5260325f 1091 if (buffer_len(&command) > 0) {
1092 int len = buffer_len(&command);
1093 if (len > 900)
1094 len = 900;
e8097dc9 1095 debug("Sending command: %.*s", len,
1096 (u_char *)buffer_ptr(&command));
5260325f 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. */
4bf9c10e 1109 return client_loop(have_tty, tty_flag ?
1110 options.escape_char : SSH_ESCAPECHAR_NONE, 0);
8ce64345 1111}
5260325f 1112
5e96b616 1113/* request pty/x11/agent/tcpfwd/shell for channel */
1114static void
1115ssh_session2_setup(int id, void *arg)
1116{
1786be35 1117 extern char **environ;
9bf083eb 1118 const char *display;
5e96b616 1119 int interactive = tty_flag;
9bf083eb 1120
56964485 1121 display = getenv("DISPLAY");
9bf083eb 1122 if (options.forward_x11 && display != NULL) {
b48eeb07 1123 char *proto, *data;
0b242b12 1124 /* Get reasonable local authentication information. */
9bf083eb 1125 client_x11_get_proto(display, options.xauth_location,
1126 options.forward_x11_trusted, &proto, &data);
0b242b12 1127 /* Request forwarding with authentication spoofing. */
e8097dc9 1128 debug("Requesting X11 forwarding with authentication "
1129 "spoofing.");
9bf083eb 1130 x11_request_forwarding_with_spoofing(id, display, proto, data);
b5c334cc 1131 interactive = 1;
0b242b12 1132 /* XXX wait for reply */
1133 }
1134
fa08c86b 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
5e96b616 1142 client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
95d3c124 1143 NULL, fileno(stdin), &command, environ);
1f3bf5aa 1144
b5c334cc 1145 packet_set_interactive(interactive);
8ce64345 1146}
1147
bcdb96c2 1148/* open new channel for a session */
396c147e 1149static int
bcdb96c2 1150ssh_session2_open(void)
8ce64345 1151{
719fc62f 1152 Channel *c;
1153 int window, packetmax, in, out, err;
2e73a022 1154
14a9a859 1155 if (stdin_null_flag) {
5ca51e19 1156 in = open(_PATH_DEVNULL, O_RDONLY);
14a9a859 1157 } else {
1158 in = dup(STDIN_FILENO);
1159 }
2e73a022 1160 out = dup(STDOUT_FILENO);
1161 err = dup(STDERR_FILENO);
8ce64345 1162
1163 if (in < 0 || out < 0 || err < 0)
14a9a859 1164 fatal("dup() in/out/err failed");
8ce64345 1165
a22aff1f 1166 /* enable nonblocking unless tty */
1167 if (!isatty(in))
1168 set_nonblock(in);
1169 if (!isatty(out))
1170 set_nonblock(out);
1171 if (!isatty(err))
1172 set_nonblock(err);
1173
bcbf86ec 1174 window = CHAN_SES_WINDOW_DEFAULT;
1175 packetmax = CHAN_SES_PACKET_DEFAULT;
ea9700ba 1176 if (tty_flag) {
1177 window >>= 1;
1178 packetmax >>= 1;
8ce64345 1179 }
719fc62f 1180 c = channel_new(
8ce64345 1181 "session", SSH_CHANNEL_OPENING, in, out, err,
bcbf86ec 1182 window, packetmax, CHAN_EXTENDED_WRITE,
0d942eff 1183 "client-session", /*nonblock*/0);
8ce64345 1184
bcdb96c2 1185 debug3("ssh_session2_open: channel_new: %d", c->self);
eb0dd41f 1186
36e1f6a1 1187 channel_send_open(c->self);
bcdb96c2 1188 if (!no_shell_flag)
3593bdc0 1189 channel_register_open_confirm(c->self,
1190 ssh_session2_setup, NULL);
5260325f 1191
719fc62f 1192 return c->self;
eb0dd41f 1193}
1194
396c147e 1195static int
eb0dd41f 1196ssh_session2(void)
1197{
bcdb96c2 1198 int id = -1;
eb0dd41f 1199
1200 /* XXX should be pre-session */
1201 ssh_init_forwarding();
1202
bcdb96c2 1203 if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1204 id = ssh_session2_open();
eb0dd41f 1205
3f0444ca 1206 /* If we don't expect to open a new session, then disallow it */
72bd2fca 1207 if (options.control_master == SSHCTL_MASTER_NO &&
1208 (datafellows & SSH_NEW_OPENSSH)) {
3f0444ca 1209 debug("Requesting no-more-sessions@openssh.com");
1210 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1211 packet_put_cstring("no-more-sessions@openssh.com");
1212 packet_put_char(0);
1213 packet_send();
1214 }
1215
d20f3c9e 1216 /* Execute a local command */
1217 if (options.local_command != NULL &&
1218 options.permit_local_command)
1219 ssh_local_cmd(options.local_command);
1220
79303c5a 1221 /* Start listening for multiplex clients */
3bcced4c 1222 muxserver_listen();
79303c5a 1223
eb0dd41f 1224 /* If requested, let ssh continue in the background. */
6c777090 1225 if (fork_after_authentication_flag) {
1226 fork_after_authentication_flag = 0;
eb0dd41f 1227 if (daemon(1, 1) < 0)
1228 fatal("daemon() failed: %.200s", strerror(errno));
6c777090 1229 }
eb0dd41f 1230
4bf9c10e 1231 return client_loop(tty_flag, tty_flag ?
1232 options.escape_char : SSH_ESCAPECHAR_NONE, id);
8efc0c15 1233}
fa08c86b 1234
396c147e 1235static void
fee56204 1236load_public_identity_files(void)
1237{
8cffe22a 1238 char *filename, *cp, thishost[NI_MAXHOST];
a7082e17 1239 char *pwdir = NULL, *pwname = NULL;
2e23cde0 1240 int i = 0;
0659cace 1241 Key *public;
8cffe22a 1242 struct passwd *pw;
383546c4 1243#ifdef SMARTCARD
0659cace 1244 Key **keys;
1245
9ff6f66f 1246 if (options.smartcard_device != NULL &&
0659cace 1247 options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
d4f40d92 1248 (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
0659cace 1249 int count = 0;
1250 for (i = 0; keys[i] != NULL; i++) {
1251 count++;
e8097dc9 1252 memmove(&options.identity_files[1],
1253 &options.identity_files[0],
0659cace 1254 sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
e8097dc9 1255 memmove(&options.identity_keys[1],
1256 &options.identity_keys[0],
0659cace 1257 sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1258 options.num_identity_files++;
1259 options.identity_keys[0] = keys[i];
244e796f 1260 options.identity_files[0] = sc_get_key_label(keys[i]);
0659cace 1261 }
1c2deed1 1262 if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1263 options.num_identity_files = SSH_MAX_IDENTITY_FILES;
0659cace 1264 i = count;
1265 xfree(keys);
383546c4 1266 }
3e984472 1267#endif /* SMARTCARD */
8cffe22a 1268 if ((pw = getpwuid(original_real_uid)) == NULL)
1269 fatal("load_public_identity_files: getpwuid failed");
42f4b33f 1270 pwname = xstrdup(pw->pw_name);
1271 pwdir = xstrdup(pw->pw_dir);
8cffe22a 1272 if (gethostname(thishost, sizeof(thishost)) == -1)
1273 fatal("load_public_identity_files: gethostname: %s",
1274 strerror(errno));
2e23cde0 1275 for (; i < options.num_identity_files; i++) {
8cffe22a 1276 cp = tilde_expand_filename(options.identity_files[i],
2e23cde0 1277 original_real_uid);
a7082e17 1278 filename = percent_expand(cp, "d", pwdir,
1279 "u", pwname, "l", thishost, "h", host,
8cffe22a 1280 "r", options.user, (char *)NULL);
1281 xfree(cp);
2e23cde0 1282 public = key_load_public(filename, NULL);
1283 debug("identity file %s type %d", filename,
1284 public ? public->type : -1);
1285 xfree(options.identity_files[i]);
1286 options.identity_files[i] = filename;
1287 options.identity_keys[i] = public;
1288 }
a7082e17 1289 bzero(pwname, strlen(pwname));
42f4b33f 1290 xfree(pwname);
a7082e17 1291 bzero(pwdir, strlen(pwdir));
42f4b33f 1292 xfree(pwdir);
fee56204 1293}
This page took 1.552242 seconds and 5 git commands to generate.