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