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