]>
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" | |
a4f24bf8 | 43 | RCSID("$OpenBSD: ssh.c,v 1.256 2005/12/08 18:34:11 reyk 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" |
80e29ee6 | 161 | " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n" |
8cf98c65 | 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" |
d20f3c9e | 165 | " [-w tunnel:tunnel] [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, | |
d20f3c9e | 247 | "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNO:PR:S:TVw:XY")) != -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; |
d20f3c9e | 343 | case 'w': |
a4f24bf8 | 344 | if (options.tun_open == -1) |
345 | options.tun_open = SSH_TUNMODE_DEFAULT; | |
d20f3c9e | 346 | options.tun_local = a2tun(optarg, &options.tun_remote); |
a4f24bf8 | 347 | if (options.tun_local == SSH_TUNID_ERR) { |
d20f3c9e | 348 | fprintf(stderr, "Bad tun device '%s'\n", optarg); |
349 | exit(1); | |
350 | } | |
351 | break; | |
5260325f | 352 | case 'q': |
353 | options.log_level = SYSLOG_LEVEL_QUIET; | |
354 | break; | |
5260325f | 355 | case 'e': |
356 | if (optarg[0] == '^' && optarg[2] == 0 && | |
f5a1a01a | 357 | (u_char) optarg[1] >= 64 && |
358 | (u_char) optarg[1] < 128) | |
1e3b8b07 | 359 | options.escape_char = (u_char) optarg[1] & 31; |
5260325f | 360 | else if (strlen(optarg) == 1) |
1e3b8b07 | 361 | options.escape_char = (u_char) optarg[0]; |
5260325f | 362 | else if (strcmp(optarg, "none") == 0) |
4bf9c10e | 363 | options.escape_char = SSH_ESCAPECHAR_NONE; |
5260325f | 364 | else { |
f5a1a01a | 365 | fprintf(stderr, "Bad escape character '%s'.\n", |
366 | optarg); | |
5260325f | 367 | exit(1); |
368 | } | |
369 | break; | |
5260325f | 370 | case 'c': |
a306f2dd | 371 | if (ciphers_valid(optarg)) { |
372 | /* SSH2 only */ | |
373 | options.ciphers = xstrdup(optarg); | |
d77347cc | 374 | options.cipher = SSH_CIPHER_INVALID; |
a306f2dd | 375 | } else { |
376 | /* SSH1 only */ | |
c523303b | 377 | options.cipher = cipher_number(optarg); |
378 | if (options.cipher == -1) { | |
f5a1a01a | 379 | fprintf(stderr, |
380 | "Unknown cipher type '%s'\n", | |
381 | optarg); | |
a306f2dd | 382 | exit(1); |
383 | } | |
f5a1a01a | 384 | if (options.cipher == SSH_CIPHER_3DES) |
c523303b | 385 | options.ciphers = "3des-cbc"; |
f5a1a01a | 386 | else if (options.cipher == SSH_CIPHER_BLOWFISH) |
c523303b | 387 | options.ciphers = "blowfish-cbc"; |
f5a1a01a | 388 | else |
c523303b | 389 | options.ciphers = (char *)-1; |
5260325f | 390 | } |
391 | break; | |
b2552997 | 392 | case 'm': |
393 | if (mac_valid(optarg)) | |
394 | options.macs = xstrdup(optarg); | |
395 | else { | |
f5a1a01a | 396 | fprintf(stderr, "Unknown mac type '%s'\n", |
397 | optarg); | |
b2552997 | 398 | exit(1); |
399 | } | |
400 | break; | |
5e96b616 | 401 | case 'M': |
9dfd96d6 | 402 | if (options.control_master == SSHCTL_MASTER_YES) |
403 | options.control_master = SSHCTL_MASTER_ASK; | |
404 | else | |
405 | options.control_master = SSHCTL_MASTER_YES; | |
5e96b616 | 406 | break; |
5260325f | 407 | case 'p': |
2d2a2c65 | 408 | options.port = a2port(optarg); |
409 | if (options.port == 0) { | |
6031af8d | 410 | fprintf(stderr, "Bad port '%s'\n", optarg); |
411 | exit(1); | |
412 | } | |
5260325f | 413 | break; |
5260325f | 414 | case 'l': |
415 | options.user = optarg; | |
416 | break; | |
d2e3df16 | 417 | |
418 | case 'L': | |
3867aa0a | 419 | if (parse_forward(&fwd, optarg)) |
420 | add_local_forward(&options, &fwd); | |
421 | else { | |
f5a1a01a | 422 | fprintf(stderr, |
3867aa0a | 423 | "Bad local forwarding specification '%s'\n", |
f5a1a01a | 424 | optarg); |
3867aa0a | 425 | exit(1); |
5260325f | 426 | } |
3867aa0a | 427 | break; |
428 | ||
429 | case 'R': | |
430 | if (parse_forward(&fwd, optarg)) { | |
431 | add_remote_forward(&options, &fwd); | |
432 | } else { | |
f5a1a01a | 433 | fprintf(stderr, |
3867aa0a | 434 | "Bad remote forwarding specification " |
435 | "'%s'\n", optarg); | |
d2e3df16 | 436 | exit(1); |
5260325f | 437 | } |
5260325f | 438 | break; |
0490e609 | 439 | |
440 | case 'D': | |
3867aa0a | 441 | cp = p = xstrdup(optarg); |
442 | memset(&fwd, '\0', sizeof(fwd)); | |
443 | fwd.connect_host = "socks"; | |
444 | if ((fwd.listen_host = hpdelim(&cp)) == NULL) { | |
445 | fprintf(stderr, "Bad dynamic forwarding " | |
446 | "specification '%.100s'\n", optarg); | |
447 | exit(1); | |
448 | } | |
449 | if (cp != NULL) { | |
450 | fwd.listen_port = a2port(cp); | |
451 | fwd.listen_host = cleanhostname(fwd.listen_host); | |
452 | } else { | |
453 | fwd.listen_port = a2port(fwd.listen_host); | |
7c840747 | 454 | fwd.listen_host = NULL; |
3867aa0a | 455 | } |
456 | ||
457 | if (fwd.listen_port == 0) { | |
f5a1a01a | 458 | fprintf(stderr, "Bad dynamic port '%s'\n", |
459 | optarg); | |
6031af8d | 460 | exit(1); |
461 | } | |
3867aa0a | 462 | add_local_forward(&options, &fwd); |
463 | xfree(p); | |
0490e609 | 464 | break; |
465 | ||
5260325f | 466 | case 'C': |
467 | options.compression = 1; | |
468 | break; | |
8ce64345 | 469 | case 'N': |
470 | no_shell_flag = 1; | |
471 | no_tty_flag = 1; | |
472 | break; | |
8ce64345 | 473 | case 'T': |
474 | no_tty_flag = 1; | |
475 | break; | |
5260325f | 476 | case 'o': |
477 | dummy = 1; | |
1aafd17a | 478 | line = xstrdup(optarg); |
f5a1a01a | 479 | if (process_config_line(&options, host ? host : "", |
1aafd17a | 480 | line, "command-line", 0, &dummy) != 0) |
5260325f | 481 | exit(1); |
1aafd17a | 482 | xfree(line); |
5260325f | 483 | break; |
ae810de7 | 484 | case 's': |
485 | subsystem_flag = 1; | |
486 | break; | |
5e96b616 | 487 | case 'S': |
488 | if (options.control_path != NULL) | |
489 | free(options.control_path); | |
490 | options.control_path = xstrdup(optarg); | |
5e96b616 | 491 | break; |
3435f5a6 | 492 | case 'b': |
493 | options.bind_address = optarg; | |
494 | break; | |
e591b98a | 495 | case 'F': |
496 | config = optarg; | |
497 | break; | |
5260325f | 498 | default: |
499 | usage(); | |
500 | } | |
501 | } | |
502 | ||
f5a1a01a | 503 | ac -= optind; |
504 | av += optind; | |
505 | ||
506 | if (ac > 0 && !host && **av != '-') { | |
15748b4d | 507 | if (strrchr(*av, '@')) { |
f5a1a01a | 508 | p = xstrdup(*av); |
15748b4d | 509 | cp = strrchr(p, '@'); |
f5a1a01a | 510 | if (cp == NULL || cp == p) |
511 | usage(); | |
512 | options.user = p; | |
513 | *cp = '\0'; | |
514 | host = ++cp; | |
515 | } else | |
516 | host = *av; | |
978ebf99 | 517 | if (ac > 1) { |
518 | optind = optreset = 1; | |
f5a1a01a | 519 | goto again; |
520 | } | |
978ebf99 | 521 | ac--, av++; |
f5a1a01a | 522 | } |
523 | ||
5260325f | 524 | /* Check that we got a host name. */ |
525 | if (!host) | |
8efc0c15 | 526 | usage(); |
5260325f | 527 | |
22d89d24 | 528 | SSLeay_add_all_algorithms(); |
fa08c86b | 529 | ERR_load_crypto_strings(); |
22d89d24 | 530 | |
5260325f | 531 | /* Initialize the command to execute on remote host. */ |
532 | buffer_init(&command); | |
533 | ||
aa3378df | 534 | /* |
535 | * Save the command to execute on the remote host in a buffer. There | |
536 | * is no limit on the length of the command, except by the maximum | |
537 | * packet size. Also sets the tty flag if there is no command. | |
538 | */ | |
f5a1a01a | 539 | if (!ac) { |
5260325f | 540 | /* No command specified - execute shell on a tty. */ |
541 | tty_flag = 1; | |
ae810de7 | 542 | if (subsystem_flag) { |
f5a1a01a | 543 | fprintf(stderr, |
544 | "You must specify a subsystem to invoke.\n"); | |
ae810de7 | 545 | usage(); |
546 | } | |
5260325f | 547 | } else { |
f5a1a01a | 548 | /* A command has been specified. Store it into the buffer. */ |
549 | for (i = 0; i < ac; i++) { | |
550 | if (i) | |
5260325f | 551 | buffer_append(&command, " ", 1); |
552 | buffer_append(&command, av[i], strlen(av[i])); | |
553 | } | |
8efc0c15 | 554 | } |
5260325f | 555 | |
556 | /* Cannot fork to background if no command. */ | |
14a9a859 | 557 | if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag) |
5260325f | 558 | fatal("Cannot fork into background without a command to execute."); |
559 | ||
560 | /* Allocate a tty by default if no command specified. */ | |
561 | if (buffer_len(&command) == 0) | |
562 | tty_flag = 1; | |
563 | ||
343288b8 | 564 | /* Force no tty */ |
0b6fbf03 | 565 | if (no_tty_flag) |
566 | tty_flag = 0; | |
5260325f | 567 | /* Do not allocate a tty if stdin is not a tty. */ |
7697ac2b | 568 | if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) { |
5260325f | 569 | if (tty_flag) |
bbe88b6d | 570 | logit("Pseudo-terminal will not be allocated because stdin is not a terminal."); |
5260325f | 571 | tty_flag = 0; |
572 | } | |
8ce64345 | 573 | |
20244695 | 574 | /* |
575 | * Initialize "log" output. Since we are the client all output | |
576 | * actually goes to stderr. | |
577 | */ | |
cc44f691 | 578 | log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, |
579 | SYSLOG_FACILITY_USER, 1); | |
5260325f | 580 | |
e591b98a | 581 | /* |
582 | * Read per-user configuration file. Ignore the system wide config | |
583 | * file if the user specifies a config file on the command line. | |
584 | */ | |
585 | if (config != NULL) { | |
f7f14143 | 586 | if (!read_config_file(config, host, &options, 0)) |
93111dfa | 587 | fatal("Can't open user config file %.100s: " |
588 | "%.100s", config, strerror(errno)); | |
e591b98a | 589 | } else { |
590 | snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, | |
591 | _PATH_SSH_USER_CONFFILE); | |
9f6cab4b | 592 | (void)read_config_file(buf, host, &options, 1); |
e591b98a | 593 | |
f67792f2 | 594 | /* Read systemwide configuration file after use config. */ |
f2107e97 | 595 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, |
9f6cab4b | 596 | &options, 0); |
e591b98a | 597 | } |
5260325f | 598 | |
599 | /* Fill configuration defaults. */ | |
600 | fill_default_options(&options); | |
601 | ||
1572b90f | 602 | channel_set_af(options.address_family); |
603 | ||
5260325f | 604 | /* reinit */ |
20244695 | 605 | log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1); |
5260325f | 606 | |
e339aa53 | 607 | seed_rng(); |
608 | ||
5260325f | 609 | if (options.user == NULL) |
610 | options.user = xstrdup(pw->pw_name); | |
611 | ||
612 | if (options.hostname != NULL) | |
613 | host = options.hostname; | |
614 | ||
03c82656 | 615 | /* force lowercase for hostkey matching */ |
616 | if (options.host_key_alias != NULL) { | |
617 | for (p = options.host_key_alias; *p; p++) | |
618 | if (isupper(*p)) | |
619 | *p = tolower(*p); | |
620 | } | |
621 | ||
de574442 | 622 | /* Get default port if port has not been set. */ |
623 | if (options.port == 0) { | |
624 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); | |
625 | options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; | |
626 | } | |
627 | ||
f78bde70 | 628 | if (options.proxy_command != NULL && |
629 | strcmp(options.proxy_command, "none") == 0) | |
630 | options.proxy_command = NULL; | |
699255b5 | 631 | if (options.control_path != NULL && |
632 | strcmp(options.control_path, "none") == 0) | |
633 | options.control_path = NULL; | |
f78bde70 | 634 | |
5e96b616 | 635 | if (options.control_path != NULL) { |
a980cbd7 | 636 | snprintf(buf, sizeof(buf), "%d", options.port); |
637 | cp = tilde_expand_filename(options.control_path, | |
638 | original_real_uid); | |
639 | options.control_path = percent_expand(cp, "p", buf, "h", host, | |
640 | "r", options.user, (char *)NULL); | |
641 | xfree(cp); | |
5e96b616 | 642 | } |
19186c3d | 643 | if (mux_command != 0 && options.control_path == NULL) |
644 | fatal("No ControlPath specified for \"-O\" command"); | |
9dfd96d6 | 645 | if (options.control_path != NULL) |
cc8ca1e6 | 646 | control_client(options.control_path); |
5e96b616 | 647 | |
6ffc9c88 | 648 | /* Open a connection to the remote host. */ |
1572b90f | 649 | if (ssh_connect(host, &hostaddr, options.port, |
650 | options.address_family, options.connection_attempts, | |
d514c907 | 651 | #ifdef HAVE_CYGWIN |
652 | options.use_privileged_port, | |
653 | #else | |
3fb156df | 654 | original_effective_uid == 0 && options.use_privileged_port, |
d514c907 | 655 | #endif |
de60473e | 656 | options.proxy_command) != 0) |
b34bec32 | 657 | exit(1); |
5260325f | 658 | |
aa3378df | 659 | /* |
660 | * If we successfully made the connection, load the host private key | |
661 | * in case we will need it later for combined rsa-rhosts | |
662 | * authentication. This must be done before releasing extra | |
663 | * privileges, because the file is only readable by root. | |
78660ed4 | 664 | * If we cannot access the private keys, load the public keys |
665 | * instead and try to execute the ssh-keysign helper instead. | |
aa3378df | 666 | */ |
8002af61 | 667 | sensitive_data.nkeys = 0; |
668 | sensitive_data.keys = NULL; | |
39c00dc2 | 669 | sensitive_data.external_keysign = 0; |
b34bec32 | 670 | if (options.rhosts_rsa_authentication || |
671 | options.hostbased_authentication) { | |
8002af61 | 672 | sensitive_data.nkeys = 3; |
343288b8 | 673 | sensitive_data.keys = xmalloc(sensitive_data.nkeys * |
674 | sizeof(Key)); | |
3fb156df | 675 | |
676 | PRIV_START; | |
8002af61 | 677 | sensitive_data.keys[0] = key_load_private_type(KEY_RSA1, |
aeaa3d9e | 678 | _PATH_HOST_KEY_FILE, "", NULL); |
8002af61 | 679 | sensitive_data.keys[1] = key_load_private_type(KEY_DSA, |
680 | _PATH_HOST_DSA_KEY_FILE, "", NULL); | |
681 | sensitive_data.keys[2] = key_load_private_type(KEY_RSA, | |
682 | _PATH_HOST_RSA_KEY_FILE, "", NULL); | |
3fb156df | 683 | PRIV_END; |
39c00dc2 | 684 | |
60cd0a97 | 685 | if (options.hostbased_authentication == 1 && |
686 | sensitive_data.keys[0] == NULL && | |
39c00dc2 | 687 | sensitive_data.keys[1] == NULL && |
688 | sensitive_data.keys[2] == NULL) { | |
689 | sensitive_data.keys[1] = key_load_public( | |
690 | _PATH_HOST_DSA_KEY_FILE, NULL); | |
691 | sensitive_data.keys[2] = key_load_public( | |
692 | _PATH_HOST_RSA_KEY_FILE, NULL); | |
693 | sensitive_data.external_keysign = 1; | |
694 | } | |
5260325f | 695 | } |
aa3378df | 696 | /* |
697 | * Get rid of any extra privileges that we may have. We will no | |
698 | * longer need them. Also, extra privileges could make it very hard | |
699 | * to read identity files and other non-world-readable files from the | |
700 | * user's home directory if it happens to be on a NFS volume where | |
701 | * root is mapped to nobody. | |
702 | */ | |
6213295d | 703 | if (original_effective_uid == 0) { |
704 | PRIV_START; | |
705 | permanently_set_uid(pw); | |
706 | } | |
5260325f | 707 | |
aa3378df | 708 | /* |
709 | * Now that we are back to our own permissions, create ~/.ssh | |
7b9b0103 | 710 | * directory if it doesn't already exist. |
aa3378df | 711 | */ |
c3abff07 | 712 | snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); |
5260325f | 713 | if (stat(buf, &st) < 0) |
704b1659 | 714 | if (mkdir(buf, 0700) < 0) |
5260325f | 715 | error("Could not create directory '%.200s'.", buf); |
716 | ||
fee56204 | 717 | /* load options.identity_files */ |
718 | load_public_identity_files(); | |
719 | ||
720 | /* Expand ~ in known host file names. */ | |
721 | /* XXX mem-leaks: */ | |
fa08c86b | 722 | options.system_hostfile = |
723 | tilde_expand_filename(options.system_hostfile, original_real_uid); | |
724 | options.user_hostfile = | |
725 | tilde_expand_filename(options.user_hostfile, original_real_uid); | |
726 | options.system_hostfile2 = | |
727 | tilde_expand_filename(options.system_hostfile2, original_real_uid); | |
728 | options.user_hostfile2 = | |
729 | tilde_expand_filename(options.user_hostfile2, original_real_uid); | |
5260325f | 730 | |
67b75437 | 731 | signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ |
732 | ||
5260325f | 733 | /* Log into the remote system. This never returns if the login fails. */ |
39c00dc2 | 734 | ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw); |
8002af61 | 735 | |
736 | /* We no longer need the private host keys. Clear them now. */ | |
737 | if (sensitive_data.nkeys != 0) { | |
738 | for (i = 0; i < sensitive_data.nkeys; i++) { | |
739 | if (sensitive_data.keys[i] != NULL) { | |
740 | /* Destroys contents safely */ | |
741 | debug3("clear hostkey %d", i); | |
742 | key_free(sensitive_data.keys[i]); | |
743 | sensitive_data.keys[i] = NULL; | |
744 | } | |
745 | } | |
746 | xfree(sensitive_data.keys); | |
747 | } | |
05b7537a | 748 | for (i = 0; i < options.num_identity_files; i++) { |
749 | if (options.identity_files[i]) { | |
750 | xfree(options.identity_files[i]); | |
751 | options.identity_files[i] = NULL; | |
752 | } | |
753 | if (options.identity_keys[i]) { | |
754 | key_free(options.identity_keys[i]); | |
755 | options.identity_keys[i] = NULL; | |
756 | } | |
757 | } | |
5260325f | 758 | |
8ce64345 | 759 | exit_status = compat20 ? ssh_session2() : ssh_session(); |
760 | packet_close(); | |
6939bbd4 | 761 | |
5e96b616 | 762 | if (options.control_path != NULL && control_fd != -1) |
763 | unlink(options.control_path); | |
764 | ||
6939bbd4 | 765 | /* |
aff51935 | 766 | * Send SIGHUP to proxy command if used. We don't wait() in |
6939bbd4 | 767 | * case it hangs and instead rely on init to reap the child |
768 | */ | |
769 | if (proxy_command_pid > 1) | |
770 | kill(proxy_command_pid, SIGHUP); | |
771 | ||
8ce64345 | 772 | return exit_status; |
773 | } | |
774 | ||
396c147e | 775 | static void |
fa08c86b | 776 | ssh_init_forwarding(void) |
777 | { | |
02a024dd | 778 | int success = 0; |
fa08c86b | 779 | int i; |
02a024dd | 780 | |
fa08c86b | 781 | /* Initiate local TCP/IP port forwardings. */ |
782 | for (i = 0; i < options.num_local_forwards; i++) { | |
3867aa0a | 783 | debug("Local connections to %.200s:%d forwarded to remote " |
784 | "address %.200s:%d", | |
f8cc7664 | 785 | (options.local_forwards[i].listen_host == NULL) ? |
786 | (options.gateway_ports ? "*" : "LOCALHOST") : | |
3867aa0a | 787 | options.local_forwards[i].listen_host, |
788 | options.local_forwards[i].listen_port, | |
789 | options.local_forwards[i].connect_host, | |
790 | options.local_forwards[i].connect_port); | |
5641aefa | 791 | success += channel_setup_local_fwd_listener( |
3867aa0a | 792 | options.local_forwards[i].listen_host, |
793 | options.local_forwards[i].listen_port, | |
794 | options.local_forwards[i].connect_host, | |
795 | options.local_forwards[i].connect_port, | |
fa08c86b | 796 | options.gateway_ports); |
797 | } | |
02a024dd | 798 | if (i > 0 && success == 0) |
799 | error("Could not request local forwarding."); | |
fa08c86b | 800 | |
801 | /* Initiate remote TCP/IP port forwardings. */ | |
802 | for (i = 0; i < options.num_remote_forwards; i++) { | |
3867aa0a | 803 | debug("Remote connections from %.200s:%d forwarded to " |
804 | "local address %.200s:%d", | |
56964485 | 805 | (options.remote_forwards[i].listen_host == NULL) ? |
aa9bc1de | 806 | "LOCALHOST" : options.remote_forwards[i].listen_host, |
3867aa0a | 807 | options.remote_forwards[i].listen_port, |
808 | options.remote_forwards[i].connect_host, | |
809 | options.remote_forwards[i].connect_port); | |
fa08c86b | 810 | channel_request_remote_forwarding( |
3867aa0a | 811 | options.remote_forwards[i].listen_host, |
812 | options.remote_forwards[i].listen_port, | |
813 | options.remote_forwards[i].connect_host, | |
814 | options.remote_forwards[i].connect_port); | |
fa08c86b | 815 | } |
816 | } | |
817 | ||
396c147e | 818 | static void |
fa08c86b | 819 | check_agent_present(void) |
820 | { | |
821 | if (options.forward_agent) { | |
7b9b0103 | 822 | /* Clear agent forwarding if we don't have an agent. */ |
8b10e20e | 823 | if (!ssh_agent_present()) |
fa08c86b | 824 | options.forward_agent = 0; |
fa08c86b | 825 | } |
826 | } | |
827 | ||
396c147e | 828 | static int |
8ce64345 | 829 | ssh_session(void) |
830 | { | |
831 | int type; | |
8ce64345 | 832 | int interactive = 0; |
833 | int have_tty = 0; | |
834 | struct winsize ws; | |
8ce64345 | 835 | char *cp; |
9bf083eb | 836 | const char *display; |
8ce64345 | 837 | |
5260325f | 838 | /* Enable compression if requested. */ |
839 | if (options.compression) { | |
840 | debug("Requesting compression at level %d.", options.compression_level); | |
841 | ||
842 | if (options.compression_level < 1 || options.compression_level > 9) | |
843 | fatal("Compression level must be from 1 (fast) to 9 (slow, best)."); | |
844 | ||
845 | /* Send the request. */ | |
846 | packet_start(SSH_CMSG_REQUEST_COMPRESSION); | |
847 | packet_put_int(options.compression_level); | |
848 | packet_send(); | |
849 | packet_write_wait(); | |
54a5250f | 850 | type = packet_read(); |
5260325f | 851 | if (type == SSH_SMSG_SUCCESS) |
852 | packet_start_compression(options.compression_level); | |
853 | else if (type == SSH_SMSG_FAILURE) | |
bbe88b6d | 854 | logit("Warning: Remote host refused compression."); |
5260325f | 855 | else |
856 | packet_disconnect("Protocol error waiting for compression response."); | |
857 | } | |
858 | /* Allocate a pseudo tty if appropriate. */ | |
859 | if (tty_flag) { | |
860 | debug("Requesting pty."); | |
861 | ||
862 | /* Start the packet. */ | |
863 | packet_start(SSH_CMSG_REQUEST_PTY); | |
864 | ||
865 | /* Store TERM in the packet. There is no limit on the | |
866 | length of the string. */ | |
867 | cp = getenv("TERM"); | |
868 | if (!cp) | |
869 | cp = ""; | |
449c5ba5 | 870 | packet_put_cstring(cp); |
5260325f | 871 | |
872 | /* Store window size in the packet. */ | |
873 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) | |
874 | memset(&ws, 0, sizeof(ws)); | |
875 | packet_put_int(ws.ws_row); | |
876 | packet_put_int(ws.ws_col); | |
877 | packet_put_int(ws.ws_xpixel); | |
878 | packet_put_int(ws.ws_ypixel); | |
879 | ||
880 | /* Store tty modes in the packet. */ | |
30dcc918 | 881 | tty_make_modes(fileno(stdin), NULL); |
5260325f | 882 | |
883 | /* Send the packet, and wait for it to leave. */ | |
884 | packet_send(); | |
885 | packet_write_wait(); | |
886 | ||
887 | /* Read response from the server. */ | |
54a5250f | 888 | type = packet_read(); |
4fe2af09 | 889 | if (type == SSH_SMSG_SUCCESS) { |
5260325f | 890 | interactive = 1; |
8ce64345 | 891 | have_tty = 1; |
4fe2af09 | 892 | } else if (type == SSH_SMSG_FAILURE) |
bbe88b6d | 893 | logit("Warning: Remote host failed or refused to allocate a pseudo tty."); |
5260325f | 894 | else |
895 | packet_disconnect("Protocol error waiting for pty request response."); | |
896 | } | |
897 | /* Request X11 forwarding if enabled and DISPLAY is set. */ | |
9bf083eb | 898 | display = getenv("DISPLAY"); |
899 | if (options.forward_x11 && display != NULL) { | |
b48eeb07 | 900 | char *proto, *data; |
0b242b12 | 901 | /* Get reasonable local authentication information. */ |
9bf083eb | 902 | client_x11_get_proto(display, options.xauth_location, |
903 | options.forward_x11_trusted, &proto, &data); | |
0b242b12 | 904 | /* Request forwarding with authentication spoofing. */ |
5260325f | 905 | debug("Requesting X11 forwarding with authentication spoofing."); |
9bf083eb | 906 | x11_request_forwarding_with_spoofing(0, display, proto, data); |
5260325f | 907 | |
908 | /* Read response from the server. */ | |
54a5250f | 909 | type = packet_read(); |
5260325f | 910 | if (type == SSH_SMSG_SUCCESS) { |
5260325f | 911 | interactive = 1; |
0b242b12 | 912 | } else if (type == SSH_SMSG_FAILURE) { |
bbe88b6d | 913 | logit("Warning: Remote host denied X11 forwarding."); |
0b242b12 | 914 | } else { |
5260325f | 915 | packet_disconnect("Protocol error waiting for X11 forwarding"); |
0b242b12 | 916 | } |
5260325f | 917 | } |
918 | /* Tell the packet module whether this is an interactive session. */ | |
b5c334cc | 919 | packet_set_interactive(interactive); |
5260325f | 920 | |
921 | /* Request authentication agent forwarding if appropriate. */ | |
fa08c86b | 922 | check_agent_present(); |
923 | ||
5260325f | 924 | if (options.forward_agent) { |
925 | debug("Requesting authentication agent forwarding."); | |
926 | auth_request_forwarding(); | |
927 | ||
928 | /* Read response from the server. */ | |
54a5250f | 929 | type = packet_read(); |
95500969 | 930 | packet_check_eom(); |
5260325f | 931 | if (type != SSH_SMSG_SUCCESS) |
bbe88b6d | 932 | logit("Warning: Remote host denied authentication agent forwarding."); |
5260325f | 933 | } |
8efc0c15 | 934 | |
fa08c86b | 935 | /* Initiate port forwardings. */ |
936 | ssh_init_forwarding(); | |
5260325f | 937 | |
aa3378df | 938 | /* If requested, let ssh continue in the background. */ |
6ae2364d | 939 | if (fork_after_authentication_flag) |
aa3378df | 940 | if (daemon(1, 1) < 0) |
941 | fatal("daemon() failed: %.200s", strerror(errno)); | |
942 | ||
943 | /* | |
944 | * If a command was specified on the command line, execute the | |
945 | * command now. Otherwise request the server to start a shell. | |
946 | */ | |
5260325f | 947 | if (buffer_len(&command) > 0) { |
948 | int len = buffer_len(&command); | |
949 | if (len > 900) | |
950 | len = 900; | |
6c0fa2b1 | 951 | debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command)); |
5260325f | 952 | packet_start(SSH_CMSG_EXEC_CMD); |
953 | packet_put_string(buffer_ptr(&command), buffer_len(&command)); | |
954 | packet_send(); | |
955 | packet_write_wait(); | |
956 | } else { | |
957 | debug("Requesting shell."); | |
958 | packet_start(SSH_CMSG_EXEC_SHELL); | |
959 | packet_send(); | |
960 | packet_write_wait(); | |
961 | } | |
962 | ||
963 | /* Enter the interactive session. */ | |
4bf9c10e | 964 | return client_loop(have_tty, tty_flag ? |
965 | options.escape_char : SSH_ESCAPECHAR_NONE, 0); | |
8ce64345 | 966 | } |
5260325f | 967 | |
396c147e | 968 | static void |
5e96b616 | 969 | ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt) |
1f3bf5aa | 970 | { |
971 | int id, len; | |
972 | ||
973 | id = packet_get_int(); | |
974 | len = buffer_len(&command); | |
ce91d6f8 | 975 | if (len > 900) |
976 | len = 900; | |
95500969 | 977 | packet_check_eom(); |
1f3bf5aa | 978 | if (type == SSH2_MSG_CHANNEL_FAILURE) |
979 | fatal("Request for subsystem '%.*s' failed on channel %d", | |
6c0fa2b1 | 980 | len, (u_char *)buffer_ptr(&command), id); |
1f3bf5aa | 981 | } |
982 | ||
e0ae8728 | 983 | void |
5d8d32a3 | 984 | client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt) |
e0ae8728 | 985 | { |
986 | int i; | |
987 | ||
988 | i = client_global_request_id++; | |
5d8d32a3 | 989 | if (i >= options.num_remote_forwards) |
e0ae8728 | 990 | return; |
e0ae8728 | 991 | debug("remote forward %s for: listen %d, connect %s:%d", |
992 | type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", | |
3867aa0a | 993 | options.remote_forwards[i].listen_port, |
994 | options.remote_forwards[i].connect_host, | |
995 | options.remote_forwards[i].connect_port); | |
e0ae8728 | 996 | if (type == SSH2_MSG_REQUEST_FAILURE) |
3867aa0a | 997 | logit("Warning: remote port forwarding failed for listen " |
998 | "port %d", options.remote_forwards[i].listen_port); | |
e0ae8728 | 999 | } |
1000 | ||
396c147e | 1001 | static void |
5e96b616 | 1002 | ssh_control_listener(void) |
8ce64345 | 1003 | { |
5e96b616 | 1004 | struct sockaddr_un addr; |
1005 | mode_t old_umask; | |
4b5df124 | 1006 | int addr_len; |
1007 | ||
9dfd96d6 | 1008 | if (options.control_path == NULL || |
1009 | options.control_master == SSHCTL_MASTER_NO) | |
5e96b616 | 1010 | return; |
b5c334cc | 1011 | |
9dfd96d6 | 1012 | debug("setting up multiplex master socket"); |
1013 | ||
5e96b616 | 1014 | memset(&addr, '\0', sizeof(addr)); |
1015 | addr.sun_family = AF_UNIX; | |
4b5df124 | 1016 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
5e96b616 | 1017 | strlen(options.control_path) + 1; |
8ce64345 | 1018 | |
5e96b616 | 1019 | if (strlcpy(addr.sun_path, options.control_path, |
1020 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) | |
1021 | fatal("ControlPath too long"); | |
8ce64345 | 1022 | |
5e96b616 | 1023 | if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
17318dd6 | 1024 | fatal("%s socket(): %s", __func__, strerror(errno)); |
5e96b616 | 1025 | |
1026 | old_umask = umask(0177); | |
4b5df124 | 1027 | if (bind(control_fd, (struct sockaddr*)&addr, addr_len) == -1) { |
5e96b616 | 1028 | control_fd = -1; |
05ada1a6 | 1029 | if (errno == EINVAL || errno == EADDRINUSE) |
5e96b616 | 1030 | fatal("ControlSocket %s already exists", |
1031 | options.control_path); | |
1032 | else | |
17318dd6 | 1033 | fatal("%s bind(): %s", __func__, strerror(errno)); |
8ce64345 | 1034 | } |
5e96b616 | 1035 | umask(old_umask); |
1036 | ||
1037 | if (listen(control_fd, 64) == -1) | |
17318dd6 | 1038 | fatal("%s listen(): %s", __func__, strerror(errno)); |
5e96b616 | 1039 | |
1040 | set_nonblock(control_fd); | |
1041 | } | |
1042 | ||
1043 | /* request pty/x11/agent/tcpfwd/shell for channel */ | |
1044 | static void | |
1045 | ssh_session2_setup(int id, void *arg) | |
1046 | { | |
1786be35 | 1047 | extern char **environ; |
9bf083eb | 1048 | const char *display; |
5e96b616 | 1049 | int interactive = tty_flag; |
9bf083eb | 1050 | |
56964485 | 1051 | display = getenv("DISPLAY"); |
9bf083eb | 1052 | if (options.forward_x11 && display != NULL) { |
b48eeb07 | 1053 | char *proto, *data; |
0b242b12 | 1054 | /* Get reasonable local authentication information. */ |
9bf083eb | 1055 | client_x11_get_proto(display, options.xauth_location, |
1056 | options.forward_x11_trusted, &proto, &data); | |
0b242b12 | 1057 | /* Request forwarding with authentication spoofing. */ |
1058 | debug("Requesting X11 forwarding with authentication spoofing."); | |
9bf083eb | 1059 | x11_request_forwarding_with_spoofing(id, display, proto, data); |
b5c334cc | 1060 | interactive = 1; |
0b242b12 | 1061 | /* XXX wait for reply */ |
1062 | } | |
1063 | ||
fa08c86b | 1064 | check_agent_present(); |
1065 | if (options.forward_agent) { | |
1066 | debug("Requesting authentication agent forwarding."); | |
1067 | channel_request_start(id, "auth-agent-req@openssh.com", 0); | |
1068 | packet_send(); | |
1069 | } | |
1070 | ||
a4f24bf8 | 1071 | if (options.tun_open != SSH_TUNMODE_NO) { |
d20f3c9e | 1072 | Channel *c; |
1073 | int fd; | |
1074 | ||
1075 | debug("Requesting tun."); | |
a4f24bf8 | 1076 | if ((fd = tun_open(options.tun_local, |
1077 | options.tun_open)) >= 0) { | |
d20f3c9e | 1078 | c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1, |
1079 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, | |
1080 | 0, "tun", 1); | |
1081 | c->datagram = 1; | |
1082 | packet_start(SSH2_MSG_CHANNEL_OPEN); | |
1083 | packet_put_cstring("tun@openssh.com"); | |
1084 | packet_put_int(c->self); | |
1085 | packet_put_int(c->local_window_max); | |
1086 | packet_put_int(c->local_maxpacket); | |
a4f24bf8 | 1087 | packet_put_int(options.tun_open); |
d20f3c9e | 1088 | packet_put_int(options.tun_remote); |
1089 | packet_send(); | |
1090 | } | |
1091 | } | |
1092 | ||
5e96b616 | 1093 | client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"), |
1786be35 | 1094 | NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply); |
1f3bf5aa | 1095 | |
b5c334cc | 1096 | packet_set_interactive(interactive); |
8ce64345 | 1097 | } |
1098 | ||
bcdb96c2 | 1099 | /* open new channel for a session */ |
396c147e | 1100 | static int |
bcdb96c2 | 1101 | ssh_session2_open(void) |
8ce64345 | 1102 | { |
719fc62f | 1103 | Channel *c; |
1104 | int window, packetmax, in, out, err; | |
2e73a022 | 1105 | |
14a9a859 | 1106 | if (stdin_null_flag) { |
5ca51e19 | 1107 | in = open(_PATH_DEVNULL, O_RDONLY); |
14a9a859 | 1108 | } else { |
1109 | in = dup(STDIN_FILENO); | |
1110 | } | |
2e73a022 | 1111 | out = dup(STDOUT_FILENO); |
1112 | err = dup(STDERR_FILENO); | |
8ce64345 | 1113 | |
1114 | if (in < 0 || out < 0 || err < 0) | |
14a9a859 | 1115 | fatal("dup() in/out/err failed"); |
8ce64345 | 1116 | |
a22aff1f | 1117 | /* enable nonblocking unless tty */ |
1118 | if (!isatty(in)) | |
1119 | set_nonblock(in); | |
1120 | if (!isatty(out)) | |
1121 | set_nonblock(out); | |
1122 | if (!isatty(err)) | |
1123 | set_nonblock(err); | |
1124 | ||
bcbf86ec | 1125 | window = CHAN_SES_WINDOW_DEFAULT; |
1126 | packetmax = CHAN_SES_PACKET_DEFAULT; | |
ea9700ba | 1127 | if (tty_flag) { |
1128 | window >>= 1; | |
1129 | packetmax >>= 1; | |
8ce64345 | 1130 | } |
719fc62f | 1131 | c = channel_new( |
8ce64345 | 1132 | "session", SSH_CHANNEL_OPENING, in, out, err, |
bcbf86ec | 1133 | window, packetmax, CHAN_EXTENDED_WRITE, |
0d942eff | 1134 | "client-session", /*nonblock*/0); |
8ce64345 | 1135 | |
bcdb96c2 | 1136 | debug3("ssh_session2_open: channel_new: %d", c->self); |
eb0dd41f | 1137 | |
36e1f6a1 | 1138 | channel_send_open(c->self); |
bcdb96c2 | 1139 | if (!no_shell_flag) |
5e96b616 | 1140 | channel_register_confirm(c->self, ssh_session2_setup, NULL); |
5260325f | 1141 | |
719fc62f | 1142 | return c->self; |
eb0dd41f | 1143 | } |
1144 | ||
396c147e | 1145 | static int |
eb0dd41f | 1146 | ssh_session2(void) |
1147 | { | |
bcdb96c2 | 1148 | int id = -1; |
eb0dd41f | 1149 | |
1150 | /* XXX should be pre-session */ | |
1151 | ssh_init_forwarding(); | |
5e96b616 | 1152 | ssh_control_listener(); |
eb0dd41f | 1153 | |
bcdb96c2 | 1154 | if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN)) |
1155 | id = ssh_session2_open(); | |
eb0dd41f | 1156 | |
d20f3c9e | 1157 | /* Execute a local command */ |
1158 | if (options.local_command != NULL && | |
1159 | options.permit_local_command) | |
1160 | ssh_local_cmd(options.local_command); | |
1161 | ||
eb0dd41f | 1162 | /* If requested, let ssh continue in the background. */ |
1163 | if (fork_after_authentication_flag) | |
1164 | if (daemon(1, 1) < 0) | |
1165 | fatal("daemon() failed: %.200s", strerror(errno)); | |
1166 | ||
4bf9c10e | 1167 | return client_loop(tty_flag, tty_flag ? |
1168 | options.escape_char : SSH_ESCAPECHAR_NONE, id); | |
8efc0c15 | 1169 | } |
fa08c86b | 1170 | |
396c147e | 1171 | static void |
fee56204 | 1172 | load_public_identity_files(void) |
1173 | { | |
1174 | char *filename; | |
2e23cde0 | 1175 | int i = 0; |
0659cace | 1176 | Key *public; |
383546c4 | 1177 | #ifdef SMARTCARD |
0659cace | 1178 | Key **keys; |
1179 | ||
9ff6f66f | 1180 | if (options.smartcard_device != NULL && |
0659cace | 1181 | options.num_identity_files < SSH_MAX_IDENTITY_FILES && |
1182 | (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) { | |
1183 | int count = 0; | |
1184 | for (i = 0; keys[i] != NULL; i++) { | |
1185 | count++; | |
0659cace | 1186 | memmove(&options.identity_files[1], &options.identity_files[0], |
1187 | sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1)); | |
1188 | memmove(&options.identity_keys[1], &options.identity_keys[0], | |
1189 | sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1)); | |
1190 | options.num_identity_files++; | |
1191 | options.identity_keys[0] = keys[i]; | |
244e796f | 1192 | options.identity_files[0] = sc_get_key_label(keys[i]); |
0659cace | 1193 | } |
1c2deed1 | 1194 | if (options.num_identity_files > SSH_MAX_IDENTITY_FILES) |
1195 | options.num_identity_files = SSH_MAX_IDENTITY_FILES; | |
0659cace | 1196 | i = count; |
1197 | xfree(keys); | |
383546c4 | 1198 | } |
3e984472 | 1199 | #endif /* SMARTCARD */ |
2e23cde0 | 1200 | for (; i < options.num_identity_files; i++) { |
1201 | filename = tilde_expand_filename(options.identity_files[i], | |
1202 | original_real_uid); | |
1203 | public = key_load_public(filename, NULL); | |
1204 | debug("identity file %s type %d", filename, | |
1205 | public ? public->type : -1); | |
1206 | xfree(options.identity_files[i]); | |
1207 | options.identity_files[i] = filename; | |
1208 | options.identity_keys[i] = public; | |
1209 | } | |
fee56204 | 1210 | } |
5e96b616 | 1211 | |
1212 | static void | |
1213 | control_client_sighandler(int signo) | |
1214 | { | |
1215 | control_client_terminate = signo; | |
1216 | } | |
1217 | ||
1218 | static void | |
1219 | control_client_sigrelay(int signo) | |
1220 | { | |
1221 | if (control_server_pid > 1) | |
1222 | kill(control_server_pid, signo); | |
1223 | } | |
1224 | ||
67a08279 | 1225 | static int |
1226 | env_permitted(char *env) | |
1227 | { | |
1228 | int i; | |
1229 | char name[1024], *cp; | |
1230 | ||
1231 | strlcpy(name, env, sizeof(name)); | |
1232 | if ((cp = strchr(name, '=')) == NULL) | |
1233 | return (0); | |
1234 | ||
1235 | *cp = '\0'; | |
1236 | ||
1237 | for (i = 0; i < options.num_send_env; i++) | |
1238 | if (match_pattern(name, options.send_env[i])) | |
1239 | return (1); | |
1240 | ||
1241 | return (0); | |
1242 | } | |
1243 | ||
5e96b616 | 1244 | static void |
1245 | control_client(const char *path) | |
1246 | { | |
1247 | struct sockaddr_un addr; | |
a7e124fe | 1248 | int i, r, fd, sock, exitval, num_env, addr_len; |
5e96b616 | 1249 | Buffer m; |
f8c6db83 | 1250 | char *term; |
1786be35 | 1251 | extern char **environ; |
f8c6db83 | 1252 | u_int flags; |
f2107e97 | 1253 | |
9dfd96d6 | 1254 | if (mux_command == 0) |
1255 | mux_command = SSHMUX_COMMAND_OPEN; | |
1256 | ||
1257 | switch (options.control_master) { | |
1258 | case SSHCTL_MASTER_AUTO: | |
1259 | case SSHCTL_MASTER_AUTO_ASK: | |
1260 | debug("auto-mux: Trying existing master"); | |
1261 | /* FALLTHROUGH */ | |
1262 | case SSHCTL_MASTER_NO: | |
1263 | break; | |
1264 | default: | |
1265 | return; | |
1266 | } | |
1267 | ||
5e96b616 | 1268 | memset(&addr, '\0', sizeof(addr)); |
1269 | addr.sun_family = AF_UNIX; | |
4b5df124 | 1270 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
5e96b616 | 1271 | strlen(path) + 1; |
1272 | ||
1273 | if (strlcpy(addr.sun_path, path, | |
1274 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) | |
1275 | fatal("ControlPath too long"); | |
1276 | ||
1277 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) | |
1278 | fatal("%s socket(): %s", __func__, strerror(errno)); | |
1279 | ||
cc8ca1e6 | 1280 | if (connect(sock, (struct sockaddr*)&addr, addr_len) == -1) { |
19186c3d | 1281 | if (mux_command != SSHMUX_COMMAND_OPEN) { |
1282 | fatal("Control socket connect(%.100s): %s", path, | |
1283 | strerror(errno)); | |
1284 | } | |
f6740270 | 1285 | if (errno == ENOENT) |
1286 | debug("Control socket \"%.100s\" does not exist", path); | |
1287 | else { | |
1288 | error("Control socket connect(%.100s): %s", path, | |
1289 | strerror(errno)); | |
1290 | } | |
ef07103c | 1291 | close(sock); |
1292 | return; | |
1293 | } | |
56964485 | 1294 | |
ef07103c | 1295 | if (stdin_null_flag) { |
1296 | if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1) | |
1297 | fatal("open(/dev/null): %s", strerror(errno)); | |
1298 | if (dup2(fd, STDIN_FILENO) == -1) | |
1299 | fatal("dup2: %s", strerror(errno)); | |
1300 | if (fd > STDERR_FILENO) | |
1301 | close(fd); | |
1302 | } | |
56964485 | 1303 | |
ef07103c | 1304 | term = getenv("TERM"); |
f8c6db83 | 1305 | |
1306 | flags = 0; | |
1307 | if (tty_flag) | |
1308 | flags |= SSHMUX_FLAG_TTY; | |
1309 | if (subsystem_flag) | |
1310 | flags |= SSHMUX_FLAG_SUBSYS; | |
ef07103c | 1311 | if (options.forward_x11) |
1312 | flags |= SSHMUX_FLAG_X11_FWD; | |
1313 | if (options.forward_agent) | |
1314 | flags |= SSHMUX_FLAG_AGENT_FWD; | |
5e96b616 | 1315 | |
5e96b616 | 1316 | buffer_init(&m); |
1317 | ||
f8c6db83 | 1318 | /* Send our command to server */ |
1319 | buffer_put_int(&m, mux_command); | |
1320 | buffer_put_int(&m, flags); | |
ef07103c | 1321 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) |
f8c6db83 | 1322 | fatal("%s: msg_send", __func__); |
1323 | buffer_clear(&m); | |
1324 | ||
1325 | /* Get authorisation status and PID of controlee */ | |
5e96b616 | 1326 | if (ssh_msg_recv(sock, &m) == -1) |
1327 | fatal("%s: msg_recv", __func__); | |
ef07103c | 1328 | if (buffer_get_char(&m) != SSHMUX_VER) |
5e96b616 | 1329 | fatal("%s: wrong version", __func__); |
0d34d6ce | 1330 | if (buffer_get_int(&m) != 1) |
1331 | fatal("Connection to master denied"); | |
5e96b616 | 1332 | control_server_pid = buffer_get_int(&m); |
1333 | ||
5e96b616 | 1334 | buffer_clear(&m); |
5e96b616 | 1335 | |
f8c6db83 | 1336 | switch (mux_command) { |
1337 | case SSHMUX_COMMAND_ALIVE_CHECK: | |
f8cc7664 | 1338 | fprintf(stderr, "Master running (pid=%d)\r\n", |
f8c6db83 | 1339 | control_server_pid); |
1340 | exit(0); | |
1341 | case SSHMUX_COMMAND_TERMINATE: | |
1342 | fprintf(stderr, "Exit request sent.\r\n"); | |
1343 | exit(0); | |
1344 | case SSHMUX_COMMAND_OPEN: | |
1345 | /* continue below */ | |
1346 | break; | |
1347 | default: | |
1348 | fatal("silly mux_command %d", mux_command); | |
1349 | } | |
1350 | ||
1351 | /* SSHMUX_COMMAND_OPEN */ | |
ef07103c | 1352 | buffer_put_cstring(&m, term ? term : ""); |
5e96b616 | 1353 | buffer_append(&command, "\0", 1); |
1354 | buffer_put_cstring(&m, buffer_ptr(&command)); | |
1355 | ||
67a08279 | 1356 | if (options.num_send_env == 0 || environ == NULL) { |
1357 | buffer_put_int(&m, 0); | |
f2107e97 | 1358 | } else { |
67a08279 | 1359 | /* Pass environment */ |
1360 | num_env = 0; | |
1361 | for (i = 0; environ[i] != NULL; i++) | |
1362 | if (env_permitted(environ[i])) | |
1363 | num_env++; /* Count */ | |
f2107e97 | 1364 | |
67a08279 | 1365 | buffer_put_int(&m, num_env); |
1366 | ||
77c50919 | 1367 | for (i = 0; environ[i] != NULL && num_env >= 0; i++) |
1368 | if (env_permitted(environ[i])) { | |
1369 | num_env--; | |
67a08279 | 1370 | buffer_put_cstring(&m, environ[i]); |
77c50919 | 1371 | } |
67a08279 | 1372 | } |
1786be35 | 1373 | |
ef07103c | 1374 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) |
5e96b616 | 1375 | fatal("%s: msg_send", __func__); |
1376 | ||
1377 | mm_send_fd(sock, STDIN_FILENO); | |
1378 | mm_send_fd(sock, STDOUT_FILENO); | |
1379 | mm_send_fd(sock, STDERR_FILENO); | |
1380 | ||
1381 | /* Wait for reply, so master has a chance to gather ttymodes */ | |
1382 | buffer_clear(&m); | |
1383 | if (ssh_msg_recv(sock, &m) == -1) | |
1384 | fatal("%s: msg_recv", __func__); | |
ef07103c | 1385 | if (buffer_get_char(&m) != SSHMUX_VER) |
f8c6db83 | 1386 | fatal("%s: wrong version", __func__); |
5e96b616 | 1387 | buffer_free(&m); |
1388 | ||
d3e5d1e9 | 1389 | signal(SIGHUP, control_client_sighandler); |
78d2b454 | 1390 | signal(SIGINT, control_client_sighandler); |
1391 | signal(SIGTERM, control_client_sighandler); | |
1392 | signal(SIGWINCH, control_client_sigrelay); | |
1393 | ||
5e96b616 | 1394 | if (tty_flag) |
1395 | enter_raw_mode(); | |
1396 | ||
1397 | /* Stick around until the controlee closes the client_fd */ | |
1398 | exitval = 0; | |
1399 | for (;!control_client_terminate;) { | |
1400 | r = read(sock, &exitval, sizeof(exitval)); | |
1401 | if (r == 0) { | |
1402 | debug2("Received EOF from master"); | |
1403 | break; | |
1404 | } | |
1405 | if (r > 0) | |
1406 | debug2("Received exit status from master %d", exitval); | |
1407 | if (r == -1 && errno != EINTR) | |
1408 | fatal("%s: read %s", __func__, strerror(errno)); | |
1409 | } | |
1410 | ||
1411 | if (control_client_terminate) | |
1412 | debug2("Exiting on signal %d", control_client_terminate); | |
1413 | ||
1414 | close(sock); | |
1415 | ||
1416 | leave_raw_mode(); | |
1417 | ||
1418 | if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET) | |
1419 | fprintf(stderr, "Connection to master closed.\r\n"); | |
1420 | ||
1421 | exit(exitval); | |
1422 | } |