Annotation of src/usr.bin/ssh/ssh.c, Revision 1.253
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.253 ! djm 43: RCSID("$OpenBSD: ssh.c,v 1.252 2005/10/14 02:17:59 stevesk 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) ?
1.253 ! djm 786: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 787: options.remote_forwards[i].listen_port,
788: options.remote_forwards[i].connect_host,
789: options.remote_forwards[i].connect_port);
1.70 markus 790: channel_request_remote_forwarding(
1.232 djm 791: options.remote_forwards[i].listen_host,
792: options.remote_forwards[i].listen_port,
793: options.remote_forwards[i].connect_host,
794: options.remote_forwards[i].connect_port);
1.70 markus 795: }
796: }
797:
1.126 itojun 798: static void
1.70 markus 799: check_agent_present(void)
800: {
801: if (options.forward_agent) {
802: /* Clear agent forwarding if we don\'t have an agent. */
1.185 stevesk 803: if (!ssh_agent_present())
1.70 markus 804: options.forward_agent = 0;
805: }
806: }
807:
1.126 itojun 808: static int
1.45 markus 809: ssh_session(void)
810: {
811: int type;
812: int interactive = 0;
813: int have_tty = 0;
814: struct winsize ws;
815: char *cp;
1.243 djm 816: const char *display;
1.45 markus 817:
1.31 markus 818: /* Enable compression if requested. */
819: if (options.compression) {
820: debug("Requesting compression at level %d.", options.compression_level);
821:
822: if (options.compression_level < 1 || options.compression_level > 9)
823: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
824:
825: /* Send the request. */
826: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
827: packet_put_int(options.compression_level);
828: packet_send();
829: packet_write_wait();
1.156 markus 830: type = packet_read();
1.31 markus 831: if (type == SSH_SMSG_SUCCESS)
832: packet_start_compression(options.compression_level);
833: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 834: logit("Warning: Remote host refused compression.");
1.31 markus 835: else
836: packet_disconnect("Protocol error waiting for compression response.");
837: }
838: /* Allocate a pseudo tty if appropriate. */
839: if (tty_flag) {
840: debug("Requesting pty.");
841:
842: /* Start the packet. */
843: packet_start(SSH_CMSG_REQUEST_PTY);
844:
845: /* Store TERM in the packet. There is no limit on the
846: length of the string. */
847: cp = getenv("TERM");
848: if (!cp)
849: cp = "";
1.124 markus 850: packet_put_cstring(cp);
1.31 markus 851:
852: /* Store window size in the packet. */
853: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
854: memset(&ws, 0, sizeof(ws));
855: packet_put_int(ws.ws_row);
856: packet_put_int(ws.ws_col);
857: packet_put_int(ws.ws_xpixel);
858: packet_put_int(ws.ws_ypixel);
859:
860: /* Store tty modes in the packet. */
1.115 stevesk 861: tty_make_modes(fileno(stdin), NULL);
1.31 markus 862:
863: /* Send the packet, and wait for it to leave. */
864: packet_send();
865: packet_write_wait();
866:
867: /* Read response from the server. */
1.156 markus 868: type = packet_read();
1.43 markus 869: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 870: interactive = 1;
1.45 markus 871: have_tty = 1;
1.43 markus 872: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 873: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 874: else
875: packet_disconnect("Protocol error waiting for pty request response.");
876: }
877: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 878: display = getenv("DISPLAY");
879: if (options.forward_x11 && display != NULL) {
1.150 stevesk 880: char *proto, *data;
1.50 markus 881: /* Get reasonable local authentication information. */
1.243 djm 882: client_x11_get_proto(display, options.xauth_location,
883: options.forward_x11_trusted, &proto, &data);
1.50 markus 884: /* Request forwarding with authentication spoofing. */
1.31 markus 885: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 886: x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31 markus 887:
888: /* Read response from the server. */
1.156 markus 889: type = packet_read();
1.31 markus 890: if (type == SSH_SMSG_SUCCESS) {
891: interactive = 1;
1.50 markus 892: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 893: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 894: } else {
1.31 markus 895: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 896: }
1.31 markus 897: }
898: /* Tell the packet module whether this is an interactive session. */
1.80 markus 899: packet_set_interactive(interactive);
1.31 markus 900:
901: /* Request authentication agent forwarding if appropriate. */
1.70 markus 902: check_agent_present();
903:
1.31 markus 904: if (options.forward_agent) {
905: debug("Requesting authentication agent forwarding.");
906: auth_request_forwarding();
907:
908: /* Read response from the server. */
1.156 markus 909: type = packet_read();
1.155 markus 910: packet_check_eom();
1.31 markus 911: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 912: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 913: }
914:
1.70 markus 915: /* Initiate port forwardings. */
916: ssh_init_forwarding();
1.34 markus 917:
918: /* If requested, let ssh continue in the background. */
1.48 markus 919: if (fork_after_authentication_flag)
1.34 markus 920: if (daemon(1, 1) < 0)
921: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 922:
1.33 markus 923: /*
924: * If a command was specified on the command line, execute the
925: * command now. Otherwise request the server to start a shell.
926: */
1.31 markus 927: if (buffer_len(&command) > 0) {
928: int len = buffer_len(&command);
929: if (len > 900)
930: len = 900;
1.152 stevesk 931: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 932: packet_start(SSH_CMSG_EXEC_CMD);
933: packet_put_string(buffer_ptr(&command), buffer_len(&command));
934: packet_send();
935: packet_write_wait();
936: } else {
937: debug("Requesting shell.");
938: packet_start(SSH_CMSG_EXEC_SHELL);
939: packet_send();
940: packet_write_wait();
941: }
942:
943: /* Enter the interactive session. */
1.119 stevesk 944: return client_loop(have_tty, tty_flag ?
945: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 946: }
947:
1.126 itojun 948: static void
1.214 djm 949: ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89 markus 950: {
951: int id, len;
952:
953: id = packet_get_int();
954: len = buffer_len(&command);
1.100 markus 955: if (len > 900)
956: len = 900;
1.155 markus 957: packet_check_eom();
1.89 markus 958: if (type == SSH2_MSG_CHANNEL_FAILURE)
959: fatal("Request for subsystem '%.*s' failed on channel %d",
1.152 stevesk 960: len, (u_char *)buffer_ptr(&command), id);
1.170 markus 961: }
962:
963: void
1.206 markus 964: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 965: {
966: int i;
967:
968: i = client_global_request_id++;
1.206 markus 969: if (i >= options.num_remote_forwards)
1.170 markus 970: return;
971: debug("remote forward %s for: listen %d, connect %s:%d",
972: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.232 djm 973: options.remote_forwards[i].listen_port,
974: options.remote_forwards[i].connect_host,
975: options.remote_forwards[i].connect_port);
1.170 markus 976: if (type == SSH2_MSG_REQUEST_FAILURE)
1.232 djm 977: logit("Warning: remote port forwarding failed for listen "
978: "port %d", options.remote_forwards[i].listen_port);
1.89 markus 979: }
980:
1.126 itojun 981: static void
1.214 djm 982: ssh_control_listener(void)
1.45 markus 983: {
1.214 djm 984: struct sockaddr_un addr;
985: mode_t old_umask;
1.223 deraadt 986:
1.242 djm 987: if (options.control_path == NULL ||
988: options.control_master == SSHCTL_MASTER_NO)
1.214 djm 989: return;
990:
1.242 djm 991: debug("setting up multiplex master socket");
992:
1.214 djm 993: memset(&addr, '\0', sizeof(addr));
994: addr.sun_family = AF_UNIX;
995: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
996: strlen(options.control_path) + 1;
997:
998: if (strlcpy(addr.sun_path, options.control_path,
999: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1000: fatal("ControlPath too long");
1001:
1002: if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1.252 stevesk 1003: fatal("%s socket(): %s", __func__, strerror(errno));
1.214 djm 1004:
1005: old_umask = umask(0177);
1006: if (bind(control_fd, (struct sockaddr*)&addr, addr.sun_len) == -1) {
1007: control_fd = -1;
1.238 djm 1008: if (errno == EINVAL || errno == EADDRINUSE)
1.214 djm 1009: fatal("ControlSocket %s already exists",
1010: options.control_path);
1011: else
1.252 stevesk 1012: fatal("%s bind(): %s", __func__, strerror(errno));
1.214 djm 1013: }
1014: umask(old_umask);
1.80 markus 1015:
1.214 djm 1016: if (listen(control_fd, 64) == -1)
1.252 stevesk 1017: fatal("%s listen(): %s", __func__, strerror(errno));
1.31 markus 1018:
1.214 djm 1019: set_nonblock(control_fd);
1020: }
1.45 markus 1021:
1.214 djm 1022: /* request pty/x11/agent/tcpfwd/shell for channel */
1023: static void
1024: ssh_session2_setup(int id, void *arg)
1025: {
1.215 djm 1026: extern char **environ;
1.243 djm 1027: const char *display;
1028: int interactive = tty_flag;
1.215 djm 1029:
1.248 djm 1030: display = getenv("DISPLAY");
1.243 djm 1031: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1032: char *proto, *data;
1.50 markus 1033: /* Get reasonable local authentication information. */
1.243 djm 1034: client_x11_get_proto(display, options.xauth_location,
1035: options.forward_x11_trusted, &proto, &data);
1.50 markus 1036: /* Request forwarding with authentication spoofing. */
1037: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 1038: x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80 markus 1039: interactive = 1;
1.50 markus 1040: /* XXX wait for reply */
1041: }
1042:
1.70 markus 1043: check_agent_present();
1044: if (options.forward_agent) {
1045: debug("Requesting authentication agent forwarding.");
1046: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1047: packet_send();
1.212 djm 1048: }
1049:
1.214 djm 1050: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.215 djm 1051: NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
1.90 markus 1052:
1.80 markus 1053: packet_set_interactive(interactive);
1.45 markus 1054: }
1055:
1.143 markus 1056: /* open new channel for a session */
1.126 itojun 1057: static int
1.143 markus 1058: ssh_session2_open(void)
1.45 markus 1059: {
1.118 markus 1060: Channel *c;
1061: int window, packetmax, in, out, err;
1.60 markus 1062:
1.62 markus 1063: if (stdin_null_flag) {
1.93 itojun 1064: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1065: } else {
1066: in = dup(STDIN_FILENO);
1067: }
1.60 markus 1068: out = dup(STDOUT_FILENO);
1069: err = dup(STDERR_FILENO);
1.45 markus 1070:
1071: if (in < 0 || out < 0 || err < 0)
1.62 markus 1072: fatal("dup() in/out/err failed");
1.45 markus 1073:
1.69 markus 1074: /* enable nonblocking unless tty */
1075: if (!isatty(in))
1076: set_nonblock(in);
1077: if (!isatty(out))
1078: set_nonblock(out);
1079: if (!isatty(err))
1080: set_nonblock(err);
1081:
1.65 markus 1082: window = CHAN_SES_WINDOW_DEFAULT;
1083: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1084: if (tty_flag) {
1085: window >>= 1;
1086: packetmax >>= 1;
1.45 markus 1087: }
1.118 markus 1088: c = channel_new(
1.45 markus 1089: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1090: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1091: "client-session", /*nonblock*/0);
1.45 markus 1092:
1.143 markus 1093: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1094:
1.122 markus 1095: channel_send_open(c->self);
1.143 markus 1096: if (!no_shell_flag)
1.214 djm 1097: channel_register_confirm(c->self, ssh_session2_setup, NULL);
1.106 markus 1098:
1.118 markus 1099: return c->self;
1.106 markus 1100: }
1101:
1.126 itojun 1102: static int
1.106 markus 1103: ssh_session2(void)
1104: {
1.143 markus 1105: int id = -1;
1.106 markus 1106:
1107: /* XXX should be pre-session */
1108: ssh_init_forwarding();
1.214 djm 1109: ssh_control_listener();
1.106 markus 1110:
1.143 markus 1111: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1112: id = ssh_session2_open();
1.106 markus 1113:
1114: /* If requested, let ssh continue in the background. */
1115: if (fork_after_authentication_flag)
1116: if (daemon(1, 1) < 0)
1117: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1118:
1.119 stevesk 1119: return client_loop(tty_flag, tty_flag ?
1120: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1121: }
1122:
1.126 itojun 1123: static void
1.104 markus 1124: load_public_identity_files(void)
1125: {
1126: char *filename;
1.167 markus 1127: int i = 0;
1.104 markus 1128: Key *public;
1.167 markus 1129: #ifdef SMARTCARD
1130: Key **keys;
1.104 markus 1131:
1.133 markus 1132: if (options.smartcard_device != NULL &&
1.167 markus 1133: options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
1134: (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {
1135: int count = 0;
1136: for (i = 0; keys[i] != NULL; i++) {
1137: count++;
1138: memmove(&options.identity_files[1], &options.identity_files[0],
1139: sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
1140: memmove(&options.identity_keys[1], &options.identity_keys[0],
1141: sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
1142: options.num_identity_files++;
1143: options.identity_keys[0] = keys[i];
1.194 markus 1144: options.identity_files[0] = sc_get_key_label(keys[i]);
1.167 markus 1145: }
1.168 markus 1146: if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
1147: options.num_identity_files = SSH_MAX_IDENTITY_FILES;
1.167 markus 1148: i = count;
1149: xfree(keys);
1.127 markus 1150: }
1.136 jakob 1151: #endif /* SMARTCARD */
1.131 millert 1152: for (; i < options.num_identity_files; i++) {
1153: filename = tilde_expand_filename(options.identity_files[i],
1154: original_real_uid);
1155: public = key_load_public(filename, NULL);
1156: debug("identity file %s type %d", filename,
1157: public ? public->type : -1);
1158: xfree(options.identity_files[i]);
1159: options.identity_files[i] = filename;
1160: options.identity_keys[i] = public;
1161: }
1.214 djm 1162: }
1163:
1164: static void
1165: control_client_sighandler(int signo)
1166: {
1167: control_client_terminate = signo;
1168: }
1169:
1170: static void
1171: control_client_sigrelay(int signo)
1172: {
1173: if (control_server_pid > 1)
1174: kill(control_server_pid, signo);
1175: }
1176:
1.220 djm 1177: static int
1178: env_permitted(char *env)
1179: {
1180: int i;
1181: char name[1024], *cp;
1182:
1183: strlcpy(name, env, sizeof(name));
1184: if ((cp = strchr(name, '=')) == NULL)
1185: return (0);
1186:
1187: *cp = '\0';
1188:
1189: for (i = 0; i < options.num_send_env; i++)
1190: if (match_pattern(name, options.send_env[i]))
1191: return (1);
1192:
1193: return (0);
1194: }
1195:
1.214 djm 1196: static void
1197: control_client(const char *path)
1198: {
1199: struct sockaddr_un addr;
1.228 djm 1200: int i, r, fd, sock, exitval, num_env;
1.214 djm 1201: Buffer m;
1.229 djm 1202: char *term;
1.215 djm 1203: extern char **environ;
1.229 djm 1204: u_int flags;
1.242 djm 1205:
1206: if (mux_command == 0)
1207: mux_command = SSHMUX_COMMAND_OPEN;
1208:
1209: switch (options.control_master) {
1210: case SSHCTL_MASTER_AUTO:
1211: case SSHCTL_MASTER_AUTO_ASK:
1212: debug("auto-mux: Trying existing master");
1213: /* FALLTHROUGH */
1214: case SSHCTL_MASTER_NO:
1215: break;
1216: default:
1217: return;
1218: }
1.228 djm 1219:
1.214 djm 1220: memset(&addr, '\0', sizeof(addr));
1221: addr.sun_family = AF_UNIX;
1222: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1223: strlen(path) + 1;
1224:
1225: if (strlcpy(addr.sun_path, path,
1226: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1227: fatal("ControlPath too long");
1228:
1229: if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1230: fatal("%s socket(): %s", __func__, strerror(errno));
1231:
1.237 jakob 1232: if (connect(sock, (struct sockaddr*)&addr, addr.sun_len) == -1) {
1.240 djm 1233: if (mux_command != SSHMUX_COMMAND_OPEN) {
1234: fatal("Control socket connect(%.100s): %s", path,
1235: strerror(errno));
1236: }
1.239 djm 1237: if (errno == ENOENT)
1238: debug("Control socket \"%.100s\" does not exist", path);
1239: else {
1240: error("Control socket connect(%.100s): %s", path,
1241: strerror(errno));
1242: }
1.237 jakob 1243: close(sock);
1244: return;
1245: }
1.248 djm 1246:
1.237 jakob 1247: if (stdin_null_flag) {
1248: if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1249: fatal("open(/dev/null): %s", strerror(errno));
1250: if (dup2(fd, STDIN_FILENO) == -1)
1251: fatal("dup2: %s", strerror(errno));
1252: if (fd > STDERR_FILENO)
1253: close(fd);
1254: }
1.248 djm 1255:
1.247 djm 1256: term = getenv("TERM");
1.229 djm 1257:
1258: flags = 0;
1259: if (tty_flag)
1260: flags |= SSHMUX_FLAG_TTY;
1261: if (subsystem_flag)
1262: flags |= SSHMUX_FLAG_SUBSYS;
1.247 djm 1263: if (options.forward_x11)
1264: flags |= SSHMUX_FLAG_X11_FWD;
1265: if (options.forward_agent)
1266: flags |= SSHMUX_FLAG_AGENT_FWD;
1.214 djm 1267:
1268: buffer_init(&m);
1269:
1.229 djm 1270: /* Send our command to server */
1271: buffer_put_int(&m, mux_command);
1272: buffer_put_int(&m, flags);
1.247 djm 1273: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.229 djm 1274: fatal("%s: msg_send", __func__);
1275: buffer_clear(&m);
1276:
1277: /* Get authorisation status and PID of controlee */
1.214 djm 1278: if (ssh_msg_recv(sock, &m) == -1)
1279: fatal("%s: msg_recv", __func__);
1.247 djm 1280: if (buffer_get_char(&m) != SSHMUX_VER)
1.214 djm 1281: fatal("%s: wrong version", __func__);
1.216 djm 1282: if (buffer_get_int(&m) != 1)
1283: fatal("Connection to master denied");
1.214 djm 1284: control_server_pid = buffer_get_int(&m);
1285:
1286: buffer_clear(&m);
1287:
1.229 djm 1288: switch (mux_command) {
1289: case SSHMUX_COMMAND_ALIVE_CHECK:
1.234 deraadt 1290: fprintf(stderr, "Master running (pid=%d)\r\n",
1.229 djm 1291: control_server_pid);
1292: exit(0);
1293: case SSHMUX_COMMAND_TERMINATE:
1294: fprintf(stderr, "Exit request sent.\r\n");
1295: exit(0);
1296: case SSHMUX_COMMAND_OPEN:
1297: /* continue below */
1298: break;
1299: default:
1300: fatal("silly mux_command %d", mux_command);
1301: }
1302:
1303: /* SSHMUX_COMMAND_OPEN */
1.247 djm 1304: buffer_put_cstring(&m, term ? term : "");
1.214 djm 1305: buffer_append(&command, "\0", 1);
1306: buffer_put_cstring(&m, buffer_ptr(&command));
1.215 djm 1307:
1.220 djm 1308: if (options.num_send_env == 0 || environ == NULL) {
1309: buffer_put_int(&m, 0);
1.223 deraadt 1310: } else {
1.220 djm 1311: /* Pass environment */
1312: num_env = 0;
1313: for (i = 0; environ[i] != NULL; i++)
1314: if (env_permitted(environ[i]))
1315: num_env++; /* Count */
1.223 deraadt 1316:
1.220 djm 1317: buffer_put_int(&m, num_env);
1318:
1.222 dtucker 1319: for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1320: if (env_permitted(environ[i])) {
1321: num_env--;
1.220 djm 1322: buffer_put_cstring(&m, environ[i]);
1.222 dtucker 1323: }
1.220 djm 1324: }
1.214 djm 1325:
1.247 djm 1326: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.214 djm 1327: fatal("%s: msg_send", __func__);
1328:
1329: mm_send_fd(sock, STDIN_FILENO);
1330: mm_send_fd(sock, STDOUT_FILENO);
1331: mm_send_fd(sock, STDERR_FILENO);
1332:
1333: /* Wait for reply, so master has a chance to gather ttymodes */
1334: buffer_clear(&m);
1335: if (ssh_msg_recv(sock, &m) == -1)
1336: fatal("%s: msg_recv", __func__);
1.247 djm 1337: if (buffer_get_char(&m) != SSHMUX_VER)
1.229 djm 1338: fatal("%s: wrong version", __func__);
1.214 djm 1339: buffer_free(&m);
1.218 djm 1340:
1.226 djm 1341: signal(SIGHUP, control_client_sighandler);
1.218 djm 1342: signal(SIGINT, control_client_sighandler);
1343: signal(SIGTERM, control_client_sighandler);
1344: signal(SIGWINCH, control_client_sigrelay);
1.214 djm 1345:
1346: if (tty_flag)
1347: enter_raw_mode();
1348:
1349: /* Stick around until the controlee closes the client_fd */
1350: exitval = 0;
1351: for (;!control_client_terminate;) {
1352: r = read(sock, &exitval, sizeof(exitval));
1353: if (r == 0) {
1354: debug2("Received EOF from master");
1355: break;
1356: }
1357: if (r > 0)
1358: debug2("Received exit status from master %d", exitval);
1359: if (r == -1 && errno != EINTR)
1360: fatal("%s: read %s", __func__, strerror(errno));
1361: }
1362:
1363: if (control_client_terminate)
1364: debug2("Exiting on signal %d", control_client_terminate);
1365:
1366: close(sock);
1367:
1368: leave_raw_mode();
1369:
1370: if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1371: fprintf(stderr, "Connection to master closed.\r\n");
1372:
1373: exit(exitval);
1.1 deraadt 1374: }