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