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