Merge from vendor branch LESS:
[dragonfly.git] / contrib / bind-9.3 / lib / isc / unix / fsaccess.c
1 /*
2  * Copyright (C) 2004, 2006  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: fsaccess.c,v 1.6.206.3 2006/08/25 05:25:50 marka Exp $ */
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #include <errno.h>
26
27 #include "errno2result.h"
28
29 /*
30  * The OS-independent part of the API is in lib/isc.
31  */
32 #include "../fsaccess.c"
33
34 isc_result_t
35 isc_fsaccess_set(const char *path, isc_fsaccess_t access) {
36         struct stat statb;
37         mode_t mode;
38         isc_boolean_t is_dir = ISC_FALSE;
39         isc_fsaccess_t bits;
40         isc_result_t result;
41
42         if (stat(path, &statb) != 0)
43                 return (isc__errno2result(errno));
44
45         if ((statb.st_mode & S_IFDIR) != 0)
46                 is_dir = ISC_TRUE;
47         else if ((statb.st_mode & S_IFREG) == 0)
48                 return (ISC_R_INVALIDFILE);
49
50         result = check_bad_bits(access, is_dir);
51         if (result != ISC_R_SUCCESS)
52                 return (result);
53
54         /*
55          * Done with checking bad bits.  Set mode_t.
56          */
57         mode = 0;
58
59 #define SET_AND_CLEAR1(modebit) \
60         if ((access & bits) != 0) { \
61                 mode |= modebit; \
62                 access &= ~bits; \
63         }
64 #define SET_AND_CLEAR(user, group, other) \
65         SET_AND_CLEAR1(user); \
66         bits <<= STEP; \
67         SET_AND_CLEAR1(group); \
68         bits <<= STEP; \
69         SET_AND_CLEAR1(other);
70
71         bits = ISC_FSACCESS_READ | ISC_FSACCESS_LISTDIRECTORY;
72
73         SET_AND_CLEAR(S_IRUSR, S_IRGRP, S_IROTH);
74
75         bits = ISC_FSACCESS_WRITE |
76                ISC_FSACCESS_CREATECHILD |
77                ISC_FSACCESS_DELETECHILD;
78
79         SET_AND_CLEAR(S_IWUSR, S_IWGRP, S_IWOTH);
80
81         bits = ISC_FSACCESS_EXECUTE |
82                ISC_FSACCESS_ACCESSCHILD;
83
84         SET_AND_CLEAR(S_IXUSR, S_IXGRP, S_IXOTH);
85
86         INSIST(access == 0);
87
88         if (chmod(path, mode) < 0)
89                 return (isc__errno2result(errno));
90
91         return (ISC_R_SUCCESS);
92 }