]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: ssh.c,v 1.331 2010/01/11 01:39:46 dtucker Exp $ */ | |
2 | /* | |
3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | |
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
5 | * All rights reserved | |
6 | * Ssh client program. This program can be used to log into a remote machine. | |
7 | * The software supports strong authentication, encryption, and forwarding | |
8 | * of X11, TCP/IP, and authentication connections. | |
9 | * | |
10 | * As far as I am concerned, the code I have written for this software | |
11 | * can be used freely for any purpose. Any derived versions of this | |
12 | * software must be clearly marked as such, and if the derived work is | |
13 | * incompatible with the protocol description in the RFC file, it must be | |
14 | * called by a name other than "ssh" or "Secure Shell". | |
15 | * | |
16 | * Copyright (c) 1999 Niels Provos. All rights reserved. | |
17 | * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved. | |
18 | * | |
19 | * Modified to work with SSL by Niels Provos <provos@citi.umich.edu> | |
20 | * in Canada (German citizen). | |
21 | * | |
22 | * Redistribution and use in source and binary forms, with or without | |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the above copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * | |
31 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
32 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
33 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
34 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
35 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
36 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
37 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
38 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
40 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
41 | */ | |
42 | ||
43 | #include "includes.h" | |
44 | ||
45 | #include <sys/types.h> | |
46 | #ifdef HAVE_SYS_STAT_H | |
47 | # include <sys/stat.h> | |
48 | #endif | |
49 | #include <sys/resource.h> | |
50 | #include <sys/ioctl.h> | |
51 | #include <sys/param.h> | |
52 | #include <sys/socket.h> | |
53 | ||
54 | #include <ctype.h> | |
55 | #include <errno.h> | |
56 | #include <fcntl.h> | |
57 | #include <netdb.h> | |
58 | #ifdef HAVE_PATHS_H | |
59 | #include <paths.h> | |
60 | #endif | |
61 | #include <pwd.h> | |
62 | #include <signal.h> | |
63 | #include <stdarg.h> | |
64 | #include <stddef.h> | |
65 | #include <stdio.h> | |
66 | #include <stdlib.h> | |
67 | #include <string.h> | |
68 | #include <unistd.h> | |
69 | ||
70 | #include <netinet/in.h> | |
71 | #include <arpa/inet.h> | |
72 | ||
73 | #include <openssl/evp.h> | |
74 | #include <openssl/err.h> | |
75 | #include "openbsd-compat/openssl-compat.h" | |
76 | #include "openbsd-compat/sys-queue.h" | |
77 | ||
78 | #include "xmalloc.h" | |
79 | #include "ssh.h" | |
80 | #include "ssh1.h" | |
81 | #include "ssh2.h" | |
82 | #include "compat.h" | |
83 | #include "cipher.h" | |
84 | #include "packet.h" | |
85 | #include "buffer.h" | |
86 | #include "channels.h" | |
87 | #include "key.h" | |
88 | #include "authfd.h" | |
89 | #include "authfile.h" | |
90 | #include "pathnames.h" | |
91 | #include "dispatch.h" | |
92 | #include "clientloop.h" | |
93 | #include "log.h" | |
94 | #include "readconf.h" | |
95 | #include "sshconnect.h" | |
96 | #include "misc.h" | |
97 | #include "kex.h" | |
98 | #include "mac.h" | |
99 | #include "sshpty.h" | |
100 | #include "match.h" | |
101 | #include "msg.h" | |
102 | #include "uidswap.h" | |
103 | #include "roaming.h" | |
104 | #include "version.h" | |
105 | ||
106 | #ifdef SMARTCARD | |
107 | #include "scard.h" | |
108 | #endif | |
109 | ||
110 | extern char *__progname; | |
111 | ||
112 | /* Flag indicating whether debug mode is on. May be set on the command line. */ | |
113 | int debug_flag = 0; | |
114 | ||
115 | /* Flag indicating whether a tty should be allocated */ | |
116 | int tty_flag = 0; | |
117 | int no_tty_flag = 0; | |
118 | int force_tty_flag = 0; | |
119 | ||
120 | /* don't exec a shell */ | |
121 | int no_shell_flag = 0; | |
122 | ||
123 | /* | |
124 | * Flag indicating that nothing should be read from stdin. This can be set | |
125 | * on the command line. | |
126 | */ | |
127 | int stdin_null_flag = 0; | |
128 | ||
129 | /* | |
130 | * Flag indicating that ssh should fork after authentication. This is useful | |
131 | * so that the passphrase can be entered manually, and then ssh goes to the | |
132 | * background. | |
133 | */ | |
134 | int fork_after_authentication_flag = 0; | |
135 | ||
136 | /* forward stdio to remote host and port */ | |
137 | char *stdio_forward_host = NULL; | |
138 | int stdio_forward_port = 0; | |
139 | ||
140 | /* | |
141 | * General data structure for command line options and options configurable | |
142 | * in configuration files. See readconf.h. | |
143 | */ | |
144 | Options options; | |
145 | ||
146 | /* optional user configfile */ | |
147 | char *config = NULL; | |
148 | ||
149 | /* | |
150 | * Name of the host we are connecting to. This is the name given on the | |
151 | * command line, or the HostName specified for the user-supplied name in a | |
152 | * configuration file. | |
153 | */ | |
154 | char *host; | |
155 | ||
156 | /* socket address the host resolves to */ | |
157 | struct sockaddr_storage hostaddr; | |
158 | ||
159 | /* Private host keys. */ | |
160 | Sensitive sensitive_data; | |
161 | ||
162 | /* Original real UID. */ | |
163 | uid_t original_real_uid; | |
164 | uid_t original_effective_uid; | |
165 | ||
166 | /* command to be executed */ | |
167 | Buffer command; | |
168 | ||
169 | /* Should we execute a command or invoke a subsystem? */ | |
170 | int subsystem_flag = 0; | |
171 | ||
172 | /* # of replies received for global requests */ | |
173 | static int remote_forward_confirms_received = 0; | |
174 | ||
175 | /* pid of proxycommand child process */ | |
176 | pid_t proxy_command_pid = 0; | |
177 | ||
178 | /* mux.c */ | |
179 | extern int muxserver_sock; | |
180 | extern u_int muxclient_command; | |
181 | ||
182 | /* Prints a help message to the user. This function never returns. */ | |
183 | ||
184 | static void | |
185 | usage(void) | |
186 | { | |
187 | fprintf(stderr, | |
188 | "usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]\n" | |
189 | " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n" | |
190 | " [-i identity_file] [-L [bind_address:]port:host:hostport]\n" | |
191 | " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n" | |
192 | " [-R [bind_address:]port:host:hostport] [-S ctl_path]\n" | |
193 | " [-W host:port] [-w local_tun[:remote_tun]]\n" | |
194 | " [user@]hostname [command]\n" | |
195 | ); | |
196 | exit(255); | |
197 | } | |
198 | ||
199 | static int ssh_session(void); | |
200 | static int ssh_session2(void); | |
201 | static void load_public_identity_files(void); | |
202 | ||
203 | /* from muxclient.c */ | |
204 | void muxclient(const char *); | |
205 | void muxserver_listen(void); | |
206 | ||
207 | /* | |
208 | * Main program for the ssh client. | |
209 | */ | |
210 | int | |
211 | main(int ac, char **av) | |
212 | { | |
213 | int i, r, opt, exit_status, use_syslog; | |
214 | char *p, *cp, *line, *argv0, buf[MAXPATHLEN]; | |
215 | struct stat st; | |
216 | struct passwd *pw; | |
217 | int dummy, timeout_ms; | |
218 | extern int optind, optreset; | |
219 | extern char *optarg; | |
220 | struct servent *sp; | |
221 | Forward fwd; | |
222 | ||
223 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ | |
224 | sanitise_stdfd(); | |
225 | ||
226 | __progname = ssh_get_progname(av[0]); | |
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(); | |
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; | |
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 | logit("You don't exist, go away!"); | |
258 | exit(255); | |
259 | } | |
260 | /* Take a copy of the returned structure. */ | |
261 | pw = pwcopy(pw); | |
262 | ||
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 | /* | |
272 | * Initialize option structure to indicate that no values have been | |
273 | * set. | |
274 | */ | |
275 | initialize_options(&options); | |
276 | ||
277 | /* Parse command-line arguments. */ | |
278 | host = NULL; | |
279 | use_syslog = 0; | |
280 | argv0 = av[0]; | |
281 | ||
282 | again: | |
283 | while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx" | |
284 | "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) { | |
285 | switch (opt) { | |
286 | case '1': | |
287 | options.protocol = SSH_PROTO_1; | |
288 | break; | |
289 | case '2': | |
290 | options.protocol = SSH_PROTO_2; | |
291 | break; | |
292 | case '4': | |
293 | options.address_family = AF_INET; | |
294 | break; | |
295 | case '6': | |
296 | options.address_family = AF_INET6; | |
297 | break; | |
298 | case 'n': | |
299 | stdin_null_flag = 1; | |
300 | break; | |
301 | case 'f': | |
302 | fork_after_authentication_flag = 1; | |
303 | stdin_null_flag = 1; | |
304 | break; | |
305 | case 'x': | |
306 | options.forward_x11 = 0; | |
307 | break; | |
308 | case 'X': | |
309 | options.forward_x11 = 1; | |
310 | break; | |
311 | case 'y': | |
312 | use_syslog = 1; | |
313 | break; | |
314 | case 'Y': | |
315 | options.forward_x11 = 1; | |
316 | options.forward_x11_trusted = 1; | |
317 | break; | |
318 | case 'g': | |
319 | options.gateway_ports = 1; | |
320 | break; | |
321 | case 'O': | |
322 | if (strcmp(optarg, "check") == 0) | |
323 | muxclient_command = SSHMUX_COMMAND_ALIVE_CHECK; | |
324 | else if (strcmp(optarg, "exit") == 0) | |
325 | muxclient_command = SSHMUX_COMMAND_TERMINATE; | |
326 | else | |
327 | fatal("Invalid multiplex command."); | |
328 | break; | |
329 | case 'P': /* deprecated */ | |
330 | options.use_privileged_port = 0; | |
331 | break; | |
332 | case 'a': | |
333 | options.forward_agent = 0; | |
334 | break; | |
335 | case 'A': | |
336 | options.forward_agent = 1; | |
337 | break; | |
338 | case 'k': | |
339 | options.gss_deleg_creds = 0; | |
340 | break; | |
341 | case 'K': | |
342 | options.gss_authentication = 1; | |
343 | options.gss_deleg_creds = 1; | |
344 | break; | |
345 | case 'i': | |
346 | if (stat(optarg, &st) < 0) { | |
347 | fprintf(stderr, "Warning: Identity file %s " | |
348 | "not accessible: %s.\n", optarg, | |
349 | strerror(errno)); | |
350 | break; | |
351 | } | |
352 | if (options.num_identity_files >= | |
353 | SSH_MAX_IDENTITY_FILES) | |
354 | fatal("Too many identity files specified " | |
355 | "(max %d)", SSH_MAX_IDENTITY_FILES); | |
356 | options.identity_files[options.num_identity_files++] = | |
357 | xstrdup(optarg); | |
358 | break; | |
359 | case 'I': | |
360 | #ifdef SMARTCARD | |
361 | options.smartcard_device = xstrdup(optarg); | |
362 | #else | |
363 | fprintf(stderr, "no support for smartcards.\n"); | |
364 | #endif | |
365 | break; | |
366 | case 't': | |
367 | if (tty_flag) | |
368 | force_tty_flag = 1; | |
369 | tty_flag = 1; | |
370 | break; | |
371 | case 'v': | |
372 | if (debug_flag == 0) { | |
373 | debug_flag = 1; | |
374 | options.log_level = SYSLOG_LEVEL_DEBUG1; | |
375 | } else { | |
376 | if (options.log_level < SYSLOG_LEVEL_DEBUG3) | |
377 | options.log_level++; | |
378 | break; | |
379 | } | |
380 | /* FALLTHROUGH */ | |
381 | case 'V': | |
382 | fprintf(stderr, "%s, %s\n", | |
383 | SSH_RELEASE, SSLeay_version(SSLEAY_VERSION)); | |
384 | if (opt == 'V') | |
385 | exit(0); | |
386 | break; | |
387 | case 'w': | |
388 | if (options.tun_open == -1) | |
389 | options.tun_open = SSH_TUNMODE_DEFAULT; | |
390 | options.tun_local = a2tun(optarg, &options.tun_remote); | |
391 | if (options.tun_local == SSH_TUNID_ERR) { | |
392 | fprintf(stderr, | |
393 | "Bad tun device '%s'\n", optarg); | |
394 | exit(255); | |
395 | } | |
396 | break; | |
397 | case 'W': | |
398 | if (parse_forward(&fwd, optarg, 1, 0)) { | |
399 | stdio_forward_host = fwd.listen_host; | |
400 | stdio_forward_port = fwd.listen_port; | |
401 | xfree(fwd.connect_host); | |
402 | } else { | |
403 | fprintf(stderr, | |
404 | "Bad stdio forwarding specification '%s'\n", | |
405 | optarg); | |
406 | exit(255); | |
407 | } | |
408 | no_tty_flag = 1; | |
409 | no_shell_flag = 1; | |
410 | options.clear_forwardings = 1; | |
411 | options.exit_on_forward_failure = 1; | |
412 | break; | |
413 | case 'q': | |
414 | options.log_level = SYSLOG_LEVEL_QUIET; | |
415 | break; | |
416 | case 'e': | |
417 | if (optarg[0] == '^' && optarg[2] == 0 && | |
418 | (u_char) optarg[1] >= 64 && | |
419 | (u_char) optarg[1] < 128) | |
420 | options.escape_char = (u_char) optarg[1] & 31; | |
421 | else if (strlen(optarg) == 1) | |
422 | options.escape_char = (u_char) optarg[0]; | |
423 | else if (strcmp(optarg, "none") == 0) | |
424 | options.escape_char = SSH_ESCAPECHAR_NONE; | |
425 | else { | |
426 | fprintf(stderr, "Bad escape character '%s'.\n", | |
427 | optarg); | |
428 | exit(255); | |
429 | } | |
430 | break; | |
431 | case 'c': | |
432 | if (ciphers_valid(optarg)) { | |
433 | /* SSH2 only */ | |
434 | options.ciphers = xstrdup(optarg); | |
435 | options.cipher = SSH_CIPHER_INVALID; | |
436 | } else { | |
437 | /* SSH1 only */ | |
438 | options.cipher = cipher_number(optarg); | |
439 | if (options.cipher == -1) { | |
440 | fprintf(stderr, | |
441 | "Unknown cipher type '%s'\n", | |
442 | optarg); | |
443 | exit(255); | |
444 | } | |
445 | if (options.cipher == SSH_CIPHER_3DES) | |
446 | options.ciphers = "3des-cbc"; | |
447 | else if (options.cipher == SSH_CIPHER_BLOWFISH) | |
448 | options.ciphers = "blowfish-cbc"; | |
449 | else | |
450 | options.ciphers = (char *)-1; | |
451 | } | |
452 | break; | |
453 | case 'm': | |
454 | if (mac_valid(optarg)) | |
455 | options.macs = xstrdup(optarg); | |
456 | else { | |
457 | fprintf(stderr, "Unknown mac type '%s'\n", | |
458 | optarg); | |
459 | exit(255); | |
460 | } | |
461 | break; | |
462 | case 'M': | |
463 | if (options.control_master == SSHCTL_MASTER_YES) | |
464 | options.control_master = SSHCTL_MASTER_ASK; | |
465 | else | |
466 | options.control_master = SSHCTL_MASTER_YES; | |
467 | break; | |
468 | case 'p': | |
469 | options.port = a2port(optarg); | |
470 | if (options.port <= 0) { | |
471 | fprintf(stderr, "Bad port '%s'\n", optarg); | |
472 | exit(255); | |
473 | } | |
474 | break; | |
475 | case 'l': | |
476 | options.user = optarg; | |
477 | break; | |
478 | ||
479 | case 'L': | |
480 | if (parse_forward(&fwd, optarg, 0, 0)) | |
481 | add_local_forward(&options, &fwd); | |
482 | else { | |
483 | fprintf(stderr, | |
484 | "Bad local forwarding specification '%s'\n", | |
485 | optarg); | |
486 | exit(255); | |
487 | } | |
488 | break; | |
489 | ||
490 | case 'R': | |
491 | if (parse_forward(&fwd, optarg, 0, 1)) { | |
492 | add_remote_forward(&options, &fwd); | |
493 | } else { | |
494 | fprintf(stderr, | |
495 | "Bad remote forwarding specification " | |
496 | "'%s'\n", optarg); | |
497 | exit(255); | |
498 | } | |
499 | break; | |
500 | ||
501 | case 'D': | |
502 | if (parse_forward(&fwd, optarg, 1, 0)) { | |
503 | add_local_forward(&options, &fwd); | |
504 | } else { | |
505 | fprintf(stderr, | |
506 | "Bad dynamic forwarding specification " | |
507 | "'%s'\n", optarg); | |
508 | exit(255); | |
509 | } | |
510 | break; | |
511 | ||
512 | case 'C': | |
513 | options.compression = 1; | |
514 | break; | |
515 | case 'N': | |
516 | no_shell_flag = 1; | |
517 | no_tty_flag = 1; | |
518 | break; | |
519 | case 'T': | |
520 | no_tty_flag = 1; | |
521 | break; | |
522 | case 'o': | |
523 | dummy = 1; | |
524 | line = xstrdup(optarg); | |
525 | if (process_config_line(&options, host ? host : "", | |
526 | line, "command-line", 0, &dummy) != 0) | |
527 | exit(255); | |
528 | xfree(line); | |
529 | break; | |
530 | case 's': | |
531 | subsystem_flag = 1; | |
532 | break; | |
533 | case 'S': | |
534 | if (options.control_path != NULL) | |
535 | free(options.control_path); | |
536 | options.control_path = xstrdup(optarg); | |
537 | break; | |
538 | case 'b': | |
539 | options.bind_address = optarg; | |
540 | break; | |
541 | case 'F': | |
542 | config = optarg; | |
543 | break; | |
544 | default: | |
545 | usage(); | |
546 | } | |
547 | } | |
548 | ||
549 | ac -= optind; | |
550 | av += optind; | |
551 | ||
552 | if (ac > 0 && !host) { | |
553 | if (strrchr(*av, '@')) { | |
554 | p = xstrdup(*av); | |
555 | cp = strrchr(p, '@'); | |
556 | if (cp == NULL || cp == p) | |
557 | usage(); | |
558 | options.user = p; | |
559 | *cp = '\0'; | |
560 | host = ++cp; | |
561 | } else | |
562 | host = *av; | |
563 | if (ac > 1) { | |
564 | optind = optreset = 1; | |
565 | goto again; | |
566 | } | |
567 | ac--, av++; | |
568 | } | |
569 | ||
570 | /* Check that we got a host name. */ | |
571 | if (!host) | |
572 | usage(); | |
573 | ||
574 | SSLeay_add_all_algorithms(); | |
575 | ERR_load_crypto_strings(); | |
576 | ||
577 | /* Initialize the command to execute on remote host. */ | |
578 | buffer_init(&command); | |
579 | ||
580 | /* | |
581 | * Save the command to execute on the remote host in a buffer. There | |
582 | * is no limit on the length of the command, except by the maximum | |
583 | * packet size. Also sets the tty flag if there is no command. | |
584 | */ | |
585 | if (!ac) { | |
586 | /* No command specified - execute shell on a tty. */ | |
587 | tty_flag = 1; | |
588 | if (subsystem_flag) { | |
589 | fprintf(stderr, | |
590 | "You must specify a subsystem to invoke.\n"); | |
591 | usage(); | |
592 | } | |
593 | } else { | |
594 | /* A command has been specified. Store it into the buffer. */ | |
595 | for (i = 0; i < ac; i++) { | |
596 | if (i) | |
597 | buffer_append(&command, " ", 1); | |
598 | buffer_append(&command, av[i], strlen(av[i])); | |
599 | } | |
600 | } | |
601 | ||
602 | /* Cannot fork to background if no command. */ | |
603 | if (fork_after_authentication_flag && buffer_len(&command) == 0 && | |
604 | !no_shell_flag) | |
605 | fatal("Cannot fork into background without a command " | |
606 | "to execute."); | |
607 | ||
608 | /* Allocate a tty by default if no command specified. */ | |
609 | if (buffer_len(&command) == 0) | |
610 | tty_flag = 1; | |
611 | ||
612 | /* Force no tty */ | |
613 | if (no_tty_flag) | |
614 | tty_flag = 0; | |
615 | /* Do not allocate a tty if stdin is not a tty. */ | |
616 | if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) { | |
617 | if (tty_flag) | |
618 | logit("Pseudo-terminal will not be allocated because " | |
619 | "stdin is not a terminal."); | |
620 | tty_flag = 0; | |
621 | } | |
622 | ||
623 | /* | |
624 | * Initialize "log" output. Since we are the client all output | |
625 | * actually goes to stderr. | |
626 | */ | |
627 | log_init(argv0, | |
628 | options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, | |
629 | SYSLOG_FACILITY_USER, !use_syslog); | |
630 | ||
631 | /* | |
632 | * Read per-user configuration file. Ignore the system wide config | |
633 | * file if the user specifies a config file on the command line. | |
634 | */ | |
635 | if (config != NULL) { | |
636 | if (!read_config_file(config, host, &options, 0)) | |
637 | fatal("Can't open user config file %.100s: " | |
638 | "%.100s", config, strerror(errno)); | |
639 | } else { | |
640 | r = snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, | |
641 | _PATH_SSH_USER_CONFFILE); | |
642 | if (r > 0 && (size_t)r < sizeof(buf)) | |
643 | (void)read_config_file(buf, host, &options, 1); | |
644 | ||
645 | /* Read systemwide configuration file after use config. */ | |
646 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, | |
647 | &options, 0); | |
648 | } | |
649 | ||
650 | /* Fill configuration defaults. */ | |
651 | fill_default_options(&options); | |
652 | ||
653 | channel_set_af(options.address_family); | |
654 | ||
655 | /* reinit */ | |
656 | log_init(argv0, options.log_level, SYSLOG_FACILITY_USER, !use_syslog); | |
657 | ||
658 | seed_rng(); | |
659 | ||
660 | if (options.user == NULL) | |
661 | options.user = xstrdup(pw->pw_name); | |
662 | ||
663 | /* Get default port if port has not been set. */ | |
664 | if (options.port == 0) { | |
665 | sp = getservbyname(SSH_SERVICE_NAME, "tcp"); | |
666 | options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT; | |
667 | } | |
668 | ||
669 | if (options.local_command != NULL) { | |
670 | char thishost[NI_MAXHOST]; | |
671 | ||
672 | if (gethostname(thishost, sizeof(thishost)) == -1) | |
673 | fatal("gethostname: %s", strerror(errno)); | |
674 | snprintf(buf, sizeof(buf), "%d", options.port); | |
675 | debug3("expanding LocalCommand: %s", options.local_command); | |
676 | cp = options.local_command; | |
677 | options.local_command = percent_expand(cp, "d", pw->pw_dir, | |
678 | "h", options.hostname? options.hostname : host, | |
679 | "l", thishost, "n", host, "r", options.user, "p", buf, | |
680 | "u", pw->pw_name, (char *)NULL); | |
681 | debug3("expanded LocalCommand: %s", options.local_command); | |
682 | xfree(cp); | |
683 | } | |
684 | ||
685 | if (options.hostname != NULL) | |
686 | host = options.hostname; | |
687 | ||
688 | /* force lowercase for hostkey matching */ | |
689 | if (options.host_key_alias != NULL) { | |
690 | for (p = options.host_key_alias; *p; p++) | |
691 | if (isupper(*p)) | |
692 | *p = (char)tolower(*p); | |
693 | } | |
694 | ||
695 | if (options.proxy_command != NULL && | |
696 | strcmp(options.proxy_command, "none") == 0) { | |
697 | xfree(options.proxy_command); | |
698 | options.proxy_command = NULL; | |
699 | } | |
700 | if (options.control_path != NULL && | |
701 | strcmp(options.control_path, "none") == 0) { | |
702 | xfree(options.control_path); | |
703 | options.control_path = NULL; | |
704 | } | |
705 | ||
706 | if (options.control_path != NULL) { | |
707 | char thishost[NI_MAXHOST]; | |
708 | ||
709 | if (gethostname(thishost, sizeof(thishost)) == -1) | |
710 | fatal("gethostname: %s", strerror(errno)); | |
711 | snprintf(buf, sizeof(buf), "%d", options.port); | |
712 | cp = tilde_expand_filename(options.control_path, | |
713 | original_real_uid); | |
714 | xfree(options.control_path); | |
715 | options.control_path = percent_expand(cp, "p", buf, "h", host, | |
716 | "r", options.user, "l", thishost, (char *)NULL); | |
717 | xfree(cp); | |
718 | } | |
719 | if (muxclient_command != 0 && options.control_path == NULL) | |
720 | fatal("No ControlPath specified for \"-O\" command"); | |
721 | if (options.control_path != NULL) | |
722 | muxclient(options.control_path); | |
723 | ||
724 | timeout_ms = options.connection_timeout * 1000; | |
725 | ||
726 | /* Open a connection to the remote host. */ | |
727 | if (ssh_connect(host, &hostaddr, options.port, | |
728 | options.address_family, options.connection_attempts, &timeout_ms, | |
729 | options.tcp_keep_alive, | |
730 | #ifdef HAVE_CYGWIN | |
731 | options.use_privileged_port, | |
732 | #else | |
733 | original_effective_uid == 0 && options.use_privileged_port, | |
734 | #endif | |
735 | options.proxy_command) != 0) | |
736 | exit(255); | |
737 | ||
738 | if (timeout_ms > 0) | |
739 | debug3("timeout: %d ms remain after connect", timeout_ms); | |
740 | ||
741 | /* | |
742 | * If we successfully made the connection, load the host private key | |
743 | * in case we will need it later for combined rsa-rhosts | |
744 | * authentication. This must be done before releasing extra | |
745 | * privileges, because the file is only readable by root. | |
746 | * If we cannot access the private keys, load the public keys | |
747 | * instead and try to execute the ssh-keysign helper instead. | |
748 | */ | |
749 | sensitive_data.nkeys = 0; | |
750 | sensitive_data.keys = NULL; | |
751 | sensitive_data.external_keysign = 0; | |
752 | if (options.rhosts_rsa_authentication || | |
753 | options.hostbased_authentication) { | |
754 | sensitive_data.nkeys = 3; | |
755 | sensitive_data.keys = xcalloc(sensitive_data.nkeys, | |
756 | sizeof(Key)); | |
757 | ||
758 | PRIV_START; | |
759 | sensitive_data.keys[0] = key_load_private_type(KEY_RSA1, | |
760 | _PATH_HOST_KEY_FILE, "", NULL, NULL); | |
761 | sensitive_data.keys[1] = key_load_private_type(KEY_DSA, | |
762 | _PATH_HOST_DSA_KEY_FILE, "", NULL, NULL); | |
763 | sensitive_data.keys[2] = key_load_private_type(KEY_RSA, | |
764 | _PATH_HOST_RSA_KEY_FILE, "", NULL, NULL); | |
765 | PRIV_END; | |
766 | ||
767 | if (options.hostbased_authentication == 1 && | |
768 | sensitive_data.keys[0] == NULL && | |
769 | sensitive_data.keys[1] == NULL && | |
770 | sensitive_data.keys[2] == NULL) { | |
771 | sensitive_data.keys[1] = key_load_public( | |
772 | _PATH_HOST_DSA_KEY_FILE, NULL); | |
773 | sensitive_data.keys[2] = key_load_public( | |
774 | _PATH_HOST_RSA_KEY_FILE, NULL); | |
775 | sensitive_data.external_keysign = 1; | |
776 | } | |
777 | } | |
778 | /* | |
779 | * Get rid of any extra privileges that we may have. We will no | |
780 | * longer need them. Also, extra privileges could make it very hard | |
781 | * to read identity files and other non-world-readable files from the | |
782 | * user's home directory if it happens to be on a NFS volume where | |
783 | * root is mapped to nobody. | |
784 | */ | |
785 | if (original_effective_uid == 0) { | |
786 | PRIV_START; | |
787 | permanently_set_uid(pw); | |
788 | } | |
789 | ||
790 | /* | |
791 | * Now that we are back to our own permissions, create ~/.ssh | |
792 | * directory if it doesn't already exist. | |
793 | */ | |
794 | r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir, | |
795 | strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR); | |
796 | if (r > 0 && (size_t)r < sizeof(buf) && stat(buf, &st) < 0) | |
797 | if (mkdir(buf, 0700) < 0) | |
798 | error("Could not create directory '%.200s'.", buf); | |
799 | ||
800 | /* load options.identity_files */ | |
801 | load_public_identity_files(); | |
802 | ||
803 | /* Expand ~ in known host file names. */ | |
804 | /* XXX mem-leaks: */ | |
805 | options.system_hostfile = | |
806 | tilde_expand_filename(options.system_hostfile, original_real_uid); | |
807 | options.user_hostfile = | |
808 | tilde_expand_filename(options.user_hostfile, original_real_uid); | |
809 | options.system_hostfile2 = | |
810 | tilde_expand_filename(options.system_hostfile2, original_real_uid); | |
811 | options.user_hostfile2 = | |
812 | tilde_expand_filename(options.user_hostfile2, original_real_uid); | |
813 | ||
814 | signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */ | |
815 | ||
816 | /* Log into the remote system. Never returns if the login fails. */ | |
817 | ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, | |
818 | pw, timeout_ms); | |
819 | ||
820 | /* We no longer need the private host keys. Clear them now. */ | |
821 | if (sensitive_data.nkeys != 0) { | |
822 | for (i = 0; i < sensitive_data.nkeys; i++) { | |
823 | if (sensitive_data.keys[i] != NULL) { | |
824 | /* Destroys contents safely */ | |
825 | debug3("clear hostkey %d", i); | |
826 | key_free(sensitive_data.keys[i]); | |
827 | sensitive_data.keys[i] = NULL; | |
828 | } | |
829 | } | |
830 | xfree(sensitive_data.keys); | |
831 | } | |
832 | for (i = 0; i < options.num_identity_files; i++) { | |
833 | if (options.identity_files[i]) { | |
834 | xfree(options.identity_files[i]); | |
835 | options.identity_files[i] = NULL; | |
836 | } | |
837 | if (options.identity_keys[i]) { | |
838 | key_free(options.identity_keys[i]); | |
839 | options.identity_keys[i] = NULL; | |
840 | } | |
841 | } | |
842 | ||
843 | exit_status = compat20 ? ssh_session2() : ssh_session(); | |
844 | packet_close(); | |
845 | ||
846 | if (options.control_path != NULL && muxserver_sock != -1) | |
847 | unlink(options.control_path); | |
848 | ||
849 | /* | |
850 | * Send SIGHUP to proxy command if used. We don't wait() in | |
851 | * case it hangs and instead rely on init to reap the child | |
852 | */ | |
853 | if (proxy_command_pid > 1) | |
854 | kill(proxy_command_pid, SIGHUP); | |
855 | ||
856 | return exit_status; | |
857 | } | |
858 | ||
859 | /* Callback for remote forward global requests */ | |
860 | static void | |
861 | ssh_confirm_remote_forward(int type, u_int32_t seq, void *ctxt) | |
862 | { | |
863 | Forward *rfwd = (Forward *)ctxt; | |
864 | ||
865 | /* XXX verbose() on failure? */ | |
866 | debug("remote forward %s for: listen %d, connect %s:%d", | |
867 | type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", | |
868 | rfwd->listen_port, rfwd->connect_host, rfwd->connect_port); | |
869 | if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) { | |
870 | logit("Allocated port %u for remote forward to %s:%d", | |
871 | packet_get_int(), | |
872 | rfwd->connect_host, rfwd->connect_port); | |
873 | } | |
874 | ||
875 | if (type == SSH2_MSG_REQUEST_FAILURE) { | |
876 | if (options.exit_on_forward_failure) | |
877 | fatal("Error: remote port forwarding failed for " | |
878 | "listen port %d", rfwd->listen_port); | |
879 | else | |
880 | logit("Warning: remote port forwarding failed for " | |
881 | "listen port %d", rfwd->listen_port); | |
882 | } | |
883 | if (++remote_forward_confirms_received == options.num_remote_forwards) { | |
884 | debug("All remote forwarding requests processed"); | |
885 | if (fork_after_authentication_flag) { | |
886 | fork_after_authentication_flag = 0; | |
887 | if (daemon(1, 1) < 0) | |
888 | fatal("daemon() failed: %.200s", | |
889 | strerror(errno)); | |
890 | } | |
891 | } | |
892 | } | |
893 | ||
894 | static void | |
895 | client_cleanup_stdio_fwd(int id, void *arg) | |
896 | { | |
897 | debug("stdio forwarding: done"); | |
898 | cleanup_exit(0); | |
899 | } | |
900 | ||
901 | static int | |
902 | client_setup_stdio_fwd(const char *host_to_connect, u_short port_to_connect) | |
903 | { | |
904 | Channel *c; | |
905 | ||
906 | debug3("client_setup_stdio_fwd %s:%d", host_to_connect, | |
907 | port_to_connect); | |
908 | if ((c = channel_connect_stdio_fwd(host_to_connect, port_to_connect)) | |
909 | == NULL) | |
910 | return 0; | |
911 | channel_register_cleanup(c->self, client_cleanup_stdio_fwd, 0); | |
912 | return 1; | |
913 | } | |
914 | ||
915 | static void | |
916 | ssh_init_forwarding(void) | |
917 | { | |
918 | int success = 0; | |
919 | int i; | |
920 | ||
921 | if (stdio_forward_host != NULL) { | |
922 | if (!compat20) { | |
923 | fatal("stdio forwarding require Protocol 2"); | |
924 | } | |
925 | if (!client_setup_stdio_fwd(stdio_forward_host, | |
926 | stdio_forward_port)) | |
927 | fatal("Failed to connect in stdio forward mode."); | |
928 | } | |
929 | ||
930 | /* Initiate local TCP/IP port forwardings. */ | |
931 | for (i = 0; i < options.num_local_forwards; i++) { | |
932 | debug("Local connections to %.200s:%d forwarded to remote " | |
933 | "address %.200s:%d", | |
934 | (options.local_forwards[i].listen_host == NULL) ? | |
935 | (options.gateway_ports ? "*" : "LOCALHOST") : | |
936 | options.local_forwards[i].listen_host, | |
937 | options.local_forwards[i].listen_port, | |
938 | options.local_forwards[i].connect_host, | |
939 | options.local_forwards[i].connect_port); | |
940 | success += channel_setup_local_fwd_listener( | |
941 | options.local_forwards[i].listen_host, | |
942 | options.local_forwards[i].listen_port, | |
943 | options.local_forwards[i].connect_host, | |
944 | options.local_forwards[i].connect_port, | |
945 | options.gateway_ports); | |
946 | } | |
947 | if (i > 0 && success != i && options.exit_on_forward_failure) | |
948 | fatal("Could not request local forwarding."); | |
949 | if (i > 0 && success == 0) | |
950 | error("Could not request local forwarding."); | |
951 | ||
952 | /* Initiate remote TCP/IP port forwardings. */ | |
953 | for (i = 0; i < options.num_remote_forwards; i++) { | |
954 | debug("Remote connections from %.200s:%d forwarded to " | |
955 | "local address %.200s:%d", | |
956 | (options.remote_forwards[i].listen_host == NULL) ? | |
957 | "LOCALHOST" : options.remote_forwards[i].listen_host, | |
958 | options.remote_forwards[i].listen_port, | |
959 | options.remote_forwards[i].connect_host, | |
960 | options.remote_forwards[i].connect_port); | |
961 | if (channel_request_remote_forwarding( | |
962 | options.remote_forwards[i].listen_host, | |
963 | options.remote_forwards[i].listen_port, | |
964 | options.remote_forwards[i].connect_host, | |
965 | options.remote_forwards[i].connect_port) < 0) { | |
966 | if (options.exit_on_forward_failure) | |
967 | fatal("Could not request remote forwarding."); | |
968 | else | |
969 | logit("Warning: Could not request remote " | |
970 | "forwarding."); | |
971 | } | |
972 | client_register_global_confirm(ssh_confirm_remote_forward, | |
973 | &options.remote_forwards[i]); | |
974 | } | |
975 | ||
976 | /* Initiate tunnel forwarding. */ | |
977 | if (options.tun_open != SSH_TUNMODE_NO) { | |
978 | if (client_request_tun_fwd(options.tun_open, | |
979 | options.tun_local, options.tun_remote) == -1) { | |
980 | if (options.exit_on_forward_failure) | |
981 | fatal("Could not request tunnel forwarding."); | |
982 | else | |
983 | error("Could not request tunnel forwarding."); | |
984 | } | |
985 | } | |
986 | } | |
987 | ||
988 | static void | |
989 | check_agent_present(void) | |
990 | { | |
991 | if (options.forward_agent) { | |
992 | /* Clear agent forwarding if we don't have an agent. */ | |
993 | if (!ssh_agent_present()) | |
994 | options.forward_agent = 0; | |
995 | } | |
996 | } | |
997 | ||
998 | static int | |
999 | ssh_session(void) | |
1000 | { | |
1001 | int type; | |
1002 | int interactive = 0; | |
1003 | int have_tty = 0; | |
1004 | struct winsize ws; | |
1005 | char *cp; | |
1006 | const char *display; | |
1007 | ||
1008 | /* Enable compression if requested. */ | |
1009 | if (options.compression) { | |
1010 | debug("Requesting compression at level %d.", | |
1011 | options.compression_level); | |
1012 | ||
1013 | if (options.compression_level < 1 || | |
1014 | options.compression_level > 9) | |
1015 | fatal("Compression level must be from 1 (fast) to " | |
1016 | "9 (slow, best)."); | |
1017 | ||
1018 | /* Send the request. */ | |
1019 | packet_start(SSH_CMSG_REQUEST_COMPRESSION); | |
1020 | packet_put_int(options.compression_level); | |
1021 | packet_send(); | |
1022 | packet_write_wait(); | |
1023 | type = packet_read(); | |
1024 | if (type == SSH_SMSG_SUCCESS) | |
1025 | packet_start_compression(options.compression_level); | |
1026 | else if (type == SSH_SMSG_FAILURE) | |
1027 | logit("Warning: Remote host refused compression."); | |
1028 | else | |
1029 | packet_disconnect("Protocol error waiting for " | |
1030 | "compression response."); | |
1031 | } | |
1032 | /* Allocate a pseudo tty if appropriate. */ | |
1033 | if (tty_flag) { | |
1034 | debug("Requesting pty."); | |
1035 | ||
1036 | /* Start the packet. */ | |
1037 | packet_start(SSH_CMSG_REQUEST_PTY); | |
1038 | ||
1039 | /* Store TERM in the packet. There is no limit on the | |
1040 | length of the string. */ | |
1041 | cp = getenv("TERM"); | |
1042 | if (!cp) | |
1043 | cp = ""; | |
1044 | packet_put_cstring(cp); | |
1045 | ||
1046 | /* Store window size in the packet. */ | |
1047 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) | |
1048 | memset(&ws, 0, sizeof(ws)); | |
1049 | packet_put_int((u_int)ws.ws_row); | |
1050 | packet_put_int((u_int)ws.ws_col); | |
1051 | packet_put_int((u_int)ws.ws_xpixel); | |
1052 | packet_put_int((u_int)ws.ws_ypixel); | |
1053 | ||
1054 | /* Store tty modes in the packet. */ | |
1055 | tty_make_modes(fileno(stdin), NULL); | |
1056 | ||
1057 | /* Send the packet, and wait for it to leave. */ | |
1058 | packet_send(); | |
1059 | packet_write_wait(); | |
1060 | ||
1061 | /* Read response from the server. */ | |
1062 | type = packet_read(); | |
1063 | if (type == SSH_SMSG_SUCCESS) { | |
1064 | interactive = 1; | |
1065 | have_tty = 1; | |
1066 | } else if (type == SSH_SMSG_FAILURE) | |
1067 | logit("Warning: Remote host failed or refused to " | |
1068 | "allocate a pseudo tty."); | |
1069 | else | |
1070 | packet_disconnect("Protocol error waiting for pty " | |
1071 | "request response."); | |
1072 | } | |
1073 | /* Request X11 forwarding if enabled and DISPLAY is set. */ | |
1074 | display = getenv("DISPLAY"); | |
1075 | if (options.forward_x11 && display != NULL) { | |
1076 | char *proto, *data; | |
1077 | /* Get reasonable local authentication information. */ | |
1078 | client_x11_get_proto(display, options.xauth_location, | |
1079 | options.forward_x11_trusted, &proto, &data); | |
1080 | /* Request forwarding with authentication spoofing. */ | |
1081 | debug("Requesting X11 forwarding with authentication " | |
1082 | "spoofing."); | |
1083 | x11_request_forwarding_with_spoofing(0, display, proto, data); | |
1084 | ||
1085 | /* Read response from the server. */ | |
1086 | type = packet_read(); | |
1087 | if (type == SSH_SMSG_SUCCESS) { | |
1088 | interactive = 1; | |
1089 | } else if (type == SSH_SMSG_FAILURE) { | |
1090 | logit("Warning: Remote host denied X11 forwarding."); | |
1091 | } else { | |
1092 | packet_disconnect("Protocol error waiting for X11 " | |
1093 | "forwarding"); | |
1094 | } | |
1095 | } | |
1096 | /* Tell the packet module whether this is an interactive session. */ | |
1097 | packet_set_interactive(interactive); | |
1098 | ||
1099 | /* Request authentication agent forwarding if appropriate. */ | |
1100 | check_agent_present(); | |
1101 | ||
1102 | if (options.forward_agent) { | |
1103 | debug("Requesting authentication agent forwarding."); | |
1104 | auth_request_forwarding(); | |
1105 | ||
1106 | /* Read response from the server. */ | |
1107 | type = packet_read(); | |
1108 | packet_check_eom(); | |
1109 | if (type != SSH_SMSG_SUCCESS) | |
1110 | logit("Warning: Remote host denied authentication agent forwarding."); | |
1111 | } | |
1112 | ||
1113 | /* Initiate port forwardings. */ | |
1114 | ssh_init_forwarding(); | |
1115 | ||
1116 | /* Execute a local command */ | |
1117 | if (options.local_command != NULL && | |
1118 | options.permit_local_command) | |
1119 | ssh_local_cmd(options.local_command); | |
1120 | ||
1121 | /* | |
1122 | * If requested and we are not interested in replies to remote | |
1123 | * forwarding requests, then let ssh continue in the background. | |
1124 | */ | |
1125 | if (fork_after_authentication_flag && | |
1126 | (!options.exit_on_forward_failure || | |
1127 | options.num_remote_forwards == 0)) { | |
1128 | fork_after_authentication_flag = 0; | |
1129 | if (daemon(1, 1) < 0) | |
1130 | fatal("daemon() failed: %.200s", strerror(errno)); | |
1131 | } | |
1132 | ||
1133 | /* | |
1134 | * If a command was specified on the command line, execute the | |
1135 | * command now. Otherwise request the server to start a shell. | |
1136 | */ | |
1137 | if (buffer_len(&command) > 0) { | |
1138 | int len = buffer_len(&command); | |
1139 | if (len > 900) | |
1140 | len = 900; | |
1141 | debug("Sending command: %.*s", len, | |
1142 | (u_char *)buffer_ptr(&command)); | |
1143 | packet_start(SSH_CMSG_EXEC_CMD); | |
1144 | packet_put_string(buffer_ptr(&command), buffer_len(&command)); | |
1145 | packet_send(); | |
1146 | packet_write_wait(); | |
1147 | } else { | |
1148 | debug("Requesting shell."); | |
1149 | packet_start(SSH_CMSG_EXEC_SHELL); | |
1150 | packet_send(); | |
1151 | packet_write_wait(); | |
1152 | } | |
1153 | ||
1154 | /* Enter the interactive session. */ | |
1155 | return client_loop(have_tty, tty_flag ? | |
1156 | options.escape_char : SSH_ESCAPECHAR_NONE, 0); | |
1157 | } | |
1158 | ||
1159 | /* request pty/x11/agent/tcpfwd/shell for channel */ | |
1160 | static void | |
1161 | ssh_session2_setup(int id, void *arg) | |
1162 | { | |
1163 | extern char **environ; | |
1164 | const char *display; | |
1165 | int interactive = tty_flag; | |
1166 | ||
1167 | display = getenv("DISPLAY"); | |
1168 | if (options.forward_x11 && display != NULL) { | |
1169 | char *proto, *data; | |
1170 | /* Get reasonable local authentication information. */ | |
1171 | client_x11_get_proto(display, options.xauth_location, | |
1172 | options.forward_x11_trusted, &proto, &data); | |
1173 | /* Request forwarding with authentication spoofing. */ | |
1174 | debug("Requesting X11 forwarding with authentication " | |
1175 | "spoofing."); | |
1176 | x11_request_forwarding_with_spoofing(id, display, proto, data); | |
1177 | interactive = 1; | |
1178 | /* XXX wait for reply */ | |
1179 | } | |
1180 | ||
1181 | check_agent_present(); | |
1182 | if (options.forward_agent) { | |
1183 | debug("Requesting authentication agent forwarding."); | |
1184 | channel_request_start(id, "auth-agent-req@openssh.com", 0); | |
1185 | packet_send(); | |
1186 | } | |
1187 | ||
1188 | client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"), | |
1189 | NULL, fileno(stdin), &command, environ); | |
1190 | ||
1191 | packet_set_interactive(interactive); | |
1192 | } | |
1193 | ||
1194 | /* open new channel for a session */ | |
1195 | static int | |
1196 | ssh_session2_open(void) | |
1197 | { | |
1198 | Channel *c; | |
1199 | int window, packetmax, in, out, err; | |
1200 | ||
1201 | if (stdin_null_flag) { | |
1202 | in = open(_PATH_DEVNULL, O_RDONLY); | |
1203 | } else { | |
1204 | in = dup(STDIN_FILENO); | |
1205 | } | |
1206 | out = dup(STDOUT_FILENO); | |
1207 | err = dup(STDERR_FILENO); | |
1208 | ||
1209 | if (in < 0 || out < 0 || err < 0) | |
1210 | fatal("dup() in/out/err failed"); | |
1211 | ||
1212 | /* enable nonblocking unless tty */ | |
1213 | if (!isatty(in)) | |
1214 | set_nonblock(in); | |
1215 | if (!isatty(out)) | |
1216 | set_nonblock(out); | |
1217 | if (!isatty(err)) | |
1218 | set_nonblock(err); | |
1219 | ||
1220 | window = CHAN_SES_WINDOW_DEFAULT; | |
1221 | packetmax = CHAN_SES_PACKET_DEFAULT; | |
1222 | if (tty_flag) { | |
1223 | window >>= 1; | |
1224 | packetmax >>= 1; | |
1225 | } | |
1226 | c = channel_new( | |
1227 | "session", SSH_CHANNEL_OPENING, in, out, err, | |
1228 | window, packetmax, CHAN_EXTENDED_WRITE, | |
1229 | "client-session", /*nonblock*/0); | |
1230 | ||
1231 | debug3("ssh_session2_open: channel_new: %d", c->self); | |
1232 | ||
1233 | channel_send_open(c->self); | |
1234 | if (!no_shell_flag) | |
1235 | channel_register_open_confirm(c->self, | |
1236 | ssh_session2_setup, NULL); | |
1237 | ||
1238 | return c->self; | |
1239 | } | |
1240 | ||
1241 | static int | |
1242 | ssh_session2(void) | |
1243 | { | |
1244 | int id = -1; | |
1245 | ||
1246 | /* XXX should be pre-session */ | |
1247 | ssh_init_forwarding(); | |
1248 | ||
1249 | if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN)) | |
1250 | id = ssh_session2_open(); | |
1251 | ||
1252 | /* If we don't expect to open a new session, then disallow it */ | |
1253 | if (options.control_master == SSHCTL_MASTER_NO && | |
1254 | (datafellows & SSH_NEW_OPENSSH)) { | |
1255 | debug("Requesting no-more-sessions@openssh.com"); | |
1256 | packet_start(SSH2_MSG_GLOBAL_REQUEST); | |
1257 | packet_put_cstring("no-more-sessions@openssh.com"); | |
1258 | packet_put_char(0); | |
1259 | packet_send(); | |
1260 | } | |
1261 | ||
1262 | /* Execute a local command */ | |
1263 | if (options.local_command != NULL && | |
1264 | options.permit_local_command) | |
1265 | ssh_local_cmd(options.local_command); | |
1266 | ||
1267 | /* Start listening for multiplex clients */ | |
1268 | muxserver_listen(); | |
1269 | ||
1270 | /* If requested, let ssh continue in the background. */ | |
1271 | if (fork_after_authentication_flag) { | |
1272 | fork_after_authentication_flag = 0; | |
1273 | if (daemon(1, 1) < 0) | |
1274 | fatal("daemon() failed: %.200s", strerror(errno)); | |
1275 | } | |
1276 | ||
1277 | if (options.use_roaming) | |
1278 | request_roaming(); | |
1279 | ||
1280 | return client_loop(tty_flag, tty_flag ? | |
1281 | options.escape_char : SSH_ESCAPECHAR_NONE, id); | |
1282 | } | |
1283 | ||
1284 | static void | |
1285 | load_public_identity_files(void) | |
1286 | { | |
1287 | char *filename, *cp, thishost[NI_MAXHOST]; | |
1288 | char *pwdir = NULL, *pwname = NULL; | |
1289 | int i = 0; | |
1290 | Key *public; | |
1291 | struct passwd *pw; | |
1292 | #ifdef SMARTCARD | |
1293 | Key **keys; | |
1294 | ||
1295 | if (options.smartcard_device != NULL && | |
1296 | options.num_identity_files < SSH_MAX_IDENTITY_FILES && | |
1297 | (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) { | |
1298 | int count = 0; | |
1299 | for (i = 0; keys[i] != NULL; i++) { | |
1300 | count++; | |
1301 | memmove(&options.identity_files[1], | |
1302 | &options.identity_files[0], | |
1303 | sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1)); | |
1304 | memmove(&options.identity_keys[1], | |
1305 | &options.identity_keys[0], | |
1306 | sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1)); | |
1307 | options.num_identity_files++; | |
1308 | options.identity_keys[0] = keys[i]; | |
1309 | options.identity_files[0] = sc_get_key_label(keys[i]); | |
1310 | } | |
1311 | if (options.num_identity_files > SSH_MAX_IDENTITY_FILES) | |
1312 | options.num_identity_files = SSH_MAX_IDENTITY_FILES; | |
1313 | i = count; | |
1314 | xfree(keys); | |
1315 | } | |
1316 | #endif /* SMARTCARD */ | |
1317 | if ((pw = getpwuid(original_real_uid)) == NULL) | |
1318 | fatal("load_public_identity_files: getpwuid failed"); | |
1319 | pwname = xstrdup(pw->pw_name); | |
1320 | pwdir = xstrdup(pw->pw_dir); | |
1321 | if (gethostname(thishost, sizeof(thishost)) == -1) | |
1322 | fatal("load_public_identity_files: gethostname: %s", | |
1323 | strerror(errno)); | |
1324 | for (; i < options.num_identity_files; i++) { | |
1325 | cp = tilde_expand_filename(options.identity_files[i], | |
1326 | original_real_uid); | |
1327 | filename = percent_expand(cp, "d", pwdir, | |
1328 | "u", pwname, "l", thishost, "h", host, | |
1329 | "r", options.user, (char *)NULL); | |
1330 | xfree(cp); | |
1331 | public = key_load_public(filename, NULL); | |
1332 | debug("identity file %s type %d", filename, | |
1333 | public ? public->type : -1); | |
1334 | xfree(options.identity_files[i]); | |
1335 | options.identity_files[i] = filename; | |
1336 | options.identity_keys[i] = public; | |
1337 | } | |
1338 | bzero(pwname, strlen(pwname)); | |
1339 | xfree(pwname); | |
1340 | bzero(pwdir, strlen(pwdir)); | |
1341 | xfree(pwdir); | |
1342 | } |