fsaccess.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-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.13 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 #include <config.h>
00021 
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 
00025 #include <errno.h>
00026 
00027 #include "errno2result.h"
00028 
00029 /*! \file
00030  * \brief
00031  * The OS-independent part of the API is in lib/isc.
00032  */
00033 #include "../fsaccess.c"
00034 
00035 isc_result_t
00036 isc_fsaccess_set(const char *path, isc_fsaccess_t access) {
00037         struct stat statb;
00038         mode_t mode;
00039         isc_boolean_t is_dir = ISC_FALSE;
00040         isc_fsaccess_t bits;
00041         isc_result_t result;
00042 
00043         if (stat(path, &statb) != 0)
00044                 return (isc__errno2result(errno));
00045 
00046         if ((statb.st_mode & S_IFDIR) != 0)
00047                 is_dir = ISC_TRUE;
00048         else if ((statb.st_mode & S_IFREG) == 0)
00049                 return (ISC_R_INVALIDFILE);
00050 
00051         result = check_bad_bits(access, is_dir);
00052         if (result != ISC_R_SUCCESS)
00053                 return (result);
00054 
00055         /*
00056          * Done with checking bad bits.  Set mode_t.
00057          */
00058         mode = 0;
00059 
00060 #define SET_AND_CLEAR1(modebit) \
00061         if ((access & bits) != 0) { \
00062                 mode |= modebit; \
00063                 access &= ~bits; \
00064         }
00065 #define SET_AND_CLEAR(user, group, other) \
00066         SET_AND_CLEAR1(user); \
00067         bits <<= STEP; \
00068         SET_AND_CLEAR1(group); \
00069         bits <<= STEP; \
00070         SET_AND_CLEAR1(other);
00071 
00072         bits = ISC_FSACCESS_READ | ISC_FSACCESS_LISTDIRECTORY;
00073 
00074         SET_AND_CLEAR(S_IRUSR, S_IRGRP, S_IROTH);
00075 
00076         bits = ISC_FSACCESS_WRITE |
00077                ISC_FSACCESS_CREATECHILD |
00078                ISC_FSACCESS_DELETECHILD;
00079 
00080         SET_AND_CLEAR(S_IWUSR, S_IWGRP, S_IWOTH);
00081 
00082         bits = ISC_FSACCESS_EXECUTE |
00083                ISC_FSACCESS_ACCESSCHILD;
00084 
00085         SET_AND_CLEAR(S_IXUSR, S_IXGRP, S_IXOTH);
00086 
00087         INSIST(access == 0);
00088 
00089         if (chmod(path, mode) < 0)
00090                 return (isc__errno2result(errno));
00091 
00092         return (ISC_R_SUCCESS);
00093 }

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