eapol/eapol_framework/eapol_symbian/am/type/mschapv2/symbian/plugin/src/EapMsChapV2DbUtils.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2006 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 the License "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:  EAP and WLAN authentication protocols.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // This is enumeration of EAPOL source code.
       
    20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    21 	#undef EAP_FILE_NUMBER_ENUM
       
    22 	#define EAP_FILE_NUMBER_ENUM 294 
       
    23 	#undef EAP_FILE_NUMBER_DATE 
       
    24 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    26 
       
    27 
       
    28 // INCLUDE FILES
       
    29 
       
    30 #include "EapMsChapV2DbUtils.h"
       
    31 #include "EapMsChapV2DbDefaults.h"
       
    32 #include "EapMsChapV2DbParameterNames.h"
       
    33 
       
    34 #include "eap_am_trace_symbian.h"
       
    35 
       
    36 const TUint KMaxSqlQueryLength = 512;
       
    37 const TInt KMicroSecsInAMinute = 60000000; // 60000000 micro seconds is 1 minute.
       
    38 
       
    39 // ================= MEMBER FUNCTIONS =======================
       
    40 
       
    41 void EapMsChapV2DbUtils::OpenDatabaseL(RDbNamedDatabase& aDatabase, RDbs& aSession, const TIndexType aIndexType,
       
    42 	const TInt aIndex, const eap_type_value_e aTunnelingType)
       
    43 {
       
    44 #ifdef USE_EAP_EXPANDED_TYPES
       
    45 
       
    46 	TUint aTunnelingVendorType = aTunnelingType.get_vendor_type();
       
    47 
       
    48 #else
       
    49 
       
    50 	TUint aTunnelingVendorType = static_cast<TUint>(aTunnelingType);
       
    51 
       
    52 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
    53 
       
    54 	EAP_TRACE_DEBUG_SYMBIAN((_L("EapMsChapV2DbUtils::OpenDatabaseL -Start- aIndexType=%d, aIndex=%d, aTunnelingVendorType=%d \n"),
       
    55 	aIndexType,aIndex,aTunnelingVendorType) );
       
    56 
       
    57 	// 1. Open/create a database	
       
    58 	
       
    59 	// Connect to the DBMS server.
       
    60 	User::LeaveIfError(aSession.Connect());		
       
    61 	CleanupClosePushL(aSession);	
       
    62 	// aSession and aDatabase are pushed to the cleanup stack even though they may be member
       
    63 	// variables of the calling class and would be closed in the destructor anyway. This ensures
       
    64 	// that if they are not member variables they will be closed. Closing the handle twice
       
    65 	// does no harm.	
       
    66 	
       
    67 #ifdef SYMBIAN_SECURE_DBMS
       
    68 	
       
    69 	// Create the secure shared database with the specified secure policy.
       
    70 	// Database will be created in the data caging path for DBMS (C:\private\100012a5).
       
    71 	
       
    72 	TInt err = aDatabase.Create(aSession, KDatabaseName, KSecureUIDFormat);
       
    73 	
       
    74 	EAP_TRACE_DEBUG_SYMBIAN((_L("EapMsChapV2DbUtils::OpenDatabaseL - Created Secure DB for eapmsmhapv2.dat. err=%d\n"), err));
       
    75 
       
    76 	
       
    77 	if(err == KErrNone)
       
    78 	{
       
    79 		aDatabase.Close();
       
    80 		
       
    81 	} else if (err != KErrAlreadyExists) 
       
    82 	{
       
    83 		User::LeaveIfError(err);
       
    84 	}
       
    85 	
       
    86 	User::LeaveIfError(aDatabase.Open(aSession, KDatabaseName, KSecureUIDFormat));
       
    87 	CleanupClosePushL(aDatabase);		
       
    88 		
       
    89 #else
       
    90 	// For non-secured database. The database will be created in the old location (c:\system\data).
       
    91 	
       
    92 	RFs fsSession;		
       
    93 	User::LeaveIfError(fsSession.Connect());
       
    94 	CleanupClosePushL(fsSession);	
       
    95 	TInt err = aDatabase.Create(fsSession, KDatabaseName);
       
    96 	
       
    97 	EAP_TRACE_DEBUG_SYMBIAN((_L("EapMsChapV2DbUtils::OpenDatabaseL - Created Non-Secure DB for eapmsmhapv2.dat. err=%d\n"), err));
       
    98 
       
    99 	
       
   100 	if(err == KErrNone)
       
   101 	{
       
   102 		aDatabase.Close();
       
   103 		
       
   104 	} else if (err != KErrAlreadyExists) 
       
   105 	{
       
   106 		User::LeaveIfError(err);
       
   107 	}
       
   108 	CleanupStack::PopAndDestroy(); // close fsSession
       
   109 	
       
   110 	User::LeaveIfError(aDatabase.Open(aSession, KDatabaseName));
       
   111 	CleanupClosePushL(aDatabase);		
       
   112 	    
       
   113 #endif // #ifdef SYMBIAN_SECURE_DBMS
       
   114 
       
   115 // 2. Create the MSCHAPv2 table to database (ignore error if database exists)
       
   116 // Table columns:
       
   117 //// NAME ///////////////////////////////////////////////// TYPE ////////////// Constant /////////
       
   118 //| ServiceType                                         | UNSIGNED INTEGER  | KServiceType      |//
       
   119 //| ServiceIndex                                        | UNSIGNED INTEGER  | KServiceIndex     |//
       
   120 //| TunnelingType                                       | UNSIGNED INTEGER  | KTunnelingType    |//
       
   121 //| EAP_MSCHAPV2_password_prompt                        | UNSIGNED INTEGER  | cf_str_EAP_MSCHAPV2_password_prompt_literal   |//
       
   122 //| EAP_MSCHAPV2_username                               | VARCHAR(255)      | cf_str_EAP_MSCHAPV2_username_literal         |//
       
   123 //| EAP_MSCHAPV2_password                               | VARCHAR(255)      | cf_str_EAP_MSCHAPV2_password_literal         |//
       
   124 //| EAP_MSCHAPv2_max_session_validity_time				| BIGINT		   	| cf_str_EAP_MSCHAPv2_max_session_validity_time_literal   |//
       
   125 //| EAP_MSCHAPv2_last_full_authentication_time			| BIGINT		   	| KMSCHAPv2LastFullAuthTime	|//
       
   126 //////////////////////////////////////////////////////////////////////////////////////////////////
       
   127 
       
   128 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   129 	TPtr sqlStatement = buf->Des();
       
   130 
       
   131 	_LIT(KSQLCreateTable1, "CREATE TABLE %S (%S UNSIGNED INTEGER, \
       
   132 											 %S UNSIGNED INTEGER, \
       
   133 											 %S UNSIGNED INTEGER, \
       
   134 											 %S UNSIGNED INTEGER, \
       
   135 											 %S VARCHAR(255), \
       
   136 											 %S VARCHAR(255), \
       
   137 											 %S BIGINT, \
       
   138 											 %S BIGINT)");
       
   139 
       
   140 	sqlStatement.Format(KSQLCreateTable1,
       
   141 						&KMsChapV2TableName,
       
   142 						&KServiceType,
       
   143 						&KServiceIndex,
       
   144 						&KTunnelingType,
       
   145 						&cf_str_EAP_MSCHAPV2_password_prompt_literal,
       
   146 						&cf_str_EAP_MSCHAPV2_username_literal,
       
   147 						&cf_str_EAP_MSCHAPV2_password_literal,
       
   148 						&cf_str_EAP_MSCHAPv2_max_session_validity_time_literal, 
       
   149 						&KMSCHAPv2LastFullAuthTime);
       
   150 
       
   151 	err = aDatabase.Execute(sqlStatement);
       
   152 	if (err != KErrNone && err != KErrAlreadyExists)
       
   153 	{
       
   154 		User::Leave(err);
       
   155 	}
       
   156 
       
   157 	// 4. Check if database table contains a row for this service type and id  
       
   158 	
       
   159 	_LIT(KSQLQueryRow, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   160 	sqlStatement.Format(KSQLQueryRow,
       
   161 						&cf_str_EAP_MSCHAPV2_username_literal,
       
   162 						&KMsChapV2TableName,
       
   163 						&KServiceType,
       
   164 						aIndexType,
       
   165 						&KServiceIndex,
       
   166 						aIndex,
       
   167 						&KTunnelingType, 
       
   168 						aTunnelingVendorType);
       
   169 							
       
   170 	RDbView view;
       
   171 	User::LeaveIfError(view.Prepare(aDatabase, TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   172 	// View must be closed when no longer needed
       
   173 	CleanupClosePushL(view);
       
   174 	User::LeaveIfError(view.EvaluateAll());
       
   175 	
       
   176 	// 5. If row is not found then add it
       
   177 	
       
   178 	TInt rows = view.CountL();
       
   179 	CleanupStack::PopAndDestroy(); // view
       
   180 	if (rows == 0)
       
   181 	{
       
   182 		_LIT(KSQLInsert, "SELECT * FROM %S");
       
   183 		sqlStatement.Format(KSQLInsert, &KMsChapV2TableName);		
       
   184 										
       
   185 		view.Prepare(aDatabase, TDbQuery(sqlStatement), TDbWindow::EUnlimited, RDbView::EInsertOnly);
       
   186 		CleanupClosePushL(view);
       
   187 		
       
   188 		// Get column set so we get the correct column numbers
       
   189 		CDbColSet* colSet = view.ColSetL();		
       
   190 		CleanupStack::PushL(colSet);
       
   191 		
       
   192 		view.InsertL();
       
   193 		view.SetColL(colSet->ColNo(KServiceType), static_cast<TInt> (aIndexType));
       
   194 		view.SetColL(colSet->ColNo(KServiceIndex), aIndex);
       
   195 		view.SetColL(colSet->ColNo(KTunnelingType), aTunnelingVendorType);
       
   196 		
       
   197 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPV2_password_prompt_literal), default_EAP_MSCHAPV2_password_prompt);
       
   198 		
       
   199 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPV2_username_literal), default_EAP_MSCHAPV2_username);
       
   200 		
       
   201 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPV2_password_literal), default_EAP_MSCHAPV2_password);
       
   202 		
       
   203 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPv2_max_session_validity_time_literal), default_MaxSessionTime);
       
   204 		
       
   205 		view.SetColL(colSet->ColNo(KMSCHAPv2LastFullAuthTime), default_FullAuthTime);		
       
   206 		
       
   207 		view.PutL();
       
   208 				
       
   209 		CleanupStack::PopAndDestroy( colSet ); // Delete colSet.
       
   210 		
       
   211 		CleanupStack::PopAndDestroy( &view ); // Close view.		
       
   212 	} 
       
   213 	
       
   214 	CleanupStack::PopAndDestroy( buf ); // Delete buf	
       
   215 	CleanupStack::Pop( &aDatabase );	
       
   216 	CleanupStack::Pop( &aSession );	
       
   217 		
       
   218 	aDatabase.Compact();
       
   219 }
       
   220 
       
   221 
       
   222 void EapMsChapV2DbUtils::SetIndexL(
       
   223 	RDbNamedDatabase& aDatabase, 		
       
   224 	const TIndexType aIndexType,
       
   225 	const TInt aIndex,
       
   226 	const eap_type_value_e aTunnelingType,
       
   227 	const TIndexType aNewIndexType,
       
   228 	const TInt aNewIndex,
       
   229 	const eap_type_value_e aNewTunnelingType)
       
   230 {
       
   231 #ifdef USE_EAP_EXPANDED_TYPES
       
   232 
       
   233 	TUint aTunnelingVendorType = aTunnelingType.get_vendor_type();
       
   234 	TUint aNewTunnelingVendorType = aNewTunnelingType.get_vendor_type();
       
   235 
       
   236 #else
       
   237 
       
   238 	TUint aTunnelingVendorType = static_cast<TUint>(aTunnelingType);
       
   239 	TUint aNewTunnelingVendorType = static_cast<TUint>(aNewTunnelingType);
       
   240 
       
   241 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
   242 
       
   243 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   244 	TPtr sqlStatement = buf->Des();
       
   245 
       
   246 	_LIT(KSQL, "SELECT * FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   247 
       
   248 	sqlStatement.Format(KSQL, &KMsChapV2TableName, 
       
   249 		&KServiceType, aIndexType, &KServiceIndex, aIndex, &KTunnelingType, aTunnelingVendorType);
       
   250 	
       
   251 	RDbView view;
       
   252 	
       
   253 	User::LeaveIfError(view.Prepare(aDatabase, TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   254 	
       
   255 	// View must be closed when no longer needed
       
   256 	CleanupClosePushL(view);
       
   257 	
       
   258 	User::LeaveIfError(view.EvaluateAll());
       
   259 			
       
   260 	TInt rows = view.CountL();
       
   261 	
       
   262 	if (rows == 0)
       
   263 	{
       
   264 		User::Leave(KErrNotFound);
       
   265 	}
       
   266 	
       
   267 	// Get the first (and only) row
       
   268 	view.FirstL();
       
   269 	view.GetL();				
       
   270 	
       
   271 	// Get column set so we get the correct column numbers
       
   272 	CDbColSet* colSet = view.ColSetL();
       
   273 	
       
   274 	CleanupStack::PushL(colSet);
       
   275 	
       
   276 	view.UpdateL();
       
   277 	
       
   278     view.SetColL(colSet->ColNo(KServiceType), static_cast<TUint>(aNewIndexType));
       
   279     
       
   280     view.SetColL(colSet->ColNo(KServiceIndex), aNewIndex);
       
   281     
       
   282     view.SetColL(colSet->ColNo(KTunnelingType), aNewTunnelingVendorType);
       
   283 
       
   284     view.PutL();
       
   285     	
       
   286 	CleanupStack::PopAndDestroy(3); // view, colset, buf
       
   287 }
       
   288 
       
   289 void EapMsChapV2DbUtils::SetConfigurationL(
       
   290 	RDbNamedDatabase& aDatabase,
       
   291 	const EAPSettings& aSettings, 
       
   292 	const TIndexType aIndexType,
       
   293 	const TInt aIndex,
       
   294 	const eap_type_value_e aTunnelingType)
       
   295 {
       
   296 #ifdef USE_EAP_EXPANDED_TYPES
       
   297 
       
   298 	TUint aTunnelingVendorType = aTunnelingType.get_vendor_type();
       
   299 
       
   300 #else
       
   301 
       
   302 	TUint aTunnelingVendorType = static_cast<TUint>(aTunnelingType);
       
   303 
       
   304 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
   305 
       
   306 	// Check if the settings are for the correct type
       
   307 	if (aSettings.iEAPType != EAPSettings::EEapMschapv2 &&
       
   308 		aSettings.iEAPType != EAPSettings::EPlainMschapv2)
       
   309 	{
       
   310 		User::Leave(KErrNotSupported);
       
   311 	}
       
   312 	
       
   313 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   314 	TPtr sqlStatement = buf->Des();	
       
   315 
       
   316 	RDbView view;
       
   317 
       
   318 	_LIT(KSQLQuery, "SELECT * FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   319 	sqlStatement.Format(KSQLQuery, &KMsChapV2TableName, 
       
   320 		&KServiceType, aIndexType, &KServiceIndex, aIndex, &KTunnelingType, aTunnelingVendorType);
       
   321 	
       
   322 	// Evaluate view
       
   323 	User::LeaveIfError(view.Prepare(aDatabase, TDbQuery(sqlStatement)));
       
   324 	
       
   325 	CleanupClosePushL(view);
       
   326 	
       
   327 	User::LeaveIfError(view.EvaluateAll());	
       
   328 
       
   329 	view.FirstL();
       
   330 	
       
   331 	view.UpdateL();
       
   332 	
       
   333 	// Get column set so we get the correct column numbers
       
   334 	CDbColSet* colSet = view.ColSetL();
       
   335 	CleanupStack::PushL(colSet);
       
   336 
       
   337 	// Username
       
   338 	if (aSettings.iUsernamePresent)
       
   339 	{
       
   340 		// Validate length.
       
   341 		if(aSettings.iUsername.Length() > KMaxUsernameLengthInDB)
       
   342 		{
       
   343 			// Username too long. Can not be stored in DB.
       
   344 			
       
   345 			EAP_TRACE_DEBUG_SYMBIAN((_L("EapMsChapV2DbUtils::SetConfigurationL: Too long Username. Length=%d \n"),
       
   346 			aSettings.iUsername.Length()));
       
   347 			
       
   348 			User::Leave(KErrArgument);
       
   349 		}
       
   350 		
       
   351 		// Length is ok. Set the value in DB.
       
   352 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPV2_username_literal), aSettings.iUsername);		
       
   353 	}
       
   354 		
       
   355 	// Password
       
   356 	if (aSettings.iPasswordPresent)
       
   357 	{
       
   358 		// Validate length.
       
   359 		if(aSettings.iPassword.Length() > KMaxPasswordLengthInDB)
       
   360 		{
       
   361 			// Password too long. Can not be stored in DB.
       
   362 			
       
   363 			EAP_TRACE_DEBUG_SYMBIAN((_L("EapMsChapV2DbUtils::SetConfigurationL: Too long Password. Length=%d \n"),
       
   364 			aSettings.iPassword.Length()));
       
   365 			
       
   366 			User::Leave(KErrArgument);
       
   367 		}
       
   368 					
       
   369 		// Length is ok. Set the value in DB.	
       
   370 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPV2_password_literal), aSettings.iPassword);
       
   371 		
       
   372 		// If password was supplied set password prompting off
       
   373 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPV2_password_prompt_literal), EMSCHAPV2PasswordPromptOff);		
       
   374 	}
       
   375 			
       
   376 	// Session validity time
       
   377 	if (aSettings.iSessionValidityTimePresent)
       
   378 	{
       
   379 		// User or device management wants to store the session validity time.
       
   380 		// Convert the time to micro seconds and save.
       
   381 		
       
   382 		TInt64 validityInMicro = (aSettings.iSessionValidityTime) *  KMicroSecsInAMinute;
       
   383 		
       
   384 		view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPv2_max_session_validity_time_literal), validityInMicro);
       
   385 		
       
   386 		// If max session validity time is supplied and non-zero, set password prompting ON.
       
   387 		// It doesn't matter even if the password is supplied. If max session validity is supplied,
       
   388 		// it means user needs to provide a password hence prompt should appear.
       
   389 		
       
   390 		if( validityInMicro != 0)
       
   391 		{
       
   392 			view.SetColL(colSet->ColNo(cf_str_EAP_MSCHAPV2_password_prompt_literal), EMSCHAPV2PasswordPromptOn);
       
   393 		}		
       
   394 	}
       
   395 	
       
   396 	// Last full authentication time should be made zero when EAP configurations are modified.
       
   397 	// This makes sure that the next authentication with this EAP would be full authentication
       
   398 	// instead of reauthentication even if the session is still valid.
       
   399 	
       
   400 	view.SetColL(colSet->ColNo(KMSCHAPv2LastFullAuthTime), default_FullAuthTime);
       
   401 
       
   402 	EAP_TRACE_DEBUG_SYMBIAN((_L("Session Validity: EAP-Type=%d, Resetting Full Auth Time since settings are modified\n"),
       
   403 								aSettings.iEAPType ));
       
   404 	
       
   405 	view.PutL();
       
   406 	CleanupStack::PopAndDestroy(3); // view, colset, buf
       
   407 
       
   408 }
       
   409 
       
   410 void EapMsChapV2DbUtils::GetConfigurationL(
       
   411 	RDbNamedDatabase& aDatabase,
       
   412 	EAPSettings& aSettings, 
       
   413 	const TIndexType aIndexType,
       
   414 	const TInt aIndex,
       
   415 	const eap_type_value_e aTunnelingType)
       
   416 {
       
   417 #ifdef USE_EAP_EXPANDED_TYPES
       
   418 
       
   419 	TUint aTunnelingVendorType = aTunnelingType.get_vendor_type();
       
   420 
       
   421 #else
       
   422 
       
   423 	TUint aTunnelingVendorType = static_cast<TUint>(aTunnelingType);
       
   424 
       
   425 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
   426 
       
   427 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   428 	TPtr sqlStatement = buf->Des();	
       
   429 
       
   430 	RDbView view;
       
   431 
       
   432 	// Form the query
       
   433 	_LIT(KSQLQuery, "SELECT * FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   434 	sqlStatement.Format(KSQLQuery, &KMsChapV2TableName, 
       
   435 		&KServiceType, aIndexType, &KServiceIndex, aIndex, &KTunnelingType, aTunnelingVendorType);
       
   436 	
       
   437 	// Evaluate view
       
   438 	User::LeaveIfError(view.Prepare(aDatabase, TDbQuery(sqlStatement)));
       
   439 	
       
   440 	CleanupClosePushL(view);
       
   441 	
       
   442 	User::LeaveIfError(view.EvaluateAll());
       
   443 
       
   444 	// Get the first (and only) row
       
   445 	view.FirstL();
       
   446 	view.GetL();
       
   447 	
       
   448 	// Get column set so we get the correct column numbers
       
   449 	CDbColSet* colSet = view.ColSetL();
       
   450 	CleanupStack::PushL(colSet);
       
   451 
       
   452 	aSettings.iEAPType = EAPSettings::EEapMschapv2; 
       
   453 	
       
   454 	// Username
       
   455 	TPtrC username = view.ColDes(colSet->ColNo(cf_str_EAP_MSCHAPV2_username_literal));
       
   456 	aSettings.iUsername.Copy(username);
       
   457 	aSettings.iUsernamePresent = ETrue;
       
   458 	
       
   459 	// Password
       
   460 	TPtrC password = view.ColDes(colSet->ColNo(cf_str_EAP_MSCHAPV2_password_literal));
       
   461 	aSettings.iPassword.Copy(password);
       
   462 	aSettings.iPasswordPresent = ETrue;
       
   463 
       
   464 	// Session validity time	
       
   465 	TInt64 maxSessionTimeMicro = view.ColInt64(colSet->ColNo(cf_str_EAP_MSCHAPv2_max_session_validity_time_literal));
       
   466 	
       
   467 	// Convert the time to minutes.	
       
   468 	TInt64 maxSessionTimeMin = maxSessionTimeMicro / KMicroSecsInAMinute;
       
   469 	
       
   470 	aSettings.iSessionValidityTime = static_cast<TUint>(maxSessionTimeMin);
       
   471 	aSettings.iSessionValidityTimePresent = ETrue;
       
   472 		
       
   473 	CleanupStack::PopAndDestroy(3); // view, colset, buf
       
   474 }
       
   475 
       
   476 void EapMsChapV2DbUtils::CopySettingsL(
       
   477 	RDbNamedDatabase& aDatabase, 		
       
   478 	const TIndexType aSrcIndexType,
       
   479 	const TInt aSrcIndex,
       
   480 	const eap_type_value_e aSrcTunnelingType,
       
   481 	const TIndexType aDestIndexType,
       
   482 	const TInt aDestIndex,
       
   483 	const eap_type_value_e aDestTunnelingType)
       
   484 {
       
   485 #ifdef USE_EAP_EXPANDED_TYPES
       
   486 
       
   487 	TUint aSrcTunnelingVendorType = aSrcTunnelingType.get_vendor_type();
       
   488 	TUint aDestTunnelingVendorType = aDestTunnelingType.get_vendor_type();
       
   489 
       
   490 #else
       
   491 
       
   492 	TUint aSrcTunnelingVendorType = static_cast<TUint>(aSrcTunnelingType);
       
   493 	TUint aDestTunnelingVendorType = static_cast<TUint>(aDestTunnelingType);
       
   494 
       
   495 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
   496 
       
   497 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   498 	TPtr sqlStatement = buf->Des();
       
   499 
       
   500 	_LIT(KSQL, "SELECT * FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   501 
       
   502 	sqlStatement.Format(KSQL, &KMsChapV2TableName, 
       
   503 		&KServiceType, aSrcIndexType, &KServiceIndex, aSrcIndex, &KTunnelingType, aSrcTunnelingVendorType);
       
   504 	
       
   505 	RDbView view;
       
   506 	
       
   507 	User::LeaveIfError(view.Prepare(aDatabase, TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   508 	
       
   509 	// View must be closed when no longer needed
       
   510 	CleanupClosePushL(view);
       
   511 	
       
   512 	User::LeaveIfError(view.EvaluateAll());
       
   513 			
       
   514 	TInt rows = view.CountL();
       
   515 	
       
   516 	if (rows == 0)
       
   517 	{
       
   518 		User::Leave(KErrNotFound);
       
   519 	}
       
   520 	
       
   521 	// Get the first (and only) row
       
   522 	view.FirstL();
       
   523 	
       
   524 	view.GetL();
       
   525 		
       
   526 	view.InsertCopyL();
       
   527 	
       
   528 	// Get column set so we get the correct column numbers
       
   529 	CDbColSet* colSet = view.ColSetL();
       
   530 	
       
   531 	CleanupStack::PushL(colSet);
       
   532 		
       
   533 	view.SetColL(colSet->ColNo(KServiceType), static_cast<TUint>(aDestIndexType));
       
   534     
       
   535     view.SetColL(colSet->ColNo(KServiceIndex), aDestIndex);
       
   536     
       
   537     view.SetColL(colSet->ColNo(KTunnelingType), aDestTunnelingVendorType);
       
   538 
       
   539     view.PutL();
       
   540     	
       
   541 	CleanupStack::PopAndDestroy(3); // view, colset, buf
       
   542 }
       
   543 
       
   544 void EapMsChapV2DbUtils::DeleteConfigurationL(	
       
   545 	const TIndexType aIndexType,
       
   546 	const TInt aIndex,
       
   547 	const eap_type_value_e aTunnelingType)
       
   548 {
       
   549 #ifdef USE_EAP_EXPANDED_TYPES
       
   550 
       
   551 	TUint aTunnelingVendorType = aTunnelingType.get_vendor_type();
       
   552 
       
   553 #else
       
   554 
       
   555 	TUint aTunnelingVendorType = static_cast<TUint>(aTunnelingType);
       
   556 
       
   557 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
   558 
       
   559 	RDbs session;
       
   560 	RDbNamedDatabase database;
       
   561 	// Connect to the DBMS server.
       
   562 	User::LeaveIfError(session.Connect());
       
   563 	CleanupClosePushL(session);	
       
   564 		
       
   565 #ifdef SYMBIAN_SECURE_DBMS
       
   566 	
       
   567 	// Create the secure shared database with the specified secure policy.
       
   568 	// Database will be created in the data caging path for DBMS (C:\private\100012a5).
       
   569 	
       
   570 	TInt err = database.Create(session, KDatabaseName, KSecureUIDFormat);
       
   571 	
       
   572 	if(err == KErrNone)
       
   573 	{
       
   574 		// Database was created so it was empty. No need for further actions.
       
   575 		database.Destroy();
       
   576 		CleanupStack::PopAndDestroy();
       
   577 		return;
       
   578 		
       
   579 	} 
       
   580 	else if (err != KErrAlreadyExists) 
       
   581 	{
       
   582 		User::LeaveIfError(err);
       
   583 	}
       
   584 	
       
   585 	// Database existed, open it.
       
   586 	User::LeaveIfError(database.Open(session, KDatabaseName, KSecureUIDFormat));
       
   587 	CleanupClosePushL(database);
       
   588 		
       
   589 #else
       
   590 	// For non-secured database. The database will be created in the old location (c:\system\data).
       
   591 	
       
   592 	RFs fsSession;		
       
   593 	User::LeaveIfError(fsSession.Connect());
       
   594 	CleanupClosePushL(fsSession);	
       
   595 	TInt err = database.Create(fsSession, KDatabaseName);
       
   596 
       
   597 	if(err == KErrNone)
       
   598 	{
       
   599 		// Database was created so it was empty. No need for further actions.
       
   600 		database.Destroy();
       
   601 		CleanupStack::PopAndDestroy(2); // fsSession, database session
       
   602 		return;
       
   603 		
       
   604 	} 
       
   605 	else if (err != KErrAlreadyExists) 
       
   606 	{
       
   607 		User::LeaveIfError(err);
       
   608 	}
       
   609 	
       
   610 	CleanupStack::PopAndDestroy(); // close fsSession
       
   611 	
       
   612 	User::LeaveIfError(database.Open(session, KDatabaseName));
       
   613 	CleanupClosePushL(database);		
       
   614 	    
       
   615 #endif // #ifdef SYMBIAN_SECURE_DBMS
       
   616 
       
   617 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   618 	TPtr sqlStatement = buf->Des();
       
   619 
       
   620 	// Main settings table
       
   621 	_LIT(KSQL, "SELECT * FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   622 	sqlStatement.Format(KSQL, &KMsChapV2TableName, 
       
   623 		&KServiceType, aIndexType, &KServiceIndex, aIndex, &KTunnelingType, aTunnelingVendorType);
       
   624 	// Evaluate view
       
   625 	RDbView view;
       
   626 	User::LeaveIfError(view.Prepare(database,TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   627 	CleanupClosePushL(view);
       
   628 	User::LeaveIfError(view.EvaluateAll());
       
   629 
       
   630 	// Delete rows
       
   631 	if (view.FirstL())
       
   632 	{		
       
   633 		do {
       
   634 			view.DeleteL();
       
   635 		} while (view.NextL() != EFalse);
       
   636 	}
       
   637 
       
   638 	// Close database
       
   639 	CleanupStack::PopAndDestroy(4); // view, buf, database, session
       
   640 }
       
   641 
       
   642 // End of File