]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCProxies.c
e15f541ed41211bf78690494b6a4933fa2d19dde
[apple/configd.git] / SystemConfiguration.fproj / SCProxies.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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
89 service = getservbyname(proxy_service, "tcp");
90 num = CFNumberCreate(NULL,
91 kCFNumberIntType,
92 (service != NULL) ? &service->s_port : &proxy_defaultport);
93 CFDictionarySetValue(proxies, proxy_port, num);
94 CFRelease(num);
95 }
96 }
97
98 return;
99
100 disable :
101
102 enabled = 0;
103 num = CFNumberCreate(NULL, kCFNumberIntType, &enabled);
104 CFDictionarySetValue(proxies, proxy_enable, num);
105 CFRelease(num);
106 CFDictionaryRemoveValue(proxies, proxy_host);
107 if (proxy_port != NULL) {
108 CFDictionaryRemoveValue(proxies, proxy_port);
109 }
110
111 return;
112 }
113
114
115 CFDictionaryRef
116 SCDynamicStoreCopyProxies(SCDynamicStoreRef store)
117 {
118 CFArrayRef array;
119 CFStringRef key;
120 CFMutableDictionaryRef newProxies = NULL;
121 CFNumberRef num;
122 CFDictionaryRef proxies;
123 Boolean tempSession = FALSE;
124
125
126 /* copy proxy information from dynamic store */
127
128 if (store == NULL) {
129 store = SCDynamicStoreCreate(NULL,
130 CFSTR("SCDynamicStoreCopyProxies"),
131 NULL,
132 NULL);
133 if (store == NULL) {
134 return NULL;
135 }
136 tempSession = TRUE;
137 }
138
139 key = SCDynamicStoreKeyCreateProxies(NULL);
140 proxies = SCDynamicStoreCopyValue(store, key);
141 CFRelease(key);
142
143 validate :
144
145 if (proxies != NULL) {
146 if (isA_CFDictionary(proxies)) {
147 newProxies = CFDictionaryCreateMutableCopy(NULL, 0, proxies);
148 }
149 CFRelease(proxies);
150 }
151
152 if (newProxies == NULL) {
153 newProxies = CFDictionaryCreateMutable(NULL,
154 0,
155 &kCFTypeDictionaryKeyCallBacks,
156 &kCFTypeDictionaryValueCallBacks);
157 }
158
159 /* validate [and augment] proxy content */
160
161 validate_proxy_content(newProxies,
162 kSCPropNetProxiesFTPEnable,
163 kSCPropNetProxiesFTPProxy,
164 kSCPropNetProxiesFTPPort,
165 "ftp",
166 21);
167 validate_proxy_content(newProxies,
168 kSCPropNetProxiesGopherEnable,
169 kSCPropNetProxiesGopherProxy,
170 kSCPropNetProxiesGopherPort,
171 "gopher",
172 70);
173 validate_proxy_content(newProxies,
174 kSCPropNetProxiesHTTPEnable,
175 kSCPropNetProxiesHTTPProxy,
176 kSCPropNetProxiesHTTPPort,
177 "http",
178 80);
179 validate_proxy_content(newProxies,
180 kSCPropNetProxiesHTTPSEnable,
181 kSCPropNetProxiesHTTPSProxy,
182 kSCPropNetProxiesHTTPSPort,
183 "https",
184 443);
185 validate_proxy_content(newProxies,
186 kSCPropNetProxiesRTSPEnable,
187 kSCPropNetProxiesRTSPProxy,
188 kSCPropNetProxiesRTSPPort,
189 "rtsp",
190 554);
191 validate_proxy_content(newProxies,
192 kSCPropNetProxiesSOCKSEnable,
193 kSCPropNetProxiesSOCKSProxy,
194 kSCPropNetProxiesSOCKSPort,
195 "socks",
196 1080);
197 validate_proxy_content(newProxies,
198 kSCPropNetProxiesProxyAutoConfigEnable,
199 kSCPropNetProxiesProxyAutoConfigURLString,
200 NULL,
201 NULL,
202 0);
203
204 // validate WPAD setting
205 num = CFDictionaryGetValue(newProxies, kSCPropNetProxiesProxyAutoDiscoveryEnable);
206 if (num != NULL) {
207 int enabled;
208
209 if (!isA_CFNumber(num) ||
210 !CFNumberGetValue(num, kCFNumberIntType, &enabled)) {
211 // if we don't like the enabled key/value
212 enabled = 0;
213 num = CFNumberCreate(NULL, kCFNumberIntType, &enabled);
214 CFDictionarySetValue(newProxies,
215 kSCPropNetProxiesProxyAutoDiscoveryEnable,
216 num);
217 CFRelease(num);
218 }
219 }
220
221 // validate proxy exception list
222 array = CFDictionaryGetValue(newProxies, kSCPropNetProxiesExceptionsList);
223 if (array != NULL) {
224 CFIndex i;
225 CFIndex n;
226
227 n = isA_CFArray(array) ? CFArrayGetCount(array) : 0;
228 for (i = 0; i < n; i++) {
229 CFStringRef str;
230
231 str = CFArrayGetValueAtIndex(array, i);
232 if (!isA_CFString(str)) {
233 // if we don't like the array contents
234 n = 0;
235 break;
236 }
237 }
238
239 if (n == 0) {
240 CFDictionaryRemoveValue(newProxies, kSCPropNetProxiesExceptionsList);
241 }
242 }
243
244 // validate exclude simple hostnames setting
245 num = CFDictionaryGetValue(newProxies, kSCPropNetProxiesExcludeSimpleHostnames);
246 if (num != NULL) {
247 int enabled;
248
249 if (!isA_CFNumber(num) ||
250 !CFNumberGetValue(num, kCFNumberIntType, &enabled)) {
251 // if we don't like the enabled key/value
252 enabled = 0;
253 num = CFNumberCreate(NULL, kCFNumberIntType, &enabled);
254 CFDictionarySetValue(newProxies,
255 kSCPropNetProxiesExcludeSimpleHostnames,
256 num);
257 CFRelease(num);
258 }
259 }
260
261
262 proxies = CFDictionaryCreateCopy(NULL, newProxies);
263 CFRelease(newProxies);
264
265 if (tempSession) CFRelease(store);
266 return proxies;
267 }