summaryrefslogtreecommitdiffstats
path: root/ldap/admin/src/ds_remove_uninst.cpp
blob: d38de4facb37a946e828b40013d67c319beb21e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
// ds_remove_uninst.cpp
//
// ds_remove routines that use c++ calls in adminsdk
//
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>		/* printf, file I/O */
#include <string.h>		/* strlen */
#include <ctype.h>
#ifdef XP_UNIX
#include <strings.h>
#include <pwd.h>
#include <grp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#include <stdlib.h>		/* memset, rand stuff */
#include <sys/types.h>
#include <errno.h>
#include <stdarg.h>
#include <time.h>

#include "ds_remove_uninst.h"

#ifdef __cplusplus
extern "C" {
#endif
#include "dsalib.h"
#ifdef __cplusplus
}

#include "prprf.h"

#endif
#ifdef XP_UNIX
#include "ux-util.h"
#endif
#include "ldapu.h"
#include "install_keywords.h"
#include "global.h"
#include "setupapi.h"

#define MAX_STR_SIZE 512

static InstallLog *installLog = NULL;

static void
dsLogMessage(const char *level, const char *which,
			 const char *format, ...)
{
	char bigbuf[BIG_BUF*4];
	va_list ap;
	va_start(ap, format);
	PR_vsnprintf(bigbuf, BIG_BUF*4, format, ap);
	va_end(ap);
#ifdef _WIN32 // always output to stdout (for CGIs), and always log
	// if a log is available
	fprintf(stdout, "%s %s %s\n", level, which, bigbuf);
	fflush(stdout);
	if (installLog)
		installLog->logMessage(level, which, bigbuf);
#else // not Windows
	if (installLog)
		installLog->logMessage(level, which, bigbuf);
	else
		fprintf(stdout, "%s %s %s\n", level, which, bigbuf);
	fflush(stdout);
#endif

	return;
}

// replace \ in path with \\ for LDAP search filters
static char *
escapePath(const char *path)
{
	char *s = 0;
	if (path) {
		s = new char [(strlen(path)+1)*2]; // worst case
		char *p = s;
		const char *pp = path;
		for (; *pp; ++pp, ++p) {
			if (*pp == '\\') {
				*p++ = *pp;
			}
			*p = *pp;
		}
		*p = 0;
	}

	return s;
}

static LdapErrorCode
localRemoveISIE(LdapEntry &isieEntry)
{
   /* stevross: for now explicitly delete ISIE because it's not getting
	  removed by removeSIE for some reason */
   LdapError err = isieEntry.dropAll(isieEntry.entryDN());
   if (err.errorCode())
   {
	   dsLogMessage(SETUP_LOG_FATAL, "Slapd",
					"Error: could not remove ISIE entry %s: error = %d",
					(const char *)isieEntry.entryDN(), (int)err.errorCode());
   }

   // OK to remove, recursively go up the tree and remove all
   char *dn = new char [strlen(isieEntry.entryDN()) + 10];
   char **explodedDN = ldap_explode_dn(isieEntry.entryDN(), 0);
   int i = 0;

   while (1)
   {
      dn[0] = 0;
      char **s = &explodedDN[i];
      while (*s != NULL)
      {
         strcat(dn, *s);
         strcat(dn, LDAP_PATHSEP);
         s++;
      }

      if (*s == NULL)
      {
         dn[strlen(dn)-strlen(LDAP_PATHSEP)] = 0;
      }

      if (strcasecmp(dn, DEFAULT_ROOT_DN) == 0)
      {
         break;
      }

      err = isieEntry.retrieve(OBJECT_CLASS_FILTER, LDAP_SCOPE_ONELEVEL, dn);

      if (err == NOT_FOUND)
      {
         isieEntry.drop(dn);
         ++i;
      }
      else
      {
         break;
      }
   }

   delete [] dn;
   ldap_value_free(explodedDN);

   return OKAY;
}

//////////////////////////////////////////////////////////////////////////////
// removeInstanceLDAPEntries
//
// 
// remove sie, isie of this instance
// 
// 
// 
//

