| 1 | /* |
| 2 | * proxyfromsystemconfiguration.c |
| 3 | * $Id: $ |
| 4 | * |
| 5 | * Copyright (c) 2008, The MacPorts Project. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of MacPorts Team nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #ifdef HAVE_CONFIG_H |
| 34 | #include <config.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "proxyfromsystemconfiguration.h" |
| 38 | |
| 39 | #ifdef HAVE_FRAMEWORK_SYSTEMCONFIGURATION |
| 40 | #include <CoreFoundation/CoreFoundation.h> |
| 41 | #include <SystemConfiguration/SystemConfiguration.h> |
| 42 | |
| 43 | int setProxyInfoForEnvWithKeys( CFDictionaryRef proxies, const char *envName, const void *enabledKey, const void *hostKey, const void *portKey ); |
| 44 | char *cfStringToCStringASCII( CFStringRef cfString ); |
| 45 | |
| 46 | #endif /* HAVE_FRAMEWORK_SYSTEMCONFIGURATION */ |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * Set various libcurl-specific environment variables based on settings |
| 52 | * gleaned from the SystemConfiguration framework on Mac OS X. If this |
| 53 | * Is run on a non-Mac system, it does nothing |
| 54 | * |
| 55 | * Synopsis: getproxyfromsystemconfiguration |
| 56 | */ |
| 57 | int getProxyFromSystemConfigurationCmd( ClientData clientData UNUSED, Tcl_Interp *interp, int objc UNUSED, Tcl_Obj *CONST objv[] UNUSED ) |
| 58 | { |
| 59 | #ifdef HAVE_FRAMEWORK_SYSTEMCONFIGURATION |
| 60 | int result = 0; |
| 61 | CFDictionaryRef proxies = SCDynamicStoreCopyProxies( NULL ); |
| 62 | if( proxies != NULL ) |
| 63 | { |
| 64 | if( !setProxyInfoForEnvWithKeys( proxies, "http_proxy", kSCPropNetProxiesHTTPEnable, kSCPropNetProxiesHTTPProxy, kSCPropNetProxiesHTTPPort ) ) |
| 65 | { |
| 66 | if( !setProxyInfoForEnvWithKeys( proxies, "HTTPS_PROXY", kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort ) ) |
| 67 | { |
| 68 | if( !setProxyInfoForEnvWithKeys( proxies, "FTP_PROXY", kSCPropNetProxiesFTPEnable, kSCPropNetProxiesFTPProxy, kSCPropNetProxiesFTPPort ) ) |
| 69 | { |
| 70 | CFArrayRef exceptionsList = CFDictionaryGetValue( proxies, kSCPropNetProxiesExceptionsList ); |
| 71 | if( exceptionsList != NULL ) |
| 72 | { |
| 73 | CFStringRef exceptionsCommaSeparated = CFStringCreateByCombiningStrings( kCFAllocatorDefault, exceptionsList, CFSTR( "," ) ); |
| 74 | char *cStringList = cfStringToCStringASCII( exceptionsCommaSeparated ); |
| 75 | if( cStringList != NULL ) |
| 76 | { |
| 77 | setenv( "NO_PROXY", cStringList, 0 ); |
| 78 | free( cStringList ); |
| 79 | } |
| 80 | else |
| 81 | result = -1; |
| 82 | CFRelease( exceptionsCommaSeparated ); |
| 83 | } |
| 84 | } |
| 85 | else |
| 86 | result = -1; |
| 87 | } |
| 88 | else |
| 89 | result = -1; |
| 90 | } |
| 91 | else |
| 92 | result = -1; |
| 93 | |
| 94 | CFRelease( proxies ); |
| 95 | } |
| 96 | |
| 97 | if( result == -1 ) |
| 98 | { |
| 99 | Tcl_SetResult( interp, (char *) Tcl_PosixError( interp ), TCL_STATIC ); |
| 100 | return TCL_ERROR; |
| 101 | } |
| 102 | return TCL_OK; |
| 103 | #else /* HAVE_FRAMEWORK_SYSTEMCONFIGURATION */ |
| 104 | return TCL_OK; |
| 105 | #endif /* HAVE_FRAMEWORK_SYSTEMCONFIGURATION */ |
| 106 | } |
| 107 | |
| 108 | |
| 109 | #ifdef HAVE_FRAMEWORK_SYSTEMCONFIGURATION |
| 110 | /** |
| 111 | * |
| 112 | * Set an environment variable based on the given SystemConfiguration |
| 113 | * dictionary keys. |
| 114 | * |
| 115 | * Returns 0 on success; -1 on failure (to allocate memory) |
| 116 | */ |
| 117 | int setProxyInfoForEnvWithKeys( CFDictionaryRef proxies, const char *envName, const void *enabledKey, const void *hostKey, const void *portKey ) |
| 118 | { |
| 119 | int result = 0; |
| 120 | CFNumberRef proxyEnabledNumber = CFDictionaryGetValue( proxies, enabledKey ); |
| 121 | if( proxyEnabledNumber != NULL ) |
| 122 | { |
| 123 | int proxyEnabled; |
| 124 | CFNumberGetValue( proxyEnabledNumber, kCFNumberIntType, &proxyEnabled ); |
| 125 | if( proxyEnabled ) |
| 126 | { |
| 127 | CFStringRef proxyHost = CFDictionaryGetValue( proxies, hostKey ); |
| 128 | if( proxyHost != NULL ) |
| 129 | { |
| 130 | char *hostname = cfStringToCStringASCII( proxyHost ); |
| 131 | if( hostname != NULL ) |
| 132 | { |
| 133 | CFNumberRef proxyPort = CFDictionaryGetValue( proxies, portKey ); |
| 134 | int port = 0; |
| 135 | if( proxyPort != NULL ) |
| 136 | CFNumberGetValue( proxyPort, kCFNumberIntType, &port ); |
| 137 | |
| 138 | if( port > 0 ) |
| 139 | { |
| 140 | /* hostname:12345\0 is adding up to 7 chars... */ |
| 141 | int newLength = strlen( hostname ) + 7; |
| 142 | char *hostAndPort = calloc( 1, newLength ); |
| 143 | if( hostAndPort != NULL ) |
| 144 | { |
| 145 | snprintf( hostAndPort, newLength, "%s:%d", hostname, port ); |
| 146 | setenv( envName, hostAndPort, 0 ); |
| 147 | free( hostAndPort ); |
| 148 | } |
| 149 | else |
| 150 | result = -1; |
| 151 | } |
| 152 | else |
| 153 | setenv( envName, hostname, 0 ); |
| 154 | free( hostname ); |
| 155 | } |
| 156 | else |
| 157 | result = -1; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * |
| 168 | * Convert a CFStringRef to an ASCII-encoded C string; be sure to free() |
| 169 | * the returned string when done with it. |
| 170 | */ |
| 171 | char *cfStringToCStringASCII( CFStringRef cfString ) |
| 172 | { |
| 173 | int strLen = CFStringGetMaximumSizeForEncoding( CFStringGetLength( cfString ), kCFStringEncodingASCII ) + 1; |
| 174 | char *cString = calloc( 1, strLen ); |
| 175 | if( cString != NULL ) |
| 176 | CFStringGetCString( cfString, cString, strLen, kCFStringEncodingASCII ); |
| 177 | |
| 178 | return cString; |
| 179 | } |
| 180 | |
| 181 | #endif |
| 182 | |