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