Annotation of src/usr.bin/ssh/ssh.c, Revision 1.275
1.275 ! djm 1: /* $OpenBSD: ssh.c,v 1.274 2006/03/28 00:12:31 deraadt Exp $ */
1.1 deraadt 2: /*
1.32 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Ssh client program. This program can be used to log into a remote machine.
7: * The software supports strong authentication, encryption, and forwarding
8: * of X11, TCP/IP, and authentication connections.
9: *
1.64 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
15: *
16: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.202 markus 17: * Copyright (c) 2000, 2001, 2002, 2003 Markus Friedl. All rights reserved.
1.64 deraadt 18: *
19: * Modified to work with SSL by Niels Provos <provos@citi.umich.edu>
20: * in Canada (German citizen).
21: *
22: * Redistribution and use in source and binary forms, with or without
23: * modification, are permitted provided that the following conditions
24: * are met:
25: * 1. Redistributions of source code must retain the above copyright
26: * notice, this list of conditions and the following disclaimer.
27: * 2. Redistributions in binary form must reproduce the above copyright
28: * notice, this list of conditions and the following disclaimer in the
29: * documentation and/or other materials provided with the distribution.
30: *
31: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32 deraadt 41: */
1.1 deraadt 42:
43: #include "includes.h"
1.259 stevesk 44:
45: #include <sys/resource.h>
1.260 stevesk 46: #include <sys/ioctl.h>
1.262 stevesk 47: #include <sys/types.h>
48: #include <sys/un.h>
1.264 stevesk 49: #include <sys/stat.h>
1.258 stevesk 50:
1.265 stevesk 51: #include <ctype.h>
1.258 stevesk 52: #include <paths.h>
1.263 stevesk 53: #include <signal.h>
1.49 markus 54:
55: #include <openssl/evp.h>
1.72 markus 56: #include <openssl/err.h>
1.1 deraadt 57:
1.84 markus 58: #include "ssh.h"
59: #include "ssh1.h"
60: #include "ssh2.h"
61: #include "compat.h"
62: #include "cipher.h"
1.1 deraadt 63: #include "xmalloc.h"
64: #include "packet.h"
65: #include "buffer.h"
1.214 djm 66: #include "bufaux.h"
1.123 markus 67: #include "channels.h"
1.49 markus 68: #include "key.h"
1.58 markus 69: #include "authfd.h"
1.49 markus 70: #include "authfile.h"
1.83 markus 71: #include "pathnames.h"
1.214 djm 72: #include "dispatch.h"
1.81 markus 73: #include "clientloop.h"
1.84 markus 74: #include "log.h"
75: #include "readconf.h"
76: #include "sshconnect.h"
77: #include "misc.h"
1.95 markus 78: #include "kex.h"
79: #include "mac.h"
1.213 deraadt 80: #include "sshpty.h"
1.212 djm 81: #include "match.h"
1.214 djm 82: #include "msg.h"
83: #include "monitor_fdpass.h"
1.225 dtucker 84: #include "uidswap.h"
1.49 markus 85:
1.127 markus 86: #ifdef SMARTCARD
87: #include "scard.h"
1.137 jakob 88: #endif
1.127 markus 89:
1.49 markus 90: extern char *__progname;
1.1 deraadt 91:
1.31 markus 92: /* Flag indicating whether debug mode is on. This can be set on the command line. */
1.1 deraadt 93: int debug_flag = 0;
94:
1.46 markus 95: /* Flag indicating whether a tty should be allocated */
1.1 deraadt 96: int tty_flag = 0;
1.79 markus 97: int no_tty_flag = 0;
98: int force_tty_flag = 0;
1.1 deraadt 99:
1.45 markus 100: /* don't exec a shell */
101: int no_shell_flag = 0;
102:
1.33 markus 103: /*
104: * Flag indicating that nothing should be read from stdin. This can be set
105: * on the command line.
106: */
1.1 deraadt 107: int stdin_null_flag = 0;
108:
1.33 markus 109: /*
110: * Flag indicating that ssh should fork after authentication. This is useful
1.172 deraadt 111: * so that the passphrase can be entered manually, and then ssh goes to the
1.33 markus 112: * background.
113: */
1.1 deraadt 114: int fork_after_authentication_flag = 0;
115:
1.33 markus 116: /*
117: * General data structure for command line options and options configurable
118: * in configuration files. See readconf.h.
119: */
1.1 deraadt 120: Options options;
121:
1.139 markus 122: /* optional user configfile */
123: char *config = NULL;
124:
1.33 markus 125: /*
126: * Name of the host we are connecting to. This is the name given on the
127: * command line, or the HostName specified for the user-supplied name in a
128: * configuration file.
129: */
1.1 deraadt 130: char *host;
131:
1.22 provos 132: /* socket address the host resolves to */
1.37 markus 133: struct sockaddr_storage hostaddr;
1.1 deraadt 134:
1.112 markus 135: /* Private host keys. */
1.173 markus 136: Sensitive sensitive_data;
1.1 deraadt 137:
1.10 dugsong 138: /* Original real UID. */
139: uid_t original_real_uid;
1.177 markus 140: uid_t original_effective_uid;
1.1 deraadt 141:
1.45 markus 142: /* command to be executed */
143: Buffer command;
144:
1.85 djm 145: /* Should we execute a command or invoke a subsystem? */
146: int subsystem_flag = 0;
147:
1.170 markus 148: /* # of replies received for global requests */
149: static int client_global_request_id = 0;
150:
1.186 djm 151: /* pid of proxycommand child process */
152: pid_t proxy_command_pid = 0;
153:
1.214 djm 154: /* fd to control socket */
155: int control_fd = -1;
156:
1.229 djm 157: /* Multiplexing control command */
1.240 djm 158: static u_int mux_command = 0;
1.229 djm 159:
1.214 djm 160: /* Only used in control client mode */
161: volatile sig_atomic_t control_client_terminate = 0;
162: u_int control_server_pid = 0;
163:
1.1 deraadt 164: /* Prints a help message to the user. This function never returns. */
165:
1.126 itojun 166: static void
1.93 itojun 167: usage(void)
1.1 deraadt 168: {
1.208 markus 169: fprintf(stderr,
1.230 jmc 170: "usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
1.251 jmc 171: " [-D [bind_address:]port] [-e escape_char] [-F configfile]\n"
1.233 jmc 172: " [-i identity_file] [-L [bind_address:]port:host:hostport]\n"
1.232 djm 173: " [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]\n"
1.233 jmc 174: " [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
1.255 reyk 175: " [-w tunnel:tunnel] [user@]hostname [command]\n"
1.208 markus 176: );
1.257 dtucker 177: exit(255);
1.1 deraadt 178: }
179:
1.126 itojun 180: static int ssh_session(void);
181: static int ssh_session2(void);
182: static void load_public_identity_files(void);
1.214 djm 183: static void control_client(const char *path);
1.45 markus 184:
1.32 deraadt 185: /*
186: * Main program for the ssh client.
187: */
1.2 provos 188: int
189: main(int ac, char **av)
1.1 deraadt 190: {
1.178 markus 191: int i, opt, exit_status;
1.205 markus 192: char *p, *cp, *line, buf[256];
1.31 markus 193: struct stat st;
1.98 markus 194: struct passwd *pw;
1.45 markus 195: int dummy;
1.144 stevesk 196: extern int optind, optreset;
197: extern char *optarg;
1.244 djm 198: struct servent *sp;
1.232 djm 199: Forward fwd;
1.250 djm 200:
201: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
202: sanitise_stdfd();
1.31 markus 203:
1.33 markus 204: /*
205: * Save the original real uid. It will be needed later (uid-swapping
206: * may clobber the real uid).
207: */
1.31 markus 208: original_real_uid = getuid();
209: original_effective_uid = geteuid();
210:
1.184 stevesk 211: /*
212: * Use uid-swapping to give up root privileges for the duration of
213: * option processing. We will re-instantiate the rights when we are
214: * ready to create the privileged port, and will permanently drop
215: * them when the port has been created (actually, when the connection
216: * has been made, as we may need to create the port several times).
217: */
218: PRIV_END;
219:
1.31 markus 220: /* If we are installed setuid root be careful to not drop core. */
221: if (original_real_uid != original_effective_uid) {
222: struct rlimit rlim;
223: rlim.rlim_cur = rlim.rlim_max = 0;
224: if (setrlimit(RLIMIT_CORE, &rlim) < 0)
225: fatal("setrlimit failed: %.100s", strerror(errno));
1.1 deraadt 226: }
1.107 markus 227: /* Get user data. */
228: pw = getpwuid(original_real_uid);
229: if (!pw) {
1.191 itojun 230: logit("You don't exist, go away!");
1.257 dtucker 231: exit(255);
1.107 markus 232: }
233: /* Take a copy of the returned structure. */
234: pw = pwcopy(pw);
1.31 markus 235:
1.33 markus 236: /*
237: * Set our umask to something reasonable, as some files are created
238: * with the default umask. This will make them world-readable but
239: * writable only by the owner, which is ok for all files for which we
240: * don't set the modes explicitly.
241: */
1.31 markus 242: umask(022);
243:
244: /* Initialize option structure to indicate that no values have been set. */
245: initialize_options(&options);
246:
247: /* Parse command-line arguments. */
248: host = NULL;
249:
1.266 djm 250: again:
1.128 fgsch 251: while ((opt = getopt(ac, av,
1.255 reyk 252: "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNO:PR:S:TVw:XY")) != -1) {
1.31 markus 253: switch (opt) {
1.91 jakob 254: case '1':
255: options.protocol = SSH_PROTO_1;
256: break;
1.47 markus 257: case '2':
258: options.protocol = SSH_PROTO_2;
259: break;
1.37 markus 260: case '4':
1.196 djm 261: options.address_family = AF_INET;
1.37 markus 262: break;
263: case '6':
1.196 djm 264: options.address_family = AF_INET6;
1.37 markus 265: break;
1.31 markus 266: case 'n':
267: stdin_null_flag = 1;
268: break;
269: case 'f':
270: fork_after_authentication_flag = 1;
271: stdin_null_flag = 1;
272: break;
273: case 'x':
274: options.forward_x11 = 0;
275: break;
276: case 'X':
277: options.forward_x11 = 1;
278: break;
1.202 markus 279: case 'Y':
280: options.forward_x11 = 1;
281: options.forward_x11_trusted = 1;
282: break;
1.31 markus 283: case 'g':
284: options.gateway_ports = 1;
285: break;
1.229 djm 286: case 'O':
287: if (strcmp(optarg, "check") == 0)
288: mux_command = SSHMUX_COMMAND_ALIVE_CHECK;
289: else if (strcmp(optarg, "exit") == 0)
290: mux_command = SSHMUX_COMMAND_TERMINATE;
291: else
292: fatal("Invalid multiplex command.");
293: break;
1.183 stevesk 294: case 'P': /* deprecated */
1.31 markus 295: options.use_privileged_port = 0;
296: break;
297: case 'a':
298: options.forward_agent = 0;
1.53 markus 299: break;
300: case 'A':
301: options.forward_agent = 1;
1.31 markus 302: break;
303: case 'k':
1.204 dtucker 304: options.gss_deleg_creds = 0;
1.31 markus 305: break;
306: case 'i':
307: if (stat(optarg, &st) < 0) {
1.128 fgsch 308: fprintf(stderr, "Warning: Identity file %s "
1.231 otto 309: "not accessible: %s.\n", optarg,
310: strerror(errno));
1.31 markus 311: break;
312: }
1.128 fgsch 313: if (options.num_identity_files >=
314: SSH_MAX_IDENTITY_FILES)
315: fatal("Too many identity files specified "
316: "(max %d)", SSH_MAX_IDENTITY_FILES);
317: options.identity_files[options.num_identity_files++] =
318: xstrdup(optarg);
1.31 markus 319: break;
1.127 markus 320: case 'I':
321: #ifdef SMARTCARD
1.133 markus 322: options.smartcard_device = xstrdup(optarg);
1.137 jakob 323: #else
1.127 markus 324: fprintf(stderr, "no support for smartcards.\n");
1.137 jakob 325: #endif
1.127 markus 326: break;
1.31 markus 327: case 't':
1.79 markus 328: if (tty_flag)
329: force_tty_flag = 1;
1.31 markus 330: tty_flag = 1;
331: break;
332: case 'v':
1.197 markus 333: if (debug_flag == 0) {
1.66 markus 334: debug_flag = 1;
335: options.log_level = SYSLOG_LEVEL_DEBUG1;
1.197 markus 336: } else {
337: if (options.log_level < SYSLOG_LEVEL_DEBUG3)
338: options.log_level++;
1.66 markus 339: break;
1.197 markus 340: }
1.227 deraadt 341: /* FALLTHROUGH */
1.31 markus 342: case 'V':
1.209 markus 343: fprintf(stderr, "%s, %s\n",
344: SSH_VERSION, SSLeay_version(SSLEAY_VERSION));
1.31 markus 345: if (opt == 'V')
346: exit(0);
347: break;
1.255 reyk 348: case 'w':
1.256 reyk 349: if (options.tun_open == -1)
350: options.tun_open = SSH_TUNMODE_DEFAULT;
1.255 reyk 351: options.tun_local = a2tun(optarg, &options.tun_remote);
1.256 reyk 352: if (options.tun_local == SSH_TUNID_ERR) {
1.255 reyk 353: fprintf(stderr, "Bad tun device '%s'\n", optarg);
1.257 dtucker 354: exit(255);
1.255 reyk 355: }
356: break;
1.31 markus 357: case 'q':
358: options.log_level = SYSLOG_LEVEL_QUIET;
359: break;
360: case 'e':
361: if (optarg[0] == '^' && optarg[2] == 0 &&
1.128 fgsch 362: (u_char) optarg[1] >= 64 &&
363: (u_char) optarg[1] < 128)
1.78 markus 364: options.escape_char = (u_char) optarg[1] & 31;
1.31 markus 365: else if (strlen(optarg) == 1)
1.78 markus 366: options.escape_char = (u_char) optarg[0];
1.31 markus 367: else if (strcmp(optarg, "none") == 0)
1.119 stevesk 368: options.escape_char = SSH_ESCAPECHAR_NONE;
1.31 markus 369: else {
1.128 fgsch 370: fprintf(stderr, "Bad escape character '%s'.\n",
371: optarg);
1.257 dtucker 372: exit(255);
1.31 markus 373: }
374: break;
375: case 'c':
1.49 markus 376: if (ciphers_valid(optarg)) {
377: /* SSH2 only */
378: options.ciphers = xstrdup(optarg);
1.224 markus 379: options.cipher = SSH_CIPHER_INVALID;
1.49 markus 380: } else {
381: /* SSH1 only */
1.74 markus 382: options.cipher = cipher_number(optarg);
383: if (options.cipher == -1) {
1.128 fgsch 384: fprintf(stderr,
385: "Unknown cipher type '%s'\n",
386: optarg);
1.257 dtucker 387: exit(255);
1.49 markus 388: }
1.128 fgsch 389: if (options.cipher == SSH_CIPHER_3DES)
1.74 markus 390: options.ciphers = "3des-cbc";
1.128 fgsch 391: else if (options.cipher == SSH_CIPHER_BLOWFISH)
1.74 markus 392: options.ciphers = "blowfish-cbc";
1.128 fgsch 393: else
1.74 markus 394: options.ciphers = (char *)-1;
1.95 markus 395: }
396: break;
397: case 'm':
398: if (mac_valid(optarg))
399: options.macs = xstrdup(optarg);
400: else {
1.128 fgsch 401: fprintf(stderr, "Unknown mac type '%s'\n",
402: optarg);
1.257 dtucker 403: exit(255);
1.31 markus 404: }
405: break;
1.214 djm 406: case 'M':
1.242 djm 407: if (options.control_master == SSHCTL_MASTER_YES)
408: options.control_master = SSHCTL_MASTER_ASK;
409: else
410: options.control_master = SSHCTL_MASTER_YES;
1.214 djm 411: break;
1.31 markus 412: case 'p':
1.113 stevesk 413: options.port = a2port(optarg);
414: if (options.port == 0) {
1.109 markus 415: fprintf(stderr, "Bad port '%s'\n", optarg);
1.257 dtucker 416: exit(255);
1.109 markus 417: }
1.31 markus 418: break;
419: case 'l':
420: options.user = optarg;
421: break;
1.141 stevesk 422:
423: case 'L':
1.232 djm 424: if (parse_forward(&fwd, optarg))
425: add_local_forward(&options, &fwd);
426: else {
1.128 fgsch 427: fprintf(stderr,
1.232 djm 428: "Bad local forwarding specification '%s'\n",
1.128 fgsch 429: optarg);
1.257 dtucker 430: exit(255);
1.31 markus 431: }
1.232 djm 432: break;
433:
434: case 'R':
435: if (parse_forward(&fwd, optarg)) {
436: add_remote_forward(&options, &fwd);
437: } else {
1.128 fgsch 438: fprintf(stderr,
1.232 djm 439: "Bad remote forwarding specification "
440: "'%s'\n", optarg);
1.257 dtucker 441: exit(255);
1.31 markus 442: }
443: break;
1.108 markus 444:
445: case 'D':
1.232 djm 446: cp = p = xstrdup(optarg);
447: memset(&fwd, '\0', sizeof(fwd));
448: fwd.connect_host = "socks";
449: if ((fwd.listen_host = hpdelim(&cp)) == NULL) {
450: fprintf(stderr, "Bad dynamic forwarding "
451: "specification '%.100s'\n", optarg);
1.257 dtucker 452: exit(255);
1.232 djm 453: }
454: if (cp != NULL) {
455: fwd.listen_port = a2port(cp);
456: fwd.listen_host = cleanhostname(fwd.listen_host);
457: } else {
458: fwd.listen_port = a2port(fwd.listen_host);
1.249 djm 459: fwd.listen_host = NULL;
1.232 djm 460: }
461:
462: if (fwd.listen_port == 0) {
1.128 fgsch 463: fprintf(stderr, "Bad dynamic port '%s'\n",
464: optarg);
1.257 dtucker 465: exit(255);
1.109 markus 466: }
1.232 djm 467: add_local_forward(&options, &fwd);
468: xfree(p);
1.108 markus 469: break;
470:
1.31 markus 471: case 'C':
472: options.compression = 1;
473: break;
1.45 markus 474: case 'N':
475: no_shell_flag = 1;
476: no_tty_flag = 1;
477: break;
478: case 'T':
479: no_tty_flag = 1;
480: break;
1.31 markus 481: case 'o':
482: dummy = 1;
1.205 markus 483: line = xstrdup(optarg);
1.128 fgsch 484: if (process_config_line(&options, host ? host : "",
1.205 markus 485: line, "command-line", 0, &dummy) != 0)
1.257 dtucker 486: exit(255);
1.205 markus 487: xfree(line);
1.31 markus 488: break;
1.85 djm 489: case 's':
490: subsystem_flag = 1;
1.117 markus 491: break;
1.214 djm 492: case 'S':
493: if (options.control_path != NULL)
494: free(options.control_path);
495: options.control_path = xstrdup(optarg);
496: break;
1.117 markus 497: case 'b':
498: options.bind_address = optarg;
1.85 djm 499: break;
1.139 markus 500: case 'F':
501: config = optarg;
502: break;
1.31 markus 503: default:
504: usage();
1.1 deraadt 505: }
1.31 markus 506: }
507:
1.128 fgsch 508: ac -= optind;
509: av += optind;
510:
511: if (ac > 0 && !host && **av != '-') {
1.188 markus 512: if (strrchr(*av, '@')) {
1.128 fgsch 513: p = xstrdup(*av);
1.188 markus 514: cp = strrchr(p, '@');
1.128 fgsch 515: if (cp == NULL || cp == p)
516: usage();
517: options.user = p;
518: *cp = '\0';
519: host = ++cp;
520: } else
521: host = *av;
1.189 millert 522: if (ac > 1) {
523: optind = optreset = 1;
1.128 fgsch 524: goto again;
525: }
1.189 millert 526: ac--, av++;
1.128 fgsch 527: }
528:
1.31 markus 529: /* Check that we got a host name. */
530: if (!host)
531: usage();
532:
1.54 markus 533: SSLeay_add_all_algorithms();
1.72 markus 534: ERR_load_crypto_strings();
1.31 markus 535:
536: /* Initialize the command to execute on remote host. */
537: buffer_init(&command);
1.1 deraadt 538:
1.33 markus 539: /*
540: * Save the command to execute on the remote host in a buffer. There
541: * is no limit on the length of the command, except by the maximum
542: * packet size. Also sets the tty flag if there is no command.
543: */
1.128 fgsch 544: if (!ac) {
1.31 markus 545: /* No command specified - execute shell on a tty. */
546: tty_flag = 1;
1.85 djm 547: if (subsystem_flag) {
1.128 fgsch 548: fprintf(stderr,
549: "You must specify a subsystem to invoke.\n");
1.85 djm 550: usage();
551: }
1.31 markus 552: } else {
1.128 fgsch 553: /* A command has been specified. Store it into the buffer. */
554: for (i = 0; i < ac; i++) {
555: if (i)
1.31 markus 556: buffer_append(&command, " ", 1);
557: buffer_append(&command, av[i], strlen(av[i]));
558: }
559: }
560:
561: /* Cannot fork to background if no command. */
1.63 markus 562: if (fork_after_authentication_flag && buffer_len(&command) == 0 && !no_shell_flag)
1.31 markus 563: fatal("Cannot fork into background without a command to execute.");
564:
565: /* Allocate a tty by default if no command specified. */
566: if (buffer_len(&command) == 0)
567: tty_flag = 1;
568:
1.180 deraadt 569: /* Force no tty */
1.75 markus 570: if (no_tty_flag)
571: tty_flag = 0;
1.31 markus 572: /* Do not allocate a tty if stdin is not a tty. */
1.236 djm 573: if ((!isatty(fileno(stdin)) || stdin_null_flag) && !force_tty_flag) {
1.31 markus 574: if (tty_flag)
1.191 itojun 575: logit("Pseudo-terminal will not be allocated because stdin is not a terminal.");
1.31 markus 576: tty_flag = 0;
577: }
1.45 markus 578:
1.101 markus 579: /*
580: * Initialize "log" output. Since we are the client all output
581: * actually goes to stderr.
582: */
1.111 markus 583: log_init(av[0], options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
584: SYSLOG_FACILITY_USER, 1);
1.31 markus 585:
1.139 markus 586: /*
587: * Read per-user configuration file. Ignore the system wide config
588: * file if the user specifies a config file on the command line.
589: */
590: if (config != NULL) {
1.211 djm 591: if (!read_config_file(config, host, &options, 0))
1.142 stevesk 592: fatal("Can't open user config file %.100s: "
593: "%.100s", config, strerror(errno));
1.139 markus 594: } else {
595: snprintf(buf, sizeof buf, "%.100s/%.100s", pw->pw_dir,
596: _PATH_SSH_USER_CONFFILE);
1.210 djm 597: (void)read_config_file(buf, host, &options, 1);
1.139 markus 598:
1.145 markus 599: /* Read systemwide configuration file after use config. */
1.223 deraadt 600: (void)read_config_file(_PATH_HOST_CONFIG_FILE, host,
1.210 djm 601: &options, 0);
1.139 markus 602: }
1.31 markus 603:
604: /* Fill configuration defaults. */
605: fill_default_options(&options);
606:
1.196 djm 607: channel_set_af(options.address_family);
608:
1.31 markus 609: /* reinit */
1.101 markus 610: log_init(av[0], options.log_level, SYSLOG_FACILITY_USER, 1);
1.31 markus 611:
612: if (options.user == NULL)
613: options.user = xstrdup(pw->pw_name);
614:
615: if (options.hostname != NULL)
616: host = options.hostname;
1.195 markus 617:
618: /* force lowercase for hostkey matching */
619: if (options.host_key_alias != NULL) {
620: for (p = options.host_key_alias; *p; p++)
621: if (isupper(*p))
1.271 deraadt 622: *p = (char)tolower(*p);
1.195 markus 623: }
1.190 markus 624:
1.246 djm 625: /* Get default port if port has not been set. */
626: if (options.port == 0) {
627: sp = getservbyname(SSH_SERVICE_NAME, "tcp");
628: options.port = sp ? ntohs(sp->s_port) : SSH_DEFAULT_PORT;
629: }
630:
1.190 markus 631: if (options.proxy_command != NULL &&
632: strcmp(options.proxy_command, "none") == 0)
633: options.proxy_command = NULL;
1.245 djm 634: if (options.control_path != NULL &&
635: strcmp(options.control_path, "none") == 0)
636: options.control_path = NULL;
1.31 markus 637:
1.214 djm 638: if (options.control_path != NULL) {
1.275 ! djm 639: char thishost[NI_MAXHOST];
1.261 djm 640:
1.275 ! djm 641: if (gethostname(thishost, sizeof(thishost)) == -1)
1.261 djm 642: fatal("gethostname: %s", strerror(errno));
1.241 djm 643: snprintf(buf, sizeof(buf), "%d", options.port);
644: cp = tilde_expand_filename(options.control_path,
645: original_real_uid);
646: options.control_path = percent_expand(cp, "p", buf, "h", host,
1.275 ! djm 647: "r", options.user, "l", thishost, (char *)NULL);
1.241 djm 648: xfree(cp);
1.214 djm 649: }
1.240 djm 650: if (mux_command != 0 && options.control_path == NULL)
651: fatal("No ControlPath specified for \"-O\" command");
1.242 djm 652: if (options.control_path != NULL)
1.237 jakob 653: control_client(options.control_path);
1.214 djm 654:
1.77 markus 655: /* Open a connection to the remote host. */
1.203 djm 656: if (ssh_connect(host, &hostaddr, options.port,
1.196 djm 657: options.address_family, options.connection_attempts,
1.177 markus 658: original_effective_uid == 0 && options.use_privileged_port,
1.179 markus 659: options.proxy_command) != 0)
1.257 dtucker 660: exit(255);
1.31 markus 661:
1.33 markus 662: /*
663: * If we successfully made the connection, load the host private key
664: * in case we will need it later for combined rsa-rhosts
665: * authentication. This must be done before releasing extra
666: * privileges, because the file is only readable by root.
1.174 markus 667: * If we cannot access the private keys, load the public keys
668: * instead and try to execute the ssh-keysign helper instead.
1.33 markus 669: */
1.112 markus 670: sensitive_data.nkeys = 0;
671: sensitive_data.keys = NULL;
1.173 markus 672: sensitive_data.external_keysign = 0;
1.178 markus 673: if (options.rhosts_rsa_authentication ||
674: options.hostbased_authentication) {
1.112 markus 675: sensitive_data.nkeys = 3;
1.274 deraadt 676: sensitive_data.keys = xcalloc(sensitive_data.nkeys,
1.180 deraadt 677: sizeof(Key));
1.177 markus 678:
679: PRIV_START;
1.112 markus 680: sensitive_data.keys[0] = key_load_private_type(KEY_RSA1,
1.105 markus 681: _PATH_HOST_KEY_FILE, "", NULL);
1.112 markus 682: sensitive_data.keys[1] = key_load_private_type(KEY_DSA,
683: _PATH_HOST_DSA_KEY_FILE, "", NULL);
684: sensitive_data.keys[2] = key_load_private_type(KEY_RSA,
685: _PATH_HOST_RSA_KEY_FILE, "", NULL);
1.177 markus 686: PRIV_END;
1.173 markus 687:
1.181 markus 688: if (options.hostbased_authentication == 1 &&
689: sensitive_data.keys[0] == NULL &&
1.173 markus 690: sensitive_data.keys[1] == NULL &&
691: sensitive_data.keys[2] == NULL) {
692: sensitive_data.keys[1] = key_load_public(
693: _PATH_HOST_DSA_KEY_FILE, NULL);
694: sensitive_data.keys[2] = key_load_public(
695: _PATH_HOST_RSA_KEY_FILE, NULL);
696: sensitive_data.external_keysign = 1;
697: }
1.31 markus 698: }
1.33 markus 699: /*
700: * Get rid of any extra privileges that we may have. We will no
701: * longer need them. Also, extra privileges could make it very hard
702: * to read identity files and other non-world-readable files from the
703: * user's home directory if it happens to be on a NFS volume where
704: * root is mapped to nobody.
705: */
1.225 dtucker 706: if (original_effective_uid == 0) {
707: PRIV_START;
708: permanently_set_uid(pw);
709: }
1.31 markus 710:
1.33 markus 711: /*
712: * Now that we are back to our own permissions, create ~/.ssh
1.254 djm 713: * directory if it doesn't already exist.
1.33 markus 714: */
1.138 jakob 715: snprintf(buf, sizeof buf, "%.100s%s%.100s", pw->pw_dir, strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
1.31 markus 716: if (stat(buf, &st) < 0)
1.57 djm 717: if (mkdir(buf, 0700) < 0)
1.31 markus 718: error("Could not create directory '%.200s'.", buf);
719:
1.104 markus 720: /* load options.identity_files */
721: load_public_identity_files();
722:
723: /* Expand ~ in known host file names. */
724: /* XXX mem-leaks: */
1.72 markus 725: options.system_hostfile =
726: tilde_expand_filename(options.system_hostfile, original_real_uid);
727: options.user_hostfile =
728: tilde_expand_filename(options.user_hostfile, original_real_uid);
729: options.system_hostfile2 =
730: tilde_expand_filename(options.system_hostfile2, original_real_uid);
731: options.user_hostfile2 =
732: tilde_expand_filename(options.user_hostfile2, original_real_uid);
1.149 markus 733:
734: signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE early */
1.31 markus 735:
736: /* Log into the remote system. This never returns if the login fails. */
1.173 markus 737: ssh_login(&sensitive_data, host, (struct sockaddr *)&hostaddr, pw);
1.31 markus 738:
1.112 markus 739: /* We no longer need the private host keys. Clear them now. */
740: if (sensitive_data.nkeys != 0) {
741: for (i = 0; i < sensitive_data.nkeys; i++) {
742: if (sensitive_data.keys[i] != NULL) {
743: /* Destroys contents safely */
744: debug3("clear hostkey %d", i);
745: key_free(sensitive_data.keys[i]);
746: sensitive_data.keys[i] = NULL;
747: }
748: }
749: xfree(sensitive_data.keys);
1.134 markus 750: }
751: for (i = 0; i < options.num_identity_files; i++) {
752: if (options.identity_files[i]) {
753: xfree(options.identity_files[i]);
754: options.identity_files[i] = NULL;
755: }
756: if (options.identity_keys[i]) {
757: key_free(options.identity_keys[i]);
758: options.identity_keys[i] = NULL;
759: }
1.112 markus 760: }
1.31 markus 761:
1.45 markus 762: exit_status = compat20 ? ssh_session2() : ssh_session();
763: packet_close();
1.186 djm 764:
1.214 djm 765: if (options.control_path != NULL && control_fd != -1)
766: unlink(options.control_path);
767:
1.186 djm 768: /*
1.203 djm 769: * Send SIGHUP to proxy command if used. We don't wait() in
1.186 djm 770: * case it hangs and instead rely on init to reap the child
771: */
772: if (proxy_command_pid > 1)
773: kill(proxy_command_pid, SIGHUP);
774:
1.45 markus 775: return exit_status;
776: }
777:
1.126 itojun 778: static void
1.70 markus 779: ssh_init_forwarding(void)
780: {
1.86 markus 781: int success = 0;
1.70 markus 782: int i;
1.86 markus 783:
1.70 markus 784: /* Initiate local TCP/IP port forwardings. */
785: for (i = 0; i < options.num_local_forwards; i++) {
1.232 djm 786: debug("Local connections to %.200s:%d forwarded to remote "
787: "address %.200s:%d",
1.234 deraadt 788: (options.local_forwards[i].listen_host == NULL) ?
789: (options.gateway_ports ? "*" : "LOCALHOST") :
1.232 djm 790: options.local_forwards[i].listen_host,
791: options.local_forwards[i].listen_port,
792: options.local_forwards[i].connect_host,
793: options.local_forwards[i].connect_port);
1.158 markus 794: success += channel_setup_local_fwd_listener(
1.232 djm 795: options.local_forwards[i].listen_host,
796: options.local_forwards[i].listen_port,
797: options.local_forwards[i].connect_host,
798: options.local_forwards[i].connect_port,
1.70 markus 799: options.gateway_ports);
800: }
1.86 markus 801: if (i > 0 && success == 0)
802: error("Could not request local forwarding.");
1.70 markus 803:
804: /* Initiate remote TCP/IP port forwardings. */
805: for (i = 0; i < options.num_remote_forwards; i++) {
1.232 djm 806: debug("Remote connections from %.200s:%d forwarded to "
807: "local address %.200s:%d",
1.248 djm 808: (options.remote_forwards[i].listen_host == NULL) ?
1.253 djm 809: "LOCALHOST" : options.remote_forwards[i].listen_host,
1.232 djm 810: options.remote_forwards[i].listen_port,
811: options.remote_forwards[i].connect_host,
812: options.remote_forwards[i].connect_port);
1.70 markus 813: channel_request_remote_forwarding(
1.232 djm 814: options.remote_forwards[i].listen_host,
815: options.remote_forwards[i].listen_port,
816: options.remote_forwards[i].connect_host,
817: options.remote_forwards[i].connect_port);
1.70 markus 818: }
819: }
820:
1.126 itojun 821: static void
1.70 markus 822: check_agent_present(void)
823: {
824: if (options.forward_agent) {
1.254 djm 825: /* Clear agent forwarding if we don't have an agent. */
1.185 stevesk 826: if (!ssh_agent_present())
1.70 markus 827: options.forward_agent = 0;
828: }
829: }
830:
1.126 itojun 831: static int
1.45 markus 832: ssh_session(void)
833: {
834: int type;
835: int interactive = 0;
836: int have_tty = 0;
837: struct winsize ws;
838: char *cp;
1.243 djm 839: const char *display;
1.45 markus 840:
1.31 markus 841: /* Enable compression if requested. */
842: if (options.compression) {
843: debug("Requesting compression at level %d.", options.compression_level);
844:
845: if (options.compression_level < 1 || options.compression_level > 9)
846: fatal("Compression level must be from 1 (fast) to 9 (slow, best).");
847:
848: /* Send the request. */
849: packet_start(SSH_CMSG_REQUEST_COMPRESSION);
850: packet_put_int(options.compression_level);
851: packet_send();
852: packet_write_wait();
1.156 markus 853: type = packet_read();
1.31 markus 854: if (type == SSH_SMSG_SUCCESS)
855: packet_start_compression(options.compression_level);
856: else if (type == SSH_SMSG_FAILURE)
1.191 itojun 857: logit("Warning: Remote host refused compression.");
1.31 markus 858: else
859: packet_disconnect("Protocol error waiting for compression response.");
860: }
861: /* Allocate a pseudo tty if appropriate. */
862: if (tty_flag) {
863: debug("Requesting pty.");
864:
865: /* Start the packet. */
866: packet_start(SSH_CMSG_REQUEST_PTY);
867:
868: /* Store TERM in the packet. There is no limit on the
869: length of the string. */
870: cp = getenv("TERM");
871: if (!cp)
872: cp = "";
1.124 markus 873: packet_put_cstring(cp);
1.31 markus 874:
875: /* Store window size in the packet. */
876: if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
877: memset(&ws, 0, sizeof(ws));
1.269 deraadt 878: packet_put_int((u_int)ws.ws_row);
879: packet_put_int((u_int)ws.ws_col);
880: packet_put_int((u_int)ws.ws_xpixel);
881: packet_put_int((u_int)ws.ws_ypixel);
1.31 markus 882:
883: /* Store tty modes in the packet. */
1.115 stevesk 884: tty_make_modes(fileno(stdin), NULL);
1.31 markus 885:
886: /* Send the packet, and wait for it to leave. */
887: packet_send();
888: packet_write_wait();
889:
890: /* Read response from the server. */
1.156 markus 891: type = packet_read();
1.43 markus 892: if (type == SSH_SMSG_SUCCESS) {
1.31 markus 893: interactive = 1;
1.45 markus 894: have_tty = 1;
1.43 markus 895: } else if (type == SSH_SMSG_FAILURE)
1.191 itojun 896: logit("Warning: Remote host failed or refused to allocate a pseudo tty.");
1.31 markus 897: else
898: packet_disconnect("Protocol error waiting for pty request response.");
899: }
900: /* Request X11 forwarding if enabled and DISPLAY is set. */
1.243 djm 901: display = getenv("DISPLAY");
902: if (options.forward_x11 && display != NULL) {
1.150 stevesk 903: char *proto, *data;
1.50 markus 904: /* Get reasonable local authentication information. */
1.243 djm 905: client_x11_get_proto(display, options.xauth_location,
906: options.forward_x11_trusted, &proto, &data);
1.50 markus 907: /* Request forwarding with authentication spoofing. */
1.31 markus 908: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 909: x11_request_forwarding_with_spoofing(0, display, proto, data);
1.31 markus 910:
911: /* Read response from the server. */
1.156 markus 912: type = packet_read();
1.31 markus 913: if (type == SSH_SMSG_SUCCESS) {
914: interactive = 1;
1.50 markus 915: } else if (type == SSH_SMSG_FAILURE) {
1.191 itojun 916: logit("Warning: Remote host denied X11 forwarding.");
1.50 markus 917: } else {
1.31 markus 918: packet_disconnect("Protocol error waiting for X11 forwarding");
1.50 markus 919: }
1.31 markus 920: }
921: /* Tell the packet module whether this is an interactive session. */
1.80 markus 922: packet_set_interactive(interactive);
1.31 markus 923:
924: /* Request authentication agent forwarding if appropriate. */
1.70 markus 925: check_agent_present();
926:
1.31 markus 927: if (options.forward_agent) {
928: debug("Requesting authentication agent forwarding.");
929: auth_request_forwarding();
930:
931: /* Read response from the server. */
1.156 markus 932: type = packet_read();
1.155 markus 933: packet_check_eom();
1.31 markus 934: if (type != SSH_SMSG_SUCCESS)
1.191 itojun 935: logit("Warning: Remote host denied authentication agent forwarding.");
1.31 markus 936: }
937:
1.70 markus 938: /* Initiate port forwardings. */
939: ssh_init_forwarding();
1.34 markus 940:
941: /* If requested, let ssh continue in the background. */
1.48 markus 942: if (fork_after_authentication_flag)
1.34 markus 943: if (daemon(1, 1) < 0)
944: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 945:
1.33 markus 946: /*
947: * If a command was specified on the command line, execute the
948: * command now. Otherwise request the server to start a shell.
949: */
1.31 markus 950: if (buffer_len(&command) > 0) {
951: int len = buffer_len(&command);
952: if (len > 900)
953: len = 900;
1.152 stevesk 954: debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));
1.31 markus 955: packet_start(SSH_CMSG_EXEC_CMD);
956: packet_put_string(buffer_ptr(&command), buffer_len(&command));
957: packet_send();
958: packet_write_wait();
959: } else {
960: debug("Requesting shell.");
961: packet_start(SSH_CMSG_EXEC_SHELL);
962: packet_send();
963: packet_write_wait();
964: }
965:
966: /* Enter the interactive session. */
1.119 stevesk 967: return client_loop(have_tty, tty_flag ?
968: options.escape_char : SSH_ESCAPECHAR_NONE, 0);
1.45 markus 969: }
970:
1.126 itojun 971: static void
1.214 djm 972: ssh_subsystem_reply(int type, u_int32_t seq, void *ctxt)
1.89 markus 973: {
974: int id, len;
975:
976: id = packet_get_int();
977: len = buffer_len(&command);
1.100 markus 978: if (len > 900)
979: len = 900;
1.155 markus 980: packet_check_eom();
1.89 markus 981: if (type == SSH2_MSG_CHANNEL_FAILURE)
982: fatal("Request for subsystem '%.*s' failed on channel %d",
1.152 stevesk 983: len, (u_char *)buffer_ptr(&command), id);
1.170 markus 984: }
985:
986: void
1.206 markus 987: client_global_request_reply_fwd(int type, u_int32_t seq, void *ctxt)
1.170 markus 988: {
989: int i;
990:
991: i = client_global_request_id++;
1.206 markus 992: if (i >= options.num_remote_forwards)
1.170 markus 993: return;
994: debug("remote forward %s for: listen %d, connect %s:%d",
995: type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",
1.232 djm 996: options.remote_forwards[i].listen_port,
997: options.remote_forwards[i].connect_host,
998: options.remote_forwards[i].connect_port);
1.170 markus 999: if (type == SSH2_MSG_REQUEST_FAILURE)
1.232 djm 1000: logit("Warning: remote port forwarding failed for listen "
1001: "port %d", options.remote_forwards[i].listen_port);
1.89 markus 1002: }
1003:
1.126 itojun 1004: static void
1.214 djm 1005: ssh_control_listener(void)
1.45 markus 1006: {
1.214 djm 1007: struct sockaddr_un addr;
1008: mode_t old_umask;
1.223 deraadt 1009:
1.242 djm 1010: if (options.control_path == NULL ||
1011: options.control_master == SSHCTL_MASTER_NO)
1.214 djm 1012: return;
1013:
1.242 djm 1014: debug("setting up multiplex master socket");
1015:
1.214 djm 1016: memset(&addr, '\0', sizeof(addr));
1017: addr.sun_family = AF_UNIX;
1018: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1019: strlen(options.control_path) + 1;
1020:
1021: if (strlcpy(addr.sun_path, options.control_path,
1022: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1023: fatal("ControlPath too long");
1024:
1025: if ((control_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1.252 stevesk 1026: fatal("%s socket(): %s", __func__, strerror(errno));
1.214 djm 1027:
1028: old_umask = umask(0177);
1.270 deraadt 1029: if (bind(control_fd, (struct sockaddr *)&addr, addr.sun_len) == -1) {
1.214 djm 1030: control_fd = -1;
1.238 djm 1031: if (errno == EINVAL || errno == EADDRINUSE)
1.214 djm 1032: fatal("ControlSocket %s already exists",
1033: options.control_path);
1034: else
1.252 stevesk 1035: fatal("%s bind(): %s", __func__, strerror(errno));
1.214 djm 1036: }
1037: umask(old_umask);
1.80 markus 1038:
1.214 djm 1039: if (listen(control_fd, 64) == -1)
1.252 stevesk 1040: fatal("%s listen(): %s", __func__, strerror(errno));
1.31 markus 1041:
1.214 djm 1042: set_nonblock(control_fd);
1043: }
1.45 markus 1044:
1.214 djm 1045: /* request pty/x11/agent/tcpfwd/shell for channel */
1046: static void
1047: ssh_session2_setup(int id, void *arg)
1048: {
1.215 djm 1049: extern char **environ;
1.243 djm 1050: const char *display;
1051: int interactive = tty_flag;
1.215 djm 1052:
1.248 djm 1053: display = getenv("DISPLAY");
1.243 djm 1054: if (options.forward_x11 && display != NULL) {
1.150 stevesk 1055: char *proto, *data;
1.50 markus 1056: /* Get reasonable local authentication information. */
1.243 djm 1057: client_x11_get_proto(display, options.xauth_location,
1058: options.forward_x11_trusted, &proto, &data);
1.50 markus 1059: /* Request forwarding with authentication spoofing. */
1060: debug("Requesting X11 forwarding with authentication spoofing.");
1.243 djm 1061: x11_request_forwarding_with_spoofing(id, display, proto, data);
1.80 markus 1062: interactive = 1;
1.50 markus 1063: /* XXX wait for reply */
1064: }
1065:
1.70 markus 1066: check_agent_present();
1067: if (options.forward_agent) {
1068: debug("Requesting authentication agent forwarding.");
1069: channel_request_start(id, "auth-agent-req@openssh.com", 0);
1070: packet_send();
1.212 djm 1071: }
1072:
1.256 reyk 1073: if (options.tun_open != SSH_TUNMODE_NO) {
1.255 reyk 1074: Channel *c;
1075: int fd;
1076:
1077: debug("Requesting tun.");
1.256 reyk 1078: if ((fd = tun_open(options.tun_local,
1079: options.tun_open)) >= 0) {
1.255 reyk 1080: c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1081: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1082: 0, "tun", 1);
1083: c->datagram = 1;
1084: packet_start(SSH2_MSG_CHANNEL_OPEN);
1085: packet_put_cstring("tun@openssh.com");
1086: packet_put_int(c->self);
1087: packet_put_int(c->local_window_max);
1088: packet_put_int(c->local_maxpacket);
1.256 reyk 1089: packet_put_int(options.tun_open);
1.255 reyk 1090: packet_put_int(options.tun_remote);
1091: packet_send();
1092: }
1093: }
1094:
1.214 djm 1095: client_session2_setup(id, tty_flag, subsystem_flag, getenv("TERM"),
1.215 djm 1096: NULL, fileno(stdin), &command, environ, &ssh_subsystem_reply);
1.90 markus 1097:
1.80 markus 1098: packet_set_interactive(interactive);
1.45 markus 1099: }
1100:
1.143 markus 1101: /* open new channel for a session */
1.126 itojun 1102: static int
1.143 markus 1103: ssh_session2_open(void)
1.45 markus 1104: {
1.118 markus 1105: Channel *c;
1106: int window, packetmax, in, out, err;
1.60 markus 1107:
1.62 markus 1108: if (stdin_null_flag) {
1.93 itojun 1109: in = open(_PATH_DEVNULL, O_RDONLY);
1.62 markus 1110: } else {
1111: in = dup(STDIN_FILENO);
1112: }
1.60 markus 1113: out = dup(STDOUT_FILENO);
1114: err = dup(STDERR_FILENO);
1.45 markus 1115:
1116: if (in < 0 || out < 0 || err < 0)
1.62 markus 1117: fatal("dup() in/out/err failed");
1.45 markus 1118:
1.69 markus 1119: /* enable nonblocking unless tty */
1120: if (!isatty(in))
1121: set_nonblock(in);
1122: if (!isatty(out))
1123: set_nonblock(out);
1124: if (!isatty(err))
1125: set_nonblock(err);
1126:
1.65 markus 1127: window = CHAN_SES_WINDOW_DEFAULT;
1128: packetmax = CHAN_SES_PACKET_DEFAULT;
1.164 markus 1129: if (tty_flag) {
1130: window >>= 1;
1131: packetmax >>= 1;
1.45 markus 1132: }
1.118 markus 1133: c = channel_new(
1.45 markus 1134: "session", SSH_CHANNEL_OPENING, in, out, err,
1.65 markus 1135: window, packetmax, CHAN_EXTENDED_WRITE,
1.192 markus 1136: "client-session", /*nonblock*/0);
1.45 markus 1137:
1.143 markus 1138: debug3("ssh_session2_open: channel_new: %d", c->self);
1.106 markus 1139:
1.122 markus 1140: channel_send_open(c->self);
1.143 markus 1141: if (!no_shell_flag)
1.214 djm 1142: channel_register_confirm(c->self, ssh_session2_setup, NULL);
1.106 markus 1143:
1.118 markus 1144: return c->self;
1.106 markus 1145: }
1146:
1.126 itojun 1147: static int
1.106 markus 1148: ssh_session2(void)
1149: {
1.143 markus 1150: int id = -1;
1.106 markus 1151:
1152: /* XXX should be pre-session */
1153: ssh_init_forwarding();
1.214 djm 1154: ssh_control_listener();
1.106 markus 1155:
1.143 markus 1156: if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))
1157: id = ssh_session2_open();
1.255 reyk 1158:
1159: /* Execute a local command */
1160: if (options.local_command != NULL &&
1161: options.permit_local_command)
1162: ssh_local_cmd(options.local_command);
1.106 markus 1163:
1164: /* If requested, let ssh continue in the background. */
1165: if (fork_after_authentication_flag)
1166: if (daemon(1, 1) < 0)
1167: fatal("daemon() failed: %.200s", strerror(errno));
1.31 markus 1168:
1.119 stevesk 1169: return client_loop(tty_flag, tty_flag ?
1170: options.escape_char : SSH_ESCAPECHAR_NONE, id);
1.72 markus 1171: }
1172:
1.126 itojun 1173: static void
1.104 markus 1174: load_public_identity_files(void)
1175: {
1.275 ! djm 1176: char *filename, *cp, thishost[NI_MAXHOST];
1.167 markus 1177: int i = 0;
1.104 markus 1178: Key *public;
1.275 ! djm 1179: struct passwd *pw;
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.275 ! djm 1203: if ((pw = getpwuid(original_real_uid)) == NULL)
! 1204: fatal("load_public_identity_files: getpwuid failed");
! 1205: if (gethostname(thishost, sizeof(thishost)) == -1)
! 1206: fatal("load_public_identity_files: gethostname: %s",
! 1207: strerror(errno));
1.131 millert 1208: for (; i < options.num_identity_files; i++) {
1.275 ! djm 1209: cp = tilde_expand_filename(options.identity_files[i],
1.131 millert 1210: original_real_uid);
1.275 ! djm 1211: filename = percent_expand(cp, "d", pw->pw_dir,
! 1212: "u", pw->pw_name, "l", thishost, "h", host,
! 1213: "r", options.user, (char *)NULL);
! 1214: xfree(cp);
1.131 millert 1215: public = key_load_public(filename, NULL);
1216: debug("identity file %s type %d", filename,
1217: public ? public->type : -1);
1218: xfree(options.identity_files[i]);
1219: options.identity_files[i] = filename;
1220: options.identity_keys[i] = public;
1221: }
1.214 djm 1222: }
1223:
1224: static void
1225: control_client_sighandler(int signo)
1226: {
1227: control_client_terminate = signo;
1228: }
1229:
1230: static void
1231: control_client_sigrelay(int signo)
1232: {
1233: if (control_server_pid > 1)
1234: kill(control_server_pid, signo);
1235: }
1236:
1.220 djm 1237: static int
1238: env_permitted(char *env)
1239: {
1240: int i;
1241: char name[1024], *cp;
1242:
1.272 djm 1243: if (strlcpy(name, env, sizeof(name)) >= sizeof(name))
1244: fatal("env_permitted: name too long");
1.220 djm 1245: if ((cp = strchr(name, '=')) == NULL)
1246: return (0);
1247:
1248: *cp = '\0';
1249:
1250: for (i = 0; i < options.num_send_env; i++)
1251: if (match_pattern(name, options.send_env[i]))
1252: return (1);
1253:
1254: return (0);
1255: }
1256:
1.214 djm 1257: static void
1258: control_client(const char *path)
1259: {
1260: struct sockaddr_un addr;
1.228 djm 1261: int i, r, fd, sock, exitval, num_env;
1.214 djm 1262: Buffer m;
1.229 djm 1263: char *term;
1.215 djm 1264: extern char **environ;
1.229 djm 1265: u_int flags;
1.242 djm 1266:
1267: if (mux_command == 0)
1268: mux_command = SSHMUX_COMMAND_OPEN;
1269:
1270: switch (options.control_master) {
1271: case SSHCTL_MASTER_AUTO:
1272: case SSHCTL_MASTER_AUTO_ASK:
1273: debug("auto-mux: Trying existing master");
1274: /* FALLTHROUGH */
1275: case SSHCTL_MASTER_NO:
1276: break;
1277: default:
1278: return;
1279: }
1.228 djm 1280:
1.214 djm 1281: memset(&addr, '\0', sizeof(addr));
1282: addr.sun_family = AF_UNIX;
1283: addr.sun_len = offsetof(struct sockaddr_un, sun_path) +
1284: strlen(path) + 1;
1285:
1286: if (strlcpy(addr.sun_path, path,
1287: sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
1288: fatal("ControlPath too long");
1289:
1290: if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1291: fatal("%s socket(): %s", __func__, strerror(errno));
1292:
1.270 deraadt 1293: if (connect(sock, (struct sockaddr *)&addr, addr.sun_len) == -1) {
1.240 djm 1294: if (mux_command != SSHMUX_COMMAND_OPEN) {
1295: fatal("Control socket connect(%.100s): %s", path,
1296: strerror(errno));
1297: }
1.239 djm 1298: if (errno == ENOENT)
1.268 deraadt 1299: debug("Control socket \"%.100s\" does not exist", path);
1.239 djm 1300: else {
1.268 deraadt 1301: error("Control socket connect(%.100s): %s", path,
1.239 djm 1302: strerror(errno));
1303: }
1.268 deraadt 1304: close(sock);
1305: return;
1306: }
1.248 djm 1307:
1.268 deraadt 1308: if (stdin_null_flag) {
1309: if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1310: fatal("open(/dev/null): %s", strerror(errno));
1311: if (dup2(fd, STDIN_FILENO) == -1)
1312: fatal("dup2: %s", strerror(errno));
1313: if (fd > STDERR_FILENO)
1314: close(fd);
1315: }
1.248 djm 1316:
1.247 djm 1317: term = getenv("TERM");
1.229 djm 1318:
1319: flags = 0;
1320: if (tty_flag)
1321: flags |= SSHMUX_FLAG_TTY;
1322: if (subsystem_flag)
1323: flags |= SSHMUX_FLAG_SUBSYS;
1.247 djm 1324: if (options.forward_x11)
1325: flags |= SSHMUX_FLAG_X11_FWD;
1326: if (options.forward_agent)
1327: flags |= SSHMUX_FLAG_AGENT_FWD;
1.214 djm 1328:
1329: buffer_init(&m);
1330:
1.229 djm 1331: /* Send our command to server */
1332: buffer_put_int(&m, mux_command);
1333: buffer_put_int(&m, flags);
1.247 djm 1334: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.229 djm 1335: fatal("%s: msg_send", __func__);
1336: buffer_clear(&m);
1337:
1338: /* Get authorisation status and PID of controlee */
1.214 djm 1339: if (ssh_msg_recv(sock, &m) == -1)
1340: fatal("%s: msg_recv", __func__);
1.247 djm 1341: if (buffer_get_char(&m) != SSHMUX_VER)
1.214 djm 1342: fatal("%s: wrong version", __func__);
1.216 djm 1343: if (buffer_get_int(&m) != 1)
1344: fatal("Connection to master denied");
1.214 djm 1345: control_server_pid = buffer_get_int(&m);
1346:
1347: buffer_clear(&m);
1348:
1.229 djm 1349: switch (mux_command) {
1350: case SSHMUX_COMMAND_ALIVE_CHECK:
1.234 deraadt 1351: fprintf(stderr, "Master running (pid=%d)\r\n",
1.229 djm 1352: control_server_pid);
1353: exit(0);
1354: case SSHMUX_COMMAND_TERMINATE:
1355: fprintf(stderr, "Exit request sent.\r\n");
1356: exit(0);
1357: case SSHMUX_COMMAND_OPEN:
1358: /* continue below */
1359: break;
1360: default:
1361: fatal("silly mux_command %d", mux_command);
1362: }
1363:
1364: /* SSHMUX_COMMAND_OPEN */
1.247 djm 1365: buffer_put_cstring(&m, term ? term : "");
1.214 djm 1366: buffer_append(&command, "\0", 1);
1367: buffer_put_cstring(&m, buffer_ptr(&command));
1.215 djm 1368:
1.220 djm 1369: if (options.num_send_env == 0 || environ == NULL) {
1370: buffer_put_int(&m, 0);
1.223 deraadt 1371: } else {
1.220 djm 1372: /* Pass environment */
1373: num_env = 0;
1374: for (i = 0; environ[i] != NULL; i++)
1375: if (env_permitted(environ[i]))
1376: num_env++; /* Count */
1.223 deraadt 1377:
1.220 djm 1378: buffer_put_int(&m, num_env);
1379:
1.222 dtucker 1380: for (i = 0; environ[i] != NULL && num_env >= 0; i++)
1381: if (env_permitted(environ[i])) {
1382: num_env--;
1.220 djm 1383: buffer_put_cstring(&m, environ[i]);
1.222 dtucker 1384: }
1.220 djm 1385: }
1.214 djm 1386:
1.247 djm 1387: if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1)
1.214 djm 1388: fatal("%s: msg_send", __func__);
1389:
1390: mm_send_fd(sock, STDIN_FILENO);
1391: mm_send_fd(sock, STDOUT_FILENO);
1392: mm_send_fd(sock, STDERR_FILENO);
1393:
1394: /* Wait for reply, so master has a chance to gather ttymodes */
1395: buffer_clear(&m);
1396: if (ssh_msg_recv(sock, &m) == -1)
1397: fatal("%s: msg_recv", __func__);
1.247 djm 1398: if (buffer_get_char(&m) != SSHMUX_VER)
1.229 djm 1399: fatal("%s: wrong version", __func__);
1.214 djm 1400: buffer_free(&m);
1.218 djm 1401:
1.226 djm 1402: signal(SIGHUP, control_client_sighandler);
1.218 djm 1403: signal(SIGINT, control_client_sighandler);
1404: signal(SIGTERM, control_client_sighandler);
1405: signal(SIGWINCH, control_client_sigrelay);
1.214 djm 1406:
1407: if (tty_flag)
1408: enter_raw_mode();
1409:
1410: /* Stick around until the controlee closes the client_fd */
1411: exitval = 0;
1412: for (;!control_client_terminate;) {
1413: r = read(sock, &exitval, sizeof(exitval));
1414: if (r == 0) {
1415: debug2("Received EOF from master");
1416: break;
1417: }
1418: if (r > 0)
1419: debug2("Received exit status from master %d", exitval);
1420: if (r == -1 && errno != EINTR)
1421: fatal("%s: read %s", __func__, strerror(errno));
1422: }
1423:
1424: if (control_client_terminate)
1425: debug2("Exiting on signal %d", control_client_terminate);
1426:
1427: close(sock);
1428:
1429: leave_raw_mode();
1430:
1431: if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
1432: fprintf(stderr, "Connection to master closed.\r\n");
1433:
1434: exit(exitval);
1.1 deraadt 1435: }