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