2004-07-10 15:04:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-09-26 03:31:24 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-19 02:14:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-03-29 06:11:48 +04:00
|
|
|
*
|
2004-04-19 02:14:17 +04:00
|
|
|
* 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/
|
2001-09-26 03:31:24 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2001-03-29 06:11:48 +04:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-19 02:14:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 03:31:24 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2003-07-09 04:20:57 +04:00
|
|
|
* Michiel van Leeuwen (mvl@exedo.nl)
|
2001-09-26 03:31:24 +04:00
|
|
|
*
|
|
|
|
* 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
|
2004-04-19 02:14:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 03:31:24 +04:00
|
|
|
* 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
|
2004-04-19 02:14:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-03-29 06:11:48 +04:00
|
|
|
*
|
2001-09-26 03:31:24 +04:00
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
#include "nsPermissionManager.h"
|
2003-03-22 04:24:51 +03:00
|
|
|
#include "nsPermission.h"
|
2001-03-29 06:11:48 +04:00
|
|
|
#include "nsCRT.h"
|
2002-08-15 22:38:46 +04:00
|
|
|
#include "nsNetUtil.h"
|
2003-03-22 04:24:51 +03:00
|
|
|
#include "nsILineInputStream.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2005-06-29 01:20:04 +04:00
|
|
|
#include "nsVoidArray.h"
|
2003-03-22 04:24:51 +03:00
|
|
|
#include "prprf.h"
|
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 3
|
|
|
|
#include "plarena.h"
|
|
|
|
|
|
|
|
static PLArenaPool *gHostArena = nsnull;
|
|
|
|
|
|
|
|
// making sHostArena 512b for nice allocation
|
|
|
|
// growing is quite cheap
|
|
|
|
#define HOST_ARENA_SIZE 512
|
|
|
|
|
|
|
|
// equivalent to strdup() - does no error checking,
|
|
|
|
// we're assuming we're only called with a valid pointer
|
|
|
|
static char *
|
|
|
|
ArenaStrDup(const char* str, PLArenaPool* aArena)
|
|
|
|
{
|
|
|
|
void* mem;
|
|
|
|
const PRUint32 size = strlen(str) + 1;
|
|
|
|
PL_ARENA_ALLOCATE(mem, aArena, size);
|
|
|
|
if (mem)
|
|
|
|
memcpy(mem, str, size);
|
2007-07-08 11:08:56 +04:00
|
|
|
return static_cast<char*>(mem);
|
2003-04-23 03:33:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHostEntry::nsHostEntry(const char* aHost)
|
|
|
|
{
|
|
|
|
mHost = ArenaStrDup(aHost, gHostArena);
|
2003-10-31 22:09:08 +03:00
|
|
|
mPermissions[0] = mPermissions[1] = 0;
|
2003-04-23 03:33:31 +04:00
|
|
|
}
|
2001-10-19 06:28:06 +04:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
nsHostEntry::nsHostEntry(const nsHostEntry& toCopy)
|
|
|
|
{
|
2003-10-31 22:09:08 +03:00
|
|
|
// nsTHashtable shouldn't allow us to end up here, since we
|
|
|
|
// set ALLOW_MEMMOVE to true.
|
|
|
|
NS_NOTREACHED("nsHostEntry copy constructor is forbidden!");
|
2003-04-23 03:33:31 +04:00
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2001-03-29 06:11:48 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsPermissionEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
2003-03-22 04:24:51 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2003-11-06 23:25:13 +03:00
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
nsPermissionEnumerator(const nsTHashtable<nsHostEntry> *aHostTable,
|
|
|
|
const char* *aHostList,
|
|
|
|
const PRUint32 aHostCount,
|
|
|
|
const char* *aTypeArray)
|
2003-04-23 03:33:31 +04:00
|
|
|
: mHostCount(aHostCount),
|
2003-03-22 04:24:51 +03:00
|
|
|
mHostIndex(0),
|
|
|
|
mTypeIndex(0),
|
2003-04-23 03:33:31 +04:00
|
|
|
mHostTable(aHostTable),
|
2003-07-09 04:20:57 +04:00
|
|
|
mHostList(aHostList),
|
|
|
|
mTypeArray(aTypeArray)
|
2003-03-22 04:24:51 +03:00
|
|
|
{
|
2003-04-23 03:33:31 +04:00
|
|
|
Prefetch();
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HasMoreElements(PRBool *aResult)
|
|
|
|
{
|
2003-04-23 03:33:31 +04:00
|
|
|
*aResult = (mNextPermission != nsnull);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetNext(nsISupports **aResult)
|
|
|
|
{
|
|
|
|
*aResult = mNextPermission;
|
|
|
|
if (!mNextPermission)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
|
|
|
|
Prefetch();
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
virtual ~nsPermissionEnumerator()
|
|
|
|
{
|
2003-04-23 03:33:31 +04:00
|
|
|
delete[] mHostList;
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
protected:
|
2003-04-23 03:33:31 +04:00
|
|
|
void Prefetch();
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
PRInt32 mHostCount;
|
|
|
|
PRInt32 mHostIndex;
|
|
|
|
PRInt32 mTypeIndex;
|
2003-04-23 03:33:31 +04:00
|
|
|
|
|
|
|
const nsTHashtable<nsHostEntry> *mHostTable;
|
|
|
|
const char* *mHostList;
|
|
|
|
nsCOMPtr<nsIPermission> mNextPermission;
|
2003-07-09 04:20:57 +04:00
|
|
|
const char* *mTypeArray;
|
2003-03-22 04:24:51 +03:00
|
|
|
};
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsPermissionEnumerator, nsISimpleEnumerator)
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
// Sets mNextPermission to a new nsIPermission on success,
|
|
|
|
// to nsnull when no new permissions are present.
|
|
|
|
void
|
|
|
|
nsPermissionEnumerator::Prefetch()
|
2003-03-22 04:24:51 +03:00
|
|
|
{
|
2003-04-23 03:33:31 +04:00
|
|
|
// init to null, so we know when we've prefetched something
|
|
|
|
mNextPermission = nsnull;
|
|
|
|
|
|
|
|
// check we have something more to get
|
|
|
|
PRUint32 permission;
|
|
|
|
while (mHostIndex < mHostCount && !mNextPermission) {
|
|
|
|
// loop over the types to find it
|
|
|
|
nsHostEntry *entry = mHostTable->GetEntry(mHostList[mHostIndex]);
|
|
|
|
if (entry) {
|
|
|
|
// see if we've found it
|
|
|
|
permission = entry->GetPermission(mTypeIndex);
|
2003-10-10 19:08:43 +04:00
|
|
|
if (permission != nsIPermissionManager::UNKNOWN_ACTION && mTypeArray[mTypeIndex]) {
|
2003-07-09 04:20:57 +04:00
|
|
|
mNextPermission = new nsPermission(entry->GetHost(),
|
|
|
|
nsDependentCString(mTypeArray[mTypeIndex]),
|
|
|
|
permission);
|
2003-04-23 03:33:31 +04:00
|
|
|
}
|
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
// increment mTypeIndex/mHostIndex as required
|
|
|
|
++mTypeIndex;
|
|
|
|
if (mTypeIndex == NUMBER_OF_TYPES) {
|
|
|
|
mTypeIndex = 0;
|
|
|
|
++mHostIndex;
|
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
}
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsPermissionManager Implementation
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
static const char kPermissionsFileName[] = "hostperm.1";
|
|
|
|
static const char kOldPermissionsFileName[] = "cookperm.txt";
|
2003-04-23 03:33:31 +04:00
|
|
|
static const char kPermissionChangeNotification[] = PERM_CHANGE_NOTIFICATION;
|
|
|
|
|
2004-03-12 21:02:38 +03:00
|
|
|
static const PRUint32 kLazyWriteTimeout = 2000; //msec
|
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
NS_IMPL_ISUPPORTS3(nsPermissionManager, nsIPermissionManager, nsIObserver, nsISupportsWeakReference)
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
nsPermissionManager::nsPermissionManager()
|
2004-07-15 19:16:42 +04:00
|
|
|
: mHostCount(0),
|
|
|
|
mChangedList(PR_FALSE)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
nsPermissionManager::~nsPermissionManager()
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
2004-03-12 21:02:38 +03:00
|
|
|
if (mWriteTimer)
|
|
|
|
mWriteTimer->Cancel();
|
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
RemoveTypeStrings();
|
2003-03-22 04:24:51 +03:00
|
|
|
RemoveAllFromMemory();
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPermissionManager::Init()
|
|
|
|
{
|
2003-03-22 04:24:51 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
if (!mHostTable.Init()) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2006-04-11 19:09:51 +04:00
|
|
|
// Clear the array of type strings
|
|
|
|
memset(mTypeArray, nsnull, sizeof(mTypeArray));
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
// Cache the permissions file
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mPermissionsFile));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-04-11 19:09:51 +04:00
|
|
|
mPermissionsFile->AppendNative(NS_LITERAL_CSTRING(kPermissionsFileName));
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2006-04-11 19:09:51 +04:00
|
|
|
// Ignore an error. That is not a problem. No cookperm.txt usually.
|
|
|
|
Read();
|
|
|
|
}
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
mObserverService = do_GetService("@mozilla.org/observer-service;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mObserverService->AddObserver(this, "profile-before-change", PR_TRUE);
|
|
|
|
mObserverService->AddObserver(this, "profile-do-change", PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-07-09 04:20:57 +04:00
|
|
|
nsPermissionManager::Add(nsIURI *aURI,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 aPermission)
|
2003-03-22 04:24:51 +03:00
|
|
|
{
|
2005-01-17 12:13:27 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2003-07-11 04:04:02 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
2005-01-17 12:13:27 +03:00
|
|
|
|
2001-03-29 06:11:48 +04:00
|
|
|
nsresult rv;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-11-06 23:21:05 +03:00
|
|
|
nsCAutoString host;
|
|
|
|
rv = GetHost(aURI, host);
|
2003-10-10 19:08:43 +04:00
|
|
|
// no host doesn't mean an error. just return the default
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
2002-08-15 22:38:46 +04:00
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
PRInt32 typeIndex = GetTypeIndex(aType, PR_TRUE);
|
|
|
|
if (typeIndex == -1 || aPermission >= NUMBER_OF_PERMISSIONS)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2003-11-06 23:21:05 +03:00
|
|
|
rv = AddInternal(host, typeIndex, aPermission, PR_TRUE);
|
2003-03-22 04:24:51 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mChangedList = PR_TRUE;
|
2004-03-12 21:02:38 +03:00
|
|
|
LazyWrite();
|
2005-01-17 12:13:27 +03:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
return NS_OK;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
// This only adds a permission to memory (it doesn't save to disk), and doesn't
|
|
|
|
// bounds check aTypeIndex or aPermission. These are up to the caller.
|
2003-03-22 04:24:51 +03:00
|
|
|
nsresult
|
2003-04-23 03:33:31 +04:00
|
|
|
nsPermissionManager::AddInternal(const nsAFlatCString &aHost,
|
2003-10-22 10:53:19 +04:00
|
|
|
PRInt32 aTypeIndex,
|
|
|
|
PRUint32 aPermission,
|
|
|
|
PRBool aNotify)
|
2003-03-22 04:24:51 +03:00
|
|
|
{
|
2003-04-23 03:33:31 +04:00
|
|
|
if (!gHostArena) {
|
|
|
|
gHostArena = new PLArenaPool;
|
|
|
|
if (!gHostArena)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
PL_INIT_ARENA_POOL(gHostArena, "PermissionHostArena", HOST_ARENA_SIZE);
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
|
2003-10-22 10:53:19 +04:00
|
|
|
// When an entry already exists, PutEntry will return that, instead
|
2003-04-23 03:33:31 +04:00
|
|
|
// of adding a new one
|
|
|
|
nsHostEntry *entry = mHostTable.PutEntry(aHost.get());
|
|
|
|
if (!entry) return NS_ERROR_FAILURE;
|
2004-02-04 19:15:38 +03:00
|
|
|
if (!entry->GetKey()) {
|
|
|
|
mHostTable.RawRemoveEntry(entry);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
if (entry->PermissionsAreEmpty()) {
|
|
|
|
++mHostCount;
|
|
|
|
}
|
2003-10-22 10:53:19 +04:00
|
|
|
|
|
|
|
PRUint32 oldPermission = entry->GetPermission(aTypeIndex);
|
2003-07-09 04:20:57 +04:00
|
|
|
entry->SetPermission(aTypeIndex, aPermission);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2005-07-17 14:10:31 +04:00
|
|
|
// If no more types are present, remove the entry
|
|
|
|
// Can happen if this add() is resetting the permission to default.
|
|
|
|
if (entry->PermissionsAreEmpty()) {
|
|
|
|
mHostTable.RawRemoveEntry(entry);
|
|
|
|
--mHostCount;
|
|
|
|
}
|
|
|
|
|
2003-10-22 10:53:19 +04:00
|
|
|
// check whether we are deleting, adding, or changing a permission,
|
|
|
|
// so we can notify observers. this would be neater to do in Add(),
|
|
|
|
// but we need to do it here because we only know what type of notification
|
|
|
|
// to send (removal, addition, or change) after we've done the hash
|
|
|
|
// lookup.
|
|
|
|
if (aNotify) {
|
|
|
|
if (aPermission == nsIPermissionManager::UNKNOWN_ACTION) {
|
|
|
|
if (oldPermission != nsIPermissionManager::UNKNOWN_ACTION)
|
|
|
|
// deleting
|
|
|
|
NotifyObserversWithPermission(aHost,
|
|
|
|
mTypeArray[aTypeIndex],
|
|
|
|
oldPermission,
|
|
|
|
NS_LITERAL_STRING("deleted").get());
|
|
|
|
} else {
|
|
|
|
if (oldPermission == nsIPermissionManager::UNKNOWN_ACTION)
|
|
|
|
// adding
|
|
|
|
NotifyObserversWithPermission(aHost,
|
|
|
|
mTypeArray[aTypeIndex],
|
|
|
|
aPermission,
|
|
|
|
NS_LITERAL_STRING("added").get());
|
|
|
|
else
|
|
|
|
// changing
|
|
|
|
NotifyObserversWithPermission(aHost,
|
|
|
|
mTypeArray[aTypeIndex],
|
|
|
|
aPermission,
|
|
|
|
NS_LITERAL_STRING("changed").get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-29 06:11:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::Remove(const nsACString &aHost,
|
2003-07-09 04:20:57 +04:00
|
|
|
const char *aType)
|
2003-03-22 04:24:51 +03:00
|
|
|
{
|
2003-07-11 04:04:02 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
2003-07-09 04:20:57 +04:00
|
|
|
PRInt32 typeIndex = GetTypeIndex(aType, PR_FALSE);
|
|
|
|
// If type == -1, the type isn't known,
|
|
|
|
// so just return NS_OK
|
|
|
|
if (typeIndex == -1) return NS_OK;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2007-02-20 18:24:36 +03:00
|
|
|
nsHostEntry *entry = GetHostEntry(PromiseFlatCString(aHost), typeIndex, PR_FALSE);
|
2003-04-23 03:33:31 +04:00
|
|
|
if (entry) {
|
2003-10-22 10:53:19 +04:00
|
|
|
// cache the old permission before we delete it, to notify observers
|
|
|
|
PRUint32 oldPermission = entry->GetPermission(typeIndex);
|
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
entry->SetPermission(typeIndex, nsIPermissionManager::UNKNOWN_ACTION);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
// If no more types are present, remove the entry
|
|
|
|
if (entry->PermissionsAreEmpty()) {
|
2003-04-26 00:13:05 +04:00
|
|
|
mHostTable.RawRemoveEntry(entry);
|
2003-04-23 03:33:31 +04:00
|
|
|
--mHostCount;
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2003-04-23 03:33:31 +04:00
|
|
|
mChangedList = PR_TRUE;
|
2004-03-12 21:02:38 +03:00
|
|
|
LazyWrite();
|
2003-04-23 03:33:31 +04:00
|
|
|
|
|
|
|
// Notify Observers
|
2003-10-22 10:53:19 +04:00
|
|
|
if (oldPermission != nsIPermissionManager::UNKNOWN_ACTION)
|
|
|
|
NotifyObserversWithPermission(PromiseFlatCString(aHost),
|
|
|
|
aType,
|
|
|
|
oldPermission,
|
|
|
|
NS_LITERAL_STRING("deleted").get());
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2002-01-11 02:33:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::RemoveAll()
|
|
|
|
{
|
|
|
|
RemoveAllFromMemory();
|
2003-10-22 10:53:19 +04:00
|
|
|
NotifyObservers(nsnull, NS_LITERAL_STRING("cleared").get());
|
2004-03-12 21:02:38 +03:00
|
|
|
LazyWrite();
|
2001-03-29 06:11:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-02-20 18:24:36 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::TestExactPermission(nsIURI *aURI,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 *aPermission)
|
|
|
|
{
|
|
|
|
return CommonTestPermission(aURI, aType, aPermission, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
NS_IMETHODIMP
|
2003-07-09 04:20:57 +04:00
|
|
|
nsPermissionManager::TestPermission(nsIURI *aURI,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 *aPermission)
|
2007-02-20 18:24:36 +03:00
|
|
|
{
|
|
|
|
return CommonTestPermission(aURI, aType, aPermission, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::CommonTestPermission(nsIURI *aURI,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 *aPermission,
|
|
|
|
PRBool aExactHostMatch)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
2005-01-17 12:13:27 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2003-07-11 04:04:02 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
// set the default
|
|
|
|
*aPermission = nsIPermissionManager::UNKNOWN_ACTION;
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2003-11-06 23:21:05 +03:00
|
|
|
nsCAutoString host;
|
|
|
|
nsresult rv = GetHost(aURI, host);
|
2003-10-10 19:08:43 +04:00
|
|
|
// no host doesn't mean an error. just return the default
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
PRInt32 typeIndex = GetTypeIndex(aType, PR_FALSE);
|
|
|
|
// If type == -1, the type isn't known,
|
|
|
|
// so just return NS_OK
|
|
|
|
if (typeIndex == -1) return NS_OK;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2007-02-20 18:24:36 +03:00
|
|
|
nsHostEntry *entry = GetHostEntry(host, typeIndex, aExactHostMatch);
|
2004-02-10 22:57:33 +03:00
|
|
|
if (entry)
|
|
|
|
*aPermission = entry->GetPermission(typeIndex);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get hostentry for given host string and permission type.
|
|
|
|
// walk up the domain if needed.
|
|
|
|
// return null if nothing found.
|
|
|
|
nsHostEntry *
|
|
|
|
nsPermissionManager::GetHostEntry(const nsAFlatCString &aHost,
|
2007-02-20 18:24:36 +03:00
|
|
|
PRUint32 aType,
|
|
|
|
PRBool aExactHostMatch)
|
2004-02-10 22:57:33 +03:00
|
|
|
{
|
2003-03-26 03:46:32 +03:00
|
|
|
PRUint32 offset = 0;
|
2004-02-10 22:57:33 +03:00
|
|
|
nsHostEntry *entry;
|
2003-03-26 03:46:32 +03:00
|
|
|
do {
|
2004-02-10 22:57:33 +03:00
|
|
|
entry = mHostTable.GetEntry(aHost.get() + offset);
|
2003-04-23 03:33:31 +04:00
|
|
|
if (entry) {
|
2004-02-10 22:57:33 +03:00
|
|
|
if (entry->GetPermission(aType) != nsIPermissionManager::UNKNOWN_ACTION)
|
2003-04-23 03:33:31 +04:00
|
|
|
break;
|
2004-02-10 22:57:33 +03:00
|
|
|
|
|
|
|
// reset entry, to be able to return null on failure
|
|
|
|
entry = nsnull;
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2007-02-20 18:24:36 +03:00
|
|
|
if (aExactHostMatch)
|
|
|
|
break; // do not try super domains
|
|
|
|
|
2004-02-10 22:57:33 +03:00
|
|
|
offset = aHost.FindChar('.', offset) + 1;
|
2003-03-26 03:46:32 +03:00
|
|
|
|
2003-04-04 17:20:37 +04:00
|
|
|
// walk up the domaintree (we stop as soon as we find a match,
|
|
|
|
// which will be the most specific domain we have an entry for).
|
|
|
|
} while (offset > 0);
|
2004-02-10 22:57:33 +03:00
|
|
|
return entry;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
// A little helper function to add the hostname to the list.
|
|
|
|
// The hostname comes from an arena, and so it won't go away
|
|
|
|
PR_STATIC_CALLBACK(PLDHashOperator)
|
|
|
|
AddHostToList(nsHostEntry *entry, void *arg)
|
|
|
|
{
|
|
|
|
// arg is a double-ptr to an string entry in the hostList array,
|
|
|
|
// so we dereference it twice to assign the |const char*| string
|
|
|
|
// and once so we can increment the |const char**| array location
|
|
|
|
// ready for the next assignment.
|
2007-07-08 11:08:56 +04:00
|
|
|
const char*** elementPtr = static_cast<const char***>(arg);
|
2003-04-23 03:33:31 +04:00
|
|
|
**elementPtr = entry->GetKey();
|
|
|
|
++(*elementPtr);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
NS_IMETHODIMP nsPermissionManager::GetEnumerator(nsISimpleEnumerator **aEnum)
|
|
|
|
{
|
2003-04-23 03:33:31 +04:00
|
|
|
*aEnum = nsnull;
|
|
|
|
// get the host list, to hand to the enumerator.
|
|
|
|
// the enumerator takes ownership of the list.
|
|
|
|
|
|
|
|
// create a new host list. enumerator takes ownership of it.
|
|
|
|
const char* *hostList = new const char*[mHostCount];
|
|
|
|
if (!hostList) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Make a copy of the pointer, so we can increase it without loosing
|
|
|
|
// the original pointer
|
|
|
|
const char** hostListCopy = hostList;
|
|
|
|
mHostTable.EnumerateEntries(AddHostToList, &hostListCopy);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2007-07-08 11:08:56 +04:00
|
|
|
nsPermissionEnumerator* permissionEnum = new nsPermissionEnumerator(&mHostTable, hostList, mHostCount, const_cast<const char**>(mTypeArray));
|
2003-03-22 04:24:51 +03:00
|
|
|
if (!permissionEnum) {
|
2003-10-30 05:09:13 +03:00
|
|
|
delete[] hostList;
|
2003-03-22 04:24:51 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
NS_ADDREF(permissionEnum);
|
|
|
|
*aEnum = permissionEnum;
|
2001-03-29 06:11:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
NS_IMETHODIMP nsPermissionManager::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
|
2001-03-29 06:11:48 +04:00
|
|
|
// The profile is about to change.
|
2004-03-12 21:02:38 +03:00
|
|
|
|
|
|
|
if (mWriteTimer) {
|
|
|
|
mWriteTimer->Cancel();
|
|
|
|
mWriteTimer = 0;
|
|
|
|
}
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
// Dump current permission. This will be done by calling
|
2003-04-23 03:33:31 +04:00
|
|
|
// RemoveAllFromMemory which clears the memory-resident
|
2001-03-29 06:11:48 +04:00
|
|
|
// permission table. The reason the permission file does not
|
|
|
|
// need to be updated is because the file was updated every time
|
|
|
|
// the memory-resident table changed (i.e., whenever a new permission
|
|
|
|
// was accepted). If this condition ever changes, the permission
|
|
|
|
// file would need to be updated here.
|
|
|
|
|
2004-03-12 21:02:38 +03:00
|
|
|
if (!nsCRT::strcmp(someData, NS_LITERAL_STRING("shutdown-cleanse").get())) {
|
2003-03-22 04:24:51 +03:00
|
|
|
if (mPermissionsFile) {
|
|
|
|
mPermissionsFile->Remove(PR_FALSE);
|
|
|
|
}
|
2004-03-12 21:02:38 +03:00
|
|
|
} else {
|
|
|
|
Write();
|
|
|
|
}
|
|
|
|
RemoveTypeStrings();
|
|
|
|
RemoveAllFromMemory();
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
2001-10-20 00:52:59 +04:00
|
|
|
else if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
|
2001-03-29 06:11:48 +04:00
|
|
|
// The profile has aleady changed.
|
|
|
|
// Now just read them from the new profile location.
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
// Re-get the permissions file
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(mPermissionsFile));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-04-11 19:09:51 +04:00
|
|
|
mPermissionsFile->AppendNative(NS_LITERAL_CSTRING(kPermissionsFileName));
|
|
|
|
Read();
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
//*****************************************************************************
|
|
|
|
//*** nsPermissionManager private methods
|
|
|
|
//*****************************************************************************
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::RemoveAllFromMemory()
|
|
|
|
{
|
2003-04-23 03:33:31 +04:00
|
|
|
mHostTable.Clear();
|
|
|
|
mHostCount = 0;
|
|
|
|
if (gHostArena) {
|
|
|
|
PL_FinishArenaPool(gHostArena);
|
|
|
|
delete gHostArena;
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2003-04-23 03:33:31 +04:00
|
|
|
gHostArena = nsnull;
|
|
|
|
mChangedList = PR_TRUE;
|
2003-03-22 04:24:51 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
void
|
|
|
|
nsPermissionManager::RemoveTypeStrings()
|
|
|
|
{
|
|
|
|
for (PRUint32 i = NUMBER_OF_TYPES; i--; ) {
|
|
|
|
if (mTypeArray[i]) {
|
|
|
|
PL_strfree(mTypeArray[i]);
|
|
|
|
mTypeArray[i] = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns -1 on failure
|
|
|
|
PRInt32
|
|
|
|
nsPermissionManager::GetTypeIndex(const char *aType,
|
|
|
|
PRBool aAdd)
|
|
|
|
{
|
|
|
|
PRInt32 firstEmpty = -1;
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < NUMBER_OF_TYPES; ++i) {
|
|
|
|
if (!mTypeArray[i]) {
|
|
|
|
if (firstEmpty == -1)
|
|
|
|
// Don't break, the type might be later in the array
|
|
|
|
firstEmpty = i;
|
|
|
|
} else if (!strcmp(aType, mTypeArray[i])) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aAdd || firstEmpty == -1)
|
|
|
|
// Not found, but that is ok - we were just looking.
|
|
|
|
// Or, no free spots left - error.
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// This type was not registered before.
|
|
|
|
// Can't use ToNewCString here, other strings in the array are allocated
|
|
|
|
// with PL_strdup too, and they all need to be freed the same way
|
|
|
|
mTypeArray[firstEmpty] = PL_strdup(aType);
|
|
|
|
if (!mTypeArray[firstEmpty])
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return firstEmpty;
|
|
|
|
}
|
|
|
|
|
2003-10-22 10:53:19 +04:00
|
|
|
// wrapper function for mangling (host,type,perm) triplet into an nsIPermission.
|
|
|
|
void
|
|
|
|
nsPermissionManager::NotifyObserversWithPermission(const nsACString &aHost,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 aPermission,
|
|
|
|
const PRUnichar *aData)
|
2003-03-22 04:24:51 +03:00
|
|
|
{
|
2003-10-22 10:53:19 +04:00
|
|
|
nsCOMPtr<nsIPermission> permission =
|
|
|
|
new nsPermission(aHost, nsDependentCString(aType), aPermission);
|
|
|
|
if (permission)
|
|
|
|
NotifyObservers(permission, aData);
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
|
2003-10-22 10:53:19 +04:00
|
|
|
// notify observers that the permission list changed. there are four possible
|
|
|
|
// values for aData:
|
|
|
|
// "deleted" means a permission was deleted. aPermission is the deleted permission.
|
|
|
|
// "added" means a permission was added. aPermission is the added permission.
|
|
|
|
// "changed" means a permission was altered. aPermission is the new permission.
|
|
|
|
// "cleared" means the entire permission list was cleared. aPermission is null.
|
|
|
|
void
|
|
|
|
nsPermissionManager::NotifyObservers(nsIPermission *aPermission,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
if (mObserverService)
|
|
|
|
mObserverService->NotifyObservers(aPermission,
|
|
|
|
kPermissionChangeNotification,
|
|
|
|
aData);
|
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
static const char kTab = '\t';
|
|
|
|
static const char kNew = '\n';
|
|
|
|
static const char kTrue = 'T';
|
|
|
|
static const char kFalse = 'F';
|
|
|
|
static const char kFirstLetter = 'a';
|
|
|
|
static const char kTypeSign = '%';
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
static const char kMatchTypeHost[] = "host";
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
nsresult
|
|
|
|
nsPermissionManager::Read()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
PRBool readingOldFile = PR_FALSE;
|
2003-03-22 04:24:51 +03:00
|
|
|
nsCOMPtr<nsIInputStream> fileInputStream;
|
2007-09-21 09:09:54 +04:00
|
|
|
|
|
|
|
PRBool fileExists = PR_FALSE;
|
|
|
|
rv = mPermissionsFile->Exists(&fileExists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (fileExists) {
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream),
|
|
|
|
mPermissionsFile);
|
|
|
|
} else {
|
2004-02-08 16:04:13 +03:00
|
|
|
// Try finding the old-style file
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> oldPermissionsFile;
|
2007-09-21 09:09:54 +04:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(oldPermissionsFile));
|
2004-02-08 16:04:13 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = oldPermissionsFile->AppendNative(NS_LITERAL_CSTRING(kOldPermissionsFileName));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-09-21 09:09:54 +04:00
|
|
|
rv = oldPermissionsFile->Exists(&fileExists);
|
|
|
|
if (NS_SUCCEEDED(rv) && fileExists) {
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream),
|
|
|
|
oldPermissionsFile);
|
|
|
|
readingOldFile = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
2004-02-08 16:04:13 +03:00
|
|
|
/* old format is:
|
|
|
|
* host \t number permission \t number permission ... \n
|
|
|
|
* if this format isn't respected we move onto the next line in the file.
|
|
|
|
* permission is T or F for accept or deny, otherwise a lowercase letter,
|
|
|
|
* with a=0, b=1 etc
|
|
|
|
*/
|
|
|
|
}
|
2006-04-11 19:09:51 +04:00
|
|
|
// An error path is expected when cookperm.txt is not found either, or when
|
|
|
|
// creating the stream failed for another reason.
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsILineInputStream> lineInputStream = do_QueryInterface(fileInputStream, &rv);
|
2004-02-08 16:04:13 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
/* format is:
|
2004-02-08 16:04:13 +03:00
|
|
|
* matchtype \t type \t permission \t host
|
|
|
|
* Only "host" is supported for matchtype
|
|
|
|
* type is a string that identifies the type of permission (e.g. "cookie")
|
|
|
|
* permission is an integer between 1 and 15
|
2003-03-22 04:24:51 +03:00
|
|
|
*/
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
mHasUnknownTypes = PR_FALSE;
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
nsCAutoString buffer;
|
|
|
|
PRBool isMore = PR_TRUE;
|
2004-04-22 19:32:54 +04:00
|
|
|
while (isMore && NS_SUCCEEDED(lineInputStream->ReadLine(buffer, &isMore))) {
|
2003-03-22 04:24:51 +03:00
|
|
|
if (buffer.IsEmpty() || buffer.First() == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
if (!readingOldFile) {
|
|
|
|
nsCStringArray lineArray;
|
2003-07-09 04:20:57 +04:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// Split the line at tabs
|
|
|
|
lineArray.ParseString(buffer.get(), "\t");
|
|
|
|
|
2004-05-23 02:15:22 +04:00
|
|
|
if (lineArray[0]->EqualsLiteral(kMatchTypeHost) &&
|
2004-02-08 16:04:13 +03:00
|
|
|
lineArray.Count() == 4) {
|
|
|
|
|
|
|
|
PRInt32 error;
|
|
|
|
PRUint32 permission = lineArray[2]->ToInteger(&error);
|
|
|
|
if (error)
|
|
|
|
continue;
|
2004-06-04 12:56:50 +04:00
|
|
|
PRInt32 type = GetTypeIndex(lineArray[1]->get(), PR_TRUE);
|
2004-02-08 16:04:13 +03:00
|
|
|
if (type < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rv = AddInternal(*lineArray[3], type, permission, PR_FALSE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
mHasUnknownTypes = PR_TRUE;
|
2003-07-09 04:20:57 +04:00
|
|
|
}
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
} else {
|
|
|
|
// Begin backwards compatibility code
|
|
|
|
nsASingleFragmentCString::char_iterator iter;
|
|
|
|
if (buffer.First() == kTypeSign) {
|
|
|
|
// A permission string line
|
|
|
|
|
|
|
|
PRInt32 stringIndex;
|
|
|
|
|
|
|
|
if ((stringIndex = buffer.FindChar(kTab) + 1) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 type;
|
2006-06-15 17:52:00 +04:00
|
|
|
if (PR_sscanf(buffer.get() + 1, "%u", &type) != 1 || type >= NUMBER_OF_TYPES) {
|
2004-02-08 16:04:13 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Older versions of mozilla can't parse the permission string lines.
|
|
|
|
// They will put them back like '%0 0F' instead of '%0 cookie'
|
|
|
|
// Ignore those lines, and revert to the defaults later.
|
|
|
|
// XXX This means that when the user has additional types besides the
|
|
|
|
// default, those will be lost after using an old version with the
|
|
|
|
// new profile, and then going back to a new version.
|
|
|
|
if (!strcmp(buffer.get() + stringIndex, "0F"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NS_ASSERTION(GetTypeIndex(buffer.get() + stringIndex, PR_FALSE) == -1, "Corrupt cookperm.txt file");
|
|
|
|
mTypeArray[type] = PL_strdup(buffer.get() + stringIndex);
|
|
|
|
|
2003-07-09 04:20:57 +04:00
|
|
|
continue;
|
|
|
|
}
|
2003-09-11 20:56:04 +04:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
PRInt32 hostIndex, permissionIndex;
|
|
|
|
PRUint32 nextPermissionIndex = 0;
|
|
|
|
hostIndex = 0;
|
2003-09-11 20:56:04 +04:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
if ((permissionIndex = buffer.FindChar('\t', hostIndex) + 1) == 0)
|
|
|
|
continue;
|
2003-07-09 04:20:57 +04:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// ignore leading periods in host name
|
|
|
|
while (hostIndex < permissionIndex && (buffer.CharAt(hostIndex) == '.'))
|
|
|
|
++hostIndex;
|
2003-07-09 04:20:57 +04:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// nullstomp the trailing tab, to get a flat string
|
|
|
|
buffer.BeginWriting(iter);
|
|
|
|
*(iter += permissionIndex - 1) = char(0);
|
|
|
|
nsDependentCString host(buffer.get() + hostIndex, iter);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
for (;;) {
|
|
|
|
if (nextPermissionIndex == buffer.Length()+1)
|
|
|
|
break;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
if ((nextPermissionIndex = buffer.FindChar(kTab, permissionIndex) + 1) == 0)
|
|
|
|
nextPermissionIndex = buffer.Length()+1;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
const nsASingleFragmentCString &permissionString = Substring(buffer, permissionIndex, nextPermissionIndex - permissionIndex - 1);
|
|
|
|
permissionIndex = nextPermissionIndex;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
PRInt32 type = 0;
|
|
|
|
PRUint32 index = 0;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
if (permissionString.IsEmpty())
|
|
|
|
continue; // empty permission entry -- should never happen
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// Parse "2T"
|
|
|
|
char c = permissionString.CharAt(index);
|
|
|
|
while (index < permissionString.Length() && c >= '0' && c <= '9') {
|
|
|
|
type = 10*type + (c-'0');
|
|
|
|
c = permissionString.CharAt(++index);
|
|
|
|
}
|
2006-04-22 17:32:25 +04:00
|
|
|
|
|
|
|
if (type >= NUMBER_OF_TYPES)
|
|
|
|
continue; // invalid type for this permission entry
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
if (index >= permissionString.Length())
|
|
|
|
continue; // bad format for this permission entry
|
|
|
|
|
|
|
|
PRUint32 permission;
|
|
|
|
if (permissionString.CharAt(index) == kTrue)
|
|
|
|
permission = nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
else if (permissionString.CharAt(index) == kFalse)
|
|
|
|
permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
else
|
|
|
|
permission = permissionString.CharAt(index) - kFirstLetter;
|
|
|
|
|
|
|
|
// |permission| is unsigned, so no need to check for < 0
|
|
|
|
if (permission >= NUMBER_OF_PERMISSIONS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore @@@ as host. Old style checkbox status
|
2004-05-23 02:15:22 +04:00
|
|
|
if (!permissionString.IsEmpty() && !host.EqualsLiteral("@@@@")) {
|
2004-02-08 16:04:13 +03:00
|
|
|
rv = AddInternal(host, type, permission, PR_FALSE);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2003-07-09 04:20:57 +04:00
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2004-02-08 16:04:13 +03:00
|
|
|
// Old files, without the part that defines the types, assume 0 is cookie
|
|
|
|
// etc. So add those types, but make sure not to overwrite types from new
|
|
|
|
// style files.
|
|
|
|
GetTypeIndex("cookie", PR_TRUE);
|
|
|
|
GetTypeIndex("image", PR_TRUE);
|
|
|
|
GetTypeIndex("popup", PR_TRUE);
|
|
|
|
|
|
|
|
// End backwards compatibility code
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
2004-02-08 16:04:13 +03:00
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-29 21:13:40 +04:00
|
|
|
mChangedList = PR_FALSE;
|
2003-07-09 04:20:57 +04:00
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
// A helper function that adds the pointer to the entry to the list.
|
|
|
|
// This is not threadsafe, and only safe if the consumer does not
|
|
|
|
// modify the list. It is only used in Write() where we are sure
|
|
|
|
// that nothing else changes the list while we are saving.
|
|
|
|
PR_STATIC_CALLBACK(PLDHashOperator)
|
|
|
|
AddEntryToList(nsHostEntry *entry, void *arg)
|
|
|
|
{
|
|
|
|
// arg is a double-ptr to an entry in the hostList array,
|
|
|
|
// so we dereference it twice to assign the |nsHostEntry*|
|
|
|
|
// and once so we can increment the |nsHostEntry**| array location
|
|
|
|
// ready for the next assignment.
|
2007-07-08 11:08:56 +04:00
|
|
|
nsHostEntry*** elementPtr = static_cast<nsHostEntry***>(arg);
|
2003-04-23 03:33:31 +04:00
|
|
|
**elementPtr = entry;
|
|
|
|
++(*elementPtr);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2004-03-12 21:02:38 +03:00
|
|
|
void
|
|
|
|
nsPermissionManager::LazyWrite()
|
|
|
|
{
|
|
|
|
if (mWriteTimer) {
|
|
|
|
mWriteTimer->SetDelay(kLazyWriteTimeout);
|
|
|
|
} else {
|
|
|
|
mWriteTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
if (mWriteTimer) {
|
|
|
|
mWriteTimer->InitWithFuncCallback(DoLazyWrite, this, kLazyWriteTimeout,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPermissionManager::DoLazyWrite(nsITimer *aTimer,
|
|
|
|
void *aClosure)
|
|
|
|
{
|
2007-07-08 11:08:56 +04:00
|
|
|
nsPermissionManager *service = reinterpret_cast<nsPermissionManager*>(aClosure);
|
2004-03-12 21:02:38 +03:00
|
|
|
service->Write();
|
|
|
|
service->mWriteTimer = 0;
|
|
|
|
}
|
2003-07-09 04:20:57 +04:00
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
nsresult
|
|
|
|
nsPermissionManager::Write()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mChangedList) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPermissionsFile) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// Start with reading the old file, and remember any data that
|
|
|
|
// wasn't parsed, to put it right back into the new file.
|
|
|
|
// But only do that if we know there is unknown stuff
|
|
|
|
nsCStringArray rememberList;
|
|
|
|
if (mHasUnknownTypes) {
|
|
|
|
nsCOMPtr<nsIInputStream> fileInputStream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), mPermissionsFile);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsILineInputStream> lineInputStream = do_QueryInterface(fileInputStream, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCAutoString buffer;
|
|
|
|
PRBool isMore = PR_TRUE;
|
2004-04-22 19:32:54 +04:00
|
|
|
while (isMore && NS_SUCCEEDED(lineInputStream->ReadLine(buffer, &isMore))) {
|
2004-02-08 16:04:13 +03:00
|
|
|
if (buffer.IsEmpty() || buffer.First() == '#' ||
|
|
|
|
StringBeginsWith(buffer, NS_LITERAL_CSTRING(kMatchTypeHost)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rememberList.AppendCString(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-22 04:24:51 +03:00
|
|
|
nsCOMPtr<nsIOutputStream> fileOutputStream;
|
2004-07-20 11:27:28 +04:00
|
|
|
rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(fileOutputStream),
|
|
|
|
mPermissionsFile,
|
|
|
|
-1,
|
|
|
|
0600);
|
2004-02-08 16:04:13 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
// get a buffered output stream 4096 bytes big, to optimize writes
|
|
|
|
nsCOMPtr<nsIOutputStream> bufferedOutputStream;
|
|
|
|
rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), fileOutputStream, 4096);
|
2004-02-08 16:04:13 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
static const char kHeader[] =
|
2004-02-08 16:04:13 +03:00
|
|
|
"# Permission File\n"
|
|
|
|
"# This is a generated file! Do not edit.\n\n";
|
2003-03-22 04:24:51 +03:00
|
|
|
|
|
|
|
bufferedOutputStream->Write(kHeader, sizeof(kHeader) - 1, &rv);
|
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
/* format is:
|
|
|
|
* matchtype \t type \t permission \t host \t
|
|
|
|
*/
|
2003-07-09 04:20:57 +04:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// Write out the list of strings we remembered from the old file
|
2003-07-09 05:29:51 +04:00
|
|
|
PRUint32 i;
|
2004-02-08 16:04:13 +03:00
|
|
|
if (mHasUnknownTypes) {
|
|
|
|
for (i = 0 ; i < rememberList.Count() ; ++i) {
|
|
|
|
bufferedOutputStream->Write(rememberList[i]->get(),
|
|
|
|
rememberList[i]->Length(), &rv);
|
2003-07-09 04:20:57 +04:00
|
|
|
bufferedOutputStream->Write(&kNew, 1, &rv);
|
|
|
|
}
|
|
|
|
}
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
// create a new host list
|
|
|
|
nsHostEntry* *hostList = new nsHostEntry*[mHostCount];
|
|
|
|
if (!hostList) return NS_ERROR_OUT_OF_MEMORY;
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// Make a copy of the pointer, so we can increase it without losing
|
2003-04-23 03:33:31 +04:00
|
|
|
// the original pointer
|
|
|
|
nsHostEntry** hostListCopy = hostList;
|
|
|
|
mHostTable.EnumerateEntries(AddEntryToList, &hostListCopy);
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-07-09 05:29:51 +04:00
|
|
|
for (i = 0; i < mHostCount; ++i) {
|
2007-07-08 11:08:56 +04:00
|
|
|
nsHostEntry *entry = static_cast<nsHostEntry*>(hostList[i]);
|
2003-04-23 03:33:31 +04:00
|
|
|
NS_ASSERTION(entry, "corrupt permission list");
|
2003-03-22 04:24:51 +03:00
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
for (PRInt32 type = 0; type < NUMBER_OF_TYPES; ++type) {
|
|
|
|
|
|
|
|
PRUint32 permission = entry->GetPermission(type);
|
2004-02-08 16:04:13 +03:00
|
|
|
if (permission && mTypeArray[type]) {
|
|
|
|
// Write matchtype
|
|
|
|
// only "host" is possible at the moment.
|
|
|
|
bufferedOutputStream->Write(kMatchTypeHost, sizeof(kMatchTypeHost) - 1, &rv);
|
|
|
|
|
|
|
|
// Write type
|
2003-07-09 04:20:57 +04:00
|
|
|
bufferedOutputStream->Write(&kTab, 1, &rv);
|
2004-02-08 16:04:13 +03:00
|
|
|
bufferedOutputStream->Write(mTypeArray[type], strlen(mTypeArray[type]), &rv);
|
2003-04-23 03:33:31 +04:00
|
|
|
|
2004-02-08 16:04:13 +03:00
|
|
|
// Write permission
|
|
|
|
bufferedOutputStream->Write(&kTab, 1, &rv);
|
|
|
|
char permissionString[5];
|
|
|
|
PRUint32 len = PR_snprintf(permissionString, sizeof(permissionString) - 1, "%u", permission);
|
|
|
|
bufferedOutputStream->Write(permissionString, len, &rv);
|
|
|
|
|
|
|
|
// Write host
|
|
|
|
bufferedOutputStream->Write(&kTab, 1, &rv);
|
|
|
|
bufferedOutputStream->Write(entry->GetHost().get(), entry->GetHost().Length(), &rv);
|
|
|
|
|
|
|
|
// Write newline
|
|
|
|
bufferedOutputStream->Write(&kNew, 1, &rv);
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-23 03:33:31 +04:00
|
|
|
delete[] hostList;
|
2004-07-17 00:08:08 +04:00
|
|
|
|
2004-07-10 15:04:33 +04:00
|
|
|
// All went ok. Maybe except for problems in Write(), but the stream detects
|
|
|
|
// that for us
|
2004-07-20 02:00:03 +04:00
|
|
|
nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(bufferedOutputStream);
|
2004-07-20 03:38:35 +04:00
|
|
|
NS_ASSERTION(safeStream, "expected a safe output stream!");
|
2004-07-20 02:00:03 +04:00
|
|
|
if (safeStream) {
|
2004-07-17 00:08:08 +04:00
|
|
|
rv = safeStream->Finish();
|
2004-07-20 02:00:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to save permissions file! possible dataloss");
|
|
|
|
return rv;
|
|
|
|
}
|
2004-07-17 00:08:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mChangedList = PR_FALSE;
|
2004-07-20 02:00:03 +04:00
|
|
|
return NS_OK;
|
2003-03-22 04:24:51 +03:00
|
|
|
}
|
|
|
|
|
2003-10-10 19:08:43 +04:00
|
|
|
nsresult
|
2003-11-06 23:21:05 +03:00
|
|
|
nsPermissionManager::GetHost(nsIURI *aURI, nsACString &aResult)
|
2003-10-10 19:08:43 +04:00
|
|
|
{
|
2007-06-21 20:32:46 +04:00
|
|
|
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
|
|
|
|
if (!innerURI) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
innerURI->GetHost(aResult);
|
2003-10-10 19:08:43 +04:00
|
|
|
|
|
|
|
// If there is no host, use the scheme, and prepend "scheme:",
|
|
|
|
// to make sure it isn't a host or something.
|
|
|
|
if (aResult.IsEmpty()) {
|
2007-06-21 20:32:46 +04:00
|
|
|
innerURI->GetScheme(aResult);
|
2003-10-10 19:08:43 +04:00
|
|
|
if (aResult.IsEmpty()) {
|
|
|
|
// still empty. Return error.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
aResult = NS_LITERAL_CSTRING("scheme:") + aResult;
|
|
|
|
}
|
2005-01-17 12:13:27 +03:00
|
|
|
|
2003-10-10 19:08:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|