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