initldap.cpp 7.6 KB

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