mapset_msc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. \file mapset_msc.c
  3. \brief GIS library - Mapset user permission routines.
  4. (C) 1999-2008 The GRASS development team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. */
  8. #include <grass/config.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. \brief Create element in the current mapset.
  19. Make the specified element in the current mapset
  20. will check for the existence of the element and
  21. do nothing if it is found so this routine
  22. can be called even if the element already exists.
  23. \param element element to be created in mapset
  24. \return 0 ?
  25. \return ?
  26. */
  27. int G__make_mapset_element(const char *p_element)
  28. {
  29. char path[GPATH_MAX];
  30. char *p;
  31. const char *element;
  32. element = p_element;
  33. if (*element == 0)
  34. return 0;
  35. G_file_name(p = path, NULL, NULL, G_mapset());
  36. while (*p)
  37. p++;
  38. /* add trailing slash if missing */
  39. --p;
  40. if (*p++ != '/') {
  41. *p++ = '/';
  42. *p = 0;
  43. }
  44. /* now append element, one directory at a time, to path */
  45. while (1) {
  46. if (*element == '/' || *element == 0) {
  47. *p = 0;
  48. if (access(path, 0) != 0) { /* directory not yet created */
  49. if (G_mkdir(path) != 0)
  50. G_fatal_error(_("Unable to make mapset element %s (%s): %s"),
  51. p_element, path, strerror(errno));
  52. }
  53. if (access(path, 0) != 0) /* directory not accessible */
  54. G_fatal_error(_("Unable to access mapset element %s (%s): %s"),
  55. p_element, path, strerror(errno));
  56. if (*element == 0)
  57. return 1;
  58. }
  59. *p++ = *element++;
  60. }
  61. }
  62. /*!
  63. \brief Create misc element in the current mapset.
  64. \param dir directory path
  65. \param name element name
  66. \return 0 ?
  67. \return ?
  68. */
  69. int G__make_mapset_element_misc(const char *dir, const char *name)
  70. {
  71. char buf[GNAME_MAX * 2 + 1];
  72. sprintf(buf, "%s/%s", dir, name);
  73. return G__make_mapset_element(buf);
  74. }
  75. static int check_owner(const struct stat *info)
  76. {
  77. #if defined(__MINGW32__) || defined(SKIP_MAPSET_OWN_CHK)
  78. return 1;
  79. #else
  80. const char *check = getenv("GRASS_SKIP_MAPSET_OWNER_CHECK");
  81. if (check && *check)
  82. return 1;
  83. if (info->st_uid != getuid())
  84. return 0;
  85. if (info->st_uid != geteuid())
  86. return 0;
  87. return 1;
  88. #endif
  89. }
  90. /*!
  91. \brief Check for user mapset permission
  92. \param mapset mapset name
  93. \return 1 mapset exists, and user has permission
  94. \return 0 mapset exists, BUT user denied permission
  95. \return -1 mapset does not exist
  96. */
  97. int G__mapset_permissions(const char *mapset)
  98. {
  99. char path[GPATH_MAX];
  100. struct stat info;
  101. G_file_name(path, "", "", mapset);
  102. if (G_stat(path, &info) != 0)
  103. return -1;
  104. if (!S_ISDIR(info.st_mode))
  105. return -1;
  106. if (!check_owner(&info))
  107. return 0;
  108. return 1;
  109. }
  110. /*!
  111. \brief Check for user mapset permission
  112. \param gisdbase full path to GISDBASE
  113. \param location location name
  114. \param mapset mapset name
  115. \return 1 mapset exists, and user has permission
  116. \return 0 mapset exists, BUT user denied permission
  117. \return -1 mapset does not exist
  118. */
  119. int G__mapset_permissions2(const char *gisdbase, const char *location,
  120. const char *mapset)
  121. {
  122. char path[GPATH_MAX];
  123. struct stat info;
  124. sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
  125. if (G_stat(path, &info) != 0)
  126. return -1;
  127. if (!S_ISDIR(info.st_mode))
  128. return -1;
  129. if (!check_owner(&info))
  130. return 0;
  131. return 1;
  132. }