1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
23
* Mitesh Shah <mitesh@netscape.com>
24
* Chip Clark <chipc@netscape.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
41
// sorry, this has to be before the pre-compiled header
42
#define FORCE_PR_LOG /* Allow logging in the release build */
44
#include "nsReadConfig.h"
45
#include "nsAppDirectoryServiceDefs.h"
46
#include "nsIAppShellService.h"
47
#include "nsDirectoryServiceDefs.h"
48
#include "nsIAutoConfig.h"
49
#include "nsIComponentManager.h"
51
#include "nsIObserverService.h"
52
#include "nsIPrefBranch.h"
53
#include "nsIPrefService.h"
54
#include "nsIPromptService.h"
55
#include "nsIServiceManager.h"
56
#include "nsIStringBundle.h"
57
#include "nsXPIDLString.h"
58
#include "nsNetUtil.h"
64
extern PRLogModuleInfo *MCD;
66
extern nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length,
68
PRBool bGlobalContext,
70
PRBool skipFirstLine);
71
extern nsresult CentralizedAdminPrefManagerInit();
72
extern nsresult CentralizedAdminPrefManagerFinish();
75
static void DisplayError(void)
79
nsCOMPtr<nsIPromptService> promptService = do_GetService("@mozilla.org/embedcomp/prompt-service;1");
83
nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
87
nsCOMPtr<nsIStringBundle> bundle;
88
bundleService->CreateBundle("chrome://autoconfig/locale/autoconfig.properties",
89
getter_AddRefs(bundle));
94
rv = bundle->GetStringFromName(NS_LITERAL_STRING("readConfigTitle").get(), getter_Copies(title));
99
rv = bundle->GetStringFromName(NS_LITERAL_STRING("readConfigMsg").get(), getter_Copies(err));
103
promptService->Alert(nsnull, title.get(), err.get());
106
// nsISupports Implementation
108
NS_IMPL_THREADSAFE_ISUPPORTS2(nsReadConfig, nsIReadConfig, nsIObserver)
110
nsReadConfig::nsReadConfig() :
114
MCD = PR_NewLogModule("MCD");
117
nsresult nsReadConfig::Init()
121
nsCOMPtr<nsIObserverService> observerService =
122
do_GetService("@mozilla.org/observer-service;1", &rv);
124
if (observerService) {
125
rv = observerService->AddObserver(this, NS_PREFSERVICE_READ_TOPIC_ID, PR_FALSE);
130
nsReadConfig::~nsReadConfig()
132
CentralizedAdminPrefManagerFinish();
135
NS_IMETHODIMP nsReadConfig::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
139
if (!nsCRT::strcmp(aTopic, NS_PREFSERVICE_READ_TOPIC_ID)) {
140
rv = readConfigFile();
144
nsCOMPtr<nsIAppShellService> appShellService =
145
do_GetService("@mozilla.org/appshell/appShellService;1");
147
appShellService->Quit(nsIAppShellService::eAttemptQuit);
154
nsresult nsReadConfig::readConfigFile()
157
nsXPIDLCString lockFileName;
158
nsXPIDLCString lockVendor;
159
PRUint32 fileNameLen = 0;
161
nsCOMPtr<nsIPrefBranch> prefBranch;
162
nsCOMPtr<nsIPrefService> prefService =
163
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
167
rv = prefService->GetBranch(nsnull, getter_AddRefs(prefBranch));
171
// This preference is set in the all.js or all-ns.js (depending whether
172
// running mozilla or netscp6)
174
rv = prefBranch->GetCharPref("general.config.filename",
175
getter_Copies(lockFileName));
178
PR_LOG(MCD, PR_LOG_DEBUG, ("general.config.filename = %s\n", lockFileName.get()));
182
// This needs to be read only once.
185
// Initiate the new JS Context for Preference management
187
rv = CentralizedAdminPrefManagerInit();
191
// Open and evaluate function calls to set/lock/unlock prefs
192
rv = openAndEvaluateJSFile("prefcalls.js", 0, PR_FALSE, PR_FALSE);
196
// Evaluate platform specific directives
197
rv = openAndEvaluateJSFile("platform.js", 0, PR_FALSE, PR_FALSE);
203
// If the lockFileName is NULL return ok, because no lockFile will be used
206
// Once the config file is read, we should check that the vendor name
207
// is consistent By checking for the vendor name after reading the config
208
// file we allow for the preference to be set (and locked) by the creator
209
// of the cfg file meaning the file can not be renamed (successfully).
211
PRInt32 obscureValue = 0;
212
(void) prefBranch->GetIntPref("general.config.obscure_value", &obscureValue);
213
PR_LOG(MCD, PR_LOG_DEBUG, ("evaluating .cfg file %s with obscureValue %d\n", lockFileName.get(), obscureValue));
214
rv = openAndEvaluateJSFile(lockFileName.get(), PR_TRUE, obscureValue, PR_TRUE);
217
PR_LOG(MCD, PR_LOG_DEBUG, ("error evaluating .cfg file %s %x\n", lockFileName.get(), rv));
221
rv = prefBranch->GetCharPref("general.config.filename",
222
getter_Copies(lockFileName));
224
// There is NO REASON we should ever get here. This is POST reading
225
// of the config file.
226
return NS_ERROR_FAILURE;
229
rv = prefBranch->GetCharPref("general.config.vendor",
230
getter_Copies(lockVendor));
231
// If vendor is not NULL, do this check
232
if (NS_SUCCEEDED(rv)) {
234
fileNameLen = PL_strlen(lockFileName);
236
// lockVendor and lockFileName should be the same with the addtion of
237
// .cfg to the filename by checking this post reading of the cfg file
238
// this value can be set within the cfg file adding a level of security.
240
if (PL_strncmp(lockFileName, lockVendor, fileNameLen - 4) != 0)
241
return NS_ERROR_FAILURE;
244
// get the value of the autoconfig url
245
nsXPIDLCString urlName;
246
rv = prefBranch->GetCharPref("autoadmin.global_config_url",
247
getter_Copies(urlName));
248
if (NS_SUCCEEDED(rv) && *urlName != '\0' ) {
250
// Instantiating nsAutoConfig object if the pref is present
251
mAutoConfig = do_CreateInstance(NS_AUTOCONFIG_CONTRACTID, &rv);
253
return NS_ERROR_OUT_OF_MEMORY;
255
rv = mAutoConfig->SetConfigURL(urlName);
257
return NS_ERROR_FAILURE;
265
nsresult nsReadConfig::openAndEvaluateJSFile(const char *aFileName, PRBool isEncoded,
266
PRInt32 obscureValue,
270
nsCOMPtr<nsIFile> jsFile;
273
rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
274
getter_AddRefs(jsFile));
279
jsFile->AppendNative(NS_LITERAL_CSTRING("Essential Files"));
282
rv = NS_GetSpecialDirectory(NS_APP_DEFAULTS_50_DIR,
283
getter_AddRefs(jsFile));
286
rv = jsFile->AppendNative(NS_LITERAL_CSTRING("autoconfig"));
290
rv = jsFile->AppendNative(nsDependentCString(aFileName));
294
nsCOMPtr<nsIInputStream> inStr;
295
rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), jsFile);
300
PRUint32 fs, amt = 0;
301
jsFile->GetFileSize(&fileSize);
302
LL_L2UI(fs, fileSize); // Converting 64 bit structure to unsigned int
304
char *buf = (char *)PR_Malloc(fs * sizeof(char));
306
return NS_ERROR_OUT_OF_MEMORY;
308
rv = inStr->Read(buf, fs, &amt);
309
NS_ASSERTION((amt == fs), "failed to read the entire configuration file!!");
310
if (NS_SUCCEEDED(rv)) {
311
if (obscureValue > 0) {
313
// Unobscure file by subtracting some value from every char.
314
for (PRUint32 i = 0; i < amt; i++)
315
buf[i] -= obscureValue;
319
jsFile->GetNativePath(path);
320
nsCAutoString fileURL;
321
fileURL = NS_LITERAL_CSTRING("file:///") + path;
322
rv = EvaluateAdminConfigScript(buf, amt, fileURL.get(),
324
isEncoded ? PR_TRUE:PR_FALSE);