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