2 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: fsaccess.c,v 1.15 2007/06/19 23:47:19 tbox Exp $ */
21 * Note that Win32 does not have the concept of files having access
22 * and ownership bits. The FAT File system only has a readonly flag
23 * for everyone and that's all. NTFS uses ACL's which is a totally
24 * different concept of controlling access.
26 * This code needs to be revisited to set up proper access control for
27 * NTFS file systems. Nothing can be done for FAT file systems.
34 #include <sys/types.h>
42 #include "errno2result.h"
45 * The OS-independent part of the API is in lib/isc.
47 #include "../fsaccess.c"
49 /* Store the user account name locally */
50 static char username[255] = "\0";
51 static DWORD namelen = 0;
54 * In order to set or retrieve access information, we need to obtain
55 * the File System type. These could be UNC-type shares.
59 is_ntfs(const char * file) {
68 REQUIRE(filename != NULL);
70 if (isc_file_absolutepath(file, filename,
71 sizeof(filename)) != ISC_R_SUCCESS) {
76 * Look for c:\path\... style, c:/path/... or \\computer\shar\path...
77 * the UNC style file specs
79 if (isalpha(filename[0]) && filename[1] == ':' &&
80 (filename[2] == '\\' || filename[2] == '/')) {
81 strncpy(drive, filename, 3);
85 else if ((filename[0] == '\\') && (filename[1] == '\\')) {
86 /* Find the machine and share name and rebuild the UNC */
87 strcpy(tmpbuf, filename);
88 machinename = strtok(tmpbuf, "\\");
89 sharename = strtok(NULL, "\\");
90 strcpy(drive, "\\\\");
91 strcat(drive, machinename);
93 strcat(drive, sharename);
97 else /* Not determinable */
100 GetVolumeInformation(drive, NULL, 0, NULL, 0, NULL, FSType,
102 if(strcmp(FSType,"NTFS") == 0)
109 * If it's not NTFS, we assume that it is FAT and proceed
110 * with almost nothing to do. Only the write flag can be set or
114 FAT_fsaccess_set(const char *path, isc_fsaccess_t access) {
119 * Done with checking bad bits. Set mode_t.
123 #define SET_AND_CLEAR1(modebit) \
124 if ((access & bits) != 0) { \
128 #define SET_AND_CLEAR(user, group, other) \
129 SET_AND_CLEAR1(user); \
131 SET_AND_CLEAR1(group); \
133 SET_AND_CLEAR1(other);
135 bits = ISC_FSACCESS_READ | ISC_FSACCESS_LISTDIRECTORY;
137 SET_AND_CLEAR(S_IRUSR, S_IRGRP, S_IROTH);
139 bits = ISC_FSACCESS_WRITE |
140 ISC_FSACCESS_CREATECHILD |
141 ISC_FSACCESS_DELETECHILD;
143 SET_AND_CLEAR(S_IWUSR, S_IWGRP, S_IWOTH);
147 if (_chmod(path, mode) < 0)
148 return (isc__errno2result(errno));
150 return (ISC_R_SUCCESS);
154 NTFS_Access_Control(const char *filename, const char *user, int access,
155 isc_boolean_t isdir) {
156 SECURITY_DESCRIPTOR sd;
157 BYTE aclBuffer[1024];
158 PACL pacl=(PACL)&aclBuffer;
160 PSID psid=(PSID) &sidBuffer;
161 DWORD sidBufferSize = sizeof(sidBuffer);
162 BYTE adminSidBuffer[100];
163 PSID padminsid=(PSID) &adminSidBuffer;
164 DWORD adminSidBufferSize = sizeof(adminSidBuffer);
165 BYTE otherSidBuffer[100];
166 PSID pothersid=(PSID) &otherSidBuffer;
167 DWORD otherSidBufferSize = sizeof(otherSidBuffer);
168 char domainBuffer[100];
169 DWORD domainBufferSize = sizeof(domainBuffer);
176 /* Initialize an ACL */
177 if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
178 return (ISC_R_NOPERM);
179 if (!InitializeAcl(pacl, sizeof(aclBuffer), ACL_REVISION))
180 return (ISC_R_NOPERM);
181 if (!LookupAccountName(0, user, psid, &sidBufferSize, domainBuffer,
182 &domainBufferSize, &snu))
183 return (ISC_R_NOPERM);
184 domainBufferSize = sizeof(domainBuffer);
185 if (!LookupAccountName(0, "Administrators", padminsid,
186 &adminSidBufferSize, domainBuffer, &domainBufferSize, &snu)) {
187 errval = GetLastError();
188 return (ISC_R_NOPERM);
190 domainBufferSize = sizeof(domainBuffer);
191 if (!LookupAccountName(0, "Everyone", pothersid,
192 &otherSidBufferSize, domainBuffer, &domainBufferSize, &snu)) {
193 errval = GetLastError();
194 return (ISC_R_NOPERM);
201 if (caccess & ISC_FSACCESS_READ)
202 NTFSbits |= FILE_GENERIC_READ;
203 if (caccess & ISC_FSACCESS_WRITE)
204 NTFSbits |= FILE_GENERIC_WRITE;
205 if (caccess & ISC_FSACCESS_EXECUTE)
206 NTFSbits |= FILE_GENERIC_EXECUTE;
208 /* For directories check the directory-specific bits */
209 if (isdir == ISC_TRUE) {
210 if (caccess & ISC_FSACCESS_CREATECHILD)
211 NTFSbits |= FILE_ADD_SUBDIRECTORY | FILE_ADD_FILE;
212 if (caccess & ISC_FSACCESS_DELETECHILD)
213 NTFSbits |= FILE_DELETE_CHILD;
214 if (caccess & ISC_FSACCESS_LISTDIRECTORY)
215 NTFSbits |= FILE_LIST_DIRECTORY;
216 if (caccess & ISC_FSACCESS_ACCESSCHILD)
217 NTFSbits |= FILE_TRAVERSE;
220 if (NTFSbits == (FILE_GENERIC_READ | FILE_GENERIC_WRITE
221 | FILE_GENERIC_EXECUTE))
222 NTFSbits |= FILE_ALL_ACCESS;
224 * Owner and Administrator also get STANDARD_RIGHTS_ALL
225 * to ensure that they have full control
228 NTFSbits |= STANDARD_RIGHTS_ALL;
230 /* Add the ACE to the ACL */
231 if (!AddAccessAllowedAce(pacl, ACL_REVISION, NTFSbits, psid))
232 return (ISC_R_NOPERM);
233 if (!AddAccessAllowedAce(pacl, ACL_REVISION, NTFSbits, padminsid))
234 return (ISC_R_NOPERM);
237 * Group is ignored since we can be in multiple groups or no group
238 * and its meaning is not clear on Win32
241 caccess = caccess >> STEP;
244 * Other check. We translate this to be the same as Everyone
247 caccess = caccess >> STEP;
250 if (caccess & ISC_FSACCESS_READ)
251 NTFSbits |= FILE_GENERIC_READ;
252 if (caccess & ISC_FSACCESS_WRITE)
253 NTFSbits |= FILE_GENERIC_WRITE;
254 if (caccess & ISC_FSACCESS_EXECUTE)
255 NTFSbits |= FILE_GENERIC_EXECUTE;
257 /* For directories check the directory-specific bits */
259 if (caccess & ISC_FSACCESS_CREATECHILD)
260 NTFSbits |= FILE_ADD_SUBDIRECTORY | FILE_ADD_FILE;
261 if (caccess & ISC_FSACCESS_DELETECHILD)
262 NTFSbits |= FILE_DELETE_CHILD;
263 if (caccess & ISC_FSACCESS_LISTDIRECTORY)
264 NTFSbits |= FILE_LIST_DIRECTORY;
265 if (caccess & ISC_FSACCESS_ACCESSCHILD)
266 NTFSbits |= FILE_TRAVERSE;
268 /* Add the ACE to the ACL */
269 if (!AddAccessAllowedAce(pacl, ACL_REVISION, NTFSbits,
271 return (ISC_R_NOPERM);
273 if (!SetSecurityDescriptorDacl(&sd, TRUE, pacl, FALSE))
274 return (ISC_R_NOPERM);
275 if (!SetFileSecurity(filename, DACL_SECURITY_INFORMATION, &sd)) {
276 return (ISC_R_NOPERM);
279 return(ISC_R_SUCCESS);
283 NTFS_fsaccess_set(const char *path, isc_fsaccess_t access,
284 isc_boolean_t isdir){
287 * For NTFS we first need to get the name of the account under
288 * which BIND is running
291 namelen = sizeof(username);
292 if (GetUserName(username, &namelen) == 0)
293 return (ISC_R_FAILURE);
295 return (NTFS_Access_Control(path, username, access, isdir));
299 isc_fsaccess_set(const char *path, isc_fsaccess_t access) {
301 isc_boolean_t is_dir = ISC_FALSE;
304 if (stat(path, &statb) != 0)
305 return (isc__errno2result(errno));
307 if ((statb.st_mode & S_IFDIR) != 0)
309 else if ((statb.st_mode & S_IFREG) == 0)
310 return (ISC_R_INVALIDFILE);
312 result = check_bad_bits(access, is_dir);
313 if (result != ISC_R_SUCCESS)
317 * Determine if this is a FAT or NTFS disk and
318 * call the appropriate function to set the permissions
321 return (NTFS_fsaccess_set(path, access, is_dir));
323 return (FAT_fsaccess_set(path, access));
327 isc_fsaccess_changeowner(const char *filename, const char *user) {
328 SECURITY_DESCRIPTOR psd;
330 BYTE groupBuffer[500];
331 PSID psid=(PSID) &sidBuffer;
332 DWORD sidBufferSize = sizeof(sidBuffer);
333 char domainBuffer[100];
334 DWORD domainBufferSize = sizeof(domainBuffer);
336 PSID pSidGroup = (PSID) &groupBuffer;
337 DWORD groupBufferSize = sizeof(groupBuffer);
341 * Determine if this is a FAT or NTFS disk and
342 * call the appropriate function to set the ownership
343 * FAT disks do not have ownership attributes so it's
346 if (is_ntfs(filename) == FALSE)
347 return (ISC_R_SUCCESS);
349 if (!InitializeSecurityDescriptor(&psd, SECURITY_DESCRIPTOR_REVISION))
350 return (ISC_R_NOPERM);
352 if (!LookupAccountName(0, user, psid, &sidBufferSize, domainBuffer,
353 &domainBufferSize, &snu))
354 return (ISC_R_NOPERM);
356 /* Make sure administrators can get to it */
357 domainBufferSize = sizeof(domainBuffer);
358 if (!LookupAccountName(0, "Administrators", pSidGroup,
359 &groupBufferSize, domainBuffer, &domainBufferSize, &snu))
360 return (ISC_R_NOPERM);
362 if (!SetSecurityDescriptorOwner(&psd, psid, FALSE))
363 return (ISC_R_NOPERM);
365 if (!SetSecurityDescriptorGroup(&psd, pSidGroup, FALSE))
366 return (ISC_R_NOPERM);
368 if (!SetFileSecurity(filename,
369 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION,
371 return (ISC_R_NOPERM);
373 return (ISC_R_SUCCESS);