policymanagement/dmutilserver/src/DMUtilSession.cpp
changeset 0 b497e44ab2fc
child 25 b183ec05bd8c
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2000 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Implementation of policymanagement components
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 
       
    21 #include "DMUtilSession.h"
       
    22 #include "ACLStorage.h"
       
    23 #include "DMUtilServer.h"
       
    24 #include "policymnginternalpskeys.h"
       
    25 #include "debug.h"
       
    26 
       
    27 #include "CentRepToolClient.h"
       
    28 
       
    29 
       
    30 #include <e32svr.h>
       
    31 #include <centralrepository.h>
       
    32 
       
    33 #include <coreapplicationuisdomainpskeys.h>
       
    34 
       
    35 
       
    36 
       
    37 #include <e32property.h>
       
    38 #include <f32file.h>
       
    39 
       
    40 #ifdef RD_MULTIPLE_DRIVE
       
    41 #include <driveinfo.h>
       
    42 #endif //RD_MULTIPLE_DRIVEs
       
    43 
       
    44 // CONSTANTS
       
    45 #ifndef RD_MULTIPLE_DRIVE
       
    46 const TUint8 KMMCDriveLetter = 'e';
       
    47 #endif
       
    48 _LIT( MDriveColon, ":");
       
    49 _LIT( KMMCEraseFlagFileName, "fmmc.dat");
       
    50 
       
    51 // MACROS
       
    52 
       
    53 
       
    54 // DATA TYPES
       
    55 // FUNCTION PROTOTYPES
       
    56 // FORWARD DECLARATIONS
       
    57 
       
    58 // DMUtilSession.cpp
       
    59 //
       
    60 
       
    61 // ----------------------------------------------------------------------------------------
       
    62 // CDMUtilSession::CDMUtilSession
       
    63 // ----------------------------------------------------------------------------------------
       
    64 CDMUtilSession::CDMUtilSession()
       
    65 	{
       
    66 	RDEBUG("CDMUtilSession::CDMUtilSession");
       
    67 	}
       
    68 
       
    69 // ----------------------------------------------------------------------------------------
       
    70 // CDMUtilSession::Server
       
    71 // ----------------------------------------------------------------------------------------
       
    72 CDMUtilServer& CDMUtilSession::Server()
       
    73 	{
       
    74 	return *static_cast<CDMUtilServer*>(const_cast<CServer2*>(CSession2::Server()));
       
    75 	}
       
    76 
       
    77 // ----------------------------------------------------------------------------------------
       
    78 // CDMUtilSession::CreateL
       
    79 // 2nd phase construct for sessions - called by the CServer framework
       
    80 // ----------------------------------------------------------------------------------------
       
    81 void CDMUtilSession::CreateL()
       
    82 {
       
    83 	RDEBUG("CDMUtilSession::CreateL");
       
    84 	
       
    85 	iACLStorage = CDMUtilServer::ACLStorage();
       
    86 }
       
    87 
       
    88 // ----------------------------------------------------------------------------------------
       
    89 // CDMUtilSession::~CDMUtilSession
       
    90 // ----------------------------------------------------------------------------------------
       
    91 CDMUtilSession::~CDMUtilSession()
       
    92 {
       
    93 	RDEBUG("CDMUtilSession::~CDMUtilSession");
       
    94 }
       
    95 
       
    96 // ----------------------------------------------------------------------------------------
       
    97 // CDMUtilSession::PerformRFS
       
    98 // ----------------------------------------------------------------------------------------
       
    99 TInt CDMUtilSession::PerformRFSL()
       
   100 {
       
   101 	RDEBUG("CDMUtilSession: Restory factory setting operation started");
       
   102 	//RFS tasks in DMUtilServer
       
   103 	// 1. Update RFS flag status
       
   104 	// 2. Reset policy management state flags
       
   105 	
       
   106 	//update RFS flag
       
   107 	CRepository* cenrep = CRepository::NewLC( KCRUidPolicyManagementUtilInternalKeys);
       
   108 	
       
   109 	TInt currentValue = 0;
       
   110 	TInt err = cenrep->Get( KRFSCounterKey, currentValue);
       
   111 	if ( err == KErrNone)
       
   112 	{
       
   113 		cenrep->Set( KRFSCounterKey, ++currentValue);
       
   114 		RProperty::Set( KPolicyMngProperty, KFactoryResetCounter, currentValue);
       
   115 	}
       
   116 	
       
   117 	//reset policy management state flags
       
   118 	cenrep->Set( KPolicyChangeCounter, 0);
       
   119 	RProperty::Set( KPolicyMngProperty, KPolicyChangedCounter, 0);
       
   120 		
       
   121 	cenrep->Set( KTerminalSecurityStateKey, 0);
       
   122 	SetIndicatorStateL( cenrep);
       
   123 	
       
   124 	CleanupStack::PopAndDestroy( cenrep);
       
   125 	
       
   126 	RDEBUG("CDMUtilSession: Restory factory setting operation finished");
       
   127 	
       
   128 	
       
   129 	return KErrNone;
       
   130 }
       
   131 
       
   132 
       
   133 // ----------------------------------------------------------------------------------------
       
   134 // CDMUtilSession::ServiceL
       
   135 // ----------------------------------------------------------------------------------------
       
   136 void CDMUtilSession::ServiceL(const RMessage2& aMessage)
       
   137 {
       
   138     TRAPD(err,DispatchMessageL(aMessage));
       
   139 	aMessage.Complete(err);
       
   140 }
       
   141 
       
   142 // ----------------------------------------------------------------------------------------
       
   143 // CDMUtilSession::DispatchMessageL
       
   144 // ----------------------------------------------------------------------------------------
       
   145 void CDMUtilSession::DispatchMessageL(const RMessage2& aMessage)
       
   146 {
       
   147 	RDEBUG_2("CDMUtilSession::ServiceL; %d",aMessage.Function());
       
   148 
       
   149 	switch ( aMessage.Function())
       
   150 	{
       
   151 		case ERemoveACL:
       
   152 		case ESetACL:
       
   153 		case EAddACL:
       
   154 		{
       
   155 			//read URI... 
       
   156 			//check for aMessage Length if message length is negative
       
   157 			//then leave the Dispatch MessageL with KErrUnderFlow
       
   158 			
       
   159 			TInt minlength = aMessage.GetDesLength(0);
       
   160 			if(minlength < 0)
       
   161 			User::Leave(KErrUnderflow);
       
   162 			
       
   163 			HBufC8* uri = HBufC8::NewLC( aMessage.GetDesLength(0));
       
   164 			TPtr8 uriPtr = uri->Des();
       
   165 			aMessage.ReadL(0, uriPtr, 0);
       
   166 			
       
   167 			if ( aMessage.Function() == ERemoveACL )
       
   168 			{
       
   169 				RDEBUG("	DispatchMessageL case ERemoveACL");
       
   170 				TBool restoreDefaults;
       
   171 				TPckg<TBool> restoreDefaultsPack( restoreDefaults);
       
   172 				aMessage.ReadL( 1, restoreDefaultsPack, 0);
       
   173 				
       
   174 				iACLStorage->RemoveACL( *uri, restoreDefaults);
       
   175 			}
       
   176 			else
       
   177 			{
       
   178 				//destination
       
   179 				TACLDestination dest;
       
   180 				TPckg<TACLDestination> destPack( dest);
       
   181 				aMessage.ReadL(1, destPack, 0);
       
   182 
       
   183 				//ACL commands
       
   184 				TAclCommands command;
       
   185 				TPckg<TAclCommands> commandPack( command);
       
   186 				aMessage.ReadL(2, commandPack, 0);
       
   187 		
       
   188 				if ( aMessage.Function() == ESetACL)
       
   189 				{
       
   190 					RDEBUG("	DispatchMessageL case ESetACL");
       
   191 					iACLStorage->SetACLForNodeL( *uri, dest, command);
       
   192 				}
       
   193 				else
       
   194 				{
       
   195 					RDEBUG("	DispatchMessageL case EAddACL");
       
   196 					iACLStorage->AddACLForNodeL( *uri, dest, command);
       
   197 				}
       
   198 			}
       
   199 
       
   200 			CleanupStack::PopAndDestroy( uri);
       
   201 		}
       
   202 		break;
       
   203 		case EMngSessionCertificate:
       
   204 		{
       
   205 			RDEBUG("	DispatchMessageL case EMngSessionCertificate");
       
   206 			//certificate information
       
   207 			TCertInfo certInfo;
       
   208 			TPckg<TCertInfo> certInfoPack( iACLStorage->MngSessionCertificate());
       
   209 			aMessage.ReadL(0, certInfoPack, 0);	
       
   210 		}
       
   211 		break;
       
   212 		case ENewDMSession:
       
   213 		{
       
   214 			TInt minlength = aMessage.GetDesLength(1);
       
   215 			if(minlength < 0)
       
   216 			User::Leave(KErrUnderflow);
       
   217 			
       
   218 					
       
   219 			RDEBUG("	DispatchMessageL case ENewDMSession");
       
   220 			//certificate information
       
   221 			TCertInfo certInfo;
       
   222 			TPckg<TCertInfo> certInfoPack( certInfo);
       
   223 			aMessage.ReadL(0, certInfoPack, 0);
       
   224 			
       
   225 			//certificate information
       
   226 			TPtr8 serverid = HBufC8::NewLC( aMessage.GetDesLength(1))->Des();
       
   227 			aMessage.ReadL(1, serverid, 0);
       
   228 
       
   229 			iACLStorage->NewSessionL( certInfo, serverid);
       
   230 			CleanupStack::PopAndDestroy();	//HBufC8
       
   231 		}
       
   232 		break;
       
   233 		case EFlush:
       
   234 			{
       
   235 			RDEBUG("	DispatchMessageL case EFlush");
       
   236 			iACLStorage->SaveACLL();
       
   237 			}
       
   238 		break;
       
   239 	    case EGetDMSessionCertInfo:
       
   240         {
       
   241         	RDEBUG("	DispatchMessageL case EGetDMSessionCertInfo");
       
   242         	//get cert info
       
   243 	        TCertInfo certInfo;
       
   244 	        User::LeaveIfError( Server().ACLStorage()->CertInfo( certInfo ) );
       
   245 	
       
   246 			//write cert info to client side
       
   247 			TPckg<TCertInfo> certInfoPack( certInfo );
       
   248 			aMessage.WriteL(0, certInfoPack );
       
   249         }
       
   250 	    break;
       
   251 		case EGetDMSessionServerId:
       
   252 		{
       
   253 			RDEBUG("	DispatchMessageL case EGetDMSessionServerId");
       
   254 			//get server id
       
   255 			HBufC8 * serverid = Server().GetServerIDL();
       
   256 			CleanupStack::PushL(  serverid);
       
   257 			
       
   258 			//write server id to client side
       
   259 			aMessage.WriteL(0, *serverid);
       
   260 
       
   261 			CleanupStack::PopAndDestroy( serverid);
       
   262 		} 	 
       
   263 		break;   
       
   264 		case EUpdatePolicyMngStatusFlags:
       
   265 	    {
       
   266 	    	RDEBUG("	DispatchMessageL case EUpdatePolicyMngStatusFlags");
       
   267 			KPolicyMngStatusFlags policyMngStatus;
       
   268 			TPckg<KPolicyMngStatusFlags> policyMngStatusPack( policyMngStatus);
       
   269 			aMessage.ReadL(0, policyMngStatusPack, 0);
       
   270 
       
   271 	    	UpdateStatusFlagsL( policyMngStatus);
       
   272 	    }
       
   273 	    break;	    
       
   274 		case EPerformDMUtilRFS:
       
   275 		{
       
   276 			RDEBUG("	DispatchMessageL case EPerformDMUtilRFS");
       
   277 			PerformRFSL();
       
   278 		}
       
   279 	    break;
       
   280 		case EMarkMMCWipe:
       
   281 				{
       
   282 				MarkMMCWipeL();
       
   283 				}
       
   284 		default:
       
   285 		break;
       
   286     }
       
   287 }
       
   288 
       
   289 
       
   290 // ----------------------------------------------------------------------------------------
       
   291 // CDMUtilServer::UpdateStatusFlagsL
       
   292 // Define publish&subscribe properties
       
   293 // ----------------------------------------------------------------------------------------
       
   294 
       
   295 void CDMUtilSession::UpdateStatusFlagsL( KPolicyMngStatusFlags aUpdateCommand)
       
   296 {
       
   297 	RDEBUG("CDMUtilSession::UpdateStatusFlagsL()");
       
   298 	//set initial values for properties
       
   299 	CRepository* cenrep = CRepository::NewLC( KCRUidPolicyManagementUtilInternalKeys);
       
   300 
       
   301 	switch ( aUpdateCommand )
       
   302 	{
       
   303 		case EPolicyChanged:
       
   304 		{
       
   305 			RDEBUG("		UpdateStatusFlagsL() case EPolicyChanged");
       
   306 			TInt currentValue = 0;
       
   307 			User::LeaveIfError( cenrep->Get( KPolicyChangeCounter, currentValue));
       
   308 			User::LeaveIfError( cenrep->Set( KPolicyChangeCounter, ++currentValue));
       
   309 			
       
   310 			User::LeaveIfError( RProperty::Set( KPolicyMngProperty, KPolicyChangedCounter, currentValue));
       
   311 		
       
   312 		}
       
   313 		break;	
       
   314 		case ETerminalSecurityActive:
       
   315 			RDEBUG("		UpdateStatusFlagsL() case ETerminalSecurityActive");
       
   316 			User::LeaveIfError( cenrep->Set( KTerminalSecurityStateKey, 1));
       
   317 			SetIndicatorStateL( cenrep);
       
   318 		break;
       
   319 		case ETerminalSecurityDeactive:
       
   320 			RDEBUG("		UpdateStatusFlagsL() case ETerminalSecurityDeactive");
       
   321 			User::LeaveIfError( cenrep->Set( KTerminalSecurityStateKey, 0));
       
   322 			SetIndicatorStateL( cenrep);
       
   323 		break;
       
   324 		default:
       
   325 		break;
       
   326 	}
       
   327 
       
   328 	
       
   329 	CleanupStack::PopAndDestroy( cenrep);
       
   330 }
       
   331 
       
   332 
       
   333 // ----------------------------------------------------------------------------------------
       
   334 // CDMUtilServer::UpdateSessionInfoL
       
   335 // ----------------------------------------------------------------------------------------
       
   336 void CDMUtilSession::SetIndicatorStateL( CRepository * aCenRep)
       
   337 {
       
   338 	RDEBUG8_2("CDMUtilSession::SetIndicatorStateL() 0x%x", aCenRep);
       
   339 	//get terminal security and management state from centrep
       
   340 	TInt terminalSecurity = 0;
       
   341 	TInt err( KErrNone );
       
   342 
       
   343 	if ( !aCenRep)
       
   344 		{
       
   345 		CRepository* rep = CRepository::NewLC( KCRUidPolicyManagementUtilInternalKeys);				
       
   346 		err = rep->Get( KTerminalSecurityStateKey, terminalSecurity);
       
   347 		RDEBUG8_2("CDMUtilSession::SetIndicatorStateL aCenRep not found ERROR: %d",err);
       
   348 		CleanupStack::PopAndDestroy( rep);
       
   349 		}
       
   350 	else
       
   351 		{
       
   352 		err = aCenRep->Get( KTerminalSecurityStateKey, terminalSecurity);
       
   353 		RDEBUG8_2("CDMUtilSession::SetIndicatorStateL aCenRep ERROR: %d",err);
       
   354 		}
       
   355 
       
   356 	//set indicator state...
       
   357 	if ( CDMUtilServer::iManagementActive )			
       
   358 		{
       
   359 		RDEBUG(" iManagementActive is ETrue -> ECoreAppUIsTarmMngActiveIndicatorOn");
       
   360 		err = RProperty::Set( KPSUidCoreApplicationUIs, KCoreAppUIsTarmIndicator, ECoreAppUIsTarmMngActiveIndicatorOn);	
       
   361 		}
       
   362 	else if ( terminalSecurity )
       
   363 		{
       
   364 		RDEBUG(" terminalSecurity is ETrue -> ECoreAppUIsTarmTerminalSecurityOnIndicatorOn");
       
   365 		err = RProperty::Set( KPSUidCoreApplicationUIs, KCoreAppUIsTarmIndicator, ECoreAppUIsTarmTerminalSecurityOnIndicatorOn);	
       
   366 		}
       
   367 	else
       
   368 		{
       
   369 		RDEBUG(" terminalSecurity is EFalse -> ECoreAppUIsTarmIndicatorsOff");
       
   370 		err = RProperty::Set( KPSUidCoreApplicationUIs, KCoreAppUIsTarmIndicator, ECoreAppUIsTarmIndicatorsOff);	
       
   371 		}	
       
   372 	
       
   373 	if( err != KErrNone )
       
   374 		{
       
   375 		RDEBUG8_2("CDMUtilSession::SetIndicatorStateL ERROR: %d",err);
       
   376 		}
       
   377 		
       
   378 	RDEBUG("CDMUtilSession::SetIndicatorStateL() - END");
       
   379 }
       
   380 
       
   381 
       
   382 // ----------------------------------------------------------------------------------------
       
   383 // CDMUtilServer::DefineProperties
       
   384 // Define publish&subscribe properties
       
   385 // ----------------------------------------------------------------------------------------
       
   386 void CDMUtilSession::DefinePropertiesL()
       
   387 {
       
   388 	RDEBUG("CDMUtilSession::DefinePropertiesL()");
       
   389 	//define properties...
       
   390 	TInt err = RProperty::Define( KPolicyMngProperty, KPolicyChangedCounter, RProperty::EInt);
       
   391 	if ( err != KErrNone && err != KErrAlreadyExists)
       
   392 	{
       
   393 		User::Leave( err);
       
   394 	}
       
   395 	
       
   396 	err = RProperty::Define( KPolicyMngProperty, KFactoryResetCounter, RProperty::EInt);
       
   397 	if ( err != KErrNone &&  err != KErrAlreadyExists)
       
   398 	{
       
   399 		User::Leave( err);
       
   400 	}
       
   401 	
       
   402 	//set initial values for properties
       
   403 	CRepository* cenrep = CRepository::NewLC( KCRUidPolicyManagementUtilInternalKeys);
       
   404 	
       
   405 	//policy change counter
       
   406 	TInt value = 0;
       
   407 	err = cenrep->Get( KPolicyChangeCounter, value);
       
   408 	if ( err == KErrNotFound)
       
   409 	{
       
   410 		User::LeaveIfError( cenrep->Create( KPolicyChangeCounter, value));
       
   411 	}
       
   412 	User::LeaveIfError( RProperty::Set( KPolicyMngProperty, KPolicyChangedCounter, 0));
       
   413 
       
   414 	//policy change counter
       
   415 	value = 0;
       
   416 	err = cenrep->Get( KRFSCounterKey, value);
       
   417 	if ( err == KErrNotFound)
       
   418 	{
       
   419 		User::LeaveIfError( cenrep->Create( KRFSCounterKey, value));
       
   420 	}
       
   421 	User::LeaveIfError( RProperty::Set( KPolicyMngProperty, KFactoryResetCounter, 0));
       
   422 	
       
   423 	//terminal security active
       
   424 	err = cenrep->Get( KTerminalSecurityStateKey, value);
       
   425 	if ( err == KErrNotFound)
       
   426 	{
       
   427 		User::LeaveIfError( cenrep->Create( KTerminalSecurityStateKey, 0));
       
   428 	}
       
   429 
       
   430 	CDMUtilSession::SetIndicatorStateL( cenrep);
       
   431 
       
   432 	CleanupStack::PopAndDestroy( cenrep);
       
   433 }
       
   434 
       
   435 
       
   436 // ----------------------------------------------------------------------------------------
       
   437 // CDMUtilSession::ServiceError
       
   438 // Handle an error from CDMUtilSession::ServiceL()
       
   439 // ----------------------------------------------------------------------------------------
       
   440 void CDMUtilSession::ServiceError(const RMessage2& aMessage,TInt aError)
       
   441 	{
       
   442 	RDEBUG_2("CDMUtilSession::ServiceError %d",aError);
       
   443 	CSession2::ServiceError(aMessage,aError);
       
   444 	}
       
   445 	
       
   446 	
       
   447 	
       
   448 	
       
   449 void CDMUtilSession::MarkMMCWipeL()
       
   450 	{
       
   451 	RDEBUG("CDMUtilSession::MarkMMCWipeL()");
       
   452 	RFs rfs;
       
   453 	User::LeaveIfError( rfs.Connect() );
       
   454 	CleanupClosePushL( rfs );	
       
   455 	
       
   456 	// create private directory to MMC
       
   457 #ifndef RD_MULTIPLE_DRIVE
       
   458 	TInt err = rfs.CreatePrivatePath( EDriveE );
       
   459 	if( err != KErrNone && err != KErrAlreadyExists )
       
   460 		{
       
   461 		RDEBUG_2("	**** ERROR, can not create private path on MMC drive: %d", err );
       
   462 		User::Leave( err );
       
   463 		}
       
   464 		
       
   465 	TFileName fullPath;
       
   466 	fullPath.Append( KMMCDriveLetter );
       
   467 	fullPath.Append( MDriveColon );
       
   468 	
       
   469 	TFileName privatePath;
       
   470 	User::LeaveIfError( rfs.PrivatePath( privatePath ) );
       
   471 	fullPath.Append( privatePath );
       
   472 	
       
   473 	fullPath.Append( KMMCEraseFlagFileName );
       
   474 	
       
   475 	RFile file;
       
   476 	// it is ok to leave if the file already exists
       
   477 	User::LeaveIfError( file.Create( rfs, fullPath, EFileWrite ) );
       
   478 	file.Close();
       
   479 #else
       
   480 	TDriveList driveList;
       
   481 	TInt driveCount;
       
   482 	//Get all removeable drive, both physically and logically
       
   483 	User::LeaveIfError(DriveInfo::GetUserVisibleDrives(
       
   484             rfs,  driveList,  driveCount, KDriveAttRemovable ));
       
   485     
       
   486     TInt max(driveList.Length());
       
   487     
       
   488     for(TInt i=0; i<max;++i)
       
   489     {
       
   490     	if (driveList[i])
       
   491     	{
       
   492     		TUint status;
       
   493     		DriveInfo::GetDriveStatus(rfs, i, status);
       
   494     		//To make sure the drive is physically removeable not logically removeable	
       
   495     		//need to format internal mass memory also. So no need to check the below condition
       
   496     	//	if (status & DriveInfo::EDriveRemovable)
       
   497     		{
       
   498     		
       
   499     			TInt err = rfs.CreatePrivatePath( i );
       
   500 				if( err != KErrNone && err != KErrAlreadyExists )
       
   501 				{
       
   502 					RDEBUG_2("	**** ERROR, can not create private path on MMC drive: %d", err );
       
   503 					User::Leave( err );
       
   504 				}
       
   505 				
       
   506     			TChar driveLetter;
       
   507     			rfs.DriveToChar(i,driveLetter);
       
   508     			TFileName fullPath;
       
   509 				fullPath.Append( driveLetter );
       
   510 				fullPath.Append( MDriveColon );
       
   511 				
       
   512 				TFileName privatePath;
       
   513 				User::LeaveIfError( rfs.PrivatePath( privatePath ) );
       
   514 				fullPath.Append( privatePath );
       
   515 				fullPath.Append( KMMCEraseFlagFileName );
       
   516 				
       
   517 				RFile file;
       
   518 				// it is ok to leave if the file already exists
       
   519 				User::LeaveIfError( file.Create( rfs, fullPath, EFileWrite ) );
       
   520 				file.Close();
       
   521     		}
       
   522     	}
       
   523      }
       
   524 
       
   525 #endif //RD_MULTIPLE_DRIVE	
       
   526 	RDEBUG("	MMC format marker file created successfully! MMC wipe enabled.");
       
   527 	CleanupStack::PopAndDestroy( &rfs );
       
   528 	}
       
   529 	
       
   530 
       
   531 // ----------------------------------------------------------------------------------------
       
   532 // End of file