Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / fsaccess.c
bloba9c038ca0d1e23b4b451be377a3a0744fa32a84c
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
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.
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.
20 /* Id: fsaccess.c,v 1.10 2007/06/19 23:47:17 tbox Exp */
22 /*! \file
23 * \brief
24 * This file contains the OS-independent functionality of the API.
26 #include <isc/fsaccess.h>
27 #include <isc/result.h>
28 #include <isc/util.h>
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.
35 #define STEP (ISC__FSACCESS_PERMISSIONBITS)
36 #define GROUP (STEP)
37 #define OTHER (STEP * 2)
39 void
40 isc_fsaccess_add(int trustee, int permission, isc_fsaccess_t *access) {
41 REQUIRE(trustee <= 0x7);
42 REQUIRE(permission <= 0xFF);
44 if ((trustee & ISC_FSACCESS_OWNER) != 0)
45 *access |= permission;
47 if ((trustee & ISC_FSACCESS_GROUP) != 0)
48 *access |= (permission << GROUP);
50 if ((trustee & ISC_FSACCESS_OTHER) != 0)
51 *access |= (permission << OTHER);
54 void
55 isc_fsaccess_remove(int trustee, int permission, isc_fsaccess_t *access) {
56 REQUIRE(trustee <= 0x7);
57 REQUIRE(permission <= 0xFF);
60 if ((trustee & ISC_FSACCESS_OWNER) != 0)
61 *access &= ~permission;
63 if ((trustee & ISC_FSACCESS_GROUP) != 0)
64 *access &= ~(permission << GROUP);
66 if ((trustee & ISC_FSACCESS_OTHER) != 0)
67 *access &= ~(permission << OTHER);
70 static isc_result_t
71 check_bad_bits(isc_fsaccess_t access, isc_boolean_t is_dir) {
72 isc_fsaccess_t bits;
75 * Check for disallowed user bits.
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;
88 * Set group bad bits.
90 bits |= bits << STEP;
92 * Set other bad bits.
94 bits |= bits << STEP;
96 if ((access & bits) != 0) {
97 if (is_dir)
98 return (ISC_R_NOTFILE);
99 else
100 return (ISC_R_NOTDIRECTORY);
103 return (ISC_R_SUCCESS);