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