]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCProxies.c
configd-212.2.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCProxies.c
1 /*
2 * Copyright (c) 2000-2004, 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * May 18, 2001 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31 #include <SystemConfiguration/SystemConfiguration.h>
32 #include <SystemConfiguration/SCValidation.h>
33 #include <SystemConfiguration/SCPrivate.h>
34
35 #include <netdb.h>
36
37
38
39
40 CFStringRef
41 SCDynamicStoreKeyCreateProxies(CFAllocatorRef allocator)
42 {
43 return SCDynamicStoreKeyCreateNetworkGlobalEntity(allocator,
44 kSCDynamicStoreDomainState,
45 kSCEntNetProxies);
46 }
47
48
49 static void
50 validate_proxy_content(CFMutableDictionaryRef proxies,
51 CFStringRef proxy_enable,
52 CFStringRef proxy_host,
53 CFStringRef proxy_port,
54 const char * proxy_service,
55 int proxy_defaultport)
56 {
57 int enabled = 0;
58 CFNumberRef num;
59 CFStringRef host;
60 CFNumberRef port = NULL;
61
62 num = CFDictionaryGetValue(proxies, proxy_enable);
63 if (num != NULL) {
64 if (!isA_CFNumber(num) ||
65 !CFNumberGetValue(num, kCFNumberIntType, &enabled)) {
66 // if we don't like the enabled key/value
67 goto disable;
68 }
69 }
70
71 host = CFDictionaryGetValue(proxies, proxy_host);
72 if (((enabled == 0) && (host != NULL)) ||
73 ((enabled != 0) && !isA_CFString(host))) {
74 // pass only valid proxy hosts and only when enabled
75 goto disable;
76 }
77
78 if (proxy_port != NULL) {
79 port = CFDictionaryGetValue(proxies, proxy_port);
80 if (((enabled == 0) && (port != NULL)) ||
81 ((enabled != 0) && (port != NULL) && !isA_CFNumber(port))) {
82 // pass only provided/valid proxy ports and only when enabled
83 goto disable;
84 }
85
86 if ((enabled != 0) && (port == NULL)) {
87 struct servent *service;
88 int s_port;
89
90 service = getservbyname(proxy_service, "tcp");
91 if (service != NULL) {
92 s_port = ntohs(service->s_port);
93 } else {
94 s_port = proxy_defaultport;
95 }
96 num = CFNumberCreate(NULL, kCFNumberIntType, &s_port);
97 CFDictionarySetValue(proxies, proxy_port, num);
98 CFRelease(num);
99 }
100 }
101
102 return;
103
104 disable :
105
106 enabled = 0;
107 num = CFNumberCreate(NULL, kCFNumberIntType, &enabled);
108 CFDictionarySetValue(proxies, proxy_enable, num);
109 CFRelease(num);
110 CFDictionaryRemoveValue(proxies, proxy_host);
111 if (proxy_port != NULL) {
112 CFDictionaryRemoveValue(proxies, proxy_port);
113 }
114
115 return;
116 }
117
118
119 CFDictionaryRef
120 SCDynamicStoreCopyProxies(SCDynamicStoreRef store)
121 {
122 CFArrayRef array;
123 CFStringRef key;
124 CFMutableDictionaryRef newProxies = NULL;
125 CFNumberRef num;
126 CFDictionaryRef proxies;
127 Boolean tempSession = FALSE;
128
129
130 /* copy proxy information from dynamic store */
131
132 if (store == NULL) {
133 store = SCDynamicStoreCreate(NULL,
134 CFSTR("SCDynamicStoreCopyProxies"),
135 NULL,
136 NULL);
137 if (store == NULL) {
138 return NULL;
139 }
140 tempSession = TRUE;
141 }
142
143 key = SCDynamicStoreKeyCreateProxies(NULL);
144 proxies = SCDynamicStoreCopyValue(store, key);
145 CFRelease(key);
146
147 validate :
148
149 if (proxies != NULL) {
150 if (isA_CFDictionary(proxies)) {
151 newProxies = CFDictionaryCreateMutableCopy(NULL, 0, proxies);
152 }
153 CFRelease(proxies);
154 }
155
156 if (newProxies == NULL) {
157 newProxies = CFDictionaryCreateMutable(NULL,
158 0,
159 &kCFTypeDictionaryKeyCallBacks,
160 &kCFTypeDictionaryValueCallBacks);
161 }
162
163 /* validate [and augment] proxy content */
164
165 validate_proxy_content(newProxies,
166 kSCPropNetProxiesFTPEnable,
167 kSCPropNetProxiesFTPProxy,
168 kSCPropNetProxiesFTPPort,
169 "ftp",
170 21);
171 validate_proxy_content(newProxies,
172 kSCPropNetProxiesGopherEnable,
173 kSCPropNetProxiesGopherProxy,
174 kSCPropNetProxiesGopherPort,
175 "gopher",
176 70);
177 validate_proxy_content(newProxies,
178 kSCPropNetProxiesHTTPEnable,
179 kSCPropNetProxiesHTTPProxy,
180 kSCPropNetProxiesHTTPPort,
181 "http",
182 80);
183 validate_proxy_content(newProxies,
184 kSCPropNetProxiesHTTPSEnable,
185 kSCPropNetProxiesHTTPSProxy,
186 kSCPropNetProxiesHTTPSPort,
187 "https",
188 443);
189 validate_proxy_content(newProxies,
190 kSCPropNetProxiesRTSPEnable,
191 kSCPropNetProxiesRTSPProxy,
192 kSCPropNetProxiesRTSPPort,
193 "rtsp",
194 554);
195 validate_proxy_content(newProxies,
196 kSCPropNetProxiesSOCKSEnable,
197 kSCPropNetProxiesSOCKSProxy,
198 kSCPropNetProxiesSOCKSPort,
199 "socks",
200 1080);
201 validate_proxy_content(newProxies,
202 kSCPropNetProxiesProxyAutoConfigEnable,
203 kSCPropNetProxiesProxyAutoConfigURLString,
204 NULL,
205 NULL,
206 0);
207
208 // validate FTP passive setting
209 num = CFDictionaryGetValue(newProxies, kSCPropNetProxiesFTPPassive);
210 if (num != NULL) {
211 int enabled;
212
213 if (!isA_CFNumber(num) ||
214 !CFNumberGetValue(num, kCFNumberIntType, &enabled)) {
215 // if we don't like the enabled key/value
216 enabled = 1;
217 num = CFNumberCreate(NULL, kCFNumberIntType, &enabled);
218 CFDictionarySetValue(newProxies,
219 kSCPropNetProxiesFTPPassive,
220 num);
221 CFRelease(num);
222 }
223 }
224
225 // validate WPAD setting
226 num = CFDictionaryGetValue(newProxies, kSCPropNetProxiesProxyAutoDiscoveryEnable);
227 if (num != NULL) {
228 int enabled;
229
230 if (!isA_CFNumber(num) ||
231 !CFNumberGetValue(num, kCFNumberIntType, &enabled)) {
232 // if we don't like the enabled key/value
233 enabled = 0;
234 num = CFNumberCreate(NULL, kCFNumberIntType, &enabled);
235 CFDictionarySetValue(newProxies,
236 kSCPropNetProxiesProxyAutoDiscoveryEnable,
237 num);
238 CFRelease(num);
239 }
240 }
241
242 // validate proxy exception list
243 array = CFDictionaryGetValue(newProxies, kSCPropNetProxiesExceptionsList);
244 if (array != NULL) {
245 CFIndex i;
246 CFIndex n;
247
248 n = isA_CFArray(array) ? CFArrayGetCount(array) : 0;
249 for (i = 0; i < n; i++) {
250 CFStringRef str;
251
252 str = CFArrayGetValueAtIndex(array, i);
253 if (!isA_CFString(str)) {
254 // if we don't like the array contents
255 n = 0;
256 break;
257 }
258 }
259
260 if (n == 0) {
261 CFDictionaryRemoveValue(newProxies, kSCPropNetProxiesExceptionsList);
262 }
263 }
264
265 // validate exclude simple hostnames setting
266 num = CFDictionaryGetValue(newProxies, kSCPropNetProxiesExcludeSimpleHostnames);
267 if (num != NULL) {
268 int enabled;
269
270 if (!isA_CFNumber(num) ||
271 !CFNumberGetValue(num, kCFNumberIntType, &enabled)) {
272 // if we don't like the enabled key/value
273 enabled = 0;
274 num = CFNumberCreate(NULL, kCFNumberIntType, &enabled);
275 CFDictionarySetValue(newProxies,
276 kSCPropNetProxiesExcludeSimpleHostnames,
277 num);
278 CFRelease(num);
279 }
280 }
281
282
283 proxies = CFDictionaryCreateCopy(NULL, newProxies);
284 CFRelease(newProxies);
285
286 if (tempSession) CFRelease(store);
287 return proxies;
288 }