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