sl@0: // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: // DBMS client/server session class - "DBMS security" related - full security support
sl@0: // 
sl@0: //
sl@0: 
sl@0: #include "SD_STD.H"
sl@0: 
sl@0: /**
sl@0: Retrieves a list of names of secure shared databases, which share the same security policy,
sl@0: as determined by the supplied UID.
sl@0: If a database name is longer than KDbMaxName, it will not be added to the list. 
sl@0: 
sl@0: @param aDrive The drive number to be searched.
sl@0: @param aPolicyUid Database security policy UID.
sl@0: @return A list with names of the found databases, which have the same database security uid.
sl@0: 		The database name output format is: \<name\>.\<ext\>. The caller is resonsible for deleting 
sl@0: 		the database names list.
sl@0: @leave KErrNoMemory - not enough memory for the operation to be done
sl@0: @leave KErrArgument - invalid UID parameter (including KNullUid value)
sl@0: @leave KErrBadName - invalid drive number (not in A-Z range)
sl@0: @leave KErrNotReady - the drive is not presented in the system
sl@0: @leave Some other system-wide error codes
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C CDbDatabaseNames* RDbs::DatabaseNamesL(TDriveNumber aDrive, TUid aPolicyUid)
sl@0: 	{
sl@0: 	TIpcArgs args(aDrive, aPolicyUid.iUid);
sl@0: 	RReadStream in(HDbsBuf::NewLC(*this, EDbsDatabaseList, args));
sl@0: 	CDbDatabaseNames* dbNames = CDbDatabaseNames::NewLC();
sl@0: 	in >> *dbNames;
sl@0: 	CleanupStack::Pop(dbNames);
sl@0: 	CleanupStack::PopAndDestroy();//HDbsBuf
sl@0: 	return dbNames;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Copies an existing secure shared database to a new secure shared database. 
sl@0: The new database will have the same security policy as the old one.
sl@0: The maximum length of the target database name (with the extension) is KDbMaxName.
sl@0: 
sl@0: @param aSrcDbName Source database name (\<drive\>:\<name\>.\<ext\> format)
sl@0: @param aDestDbName Destination database name (\<drive\>:\<name\>.\<ext\> format)
sl@0: @param aPolicyUid The database security policy UID. The destination database will have 
sl@0:                   the same policy UID.
sl@0: @return KErrNone if successful, otherwise one of the system-wide error codes, including:
sl@0: 		KErrPermissionDenied - the caller has not enough rights to do the operation or
sl@0: 		                   the destination drive is a ROM drive;
sl@0: 		KErrArgument - invalid source or destination database names (null name, too long name, only drive letter);
sl@0: 		               invalid or null UID;
sl@0: 		KErrNotReady - the drive in database name is not presented in the system;
sl@0: 		KErrNotFound - the source database not found;
sl@0: 		KErrInUse - the source database is in use;
sl@0: 		KErrAlreadyExists - the destination database already exists;
sl@0:  		KErrNoMemory - not enough memory for the operation to be done;
sl@0: 
sl@0: @capability Note For a secure shared database, the caller must satisfy the schema 
sl@0:             access policy for the database.
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C TInt RDbs::CopyDatabase(const TDesC& aSrcDbName, const TDesC& aDestDbName, TUid aPolicyUid)
sl@0: 	{
sl@0: 	TIpcArgs args(&aSrcDbName, &aDestDbName, aPolicyUid.iUid);
sl@0: 	return SendReceive(DbsMessage(EDbsCopyDatabase, KDbsSessionHandle), args);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Deletes an existing secure shared database.
sl@0: 
sl@0: @param aDbName Source database name (\<drive\>:\<name\>.\<ext\> format)
sl@0: @param aPolicyUid Database security policy UID.
sl@0: @return KErrNone if successful, otherwise one of the system-wide error codes, including:
sl@0: 		KErrInUse (if the database is in use at the moment);
sl@0: 		KErrNotFound - the database not found;
sl@0: 		KErrPermissionDenied - the caller has not enough rights to do the operation;
sl@0: 
sl@0: @capability Note For a secure shared database, the caller must satisfy the schema 
sl@0:             access policy for the database.
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C TInt RDbs::DeleteDatabase(const TDesC& aDbName, TUid aPolicyUid)
sl@0: 	{
sl@0: 	TIpcArgs args(&aDbName, aPolicyUid.iUid);
sl@0: 	return SendReceive(DbsMessage(EDbsDeleteDatabase, KDbsSessionHandle), args);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns in aPolicy output parameter requested database/table security policy of type aPolicyType.
sl@0: @param aPolicyUid Database security policy UID
sl@0: @param aTableName Table name.
sl@0: @param aMask Bit-field: it includes ther policy type: EReadPolicy, EWritePolicy, ESchemaPolicy
sl@0:              and the request type - database or table.
sl@0: @param aPolicy It will be initialized with the requested security policy data after a successfull call.
sl@0: @return KErrNone if successful, otherwise some of the system-wide error codes, including:
sl@0: 				KErrArgument - some of the arguments has an invalid value.
sl@0: 				KErrNotSupported - the method has been called with aMask containing ESchemaPolicy
sl@0: 				                   for a table object.
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: TInt RDbs::GetPolicy(TUid aPolicyUid, const TDesC& aTableName, TUint aMask, 
sl@0: 					 TSecurityPolicy& aPolicy)
sl@0: 	{
sl@0: 	TBuf8<sizeof(TSecurityPolicy)> spData;
sl@0: 	TIpcArgs args(aPolicyUid.iUid, aMask, &aTableName, &spData);
sl@0: 	TInt err = SendReceive(DbsMessage(EDbsGetSecurityPolicy, KDbsSessionHandle), args);
sl@0: 	if(err == KErrNone)
sl@0: 		{
sl@0: 		err = aPolicy.Set(spData);
sl@0: 		}
sl@0: 	return err;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns in the aDbPolicy output parameter the requested database security policy of type aPolicyType.
sl@0: 
sl@0: @param aPolicyUid Database security policy UID.
sl@0: @param aPolicyType Policy type: EReadPolicy, EWritePolicy, ESchemaPolicy.
sl@0: @param aDbPolicy It will be initialized with the requested security policy data after a successfull call.
sl@0: @return KErrNone if successful, otherwise one of the system-wide error codes, including
sl@0: 				KErrArgument - some of the arguments has an invalid value.
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C TInt RDbs::GetDatabasePolicy(TUid aPolicyUid, TPolicyType aPolicyType, 
sl@0: 									  TSecurityPolicy& aDbPolicy)
sl@0: 	{
sl@0: 	return GetPolicy(aPolicyUid, KNullDesC, aPolicyType, aDbPolicy);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns in the aTablePolicy output parameter the requested table security policy of type aPolicyType.
sl@0: 
sl@0: @param aPolicyUid Database security policy UID.
sl@0: @param aTableName Table name.
sl@0: @param aPolicyType Policy type: EReadPolicy, EWritePolicy.
sl@0: @param aTablePolicy It will be initialized with the requested security policy data after a successfull call.
sl@0: @return KErrNone if successful, otherwise one of the system-wide error codes, including:
sl@0: 		KErrArgument - some of the arguments has an invalid value.
sl@0: 		KErrNotSupported - the method has been called with aPolicyType = ESchemaPolicy;
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C TInt RDbs::GetTablePolicy(TUid aPolicyUid, const TDesC& aTableName, TPolicyType aPolicyType, 
sl@0: 								   TSecurityPolicy& aTablePolicy)
sl@0: 	{
sl@0: 	return GetPolicy(aPolicyUid, aTableName, aPolicyType | KTablePolicyMaskBit, aTablePolicy);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns in the aDbPolicy and aTablePolicy output parameters the requested database and table 
sl@0: security policies of type aPolicyType.
sl@0: 
sl@0: @param aPolicyUid Database security policy UID.
sl@0: @param aTableName Table name.
sl@0: @param aPolicyType Policy type: EReadPolicy, EWritePolicy.
sl@0: @param aDbPolicy It will be initialized with the requested security policy data after a successfull call.
sl@0: @param aTablePolicy It will be initialized with the requested security policy data after a successfull call.
sl@0: @return KErrNone if successful, otherwise one of the system-wide error codes, including:
sl@0: 		KErrArgument - some of the arguments has an invalid value.
sl@0: 		KErrNotSupported - the method has been called with aPolicyType = ESchemaPolicy;
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C TInt RDbs::GetTablePolicies(TUid aPolicyUid, const TDesC& aTableName, TPolicyType aPolicyType, 
sl@0: 									 TSecurityPolicy& aDbPolicy, TSecurityPolicy& aTablePolicy)
sl@0: 	{
sl@0: 	TInt err = GetDatabasePolicy(aPolicyUid, aPolicyType, aDbPolicy);
sl@0: 	if(err == KErrNone)
sl@0: 		{
sl@0: 		err = GetTablePolicy(aPolicyUid, aTableName, aPolicyType, aTablePolicy);
sl@0: 		}
sl@0: 	return err;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: The method will fill out aBackupPath argument with the full path of aDbName secure
sl@0: shared database.
sl@0: @param aRequesterSID Security ID of the process which is supposed to backup or restore 
sl@0: 					 the database. 0 or ECapability_None are invalid values for 
sl@0: 					 aRequesterSID parameter.
sl@0: @param aDbName       Secure shared database name, which path will be set in aBackupPath
sl@0:                      parameter. The name's format is \<drive\>:\<name\>.\<ext\>
sl@0: @param aDbPolicyUid  Database security policy UID.
sl@0: @param aBackupPath   An output parameter. After a successfull call, the DBMS server
sl@0:  					 will fill out the full database path there. aBackupPath must offer
sl@0:  					 enough space to get the whole database path. Probably the best
sl@0:  					 aBackupPath length is KMaxPath value.
sl@0: @return KErrNone if successful, otherwise one of the system-wide error codes, including:
sl@0: 		- KErrArgument - 0 or ECapability_None process SID, null UID, 
sl@0: 						 null or invalid database name,
sl@0: 						 the database is not secure shared database;
sl@0: 		- KErrNotFound - the database file does not exist;
sl@0: 		- KErrPermissionDenied - the supplied process SID does not match the database backup&
sl@0: 						 restore SID or the database backup&restore SID is 0 or ECapability_None. 
sl@0: @deprecated
sl@0: */
sl@0: EXPORT_C TInt RDbs::GetBackupPath(TSecureId aRequesterSID, const TDesC& aDbName, 
sl@0: 								  TUid aDbPolicyUid, TDes& aBackupPath)
sl@0: 	{
sl@0: 	TIpcArgs args(aRequesterSID.iId, aDbPolicyUid.iUid, &aDbName, &aBackupPath);
sl@0: 	return SendReceive(DbsMessage(EDbsGetBackupPath, KDbsSessionHandle), args);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Retrieves a list of paths of secure shared databases, which share the same security policy,
sl@0: as determined by the supplied aDbPolicyUid parameter.
sl@0: Note: If there is a database file which full path length is bigger than KDbMaxStrLen characters, 
sl@0: 	  then this file will not be added to the returned CDbStrings array.
sl@0: 
sl@0: @param aRequesterSID Security ID of the process which is supposed to backup or restore 
sl@0: 					 the database. 0 and ECapability_None are invalid values for 
sl@0: 					 aRequesterSID parameter.
sl@0: @param aDbPolicyUid  Database security policy UID.
sl@0: @return A list with paths of the found databases, which have the same database security uid.
sl@0: 		The caller is resonsible for deleting the database paths list.
sl@0: @leave KErrArgument - 0 or ECapability_None process SID, null database security UID.
sl@0: @leave KErrPermissionDenied - the supplied process SID does not match the database backup&
sl@0: 						 restore SID or the database backup&restore SID is 0 or ECapability_None. 
sl@0: @leave Some other system-wide error codes
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C CDbStrings* RDbs::BackupPathsL(TSecureId aRequesterSid, TUid aDbPolicyUid)
sl@0: 	{
sl@0: 	TIpcArgs args(aRequesterSid.iId, aDbPolicyUid.iUid);
sl@0: 	RReadStream in(HDbsBuf::NewLC(*this, EDbsGetBackupPaths, args));
sl@0: 	CDbStrings* dbPaths = CDbStrings::NewLC();
sl@0: 	in >> *dbPaths;
sl@0: 	CleanupStack::Pop(dbPaths);
sl@0: 	CleanupStack::PopAndDestroy();//HDbsBuf
sl@0: 	return dbPaths;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Creates a secure shared database.
sl@0: Max allowed database name length (with the extension) is KDbMaxName symbols.
sl@0: 
sl@0: In this "client-server" mode the database can be shared with the other clients.
sl@0: 
sl@0: For creating a non-secure database, see RDbNamedDatabase::Create(), which first
sl@0: argument is a RFs reference (or RDbNamedDatabase::Replace()).
sl@0: 
sl@0: @param aDbs A reference to DBMS session instance.
sl@0: @param aDatabase Database name. The name format is: \<drive\>:\<name\>.\<ext\>
sl@0: @param aFormat Database format string. The string format is: "SECURE[UID]", where UID
sl@0: 			   is the database security policy UID. "SECURE" keyword is case insensitive.
sl@0: @return KErrNone if successful otherwise one of the system-wide error codes, including:
sl@0: 		KErrAlreadyExists - the database already exists;
sl@0: 		KErrNotSupported - invalid format string;
sl@0: 		KErrArgument - bad argument, including null/invaluid uids, the database name includes a path;
sl@0: 		KErrPermissionDenied - the caller has not enough rights to do the operation;
sl@0: 
sl@0: @capability Note For a secure shared database, the caller must satisfy the schema 
sl@0:             access policy for the database.
sl@0: 
sl@0: @see RDbNamedDatabase::Create(RFs& aFs, const TDesC& aSource, const TDesC& aFormat)
sl@0: @see RDbNamedDatabase::Replace(RFs& aFs, const TDesC& aSource, const TDesC& aFormat)
sl@0: 
sl@0: @publishedAll
sl@0: @released
sl@0: */
sl@0: EXPORT_C TInt RDbNamedDatabase::Create(RDbs& aDbs, const TDesC& aDatabase, const TDesC& aFormat)
sl@0: 	{
sl@0: 	TRAPD(r,iDatabase=CDbsSecureDatabase::NewL(aDbs,aDatabase,aFormat));
sl@0: 	return r;
sl@0: 	}