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