mapset_msc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*!
  2. \file lib/gis/mapset_msc.c
  3. \brief GIS library - Mapset user permission routines.
  4. (C) 1999-2014 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. static int make_mapset_element(const char *, const char *);
  18. /*!
  19. \brief Create element in the current mapset.
  20. Make the specified element in the current mapset will check for the
  21. existence of the element and do nothing if it is found so this
  22. routine can be called even if the element already exists.
  23. Calls G_fatal_error() on failure.
  24. \param p_element element to be created in mapset
  25. \return 0 no element defined
  26. \return 1 on success
  27. */
  28. int G_make_mapset_element(const char *p_element)
  29. {
  30. char path[GPATH_MAX];
  31. G_file_name(path, NULL, NULL, G_mapset());
  32. return make_mapset_element(path, p_element);
  33. }
  34. /*!
  35. \brief Create element in the temporary directory.
  36. See G_file_name_tmp() for details.
  37. \param p_element element to be created in mapset
  38. \return 0 no element defined
  39. \return 1 on success
  40. */
  41. int G_make_mapset_element_tmp(const char *p_element)
  42. {
  43. char path[GPATH_MAX];
  44. G_file_name_tmp(path, NULL, NULL, G_mapset());
  45. return make_mapset_element(path, p_element);
  46. }
  47. int make_mapset_element(const char *p_path, const char *p_element)
  48. {
  49. char path[GPATH_MAX], *p;
  50. const char *element;
  51. element = p_element;
  52. if (*element == 0)
  53. return 0;
  54. strncpy(path, p_path, GPATH_MAX);
  55. p = path;
  56. while (*p)
  57. p++;
  58. /* add trailing slash if missing */
  59. --p;
  60. if (*p++ != '/') {
  61. *p++ = '/';
  62. *p = 0;
  63. }
  64. /* now append element, one directory at a time, to path */
  65. while (1) {
  66. if (*element == '/' || *element == 0) {
  67. *p = 0;
  68. if (access(path, 0) != 0) { /* directory not yet created */
  69. if (G_mkdir(path) != 0)
  70. G_fatal_error(_("Unable to make mapset element %s (%s): %s"),
  71. p_element, path, strerror(errno));
  72. }
  73. if (access(path, 0) != 0) /* directory not accessible */
  74. G_fatal_error(_("Unable to access mapset element %s (%s): %s"),
  75. p_element, path, strerror(errno));
  76. if (*element == 0)
  77. return 1;
  78. }
  79. *p++ = *element++;
  80. }
  81. }
  82. /*!
  83. \brief Create misc element in the current mapset.
  84. \param dir directory path
  85. \param name element to be created in mapset
  86. \return 0 no element defined
  87. \return 1 on success
  88. */
  89. int G__make_mapset_element_misc(const char *dir, const char *name)
  90. {
  91. char buf[GNAME_MAX * 2 + 1];
  92. sprintf(buf, "%s/%s", dir, name);
  93. return G_make_mapset_element(buf);
  94. }
  95. static int check_owner(const struct stat *info)
  96. {
  97. #if defined(__MINGW32__) || defined(SKIP_MAPSET_OWN_CHK)
  98. return 1;
  99. #else
  100. const char *check = getenv("GRASS_SKIP_MAPSET_OWNER_CHECK");
  101. if (check && *check)
  102. return 1;
  103. if (info->st_uid != getuid())
  104. return 0;
  105. if (info->st_uid != geteuid())
  106. return 0;
  107. return 1;
  108. #endif
  109. }
  110. /*!
  111. \brief Check for user mapset permission
  112. \param mapset mapset name
  113. \return 1 mapset exists, and user has permission
  114. \return 0 mapset exists, BUT user denied permission
  115. \return -1 mapset does not exist
  116. */
  117. int G_mapset_permissions(const char *mapset)
  118. {
  119. char path[GPATH_MAX];
  120. struct stat info;
  121. G_file_name(path, "", "", mapset);
  122. if (G_stat(path, &info) != 0)
  123. return -1;
  124. if (!S_ISDIR(info.st_mode))
  125. return -1;
  126. if (!check_owner(&info))
  127. return 0;
  128. return 1;
  129. }
  130. /*!
  131. \brief Check for user mapset permission
  132. \param gisdbase full path to GISDBASE
  133. \param location location name
  134. \param mapset mapset name
  135. \return 1 mapset exists, and user has permission
  136. \return 0 mapset exists, BUT user denied permission
  137. \return -1 mapset does not exist
  138. */
  139. int G_mapset_permissions2(const char *gisdbase, const char *location,
  140. const char *mapset)
  141. {
  142. char path[GPATH_MAX];
  143. struct stat info;
  144. sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
  145. if (G_stat(path, &info) != 0)
  146. return -1;
  147. if (!S_ISDIR(info.st_mode))
  148. return -1;
  149. if (!check_owner(&info))
  150. return 0;
  151. return 1;
  152. }