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