SecureUser.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifndef SECUREUSER_INCL
  15. #define SECUREUSER_INCL
  16. #include "seclib.hpp"
  17. //#include "MD5.hpp"
  18. class CSecureUser : public CInterface,
  19. implements ISecUser,
  20. implements ISecCredentials
  21. {
  22. private:
  23. StringBuffer m_realm;
  24. StringBuffer m_name;
  25. StringBuffer m_pw;
  26. StringBuffer m_encodedPw;
  27. bool m_isAuthenticated;
  28. StringBuffer m_fullname;
  29. StringBuffer m_firstname;
  30. StringBuffer m_lastname;
  31. unsigned m_userID;
  32. StringBuffer m_Fqdn;
  33. StringBuffer m_Peer;
  34. SecUserStatus m_status;
  35. Owned<IProperties> m_parameters;
  36. CriticalSection crit;
  37. public:
  38. IMPLEMENT_IINTERFACE
  39. CSecureUser(const char *name, const char *pw) :
  40. m_name(name), m_pw(pw), m_isAuthenticated(false),m_userID(0), m_status(SecUserStatus_Unknown)
  41. {
  42. }
  43. virtual ~CSecureUser()
  44. {
  45. }
  46. virtual void setAuthenticated(bool authenticated)
  47. {
  48. m_isAuthenticated = authenticated;
  49. }
  50. //interface ISecUser
  51. const char * getName()
  52. {
  53. return m_name.str();
  54. }
  55. bool setName(const char * name)
  56. {
  57. m_name.clear().append(name);
  58. return true;
  59. }
  60. const char * getFullName()
  61. {
  62. return m_fullname.str();
  63. }
  64. bool setFullName(const char * name)
  65. {
  66. m_fullname.clear().append(name);
  67. return TRUE;
  68. }
  69. virtual const char * getFirstName()
  70. {
  71. return m_firstname.str();
  72. }
  73. virtual bool setFirstName(const char * fname)
  74. {
  75. if(fname != NULL)
  76. {
  77. m_firstname.clear().append(fname);
  78. }
  79. return true;
  80. }
  81. virtual const char * getLastName()
  82. {
  83. return m_lastname.str();
  84. }
  85. virtual bool setLastName(const char * lname)
  86. {
  87. if(lname != NULL)
  88. {
  89. m_lastname.clear().append(lname);
  90. }
  91. return true;
  92. }
  93. const char * getRealm()
  94. {
  95. return m_realm.str();
  96. }
  97. bool setRealm(const char * name)
  98. {
  99. m_realm.clear().append(name);
  100. return true;
  101. }
  102. const char * getFqdn()
  103. {
  104. return m_Fqdn.str();
  105. }
  106. bool setFqdn(const char * Fqdn)
  107. {
  108. m_Fqdn.clear().append(Fqdn);
  109. return true;
  110. }
  111. const char *getPeer()
  112. {
  113. return m_Peer.str();
  114. }
  115. bool setPeer(const char *Peer)
  116. {
  117. m_Peer.clear().append(Peer);
  118. return true;
  119. }
  120. virtual SecUserStatus getStatus()
  121. {
  122. return m_status;
  123. }
  124. virtual bool setStatus(SecUserStatus Status)
  125. {
  126. m_status = Status;
  127. return true;
  128. }
  129. bool isAuthenticated()
  130. {
  131. return m_isAuthenticated;
  132. }
  133. ISecCredentials & credentials()
  134. {
  135. return *this;
  136. }
  137. void setProperty(const char* name, const char* value)
  138. {
  139. if (!m_parameters)
  140. m_parameters.setown(createProperties(false));
  141. m_parameters->setProp(name, value);
  142. }
  143. const char* getProperty(const char* name)
  144. {
  145. if (m_parameters)
  146. return m_parameters->queryProp(name);
  147. return NULL;
  148. }
  149. void setPropertyInt(const char* name, int value)
  150. {
  151. if (!m_parameters)
  152. m_parameters.setown(createProperties(false));
  153. m_parameters->setProp(name, value);
  154. }
  155. int getPropertyInt(const char* name)
  156. {
  157. if (m_parameters)
  158. return m_parameters->getPropInt(name);
  159. return 0;
  160. }
  161. //interface ISecCredentials
  162. bool setPassword(const char * pw)
  163. {
  164. m_pw.clear();
  165. m_pw.append(pw);
  166. return true;
  167. }
  168. const char* getPassword()
  169. {
  170. return m_pw.str();
  171. }
  172. bool addToken(unsigned type, void * data, unsigned length)
  173. {
  174. return false; //not supported yet
  175. }
  176. virtual unsigned getUserID()
  177. {
  178. return m_userID;
  179. }
  180. virtual CDateTime & getPasswordExpiration(CDateTime& expirationDate){ assertex(false); return expirationDate; }
  181. virtual bool setPasswordExpiration(CDateTime& expirationDate) { assertex(false);return true; }
  182. virtual int getPasswordDaysRemaining() {assertex(false);return -1;}
  183. virtual void copyTo(ISecUser& destination)
  184. {
  185. destination.setAuthenticated(isAuthenticated());
  186. destination.setName(getName());
  187. destination.setFullName(getFullName());
  188. destination.setFirstName(getFirstName());
  189. destination.setLastName(getLastName());
  190. destination.setRealm(getRealm());
  191. destination.setFqdn(getFqdn());
  192. destination.setPeer(getPeer());
  193. destination.credentials().setPassword(credentials().getPassword());
  194. CDateTime tmpTime;
  195. destination.setPasswordExpiration(getPasswordExpiration(tmpTime));
  196. destination.setStatus(getStatus());
  197. if(m_parameters.get()==NULL)
  198. return;
  199. CriticalBlock b(crit);
  200. Owned<IPropertyIterator> Itr = m_parameters->getIterator();
  201. Itr->first();
  202. while(Itr->isValid())
  203. {
  204. destination.setProperty(Itr->getPropKey(),m_parameters->queryProp(Itr->getPropKey()));
  205. Itr->next();
  206. }
  207. //addToken is not currently implemented....
  208. // DBGLOG("Copied name %s to %s",getName(),destination.getName());
  209. }
  210. ISecUser * clone()
  211. {
  212. //DBGLOG("Beginning of clone()");
  213. CSecureUser* newuser = new CSecureUser(m_name.str(), m_pw.str());
  214. //DBGLOG("Before copy to");
  215. if(newuser)
  216. copyTo(*newuser);
  217. //DBGLOG("After copy to");
  218. return newuser;
  219. }
  220. };
  221. #endif // SECUREUSER_INCL
  222. //end