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