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