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