@Override public String getValue(String key) { // See com.jcraft.jsch.OpenSSHConfig.MyConfig.getValue() // for this special case. if (key.equals("compression.s2c") //$NON-NLS-1$ || key.equals("compression.c2s")) { //$NON-NLS-1$ if (!OpenSshConfigFile.flag( Host.this.entry.getValue(mapKey(key)))) { return "none,zlib@openssh.com,zlib"; //$NON-NLS-1$ } return "zlib@openssh.com,zlib,none"; //$NON-NLS-1$ } return Host.this.entry.getValue(mapKey(key)); }
@Override public String[] getValues(String key) { List<String> values = Host.this.entry .getValues(mapKey(key)); if (values == null) { return new String[0]; } return values.toArray(new String[0]); } };
private static boolean isHostMatch(String pattern, String name) { if (pattern.startsWith("!")) { //$NON-NLS-1$ return !patternMatchesHost(pattern.substring(1), name); } else { return patternMatchesHost(pattern, name); } }
void substitute(String originalHostName, int port, String userName, String localUserName, File home) { int p = port >= 0 ? port : positive(getValue(SshConstants.PORT)); if (p < 0) { p = SshConstants.SSH_DEFAULT_PORT; : getValue(SshConstants.USER); if (u == null || u.isEmpty()) { u = localUserName; Replacer r = new Replacer(originalHostName, p, u, localUserName, home); if (options != null) { options.put(SshConstants.HOST_NAME, originalHostName); } else { hostName = r.substitute(hostName, "h"); //$NON-NLS-1$ options.put(SshConstants.HOST_NAME, hostName); r.update('h', hostName); .get(SshConstants.IDENTITY_FILE); if (values != null) { values = substitute(values, "dhlru", r); //$NON-NLS-1$ values = replaceTilde(values, home); multiOptions.put(SshConstants.IDENTITY_FILE, values); values = substitute(values, "dhlru", r); //$NON-NLS-1$ values = replaceTilde(values, home); multiOptions.put(SshConstants.CERTIFICATE_FILE, values);
private void complete(String initialHostName, String localUserName) { hostName = entry.getValue(SshConstants.HOST_NAME); user = entry.getValue(SshConstants.USER); port = positive(entry.getValue(SshConstants.PORT)); connectionAttempts = positive( entry.getValue(SshConstants.CONNECTION_ATTEMPTS)); strictHostKeyChecking = entry .getValue(SshConstants.STRICT_HOST_KEY_CHECKING); batchMode = Boolean.valueOf(OpenSshConfigFile .flag(entry.getValue(SshConstants.BATCH_MODE))); preferredAuthentications = entry .getValue(SshConstants.PREFERRED_AUTHENTICATIONS); .getValues(SshConstants.IDENTITY_FILE); if (identityFiles != null && !identityFiles.isEmpty()) { identityFile = new File(identityFiles.get(0));
public HostEntry lookup(@NonNull String hostName, int port, String userName) { final State cache = refresh(); String cacheKey = toCacheKey(hostName, port, userName); HostEntry h = cache.hosts.get(cacheKey); if (h != null) { return h; HostEntry fullConfig = new HostEntry(); fullConfig.merge(cache.entries.get(DEFAULT_NAME)); for (Map.Entry<String, HostEntry> e : cache.entries.entrySet()) { String pattern = e.getKey(); if (isHostMatch(pattern, hostName)) { fullConfig.merge(e.getValue()); fullConfig.substitute(hostName, port, userName, localUserName, home); cache.hosts.put(cacheKey, fullConfig); return fullConfig;
HostEntry defaults = new HostEntry(); current.add(defaults); entries.put(DEFAULT_NAME, defaults); String keyword = dequote(parts[0].trim()); for (String name : parseList(argValue)) { if (name == null || name.isEmpty()) { c = new HostEntry(); entries.put(name, c); if (HostEntry.isListKey(keyword)) { List<String> args = validate(keyword, parseList(argValue)); for (HostEntry entry : current) { entry.setValue(keyword, args); argValue = validate(keyword, dequote(argValue)); for (HostEntry entry : current) { entry.setValue(keyword, argValue);
/** * Locate the configuration for a specific host request. * * @param hostName * the name the user has supplied to the SSH tool. This may be a * real host name, or it may just be a "Host" block in the * configuration file. * @return r configuration for the requested name. Never null. */ public Host lookup(String hostName) { HostEntry entry = configFile.lookup(hostName, -1, null); return new Host(entry, hostName, configFile.getLocalUserName()); }
private synchronized State refresh() { final long mtime = configFile.lastModified(); if (mtime != lastModified) { State newState = new State(); try (BufferedReader br = Files .newBufferedReader(configFile.toPath(), UTF_8)) { newState.entries = parse(br); } catch (IOException | RuntimeException none) { // Ignore -- we'll set and return an empty state } lastModified = mtime; state = newState; } return state; }
OpenSshConfig(File h, File cfg) { configFile = new OpenSshConfigFile(h, cfg, SshSessionFactory.getLocalUserName()); }
private List<String> substitute(List<String> values, String allowed, Replacer r) { List<String> result = new ArrayList<>(values.size()); for (String value : values) { result.add(r.substitute(value, allowed)); } return result; }
/** * Hook to perform validation on a single value, or to sanitize it. If this * throws an (unchecked) exception, parsing of the file is abandoned. * * @param key * of the entry * @param value * as read from the config file * @return the validated and possibly sanitized value */ protected String validate(String key, String value) { if (String.CASE_INSENSITIVE_ORDER.compare(key, SshConstants.PREFERRED_AUTHENTICATIONS) == 0) { return stripWhitespace(value); } return value; }
/** * Creates a new {@link OpenSshConfigFile} that will read the config from * file {@code config} use the given file {@code home} as "home" directory. * * @param home * user's home directory for the purpose of ~ replacement * @param config * file to load. * @param localUserName * user name of the current user on the local host OS */ public OpenSshConfigFile(@NonNull File home, @NonNull File config, @NonNull String localUserName) { this.home = home; this.configFile = config; this.localUserName = localUserName; state = new State(); }
@Override public String getValue(String key) { // See com.jcraft.jsch.OpenSSHConfig.MyConfig.getValue() // for this special case. if (key.equals("compression.s2c") //$NON-NLS-1$ || key.equals("compression.c2s")) { //$NON-NLS-1$ if (!OpenSshConfigFile.flag( Host.this.entry.getValue(mapKey(key)))) { return "none,zlib@openssh.com,zlib"; //$NON-NLS-1$ } return "zlib@openssh.com,zlib,none"; //$NON-NLS-1$ } return Host.this.entry.getValue(mapKey(key)); }
/** * Locate the configuration for a specific host request. * * @param hostName * the name the user has supplied to the SSH tool. This may be a * real host name, or it may just be a "Host" block in the * configuration file. * @return r configuration for the requested name. Never null. */ public Host lookup(String hostName) { HostEntry entry = configFile.lookup(hostName, -1, null); return new Host(entry, hostName, configFile.getLocalUserName()); }
private synchronized State refresh() { final long mtime = configFile.lastModified(); if (mtime != lastModified) { State newState = new State(); try (BufferedReader br = Files .newBufferedReader(configFile.toPath(), UTF_8)) { newState.entries = parse(br); } catch (IOException | RuntimeException none) { // Ignore -- we'll set and return an empty state } lastModified = mtime; state = newState; } return state; }
@Override public String[] getValues(String key) { List<String> values = Host.this.entry .getValues(mapKey(key)); if (values == null) { return new String[0]; } return values.toArray(new String[0]); } };
public void update(char key, String value) { replacements.put(Character.valueOf(key), value); if ("lhpr".indexOf(key) >= 0) { //$NON-NLS-1$ replacements.put(Character.valueOf('C'), substitute("%l%h%p%r", "hlpr")); //$NON-NLS-1$ //$NON-NLS-2$ } }
private static boolean isHostMatch(String pattern, String name) { if (pattern.startsWith("!")) { //$NON-NLS-1$ return !patternMatchesHost(pattern.substring(1), name); } else { return patternMatchesHost(pattern, name); } }
public Replacer(String host, int port, String user, String localUserName, File home) { replacements.put(Character.valueOf('%'), "%"); //$NON-NLS-1$ replacements.put(Character.valueOf('d'), home.getPath()); replacements.put(Character.valueOf('h'), host); String localhost = SystemReader.getInstance().getHostname(); replacements.put(Character.valueOf('l'), localhost); int period = localhost.indexOf('.'); if (period > 0) { localhost = localhost.substring(0, period); } replacements.put(Character.valueOf('L'), localhost); replacements.put(Character.valueOf('n'), host); replacements.put(Character.valueOf('p'), Integer.toString(port)); replacements.put(Character.valueOf('r'), user == null ? "" : user); //$NON-NLS-1$ replacements.put(Character.valueOf('u'), localUserName); replacements.put(Character.valueOf('C'), substitute("%l%h%p%r", "hlpr")); //$NON-NLS-1$ //$NON-NLS-2$ replacements.put(Character.valueOf('T'), "NONE"); //$NON-NLS-1$ }