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