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