]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configremove.c
configd-1061.120.2.tar.gz
[apple/configd.git] / configd.tproj / _configremove.c
1 /*
2 * Copyright (c) 2000-2004, 2006, 2008, 2011, 2012, 2015-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 __SCDynamicStoreRemoveValue(SCDynamicStoreRef store, CFStringRef key, Boolean internal)
40 {
41 CFDictionaryRef dict;
42 CFMutableDictionaryRef newDict;
43 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
44 int sc_status = kSCStatusOK;
45 CFStringRef sessionKey;
46
47 SC_trace("%s : %5d : %@",
48 internal ? "*remove" : "remove ",
49 storePrivate->server,
50 key);
51
52 /*
53 * Ensure that this key exists.
54 */
55 dict = CFDictionaryGetValue(storeData, key);
56 if ((dict == NULL) || !CFDictionaryContainsKey(dict, kSCDData)) {
57 /* key doesn't exist (or data never defined) */
58 sc_status = kSCStatusNoKey;
59 goto done;
60 }
61 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
62
63 /*
64 * Mark this key as "changed". Any "watchers" will be
65 * notified as soon as the lock is released.
66 */
67 CFSetAddValue(changedKeys, key);
68
69 /*
70 * Add this key to a deferred cleanup list so that, after
71 * the change notifications are posted, any associated
72 * regex keys can be removed.
73 */
74 CFSetAddValue(deferredRemovals, key);
75
76 /*
77 * Check if this is a session key and, if so, add it
78 * to the (session) removal list
79 */
80 sessionKey = CFDictionaryGetValue(newDict, kSCDSession);
81 if (sessionKey) {
82 CFStringRef removedKey;
83
84 /* We are no longer a session key! */
85 CFDictionaryRemoveValue(newDict, kSCDSession);
86
87 /* add this session key to the (session) removal list */
88 removedKey = CFStringCreateWithFormat(NULL, 0, CFSTR("%@:%@"), sessionKey, key);
89 CFSetAddValue(removedSessionKeys, removedKey);
90 CFRelease(removedKey);
91 }
92
93 /*
94 * Remove data and update/remove the dictionary store entry.
95 */
96 CFDictionaryRemoveValue(newDict, kSCDData);
97 if (CFDictionaryGetCount(newDict) > 0) {
98 /* this key is still being "watched" */
99 CFDictionarySetValue(storeData, key, newDict);
100 } else {
101 /* no information left, remove the empty dictionary */
102 CFDictionaryRemoveValue(storeData, key);
103 }
104 CFRelease(newDict);
105
106 if (!internal) {
107 /* push changes */
108 __SCDynamicStorePush();
109 }
110
111 done:
112
113 return sc_status;
114 }
115
116
117 __private_extern__
118 kern_return_t
119 _configremove(mach_port_t server,
120 xmlData_t keyRef, /* raw XML bytes */
121 mach_msg_type_number_t keyLen,
122 int *sc_status,
123 audit_token_t audit_token)
124 {
125 CFStringRef key = NULL; /* key (un-serialized) */
126 serverSessionRef mySession;
127
128 /* un-serialize the key */
129 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
130 *sc_status = kSCStatusFailed;
131 goto done;
132 }
133
134 if (!isA_CFString(key)) {
135 *sc_status = kSCStatusInvalidArgument;
136 goto done;
137 }
138
139 mySession = getSession(server);
140 if (mySession == NULL) {
141 mySession = tempSession(server, CFSTR("SCDynamicStoreRemoveValue"), audit_token);
142 if (mySession == NULL) {
143 /* you must have an open session to play */
144 *sc_status = kSCStatusNoStoreSession;
145 goto done;
146 }
147 }
148
149 if (!hasWriteAccess(mySession, "remove", key)) {
150 *sc_status = kSCStatusAccessError;
151 goto done;
152 }
153
154 *sc_status = __SCDynamicStoreRemoveValue(mySession->store, key, FALSE);
155
156 done :
157
158 if (key) CFRelease(key);
159
160 return KERN_SUCCESS;
161 }
162