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