authmap.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "authmap.ipp"
  14. int CAuthMap::add(const char* path, ISecResourceList* resourceList)
  15. {
  16. if(!path || !*path)
  17. {
  18. DBGLOG("can't add to CAuthMap, path is NULL");
  19. return -1;
  20. }
  21. StringBuffer s;
  22. DBGLOG("Adding authentication information for %s: %s", path, resourceList?resourceList->toString(s).str():"");
  23. m_paths.append(path);
  24. m_resourcelists.append(*new CSecResourceListHolder(resourceList));
  25. return 0;
  26. }
  27. ISecResourceList* CAuthMap::queryResourceList(const char* path)
  28. {
  29. if(!path || !*path)
  30. return NULL;
  31. int pathlen = strlen(path);
  32. const char* curmatch = NULL;
  33. ISecResourceList* curlist = NULL;
  34. ForEachItemIn(x, m_paths)
  35. {
  36. const char* curpath = (char*)m_paths.item(x);
  37. if(!curpath || !*curpath)
  38. continue;
  39. int curlen = strlen(curpath);
  40. if(pathlen >= curlen && strncmp(curpath, path, strlen(curpath)) == 0 && (path[curlen - 1] == '/' || path[curlen] == '/' || path[curlen] == '\0'))
  41. {
  42. if(curmatch == NULL || strlen(curmatch) < strlen(curpath))
  43. {
  44. curmatch = curpath;
  45. curlist = m_resourcelists.item(x).list();
  46. //Keep comparing, because it need to find the longest fit.
  47. }
  48. }
  49. }
  50. return curlist;
  51. }
  52. ISecResourceList* CAuthMap::getResourceList(const char* path)
  53. {
  54. if(!path || !*path)
  55. return NULL;
  56. if(strcmp(path, "*") == 0)
  57. {
  58. ISecResourceList* dest = NULL;
  59. ForEachItemIn(x, m_resourcelists)
  60. {
  61. ISecResourceList* rlist = m_resourcelists.item(x).list();
  62. if(!dest)
  63. dest = rlist->clone();
  64. else
  65. rlist->copyTo(*dest);
  66. }
  67. return dest;
  68. }
  69. ISecResourceList* rlist = queryResourceList(path);
  70. if(rlist)
  71. rlist = rlist->clone();
  72. return rlist;
  73. }
  74. bool CAuthMap::shouldAuth(const char* path)
  75. {
  76. if(!path || !*path)
  77. return false;
  78. int pathlen = strlen(path);
  79. ForEachItemIn(x, m_paths)
  80. {
  81. const char* curpath = (char*)m_paths.item(x);
  82. if(!curpath || !*curpath)
  83. continue;
  84. int curlen = strlen(curpath);
  85. if(pathlen >= curlen && strncmp(curpath, path, strlen(curpath)) == 0 && (path[curlen - 1] == '/' || path[curlen] == '/' || path[curlen] == '\0'))
  86. {
  87. // Can return because it only need to find one match, not the longest.
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. bool CAuthMap::addToBackend()
  94. {
  95. if(m_secmgr == NULL)
  96. return false;
  97. bool ok = true;
  98. ForEachItemIn(x, m_resourcelists)
  99. {
  100. ISecResourceList* curlist = (ISecResourceList*)m_resourcelists.item(x).list();
  101. if(curlist == NULL)
  102. continue;
  103. ISecUser* usr = NULL;
  104. bool ret = m_secmgr->addResources(*usr, curlist);
  105. ok = ok && ret;
  106. }
  107. return ok;
  108. }
  109. SecAccessFlags str2perm(const char* permstr)
  110. {
  111. SecAccessFlags perm;
  112. if(permstr == NULL)
  113. {
  114. PROGLOG("permission string is NULL, using default");
  115. perm = DEFAULT_REQUIRED_ACCESS;
  116. }
  117. else if(stricmp(permstr, "None") == 0)
  118. {
  119. perm = SecAccess_None;
  120. }
  121. else if(stricmp(permstr, "Access") == 0)
  122. {
  123. perm = SecAccess_Access;
  124. }
  125. else if(stricmp(permstr, "Read") == 0)
  126. {
  127. perm = SecAccess_Read;
  128. }
  129. else if(stricmp(permstr, "Write") == 0)
  130. {
  131. perm = SecAccess_Write;
  132. }
  133. else if(stricmp(permstr, "Full") == 0)
  134. {
  135. perm = SecAccess_Full;
  136. }
  137. else
  138. {
  139. PROGLOG("using default required access permission");
  140. perm = DEFAULT_REQUIRED_ACCESS;
  141. }
  142. return perm;
  143. }
  144. const char* resTypeDesc(SecResourceType type)
  145. {
  146. switch(type)
  147. {
  148. case RT_DEFAULT: return "Default";
  149. case RT_MODULE: return "Module";
  150. case RT_SERVICE: return "Service";
  151. case RT_FILE_SCOPE: return "FileScope";
  152. case RT_WORKUNIT_SCOPE: return "Workunit_Scope";
  153. case RT_SUDOERS: return "Sudoers";
  154. case RT_TRIAL: return "Trial";
  155. case RT_VIEW_SCOPE: return "View";
  156. default: return "<unknown>";
  157. }
  158. }