]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configadd.c
configd-963.30.1.tar.gz
[apple/configd.git] / configd.tproj / _configadd.c
1 /*
2 * Copyright (c) 2000, 2001, 2003, 2004, 2006, 2008, 2011, 2012, 2014-2017 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 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include "configd.h"
35 #include "session.h"
36
37 __private_extern__
38 int
39 __SCDynamicStoreAddValue(SCDynamicStoreRef store, CFStringRef key, CFDataRef value)
40 {
41 int sc_status = kSCStatusOK;
42 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
43 CFDataRef tempValue;
44
45 SC_trace("add %s : %5d : %@",
46 storePrivate->useSessionKeys ? "t " : " ",
47 storePrivate->server,
48 key);
49
50 /*
51 * Ensure that this is a new key.
52 */
53 sc_status = __SCDynamicStoreCopyValue(store, key, &tempValue, TRUE);
54 switch (sc_status) {
55 case kSCStatusNoKey :
56 /* store key does not exist, proceed */
57 break;
58
59 case kSCStatusOK :
60 /* store key exists, sorry */
61 CFRelease(tempValue);
62 sc_status = kSCStatusKeyExists;
63 goto done;
64
65 default :
66 #ifdef DEBUG
67 SC_log(LOG_DEBUG, "__SCDynamicStoreCopyValue() failed: %s", SCErrorString(sc_status));
68 #endif /* DEBUG */
69 goto done;
70 }
71
72 /*
73 * Save the new key.
74 */
75 sc_status = __SCDynamicStoreSetValue(store, key, value, TRUE);
76
77 /* push changes */
78 __SCDynamicStorePush();
79
80 done:
81
82 return sc_status;
83 }
84
85
86 __private_extern__
87 kern_return_t
88 _configadd(mach_port_t server,
89 xmlData_t keyRef, /* raw XML bytes */
90 mach_msg_type_number_t keyLen,
91 xmlData_t dataRef, /* raw XML bytes */
92 mach_msg_type_number_t dataLen,
93 int *newInstance,
94 int *sc_status,
95 audit_token_t audit_token)
96 {
97 CFStringRef key = NULL; /* key (un-serialized) */
98 CFDataRef data = NULL; /* data (un-serialized) */
99 serverSessionRef mySession;
100
101 *sc_status = kSCStatusOK;
102
103 /* un-serialize the key */
104 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
105 *sc_status = kSCStatusFailed;
106 goto done;
107 }
108
109 /* un-serialize the data */
110 if (!_SCUnserializeData(&data, (void *)dataRef, dataLen)) {
111 *sc_status = kSCStatusFailed;
112 }
113
114 if (*sc_status != kSCStatusOK) {
115 goto done;
116 }
117
118 if (!isA_CFString(key)) {
119 *sc_status = kSCStatusInvalidArgument;
120 goto done;
121 }
122
123 mySession = getSession(server);
124 if (mySession == NULL) {
125 mySession = tempSession(server, CFSTR("SCDynamicStoreAddValue"), audit_token);
126 if (mySession == NULL) {
127 /* you must have an open session to play */
128 *sc_status = kSCStatusNoStoreSession;
129 goto done;
130 }
131 }
132
133 if (!hasWriteAccess(mySession, "add", key)) {
134 *sc_status = kSCStatusAccessError;
135 goto done;
136 }
137
138 *sc_status = __SCDynamicStoreAddValue(mySession->store, key, data);
139 if (*sc_status == kSCStatusOK) {
140 *newInstance = 0;
141 }
142
143 done :
144
145 if (key != NULL) CFRelease(key);
146 if (data != NULL) CFRelease(data);
147
148 return KERN_SUCCESS;
149 }
150
151
152 __private_extern__
153 kern_return_t
154 _configadd_s(mach_port_t server,
155 xmlData_t keyRef, /* raw XML bytes */
156 mach_msg_type_number_t keyLen,
157 xmlData_t dataRef, /* raw XML bytes */
158 mach_msg_type_number_t dataLen,
159 int *newInstance,
160 int *sc_status)
161 {
162 CFDataRef data = NULL; /* data (un-serialized) */
163 CFStringRef key = NULL; /* key (un-serialized) */
164 serverSessionRef mySession;
165 SCDynamicStorePrivateRef storePrivate;
166 Boolean useSessionKeys;
167
168 *sc_status = kSCStatusOK;
169
170 /* un-serialize the key */
171 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
172 *sc_status = kSCStatusFailed;
173 }
174
175 /* un-serialize the data */
176 if (!_SCUnserializeData(&data, (void *)dataRef, dataLen)) {
177 *sc_status = kSCStatusFailed;
178 }
179
180 if (*sc_status != kSCStatusOK) {
181 goto done;
182 }
183
184 if (!isA_CFString(key)) {
185 *sc_status = kSCStatusInvalidArgument;
186 goto done;
187 }
188
189 mySession = getSession(server);
190 if (mySession == NULL) {
191 /* you must have an open session to play */
192 *sc_status = kSCStatusNoStoreSession;
193 goto done;
194 }
195
196 if (!hasWriteAccess(mySession, "add (session)", key)) {
197 *sc_status = kSCStatusAccessError;
198 goto done;
199 }
200
201 // force "useSessionKeys"
202 storePrivate = (SCDynamicStorePrivateRef)mySession->store;
203 useSessionKeys = storePrivate->useSessionKeys;
204 storePrivate->useSessionKeys = TRUE;
205
206 *sc_status = __SCDynamicStoreAddValue(mySession->store, key, data);
207 if (*sc_status == kSCStatusOK) {
208 *newInstance = 0;
209 }
210
211 // restore "useSessionKeys"
212 storePrivate->useSessionKeys = useSessionKeys;
213
214 done :
215
216 if (key != NULL) CFRelease(key);
217 if (data != NULL) CFRelease(data);
218
219 return KERN_SUCCESS;
220 }