1 /* 2 * Copyright (c) 1998, 2016, 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 switch (cat) { 50 case LC_MESSAGES: 51 { 52 // get preferred language code 53 CFArrayRef languages = CFLocaleCopyPreferredLanguages(); 54 if (languages == NULL) { 55 return NULL; 56 } 57 if (CFArrayGetCount(languages) <= 0) { 58 CFRelease(languages); 59 return NULL; 60 } 61 CFStringRef primaryLanguage = (CFStringRef)CFArrayGetValueAtIndex(languages, 0); 62 if (primaryLanguage == NULL) { 63 CFRelease(languages); // was missing from original patch 64 return NULL; 65 } 66 char languageString[LOCALEIDLENGTH]; 67 if (CFStringGetCString(primaryLanguage, languageString, 68 LOCALEIDLENGTH, CFStringGetSystemEncoding()) == false) { 69 CFRelease(languages); 70 return NULL; 71 } 72 CFRelease(languages); 73 if (languageString[2] == '-') { 74 // aa-AA into aa_AA 75 languageString[2] = '_'; // BCP 47 lang-region --> ISO_639-1 76 } 77 return strdup(languageString); 78 } 79 break; 80 default: 81 { 82 char localeString[LOCALEIDLENGTH]; 83 if (CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()), 84 localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) { 85 return strdup(localeString); 86 } 87 } 88 break; 89 } 90 91 return NULL; 92 } 93 94 char *setupMacOSXLocale(int cat) { 95 char * ret = getMacOSXLocale(cat); 96 97 if (ret == NULL) { 98 return getPosixLocale(cat); 99 } else { 100 return ret; 101 } 102 } 103 104 int isInAquaSession() { 105 // environment variable to bypass the aqua session check 106 char *ev = getenv("AWT_FORCE_HEADFUL"); 107 if (ev && (strncasecmp(ev, "true", 4) == 0)) { 108 // if "true" then tell the caller we're in an Aqua session without actually checking 109 return 1; 110 } 111 // Is the WindowServer available? 112 SecuritySessionId session_id; 113 SessionAttributeBits session_info; 114 OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info); 115 if (status == noErr) { 116 if (session_info & sessionHasGraphicAccess) { 117 return 1; 118 } 119 } 120 return 0; 121 } 122 123 // 10.9 SDK does not include the NSOperatingSystemVersion struct. 124 // For now, create our own 125 typedef struct { 126 NSInteger majorVersion; 127 NSInteger minorVersion; 128 NSInteger patchVersion; 129 } OSVerStruct; 130 131 void setOSNameAndVersion(java_props_t *sprops) { 132 /* Don't rely on JRSCopyOSName because there's no guarantee the value will 133 * remain the same, or even if the JRS functions will continue to be part of 134 * Mac OS X. So hardcode os_name, and fill in os_version if we can. 135 */ 136 sprops->os_name = strdup("Mac OS X"); 137 138 char *osVersionCStr = NULL; 139 // Mac OS 10.9 includes the [NSProcessInfo operatingSystemVersion] function, 140 // but it's not in the 10.9 SDK. So, call it via objc_msgSend_stret. 141 if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) { 142 OSVerStruct (*procInfoFn)(id rec, SEL sel) = (OSVerStruct(*)(id, SEL))objc_msgSend_stret; 143 OSVerStruct osVer = procInfoFn([NSProcessInfo processInfo], 144 @selector(operatingSystemVersion)); 145 NSString *nsVerStr = [NSString stringWithFormat:@"%ld.%ld.%ld", 146 (long)osVer.majorVersion, (long)osVer.minorVersion, (long)osVer.patchVersion]; 147 // Copy out the char* 148 osVersionCStr = strdup([nsVerStr UTF8String]); 149 } 150 151 if (osVersionCStr == NULL) { 152 osVersionCStr = strdup("Unknown"); 153 } 154 sprops->os_version = osVersionCStr; 155 } 156 157 158 static Boolean getProxyInfoForProtocol(CFDictionaryRef inDict, CFStringRef inEnabledKey, CFStringRef inHostKey, CFStringRef inPortKey, CFStringRef *outProxyHost, int *ioProxyPort) { 159 /* See if the proxy is enabled. */ 160 CFNumberRef cf_enabled = CFDictionaryGetValue(inDict, inEnabledKey); 161 if (cf_enabled == NULL) { 162 return false; 163 } 164 165 int isEnabled = false; 166 if (!CFNumberGetValue(cf_enabled, kCFNumberIntType, &isEnabled)) { 167 return isEnabled; 168 } 169 170 if (!isEnabled) return false; 171 *outProxyHost = CFDictionaryGetValue(inDict, inHostKey); 172 173 // If cf_host is null, that means the checkbox is set, 174 // but no host was entered. We'll treat that as NOT ENABLED. 175 // If cf_port is null or cf_port isn't a number, that means 176 // no port number was entered. Treat this as ENABLED with the 177 // protocol's default port. 178 if (*outProxyHost == NULL) { 179 return false; 180 } 181 182 if (CFStringGetLength(*outProxyHost) == 0) { 183 return false; 184 } 185 186 int newPort = 0; 187 CFNumberRef cf_port = NULL; 188 if ((cf_port = CFDictionaryGetValue(inDict, inPortKey)) != NULL && 189 CFNumberGetValue(cf_port, kCFNumberIntType, &newPort) && 190 newPort > 0) { 191 *ioProxyPort = newPort; 192 } else { 193 // bad port or no port - leave *ioProxyPort unchanged 194 } 195 196 return true; 197 } 198 199 static char *createUTF8CString(const CFStringRef theString) { 200 if (theString == NULL) return NULL; 201 202 const CFIndex stringLength = CFStringGetLength(theString); 203 const CFIndex bufSize = CFStringGetMaximumSizeForEncoding(stringLength, kCFStringEncodingUTF8) + 1; 204 char *returnVal = (char *)malloc(bufSize); 205 206 if (CFStringGetCString(theString, returnVal, bufSize, kCFStringEncodingUTF8)) { 207 return returnVal; 208 } 209 210 free(returnVal); 211 return NULL; 212 } 213 214 // Return TRUE if str is a syntactically valid IP address. 215 // Using inet_pton() instead of inet_aton() for IPv6 support. 216 // len is only a hint; cstr must still be nul-terminated 217 static int looksLikeIPAddress(char *cstr, size_t len) { 218 if (len == 0 || (len == 1 && cstr[0] == '.')) return FALSE; 219 220 char dst[16]; // big enough for INET6 221 return (1 == inet_pton(AF_INET, cstr, dst) || 222 1 == inet_pton(AF_INET6, cstr, dst)); 223 } 224 225 226 227 // Convert Mac OS X proxy exception entry to Java syntax. 228 // See Radar #3441134 for details. 229 // Returns NULL if this exception should be ignored by Java. 230 // May generate a string with multiple exceptions separated by '|'. 231 static char * createConvertedException(CFStringRef cf_original) { 232 // This is done with char* instead of CFString because inet_pton() 233 // needs a C string. 234 char *c_exception = createUTF8CString(cf_original); 235 if (!c_exception) return NULL; 236 237 int c_len = strlen(c_exception); 238 239 // 1. sanitize exception prefix 240 if (c_len >= 1 && 0 == strncmp(c_exception, ".", 1)) { 241 memmove(c_exception, c_exception+1, c_len); 242 c_len -= 1; 243 } else if (c_len >= 2 && 0 == strncmp(c_exception, "*.", 2)) { 244 memmove(c_exception, c_exception+2, c_len-1); 245 c_len -= 2; 246 } 247 248 // 2. pre-reject other exception wildcards 249 if (strchr(c_exception, '*')) { 250 free(c_exception); 251 return NULL; 252 } 253 254 // 3. no IP wildcarding 255 if (looksLikeIPAddress(c_exception, c_len)) { 256 return c_exception; 257 } 258 259 // 4. allow domain suffixes 260 // c_exception is now "str\0" - change to "str|*.str\0" 261 c_exception = reallocf(c_exception, c_len+3+c_len+1); 262 if (!c_exception) return NULL; 263 264 strncpy(c_exception+c_len, "|*.", 3); 265 strncpy(c_exception+c_len+3, c_exception, c_len); 266 c_exception[c_len+3+c_len] = '\0'; 267 return c_exception; 268 } 269 270 /* 271 * Method for fetching the user.home path and storing it in the property list. 272 * For signed .apps running in the Mac App Sandbox, user.home is set to the 273 * app's sandbox container. 274 */ 275 void setUserHome(java_props_t *sprops) { 276 if (sprops == NULL) { return; } 277 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 278 sprops->user_home = createUTF8CString((CFStringRef)NSHomeDirectory()); 279 [pool drain]; 280 } 281 282 /* 283 * Method for fetching proxy info and storing it in the property list. 284 */ 285 void setProxyProperties(java_props_t *sProps) { 286 if (sProps == NULL) return; 287 288 char buf[16]; /* Used for %d of an int - 16 is plenty */ 289 CFStringRef 290 cf_httpHost = NULL, 291 cf_httpsHost = NULL, 292 cf_ftpHost = NULL, 293 cf_socksHost = NULL, 294 cf_gopherHost = NULL; 295 int 296 httpPort = 80, // Default proxy port values 297 httpsPort = 443, 298 ftpPort = 21, 299 socksPort = 1080, 300 gopherPort = 70; 301 302 CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL); 303 if (dict == NULL) return; 304 305 /* Read the proxy exceptions list */ 306 CFArrayRef cf_list = CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList); 307 308 CFMutableStringRef cf_exceptionList = NULL; 309 if (cf_list != NULL) { 310 CFIndex len = CFArrayGetCount(cf_list), idx; 311 312 cf_exceptionList = CFStringCreateMutable(NULL, 0); 313 for (idx = (CFIndex)0; idx < len; idx++) { 314 CFStringRef cf_ehost; 315 if ((cf_ehost = CFArrayGetValueAtIndex(cf_list, idx))) { 316 /* Convert this exception from Mac OS X syntax to Java syntax. 317 See Radar #3441134 for details. This may generate a string 318 with multiple Java exceptions separated by '|'. */ 319 char *c_exception = createConvertedException(cf_ehost); 320 if (c_exception) { 321 /* Append the host to the list of exclusions. */ 322 if (CFStringGetLength(cf_exceptionList) > 0) { 323 CFStringAppendCString(cf_exceptionList, "|", kCFStringEncodingMacRoman); 324 } 325 CFStringAppendCString(cf_exceptionList, c_exception, kCFStringEncodingMacRoman); 326 free(c_exception); 327 } 328 } 329 } 330 } 331 332 if (cf_exceptionList != NULL) { 333 if (CFStringGetLength(cf_exceptionList) > 0) { 334 sProps->exceptionList = createUTF8CString(cf_exceptionList); 335 } 336 CFRelease(cf_exceptionList); 337 } 338 339 #define CHECK_PROXY(protocol, PROTOCOL) \ 340 sProps->protocol##ProxyEnabled = \ 341 getProxyInfoForProtocol(dict, kSCPropNetProxies##PROTOCOL##Enable, \ 342 kSCPropNetProxies##PROTOCOL##Proxy, \ 343 kSCPropNetProxies##PROTOCOL##Port, \ 344 &cf_##protocol##Host, &protocol##Port); \ 345 if (sProps->protocol##ProxyEnabled) { \ 346 sProps->protocol##Host = createUTF8CString(cf_##protocol##Host); \ 347 snprintf(buf, sizeof(buf), "%d", protocol##Port); \ 348 sProps->protocol##Port = malloc(strlen(buf) + 1); \ 349 strcpy(sProps->protocol##Port, buf); \ 350 } 351 352 CHECK_PROXY(http, HTTP); 353 CHECK_PROXY(https, HTTPS); 354 CHECK_PROXY(ftp, FTP); 355 CHECK_PROXY(socks, SOCKS); 356 CHECK_PROXY(gopher, Gopher); 357 358 #undef CHECK_PROXY 359 360 CFRelease(dict); 361 }