upnpsharing/upnpsecurity/src/server/upnpsecuritymanagerengine.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /** @file   
       
     2  * Copyright (c) 2009 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:  Defines CUpnpSecurityManagerEngine
       
    15  *
       
    16  */
       
    17 // INCLUDES
       
    18 #include <e32property.h> 
       
    19 #include "upnpsecuritymanagerengine.h"
       
    20 #include "upnpsecuritydbconnection.h"
       
    21 _LIT( KComponentLogfile, "upnpsecurityserver.txt");
       
    22 #include "upnplog.h"
       
    23 
       
    24 // ============================= MEMBER FUNCTIONS ==============================
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CUpnpSecurityManagerEngine::CUpnpSecurityManagerEngine
       
    28 // C++ default constructor can NOT contain any code, that
       
    29 // might leave.
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CUpnpSecurityManagerEngine::CUpnpSecurityManagerEngine()
       
    33     {
       
    34     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::\
       
    35 CUpnpSecurityManagerEngine" );
       
    36     }
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CUpnpSecurityManagerEngine::ConstructL
       
    40 // Symbian 2nd phase constructor can leave.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 void CUpnpSecurityManagerEngine::ConstructL()
       
    44     {
       
    45     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::ConstructL" );
       
    46 
       
    47     // create database connection object
       
    48     iDbConnection = CUpnpSecurityDbConnection::NewL();
       
    49     iDbConnection->OpenDatabaseL();
       
    50 
       
    51     // Clean the access lists after phone reboot 
       
    52     ResetListsOnFirstRunL();
       
    53 
       
    54     // load lists from database
       
    55     iDbConnection->GetAllFilenamesL( iAllowedFiles );
       
    56     iDbConnection->GetAllIpAddressesL( iAllowedAddresses );
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CUpnpSecurityManagerEngine::ResetListsOnFirstRunL
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void CUpnpSecurityManagerEngine::ResetListsOnFirstRunL()
       
    64     {
       
    65     // create RProperty used as reboot marker
       
    66     TInt err = RProperty::Define( RProcess().SecureId(), EUPnPSecManFirstRun,
       
    67         RProperty::EInt );
       
    68     if ( err != KErrAlreadyExists )
       
    69         {
       
    70         // property wasn't defined, so it's the first run and we clear lists
       
    71         User::LeaveIfError( err ); // other errors may occur
       
    72 
       
    73         // clear lists
       
    74         ResetFileListL();
       
    75         ResetAddressListL();
       
    76         }
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CUpnpSecurityManagerEngine::NewL
       
    81 // Two-phased constructor.
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 CUpnpSecurityManagerEngine* CUpnpSecurityManagerEngine::NewL()
       
    85     {
       
    86     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::NewL" );
       
    87 
       
    88     CUpnpSecurityManagerEngine* self =
       
    89             new (ELeave) CUpnpSecurityManagerEngine();
       
    90     CleanupStack::PushL( self );
       
    91     self->ConstructL();
       
    92     CleanupStack::Pop( self );
       
    93     return self;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CUpnpSecurityManagerEngine::CUpnpSecurityManagerEngine
       
    98 // Destructor.
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CUpnpSecurityManagerEngine::~CUpnpSecurityManagerEngine()
       
   102     {
       
   103     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::\
       
   104 ~CUpnpAuthorisationNotifier" );
       
   105 
       
   106     // Reset whitelists
       
   107     iAllowedFiles.ResetAndDestroy();
       
   108     iAllowedAddresses.Reset();
       
   109 
       
   110     delete iDbConnection;
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CUpnpSecurityManagerEngine::ResetFileListL
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 void CUpnpSecurityManagerEngine::ResetFileListL()
       
   118     {
       
   119     iAllowedFiles.ResetAndDestroy();
       
   120     iDbConnection->DeleteAllFilenamesL();
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CUpnpSecurityManagerEngine::ResetAddressListL
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 void CUpnpSecurityManagerEngine::ResetAddressListL()
       
   128     {
       
   129     iAllowedAddresses.Reset();
       
   130     iDbConnection->DeleteAllIpAddressesL();
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CUpnpSecurityManagerEngine::CheckAuthorizationL
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 TAccessType CUpnpSecurityManagerEngine::CheckAuthorization(
       
   138     const TInetAddr& aIpAddress, const TDesC& aFileName )
       
   139     {
       
   140     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::CheckAuthorization" );
       
   141 
       
   142     TAccessType accessType = ENoneAllowed;
       
   143 
       
   144     // Check the lists 
       
   145     TInt addressFound = KErrNotFound;
       
   146     if ( aIpAddress != TInetAddr( KInetAddrNone ) )
       
   147         {
       
   148         addressFound = FindAddressFromArray( aIpAddress );
       
   149         }
       
   150     TInt fileFound = KErrNotFound;
       
   151     if ( aFileName.Length() > 0 )
       
   152         {
       
   153         fileFound = FindFileFromArray( aFileName );
       
   154         }
       
   155 
       
   156     // Neither address or file was not
       
   157     if ( (addressFound == KErrNotFound) && (fileFound == KErrNotFound) )
       
   158         {
       
   159         accessType = ENoneAllowed;
       
   160         }
       
   161     // Address could be found but file not
       
   162     else if ( (addressFound >= 0) && (fileFound == KErrNotFound) )
       
   163         {
       
   164         accessType = EAddressAllowed;
       
   165         }
       
   166     // File could be found but not address
       
   167     else if ( (addressFound == KErrNotFound) && (fileFound >= 0) )
       
   168         {
       
   169         accessType = EFileAllowed;
       
   170         }
       
   171     // Both were in the white lists
       
   172     else
       
   173         {
       
   174         accessType = EFileAndAddressAllowed;
       
   175         }
       
   176 
       
   177     return accessType;
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CUpnpSecurityManagerEngine::AddNewFileL
       
   182 // Adds new file to allowed file array, if not existing yet.
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CUpnpSecurityManagerEngine::AddNewFileL( const TDesC& aFileName )
       
   186     {
       
   187     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::AddNewFileL" );
       
   188 
       
   189     if ( FindFileFromArray( aFileName ) == KErrNotFound )
       
   190         {
       
   191         HBufC* tmpBuffer = aFileName.AllocLC();
       
   192         iAllowedFiles.AppendL( tmpBuffer );
       
   193         CleanupStack::Pop( tmpBuffer );
       
   194         iDbConnection->AddFilenameL( aFileName );
       
   195         }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CUpnpSecurityManagerEngine::RemoveFileL
       
   200 // Removes file from allowed array.
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void CUpnpSecurityManagerEngine::RemoveFileL( const TDesC& aFileName )
       
   204     {
       
   205     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::RemoveFileL" );
       
   206 
       
   207     TInt index = FindFileFromArray( aFileName );
       
   208     if ( index != KErrNotFound )
       
   209         {
       
   210         __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine, \
       
   211 file removed from whitelist." );
       
   212         delete iAllowedFiles[index];
       
   213         iAllowedFiles.Remove( index );
       
   214         iDbConnection->DeleteFilenameL( aFileName );
       
   215         }
       
   216     else
       
   217         {
       
   218         __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine, \
       
   219 FILE NOT FOUND!" );
       
   220         User::Leave( KErrNotFound );
       
   221         }
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CUpnpSecurityManagerEngine::FindFileFromArray
       
   226 // Finds file from array and returns the index.
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 TInt CUpnpSecurityManagerEngine::FindFileFromArray( const TDesC& aFileName )
       
   230     {
       
   231     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::\
       
   232 FindFileFromArray" );
       
   233 
       
   234     TInt retVal = KErrNotFound;
       
   235     for ( TInt index = 0; index < iAllowedFiles.Count(); ++index )
       
   236         {
       
   237         // Check if file names are the same
       
   238         // Use folding to remove case sensitiveness
       
   239         if ( aFileName.CompareF( *iAllowedFiles[index] ) == 0 )
       
   240             {
       
   241             retVal = index;
       
   242             break;
       
   243             }
       
   244         }
       
   245 
       
   246     __LOG2( "CUpnpSecurityManagerEngine::FindFileFromArray(%S) retVal=%d",
       
   247             &aFileName, retVal );
       
   248 
       
   249     return retVal;
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CUpnpSecurityManagerEngine::AddNewAddressL
       
   254 // Adds new address to allowed address array, if not existing yet.
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 void CUpnpSecurityManagerEngine::AddNewAddressL( const TInetAddr& aIpAddress )
       
   258     {
       
   259     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::AddNewAddressL" );
       
   260 
       
   261     if ( FindAddressFromArray( aIpAddress ) == KErrNotFound )
       
   262         {
       
   263         iAllowedAddresses.AppendL( aIpAddress );
       
   264         iDbConnection->AddIpAddressL( aIpAddress );
       
   265         }
       
   266     }
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CUpnpSecurityManagerEngine::RemoveAddressL
       
   270 // Removes existing address from IP address array.
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 void CUpnpSecurityManagerEngine::RemoveAddressL( const TInetAddr& aIpAddress )
       
   274     {
       
   275     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::RemoveAddressL" );
       
   276 
       
   277     TInt index = FindAddressFromArray( aIpAddress );
       
   278     if ( index != KErrNotFound )
       
   279         {
       
   280         iAllowedAddresses.Remove( index );
       
   281         iDbConnection->DeleteIpAddressL( aIpAddress );
       
   282         __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine, \
       
   283 address remove from array." );
       
   284         }
       
   285     else
       
   286         {
       
   287         __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine, \
       
   288 address NOT found from the array!" );
       
   289         User::Leave( KErrNotFound );
       
   290         }
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CUpnpSecurityManagerEngine::FindAddressFromArray
       
   295 // Finds file from array and returns the index.
       
   296 // -----------------------------------------------------------------------------
       
   297 //
       
   298 TInt CUpnpSecurityManagerEngine::FindAddressFromArray(
       
   299     const TInetAddr& aIpAddress )
       
   300     {
       
   301     __LOG( "[UpnpSecurity]\t CUpnpSecurityManagerEngine::\
       
   302 FindAddressFromArray" );
       
   303 
       
   304     TInt retVal = KErrNotFound;
       
   305     for ( TInt index = 0; index < iAllowedAddresses.Count(); ++index )
       
   306         {
       
   307         // Compare only IP Address
       
   308         if ( iAllowedAddresses[index].Address() == aIpAddress.Address() )
       
   309             {
       
   310             retVal = index;
       
   311             break;
       
   312             }
       
   313         }
       
   314 
       
   315     TFileName ipAddress;
       
   316     aIpAddress.Output( ipAddress );
       
   317     __LOG2( "CUpnpSecurityManagerEngine::FindAddressFromArray(%S) retVal=%d",
       
   318             &ipAddress, retVal );
       
   319 
       
   320     return retVal;
       
   321     }
       
   322 
       
   323 // End of File