int removeInstanceLDAPEntries(const char *pszLdapHost,
							   const char *pszPort,
							   const char *pszLdapSuffix,
							   const char *pszUser, 
							   const char *pszPw,
							   const char *pszInstanceName, 
							   const char *pszInstanceHost,
							   const char *pszServerRoot)
{
	LDAP *ld			= NULL;
	char szSearchBase[] = "o=NetscapeRoot";

	/* open LDAP connection */
	LdapError ldapError = 0;
	NSString newURL = NSString("ldap://") + pszLdapHost + ":" +
		pszPort + "/" + pszLdapSuffix;
	Ldap ldap(ldapError, newURL, pszUser, pszPw, 0, 0);
	if (ldapError.errorCode())
	{
		return 1;
	}

	/* get SIE entry */
	char *sroot = escapePath(pszServerRoot);
	LdapEntry sieEntry(&ldap);
	NSString sieFilter = NSString("(&(serverhostname=") + pszInstanceHost +
		")(cn=" + pszInstanceName + ")(serverroot=" +
		sroot + "))";
	ldapError = sieEntry.retrieve(sieFilter, LDAP_SCOPE_SUBTREE, szSearchBase);
	if (ldapError.errorCode())
	{
		dsLogMessage(SETUP_LOG_FATAL, "Slapd",
					 "Error: could not find the SIE entry using filter %s: error = %d",
					 (const char *)sieFilter, (int)ldapError.errorCode());
		delete [] sroot;
		return 1;
	}

	/* get ISIE entry */
	LdapEntry isieEntry(&ldap);
	NSString isieFilter =
		NSString("(&(objectclass=nsApplication)(uniquemember=") +
		sieEntry.entryDN() + ")(nsinstalledlocation=" +
		sroot + "))";
	ldapError = isieEntry.retrieve(isieFilter, LDAP_SCOPE_SUBTREE, szSearchBase);
	if (ldapError.errorCode())
	{
		dsLogMessage(SETUP_LOG_FATAL, "Slapd",
					 "Error: could not find the ISIE entry using filter %s: error = %d",
					 (const char *)isieFilter, (int)ldapError.errorCode());
		delete [] sroot;
		return 1;
	}

	/* delete the SIE and ISIE entry */
	LdapErrorCode code = removeSIE(&ldap, sieEntry.entryDN(), False);
	if (code)
	{
		dsLogMessage(SETUP_LOG_FATAL, "Slapd",
					 "Error: could not remove SIE entry %s: error = %d",
					 (const char *)sieEntry.entryDN(), (int)code);
		return code;
	}

	code = localRemoveISIE(isieEntry);

	delete [] sroot;
	return code;
}


int ds_uninst_set_cgi_env(char *pszInfoFileName)
{
	InstallInfo *uninstallInfo = NULL;
	InstallInfo *instanceInfo = NULL;
	static char szQueryString[512] = {0};
	static char szScriptName[512] = {0};
	static char szNetsiteRoot[512] = {0};
	const char *serverID = 0;
	const char *tmp;

	uninstallInfo = new InstallInfo(pszInfoFileName);

	if (!uninstallInfo)
		return 1;

	instanceInfo = uninstallInfo->getSection("uninstall");
	if (!instanceInfo)
		instanceInfo = uninstallInfo;

	putenv("REQUEST_METHOD=GET");
	if (instanceInfo->get(SLAPD_KEY_SERVER_IDENTIFIER))
		serverID = instanceInfo->get(SLAPD_KEY_SERVER_IDENTIFIER);
	else if (ds_get_server_name())
		serverID = ds_get_server_name();

	if (serverID)
		sprintf(szQueryString, "QUERY_STRING=InstanceName=%s",
				serverID);

	putenv(szQueryString);

	if (instanceInfo->get(SLAPD_KEY_SERVER_ROOT))
		sprintf(szNetsiteRoot, "NETSITE_ROOT=%s",
				instanceInfo->get(SLAPD_KEY_SERVER_ROOT));
	putenv(szNetsiteRoot);

	if (serverID)
		sprintf(szScriptName, "SCRIPT_NAME=/%s/Tasks/Operation/Remove",
				serverID);
	putenv(szScriptName);

	// remove SIE entry
	const char *host = instanceInfo->get(SLAPD_KEY_K_LDAP_HOST);
	char port[20] = {0};
	if (instanceInfo->get(SLAPD_KEY_K_LDAP_PORT))
		strcpy(port, instanceInfo->get(SLAPD_KEY_K_LDAP_PORT));
	const char *suffix = instanceInfo->get(SLAPD_KEY_SUFFIX);
	const char *ldapurl = instanceInfo->get(SLAPD_KEY_K_LDAP_URL);
	LDAPURLDesc *desc = 0;
	if (ldapurl && !ldap_url_parse((char *)ldapurl, &desc) && desc) {
		if (!host)
			host = desc->lud_host;
		if (port[0] == 0)
			sprintf(port, "%d", desc->lud_port);
		if (!suffix)
			suffix = desc->lud_dn;
	}

	// get and set the log file
	if (tmp = instanceInfo->get(SLAPD_INSTALL_LOG_FILE_NAME))
	{
		static char s_logfile[PATH_MAX+32];
		PR_snprintf(s_logfile, PATH_MAX+32, "DEBUG_LOGFILE=%s", tmp);
		putenv(s_logfile);
		installLog = new InstallLog(tmp);
	}
		
	removeInstanceLDAPEntries(host, port, suffix,
							  instanceInfo->get(SLAPD_KEY_SERVER_ADMIN_ID),
							  instanceInfo->get(SLAPD_KEY_SERVER_ADMIN_PWD),
							  serverID, 
							  instanceInfo->get(SLAPD_KEY_FULL_MACHINE_NAME),
							  instanceInfo->get(SLAPD_KEY_SERVER_ROOT));

	if (desc)
		ldap_free_urldesc(desc);
	return 0;
}