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