]>
Commit | Line | Data |
---|---|---|
5156b1a1 | 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. | |
540d72c3 | 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" | |
30460aeb | 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> | |
30460aeb | 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> | |
e74dc197 | 74 | #include "openbsd-compat/openssl-compat.h" |
5156b1a1 | 75 | #include "openbsd-compat/sys-queue.h" |
3c0ef626 | 76 | |
30460aeb | 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" | |
7e82606e | 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" | |
7e82606e | 98 | #include "sshpty.h" |
99 | #include "match.h" | |
100 | #include "msg.h" | |
7e82606e | 101 | #include "uidswap.h" |
30460aeb | 102 | #include "version.h" |
3c0ef626 | 103 | |
104 | #ifdef SMARTCARD | |
3c0ef626 | 105 | #include "scard.h" |
106 | #endif | |
107 | ||
3c0ef626 | 108 | extern char *__progname; |
3c0ef626 | 109 | |
5156b1a1 | 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 | |
44a053a3 | 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. */ | |
44a053a3 | 154 | Sensitive sensitive_data; |
3c0ef626 | 155 | |
156 | /* Original real UID. */ | |
157 | uid_t original_real_uid; | |
44a053a3 | 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 | ||
c6046bb0 | 166 | /* # of replies received for global requests */ |
5156b1a1 | 167 | static int remote_forward_confirms_received = 0; |
c6046bb0 | 168 | |
d03f4262 | 169 | /* pid of proxycommand child process */ |
170 | pid_t proxy_command_pid = 0; | |
171 | ||
5156b1a1 | 172 | /* mux.c */ |
173 | extern int muxserver_sock; | |
174 | extern u_int muxclient_command; | |
7e82606e | 175 | |
3c0ef626 | 176 | /* Prints a help message to the user. This function never returns. */ |
177 | ||
178 | static void | |
179 | usage(void) | |
180 | { | |
12a403af | 181 | fprintf(stderr, |
fa0f0f45 | 182 | "usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n" |
08822d99 | 183 | " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n" |
dfddba3d | 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" | |
30460aeb | 187 | " [-w local_tun[:remote_tun]] [user@]hostname [command]\n" |
12a403af | 188 | ); |
08822d99 | 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); | |
5156b1a1 | 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 | { | |
44a053a3 | 206 | int i, opt, exit_status; |
540d72c3 | 207 | char *p, *cp, *line, buf[256]; |
3c0ef626 | 208 | struct stat st; |
209 | struct passwd *pw; | |
e74dc197 | 210 | int dummy, timeout_ms; |
3c0ef626 | 211 | extern int optind, optreset; |
212 | extern char *optarg; | |
2ce0bfe4 | 213 | struct servent *sp; |
dfddba3d | 214 | Forward fwd; |
3c0ef626 | 215 | |
08822d99 | 216 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
217 | sanitise_stdfd(); | |
218 | ||
7cac2b65 | 219 | __progname = ssh_get_progname(av[0]); |
3c0ef626 | 220 | init_rng(); |
221 | ||
222 | /* | |
223 | * Save the original real uid. It will be needed later (uid-swapping | |
224 | * may clobber the real uid). | |
225 | */ | |
226 | original_real_uid = getuid(); | |
227 | original_effective_uid = geteuid(); | |
540d72c3 | 228 | |
d03f4262 | 229 | /* |
230 | * Use uid-swapping to give up root privileges for the duration of | |
231 | * option processing. We will re-instantiate the rights when we are | |
232 | * ready to create the privileged port, and will permanently drop | |
233 | * them when the port has been created (actually, when the connection | |
234 | * has been made, as we may need to create the port several times). | |
235 | */ | |
236 | PRIV_END; | |
3c0ef626 | 237 | |
238 | #ifdef HAVE_SETRLIMIT | |
239 | /* If we are installed setuid root be careful to not drop core. */ | |
240 | if (original_real_uid != original_effective_uid) { | |
241 | struct rlimit rlim; | |
242 | rlim.rlim_cur = rlim.rlim_max = 0; | |
243 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) | |
244 | fatal("setrlimit failed: %.100s", strerror(errno)); | |
245 | } | |
246 | #endif | |
247 | /* Get user data. */ | |
248 | pw = getpwuid(original_real_uid); | |
249 | if (!pw) { | |
7cac2b65 | 250 | logit("You don't exist, go away!"); |
08822d99 | 251 | exit(255); |
3c0ef626 | 252 | } |
253 | /* Take a copy of the returned structure. */ | |
254 | pw = pwcopy(pw); | |
255 | ||
3c0ef626 | 256 | /* |
257 | * Set our umask to something reasonable, as some files are created | |
258 | * with the default umask. This will make them world-readable but | |
259 | * writable only by the owner, which is ok for all files for which we | |
260 | * don't set the modes explicitly. | |
261 | */ | |
262 | umask(022); | |
263 | ||
5156b1a1 | 264 | /* |
265 | * Initialize option structure to indicate that no values have been | |
266 | * set. | |
267 | */ | |
3c0ef626 | 268 | initialize_options(&options); |
269 | ||
270 | /* Parse command-line arguments. */ | |
271 | host = NULL; | |
473db5ab | 272 | |
30460aeb | 273 | again: |
5156b1a1 | 274 | while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx" |
275 | "ACD:F:I:KL:MNO:PR:S:TVw:XY")) != -1) { | |
3c0ef626 | 276 | switch (opt) { |
277 | case '1': | |
278 | options.protocol = SSH_PROTO_1; | |
279 | break; | |
280 | case '2': | |
281 | options.protocol = SSH_PROTO_2; | |
282 | break; | |
283 | case '4': | |
7cac2b65 | 284 | options.address_family = AF_INET; |
3c0ef626 | 285 | break; |
286 | case '6': | |
7cac2b65 | 287 | options.address_family = AF_INET6; |
3c0ef626 | 288 | break; |
289 | case 'n': | |
290 | stdin_null_flag = 1; | |
291 | break; | |
292 | case 'f': | |
293 | fork_after_authentication_flag = 1; | |
294 | stdin_null_flag = 1; | |
295 | break; | |
296 | case 'x': | |
297 | options.forward_x11 = 0; | |
298 | break; | |
299 | case 'X': | |
300 | options.forward_x11 = 1; | |
301 | break; | |
540d72c3 | 302 | case 'Y': |
303 | options.forward_x11 = 1; | |
304 | options.forward_x11_trusted = 1; | |
305 | break; | |
3c0ef626 | 306 | case 'g': |
307 | options.gateway_ports = 1; | |
308 | break; | |
dfddba3d | 309 | case 'O': |
310 | if (strcmp(optarg, "check") == 0) | |
5156b1a1 | 311 | muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK; |
dfddba3d | 312 | else if (strcmp(optarg, "exit") == 0) |
5156b1a1 | 313 | muxclient_command = SSHMUX_COMMAND_TERMINATE; |
dfddba3d | 314 | else |
315 | fatal("Invalid multiplex command."); | |
316 | break; | |
d03f4262 | 317 | case 'P': /* deprecated */ |
3c0ef626 | 318 | options.use_privileged_port = 0; |
319 | break; | |
320 | case 'a': | |
321 | options.forward_agent = 0; | |
322 | break; | |
323 | case 'A': | |
324 | options.forward_agent = 1; | |
325 | break; | |
3c0ef626 | 326 | case 'k': |
540d72c3 | 327 | options.gss_deleg_creds = 0; |
3c0ef626 | 328 | break; |
fa0f0f45 | 329 | case 'K': |
330 | options.gss_authentication = 1; | |
331 | options.gss_deleg_creds = 1; | |
332 | break; | |
3c0ef626 | 333 | case 'i': |
334 | if (stat(optarg, &st) < 0) { | |
335 | fprintf(stderr, "Warning: Identity file %s " | |
dfddba3d | 336 | "not accessible: %s.\n", optarg, |
337 | strerror(errno)); | |
3c0ef626 | 338 | break; |
339 | } | |
340 | if (options.num_identity_files >= | |
341 | SSH_MAX_IDENTITY_FILES) | |
342 | fatal("Too many identity files specified " | |
343 | "(max %d)", SSH_MAX_IDENTITY_FILES); | |
344 | options.identity_files[options.num_identity_files++] = | |
345 | xstrdup(optarg); | |
346 | break; | |
347 | case 'I': | |
348 | #ifdef SMARTCARD | |
349 | options.smartcard_device = xstrdup(optarg); | |
350 | #else | |
351 | fprintf(stderr, "no support for smartcards.\n"); | |
352 | #endif | |
353 | break; | |
354 | case 't': | |
355 | if (tty_flag) | |
356 | force_tty_flag = 1; | |
357 | tty_flag = 1; | |
358 | break; | |
359 | case 'v': | |
7cac2b65 | 360 | if (debug_flag == 0) { |
3c0ef626 | 361 | debug_flag = 1; |
362 | options.log_level = SYSLOG_LEVEL_DEBUG1; | |
7cac2b65 | 363 | } else { |
364 | if (options.log_level < SYSLOG_LEVEL_DEBUG3) | |
365 | options.log_level++; | |
3c0ef626 | 366 | break; |
7cac2b65 | 367 | } |
dfddba3d | 368 | /* FALLTHROUGH */ |
3c0ef626 | 369 | case 'V': |
12a403af | 370 | fprintf(stderr, "%s, %s\n", |
dfddba3d | 371 | SSH_RELEASE, SSLeay_version(SSLEAY_VERSION)); |
3c0ef626 | 372 | if (opt == 'V') |
373 | exit(0); | |
374 | break; | |
08822d99 | 375 | case 'w': |
376 | if (options.tun_open == -1) | |
377 | options.tun_open = SSH_TUNMODE_DEFAULT; | |
378 | options.tun_local = a2tun(optarg, &options.tun_remote); | |
379 | if (options.tun_local == SSH_TUNID_ERR) { | |
5156b1a1 | 380 | fprintf(stderr, |
381 | "Bad tun device '%s'\n", optarg); | |
08822d99 | 382 | exit(255); |
383 | } | |
384 | break; | |
3c0ef626 | 385 | case 'q': |
386 | options.log_level = SYSLOG_LEVEL_QUIET; | |
387 | break; | |
388 | case 'e': | |
389 | if (optarg[0] == '^' && optarg[2] == 0 && | |
390 | (u_char) optarg[1] >= 64 && | |
391 | (u_char) optarg[1] < 128) | |
392 | options.escape_char = (u_char) optarg[1] & 31; | |
393 | else if (strlen(optarg) == 1) | |
394 | options.escape_char = (u_char) optarg[0]; | |
395 | else if (strcmp(optarg, "none") == 0) | |
396 | options.escape_char = SSH_ESCAPECHAR_NONE; | |
397 | else { | |
398 | fprintf(stderr, "Bad escape character '%s'.\n", | |
399 | optarg); | |
08822d99 | 400 | exit(255); |
3c0ef626 | 401 | } |
402 | break; | |
403 | case 'c': | |
404 | if (ciphers_valid(optarg)) { | |
405 | /* SSH2 only */ | |
406 | options.ciphers = xstrdup(optarg); | |
7e82606e | 407 | options.cipher = SSH_CIPHER_INVALID; |
3c0ef626 | 408 | } else { |
409 | /* SSH1 only */ | |
410 | options.cipher = cipher_number(optarg); | |
411 | if (options.cipher == -1) { | |
412 | fprintf(stderr, | |
413 | "Unknown cipher type '%s'\n", | |
414 | optarg); | |
08822d99 | 415 | exit(255); |
3c0ef626 | 416 | } |
417 | if (options.cipher == SSH_CIPHER_3DES) | |
418 | options.ciphers = "3des-cbc"; | |
419 | else if (options.cipher == SSH_CIPHER_BLOWFISH) | |
420 | options.ciphers = "blowfish-cbc"; | |
421 | else | |
422 | options.ciphers = (char *)-1; | |
423 | } | |
424 | break; | |
425 | case 'm': | |
426 | if (mac_valid(optarg)) | |
427 | options.macs = xstrdup(optarg); | |
428 | else { | |
429 | fprintf(stderr, "Unknown mac type '%s'\n", | |
430 | optarg); | |
08822d99 | 431 | exit(255); |
3c0ef626 | 432 | } |
433 | break; | |
7e82606e | 434 | case 'M': |
2ce0bfe4 | 435 | if (options.control_master == SSHCTL_MASTER_YES) |
436 | options.control_master = SSHCTL_MASTER_ASK; | |
437 | else | |
438 | options.control_master = SSHCTL_MASTER_YES; | |
7e82606e | 439 | break; |
3c0ef626 | 440 | case 'p': |
441 | options.port = a2port(optarg); | |
442 | if (options.port == 0) { | |
443 | fprintf(stderr, "Bad port '%s'\n", optarg); | |
08822d99 | 444 | exit(255); |
3c0ef626 | 445 | } |
446 | break; | |
447 | case 'l': | |
448 | options.user = optarg; | |
449 | break; | |
450 | ||
451 | case 'L': | |
dfddba3d | 452 | if (parse_forward(&fwd, optarg)) |
453 | add_local_forward(&options, &fwd); | |
454 | else { | |
3c0ef626 | 455 | fprintf(stderr, |
dfddba3d | 456 | "Bad local forwarding specification '%s'\n", |
3c0ef626 | 457 | optarg); |
08822d99 | 458 | exit(255); |
3c0ef626 | 459 | } |
dfddba3d | 460 | break; |
461 | ||
462 | case 'R': | |
463 | if (parse_forward(&fwd, optarg)) { | |
464 | add_remote_forward(&options, &fwd); | |
465 | } else { | |
3c0ef626 | 466 | fprintf(stderr, |
dfddba3d | 467 | "Bad remote forwarding specification " |
468 | "'%s'\n", optarg); | |
08822d99 | 469 | exit(255); |
3c0ef626 | 470 | } |
3c0ef626 | 471 | break; |
472 | ||
473 | case 'D': | |
dfddba3d | 474 | cp = p = xstrdup(optarg); |
475 | memset(&fwd, '\0', sizeof(fwd)); | |
476 | fwd.connect_host = "socks"; | |
477 | if ((fwd.listen_host = hpdelim(&cp)) == NULL) { | |
478 | fprintf(stderr, "Bad dynamic forwarding " | |
479 | "specification '%.100s'\n", optarg); | |
08822d99 | 480 | exit(255); |
dfddba3d | 481 | } |
482 | if (cp != NULL) { | |
483 | fwd.listen_port = a2port(cp); | |
5156b1a1 | 484 | fwd.listen_host = |
485 | cleanhostname(fwd.listen_host); | |
dfddba3d | 486 | } else { |
487 | fwd.listen_port = a2port(fwd.listen_host); | |
2ce0bfe4 | 488 | fwd.listen_host = NULL; |
dfddba3d | 489 | } |
490 | ||
491 | if (fwd.listen_port == 0) { | |
3c0ef626 | 492 | fprintf(stderr, "Bad dynamic port '%s'\n", |
493 | optarg); | |
08822d99 | 494 | exit(255); |
3c0ef626 | 495 | } |
dfddba3d | 496 | add_local_forward(&options, &fwd); |
497 | xfree(p); | |
3c0ef626 | 498 | break; |
499 | ||
500 | case 'C': | |
501 | options.compression = 1; | |
502 | break; | |
503 | case 'N': | |
504 | no_shell_flag = 1; | |
505 | no_tty_flag = 1; | |
3c0ef626 | 506 | break; |
e74dc197 | 507 | case 'T': |
508 | no_tty_flag = 1; | |
509 | /* ensure that the user doesn't try to backdoor a */ | |
510 | /* null cipher switch on an interactive session */ | |
511 | /* so explicitly disable it no matter what */ | |
512 | options.none_switch=0; | |
513 | break; | |
3c0ef626 | 514 | case 'o': |
515 | dummy = 1; | |
540d72c3 | 516 | line = xstrdup(optarg); |
3c0ef626 | 517 | if (process_config_line(&options, host ? host : "", |
540d72c3 | 518 | line, "command-line", 0, &dummy) != 0) |
08822d99 | 519 | exit(255); |
540d72c3 | 520 | xfree(line); |
3c0ef626 | 521 | break; |
522 | case 's': | |
523 | subsystem_flag = 1; | |
524 | break; | |
7e82606e | 525 | case 'S': |
526 | if (options.control_path != NULL) | |
527 | free(options.control_path); | |
528 | options.control_path = xstrdup(optarg); | |
529 | break; | |
3c0ef626 | 530 | case 'b': |
531 | options.bind_address = optarg; | |
3f48cae5 | 532 | break; |
3f750d99 | 533 | case 'F': |
534 | config = optarg; | |
535 | break; | |
3c0ef626 | 536 | default: |
537 | usage(); | |
538 | } | |
539 | } | |
540 | ||
541 | ac -= optind; | |
542 | av += optind; | |
543 | ||
544 | if (ac > 0 && !host && **av != '-') { | |
bfe49944 | 545 | if (strrchr(*av, '@')) { |
3c0ef626 | 546 | p = xstrdup(*av); |
bfe49944 | 547 | cp = strrchr(p, '@'); |
3c0ef626 | 548 | if (cp == NULL || cp == p) |
549 | usage(); | |
550 | options.user = p; | |
551 | *cp = '\0'; | |
552 | host = ++cp; | |
553 | } else | |
554 | host = *av; | |
bfe49944 | 555 | if (ac > 1) { |
556 | optind = optreset = 1; | |
3c0ef626 | 557 | goto again; |
558 | } | |
bfe49944 | 559 | ac--, av++; |
3c0ef626 | 560 | } |
561 | ||
562 | /* Check that we got a host name. */ | |
563 | if (!host) | |
564 | usage(); | |
565 | ||
566 | SSLeay_add_all_algorithms(); | |
567 | ERR_load_crypto_strings(); | |
3c0ef626 | 568 | |
569 | /* Initialize the command to execute on remote host. */ | |
570 | buffer_init(&command); | |
571 | ||
572 | /* | |
573 | * Save the command to execute on the remote host in a buffer. There | |
574 | * is no limit on the length of the command, except by the maximum | |
575 | * packet size. Also sets the tty flag if there is no command. | |
576 | */ | |
577 | if (!ac) { | |
578 | /* No command specified - execute shell on a tty. */ | |
579 | tty_flag = 1; | |
580 | if (subsystem_flag) { | |
581 | fprintf(stderr, | |
582 | "You must specify a subsystem to invoke.\n"); | |
583 | usage(); | |
584 | } | |
585 | } else { | |
586 | /* A command has been specified. Store it into the buffer. */ | |
587 | for (i = 0; i < ac; i++) { | |
588 | if (i) | |
589 | buffer_append(&command, " ", 1); | |
590 | buffer_append(&command, av[i], strlen(av[i])); | |
591 | } | |
592 | } | |
593 | ||
594 | /* Cannot fork to background if no command. */ | |
5156b1a1 | 595 | if (fork_after_authentication_flag && buffer_len(&command) == 0 && |
596 | !no_shell_flag) | |
597 | fatal("Cannot fork into background without a command " | |
598 | "to execute."); | |
3c0ef626 | 599 | |
600 | /* Allocate a tty by default if no command specified. */ | |
601 | if (buffer_len(&command) == 0) | |
602 | tty_flag = 1; | |
603 | ||
d03f4262 | 604 | /* Force no tty */ |
3c0ef626 | 605 | if (no_tty_flag) |
606 | tty_flag = 0; | |
607 | /* Do not allocate a tty if stdin is not a tty. */ | |
2ce0bfe4 | 608 | if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) { |
3c0ef626 | 609 | if (tty_flag) |
5156b1a1 | 610 | logit("Pseudo-terminal will not be allocated because " |
611 | "stdin is not a terminal."); | |
3c0ef626 | 612 | tty_flag = 0; |
613 | } | |
614 | ||
615 | /* | |
616 | * Initialize "log" output. Since we are the client all output | |
617 | * actually goes to stderr. | |
618 | */ | |
5156b1a1 | 619 | log_init(av[0], |
620 | options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, | |
3c0ef626 | 621 | SYSLOG_FACILITY_USER, 1); |
622 | ||
623 | /* | |
624 | * Read per-user configuration file. Ignore the system wide config | |
625 | * file if the user specifies a config file on the command line. | |
626 | */ | |
627 | if (config != NULL) { | |
7e82606e | 628 | if (!read_config_file(config, host, &options, 0)) |
3c0ef626 | 629 | fatal("Can't open user config file %.100s: " |
630 | "%.100s", config, strerror(errno)); | |
0b90ac93 | 631 | } else { |
c6046bb0 | 632 | /* |
633 | * Since the config file parsing code aborts if it sees | |
634 | * options it doesn't recognize, allow users to put | |
635 | * options specific to compile-time add-ons in alternate | |
636 | * config files so their primary config file will | |
637 | * interoperate SSH versions that don't support those | |
638 | * options. | |
639 | */ | |
640 | #ifdef GSSAPI | |
641 | snprintf(buf, sizeof buf, "%.100s/%.100s.gssapi", pw->pw_dir, | |
642 | _PATH_SSH_USER_CONFFILE); | |
7e82606e | 643 | (void)read_config_file(buf, host, &options, 1); |
c6046bb0 | 644 | #ifdef GSI |
645 | snprintf(buf, sizeof buf, "%.100s/%.100s.gsi", pw->pw_dir, | |
646 | _PATH_SSH_USER_CONFFILE); | |
7e82606e | 647 | (void)read_config_file(buf, host, &options, 1); |
c6046bb0 | 648 | #endif |
7cac2b65 | 649 | #if defined(KRB5) |
c6046bb0 | 650 | snprintf(buf, sizeof buf, "%.100s/%.100s.krb", pw->pw_dir, |
651 | _PATH_SSH_USER_CONFFILE); | |
7e82606e | 652 | (void)read_config_file(buf, host, &options, 1); |
c6046bb0 | 653 | #endif |
c6046bb0 | 654 | #endif |
3c0ef626 | 655 | snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, |
656 | _PATH_SSH_USER_CONFFILE); | |
7e82606e | 657 | (void)read_config_file(buf, host, &options, 1); |
3c0ef626 | 658 | |
659 | /* Read systemwide configuration file after use config. */ | |
7e82606e | 660 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, |
661 | &options, 0); | |
3c0ef626 | 662 | } |
663 | ||
664 | /* Fill configuration defaults. */ | |
665 | fill_default_options(&options); | |
666 | ||
7cac2b65 | 667 | channel_set_af(options.address_family); |
668 | ||
3c0ef626 | 669 | /* reinit */ |
670 | log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1); | |
671 | ||
672 | seed_rng(); | |
673 | ||
7cac2b65 | 674 | if (options.user == NULL) { |
675 | options.user = xstrdup(pw->pw_name); | |
676 | options.implicit = 1; | |
677 | } | |
73a68d83 | 678 | else options.implicit = 0; |
3c0ef626 | 679 | |
5156b1a1 | 680 | /* Get default port if port has not been set. */ |
681 | if (options.port == 0) { | |
682 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); | |
683 | options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; | |
684 | } | |
685 | ||
686 | if (options.local_command != NULL) { | |
687 | char thishost[NI_MAXHOST]; | |
688 | ||
689 | if (gethostname(thishost, sizeof(thishost)) == -1) | |
690 | fatal("gethostname: %s", strerror(errno)); | |
691 | snprintf(buf, sizeof(buf), "%d", options.port); | |
692 | debug3("expanding LocalCommand: %s", options.local_command); | |
693 | cp = options.local_command; | |
694 | options.local_command = percent_expand(cp, "d", pw->pw_dir, | |
695 | "h", options.hostname? options.hostname : host, | |
696 | "l", thishost, "n", host, "r", options.user, "p", buf, | |
697 | "u", pw->pw_name, (char *)NULL); | |
698 | debug3("expanded LocalCommand: %s", options.local_command); | |
699 | xfree(cp); | |
700 | } | |
701 | ||
3c0ef626 | 702 | if (options.hostname != NULL) |
703 | host = options.hostname; | |
704 | ||
7cac2b65 | 705 | /* force lowercase for hostkey matching */ |
706 | if (options.host_key_alias != NULL) { | |
707 | for (p = options.host_key_alias; *p; p++) | |
708 | if (isupper(*p)) | |
30460aeb | 709 | *p = (char)tolower(*p); |
7cac2b65 | 710 | } |
711 | ||
bfe49944 | 712 | if (options.proxy_command != NULL && |
e74dc197 | 713 | strcmp(options.proxy_command, "none") == 0) { |
714 | xfree(options.proxy_command); | |
bfe49944 | 715 | options.proxy_command = NULL; |
e74dc197 | 716 | } |
2ce0bfe4 | 717 | if (options.control_path != NULL && |
e74dc197 | 718 | strcmp(options.control_path, "none") == 0) { |
719 | xfree(options.control_path); | |
2ce0bfe4 | 720 | options.control_path = NULL; |
e74dc197 | 721 | } |
bfe49944 | 722 | |
7e82606e | 723 | if (options.control_path != NULL) { |
30460aeb | 724 | char thishost[NI_MAXHOST]; |
725 | ||
726 | if (gethostname(thishost, sizeof(thishost)) == -1) | |
727 | fatal("gethostname: %s", strerror(errno)); | |
2ce0bfe4 | 728 | snprintf(buf, sizeof(buf), "%d", options.port); |
729 | cp = tilde_expand_filename(options.control_path, | |
730 | original_real_uid); | |
e74dc197 | 731 | xfree(options.control_path); |
2ce0bfe4 | 732 | options.control_path = percent_expand(cp, "p", buf, "h", host, |
30460aeb | 733 | "r", options.user, "l", thishost, (char *)NULL); |
2ce0bfe4 | 734 | xfree(cp); |
7e82606e | 735 | } |
5156b1a1 | 736 | if (muxclient_command != 0 && options.control_path == NULL) |
2ce0bfe4 | 737 | fatal("No ControlPath specified for \"-O\" command"); |
738 | if (options.control_path != NULL) | |
5156b1a1 | 739 | muxclient(options.control_path); |
7e82606e | 740 | |
e74dc197 | 741 | timeout_ms = options.connection_timeout * 1000; |
742 | ||
3c0ef626 | 743 | /* Open a connection to the remote host. */ |
7cac2b65 | 744 | if (ssh_connect(host, &hostaddr, options.port, |
e74dc197 | 745 | options.address_family, options.connection_attempts, &timeout_ms, |
746 | options.tcp_keep_alive, | |
44a053a3 | 747 | #ifdef HAVE_CYGWIN |
748 | options.use_privileged_port, | |
749 | #else | |
750 | original_effective_uid == 0 && options.use_privileged_port, | |
751 | #endif | |
752 | options.proxy_command) != 0) | |
08822d99 | 753 | exit(255); |
3c0ef626 | 754 | |
e74dc197 | 755 | if (timeout_ms > 0) |
756 | debug3("timeout: %d ms remain after connect", timeout_ms); | |
757 | ||
3c0ef626 | 758 | /* |
759 | * If we successfully made the connection, load the host private key | |
760 | * in case we will need it later for combined rsa-rhosts | |
761 | * authentication. This must be done before releasing extra | |
762 | * privileges, because the file is only readable by root. | |
44a053a3 | 763 | * If we cannot access the private keys, load the public keys |
764 | * instead and try to execute the ssh-keysign helper instead. | |
3c0ef626 | 765 | */ |
766 | sensitive_data.nkeys = 0; | |
767 | sensitive_data.keys = NULL; | |
44a053a3 | 768 | sensitive_data.external_keysign = 0; |
769 | if (options.rhosts_rsa_authentication || | |
770 | options.hostbased_authentication) { | |
3c0ef626 | 771 | sensitive_data.nkeys = 3; |
30460aeb | 772 | sensitive_data.keys = xcalloc(sensitive_data.nkeys, |
d03f4262 | 773 | sizeof(Key)); |
44a053a3 | 774 | |
775 | PRIV_START; | |
3c0ef626 | 776 | sensitive_data.keys[0] = key_load_private_type(KEY_RSA1, |
30460aeb | 777 | _PATH_HOST_KEY_FILE, "", NULL, NULL); |
3c0ef626 | 778 | sensitive_data.keys[1] = key_load_private_type(KEY_DSA, |
30460aeb | 779 | _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL); |
3c0ef626 | 780 | sensitive_data.keys[2] = key_load_private_type(KEY_RSA, |
30460aeb | 781 | _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL); |
44a053a3 | 782 | PRIV_END; |
783 | ||
d03f4262 | 784 | if (options.hostbased_authentication == 1 && |
785 | sensitive_data.keys[0] == NULL && | |
44a053a3 | 786 | sensitive_data.keys[1] == NULL && |
787 | sensitive_data.keys[2] == NULL) { | |
788 | sensitive_data.keys[1] = key_load_public( | |
789 | _PATH_HOST_DSA_KEY_FILE, NULL); | |
790 | sensitive_data.keys[2] = key_load_public( | |
791 | _PATH_HOST_RSA_KEY_FILE, NULL); | |
792 | sensitive_data.external_keysign = 1; | |
793 | } | |
3c0ef626 | 794 | } |
795 | /* | |
796 | * Get rid of any extra privileges that we may have. We will no | |
797 | * longer need them. Also, extra privileges could make it very hard | |
798 | * to read identity files and other non-world-readable files from the | |
799 | * user's home directory if it happens to be on a NFS volume where | |
800 | * root is mapped to nobody. | |
801 | */ | |
7e82606e | 802 | if (original_effective_uid == 0) { |
803 | PRIV_START; | |
804 | permanently_set_uid(pw); | |
805 | } | |
3c0ef626 | 806 | |
807 | /* | |
808 | * Now that we are back to our own permissions, create ~/.ssh | |
08822d99 | 809 | * directory if it doesn't already exist. |
3c0ef626 | 810 | */ |
5156b1a1 | 811 | snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, |
812 | strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); | |
3c0ef626 | 813 | if (stat(buf, &st) < 0) |
814 | if (mkdir(buf, 0700) < 0) | |
815 | error("Could not create directory '%.200s'.", buf); | |
816 | ||
3c0ef626 | 817 | /* load options.identity_files */ |
818 | load_public_identity_files(); | |
819 | ||
820 | /* Expand ~ in known host file names. */ | |
821 | /* XXX mem-leaks: */ | |
822 | options.system_hostfile = | |
823 | tilde_expand_filename(options.system_hostfile, original_real_uid); | |
824 | options.user_hostfile = | |
825 | tilde_expand_filename(options.user_hostfile, original_real_uid); | |
826 | options.system_hostfile2 = | |
827 | tilde_expand_filename(options.system_hostfile2, original_real_uid); | |
828 | options.user_hostfile2 = | |
829 | tilde_expand_filename(options.user_hostfile2, original_real_uid); | |
830 | ||
831 | signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ | |
832 | ||
5156b1a1 | 833 | /* Log into the remote system. Never returns if the login fails. */ |
e74dc197 | 834 | ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, |
835 | pw, timeout_ms); | |
3c0ef626 | 836 | |
837 | /* We no longer need the private host keys. Clear them now. */ | |
838 | if (sensitive_data.nkeys != 0) { | |
839 | for (i = 0; i < sensitive_data.nkeys; i++) { | |
840 | if (sensitive_data.keys[i] != NULL) { | |
841 | /* Destroys contents safely */ | |
842 | debug3("clear hostkey %d", i); | |
843 | key_free(sensitive_data.keys[i]); | |
844 | sensitive_data.keys[i] = NULL; | |
845 | } | |
846 | } | |
847 | xfree(sensitive_data.keys); | |
848 | } | |
849 | for (i = 0; i < options.num_identity_files; i++) { | |
850 | if (options.identity_files[i]) { | |
851 | xfree(options.identity_files[i]); | |
852 | options.identity_files[i] = NULL; | |
853 | } | |
854 | if (options.identity_keys[i]) { | |
855 | key_free(options.identity_keys[i]); | |
856 | options.identity_keys[i] = NULL; | |
857 | } | |
858 | } | |
859 | ||
860 | exit_status = compat20 ? ssh_session2() : ssh_session(); | |
861 | packet_close(); | |
d03f4262 | 862 | |
5156b1a1 | 863 | if (options.control_path != NULL && muxserver_sock != -1) |
7e82606e | 864 | unlink(options.control_path); |
865 | ||
d03f4262 | 866 | /* |
540d72c3 | 867 | * Send SIGHUP to proxy command if used. We don't wait() in |
d03f4262 | 868 | * case it hangs and instead rely on init to reap the child |
869 | */ | |
870 | if (proxy_command_pid > 1) | |
871 | kill(proxy_command_pid, SIGHUP); | |
872 | ||
3c0ef626 | 873 | return exit_status; |
874 | } | |
875 | ||
5156b1a1 | 876 | /* Callback for remote forward global requests */ |
877 | static void | |
878 | ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt) | |
879 | { | |
880 | Forward *rfwd = (Forward *)ctxt; | |
881 | ||
882 | debug("remote forward %s for: listen %d, connect %s:%d", | |
883 | type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", | |
884 | rfwd->listen_port, rfwd->connect_host, rfwd->connect_port); | |
885 | if (type == SSH2_MSG_REQUEST_FAILURE) { | |
886 | if (options.exit_on_forward_failure) | |
887 | fatal("Error: remote port forwarding failed for " | |
888 | "listen port %d", rfwd->listen_port); | |
889 | else | |
890 | logit("Warning: remote port forwarding failed for " | |
891 | "listen port %d", rfwd->listen_port); | |
892 | } | |
893 | if (++remote_forward_confirms_received == options.num_remote_forwards) { | |
894 | debug("All remote forwarding requests processed"); | |
895 | if (fork_after_authentication_flag) { | |
896 | fork_after_authentication_flag = 0; | |
897 | if (daemon(1, 1) < 0) | |
898 | fatal("daemon() failed: %.200s", | |
899 | strerror(errno)); | |
900 | } | |
901 | } | |
902 | } | |
903 | ||
3c0ef626 | 904 | static void |
905 | ssh_init_forwarding(void) | |
906 | { | |
907 | int success = 0; | |
908 | int i; | |
909 | ||
910 | /* Initiate local TCP/IP port forwardings. */ | |
911 | for (i = 0; i < options.num_local_forwards; i++) { | |
dfddba3d | 912 | debug("Local connections to %.200s:%d forwarded to remote " |
913 | "address %.200s:%d", | |
8b32eddc | 914 | (options.local_forwards[i].listen_host == NULL) ? |
915 | (options.gateway_ports ? "*" : "LOCALHOST") : | |
dfddba3d | 916 | options.local_forwards[i].listen_host, |
917 | options.local_forwards[i].listen_port, | |
918 | options.local_forwards[i].connect_host, | |
919 | options.local_forwards[i].connect_port); | |
c6046bb0 | 920 | success += channel_setup_local_fwd_listener( |
dfddba3d | 921 | options.local_forwards[i].listen_host, |
922 | options.local_forwards[i].listen_port, | |
923 | options.local_forwards[i].connect_host, | |
924 | options.local_forwards[i].connect_port, | |
a7213e65 | 925 | options.gateway_ports, options.hpn_disabled, |
926 | options.hpn_buffer_size); | |
3c0ef626 | 927 | } |
30460aeb | 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++) { | |
dfddba3d | 935 | debug("Remote connections from %.200s:%d forwarded to " |
936 | "local address %.200s:%d", | |
2ce0bfe4 | 937 | (options.remote_forwards[i].listen_host == NULL) ? |
08822d99 | 938 | "LOCALHOST" : options.remote_forwards[i].listen_host, |
dfddba3d | 939 | options.remote_forwards[i].listen_port, |
940 | options.remote_forwards[i].connect_host, | |
941 | options.remote_forwards[i].connect_port); | |
30460aeb | 942 | if (channel_request_remote_forwarding( |
dfddba3d | 943 | options.remote_forwards[i].listen_host, |
944 | options.remote_forwards[i].listen_port, | |
945 | options.remote_forwards[i].connect_host, | |
30460aeb | 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 | } | |
5156b1a1 | 953 | client_register_global_confirm(ssh_confirm_remote_forward, |
954 | &options.remote_forwards[i]); | |
3c0ef626 | 955 | } |
fa0f0f45 | 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) { | |
08822d99 | 973 | /* Clear agent forwarding if we don't have an agent. */ |
d03f4262 | 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; | |
2ce0bfe4 | 987 | const char *display; |
3c0ef626 | 988 | |
989 | /* Enable compression if requested. */ | |
990 | if (options.compression) { | |
5156b1a1 | 991 | debug("Requesting compression at level %d.", |
992 | options.compression_level); | |
3c0ef626 | 993 | |
5156b1a1 | 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(); | |
c6046bb0 | 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) | |
7cac2b65 | 1008 | logit("Warning: Remote host refused compression."); |
3c0ef626 | 1009 | else |
5156b1a1 | 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)); | |
30460aeb | 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. */ | |
c6046bb0 | 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) | |
5156b1a1 | 1048 | logit("Warning: Remote host failed or refused to " |
1049 | "allocate a pseudo tty."); | |
3c0ef626 | 1050 | else |
5156b1a1 | 1051 | packet_disconnect("Protocol error waiting for pty " |
1052 | "request response."); | |
3c0ef626 | 1053 | } |
1054 | /* Request X11 forwarding if enabled and DISPLAY is set. */ | |
2ce0bfe4 | 1055 | display = getenv("DISPLAY"); |
1056 | if (options.forward_x11 && display != NULL) { | |
c6046bb0 | 1057 | char *proto, *data; |
3c0ef626 | 1058 | /* Get reasonable local authentication information. */ |
2ce0bfe4 | 1059 | client_x11_get_proto(display, options.xauth_location, |
1060 | options.forward_x11_trusted, &proto, &data); | |
3c0ef626 | 1061 | /* Request forwarding with authentication spoofing. */ |
5156b1a1 | 1062 | debug("Requesting X11 forwarding with authentication " |
1063 | "spoofing."); | |
2ce0bfe4 | 1064 | x11_request_forwarding_with_spoofing(0, display, proto, data); |
3c0ef626 | 1065 | |
1066 | /* Read response from the server. */ | |
c6046bb0 | 1067 | type = packet_read(); |
3c0ef626 | 1068 | if (type == SSH_SMSG_SUCCESS) { |
1069 | interactive = 1; | |
1070 | } else if (type == SSH_SMSG_FAILURE) { | |
7cac2b65 | 1071 | logit("Warning: Remote host denied X11 forwarding."); |
3c0ef626 | 1072 | } else { |
5156b1a1 | 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. */ | |
c6046bb0 | 1088 | type = packet_read(); |
1089 | packet_check_eom(); | |
3c0ef626 | 1090 | if (type != SSH_SMSG_SUCCESS) |
7cac2b65 | 1091 | logit("Warning: Remote host denied authentication agent forwarding."); |
3c0ef626 | 1092 | } |
1093 | ||
1094 | /* Initiate port forwardings. */ | |
1095 | ssh_init_forwarding(); | |
1096 | ||
e74dc197 | 1097 | /* Execute a local command */ |
1098 | if (options.local_command != NULL && | |
1099 | options.permit_local_command) | |
1100 | ssh_local_cmd(options.local_command); | |
1101 | ||
5156b1a1 | 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)); | |
5156b1a1 | 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; | |
5156b1a1 | 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 | ||
7e82606e | 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; | |
2ce0bfe4 | 1145 | const char *display; |
7e82606e | 1146 | int interactive = tty_flag; |
2ce0bfe4 | 1147 | |
1148 | display = getenv("DISPLAY"); | |
1149 | if (options.forward_x11 && display != NULL) { | |
c6046bb0 | 1150 | char *proto, *data; |
3c0ef626 | 1151 | /* Get reasonable local authentication information. */ |
2ce0bfe4 | 1152 | client_x11_get_proto(display, options.xauth_location, |
1153 | options.forward_x11_trusted, &proto, &data); | |
3c0ef626 | 1154 | /* Request forwarding with authentication spoofing. */ |
5156b1a1 | 1155 | debug("Requesting X11 forwarding with authentication " |
1156 | "spoofing."); | |
2ce0bfe4 | 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 | ||
7e82606e | 1169 | client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"), |
5156b1a1 | 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; | |
f713db99 | 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 | ||
f713db99 | 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 */ | |
6df46d40 | 1216 | /* unless the user has specifically set the tcprcvbufpoll */ |
f713db99 | 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 | ||
a7213e65 | 1220 | if(options.hpn_disabled) |
f713db99 | 1221 | { |
1222 | options.hpn_buffer_size = CHAN_SES_WINDOW_DEFAULT; | |
1223 | } | |
1224 | else if (datafellows & SSH_BUG_LARGEWINDOW) | |
1225 | { | |
1226 | debug("HPN to Non-HPN Connection"); | |
1227 | if (options.hpn_buffer_size < 0) | |
1228 | options.hpn_buffer_size = 2*1024*1024; | |
1229 | } | |
1230 | else | |
1231 | { | |
1232 | if (options.hpn_buffer_size < 0) | |
1233 | options.hpn_buffer_size = BUFFER_MAX_LEN_HPN; | |
2d7c038e | 1234 | |
1235 | /*create a socket but don't connect it */ | |
1236 | /* we use that the get the rcv socket size */ | |
1237 | sock = socket(AF_INET, SOCK_STREAM, 0); | |
1238 | /* if they are using the tcp_rcv_buf option */ | |
1239 | /* attempt to set the buffer size to that */ | |
1240 | if (options.tcp_rcv_buf) | |
1241 | setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&options.tcp_rcv_buf, | |
1242 | sizeof(options.tcp_rcv_buf)); | |
1243 | getsockopt(sock, SOL_SOCKET, SO_RCVBUF, | |
1244 | &socksize, &socksizelen); | |
1245 | close(sock); | |
1246 | debug("socksize %d", socksize); | |
f713db99 | 1247 | if (options.tcp_rcv_buf_poll <= 0) |
1248 | { | |
2d7c038e | 1249 | options.hpn_buffer_size = MIN(socksize,options.hpn_buffer_size); |
1250 | debug ("MIN of TCP RWIN and HPNBufferSize: %d", options.hpn_buffer_size); | |
f713db99 | 1251 | } |
1252 | else | |
1253 | { | |
1254 | if (options.tcp_rcv_buf > 0) | |
1255 | options.hpn_buffer_size = MIN(options.tcp_rcv_buf, options.hpn_buffer_size); | |
2d7c038e | 1256 | debug ("MIN of TCPRcvBuf and HPNBufferSize: %d", options.hpn_buffer_size); |
f713db99 | 1257 | } |
1258 | ||
1259 | } | |
1260 | ||
1261 | debug("Final hpn_buffer_size = %d", options.hpn_buffer_size); | |
1262 | ||
1263 | window = options.hpn_buffer_size; | |
1264 | ||
3c0ef626 | 1265 | packetmax = CHAN_SES_PACKET_DEFAULT; |
c6046bb0 | 1266 | if (tty_flag) { |
473db5ab | 1267 | window = 4*CHAN_SES_PACKET_DEFAULT; |
c6046bb0 | 1268 | window >>= 1; |
1269 | packetmax >>= 1; | |
3c0ef626 | 1270 | } |
1271 | c = channel_new( | |
1272 | "session", SSH_CHANNEL_OPENING, in, out, err, | |
1273 | window, packetmax, CHAN_EXTENDED_WRITE, | |
7cac2b65 | 1274 | "client-session", /*nonblock*/0); |
4834571d | 1275 | |
a7213e65 | 1276 | if ((options.tcp_rcv_buf_poll > 0) && (!options.hpn_disabled)) { |
473db5ab | 1277 | c->dynamic_window = 1; |
a7213e65 | 1278 | debug ("Enabled Dynamic Window Scaling\n"); |
473db5ab | 1279 | } |
3c0ef626 | 1280 | debug3("ssh_session2_open: channel_new: %d", c->self); |
1281 | ||
1282 | channel_send_open(c->self); | |
1283 | if (!no_shell_flag) | |
5156b1a1 | 1284 | channel_register_open_confirm(c->self, |
1285 | ssh_session2_setup, NULL); | |
3c0ef626 | 1286 | |
1287 | return c->self; | |
1288 | } | |
1289 | ||
1290 | static int | |
1291 | ssh_session2(void) | |
1292 | { | |
1293 | int id = -1; | |
1294 | ||
1295 | /* XXX should be pre-session */ | |
1296 | ssh_init_forwarding(); | |
1297 | ||
1298 | if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN)) | |
1299 | id = ssh_session2_open(); | |
1300 | ||
5156b1a1 | 1301 | /* If we don't expect to open a new session, then disallow it */ |
1302 | if (options.control_master == SSHCTL_MASTER_NO) { | |
1303 | debug("Requesting no-more-sessions@openssh.com"); | |
1304 | packet_start(SSH2_MSG_GLOBAL_REQUEST); | |
1305 | packet_put_cstring("no-more-sessions@openssh.com"); | |
1306 | packet_put_char(0); | |
1307 | packet_send(); | |
1308 | } | |
1309 | ||
08822d99 | 1310 | /* Execute a local command */ |
1311 | if (options.local_command != NULL && | |
1312 | options.permit_local_command) | |
1313 | ssh_local_cmd(options.local_command); | |
1314 | ||
fa0f0f45 | 1315 | /* Start listening for multiplex clients */ |
5156b1a1 | 1316 | muxserver_listen(); |
fa0f0f45 | 1317 | |
3c0ef626 | 1318 | /* If requested, let ssh continue in the background. */ |
5156b1a1 | 1319 | if (fork_after_authentication_flag) { |
1320 | fork_after_authentication_flag = 0; | |
3c0ef626 | 1321 | if (daemon(1, 1) < 0) |
1322 | fatal("daemon() failed: %.200s", strerror(errno)); | |
5156b1a1 | 1323 | } |
3c0ef626 | 1324 | |
1325 | return client_loop(tty_flag, tty_flag ? | |
1326 | options.escape_char : SSH_ESCAPECHAR_NONE, id); | |
1327 | } | |
1328 | ||
1329 | static void | |
1330 | load_public_identity_files(void) | |
1331 | { | |
30460aeb | 1332 | char *filename, *cp, thishost[NI_MAXHOST]; |
e74dc197 | 1333 | char *pwdir = NULL, *pwname = NULL; |
3c0ef626 | 1334 | int i = 0; |
c6046bb0 | 1335 | Key *public; |
30460aeb | 1336 | struct passwd *pw; |
3c0ef626 | 1337 | #ifdef SMARTCARD |
c6046bb0 | 1338 | Key **keys; |
1339 | ||
3c0ef626 | 1340 | if (options.smartcard_device != NULL && |
c6046bb0 | 1341 | options.num_identity_files < SSH_MAX_IDENTITY_FILES && |
30460aeb | 1342 | (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) { |
c6046bb0 | 1343 | int count = 0; |
1344 | for (i = 0; keys[i] != NULL; i++) { | |
1345 | count++; | |
5156b1a1 | 1346 | memmove(&options.identity_files[1], |
1347 | &options.identity_files[0], | |
c6046bb0 | 1348 | sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1)); |
5156b1a1 | 1349 | memmove(&options.identity_keys[1], |
1350 | &options.identity_keys[0], | |
c6046bb0 | 1351 | sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1)); |
1352 | options.num_identity_files++; | |
1353 | options.identity_keys[0] = keys[i]; | |
7cac2b65 | 1354 | options.identity_files[0] = sc_get_key_label(keys[i]); |
c6046bb0 | 1355 | } |
1356 | if (options.num_identity_files > SSH_MAX_IDENTITY_FILES) | |
1357 | options.num_identity_files = SSH_MAX_IDENTITY_FILES; | |
1358 | i = count; | |
1359 | xfree(keys); | |
3c0ef626 | 1360 | } |
1361 | #endif /* SMARTCARD */ | |
30460aeb | 1362 | if ((pw = getpwuid(original_real_uid)) == NULL) |
1363 | fatal("load_public_identity_files: getpwuid failed"); | |
e74dc197 | 1364 | pwname = xstrdup(pw->pw_name); |
1365 | pwdir = xstrdup(pw->pw_dir); | |
30460aeb | 1366 | if (gethostname(thishost, sizeof(thishost)) == -1) |
1367 | fatal("load_public_identity_files: gethostname: %s", | |
1368 | strerror(errno)); | |
3c0ef626 | 1369 | for (; i < options.num_identity_files; i++) { |
30460aeb | 1370 | cp = tilde_expand_filename(options.identity_files[i], |
3c0ef626 | 1371 | original_real_uid); |
e74dc197 | 1372 | filename = percent_expand(cp, "d", pwdir, |
1373 | "u", pwname, "l", thishost, "h", host, | |
30460aeb | 1374 | "r", options.user, (char *)NULL); |
1375 | xfree(cp); | |
3c0ef626 | 1376 | public = key_load_public(filename, NULL); |
1377 | debug("identity file %s type %d", filename, | |
1378 | public ? public->type : -1); | |
1379 | xfree(options.identity_files[i]); | |
1380 | options.identity_files[i] = filename; | |
1381 | options.identity_keys[i] = public; | |
1382 | } | |
e74dc197 | 1383 | bzero(pwname, strlen(pwname)); |
1384 | xfree(pwname); | |
1385 | bzero(pwdir, strlen(pwdir)); | |
1386 | xfree(pwdir); | |
3c0ef626 | 1387 | } |