4632N/A/*
4632N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#include <dlfcn.h>
4632N/A#include <sys/socket.h>
4632N/A#include <netinet/in.h>
4632N/A#include <arpa/inet.h>
4632N/A
4632N/A#include <Security/AuthSession.h>
4632N/A#include <CoreFoundation/CoreFoundation.h>
4632N/A#include <SystemConfiguration/SystemConfiguration.h>
4632N/A
4632N/A#include "java_props_macosx.h"
4632N/A
4632N/A
4632N/A// need dlopen/dlsym trick to avoid pulling in JavaRuntimeSupport before libjava.dylib is loaded
4632N/Astatic void *getJRSFramework() {
4632N/A static void *jrsFwk = NULL;
4632N/A if (jrsFwk == NULL) {
4632N/A jrsFwk = dlopen("/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/JavaRuntimeSupport", RTLD_LAZY | RTLD_LOCAL);
4632N/A }
4632N/A return jrsFwk;
4632N/A}
4632N/A
4632N/Astatic char *getPosixLocale(int cat) {
4632N/A char *lc = setlocale(cat, NULL);
4632N/A if ((lc == NULL) || (strcmp(lc, "C") == 0)) {
4632N/A lc = getenv("LANG");
4632N/A }
4632N/A if (lc == NULL) return NULL;
4632N/A return strdup(lc);
4632N/A}
4632N/A
4632N/A#define LOCALEIDLENGTH 128
4632N/Achar *setupMacOSXLocale(int cat) {
4632N/A switch (cat) {
4632N/A case LC_MESSAGES:
4632N/A {
4632N/A void *jrsFwk = getJRSFramework();
4632N/A if (jrsFwk == NULL) return getPosixLocale(cat);
4632N/A
4632N/A char *(*JRSCopyPrimaryLanguage)() = dlsym(jrsFwk, "JRSCopyPrimaryLanguage");
4632N/A char *primaryLanguage = JRSCopyPrimaryLanguage ? JRSCopyPrimaryLanguage() : NULL;
4632N/A if (primaryLanguage == NULL) return getPosixLocale(cat);
4632N/A
4632N/A char *(*JRSCopyCanonicalLanguageForPrimaryLanguage)(char *) = dlsym(jrsFwk, "JRSCopyCanonicalLanguageForPrimaryLanguage");
4632N/A char *canonicalLanguage = JRSCopyCanonicalLanguageForPrimaryLanguage ? JRSCopyCanonicalLanguageForPrimaryLanguage(primaryLanguage) : NULL;
4632N/A free (primaryLanguage);
4632N/A if (canonicalLanguage == NULL) return getPosixLocale(cat);
4632N/A
4632N/A void (*JRSSetDefaultLocalization)(char *) = dlsym(jrsFwk, "JRSSetDefaultLocalization");
4632N/A if (JRSSetDefaultLocalization) JRSSetDefaultLocalization(canonicalLanguage);
4632N/A
4632N/A return canonicalLanguage;
4632N/A }
4632N/A break;
4632N/A default:
4632N/A {
4632N/A char localeString[LOCALEIDLENGTH];
4632N/A if (CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
4632N/A localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) {
4632N/A return strdup(localeString);
4632N/A }
4632N/A }
4632N/A break;
4632N/A }
4632N/A
4632N/A return NULL;
4632N/A}
4632N/A
4632N/A/* There are several toolkit options on Mac OS X, so we should try to
4632N/A * pick the "best" one, given what we know about the environment Java
4632N/A * is running under
4632N/A */
4632N/A
4632N/Astatic PreferredToolkit getPreferredToolkitFromEnv() {
4632N/A char *envVar = getenv("AWT_TOOLKIT");
4632N/A if (envVar == NULL) return unset;
4632N/A
4632N/A if (strcasecmp(envVar, "CToolkit") == 0) return CToolkit;
4632N/A if (strcasecmp(envVar, "XToolkit") == 0) return XToolkit;
4632N/A if (strcasecmp(envVar, "HToolkit") == 0) return HToolkit;
4632N/A return unset;
4632N/A}
4632N/A
4632N/Astatic bool isInAquaSession() {
4632N/A // Is the WindowServer available?
4632N/A SecuritySessionId session_id;
4632N/A SessionAttributeBits session_info;
4632N/A OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info);
4632N/A if (status != noErr) return false;
4632N/A if (!(session_info & sessionHasGraphicAccess)) return false;
4632N/A return true;
4632N/A}
4632N/A
4632N/APreferredToolkit getPreferredToolkit() {
4632N/A static PreferredToolkit pref = unset;
4632N/A if (pref != unset) return pref;
4632N/A
4632N/A PreferredToolkit prefFromEnv = getPreferredToolkitFromEnv();
4632N/A if (prefFromEnv != unset) return pref = prefFromEnv;
4632N/A
4632N/A if (isInAquaSession()) return pref = CToolkit;
4632N/A return pref = HToolkit;
4632N/A}
4632N/A
4632N/Avoid setUnknownOSAndVersion(java_props_t *sprops) {
4632N/A sprops->os_name = strdup("Unknown");
4632N/A sprops->os_version = strdup("Unknown");
4632N/A}
4632N/A
4632N/Avoid setOSNameAndVersion(java_props_t *sprops) {
4632N/A void *jrsFwk = getJRSFramework();
4632N/A if (jrsFwk == NULL) {
4632N/A setUnknownOSAndVersion(sprops);
4632N/A return;
4632N/A }
4632N/A
4632N/A char *(*copyOSName)() = dlsym(jrsFwk, "JRSCopyOSName");
4632N/A char *(*copyOSVersion)() = dlsym(jrsFwk, "JRSCopyOSVersion");
4632N/A if (copyOSName == NULL || copyOSVersion == NULL) {
4632N/A setUnknownOSAndVersion(sprops);
4632N/A return;
4632N/A }
4632N/A
4632N/A sprops->os_name = copyOSName();
4632N/A sprops->os_version = copyOSVersion();
4632N/A}
4632N/A
4632N/A
4632N/Astatic Boolean getProxyInfoForProtocol(CFDictionaryRef inDict, CFStringRef inEnabledKey, CFStringRef inHostKey, CFStringRef inPortKey, CFStringRef *outProxyHost, int *ioProxyPort) {
4632N/A /* See if the proxy is enabled. */
4632N/A CFNumberRef cf_enabled = CFDictionaryGetValue(inDict, inEnabledKey);
4632N/A if (cf_enabled == NULL) {
4632N/A return false;
4632N/A }
4632N/A
4632N/A int isEnabled = false;
4632N/A if (!CFNumberGetValue(cf_enabled, kCFNumberIntType, &isEnabled)) {
4632N/A return isEnabled;
4632N/A }
4632N/A
4632N/A if (!isEnabled) return false;
4632N/A *outProxyHost = CFDictionaryGetValue(inDict, inHostKey);
4632N/A
4632N/A // If cf_host is null, that means the checkbox is set,
4632N/A // but no host was entered. We'll treat that as NOT ENABLED.
4632N/A // If cf_port is null or cf_port isn't a number, that means
4632N/A // no port number was entered. Treat this as ENABLED with the
4632N/A // protocol's default port.
4632N/A if (*outProxyHost == NULL) {
4632N/A return false;
4632N/A }
4632N/A
4632N/A if (CFStringGetLength(*outProxyHost) == 0) {
4632N/A return false;
4632N/A }
4632N/A
4632N/A int newPort = 0;
4632N/A CFNumberRef cf_port = NULL;
4632N/A if ((cf_port = CFDictionaryGetValue(inDict, inPortKey)) != NULL &&
4632N/A CFNumberGetValue(cf_port, kCFNumberIntType, &newPort) &&
4632N/A newPort > 0) {
4632N/A *ioProxyPort = newPort;
4632N/A } else {
4632N/A // bad port or no port - leave *ioProxyPort unchanged
4632N/A }
4632N/A
4632N/A return true;
4632N/A}
4632N/A
4632N/Astatic char *createUTF8CString(const CFStringRef theString) {
4632N/A if (theString == NULL) return NULL;
4632N/A
4632N/A const CFIndex stringLength = CFStringGetLength(theString);
4632N/A const CFIndex bufSize = CFStringGetMaximumSizeForEncoding(stringLength, kCFStringEncodingUTF8) + 1;
4632N/A char *returnVal = (char *)malloc(bufSize);
4632N/A
4632N/A if (CFStringGetCString(theString, returnVal, bufSize, kCFStringEncodingUTF8)) {
4632N/A return returnVal;
4632N/A }
4632N/A
4632N/A free(returnVal);
4632N/A return NULL;
4632N/A}
4632N/A
4632N/A// Return TRUE if str is a syntactically valid IP address.
4632N/A// Using inet_pton() instead of inet_aton() for IPv6 support.
4632N/A// len is only a hint; cstr must still be nul-terminated
4632N/Astatic int looksLikeIPAddress(char *cstr, size_t len) {
4632N/A if (len == 0 || (len == 1 && cstr[0] == '.')) return FALSE;
4632N/A
4632N/A char dst[16]; // big enough for INET6
4632N/A return (1 == inet_pton(AF_INET, cstr, dst) ||
4632N/A 1 == inet_pton(AF_INET6, cstr, dst));
4632N/A}
4632N/A
4632N/A
4632N/A
4632N/A// Convert Mac OS X proxy exception entry to Java syntax.
4632N/A// See Radar #3441134 for details.
4632N/A// Returns NULL if this exception should be ignored by Java.
4632N/A// May generate a string with multiple exceptions separated by '|'.
4632N/Astatic char * createConvertedException(CFStringRef cf_original) {
4632N/A // This is done with char* instead of CFString because inet_pton()
4632N/A // needs a C string.
4632N/A char *c_exception = createUTF8CString(cf_original);
4632N/A if (!c_exception) return NULL;
4632N/A
4632N/A int c_len = strlen(c_exception);
4632N/A
4632N/A // 1. sanitize exception prefix
4632N/A if (c_len >= 1 && 0 == strncmp(c_exception, ".", 1)) {
4632N/A memmove(c_exception, c_exception+1, c_len);
4632N/A c_len -= 1;
4632N/A } else if (c_len >= 2 && 0 == strncmp(c_exception, "*.", 2)) {
4632N/A memmove(c_exception, c_exception+2, c_len-1);
4632N/A c_len -= 2;
4632N/A }
4632N/A
4632N/A // 2. pre-reject other exception wildcards
4632N/A if (strchr(c_exception, '*')) {
4632N/A free(c_exception);
4632N/A return NULL;
4632N/A }
4632N/A
4632N/A // 3. no IP wildcarding
4632N/A if (looksLikeIPAddress(c_exception, c_len)) {
4632N/A return c_exception;
4632N/A }
4632N/A
4632N/A // 4. allow domain suffixes
4632N/A // c_exception is now "str\0" - change to "str|*.str\0"
4632N/A c_exception = reallocf(c_exception, c_len+3+c_len+1);
4632N/A if (!c_exception) return NULL;
4632N/A
4632N/A strncpy(c_exception+c_len, "|*.", 3);
4632N/A strncpy(c_exception+c_len+3, c_exception, c_len);
4632N/A c_exception[c_len+3+c_len] = '\0';
4632N/A return c_exception;
4632N/A}
4632N/A
4632N/A
4632N/A/*
4632N/A * Method for fetching proxy info and storing it in the propery list.
4632N/A */
4632N/Avoid setProxyProperties(java_props_t *sProps) {
4632N/A if (sProps == NULL) return;
4632N/A
4632N/A char buf[16]; /* Used for %d of an int - 16 is plenty */
4632N/A CFStringRef
4632N/A cf_httpHost = NULL,
4632N/A cf_httpsHost = NULL,
4632N/A cf_ftpHost = NULL,
4632N/A cf_socksHost = NULL,
4632N/A cf_gopherHost = NULL;
4632N/A int
4632N/A httpPort = 80, // Default proxy port values
4632N/A httpsPort = 443,
4632N/A ftpPort = 21,
4632N/A socksPort = 1080,
4632N/A gopherPort = 70;
4632N/A
4632N/A CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
4632N/A if (dict == NULL) return;
4632N/A
4632N/A /* Read the proxy exceptions list */
4632N/A CFArrayRef cf_list = CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList);
4632N/A
4632N/A CFMutableStringRef cf_exceptionList = NULL;
4632N/A if (cf_list != NULL) {
4632N/A CFIndex len = CFArrayGetCount(cf_list), idx;
4632N/A
4632N/A cf_exceptionList = CFStringCreateMutable(NULL, 0);
4632N/A for (idx = (CFIndex)0; idx < len; idx++) {
4632N/A CFStringRef cf_ehost;
4632N/A if ((cf_ehost = CFArrayGetValueAtIndex(cf_list, idx))) {
4632N/A /* Convert this exception from Mac OS X syntax to Java syntax.
4632N/A See Radar #3441134 for details. This may generate a string
4632N/A with multiple Java exceptions separated by '|'. */
4632N/A char *c_exception = createConvertedException(cf_ehost);
4632N/A if (c_exception) {
4632N/A /* Append the host to the list of exclusions. */
4632N/A if (CFStringGetLength(cf_exceptionList) > 0) {
4632N/A CFStringAppendCString(cf_exceptionList, "|", kCFStringEncodingMacRoman);
4632N/A }
4632N/A CFStringAppendCString(cf_exceptionList, c_exception, kCFStringEncodingMacRoman);
4632N/A free(c_exception);
4632N/A }
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A if (cf_exceptionList != NULL) {
4632N/A if (CFStringGetLength(cf_exceptionList) > 0) {
4632N/A sProps->exceptionList = createUTF8CString(cf_exceptionList);
4632N/A }
4632N/A CFRelease(cf_exceptionList);
4632N/A }
4632N/A
4632N/A#define CHECK_PROXY(protocol, PROTOCOL) \
4632N/A sProps->protocol##ProxyEnabled = \
4632N/A getProxyInfoForProtocol(dict, kSCPropNetProxies##PROTOCOL##Enable, \
4632N/A kSCPropNetProxies##PROTOCOL##Proxy, \
4632N/A kSCPropNetProxies##PROTOCOL##Port, \
4632N/A &cf_##protocol##Host, &protocol##Port); \
4632N/A if (sProps->protocol##ProxyEnabled) { \
4632N/A sProps->protocol##Host = createUTF8CString(cf_##protocol##Host); \
4632N/A snprintf(buf, sizeof(buf), "%d", protocol##Port); \
4632N/A sProps->protocol##Port = malloc(strlen(buf) + 1); \
4632N/A strcpy(sProps->protocol##Port, buf); \
4632N/A }
4632N/A
4632N/A CHECK_PROXY(http, HTTP);
4632N/A CHECK_PROXY(https, HTTPS);
4632N/A CHECK_PROXY(ftp, FTP);
4632N/A CHECK_PROXY(socks, SOCKS);
4632N/A CHECK_PROXY(gopher, Gopher);
4632N/A
4632N/A#undef CHECK_PROXY
4632N/A
4632N/A CFRelease(dict);
4632N/A}