~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/pref/autoconfig/src/nsReadConfig.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
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 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.
 
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 "nsIAppShellService.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 "nsXPIDLString.h"
 
58
#include "nsNetUtil.h"
 
59
#include "prmem.h"
 
60
#include "nsString.h"
 
61
#include "nsCRT.h"
 
62
#include "nspr.h"
 
63
 
 
64
extern PRLogModuleInfo *MCD;
 
65
 
 
66
extern nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length,
 
67
                                          const char *filename, 
 
68
                                          PRBool bGlobalContext, 
 
69
                                          PRBool bCallbacks, 
 
70
                                          PRBool skipFirstLine);
 
71
extern nsresult CentralizedAdminPrefManagerInit();
 
72
extern nsresult CentralizedAdminPrefManagerFinish();
 
73
 
 
74
 
 
75
static void DisplayError(void)
 
76
{
 
77
    nsresult rv;
 
78
 
 
79
    nsCOMPtr<nsIPromptService> promptService = do_GetService("@mozilla.org/embedcomp/prompt-service;1");
 
80
    if (!promptService)
 
81
        return;
 
82
 
 
83
    nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
 
84
    if (!bundleService)
 
85
        return;
 
86
 
 
87
    nsCOMPtr<nsIStringBundle> bundle;
 
88
    bundleService->CreateBundle("chrome://autoconfig/locale/autoconfig.properties",
 
89
                                getter_AddRefs(bundle));
 
90
    if (!bundle)
 
91
        return;
 
92
 
 
93
    nsXPIDLString title;
 
94
    rv = bundle->GetStringFromName(NS_LITERAL_STRING("readConfigTitle").get(), getter_Copies(title));
 
95
    if (NS_FAILED(rv))
 
96
        return;
 
97
 
 
98
    nsXPIDLString err;
 
99
    rv = bundle->GetStringFromName(NS_LITERAL_STRING("readConfigMsg").get(), getter_Copies(err));
 
100
    if (NS_FAILED(rv))
 
101
        return;
 
102
 
 
103
    promptService->Alert(nsnull, title.get(), err.get());
 
104
}
 
105
 
 
106
// nsISupports Implementation
 
107
 
 
108
NS_IMPL_THREADSAFE_ISUPPORTS2(nsReadConfig, nsIReadConfig, nsIObserver)
 
109
 
 
110
nsReadConfig::nsReadConfig() :
 
111
    mRead(PR_FALSE)
 
112
{
 
113
    if (!MCD)
 
114
      MCD = PR_NewLogModule("MCD");
 
115
}
 
116
 
 
117
nsresult nsReadConfig::Init()
 
118
{
 
119
    nsresult rv;
 
120
    
 
121
    nsCOMPtr<nsIObserverService> observerService = 
 
122
        do_GetService("@mozilla.org/observer-service;1", &rv);
 
123
 
 
124
    if (observerService) {
 
125
        rv = observerService->AddObserver(this, NS_PREFSERVICE_READ_TOPIC_ID, PR_FALSE);
 
126
    }
 
127
    return(rv);
 
128
}
 
129
 
 
130
nsReadConfig::~nsReadConfig()
 
131
{
 
132
    CentralizedAdminPrefManagerFinish();
 
133
}
 
134
 
 
135
NS_IMETHODIMP nsReadConfig::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
 
136
{
 
137
    nsresult rv = NS_OK;
 
138
 
 
139
    if (!nsCRT::strcmp(aTopic, NS_PREFSERVICE_READ_TOPIC_ID)) {
 
140
        rv = readConfigFile();
 
141
        if (NS_FAILED(rv)) {
 
142
            DisplayError();
 
143
 
 
144
            nsCOMPtr<nsIAppShellService> appShellService =
 
145
                do_GetService("@mozilla.org/appshell/appShellService;1");
 
146
            if (appShellService)
 
147
                appShellService->Quit(nsIAppShellService::eAttemptQuit);
 
148
        }
 
149
    }
 
150
    return rv;
 
151
}
 
152
 
 
153
 
 
154
nsresult nsReadConfig::readConfigFile()
 
155
{
 
156
    nsresult rv = NS_OK;
 
157
    nsXPIDLCString lockFileName;
 
158
    nsXPIDLCString lockVendor;
 
159
    PRUint32 fileNameLen = 0;
 
160
    
 
161
    nsCOMPtr<nsIPrefBranch> prefBranch;
 
162
    nsCOMPtr<nsIPrefService> prefService = 
 
163
        do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
 
164
    if (NS_FAILED(rv))
 
165
        return rv;
 
166
 
 
167
    rv = prefService->GetBranch(nsnull, getter_AddRefs(prefBranch));
 
168
    if (NS_FAILED(rv))
 
169
        return rv;
 
170
        
 
171
    // This preference is set in the all.js or all-ns.js (depending whether 
 
172
    // running mozilla or netscp6)
 
173
 
 
174
    rv = prefBranch->GetCharPref("general.config.filename", 
 
175
                                  getter_Copies(lockFileName));
 
176
 
 
177
 
 
178
    PR_LOG(MCD, PR_LOG_DEBUG, ("general.config.filename = %s\n", lockFileName.get()));
 
179
    if (NS_FAILED(rv))
 
180
        return rv;
 
181
 
 
182
    // This needs to be read only once.
 
183
    //
 
184
    if (!mRead) {
 
185
        // Initiate the new JS Context for Preference management
 
186
        
 
187
        rv = CentralizedAdminPrefManagerInit();
 
188
        if (NS_FAILED(rv))
 
189
            return rv;
 
190
        
 
191
        // Open and evaluate function calls to set/lock/unlock prefs
 
192
        rv = openAndEvaluateJSFile("prefcalls.js", 0, PR_FALSE, PR_FALSE);
 
193
        if (NS_FAILED(rv)) 
 
194
            return rv;
 
195
 
 
196
        // Evaluate platform specific directives
 
197
        rv = openAndEvaluateJSFile("platform.js", 0, PR_FALSE, PR_FALSE);
 
198
        if (NS_FAILED(rv)) 
 
199
            return rv;
 
200
 
 
201
        mRead = PR_TRUE;
 
202
    }
 
203
    // If the lockFileName is NULL return ok, because no lockFile will be used
 
204
  
 
205
  
 
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).
 
