persistentstorage/dbms/sdbms/Sd_DbProps2.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // RDbPropsFactory class - "DBMS Security" related - full security support
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <f32file.h>
       
    19 #include "D32DRVR.H"
       
    20 #include "U32STD.H"
       
    21 #include "D32Strings.h"
       
    22 #include "Sd_DbProps.h"
       
    23 #include "Sd_DbList.h"
       
    24 
       
    25 using namespace DBSC;
       
    26 
       
    27 /**
       
    28 Utility method, which can be used separately to remove the common part of a secure
       
    29 shared database name. The input string format is:
       
    30 "DBS_UID_<DbName>". 
       
    31 The output string format is:
       
    32 "<DbName>". 
       
    33 @param aDbName An output/input parameter. Th input format is: "DBS_UID_<DbName>".
       
    34                The output format is: "<DbName>". 
       
    35 @panic The method will panic in debug builds in case of a bad input string.
       
    36 */
       
    37 void RDbPropsFactory::StripCommonPart(TDes& aDbName)
       
    38 	{
       
    39 	TInt pos = aDbName.FindF(KDbsPrefix);
       
    40 	__ASSERT_DEBUG(pos != KErrNotFound, User::Invariant());
       
    41 	aDbName.Delete(pos, KDbsPrefix().Length());
       
    42 	//Remove the UID from the string
       
    43 	TInt pos_b = aDbName.Locate('_');
       
    44 	
       
    45 	TPtrC right = aDbName.Mid(pos_b+1);
       
    46 	TInt pos_e = right.Locate('_') + pos_b + 1;
       
    47 	
       
    48 	__ASSERT_DEBUG(pos_b != KErrNotFound && pos_e != KErrNotFound, User::Invariant());
       
    49 	aDbName.Delete(pos_b, pos_e - pos_b + 1);
       
    50 	}
       
    51 
       
    52 /**
       
    53 Utility method, which can be used separately to construct the common part of the secure
       
    54 shared database name. The result string format is:
       
    55 "DBS_UID_"
       
    56 @param aPolicyUid Security policy UID.
       
    57 @param aRes An output parameter, referencing the location, where the constructed string will be stored.
       
    58 */
       
    59 void RDbPropsFactory::ConstructCommonPart(TUid aPolicyUid, TDes& aRes)
       
    60 	{
       
    61 	aRes.Zero();
       
    62 	aRes.Append(KDbsPrefix);
       
    63 	aRes.Append('_');
       
    64 	aRes.AppendNumFixedWidthUC(aPolicyUid.iUid, EHex, 8);
       
    65 	aRes.Append('_');
       
    66 	}
       
    67 
       
    68 /**
       
    69 Standard factory method for TDbProps instances.
       
    70 The created TDbProps instance will be pushed in the cleanup stack.
       
    71 @return A pointer to the created TDbProps instance.
       
    72 @leave KErrNoMemory
       
    73 @internalComponent
       
    74 */
       
    75 static TDbProps* NewDbPropsLC()
       
    76 	{
       
    77 	TDbProps* dbProps = new (ELeave) TDbProps;
       
    78 	CleanupStack::PushL(dbProps);
       
    79 	return dbProps;
       
    80 	}
       
    81 
       
    82 /**
       
    83 Extracts the drive number from the supplied TParse instance.
       
    84 @param aFileNameParser A reference to TParse instance, which will be used to extract the 
       
    85 drive number.
       
    86 @return Extracted TDriveNumber value.
       
    87 @leave KErrArgument aFileNameParser parameter does not contain a drive number or it cannot
       
    88 					be constructed from the string.
       
    89 @internalComponent
       
    90 */
       
    91 static TDriveNumber ExtractDriveNumberL(TParse& aFileNameParser)
       
    92 	{
       
    93 	TPtrC drvPtr = aFileNameParser.Drive();
       
    94 	if(drvPtr.Length() == 0)
       
    95 		{
       
    96 		__LEAVE(KErrArgument);
       
    97 		}
       
    98 	TInt drvId = 0;
       
    99 	__LEAVE_IF_ERROR(RFs::CharToDrive(drvPtr[0], drvId));
       
   100 	return static_cast <TDriveNumber> (drvId);
       
   101 	}
       
   102 
       
   103 /**
       
   104 Creates private directory of the DBMS server if it does not exist (on a specific drive).
       
   105 If the supplied aDriveNumber parameter refers to a rom drive, the method does nothing.
       
   106 @param aDriveNumber The drive number, where the private DBMS data directory has to be created.
       
   107 @param aFs A file session instance.
       
   108 @leave RFs::CreatePrivatePath() leave error codes.
       
   109 @internalComponent
       
   110 */
       
   111 static void CreatePrivateDataPathL(TDriveNumber aDriveNumber, RFs& aFs)
       
   112 	{
       
   113 	TDriveInfo driveInfo;
       
   114 	__LEAVE_IF_ERROR(aFs.Drive(driveInfo, aDriveNumber));
       
   115 	if(driveInfo.iDriveAtt & KDriveAttRom)
       
   116 		{//ROM drive - do nothing.
       
   117 		return;
       
   118 		}
       
   119 	TInt err = aFs.CreatePrivatePath(aDriveNumber);
       
   120 	if(err != KErrNone && err != KErrAlreadyExists)
       
   121 		{
       
   122 		__LEAVE(err);
       
   123 		}
       
   124 	}
       
   125 
       
   126 /**
       
   127 */
       
   128 RDbPropsFactory::RDbPropsFactory(RFs& aFs) :
       
   129 	iFs(aFs),
       
   130 	iFileNameParser(NULL),
       
   131 	iPrivateDataPath(NULL)
       
   132 	{
       
   133 	}
       
   134 
       
   135 /**
       
   136 Initializes RDbPropsFactory instance
       
   137 @leave One of the system wide error codes, including KErrNoMemory.
       
   138 */
       
   139 void RDbPropsFactory::OpenL()
       
   140 	{
       
   141 	iFileNameParser = new (ELeave) TParse;
       
   142 	iPrivateDataPath = HBufC::NewL(KMaxFileName);
       
   143 	TPtr ptr(iPrivateDataPath->Des());
       
   144 	__LEAVE_IF_ERROR(iFs.PrivatePath(ptr));
       
   145 	}
       
   146 
       
   147 /**
       
   148 */
       
   149 void RDbPropsFactory::Close()
       
   150 	{
       
   151 	delete iPrivateDataPath;
       
   152 	delete iFileNameParser;
       
   153 	}
       
   154 
       
   155 /**
       
   156 Extracts database properties from the database path and format string.
       
   157 The created and returned TDbProps instance will be pushed in the cleanup stack.
       
   158 @param aPath Database path.
       
   159 @param aFormatStr database format string.
       
   160 @return A pointer to the created TDbProps instance.
       
   161 @leave One of the system-wide error codes, including KErrNoMemory.
       
   162 */
       
   163 TDbProps* RDbPropsFactory::ExtractLC(const TDesC& aPath, const TDesC& aFormatStr)
       
   164 	{
       
   165 	__ASSERT(iFileNameParser);
       
   166 	__ASSERT(iPrivateDataPath);
       
   167 
       
   168 	__LEAVE_IF_ERROR(iFileNameParser->Set(aPath, NULL, NULL));
       
   169 	TDbProps* dbProps = ::NewDbPropsLC();
       
   170 
       
   171 	//TDbProps::iDbsUid.iUid, TDbProps::iDbsUid.iRqAccess
       
   172 	TPtrC fmtIdent;//fmtIdent may contain KSecure keyword.
       
   173 	::ExtractUidAndName(aFormatStr, dbProps->iDbPolicyRequest.iUid, fmtIdent);
       
   174 	dbProps->iDbPolicyRequest.iAccessType = (fmtIdent.CompareF(KSecure) == 0 ? EATSecure : EATNonSecure);
       
   175 
       
   176 	//TDbProps::iDriveNumber
       
   177 	dbProps->iDriveNumber = ::ExtractDriveNumberL(*iFileNameParser);
       
   178 	::CheckDriveL(iFs, dbProps->iDriveNumber);
       
   179 
       
   180 	if(dbProps->iDbPolicyRequest.iAccessType == EATSecure)
       
   181 		{//requested access to a secure shared database
       
   182 		ExtractSecureL(aFormatStr, *dbProps);
       
   183 		}
       
   184 	else
       
   185 		{//requested access to a non-secure database
       
   186 		ExtractNonSecureL(aPath, aFormatStr, *dbProps);
       
   187 		}
       
   188 
       
   189 	return dbProps;
       
   190 	}
       
   191 
       
   192 /**
       
   193 Extracts database properties from the database path, assuming that this is a secure shared
       
   194 database.
       
   195 The created and returned TDbProps instance will be pushed in the cleanup stack.
       
   196 @param aPath Database path.
       
   197 @param aPolicyUid Security policy UID.
       
   198 @return A pointer to the created TDbProps instance.
       
   199 @leave One of the system-wide error codes, including KErrNoMemory.
       
   200 */
       
   201 TDbProps* RDbPropsFactory::ExtractLC(const TDesC& aPath, TUid aPolicyUid)
       
   202 	{
       
   203 	TBuf<32> dbFormat;
       
   204 	dbFormat.Copy(KSecure);
       
   205 	dbFormat.Append(aPolicyUid.Name());
       
   206 	return ExtractLC(aPath, dbFormat);
       
   207 	}
       
   208 
       
   209 /**
       
   210 Utility method, which can be used separately to get the common part of the secure
       
   211 shared database full path. The result string format is:
       
   212 "<Drive>:\<PrivatePath>\"
       
   213 @param aDriveNumber A drive number, for which the private data path string has to be constructed.
       
   214 @param aRes An output parameter, referencing the location, where the created private path has to be copied.
       
   215 @leave RFs::DriveToChar() leave error codes
       
   216 */
       
   217 void RDbPropsFactory::GetPrivatePathL(TDriveNumber aDriveNumber, TDes& aRes) const
       
   218 	{
       
   219 	aRes.Zero();
       
   220 	TChar driveChar;
       
   221 	__LEAVE_IF_ERROR(RFs::DriveToChar(aDriveNumber, driveChar));
       
   222 	aRes.Append(driveChar);
       
   223 	aRes.Append(':');
       
   224 	aRes.Append(*iPrivateDataPath);
       
   225 	}
       
   226 
       
   227 /**
       
   228 Extracts secure shared database properties.
       
   229 @param aFormatStr Secure shared database format string.
       
   230 @param aDbProps An output parameter, referencing the location, where the datapase properties will be stored.
       
   231 @leave KErrArgument Bad format string. Some of the other system-wide error codes.
       
   232 */
       
   233 void RDbPropsFactory::ExtractSecureL(const TDesC& aFormatStr, TDbProps& aDbProps)
       
   234 	{	
       
   235 	if(aDbProps.iDbPolicyRequest.iUid == KNullUid)
       
   236 		{//Secure shared database cannot have null uid.
       
   237 		__LEAVE(KErrArgument);
       
   238 		}
       
   239 	if(iFileNameParser->PathPresent())
       
   240 		{//The path can contain only the database name.
       
   241 		__LEAVE(KErrArgument);
       
   242 		}
       
   243 	TPtrC dbName = iFileNameParser->NameAndExt();
       
   244 	if(dbName.Length() > KDbMaxName)
       
   245 		{//There is a limit for the secure shared database names
       
   246 		__LEAVE(KErrArgument);
       
   247 		}
       
   248 	::CreatePrivateDataPathL(aDbProps.iDriveNumber, iFs);
       
   249 	ConstructFullDbPathL(aDbProps);
       
   250 	ConstructFormatString(aDbProps, aFormatStr);
       
   251 	}
       
   252 
       
   253 /**
       
   254 Extracts non-secure database properties.
       
   255 @param aPath Database path.
       
   256 @param aFormatStr Database format string.
       
   257 @param aDbProps An output parameter, referencing the location, where the datapase properties will be stored.
       
   258 @leave KErrPermissionDenied The database path contains the DBMS server private data path.
       
   259 */
       
   260 void RDbPropsFactory::ExtractNonSecureL(const TDesC& aPath, const TDesC& aFormatStr, 
       
   261 										TDbProps& aDbProps)
       
   262 	{
       
   263 	//DBMS private data path cannot be the first in the database path. This is non-secure database.
       
   264 	TInt pos = aPath.FindF(iPrivateDataPath->Des());
       
   265 	if(pos != KErrNotFound)
       
   266 		{//If pos is 2 (pos 0 - drive letter, pos 1 - ':'), then 
       
   267 		 //the caller wants to create/open non-secure database in the DBMS private directory,
       
   268 		 //which is not allowed. 
       
   269 		if(pos == 2)
       
   270 			{
       
   271 			__LEAVE(KErrPermissionDenied);
       
   272 			}
       
   273 		}
       
   274 	//The database path and format string stay the same
       
   275 	aDbProps.iPath.Copy(aPath);
       
   276 	aDbProps.iFormatStr.Copy(aFormatStr);
       
   277 	}
       
   278 
       
   279 /**
       
   280 Constructs the full physical path of the secure shared database
       
   281 @param aDbProps An output parameter, where the database path will be stored.
       
   282 @leave RDbPropsFactory::GetPrivatePathL() leaving error codes
       
   283 @see RDbPropsFactory::GetPrivatePathL()
       
   284 */
       
   285 void RDbPropsFactory::ConstructFullDbPathL(TDbProps& aDbProps)
       
   286 	{
       
   287 	GetPrivatePathL(aDbProps.iDriveNumber, aDbProps.iPath);
       
   288 	TBuf<32> dbNameCmnPart;
       
   289 	RDbPropsFactory::ConstructCommonPart(aDbProps.iDbPolicyRequest.iUid, dbNameCmnPart);
       
   290 	aDbProps.iPath.Append(dbNameCmnPart);
       
   291 	aDbProps.iPath.Append(iFileNameParser->Name());
       
   292 	aDbProps.iPath.Append(iFileNameParser->Ext());
       
   293 	}
       
   294 
       
   295 /**
       
   296 Processes the format string of secure shared database. "SECURE" keyword and security policy UID
       
   297 will be removed.
       
   298 @param aDbProps An input/output parameter, referencing TDbProps instance, where the processed
       
   299                 database format string will be stored.
       
   300 @param aFormatStr The database format string.
       
   301 */
       
   302 void RDbPropsFactory::ConstructFormatString(TDbProps& aDbProps, const TDesC& aFormatStr)
       
   303 	{
       
   304 	TDes& fmtStr = aDbProps.iFormatStr;
       
   305 	fmtStr.Copy(aFormatStr);
       
   306 	//Remove KSecure keyword from the format string 
       
   307 	TInt pos = fmtStr.FindF(KSecure);
       
   308 	__ASSERT_DEBUG(pos != KErrNotFound, User::Invariant());
       
   309 	fmtStr.Delete(pos, KSecure().Length());
       
   310 	//Remove the UID from the format string
       
   311 	TInt pos_b = fmtStr.Locate('[');
       
   312 	TInt pos_e = fmtStr.Locate(']');
       
   313 	__ASSERT_DEBUG(pos_b != KErrNotFound && pos_e != KErrNotFound, User::Invariant());
       
   314 	fmtStr.Delete(pos_b, pos_e - pos_b + 1);
       
   315 	}