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