]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/CaptiveNetwork.c
configd-963.200.27.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / CaptiveNetwork.c
1 /*
2 * Copyright (c) 2009, 2010, 2012, 2013, 2015, 2018 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 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/stat.h>
28 #include <dlfcn.h>
29 #include <mach-o/dyld_priv.h>
30 #include <sys/codesign.h>
31
32 #include <SystemConfiguration/CaptiveNetwork.h>
33 #include <SystemConfiguration/SCPrivate.h>
34
35
36 #pragma mark -
37 #pragma mark CaptiveNetwork.framework APIs (exported through the SystemConfiguration.framework)
38
39
40 const CFStringRef kCNNetworkInfoKeySSIDData = CFSTR("SSIDDATA");
41 const CFStringRef kCNNetworkInfoKeySSID = CFSTR("SSID");
42 const CFStringRef kCNNetworkInfoKeyBSSID = CFSTR("BSSID");
43
44
45 static void *
46 __loadCaptiveNetwork(void) {
47 static void *image = NULL;
48 static dispatch_once_t once;
49
50 dispatch_once(&once, ^{
51 image = _SC_dlopen("/System/Library/PrivateFrameworks/CaptiveNetwork.framework/CaptiveNetwork");
52 });
53
54 return image;
55 }
56
57
58 Boolean
59 CNSetSupportedSSIDs(CFArrayRef ssidArray)
60 {
61 static typeof (CNSetSupportedSSIDs) *dyfunc = NULL;
62 if (!dyfunc) {
63 void *image = __loadCaptiveNetwork();
64 if (image) dyfunc = dlsym(image, "__CNSetSupportedSSIDs");
65 }
66 return dyfunc ? dyfunc(ssidArray) : FALSE;
67 }
68
69
70 Boolean
71 CNMarkPortalOnline(CFStringRef interfaceName)
72 {
73 static typeof (CNMarkPortalOnline) *dyfunc = NULL;
74 if (!dyfunc) {
75 void *image = __loadCaptiveNetwork();
76 if (image) dyfunc = dlsym(image, "__CNMarkPortalOnline");
77 }
78 return dyfunc ? dyfunc(interfaceName) : FALSE;
79 }
80
81
82 Boolean
83 CNMarkPortalOffline(CFStringRef interfaceName)
84 {
85 static typeof (CNMarkPortalOffline) *dyfunc = NULL;
86 if (!dyfunc) {
87 void *image = __loadCaptiveNetwork();
88 if (image) dyfunc = dlsym(image, "__CNMarkPortalOffline");
89 }
90 return dyfunc ? dyfunc(interfaceName) : FALSE;
91 }
92
93 CFArrayRef
94 CNCopySupportedInterfaces(void)
95 {
96 static typeof (CNCopySupportedInterfaces) *dyfunc = NULL;
97 if (!dyfunc) {
98 void *image = __loadCaptiveNetwork();
99 if (image) dyfunc = dlsym(image, "__CNCopySupportedInterfaces");
100 }
101 return dyfunc ? dyfunc() : NULL;
102 }
103
104 #if TARGET_OS_IPHONE
105
106 #define CN_COPY_ENTITLEMENT CFSTR("com.apple.developer.networking.wifi-info")
107
108 static CFDictionaryRef
109 __CopyEntitlementsForPID(pid_t pid)
110 {
111 uint8_t *buffer = NULL;
112 size_t bufferlen = 0L;
113 int64_t datalen = 0L;
114 CFDataRef cfdata = NULL;
115 struct csheader {
116 uint32_t magic;
117 uint32_t length;
118 } csheader = { 0, 0 };
119 int error = -1;
120 CFPropertyListRef plist = NULL;
121
122 /*
123 * Get the length of the actual entitlement data
124 */
125 error = csops(pid, CS_OPS_ENTITLEMENTS_BLOB, &csheader, sizeof(csheader));
126
127 if (error == -1 && errno == ERANGE) {
128 bufferlen = ntohl(csheader.length);
129 if (bufferlen > 1024 * 1024 || bufferlen < 8) {
130 errno = EINVAL;
131 goto out;
132 }
133 buffer = malloc(bufferlen);
134 if (buffer == NULL) {
135 goto out;
136 }
137 error = csops(pid, CS_OPS_ENTITLEMENTS_BLOB, buffer, bufferlen);
138 if (error < 0) {
139 goto out;
140 }
141 }
142
143 datalen = bufferlen - sizeof(csheader);
144
145 if (error == 0 && buffer && datalen > 0) {
146 cfdata = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, buffer + sizeof(csheader), datalen, kCFAllocatorNull);
147 if (cfdata == NULL) {
148 goto out;
149 }
150
151 plist = CFPropertyListCreateWithData(NULL, cfdata, kCFPropertyListImmutable, NULL, NULL);
152 if (!plist) {
153 SC_log(LOG_ERR, "Could not decode entitlements for pid %d", pid);
154 }
155 }
156 else {
157 SC_log(LOG_ERR, "Could not get valid codesigning data for pid %d. Error %d", pid, error);
158 }
159 out:
160 if (error < 0) {
161 SC_log(LOG_ERR, "Error getting entitlements for pid %d: %s", pid, strerror(errno));
162 }
163 if (cfdata != NULL) {
164 CFRelease(cfdata);
165 }
166 if (buffer != NULL) {
167 free(buffer);
168 }
169 if (plist && !isA_CFDictionary(plist)) {
170 SC_log(LOG_ERR, "Could not decode entitlements for pid %d as a dictionary.", pid);
171 CFRelease(plist);
172 plist = NULL;
173 }
174
175 return plist;
176 }
177
178 static Boolean
179 __isApplicationEntitled(void)
180 {
181 if (dyld_get_program_sdk_version() >= DYLD_IOS_VERSION_12_0) {
182 /* application is linked on or after iOS 12.0 SDK so it must have the entitlement */
183 CFTypeRef entitlement = NULL;
184 CFDictionaryRef entitlements = __CopyEntitlementsForPID(getpid());
185 if (entitlements != NULL) {
186 Boolean entitled = FALSE;
187 entitlement = CFDictionaryGetValue(entitlements, CN_COPY_ENTITLEMENT);
188 if(isA_CFBoolean(entitlement)) {
189 entitled = CFBooleanGetValue(entitlement);
190 }
191 CFRelease(entitlements);
192 return entitled;
193 }
194 /* application is linked on or after iOS 12.0 SDK but missing entitlement */
195 return FALSE;
196 }
197 /* application is linked before iOS 12.0 SDK */
198 return TRUE;
199 }
200
201 #endif /* TARGET_OS_IPHONE */
202
203 CFDictionaryRef
204 CNCopyCurrentNetworkInfo(CFStringRef interfaceName)
205 {
206 #if TARGET_OS_IPHONE && !TARGET_OS_IOSMAC
207 if (__isApplicationEntitled() == FALSE) {
208 SC_log(LOG_DEBUG, "Application does not have %@ entitlement", CN_COPY_ENTITLEMENT);
209 return NULL;
210 }
211 static typeof (CNCopyCurrentNetworkInfo) *dyfunc = NULL;
212 if (!dyfunc) {
213 void *image = __loadCaptiveNetwork();
214 if (image) dyfunc = dlsym(image, "__CNCopyCurrentNetworkInfo");
215 }
216 return dyfunc ? dyfunc(interfaceName) : NULL;
217 #else // TARGET_OS_IPHONE && !TARGET_OS_IOSMAC
218 #pragma unused(interfaceName)
219 return NULL;
220 #endif // TARGET_OS_IPHONE && !TARGET_OS_IOSMAC
221 }
222