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