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