1 19 20 package com.sslexplorer.activedirectory; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileReader ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import com.sslexplorer.boot.ContextHolder; 36 import com.sslexplorer.boot.PropertyList; 37 import com.sslexplorer.properties.Property; 38 import com.sslexplorer.properties.impl.realms.RealmKey; 39 import com.sslexplorer.realms.Realm; 40 41 final class ActiveDirectoryPropertyManager { 42 43 private static final Log log = LogFactory.getLog(ActiveDirectoryPropertyManager.class); 44 private static final String TEMPLATE_FILE = "krb5.template"; 45 private static final String CONF_DIRECTORY = "activedirectory"; 46 private static final String CONF_FILE = "krb5.conf"; 47 48 private static final String PORT_SEPARATOR = ":"; 49 private static final String START_REPLACEMENT = "${"; 50 private static final String END_REPLACEMENT = "}"; 51 52 private static final String DOMAIN = "DOMAIN"; 53 private static final String KDC_TIMEOUT = "KDC_TIMEOUT"; 54 private static final String KDC_MAX_TRIES = "KDC_MAX_RETRIES"; 55 private static final String HOST_REALMS = "HOST_REALMS"; 56 private static final String HOST_NAME = "HOST_NAME"; 57 private static final String HOST_REALM_REPLACEMENT = "kdc = " + START_REPLACEMENT + HOST_NAME + END_REPLACEMENT; 58 private static final String DOMAIN_REALMS = "DOMAIN_REALMS"; 59 private static final String DOMAIN_REALM_REPLACEMENT = "." + START_REPLACEMENT + HOST_NAME + END_REPLACEMENT + " = " + START_REPLACEMENT + DOMAIN + END_REPLACEMENT; 60 61 private final Realm realm; 62 63 ActiveDirectoryPropertyManager(Realm realm) { 64 this.realm = realm; 65 System.setProperty("java.security.krb5.conf", getConfFile()); 66 } 67 68 private static String getConfFile() { 69 File tempDirectory = ContextHolder.getContext().getTempDirectory(); 70 File configurationFile = new File (tempDirectory, CONF_FILE); 71 return configurationFile.getAbsolutePath(); 72 } 73 74 void refresh() { 75 refresh(Collections.<String , String >emptyMap()); 76 } 77 78 void refresh(Map <String , String > alternativeValues) { 79 try { 80 doFileReplacement(alternativeValues); 81 } catch (IOException e) { 82 log.error("Failed to update Active Directory configuration " + CONF_FILE, e); 83 } 84 } 85 86 private void doFileReplacement(Map <String , String > alternativeValues) throws IOException { 87 File confDirectory = ContextHolder.getContext().getConfDirectory(); 88 File templateFile = new File (confDirectory, CONF_DIRECTORY + getFileSeparator() + TEMPLATE_FILE); 89 String readFile = readFile(templateFile); 90 91 File tempDirectory = ContextHolder.getContext().getTempDirectory(); 92 File configurationFile = new File (tempDirectory, CONF_FILE); 93 if (!configurationFile.exists() && !configurationFile.createNewFile()) { 94 log.error("Failed to create file " + CONF_FILE + "."); 95 } else { 96 String replacement = getReplacement(readFile, alternativeValues); 97 writeFile(configurationFile, replacement); 98 } 99 } 100 101 private String getReplacement(String fileContents, Map <String , String > alternativeValues) { 102 Map <String , String > replacements = buildReplacements(alternativeValues); 103 for (Map.Entry <String , String > entry : replacements.entrySet()) { 104 fileContents = replaceValue(fileContents, entry.getKey(), entry.getValue()); 105 } 106 return fileContents; 107 } 108 109 private Map <String , String > buildReplacements(Map <String , String > alternativeValues) { 110 Map <String , String > replacements = new HashMap <String , String >(); 111 112 String dbDomain = Property.getProperty(getRealmKey("activeDirectory.domain")).toUpperCase().trim(); 113 String domain = getRealValue(alternativeValues, DOMAIN, dbDomain); 114 replacements.put(DOMAIN, domain); 115 116 String dbControllerHost = Property.getProperty(getRealmKey("activeDirectory.controllerHost")); 117 String controllerHost = getRealValue(alternativeValues, "activeDirectory.controllerHost", dbControllerHost); 118 119 String dbTimeout = String.valueOf(Property.getPropertyInt(getRealmKey("activeDirectory.kdcTimeout")) * 1000); 120 String timeout = getRealValue(alternativeValues, "activeDirectory.kdcTimeout", dbTimeout); 121 replacements.put(KDC_TIMEOUT, timeout); 122 123 String dbMaxTries = String.valueOf(Property.getPropertyInt(getRealmKey("activeDirectory.kdcMaxTries"))); 124 String maxTries = getRealValue(alternativeValues, "activeDirectory.kdcMaxTries", dbMaxTries); 125 replacements.put(KDC_MAX_TRIES, maxTries); 126 127 PropertyList dbActiveDirectryUris = new PropertyList(); 128 dbActiveDirectryUris.add(controllerHost); 129 dbActiveDirectryUris.addAll(Property.getPropertyList(getRealmKey("activeDirectory.backupControllerHosts"))); 130 PropertyList activeDirectryUris = getRealValue(alternativeValues,"activeDirectory.backupControllerHosts", dbActiveDirectryUris); 131 replacements.put(HOST_REALMS, buildBackupHostRealms(activeDirectryUris)); 132 replacements.put(DOMAIN_REALMS, buildBackupDomainRealms(domain, activeDirectryUris)); 133 return replacements; 134 } 135 136 private static String getRealValue(Map <String , String > alternativeValues, String key, String value) { 137 return alternativeValues.containsKey(key) ? alternativeValues.get(key): value; 138 } 139 140 private static PropertyList getRealValue(Map <String , String > alternativeValues, String key, PropertyList values) { 141 return alternativeValues.containsKey(key) ? new PropertyList (alternativeValues.get(key) ): values; 142 } 143 144 private static String replaceValue(String contents, String key, String value) { 145 key = START_REPLACEMENT + key + END_REPLACEMENT; 146 return contents.replace(key, value); 147 } 148 149 private static String buildBackupHostRealms(PropertyList activeDirectryUris) { 150 StringBuffer buffer = new StringBuffer (); 151 for (String uri : activeDirectryUris) { 152 uri = uri.contains(PORT_SEPARATOR) ? uri.substring(0, uri.lastIndexOf(PORT_SEPARATOR)) : uri; 153 String replace = replaceValue(HOST_REALM_REPLACEMENT, HOST_NAME, uri); 154 buffer.append(replace).append(getLineSeparator()); 155 } 156 return buffer.toString(); 157 } 158 159 private static String buildBackupDomainRealms(String domain, PropertyList activeDirectryUris) { 160 StringBuffer buffer = new StringBuffer (); 161 for (String uri : activeDirectryUris) { 162 uri = uri.contains(PORT_SEPARATOR) ? uri.substring(0, uri.lastIndexOf(PORT_SEPARATOR)) : uri; 163 String replace = replaceValue(DOMAIN_REALM_REPLACEMENT, HOST_NAME, uri); 164 replace = replaceValue(replace, DOMAIN, domain); 165 buffer.append(replace).append(getLineSeparator()); 166 } 167 return buffer.toString(); 168 } 169 170 private RealmKey getRealmKey(String name) { 171 return new RealmKey(name, realm); 172 } 173 174 private static void writeFile(File file, String contents) throws IOException { 175 BufferedWriter output = null; 176 try { 177 output = new BufferedWriter (new FileWriter (file)); 178 output.write(contents); 179 } finally { 180 close(output); 181 } 182 } 183 184 private static void close(BufferedWriter writer) { 185 try { 186 if (writer != null) { 187 writer.close(); 188 } 189 } catch (IOException e) { 190 } 192 } 193 194 private static String readFile(File file) throws IOException { 195 StringBuffer contents = new StringBuffer (); 196 BufferedReader input = null; 197 try { 198 input = new BufferedReader (new FileReader (file)); 199 String line = null; 200 while ((line = input.readLine()) != null) { 201 contents.append(line); 202 contents.append(getLineSeparator()); 203 } 204 } finally { 205 close(input); 206 } 207 return contents.toString(); 208 } 209 210 private static String getFileSeparator() { 211 return System.getProperty("file.separator"); 212 } 213 214 private static String getLineSeparator() { 215 return System.getProperty("line.separator"); 216 } 217 218 private static void close(BufferedReader reader) { 219 try { 220 if (reader != null) { 221 reader.close(); 222 } 223 } catch (IOException e) { 224 } 226 } 227 } | Popular Tags |