Annotation of src/usr.bin/ssh/ssh.c, Revision 1.225
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.202 markus 16: * Copyright (c) 2000, 2001, 2002, 2003 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.225 ! dtucker 43: RCSID("$OpenBSD: ssh.c,v 1.224 2004/07/28 09:40:29 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.214 djm 56: #include "bufaux.h"
1.123 markus 57: #include "channels.h"
1.49 markus 58: #include "key.h"
1.58 markus 59: #include "authfd.h"
1.49 markus 60: #include "authfile.h"
1.83 markus 61: #include "pathnames.h"
1.214 djm 62: #include "dispatch.h"
1.81 markus 63: #include "clientloop.h"
1.84 markus 64: #include "log.h"
65: #include "readconf.h"
66: #include "sshconnect.h"
67: #include "misc.h"
1.95 markus 68: #include "kex.h"
69: #include "mac.h"
1.213 deraadt 70: #include "sshpty.h"
1.212 djm 71: #include "match.h"
1.214 djm 72: #include "msg.h"
73: #include "monitor_fdpass.h"
1.225 ! dtucker 74: #include "uidswap.h"
1.49 markus 75:
1.127 markus 76: #ifdef SMARTCARD
77: #include "scard.h"
1.137 jakob 78: #endif
1.127 markus 79:
1.49 markus 80: extern char *__progname;
1.1 deraadt 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.214 djm 144: /* fd to control socket */
145: int control_fd = -1;
146:
147: /* Only used in control client mode */
148: volatile sig_atomic_t control_client_terminate = 0;
149: u_int control_server_pid = 0;
150:
1.1 deraadt 151: /* Prints a help message to the user. This function never returns. */
152:
1.126 itojun 153: static void
1.93 itojun 154: usage(void)
1.1 deraadt 155: {
1.208 markus 156: fprintf(stderr,
1.217 djm 157: "usage: ssh [-1246AaCfghkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
1.208 markus 158: " [-D port] [-e escape_char] [-F configfile] [-i identity_file]\n"
159: " [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]\n"
1.219 markus 160: " [-p port] [-R port:host:hostport] [-S ctl] [user@]hostname [command]\n"
1.208 markus 161: );
1.31 markus 162: exit(1);
1.1 deraadt 163: }
164:
1.126 itojun 165: static int ssh_session(void);
166: static int ssh_session2(void);
167: static void load_public_identity_files(void);
1.214 djm 168: static void control_client(const char *path);
1.45 markus 169:
1.32 deraadt 170: /*
171: * Main program for the ssh client.
172: */
1.2 provos 173: int
174: main(int ac, char **av)
1.1 deraadt 175: {
1.178 markus 176: int i, opt, exit_status;
1.35 markus 177: u_short fwd_port, fwd_host_port;
1.141 stevesk 178: char sfwd_port[6], sfwd_host_port[6];
1.205 markus 179: char *p, *cp, *line, buf[256];
1.31 markus 180: struct stat st;
1.98 markus 181: struct passwd *pw;
1.45 markus 182: int dummy;
1.144 stevesk 183: extern int optind, optreset;
184: extern char *optarg;
1.31 markus 185:
1.33 markus 186: /*
187: * Save the original real uid. It will be needed later (uid-swapping
188: * may clobber the real uid).
189: */
1.31 markus 190: original_real_uid = getuid();
191: original_effective_uid = geteuid();
192:
1.184 stevesk 193: /*
194: * Use uid-swapping to give up root privileges for the duration of
195: * option processing. We will re-instantiate the rights when we are
196: * ready to create the privileged port, and will permanently drop
197: * them when the port has been created (actually, when the connection
198: * has been made, as we may need to create the port several times).
199: */
200: PRIV_END;
201:
1.31 markus 202: /* If we are installed setuid root be careful to not drop core. */
203: if (original_real_uid != original_effective_uid) {
204: struct rlimit rlim;
205: rlim.rlim_cur = rlim.rlim_max = 0;
206: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
207: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 208: }
1.107 markus 209: /* Get user data. */
210: pw = getpwuid(original_real_uid);
211: if (!pw) {
1.191 itojun 212: logit("You don't exist, go away!");
1.107 markus 213: exit(1);
214: }
215: /* Take a copy of the returned structure. */
216: pw = pwcopy(pw);
1.31 markus 217:
1.33 markus 218: /*
219: * Set our umask to something reasonable, as some files are created
220: * with the default umask. This will make them world-readable but
221: * writable only by the owner, which is ok for all files for which we
222: * don't set the modes explicitly.
223: */
1.31 markus 224: umask(022);
225:
226: /* Initialize option structure to indicate that no values have been set. */
227: initialize_options(&options);
228:
229: /* Parse command-line arguments. */
230: host = NULL;
231:
1.128 fgsch 232: again:
233: while ((opt = getopt(ac, av,
1.214 djm 234: "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNPR:S:TVXY")) != -1) {
1.31 markus 235: switch (opt) {
1.91 jakob 236: case '1':
237: options.protocol = SSH_PROTO_1;
238: break;
1.47 markus 239: case '2':
240: options.protocol = SSH_PROTO_2;
241: break;
1.37 markus 242: case '4':
1.196 djm 243: options.address_family = AF_INET;
1.37 markus 244: break;
245: case '6':
1.196 djm 246: options.address_family = AF_INET6;
1.37 markus 247: break;
1.31 markus 248: case 'n':
249: stdin_null_flag = 1;
250: break;
251: case 'f':
252: fork_after_authentication_flag = 1;
253: stdin_null_flag = 1;
254: break;
255: case 'x':
256: options.forward_x11 = 0;
257: break;
258: case 'X':
259: options.forward_x11 = 1;
260: break;
1.202 markus 261: case 'Y':
262: options.forward_x11 = 1;
263: options.forward_x11_trusted = 1;
264: break;
1.31 markus 265: case 'g':
266: options.gateway_ports = 1;
267: break;
1.183 stevesk 268: case 'P': /* deprecated */
1.31 markus 269: options.use_privileged_port = 0;
270: break;
271: case 'a':
272: options.forward_agent = 0;
1.53 markus 273: break;
274: case 'A':
275: options.forward_agent = 1;
1.31 markus 276: break;
277: case 'k':
1.204 dtucker 278: options.gss_deleg_creds = 0;
1.31 markus 279: break;
280: case 'i':
281: if (stat(optarg, &st) < 0) {
1.128 fgsch 282: fprintf(stderr, "Warning: Identity file %s "
283: "does not exist.\n", optarg);
1.31 markus 284: break;
285: }
1.128 fgsch 286: if (options.num_identity_files >=
287: SSH_MAX_IDENTITY_FILES)
288: fatal("Too many identity files specified "
289: "(max %d)", SSH_MAX_IDENTITY_FILES);
290: options.identity_files[options.num_identity_files++] =
291: xstrdup(optarg);
1.31 markus 292: break;
1.127 markus 293: case 'I':
294: #ifdef SMARTCARD
1.133 markus 295: options.smartcard_device = xstrdup(optarg);
1.137 jakob 296: #else
1.127 markus 297: fprintf(stderr, "no support for smartcards.\n");
1.137 jakob 298: #endif
1.127 markus 299: break;
1.31 markus 300: case 't':
1.79 markus 301: if (tty_flag)
302: force_tty_flag = 1;
1.31 markus 303: tty_flag = 1;
304: break;
305: case 'v':
1.197 markus 306: if (debug_flag == 0) {
1.66 markus 307: debug_flag = 1;
308: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 309: } else {
310: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
311: options.log_level++;
1.66 markus 312: break;
1.197 markus 313: }
1.66 markus 314: /* fallthrough */
1.31 markus 315: case 'V':
1.209 markus 316: fprintf(stderr, "%s, %s\n",
317: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 318: if (opt == 'V')
319: exit(0);
320: break;
321: case 'q':
322: options.log_level = SYSLOG_LEVEL_QUIET;
323: break;
324: case 'e':
325: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 326: (u_char) optarg[1] >= 64 &&
327: (u_char) optarg[1] < 128)
1.78 markus 328: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 329: else if (strlen(optarg) == 1)
1.78 markus 330: options.escape_char = (u_char) optarg[0];
1.31 markus 331: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 332: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 333: else {
1.128 fgsch 334: fprintf(stderr, "Bad escape character '%s'.\n",
335: optarg);
1.31 markus 336: exit(1);
337: }
338: break;
339: case 'c':
1.49 markus 340: if (ciphers_valid(optarg)) {
341: /* SSH2 only */
342: options.ciphers = xstrdup(optarg);
1.224 markus 343: options.cipher = SSH_CIPHER_INVALID;
1.49 markus 344: } else {
345: /* SSH1 only */
1.74 markus 346: options.cipher = cipher_number(optarg);
347: if (options.cipher == -1) {
1.128 fgsch 348: fprintf(stderr,
349: "Unknown cipher type '%s'\n",
350: optarg);
1.49 markus 351: exit(1);
352: }
1.128 fgsch 353: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 354: options.ciphers = "3des-cbc";
1.128 fgsch 355: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 356: options.ciphers = "blowfish-cbc";
1.128 fgsch 357: else
1.74 markus 358: options.ciphers = (char *)-1;
1.95 markus 359: }
360: break;
361: case 'm':
362: if (mac_valid(optarg))
363: options.macs = xstrdup(optarg);
364: else {
1.128 fgsch 365: fprintf(stderr, "Unknown mac type '%s'\n",
366: optarg);
1.95 markus 367: exit(1);
1.31 markus 368: }
369: break;
1.214 djm 370: case 'M':
1.219 markus 371: options.control_master =
372: (options.control_master >= 1) ? 2 : 1;
1.214 djm 373: break;
1.31 markus 374: case 'p':
1.113 stevesk 375: options.port = a2port(optarg);
376: if (options.port == 0) {
1.109 markus 377: fprintf(stderr, "Bad port '%s'\n", optarg);
378: exit(1);
379: }
1.31 markus 380: break;
381: case 'l':
382: options.user = optarg;
383: break;
1.141 stevesk 384:
385: case 'L':
1.31 markus 386: case 'R':
1.141 stevesk 387: if (sscanf(optarg, "%5[0-9]:%255[^:]:%5[0-9]",
388: sfwd_port, buf, sfwd_host_port) != 3 &&
389: sscanf(optarg, "%5[0-9]/%255[^/]/%5[0-9]",
390: sfwd_port, buf, sfwd_host_port) != 3) {
1.128 fgsch 391: fprintf(stderr,
1.141 stevesk 392: "Bad forwarding specification '%s'\n",
1.128 fgsch 393: optarg);
1.31 markus 394: usage();
395: /* NOTREACHED */
396: }
1.141 stevesk 397: if ((fwd_port = a2port(sfwd_port)) == 0 ||
1.165 markus 398: (fwd_host_port = a2port(sfwd_host_port)) == 0) {
1.128 fgsch 399: fprintf(stderr,
1.141 stevesk 400: "Bad forwarding port(s) '%s'\n", optarg);
401: exit(1);
1.31 markus 402: }
1.141 stevesk 403: if (opt == 'L')
404: add_local_forward(&options, fwd_port, buf,
405: fwd_host_port);
406: else if (opt == 'R')
407: add_remote_forward(&options, fwd_port, buf,
1.151 deraadt 408: fwd_host_port);
1.31 markus 409: break;
1.108 markus 410:
411: case 'D':
1.113 stevesk 412: fwd_port = a2port(optarg);
413: if (fwd_port == 0) {
1.128 fgsch 414: fprintf(stderr, "Bad dynamic port '%s'\n",
415: optarg);
1.109 markus 416: exit(1);
417: }
1.200 markus 418: add_local_forward(&options, fwd_port, "socks", 0);
1.108 markus 419: break;
420:
1.31 markus 421: case 'C':
422: options.compression = 1;
423: break;
1.45 markus 424: case 'N':
425: no_shell_flag = 1;
426: no_tty_flag = 1;
427: break;
428: case 'T':
429: no_tty_flag = 1;
430: break;
1.31 markus 431: case 'o':
432: dummy = 1;
1.205 markus 433: line = xstrdup(optarg);
1.128 fgsch 434: if (process_config_line(&options, host ? host : "",
1.205 markus 435: line, "command-line", 0, &dummy) != 0)
1.31 markus 436: exit(1);
1.205 markus 437: xfree(line);
1.31 markus 438: break;
1.85 djm 439: case 's':
440: subsystem_flag = 1;
1.117 markus 441: break;
1.214 djm 442: case 'S':
443: if (options.control_path != NULL)
444: free(options.control_path);
445: options.control_path = xstrdup(optarg);
446: break;
1.117 markus 447: case 'b':
448: options.bind_address = optarg;
1.85 djm 449: break;
1.139 markus 450: case 'F':
451: config = optarg;
452: break;
1.31 markus 453: default:
454: usage();
1.1 deraadt 455: }
1.31 markus 456: }
457:
1.128 fgsch 458: ac -= optind;
459: av += optind;
460:
461: if (ac > 0 && !host && **av != '-') {
1.188 markus 462: if (strrchr(*av, '@')) {
1.128 fgsch 463: p = xstrdup(*av);
1.188 markus 464: cp = strrchr(p, '@');
1.128 fgsch 465: if (cp == NULL || cp == p)
466: usage();
467: options.user = p;
468: *cp = '\0';
469: host = ++cp;
470: } else
471: host = *av;
1.189 millert 472: if (ac > 1) {
473: optind = optreset = 1;
1.128 fgsch 474: goto again;
475: }
1.189 millert 476: ac--, av++;
1.128 fgsch 477: }
478:
1.31 markus 479: /* Check that we got a host name. */
480: if (!host)
481: usage();
482:
1.54 markus 483: SSLeay_add_all_algorithms();
1.72 markus 484: ERR_load_crypto_strings();
1.31 markus 485:
486: /* Initialize the command to execute on remote host. */
487: buffer_init(&command);
1.1 deraadt 488:
1.33 markus 489: /*
490: * Save the command to execute on the remote host in a buffer. There
491: * is no limit on the length of the command, except by the maximum
492: * packet size. Also sets the tty flag if there is no command.
493: */
1.128 fgsch 494: if (!ac) {
1.31 markus 495: /* No command specified - execute shell on a tty. */
496: tty_flag = 1;
1.85 djm 497: if (subsystem_flag) {
1.128 fgsch 498: fprintf(stderr,
499: "You must specify a subsystem to invoke.\n");
1.85 djm 500: usage();
501: }
1.31 markus 502: } else {
1.128 fgsch 503: /* A command has been specified. Store it into the buffer. */
504: for (i = 0; i < ac; i++) {
505: if (i)
1.31 markus 506: buffer_append(&command, " ", 1);
507: buffer_append(&command, av[i], strlen(av[i]));
508: }
509: }
510:
511: /* Cannot fork to background if no command. */
1.63 markus 512: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 513: fatal("Cannot fork into background without a command to execute.");
514:
515: /* Allocate a tty by default if no command specified. */
516: if (buffer_len(&command) == 0)
517: tty_flag = 1;
518:
1.180 deraadt 519: /* Force no tty */
1.75 markus 520: if (no_tty_flag)
521: tty_flag = 0;
1.31 markus 522: /* Do not allocate a tty if stdin is not a tty. */
1.79 markus 523: if (!isatty(fileno(stdin)) && !force_tty_flag) {
1.31 markus 524: if (tty_flag)
1.191 itojun 525: logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 526: tty_flag = 0;
527: }
1.45 markus 528:
1.101 markus 529: /*
530: * Initialize "log" output. Since we are the client all output
531: * actually goes to stderr.
532: */
1.111 markus 533: log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
534: SYSLOG_FACILITY_USER, 1);
1.31 markus 535:
1.139 markus 536: /*
537: * Read per-user configuration file. Ignore the system wide config
538: * file if the user specifies a config file on the command line.
539: */
540: if (config != NULL) {
1.211 djm 541: if (!read_config_file(config, host, &options, 0))
1.142 stevesk 542: fatal("Can't open user config file %.100s: "
543: "%.100s", config, strerror(errno));
1.139 markus 544: } else {
545: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
546: _PATH_SSH_USER_CONFFILE);
1.210 djm 547: (void)read_config_file(buf, host, &options, 1);
1.139 markus 548:
1.145 markus 549: /* Read systemwide configuration file after use config. */
1.223 deraadt 550: (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
1.210 djm 551: &options, 0);
1.139 markus 552: }
1.31 markus 553:
554: /* Fill configuration defaults. */
555: fill_default_options(&options);
556:
1.196 djm 557: channel_set_af(options.address_family);
558:
1.31 markus 559: /* reinit */
1.101 markus 560: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 561:
562: if (options.user == NULL)
563: options.user = xstrdup(pw->pw_name);
564:
565: if (options.hostname != NULL)
566: host = options.hostname;
1.195 markus 567:
568: /* force lowercase for hostkey matching */
569: if (options.host_key_alias != NULL) {
570: for (p = options.host_key_alias; *p; p++)
571: if (isupper(*p))
572: *p = tolower(*p);
573: }
1.190 markus 574:
575: if (options.proxy_command != NULL &&
576: strcmp(options.proxy_command, "none") == 0)
577: options.proxy_command = NULL;
1.31 markus 578:
1.214 djm 579: if (options.control_path != NULL) {
580: options.control_path = tilde_expand_filename(
581: options.control_path, original_real_uid);
582: }
583: if (options.control_path != NULL && options.control_master == 0)
584: control_client(options.control_path); /* This doesn't return */
585:
1.77 markus 586: /* Open a connection to the remote host. */
1.203 djm 587: if (ssh_connect(host, &hostaddr, options.port,
1.196 djm 588: options.address_family, options.connection_attempts,
1.177 markus 589: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 590: options.proxy_command) != 0)
1.178 markus 591: exit(1);
1.31 markus 592:
1.33 markus 593: /*
594: * If we successfully made the connection, load the host private key
595: * in case we will need it later for combined rsa-rhosts
596: * authentication. This must be done before releasing extra
597: * privileges, because the file is only readable by root.
1.174 markus 598: * If we cannot access the private keys, load the public keys
599: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 600: */
1.112 markus 601: sensitive_data.nkeys = 0;
602: sensitive_data.keys = NULL;
1.173 markus 603: sensitive_data.external_keysign = 0;
1.178 markus 604: if (options.rhosts_rsa_authentication ||
605: options.hostbased_authentication) {
1.112 markus 606: sensitive_data.nkeys = 3;
1.180 deraadt 607: sensitive_data.keys = xmalloc(sensitive_data.nkeys *
608: sizeof(Key));
1.177 markus 609:
610: PRIV_START;
1.112 markus 611: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.105 markus 612: _PATH_HOST_KEY_FILE, "", NULL);
1.112 markus 613: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
614: _PATH_HOST_DSA_KEY_FILE, "", NULL);
615: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
616: _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.177 markus 617: PRIV_END;
1.173 markus 618:
1.181 markus 619: if (options.hostbased_authentication == 1 &&
620: sensitive_data.keys[0] == NULL &&
1.173 markus 621: sensitive_data.keys[1] == NULL &&
622: sensitive_data.keys[2] == NULL) {
623: sensitive_data.keys[1] = key_load_public(
624: _PATH_HOST_DSA_KEY_FILE, NULL);
625: sensitive_data.keys[2] = key_load_public(
626: _PATH_HOST_RSA_KEY_FILE, NULL);
627: sensitive_data.external_keysign = 1;
628: }
1.31 markus 629: }
1.33 markus 630: /*
631: * Get rid of any extra privileges that we may have. We will no
632: * longer need them. Also, extra privileges could make it very hard
633: * to read identity files and other non-world-readable files from the
634: * user's home directory if it happens to be on a NFS volume where
635: * root is mapped to nobody.
636: */
1.225 ! dtucker 637: if (original_effective_uid == 0) {
! 638: PRIV_START;
! 639: permanently_set_uid(pw);
! 640: }
1.31 markus 641:
1.33 markus 642: /*
643: * Now that we are back to our own permissions, create ~/.ssh
644: * directory if it doesn\'t already exist.
645: */
1.138 jakob 646: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 647: if (stat(buf, &st) < 0)
1.57 djm 648: if (mkdir(buf, 0700) < 0)
1.31 markus 649: error("Could not create directory '%.200s'.", buf);
650:
1.104 markus 651: /* load options.identity_files */
652: load_public_identity_files();
653:
654: /* Expand ~ in known host file names. */
655: /* XXX mem-leaks: */
1.72 markus 656: options.system_hostfile =
657: tilde_expand_filename(options.system_hostfile, original_real_uid);
658: options.user_hostfile =
659: tilde_expand_filename(options.user_hostfile, original_real_uid);
660: options.system_hostfile2 =
661: tilde_expand_filename(options.system_hostfile2, original_real_uid);
662: options.user_hostfile2 =
663: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 664:
665: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 666:
667: /* Log into the remote system. This never returns if the login fails. */
1.173 markus 668: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 669:
1.112 markus 670: /* We no longer need the private host keys. Clear them now. */
671: if (sensitive_data.nkeys != 0) {
672: for (i = 0; i < sensitive_data.nkeys; i++) {
673: if (sensitive_data.keys[i] != NULL) {
674: /* Destroys contents safely */
675: debug3("clear hostkey %d", i);
676: key_free(sensitive_data.keys[i]);
677: sensitive_data.keys[i] = NULL;
678: }
679: }
680: xfree(sensitive_data.keys);
1.134 markus 681: }
682: for (i = 0; i < options.num_identity_files; i++) {
683: if (options.identity_files[i]) {
684: xfree(options.identity_files[i]);
685: options.identity_files[i] = NULL;
686: }
687: if (options.identity_keys[i]) {
688: key_free(options.identity_keys[i]);
689: options.identity_keys[i] = NULL;
690: }
1.112 markus 691: }
1.31 markus 692:
1.45 markus 693: exit_status = compat20 ? ssh_session2() : ssh_session();
694: packet_close();
1.186 djm 695:
1.214 djm 696: if (options.control_path != NULL && control_fd != -1)
697: unlink(options.control_path);
698:
1.186 djm 699: /*
1.203 djm 700: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 701: * case it hangs and instead rely on init to reap the child
702: */
703: if (proxy_command_pid > 1)
704: kill(proxy_command_pid, SIGHUP);
705:
1.45 markus 706: return exit_status;
707: }
708:
1.202 markus 709: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
710:
1.126 itojun 711: static void
1.150 stevesk 712: x11_get_proto(char **_proto, char **_data)
1.50 markus 713: {
1.202 markus 714: char cmd[1024];
1.50 markus 715: char line[512];
1.202 markus 716: char xdisplay[512];
1.150 stevesk 717: static char proto[512], data[512];
1.50 markus 718: FILE *f;
1.202 markus 719: int got_data = 0, generated = 0, do_unlink = 0, i;
720: char *display, *xauthdir, *xauthfile;
1.182 stevesk 721: struct stat st;
1.50 markus 722:
1.202 markus 723: xauthdir = xauthfile = NULL;
1.150 stevesk 724: *_proto = proto;
725: *_data = data;
726: proto[0] = data[0] = '\0';
1.202 markus 727:
1.182 stevesk 728: if (!options.xauth_location ||
729: (stat(options.xauth_location, &st) == -1)) {
730: debug("No xauth program.");
731: } else {
732: if ((display = getenv("DISPLAY")) == NULL) {
733: debug("x11_get_proto: DISPLAY not set");
734: return;
735: }
1.202 markus 736: /*
737: * Handle FamilyLocal case where $DISPLAY does
738: * not match an authorization entry. For this we
739: * just try "xauth list unix:displaynum.screennum".
740: * XXX: "localhost" match to determine FamilyLocal
741: * is not perfect.
742: */
743: if (strncmp(display, "localhost:", 10) == 0) {
744: snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
745: display + 10);
746: display = xdisplay;
747: }
748: if (options.forward_x11_trusted == 0) {
749: xauthdir = xmalloc(MAXPATHLEN);
750: xauthfile = xmalloc(MAXPATHLEN);
751: strlcpy(xauthdir, "/tmp/ssh-XXXXXXXXXX", MAXPATHLEN);
752: if (mkdtemp(xauthdir) != NULL) {
753: do_unlink = 1;
754: snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile",
755: xauthdir);
756: snprintf(cmd, sizeof(cmd),
757: "%s -f %s generate %s " SSH_X11_PROTO
1.207 markus 758: " untrusted timeout 1200 2>" _PATH_DEVNULL,
1.202 markus 759: options.xauth_location, xauthfile, display);
760: debug2("x11_get_proto: %s", cmd);
761: if (system(cmd) == 0)
762: generated = 1;
763: }
764: }
765: snprintf(cmd, sizeof(cmd),
766: "%s %s%s list %s . 2>" _PATH_DEVNULL,
767: options.xauth_location,
768: generated ? "-f " : "" ,
769: generated ? xauthfile : "",
770: display);
771: debug2("x11_get_proto: %s", cmd);
772: f = popen(cmd, "r");
1.55 markus 773: if (f && fgets(line, sizeof(line), f) &&
1.150 stevesk 774: sscanf(line, "%*s %511s %511s", proto, data) == 2)
1.55 markus 775: got_data = 1;
776: if (f)
777: pclose(f);
778: }
1.202 markus 779:
780: if (do_unlink) {
781: unlink(xauthfile);
782: rmdir(xauthdir);
783: }
784: if (xauthdir)
785: xfree(xauthdir);
786: if (xauthfile)
787: xfree(xauthfile);
788:
1.50 markus 789: /*
790: * If we didn't get authentication data, just make up some
791: * data. The forwarding code will check the validity of the
792: * response anyway, and substitute this data. The X11
793: * server, however, will ignore this fake data and use
794: * whatever authentication mechanisms it was using otherwise
795: * for the local connection.
796: */
797: if (!got_data) {
1.221 avsm 798: u_int32_t rnd = 0;
1.50 markus 799:
1.202 markus 800: logit("Warning: No xauth data; "
801: "using fake authentication data for X11 forwarding.");
802: strlcpy(proto, SSH_X11_PROTO, sizeof proto);
1.50 markus 803: for (i = 0; i < 16; i++) {
804: if (i % 4 == 0)
1.221 avsm 805: rnd = arc4random();
1.202 markus 806: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
1.221 avsm 807: rnd & 0xff);
808: rnd >>= 8;
1.50 markus 809: }
810: }
811: }
812:
1.126 itojun 813: static void
1.70 markus 814: ssh_init_forwarding(void)
815: {
1.86 markus 816: int success = 0;
1.70 markus 817: int i;
1.86 markus 818:
1.70 markus 819: /* Initiate local TCP/IP port forwardings. */
820: for (i = 0; i < options.num_local_forwards; i++) {
821: debug("Connections to local port %d forwarded to remote address %.200s:%d",
822: options.local_forwards[i].port,
823: options.local_forwards[i].host,
824: options.local_forwards[i].host_port);
1.158 markus 825: success += channel_setup_local_fwd_listener(
1.70 markus 826: options.local_forwards[i].port,
827: options.local_forwards[i].host,
828: options.local_forwards[i].host_port,
829: options.gateway_ports);
830: }
1.86 markus 831: if (i > 0 && success == 0)
832: error("Could not request local forwarding.");
1.70 markus 833:
834: /* Initiate remote TCP/IP port forwardings. */
835: for (i = 0; i < options.num_remote_forwards; i++) {
836: debug("Connections to remote port %d forwarded to local address %.200s:%d",
837: options.remote_forwards[i].port,
838: options.remote_forwards[i].host,
839: options.remote_forwards[i].host_port);
840: channel_request_remote_forwarding(
841: options.remote_forwards[i].port,
842: options.remote_forwards[i].host,
843: options.remote_forwards[i].host_port);
844: }
845: }
846:
1.126 itojun 847: static void
1.70 markus 848: check_agent_present(void)
849: {
850: if (options.forward_agent) {
851: /* Clear agent forwarding if we don\'t have an agent. */
1.185 stevesk 852: if (!ssh_agent_present())
1.70 markus 853: options.forward_agent = 0;
854: }
855: }
856:
1.126 itojun 857: static int
1.45 markus 858: ssh_session(void)
859: {
860: int type;
861: int interactive = 0;
862: int have_tty = 0;
863: struct winsize ws;
864: char *cp;
865:
1.31 markus 866: /* Enable compression if requested. */
867: if (options.compression) {
868: debug("Requesting compression at level %d.", options.compression_level);
869:
870: if (options.compression_level < 1 || options.compression_level > 9)
871: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
872:
873: /* Send the request. */
874: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
875: packet_put_int(options.compression_level);
876: packet_send();
877: packet_write_wait();
1.156 markus 878: type = packet_read();
1.31 markus 879: if (type == SSH_SMSG_SUCCESS)
880: packet_start_compression(options.compression_level);
881: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 882: logit("Warning: Remote host refused compression.");
1.31 markus 883: else
884: packet_disconnect("Protocol error waiting for compression response.");
885: }
886: /* Allocate a pseudo tty if appropriate. */
887: if (tty_flag) {
888: debug("Requesting pty.");
889:
890: /* Start the packet. */
891: packet_start(SSH_CMSG_REQUEST_PTY);
892:
893: /* Store TERM in the packet. There is no limit on the
894: length of the string. */
895: cp = getenv("TERM");
896: if (!cp)
897: cp = "";
1.124 markus 898: packet_put_cstring(cp);
1.31 markus 899:
900: /* Store window size in the packet. */
901: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
902: memset(&ws, 0, sizeof(ws));
903: packet_put_int(ws.ws_row);
904: packet_put_int(ws.ws_col);
905: packet_put_int(ws.ws_xpixel);
906: packet_put_int(ws.ws_ypixel);
907:
908: /* Store tty modes in the packet. */
1.115 stevesk 909: tty_make_modes(fileno(stdin), NULL);
1.31 markus 910:
911: /* Send the packet, and wait for it to leave. */
912: packet_send();
913: packet_write_wait();
914:
915: /* Read response from the server. */
1.156 markus 916: type = packet_read();
1.43 markus 917: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 918: interactive = 1;
1.45 markus 919: have_tty = 1;
1.43 markus 920: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 921: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 922: else
923: packet_disconnect("Protocol error waiting for pty request response.");
924: }
925: /* Request X11 forwarding if enabled and DISPLAY is set. */
926: if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.150 stevesk 927: char *proto, *data;
1.50 markus 928: /* Get reasonable local authentication information. */
1.150 stevesk 929: x11_get_proto(&proto, &data);
1.50 markus 930: /* Request forwarding with authentication spoofing. */
1.31 markus 931: debug("Requesting X11 forwarding with authentication spoofing.");
1.50 markus 932: x11_request_forwarding_with_spoofing(0, proto, data);
1.31 markus 933:
934: /* Read response from the server. */
1.156 markus 935: type = packet_read();
1.31 markus 936: if (type == SSH_SMSG_SUCCESS) {
937: interactive = 1;
1.50 markus 938: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 939: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 940: } else {
1.31 markus 941: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 942: }
1.31 markus 943: }
944: /* Tell the packet module whether this is an interactive session. */
1.80 markus 945: packet_set_interactive(interactive);
1.31 markus 946:
947: /* Request authentication agent forwarding if appropriate. */
1.70 markus 948: check_agent_present();
949:
1.31 markus 950: if (options.forward_agent) {
951: debug("Requesting authentication agent forwarding.");
952: auth_request_forwarding();
953:
954: /* Read response from the server. */
1.156 markus 955: type = packet_read();
1.155 markus 956: packet_check_eom();
1.31 markus 957: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 958: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 959: }
960:
1.70 markus 961: /* Initiate port forwardings. */
962: ssh_init_forwarding();
1.34 markus 963:
964: /* If requested, let ssh continue in the background. */
1.48 markus 965: if (fork_after_authentication_flag)
1.34 markus 966: if (daemon(1, 1) < 0)
967: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 968:
1.33 markus 969: /*
970: * If a command was specified on the command line, execute the
971: * command now. Otherwise request the server to start a shell.
972: */
1.31 markus 973: if (buffer_len(&command) > 0) {
974: int len = buffer_len(&command);
975: if (len > 900)
976: len = 900;
1.152 stevesk 977: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 978: packet_start(SSH_CMSG_EXEC_CMD);
979: packet_put_string(buffer_ptr(&command), buffer_len(&command));
980: packet_send();
981: packet_write_wait();
982: } else {
983: debug("Requesting shell.");
984: packet_start(SSH_CMSG_EXEC_SHELL);
985: packet_send();
986: packet_write_wait();
987: }
988:
989: /* Enter the interactive session. */
1.119 stevesk 990: return client_loop(have_tty, tty_flag ?
991: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 992: }
993:
1.126 itojun 994: static void
1.214 djm 995: ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89 markus 996: {
997: int id, len;
998:
999: id = packet_get_int();
1000: len = buffer_len(&command);
1.100 markus 1001: if (len > 900)
1002: len = 900;
1.155 markus 1003: packet_check_eom();
1.89 markus 1004: if (type == SSH2_MSG_CHANNEL_FAILURE)
1005: fatal("Request for subsystem '%.*s' failed on channel %d",
1.152 stevesk 1006: len, (u_char *)buffer_ptr(&command), id);
1.170 markus 1007: }
1008:
1009: void
1.206 markus 1010: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 1011: {
1012: int i;
1013:
1014: i = client_global_request_id++;
1.206 markus 1015: if (i >= options.num_remote_forwards)
1.170 markus 1016: return;
1017: debug("remote forward %s for: listen %d, connect %s:%d",
1018: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1019: options.remote_forwards[i].port,
1020: options.remote_forwards[i].host,
1021: options.remote_forwards[i].host_port);
1022: if (type == SSH2_MSG_REQUEST_FAILURE)
1.191 itojun 1023: logit("Warning: remote port forwarding failed for listen port %d",
1.170 markus 1024: options.remote_forwards[i].port);
1.89 markus 1025: }
1026:
1.126 itojun 1027: static void
1.214 djm 1028: ssh_control_listener(void)
1.45 markus 1029: {
1.214 djm 1030: struct sockaddr_un addr;
1031: mode_t old_umask;
1.223 deraadt 1032:
1.216 djm 1033: if (options.control_path == NULL || options.control_master <= 0)
1.214 djm 1034: return;
1035:
1036: memset(&addr, '\0', sizeof(addr));
1037: addr.sun_family = AF_UNIX;
1038: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1039: strlen(options.control_path) + 1;
1040:
1041: if (strlcpy(addr.sun_path, options.control_path,
1042: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1043: fatal("ControlPath too long");
1044:
1045: if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1046: fatal("%s socket(): %s\n", __func__, strerror(errno));
1047:
1048: old_umask = umask(0177);
1049: if (bind(control_fd, (struct sockaddr*)&addr, addr.sun_len) == -1) {
1050: control_fd = -1;
1051: if (errno == EINVAL)
1052: fatal("ControlSocket %s already exists",
1053: options.control_path);
1054: else
1055: fatal("%s bind(): %s\n", __func__, strerror(errno));
1056: }
1057: umask(old_umask);
1.80 markus 1058:
1.214 djm 1059: if (listen(control_fd, 64) == -1)
1060: fatal("%s listen(): %s\n", __func__, strerror(errno));
1.31 markus 1061:
1.214 djm 1062: set_nonblock(control_fd);
1063: }
1.45 markus 1064:
1.214 djm 1065: /* request pty/x11/agent/tcpfwd/shell for channel */
1066: static void
1067: ssh_session2_setup(int id, void *arg)
1068: {
1.215 djm 1069: extern char **environ;
1070:
1.214 djm 1071: int interactive = tty_flag;
1072: if (options.forward_x11 && getenv("DISPLAY") != NULL) {
1.150 stevesk 1073: char *proto, *data;
1.50 markus 1074: /* Get reasonable local authentication information. */
1.150 stevesk 1075: x11_get_proto(&proto, &data);
1.50 markus 1076: /* Request forwarding with authentication spoofing. */
1077: debug("Requesting X11 forwarding with authentication spoofing.");
1078: x11_request_forwarding_with_spoofing(id, proto, data);
1.80 markus 1079: interactive = 1;
1.50 markus 1080: /* XXX wait for reply */
1081: }
1082:
1.70 markus 1083: check_agent_present();
1084: if (options.forward_agent) {
1085: debug("Requesting authentication agent forwarding.");
1086: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1087: packet_send();
1.212 djm 1088: }
1089:
1.214 djm 1090: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.215 djm 1091: NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
1.90 markus 1092:
1.80 markus 1093: packet_set_interactive(interactive);
1.45 markus 1094: }
1095:
1.143 markus 1096: /* open new channel for a session */
1.126 itojun 1097: static int
1.143 markus 1098: ssh_session2_open(void)
1.45 markus 1099: {
1.118 markus 1100: Channel *c;
1101: int window, packetmax, in, out, err;
1.60 markus 1102:
1.62 markus 1103: if (stdin_null_flag) {
1.93 itojun 1104: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1105: } else {
1106: in = dup(STDIN_FILENO);
1107: }
1.60 markus 1108: out = dup(STDOUT_FILENO);
1109: err = dup(STDERR_FILENO);
1.45 markus 1110:
1111: if (in < 0 || out < 0 || err < 0)
1.62 markus 1112: fatal("dup() in/out/err failed");
1.45 markus 1113:
1.69 markus 1114: /* enable nonblocking unless tty */
1115: if (!isatty(in))
1116: set_nonblock(in);
1117: if (!isatty(out))
1118: set_nonblock(out);
1119: if (!isatty(err))
1120: set_nonblock(err);
1121:
1.65 markus 1122: window = CHAN_SES_WINDOW_DEFAULT;
1123: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1124: if (tty_flag) {
1125: window >>= 1;
1126: packetmax >>= 1;
1.45 markus 1127: }
1.118 markus 1128: c = channel_new(
1.45 markus 1129: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1130: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1131: "client-session", /*nonblock*/0);
1.45 markus 1132:
1.143 markus 1133: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1134:
1.122 markus 1135: channel_send_open(c->self);
1.143 markus 1136: if (!no_shell_flag)
1.214 djm 1137: channel_register_confirm(c->self, ssh_session2_setup, NULL);
1.106 markus 1138:
1.118 markus 1139: return c->self;
1.106 markus 1140: }
1141:
1.126 itojun 1142: static int
1.106 markus 1143: ssh_session2(void)
1144: {
1.143 markus 1145: int id = -1;
1.106 markus 1146:
1147: /* XXX should be pre-session */
1148: ssh_init_forwarding();
1.214 djm 1149: ssh_control_listener();
1.106 markus 1150:
1.143 markus 1151: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1152: id = ssh_session2_open();
1.106 markus 1153:
1154: /* If requested, let ssh continue in the background. */
1155: if (fork_after_authentication_flag)
1156: if (daemon(1, 1) < 0)
1157: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1158:
1.119 stevesk 1159: return client_loop(tty_flag, tty_flag ?
1160: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1161: }
1162:
1.126 itojun 1163: static void
1.104 markus 1164: load_public_identity_files(void)
1165: {
1166: char *filename;
1.167 markus 1167: int i = 0;
1.104 markus 1168: Key *public;
1.167 markus 1169: #ifdef SMARTCARD
1170: Key **keys;
1.104 markus 1171:
1.133 markus 1172: if (options.smartcard_device != NULL &&
1.167 markus 1173: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1174: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1175: int count = 0;
1176: for (i = 0; keys[i] != NULL; i++) {
1177: count++;
1178: memmove(&options.identity_files[1], &options.identity_files[0],
1179: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1180: memmove(&options.identity_keys[1], &options.identity_keys[0],
1181: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1182: options.num_identity_files++;
1183: options.identity_keys[0] = keys[i];
1.194 markus 1184: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1185: }
1.168 markus 1186: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1187: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1188: i = count;
1189: xfree(keys);
1.127 markus 1190: }
1.136 jakob 1191: #endif /* SMARTCARD */
1.131 millert 1192: for (; i < options.num_identity_files; i++) {
1193: filename = tilde_expand_filename(options.identity_files[i],
1194: original_real_uid);
1195: public = key_load_public(filename, NULL);
1196: debug("identity file %s type %d", filename,
1197: public ? public->type : -1);
1198: xfree(options.identity_files[i]);
1199: options.identity_files[i] = filename;
1200: options.identity_keys[i] = public;
1201: }
1.214 djm 1202: }
1203:
1204: static void
1205: control_client_sighandler(int signo)
1206: {
1207: control_client_terminate = signo;
1208: }
1209:
1210: static void
1211: control_client_sigrelay(int signo)
1212: {
1213: if (control_server_pid > 1)
1214: kill(control_server_pid, signo);
1215: }
1216:
1.220 djm 1217: static int
1218: env_permitted(char *env)
1219: {
1220: int i;
1221: char name[1024], *cp;
1222:
1223: strlcpy(name, env, sizeof(name));
1224: if ((cp = strchr(name, '=')) == NULL)
1225: return (0);
1226:
1227: *cp = '\0';
1228:
1229: for (i = 0; i < options.num_send_env; i++)
1230: if (match_pattern(name, options.send_env[i]))
1231: return (1);
1232:
1233: return (0);
1234: }
1235:
1.214 djm 1236: static void
1237: control_client(const char *path)
1238: {
1239: struct sockaddr_un addr;
1.220 djm 1240: int i, r, sock, exitval, num_env;
1.214 djm 1241: Buffer m;
1242: char *cp;
1.215 djm 1243: extern char **environ;
1.223 deraadt 1244:
1.214 djm 1245: memset(&addr, '\0', sizeof(addr));
1246: addr.sun_family = AF_UNIX;
1247: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1248: strlen(path) + 1;
1249:
1250: if (strlcpy(addr.sun_path, path,
1251: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1252: fatal("ControlPath too long");
1253:
1254: if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1255: fatal("%s socket(): %s", __func__, strerror(errno));
1256:
1257: if (connect(sock, (struct sockaddr*)&addr, addr.sun_len) == -1)
1258: fatal("Couldn't connect to %s: %s", path, strerror(errno));
1259:
1260: if ((cp = getenv("TERM")) == NULL)
1261: cp = "";
1262:
1263: buffer_init(&m);
1264:
1265: /* Get PID of controlee */
1266: if (ssh_msg_recv(sock, &m) == -1)
1267: fatal("%s: msg_recv", __func__);
1268: if (buffer_get_char(&m) != 0)
1269: fatal("%s: wrong version", __func__);
1.216 djm 1270: /* Connection allowed? */
1271: if (buffer_get_int(&m) != 1)
1272: fatal("Connection to master denied");
1.214 djm 1273: control_server_pid = buffer_get_int(&m);
1274:
1275: buffer_clear(&m);
1276: buffer_put_int(&m, tty_flag);
1277: buffer_put_int(&m, subsystem_flag);
1278: buffer_put_cstring(&m, cp);
1279:
1280: buffer_append(&command, "\0", 1);
1281: buffer_put_cstring(&m, buffer_ptr(&command));
1.215 djm 1282:
1.220 djm 1283: if (options.num_send_env == 0 || environ == NULL) {
1284: buffer_put_int(&m, 0);
1.223 deraadt 1285: } else {
1.220 djm 1286: /* Pass environment */
1287: num_env = 0;
1288: for (i = 0; environ[i] != NULL; i++)
1289: if (env_permitted(environ[i]))
1290: num_env++; /* Count */
1.223 deraadt 1291:
1.220 djm 1292: buffer_put_int(&m, num_env);
1293:
1.222 dtucker 1294: for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1295: if (env_permitted(environ[i])) {
1296: num_env--;
1.220 djm 1297: buffer_put_cstring(&m, environ[i]);
1.222 dtucker 1298: }
1.220 djm 1299: }
1.214 djm 1300:
1301: if (ssh_msg_send(sock, /* version */0, &m) == -1)
1302: fatal("%s: msg_send", __func__);
1303:
1304: mm_send_fd(sock, STDIN_FILENO);
1305: mm_send_fd(sock, STDOUT_FILENO);
1306: mm_send_fd(sock, STDERR_FILENO);
1307:
1308: /* Wait for reply, so master has a chance to gather ttymodes */
1309: buffer_clear(&m);
1310: if (ssh_msg_recv(sock, &m) == -1)
1311: fatal("%s: msg_recv", __func__);
1312: if (buffer_get_char(&m) != 0)
1313: fatal("%s: master returned error", __func__);
1314: buffer_free(&m);
1.218 djm 1315:
1316: signal(SIGINT, control_client_sighandler);
1317: signal(SIGTERM, control_client_sighandler);
1318: signal(SIGWINCH, control_client_sigrelay);
1.214 djm 1319:
1320: if (tty_flag)
1321: enter_raw_mode();
1322:
1323: /* Stick around until the controlee closes the client_fd */
1324: exitval = 0;
1325: for (;!control_client_terminate;) {
1326: r = read(sock, &exitval, sizeof(exitval));
1327: if (r == 0) {
1328: debug2("Received EOF from master");
1329: break;
1330: }
1331: if (r > 0)
1332: debug2("Received exit status from master %d", exitval);
1333: if (r == -1 && errno != EINTR)
1334: fatal("%s: read %s", __func__, strerror(errno));
1335: }
1336:
1337: if (control_client_terminate)
1338: debug2("Exiting on signal %d", control_client_terminate);
1339:
1340: close(sock);
1341:
1342: leave_raw_mode();
1343:
1344: if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1345: fprintf(stderr, "Connection to master closed.\r\n");
1346:
1347: exit(exitval);
1.1 deraadt 1348: }