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