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