initldap.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 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 "ldapsecurity.ipp"
  14. #include "ldapsecurity.hpp"
  15. #include "build-config.h"
  16. #ifndef _WIN32
  17. #include <unistd.h>
  18. #endif
  19. //-----------------------------------------------------
  20. //
  21. //-----------------------------------------------------
  22. void usage()
  23. {
  24. fprintf(stdout, "\nUsage: initldap");
  25. fprintf(stdout, "\n\n\tinitldap creates an initial HPCC Admin user account\n\tand all HPCC organization units, using the setting entered into configmanager 'LDAPServer' component\n");
  26. fprintf(stdout, "\n");
  27. }
  28. //-----------------------------------------------------
  29. //
  30. //-----------------------------------------------------
  31. bool initLDAP(IPropertyTree * ldapProps)
  32. {
  33. StringAttr serverType( ldapProps->queryProp("@serverType") );
  34. if (!serverType.length())
  35. {
  36. fprintf(stderr, "\nERROR: serverType not set in LDAPServer component");
  37. return false;
  38. }
  39. StringBuffer hpccUser;
  40. StringBuffer hpccPwd;
  41. ldapProps->getProp("@systemUser", hpccUser);
  42. ldapProps->getProp("@systemPassword", hpccPwd);
  43. if (0==hpccUser.length() || 0==hpccPwd.length())
  44. {
  45. fprintf(stderr, "\nERROR: HPCC systemUser credentials not found in configuration");
  46. return false;
  47. }
  48. StringBuffer ldapAddress;
  49. ldapProps->getProp("@ldapAddress", ldapAddress);
  50. //Get LDAP admin creds from user
  51. char buff[100];
  52. fprintf(stdout, "\nEnter the '%s' LDAP Admin User name on '%s'...",serverType.get(),ldapAddress.str());
  53. do
  54. {
  55. fgets(buff, sizeof(buff), stdin);
  56. }
  57. while (buff[0] == (char)'\n');
  58. if (buff[strlen(buff)-1] == '\n')
  59. buff[strlen(buff)-1] = (char)NULL;
  60. StringAttr ldapUser(buff);
  61. fprintf(stdout, "Enter the LDAP Admin user '%s' password...",ldapUser.get());
  62. fgets(buff, sizeof(buff), stdin);
  63. if (buff[strlen(buff)-1] == '\n')
  64. buff[strlen(buff)-1] = (char)NULL;
  65. StringAttr ldapPwd(buff);
  66. if (0==ldapUser.length() || 0==ldapPwd.length())
  67. {
  68. fprintf(stderr, "\nERROR: Invalid LDAP Admin account credentials entered");
  69. return false;
  70. }
  71. fprintf(stdout, "\nReady to initialize HPCC LDAP Environment, using the following settings");
  72. fprintf(stdout, "\n\tLDAP Server : %s", ldapAddress.str());
  73. fprintf(stdout, "\n\tLDAP Type : %s", serverType.get());
  74. fprintf(stdout, "\n\tHPCC Admin User : %s", hpccUser.str());
  75. fprintf(stdout, "\nProceed? y/n ");
  76. for (;;)
  77. {
  78. int c = getchar();
  79. if (c == 'y' || c == 'Y')
  80. break;
  81. else if (c == 'n' || c == 'N')
  82. return true;
  83. }
  84. if (stricmp(serverType.get(),"ActiveDirectory"))
  85. ldapProps->setProp("@systemBasedn", "");
  86. //Replace system user with LDAP Admin credentials
  87. ldapProps->setProp("@systemUser", ldapUser);
  88. ldapProps->setProp("@systemCommonName", ldapUser);
  89. StringBuffer sb;
  90. encrypt(sb,ldapPwd);
  91. ldapProps->setProp("@systemPassword", sb.str());
  92. //Create security manager. This creates the required OUs
  93. Owned<ISecManager> secMgr;
  94. try
  95. {
  96. secMgr.setown(newLdapSecManager("initldap", *LINK(ldapProps)));
  97. }
  98. catch(IException *e)
  99. {
  100. StringBuffer buff;
  101. e->errorMessage(buff);
  102. e->Release();
  103. fprintf(stderr, "\nERROR: Unable to create security manager : %s", buff.str());
  104. return false;
  105. }
  106. //Create HPCC Admin user
  107. Owned<ISecUser> user = secMgr->createUser(hpccUser.str());
  108. StringBuffer pwd;
  109. decrypt(pwd, hpccPwd.str());
  110. user->credentials().setPassword(pwd.str());
  111. try { secMgr->addUser(*user.get()); }
  112. catch(...) {}//user may already exist, so just move on
  113. //Add HPCC admin user to Administrators group
  114. CLdapSecManager* ldapSecMgr = dynamic_cast<CLdapSecManager*>(secMgr.get());
  115. if (!ldapSecMgr)
  116. {
  117. fprintf(stderr, "\nERROR: Unable to access CLdapSecManager object");
  118. return false;
  119. }
  120. StringAttr adminGroup;
  121. bool isActiveDir = true;
  122. if (0 == stricmp(serverType.get(),"ActiveDirectory"))
  123. adminGroup.set("Administrators");
  124. else
  125. adminGroup.set("Directory Administrators");
  126. try { ldapSecMgr->changeUserGroup("add", hpccUser.str(), adminGroup); }
  127. catch(...) {}//user may already be in group so just move on
  128. fprintf(stdout, "\n\nLDAP Initialization successful\n");
  129. return true;
  130. }
  131. //-----------------------------------------------------
  132. //
  133. //-----------------------------------------------------
  134. int main(int argc, char* argv[])
  135. {
  136. #ifdef _NO_LDAP
  137. fprintf(stderr, "System was built with _NO_LDAP\n");
  138. return -1;
  139. #endif
  140. for (int x = 1; x < argc; x++)
  141. {
  142. if (0==strncmp("-h", argv[x], 2))
  143. {
  144. usage();
  145. exit(0);
  146. }
  147. else
  148. {
  149. fprintf(stderr, "\nERROR: Unrecognized parameter : '%s', enter 'initldap -h' for help\n", argv[x]);
  150. exit(1);
  151. }
  152. }
  153. InitModuleObjects();
  154. //execute configgen to query the LDAP Server configuration(s)
  155. StringBuffer cmd;
  156. cmd.appendf("%s%cconfiggen -env %s%c%s -listldapservers", ADMIN_DIR,PATHSEPCHAR,CONFIG_DIR, PATHSEPCHAR, ENV_XML_FILE);
  157. char * configBuffer = NULL;
  158. //acquire LDAP configuration by executing configgen and capturing output
  159. {
  160. StringBuffer configBuff;
  161. Owned<IPipeProcess> pipe = createPipeProcess();
  162. if (pipe->run("configgen", cmd.str(), ".", false, true, true, 0))
  163. {
  164. Owned<ISimpleReadStream> pipeReader = pipe->getOutputStream();
  165. const size32_t chunkSize = 8192;
  166. for (;;)
  167. {
  168. size32_t sizeRead = pipeReader->read(chunkSize, configBuff.reserve(chunkSize));
  169. if (sizeRead < chunkSize)
  170. {
  171. configBuff.setLength(configBuff.length() - (chunkSize - sizeRead));
  172. break;
  173. }
  174. }
  175. pipe->closeOutput();
  176. }
  177. int retcode = pipe->wait();
  178. if (retcode)
  179. {
  180. fprintf(stderr, "\nERROR %d: unable to execute %s", retcode, cmd.str());
  181. exit(1);
  182. }
  183. configBuffer = strdup(configBuff.str());
  184. }
  185. //Using the LDAP Server parms queried from configgen, build an
  186. //LDAPSecurity property tree for each LDAP Server and call the LDAP
  187. //Security Manager to create the needed entries
  188. Owned<IPropertyTree> ldapProps;
  189. char *saveptr;
  190. char * pLine = strtok_r(configBuffer, "\n", &saveptr);
  191. while (pLine)
  192. {
  193. if (pLine && 0==strcmp(pLine, "LDAPServerProcess"))
  194. {
  195. if (ldapProps)
  196. initLDAP(ldapProps);
  197. ldapProps.clear();
  198. ldapProps.setown(createPTree("ldapSecurity"));
  199. }
  200. else
  201. {
  202. char * sep = strchr(pLine, ',');
  203. if (sep)
  204. {
  205. *sep = (char)NULL;
  206. ldapProps->addProp(pLine, sep+1);
  207. }
  208. }
  209. pLine = strtok_r(NULL, "\n", &saveptr);
  210. }
  211. if (ldapProps)
  212. initLDAP(ldapProps);
  213. if (configBuffer)
  214. free(configBuffer);
  215. ldapProps.clear();
  216. releaseAtoms();
  217. return 0;
  218. }