]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyviafd.c
configd-801.1.1.tar.gz
[apple/configd.git] / configd.tproj / _notifyviafd.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2006, 2008-2011, 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 * April 5, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <unistd.h>
35 #include <sys/ioctl.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/un.h>
40
41 #include "configd.h"
42 #include "session.h"
43
44
45 __private_extern__
46 int
47 __SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store,
48 int32_t identifier,
49 int *fd)
50 {
51 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
52 int sock;
53 CFStringRef sessionKey;
54 CFDictionaryRef info;
55
56 if (storePrivate->notifyStatus != NotifierNotRegistered) {
57 /* sorry, you can only have one notification registered at once */
58 return kSCStatusNotifierActive;
59 }
60
61 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
62 SC_log(LOG_ERR, "socket() failed: %s", strerror(errno));
63 return kSCStatusFailed;
64 }
65
66 *fd = sock;
67
68 /* push out a notification if any changes are pending */
69 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
70 info = CFDictionaryGetValue(sessionData, sessionKey);
71 CFRelease(sessionKey);
72 if (info && CFDictionaryContainsKey(info, kSCDChangedKeys)) {
73 CFNumberRef sessionNum;
74
75 if (needsNotification == NULL)
76 needsNotification = CFSetCreateMutable(NULL,
77 0,
78 &kCFTypeSetCallBacks);
79
80 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
81 CFSetAddValue(needsNotification, sessionNum);
82 CFRelease(sessionNum);
83 }
84
85 return kSCStatusOK;
86 }
87
88
89 __private_extern__
90 kern_return_t
91 _notifyviafd(mach_port_t server,
92 xmlData_t pathRef,
93 mach_msg_type_number_t pathLen,
94 int identifier,
95 int *sc_status
96 )
97 {
98 int bufSiz;
99 serverSessionRef mySession = getSession(server);
100 int nbioYes;
101 int sock;
102 SCDynamicStorePrivateRef storePrivate;
103 struct sockaddr_un un;
104
105 /*
106 * if socket currently open, close it!
107 */
108 /* validate the UNIX domain socket path */
109 if (pathLen > (sizeof(un.sun_path) - 1)) {
110 SC_log(LOG_INFO, "domain socket path length too long!");
111 (void) vm_deallocate(mach_task_self(), (vm_address_t)pathRef, pathLen);
112 *sc_status = kSCStatusFailed;
113 return KERN_SUCCESS;
114 }
115
116 /* un-serialize the UNIX domain socket path */
117 un.sun_family = AF_UNIX;
118 bcopy(pathRef, un.sun_path, pathLen);
119 un.sun_path[pathLen] = '\0';
120 (void) vm_deallocate(mach_task_self(), (vm_address_t)pathRef, pathLen);
121
122 if (mySession == NULL) {
123 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
124 return KERN_SUCCESS;
125 }
126 storePrivate = (SCDynamicStorePrivateRef)mySession->store;
127
128 /* check permissions */
129 if (!hasRootAccess(mySession)) {
130 struct stat statbuf;
131
132 bzero(&statbuf, sizeof(statbuf));
133 if (stat(un.sun_path, &statbuf) == -1) {
134 *sc_status = errno;
135 SC_log(LOG_INFO, "stat() failed: %s", strerror(errno));
136 return KERN_SUCCESS;
137 }
138 if (mySession->callerEUID != statbuf.st_uid) {
139 *sc_status = kSCStatusAccessError;
140 SC_log(LOG_INFO, "permissions error [eUID]");
141 return KERN_SUCCESS;
142 }
143 }
144
145 if (!hasPathAccess(mySession, un.sun_path)) {
146 *sc_status = kSCStatusAccessError;
147 SC_log(LOG_INFO, "permissions error [path]");
148 return KERN_SUCCESS;
149 }
150
151 /* do common sanity checks, get socket */
152 *sc_status = __SCDynamicStoreNotifyFileDescriptor(mySession->store, identifier, &sock);
153
154 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
155 if (*sc_status != kSCStatusOK) {
156 return KERN_SUCCESS;
157 }
158
159 /* establish the connection, get ready for a read() */
160 if (connect(sock, (struct sockaddr *)&un, sizeof(un)) == -1) {
161 *sc_status = errno;
162 SC_log(LOG_INFO, "connect() failed: %s", strerror(errno));
163 (void) close(sock);
164 return KERN_SUCCESS;
165 }
166
167 bufSiz = sizeof(storePrivate->notifyFileIdentifier);
168 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufSiz, sizeof(bufSiz)) == -1) {
169 *sc_status = errno;
170 SC_log(LOG_INFO, "setsockopt() failed: %s", strerror(errno));
171 (void) close(sock);
172 return KERN_SUCCESS;
173 }
174
175 nbioYes = 1;
176 if (ioctl(sock, FIONBIO, &nbioYes) == -1) {
177 *sc_status = errno;
178 SC_log(LOG_INFO, "ioctl(,FIONBIO,) failed: %s", strerror(errno));
179 (void) close(sock);
180 return KERN_SUCCESS;
181 }
182
183 /* set notifier active */
184 storePrivate->notifyStatus = Using_NotifierInformViaFD;
185 storePrivate->notifyFile = sock;
186 storePrivate->notifyFileIdentifier = identifier;
187
188 return KERN_SUCCESS;
189 }