"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/fsaccess.c" (7 Sep 2020, 2272 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fsaccess.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 
   13 /*! \file
   14  * \brief
   15  * This file contains the OS-independent functionality of the API.
   16  */
   17 
   18 #include <stdbool.h>
   19 
   20 #include <isc/fsaccess.h>
   21 #include <isc/print.h>
   22 #include <isc/result.h>
   23 #include <isc/util.h>
   24 
   25 /*!
   26  * Shorthand.  Maybe ISC__FSACCESS_PERMISSIONBITS should not even be in
   27  * <isc/fsaccess.h>.  Could check consistency with sizeof(isc_fsaccess_t)
   28  * and the number of bits in each function.
   29  */
   30 #define STEP        (ISC__FSACCESS_PERMISSIONBITS)
   31 #define GROUP       (STEP)
   32 #define OTHER       (STEP * 2)
   33 
   34 void
   35 isc_fsaccess_add(int trustee, int permission, isc_fsaccess_t *access) {
   36     REQUIRE(trustee <= 0x7);
   37     REQUIRE(permission <= 0xFF);
   38 
   39     if ((trustee & ISC_FSACCESS_OWNER) != 0)
   40         *access |= permission;
   41 
   42     if ((trustee & ISC_FSACCESS_GROUP) != 0)
   43         *access |= (permission << GROUP);
   44 
   45     if ((trustee & ISC_FSACCESS_OTHER) != 0)
   46         *access |= (permission << OTHER);
   47 }
   48 
   49 void
   50 isc_fsaccess_remove(int trustee, int permission, isc_fsaccess_t *access) {
   51     REQUIRE(trustee <= 0x7);
   52     REQUIRE(permission <= 0xFF);
   53 
   54 
   55     if ((trustee & ISC_FSACCESS_OWNER) != 0)
   56         *access &= ~permission;
   57 
   58     if ((trustee & ISC_FSACCESS_GROUP) != 0)
   59         *access &= ~(permission << GROUP);
   60 
   61     if ((trustee & ISC_FSACCESS_OTHER) != 0)
   62         *access &= ~(permission << OTHER);
   63 }
   64 
   65 static isc_result_t
   66 check_bad_bits(isc_fsaccess_t access, bool is_dir) {
   67     isc_fsaccess_t bits;
   68 
   69     /*
   70      * Check for disallowed user bits.
   71      */
   72     if (is_dir)
   73         bits = ISC_FSACCESS_READ |
   74                ISC_FSACCESS_WRITE |
   75                ISC_FSACCESS_EXECUTE;
   76     else
   77         bits = ISC_FSACCESS_CREATECHILD |
   78                ISC_FSACCESS_ACCESSCHILD |
   79                ISC_FSACCESS_DELETECHILD |
   80                ISC_FSACCESS_LISTDIRECTORY;
   81 
   82     /*
   83      * Set group bad bits.
   84      */
   85     bits |= bits << STEP;
   86     /*
   87      * Set other bad bits.
   88      */
   89     bits |= bits << STEP;
   90 
   91     if ((access & bits) != 0) {
   92         if (is_dir)
   93             return (ISC_R_NOTFILE);
   94         else
   95             return (ISC_R_NOTDIRECTORY);
   96     }
   97 
   98     return (ISC_R_SUCCESS);
   99 }