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