]>
Commit | Line | Data |
---|---|---|
22616013 | 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. | |
cdd66111 | 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" | |
9108f8d9 | 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> | |
9108f8d9 | 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> | |
47686178 | 74 | #include "openbsd-compat/openssl-compat.h" |
22616013 | 75 | #include "openbsd-compat/sys-queue.h" |
3c0ef626 | 76 | |
9108f8d9 | 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" | |
c9f39d2c | 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" | |
c9f39d2c | 98 | #include "sshpty.h" |
99 | #include "match.h" | |
100 | #include "msg.h" | |
c9f39d2c | 101 | #include "uidswap.h" |
9108f8d9 | 102 | #include "version.h" |
3c0ef626 | 103 | |
104 | #ifdef SMARTCARD | |
3c0ef626 | 105 | #include "scard.h" |
106 | #endif | |
107 | ||
3c0ef626 | 108 | extern char *__progname; |
3c0ef626 | 109 | |
22616013 | 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 | |
f5799ae1 | 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. */ | |
f5799ae1 | 154 | Sensitive sensitive_data; |
3c0ef626 | 155 | |
156 | /* Original real UID. */ | |
157 | uid_t original_real_uid; | |
f5799ae1 | 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 | ||
700318f3 | 166 | /* # of replies received for global requests */ |
22616013 | 167 | static int remote_forward_confirms_received = 0; |
700318f3 | 168 | |
41b2f314 | 169 | /* pid of proxycommand child process */ |
170 | pid_t proxy_command_pid = 0; | |
171 | ||
22616013 | 172 | /* mux.c */ |
173 | extern int muxserver_sock; | |
174 | extern u_int muxclient_command; | |
c9f39d2c | 175 | |
3c0ef626 | 176 | /* Prints a help message to the user. This function never returns. */ |
177 | ||
178 | static void | |
179 | usage(void) | |
180 | { | |
99be0775 | 181 | fprintf(stderr, |
d4487008 | 182 | "usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n" |
2c06c99b | 183 | " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n" |
996d5e62 | 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" | |
9108f8d9 | 187 | " [-w local_tun[:remote_tun]] [user@]hostname [command]\n" |
99be0775 | 188 | ); |
2c06c99b | 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); | |
22616013 | 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 | { | |
f5799ae1 | 206 | int i, opt, exit_status; |
cdd66111 | 207 | char *p, *cp, *line, buf[256]; |
3c0ef626 | 208 | struct stat st; |
209 | struct passwd *pw; | |
47686178 | 210 | int dummy, timeout_ms; |
3c0ef626 | 211 | extern int optind, optreset; |
212 | extern char *optarg; | |
665a873d | 213 | struct servent *sp; |
996d5e62 | 214 | Forward fwd; |
3c0ef626 | 215 | |
2c06c99b | 216 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
217 | sanitise_stdfd(); | |
218 | ||
0fff78ff | 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(); | |
cdd66111 | 228 | |
41b2f314 | 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) { | |
0fff78ff | 250 | logit("You don't exist, go away!"); |
2c06c99b | 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 | ||
22616013 | 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; | |
272 | ||
9108f8d9 | 273 | again: |
22616013 | 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': | |
0fff78ff | 284 | options.address_family = AF_INET; |
3c0ef626 | 285 | break; |
286 | case '6': | |
0fff78ff | 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; | |
cdd66111 | 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; | |
996d5e62 | 309 | case 'O': |
310 | if (strcmp(optarg, "check") == 0) | |
22616013 | 311 | muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK; |
996d5e62 | 312 | else if (strcmp(optarg, "exit") == 0) |
22616013 | 313 | muxclient_command = SSHMUX_COMMAND_TERMINATE; |
996d5e62 | 314 | else |
315 | fatal("Invalid multiplex command."); | |
316 | break; | |
41b2f314 | 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': |
cdd66111 | 327 | options.gss_deleg_creds = 0; |
3c0ef626 | 328 | break; |
d4487008 | 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 " | |
996d5e62 | 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': | |
0fff78ff | 360 | if (debug_flag == 0) { |
3c0ef626 | 361 | debug_flag = 1; |
362 | options.log_level = SYSLOG_LEVEL_DEBUG1; | |
0fff78ff | 363 | } else { |
364 | if (options.log_level < SYSLOG_LEVEL_DEBUG3) | |
365 | options.log_level++; | |
3c0ef626 | 366 | break; |
0fff78ff | 367 | } |
996d5e62 | 368 | /* FALLTHROUGH */ |
3c0ef626 | 369 | case 'V': |
99be0775 | 370 | fprintf(stderr, "%s, %s\n", |
996d5e62 | 371 | SSH_RELEASE, SSLeay_version(SSLEAY_VERSION)); |
3c0ef626 | 372 | if (opt == 'V') |
373 | exit(0); | |
374 | break; | |
2c06c99b | 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) { | |
22616013 | 380 | fprintf(stderr, |
381 | "Bad tun device '%s'\n", optarg); | |
2c06c99b | 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); | |
2c06c99b | 400 | exit(255); |
3c0ef626 | 401 | } |
402 | break; | |
403 | case 'c': | |
404 | if (ciphers_valid(optarg)) { | |
405 | /* SSH2 only */ | |
406 | options.ciphers = xstrdup(optarg); | |
c9f39d2c | 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); | |
2c06c99b | 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); | |
2c06c99b | 431 | exit(255); |
3c0ef626 | 432 | } |
433 | break; | |
c9f39d2c | 434 | case 'M': |
665a873d | 435 | if (options.control_master == SSHCTL_MASTER_YES) |
436 | options.control_master = SSHCTL_MASTER_ASK; | |
437 | else | |
438 | options.control_master = SSHCTL_MASTER_YES; | |
c9f39d2c | 439 | break; |
3c0ef626 | 440 | case 'p': |
441 | options.port = a2port(optarg); | |
442 | if (options.port == 0) { | |
443 | fprintf(stderr, "Bad port '%s'\n", optarg); | |
2c06c99b | 444 | exit(255); |
3c0ef626 | 445 | } |
446 | break; | |
447 | case 'l': | |
448 | options.user = optarg; | |
449 | break; | |
450 | ||
451 | case 'L': | |
996d5e62 | 452 | if (parse_forward(&fwd, optarg)) |
453 | add_local_forward(&options, &fwd); | |
454 | else { | |
3c0ef626 | 455 | fprintf(stderr, |
996d5e62 | 456 | "Bad local forwarding specification '%s'\n", |
3c0ef626 | 457 | optarg); |
2c06c99b | 458 | exit(255); |
3c0ef626 | 459 | } |
996d5e62 | 460 | break; |
461 | ||
462 | case 'R': | |
463 | if (parse_forward(&fwd, optarg)) { | |
464 | add_remote_forward(&options, &fwd); | |
465 | } else { | |
3c0ef626 | 466 | fprintf(stderr, |
996d5e62 | 467 | "Bad remote forwarding specification " |
468 | "'%s'\n", optarg); | |
2c06c99b | 469 | exit(255); |
3c0ef626 | 470 | } |
3c0ef626 | 471 | break; |
472 | ||
473 | case 'D': | |
996d5e62 | 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); | |
2c06c99b | 480 | exit(255); |
996d5e62 | 481 | } |
482 | if (cp != NULL) { | |
483 | fwd.listen_port = a2port(cp); | |
22616013 | 484 | fwd.listen_host = |
485 | cleanhostname(fwd.listen_host); | |
996d5e62 | 486 | } else { |
487 | fwd.listen_port = a2port(fwd.listen_host); | |
665a873d | 488 | fwd.listen_host = NULL; |
996d5e62 | 489 | } |
490 | ||
491 | if (fwd.listen_port == 0) { | |
3c0ef626 | 492 | fprintf(stderr, "Bad dynamic port '%s'\n", |
493 | optarg); | |
2c06c99b | 494 | exit(255); |
3c0ef626 | 495 | } |
996d5e62 | 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; | |
506 | break; | |
507 | case 'T': | |
508 | no_tty_flag = 1; | |
509 | break; | |
510 | case 'o': | |
511 | dummy = 1; | |
cdd66111 | 512 | line = xstrdup(optarg); |
3c0ef626 | 513 | if (process_config_line(&options, host ? host : "", |
cdd66111 | 514 | line, "command-line", 0, &dummy) != 0) |
2c06c99b | 515 | exit(255); |
cdd66111 | 516 | xfree(line); |
3c0ef626 | 517 | break; |
518 | case 's': | |
519 | subsystem_flag = 1; | |
520 | break; | |
c9f39d2c | 521 | case 'S': |
522 | if (options.control_path != NULL) | |
523 | free(options.control_path); | |
524 | options.control_path = xstrdup(optarg); | |
525 | break; | |
3c0ef626 | 526 | case 'b': |
527 | options.bind_address = optarg; | |
528 | break; | |
529 | case 'F': | |
530 | config = optarg; | |
531 | break; | |
532 | default: | |
533 | usage(); | |
534 | } | |
535 | } | |
536 | ||
537 | ac -= optind; | |
538 | av += optind; | |
539 | ||
540 | if (ac > 0 && !host && **av != '-') { | |
6a9b3198 | 541 | if (strrchr(*av, '@')) { |
3c0ef626 | 542 | p = xstrdup(*av); |
6a9b3198 | 543 | cp = strrchr(p, '@'); |
3c0ef626 | 544 | if (cp == NULL || cp == p) |
545 | usage(); | |
546 | options.user = p; | |
547 | *cp = '\0'; | |
548 | host = ++cp; | |
549 | } else | |
550 | host = *av; | |
6a9b3198 | 551 | if (ac > 1) { |
552 | optind = optreset = 1; | |
3c0ef626 | 553 | goto again; |
554 | } | |
6a9b3198 | 555 | ac--, av++; |
3c0ef626 | 556 | } |
557 | ||
558 | /* Check that we got a host name. */ | |
559 | if (!host) | |
560 | usage(); | |
561 | ||
562 | SSLeay_add_all_algorithms(); | |
563 | ERR_load_crypto_strings(); | |
3c0ef626 | 564 | |
565 | /* Initialize the command to execute on remote host. */ | |
566 | buffer_init(&command); | |
567 | ||
568 | /* | |
569 | * Save the command to execute on the remote host in a buffer. There | |
570 | * is no limit on the length of the command, except by the maximum | |
571 | * packet size. Also sets the tty flag if there is no command. | |
572 | */ | |
573 | if (!ac) { | |
574 | /* No command specified - execute shell on a tty. */ | |
575 | tty_flag = 1; | |
576 | if (subsystem_flag) { | |
577 | fprintf(stderr, | |
578 | "You must specify a subsystem to invoke.\n"); | |
579 | usage(); | |
580 | } | |
581 | } else { | |
582 | /* A command has been specified. Store it into the buffer. */ | |
583 | for (i = 0; i < ac; i++) { | |
584 | if (i) | |
585 | buffer_append(&command, " ", 1); | |
586 | buffer_append(&command, av[i], strlen(av[i])); | |
587 | } | |
588 | } | |
589 | ||
590 | /* Cannot fork to background if no command. */ | |
22616013 | 591 | if (fork_after_authentication_flag && buffer_len(&command) == 0 && |
592 | !no_shell_flag) | |
593 | fatal("Cannot fork into background without a command " | |
594 | "to execute."); | |
3c0ef626 | 595 | |
596 | /* Allocate a tty by default if no command specified. */ | |
597 | if (buffer_len(&command) == 0) | |
598 | tty_flag = 1; | |
599 | ||
41b2f314 | 600 | /* Force no tty */ |
3c0ef626 | 601 | if (no_tty_flag) |
602 | tty_flag = 0; | |
603 | /* Do not allocate a tty if stdin is not a tty. */ | |
665a873d | 604 | if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) { |
3c0ef626 | 605 | if (tty_flag) |
22616013 | 606 | logit("Pseudo-terminal will not be allocated because " |
607 | "stdin is not a terminal."); | |
3c0ef626 | 608 | tty_flag = 0; |
609 | } | |
610 | ||
611 | /* | |
612 | * Initialize "log" output. Since we are the client all output | |
613 | * actually goes to stderr. | |
614 | */ | |
22616013 | 615 | log_init(av[0], |
616 | options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, | |
3c0ef626 | 617 | SYSLOG_FACILITY_USER, 1); |
618 | ||
619 | /* | |
620 | * Read per-user configuration file. Ignore the system wide config | |
621 | * file if the user specifies a config file on the command line. | |
622 | */ | |
623 | if (config != NULL) { | |
c9f39d2c | 624 | if (!read_config_file(config, host, &options, 0)) |
3c0ef626 | 625 | fatal("Can't open user config file %.100s: " |
626 | "%.100s", config, strerror(errno)); | |
799ae497 | 627 | } else { |
3c0ef626 | 628 | snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, |
629 | _PATH_SSH_USER_CONFFILE); | |
c9f39d2c | 630 | (void)read_config_file(buf, host, &options, 1); |
3c0ef626 | 631 | |
632 | /* Read systemwide configuration file after use config. */ | |
c9f39d2c | 633 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, |
634 | &options, 0); | |
3c0ef626 | 635 | } |
636 | ||
637 | /* Fill configuration defaults. */ | |
638 | fill_default_options(&options); | |
639 | ||
0fff78ff | 640 | channel_set_af(options.address_family); |
641 | ||
3c0ef626 | 642 | /* reinit */ |
643 | log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1); | |
644 | ||
645 | seed_rng(); | |
646 | ||
647 | if (options.user == NULL) | |
648 | options.user = xstrdup(pw->pw_name); | |
649 | ||
22616013 | 650 | /* Get default port if port has not been set. */ |
651 | if (options.port == 0) { | |
652 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); | |
653 | options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; | |
654 | } | |
655 | ||
656 | if (options.local_command != NULL) { | |
657 | char thishost[NI_MAXHOST]; | |
658 | ||
659 | if (gethostname(thishost, sizeof(thishost)) == -1) | |
660 | fatal("gethostname: %s", strerror(errno)); | |
661 | snprintf(buf, sizeof(buf), "%d", options.port); | |
662 | debug3("expanding LocalCommand: %s", options.local_command); | |
663 | cp = options.local_command; | |
664 | options.local_command = percent_expand(cp, "d", pw->pw_dir, | |
665 | "h", options.hostname? options.hostname : host, | |
666 | "l", thishost, "n", host, "r", options.user, "p", buf, | |
667 | "u", pw->pw_name, (char *)NULL); | |
668 | debug3("expanded LocalCommand: %s", options.local_command); | |
669 | xfree(cp); | |
670 | } | |
671 | ||
3c0ef626 | 672 | if (options.hostname != NULL) |
673 | host = options.hostname; | |
674 | ||
0fff78ff | 675 | /* force lowercase for hostkey matching */ |
676 | if (options.host_key_alias != NULL) { | |
677 | for (p = options.host_key_alias; *p; p++) | |
678 | if (isupper(*p)) | |
9108f8d9 | 679 | *p = (char)tolower(*p); |
0fff78ff | 680 | } |
681 | ||
6a9b3198 | 682 | if (options.proxy_command != NULL && |
47686178 | 683 | strcmp(options.proxy_command, "none") == 0) { |
684 | xfree(options.proxy_command); | |
6a9b3198 | 685 | options.proxy_command = NULL; |
47686178 | 686 | } |
665a873d | 687 | if (options.control_path != NULL && |
47686178 | 688 | strcmp(options.control_path, "none") == 0) { |
689 | xfree(options.control_path); | |
665a873d | 690 | options.control_path = NULL; |
47686178 | 691 | } |
6a9b3198 | 692 | |
c9f39d2c | 693 | if (options.control_path != NULL) { |
9108f8d9 | 694 | char thishost[NI_MAXHOST]; |
695 | ||
696 | if (gethostname(thishost, sizeof(thishost)) == -1) | |
697 | fatal("gethostname: %s", strerror(errno)); | |
665a873d | 698 | snprintf(buf, sizeof(buf), "%d", options.port); |
699 | cp = tilde_expand_filename(options.control_path, | |
700 | original_real_uid); | |
47686178 | 701 | xfree(options.control_path); |
665a873d | 702 | options.control_path = percent_expand(cp, "p", buf, "h", host, |
9108f8d9 | 703 | "r", options.user, "l", thishost, (char *)NULL); |
665a873d | 704 | xfree(cp); |
c9f39d2c | 705 | } |
22616013 | 706 | if (muxclient_command != 0 && options.control_path == NULL) |
665a873d | 707 | fatal("No ControlPath specified for \"-O\" command"); |
708 | if (options.control_path != NULL) | |
22616013 | 709 | muxclient(options.control_path); |
c9f39d2c | 710 | |
47686178 | 711 | timeout_ms = options.connection_timeout * 1000; |
712 | ||
3c0ef626 | 713 | /* Open a connection to the remote host. */ |
0fff78ff | 714 | if (ssh_connect(host, &hostaddr, options.port, |
47686178 | 715 | options.address_family, options.connection_attempts, &timeout_ms, |
716 | options.tcp_keep_alive, | |
f5799ae1 | 717 | #ifdef HAVE_CYGWIN |
718 | options.use_privileged_port, | |
719 | #else | |
720 | original_effective_uid == 0 && options.use_privileged_port, | |
721 | #endif | |
722 | options.proxy_command) != 0) | |
2c06c99b | 723 | exit(255); |
3c0ef626 | 724 | |
47686178 | 725 | if (timeout_ms > 0) |
726 | debug3("timeout: %d ms remain after connect", timeout_ms); | |
727 | ||
3c0ef626 | 728 | /* |
729 | * If we successfully made the connection, load the host private key | |
730 | * in case we will need it later for combined rsa-rhosts | |
731 | * authentication. This must be done before releasing extra | |
732 | * privileges, because the file is only readable by root. | |
f5799ae1 | 733 | * If we cannot access the private keys, load the public keys |
734 | * instead and try to execute the ssh-keysign helper instead. | |
3c0ef626 | 735 | */ |
736 | sensitive_data.nkeys = 0; | |
737 | sensitive_data.keys = NULL; | |
f5799ae1 | 738 | sensitive_data.external_keysign = 0; |
739 | if (options.rhosts_rsa_authentication || | |
740 | options.hostbased_authentication) { | |
3c0ef626 | 741 | sensitive_data.nkeys = 3; |
9108f8d9 | 742 | sensitive_data.keys = xcalloc(sensitive_data.nkeys, |
41b2f314 | 743 | sizeof(Key)); |
f5799ae1 | 744 | |
745 | PRIV_START; | |
3c0ef626 | 746 | sensitive_data.keys[0] = key_load_private_type(KEY_RSA1, |
9108f8d9 | 747 | _PATH_HOST_KEY_FILE, "", NULL, NULL); |
3c0ef626 | 748 | sensitive_data.keys[1] = key_load_private_type(KEY_DSA, |
9108f8d9 | 749 | _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL); |
3c0ef626 | 750 | sensitive_data.keys[2] = key_load_private_type(KEY_RSA, |
9108f8d9 | 751 | _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL); |
f5799ae1 | 752 | PRIV_END; |
753 | ||
41b2f314 | 754 | if (options.hostbased_authentication == 1 && |
755 | sensitive_data.keys[0] == NULL && | |
f5799ae1 | 756 | sensitive_data.keys[1] == NULL && |
757 | sensitive_data.keys[2] == NULL) { | |
758 | sensitive_data.keys[1] = key_load_public( | |
759 | _PATH_HOST_DSA_KEY_FILE, NULL); | |
760 | sensitive_data.keys[2] = key_load_public( | |
761 | _PATH_HOST_RSA_KEY_FILE, NULL); | |
762 | sensitive_data.external_keysign = 1; | |
763 | } | |
3c0ef626 | 764 | } |
765 | /* | |
766 | * Get rid of any extra privileges that we may have. We will no | |
767 | * longer need them. Also, extra privileges could make it very hard | |
768 | * to read identity files and other non-world-readable files from the | |
769 | * user's home directory if it happens to be on a NFS volume where | |
770 | * root is mapped to nobody. | |
771 | */ | |
c9f39d2c | 772 | if (original_effective_uid == 0) { |
773 | PRIV_START; | |
774 | permanently_set_uid(pw); | |
775 | } | |
3c0ef626 | 776 | |
777 | /* | |
778 | * Now that we are back to our own permissions, create ~/.ssh | |
2c06c99b | 779 | * directory if it doesn't already exist. |
3c0ef626 | 780 | */ |
22616013 | 781 | snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, |
782 | strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); | |
3c0ef626 | 783 | if (stat(buf, &st) < 0) |
784 | if (mkdir(buf, 0700) < 0) | |
785 | error("Could not create directory '%.200s'.", buf); | |
786 | ||
3c0ef626 | 787 | /* load options.identity_files */ |
788 | load_public_identity_files(); | |
789 | ||
790 | /* Expand ~ in known host file names. */ | |
791 | /* XXX mem-leaks: */ | |
792 | options.system_hostfile = | |
793 | tilde_expand_filename(options.system_hostfile, original_real_uid); | |
794 | options.user_hostfile = | |
795 | tilde_expand_filename(options.user_hostfile, original_real_uid); | |
796 | options.system_hostfile2 = | |
797 | tilde_expand_filename(options.system_hostfile2, original_real_uid); | |
798 | options.user_hostfile2 = | |
799 | tilde_expand_filename(options.user_hostfile2, original_real_uid); | |
800 | ||
801 | signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ | |
802 | ||
22616013 | 803 | /* Log into the remote system. Never returns if the login fails. */ |
47686178 | 804 | ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, |
805 | pw, timeout_ms); | |
3c0ef626 | 806 | |
807 | /* We no longer need the private host keys. Clear them now. */ | |
808 | if (sensitive_data.nkeys != 0) { | |
809 | for (i = 0; i < sensitive_data.nkeys; i++) { | |
810 | if (sensitive_data.keys[i] != NULL) { | |
811 | /* Destroys contents safely */ | |
812 | debug3("clear hostkey %d", i); | |
813 | key_free(sensitive_data.keys[i]); | |
814 | sensitive_data.keys[i] = NULL; | |
815 | } | |
816 | } | |
817 | xfree(sensitive_data.keys); | |
818 | } | |
819 | for (i = 0; i < options.num_identity_files; i++) { | |
820 | if (options.identity_files[i]) { | |
821 | xfree(options.identity_files[i]); | |
822 | options.identity_files[i] = NULL; | |
823 | } | |
824 | if (options.identity_keys[i]) { | |
825 | key_free(options.identity_keys[i]); | |
826 | options.identity_keys[i] = NULL; | |
827 | } | |
828 | } | |
829 | ||
830 | exit_status = compat20 ? ssh_session2() : ssh_session(); | |
831 | packet_close(); | |
41b2f314 | 832 | |
22616013 | 833 | if (options.control_path != NULL && muxserver_sock != -1) |
c9f39d2c | 834 | unlink(options.control_path); |
835 | ||
41b2f314 | 836 | /* |
cdd66111 | 837 | * Send SIGHUP to proxy command if used. We don't wait() in |
41b2f314 | 838 | * case it hangs and instead rely on init to reap the child |
839 | */ | |
840 | if (proxy_command_pid > 1) | |
841 | kill(proxy_command_pid, SIGHUP); | |
842 | ||
3c0ef626 | 843 | return exit_status; |
844 | } | |
845 | ||
22616013 | 846 | /* Callback for remote forward global requests */ |
847 | static void | |
848 | ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt) | |
849 | { | |
850 | Forward *rfwd = (Forward *)ctxt; | |
851 | ||
852 | debug("remote forward %s for: listen %d, connect %s:%d", | |
853 | type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", | |
854 | rfwd->listen_port, rfwd->connect_host, rfwd->connect_port); | |
855 | if (type == SSH2_MSG_REQUEST_FAILURE) { | |
856 | if (options.exit_on_forward_failure) | |
857 | fatal("Error: remote port forwarding failed for " | |
858 | "listen port %d", rfwd->listen_port); | |
859 | else | |
860 | logit("Warning: remote port forwarding failed for " | |
861 | "listen port %d", rfwd->listen_port); | |
862 | } | |
863 | if (++remote_forward_confirms_received == options.num_remote_forwards) { | |
864 | debug("All remote forwarding requests processed"); | |
865 | if (fork_after_authentication_flag) { | |
866 | fork_after_authentication_flag = 0; | |
867 | if (daemon(1, 1) < 0) | |
868 | fatal("daemon() failed: %.200s", | |
869 | strerror(errno)); | |
870 | } | |
871 | } | |
872 | } | |
873 | ||
3c0ef626 | 874 | static void |
875 | ssh_init_forwarding(void) | |
876 | { | |
877 | int success = 0; | |
878 | int i; | |
879 | ||
880 | /* Initiate local TCP/IP port forwardings. */ | |
881 | for (i = 0; i < options.num_local_forwards; i++) { | |
996d5e62 | 882 | debug("Local connections to %.200s:%d forwarded to remote " |
883 | "address %.200s:%d", | |
dec6d9fe | 884 | (options.local_forwards[i].listen_host == NULL) ? |
885 | (options.gateway_ports ? "*" : "LOCALHOST") : | |
996d5e62 | 886 | options.local_forwards[i].listen_host, |
887 | options.local_forwards[i].listen_port, | |
888 | options.local_forwards[i].connect_host, | |
889 | options.local_forwards[i].connect_port); | |
e9a17296 | 890 | success += channel_setup_local_fwd_listener( |
996d5e62 | 891 | options.local_forwards[i].listen_host, |
892 | options.local_forwards[i].listen_port, | |
893 | options.local_forwards[i].connect_host, | |
894 | options.local_forwards[i].connect_port, | |
3c0ef626 | 895 | options.gateway_ports); |
896 | } | |
9108f8d9 | 897 | if (i > 0 && success != i && options.exit_on_forward_failure) |
898 | fatal("Could not request local forwarding."); | |
3c0ef626 | 899 | if (i > 0 && success == 0) |
900 | error("Could not request local forwarding."); | |
901 | ||
902 | /* Initiate remote TCP/IP port forwardings. */ | |
903 | for (i = 0; i < options.num_remote_forwards; i++) { | |
996d5e62 | 904 | debug("Remote connections from %.200s:%d forwarded to " |
905 | "local address %.200s:%d", | |
665a873d | 906 | (options.remote_forwards[i].listen_host == NULL) ? |
2c06c99b | 907 | "LOCALHOST" : options.remote_forwards[i].listen_host, |
996d5e62 | 908 | options.remote_forwards[i].listen_port, |
909 | options.remote_forwards[i].connect_host, | |
910 | options.remote_forwards[i].connect_port); | |
9108f8d9 | 911 | if (channel_request_remote_forwarding( |
996d5e62 | 912 | options.remote_forwards[i].listen_host, |
913 | options.remote_forwards[i].listen_port, | |
914 | options.remote_forwards[i].connect_host, | |
9108f8d9 | 915 | options.remote_forwards[i].connect_port) < 0) { |
916 | if (options.exit_on_forward_failure) | |
917 | fatal("Could not request remote forwarding."); | |
918 | else | |
919 | logit("Warning: Could not request remote " | |
920 | "forwarding."); | |
921 | } | |
22616013 | 922 | client_register_global_confirm(ssh_confirm_remote_forward, |
923 | &options.remote_forwards[i]); | |
3c0ef626 | 924 | } |
d4487008 | 925 | |
926 | /* Initiate tunnel forwarding. */ | |
927 | if (options.tun_open != SSH_TUNMODE_NO) { | |
928 | if (client_request_tun_fwd(options.tun_open, | |
929 | options.tun_local, options.tun_remote) == -1) { | |
930 | if (options.exit_on_forward_failure) | |
931 | fatal("Could not request tunnel forwarding."); | |
932 | else | |
933 | error("Could not request tunnel forwarding."); | |
934 | } | |
935 | } | |
3c0ef626 | 936 | } |
937 | ||
938 | static void | |
939 | check_agent_present(void) | |
940 | { | |
941 | if (options.forward_agent) { | |
2c06c99b | 942 | /* Clear agent forwarding if we don't have an agent. */ |
41b2f314 | 943 | if (!ssh_agent_present()) |
3c0ef626 | 944 | options.forward_agent = 0; |
3c0ef626 | 945 | } |
946 | } | |
947 | ||
948 | static int | |
949 | ssh_session(void) | |
950 | { | |
951 | int type; | |
3c0ef626 | 952 | int interactive = 0; |
953 | int have_tty = 0; | |
954 | struct winsize ws; | |
955 | char *cp; | |
665a873d | 956 | const char *display; |
3c0ef626 | 957 | |
958 | /* Enable compression if requested. */ | |
959 | if (options.compression) { | |
22616013 | 960 | debug("Requesting compression at level %d.", |
961 | options.compression_level); | |
3c0ef626 | 962 | |
22616013 | 963 | if (options.compression_level < 1 || |
964 | options.compression_level > 9) | |
965 | fatal("Compression level must be from 1 (fast) to " | |
966 | "9 (slow, best)."); | |
3c0ef626 | 967 | |
968 | /* Send the request. */ | |
969 | packet_start(SSH_CMSG_REQUEST_COMPRESSION); | |
970 | packet_put_int(options.compression_level); | |
971 | packet_send(); | |
972 | packet_write_wait(); | |
e9a17296 | 973 | type = packet_read(); |
3c0ef626 | 974 | if (type == SSH_SMSG_SUCCESS) |
975 | packet_start_compression(options.compression_level); | |
976 | else if (type == SSH_SMSG_FAILURE) | |
0fff78ff | 977 | logit("Warning: Remote host refused compression."); |
3c0ef626 | 978 | else |
22616013 | 979 | packet_disconnect("Protocol error waiting for " |
980 | "compression response."); | |
3c0ef626 | 981 | } |
982 | /* Allocate a pseudo tty if appropriate. */ | |
983 | if (tty_flag) { | |
984 | debug("Requesting pty."); | |
985 | ||
986 | /* Start the packet. */ | |
987 | packet_start(SSH_CMSG_REQUEST_PTY); | |
988 | ||
989 | /* Store TERM in the packet. There is no limit on the | |
990 | length of the string. */ | |
991 | cp = getenv("TERM"); | |
992 | if (!cp) | |
993 | cp = ""; | |
994 | packet_put_cstring(cp); | |
995 | ||
996 | /* Store window size in the packet. */ | |
997 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) | |
998 | memset(&ws, 0, sizeof(ws)); | |
9108f8d9 | 999 | packet_put_int((u_int)ws.ws_row); |
1000 | packet_put_int((u_int)ws.ws_col); | |
1001 | packet_put_int((u_int)ws.ws_xpixel); | |
1002 | packet_put_int((u_int)ws.ws_ypixel); | |
3c0ef626 | 1003 | |
1004 | /* Store tty modes in the packet. */ | |
1005 | tty_make_modes(fileno(stdin), NULL); | |
1006 | ||
1007 | /* Send the packet, and wait for it to leave. */ | |
1008 | packet_send(); | |
1009 | packet_write_wait(); | |
1010 | ||
1011 | /* Read response from the server. */ | |
e9a17296 | 1012 | type = packet_read(); |
3c0ef626 | 1013 | if (type == SSH_SMSG_SUCCESS) { |
1014 | interactive = 1; | |
1015 | have_tty = 1; | |
1016 | } else if (type == SSH_SMSG_FAILURE) | |
22616013 | 1017 | logit("Warning: Remote host failed or refused to " |
1018 | "allocate a pseudo tty."); | |
3c0ef626 | 1019 | else |
22616013 | 1020 | packet_disconnect("Protocol error waiting for pty " |
1021 | "request response."); | |
3c0ef626 | 1022 | } |
1023 | /* Request X11 forwarding if enabled and DISPLAY is set. */ | |
665a873d | 1024 | display = getenv("DISPLAY"); |
1025 | if (options.forward_x11 && display != NULL) { | |
e9a17296 | 1026 | char *proto, *data; |
3c0ef626 | 1027 | /* Get reasonable local authentication information. */ |
665a873d | 1028 | client_x11_get_proto(display, options.xauth_location, |
1029 | options.forward_x11_trusted, &proto, &data); | |
3c0ef626 | 1030 | /* Request forwarding with authentication spoofing. */ |
22616013 | 1031 | debug("Requesting X11 forwarding with authentication " |
1032 | "spoofing."); | |
665a873d | 1033 | x11_request_forwarding_with_spoofing(0, display, proto, data); |
3c0ef626 | 1034 | |
1035 | /* Read response from the server. */ | |
e9a17296 | 1036 | type = packet_read(); |
3c0ef626 | 1037 | if (type == SSH_SMSG_SUCCESS) { |
1038 | interactive = 1; | |
1039 | } else if (type == SSH_SMSG_FAILURE) { | |
0fff78ff | 1040 | logit("Warning: Remote host denied X11 forwarding."); |
3c0ef626 | 1041 | } else { |
22616013 | 1042 | packet_disconnect("Protocol error waiting for X11 " |
1043 | "forwarding"); | |
3c0ef626 | 1044 | } |
1045 | } | |
1046 | /* Tell the packet module whether this is an interactive session. */ | |
1047 | packet_set_interactive(interactive); | |
1048 | ||
1049 | /* Request authentication agent forwarding if appropriate. */ | |
1050 | check_agent_present(); | |
1051 | ||
1052 | if (options.forward_agent) { | |
1053 | debug("Requesting authentication agent forwarding."); | |
1054 | auth_request_forwarding(); | |
1055 | ||
1056 | /* Read response from the server. */ | |
e9a17296 | 1057 | type = packet_read(); |
1058 | packet_check_eom(); | |
3c0ef626 | 1059 | if (type != SSH_SMSG_SUCCESS) |
0fff78ff | 1060 | logit("Warning: Remote host denied authentication agent forwarding."); |
3c0ef626 | 1061 | } |
1062 | ||
1063 | /* Initiate port forwardings. */ | |
1064 | ssh_init_forwarding(); | |
1065 | ||
47686178 | 1066 | /* Execute a local command */ |
1067 | if (options.local_command != NULL && | |
1068 | options.permit_local_command) | |
1069 | ssh_local_cmd(options.local_command); | |
1070 | ||
22616013 | 1071 | /* |
1072 | * If requested and we are not interested in replies to remote | |
1073 | * forwarding requests, then let ssh continue in the background. | |
1074 | */ | |
1075 | if (fork_after_authentication_flag && | |
1076 | (!options.exit_on_forward_failure || | |
1077 | options.num_remote_forwards == 0)) { | |
1078 | fork_after_authentication_flag = 0; | |
3c0ef626 | 1079 | if (daemon(1, 1) < 0) |
1080 | fatal("daemon() failed: %.200s", strerror(errno)); | |
22616013 | 1081 | } |
3c0ef626 | 1082 | |
1083 | /* | |
1084 | * If a command was specified on the command line, execute the | |
1085 | * command now. Otherwise request the server to start a shell. | |
1086 | */ | |
1087 | if (buffer_len(&command) > 0) { | |
1088 | int len = buffer_len(&command); | |
1089 | if (len > 900) | |
1090 | len = 900; | |
22616013 | 1091 | debug("Sending command: %.*s", len, |
1092 | (u_char *)buffer_ptr(&command)); | |
3c0ef626 | 1093 | packet_start(SSH_CMSG_EXEC_CMD); |
1094 | packet_put_string(buffer_ptr(&command), buffer_len(&command)); | |
1095 | packet_send(); | |
1096 | packet_write_wait(); | |
1097 | } else { | |
1098 | debug("Requesting shell."); | |
1099 | packet_start(SSH_CMSG_EXEC_SHELL); | |
1100 | packet_send(); | |
1101 | packet_write_wait(); | |
1102 | } | |
1103 | ||
1104 | /* Enter the interactive session. */ | |
1105 | return client_loop(have_tty, tty_flag ? | |
1106 | options.escape_char : SSH_ESCAPECHAR_NONE, 0); | |
1107 | } | |
1108 | ||
c9f39d2c | 1109 | /* request pty/x11/agent/tcpfwd/shell for channel */ |
1110 | static void | |
1111 | ssh_session2_setup(int id, void *arg) | |
1112 | { | |
1113 | extern char **environ; | |
665a873d | 1114 | const char *display; |
c9f39d2c | 1115 | int interactive = tty_flag; |
665a873d | 1116 | |
1117 | display = getenv("DISPLAY"); | |
1118 | if (options.forward_x11 && display != NULL) { | |
e9a17296 | 1119 | char *proto, *data; |
3c0ef626 | 1120 | /* Get reasonable local authentication information. */ |
665a873d | 1121 | client_x11_get_proto(display, options.xauth_location, |
1122 | options.forward_x11_trusted, &proto, &data); | |
3c0ef626 | 1123 | /* Request forwarding with authentication spoofing. */ |
22616013 | 1124 | debug("Requesting X11 forwarding with authentication " |
1125 | "spoofing."); | |
665a873d | 1126 | x11_request_forwarding_with_spoofing(id, display, proto, data); |
3c0ef626 | 1127 | interactive = 1; |
1128 | /* XXX wait for reply */ | |
1129 | } | |
1130 | ||
1131 | check_agent_present(); | |
1132 | if (options.forward_agent) { | |
1133 | debug("Requesting authentication agent forwarding."); | |
1134 | channel_request_start(id, "auth-agent-req@openssh.com", 0); | |
1135 | packet_send(); | |
1136 | } | |
1137 | ||
c9f39d2c | 1138 | client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"), |
22616013 | 1139 | NULL, fileno(stdin), &command, environ); |
3c0ef626 | 1140 | |
3c0ef626 | 1141 | packet_set_interactive(interactive); |
1142 | } | |
1143 | ||
1144 | /* open new channel for a session */ | |
1145 | static int | |
1146 | ssh_session2_open(void) | |
1147 | { | |
1148 | Channel *c; | |
1149 | int window, packetmax, in, out, err; | |
1150 | ||
1151 | if (stdin_null_flag) { | |
1152 | in = open(_PATH_DEVNULL, O_RDONLY); | |
1153 | } else { | |
1154 | in = dup(STDIN_FILENO); | |
1155 | } | |
1156 | out = dup(STDOUT_FILENO); | |
1157 | err = dup(STDERR_FILENO); | |
1158 | ||
1159 | if (in < 0 || out < 0 || err < 0) | |
1160 | fatal("dup() in/out/err failed"); | |
1161 | ||
1162 | /* enable nonblocking unless tty */ | |
1163 | if (!isatty(in)) | |
1164 | set_nonblock(in); | |
1165 | if (!isatty(out)) | |
1166 | set_nonblock(out); | |
1167 | if (!isatty(err)) | |
1168 | set_nonblock(err); | |
1169 | ||
1170 | window = CHAN_SES_WINDOW_DEFAULT; | |
1171 | packetmax = CHAN_SES_PACKET_DEFAULT; | |
e9a17296 | 1172 | if (tty_flag) { |
1173 | window >>= 1; | |
1174 | packetmax >>= 1; | |
3c0ef626 | 1175 | } |
1176 | c = channel_new( | |
1177 | "session", SSH_CHANNEL_OPENING, in, out, err, | |
1178 | window, packetmax, CHAN_EXTENDED_WRITE, | |
0fff78ff | 1179 | "client-session", /*nonblock*/0); |
3c0ef626 | 1180 | |
1181 | debug3("ssh_session2_open: channel_new: %d", c->self); | |
1182 | ||
1183 | channel_send_open(c->self); | |
1184 | if (!no_shell_flag) | |
22616013 | 1185 | channel_register_open_confirm(c->self, |
1186 | ssh_session2_setup, NULL); | |
3c0ef626 | 1187 | |
1188 | return c->self; | |
1189 | } | |
1190 | ||
1191 | static int | |
1192 | ssh_session2(void) | |
1193 | { | |
1194 | int id = -1; | |
1195 | ||
1196 | /* XXX should be pre-session */ | |
1197 | ssh_init_forwarding(); | |
1198 | ||
1199 | if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN)) | |
1200 | id = ssh_session2_open(); | |
1201 | ||
22616013 | 1202 | /* If we don't expect to open a new session, then disallow it */ |
1203 | if (options.control_master == SSHCTL_MASTER_NO) { | |
1204 | debug("Requesting no-more-sessions@openssh.com"); | |
1205 | packet_start(SSH2_MSG_GLOBAL_REQUEST); | |
1206 | packet_put_cstring("no-more-sessions@openssh.com"); | |
1207 | packet_put_char(0); | |
1208 | packet_send(); | |
1209 | } | |
1210 | ||
2c06c99b | 1211 | /* Execute a local command */ |
1212 | if (options.local_command != NULL && | |
1213 | options.permit_local_command) | |
1214 | ssh_local_cmd(options.local_command); | |
1215 | ||
d4487008 | 1216 | /* Start listening for multiplex clients */ |
22616013 | 1217 | muxserver_listen(); |
d4487008 | 1218 | |
3c0ef626 | 1219 | /* If requested, let ssh continue in the background. */ |
22616013 | 1220 | if (fork_after_authentication_flag) { |
1221 | fork_after_authentication_flag = 0; | |
3c0ef626 | 1222 | if (daemon(1, 1) < 0) |
1223 | fatal("daemon() failed: %.200s", strerror(errno)); | |
22616013 | 1224 | } |
3c0ef626 | 1225 | |
1226 | return client_loop(tty_flag, tty_flag ? | |
1227 | options.escape_char : SSH_ESCAPECHAR_NONE, id); | |
1228 | } | |
1229 | ||
1230 | static void | |
1231 | load_public_identity_files(void) | |
1232 | { | |
9108f8d9 | 1233 | char *filename, *cp, thishost[NI_MAXHOST]; |
47686178 | 1234 | char *pwdir = NULL, *pwname = NULL; |
3c0ef626 | 1235 | int i = 0; |
700318f3 | 1236 | Key *public; |
9108f8d9 | 1237 | struct passwd *pw; |
3c0ef626 | 1238 | #ifdef SMARTCARD |
700318f3 | 1239 | Key **keys; |
1240 | ||
3c0ef626 | 1241 | if (options.smartcard_device != NULL && |
700318f3 | 1242 | options.num_identity_files < SSH_MAX_IDENTITY_FILES && |
9108f8d9 | 1243 | (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) { |
700318f3 | 1244 | int count = 0; |
1245 | for (i = 0; keys[i] != NULL; i++) { | |
1246 | count++; | |
22616013 | 1247 | memmove(&options.identity_files[1], |
1248 | &options.identity_files[0], | |
700318f3 | 1249 | sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1)); |
22616013 | 1250 | memmove(&options.identity_keys[1], |
1251 | &options.identity_keys[0], | |
700318f3 | 1252 | sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1)); |
1253 | options.num_identity_files++; | |
1254 | options.identity_keys[0] = keys[i]; | |
0fff78ff | 1255 | options.identity_files[0] = sc_get_key_label(keys[i]); |
700318f3 | 1256 | } |
1257 | if (options.num_identity_files > SSH_MAX_IDENTITY_FILES) | |
1258 | options.num_identity_files = SSH_MAX_IDENTITY_FILES; | |
1259 | i = count; | |
1260 | xfree(keys); | |
3c0ef626 | 1261 | } |
1262 | #endif /* SMARTCARD */ | |
9108f8d9 | 1263 | if ((pw = getpwuid(original_real_uid)) == NULL) |
1264 | fatal("load_public_identity_files: getpwuid failed"); | |
47686178 | 1265 | pwname = xstrdup(pw->pw_name); |
1266 | pwdir = xstrdup(pw->pw_dir); | |
9108f8d9 | 1267 | if (gethostname(thishost, sizeof(thishost)) == -1) |
1268 | fatal("load_public_identity_files: gethostname: %s", | |
1269 | strerror(errno)); | |
3c0ef626 | 1270 | for (; i < options.num_identity_files; i++) { |
9108f8d9 | 1271 | cp = tilde_expand_filename(options.identity_files[i], |
3c0ef626 | 1272 | original_real_uid); |
47686178 | 1273 | filename = percent_expand(cp, "d", pwdir, |
1274 | "u", pwname, "l", thishost, "h", host, | |
9108f8d9 | 1275 | "r", options.user, (char *)NULL); |
1276 | xfree(cp); | |
3c0ef626 | 1277 | public = key_load_public(filename, NULL); |
1278 | debug("identity file %s type %d", filename, | |
1279 | public ? public->type : -1); | |
1280 | xfree(options.identity_files[i]); | |
1281 | options.identity_files[i] = filename; | |
1282 | options.identity_keys[i] = public; | |
1283 | } | |
47686178 | 1284 | bzero(pwname, strlen(pwname)); |
1285 | xfree(pwname); | |
1286 | bzero(pwdir, strlen(pwdir)); | |
1287 | xfree(pwdir); | |
3c0ef626 | 1288 | } |