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