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