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