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