eapol/eapol_framework/eapol_symbian/am/type/securid/symbian/plugin/src/EapSecurIDDbUtils.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 349 
       
    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 "EapSecurIDDbUtils.h"
       
    31 #include "EapSecurIDDbDefaults.h"
       
    32 #include "EapSecurIDDbParameterNames.h"
       
    33 
       
    34 #include "eap_am_trace_symbian.h"
       
    35 
       
    36 const TUint KMaxSqlQueryLength = 512;
       
    37 
       
    38 // ================= MEMBER FUNCTIONS =======================
       
    39 
       
    40 void EapSecurIDDbUtils::OpenDatabaseL(RDbNamedDatabase& aDatabase, RDbs& aSession, const TIndexType aIndexType,
       
    41 	const TInt aIndex, const eap_type_value_e aTunnelingType)
       
    42 {
       
    43 #ifdef USE_EAP_EXPANDED_TYPES
       
    44 
       
    45 	TUint aTunnelingVendorType = aTunnelingType.get_vendor_type();
       
    46 
       
    47 #else
       
    48 
       
    49 	TUint aTunnelingVendorType = static_cast<TUint>(aTunnelingType);
       
    50 
       
    51 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
    52 
       
    53 	// 1. Open/create a database	
       
    54 	
       
    55 	// Connect to the DBMS server.
       
    56 	User::LeaveIfError(aSession.Connect());		
       
    57 	CleanupClosePushL(aSession);	
       
    58 	// aSession and aDatabase are pushed to the cleanup stack even though they may be member
       
    59 	// variables of the calling class and would be closed in the destructor anyway. This ensures
       
    60 	// that if they are not member variables they will be closed. Closing the handle twice
       
    61 	// does no harm.	
       
    62 	
       
    63 #ifdef SYMBIAN_SECURE_DBMS
       
    64 	
       
    65 	// Create the secure shared database with the specified secure policy.
       
    66 	// Database will be created in the data caging path for DBMS (C:\private\100012a5).
       
    67 	
       
    68 	TInt err = aDatabase.Create(aSession, KDatabaseName, KSecureUIDFormat);
       
    69 
       
    70 	EAP_TRACE_DEBUG_SYMBIAN((_L("EapSecurIDDbUtils::OpenDatabaseL - Created Secure DB for eapsecurid.dat. err=%d\n"), err));
       
    71 
       
    72 	
       
    73 	if(err == KErrNone)
       
    74 	{
       
    75 		aDatabase.Close();
       
    76 		
       
    77 	} else if (err != KErrAlreadyExists) 
       
    78 	{
       
    79 		User::LeaveIfError(err);
       
    80 	}
       
    81 	
       
    82 	User::LeaveIfError(aDatabase.Open(aSession, KDatabaseName, KSecureUIDFormat));
       
    83 	CleanupClosePushL(aDatabase);		
       
    84 		
       
    85 #else
       
    86 	// For non-secured database. The database will be created in the old location (c:\system\data).
       
    87 	
       
    88 	RFs fsSession;		
       
    89 	User::LeaveIfError(fsSession.Connect());
       
    90 	CleanupClosePushL(fsSession);	
       
    91 	TInt err = aDatabase.Create(fsSession, KDatabaseName);
       
    92 	
       
    93 	EAP_TRACE_DEBUG_SYMBIAN((_L("EapSecurIDDbUtils::OpenDatabaseL - Created Non-Secure DB for eapsecurid.dat. err=%d\n"), err));
       
    94 	
       
    95 	
       
    96 	if(err == KErrNone)
       
    97 	{
       
    98 		aDatabase.Close();
       
    99 		
       
   100 	} else if (err != KErrAlreadyExists) 
       
   101 	{
       
   102 		User::LeaveIfError(err);
       
   103 	}
       
   104 	CleanupStack::PopAndDestroy(); // close fsSession
       
   105 	
       
   106 	User::LeaveIfError(aDatabase.Open(aSession, KDatabaseName));
       
   107 	CleanupClosePushL(aDatabase);		
       
   108 	    
       
   109 #endif // #ifdef SYMBIAN_SECURE_DBMS
       
   110 
       
   111 	// 2. Create the eap-securid table to database (ignore error if exists)
       
   112 	// Table columns:
       
   113 	//// NAME ///////////////////////////////////////////////// TYPE ////////////// Constant /////////
       
   114 	//| ServiceType											| UNSIGNED INTEGER | KServiceType      |//
       
   115 	//| ServiceIndex										| UNSIGNED INTEGER | KServiceIndex     |//
       
   116 	//| TunnelingType										| UNSIGNED INTEGER | KTunnelingType    |//
       
   117 	//| EAP_SECURID_identity				        		| VARCHAR(255)     | cf_str_EAP_SECURID_identity_literal         |//
       
   118 	//////////////////////////////////////////////////////////////////////////////////////////////////
       
   119 
       
   120 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   121 	TPtr sqlStatement = buf->Des();
       
   122 
       
   123 	_LIT(KSQLCreateTable1, "CREATE TABLE %S (%S UNSIGNED INTEGER, \
       
   124 											 %S UNSIGNED INTEGER, \
       
   125 											 %S UNSIGNED INTEGER, \
       
   126 											 %S VARCHAR(255))");
       
   127 	sqlStatement.Format(KSQLCreateTable1, &KSecurIDTableName, &KServiceType, &KServiceIndex, &KTunnelingType, &cf_str_EAP_SECURID_identity_literal);
       
   128 	err = aDatabase.Execute(sqlStatement);
       
   129 	if (err != KErrNone && err != KErrAlreadyExists)
       
   130 	{
       
   131 		User::Leave(err);
       
   132 	}
       
   133 
       
   134 	// 4. Check if database table contains a row for this service type and id  
       
   135 	
       
   136 	_LIT(KSQLQueryRow, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   137 	sqlStatement.Format(KSQLQueryRow, &cf_str_EAP_SECURID_identity_literal, &KSecurIDTableName, 
       
   138 		&KServiceType, aIndexType, &KServiceIndex, aIndex, &KTunnelingType, aTunnelingVendorType);
       
   139 	RDbView view;
       
   140 	User::LeaveIfError(view.Prepare(aDatabase, TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   141 	// View must be closed when no longer needed
       
   142 	CleanupClosePushL(view);
       
   143 	User::LeaveIfError(view.EvaluateAll());
       
   144 	// 5. If row is not found then add it
       
   145 	TInt rows = view.CountL();
       
   146 	CleanupStack::PopAndDestroy(); // view
       
   147 	if (rows == 0)
       
   148 	{
       
   149 		_LIT(KSQLInsert, "SELECT * FROM %S");
       
   150 		sqlStatement.Format(KSQLInsert, &KSecurIDTableName);		
       
   151 		
       
   152 		view.Prepare(aDatabase, TDbQuery(sqlStatement), TDbWindow::EUnlimited, RDbView::EInsertOnly);
       
   153 		CleanupClosePushL(view);
       
   154 		
       
   155 		// Get column set so we get the correct column numbers
       
   156 		CDbColSet* colSet = view.ColSetL();		
       
   157 		CleanupStack::PushL(colSet);
       
   158 		
       
   159 		view.InsertL();
       
   160 		view.SetColL(colSet->ColNo(KServiceType), static_cast<TInt> (aIndexType));
       
   161 		view.SetColL(colSet->ColNo(KServiceIndex), aIndex);
       
   162 		view.SetColL(colSet->ColNo(KTunnelingType), aTunnelingVendorType);
       
   163 		view.SetColL(colSet->ColNo(cf_str_EAP_SECURID_identity_literal), default_EAP_SECURID_identity);
       
   164 		view.PutL();
       
   165 		
       
   166 		CleanupStack::PopAndDestroy( colSet ); // Delete colSet.
       
   167 		
       
   168 		CleanupStack::PopAndDestroy( &view ); // Close view.
       
   169 	} 
       
   170 	
       
   171 	CleanupStack::PopAndDestroy(); // sqlStatement
       
   172 	CleanupStack::Pop(2); // database, session
       
   173 	aDatabase.Compact();
       
   174 }
       
   175 
       
   176 // End of File