fsaccess.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: fsaccess.c,v 1.10 2007/06/19 23:47:17 tbox Exp $ */
00019 
00020 /*! \file
00021  * \brief
00022  * This file contains the OS-independent functionality of the API.
00023  */
00024 #include <isc/fsaccess.h>
00025 #include <isc/result.h>
00026 #include <isc/util.h>
00027 
00028 /*!
00029  * Shorthand.  Maybe ISC__FSACCESS_PERMISSIONBITS should not even be in
00030  * <isc/fsaccess.h>.  Could check consistency with sizeof(isc_fsaccess_t)
00031  * and the number of bits in each function.
00032  */
00033 #define STEP            (ISC__FSACCESS_PERMISSIONBITS)
00034 #define GROUP           (STEP)
00035 #define OTHER           (STEP * 2)
00036 
00037 void
00038 isc_fsaccess_add(int trustee, int permission, isc_fsaccess_t *access) {
00039         REQUIRE(trustee <= 0x7);
00040         REQUIRE(permission <= 0xFF);
00041 
00042         if ((trustee & ISC_FSACCESS_OWNER) != 0)
00043                 *access |= permission;
00044 
00045         if ((trustee & ISC_FSACCESS_GROUP) != 0)
00046                 *access |= (permission << GROUP);
00047 
00048         if ((trustee & ISC_FSACCESS_OTHER) != 0)
00049                 *access |= (permission << OTHER);
00050 }
00051 
00052 void
00053 isc_fsaccess_remove(int trustee, int permission, isc_fsaccess_t *access) {
00054         REQUIRE(trustee <= 0x7);
00055         REQUIRE(permission <= 0xFF);
00056 
00057 
00058         if ((trustee & ISC_FSACCESS_OWNER) != 0)
00059                 *access &= ~permission;
00060 
00061         if ((trustee & ISC_FSACCESS_GROUP) != 0)
00062                 *access &= ~(permission << GROUP);
00063 
00064         if ((trustee & ISC_FSACCESS_OTHER) != 0)
00065                 *access &= ~(permission << OTHER);
00066 }
00067 
00068 static isc_result_t
00069 check_bad_bits(isc_fsaccess_t access, isc_boolean_t is_dir) {
00070         isc_fsaccess_t bits;
00071 
00072         /*
00073          * Check for disallowed user bits.
00074          */
00075         if (is_dir)
00076                 bits = ISC_FSACCESS_READ |
00077                        ISC_FSACCESS_WRITE |
00078                        ISC_FSACCESS_EXECUTE;
00079         else
00080                 bits = ISC_FSACCESS_CREATECHILD |
00081                        ISC_FSACCESS_ACCESSCHILD |
00082                        ISC_FSACCESS_DELETECHILD |
00083                        ISC_FSACCESS_LISTDIRECTORY;
00084 
00085         /*
00086          * Set group bad bits.
00087          */
00088         bits |= bits << STEP;
00089         /*
00090          * Set other bad bits.
00091          */
00092         bits |= bits << STEP;
00093 
00094         if ((access & bits) != 0) {
00095                 if (is_dir)
00096                         return (ISC_R_NOTFILE);
00097                 else
00098                         return (ISC_R_NOTDIRECTORY);
00099         }
00100 
00101         return (ISC_R_SUCCESS);
00102 }

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha