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