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