"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/isc/unix/fsaccess.c" (4 Sep 2020, 1966 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


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 #include <errno.h>
   13 #include <stdbool.h>
   14 #include <sys/stat.h>
   15 
   16 #include <isc/fsaccess.h>
   17 #include <isc/result.h>
   18 #include <isc/types.h>
   19 #include <isc/util.h>
   20 
   21 #include "../fsaccess_common_p.h"
   22 #include "errno2result.h"
   23 
   24 isc_result_t
   25 isc_fsaccess_set(const char *path, isc_fsaccess_t access) {
   26     struct stat statb;
   27     mode_t mode;
   28     bool is_dir = false;
   29     isc_fsaccess_t bits;
   30     isc_result_t result;
   31 
   32     if (stat(path, &statb) != 0) {
   33         return (isc__errno2result(errno));
   34     }
   35 
   36     if ((statb.st_mode & S_IFDIR) != 0) {
   37         is_dir = true;
   38     } else if ((statb.st_mode & S_IFREG) == 0) {
   39         return (ISC_R_INVALIDFILE);
   40     }
   41 
   42     result = isc__fsaccess_check_bad_bits(access, is_dir);
   43     if (result != ISC_R_SUCCESS) {
   44         return (result);
   45     }
   46 
   47     /*
   48      * Done with checking bad bits.  Set mode_t.
   49      */
   50     mode = 0;
   51 
   52 #define SET_AND_CLEAR1(modebit)     \
   53     if ((access & bits) != 0) { \
   54         mode |= modebit;    \
   55         access &= ~bits;    \
   56     }
   57 #define SET_AND_CLEAR(user, group, other) \
   58     SET_AND_CLEAR1(user);             \
   59     bits <<= STEP;                    \
   60     SET_AND_CLEAR1(group);            \
   61     bits <<= STEP;                    \
   62     SET_AND_CLEAR1(other);
   63 
   64     bits = ISC_FSACCESS_READ | ISC_FSACCESS_LISTDIRECTORY;
   65 
   66     SET_AND_CLEAR(S_IRUSR, S_IRGRP, S_IROTH);
   67 
   68     bits = ISC_FSACCESS_WRITE | ISC_FSACCESS_CREATECHILD |
   69            ISC_FSACCESS_DELETECHILD;
   70 
   71     SET_AND_CLEAR(S_IWUSR, S_IWGRP, S_IWOTH);
   72 
   73     bits = ISC_FSACCESS_EXECUTE | ISC_FSACCESS_ACCESSCHILD;
   74 
   75     SET_AND_CLEAR(S_IXUSR, S_IXGRP, S_IXOTH);
   76 
   77     INSIST(access == 0);
   78 
   79     if (chmod(path, mode) < 0) {
   80         return (isc__errno2result(errno));
   81     }
   82 
   83     return (ISC_R_SUCCESS);
   84 }