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