Annotation of src/usr.bin/ssh/ssh.c, Revision 1.69.2.6
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.69.2.6! miod 42: RCSID("$OpenBSD: ssh.c,v 1.69.2.5 2001/09/27 00:15:42 miod Exp $");
1.49 markus 43:
44: #include <openssl/evp.h>
1.69.2.1 jason 45: #include <openssl/err.h>
1.1 deraadt 46:
47: #include "ssh.h"
1.69.2.1 jason 48: #include "ssh1.h"
49: #include "ssh2.h"
50: #include "compat.h"
51: #include "cipher.h"
52: #include "xmalloc.h"
1.1 deraadt 53: #include "packet.h"
54: #include "buffer.h"
55: #include "uidswap.h"
1.44 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.69.2.1 jason 60: #include "pathnames.h"
61: #include "clientloop.h"
62: #include "log.h"
63: #include "readconf.h"
64: #include "sshconnect.h"
65: #include "tildexpand.h"
66: #include "dispatch.h"
67: #include "misc.h"
68: #include "kex.h"
69: #include "mac.h"
1.69.2.4 jason 70: #include "sshtty.h"
1.49 markus 71:
1.69.2.5 miod 72: #ifdef SMARTCARD
73: #include <openssl/engine.h>
74: #include "scard.h"
75: #endif
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.69.2.1 jason 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.69.2.5 miod 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.69.2.4 jason 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.69.2.1 jason 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.69.2.5 miod 151: static void
1.69.2.1 jason 152: usage(void)
1.1 deraadt 153: {
1.69.2.1 jason 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.69.2.1 jason 157: fprintf(stderr, " -n Redirect input from " _PATH_DEVNULL ".\n");
1.69.2.5 miod 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.69.2.5 miod 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.69.2.1 jason 165: fprintf(stderr, " -X Enable X11 connection forwarding.\n");
1.69.2.5 miod 166: fprintf(stderr, " -x Disable X11 connection forwarding (default).\n");
1.69.2.3 jason 167: fprintf(stderr, " -i file Identity for public key authentication "
168: "(default: ~/.ssh/identity)\n");
1.69.2.5 miod 169: #ifdef SMARTCARD
170: fprintf(stderr, " -I reader Set smartcard reader.\n");
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.69.2.5 miod 182: fprintf(stderr, " -c cipher Select encryption algorithm\n");
1.69.2.3 jason 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.69.2.1 jason 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.69.2.5 miod 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.69.2.1 jason 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.69.2.1 jason 198: fprintf(stderr, " -s Invoke command (mandatory) as SSH2 subsystem.\n");
1.69.2.5 miod 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.69.2.5 miod 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.69.2.5 miod 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.69.2.5 miod 251: int i, opt, exit_status, cerr;
1.35 markus 252: u_short fwd_port, fwd_host_port;
1.69.2.5 miod 253: char sfwd_port[6], sfwd_host_port[6];
254: char *p, *cp, buf[256];
1.31 markus 255: struct stat st;
1.69.2.3 jason 256: struct passwd *pw;
1.45 markus 257: int dummy;
1.31 markus 258: uid_t original_effective_uid;
1.69.2.5 miod 259: extern int optind, optreset;
260: extern char *optarg;
1.31 markus 261:
1.33 markus 262: /*
263: * Save the original real uid. It will be needed later (uid-swapping
264: * may clobber the real uid).
265: */
1.31 markus 266: original_real_uid = getuid();
267: original_effective_uid = geteuid();
268:
269: /* If we are installed setuid root be careful to not drop core. */
270: if (original_real_uid != original_effective_uid) {
271: struct rlimit rlim;
272: rlim.rlim_cur = rlim.rlim_max = 0;
273: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
274: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 275: }
1.69.2.4 jason 276: /* Get user data. */
277: pw = getpwuid(original_real_uid);
278: if (!pw) {
279: log("You don't exist, go away!");
280: exit(1);
281: }
282: /* Take a copy of the returned structure. */
283: pw = pwcopy(pw);
284:
1.33 markus 285: /*
286: * Use uid-swapping to give up root privileges for the duration of
287: * option processing. We will re-instantiate the rights when we are
288: * ready to create the privileged port, and will permanently drop
289: * them when the port has been created (actually, when the connection
290: * has been made, as we may need to create the port several times).
291: */
1.69.2.4 jason 292: temporarily_use_uid(pw);
1.31 markus 293:
1.33 markus 294: /*
295: * Set our umask to something reasonable, as some files are created
296: * with the default umask. This will make them world-readable but
297: * writable only by the owner, which is ok for all files for which we
298: * don't set the modes explicitly.
299: */
1.31 markus 300: umask(022);
301:
302: /* Initialize option structure to indicate that no values have been set. */
303: initialize_options(&options);
304:
305: /* Parse command-line arguments. */
306: host = NULL;
307:
1.69.2.5 miod 308: again:
309: while ((opt = getopt(ac, av,
310: "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVX")) != -1) {
1.31 markus 311: switch (opt) {
1.69.2.1 jason 312: case '1':
313: options.protocol = SSH_PROTO_1;
314: break;
1.47 markus 315: case '2':
316: options.protocol = SSH_PROTO_2;
317: break;
1.37 markus 318: case '4':
319: IPv4or6 = AF_INET;
320: break;
321: case '6':
322: IPv4or6 = AF_INET6;
323: break;
1.31 markus 324: case 'n':
325: stdin_null_flag = 1;
326: break;
327: case 'f':
328: fork_after_authentication_flag = 1;
329: stdin_null_flag = 1;
330: break;
331: case 'x':
332: options.forward_x11 = 0;
333: break;
334: case 'X':
335: options.forward_x11 = 1;
336: break;
337: case 'g':
338: options.gateway_ports = 1;
339: break;
340: case 'P':
341: options.use_privileged_port = 0;
342: break;
343: case 'a':
344: options.forward_agent = 0;
1.53 markus 345: break;
346: case 'A':
347: options.forward_agent = 1;
1.31 markus 348: break;
1.9 dugsong 349: #ifdef AFS
1.31 markus 350: case 'k':
351: options.kerberos_tgt_passing = 0;
352: options.afs_token_passing = 0;
353: break;
1.1 deraadt 354: #endif
1.31 markus 355: case 'i':
356: if (stat(optarg, &st) < 0) {
1.69.2.5 miod 357: fprintf(stderr, "Warning: Identity file %s "
358: "does not exist.\n", optarg);
1.31 markus 359: break;
360: }
1.69.2.5 miod 361: if (options.num_identity_files >=
362: SSH_MAX_IDENTITY_FILES)
363: fatal("Too many identity files specified "
364: "(max %d)", SSH_MAX_IDENTITY_FILES);
365: options.identity_files[options.num_identity_files++] =
366: xstrdup(optarg);
367: break;
368: case 'I':
369: #ifdef SMARTCARD
370: options.smartcard_device = xstrdup(optarg);
371: #else
372: fprintf(stderr, "no support for smartcards.\n");
373: #endif
1.31 markus 374: break;
375: case 't':
1.69.2.1 jason 376: if (tty_flag)
377: force_tty_flag = 1;
1.31 markus 378: tty_flag = 1;
379: break;
380: case 'v':
1.66 markus 381: if (0 == debug_flag) {
382: debug_flag = 1;
383: options.log_level = SYSLOG_LEVEL_DEBUG1;
384: } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
385: options.log_level++;
386: break;
1.69.2.5 miod 387: } else
1.69.2.3 jason 388: fatal("Too high debugging level.");
1.66 markus 389: /* fallthrough */
1.31 markus 390: case 'V':
1.69.2.2 jason 391: fprintf(stderr,
392: "%s, SSH protocols %d.%d/%d.%d, OpenSSL 0x%8.8lx\n",
1.46 markus 393: SSH_VERSION,
394: PROTOCOL_MAJOR_1, PROTOCOL_MINOR_1,
1.69.2.2 jason 395: PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2,
396: SSLeay());
1.31 markus 397: if (opt == 'V')
398: exit(0);
399: break;
400: case 'q':
401: options.log_level = SYSLOG_LEVEL_QUIET;
402: break;
403: case 'e':
404: if (optarg[0] == '^' && optarg[2] == 0 &&
1.69.2.5 miod 405: (u_char) optarg[1] >= 64 &&
406: (u_char) optarg[1] < 128)
1.69.2.1 jason 407: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 408: else if (strlen(optarg) == 1)
1.69.2.1 jason 409: options.escape_char = (u_char) optarg[0];
1.31 markus 410: else if (strcmp(optarg, "none") == 0)
1.69.2.5 miod 411: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 412: else {
1.69.2.5 miod 413: fprintf(stderr, "Bad escape character '%s'.\n",
414: optarg);
1.31 markus 415: exit(1);
416: }
417: break;
418: case 'c':
1.49 markus 419: if (ciphers_valid(optarg)) {
420: /* SSH2 only */
421: options.ciphers = xstrdup(optarg);
1.51 markus 422: options.cipher = SSH_CIPHER_ILLEGAL;
1.49 markus 423: } else {
424: /* SSH1 only */
1.69.2.1 jason 425: options.cipher = cipher_number(optarg);
426: if (options.cipher == -1) {
1.69.2.5 miod 427: fprintf(stderr,
428: "Unknown cipher type '%s'\n",
429: optarg);
1.49 markus 430: exit(1);
431: }
1.69.2.5 miod 432: if (options.cipher == SSH_CIPHER_3DES)
1.69.2.1 jason 433: options.ciphers = "3des-cbc";
1.69.2.5 miod 434: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.69.2.1 jason 435: options.ciphers = "blowfish-cbc";
1.69.2.5 miod 436: else
1.69.2.1 jason 437: options.ciphers = (char *)-1;
438: }
439: break;
440: case 'm':
441: if (mac_valid(optarg))
442: options.macs = xstrdup(optarg);
443: else {
1.69.2.5 miod 444: fprintf(stderr, "Unknown mac type '%s'\n",
445: optarg);
1.69.2.1 jason 446: exit(1);
1.31 markus 447: }
448: break;
449: case 'p':
1.69.2.4 jason 450: options.port = a2port(optarg);
451: if (options.port == 0) {
452: fprintf(stderr, "Bad port '%s'\n", optarg);
453: exit(1);
454: }
1.31 markus 455: break;
456: case 'l':
457: options.user = optarg;
458: break;
1.69.2.5 miod 459:
460: case 'L':
1.31 markus 461: case 'R':
1.69.2.5 miod 462: if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]",
463: sfwd_port, buf, sfwd_host_port) != 3 &&
464: sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]",
465: sfwd_port, buf, sfwd_host_port) != 3) {
466: fprintf(stderr,
467: "Bad forwarding specification '%s'\n",
468: optarg);
1.31 markus 469: usage();
470: /* NOTREACHED */
471: }
1.69.2.5 miod 472: if ((fwd_port = a2port(sfwd_port)) == 0 ||
473: (fwd_host_port = a2port(sfwd_host_port)) == 0) {
474: fprintf(stderr,
475: "Bad forwarding port(s) '%s'\n", optarg);
476: exit(1);
1.31 markus 477: }
1.69.2.5 miod 478: if (opt == 'L')
479: add_local_forward(&options, fwd_port, buf,
480: fwd_host_port);
481: else if (opt == 'R')
482: add_remote_forward(&options, fwd_port, buf,
483: fwd_host_port);
1.31 markus 484: break;
1.69.2.4 jason 485:
486: case 'D':
487: fwd_port = a2port(optarg);
488: if (fwd_port == 0) {
1.69.2.5 miod 489: fprintf(stderr, "Bad dynamic port '%s'\n",
490: optarg);
1.69.2.4 jason 491: exit(1);
492: }
493: add_local_forward(&options, fwd_port, "socks4", 0);
494: break;
495:
1.31 markus 496: case 'C':
497: options.compression = 1;
498: break;
1.45 markus 499: case 'N':
500: no_shell_flag = 1;
501: no_tty_flag = 1;
502: break;
503: case 'T':
504: no_tty_flag = 1;
505: break;
1.31 markus 506: case 'o':
507: dummy = 1;
1.69.2.5 miod 508: if (process_config_line(&options, host ? host : "",
509: optarg, "command-line", 0, &dummy) != 0)
1.31 markus 510: exit(1);
511: break;
1.69.2.1 jason 512: case 's':
513: subsystem_flag = 1;
514: break;
1.69.2.5 miod 515: case 'b':
516: options.bind_address = optarg;
517: break;
518: case 'F':
519: config = optarg;
520: break;
1.31 markus 521: default:
522: usage();
1.1 deraadt 523: }
1.31 markus 524: }
525:
1.69.2.5 miod 526: ac -= optind;
527: av += optind;
528:
529: if (ac > 0 && !host && **av != '-') {
530: if (strchr(*av, '@')) {
531: p = xstrdup(*av);
532: cp = strchr(p, '@');
533: if (cp == NULL || cp == p)
534: usage();
535: options.user = p;
536: *cp = '\0';
537: host = ++cp;
538: } else
539: host = *av;
540: ac--, av++;
541: if (ac > 0) {
542: optind = 0;
543: optreset = 1;
544: goto again;
545: }
546: }
547:
1.31 markus 548: /* Check that we got a host name. */
549: if (!host)
550: usage();
551:
1.54 markus 552: SSLeay_add_all_algorithms();
1.69.2.1 jason 553: ERR_load_crypto_strings();
1.69.2.6! miod 554: channel_set_af(IPv4or6);
1.31 markus 555:
556: /* Initialize the command to execute on remote host. */
557: buffer_init(&command);
1.1 deraadt 558:
1.33 markus 559: /*
560: * Save the command to execute on the remote host in a buffer. There
561: * is no limit on the length of the command, except by the maximum
562: * packet size. Also sets the tty flag if there is no command.
563: */
1.69.2.5 miod 564: if (!ac) {
1.31 markus 565: /* No command specified - execute shell on a tty. */
566: tty_flag = 1;
1.69.2.1 jason 567: if (subsystem_flag) {
1.69.2.5 miod 568: fprintf(stderr,
569: "You must specify a subsystem to invoke.\n");
1.69.2.1 jason 570: usage();
571: }
1.31 markus 572: } else {
1.69.2.5 miod 573: /* A command has been specified. Store it into the buffer. */
574: for (i = 0; i < ac; i++) {
575: if (i)
1.31 markus 576: buffer_append(&command, " ", 1);
577: buffer_append(&command, av[i], strlen(av[i]));
578: }
579: }
580:
581: /* Cannot fork to background if no command. */
1.63 markus 582: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 583: fatal("Cannot fork into background without a command to execute.");
584:
585: /* Allocate a tty by default if no command specified. */
586: if (buffer_len(&command) == 0)
587: tty_flag = 1;
588:
1.69.2.1 jason 589: /* Force no tty*/
590: if (no_tty_flag)
591: tty_flag = 0;
1.31 markus 592: /* Do not allocate a tty if stdin is not a tty. */
1.69.2.1 jason 593: if (!isatty(fileno(stdin)) && !force_tty_flag) {
1.31 markus 594: if (tty_flag)
1.69.2.3 jason 595: log("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 596: tty_flag = 0;
597: }
1.45 markus 598:
1.69.2.3 jason 599: /*
600: * Initialize "log" output. Since we are the client all output
601: * actually goes to stderr.
602: */
1.69.2.4 jason 603: log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
604: SYSLOG_FACILITY_USER, 1);
1.31 markus 605:
1.69.2.5 miod 606: /*
607: * Read per-user configuration file. Ignore the system wide config
608: * file if the user specifies a config file on the command line.
609: */
610: if (config != NULL) {
611: if (!read_config_file(config, host, &options))
612: fatal("Can't open user config file %.100s: "
613: "%.100s", config, strerror(errno));
614: } else {
615: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
616: _PATH_SSH_USER_CONFFILE);
1.69.2.6! miod 617: (void)read_config_file(buf, host, &options);
1.69.2.5 miod 618:
1.69.2.6! miod 619: /* Read systemwide configuration file after use config. */
1.69.2.5 miod 620: (void)read_config_file(_PATH_HOST_CONFIG_FILE, host, &options);
621: }
1.31 markus 622:
623: /* Fill configuration defaults. */
624: fill_default_options(&options);
625:
626: /* reinit */
1.69.2.3 jason 627: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 628:
629: if (options.user == NULL)
630: options.user = xstrdup(pw->pw_name);
631:
632: if (options.hostname != NULL)
633: host = options.hostname;
634:
635: /* Disable rhosts authentication if not running as root. */
636: if (original_effective_uid != 0 || !options.use_privileged_port) {
1.69.2.1 jason 637: debug("Rhosts Authentication disabled, "
638: "originating port will not be trusted.");
1.31 markus 639: options.rhosts_authentication = 0;
640: }
1.33 markus 641: /*
642: * If using rsh has been selected, exec it now (without trying
643: * anything else). Note that we must release privileges first.
644: */
1.31 markus 645: if (options.use_rsh) {
1.33 markus 646: /*
647: * Restore our superuser privileges. This must be done
648: * before permanently setting the uid.
649: */
1.31 markus 650: restore_uid();
651:
652: /* Switch to the original uid permanently. */
1.69.2.4 jason 653: permanently_set_uid(pw);
1.31 markus 654:
655: /* Execute rsh. */
656: rsh_connect(host, options.user, &command);
657: fatal("rsh_connect returned");
658: }
659: /* Restore our superuser privileges. */
660: restore_uid();
661:
1.69.2.1 jason 662: /* Open a connection to the remote host. */
1.31 markus 663:
1.69.2.6! miod 664: cerr = ssh_connect(host, &hostaddr, options.port, IPv4or6,
1.69.2.1 jason 665: options.connection_attempts,
666: original_effective_uid != 0 || !options.use_privileged_port,
1.69.2.4 jason 667: pw, options.proxy_command);
1.31 markus 668:
1.33 markus 669: /*
670: * If we successfully made the connection, load the host private key
671: * in case we will need it later for combined rsa-rhosts
672: * authentication. This must be done before releasing extra
673: * privileges, because the file is only readable by root.
674: */
1.69.2.4 jason 675: sensitive_data.nkeys = 0;
676: sensitive_data.keys = NULL;
1.69.2.5 miod 677: if (!cerr && (options.rhosts_rsa_authentication ||
1.69.2.4 jason 678: options.hostbased_authentication)) {
679: sensitive_data.nkeys = 3;
680: sensitive_data.keys = xmalloc(sensitive_data.nkeys*sizeof(Key));
681: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
682: _PATH_HOST_KEY_FILE, "", NULL);
683: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
684: _PATH_HOST_DSA_KEY_FILE, "", NULL);
685: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
686: _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.31 markus 687: }
1.33 markus 688: /*
689: * Get rid of any extra privileges that we may have. We will no
690: * longer need them. Also, extra privileges could make it very hard
691: * to read identity files and other non-world-readable files from the
692: * user's home directory if it happens to be on a NFS volume where
693: * root is mapped to nobody.
694: */
695:
696: /*
697: * Note that some legacy systems need to postpone the following call
698: * to permanently_set_uid() until the private hostkey is destroyed
699: * with RSA_free(). Otherwise the calling user could ptrace() the
700: * process, read the private hostkey and impersonate the host.
701: * OpenBSD does not allow ptracing of setuid processes.
702: */
1.69.2.4 jason 703: permanently_set_uid(pw);
1.31 markus 704:
1.33 markus 705: /*
706: * Now that we are back to our own permissions, create ~/.ssh
707: * directory if it doesn\'t already exist.
708: */
1.69.2.5 miod 709: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 710: if (stat(buf, &st) < 0)
1.57 djm 711: if (mkdir(buf, 0700) < 0)
1.31 markus 712: error("Could not create directory '%.200s'.", buf);
713:
714: /* Check if the connection failed, and try "rsh" if appropriate. */
1.69.2.5 miod 715: if (cerr) {
716: if (!options.fallback_to_rsh)
717: exit(1);
1.31 markus 718: if (options.port != 0)
1.69.2.5 miod 719: log("Secure connection to %.100s on port %hu refused; "
720: "reverting to insecure method",
721: host, options.port);
1.31 markus 722: else
1.69.2.5 miod 723: log("Secure connection to %.100s refused; "
724: "reverting to insecure method.", host);
1.31 markus 725:
1.69.2.5 miod 726: rsh_connect(host, options.user, &command);
727: fatal("rsh_connect returned");
1.31 markus 728: }
1.69.2.3 jason 729: /* load options.identity_files */
730: load_public_identity_files();
731:
732: /* Expand ~ in known host file names. */
733: /* XXX mem-leaks: */
1.69.2.1 jason 734: options.system_hostfile =
735: tilde_expand_filename(options.system_hostfile, original_real_uid);
736: options.user_hostfile =
737: tilde_expand_filename(options.user_hostfile, original_real_uid);
738: options.system_hostfile2 =
739: tilde_expand_filename(options.system_hostfile2, original_real_uid);
740: options.user_hostfile2 =
741: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.31 markus 742:
743: /* Log into the remote system. This never returns if the login fails. */
1.69.2.4 jason 744: ssh_login(sensitive_data.keys, sensitive_data.nkeys,
745: host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 746:
1.69.2.4 jason 747: /* We no longer need the private host keys. Clear them now. */
748: if (sensitive_data.nkeys != 0) {
749: for (i = 0; i < sensitive_data.nkeys; i++) {
750: if (sensitive_data.keys[i] != NULL) {
751: /* Destroys contents safely */
752: debug3("clear hostkey %d", i);
753: key_free(sensitive_data.keys[i]);
754: sensitive_data.keys[i] = NULL;
755: }
756: }
757: xfree(sensitive_data.keys);
758: }
1.69.2.5 miod 759: for (i = 0; i < options.num_identity_files; i++) {
760: if (options.identity_files[i]) {
761: xfree(options.identity_files[i]);
762: options.identity_files[i] = NULL;
763: }
764: if (options.identity_keys[i]) {
765: key_free(options.identity_keys[i]);
766: options.identity_keys[i] = NULL;
767: }
768: }
1.31 markus 769:
1.45 markus 770: exit_status = compat20 ? ssh_session2() : ssh_session();
771: packet_close();
772: return exit_status;
773: }
774:
1.69.2.5 miod 775: static void
1.50 markus 776: x11_get_proto(char *proto, int proto_len, char *data, int data_len)
777: {
778: char line[512];
779: FILE *f;
780: int got_data = 0, i;
781:
1.55 markus 782: if (options.xauth_location) {
783: /* Try to get Xauthority information for the display. */
1.69.2.1 jason 784: snprintf(line, sizeof line, "%.100s list %.200s 2>" _PATH_DEVNULL,
1.55 markus 785: options.xauth_location, getenv("DISPLAY"));
786: f = popen(line, "r");
787: if (f && fgets(line, sizeof(line), f) &&
788: sscanf(line, "%*s %s %s", proto, data) == 2)
789: got_data = 1;
790: if (f)
791: pclose(f);
792: }
1.50 markus 793: /*
794: * If we didn't get authentication data, just make up some
795: * data. The forwarding code will check the validity of the
796: * response anyway, and substitute this data. The X11
797: * server, however, will ignore this fake data and use
798: * whatever authentication mechanisms it was using otherwise
799: * for the local connection.
800: */
801: if (!got_data) {
802: u_int32_t rand = 0;
803:
804: strlcpy(proto, "MIT-MAGIC-COOKIE-1", proto_len);
805: for (i = 0; i < 16; i++) {
806: if (i % 4 == 0)
807: rand = arc4random();
808: snprintf(data + 2 * i, data_len - 2 * i, "%02x", rand & 0xff);
809: rand >>= 8;
810: }
811: }
812: }
813:
1.69.2.5 miod 814: static void
1.69.2.1 jason 815: ssh_init_forwarding(void)
816: {
817: int success = 0;
818: int i;
819:
820: /* Initiate local TCP/IP port forwardings. */
821: for (i = 0; i < options.num_local_forwards; i++) {
822: debug("Connections to local port %d forwarded to remote address %.200s:%d",
823: options.local_forwards[i].port,
824: options.local_forwards[i].host,
825: options.local_forwards[i].host_port);
826: success += channel_request_local_forwarding(
827: options.local_forwards[i].port,
828: options.local_forwards[i].host,
829: options.local_forwards[i].host_port,
830: options.gateway_ports);
831: }
832: if (i > 0 && success == 0)
833: error("Could not request local forwarding.");
834:
835: /* Initiate remote TCP/IP port forwardings. */
836: for (i = 0; i < options.num_remote_forwards; i++) {
837: debug("Connections to remote port %d forwarded to local address %.200s:%d",
838: options.remote_forwards[i].port,
839: options.remote_forwards[i].host,
840: options.remote_forwards[i].host_port);
841: channel_request_remote_forwarding(
842: options.remote_forwards[i].port,
843: options.remote_forwards[i].host,
844: options.remote_forwards[i].host_port);
845: }
846: }
847:
1.69.2.5 miod 848: static void
1.69.2.1 jason 849: check_agent_present(void)
850: {
851: if (options.forward_agent) {
852: /* Clear agent forwarding if we don\'t have an agent. */
853: int authfd = ssh_get_authentication_socket();
854: if (authfd < 0)
855: options.forward_agent = 0;
856: else
857: ssh_close_authentication_socket(authfd);
858: }
859: }
860:
1.69.2.5 miod 861: static int
1.45 markus 862: ssh_session(void)
863: {
864: int type;
865: int plen;
866: int interactive = 0;
867: int have_tty = 0;
868: struct winsize ws;
869: char *cp;
870:
1.31 markus 871: /* Enable compression if requested. */
872: if (options.compression) {
873: debug("Requesting compression at level %d.", options.compression_level);
874:
875: if (options.compression_level < 1 || options.compression_level > 9)
876: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
877:
878: /* Send the request. */
879: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
880: packet_put_int(options.compression_level);
881: packet_send();
882: packet_write_wait();
883: type = packet_read(&plen);
884: if (type == SSH_SMSG_SUCCESS)
885: packet_start_compression(options.compression_level);
886: else if (type == SSH_SMSG_FAILURE)
887: log("Warning: Remote host refused compression.");
888: else
889: packet_disconnect("Protocol error waiting for compression response.");
890: }
891: /* Allocate a pseudo tty if appropriate. */
892: if (tty_flag) {
893: debug("Requesting pty.");
894:
895: /* Start the packet. */
896: packet_start(SSH_CMSG_REQUEST_PTY);
897:
898: /* Store TERM in the packet. There is no limit on the
899: length of the string. */
900: cp = getenv("TERM");
901: if (!cp)
902: cp = "";
1.69.2.5 miod 903: packet_put_cstring(cp);
1.31 markus 904:
905: /* Store window size in the packet. */
906: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
907: memset(&ws, 0, sizeof(ws));
908: packet_put_int(ws.ws_row);
909: packet_put_int(ws.ws_col);
910: packet_put_int(ws.ws_xpixel);
911: packet_put_int(ws.ws_ypixel);
912:
913: /* Store tty modes in the packet. */
1.69.2.4 jason 914: tty_make_modes(fileno(stdin), NULL);
1.31 markus 915:
916: /* Send the packet, and wait for it to leave. */
917: packet_send();
918: packet_write_wait();
919:
920: /* Read response from the server. */
921: type = packet_read(&plen);
1.43 markus 922: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 923: interactive = 1;
1.45 markus 924: have_tty = 1;
1.43 markus 925: } else if (type == SSH_SMSG_FAILURE)
1.31 markus 926: log("Warning: Remote host failed or refused to allocate a pseudo tty.");
927: else
928: packet_disconnect("Protocol error waiting for pty request response.");
929: }
930: /* Request X11 forwarding if enabled and DISPLAY is set. */
931: if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.50 markus 932: char proto[512], data[512];
933: /* Get reasonable local authentication information. */
934: x11_get_proto(proto, sizeof proto, data, sizeof data);
935: /* Request forwarding with authentication spoofing. */
1.31 markus 936: debug("Requesting X11 forwarding with authentication spoofing.");
1.50 markus 937: x11_request_forwarding_with_spoofing(0, proto, data);
1.31 markus 938:
939: /* Read response from the server. */
940: type = packet_read(&plen);
941: if (type == SSH_SMSG_SUCCESS) {
942: interactive = 1;
1.50 markus 943: } else if (type == SSH_SMSG_FAILURE) {
1.31 markus 944: log("Warning: Remote host denied X11 forwarding.");
1.50 markus 945: } else {
1.31 markus 946: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 947: }
1.31 markus 948: }
949: /* Tell the packet module whether this is an interactive session. */
1.69.2.1 jason 950: packet_set_interactive(interactive);
1.31 markus 951:
952: /* Request authentication agent forwarding if appropriate. */
1.69.2.1 jason 953: check_agent_present();
954:
1.31 markus 955: if (options.forward_agent) {
956: debug("Requesting authentication agent forwarding.");
957: auth_request_forwarding();
958:
959: /* Read response from the server. */
960: type = packet_read(&plen);
961: packet_integrity_check(plen, 0, type);
962: if (type != SSH_SMSG_SUCCESS)
963: log("Warning: Remote host denied authentication agent forwarding.");
964: }
965:
1.69.2.1 jason 966: /* Initiate port forwardings. */
967: ssh_init_forwarding();
1.34 markus 968:
969: /* If requested, let ssh continue in the background. */
1.48 markus 970: if (fork_after_authentication_flag)
1.34 markus 971: if (daemon(1, 1) < 0)
972: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 973:
1.33 markus 974: /*
975: * If a command was specified on the command line, execute the
976: * command now. Otherwise request the server to start a shell.
977: */
1.31 markus 978: if (buffer_len(&command) > 0) {
979: int len = buffer_len(&command);
980: if (len > 900)
981: len = 900;
982: debug("Sending command: %.*s", len, buffer_ptr(&command));
983: packet_start(SSH_CMSG_EXEC_CMD);
984: packet_put_string(buffer_ptr(&command), buffer_len(&command));
985: packet_send();
986: packet_write_wait();
987: } else {
988: debug("Requesting shell.");
989: packet_start(SSH_CMSG_EXEC_SHELL);
990: packet_send();
991: packet_write_wait();
992: }
993:
994: /* Enter the interactive session. */
1.69.2.5 miod 995: return client_loop(have_tty, tty_flag ?
996: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 997: }
998:
1.69.2.5 miod 999: static void
1.69.2.1 jason 1000: client_subsystem_reply(int type, int plen, void *ctxt)
1.45 markus 1001: {
1.69.2.1 jason 1002: int id, len;
1.45 markus 1003:
1.69.2.1 jason 1004: id = packet_get_int();
1005: len = buffer_len(&command);
1.69.2.3 jason 1006: if (len > 900)
1007: len = 900;
1.69.2.1 jason 1008: packet_done();
1009: if (type == SSH2_MSG_CHANNEL_FAILURE)
1010: fatal("Request for subsystem '%.*s' failed on channel %d",
1011: len, buffer_ptr(&command), id);
1012: }
1.45 markus 1013:
1.69.2.5 miod 1014: /* request pty/x11/agent/tcpfwd/shell for channel */
1015: static void
1016: ssh_session2_setup(int id, void *arg)
1.45 markus 1017: {
1018: int len;
1.69.2.1 jason 1019: int interactive = 0;
1.69.2.4 jason 1020: struct termios tio;
1.69.2.1 jason 1021:
1.69.2.5 miod 1022: debug("ssh_session2_setup: id %d", id);
1.31 markus 1023:
1.45 markus 1024: if (tty_flag) {
1025: struct winsize ws;
1026: char *cp;
1027: cp = getenv("TERM");
1028: if (!cp)
1029: cp = "";
1030: /* Store window size in the packet. */
1031: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
1032: memset(&ws, 0, sizeof(ws));
1033:
1034: channel_request_start(id, "pty-req", 0);
1035: packet_put_cstring(cp);
1036: packet_put_int(ws.ws_col);
1037: packet_put_int(ws.ws_row);
1038: packet_put_int(ws.ws_xpixel);
1039: packet_put_int(ws.ws_ypixel);
1.69.2.4 jason 1040: tio = get_saved_tio();
1041: tty_make_modes(/*ignored*/ 0, &tio);
1.45 markus 1042: packet_send();
1.69.2.1 jason 1043: interactive = 1;
1.45 markus 1044: /* XXX wait for reply */
1045: }
1.50 markus 1046: if (options.forward_x11 &&
1047: getenv("DISPLAY") != NULL) {
1048: char proto[512], data[512];
1049: /* Get reasonable local authentication information. */
1050: x11_get_proto(proto, sizeof proto, data, sizeof data);
1051: /* Request forwarding with authentication spoofing. */
1052: debug("Requesting X11 forwarding with authentication spoofing.");
1053: x11_request_forwarding_with_spoofing(id, proto, data);
1.69.2.1 jason 1054: interactive = 1;
1.50 markus 1055: /* XXX wait for reply */
1056: }
1057:
1.69.2.1 jason 1058: check_agent_present();
1059: if (options.forward_agent) {
1060: debug("Requesting authentication agent forwarding.");
1061: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1062: packet_send();
1063: }
1064:
1.45 markus 1065: len = buffer_len(&command);
1066: if (len > 0) {
1067: if (len > 900)
1068: len = 900;
1.69.2.1 jason 1069: if (subsystem_flag) {
1070: debug("Sending subsystem: %.*s", len, buffer_ptr(&command));
1071: channel_request_start(id, "subsystem", /*want reply*/ 1);
1072: /* register callback for reply */
1073: /* XXX we asume that client_loop has already been called */
1074: dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);
1075: dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);
1076: } else {
1077: debug("Sending command: %.*s", len, buffer_ptr(&command));
1078: channel_request_start(id, "exec", 0);
1079: }
1.69.2.3 jason 1080: packet_put_string(buffer_ptr(&command), buffer_len(&command));
1.45 markus 1081: packet_send();
1082: } else {
1083: channel_request(id, "shell", 0);
1084: }
1085: /* channel_callback(id, SSH2_MSG_OPEN_CONFIGMATION, client_init, 0); */
1.69.2.1 jason 1086:
1.45 markus 1087: /* register different callback, etc. XXX */
1.69.2.1 jason 1088: packet_set_interactive(interactive);
1.45 markus 1089: }
1090:
1.69.2.5 miod 1091: /* open new channel for a session */
1092: static int
1093: ssh_session2_open(void)
1.45 markus 1094: {
1.69.2.5 miod 1095: Channel *c;
1096: int window, packetmax, in, out, err;
1.60 markus 1097:
1.62 markus 1098: if (stdin_null_flag) {
1.69.2.1 jason 1099: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1100: } else {
1101: in = dup(STDIN_FILENO);
1102: }
1.60 markus 1103: out = dup(STDOUT_FILENO);
1104: err = dup(STDERR_FILENO);
1.45 markus 1105:
1106: if (in < 0 || out < 0 || err < 0)
1.62 markus 1107: fatal("dup() in/out/err failed");
1.45 markus 1108:
1.69 markus 1109: /* enable nonblocking unless tty */
1110: if (!isatty(in))
1111: set_nonblock(in);
1112: if (!isatty(out))
1113: set_nonblock(out);
1114: if (!isatty(err))
1115: set_nonblock(err);
1116:
1.65 markus 1117: window = CHAN_SES_WINDOW_DEFAULT;
1118: packetmax = CHAN_SES_PACKET_DEFAULT;
1119: if (!tty_flag) {
1.45 markus 1120: window *= 2;
1.65 markus 1121: packetmax *=2;
1.45 markus 1122: }
1.69.2.5 miod 1123: c = channel_new(
1.45 markus 1124: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1125: window, packetmax, CHAN_EXTENDED_WRITE,
1.69 markus 1126: xstrdup("client-session"), /*nonblock*/0);
1.69.2.5 miod 1127: if (c == NULL)
1128: fatal("ssh_session2_open: channel_new failed");
1.45 markus 1129:
1.69.2.5 miod 1130: debug3("ssh_session2_open: channel_new: %d", c->self);
1.69.2.4 jason 1131:
1.69.2.5 miod 1132: channel_send_open(c->self);
1133: if (!no_shell_flag)
1134: channel_register_callback(c->self,
1135: SSH2_MSG_CHANNEL_OPEN_CONFIRMATION,
1136: ssh_session2_setup, (void *)0);
1.31 markus 1137:
1.69.2.5 miod 1138: return c->self;
1.69.2.1 jason 1139: }
1140:
1.69.2.5 miod 1141: static int
1.69.2.4 jason 1142: ssh_session2(void)
1.69.2.1 jason 1143: {
1.69.2.5 miod 1144: int id = -1;
1.69.2.1 jason 1145:
1.69.2.4 jason 1146: /* XXX should be pre-session */
1147: ssh_init_forwarding();
1148:
1.69.2.5 miod 1149: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1150: id = ssh_session2_open();
1.69.2.4 jason 1151:
1152: /* If requested, let ssh continue in the background. */
1153: if (fork_after_authentication_flag)
1154: if (daemon(1, 1) < 0)
1155: fatal("daemon() failed: %.200s", strerror(errno));
1156:
1.69.2.5 miod 1157: return client_loop(tty_flag, tty_flag ?
1158: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.69.2.3 jason 1159: }
1160:
1.69.2.5 miod 1161: static void
1.69.2.3 jason 1162: load_public_identity_files(void)
1163: {
1164: char *filename;
1165: Key *public;
1.69.2.5 miod 1166: int i = 0;
1.69.2.3 jason 1167:
1.69.2.5 miod 1168: #ifdef SMARTCARD
1169: if (options.smartcard_device != NULL &&
1170: options.num_identity_files + 1 < SSH_MAX_IDENTITY_FILES &&
1171: (public = sc_get_key(options.smartcard_device)) != NULL ) {
1172: Key *new;
1173:
1174: if (options.num_identity_files + 2 > SSH_MAX_IDENTITY_FILES)
1175: options.num_identity_files = SSH_MAX_IDENTITY_FILES - 2;
1176: memmove(&options.identity_files[2], &options.identity_files[0],
1177: sizeof(char *) * options.num_identity_files);
1178: options.num_identity_files += 2;
1179: i = 2;
1180:
1181: /* XXX ssh1 vs ssh2 */
1182: new = key_new(KEY_RSA);
1183: new->flags = KEY_FLAG_EXT;
1184: BN_copy(new->rsa->n, public->rsa->n);
1185: BN_copy(new->rsa->e, public->rsa->e);
1186: RSA_set_method(new->rsa, sc_get_engine());
1187: options.identity_keys[0] = new;
1188: options.identity_files[0] = xstrdup("smartcard rsa key");;
1189:
1190: new = key_new(KEY_RSA1);
1191: new->flags = KEY_FLAG_EXT;
1192: BN_copy(new->rsa->n, public->rsa->n);
1193: BN_copy(new->rsa->e, public->rsa->e);
1194: RSA_set_method(new->rsa, sc_get_engine());
1195: options.identity_keys[1] = new;
1196: options.identity_files[1] = xstrdup("smartcard rsa1 key");
1197:
1198: key_free(public);
1199: }
1200: #endif /* SMARTCARD */
1201: for (; i < options.num_identity_files; i++) {
1.69.2.3 jason 1202: filename = tilde_expand_filename(options.identity_files[i],
1203: original_real_uid);
1.69.2.4 jason 1204: public = key_load_public(filename, NULL);
1.69.2.3 jason 1205: debug("identity file %s type %d", filename,
1206: public ? public->type : -1);
1207: xfree(options.identity_files[i]);
1208: options.identity_files[i] = filename;
1209: options.identity_keys[i] = public;
1210: }
1.1 deraadt 1211: }