1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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 MPL, 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 MPL, 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 "nsIAppStartup.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 "nsToolkitCompsCID.h"
58 #include "nsXPIDLString.h"
59 #include "nsNetUtil.h"
65 extern PRLogModuleInfo
*MCD
;
67 extern nsresult
EvaluateAdminConfigScript(const char *js_buffer
, size_t length
,
69 PRBool bGlobalContext
,
71 PRBool skipFirstLine
);
72 extern nsresult
CentralizedAdminPrefManagerInit();
73 extern nsresult
CentralizedAdminPrefManagerFinish();
76 static void DisplayError(void)
80 nsCOMPtr
<nsIPromptService
> promptService
= do_GetService("@mozilla.org/embedcomp/prompt-service;1");
84 nsCOMPtr
<nsIStringBundleService
> bundleService
= do_GetService(NS_STRINGBUNDLE_CONTRACTID
);
88 nsCOMPtr
<nsIStringBundle
> bundle
;
89 bundleService
->CreateBundle("chrome://autoconfig/locale/autoconfig.properties",
90 getter_AddRefs(bundle
));
95 rv
= bundle
->GetStringFromName(NS_LITERAL_STRING("readConfigTitle").get(), getter_Copies(title
));
100 rv
= bundle
->GetStringFromName(NS_LITERAL_STRING("readConfigMsg").get(), getter_Copies(err
));
104 promptService
->Alert(nsnull
, title
.get(), err
.get());
107 // nsISupports Implementation
109 NS_IMPL_THREADSAFE_ISUPPORTS2(nsReadConfig
, nsIReadConfig
, nsIObserver
)
111 nsReadConfig::nsReadConfig() :
115 MCD
= PR_NewLogModule("MCD");
118 nsresult
nsReadConfig::Init()
122 nsCOMPtr
<nsIObserverService
> observerService
=
123 do_GetService("@mozilla.org/observer-service;1", &rv
);
125 if (observerService
) {
126 rv
= observerService
->AddObserver(this, NS_PREFSERVICE_READ_TOPIC_ID
, PR_FALSE
);
131 nsReadConfig::~nsReadConfig()
133 CentralizedAdminPrefManagerFinish();
136 NS_IMETHODIMP
nsReadConfig::Observe(nsISupports
*aSubject
, const char *aTopic
, const PRUnichar
*someData
)
140 if (!nsCRT::strcmp(aTopic
, NS_PREFSERVICE_READ_TOPIC_ID
)) {
141 rv
= readConfigFile();
145 nsCOMPtr
<nsIAppStartup
> appStartup
=
146 do_GetService(NS_APPSTARTUP_CONTRACTID
);
148 appStartup
->Quit(nsIAppStartup::eAttemptQuit
);
155 nsresult
nsReadConfig::readConfigFile()
158 nsXPIDLCString lockFileName
;
159 nsXPIDLCString lockVendor
;
160 PRUint32 fileNameLen
= 0;
162 nsCOMPtr
<nsIPrefBranch
> defaultPrefBranch
;
163 nsCOMPtr
<nsIPrefService
> prefService
=
164 do_GetService(NS_PREFSERVICE_CONTRACTID
, &rv
);
168 rv
= prefService
->GetDefaultBranch(nsnull
, getter_AddRefs(defaultPrefBranch
));
172 // This preference is set in the all.js or all-ns.js (depending whether
173 // running mozilla or netscp6)
175 rv
= defaultPrefBranch
->GetCharPref("general.config.filename",
176 getter_Copies(lockFileName
));
179 PR_LOG(MCD
, PR_LOG_DEBUG
, ("general.config.filename = %s\n", lockFileName
.get()));
183 // This needs to be read only once.
186 // Initiate the new JS Context for Preference management
188 rv
= CentralizedAdminPrefManagerInit();
192 // Open and evaluate function calls to set/lock/unlock prefs
193 rv
= openAndEvaluateJSFile("prefcalls.js", 0, PR_FALSE
, PR_FALSE
);
197 // Evaluate platform specific directives
198 rv
= openAndEvaluateJSFile("platform.js", 0, PR_FALSE
, PR_FALSE
);
204 // If the lockFileName is NULL return ok, because no lockFile will be used
207 // Once the config file is read, we should check that the vendor name
208 // is consistent By checking for the vendor name after reading the config
209 // file we allow for the preference to be set (and locked) by the creator
210 // of the cfg file meaning the file can not be renamed (successfully).
212 nsCOMPtr
<nsIPrefBranch
> prefBranch
;
213 rv
= prefService
->GetBranch(nsnull
, getter_AddRefs(prefBranch
));
214 NS_ENSURE_SUCCESS(rv
, rv
);
216 PRInt32 obscureValue
= 0;
217 (void) defaultPrefBranch
->GetIntPref("general.config.obscure_value", &obscureValue
);
218 PR_LOG(MCD
, PR_LOG_DEBUG
, ("evaluating .cfg file %s with obscureValue %d\n", lockFileName
.get(), obscureValue
));
219 rv
= openAndEvaluateJSFile(lockFileName
.get(), obscureValue
, PR_TRUE
, PR_TRUE
);
222 PR_LOG(MCD
, PR_LOG_DEBUG
, ("error evaluating .cfg file %s %x\n", lockFileName
.get(), rv
));
226 rv
= prefBranch
->GetCharPref("general.config.filename",
227 getter_Copies(lockFileName
));
229 // There is NO REASON we should ever get here. This is POST reading
230 // of the config file.
231 return NS_ERROR_FAILURE
;
234 rv
= prefBranch
->GetCharPref("general.config.vendor",
235 getter_Copies(lockVendor
));
236 // If vendor is not NULL, do this check
237 if (NS_SUCCEEDED(rv
)) {
239 fileNameLen
= PL_strlen(lockFileName
);
241 // lockVendor and lockFileName should be the same with the addtion of
242 // .cfg to the filename by checking this post reading of the cfg file
243 // this value can be set within the cfg file adding a level of security.
245 if (PL_strncmp(lockFileName
, lockVendor
, fileNameLen
- 4) != 0)
246 return NS_ERROR_FAILURE
;
249 // get the value of the autoconfig url
250 nsXPIDLCString urlName
;
251 rv
= prefBranch
->GetCharPref("autoadmin.global_config_url",
252 getter_Copies(urlName
));
253 if (NS_SUCCEEDED(rv
) && !urlName
.IsEmpty()) {
255 // Instantiating nsAutoConfig object if the pref is present
256 mAutoConfig
= do_CreateInstance(NS_AUTOCONFIG_CONTRACTID
, &rv
);
258 return NS_ERROR_OUT_OF_MEMORY
;
260 rv
= mAutoConfig
->SetConfigURL(urlName
);
262 return NS_ERROR_FAILURE
;
270 nsresult
nsReadConfig::openAndEvaluateJSFile(const char *aFileName
, PRInt32 obscureValue
,
276 nsCOMPtr
<nsIInputStream
> inStr
;
278 nsCOMPtr
<nsIFile
> jsFile
;
279 rv
= NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR
,
280 getter_AddRefs(jsFile
));
285 jsFile
->AppendNative(NS_LITERAL_CSTRING("Essential Files"));
287 rv
= jsFile
->AppendNative(nsDependentCString(aFileName
));
291 rv
= NS_NewLocalFileInputStream(getter_AddRefs(inStr
), jsFile
);
296 nsCOMPtr
<nsIIOService
> ioService
= do_GetIOService(&rv
);
300 nsCAutoString
location("resource://gre/defaults/autoconfig/");
301 location
+= aFileName
;
303 nsCOMPtr
<nsIURI
> uri
;
304 rv
= ioService
->NewURI(location
, nsnull
, nsnull
, getter_AddRefs(uri
));
308 nsCOMPtr
<nsIChannel
> channel
;
309 rv
= ioService
->NewChannelFromURI(uri
, getter_AddRefs(channel
));
313 rv
= channel
->Open(getter_AddRefs(inStr
));
318 PRUint32 fs
, amt
= 0;
319 inStr
->Available(&fs
);
321 char *buf
= (char *)PR_Malloc(fs
* sizeof(char));
323 return NS_ERROR_OUT_OF_MEMORY
;
325 rv
= inStr
->Read(buf
, fs
, &amt
);
326 NS_ASSERTION((amt
== fs
), "failed to read the entire configuration file!!");
327 if (NS_SUCCEEDED(rv
)) {
328 if (obscureValue
> 0) {
330 // Unobscure file by subtracting some value from every char.
331 for (PRUint32 i
= 0; i
< amt
; i
++)
332 buf
[i
] -= obscureValue
;
334 rv
= EvaluateAdminConfigScript(buf
, amt
, aFileName
,
336 isEncoded
? PR_TRUE
:PR_FALSE
);