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