]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
5260325f | 5 | * Ssh client program. This program can be used to log into a remote machine. |
6 | * The software supports strong authentication, encryption, and forwarding | |
7 | * of X11, TCP/IP, and authentication connections. | |
8 | * | |
bcbf86ec | 9 | * As far as I am concerned, the code I have written for this software |
10 | * can be used freely for any purpose. Any derived versions of this | |
11 | * software must be clearly marked as such, and if the derived work is | |
12 | * incompatible with the protocol description in the RFC file, it must be | |
13 | * called by a name other than "ssh" or "Secure Shell". | |
14 | * | |
15 | * Copyright (c) 1999 Niels Provos. All rights reserved. | |
16 | * | |
17 | * Modified to work with SSL by Niels Provos <provos@citi.umich.edu> | |
18 | * in Canada (German citizen). | |
19 | * | |
20 | * Redistribution and use in source and binary forms, with or without | |
21 | * modification, are permitted provided that the following conditions | |
22 | * are met: | |
23 | * 1. Redistributions of source code must retain the above copyright | |
24 | * notice, this list of conditions and the following disclaimer. | |
25 | * 2. Redistributions in binary form must reproduce the above copyright | |
26 | * notice, this list of conditions and the following disclaimer in the | |
27 | * documentation and/or other materials provided with the distribution. | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
30 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
31 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
32 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
33 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
34 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
36 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
38 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
5260325f | 39 | */ |
8efc0c15 | 40 | |
41 | #include "includes.h" | |
0659cace | 42 | RCSID("$OpenBSD: ssh.c,v 1.167 2002/03/25 17:34:27 markus Exp $"); |
8efc0c15 | 43 | |
a306f2dd | 44 | #include <openssl/evp.h> |
fa08c86b | 45 | #include <openssl/err.h> |
a306f2dd | 46 | |
8efc0c15 | 47 | #include "ssh.h" |
42f11eb2 | 48 | #include "ssh1.h" |
49 | #include "ssh2.h" | |
50 | #include "compat.h" | |
51 | #include "cipher.h" | |
52 | #include "xmalloc.h" | |
8efc0c15 | 53 | #include "packet.h" |
54 | #include "buffer.h" | |
8efc0c15 | 55 | #include "uidswap.h" |
a5efa1bb | 56 | #include "channels.h" |
a306f2dd | 57 | #include "key.h" |
4c8722d9 | 58 | #include "authfd.h" |
a306f2dd | 59 | #include "authfile.h" |
42f11eb2 | 60 | #include "pathnames.h" |
b5c334cc | 61 | #include "clientloop.h" |
42f11eb2 | 62 | #include "log.h" |
63 | #include "readconf.h" | |
64 | #include "sshconnect.h" | |
65 | #include "tildexpand.h" | |
1f3bf5aa | 66 | #include "dispatch.h" |
42f11eb2 | 67 | #include "misc.h" |
b2552997 | 68 | #include "kex.h" |
69 | #include "mac.h" | |
30dcc918 | 70 | #include "sshtty.h" |
8efc0c15 | 71 | |
383546c4 | 72 | #ifdef SMARTCARD |
383546c4 | 73 | #include "scard.h" |
dd2495cb | 74 | #endif |
383546c4 | 75 | |
f601d847 | 76 | #ifdef HAVE___PROGNAME |
77 | extern char *__progname; | |
260d427b | 78 | #else |
79 | char *__progname; | |
80 | #endif | |
f601d847 | 81 | |
48e671d5 | 82 | /* Flag indicating whether IPv4 or IPv6. This can be set on the command line. |
83 | Default value is AF_UNSPEC means both IPv4 and IPv6. */ | |
59e76f33 | 84 | #ifdef IPV4_DEFAULT |
85 | int IPv4or6 = AF_INET; | |
86 | #else | |
48e671d5 | 87 | int IPv4or6 = AF_UNSPEC; |
59e76f33 | 88 | #endif |
48e671d5 | 89 | |
5260325f | 90 | /* Flag indicating whether debug mode is on. This can be set on the command line. */ |
8efc0c15 | 91 | int debug_flag = 0; |
92 | ||
a8be9f80 | 93 | /* Flag indicating whether a tty should be allocated */ |
8efc0c15 | 94 | int tty_flag = 0; |
8abcdba4 | 95 | int no_tty_flag = 0; |
96 | int force_tty_flag = 0; | |
8efc0c15 | 97 | |
8ce64345 | 98 | /* don't exec a shell */ |
99 | int no_shell_flag = 0; | |
8ce64345 | 100 | |
aa3378df | 101 | /* |
102 | * Flag indicating that nothing should be read from stdin. This can be set | |
103 | * on the command line. | |
104 | */ | |
8efc0c15 | 105 | int stdin_null_flag = 0; |
106 | ||
aa3378df | 107 | /* |
108 | * Flag indicating that ssh should fork after authentication. This is useful | |
109 | * so that the pasphrase can be entered manually, and then ssh goes to the | |
110 | * background. | |
111 | */ | |
8efc0c15 | 112 | int fork_after_authentication_flag = 0; |
113 | ||
aa3378df | 114 | /* |
115 | * General data structure for command line options and options configurable | |
116 | * in configuration files. See readconf.h. | |
117 | */ | |
8efc0c15 | 118 | Options options; |
119 | ||
e591b98a | 120 | /* optional user configfile */ |
121 | char *config = NULL; | |
122 | ||
aa3378df | 123 | /* |
124 | * Name of the host we are connecting to. This is the name given on the | |
125 | * command line, or the HostName specified for the user-supplied name in a | |
126 | * configuration file. | |
127 | */ | |
8efc0c15 | 128 | char *host; |
129 | ||
130 | /* socket address the host resolves to */ | |
48e671d5 | 131 | struct sockaddr_storage hostaddr; |
8efc0c15 | 132 | |
8002af61 | 133 | /* Private host keys. */ |
134 | struct { | |
135 | Key **keys; | |
136 | int nkeys; | |
137 | } sensitive_data; | |
8efc0c15 | 138 | |
139 | /* Original real UID. */ | |
140 | uid_t original_real_uid; | |
141 | ||
8ce64345 | 142 | /* command to be executed */ |
143 | Buffer command; | |
144 | ||
ae810de7 | 145 | /* Should we execute a command or invoke a subsystem? */ |
146 | int subsystem_flag = 0; | |
147 | ||
8efc0c15 | 148 | /* Prints a help message to the user. This function never returns. */ |
149 | ||
396c147e | 150 | static void |
5ca51e19 | 151 | usage(void) |
8efc0c15 | 152 | { |
1fe6a48f | 153 | fprintf(stderr, "Usage: %s [options] host [command]\n", __progname); |
5260325f | 154 | fprintf(stderr, "Options:\n"); |
155 | fprintf(stderr, " -l user Log in using this user name.\n"); | |
5ca51e19 | 156 | fprintf(stderr, " -n Redirect input from " _PATH_DEVNULL ".\n"); |
e591b98a | 157 | fprintf(stderr, " -F config Config file (default: ~/%s).\n", |
158 | _PATH_SSH_USER_CONFFILE); | |
71276795 | 159 | fprintf(stderr, " -A Enable authentication agent forwarding.\n"); |
63cd7dd0 | 160 | fprintf(stderr, " -a Disable authentication agent forwarding (default).\n"); |
8efc0c15 | 161 | #ifdef AFS |
5260325f | 162 | fprintf(stderr, " -k Disable Kerberos ticket and AFS token forwarding.\n"); |
163 | #endif /* AFS */ | |
2b87da3b | 164 | fprintf(stderr, " -X Enable X11 connection forwarding.\n"); |
63cd7dd0 | 165 | fprintf(stderr, " -x Disable X11 connection forwarding (default).\n"); |
a01a5f30 | 166 | fprintf(stderr, " -i file Identity for public key authentication " |
167 | "(default: ~/.ssh/identity)\n"); | |
315dfb04 | 168 | #ifdef SMARTCARD |
169 | fprintf(stderr, " -I reader Set smartcard reader.\n"); | |
dd2495cb | 170 | #endif |
5260325f | 171 | fprintf(stderr, " -t Tty; allocate a tty even if command is given.\n"); |
8ce64345 | 172 | fprintf(stderr, " -T Do not allocate a tty.\n"); |
5260325f | 173 | fprintf(stderr, " -v Verbose; display verbose debugging messages.\n"); |
bcbf86ec | 174 | fprintf(stderr, " Multiple -v increases verbosity.\n"); |
5260325f | 175 | fprintf(stderr, " -V Display version number only.\n"); |
176 | fprintf(stderr, " -P Don't allocate a privileged port.\n"); | |
177 | fprintf(stderr, " -q Quiet; don't display any warning messages.\n"); | |
178 | fprintf(stderr, " -f Fork into background after authentication.\n"); | |
179 | fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n"); | |
180 | ||
63cd7dd0 | 181 | fprintf(stderr, " -c cipher Select encryption algorithm\n"); |
38967add | 182 | fprintf(stderr, " -m macs Specify MAC algorithms for protocol version 2.\n"); |
5260325f | 183 | fprintf(stderr, " -p port Connect to this port. Server must be on the same port.\n"); |
184 | fprintf(stderr, " -L listen-port:host:port Forward local port to remote address\n"); | |
185 | fprintf(stderr, " -R listen-port:host:port Forward remote port to local address\n"); | |
1fe6a48f | 186 | fprintf(stderr, " These cause %s to listen for connections on a port, and\n", __progname); |
5260325f | 187 | fprintf(stderr, " forward them to the other side by connecting to host:port.\n"); |
4c780c2a | 188 | fprintf(stderr, " -D port Enable dynamic application-level port forwarding.\n"); |
5260325f | 189 | fprintf(stderr, " -C Enable compression.\n"); |
8ce64345 | 190 | fprintf(stderr, " -N Do not execute a shell or command.\n"); |
5260325f | 191 | fprintf(stderr, " -g Allow remote hosts to connect to forwarded ports.\n"); |
1f3bf5aa | 192 | fprintf(stderr, " -1 Force protocol version 1.\n"); |
193 | fprintf(stderr, " -2 Force protocol version 2.\n"); | |
48e671d5 | 194 | fprintf(stderr, " -4 Use IPv4 only.\n"); |
195 | fprintf(stderr, " -6 Use IPv6 only.\n"); | |
5260325f | 196 | fprintf(stderr, " -o 'option' Process the option as if it was read from a configuration file.\n"); |
ae810de7 | 197 | fprintf(stderr, " -s Invoke command (mandatory) as SSH2 subsystem.\n"); |
63cd7dd0 | 198 | fprintf(stderr, " -b addr Local IP address.\n"); |
5260325f | 199 | exit(1); |
8efc0c15 | 200 | } |
201 | ||
5260325f | 202 | /* |
203 | * Connects to the given host using rsh (or prints an error message and exits | |
204 | * if rsh is not available). This function never returns. | |
205 | */ | |
396c147e | 206 | static void |
5260325f | 207 | rsh_connect(char *host, char *user, Buffer * command) |
8efc0c15 | 208 | { |
5260325f | 209 | char *args[10]; |
210 | int i; | |
211 | ||
212 | log("Using rsh. WARNING: Connection will not be encrypted."); | |
213 | /* Build argument list for rsh. */ | |
214 | i = 0; | |
215 | args[i++] = _PATH_RSH; | |
216 | /* host may have to come after user on some systems */ | |
217 | args[i++] = host; | |
218 | if (user) { | |
219 | args[i++] = "-l"; | |
220 | args[i++] = user; | |
221 | } | |
222 | if (buffer_len(command) > 0) { | |
223 | buffer_append(command, "\0", 1); | |
224 | args[i++] = buffer_ptr(command); | |
225 | } | |
226 | args[i++] = NULL; | |
227 | if (debug_flag) { | |
228 | for (i = 0; args[i]; i++) { | |
229 | if (i != 0) | |
230 | fprintf(stderr, " "); | |
231 | fprintf(stderr, "%s", args[i]); | |
232 | } | |
233 | fprintf(stderr, "\n"); | |
8efc0c15 | 234 | } |
5260325f | 235 | execv(_PATH_RSH, args); |
236 | perror(_PATH_RSH); | |
237 | exit(1); | |
8efc0c15 | 238 | } |
239 | ||
396c147e | 240 | static int ssh_session(void); |
241 | static int ssh_session2(void); | |
242 | static void load_public_identity_files(void); | |
8ce64345 | 243 | |
5260325f | 244 | /* |
245 | * Main program for the ssh client. | |
246 | */ | |
8efc0c15 | 247 | int |
248 | main(int ac, char **av) | |
249 | { | |
ce773142 | 250 | int i, opt, exit_status, cerr; |
57112b5a | 251 | u_short fwd_port, fwd_host_port; |
d2e3df16 | 252 | char sfwd_port[6], sfwd_host_port[6]; |
f5a1a01a | 253 | char *p, *cp, buf[256]; |
5260325f | 254 | struct stat st; |
3b1a83df | 255 | struct passwd *pw; |
8ce64345 | 256 | int dummy; |
5260325f | 257 | uid_t original_effective_uid; |
57dade33 | 258 | extern int optind, optreset; |
1812a662 | 259 | extern char *optarg; |
5260325f | 260 | |
260d427b | 261 | __progname = get_progname(av[0]); |
264dce47 | 262 | init_rng(); |
263 | ||
aa3378df | 264 | /* |
265 | * Save the original real uid. It will be needed later (uid-swapping | |
266 | * may clobber the real uid). | |
267 | */ | |
5260325f | 268 | original_real_uid = getuid(); |
269 | original_effective_uid = geteuid(); | |
270 | ||
7322ef0e | 271 | #ifdef HAVE_SETRLIMIT |
5260325f | 272 | /* If we are installed setuid root be careful to not drop core. */ |
273 | if (original_real_uid != original_effective_uid) { | |
274 | struct rlimit rlim; | |
275 | rlim.rlim_cur = rlim.rlim_max = 0; | |
276 | if (setrlimit(RLIMIT_CORE, &rlim) < 0) | |
277 | fatal("setrlimit failed: %.100s", strerror(errno)); | |
8efc0c15 | 278 | } |
3c62e7eb | 279 | #endif |
63bd8c36 | 280 | /* Get user data. */ |
281 | pw = getpwuid(original_real_uid); | |
282 | if (!pw) { | |
283 | log("You don't exist, go away!"); | |
284 | exit(1); | |
285 | } | |
286 | /* Take a copy of the returned structure. */ | |
287 | pw = pwcopy(pw); | |
288 | ||
aa3378df | 289 | /* |
290 | * Use uid-swapping to give up root privileges for the duration of | |
291 | * option processing. We will re-instantiate the rights when we are | |
292 | * ready to create the privileged port, and will permanently drop | |
293 | * them when the port has been created (actually, when the connection | |
294 | * has been made, as we may need to create the port several times). | |
295 | */ | |
63bd8c36 | 296 | temporarily_use_uid(pw); |
5260325f | 297 | |
aa3378df | 298 | /* |
299 | * Set our umask to something reasonable, as some files are created | |
300 | * with the default umask. This will make them world-readable but | |
301 | * writable only by the owner, which is ok for all files for which we | |
302 | * don't set the modes explicitly. | |
303 | */ | |
5260325f | 304 | umask(022); |
305 | ||
5260325f | 306 | /* Initialize option structure to indicate that no values have been set. */ |
307 | initialize_options(&options); | |
308 | ||
309 | /* Parse command-line arguments. */ | |
310 | host = NULL; | |
311 | ||
f5a1a01a | 312 | again: |
313 | while ((opt = getopt(ac, av, | |
e591b98a | 314 | "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVX")) != -1) { |
5260325f | 315 | switch (opt) { |
1f3bf5aa | 316 | case '1': |
317 | options.protocol = SSH_PROTO_1; | |
318 | break; | |
6ae2364d | 319 | case '2': |
320 | options.protocol = SSH_PROTO_2; | |
321 | break; | |
48e671d5 | 322 | case '4': |
323 | IPv4or6 = AF_INET; | |
324 | break; | |
48e671d5 | 325 | case '6': |
326 | IPv4or6 = AF_INET6; | |
327 | break; | |
5260325f | 328 | case 'n': |
329 | stdin_null_flag = 1; | |
330 | break; | |
5260325f | 331 | case 'f': |
332 | fork_after_authentication_flag = 1; | |
333 | stdin_null_flag = 1; | |
334 | break; | |
5260325f | 335 | case 'x': |
336 | options.forward_x11 = 0; | |
337 | break; | |
5260325f | 338 | case 'X': |
339 | options.forward_x11 = 1; | |
340 | break; | |
5260325f | 341 | case 'g': |
342 | options.gateway_ports = 1; | |
343 | break; | |
5260325f | 344 | case 'P': |
345 | options.use_privileged_port = 0; | |
346 | break; | |
5260325f | 347 | case 'a': |
348 | options.forward_agent = 0; | |
349 | break; | |
71276795 | 350 | case 'A': |
351 | options.forward_agent = 1; | |
352 | break; | |
5260325f | 353 | #ifdef AFS |
354 | case 'k': | |
355 | options.kerberos_tgt_passing = 0; | |
356 | options.afs_token_passing = 0; | |
357 | break; | |
358 | #endif | |
359 | case 'i': | |
360 | if (stat(optarg, &st) < 0) { | |
f5a1a01a | 361 | fprintf(stderr, "Warning: Identity file %s " |
362 | "does not exist.\n", optarg); | |
5260325f | 363 | break; |
364 | } | |
f5a1a01a | 365 | if (options.num_identity_files >= |
366 | SSH_MAX_IDENTITY_FILES) | |
367 | fatal("Too many identity files specified " | |
368 | "(max %d)", SSH_MAX_IDENTITY_FILES); | |
369 | options.identity_files[options.num_identity_files++] = | |
370 | xstrdup(optarg); | |
5260325f | 371 | break; |
383546c4 | 372 | case 'I': |
373 | #ifdef SMARTCARD | |
9ff6f66f | 374 | options.smartcard_device = xstrdup(optarg); |
dd2495cb | 375 | #else |
383546c4 | 376 | fprintf(stderr, "no support for smartcards.\n"); |
dd2495cb | 377 | #endif |
383546c4 | 378 | break; |
5260325f | 379 | case 't': |
8abcdba4 | 380 | if (tty_flag) |
381 | force_tty_flag = 1; | |
5260325f | 382 | tty_flag = 1; |
383 | break; | |
5260325f | 384 | case 'v': |
bcbf86ec | 385 | if (0 == debug_flag) { |
386 | debug_flag = 1; | |
387 | options.log_level = SYSLOG_LEVEL_DEBUG1; | |
388 | } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) { | |
389 | options.log_level++; | |
390 | break; | |
f5a1a01a | 391 | } else |
54b974dc | 392 | fatal("Too high debugging level."); |
bcbf86ec | 393 | /* fallthrough */ |
5260325f | 394 | case 'V': |
e68225b2 | 395 | fprintf(stderr, |
396 | "%s, SSH protocols %d.%d/%d.%d, OpenSSL 0x%8.8lx\n", | |
a8be9f80 | 397 | SSH_VERSION, |
398 | PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1, | |
e68225b2 | 399 | PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, |
400 | SSLeay()); | |
5260325f | 401 | if (opt == 'V') |
402 | exit(0); | |
5260325f | 403 | break; |
5260325f | 404 | case 'q': |
405 | options.log_level = SYSLOG_LEVEL_QUIET; | |
406 | break; | |
5260325f | 407 | case 'e': |
408 | if (optarg[0] == '^' && optarg[2] == 0 && | |
f5a1a01a | 409 | (u_char) optarg[1] >= 64 && |
410 | (u_char) optarg[1] < 128) | |
1e3b8b07 | 411 | options.escape_char = (u_char) optarg[1] & 31; |
5260325f | 412 | else if (strlen(optarg) == 1) |
1e3b8b07 | 413 | options.escape_char = (u_char) optarg[0]; |
5260325f | 414 | else if (strcmp(optarg, "none") == 0) |
4bf9c10e | 415 | options.escape_char = SSH_ESCAPECHAR_NONE; |
5260325f | 416 | else { |
f5a1a01a | 417 | fprintf(stderr, "Bad escape character '%s'.\n", |
418 | optarg); | |
5260325f | 419 | exit(1); |
420 | } | |
421 | break; | |
5260325f | 422 | case 'c': |
a306f2dd | 423 | if (ciphers_valid(optarg)) { |
424 | /* SSH2 only */ | |
425 | options.ciphers = xstrdup(optarg); | |
d0c832f3 | 426 | options.cipher = SSH_CIPHER_ILLEGAL; |
a306f2dd | 427 | } else { |
428 | /* SSH1 only */ | |
c523303b | 429 | options.cipher = cipher_number(optarg); |
430 | if (options.cipher == -1) { | |
f5a1a01a | 431 | fprintf(stderr, |
432 | "Unknown cipher type '%s'\n", | |
433 | optarg); | |
a306f2dd | 434 | exit(1); |
435 | } | |
f5a1a01a | 436 | if (options.cipher == SSH_CIPHER_3DES) |
c523303b | 437 | options.ciphers = "3des-cbc"; |
f5a1a01a | 438 | else if (options.cipher == SSH_CIPHER_BLOWFISH) |
c523303b | 439 | options.ciphers = "blowfish-cbc"; |
f5a1a01a | 440 | else |
c523303b | 441 | options.ciphers = (char *)-1; |
5260325f | 442 | } |
443 | break; | |
b2552997 | 444 | case 'm': |
445 | if (mac_valid(optarg)) | |
446 | options.macs = xstrdup(optarg); | |
447 | else { | |
f5a1a01a | 448 | fprintf(stderr, "Unknown mac type '%s'\n", |
449 | optarg); | |
b2552997 | 450 | exit(1); |
451 | } | |
452 | break; | |
5260325f | 453 | case 'p': |
2d2a2c65 | 454 | options.port = a2port(optarg); |
455 | if (options.port == 0) { | |
6031af8d | 456 | fprintf(stderr, "Bad port '%s'\n", optarg); |
457 | exit(1); | |
458 | } | |
5260325f | 459 | break; |
5260325f | 460 | case 'l': |
461 | options.user = optarg; | |
462 | break; | |
d2e3df16 | 463 | |
464 | case 'L': | |
5260325f | 465 | case 'R': |
d2e3df16 | 466 | if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]", |
467 | sfwd_port, buf, sfwd_host_port) != 3 && | |
468 | sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]", | |
469 | sfwd_port, buf, sfwd_host_port) != 3) { | |
f5a1a01a | 470 | fprintf(stderr, |
d2e3df16 | 471 | "Bad forwarding specification '%s'\n", |
f5a1a01a | 472 | optarg); |
5260325f | 473 | usage(); |
474 | /* NOTREACHED */ | |
475 | } | |
d2e3df16 | 476 | if ((fwd_port = a2port(sfwd_port)) == 0 || |
762715ce | 477 | (fwd_host_port = a2port(sfwd_host_port)) == 0) { |
f5a1a01a | 478 | fprintf(stderr, |
d2e3df16 | 479 | "Bad forwarding port(s) '%s'\n", optarg); |
480 | exit(1); | |
5260325f | 481 | } |
d2e3df16 | 482 | if (opt == 'L') |
483 | add_local_forward(&options, fwd_port, buf, | |
484 | fwd_host_port); | |
485 | else if (opt == 'R') | |
486 | add_remote_forward(&options, fwd_port, buf, | |
184eed6a | 487 | fwd_host_port); |
5260325f | 488 | break; |
0490e609 | 489 | |
490 | case 'D': | |
2d2a2c65 | 491 | fwd_port = a2port(optarg); |
492 | if (fwd_port == 0) { | |
f5a1a01a | 493 | fprintf(stderr, "Bad dynamic port '%s'\n", |
494 | optarg); | |
6031af8d | 495 | exit(1); |
496 | } | |
0490e609 | 497 | add_local_forward(&options, fwd_port, "socks4", 0); |
498 | break; | |
499 | ||
5260325f | 500 | case 'C': |
501 | options.compression = 1; | |
502 | break; | |
8ce64345 | 503 | case 'N': |
504 | no_shell_flag = 1; | |
505 | no_tty_flag = 1; | |
506 | break; | |
8ce64345 | 507 | case 'T': |
508 | no_tty_flag = 1; | |
509 | break; | |
5260325f | 510 | case 'o': |
511 | dummy = 1; | |
f5a1a01a | 512 | if (process_config_line(&options, host ? host : "", |
513 | optarg, "command-line", 0, &dummy) != 0) | |
5260325f | 514 | exit(1); |
515 | break; | |
ae810de7 | 516 | case 's': |
517 | subsystem_flag = 1; | |
518 | break; | |
3435f5a6 | 519 | case 'b': |
520 | options.bind_address = optarg; | |
521 | break; | |
e591b98a | 522 | case 'F': |
523 | config = optarg; | |
524 | break; | |
5260325f | 525 | default: |
526 | usage(); | |
527 | } | |
528 | } | |
529 | ||
f5a1a01a | 530 | ac -= optind; |
531 | av += optind; | |
532 | ||
533 | if (ac > 0 && !host && **av != '-') { | |
534 | if (strchr(*av, '@')) { | |
535 | p = xstrdup(*av); | |
536 | cp = strchr(p, '@'); | |
537 | if (cp == NULL || cp == p) | |
538 | usage(); | |
539 | options.user = p; | |
540 | *cp = '\0'; | |
541 | host = ++cp; | |
542 | } else | |
543 | host = *av; | |
544 | ac--, av++; | |
545 | if (ac > 0) { | |
546 | optind = 0; | |
547 | optreset = 1; | |
548 | goto again; | |
549 | } | |
550 | } | |
551 | ||
5260325f | 552 | /* Check that we got a host name. */ |
553 | if (!host) | |
8efc0c15 | 554 | usage(); |
5260325f | 555 | |
22d89d24 | 556 | SSLeay_add_all_algorithms(); |
fa08c86b | 557 | ERR_load_crypto_strings(); |
5e4a7219 | 558 | channel_set_af(IPv4or6); |
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 | ||
0b6fbf03 | 593 | /* Force no tty*/ |
594 | if (no_tty_flag) | |
595 | tty_flag = 0; | |
5260325f | 596 | /* Do not allocate a tty if stdin is not a tty. */ |
8abcdba4 | 597 | if (!isatty(fileno(stdin)) && !force_tty_flag) { |
5260325f | 598 | if (tty_flag) |
54b974dc | 599 | log("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) { | |
93111dfa | 615 | if (!read_config_file(config, host, &options)) |
616 | fatal("Can't open user config file %.100s: " | |
617 | "%.100s", config, strerror(errno)); | |
e591b98a | 618 | } else { |
619 | snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir, | |
620 | _PATH_SSH_USER_CONFFILE); | |
f67792f2 | 621 | (void)read_config_file(buf, host, &options); |
e591b98a | 622 | |
f67792f2 | 623 | /* Read systemwide configuration file after use config. */ |
93111dfa | 624 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, &options); |
e591b98a | 625 | } |
5260325f | 626 | |
627 | /* Fill configuration defaults. */ | |
628 | fill_default_options(&options); | |
629 | ||
630 | /* reinit */ | |
20244695 | 631 | log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1); |
5260325f | 632 | |
e339aa53 | 633 | seed_rng(); |
634 | ||
5260325f | 635 | if (options.user == NULL) |
636 | options.user = xstrdup(pw->pw_name); | |
637 | ||
638 | if (options.hostname != NULL) | |
639 | host = options.hostname; | |
640 | ||
5260325f | 641 | /* Disable rhosts authentication if not running as root. */ |
3c62e7eb | 642 | #ifdef HAVE_CYGWIN |
643 | /* Ignore uid if running under Windows */ | |
644 | if (!options.use_privileged_port) { | |
645 | #else | |
5260325f | 646 | if (original_effective_uid != 0 || !options.use_privileged_port) { |
3c62e7eb | 647 | #endif |
6ffc9c88 | 648 | debug("Rhosts Authentication disabled, " |
649 | "originating port will not be trusted."); | |
5260325f | 650 | options.rhosts_authentication = 0; |
5260325f | 651 | } |
aa3378df | 652 | /* |
653 | * If using rsh has been selected, exec it now (without trying | |
654 | * anything else). Note that we must release privileges first. | |
655 | */ | |
5260325f | 656 | if (options.use_rsh) { |
aa3378df | 657 | /* |
658 | * Restore our superuser privileges. This must be done | |
659 | * before permanently setting the uid. | |
660 | */ | |
5260325f | 661 | restore_uid(); |
662 | ||
663 | /* Switch to the original uid permanently. */ | |
63bd8c36 | 664 | permanently_set_uid(pw); |
5260325f | 665 | |
666 | /* Execute rsh. */ | |
667 | rsh_connect(host, options.user, &command); | |
668 | fatal("rsh_connect returned"); | |
8efc0c15 | 669 | } |
5260325f | 670 | /* Restore our superuser privileges. */ |
671 | restore_uid(); | |
672 | ||
6ffc9c88 | 673 | /* Open a connection to the remote host. */ |
5260325f | 674 | |
dab89049 | 675 | cerr = ssh_connect(host, &hostaddr, options.port, IPv4or6, |
6ffc9c88 | 676 | options.connection_attempts, |
677 | original_effective_uid != 0 || !options.use_privileged_port, | |
63bd8c36 | 678 | pw, options.proxy_command); |
5260325f | 679 | |
aa3378df | 680 | /* |
681 | * If we successfully made the connection, load the host private key | |
682 | * in case we will need it later for combined rsa-rhosts | |
683 | * authentication. This must be done before releasing extra | |
684 | * privileges, because the file is only readable by root. | |
685 | */ | |
8002af61 | 686 | sensitive_data.nkeys = 0; |
687 | sensitive_data.keys = NULL; | |
ce773142 | 688 | if (!cerr && (options.rhosts_rsa_authentication || |
8002af61 | 689 | options.hostbased_authentication)) { |
690 | sensitive_data.nkeys = 3; | |
691 | sensitive_data.keys = xmalloc(sensitive_data.nkeys*sizeof(Key)); | |
692 | sensitive_data.keys[0] = key_load_private_type(KEY_RSA1, | |
aeaa3d9e | 693 | _PATH_HOST_KEY_FILE, "", NULL); |
8002af61 | 694 | sensitive_data.keys[1] = key_load_private_type(KEY_DSA, |
695 | _PATH_HOST_DSA_KEY_FILE, "", NULL); | |
696 | sensitive_data.keys[2] = key_load_private_type(KEY_RSA, | |
697 | _PATH_HOST_RSA_KEY_FILE, "", NULL); | |
5260325f | 698 | } |
aa3378df | 699 | /* |
700 | * Get rid of any extra privileges that we may have. We will no | |
701 | * longer need them. Also, extra privileges could make it very hard | |
702 | * to read identity files and other non-world-readable files from the | |
703 | * user's home directory if it happens to be on a NFS volume where | |
704 | * root is mapped to nobody. | |
705 | */ | |
706 | ||
707 | /* | |
708 | * Note that some legacy systems need to postpone the following call | |
709 | * to permanently_set_uid() until the private hostkey is destroyed | |
710 | * with RSA_free(). Otherwise the calling user could ptrace() the | |
711 | * process, read the private hostkey and impersonate the host. | |
712 | * OpenBSD does not allow ptracing of setuid processes. | |
713 | */ | |
63bd8c36 | 714 | permanently_set_uid(pw); |
5260325f | 715 | |
aa3378df | 716 | /* |
717 | * Now that we are back to our own permissions, create ~/.ssh | |
718 | * directory if it doesn\'t already exist. | |
719 | */ | |
c3abff07 | 720 | snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); |
5260325f | 721 | if (stat(buf, &st) < 0) |
704b1659 | 722 | if (mkdir(buf, 0700) < 0) |
5260325f | 723 | error("Could not create directory '%.200s'.", buf); |
724 | ||
725 | /* Check if the connection failed, and try "rsh" if appropriate. */ | |
ce773142 | 726 | if (cerr) { |
727 | if (!options.fallback_to_rsh) | |
728 | exit(1); | |
5260325f | 729 | if (options.port != 0) |
ce773142 | 730 | log("Secure connection to %.100s on port %hu refused; " |
731 | "reverting to insecure method", | |
732 | host, options.port); | |
5260325f | 733 | else |
ce773142 | 734 | log("Secure connection to %.100s refused; " |
735 | "reverting to insecure method.", host); | |
5260325f | 736 | |
ce773142 | 737 | rsh_connect(host, options.user, &command); |
738 | fatal("rsh_connect returned"); | |
8efc0c15 | 739 | } |
fee56204 | 740 | /* load options.identity_files */ |
741 | load_public_identity_files(); | |
742 | ||
743 | /* Expand ~ in known host file names. */ | |
744 | /* XXX mem-leaks: */ | |
fa08c86b | 745 | options.system_hostfile = |
746 | tilde_expand_filename(options.system_hostfile, original_real_uid); | |
747 | options.user_hostfile = | |
748 | tilde_expand_filename(options.user_hostfile, original_real_uid); | |
749 | options.system_hostfile2 = | |
750 | tilde_expand_filename(options.system_hostfile2, original_real_uid); | |
751 | options.user_hostfile2 = | |
752 | tilde_expand_filename(options.user_hostfile2, original_real_uid); | |
5260325f | 753 | |
67b75437 | 754 | signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ |
755 | ||
5260325f | 756 | /* Log into the remote system. This never returns if the login fails. */ |
8002af61 | 757 | ssh_login(sensitive_data.keys, sensitive_data.nkeys, |
758 | host, (struct sockaddr *)&hostaddr, pw); | |
759 | ||
760 | /* We no longer need the private host keys. Clear them now. */ | |
761 | if (sensitive_data.nkeys != 0) { | |
762 | for (i = 0; i < sensitive_data.nkeys; i++) { | |
763 | if (sensitive_data.keys[i] != NULL) { | |
764 | /* Destroys contents safely */ | |
765 | debug3("clear hostkey %d", i); | |
766 | key_free(sensitive_data.keys[i]); | |
767 | sensitive_data.keys[i] = NULL; | |
768 | } | |
769 | } | |
770 | xfree(sensitive_data.keys); | |
771 | } | |
05b7537a | 772 | for (i = 0; i < options.num_identity_files; i++) { |
773 | if (options.identity_files[i]) { | |
774 | xfree(options.identity_files[i]); | |
775 | options.identity_files[i] = NULL; | |
776 | } | |
777 | if (options.identity_keys[i]) { | |
778 | key_free(options.identity_keys[i]); | |
779 | options.identity_keys[i] = NULL; | |
780 | } | |
781 | } | |
5260325f | 782 | |
8ce64345 | 783 | exit_status = compat20 ? ssh_session2() : ssh_session(); |
784 | packet_close(); | |
785 | return exit_status; | |
786 | } | |
787 | ||
396c147e | 788 | static void |
b48eeb07 | 789 | x11_get_proto(char **_proto, char **_data) |
0b242b12 | 790 | { |
791 | char line[512]; | |
b48eeb07 | 792 | static char proto[512], data[512]; |
0b242b12 | 793 | FILE *f; |
794 | int got_data = 0, i; | |
75a624f0 | 795 | char *display; |
0b242b12 | 796 | |
b48eeb07 | 797 | *_proto = proto; |
798 | *_data = data; | |
799 | proto[0] = data[0] = '\0'; | |
75a624f0 | 800 | if (options.xauth_location && (display = getenv("DISPLAY"))) { |
fa649821 | 801 | /* Try to get Xauthority information for the display. */ |
75a624f0 | 802 | if (strncmp(display, "localhost:", 10) == 0) |
803 | /* | |
804 | * Handle FamilyLocal case where $DISPLAY does | |
805 | * not match an authorization entry. For this we | |
806 | * just try "xauth list unix:displaynum.screennum". | |
807 | * XXX: "localhost" match to determine FamilyLocal | |
808 | * is not perfect. | |
809 | */ | |
810 | snprintf(line, sizeof line, "%.100s list unix:%s 2>" | |
811 | _PATH_DEVNULL, options.xauth_location, display+10); | |
812 | else | |
813 | snprintf(line, sizeof line, "%.100s list %.200s 2>" | |
814 | _PATH_DEVNULL, options.xauth_location, display); | |
815 | debug2("x11_get_proto %s", line); | |
fa649821 | 816 | f = popen(line, "r"); |
817 | if (f && fgets(line, sizeof(line), f) && | |
b48eeb07 | 818 | sscanf(line, "%*s %511s %511s", proto, data) == 2) |
fa649821 | 819 | got_data = 1; |
820 | if (f) | |
821 | pclose(f); | |
822 | } | |
0b242b12 | 823 | /* |
824 | * If we didn't get authentication data, just make up some | |
825 | * data. The forwarding code will check the validity of the | |
826 | * response anyway, and substitute this data. The X11 | |
827 | * server, however, will ignore this fake data and use | |
828 | * whatever authentication mechanisms it was using otherwise | |
829 | * for the local connection. | |
830 | */ | |
831 | if (!got_data) { | |
832 | u_int32_t rand = 0; | |
833 | ||
b48eeb07 | 834 | strlcpy(proto, "MIT-MAGIC-COOKIE-1", sizeof proto); |
0b242b12 | 835 | for (i = 0; i < 16; i++) { |
836 | if (i % 4 == 0) | |
837 | rand = arc4random(); | |
b48eeb07 | 838 | snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", rand & 0xff); |
0b242b12 | 839 | rand >>= 8; |
840 | } | |
841 | } | |
842 | } | |
843 | ||
396c147e | 844 | static void |
fa08c86b | 845 | ssh_init_forwarding(void) |
846 | { | |
02a024dd | 847 | int success = 0; |
fa08c86b | 848 | int i; |
02a024dd | 849 | |
fa08c86b | 850 | /* Initiate local TCP/IP port forwardings. */ |
851 | for (i = 0; i < options.num_local_forwards; i++) { | |
852 | debug("Connections to local port %d forwarded to remote address %.200s:%d", | |
853 | options.local_forwards[i].port, | |
854 | options.local_forwards[i].host, | |
855 | options.local_forwards[i].host_port); | |
5641aefa | 856 | success += channel_setup_local_fwd_listener( |
fa08c86b | 857 | options.local_forwards[i].port, |
858 | options.local_forwards[i].host, | |
859 | options.local_forwards[i].host_port, | |
860 | options.gateway_ports); | |
861 | } | |
02a024dd | 862 | if (i > 0 && success == 0) |
863 | error("Could not request local forwarding."); | |
fa08c86b | 864 | |
865 | /* Initiate remote TCP/IP port forwardings. */ | |
866 | for (i = 0; i < options.num_remote_forwards; i++) { | |
867 | debug("Connections to remote port %d forwarded to local address %.200s:%d", | |
868 | options.remote_forwards[i].port, | |
869 | options.remote_forwards[i].host, | |
870 | options.remote_forwards[i].host_port); | |
871 | channel_request_remote_forwarding( | |
872 | options.remote_forwards[i].port, | |
873 | options.remote_forwards[i].host, | |
874 | options.remote_forwards[i].host_port); | |
875 | } | |
876 | } | |
877 | ||
396c147e | 878 | static void |
fa08c86b | 879 | check_agent_present(void) |
880 | { | |
881 | if (options.forward_agent) { | |
882 | /* Clear agent forwarding if we don\'t have an agent. */ | |
883 | int authfd = ssh_get_authentication_socket(); | |
884 | if (authfd < 0) | |
885 | options.forward_agent = 0; | |
886 | else | |
887 | ssh_close_authentication_socket(authfd); | |
888 | } | |
889 | } | |
890 | ||
396c147e | 891 | static int |
8ce64345 | 892 | ssh_session(void) |
893 | { | |
894 | int type; | |
8ce64345 | 895 | int interactive = 0; |
896 | int have_tty = 0; | |
897 | struct winsize ws; | |
8ce64345 | 898 | char *cp; |
899 | ||
5260325f | 900 | /* Enable compression if requested. */ |
901 | if (options.compression) { | |
902 | debug("Requesting compression at level %d.", options.compression_level); | |
903 | ||
904 | if (options.compression_level < 1 || options.compression_level > 9) | |
905 | fatal("Compression level must be from 1 (fast) to 9 (slow, best)."); | |
906 | ||
907 | /* Send the request. */ | |
908 | packet_start(SSH_CMSG_REQUEST_COMPRESSION); | |
909 | packet_put_int(options.compression_level); | |
910 | packet_send(); | |
911 | packet_write_wait(); | |
54a5250f | 912 | type = packet_read(); |
5260325f | 913 | if (type == SSH_SMSG_SUCCESS) |
914 | packet_start_compression(options.compression_level); | |
915 | else if (type == SSH_SMSG_FAILURE) | |
916 | log("Warning: Remote host refused compression."); | |
917 | else | |
918 | packet_disconnect("Protocol error waiting for compression response."); | |
919 | } | |
920 | /* Allocate a pseudo tty if appropriate. */ | |
921 | if (tty_flag) { | |
922 | debug("Requesting pty."); | |
923 | ||
924 | /* Start the packet. */ | |
925 | packet_start(SSH_CMSG_REQUEST_PTY); | |
926 | ||
927 | /* Store TERM in the packet. There is no limit on the | |
928 | length of the string. */ | |
929 | cp = getenv("TERM"); | |
930 | if (!cp) | |
931 | cp = ""; | |
449c5ba5 | 932 | packet_put_cstring(cp); |
5260325f | 933 | |
934 | /* Store window size in the packet. */ | |
935 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) | |
936 | memset(&ws, 0, sizeof(ws)); | |
937 | packet_put_int(ws.ws_row); | |
938 | packet_put_int(ws.ws_col); | |
939 | packet_put_int(ws.ws_xpixel); | |
940 | packet_put_int(ws.ws_ypixel); | |
941 | ||
942 | /* Store tty modes in the packet. */ | |
30dcc918 | 943 | tty_make_modes(fileno(stdin), NULL); |
5260325f | 944 | |
945 | /* Send the packet, and wait for it to leave. */ | |
946 | packet_send(); | |
947 | packet_write_wait(); | |
948 | ||
949 | /* Read response from the server. */ | |
54a5250f | 950 | type = packet_read(); |
4fe2af09 | 951 | if (type == SSH_SMSG_SUCCESS) { |
5260325f | 952 | interactive = 1; |
8ce64345 | 953 | have_tty = 1; |
4fe2af09 | 954 | } else if (type == SSH_SMSG_FAILURE) |
5260325f | 955 | log("Warning: Remote host failed or refused to allocate a pseudo tty."); |
956 | else | |
957 | packet_disconnect("Protocol error waiting for pty request response."); | |
958 | } | |
959 | /* Request X11 forwarding if enabled and DISPLAY is set. */ | |
960 | if (options.forward_x11 && getenv("DISPLAY") != NULL) { | |
b48eeb07 | 961 | char *proto, *data; |
0b242b12 | 962 | /* Get reasonable local authentication information. */ |
b48eeb07 | 963 | x11_get_proto(&proto, &data); |
0b242b12 | 964 | /* Request forwarding with authentication spoofing. */ |
5260325f | 965 | debug("Requesting X11 forwarding with authentication spoofing."); |
0b242b12 | 966 | x11_request_forwarding_with_spoofing(0, proto, data); |
5260325f | 967 | |
968 | /* Read response from the server. */ | |
54a5250f | 969 | type = packet_read(); |
5260325f | 970 | if (type == SSH_SMSG_SUCCESS) { |
5260325f | 971 | interactive = 1; |
0b242b12 | 972 | } else if (type == SSH_SMSG_FAILURE) { |
5260325f | 973 | log("Warning: Remote host denied X11 forwarding."); |
0b242b12 | 974 | } else { |
5260325f | 975 | packet_disconnect("Protocol error waiting for X11 forwarding"); |
0b242b12 | 976 | } |
5260325f | 977 | } |
978 | /* Tell the packet module whether this is an interactive session. */ | |
b5c334cc | 979 | packet_set_interactive(interactive); |
5260325f | 980 | |
981 | /* Request authentication agent forwarding if appropriate. */ | |
fa08c86b | 982 | check_agent_present(); |
983 | ||
5260325f | 984 | if (options.forward_agent) { |
985 | debug("Requesting authentication agent forwarding."); | |
986 | auth_request_forwarding(); | |
987 | ||
988 | /* Read response from the server. */ | |
54a5250f | 989 | type = packet_read(); |
95500969 | 990 | packet_check_eom(); |
5260325f | 991 | if (type != SSH_SMSG_SUCCESS) |
992 | log("Warning: Remote host denied authentication agent forwarding."); | |
993 | } | |
8efc0c15 | 994 | |
fa08c86b | 995 | /* Initiate port forwardings. */ |
996 | ssh_init_forwarding(); | |
5260325f | 997 | |
aa3378df | 998 | /* If requested, let ssh continue in the background. */ |
6ae2364d | 999 | if (fork_after_authentication_flag) |
aa3378df | 1000 | if (daemon(1, 1) < 0) |
1001 | fatal("daemon() failed: %.200s", strerror(errno)); | |
1002 | ||
1003 | /* | |
1004 | * If a command was specified on the command line, execute the | |
1005 | * command now. Otherwise request the server to start a shell. | |
1006 | */ | |
5260325f | 1007 | if (buffer_len(&command) > 0) { |
1008 | int len = buffer_len(&command); | |
1009 | if (len > 900) | |
1010 | len = 900; | |
6c0fa2b1 | 1011 | debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command)); |
5260325f | 1012 | packet_start(SSH_CMSG_EXEC_CMD); |
1013 | packet_put_string(buffer_ptr(&command), buffer_len(&command)); | |
1014 | packet_send(); | |
1015 | packet_write_wait(); | |
1016 | } else { | |
1017 | debug("Requesting shell."); | |
1018 | packet_start(SSH_CMSG_EXEC_SHELL); | |
1019 | packet_send(); | |
1020 | packet_write_wait(); | |
1021 | } | |
1022 | ||
1023 | /* Enter the interactive session. */ | |
4bf9c10e | 1024 | return client_loop(have_tty, tty_flag ? |
1025 | options.escape_char : SSH_ESCAPECHAR_NONE, 0); | |
8ce64345 | 1026 | } |
5260325f | 1027 | |
396c147e | 1028 | static void |
7819b5c3 | 1029 | client_subsystem_reply(int type, u_int32_t seq, void *ctxt) |
1f3bf5aa | 1030 | { |
1031 | int id, len; | |
1032 | ||
1033 | id = packet_get_int(); | |
1034 | len = buffer_len(&command); | |
ce91d6f8 | 1035 | if (len > 900) |
1036 | len = 900; | |
95500969 | 1037 | packet_check_eom(); |
1f3bf5aa | 1038 | if (type == SSH2_MSG_CHANNEL_FAILURE) |
1039 | fatal("Request for subsystem '%.*s' failed on channel %d", | |
6c0fa2b1 | 1040 | len, (u_char *)buffer_ptr(&command), id); |
1f3bf5aa | 1041 | } |
1042 | ||
bcdb96c2 | 1043 | /* request pty/x11/agent/tcpfwd/shell for channel */ |
396c147e | 1044 | static void |
bcdb96c2 | 1045 | ssh_session2_setup(int id, void *arg) |
8ce64345 | 1046 | { |
1047 | int len; | |
b5c334cc | 1048 | int interactive = 0; |
30dcc918 | 1049 | struct termios tio; |
b5c334cc | 1050 | |
bcdb96c2 | 1051 | debug("ssh_session2_setup: id %d", id); |
8ce64345 | 1052 | |
8ce64345 | 1053 | if (tty_flag) { |
1054 | struct winsize ws; | |
1055 | char *cp; | |
1056 | cp = getenv("TERM"); | |
1057 | if (!cp) | |
1058 | cp = ""; | |
1059 | /* Store window size in the packet. */ | |
1060 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) | |
1061 | memset(&ws, 0, sizeof(ws)); | |
1062 | ||
1063 | channel_request_start(id, "pty-req", 0); | |
1064 | packet_put_cstring(cp); | |
1065 | packet_put_int(ws.ws_col); | |
1066 | packet_put_int(ws.ws_row); | |
1067 | packet_put_int(ws.ws_xpixel); | |
1068 | packet_put_int(ws.ws_ypixel); | |
30dcc918 | 1069 | tio = get_saved_tio(); |
1070 | tty_make_modes(/*ignored*/ 0, &tio); | |
8ce64345 | 1071 | packet_send(); |
b5c334cc | 1072 | interactive = 1; |
8ce64345 | 1073 | /* XXX wait for reply */ |
1074 | } | |
0b242b12 | 1075 | if (options.forward_x11 && |
1076 | getenv("DISPLAY") != NULL) { | |
b48eeb07 | 1077 | char *proto, *data; |
0b242b12 | 1078 | /* Get reasonable local authentication information. */ |
b48eeb07 | 1079 | x11_get_proto(&proto, &data); |
0b242b12 | 1080 | /* Request forwarding with authentication spoofing. */ |
1081 | debug("Requesting X11 forwarding with authentication spoofing."); | |
1082 | x11_request_forwarding_with_spoofing(id, proto, data); | |
b5c334cc | 1083 | interactive = 1; |
0b242b12 | 1084 | /* XXX wait for reply */ |
1085 | } | |
1086 | ||
fa08c86b | 1087 | check_agent_present(); |
1088 | if (options.forward_agent) { | |
1089 | debug("Requesting authentication agent forwarding."); | |
1090 | channel_request_start(id, "auth-agent-req@openssh.com", 0); | |
1091 | packet_send(); | |
1092 | } | |
1093 | ||
8ce64345 | 1094 | len = buffer_len(&command); |
1095 | if (len > 0) { | |
1096 | if (len > 900) | |
1097 | len = 900; | |
ae810de7 | 1098 | if (subsystem_flag) { |
6c0fa2b1 | 1099 | debug("Sending subsystem: %.*s", len, (u_char *)buffer_ptr(&command)); |
1f3bf5aa | 1100 | channel_request_start(id, "subsystem", /*want reply*/ 1); |
1101 | /* register callback for reply */ | |
1102 | /* XXX we asume that client_loop has already been called */ | |
1103 | dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply); | |
1104 | dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply); | |
ae810de7 | 1105 | } else { |
6c0fa2b1 | 1106 | debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command)); |
ae810de7 | 1107 | channel_request_start(id, "exec", 0); |
1108 | } | |
ce91d6f8 | 1109 | packet_put_string(buffer_ptr(&command), buffer_len(&command)); |
8ce64345 | 1110 | packet_send(); |
1111 | } else { | |
06ee33fb | 1112 | channel_request_start(id, "shell", 0); |
1113 | packet_send(); | |
8ce64345 | 1114 | } |
1f3bf5aa | 1115 | |
b5c334cc | 1116 | packet_set_interactive(interactive); |
8ce64345 | 1117 | } |
1118 | ||
bcdb96c2 | 1119 | /* open new channel for a session */ |
396c147e | 1120 | static int |
bcdb96c2 | 1121 | ssh_session2_open(void) |
8ce64345 | 1122 | { |
719fc62f | 1123 | Channel *c; |
1124 | int window, packetmax, in, out, err; | |
2e73a022 | 1125 | |
14a9a859 | 1126 | if (stdin_null_flag) { |
5ca51e19 | 1127 | in = open(_PATH_DEVNULL, O_RDONLY); |
14a9a859 | 1128 | } else { |
1129 | in = dup(STDIN_FILENO); | |
1130 | } | |
2e73a022 | 1131 | out = dup(STDOUT_FILENO); |
1132 | err = dup(STDERR_FILENO); | |
8ce64345 | 1133 | |
1134 | if (in < 0 || out < 0 || err < 0) | |
14a9a859 | 1135 | fatal("dup() in/out/err failed"); |
8ce64345 | 1136 | |
a22aff1f | 1137 | /* enable nonblocking unless tty */ |
1138 | if (!isatty(in)) | |
1139 | set_nonblock(in); | |
1140 | if (!isatty(out)) | |
1141 | set_nonblock(out); | |
1142 | if (!isatty(err)) | |
1143 | set_nonblock(err); | |
1144 | ||
bcbf86ec | 1145 | window = CHAN_SES_WINDOW_DEFAULT; |
1146 | packetmax = CHAN_SES_PACKET_DEFAULT; | |
ea9700ba | 1147 | if (tty_flag) { |
1148 | window >>= 1; | |
1149 | packetmax >>= 1; | |
8ce64345 | 1150 | } |
719fc62f | 1151 | c = channel_new( |
8ce64345 | 1152 | "session", SSH_CHANNEL_OPENING, in, out, err, |
bcbf86ec | 1153 | window, packetmax, CHAN_EXTENDED_WRITE, |
a22aff1f | 1154 | xstrdup("client-session"), /*nonblock*/0); |
8ce64345 | 1155 | |
bcdb96c2 | 1156 | debug3("ssh_session2_open: channel_new: %d", c->self); |
eb0dd41f | 1157 | |
36e1f6a1 | 1158 | channel_send_open(c->self); |
bcdb96c2 | 1159 | if (!no_shell_flag) |
05ca0898 | 1160 | channel_register_confirm(c->self, ssh_session2_setup); |
5260325f | 1161 | |
719fc62f | 1162 | return c->self; |
eb0dd41f | 1163 | } |
1164 | ||
396c147e | 1165 | static int |
eb0dd41f | 1166 | ssh_session2(void) |
1167 | { | |
bcdb96c2 | 1168 | int id = -1; |
eb0dd41f | 1169 | |
1170 | /* XXX should be pre-session */ | |
1171 | ssh_init_forwarding(); | |
1172 | ||
bcdb96c2 | 1173 | if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN)) |
1174 | id = ssh_session2_open(); | |
eb0dd41f | 1175 | |
1176 | /* If requested, let ssh continue in the background. */ | |
1177 | if (fork_after_authentication_flag) | |
1178 | if (daemon(1, 1) < 0) | |
1179 | fatal("daemon() failed: %.200s", strerror(errno)); | |
1180 | ||
4bf9c10e | 1181 | return client_loop(tty_flag, tty_flag ? |
1182 | options.escape_char : SSH_ESCAPECHAR_NONE, id); | |
8efc0c15 | 1183 | } |
fa08c86b | 1184 | |
396c147e | 1185 | static void |
fee56204 | 1186 | load_public_identity_files(void) |
1187 | { | |
1188 | char *filename; | |
2e23cde0 | 1189 | int i = 0; |
0659cace | 1190 | Key *public; |
383546c4 | 1191 | #ifdef SMARTCARD |
0659cace | 1192 | Key **keys; |
1193 | ||
9ff6f66f | 1194 | if (options.smartcard_device != NULL && |
0659cace | 1195 | options.num_identity_files < SSH_MAX_IDENTITY_FILES && |
1196 | (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) { | |
1197 | int count = 0; | |
1198 | for (i = 0; keys[i] != NULL; i++) { | |
1199 | count++; | |
1200 | if (options.num_identity_files + 1 > SSH_MAX_IDENTITY_FILES) | |
1201 | options.num_identity_files = SSH_MAX_IDENTITY_FILES - 1; | |
1202 | memmove(&options.identity_files[1], &options.identity_files[0], | |
1203 | sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1)); | |
1204 | memmove(&options.identity_keys[1], &options.identity_keys[0], | |
1205 | sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1)); | |
1206 | options.num_identity_files++; | |
1207 | options.identity_keys[0] = keys[i]; | |
1208 | options.identity_files[0] = xstrdup("smartcard key");; | |
1209 | } | |
1210 | i = count; | |
1211 | xfree(keys); | |
383546c4 | 1212 | } |
3e984472 | 1213 | #endif /* SMARTCARD */ |
2e23cde0 | 1214 | for (; i < options.num_identity_files; i++) { |
1215 | filename = tilde_expand_filename(options.identity_files[i], | |
1216 | original_real_uid); | |
1217 | public = key_load_public(filename, NULL); | |
1218 | debug("identity file %s type %d", filename, | |
1219 | public ? public->type : -1); | |
1220 | xfree(options.identity_files[i]); | |
1221 | options.identity_files[i] = filename; | |
1222 | options.identity_keys[i] = public; | |
1223 | } | |
fee56204 | 1224 | } |