Bug 83536.

Merge script principal implementations into one class.
Should reduce footprint, speed up calls to caps a little bit, and fixes several memory leaks.
Also fixes bugs 211174 and 211263
r=jst@netscape.com
sr=bzbarsky@mit.edu
moa=mstoltz@netscape.com (he looked at an earlier patch and said it looked fine, and will do a retroactive review when he returns from vacation as well)


git-svn-id: svn://10.0.0.236/trunk@145137 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
caillon%returnzero.com
2003-07-24 05:15:20 +00:00
parent e07c163859
commit cd46cbbaad
68 changed files with 1812 additions and 2975 deletions

View File

@@ -50,11 +50,8 @@ REQUIRES = xpcom \
$(NULL)
CPPSRCS = \
nsBasePrincipal.cpp \
nsPrincipal.cpp \
nsSystemPrincipal.cpp \
nsCertificatePrincipal.cpp \
nsCodebasePrincipal.cpp \
nsAggregatePrincipal.cpp \
nsJSPrincipals.cpp \
nsScriptSecurityManager.cpp \
nsSecurityManagerFactory.cpp \

View File

@@ -1,479 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mitch Stoltz <mstoltz@netscape.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*describes principals which combine one or more principals*/
#include "nsAggregatePrincipal.h"
#include "nsIURI.h"
static NS_DEFINE_IID(kIAggregatePrincipalIID, NS_IAGGREGATEPRINCIPAL_IID);
NS_IMPL_QUERY_INTERFACE5_CI(nsAggregatePrincipal, nsIAggregatePrincipal,
nsICertificatePrincipal, nsICodebasePrincipal,
nsIPrincipal, nsISerializable)
NS_IMPL_CI_INTERFACE_GETTER5(nsAggregatePrincipal, nsIAggregatePrincipal,
nsICertificatePrincipal, nsICodebasePrincipal,
nsIPrincipal, nsISerializable)
NSBASEPRINCIPALS_ADDREF(nsAggregatePrincipal);
NSBASEPRINCIPALS_RELEASE(nsAggregatePrincipal);
//////////////////////////////////////////////////
// Methods implementing nsICertificatePrincipal //
//////////////////////////////////////////////////
NS_IMETHODIMP
nsAggregatePrincipal::GetCertificateID(char** aCertificateID)
{
if (!mCertificate)
{
*aCertificateID = nsnull;
return NS_OK;
}
nsCOMPtr<nsICertificatePrincipal> certificate = do_QueryInterface(mCertificate);
return certificate->GetCertificateID(aCertificateID);
}
NS_IMETHODIMP
nsAggregatePrincipal::GetCommonName(char** aCommonName)
{
if (!mCertificate)
{
*aCommonName = nsnull;
return NS_OK;
}
nsCOMPtr<nsICertificatePrincipal> certificate = do_QueryInterface(mCertificate);
return certificate->GetCommonName(aCommonName);
}
NS_IMETHODIMP
nsAggregatePrincipal::SetCommonName(const char* aCommonName)
{
if (!mCertificate)
return NS_ERROR_FAILURE;
nsCOMPtr<nsICertificatePrincipal> certificate = do_QueryInterface(mCertificate);
return certificate->SetCommonName(aCommonName);
}
///////////////////////////////////////////////
// Methods implementing nsICodebasePrincipal //
///////////////////////////////////////////////
NS_IMETHODIMP
nsAggregatePrincipal::GetURI(nsIURI** aURI)
{
if (!mCodebase)
{
*aURI = nsnull;
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsICodebasePrincipal> codebase = do_QueryInterface(mCodebase);
return codebase->GetURI(aURI);
}
NS_IMETHODIMP
nsAggregatePrincipal::GetOrigin(char** aOrigin)
{
if (!mCodebase)
{
*aOrigin = nsnull;
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsICodebasePrincipal> codebase = do_QueryInterface(mCodebase);
return codebase->GetOrigin(aOrigin);
}
NS_IMETHODIMP
nsAggregatePrincipal::GetSpec(char** aSpec)
{
if (!mCodebase)
{
*aSpec = nsnull;
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsICodebasePrincipal> codebase = do_QueryInterface(mCodebase);
return codebase->GetSpec(aSpec);
}
////////////////////////////////////////////////
// Methods implementing nsIAggregatePrincipal //
////////////////////////////////////////////////
NS_IMETHODIMP
nsAggregatePrincipal::GetCertificate(nsIPrincipal** result)
{
*result = mCertificate;
NS_IF_ADDREF(*result);
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::GetCodebase(nsIPrincipal** result)
{
*result = mCodebase;
NS_IF_ADDREF(*result);
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::SetCertificate(nsIPrincipal* aCertificate)
{
nsresult rv;
//-- Make sure this really is a certificate principal
if (aCertificate)
{
nsCOMPtr<nsICertificatePrincipal> tempCertificate =
do_QueryInterface(aCertificate, &rv);
if (NS_FAILED(rv))
return NS_ERROR_FAILURE;
}
//-- If aCertificate is an aggregate, get its underlying certificate
nsCOMPtr<nsIAggregatePrincipal> agg =
do_QueryInterface(aCertificate, &rv);
if (NS_SUCCEEDED(rv))
{
nsCOMPtr<nsIPrincipal> underlying;
rv = agg->GetCertificate(getter_AddRefs(underlying));
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
mCertificate = underlying.get();
}
else
mCertificate = aCertificate;
// New certificate, so forget cached security policy
mCachedSecurityPolicy = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::SetCodebase(nsIPrincipal* aCodebase)
{
nsresult rv;
nsCOMPtr<nsIPrincipal> newCodebase(aCodebase);
//-- If newCodebase is an aggregate, get its underlying codebase
nsCOMPtr<nsIAggregatePrincipal> agg =
do_QueryInterface(newCodebase, &rv);
if (NS_SUCCEEDED(rv))
{
rv = agg->GetCodebase(getter_AddRefs(newCodebase));
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
}
else
{ //-- Make sure this really is a codebase principal
nsCOMPtr<nsICodebasePrincipal> tempCodebase =
do_QueryInterface(newCodebase, &rv);
if (NS_FAILED(rv))
return NS_ERROR_FAILURE;
}
mCodebase = newCodebase;
//-- If this is the first codebase set, remember it.
if (!mOriginalCodebase)
mOriginalCodebase = newCodebase;
else
{
mDomainChanged = PR_TRUE;
// Codebase has changed, forget cached security policy
mCachedSecurityPolicy = nsnull;
}
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::GetOriginalCodebase(nsIPrincipal** aOriginalCodebase)
{
NS_ENSURE_ARG_POINTER(aOriginalCodebase);
*aOriginalCodebase = mOriginalCodebase;
NS_IF_ADDREF(*aOriginalCodebase);
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::GetPrimaryChild(nsIPrincipal** aPrimaryChild)
{
//-- If a certificate is present, then that's the PrimaryChild principal.
// Otherwise we use the codebase.
if (mCertificate)
*aPrimaryChild = mCertificate.get();
else if (mCodebase)
*aPrimaryChild = mCodebase.get();
else
{
*aPrimaryChild = nsnull;
return NS_ERROR_FAILURE;
}
NS_IF_ADDREF(*aPrimaryChild);
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::Intersect(nsIPrincipal* other)
{
NS_ASSERTION(mCodebase, "Principal without codebase");
if (mCertificate)
{
PRBool sameCert = PR_FALSE;
if (NS_FAILED(mCertificate->Equals(other, &sameCert)))
return NS_ERROR_FAILURE;
if (!sameCert)
SetCertificate(nsnull);
}
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::SetDomainChanged(PRBool aDomainChanged)
{
mDomainChanged = aDomainChanged;
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::GetDomainChanged(PRBool* aDomainChanged)
{
*aDomainChanged = mDomainChanged;
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::GetCachedSecurityPolicy(void** aCachedSecurityPolicy)
{
*aCachedSecurityPolicy = mCachedSecurityPolicy;
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::SetCachedSecurityPolicy(void* aCachedSecurityPolicy)
{
mCachedSecurityPolicy = aCachedSecurityPolicy;
return NS_OK;
}
///////////////////////////////////////
// Methods implementing nsIPrincipal //
///////////////////////////////////////
NS_IMETHODIMP
nsAggregatePrincipal::ToString(char **result)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->ToString(result);
}
NS_IMETHODIMP
nsAggregatePrincipal::ToUserVisibleString(char **result)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->ToUserVisibleString(result);
}
NS_IMETHODIMP
nsAggregatePrincipal::Equals(nsIPrincipal * other, PRBool * result)
{
*result = PR_FALSE;
if (this == other) {
*result = PR_TRUE;
return NS_OK;
}
if (!other)
return NS_OK;
nsresult rv;
nsCOMPtr<nsIAggregatePrincipal> otherAgg =
do_QueryInterface(other, &rv);
if (NS_FAILED(rv))
return NS_OK;
//-- Two aggregates are equal if both codebase and certificate are equal
PRBool certEqual = PR_TRUE;
if (mCertificate)
{
rv = mCertificate->Equals(other, &certEqual);
if(NS_FAILED(rv)) return rv;
}
PRBool cbEqual = PR_TRUE;
if (mCodebase)
{
rv = mCodebase->Equals(other, &cbEqual);
if(NS_FAILED(rv)) return rv;
}
if (mCertificate || mCodebase) // At least one must be present
*result = certEqual && cbEqual;
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::HashValue(PRUint32 *result)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->HashValue(result);
}
NS_IMETHODIMP
nsAggregatePrincipal::CanEnableCapability(const char *capability,
PRInt16 *result)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->CanEnableCapability(capability, result);
}
NS_IMETHODIMP
nsAggregatePrincipal::SetCanEnableCapability(const char *capability,
PRInt16 canEnable)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->SetCanEnableCapability(capability, canEnable);
}
NS_IMETHODIMP
nsAggregatePrincipal::IsCapabilityEnabled(const char *capability, void *annotation,
PRBool *result)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->IsCapabilityEnabled(capability, annotation, result);
}
NS_IMETHODIMP
nsAggregatePrincipal::EnableCapability(const char *capability, void **annotation)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->EnableCapability(capability, annotation);
}
NS_IMETHODIMP
nsAggregatePrincipal::RevertCapability(const char *capability, void **annotation)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->RevertCapability(capability, annotation);
}
NS_IMETHODIMP
nsAggregatePrincipal::DisableCapability(const char *capability, void **annotation)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->DisableCapability(capability, annotation);
}
NS_IMETHODIMP
nsAggregatePrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
nsCOMPtr<nsIPrincipal> PrimaryChild;
if (NS_FAILED(GetPrimaryChild(getter_AddRefs(PrimaryChild))))
return NS_ERROR_FAILURE;
return PrimaryChild->GetPreferences(aPrefName, aID,
aGrantedList, aDeniedList);
}
//////////////////////////////////////////
// Methods implementing nsISerializable //
//////////////////////////////////////////
NS_IMETHODIMP
nsAggregatePrincipal::Read(nsIObjectInputStream* aStream)
{
nsresult rv;
rv = nsBasePrincipal::Read(aStream);
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalObject(aStream, PR_TRUE, getter_AddRefs(mCertificate));
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalObject(aStream, PR_TRUE, getter_AddRefs(mCodebase));
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
NS_IMETHODIMP
nsAggregatePrincipal::Write(nsIObjectOutputStream* aStream)
{
nsresult rv;
rv = nsBasePrincipal::Write(aStream);
if (NS_FAILED(rv)) return rv;
rv = NS_WriteOptionalObject(aStream, mCertificate, PR_TRUE);
if (NS_FAILED(rv)) return rv;
rv = NS_WriteOptionalCompoundObject(aStream, mCodebase, NS_GET_IID(nsIPrincipal), PR_TRUE);
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
/////////////////////////////////////////////
// Constructor, Destructor, initialization //
/////////////////////////////////////////////
nsAggregatePrincipal::nsAggregatePrincipal() : mCachedSecurityPolicy(nsnull),
mDomainChanged(PR_FALSE)
{
}
nsAggregatePrincipal::~nsAggregatePrincipal()
{
}

View File

@@ -1,421 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999-2000 Netscape Communications Corporation. All Rights
* Reserved.
*
* Contributor(s):
* Norris Boyd
* Mitch Stoltz
*/
#include "nscore.h"
#include "nsBasePrincipal.h"
#include "nsScriptSecurityManager.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "plstr.h"
#include "nsCRT.h"
//////////////////////////
nsBasePrincipal::nsBasePrincipal()
: mCapabilities(nsnull)
{
}
PR_STATIC_CALLBACK(PRBool)
deleteElement(void* aElement, void *aData)
{
nsHashtable *ht = (nsHashtable *) aElement;
delete ht;
return PR_TRUE;
}
nsBasePrincipal::~nsBasePrincipal(void)
{
mAnnotations.EnumerateForwards(deleteElement, nsnull);
delete mCapabilities;
}
NS_IMETHODIMP
nsBasePrincipal::GetJSPrincipals(JSPrincipals **jsprin)
{
if (mJSPrincipals.nsIPrincipalPtr == nsnull) {
mJSPrincipals.nsIPrincipalPtr = this;
// No need for a ADDREF since it is a self-reference
}
*jsprin = &mJSPrincipals;
JSPRINCIPALS_HOLD(cx, *jsprin);
return NS_OK;
}
const char
nsBasePrincipal::Invalid[] = "Invalid";
NS_IMETHODIMP
nsBasePrincipal::CanEnableCapability(const char *capability, PRInt16 *result)
{
if (!mCapabilities) {
*result = nsIPrincipal::ENABLE_UNKNOWN;
return NS_OK;
}
else // If this principal is marked invalid, can't enable any capabilities
{
nsCStringKey invalidKey(Invalid);
if (mCapabilities->Exists(&invalidKey))
{
*result = nsIPrincipal::ENABLE_DENIED;
return NS_OK;
}
}
const char *start = capability;
*result = nsIPrincipal::ENABLE_GRANTED;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
PRInt16 value = (PRInt16)NS_PTR_TO_INT32(mCapabilities->Get(&key));
if (value == 0)
value = nsIPrincipal::ENABLE_UNKNOWN;
if (value < *result)
*result = value;
if (!space)
return NS_OK;
start = space + 1;
}
/* NOTREACHED */
return NS_OK;
}
NS_IMETHODIMP
nsBasePrincipal::SetCanEnableCapability(const char *capability,
PRInt16 canEnable)
{
if (!mCapabilities) {
mCapabilities = new nsHashtable(7);
if (!mCapabilities)
return NS_ERROR_OUT_OF_MEMORY;
}
else // If this principal is marked invalid, can't enable any capabilities
{
nsCStringKey invalidKey(Invalid);
if (mCapabilities->Exists(&invalidKey))
return NS_OK;
}
if (PL_strcmp(capability, Invalid) == 0)
mCapabilities->Reset();
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
mCapabilities->Put(&key, (void *) canEnable);
if (!space)
return NS_OK;
start = space + 1;
}
/* NOTREACHED */
return NS_OK;
}
NS_IMETHODIMP
nsBasePrincipal::IsCapabilityEnabled(const char *capability, void *annotation,
PRBool *result)
{
*result = PR_FALSE;
nsHashtable *ht = (nsHashtable *) annotation;
if (!ht) {
return NS_OK;
}
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
*result = (ht->Get(&key) == (void *) AnnotationEnabled);
if (!*result) {
// If any single capability is not enabled, then return false.
return NS_OK;
}
if (!space)
return NS_OK;
start = space + 1;
}
return NS_OK;
}
NS_IMETHODIMP
nsBasePrincipal::EnableCapability(const char *capability, void **annotation)
{
return SetCapability(capability, annotation, AnnotationEnabled);
}
NS_IMETHODIMP
nsBasePrincipal::DisableCapability(const char *capability, void **annotation)
{
return SetCapability(capability, annotation, AnnotationDisabled);
}
NS_IMETHODIMP
nsBasePrincipal::RevertCapability(const char *capability, void **annotation)
{
if (*annotation) {
nsHashtable *ht = (nsHashtable *) *annotation;
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
ht->Remove(&key);
if (!space)
return NS_OK;
start = space + 1;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsBasePrincipal::SetCapability(const char *capability, void **annotation,
AnnotationValue value)
{
if (*annotation == nsnull) {
*annotation = new nsHashtable(5);
if (!*annotation)
return NS_ERROR_OUT_OF_MEMORY;
// This object owns its annotations. Save them so we can release
// them when we destroy this object.
mAnnotations.AppendElement(*annotation);
}
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
nsHashtable *ht = (nsHashtable *) *annotation;
ht->Put(&key, (void *) value);
if (!space)
return NS_OK;
start = space + 1;
}
/* NOTREACHED */
return NS_OK;
}
int nsBasePrincipal::mCapabilitiesOrdinal = 0;
nsresult
nsBasePrincipal::InitFromPersistent(const char* aPrefName, const char* aID,
const char* aGrantedList, const char* aDeniedList)
{
//-- Empty the capability table
if (mCapabilities)
mCapabilities->Reset();
//-- Save the preference name
mPrefName = aPrefName;
const char* ordinalBegin = PL_strpbrk(aPrefName, "1234567890");
if (ordinalBegin) {
int n = atoi(ordinalBegin);
if (mCapabilitiesOrdinal <= n)
mCapabilitiesOrdinal = n+1;
}
//-- Store the capabilities
if (aGrantedList)
if(NS_FAILED(SetCanEnableCapability(aGrantedList, nsIPrincipal::ENABLE_GRANTED)))
return NS_ERROR_FAILURE;
if (aDeniedList)
if(NS_FAILED(SetCanEnableCapability(aDeniedList, nsIPrincipal::ENABLE_DENIED)))
return NS_ERROR_FAILURE;
return NS_OK;
}
struct CapabilityList
{
nsCString* granted;
nsCString* denied;
};
PR_STATIC_CALLBACK(PRBool)
AppendCapability(nsHashKey *aKey, void *aData, void *capListPtr)
{
CapabilityList* capList = (CapabilityList*)capListPtr;
PRInt16 value = (PRInt16)NS_PTR_TO_INT32(aData);
nsCStringKey* key = (nsCStringKey *)aKey;
if (value == nsIPrincipal::ENABLE_GRANTED)
{
capList->granted->Append(key->GetString(), key->GetStringLength());
capList->granted->Append(' ');
}
else if (value == nsIPrincipal::ENABLE_DENIED)
{
capList->denied->Append(key->GetString(), key->GetStringLength());
capList->denied->Append(' ');
}
return PR_TRUE;
}
NS_IMETHODIMP
nsBasePrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
//-- Preference name
*aPrefName = ToNewCString(mPrefName);
if (!aPrefName)
return NS_ERROR_OUT_OF_MEMORY;
//-- ID
if (NS_FAILED(ToString(aID)))
return NS_ERROR_FAILURE;
//-- Capabilities
*aGrantedList = nsnull;
*aDeniedList = nsnull;
if (mCapabilities) {
nsCAutoString grantedListStr;
nsCAutoString deniedListStr;
CapabilityList* capList = new CapabilityList();
capList->granted = &grantedListStr;
capList->denied = &deniedListStr;
mCapabilities->Enumerate(AppendCapability, (void*)capList);
if (!grantedListStr.IsEmpty())
{
grantedListStr.Truncate(grantedListStr.Length()-1);
*aGrantedList = ToNewCString(grantedListStr);
if (!*aGrantedList) return NS_ERROR_OUT_OF_MEMORY;
}
if (!deniedListStr.IsEmpty())
{
deniedListStr.Truncate(deniedListStr.Length()-1);
*aDeniedList = ToNewCString(deniedListStr);
if (!*aDeniedList) return NS_ERROR_OUT_OF_MEMORY;
}
}
return NS_OK;
}
PR_STATIC_CALLBACK(nsresult)
ReadAnnotationEntry(nsIObjectInputStream* aStream, nsHashKey** aKey,
void** aData)
{
nsresult rv;
nsCStringKey* key = new nsCStringKey(aStream, &rv);
if (NS_FAILED(rv)) return rv;
PRUint32 value;
rv = aStream->Read32(&value);
if (NS_FAILED(rv)) {
delete key;
return rv;
}
*aKey = key;
*aData = (void*) value;
return NS_OK;
}
PR_STATIC_CALLBACK(void)
FreeAnnotationEntry(nsIObjectInputStream* aStream, nsHashKey* aKey,
void* aData)
{
if (aKey)
delete NS_STATIC_CAST(nsCStringKey*, aKey);
}
nsresult
nsBasePrincipal::Read(nsIObjectInputStream* aStream)
{
nsresult rv;
PRUint32 annotationCount;
rv = aStream->Read32(&annotationCount);
if (NS_FAILED(rv)) return rv;
for (PRInt32 i = 0, n = PRInt32(annotationCount); i < n; i++) {
nsHashtable *ht = new nsHashtable(aStream,
ReadAnnotationEntry,
FreeAnnotationEntry,
&rv);
NS_ASSERTION(NS_SUCCEEDED(rv) || ht == nsnull,
"failure but non-null return from nsHashtable ctor!");
if (NS_FAILED(rv)) return rv;
if (!mAnnotations.InsertElementAt(NS_REINTERPRET_CAST(void*, ht), i)) {
delete ht;
return NS_ERROR_OUT_OF_MEMORY;
}
}
PRBool hasCapabilities;
rv = aStream->ReadBoolean(&hasCapabilities);
if (NS_SUCCEEDED(rv) && hasCapabilities) {
mCapabilities = new nsHashtable(aStream,
ReadAnnotationEntry,
FreeAnnotationEntry,
&rv);
}
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalCString(aStream, mPrefName);
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
PR_STATIC_CALLBACK(nsresult)
WriteScalarValue(nsIObjectOutputStream* aStream, void* aData)
{
PRUint32 value = NS_PTR_TO_INT32(aData);
return aStream->Write32(value);
}
nsresult
nsBasePrincipal::Write(nsIObjectOutputStream* aStream)
{
nsresult rv;
PRUint32 annotationCount = PRUint32(mAnnotations.Count());
rv = aStream->Write32(annotationCount);
if (NS_FAILED(rv)) return rv;
for (PRInt32 i = 0, n = PRInt32(annotationCount); i < n; i++) {
nsHashtable *ht = NS_REINTERPRET_CAST(nsHashtable *, mAnnotations[i]);
rv = ht->Write(aStream, WriteScalarValue);
if (NS_FAILED(rv)) return rv;
}
PRBool hasCapabilities = (mCapabilities != nsnull);
rv = aStream->WriteBoolean(hasCapabilities);
if (NS_SUCCEEDED(rv) && hasCapabilities)
rv = mCapabilities->Write(aStream, WriteScalarValue);
if (NS_FAILED(rv)) return rv;
rv = NS_WriteOptionalStringZ(aStream, mPrefName.get());
if (NS_FAILED(rv)) return rv;
return NS_OK;
}

View File

@@ -1,227 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mitch Stoltz <mstoltz@netscape.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*describes principals for use in signed scripts*/
#include "nsCertificatePrincipal.h"
#include "prmem.h"
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
static NS_DEFINE_IID(kICertificatePrincipalIID, NS_ICERTIFICATEPRINCIPAL_IID);
NS_IMPL_QUERY_INTERFACE3_CI(nsCertificatePrincipal,
nsICertificatePrincipal,
nsIPrincipal,
nsISerializable)
NS_IMPL_CI_INTERFACE_GETTER3(nsCertificatePrincipal,
nsICertificatePrincipal,
nsIPrincipal,
nsISerializable)
NSBASEPRINCIPALS_ADDREF(nsCertificatePrincipal);
NSBASEPRINCIPALS_RELEASE(nsCertificatePrincipal);
//////////////////////////////////////////////////
// Methods implementing nsICertificatePrincipal //
//////////////////////////////////////////////////
NS_IMETHODIMP
nsCertificatePrincipal::GetCertificateID(char** aCertificateID)
{
*aCertificateID = ToNewCString(mCertificateID);
return *aCertificateID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsCertificatePrincipal::GetCommonName(char** aCommonName)
{
*aCommonName = ToNewCString(mCommonName);
return *aCommonName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsCertificatePrincipal::SetCommonName(const char* aCommonName)
{
mCommonName = aCommonName;
return NS_OK;
}
///////////////////////////////////////
// Methods implementing nsIPrincipal //
///////////////////////////////////////
NS_IMETHODIMP
nsCertificatePrincipal::CanEnableCapability(const char *capability,
PRInt16 *result)
{
if(NS_FAILED(nsBasePrincipal::CanEnableCapability(capability, result)))
return NS_ERROR_FAILURE;
if (*result == nsIPrincipal::ENABLE_UNKNOWN)
*result = ENABLE_WITH_USER_PERMISSION;
return NS_OK;
}
NS_IMETHODIMP
nsCertificatePrincipal::ToString(char **result)
{
return GetCertificateID(result);
}
NS_IMETHODIMP
nsCertificatePrincipal::ToUserVisibleString(char **result)
{
return GetCommonName(result);
}
NS_IMETHODIMP
nsCertificatePrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
if (mPrefName.IsEmpty()) {
mPrefName.Assign("capability.principal.certificate.p");
mPrefName.AppendInt(mCapabilitiesOrdinal++);
mPrefName.Append(".id");
}
return nsBasePrincipal::GetPreferences(aPrefName, aID,
aGrantedList, aDeniedList);
}
NS_IMETHODIMP
nsCertificatePrincipal::Equals(nsIPrincipal * other, PRBool * result)
{
*result = PR_FALSE;
if (this == other) {
*result = PR_TRUE;
return NS_OK;
}
if (!other)
return NS_OK;
nsresult rv;
nsCOMPtr<nsICertificatePrincipal> otherCertificate =
do_QueryInterface(other, &rv);
if (NS_FAILED(rv))
return NS_OK;
//-- Compare cert ID's
char* otherID;
rv = otherCertificate->GetCertificateID(&otherID);
if (NS_FAILED(rv))
{
PR_FREEIF(otherID);
return rv;
}
*result = mCertificateID.Equals(otherID);
PR_FREEIF(otherID);
return NS_OK;
}
NS_IMETHODIMP
nsCertificatePrincipal::HashValue(PRUint32 *result)
{
char* str;
if (NS_FAILED(ToString(&str)) || !str) return NS_ERROR_FAILURE;
*result = nsCRT::HashCode(str, nsnull);
nsCRT::free(str);
return NS_OK;
}
//////////////////////////////////////////
// Methods implementing nsISerializable //
//////////////////////////////////////////
NS_IMETHODIMP
nsCertificatePrincipal::Read(nsIObjectInputStream* aStream)
{
nsresult rv;
rv = nsBasePrincipal::Read(aStream);
if (NS_FAILED(rv)) return rv;
rv = aStream->ReadCString(mCertificateID);
if (NS_FAILED(rv)) return rv;
rv = NS_ReadOptionalCString(aStream, mCommonName);
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
NS_IMETHODIMP
nsCertificatePrincipal::Write(nsIObjectOutputStream* aStream)
{
nsresult rv;
rv = nsBasePrincipal::Write(aStream);
if (NS_FAILED(rv)) return rv;
rv = aStream->WriteStringZ(mCertificateID.get());
if (NS_FAILED(rv)) return rv;
rv = NS_WriteOptionalStringZ(aStream, mCommonName.get());
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
/////////////////////////////////////////////
// Constructor, Destructor, initialization //
/////////////////////////////////////////////
nsresult
nsCertificatePrincipal::InitFromPersistent(const char* aPrefName, const char* aCertID,
const char* aGrantedList, const char* aDeniedList)
{
if (NS_FAILED(Init(aCertID)))
return NS_ERROR_FAILURE;
return nsBasePrincipal::InitFromPersistent(aPrefName, aCertID,
aGrantedList, aDeniedList);
}
NS_IMETHODIMP
nsCertificatePrincipal::Init(const char* aCertificateID)
{
mCertificateID = aCertificateID;
return NS_OK;
}
nsCertificatePrincipal::nsCertificatePrincipal()
{
}
nsCertificatePrincipal::~nsCertificatePrincipal()
{
}

View File

@@ -1,311 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* Describes principals by their orginating uris */
#include "nsCodebasePrincipal.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsNetUtil.h"
#include "nsIURL.h"
#include "nsIJARURI.h"
#include "nsCOMPtr.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsXPIDLString.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
#include "nsScriptSecurityManager.h"
NS_IMPL_QUERY_INTERFACE3_CI(nsCodebasePrincipal,
nsICodebasePrincipal,
nsIPrincipal,
nsISerializable)
NS_IMPL_CI_INTERFACE_GETTER3(nsCodebasePrincipal,
nsICodebasePrincipal,
nsIPrincipal,
nsISerializable)
NSBASEPRINCIPALS_ADDREF(nsCodebasePrincipal);
NSBASEPRINCIPALS_RELEASE(nsCodebasePrincipal);
///////////////////////////////////////
// Methods implementing nsIPrincipal //
///////////////////////////////////////
NS_IMETHODIMP
nsCodebasePrincipal::ToString(char **result)
{
*result = nsnull;
PRBool isFile = PR_TRUE;
if(NS_FAILED(mURI->SchemeIs("file", &isFile)))
return NS_ERROR_FAILURE;
if (isFile)
{
nsCOMPtr<nsIURL> url(do_QueryInterface(mURI));
if (url)
{
nsCAutoString directory;
nsresult rv = url->GetDirectory(directory);
if (NS_FAILED(rv))
return rv;
nsCAutoString fileName;
rv = url->GetFileName(fileName);
if (NS_FAILED(rv))
return rv;
*result =
ToNewCString(NS_LITERAL_CSTRING("file://") + directory + fileName);
if (!*result)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
}
return GetOrigin(result);
}
NS_IMETHODIMP
nsCodebasePrincipal::ToUserVisibleString(char **result)
{
return ToString(result);
}
NS_IMETHODIMP
nsCodebasePrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
if (mPrefName.IsEmpty())
{
mPrefName.Assign("capability.principal.codebase.p");
mPrefName.AppendInt(mCapabilitiesOrdinal++);
mPrefName.Append(".id");
}
return nsBasePrincipal::GetPreferences(aPrefName, aID,
aGrantedList, aDeniedList);
}
NS_IMETHODIMP
nsCodebasePrincipal::HashValue(PRUint32 *result)
{
nsXPIDLCString spec;
if (NS_FAILED(GetSpec(getter_Copies(spec))))
return NS_ERROR_FAILURE;
*result = nsCRT::HashCode(spec, nsnull);
return NS_OK;
}
NS_IMETHODIMP
nsCodebasePrincipal::CanEnableCapability(const char *capability,
PRInt16 *result)
{
// Either this principal must be preconfigured as a trusted source
// (mTrusted), or else the codebase principal pref must be enabled
if (!mTrusted)
{
static char pref[] = "signed.applets.codebase_principal_support";
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (!prefBranch)
return NS_ERROR_FAILURE;
PRBool enabled;
if (NS_FAILED(prefBranch->GetBoolPref(pref, &enabled)) || !enabled)
{
// Deny unless subject is executing from file: or resource:
PRBool isFile = PR_FALSE;
PRBool isRes = PR_FALSE;
if (NS_FAILED(mURI->SchemeIs("file", &isFile)) ||
NS_FAILED(mURI->SchemeIs("resource", &isRes)) ||
(!isFile && !isRes))
{
*result = nsIPrincipal::ENABLE_DENIED;
return NS_OK;
}
}
}
nsBasePrincipal::CanEnableCapability(capability, result);
if (*result == nsIPrincipal::ENABLE_UNKNOWN)
*result = ENABLE_WITH_USER_PERMISSION;
return NS_OK;
}
///////////////////////////////////////////////
// Methods implementing nsICodebasePrincipal //
///////////////////////////////////////////////
NS_IMETHODIMP
nsCodebasePrincipal::GetURI(nsIURI **uri)
{
*uri = mURI;
NS_ADDREF(*uri);
return NS_OK;
}
NS_IMETHODIMP
nsCodebasePrincipal::GetOrigin(char **origin)
{
nsresult rv;
nsCAutoString hostPort;
if (NS_SUCCEEDED(mURI->GetHostPort(hostPort)))
{
nsCAutoString scheme;
rv = mURI->GetScheme(scheme);
NS_ENSURE_SUCCESS(rv, rv);
*origin = ToNewCString(scheme + NS_LITERAL_CSTRING("://") + hostPort);
}
else
{
// Some URIs (e.g., nsSimpleURI) don't support host. Just
// get the full spec.
nsCAutoString spec;
rv = mURI->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
*origin = ToNewCString(spec);
}
return *origin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsCodebasePrincipal::GetSpec(char **spec)
{
nsCAutoString buf;
nsresult rv = mURI->GetSpec(buf);
if (NS_FAILED(rv)) return rv;
*spec = ToNewCString(buf);
return *spec ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsCodebasePrincipal::Equals(nsIPrincipal *aOther, PRBool *result)
{
if (this == aOther)
{
*result = PR_TRUE;
return NS_OK;
}
*result = PR_FALSE;
if (!aOther)
return NS_OK;
// Get a URI from the other principal
nsCOMPtr<nsICodebasePrincipal> otherCodebase(
do_QueryInterface(aOther));
if (!otherCodebase)
{
// Other principal is not a codebase, so return false
return NS_OK;
}
nsCOMPtr<nsIURI> otherURI;
otherCodebase->GetURI(getter_AddRefs(otherURI));
NS_ENSURE_TRUE(otherURI, NS_ERROR_FAILURE);
return nsScriptSecurityManager::SecurityCompareURIs(mURI,
otherURI,
result);
}
//////////////////////////////////////////
// Methods implementing nsISerializable //
//////////////////////////////////////////
NS_IMETHODIMP
nsCodebasePrincipal::Read(nsIObjectInputStream* aStream)
{
nsresult rv;
rv = nsBasePrincipal::Read(aStream);
if (NS_FAILED(rv)) return rv;
return aStream->ReadObject(PR_TRUE, getter_AddRefs(mURI));
}
NS_IMETHODIMP
nsCodebasePrincipal::Write(nsIObjectOutputStream* aStream)
{
nsresult rv;
rv = nsBasePrincipal::Write(aStream);
if (NS_FAILED(rv)) return rv;
return aStream->WriteCompoundObject(mURI, NS_GET_IID(nsIURI), PR_TRUE);
}
/////////////////////////////////////////////
// Constructor, Destructor, initialization //
/////////////////////////////////////////////
nsCodebasePrincipal::nsCodebasePrincipal() : mTrusted(PR_FALSE)
{
}
nsresult
nsCodebasePrincipal::Init(nsIURI *uri)
{
nsCAutoString codebase;
if (uri == nsnull || NS_FAILED(uri->GetSpec(codebase)))
return NS_ERROR_FAILURE;
if (NS_FAILED(mJSPrincipals.Init(ToNewCString(codebase))))
return NS_ERROR_FAILURE;
// JSPrincipals::Init adopts its input
mURI = uri;
return NS_OK;
}
// This method overrides nsBasePrincipal::InitFromPersistent
nsresult
nsCodebasePrincipal::InitFromPersistent(const char* aPrefName, const char* aURLStr,
const char* aGrantedList, const char* aDeniedList,
PRBool aTrusted)
{
nsresult rv;
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(aURLStr), nsnull);
NS_ASSERTION(NS_SUCCEEDED(rv), "Malformed URI in security.principal preference.");
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(Init(uri))) return NS_ERROR_FAILURE;
// XXX: Add check for trusted = SSL only here?
mTrusted = aTrusted;
return nsBasePrincipal::InitFromPersistent(aPrefName, aURLStr,
aGrantedList, aDeniedList);
}
nsCodebasePrincipal::~nsCodebasePrincipal()
{
}

View File

@@ -35,7 +35,10 @@
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsCodebasePrincipal.h"
#include "nsString.h"
#include "nsIObjectOutputStream.h"
#include "nsIObjectInputStream.h"
#include "nsJSPrincipals.h"
#include "plstr.h"
#include "nsXPIDLString.h"
@@ -138,7 +141,7 @@ nsTranscodeJSPrincipals(JSXDRState *xdr, JSPrincipals **jsprinp)
nsMemory::Free(olddata);
::JS_XDRMemSetData(xdr, data, size);
prin->GetJSPrincipals(jsprinp);
prin->GetJsPrincipals(jsprinp);
}
}
}
@@ -185,6 +188,12 @@ nsJSPrincipals::nsJSPrincipals()
nsresult
nsJSPrincipals::Init(char *aCodebase)
{
if (codebase)
{
NS_ERROR("Init called twice!");
return NS_ERROR_UNEXPECTED;
}
codebase = aCodebase;
return NS_OK;
}

796
mozilla/caps/src/nsPrincipal.cpp Executable file
View File

@@ -0,0 +1,796 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2003
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Christopher A. Aillon <christopher@aillon.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nscore.h"
#include "nsScriptSecurityManager.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "plstr.h"
#include "nsCRT.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsJSPrincipals.h"
#include "nsVoidArray.h"
#include "nsHashtable.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#include "nsPrincipal.h"
// Static member variables
PRInt32 nsPrincipal::sCapabilitiesOrdinal = 0;
const char nsPrincipal::sInvalid[] = "Invalid";
nsPrincipal::nsPrincipal()
: mCapabilities(7)
{
}
NS_IMPL_QUERY_INTERFACE2_CI(nsPrincipal,
nsIPrincipal,
nsISerializable)
NS_IMPL_CI_INTERFACE_GETTER2(nsPrincipal,
nsIPrincipal,
nsISerializable)
NS_IMETHODIMP_(nsrefcnt)
nsPrincipal::AddRef()
{
NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt");
// XXXcaa does this need to be threadsafe? See bug 143559.
nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount);
NS_LOG_ADDREF(this, count, "nsPrincipal", sizeof(*this));
return count;
}
NS_IMETHODIMP_(nsrefcnt)
nsPrincipal::Release()
{
NS_PRECONDITION(0 != mJSPrincipals.refcount, "dup release");
nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mJSPrincipals.refcount);
NS_LOG_RELEASE(this, count, "nsPrincipal");
if (count == 0) {
NS_DELETEXPCOM(this);
}
return count;
}
nsPrincipal::nsPrincipal(nsIURI *aURI)
: mSecurityPolicy(nsnull),
mCodebase(aURI)
{
}
PR_STATIC_CALLBACK(PRBool)
deleteElement(void* aElement, void *aData)
{
nsHashtable *ht = (nsHashtable *) aElement;
delete ht;
return PR_TRUE;
}
nsPrincipal::~nsPrincipal(void)
{
mAnnotations.EnumerateForwards(deleteElement, nsnull);
delete mCert;
}
NS_IMETHODIMP
nsPrincipal::GetJsPrincipals(JSPrincipals **jsprin)
{
if (!mJSPrincipals.nsIPrincipalPtr) {
// Don't addref here, since we are referencing each other.
mJSPrincipals.nsIPrincipalPtr = this;
}
*jsprin = &mJSPrincipals;
// JSPRINCIPALS_HOLD does not use its first argument.
// Just use a dummy cx to save the codesize.
JSPRINCIPALS_HOLD(cx, *jsprin);
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetOrigin(char **aOrigin)
{
nsIURI* uri = mDomain ? mDomain : mCodebase;
NS_ASSERTION(uri, "No Domain or Codebase");
nsCAutoString hostPort;
nsresult rv = uri->GetHostPort(hostPort);
if (NS_SUCCEEDED(rv)) {
nsCAutoString scheme;
rv = uri->GetScheme(scheme);
NS_ENSURE_SUCCESS(rv, rv);
*aOrigin = ToNewCString(scheme + NS_LITERAL_CSTRING("://") + hostPort);
}
else {
// Some URIs (e.g., nsSimpleURI) don't support host. Just
// get the full spec.
nsCAutoString spec;
rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
*aOrigin = ToNewCString(spec);
}
return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
{
*aSecurityPolicy = mSecurityPolicy;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
{
mSecurityPolicy = aSecurityPolicy;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::Equals(nsIPrincipal *aOther, PRBool *aResult)
{
*aResult = PR_FALSE;
if (!aOther) {
NS_WARNING("Need a principal to compare this to!");
return NS_OK;
}
if (this != aOther) {
if (mCert) {
PRBool otherHasCert;
aOther->GetHasCertificate(&otherHasCert);
if (!otherHasCert) {
return NS_OK;
}
nsXPIDLCString otherCertID;
aOther->GetCertificateID(getter_Copies(otherCertID));
if (!otherCertID.Equals(mCert->certificateID)) {
return NS_OK;
}
}
// Codebases are equal if they have the same origin.
nsIURI *origin = mDomain ? mDomain : mCodebase;
nsCOMPtr<nsIURI> otherOrigin;
aOther->GetDomain(getter_AddRefs(otherOrigin));
if (!otherOrigin) {
aOther->GetURI(getter_AddRefs(otherOrigin));
}
return nsScriptSecurityManager::SecurityCompareURIs(origin,
otherOrigin,
aResult);
}
*aResult = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::CanEnableCapability(const char *capability, PRInt16 *result)
{
// If this principal is marked invalid, can't enable any capabilities
nsCStringKey invalidKey(sInvalid);
if (mCapabilities.Exists(&invalidKey)) {
*result = nsIPrincipal::ENABLE_DENIED;
return NS_OK;
}
const char *start = capability;
*result = nsIPrincipal::ENABLE_GRANTED;
for(;;) {
const char *space = PL_strchr(start, ' ');
PRInt32 len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
PRInt16 value = (PRInt16)NS_PTR_TO_INT32(mCapabilities.Get(&key));
if (value == 0) {
value = nsIPrincipal::ENABLE_UNKNOWN;
}
if (value < *result) {
*result = value;
}
if (!space) {
break;
}
start = space + 1;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::SetCanEnableCapability(const char *capability,
PRInt16 canEnable)
{
// If this principal is marked invalid, can't enable any capabilities
nsCStringKey invalidKey(sInvalid);
if (mCapabilities.Exists(&invalidKey)) {
return NS_OK;
}
if (PL_strcmp(capability, sInvalid) == 0) {
mCapabilities.Reset();
}
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
mCapabilities.Put(&key, NS_INT32_TO_PTR(canEnable));
if (!space) {
break;
}
start = space + 1;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::IsCapabilityEnabled(const char *capability, void *annotation,
PRBool *result)
{
*result = PR_FALSE;
nsHashtable *ht = (nsHashtable *) annotation;
if (!ht) {
return NS_OK;
}
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
*result = (ht->Get(&key) == (void *) AnnotationEnabled);
if (!*result) {
// If any single capability is not enabled, then return false.
return NS_OK;
}
if (!space) {
return NS_OK;
}
start = space + 1;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::EnableCapability(const char *capability, void **annotation)
{
return SetCapability(capability, annotation, AnnotationEnabled);
}
NS_IMETHODIMP
nsPrincipal::DisableCapability(const char *capability, void **annotation)
{
return SetCapability(capability, annotation, AnnotationDisabled);
}
NS_IMETHODIMP
nsPrincipal::RevertCapability(const char *capability, void **annotation)
{
if (*annotation) {
nsHashtable *ht = (nsHashtable *) *annotation;
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
ht->Remove(&key);
if (!space) {
return NS_OK;
}
start = space + 1;
}
}
return NS_OK;
}
nsresult
nsPrincipal::SetCapability(const char *capability, void **annotation,
AnnotationValue value)
{
if (*annotation == nsnull) {
*annotation = new nsHashtable(5);
if (!*annotation) {
return NS_ERROR_OUT_OF_MEMORY;
}
// This object owns its annotations. Save them so we can release
// them when we destroy this object.
mAnnotations.AppendElement(*annotation);
}
const char *start = capability;
for(;;) {
const char *space = PL_strchr(start, ' ');
int len = space ? space - start : strlen(start);
nsCAutoString capString(start, len);
nsCStringKey key(capString);
nsHashtable *ht = (nsHashtable *) *annotation;
ht->Put(&key, (void *) value);
if (!space) {
break;
}
start = space + 1;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetHasCertificate(PRBool* aResult)
{
*aResult = (mCert != nsnull);
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetURI(nsIURI** aURI)
{
NS_IF_ADDREF(*aURI = mCodebase);
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::SetURI(nsIURI* aURI)
{
mCodebase = aURI;
mDomain = nsnull;
// Codebase has changed, forget cached security policy
mSecurityPolicy = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::SetCertificateID(const char* aID)
{
if (!aID) {
mCert = nsnull;
return NS_OK;
}
if (!mCert) {
mCert = new Certificate(aID, "");
if (!mCert) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetCertificateID(char** aID)
{
NS_ENSURE_STATE(mCert);
*aID = ToNewCString(mCert->certificateID);
if (!*aID) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetCommonName(char** aName)
{
NS_ENSURE_STATE(mCert);
*aName = ToNewCString(mCert->commonName);
if (!*aName) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::SetCommonName(const char* aName)
{
if (!mCert) {
NS_ERROR("You must first initialize the certificate with an ID");
return NS_ERROR_FAILURE;
}
mCert->commonName = aName;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetHashValue(PRUint32* aValue)
{
NS_PRECONDITION(mCert || mCodebase, "Need a cert or codebase");
// If there is a certificate, it takes precendence over the codebase.
if (mCert) {
*aValue = nsCRT::HashCode(mCert->certificateID.get(), nsnull);
}
else {
nsCAutoString str;
mCodebase->GetSpec(str);
*aValue = nsCRT::HashCode(str.get(), nsnull);
}
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetDomain(nsIURI** aDomain)
{
NS_IF_ADDREF(*aDomain = mDomain);
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::SetDomain(nsIURI* aDomain)
{
mDomain = aDomain;
// Domain has changed, forget cached security policy
mSecurityPolicy = nsnull;
return NS_OK;
}
nsresult
nsPrincipal::InitFromPersistent(const char* aPrefName,
const char* aToken,
const char* aGrantedList,
const char* aDeniedList,
PRBool aIsCert,
PRBool aTrusted)
{
NS_PRECONDITION(mCapabilities.Count() == 0,
"mCapabilities was already initialized?");
NS_PRECONDITION(mAnnotations.Count() == 0,
"mAnnotations was already initialized?");
if (aIsCert) {
SetCertificateID(aToken);
}
else {
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aToken, nsnull);
if (NS_FAILED(rv)) {
NS_ERROR("Malformed URI in capability.principal preference.");
return rv;
}
nsCAutoString token;
rv = uri->GetSpec(token);
if (NS_FAILED(rv)) {
return rv;
}
rv = mJSPrincipals.Init(PL_strdup(token.get()));
if (NS_FAILED(rv)) {
return rv;
}
mTrusted = aTrusted;
}
//-- Save the preference name
mPrefName = aPrefName;
const char* ordinalBegin = PL_strpbrk(aPrefName, "1234567890");
if (ordinalBegin) {
PRIntn n = atoi(ordinalBegin);
if (sCapabilitiesOrdinal <= n) {
sCapabilitiesOrdinal = n + 1;
}
}
//-- Store the capabilities
nsresult rv = NS_OK;
if (aGrantedList) {
rv = SetCanEnableCapability(aGrantedList, nsIPrincipal::ENABLE_GRANTED);
}
if (NS_SUCCEEDED(rv) && aDeniedList) {
rv = SetCanEnableCapability(aDeniedList, nsIPrincipal::ENABLE_DENIED);
}
return rv;
}
struct CapabilityList
{
nsCString* granted;
nsCString* denied;
};
PR_STATIC_CALLBACK(PRBool)
AppendCapability(nsHashKey *aKey, void *aData, void *capListPtr)
{
CapabilityList* capList = (CapabilityList*)capListPtr;
PRInt16 value = (PRInt16)NS_PTR_TO_INT32(aData);
nsCStringKey* key = (nsCStringKey *)aKey;
if (value == nsIPrincipal::ENABLE_GRANTED) {
capList->granted->Append(key->GetString(), key->GetStringLength());
capList->granted->Append(' ');
}
else if (value == nsIPrincipal::ENABLE_DENIED) {
capList->denied->Append(key->GetString(), key->GetStringLength());
capList->denied->Append(' ');
}
return PR_TRUE;
}
NS_IMETHODIMP
nsPrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
if (mPrefName.IsEmpty()) {
if (mCert) {
mPrefName.Assign("capability.principal.certificate.p");
}
else {
mPrefName.Assign("capability.principal.codebase.p");
}
mPrefName.AppendInt(sCapabilitiesOrdinal++);
mPrefName.Append(".id");
}
*aPrefName = nsnull;
*aID = nsnull;
*aGrantedList = nsnull;
*aDeniedList = nsnull;
char *prefName = nsnull;
char *id = nsnull;
char *granted = nsnull;
char *denied = nsnull;
//-- Preference name
prefName = ToNewCString(mPrefName);
if (!prefName) {
return NS_ERROR_OUT_OF_MEMORY;
}
//-- ID
nsresult rv;
if (mCert) {
rv = GetCertificateID(&id);
}
else {
rv = GetOrigin(&id);
}
if (NS_FAILED(rv)) {
nsMemory::Free(prefName);
return rv;
}
//-- Capabilities
nsCAutoString grantedListStr, deniedListStr;
CapabilityList capList = CapabilityList();
capList.granted = &grantedListStr;
capList.denied = &deniedListStr;
mCapabilities.Enumerate(AppendCapability, (void*)&capList);
if (!grantedListStr.IsEmpty()) {
grantedListStr.Truncate(grantedListStr.Length() - 1);
granted = ToNewCString(grantedListStr);
if (!granted) {
nsMemory::Free(prefName);
nsMemory::Free(id);
return NS_ERROR_OUT_OF_MEMORY;
}
}
if (!deniedListStr.IsEmpty()) {
deniedListStr.Truncate(deniedListStr.Length() - 1);
denied = ToNewCString(deniedListStr);
if (!denied) {
nsMemory::Free(prefName);
nsMemory::Free(id);
if (granted) {
nsMemory::Free(granted);
}
return NS_ERROR_OUT_OF_MEMORY;
}
}
*aPrefName = prefName;
*aID = id;
*aGrantedList = granted;
*aDeniedList = denied;
return NS_OK;
}
PR_STATIC_CALLBACK(nsresult)
ReadAnnotationEntry(nsIObjectInputStream* aStream, nsHashKey** aKey,
void** aData)
{
nsresult rv;
nsCStringKey* key = new nsCStringKey(aStream, &rv);
if (NS_FAILED(rv)) {
return rv;
}
PRUint32 value;
rv = aStream->Read32(&value);
if (NS_FAILED(rv)) {
delete key;
return rv;
}
*aKey = key;
*aData = (void*) value;
return NS_OK;
}
PR_STATIC_CALLBACK(void)
FreeAnnotationEntry(nsIObjectInputStream* aStream, nsHashKey* aKey,
void* aData)
{
delete aKey;
}
NS_IMETHODIMP
nsPrincipal::Read(nsIObjectInputStream* aStream)
{
PRUint32 annotationCount;
nsresult rv = aStream->Read32(&annotationCount);
if (NS_FAILED(rv)) {
return rv;
}
for (PRInt32 i = 0, n = PRInt32(annotationCount); i < n; i++) {
nsHashtable *ht = new nsHashtable(aStream,
ReadAnnotationEntry,
FreeAnnotationEntry,
&rv);
if (!ht) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (NS_FAILED(rv)) {
delete ht;
return rv;
}
if (!mAnnotations.InsertElementAt(NS_REINTERPRET_CAST(void*, ht), i)) {
delete ht;
return NS_ERROR_OUT_OF_MEMORY;
}
}
PRBool hasCapabilities;
rv = aStream->ReadBoolean(&hasCapabilities);
if (NS_SUCCEEDED(rv) && hasCapabilities) {
mCapabilities = nsHashtable(aStream,
ReadAnnotationEntry,
FreeAnnotationEntry,
&rv);
}
if (NS_FAILED(rv)) {
return rv;
}
rv = NS_ReadOptionalCString(aStream, mPrefName);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
PR_STATIC_CALLBACK(nsresult)
WriteScalarValue(nsIObjectOutputStream* aStream, void* aData)
{
PRUint32 value = NS_PTR_TO_INT32(aData);
return aStream->Write32(value);
}
NS_IMETHODIMP
nsPrincipal::Write(nsIObjectOutputStream* aStream)
{
PRUint32 annotationCount = PRUint32(mAnnotations.Count());
nsresult rv = aStream->Write32(annotationCount);
if (NS_FAILED(rv)) {
return rv;
}
for (PRInt32 i = 0, n = PRInt32(annotationCount); i < n; i++) {
nsHashtable *ht = NS_REINTERPRET_CAST(nsHashtable *, mAnnotations[i]);
rv = ht->Write(aStream, WriteScalarValue);
if (NS_FAILED(rv)) {
return rv;
}
}
PRBool hasCapabilities = (mCapabilities.Count() > 0);
rv = aStream->WriteBoolean(hasCapabilities);
if (NS_SUCCEEDED(rv) && hasCapabilities) {
rv = mCapabilities.Write(aStream, WriteScalarValue);
}
if (NS_FAILED(rv)) {
return rv;
}
rv = NS_WriteOptionalStringZ(aStream, mPrefName.get());
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}

File diff suppressed because it is too large Load Diff

View File

@@ -43,9 +43,7 @@
#include "nsIScriptSecurityManager.h"
#include "nsScriptSecurityManager.h"
#include "nsIPrincipal.h"
#include "nsAggregatePrincipal.h"
#include "nsCertificatePrincipal.h"
#include "nsCodebasePrincipal.h"
#include "nsPrincipal.h"
#include "nsSystemPrincipal.h"
#include "nsIScriptNameSpaceManager.h"
#include "nsIScriptExternalNameSet.h"
@@ -234,8 +232,8 @@ netscape_security_invalidate(JSContext *cx, JSObject *obj, uintN argc,
// NS_ASSERTION(cx == GetCurrentContext(), "unexpected context");
rv = securityManager->SetCanEnableCapability(principalID,
nsBasePrincipal::Invalid,
rv = securityManager->SetCanEnableCapability(principalID,
nsPrincipal::sInvalid,
nsIPrincipal::ENABLE_GRANTED);
if (NS_FAILED(rv))
return JS_FALSE;
@@ -310,17 +308,13 @@ nsSecurityNameSet::InitializeNameSet(nsIScriptContext* aScriptContext)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsAggregatePrincipal)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCertificatePrincipal)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCodebasePrincipal)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsPrincipal)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSecurityNameSet)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsSystemPrincipal,
nsScriptSecurityManager::SystemPrincipalSingletonConstructor)
NS_DECL_CLASSINFO(nsAggregatePrincipal)
NS_DECL_CLASSINFO(nsCertificatePrincipal)
NS_DECL_CLASSINFO(nsCodebasePrincipal)
NS_DECL_CLASSINFO(nsPrincipal)
NS_DECL_CLASSINFO(nsSystemPrincipal)
@@ -388,42 +382,16 @@ static const nsModuleComponentInfo capsComponentInfo[] =
nsIClassInfo::MAIN_THREAD_ONLY
},
{ NS_AGGREGATEPRINCIPAL_CLASSNAME,
NS_AGGREGATEPRINCIPAL_CID,
NS_AGGREGATEPRINCIPAL_CONTRACTID,
nsAggregatePrincipalConstructor,
{ NS_PRINCIPAL_CLASSNAME,
NS_PRINCIPAL_CID,
NS_PRINCIPAL_CONTRACTID,
nsPrincipalConstructor,
nsnull,
nsnull,
nsnull,
NS_CI_INTERFACE_GETTER_NAME(nsAggregatePrincipal),
NS_CI_INTERFACE_GETTER_NAME(nsPrincipal),
nsnull,
&NS_CLASSINFO_NAME(nsAggregatePrincipal),
nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::EAGER_CLASSINFO
},
{ NS_CERTIFICATEPRINCIPAL_CLASSNAME,
NS_CERTIFICATEPRINCIPAL_CID,
NS_CERTIFICATEPRINCIPAL_CONTRACTID,
nsCertificatePrincipalConstructor,
nsnull,
nsnull,
nsnull,
NS_CI_INTERFACE_GETTER_NAME(nsCertificatePrincipal),
nsnull,
&NS_CLASSINFO_NAME(nsCertificatePrincipal),
nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::EAGER_CLASSINFO
},
{ NS_CODEBASEPRINCIPAL_CLASSNAME,
NS_CODEBASEPRINCIPAL_CID,
NS_CODEBASEPRINCIPAL_CONTRACTID,
nsCodebasePrincipalConstructor,
nsnull,
nsnull,
nsnull,
NS_CI_INTERFACE_GETTER_NAME(nsCodebasePrincipal),
nsnull,
&NS_CLASSINFO_NAME(nsCodebasePrincipal),
&NS_CLASSINFO_NAME(nsPrincipal),
nsIClassInfo::MAIN_THREAD_ONLY | nsIClassInfo::EAGER_CLASSINFO
},

View File

@@ -47,40 +47,53 @@
#include "nsXPIDLString.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
#include "nsString.h"
NS_IMPL_QUERY_INTERFACE2_CI(nsSystemPrincipal, nsIPrincipal, nsISerializable)
NS_IMPL_CI_INTERFACE_GETTER2(nsSystemPrincipal, nsIPrincipal, nsISerializable)
NS_IMPL_QUERY_INTERFACE2_CI(nsSystemPrincipal,
nsIPrincipal,
nsISerializable)
NS_IMPL_CI_INTERFACE_GETTER2(nsSystemPrincipal,
nsIPrincipal,
nsISerializable)
NSBASEPRINCIPALS_ADDREF(nsSystemPrincipal);
NSBASEPRINCIPALS_RELEASE(nsSystemPrincipal);
NS_IMETHODIMP_(nsrefcnt)
nsSystemPrincipal::AddRef()
{
NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt");
nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount);
NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this));
return count;
}
NS_IMETHODIMP_(nsrefcnt)
nsSystemPrincipal::Release()
{
NS_PRECONDITION(0 != mJSPrincipals.refcount, "dup release");
nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mJSPrincipals.refcount);
NS_LOG_RELEASE(this, count, "nsSystemPrincipal");
if (count == 0) {
NS_DELETEXPCOM(this);
}
return count;
}
///////////////////////////////////////
// Methods implementing nsIPrincipal //
///////////////////////////////////////
NS_IMETHODIMP
nsSystemPrincipal::ToString(char **result)
{
nsAutoString buf;
buf.Assign(NS_LITERAL_STRING("[System]"));
*result = ToNewCString(buf);
return *result ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsSystemPrincipal::ToUserVisibleString(char **result)
{
return ToString(result);
}
NS_IMETHODIMP
nsSystemPrincipal::GetPreferences(char** aPrefName, char** aID,
char** aGrantedList, char** aDeniedList)
{
// The system principal should never be streamed out
*aPrefName = nsnull;
*aID = nsnull;
*aGrantedList = nsnull;
*aDeniedList = nsnull;
return NS_ERROR_FAILURE;
}
@@ -92,7 +105,7 @@ nsSystemPrincipal::Equals(nsIPrincipal *other, PRBool *result)
}
NS_IMETHODIMP
nsSystemPrincipal::HashValue(PRUint32 *result)
nsSystemPrincipal::GetHashValue(PRUint32 *result)
{
*result = NS_PTR_TO_INT32(this);
return NS_OK;
@@ -127,12 +140,14 @@ nsSystemPrincipal::IsCapabilityEnabled(const char *capability,
NS_IMETHODIMP
nsSystemPrincipal::EnableCapability(const char *capability, void **annotation)
{
*annotation = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::RevertCapability(const char *capability, void **annotation)
{
*annotation = nsnull;
return NS_OK;
}
@@ -141,9 +156,104 @@ nsSystemPrincipal::DisableCapability(const char *capability, void **annotation)
{
// Can't disable the capabilities of the system principal.
// XXX might be handy to be able to do so!
*annotation = nsnull;
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSystemPrincipal::GetURI(nsIURI** aURI)
{
*aURI = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::SetURI(nsIURI* aURI)
{
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetOrigin(char** aOrigin)
{
*aOrigin = ToNewCString(NS_LITERAL_CSTRING("[System]"));
return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP
nsSystemPrincipal::SetCertificateID(const char* aID)
{
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetCertificateID(char** aID)
{
*aID = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetCommonName(char** aName)
{
*aName = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::SetCommonName(const char* aName)
{
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetHasCertificate(PRBool* aResult)
{
*aResult = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetDomain(nsIURI** aDomain)
{
*aDomain = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::SetDomain(nsIURI* aDomain)
{
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetSecurityPolicy(void** aSecurityPolicy)
{
*aSecurityPolicy = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::SetSecurityPolicy(void* aSecurityPolicy)
{
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetJsPrincipals(JSPrincipals **jsprin)
{
if (mJSPrincipals.nsIPrincipalPtr == nsnull) {
mJSPrincipals.nsIPrincipalPtr = this;
// No need for a ADDREF since it is a self-reference
}
*jsprin = &mJSPrincipals;
JSPRINCIPALS_HOLD(cx, *jsprin);
return NS_OK;
}
//////////////////////////////////////////
// Methods implementing nsISerializable //
//////////////////////////////////////////
@@ -170,7 +280,7 @@ nsSystemPrincipal::nsSystemPrincipal()
{
}
NS_IMETHODIMP
nsresult
nsSystemPrincipal::Init()
{
char *codebase = nsCRT::strdup("[System Principal]");