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