mapset_msc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, "", "", 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. /*!
  76. \brief Check for user mapset permission
  77. \param mapset mapset name
  78. \return 1 mapset exists, and user has permission
  79. \return 0 mapset exists, BUT user denied permission
  80. \return -1 mapset does not exist
  81. */
  82. int G__mapset_permissions(const char *mapset)
  83. {
  84. char path[GPATH_MAX];
  85. STRUCT_STAT info;
  86. G_file_name(path, "", "", mapset);
  87. if (G_stat(path, &info) != 0)
  88. return -1;
  89. if (!S_ISDIR(info.st_mode))
  90. return -1;
  91. #ifndef __MINGW32__
  92. if (info.st_uid != getuid())
  93. return 0;
  94. if (info.st_uid != geteuid())
  95. return 0;
  96. #endif
  97. return 1;
  98. }
  99. /*!
  100. \brief Check for user mapset permission
  101. \param gisdbase full path to GISDBASE
  102. \param location location name
  103. \param mapset mapset name
  104. \return 1 mapset exists, and user has permission
  105. \return 0 mapset exists, BUT user denied permission
  106. \return -1 mapset does not exist
  107. */
  108. int G__mapset_permissions2(const char *gisdbase, const char *location,
  109. const char *mapset)
  110. {
  111. char path[GPATH_MAX];
  112. STRUCT_STAT info;
  113. sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
  114. if (G_stat(path, &info) != 0)
  115. return -1;
  116. if (!S_ISDIR(info.st_mode))
  117. return -1;
  118. #ifndef SKIP_MAPSET_OWN_CHK
  119. #ifndef __MINGW32__
  120. if (info.st_uid != getuid())
  121. return 0;
  122. if (info.st_uid != geteuid())
  123. return 0;
  124. #endif
  125. #endif
  126. return 1;
  127. }