]> git.saurik.com Git - apple/configd.git/blob - dnsinfo/dnsinfo_server.c
configd-453.16.tar.gz
[apple/configd.git] / dnsinfo / dnsinfo_server.c
1 /*
2 * Copyright (c) 2004-2008, 2011 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 * March 9, 2004 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31 #include <notify.h>
32 #include <sysexits.h>
33 #include <syslog.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <servers/bootstrap.h>
37 #include <mach/mach.h>
38 #include <mach/mach_error.h>
39 #include <bsm/libbsm.h>
40
41 #include <CoreFoundation/CoreFoundation.h>
42 #include <SystemConfiguration/SCPrivate.h>
43
44 #include "dnsinfo_server.h"
45 #include "dnsinfo_private.h"
46 #include <network_information.h>
47
48 static CFDataRef shared_dns_info = NULL;
49 static CFDataRef shared_nwi_state = NULL;
50
51 __private_extern__
52 kern_return_t
53 _shared_dns_infoGet(mach_port_t server, dnsDataOut_t *dataRef, mach_msg_type_number_t *dataLen)
54 {
55 *dataRef = NULL;
56 *dataLen = 0;
57
58 if (shared_dns_info != NULL) {
59 CFIndex len;
60 Boolean ok;
61
62 ok = _SCSerializeData(shared_dns_info, (void **)dataRef, &len);
63 *dataLen = len;
64 if (!ok) {
65 return KERN_FAILURE;
66 }
67 }
68
69 return KERN_SUCCESS;
70 }
71
72
73 static
74 kern_return_t
75 _shared_infoSet_common(mach_port_t server,
76 dnsData_t dataRef,
77 mach_msg_type_number_t dataLen,
78 audit_token_t audit_token,
79 CFDataRef* cache,
80 const char* notify_key)
81 {
82 uid_t euid = 0;
83 CFDataRef new_info = NULL;
84 CFDataRef n_cache = *cache;
85
86 if ((dataRef != NULL) && (dataLen > 0)) {
87 if (!_SCUnserializeData(&new_info, (void *)dataRef, dataLen)) {
88 goto error;
89 }
90 }
91
92 audit_token_to_au32(audit_token,
93 NULL, // auidp
94 &euid, // euid
95 NULL, // egid
96 NULL, // ruid
97 NULL, // rgid
98 NULL, // pid
99 NULL, // asid
100 NULL); // tid
101 if (euid != 0) {
102 goto error;
103 }
104
105 if ((n_cache != NULL) &&
106 (new_info != NULL) &&
107 CFEqual(n_cache, new_info)) {
108 CFRelease(new_info);
109 return KERN_SUCCESS;
110 }
111
112 if (n_cache != NULL) CFRelease(n_cache);
113 *cache = new_info;
114
115 if (notify_key != NULL) {
116 uint32_t status;
117
118 status = notify_post(notify_key);
119 if (status != NOTIFY_STATUS_OK) {
120 SCLog(TRUE, LOG_ERR, CFSTR("notify_post() failed: %d"), status);
121 // notification posting failures are non-fatal
122 }
123 }
124
125 return KERN_SUCCESS;
126
127 error :
128
129 if (new_info != NULL) CFRelease(new_info);
130 return KERN_FAILURE;
131 }
132
133
134 __private_extern__
135 kern_return_t
136 _shared_dns_infoSet(mach_port_t server,
137 dnsData_t dataRef,
138 mach_msg_type_number_t dataLen,
139 audit_token_t audit_token)
140 {
141 const char *notify_key;
142
143 notify_key = _dns_configuration_notify_key();
144 return _shared_infoSet_common(server, dataRef, dataLen,
145 audit_token, &shared_dns_info,
146 notify_key);
147 }
148
149
150 __private_extern__
151 kern_return_t
152 _shared_nwi_stateGet(mach_port_t server, dnsDataOut_t *dataRef, mach_msg_type_number_t *dataLen)
153 {
154 *dataRef = NULL;
155 *dataLen = 0;
156
157 if (shared_nwi_state != NULL) {
158 CFIndex len;
159 Boolean ok;
160
161 ok = _SCSerializeData(shared_nwi_state, (void **)dataRef, &len);
162 *dataLen = len;
163 if (!ok) {
164 return KERN_FAILURE;
165 }
166 }
167
168 return KERN_SUCCESS;
169
170 }
171
172
173 __private_extern__
174 kern_return_t
175 _shared_nwi_stateSet(mach_port_t server,
176 dnsData_t dataRef,
177 mach_msg_type_number_t dataLen,
178 audit_token_t audit_token)
179 {
180 const char *notify_key;
181
182 notify_key = nwi_state_get_notify_key();
183
184 return _shared_infoSet_common(server, dataRef, dataLen,
185 audit_token, &shared_nwi_state,
186 notify_key);
187 }