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