Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "nsReadConfig.h"
7 : #include "nsAppDirectoryServiceDefs.h"
8 : #include "nsIAppStartup.h"
9 : #include "nsDirectoryServiceDefs.h"
10 : #include "nsIAutoConfig.h"
11 : #include "nsIComponentManager.h"
12 : #include "nsIFile.h"
13 : #include "nsIObserverService.h"
14 : #include "nsIPrefBranch.h"
15 : #include "nsIPrefService.h"
16 : #include "nsIPromptService.h"
17 : #include "nsIServiceManager.h"
18 : #include "nsIStringBundle.h"
19 : #include "nsToolkitCompsCID.h"
20 : #include "nsXPIDLString.h"
21 : #include "nsNetUtil.h"
22 : #include "nsString.h"
23 : #include "nsCRT.h"
24 : #include "nspr.h"
25 : #include "nsXULAppAPI.h"
26 : #include "nsContentUtils.h"
27 :
28 : extern mozilla::LazyLogModule MCD;
29 :
30 : extern nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length,
31 : const char *filename,
32 : bool bGlobalContext,
33 : bool bCallbacks,
34 : bool skipFirstLine);
35 : extern nsresult CentralizedAdminPrefManagerInit();
36 : extern nsresult CentralizedAdminPrefManagerFinish();
37 :
38 :
39 0 : static void DisplayError(void)
40 : {
41 : nsresult rv;
42 :
43 0 : nsCOMPtr<nsIPromptService> promptService = do_GetService("@mozilla.org/embedcomp/prompt-service;1");
44 0 : if (!promptService)
45 0 : return;
46 :
47 0 : nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
48 0 : if (!bundleService)
49 0 : return;
50 :
51 0 : nsCOMPtr<nsIStringBundle> bundle;
52 0 : bundleService->CreateBundle("chrome://autoconfig/locale/autoconfig.properties",
53 0 : getter_AddRefs(bundle));
54 0 : if (!bundle)
55 0 : return;
56 :
57 0 : nsXPIDLString title;
58 0 : rv = bundle->GetStringFromName(u"readConfigTitle", getter_Copies(title));
59 0 : if (NS_FAILED(rv))
60 0 : return;
61 :
62 0 : nsXPIDLString err;
63 0 : rv = bundle->GetStringFromName(u"readConfigMsg", getter_Copies(err));
64 0 : if (NS_FAILED(rv))
65 0 : return;
66 :
67 0 : promptService->Alert(nullptr, title.get(), err.get());
68 : }
69 :
70 : // nsISupports Implementation
71 :
72 0 : NS_IMPL_ISUPPORTS(nsReadConfig, nsIReadConfig, nsIObserver)
73 :
74 0 : nsReadConfig::nsReadConfig() :
75 0 : mRead(false)
76 : {
77 0 : }
78 :
79 0 : nsresult nsReadConfig::Init()
80 : {
81 : nsresult rv;
82 :
83 : nsCOMPtr<nsIObserverService> observerService =
84 0 : do_GetService("@mozilla.org/observer-service;1", &rv);
85 :
86 0 : if (observerService) {
87 0 : rv = observerService->AddObserver(this, NS_PREFSERVICE_READ_TOPIC_ID, false);
88 : }
89 0 : return(rv);
90 : }
91 :
92 0 : nsReadConfig::~nsReadConfig()
93 : {
94 0 : CentralizedAdminPrefManagerFinish();
95 0 : }
96 :
97 0 : NS_IMETHODIMP nsReadConfig::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData)
98 : {
99 0 : nsresult rv = NS_OK;
100 :
101 0 : if (!nsCRT::strcmp(aTopic, NS_PREFSERVICE_READ_TOPIC_ID)) {
102 0 : rv = readConfigFile();
103 0 : if (NS_FAILED(rv)) {
104 0 : DisplayError();
105 :
106 : nsCOMPtr<nsIAppStartup> appStartup =
107 0 : do_GetService(NS_APPSTARTUP_CONTRACTID);
108 0 : if (appStartup)
109 0 : appStartup->Quit(nsIAppStartup::eAttemptQuit);
110 : }
111 : }
112 0 : return rv;
113 : }
114 :
115 :
116 0 : nsresult nsReadConfig::readConfigFile()
117 : {
118 0 : nsresult rv = NS_OK;
119 0 : nsXPIDLCString lockFileName;
120 0 : nsXPIDLCString lockVendor;
121 0 : uint32_t fileNameLen = 0;
122 :
123 0 : nsCOMPtr<nsIPrefBranch> defaultPrefBranch;
124 : nsCOMPtr<nsIPrefService> prefService =
125 0 : do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
126 0 : if (NS_FAILED(rv))
127 0 : return rv;
128 :
129 0 : rv = prefService->GetDefaultBranch(nullptr, getter_AddRefs(defaultPrefBranch));
130 0 : if (NS_FAILED(rv))
131 0 : return rv;
132 :
133 : // This preference is set in the all.js or all-ns.js (depending whether
134 : // running mozilla or netscp6)
135 :
136 0 : rv = defaultPrefBranch->GetCharPref("general.config.filename",
137 0 : getter_Copies(lockFileName));
138 :
139 :
140 0 : MOZ_LOG(MCD, LogLevel::Debug, ("general.config.filename = %s\n", lockFileName.get()));
141 0 : if (NS_FAILED(rv))
142 0 : return rv;
143 :
144 : // This needs to be read only once.
145 : //
146 0 : if (!mRead) {
147 : // Initiate the new JS Context for Preference management
148 :
149 0 : rv = CentralizedAdminPrefManagerInit();
150 0 : if (NS_FAILED(rv))
151 0 : return rv;
152 :
153 : // Open and evaluate function calls to set/lock/unlock prefs
154 0 : rv = openAndEvaluateJSFile("prefcalls.js", 0, false, false);
155 0 : if (NS_FAILED(rv))
156 0 : return rv;
157 :
158 0 : mRead = true;
159 : }
160 : // If the lockFileName is nullptr return ok, because no lockFile will be used
161 :
162 :
163 : // Once the config file is read, we should check that the vendor name
164 : // is consistent By checking for the vendor name after reading the config
165 : // file we allow for the preference to be set (and locked) by the creator
166 : // of the cfg file meaning the file can not be renamed (successfully).
167 :
168 0 : nsCOMPtr<nsIPrefBranch> prefBranch;
169 0 : rv = prefService->GetBranch(nullptr, getter_AddRefs(prefBranch));
170 0 : NS_ENSURE_SUCCESS(rv, rv);
171 :
172 0 : int32_t obscureValue = 0;
173 0 : (void) defaultPrefBranch->GetIntPref("general.config.obscure_value", &obscureValue);
174 0 : MOZ_LOG(MCD, LogLevel::Debug, ("evaluating .cfg file %s with obscureValue %d\n", lockFileName.get(), obscureValue));
175 0 : rv = openAndEvaluateJSFile(lockFileName.get(), obscureValue, true, true);
176 0 : if (NS_FAILED(rv))
177 : {
178 0 : MOZ_LOG(MCD, LogLevel::Debug, ("error evaluating .cfg file %s %" PRIx32 "\n",
179 : lockFileName.get(), static_cast<uint32_t>(rv)));
180 0 : return rv;
181 : }
182 :
183 0 : rv = prefBranch->GetCharPref("general.config.filename",
184 0 : getter_Copies(lockFileName));
185 0 : if (NS_FAILED(rv))
186 : // There is NO REASON we should ever get here. This is POST reading
187 : // of the config file.
188 0 : return NS_ERROR_FAILURE;
189 :
190 :
191 0 : rv = prefBranch->GetCharPref("general.config.vendor",
192 0 : getter_Copies(lockVendor));
193 : // If vendor is not nullptr, do this check
194 0 : if (NS_SUCCEEDED(rv)) {
195 :
196 0 : fileNameLen = strlen(lockFileName);
197 :
198 : // lockVendor and lockFileName should be the same with the addtion of
199 : // .cfg to the filename by checking this post reading of the cfg file
200 : // this value can be set within the cfg file adding a level of security.
201 :
202 0 : if (PL_strncmp(lockFileName, lockVendor, fileNameLen - 4) != 0)
203 0 : return NS_ERROR_FAILURE;
204 : }
205 :
206 : // get the value of the autoconfig url
207 0 : nsXPIDLCString urlName;
208 0 : rv = prefBranch->GetCharPref("autoadmin.global_config_url",
209 0 : getter_Copies(urlName));
210 0 : if (NS_SUCCEEDED(rv) && !urlName.IsEmpty()) {
211 :
212 : // Instantiating nsAutoConfig object if the pref is present
213 0 : mAutoConfig = do_CreateInstance(NS_AUTOCONFIG_CONTRACTID, &rv);
214 0 : if (NS_FAILED(rv))
215 0 : return NS_ERROR_OUT_OF_MEMORY;
216 :
217 0 : rv = mAutoConfig->SetConfigURL(urlName);
218 0 : if (NS_FAILED(rv))
219 0 : return NS_ERROR_FAILURE;
220 :
221 : }
222 :
223 0 : return NS_OK;
224 : } // ReadConfigFile
225 :
226 :
227 0 : nsresult nsReadConfig::openAndEvaluateJSFile(const char *aFileName, int32_t obscureValue,
228 : bool isEncoded,
229 : bool isBinDir)
230 : {
231 : nsresult rv;
232 :
233 0 : nsCOMPtr<nsIInputStream> inStr;
234 0 : if (isBinDir) {
235 0 : nsCOMPtr<nsIFile> jsFile;
236 0 : rv = NS_GetSpecialDirectory(NS_GRE_DIR,
237 0 : getter_AddRefs(jsFile));
238 0 : if (NS_FAILED(rv))
239 0 : return rv;
240 :
241 0 : rv = jsFile->AppendNative(nsDependentCString(aFileName));
242 0 : if (NS_FAILED(rv))
243 0 : return rv;
244 :
245 0 : rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), jsFile);
246 0 : if (NS_FAILED(rv))
247 0 : return rv;
248 :
249 : } else {
250 0 : nsAutoCString location("resource://gre/defaults/autoconfig/");
251 0 : location += aFileName;
252 :
253 0 : nsCOMPtr<nsIURI> uri;
254 0 : rv = NS_NewURI(getter_AddRefs(uri), location);
255 0 : NS_ENSURE_SUCCESS(rv, rv);
256 :
257 0 : nsCOMPtr<nsIChannel> channel;
258 0 : rv = NS_NewChannel(getter_AddRefs(channel),
259 : uri,
260 : nsContentUtils::GetSystemPrincipal(),
261 : nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
262 0 : nsIContentPolicy::TYPE_OTHER);
263 0 : NS_ENSURE_SUCCESS(rv, rv);
264 :
265 0 : rv = channel->Open2(getter_AddRefs(inStr));
266 0 : NS_ENSURE_SUCCESS(rv, rv);
267 : }
268 :
269 : uint64_t fs64;
270 0 : uint32_t amt = 0;
271 0 : rv = inStr->Available(&fs64);
272 0 : if (NS_FAILED(rv))
273 0 : return rv;
274 : // This used to use PR_Malloc(), which doesn't support over 4GB.
275 0 : if (fs64 > UINT32_MAX)
276 0 : return NS_ERROR_FILE_TOO_BIG;
277 0 : uint32_t fs = (uint32_t)fs64;
278 :
279 0 : char* buf = (char*) malloc(fs * sizeof(char));
280 0 : if (!buf)
281 0 : return NS_ERROR_OUT_OF_MEMORY;
282 :
283 0 : rv = inStr->Read(buf, (uint32_t)fs, &amt);
284 0 : NS_ASSERTION((amt == fs), "failed to read the entire configuration file!!");
285 0 : if (NS_SUCCEEDED(rv)) {
286 0 : if (obscureValue > 0) {
287 :
288 : // Unobscure file by subtracting some value from every char.
289 0 : for (uint32_t i = 0; i < amt; i++)
290 0 : buf[i] -= obscureValue;
291 : }
292 0 : rv = EvaluateAdminConfigScript(buf, amt, aFileName,
293 : false, true,
294 0 : isEncoded ? true:false);
295 : }
296 0 : inStr->Close();
297 0 : free(buf);
298 :
299 0 : return rv;
300 9 : }
|