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