1 /* 2 * Copyright (c) 1998, 2011, 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 static 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 *setupMacOSXLocale(int cat) { 58 switch (cat) { 59 case LC_MESSAGES: 60 { 61 void *jrsFwk = getJRSFramework(); 62 if (jrsFwk == NULL) return getPosixLocale(cat); 63 64 char *(*JRSCopyPrimaryLanguage)() = dlsym(jrsFwk, "JRSCopyPrimaryLanguage"); 65 char *primaryLanguage = JRSCopyPrimaryLanguage ? JRSCopyPrimaryLanguage() : NULL; 66 if (primaryLanguage == NULL) return getPosixLocale(cat); 67 68 char *(*JRSCopyCanonicalLanguageForPrimaryLanguage)(char *) = dlsym(jrsFwk, "JRSCopyCanonicalLanguageForPrimaryLanguage"); 69 char *canonicalLanguage = JRSCopyCanonicalLanguageForPrimaryLanguage ? JRSCopyCanonicalLanguageForPrimaryLanguage(primaryLanguage) : NULL; 70 free (primaryLanguage); 71 if (canonicalLanguage == NULL) return getPosixLocale(cat); 72 73 void (*JRSSetDefaultLocalization)(char *) = dlsym(jrsFwk, "JRSSetDefaultLocalization"); 74 if (JRSSetDefaultLocalization) JRSSetDefaultLocalization(canonicalLanguage); 75 76 return canonicalLanguage; 77 } 78 break; 79 default: 80 { 81 char localeString[LOCALEIDLENGTH]; 82 if (CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()), 83 localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) { 84 return strdup(localeString); 85 } 86 } 87 break; 88 } 89 90 return NULL; 91 } 92 93 /* There are several toolkit options on Mac OS X, so we should try to 94 * pick the "best" one, given what we know about the environment Java 95 * is running under 96 */ 97 98 static PreferredToolkit getPreferredToolkitFromEnv() { 99 char *envVar = getenv("AWT_TOOLKIT"); 100 if (envVar == NULL) return unset; 101 102 if (strcasecmp(envVar, "CToolkit") == 0) return CToolkit; 103 if (strcasecmp(envVar, "XToolkit") == 0) return XToolkit; 104 if (strcasecmp(envVar, "HToolkit") == 0) return HToolkit; 105 return unset; 106 } 107 108 static bool isInAquaSession() { 109 // Is the WindowServer available? 110 SecuritySessionId session_id; 111 SessionAttributeBits session_info; 112 OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info); 113 if (status != noErr) return false; 114 if (!(session_info & sessionHasGraphicAccess)) return false; 115 return true; 116 } 117 118 PreferredToolkit getPreferredToolkit() { 119 static PreferredToolkit pref = unset; 120 if (pref != unset) return pref; 121 122 PreferredToolkit prefFromEnv = getPreferredToolkitFromEnv(); 123 if (prefFromEnv != unset) return pref = prefFromEnv; 124 125 if (isInAquaSession()) return pref = CToolkit; 126 return pref = HToolkit; 127 } 128 129 void setUnknownOSAndVersion(java_props_t *sprops) { 130 sprops->os_name = strdup("Unknown"); 131 sprops->os_version = strdup("Unknown"); 132 } 133 134 void setOSNameAndVersion(java_props_t *sprops) { 135 void *jrsFwk = getJRSFramework(); 136 if (jrsFwk == NULL) { 137 setUnknownOSAndVersion(sprops); 138 return; 139 } 140 141 char *(*copyOSName)() = dlsym(jrsFwk, "JRSCopyOSName"); 142 char *(*copyOSVersion)() = dlsym(jrsFwk, "JRSCopyOSVersion"); 143 if (copyOSName == NULL || copyOSVersion == NULL) { 144 setUnknownOSAndVersion(sprops); 145 return; 146 } 147 148 sprops->os_name = copyOSName(); 149 sprops->os_version = copyOSVersion(); 150 } 151 152 153 static Boolean getProxyInfoForProtocol(CFDictionaryRef inDict, CFStringRef inEnabledKey, CFStringRef inHostKey, CFStringRef inPortKey, CFStringRef *outProxyHost, int *ioProxyPort) { 154 /* See if the proxy is enabled. */ 155 CFNumberRef cf_enabled = CFDictionaryGetValue(inDict, inEnabledKey); 156 if (cf_enabled == NULL) { 157 return false; 158 } 159 160 int isEnabled = false; 161 if (!CFNumberGetValue(cf_enabled, kCFNumberIntType, &isEnabled)) { 162 return isEnabled; 163 } 164 165 if (!isEnabled) return false; 166 *outProxyHost = CFDictionaryGetValue(inDict, inHostKey); 167 168 // If cf_host is null, that means the checkbox is set, 169 // but no host was entered. We'll treat that as NOT ENABLED. 170 // If cf_port is null or cf_port isn't a number, that means 171 // no port number was entered. Treat this as ENABLED with the 172 // protocol's default port. 173 if (*outProxyHost == NULL) { 174 return false; 175 } 176 177 if (CFStringGetLength(*outProxyHost) == 0) { 178 return false; 179 } 180 181 int newPort = 0; 182 CFNumberRef cf_port = NULL; 183 if ((cf_port = CFDictionaryGetValue(inDict, inPortKey)) != NULL && 184 CFNumberGetValue(cf_port, kCFNumberIntType, &newPort) && 185 newPort > 0) { 186 *ioProxyPort = newPort; 187 } else { 188 // bad port or no port - leave *ioProxyPort unchanged 189 } 190 191 return true; 192 } 193 194 static char *createUTF8CString(const CFStringRef theString) { 195 if (theString == NULL) return NULL; 196 197 const CFIndex stringLength = CFStringGetLength(theString); 198 const CFIndex bufSize = CFStringGetMaximumSizeForEncoding(stringLength, kCFStringEncodingUTF8) + 1; 199 char *returnVal = (char *)malloc(bufSize); 200 201 if (CFStringGetCString(theString, returnVal, bufSize, kCFStringEncodingUTF8)) { 202 return returnVal; 203 } 204 205 free(returnVal); 206 return NULL; 207 } 208 209 // Return TRUE if str is a syntactically valid IP address. 210 // Using inet_pton() instead of inet_aton() for IPv6 support. 211 // len is only a hint; cstr must still be nul-terminated 212 static int looksLikeIPAddress(char *cstr, size_t len) { 213 if (len == 0 || (len == 1 && cstr[0] == '.')) return FALSE; 214 215 char dst[16]; // big enough for INET6 216 return (1 == inet_pton(AF_INET, cstr, dst) || 217 1 == inet_pton(AF_INET6, cstr, dst)); 218 } 219 220 221 222 // Convert Mac OS X proxy exception entry to Java syntax. 223 // See Radar #3441134 for details. 224 // Returns NULL if this exception should be ignored by Java. 225 // May generate a string with multiple exceptions separated by '|'. 226 static char * createConvertedException(CFStringRef cf_original) { 227 // This is done with char* instead of CFString because inet_pton() 228 // needs a C string. 229 char *c_exception = createUTF8CString(cf_original); 230 if (!c_exception) return NULL; 231 232 int c_len = strlen(c_exception); 233 234 // 1. sanitize exception prefix 235 if (c_len >= 1 && 0 == strncmp(c_exception, ".", 1)) { 236 memmove(c_exception, c_exception+1, c_len); 237 c_len -= 1; 238 } else if (c_len >= 2 && 0 == strncmp(c_exception, "*.", 2)) { 239 memmove(c_exception, c_exception+2, c_len-1); 240 c_len -= 2; 241 } 242 243 // 2. pre-reject other exception wildcards 244 if (strchr(c_exception, '*')) { 245 free(c_exception); 246 return NULL; 247 } 248 249 // 3. no IP wildcarding 250 if (looksLikeIPAddress(c_exception, c_len)) { 251 return c_exception; 252 } 253 254 // 4. allow domain suffixes 255 // c_exception is now "str\0" - change to "str|*.str\0" 256 c_exception = reallocf(c_exception, c_len+3+c_len+1); 257 if (!c_exception) return NULL; 258 259 strncpy(c_exception+c_len, "|*.", 3); 260 strncpy(c_exception+c_len+3, c_exception, c_len); 261 c_exception[c_len+3+c_len] = '\0'; 262 return c_exception; 263 } 264 265 266 /* 267 * Method for fetching proxy info and storing it in the propery list. 268 */ 269 void setProxyProperties(java_props_t *sProps) { 270 if (sProps == NULL) return; 271 272 char buf[16]; /* Used for %d of an int - 16 is plenty */ 273 CFStringRef 274 cf_httpHost = NULL, 275 cf_httpsHost = NULL, 276 cf_ftpHost = NULL, 277 cf_socksHost = NULL, 278 cf_gopherHost = NULL; 279 int 280 httpPort = 80, // Default proxy port values 281 httpsPort = 443, 282 ftpPort = 21, 283 socksPort = 1080, 284 gopherPort = 70; 285 286 CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL); 287 if (dict == NULL) return; 288 289 /* Read the proxy exceptions list */ 290 CFArrayRef cf_list = CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList); 291 292 CFMutableStringRef cf_exceptionList = NULL; 293 if (cf_list != NULL) { 294 CFIndex len = CFArrayGetCount(cf_list), idx; 295 296 cf_exceptionList = CFStringCreateMutable(NULL, 0); 297 for (idx = (CFIndex)0; idx < len; idx++) { 298 CFStringRef cf_ehost; 299 if ((cf_ehost = CFArrayGetValueAtIndex(cf_list, idx))) { 300 /* Convert this exception from Mac OS X syntax to Java syntax. 301 See Radar #3441134 for details. This may generate a string 302 with multiple Java exceptions separated by '|'. */ 303 char *c_exception = createConvertedException(cf_ehost); 304 if (c_exception) { 305 /* Append the host to the list of exclusions. */ 306 if (CFStringGetLength(cf_exceptionList) > 0) { 307 CFStringAppendCString(cf_exceptionList, "|", kCFStringEncodingMacRoman); 308 } 309 CFStringAppendCString(cf_exceptionList, c_exception, kCFStringEncodingMacRoman); 310 free(c_exception); 311 } 312 } 313 } 314 } 315 316 if (cf_exceptionList != NULL) { 317 if (CFStringGetLength(cf_exceptionList) > 0) { 318 sProps->exceptionList = createUTF8CString(cf_exceptionList); 319 } 320 CFRelease(cf_exceptionList); 321 } 322 323 #define CHECK_PROXY(protocol, PROTOCOL) \ 324 sProps->protocol##ProxyEnabled = \ 325 getProxyInfoForProtocol(dict, kSCPropNetProxies##PROTOCOL##Enable, \ 326 kSCPropNetProxies##PROTOCOL##Proxy, \ 327 kSCPropNetProxies##PROTOCOL##Port, \ 328 &cf_##protocol##Host, &protocol##Port); \ 329 if (sProps->protocol##ProxyEnabled) { \ 330 sProps->protocol##Host = createUTF8CString(cf_##protocol##Host); \ 331 snprintf(buf, sizeof(buf), "%d", protocol##Port); \ 332 sProps->protocol##Port = malloc(strlen(buf) + 1); \ 333 strcpy(sProps->protocol##Port, buf); \ 334 } 335 336 CHECK_PROXY(http, HTTP); 337 CHECK_PROXY(https, HTTPS); 338 CHECK_PROXY(ftp, FTP); 339 CHECK_PROXY(socks, SOCKS); 340 CHECK_PROXY(gopher, Gopher); 341 342 #undef CHECK_PROXY 343 344 CFRelease(dict); 345 }