Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / unix / fsaccess.c
blob06c94e5e4f74db9521aeab4f56c02fe4640c0cc8
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.6.2.1 2004/03/09 06:12:10 marka Exp $ */
20 #include <sys/types.h>
21 #include <sys/stat.h>
23 #include <errno.h>
25 #include "errno2result.h"
28 * The OS-independent part of the API is in lib/isc.
30 #include "../fsaccess.c"
32 isc_result_t
33 isc_fsaccess_set(const char *path, isc_fsaccess_t access) {
34 struct stat statb;
35 mode_t mode;
36 isc_boolean_t is_dir = ISC_FALSE;
37 isc_fsaccess_t bits;
38 isc_result_t result;
40 if (stat(path, &statb) != 0)
41 return (isc__errno2result(errno));
43 if ((statb.st_mode & S_IFDIR) != 0)
44 is_dir = ISC_TRUE;
45 else if ((statb.st_mode & S_IFREG) == 0)
46 return (ISC_R_INVALIDFILE);
48 result = check_bad_bits(access, is_dir);
49 if (result != ISC_R_SUCCESS)
50 return (result);
53 * Done with checking bad bits. Set mode_t.
55 mode = 0;
57 #define SET_AND_CLEAR1(modebit) \
58 if ((access & bits) != 0) { \
59 mode |= modebit; \
60 access &= ~bits; \
62 #define SET_AND_CLEAR(user, group, other) \
63 SET_AND_CLEAR1(user); \
64 bits <<= STEP; \
65 SET_AND_CLEAR1(group); \
66 bits <<= STEP; \
67 SET_AND_CLEAR1(other);
69 bits = ISC_FSACCESS_READ | ISC_FSACCESS_LISTDIRECTORY;
71 SET_AND_CLEAR(S_IRUSR, S_IRGRP, S_IROTH);
73 bits = ISC_FSACCESS_WRITE |
74 ISC_FSACCESS_CREATECHILD |
75 ISC_FSACCESS_DELETECHILD;
77 SET_AND_CLEAR(S_IWUSR, S_IWGRP, S_IWOTH);
79 bits = ISC_FSACCESS_EXECUTE |
80 ISC_FSACCESS_ACCESSCHILD;
82 SET_AND_CLEAR(S_IXUSR, S_IXGRP, S_IXOTH);
84 INSIST(access == 0);
86 if (chmod(path, mode) < 0)
87 return (isc__errno2result(errno));
89 return (ISC_R_SUCCESS);