/contrib/bind9/lib/isc/fsaccess.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 102 lines · 50 code · 17 blank · 35 comment · 16 complexity · 8229491cb064e73b3e488bd1f308c5ba MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000, 2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: fsaccess.c,v 1.10 2007/06/19 23:47:17 tbox Exp $ */
  18. /*! \file
  19. * \brief
  20. * This file contains the OS-independent functionality of the API.
  21. */
  22. #include <isc/fsaccess.h>
  23. #include <isc/result.h>
  24. #include <isc/util.h>
  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. void
  34. isc_fsaccess_add(int trustee, int permission, isc_fsaccess_t *access) {
  35. REQUIRE(trustee <= 0x7);
  36. REQUIRE(permission <= 0xFF);
  37. if ((trustee & ISC_FSACCESS_OWNER) != 0)
  38. *access |= permission;
  39. if ((trustee & ISC_FSACCESS_GROUP) != 0)
  40. *access |= (permission << GROUP);
  41. if ((trustee & ISC_FSACCESS_OTHER) != 0)
  42. *access |= (permission << OTHER);
  43. }
  44. void
  45. isc_fsaccess_remove(int trustee, int permission, isc_fsaccess_t *access) {
  46. REQUIRE(trustee <= 0x7);
  47. REQUIRE(permission <= 0xFF);
  48. if ((trustee & ISC_FSACCESS_OWNER) != 0)
  49. *access &= ~permission;
  50. if ((trustee & ISC_FSACCESS_GROUP) != 0)
  51. *access &= ~(permission << GROUP);
  52. if ((trustee & ISC_FSACCESS_OTHER) != 0)
  53. *access &= ~(permission << OTHER);
  54. }
  55. static isc_result_t
  56. check_bad_bits(isc_fsaccess_t access, isc_boolean_t is_dir) {
  57. isc_fsaccess_t bits;
  58. /*
  59. * Check for disallowed user bits.
  60. */
  61. if (is_dir)
  62. bits = ISC_FSACCESS_READ |
  63. ISC_FSACCESS_WRITE |
  64. ISC_FSACCESS_EXECUTE;
  65. else
  66. bits = ISC_FSACCESS_CREATECHILD |
  67. ISC_FSACCESS_ACCESSCHILD |
  68. ISC_FSACCESS_DELETECHILD |
  69. ISC_FSACCESS_LISTDIRECTORY;
  70. /*
  71. * Set group bad bits.
  72. */
  73. bits |= bits << STEP;
  74. /*
  75. * Set other bad bits.
  76. */
  77. bits |= bits << STEP;
  78. if ((access & bits) != 0) {
  79. if (is_dir)
  80. return (ISC_R_NOTFILE);
  81. else
  82. return (ISC_R_NOTDIRECTORY);
  83. }
  84. return (ISC_R_SUCCESS);
  85. }