addScopes.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "seclib.hpp"
  14. #include "ldapsecurity.hpp"
  15. #include "jliball.hpp"
  16. #include "dasess.hpp"
  17. #ifndef _WIN32
  18. #include <unistd.h>
  19. #endif
  20. int main(int argc, char* argv[])
  21. {
  22. if(argc < 2 || argc > 3)
  23. {
  24. printf("usage: addScopes daliconf.xml [-c]\n");
  25. printf("\n\tCreates all user-specific LDAP private file scopes 'hpccinternal::<user>'\n\tand grants users access to their scope. The configuration file\n\tdaliconf.xml is the dali configuration file, typically\n\tfound in /var/lib/HPCCSystems/mydali\n\tSpecify -c to make changes immediately visible by clearing permission caches\n\n");
  26. return -1;
  27. }
  28. InitModuleObjects();
  29. try
  30. {
  31. Owned<IPropertyTree> cfg = createPTreeFromXMLFile(argv[1]);
  32. Owned<IPropertyTree> seccfg = cfg->getPropTree(".//ldapSecurity");
  33. if(seccfg == NULL)
  34. {
  35. printf("ldapSecurity not found\n");
  36. return -1;
  37. }
  38. #ifdef _NO_LDAP
  39. printf("System was built with _NO_LDAP\n");
  40. return -1;
  41. #else
  42. Owned<ISecManager> secmgr = newLdapSecManager("addScopes", *LINK(seccfg));
  43. if(secmgr == NULL)
  44. {
  45. printf("Security manager can't be created\n");
  46. releaseAtoms();
  47. return -1;
  48. }
  49. bool ok = secmgr->createUserScopes();
  50. printf(ok ? "User scopes added\n" : "Some scopes not added\n");
  51. //Clear permission caches?
  52. if (argc == 3 && 0==stricmp(argv[2], "-c"))
  53. {
  54. //Clear ESP Cache
  55. StringBuffer sysuser;
  56. StringBuffer passbuf;
  57. seccfg->getProp(".//@systemUser", sysuser);
  58. seccfg->getProp(".//@systemPassword", passbuf);
  59. if (0 == sysuser.length())
  60. {
  61. printf("Error in configuration file %s - systemUser not specified", argv[1]);
  62. releaseAtoms();
  63. return -1;
  64. }
  65. if (0 == passbuf.length())
  66. {
  67. printf("Error in configuration file %s - systemPassword not specified", argv[1]);
  68. releaseAtoms();
  69. return -1;
  70. }
  71. Owned<ISecUser> user = secmgr->createUser(sysuser.str());
  72. ISecCredentials& cred = user->credentials();
  73. StringBuffer decPwd;
  74. decrypt(decPwd, passbuf.str());
  75. cred.setPassword(decPwd.str());
  76. ok = secmgr->clearPermissionsCache(*user);
  77. printf(ok ? "ESP Cache cleared\n" : "Error clearing ESP Cache\n");
  78. //Clear Dali cache
  79. Owned<IUserDescriptor> userdesc(createUserDescriptor());
  80. userdesc->set(sysuser, decPwd);
  81. ok = querySessionManager().clearPermissionsCache(userdesc);
  82. printf(ok ? "Dali Cache cleared\n" : "Error clearing Dali Cache\n");
  83. }
  84. #endif
  85. }
  86. catch(IException* e)
  87. {
  88. StringBuffer errmsg;
  89. e->errorMessage(errmsg);
  90. printf("%s\n", errmsg.str());
  91. }
  92. catch(...)
  93. {
  94. printf("Unknown exception\n");
  95. }
  96. releaseAtoms();
  97. return 0;
  98. }