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