ds_remove_uninst.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. // ds_remove_uninst.cpp
  42. //
  43. // ds_remove routines that use c++ calls in adminutil
  44. //
  45. #include <iostream.h>
  46. #include <fstream.h>
  47. #include <stdio.h> /* printf, file I/O */
  48. #include <string.h> /* strlen */
  49. #include <ctype.h>
  50. #ifdef XP_UNIX
  51. #include <strings.h>
  52. #include <pwd.h>
  53. #include <grp.h>
  54. #include <sys/socket.h>
  55. #include <netinet/in.h>
  56. #include <arpa/inet.h>
  57. #include <netdb.h>
  58. #endif
  59. #include <stdlib.h> /* memset, rand stuff */
  60. #include <sys/types.h>
  61. #include <errno.h>
  62. #include <stdarg.h>
  63. #include <time.h>
  64. #include "ds_remove_uninst.h"
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. #include "dsalib.h"
  69. #ifdef __cplusplus
  70. }
  71. #include "prprf.h"
  72. #endif
  73. #ifdef XP_UNIX
  74. #include "ux-util.h"
  75. #endif
  76. #include "ldapu.h"
  77. #include "install_keywords.h"
  78. #include "global.h"
  79. #include "setupapi.h"
  80. #define MAX_STR_SIZE 512
  81. static void dsLogMessage(const char *level, const char *which,
  82. const char *format, ...)
  83. #ifdef __GNUC__
  84. __attribute__ ((format (printf, 3, 4)));
  85. #else
  86. ;
  87. #endif
  88. static InstallLog *installLog = NULL;
  89. static void
  90. dsLogMessage(const char *level, const char *which,
  91. const char *format, ...)
  92. {
  93. char bigbuf[BIG_BUF*4];
  94. va_list ap;
  95. va_start(ap, format);
  96. PR_vsnprintf(bigbuf, sizeof(bigbuf), format, ap);
  97. va_end(ap);
  98. #ifdef _WIN32 // always output to stdout (for CGIs), and always log
  99. // if a log is available
  100. fprintf(stdout, "%s %s %s\n", level, which, bigbuf);
  101. fflush(stdout);
  102. if (installLog)
  103. installLog->logMessage(level, which, bigbuf);
  104. #else // not Windows
  105. if (installLog)
  106. installLog->logMessage(level, which, bigbuf);
  107. else
  108. fprintf(stdout, "%s %s %s\n", level, which, bigbuf);
  109. fflush(stdout);
  110. #endif
  111. return;
  112. }
  113. // replace \ in path with \\ for LDAP search filters
  114. static char *
  115. escapePath(const char *path)
  116. {
  117. char *s = 0;
  118. if (path) {
  119. s = new char [(strlen(path)+1)*2]; // worst case
  120. char *p = s;
  121. const char *pp = path;
  122. for (; *pp; ++pp, ++p) {
  123. if (*pp == '\\') {
  124. *p++ = *pp;
  125. }
  126. *p = *pp;
  127. }
  128. *p = 0;
  129. }
  130. return s;
  131. }
  132. static LdapErrorCode
  133. localRemoveISIE(LdapEntry &isieEntry)
  134. {
  135. /* stevross: for now explicitly delete ISIE because it's not getting
  136. removed by removeSIE for some reason */
  137. LdapError err = isieEntry.dropAll(isieEntry.entryDN());
  138. if (err.errorCode())
  139. {
  140. dsLogMessage(SETUP_LOG_FATAL, "Slapd",
  141. "Error: could not remove ISIE entry %s: error = %d",
  142. (const char *)isieEntry.entryDN(), (int)err.errorCode());
  143. }
  144. // OK to remove, recursively go up the tree and remove all
  145. char *dn = new char [strlen(isieEntry.entryDN()) + 10];
  146. char **explodedDN = ldap_explode_dn(isieEntry.entryDN(), 0);
  147. int i = 0;
  148. while (1)
  149. {
  150. dn[0] = 0;
  151. char **s = &explodedDN[i];
  152. while (*s != NULL)
  153. {
  154. strcat(dn, *s);
  155. strcat(dn, LDAP_PATHSEP);
  156. s++;
  157. }
  158. if (*s == NULL)
  159. {
  160. dn[strlen(dn)-strlen(LDAP_PATHSEP)] = 0;
  161. }
  162. if (strcasecmp(dn, DEFAULT_ROOT_DN) == 0)
  163. {
  164. break;
  165. }
  166. err = isieEntry.retrieve(OBJECT_CLASS_FILTER, LDAP_SCOPE_ONELEVEL, dn);
  167. if (err == NOT_FOUND)
  168. {
  169. isieEntry.drop(dn);
  170. ++i;
  171. }
  172. else
  173. {
  174. break;
  175. }
  176. }
  177. delete [] dn;
  178. ldap_value_free(explodedDN);
  179. return OKAY;
  180. }
  181. //////////////////////////////////////////////////////////////////////////////
  182. // removeInstanceLDAPEntries
  183. //
  184. //
  185. // remove sie, isie of this instance
  186. //
  187. //
  188. //
  189. //
  190. int removeInstanceLDAPEntries(const char *pszLdapHost,
  191. const char *pszPort,
  192. const char *pszLdapSuffix,
  193. const char *pszUser,
  194. const char *pszPw,
  195. const char *pszInstanceName,
  196. const char *pszInstanceHost,
  197. const char *pszServerRoot)
  198. {
  199. char szSearchBase[] = "o=NetscapeRoot";
  200. /* open LDAP connection */
  201. LdapError ldapError = 0;
  202. NSString newURL = NSString("ldap://") + pszLdapHost + ":" +
  203. pszPort + "/" + pszLdapSuffix;
  204. Ldap ldap(ldapError, newURL, pszUser, pszPw, 0, 0);
  205. if (ldapError.errorCode())
  206. {
  207. return 1;
  208. }
  209. /* get SIE entry */
  210. char *sroot = escapePath(pszServerRoot);
  211. LdapEntry sieEntry(&ldap);
  212. NSString sieFilter = NSString("(&(serverhostname=") + pszInstanceHost +
  213. ")(cn=" + pszInstanceName + ")(serverroot=" +
  214. sroot + "))";
  215. ldapError = sieEntry.retrieve(sieFilter, LDAP_SCOPE_SUBTREE, szSearchBase);
  216. if (ldapError.errorCode())
  217. {
  218. dsLogMessage(SETUP_LOG_FATAL, "Slapd",
  219. "Error: could not find the SIE entry using filter %s: error = %d",
  220. (const char *)sieFilter, (int)ldapError.errorCode());
  221. delete [] sroot;
  222. return 1;
  223. }
  224. /* get ISIE entry */
  225. LdapEntry isieEntry(&ldap);
  226. NSString isieFilter =
  227. NSString("(&(objectclass=nsApplication)(uniquemember=") +
  228. sieEntry.entryDN() + ")(nsinstalledlocation=" +
  229. sroot + "))";
  230. ldapError = isieEntry.retrieve(isieFilter, LDAP_SCOPE_SUBTREE, szSearchBase);
  231. if (ldapError.errorCode())
  232. {
  233. dsLogMessage(SETUP_LOG_FATAL, "Slapd",
  234. "Error: could not find the ISIE entry using filter %s: error = %d",
  235. (const char *)isieFilter, (int)ldapError.errorCode());
  236. delete [] sroot;
  237. return 1;
  238. }
  239. /* delete the SIE and ISIE entry */
  240. LdapErrorCode code = removeSIE(&ldap, sieEntry.entryDN(), False);
  241. if (code)
  242. {
  243. dsLogMessage(SETUP_LOG_FATAL, "Slapd",
  244. "Error: could not remove SIE entry %s: error = %d",
  245. (const char *)sieEntry.entryDN(), (int)code);
  246. return code;
  247. }
  248. code = localRemoveISIE(isieEntry);
  249. delete [] sroot;
  250. return code;
  251. }
  252. int ds_uninst_set_cgi_env(char *pszInfoFileName)
  253. {
  254. InstallInfo *uninstallInfo = NULL;
  255. InstallInfo *instanceInfo = NULL;
  256. static char szQueryString[512] = {0};
  257. static char szScriptName[512] = {0};
  258. static char szNetsiteRoot[512] = {0};
  259. const char *serverID = 0;
  260. const char *tmp;
  261. uninstallInfo = new InstallInfo(pszInfoFileName);
  262. if (!uninstallInfo)
  263. return 1;
  264. instanceInfo = uninstallInfo->getSection("uninstall");
  265. if (!instanceInfo)
  266. instanceInfo = uninstallInfo;
  267. putenv("REQUEST_METHOD=GET");
  268. if (instanceInfo->get(SLAPD_KEY_SERVER_IDENTIFIER))
  269. serverID = instanceInfo->get(SLAPD_KEY_SERVER_IDENTIFIER);
  270. else if (ds_get_server_name())
  271. serverID = ds_get_server_name();
  272. if (serverID)
  273. PR_snprintf(szQueryString, sizeof(szQueryString), "QUERY_STRING=InstanceName=%s",
  274. serverID);
  275. putenv(szQueryString);
  276. if (instanceInfo->get(SLAPD_KEY_SERVER_ROOT))
  277. PR_snprintf(szNetsiteRoot, sizeof(szNetsiteRoot), "NETSITE_ROOT=%s",
  278. instanceInfo->get(SLAPD_KEY_SERVER_ROOT));
  279. putenv(szNetsiteRoot);
  280. if (serverID)
  281. PR_snprintf(szScriptName, sizeof(szScriptName), "SCRIPT_NAME=/%s/Tasks/Operation/Remove",
  282. serverID);
  283. putenv(szScriptName);
  284. // remove SIE entry
  285. const char *host = instanceInfo->get(SLAPD_KEY_K_LDAP_HOST);
  286. char port[20] = {0};
  287. if (instanceInfo->get(SLAPD_KEY_K_LDAP_PORT))
  288. strncpy(port, instanceInfo->get(SLAPD_KEY_K_LDAP_PORT), sizeof(port)-1);
  289. const char *suffix = instanceInfo->get(SLAPD_KEY_SUFFIX);
  290. const char *ldapurl = instanceInfo->get(SLAPD_KEY_K_LDAP_URL);
  291. LDAPURLDesc *desc = 0;
  292. if (ldapurl && !ldap_url_parse((char *)ldapurl, &desc) && desc) {
  293. if (!host)
  294. host = desc->lud_host;
  295. if (port[0] == 0)
  296. PR_snprintf(port, sizeof(port), "%d", desc->lud_port);
  297. if (!suffix)
  298. suffix = desc->lud_dn;
  299. }
  300. // get and set the log file
  301. if ((tmp = instanceInfo->get(SLAPD_INSTALL_LOG_FILE_NAME)))
  302. {
  303. static char s_logfile[PATH_MAX+32];
  304. PR_snprintf(s_logfile, sizeof(s_logfile), "DEBUG_LOGFILE=%s", tmp);
  305. putenv(s_logfile);
  306. installLog = new InstallLog(tmp);
  307. }
  308. removeInstanceLDAPEntries(host, port, suffix,
  309. instanceInfo->get(SLAPD_KEY_SERVER_ADMIN_ID),
  310. instanceInfo->get(SLAPD_KEY_SERVER_ADMIN_PWD),
  311. serverID,
  312. instanceInfo->get(SLAPD_KEY_FULL_MACHINE_NAME),
  313. instanceInfo->get(SLAPD_KEY_SERVER_ROOT));
  314. if (desc)
  315. ldap_free_urldesc(desc);
  316. return 0;
  317. }