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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Mitesh Shah <mitesh@netscape.com>
24 : * Chip Clark <chipc@netscape.com>
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifdef MOZ_LOGGING
41 : // sorry, this has to be before the pre-compiled header
42 : #define FORCE_PR_LOG /* Allow logging in the release build */
43 : #endif
44 : #include "nsReadConfig.h"
45 : #include "nsAppDirectoryServiceDefs.h"
46 : #include "nsIAppStartup.h"
47 : #include "nsDirectoryServiceDefs.h"
48 : #include "nsIAutoConfig.h"
49 : #include "nsIComponentManager.h"
50 : #include "nsIFile.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"
60 : #include "prmem.h"
61 : #include "nsString.h"
62 : #include "nsCRT.h"
63 : #include "nspr.h"
64 :
65 : extern PRLogModuleInfo *MCD;
66 :
67 : extern nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length,
68 : const char *filename,
69 : bool bGlobalContext,
70 : bool bCallbacks,
71 : bool skipFirstLine);
72 : extern nsresult CentralizedAdminPrefManagerInit();
73 : extern nsresult CentralizedAdminPrefManagerFinish();
74 :
75 :
76 0 : static void DisplayError(void)
77 : {
78 : nsresult rv;
79 :
80 0 : nsCOMPtr<nsIPromptService> promptService = do_GetService("@mozilla.org/embedcomp/prompt-service;1");
81 0 : if (!promptService)
82 : return;
83 :
84 0 : nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
85 0 : if (!bundleService)
86 : return;
87 :
88 0 : nsCOMPtr<nsIStringBundle> bundle;
89 0 : bundleService->CreateBundle("chrome://autoconfig/locale/autoconfig.properties",
90 0 : getter_AddRefs(bundle));
91 0 : if (!bundle)
92 : return;
93 :
94 0 : nsXPIDLString title;
95 0 : rv = bundle->GetStringFromName(NS_LITERAL_STRING("readConfigTitle").get(), getter_Copies(title));
96 0 : if (NS_FAILED(rv))
97 : return;
98 :
99 0 : nsXPIDLString err;
100 0 : rv = bundle->GetStringFromName(NS_LITERAL_STRING("readConfigMsg").get(), getter_Copies(err));
101 0 : if (NS_FAILED(rv))
102 : return;
103 :
104 0 : promptService->Alert(nsnull, title.get(), err.get());
105 : }
106 :
107 : // nsISupports Implementation
108 :
109 0 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsReadConfig, nsIReadConfig, nsIObserver)
110 :
111 0 : nsReadConfig::nsReadConfig() :
112 0 : mRead(false)
113 : {
114 0 : if (!MCD)
115 0 : MCD = PR_NewLogModule("MCD");
116 0 : }
117 :
118 0 : nsresult nsReadConfig::Init()
119 : {
120 : nsresult rv;
121 :
122 : nsCOMPtr<nsIObserverService> observerService =
123 0 : do_GetService("@mozilla.org/observer-service;1", &rv);
124 :
125 0 : if (observerService) {
126 0 : rv = observerService->AddObserver(this, NS_PREFSERVICE_READ_TOPIC_ID, false);
127 : }
128 0 : return(rv);
129 : }
130 :
131 0 : nsReadConfig::~nsReadConfig()
132 : {
133 0 : CentralizedAdminPrefManagerFinish();
134 0 : }
135 :
136 0 : NS_IMETHODIMP nsReadConfig::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
137 : {
138 0 : nsresult rv = NS_OK;
139 :
140 0 : if (!nsCRT::strcmp(aTopic, NS_PREFSERVICE_READ_TOPIC_ID)) {
141 0 : rv = readConfigFile();
142 0 : if (NS_FAILED(rv)) {
143 0 : DisplayError();
144 :
145 : nsCOMPtr<nsIAppStartup> appStartup =
146 0 : do_GetService(NS_APPSTARTUP_CONTRACTID);
147 0 : if (appStartup)
148 0 : appStartup->Quit(nsIAppStartup::eAttemptQuit);
149 : }
150 : }
151 0 : return rv;
152 : }
153 :
154 :
155 0 : nsresult nsReadConfig::readConfigFile()
156 : {
157 0 : nsresult rv = NS_OK;
158 0 : nsXPIDLCString lockFileName;
159 0 : nsXPIDLCString lockVendor;
160 0 : PRUint32 fileNameLen = 0;
161 :
162 0 : nsCOMPtr<nsIPrefBranch> defaultPrefBranch;
163 : nsCOMPtr<nsIPrefService> prefService =
164 0 : do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
165 0 : if (NS_FAILED(rv))
166 0 : return rv;
167 :
168 0 : rv = prefService->GetDefaultBranch(nsnull, getter_AddRefs(defaultPrefBranch));
169 0 : if (NS_FAILED(rv))
170 0 : return rv;
171 :
172 : // This preference is set in the all.js or all-ns.js (depending whether
173 : // running mozilla or netscp6)
174 :
175 0 : rv = defaultPrefBranch->GetCharPref("general.config.filename",
176 0 : getter_Copies(lockFileName));
177 :
178 :
179 0 : PR_LOG(MCD, PR_LOG_DEBUG, ("general.config.filename = %s\n", lockFileName.get()));
180 0 : if (NS_FAILED(rv))
181 0 : return rv;
182 :
183 : // This needs to be read only once.
184 : //
185 0 : if (!mRead) {
186 : // Initiate the new JS Context for Preference management
187 :
188 0 : rv = CentralizedAdminPrefManagerInit();
189 0 : if (NS_FAILED(rv))
190 0 : return rv;
191 :
192 : // Open and evaluate function calls to set/lock/unlock prefs
193 0 : rv = openAndEvaluateJSFile("prefcalls.js", 0, false, false);
194 0 : if (NS_FAILED(rv))
195 0 : return rv;
196 :
197 : // Evaluate platform specific directives
198 0 : rv = openAndEvaluateJSFile("platform.js", 0, false, false);
199 0 : if (NS_FAILED(rv))
200 0 : return rv;
201 :
202 0 : mRead = true;
203 : }
204 : // If the lockFileName is NULL return ok, because no lockFile will be used
205 :
206 :
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).
211 :
212 0 : nsCOMPtr<nsIPrefBranch> prefBranch;
213 0 : rv = prefService->GetBranch(nsnull, getter_AddRefs(prefBranch));
214 0 : NS_ENSURE_SUCCESS(rv, rv);
215 :
216 0 : PRInt32 obscureValue = 0;
217 0 : (void) defaultPrefBranch->GetIntPref("general.config.obscure_value", &obscureValue);
218 0 : PR_LOG(MCD, PR_LOG_DEBUG, ("evaluating .cfg file %s with obscureValue %d\n", lockFileName.get(), obscureValue));
219 0 : rv = openAndEvaluateJSFile(lockFileName.get(), obscureValue, true, true);
220 0 : if (NS_FAILED(rv))
221 : {
222 0 : PR_LOG(MCD, PR_LOG_DEBUG, ("error evaluating .cfg file %s %x\n", lockFileName.get(), rv));
223 0 : return rv;
224 : }
225 :
226 0 : rv = prefBranch->GetCharPref("general.config.filename",
227 0 : getter_Copies(lockFileName));
228 0 : if (NS_FAILED(rv))
229 : // There is NO REASON we should ever get here. This is POST reading
230 : // of the config file.
231 0 : return NS_ERROR_FAILURE;
232 :
233 :
234 0 : rv = prefBranch->GetCharPref("general.config.vendor",
235 0 : getter_Copies(lockVendor));
236 : // If vendor is not NULL, do this check
237 0 : if (NS_SUCCEEDED(rv)) {
238 :
239 0 : fileNameLen = PL_strlen(lockFileName);
240 :
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.
244 :
245 0 : if (PL_strncmp(lockFileName, lockVendor, fileNameLen - 4) != 0)
246 0 : return NS_ERROR_FAILURE;
247 : }
248 :
249 : // get the value of the autoconfig url
250 0 : nsXPIDLCString urlName;
251 0 : rv = prefBranch->GetCharPref("autoadmin.global_config_url",
252 0 : getter_Copies(urlName));
253 0 : if (NS_SUCCEEDED(rv) && !urlName.IsEmpty()) {
254 :
255 : // Instantiating nsAutoConfig object if the pref is present
256 0 : mAutoConfig = do_CreateInstance(NS_AUTOCONFIG_CONTRACTID, &rv);
257 0 : if (NS_FAILED(rv))
258 0 : return NS_ERROR_OUT_OF_MEMORY;
259 :
260 0 : rv = mAutoConfig->SetConfigURL(urlName);
261 0 : if (NS_FAILED(rv))
262 0 : return NS_ERROR_FAILURE;
263 :
264 : }
265 :
266 0 : return NS_OK;
267 : } // ReadConfigFile
268 :
269 :
270 0 : nsresult nsReadConfig::openAndEvaluateJSFile(const char *aFileName, PRInt32 obscureValue,
271 : bool isEncoded,
272 : bool isBinDir)
273 : {
274 : nsresult rv;
275 :
276 0 : nsCOMPtr<nsIInputStream> inStr;
277 0 : if (isBinDir) {
278 0 : nsCOMPtr<nsIFile> jsFile;
279 : rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
280 0 : getter_AddRefs(jsFile));
281 0 : if (NS_FAILED(rv))
282 0 : return rv;
283 :
284 0 : rv = jsFile->AppendNative(nsDependentCString(aFileName));
285 0 : if (NS_FAILED(rv))
286 0 : return rv;
287 :
288 0 : rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), jsFile);
289 0 : if (NS_FAILED(rv))
290 0 : return rv;
291 :
292 : } else {
293 0 : nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
294 0 : if (NS_FAILED(rv))
295 0 : return rv;
296 :
297 0 : nsCAutoString location("resource://gre/defaults/autoconfig/");
298 0 : location += aFileName;
299 :
300 0 : nsCOMPtr<nsIURI> uri;
301 0 : rv = ioService->NewURI(location, nsnull, nsnull, getter_AddRefs(uri));
302 0 : if (NS_FAILED(rv))
303 0 : return rv;
304 :
305 0 : nsCOMPtr<nsIChannel> channel;
306 0 : rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
307 0 : if (NS_FAILED(rv))
308 0 : return rv;
309 :
310 0 : rv = channel->Open(getter_AddRefs(inStr));
311 0 : if (NS_FAILED(rv))
312 0 : return rv;
313 : }
314 :
315 0 : PRUint32 fs, amt = 0;
316 0 : inStr->Available(&fs);
317 :
318 0 : char *buf = (char *)PR_Malloc(fs * sizeof(char));
319 0 : if (!buf)
320 0 : return NS_ERROR_OUT_OF_MEMORY;
321 :
322 0 : rv = inStr->Read(buf, fs, &amt);
323 0 : NS_ASSERTION((amt == fs), "failed to read the entire configuration file!!");
324 0 : if (NS_SUCCEEDED(rv)) {
325 0 : if (obscureValue > 0) {
326 :
327 : // Unobscure file by subtracting some value from every char.
328 0 : for (PRUint32 i = 0; i < amt; i++)
329 0 : buf[i] -= obscureValue;
330 : }
331 : rv = EvaluateAdminConfigScript(buf, amt, aFileName,
332 : false, true,
333 0 : isEncoded ? true:false);
334 : }
335 0 : inStr->Close();
336 0 : PR_Free(buf);
337 :
338 0 : return rv;
339 : }
|