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