vpnengine/ikev1lib/src/credentialcache.cpp
branchRCL_3
changeset 49 5960d2d03390
parent 46 29c8f9bc68e1
child 51 1bffc46e983d
equal deleted inserted replaced
46:29c8f9bc68e1 49:5960d2d03390
     1  /*
       
     2 * Copyright (c) 2010 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:  Cache for authentication credentials
       
    15 *
       
    16 */
       
    17 
       
    18 #include <f32file.h>
       
    19 
       
    20 #include "credentialcache.h"
       
    21 #include "ikedebug.h"
       
    22 #include "cmutils.h"
       
    23 
       
    24 
       
    25 _LIT( KFileName, "cache" );
       
    26 
       
    27 
       
    28 CCredentialCache* CCredentialCache::NewL( MIkeDebug& aDebug )
       
    29 {
       
    30     CCredentialCache* cache = new (ELeave) CCredentialCache( aDebug );
       
    31 
       
    32     CleanupStack::PushL( cache );
       
    33 
       
    34     cache->ConstructL();
       
    35 
       
    36     CleanupStack::Pop();
       
    37 
       
    38     return cache;
       
    39 }
       
    40 
       
    41 
       
    42 CCredentialCache::CCredentialCache( MIkeDebug& aDebug )
       
    43   : iDebug( aDebug )
       
    44 {
       
    45 }
       
    46 
       
    47 
       
    48 void CCredentialCache::ConstructL()
       
    49 {
       
    50     User::LeaveIfError( iFs.Connect() );
       
    51         
       
    52     DEBUG_LOG( _L( "CCredentialCache::ConstructL" ) );
       
    53 }
       
    54 
       
    55 
       
    56 CCredentialCache::~CCredentialCache()
       
    57 {
       
    58     iFs.Close();
       
    59 }
       
    60 
       
    61 
       
    62 void CCredentialCache::SetUserName( const TDesC8& aUser )
       
    63 {
       
    64     if( KErrNone != CheckCredential( aUser ) )
       
    65     {
       
    66         return;   
       
    67     }
       
    68 
       
    69     iBuf.iUser.Copy( aUser );
       
    70 }
       
    71 
       
    72 
       
    73 void CCredentialCache::SetSecret( const TDesC8& aSecret )
       
    74 {
       
    75     if( KErrNone != CheckCredential( aSecret ) )
       
    76     {
       
    77         return;   
       
    78     }
       
    79 
       
    80     iBuf.iSecret.Copy( aSecret );
       
    81 }
       
    82 
       
    83 
       
    84 TInt CCredentialCache::GetCredentials(
       
    85     const TUint32 aVpnApId, HBufC8*& aUser, HBufC8*& aSecret )
       
    86 {
       
    87     TInt ret = KErrNone;
       
    88     
       
    89     TRAPD( err, ret = GetCredentialsL( aVpnApId, aUser, aSecret ) );
       
    90     
       
    91     if( KErrNone != err )
       
    92     {
       
    93         DEBUG_LOG1( _L("CCredentialCache::GetCredentials, err=%d"), err );
       
    94         return err;
       
    95     }
       
    96     
       
    97     return ret;
       
    98 }
       
    99 
       
   100 
       
   101 TInt CCredentialCache::GetCredentialsL(
       
   102     const TUint32 aVpnApId, HBufC8*& aUser, HBufC8*& aSecret )
       
   103 {
       
   104     TInt ret = ReadFile();
       
   105 
       
   106     if( KErrNone != ret )
       
   107     {
       
   108         return ret;   
       
   109     }
       
   110 
       
   111     TVpnPolicyId id;
       
   112     
       
   113     CmUtils::GetPolicyIdL( aVpnApId, id );
       
   114 
       
   115     if( id != iBuf.iId )
       
   116     {
       
   117         DEBUG_LOG1(
       
   118             _L("CCredentialCache::GetCredentialsL, pol=%S"), &iBuf.iId
       
   119         );
       
   120         
       
   121         return KErrNotFound;
       
   122     }
       
   123 
       
   124     aUser   = iBuf.iUser.AllocL();
       
   125     aSecret = iBuf.iSecret.AllocL();
       
   126     
       
   127     return KErrNone;
       
   128 }
       
   129 
       
   130 
       
   131 void CCredentialCache::Store( const TUint32 aVpnApId )
       
   132 {
       
   133     TRAPD( err, StoreL( aVpnApId ) );
       
   134     
       
   135     if( KErrNone != err )
       
   136     {
       
   137         DEBUG_LOG1( _L("CCredentialCache::Store, err=%d"), err );
       
   138     }
       
   139 }
       
   140 
       
   141 
       
   142 void CCredentialCache::StoreL( const TUint32 aVpnApId )
       
   143 {
       
   144     CmUtils::GetPolicyIdL( aVpnApId, iBuf.iId );
       
   145 
       
   146     StoreToFileL();
       
   147 }
       
   148 
       
   149 
       
   150 void CCredentialCache::Clear()
       
   151 {
       
   152     TInt ret = CreateFileNameAndPath();
       
   153     
       
   154     if( KErrNone != ret )
       
   155     {
       
   156         return;
       
   157     }
       
   158 
       
   159     ret = iFs.Delete( iFileName );
       
   160     
       
   161     if( KErrNone != ret )
       
   162     {
       
   163         DEBUG_LOG1( _L("CCredentialCache::Clear, ret=%d"), ret );
       
   164     }
       
   165 }
       
   166 
       
   167 
       
   168 TInt CCredentialCache::CheckCredential( const TDesC8& cr )
       
   169 {
       
   170     TInt len = cr.Length();
       
   171     
       
   172     if( 0 == len || KCredentialMaxLen < len )
       
   173     {
       
   174         DEBUG_LOG1( _L("CCredentialCache::CheckCredential, len=%d"), len );
       
   175         return KErrArgument;
       
   176     }
       
   177 
       
   178     return KErrNone;
       
   179 }
       
   180 
       
   181 
       
   182 void CCredentialCache::StoreToFileL()
       
   183 {
       
   184     RFile cache;
       
   185 
       
   186     User::LeaveIfError( CreateFileNameAndPath() );
       
   187 
       
   188     User::LeaveIfError( cache.Replace(
       
   189         iFs, iFileName, EFileShareExclusive | EFileWrite
       
   190     ) );
       
   191 
       
   192     CleanupClosePushL( cache );
       
   193 
       
   194     TPckg< TCacheBuffer > data( iBuf );
       
   195 
       
   196     User::LeaveIfError( cache.Write( data ) );
       
   197 
       
   198     CleanupStack::PopAndDestroy( &cache );
       
   199 }
       
   200 
       
   201 
       
   202 TInt CCredentialCache::ReadFile()
       
   203 {
       
   204     RFile cache;
       
   205     
       
   206     TInt ret = cache.Open( iFs, KFileName, EFileRead );
       
   207     
       
   208     if( KErrNone != ret )
       
   209     {
       
   210       return ret;   
       
   211     }
       
   212 
       
   213     TInt size = 0;
       
   214     
       
   215     ret = cache.Size( size );
       
   216 
       
   217     TPckg< TCacheBuffer > data( iBuf );
       
   218 
       
   219     if( size != data.Size() )
       
   220     {
       
   221         DEBUG_LOG1( _L("CCredentialCache::ReadFile, size=%d"), size );
       
   222         cache.Close();
       
   223         Clear();
       
   224         return KErrCorrupt;
       
   225     }
       
   226 
       
   227     ret = cache.Read( data );
       
   228 
       
   229     cache.Close();
       
   230     
       
   231     return ret;
       
   232 }
       
   233 
       
   234 
       
   235 TInt CCredentialCache::CreateFileNameAndPath()
       
   236 {
       
   237     TInt ret = iFs.CreatePrivatePath( RFs::GetSystemDrive() );
       
   238 
       
   239     if( KErrNone          != ret &&
       
   240         KErrAlreadyExists != ret )
       
   241     {
       
   242         DEBUG_LOG1( _L("CCredentialCache, CreatePrivatePath ret=%d"), ret );
       
   243         return ret;
       
   244     }
       
   245  
       
   246     ret = iFs.PrivatePath( iFileName );
       
   247 
       
   248     if( KErrNone != ret )
       
   249     {
       
   250         DEBUG_LOG1( _L("CCredentialCache, PrivatePath ret=%d"), ret );
       
   251         return ret;
       
   252     }
       
   253 
       
   254     iFileName.Append( KFileName );
       
   255     
       
   256     return KErrNone;
       
   257 }
       
   258 
       
   259 
       
   260 /***/