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