1 /* $NetBSD: fsaccess.c,v 1.4 2014/12/10 04:37:59 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /* Id: fsaccess.c,v 1.10 2007/06/19 23:47:17 tbox Exp */
21
22 /*! \file
23 * \brief
24 * This file contains the OS-independent functionality of the API.
25 */
26 #include <isc/fsaccess.h>
27 #include <isc/result.h>
28 #include <isc/util.h>
29
30 /*!
31 * Shorthand. Maybe ISC__FSACCESS_PERMISSIONBITS should not even be in
32 * <isc/fsaccess.h>. Could check consistency with sizeof(isc_fsaccess_t)
33 * and the number of bits in each function.
34 */
35 #define STEP (ISC__FSACCESS_PERMISSIONBITS)
36 #define GROUP (STEP)
37 #define OTHER (STEP * 2)
38
39 void
isc_fsaccess_add(int trustee,int permission,isc_fsaccess_t * access)40 isc_fsaccess_add(int trustee, int permission, isc_fsaccess_t *access) {
41 REQUIRE(trustee <= 0x7);
42 REQUIRE(permission <= 0xFF);
43
44 if ((trustee & ISC_FSACCESS_OWNER) != 0)
45 *access |= permission;
46
47 if ((trustee & ISC_FSACCESS_GROUP) != 0)
48 *access |= (permission << GROUP);
49
50 if ((trustee & ISC_FSACCESS_OTHER) != 0)
51 *access |= (permission << OTHER);
52 }
53
54 void
isc_fsaccess_remove(int trustee,int permission,isc_fsaccess_t * access)55 isc_fsaccess_remove(int trustee, int permission, isc_fsaccess_t *access) {
56 REQUIRE(trustee <= 0x7);
57 REQUIRE(permission <= 0xFF);
58
59
60 if ((trustee & ISC_FSACCESS_OWNER) != 0)
61 *access &= ~permission;
62
63 if ((trustee & ISC_FSACCESS_GROUP) != 0)
64 *access &= ~(permission << GROUP);
65
66 if ((trustee & ISC_FSACCESS_OTHER) != 0)
67 *access &= ~(permission << OTHER);
68 }
69
70 static isc_result_t
check_bad_bits(isc_fsaccess_t access,isc_boolean_t is_dir)71 check_bad_bits(isc_fsaccess_t access, isc_boolean_t is_dir) {
72 isc_fsaccess_t bits;
73
74 /*
75 * Check for disallowed user bits.
76 */
77 if (is_dir)
78 bits = ISC_FSACCESS_READ |
79 ISC_FSACCESS_WRITE |
80 ISC_FSACCESS_EXECUTE;
81 else
82 bits = ISC_FSACCESS_CREATECHILD |
83 ISC_FSACCESS_ACCESSCHILD |
84 ISC_FSACCESS_DELETECHILD |
85 ISC_FSACCESS_LISTDIRECTORY;
86
87 /*
88 * Set group bad bits.
89 */
90 bits |= bits << STEP;
91 /*
92 * Set other bad bits.
93 */
94 bits |= bits << STEP;
95
96 if ((access & bits) != 0) {
97 if (is_dir)
98 return (ISC_R_NOTFILE);
99 else
100 return (ISC_R_NOTDIRECTORY);
101 }
102
103 return (ISC_R_SUCCESS);
104 }
105