1 /* -*- Mode: C++; tab-width: 8; 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.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 /*Factory for internal browser security resource managers*/
40 #include "nsIModule.h"
41 #include "nsIGenericFactory.h"
42 #include "nsIScriptSecurityManager.h"
43 #include "nsScriptSecurityManager.h"
44 #include "nsIPrincipal.h"
45 #include "nsPrincipal.h"
46 #include "nsSystemPrincipal.h"
47 #include "nsNullPrincipal.h"
48 #include "nsIScriptNameSpaceManager.h"
49 #include "nsIScriptContext.h"
50 #include "nsICategoryManager.h"
51 #include "nsXPIDLString.h"
53 #include "nsIServiceManager.h"
56 #include "nsIClassInfoImpl.h"
58 ///////////////////////
59 // nsSecurityNameSet //
60 ///////////////////////
62 nsSecurityNameSet::nsSecurityNameSet()
66 nsSecurityNameSet::~nsSecurityNameSet()
70 NS_IMPL_ISUPPORTS1(nsSecurityNameSet
, nsIScriptExternalNameSet
)
73 getStringArgument(JSContext
*cx
, JSObject
*obj
, PRUint16 argNum
, uintN argc
, jsval
*argv
)
75 if (argc
<= argNum
|| !JSVAL_IS_STRING(argv
[argNum
])) {
76 JS_ReportError(cx
, "String argument expected");
81 * We don't want to use JS_ValueToString because we want to be able
82 * to have an object to represent a target in subsequent versions.
84 JSString
*str
= JSVAL_TO_STRING(argv
[argNum
]);
88 return JS_GetStringBytes(str
);
92 getUTF8StringArgument(JSContext
*cx
, JSObject
*obj
, PRUint16 argNum
,
93 uintN argc
, jsval
*argv
, nsCString
& aRetval
)
95 if (argc
<= argNum
|| !JSVAL_IS_STRING(argv
[argNum
])) {
96 JS_ReportError(cx
, "String argument expected");
102 * We don't want to use JS_ValueToString because we want to be able
103 * to have an object to represent a target in subsequent versions.
105 JSString
*str
= JSVAL_TO_STRING(argv
[argNum
]);
111 PRUnichar
*data
= (PRUnichar
*)JS_GetStringChars(str
);
112 CopyUTF16toUTF8(data
, aRetval
);
116 netscape_security_isPrivilegeEnabled(JSContext
*cx
, JSObject
*obj
, uintN argc
,
117 jsval
*argv
, jsval
*rval
)
119 JSBool result
= JS_FALSE
;
120 char *cap
= getStringArgument(cx
, obj
, 0, argc
, argv
);
123 nsCOMPtr
<nsIScriptSecurityManager
> securityManager
=
124 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
125 if (NS_SUCCEEDED(rv
)) {
126 // NS_ASSERTION(cx == GetCurrentContext(), "unexpected context");
128 rv
= securityManager
->IsCapabilityEnabled(cap
, &result
);
133 *rval
= BOOLEAN_TO_JSVAL(result
);
139 netscape_security_enablePrivilege(JSContext
*cx
, JSObject
*obj
, uintN argc
,
140 jsval
*argv
, jsval
*rval
)
142 char *cap
= getStringArgument(cx
, obj
, 0, argc
, argv
);
147 nsCOMPtr
<nsIScriptSecurityManager
> securityManager
=
148 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
152 // NS_ASSERTION(cx == GetCurrentContext(), "unexpected context");
154 rv
= securityManager
->EnableCapability(cap
);
161 netscape_security_disablePrivilege(JSContext
*cx
, JSObject
*obj
, uintN argc
,
162 jsval
*argv
, jsval
*rval
)
164 char *cap
= getStringArgument(cx
, obj
, 0, argc
, argv
);
169 nsCOMPtr
<nsIScriptSecurityManager
> securityManager
=
170 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
174 // NS_ASSERTION(cx == GetCurrentContext(), "unexpected context");
176 rv
= securityManager
->DisableCapability(cap
);
183 netscape_security_revertPrivilege(JSContext
*cx
, JSObject
*obj
, uintN argc
,
184 jsval
*argv
, jsval
*rval
)
186 char *cap
= getStringArgument(cx
, obj
, 0, argc
, argv
);
191 nsCOMPtr
<nsIScriptSecurityManager
> securityManager
=
192 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
196 // NS_ASSERTION(cx == GetCurrentContext(), "unexpected context");
198 rv
= securityManager
->RevertCapability(cap
);
205 netscape_security_setCanEnablePrivilege(JSContext
*cx
, JSObject
*obj
, uintN argc
,
206 jsval
*argv
, jsval
*rval
)
208 if (argc
< 2) return JS_FALSE
;
209 nsCAutoString principalFingerprint
;
210 getUTF8StringArgument(cx
, obj
, 0, argc
, argv
, principalFingerprint
);
211 char *cap
= getStringArgument(cx
, obj
, 1, argc
, argv
);
212 if (principalFingerprint
.IsEmpty() || !cap
)
216 nsCOMPtr
<nsIScriptSecurityManager
> securityManager
=
217 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
221 // NS_ASSERTION(cx == GetCurrentContext(), "unexpected context");
223 rv
= securityManager
->SetCanEnableCapability(principalFingerprint
, cap
,
224 nsIPrincipal::ENABLE_GRANTED
);
231 netscape_security_invalidate(JSContext
*cx
, JSObject
*obj
, uintN argc
,
232 jsval
*argv
, jsval
*rval
)
234 nsCAutoString principalFingerprint
;
235 getUTF8StringArgument(cx
, obj
, 0, argc
, argv
, principalFingerprint
);
236 if (principalFingerprint
.IsEmpty())
240 nsCOMPtr
<nsIScriptSecurityManager
> securityManager
=
241 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID
, &rv
);
245 // NS_ASSERTION(cx == GetCurrentContext(), "unexpected context");
247 rv
= securityManager
->SetCanEnableCapability(principalFingerprint
,
248 nsPrincipal::sInvalid
,
249 nsIPrincipal::ENABLE_GRANTED
);
255 static JSFunctionSpec PrivilegeManager_static_methods
[] = {
256 { "isPrivilegeEnabled", netscape_security_isPrivilegeEnabled
, 1,0,0},
257 { "enablePrivilege", netscape_security_enablePrivilege
, 1,0,0},
258 { "disablePrivilege", netscape_security_disablePrivilege
, 1,0,0},
259 { "revertPrivilege", netscape_security_revertPrivilege
, 1,0,0},
260 //-- System Cert Functions
261 { "setCanEnablePrivilege", netscape_security_setCanEnablePrivilege
,
263 { "invalidate", netscape_security_invalidate
, 1,0,0},
264 {nsnull
,nsnull
,0,0,0}
268 * "Steal" calls to netscape.security.PrivilegeManager.enablePrivilege,
269 * et. al. so that code that worked with 4.0 can still work.
272 nsSecurityNameSet::InitializeNameSet(nsIScriptContext
* aScriptContext
)
274 JSContext
*cx
= (JSContext
*) aScriptContext
->GetNativeContext();
275 JSObject
*global
= JS_GetGlobalObject(cx
);
278 * Find Object.prototype's class by walking up the global object's
281 JSObject
*obj
= global
;
283 JSAutoRequest
ar(cx
);
284 while ((proto
= JS_GetPrototype(cx
, obj
)) != nsnull
)
286 JSClass
*objectClass
= JS_GET_CLASS(cx
, obj
);
289 if (!JS_GetProperty(cx
, global
, "netscape", &v
))
290 return NS_ERROR_FAILURE
;
291 JSObject
*securityObj
;
292 if (JSVAL_IS_OBJECT(v
)) {
294 * "netscape" property of window object exists; get the
295 * "security" property.
297 obj
= JSVAL_TO_OBJECT(v
);
298 if (!JS_GetProperty(cx
, obj
, "security", &v
) || !JSVAL_IS_OBJECT(v
))
299 return NS_ERROR_FAILURE
;
300 securityObj
= JSVAL_TO_OBJECT(v
);
302 /* define netscape.security object */
303 obj
= JS_DefineObject(cx
, global
, "netscape", objectClass
, nsnull
, 0);
305 return NS_ERROR_FAILURE
;
306 securityObj
= JS_DefineObject(cx
, obj
, "security", objectClass
,
308 if (securityObj
== nsnull
)
309 return NS_ERROR_FAILURE
;
312 /* Define PrivilegeManager object with the necessary "static" methods. */
313 obj
= JS_DefineObject(cx
, securityObj
, "PrivilegeManager", objectClass
,
316 return NS_ERROR_FAILURE
;
318 return JS_DefineFunctions(cx
, obj
, PrivilegeManager_static_methods
)