210
 
 
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);
 
215
    if (NS_FAILED(rv))
 
216
    {
 
217
      PR_LOG(MCD, PR_LOG_DEBUG, ("error evaluating .cfg file %s %x\n", lockFileName.get(), rv));
 
218
      return rv;
 
219
    }
 
220
    
 
221
    rv = prefBranch->GetCharPref("general.config.filename", 
 
222
                                  getter_Copies(lockFileName));
 
223
    if (NS_FAILED(rv))
 
224
        // There is NO REASON we should ever get here. This is POST reading 
 
225
        // of the config file.
 
226
        return NS_ERROR_FAILURE;
 
227
 
 
228
  
 
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)) {
 
233
 
 
234
        fileNameLen = PL_strlen(lockFileName);
 
235
    
 
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.
 
239
    
 
240
        if (PL_strncmp(lockFileName, lockVendor, fileNameLen - 4) != 0)
 
241
            return NS_ERROR_FAILURE;
 
242
    }
 
243
  
 
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' ) {  
 
249
    
 
250
        // Instantiating nsAutoConfig object if the pref is present
 
251
        mAutoConfig = do_CreateInstance(NS_AUTOCONFIG_CONTRACTID, &rv);
 
252
        if (NS_FAILED(rv))
 
253
            return NS_ERROR_OUT_OF_MEMORY;
 
254
 
 
255
        rv = mAutoConfig->SetConfigURL(urlName);
 
256
        if (NS_FAILED(rv))
 
257
            return NS_ERROR_FAILURE;
 
258
 
 
259
    }
 
260
  
 
261
    return NS_OK;
 
262
} // ReadConfigFile
 
263
 
 
264
 
 
265
nsresult nsReadConfig::openAndEvaluateJSFile(const char *aFileName, PRBool isEncoded, 
 
266
                                             PRInt32 obscureValue,
 
267
                                             PRBool isBinDir)
 
268
{
 
269
    nsresult rv;
 
270
    nsCOMPtr<nsIFile> jsFile;
 
271
 
 
272
    if (isBinDir) {
 
273
        rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR, 
 
274
                                    getter_AddRefs(jsFile));
 
275
        if (NS_FAILED(rv)) 
 
276
            return rv;
 
277
        
 
278
#ifdef XP_MAC
 
279
        jsFile->AppendNative(NS_LITERAL_CSTRING("Essential Files"));
 
280
#endif
 
281
    } else {
 
282
        rv = NS_GetSpecialDirectory(NS_APP_DEFAULTS_50_DIR,
 
283
                                    getter_AddRefs(jsFile));
 
284
        if (NS_FAILED(rv)) 
 
285
            return rv;
 
286
        rv = jsFile->AppendNative(NS_LITERAL_CSTRING("autoconfig"));
 
287
        if (NS_FAILED(rv))
 
288
            return rv;
 
289
    }
 
290
    rv = jsFile->AppendNative(nsDependentCString(aFileName));
 
291
    if (NS_FAILED(rv)) 
 
292
        return rv;
 
293
 
 
294
    nsCOMPtr<nsIInputStream> inStr;
 
295
    rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), jsFile);
 
296
    if (NS_FAILED(rv)) 
 
297
        return rv;        
 
298
        
 
299
    PRInt64 fileSize;
 
300
    PRUint32 fs, amt = 0;
 
301
    jsFile->GetFileSize(&fileSize);
 
302
    LL_L2UI(fs, fileSize); // Converting 64 bit structure to unsigned int
 
303
 
 
304
    char *buf = (char *)PR_Malloc(fs * sizeof(char));
 
305
    if (!buf) 
 
306
        return NS_ERROR_OUT_OF_MEMORY;
 
307
      
 
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) {
 
312
 
 
313
            // Unobscure file by subtracting some value from every char. 
 
314
            for (PRUint32 i = 0; i < amt; i++)
 
315
                buf[i] -= obscureValue;
 
316
        }
 
317
        nsCAutoString path;
 
318
 
 
319
        jsFile->GetNativePath(path);
 
320
        nsCAutoString fileURL;
 
321
        fileURL = NS_LITERAL_CSTRING("file:///") + path;
 
322
        rv = EvaluateAdminConfigScript(buf, amt, fileURL.get(), 
 
323
                                       PR_FALSE, PR_TRUE, 
 
324
                                       isEncoded ? PR_TRUE:PR_FALSE);
 
325
    }
 
326
    inStr->Close();
 
327
    PR_Free(buf);
 
328
    
 
329
    return rv;
 
330
}