htpasswdSecurity.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2013 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. #pragma warning( disable : 4786 )
  14. #include "defaultsecuritymanager.hpp"
  15. #include "authmap.ipp"
  16. #include <apr_md5.h>
  17. #include "htpasswdSecurity.hpp"
  18. class CHtpasswdSecurityManager : public CDefaultSecurityManager
  19. {
  20. public:
  21. CHtpasswdSecurityManager(const char *serviceName, IPropertyTree *authconfig) : CDefaultSecurityManager(serviceName, (IPropertyTree *)NULL)
  22. {
  23. if (authconfig)
  24. authconfig->getProp("@htpasswdFile", pwFile);
  25. apr_initialized = false;
  26. }
  27. ~CHtpasswdSecurityManager()
  28. {
  29. userMap.kill();
  30. }
  31. secManagerType querySecMgrType()
  32. {
  33. return SMT_HTPasswd;
  34. }
  35. IAuthMap * createAuthMap(IPropertyTree * authconfig)
  36. {
  37. CAuthMap* authmap = new CAuthMap(this);
  38. Owned<IPropertyTreeIterator> loc_iter;
  39. loc_iter.setown(authconfig->getElements(".//Location"));
  40. if (loc_iter)
  41. {
  42. IPropertyTree *location = NULL;
  43. loc_iter->first();
  44. while(loc_iter->isValid())
  45. {
  46. location = &loc_iter->query();
  47. if (location)
  48. {
  49. StringBuffer pathstr, rstr, required, description;
  50. location->getProp("@path", pathstr);
  51. location->getProp("@resource", rstr);
  52. location->getProp("@required", required);
  53. location->getProp("@description", description);
  54. if(pathstr.length() == 0)
  55. throw MakeStringException(-1, "path empty in Authenticate/Location");
  56. if(rstr.length() == 0)
  57. throw MakeStringException(-1, "resource empty in Authenticate/Location");
  58. ISecResourceList* rlist = authmap->queryResourceList(pathstr.str());
  59. if(rlist == NULL)
  60. {
  61. rlist = createResourceList("htpasswdsecurity");
  62. authmap->add(pathstr.str(), rlist);
  63. }
  64. ISecResource* rs = rlist->addResource(rstr.str());
  65. unsigned requiredaccess = str2perm(required.str());
  66. rs->setRequiredAccessFlags(requiredaccess);
  67. rs->setDescription(description.str());
  68. }
  69. loc_iter->next();
  70. }
  71. }
  72. return authmap;
  73. }
  74. protected:
  75. bool IsPasswordValid(ISecUser& sec_user)
  76. {
  77. StringBuffer user;
  78. user.append(sec_user.getName());
  79. if (0 == user.length())
  80. throw MakeStringException(-1, "htpasswd User name is NULL");
  81. CriticalBlock block(crit);
  82. if (!apr_initialized)
  83. initAPR();
  84. loadPwds();//reload password file if modified
  85. StringBuffer *encPW = userMap.getValue(user.str());
  86. if (encPW && encPW->length())
  87. {
  88. apr_status_t rc = apr_password_validate(sec_user.credentials().getPassword(), encPW->str());
  89. if (rc != APR_SUCCESS)
  90. DBGLOG("htpasswd authentication for user %s failed - APR RC %d", user.str(), rc );
  91. return rc == APR_SUCCESS;
  92. }
  93. DBGLOG("User %s not in htpasswd file", user.str());
  94. return false;
  95. }
  96. private:
  97. void initAPR()
  98. {
  99. try
  100. {
  101. apr_status_t rc = apr_md5_init(&md5_ctx);
  102. if (rc != APR_SUCCESS)
  103. throw MakeStringException(-1, "htpasswd apr_md5_init returns error %d", rc );
  104. apr_initialized = true;
  105. }
  106. catch (...)
  107. {
  108. throw MakeStringException(-1, "htpasswd exception calling apr_md5_init");
  109. }
  110. }
  111. bool loadPwds()
  112. {
  113. try
  114. {
  115. if (!pwFile.length())
  116. throw MakeStringException(-1, "htpasswd Password file not specified");
  117. Owned<IFile> file = createIFile(pwFile.str());
  118. if (!file->exists())
  119. {
  120. userMap.kill();
  121. throw MakeStringException(-1, "htpasswd Password file does not exist");
  122. }
  123. bool isDir;
  124. offset_t size;
  125. CDateTime whenChanged;
  126. file->getInfo(isDir,size,whenChanged);
  127. if (isDir)
  128. {
  129. userMap.kill();
  130. throw MakeStringException(-1, "htpasswd Password file specifies a directory");
  131. }
  132. if (0 == whenChanged.compare(pwFileLastMod))
  133. return true;//Don't reload if file unchanged
  134. userMap.kill();
  135. OwnedIFileIO io = file->open(IFOread);
  136. if (!io)
  137. throw MakeStringException(-1, "htpasswd Unable to open Password file");
  138. MemoryBuffer mb;
  139. size32_t count = read(io, 0, (size32_t)-1, mb);
  140. if (0 == count)
  141. throw MakeStringException(-1, "htpasswd Password file is empty");
  142. mb.append((char)NULL);
  143. char * p = (char*)mb.toByteArray();
  144. char *saveptr;
  145. const char * seps = "\f\r\n";
  146. char * next = strtok_r(p, seps, &saveptr);
  147. if (next)
  148. {
  149. do
  150. {
  151. char * colon = strchr(next,':');
  152. if (NULL == colon)
  153. throw MakeStringException(-1, "htpasswd Password file appears malformed");
  154. *colon = (char)NULL;
  155. userMap.setValue(next, colon+1);//username, enctypted password
  156. } while (next = strtok_r(NULL, seps, &saveptr));
  157. }
  158. io->close();
  159. pwFileLastMod = whenChanged;//remember when last changed
  160. }
  161. catch(IException*)
  162. {
  163. throw MakeStringException(-1, "htpasswd Exception accessing Password file");
  164. }
  165. return true;
  166. }
  167. private:
  168. mutable CriticalSection crit;
  169. StringBuffer pwFile;
  170. CDateTime pwFileLastMod;
  171. bool apr_initialized;
  172. MapStringTo<StringBuffer> userMap;
  173. apr_md5_ctx_t md5_ctx;
  174. };
  175. extern "C"
  176. {
  177. HTPASSWDSECURITY_API ISecManager * newHtpasswdSecManager(const char *serviceName, IPropertyTree &config)
  178. {
  179. return new CHtpasswdSecurityManager(serviceName, &config);
  180. }
  181. }