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