1 /* 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <dlfcn.h> 27 #include <sys/socket.h> 28 #include <netinet/in.h> 29 #include <arpa/inet.h> 30 31 #include <Security/AuthSession.h> 32 #include <CoreFoundation/CoreFoundation.h> 33 #include <SystemConfiguration/SystemConfiguration.h> 34 35 #include "java_props_macosx.h" 36 37 38 // need dlopen/dlsym trick to avoid pulling in JavaRuntimeSupport before libjava.dylib is loaded 39 static void *getJRSFramework() { 40 static void *jrsFwk = NULL; 41 if (jrsFwk == NULL) { 42 jrsFwk = dlopen("/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/JavaRuntimeSupport", RTLD_LAZY | RTLD_LOCAL); 43 } 44 return jrsFwk; 45 } 46 47 char *getPosixLocale(int cat) { 48 char *lc = setlocale(cat, NULL); 49 if ((lc == NULL) || (strcmp(lc, "C") == 0)) { 50 lc = getenv("LANG"); 51 } 52 if (lc == NULL) return NULL; 53 return strdup(lc); 54 } 55 56 #define LOCALEIDLENGTH 128 57 char *getMacOSXLocale(int cat) { 58 switch (cat) { 59 case LC_MESSAGES: 60 { 61 void *jrsFwk = getJRSFramework(); 62 if (jrsFwk == NULL) return NULL; 63 64 char *(*JRSCopyPrimaryLanguage)() = dlsym(jrsFwk, "JRSCopyPrimaryLanguage"); 65 char *primaryLanguage = JRSCopyPrimaryLanguage ? JRSCopyPrimaryLanguage() : NULL; 66 if (primaryLanguage == NULL) return NULL; 67 68 char *(*JRSCopyCanonicalLanguageForPrimaryLanguage)(char *) = dlsym(jrsFwk, "JRSCopyCanonicalLanguageForPrimaryLanguage"); 69 char *canonicalLanguage = JRSCopyCanonicalLanguageForPrimaryLanguage ? JRSCopyCanonicalLanguageForPrimaryLanguage(primaryLanguage) : NULL; 70 free (primaryLanguage); 71 72 return canonicalLanguage; 73 } 74 break; 75 default: 76 { 77 char localeString[LOCALEIDLENGTH]; 78 if (CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()), 79 localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) { 80 return strdup(localeString); 81 } 82 } 83 break; 84 } 85 86 return NULL; 87 } 88 89 char *setupMacOSXLocale(int cat) { 90 char * ret = getMacOSXLocale(cat); 91 92 if (cat == LC_MESSAGES && ret != NULL) { 93 void *jrsFwk = getJRSFramework(); 94 if (jrsFwk != NULL) { 95 void (*JRSSetDefaultLocalization)(char *) = dlsym(jrsFwk, "JRSSetDefaultLocalization"); 96 if (JRSSetDefaultLocalization) JRSSetDefaultLocalization(ret); 97 } 98 } 99 100 if (ret == NULL) { 101 return getPosixLocale(cat); 102 } else { 103 return ret; 104 } 105 } 106 107 /* There are several toolkit options on Mac OS X, so we should try to 108 * pick the "best" one, given what we know about the environment Java 109 * is running under 110 */ 111 112 static PreferredToolkit getPreferredToolkitFromEnv() { 113 char *envVar = getenv("AWT_TOOLKIT"); 114 if (envVar == NULL) return unset; 115 116 if (strcasecmp(envVar, "CToolkit") == 0) return CToolkit; 117 if (strcasecmp(envVar, "XToolkit") == 0) return XToolkit; 118 if (strcasecmp(envVar, "HToolkit") == 0) return HToolkit; 119 return unset; 120 } 121 122 static bool isInAquaSession() { 123 // Is the WindowServer available? 124 SecuritySessionId session_id; 125 SessionAttributeBits session_info; 126 OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info); 127 if (status != noErr) return false; 128 if (!(session_info & sessionHasGraphicAccess)) return false; 129 return true; 130 } 131 132 static bool isXDisplayDefined() { 133 return getenv("DISPLAY") != NULL; 134 } 135 136 PreferredToolkit getPreferredToolkit() { 137 static PreferredToolkit pref = unset; 138 if (pref != unset) return pref; 139 140 PreferredToolkit prefFromEnv = getPreferredToolkitFromEnv(); 141 if (prefFromEnv != unset) return pref = prefFromEnv; 142 143 if (isInAquaSession()) return pref = CToolkit; 144 if (isXDisplayDefined()) return pref = XToolkit; 145 return pref = HToolkit; 146 } 147 148 void setOSNameAndVersion(java_props_t *sprops) { 149 /* Don't rely on JRSCopyOSName because there's no guarantee the value will 150 * remain the same, or even if the JRS functions will continue to be part of 151 * Mac OS X. So hardcode os_name, and fill in os_version if we can. 152 */ 153 sprops->os_name = strdup("Mac OS X"); 154 155 void *jrsFwk = getJRSFramework(); 156 if (jrsFwk != NULL) { 157 char *(*copyOSVersion)() = dlsym(jrsFwk, "JRSCopyOSVersion"); 158 if (copyOSVersion != NULL) { 159 sprops->os_version = copyOSVersion(); 160 return; 161 } 162 } 163 sprops->os_version = strdup("Unknown"); 164 } 165 166 167 static Boolean getProxyInfoForProtocol(CFDictionaryRef inDict, CFStringRef inEnabledKey, CFStringRef inHostKey, CFStringRef inPortKey, CFStringRef *outProxyHost, int *ioProxyPort) { 168 /* See if the proxy is enabled. */ 169 CFNumberRef cf_enabled = CFDictionaryGetValue(inDict, inEnabledKey); 170 if (cf_enabled == NULL) { 171 return false; 172 } 173 174 int isEnabled = false; 175 if (!CFNumberGetValue(cf_enabled, kCFNumberIntType, &isEnabled)) { 176 return isEnabled; 177 } 178 179 if (!isEnabled) return false; 180 *outProxyHost = CFDictionaryGetValue(inDict, inHostKey); 181 182 // If cf_host is null, that means the checkbox is set, 183 // but no host was entered. We'll treat that as NOT ENABLED. 184 // If cf_port is null or cf_port isn't a number, that means 185 // no port number was entered. Treat this as ENABLED with the 186 // protocol's default port. 187 if (*outProxyHost == NULL) { 188 return false; 189 } 190 191 if (CFStringGetLength(*outProxyHost) == 0) { 192 return false; 193 } 194 195 int newPort = 0; 196 CFNumberRef cf_port = NULL; 197 if ((cf_port = CFDictionaryGetValue(inDict, inPortKey)) != NULL && 198 CFNumberGetValue(cf_port, kCFNumberIntType, &newPort) && 199 newPort > 0) { 200 *ioProxyPort = newPort; 201 } else { 202 // bad port or no port - leave *ioProxyPort unchanged 203 } 204 205 return true; 206 } 207 208 static char *createUTF8CString(const CFStringRef theString) { 209 if (theString == NULL) return NULL; 210 211 const CFIndex stringLength = CFStringGetLength(theString); 212 const CFIndex bufSize = CFStringGetMaximumSizeForEncoding(stringLength, kCFStringEncodingUTF8) + 1; 213 char *returnVal = (char *)malloc(bufSize); 214 215 if (CFStringGetCString(theString, returnVal, bufSize, kCFStringEncodingUTF8)) { 216 return returnVal; 217 } 218 219 free(returnVal); 220 return NULL; 221 } 222 223 // Return TRUE if str is a syntactically valid IP address. 224 // Using inet_pton() instead of inet_aton() for IPv6 support. 225 // len is only a hint; cstr must still be nul-terminated 226 static int looksLikeIPAddress(char *cstr, size_t len) { 227 if (len == 0 || (len == 1 && cstr[0] == '.')) return FALSE; 228 229 char dst[16]; // big enough for INET6 230 return (1 == inet_pton(AF_INET, cstr, dst) || 231 1 == inet_pton(AF_INET6, cstr, dst)); 232 } 233 234 235 236 // Convert Mac OS X proxy exception entry to Java syntax. 237 // See Radar #3441134 for details. 238 // Returns NULL if this exception should be ignored by Java. 239 // May generate a string with multiple exceptions separated by '|'. 240 static char * createConvertedException(CFStringRef cf_original) { 241 // This is done with char* instead of CFString because inet_pton() 242 // needs a C string. 243 char *c_exception = createUTF8CString(cf_original); 244 if (!c_exception) return NULL; 245 246 int c_len = strlen(c_exception); 247 248 // 1. sanitize exception prefix 249 if (c_len >= 1 && 0 == strncmp(c_exception, ".", 1)) { 250 memmove(c_exception, c_exception+1, c_len); 251 c_len -= 1; 252 } else if (c_len >= 2 && 0 == strncmp(c_exception, "*.", 2)) { 253 memmove(c_exception, c_exception+2, c_len-1); 254 c_len -= 2; 255 } 256 257 // 2. pre-reject other exception wildcards 258 if (strchr(c_exception, '*')) { 259 free(c_exception); 260 return NULL; 261 } 262 263 // 3. no IP wildcarding 264 if (looksLikeIPAddress(c_exception, c_len)) { 265 return c_exception; 266 } 267 268 // 4. allow domain suffixes 269 // c_exception is now "str\0" - change to "str|*.str\0" 270 c_exception = reallocf(c_exception, c_len+3+c_len+1); 271 if (!c_exception) return NULL; 272 273 strncpy(c_exception+c_len, "|*.", 3); 274 strncpy(c_exception+c_len+3, c_exception, c_len); 275 c_exception[c_len+3+c_len] = '\0'; 276 return c_exception; 277 } 278 279 280 /* 281 * Method for fetching proxy info and storing it in the propery list. 282 */ 283 void setProxyProperties(java_props_t *sProps) { 284 if (sProps == NULL) return; 285 286 char buf[16]; /* Used for %d of an int - 16 is plenty */ 287 CFStringRef 288 cf_httpHost = NULL, 289 cf_httpsHost = NULL, 290 cf_ftpHost = NULL, 291 cf_socksHost = NULL, 292 cf_gopherHost = NULL; 293 int 294 httpPort = 80, // Default proxy port values 295 httpsPort = 443, 296 ftpPort = 21, 297 socksPort = 1080, 298 gopherPort = 70; 299 300 CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL); 301 if (dict == NULL) return; 302 303 /* Read the proxy exceptions list */ 304 CFArrayRef cf_list = CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList); 305 306 CFMutableStringRef cf_exceptionList = NULL; 307 if (cf_list != NULL) { 308 CFIndex len = CFArrayGetCount(cf_list), idx; 309 310 cf_exceptionList = CFStringCreateMutable(NULL, 0); 311 for (idx = (CFIndex)0; idx < len; idx++) { 312 CFStringRef cf_ehost; 313 if ((cf_ehost = CFArrayGetValueAtIndex(cf_list, idx))) { 314 /* Convert this exception from Mac OS X syntax to Java syntax. 315 See Radar #3441134 for details. This may generate a string 316 with multiple Java exceptions separated by '|'. */ 317 char *c_exception = createConvertedException(cf_ehost); 318 if (c_exception) { 319 /* Append the host to the list of exclusions. */ 320 if (CFStringGetLength(cf_exceptionList) > 0) { 321 CFStringAppendCString(cf_exceptionList, "|", kCFStringEncodingMacRoman); 322 } 323 CFStringAppendCString(cf_exceptionList, c_exception, kCFStringEncodingMacRoman); 324 free(c_exception); 325 } 326 } 327 } 328 } 329 330 if (cf_exceptionList != NULL) { 331 if (CFStringGetLength(cf_exceptionList) > 0) { 332 sProps->exceptionList = createUTF8CString(cf_exceptionList); 333 } 334 CFRelease(cf_exceptionList); 335 } 336 337 #define CHECK_PROXY(protocol, PROTOCOL) \ 338 sProps->protocol##ProxyEnabled = \ 339 getProxyInfoForProtocol(dict, kSCPropNetProxies##PROTOCOL##Enable, \ 340 kSCPropNetProxies##PROTOCOL##Proxy, \ 341 kSCPropNetProxies##PROTOCOL##Port, \ 342 &cf_##protocol##Host, &protocol##Port); \ 343 if (sProps->protocol##ProxyEnabled) { \ 344 sProps->protocol##Host = createUTF8CString(cf_##protocol##Host); \ 345 snprintf(buf, sizeof(buf), "%d", protocol##Port); \ 346 sProps->protocol##Port = malloc(strlen(buf) + 1); \ 347 strcpy(sProps->protocol##Port, buf); \ 348 } 349 350 CHECK_PROXY(http, HTTP); 351 CHECK_PROXY(https, HTTPS); 352 CHECK_PROXY(ftp, FTP); 353 CHECK_PROXY(socks, SOCKS); 354 CHECK_PROXY(gopher, Gopher); 355 356 #undef CHECK_PROXY 357 358 CFRelease(dict); 359 }