1 /* 2 * Copyright (c) 1998, 2017, 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 <sys/socket.h> 27 #include <netinet/in.h> 28 #include <arpa/inet.h> 29 #include <objc/objc-runtime.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 char *getPosixLocale(int cat) { 39 char *lc = setlocale(cat, NULL); 40 if ((lc == NULL) || (strcmp(lc, "C") == 0)) { 41 lc = getenv("LANG"); 42 } 43 if (lc == NULL) return NULL; 44 return strdup(lc); 45 } 46 47 #define LOCALEIDLENGTH 128 48 char *getMacOSXLocale(int cat) { 49 const char* retVal = NULL; 50 char localeString[LOCALEIDLENGTH]; 51 52 switch (cat) { 53 case LC_MESSAGES: 54 { 55 // get preferred language code 56 CFArrayRef languages = CFLocaleCopyPreferredLanguages(); 57 if (languages == NULL) { 58 return NULL; 59 } 60 if (CFArrayGetCount(languages) <= 0) { 61 CFRelease(languages); 62 return NULL; 63 } 64 65 CFStringRef primaryLanguage = (CFStringRef)CFArrayGetValueAtIndex(languages, 0); 66 if (primaryLanguage == NULL) { 67 CFRelease(languages); 68 return NULL; 69 } 70 char languageString[LOCALEIDLENGTH]; 71 if (CFStringGetCString(primaryLanguage, languageString, 72 LOCALEIDLENGTH, CFStringGetSystemEncoding()) == false) { 73 CFRelease(languages); 74 return NULL; 75 } 76 CFRelease(languages); 77 78 // Explicitly supply region, if there is none 79 char *hyphenPos = strchr(languageString, '-'); 80 int langStrLen = strlen(languageString); 81 82 if (hyphenPos == NULL || // languageString contains ISO639 only, e.g., "en" 83 languageString + langStrLen - hyphenPos == 5) { // ISO639-ScriptCode, e.g., "en-Latn" 84 CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()), 85 localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding()); 86 char *underscorePos = strrchr(localeString, '_'); 87 char *region = NULL; 88 89 if (underscorePos != NULL) { 90 region = underscorePos + 1; 91 } 92 93 if (region != NULL) { 94 strcat(languageString, "-"); 95 strcat(languageString, region); 96 } 97 } 98 99 retVal = languageString; 100 } 101 break; 102 103 default: 104 { 105 if (!CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()), 106 localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) { 107 return NULL; 108 } 109 110 retVal = localeString; 111 } 112 break; 113 } 114 115 if (retVal != NULL) { 116 return strdup(convertToPOSIXLocale(retVal)); 117 } 118 119 return NULL; 120 } 121 122 /* Language IDs use the language designators and (optional) region 123 * and script designators of BCP 47. So possible formats are: 124 * 125 * "en" (language designator only) 126 * "haw" (3-letter lanuage designator) 127 * "en-GB" (language with alpha-2 region designator) 128 * "es-419" (language with 3-digit UN M.49 area code) 129 * "zh-Hans" (language with ISO 15924 script designator) 130 * "zh-Hans-US" (language with ISO 15924 script designator and region) 131 * "zh-Hans-419" (language with ISO 15924 script designator and UN M.49) 132 * 133 * convert these tags into POSIX conforming locale string, i.e., 134 * lang{_region}{@script}. e.g., for "zh-Hans-US" into "zh_US@Hans" 135 */ 136 const char * convertToPOSIXLocale(const char* src) { 137 char* scriptRegion = strchr(src, '-'); 138 if (scriptRegion != NULL) { 139 int length = strlen(scriptRegion); 140 char* region = strchr(scriptRegion + 1, '-'); 141 char* atMark = NULL; 142 143 if (region == NULL) { 144 // CFLocaleGetIdentifier() returns '_' before region 145 region = strchr(scriptRegion + 1, '_'); 146 } 147 148 *scriptRegion = '_'; 149 if (length > 5) { 150 // Region and script both exist. 151 char tmpScript[4]; 152 int regionLength = length - 6; 153 atMark = scriptRegion + 1 + regionLength; 154 memcpy(tmpScript, scriptRegion + 1, 4); 155 memmove(scriptRegion + 1, region + 1, regionLength); 156 memcpy(atMark + 1, tmpScript, 4); 157 } else if (length == 5) { 158 // script only 159 atMark = scriptRegion; 160 } 161 162 if (atMark != NULL) { 163 *atMark = '@'; 164 165 // assert script code 166 assert(isalpha(atMark[1]) && 167 isalpha(atMark[2]) && 168 isalpha(atMark[3]) && 169 isalpha(atMark[4])); 170 } 171 172 assert(((length == 3 || length == 8) && 173 // '_' followed by a 2 character region designator 174 isalpha(scriptRegion[1]) && 175 isalpha(scriptRegion[2])) || 176 ((length == 4 || length == 9) && 177 // '_' followed by a 3-digit UN M.49 area code 178 isdigit(scriptRegion[1]) && 179 isdigit(scriptRegion[2]) && 180 isdigit(scriptRegion[3])) || 181 // '@' followed by a 4 character script code (already validated above) 182 (length == 5)); 183 } 184 185 return src; 186 } 187 188 char *setupMacOSXLocale(int cat) { 189 char * ret = getMacOSXLocale(cat); 190 191 if (ret == NULL) { 192 return getPosixLocale(cat); 193 } else { 194 return ret; 195 } 196 } 197 198 int isInAquaSession() { 199 // environment variable to bypass the aqua session check 200 char *ev = getenv("AWT_FORCE_HEADFUL"); 201 if (ev && (strncasecmp(ev, "true", 4) == 0)) { 202 // if "true" then tell the caller we're in an Aqua session without actually checking 203 return 1; 204 } 205 // Is the WindowServer available? 206 SecuritySessionId session_id; 207 SessionAttributeBits session_info; 208 OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info); 209 if (status == noErr) { 210 if (session_info & sessionHasGraphicAccess) { 211 return 1; 212 } 213 } 214 return 0; 215 } 216 217 // 10.9 SDK does not include the NSOperatingSystemVersion struct. 218 // For now, create our own 219 typedef struct { 220 NSInteger majorVersion; 221 NSInteger minorVersion; 222 NSInteger patchVersion; 223 } OSVerStruct; 224 225 void setOSNameAndVersion(java_props_t *sprops) { 226 // Hardcode os_name, and fill in os_version 227 sprops->os_name = strdup("Mac OS X"); 228 229 char* osVersionCStr = NULL; 230 // Mac OS 10.9 includes the [NSProcessInfo operatingSystemVersion] function, 231 // but it's not in the 10.9 SDK. So, call it via objc_msgSend_stret. 232 if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) { 233 OSVerStruct (*procInfoFn)(id rec, SEL sel) = (OSVerStruct(*)(id, SEL))objc_msgSend_stret; 234 OSVerStruct osVer = procInfoFn([NSProcessInfo processInfo], 235 @selector(operatingSystemVersion)); 236 NSString *nsVerStr; 237 if (osVer.patchVersion == 0) { // Omit trailing ".0" 238 nsVerStr = [NSString stringWithFormat:@"%ld.%ld", 239 (long)osVer.majorVersion, (long)osVer.minorVersion]; 240 } else { 241 nsVerStr = [NSString stringWithFormat:@"%ld.%ld.%ld", 242 (long)osVer.majorVersion, (long)osVer.minorVersion, (long)osVer.patchVersion]; 243 } 244 // Copy out the char* 245 osVersionCStr = strdup([nsVerStr UTF8String]); 246 } 247 // Fallback if running on pre-10.9 Mac OS 248 if (osVersionCStr == NULL) { 249 NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile : 250 @"/System/Library/CoreServices/SystemVersion.plist"]; 251 if (version != NULL) { 252 NSString *nsVerStr = [version objectForKey : @"ProductVersion"]; 253 if (nsVerStr != NULL) { 254 osVersionCStr = strdup([nsVerStr UTF8String]); 255 } 256 } 257 } 258 if (osVersionCStr == NULL) { 259 osVersionCStr = strdup("Unknown"); 260 } 261 sprops->os_version = osVersionCStr; 262 } 263 264 265 static Boolean getProxyInfoForProtocol(CFDictionaryRef inDict, CFStringRef inEnabledKey, 266 CFStringRef inHostKey, CFStringRef inPortKey, 267 CFStringRef *outProxyHost, int *ioProxyPort) { 268 /* See if the proxy is enabled. */ 269 CFNumberRef cf_enabled = CFDictionaryGetValue(inDict, inEnabledKey); 270 if (cf_enabled == NULL) { 271 return false; 272 } 273 274 int isEnabled = false; 275 if (!CFNumberGetValue(cf_enabled, kCFNumberIntType, &isEnabled)) { 276 return isEnabled; 277 } 278 279 if (!isEnabled) return false; 280 *outProxyHost = CFDictionaryGetValue(inDict, inHostKey); 281 282 // If cf_host is null, that means the checkbox is set, 283 // but no host was entered. We'll treat that as NOT ENABLED. 284 // If cf_port is null or cf_port isn't a number, that means 285 // no port number was entered. Treat this as ENABLED with the 286 // protocol's default port. 287 if (*outProxyHost == NULL) { 288 return false; 289 } 290 291 if (CFStringGetLength(*outProxyHost) == 0) { 292 return false; 293 } 294 295 int newPort = 0; 296 CFNumberRef cf_port = NULL; 297 if ((cf_port = CFDictionaryGetValue(inDict, inPortKey)) != NULL && 298 CFNumberGetValue(cf_port, kCFNumberIntType, &newPort) && 299 newPort > 0) { 300 *ioProxyPort = newPort; 301 } else { 302 // bad port or no port - leave *ioProxyPort unchanged 303 } 304 305 return true; 306 } 307 308 static char *createUTF8CString(const CFStringRef theString) { 309 if (theString == NULL) return NULL; 310 311 const CFIndex stringLength = CFStringGetLength(theString); 312 const CFIndex bufSize = CFStringGetMaximumSizeForEncoding(stringLength, kCFStringEncodingUTF8) + 1; 313 char *returnVal = (char *)malloc(bufSize); 314 315 if (CFStringGetCString(theString, returnVal, bufSize, kCFStringEncodingUTF8)) { 316 return returnVal; 317 } 318 319 free(returnVal); 320 return NULL; 321 } 322 323 // Return TRUE if str is a syntactically valid IP address. 324 // Using inet_pton() instead of inet_aton() for IPv6 support. 325 // len is only a hint; cstr must still be nul-terminated 326 static int looksLikeIPAddress(char *cstr, size_t len) { 327 if (len == 0 || (len == 1 && cstr[0] == '.')) return FALSE; 328 329 char dst[16]; // big enough for INET6 330 return (1 == inet_pton(AF_INET, cstr, dst) || 331 1 == inet_pton(AF_INET6, cstr, dst)); 332 } 333 334 335 336 // Convert Mac OS X proxy exception entry to Java syntax. 337 // See Radar #3441134 for details. 338 // Returns NULL if this exception should be ignored by Java. 339 // May generate a string with multiple exceptions separated by '|'. 340 static char * createConvertedException(CFStringRef cf_original) { 341 // This is done with char* instead of CFString because inet_pton() 342 // needs a C string. 343 char *c_exception = createUTF8CString(cf_original); 344 if (!c_exception) return NULL; 345 346 int c_len = strlen(c_exception); 347 348 // 1. sanitize exception prefix 349 if (c_len >= 1 && 0 == strncmp(c_exception, ".", 1)) { 350 memmove(c_exception, c_exception+1, c_len); 351 c_len -= 1; 352 } else if (c_len >= 2 && 0 == strncmp(c_exception, "*.", 2)) { 353 memmove(c_exception, c_exception+2, c_len-1); 354 c_len -= 2; 355 } 356 357 // 2. pre-reject other exception wildcards 358 if (strchr(c_exception, '*')) { 359 free(c_exception); 360 return NULL; 361 } 362 363 // 3. no IP wildcarding 364 if (looksLikeIPAddress(c_exception, c_len)) { 365 return c_exception; 366 } 367 368 // 4. allow domain suffixes 369 // c_exception is now "str\0" - change to "str|*.str\0" 370 c_exception = reallocf(c_exception, c_len+3+c_len+1); 371 if (!c_exception) return NULL; 372 373 strncpy(c_exception+c_len, "|*.", 3); 374 strncpy(c_exception+c_len+3, c_exception, c_len); 375 c_exception[c_len+3+c_len] = '\0'; 376 return c_exception; 377 } 378 379 /* 380 * Method for fetching the user.home path and storing it in the property list. 381 * For signed .apps running in the Mac App Sandbox, user.home is set to the 382 * app's sandbox container. 383 */ 384 void setUserHome(java_props_t *sprops) { 385 if (sprops == NULL) { return; } 386 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 387 sprops->user_home = createUTF8CString((CFStringRef)NSHomeDirectory()); 388 [pool drain]; 389 } 390 391 /* 392 * Method for fetching proxy info and storing it in the property list. 393 */ 394 void setProxyProperties(java_props_t *sProps) { 395 if (sProps == NULL) return; 396 397 char buf[16]; /* Used for %d of an int - 16 is plenty */ 398 CFStringRef 399 cf_httpHost = NULL, 400 cf_httpsHost = NULL, 401 cf_ftpHost = NULL, 402 cf_socksHost = NULL, 403 cf_gopherHost = NULL; 404 int 405 httpPort = 80, // Default proxy port values 406 httpsPort = 443, 407 ftpPort = 21, 408 socksPort = 1080, 409 gopherPort = 70; 410 411 CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL); 412 if (dict == NULL) return; 413 414 /* Read the proxy exceptions list */ 415 CFArrayRef cf_list = CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList); 416 417 CFMutableStringRef cf_exceptionList = NULL; 418 if (cf_list != NULL) { 419 CFIndex len = CFArrayGetCount(cf_list), idx; 420 421 cf_exceptionList = CFStringCreateMutable(NULL, 0); 422 for (idx = (CFIndex)0; idx < len; idx++) { 423 CFStringRef cf_ehost; 424 if ((cf_ehost = CFArrayGetValueAtIndex(cf_list, idx))) { 425 /* Convert this exception from Mac OS X syntax to Java syntax. 426 See Radar #3441134 for details. This may generate a string 427 with multiple Java exceptions separated by '|'. */ 428 char *c_exception = createConvertedException(cf_ehost); 429 if (c_exception) { 430 /* Append the host to the list of exclusions. */ 431 if (CFStringGetLength(cf_exceptionList) > 0) { 432 CFStringAppendCString(cf_exceptionList, "|", kCFStringEncodingMacRoman); 433 } 434 CFStringAppendCString(cf_exceptionList, c_exception, kCFStringEncodingMacRoman); 435 free(c_exception); 436 } 437 } 438 } 439 } 440 441 if (cf_exceptionList != NULL) { 442 if (CFStringGetLength(cf_exceptionList) > 0) { 443 sProps->exceptionList = createUTF8CString(cf_exceptionList); 444 } 445 CFRelease(cf_exceptionList); 446 } 447 448 #define CHECK_PROXY(protocol, PROTOCOL) \ 449 sProps->protocol##ProxyEnabled = \ 450 getProxyInfoForProtocol(dict, kSCPropNetProxies##PROTOCOL##Enable, \ 451 kSCPropNetProxies##PROTOCOL##Proxy, \ 452 kSCPropNetProxies##PROTOCOL##Port, \ 453 &cf_##protocol##Host, &protocol##Port); \ 454 if (sProps->protocol##ProxyEnabled) { \ 455 sProps->protocol##Host = createUTF8CString(cf_##protocol##Host); \ 456 snprintf(buf, sizeof(buf), "%d", protocol##Port); \ 457 sProps->protocol##Port = malloc(strlen(buf) + 1); \ 458 strcpy(sProps->protocol##Port, buf); \ 459 } 460 461 CHECK_PROXY(http, HTTP); 462 CHECK_PROXY(https, HTTPS); 463 CHECK_PROXY(ftp, FTP); 464 CHECK_PROXY(socks, SOCKS); 465 CHECK_PROXY(gopher, Gopher); 466 467 #undef CHECK_PROXY 468 469 CFRelease(dict); 470 }