Merge from vendor branch GROFF:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / fsaccess.c
1 /*
2  * Copyright (C) 2004  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.5.2.1 2004/03/09 06:11:46 marka Exp $ */
19
20 /*
21  * This file contains the OS-independent functionality of the API.
22  */
23 #include <isc/fsaccess.h>
24 #include <isc/result.h>
25 #include <isc/util.h>
26
27 /*
28  * Shorthand.  Maybe ISC__FSACCESS_PERMISSIONBITS should not even be in
29  * <isc/fsaccess.h>.  Could check consistency with sizeof(isc_fsaccess_t)
30  * and the number of bits in each function.
31  */
32 #define STEP            (ISC__FSACCESS_PERMISSIONBITS)
33 #define GROUP           (STEP)
34 #define OTHER           (STEP * 2)
35
36 void
37 isc_fsaccess_add(int trustee, int permission, isc_fsaccess_t *access) {
38         REQUIRE(trustee <= 0x7);
39         REQUIRE(permission <= 0xFF);
40
41         if ((trustee & ISC_FSACCESS_OWNER) != 0)
42                 *access |= permission;
43
44         if ((trustee & ISC_FSACCESS_GROUP) != 0)
45                 *access |= (permission << GROUP);
46
47         if ((trustee & ISC_FSACCESS_OTHER) != 0)
48                 *access |= (permission << OTHER);
49 }
50
51 void
52 isc_fsaccess_remove(int trustee, int permission, isc_fsaccess_t *access) {
53         REQUIRE(trustee <= 0x7);
54         REQUIRE(permission <= 0xFF);
55
56
57         if ((trustee & ISC_FSACCESS_OWNER) != 0)
58                 *access &= ~permission;
59
60         if ((trustee & ISC_FSACCESS_GROUP) != 0)
61                 *access &= ~(permission << GROUP);
62
63         if ((trustee & ISC_FSACCESS_OTHER) != 0)
64                 *access &= ~(permission << OTHER);
65 }
66
67 static isc_result_t
68 check_bad_bits(isc_fsaccess_t access, isc_boolean_t is_dir) {
69         isc_fsaccess_t bits;
70
71         /*
72          * Check for disallowed user bits.
73          */
74         if (is_dir)
75                 bits = ISC_FSACCESS_READ |
76                        ISC_FSACCESS_WRITE |
77                        ISC_FSACCESS_EXECUTE;
78         else
79                 bits = ISC_FSACCESS_CREATECHILD |
80                        ISC_FSACCESS_ACCESSCHILD |
81                        ISC_FSACCESS_DELETECHILD |
82                        ISC_FSACCESS_LISTDIRECTORY;
83
84         /*
85          * Set group bad bits.
86          */
87         bits |= bits << STEP;
88         /*
89          * Set other bad bits.
90          */
91         bits |= bits << STEP;
92
93         if ((access & bits) != 0) {
94                 if (is_dir)
95                         return (ISC_R_NOTFILE);
96                 else
97                         return (ISC_R_NOTDIRECTORY);
98         }
99
100         return (ISC_R_SUCCESS);
101 }