webservices/wsidentitymanager/src/senbaseidentitymanager.cpp
changeset 0 62f9d29f7211
child 21 6f5ef5fc65b4
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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:           
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 #include <s32mem.h>
       
    29 #include <s32file.h>
       
    30 #include <sysutil.h>
       
    31 
       
    32 #include <SenBaseFragment.h>
       
    33 #include <SenXmlUtils.h>
       
    34 
       
    35 #include "msenidentitymanager.h"
       
    36 #include "msenidentity.h"
       
    37 #include "senbaseidentitymanager.h"
       
    38 #include "sensecuritymechanismobserver.h"
       
    39 #include "senlogger.h"
       
    40 #include "sensecuritymechanism.h"  // internal  Framework\inc - ECOM interface
       
    41 
       
    42 #include "senservicemanagerdefines.h" // KMaxTicks (1209600000)
       
    43                                       // 1 tick = 1 millisec
       
    44                                       // 86400 sec = 1 day
       
    45                                       // 86400*1000 millisec = 1 day
       
    46                                       // 86400*14*1000 = 1209600000 millisec = 2 weeks
       
    47 
       
    48 #include "sendebug.h" // internal Utils\inc - filelogging MACROs
       
    49 #include "sennotplugindlg.h" // internal NotifierPlugin\inc, KSenNotifierPluginUID
       
    50 
       
    51 #include "senserviceinvocationframework.h"  // internal Framework\inc (framework event codes)
       
    52 
       
    53 #ifndef __WINS__ // only required in release builds
       
    54 // For reading IMEI. NOTE: not found from Series 60 2.1 public SDK
       
    55 //Since plpvariant.h is depricated in TB9.2 from wk48 2009 on words.
       
    56 //provided alternative solution  
       
    57 //#include <plpvariant.h>
       
    58 #endif
       
    59 #include<SenBaseAttribute.h>
       
    60 
       
    61 namespace
       
    62     {
       
    63     //_LIT8(KSenBaseIdentityManagerPanic, "SenBaseIdentityManager");
       
    64 
       
    65 #if !defined( EKA2 ) && !defined( RD_SECURE_PRIV_DATA )
       
    66     _LIT(KIdentityFile, "C:\\system\\data\\senidentities.xml");
       
    67 #else
       
    68     _LIT(KIdentityFile, "senidentities.xml");
       
    69 #endif
       
    70 
       
    71     _LIT8(KSpace, " ");
       
    72     _LIT8(KUserName, "User");
       
    73     _LIT8(KUsersStart, "<Users><User>");
       
    74     _LIT8(KUsersEnd, "</User></Users>");
       
    75     _LIT8(KIdentityProvider, "IdentityProvider");
       
    76     _LIT8(KTouch, "touch");
       
    77 
       
    78     const TInt KFLATBUF_SIZE = 128;
       
    79     }
       
    80 
       
    81 
       
    82 EXPORT_C CSenBaseIdentityManager* CSenBaseIdentityManager::NewL(
       
    83                                             MSenCoreServiceManager& aManager)
       
    84     {
       
    85     CSenBaseIdentityManager* pNew = NewLC(aManager);
       
    86     CleanupStack::Pop();
       
    87     return(pNew) ;
       
    88     }
       
    89 
       
    90 EXPORT_C CSenBaseIdentityManager* CSenBaseIdentityManager::NewLC(
       
    91                                             MSenCoreServiceManager& aManager)
       
    92     {
       
    93     CSenBaseIdentityManager* pNew =
       
    94                         new (ELeave) CSenBaseIdentityManager(aManager);
       
    95     CleanupStack::PushL(pNew);
       
    96     pNew->ConstructL();
       
    97     return pNew;
       
    98     }
       
    99 
       
   100 CSenBaseIdentityManager::CSenBaseIdentityManager(
       
   101                                             MSenCoreServiceManager& aManager)
       
   102 :   iIdentity(NULL),
       
   103     iProvider(NULL),
       
   104     iManager(aManager),
       
   105     iConfigReadonly(EFalse),
       
   106     iLog((RFileLogger*)aManager.Log()),
       
   107     iObserver(NULL),
       
   108     ipMechanicsNames(NULL),
       
   109     ipDeviceID(NULL),
       
   110     iShowPasswordDialog(EFalse)
       
   111     {
       
   112     }
       
   113 
       
   114 EXPORT_C CSenBaseIdentityManager::~CSenBaseIdentityManager()
       
   115     {
       
   116     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CSenBaseIdentityManager::~CSenBaseIdentityManager");        
       
   117     iEcomInfoArray.ResetAndDestroy();
       
   118     iSechMechNames.ResetAndDestroy();
       
   119     iSechMechCues.ResetAndDestroy();
       
   120     delete ipMechanicsNames;
       
   121     delete iObserver;
       
   122     
       
   123     delete iProvider;
       
   124     delete ipDeviceID;
       
   125     }
       
   126 
       
   127 
       
   128 void CSenBaseIdentityManager::ConstructL()
       
   129     {
       
   130     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CSenBaseIdentityManager::ConstructL - Version 2 [2006-05-09]");        
       
   131     BaseConstructL(KIdentityElementLocalName);
       
   132 
       
   133 
       
   134     delete ipDeviceID;
       
   135     ipDeviceID=NULL;
       
   136 
       
   137     #ifndef __WINS__
       
   138 	//Since plpvariant.h is depricated in TB9.2 from wk48 2009 on words.
       
   139 	//provided alternative solution  
       
   140     /*
       
   141     // This only works on target machine
       
   142     TPlpVariantMachineId imei;
       
   143     PlpVariant::GetMachineIdL(imei);
       
   144 
       
   145 
       
   146     ipDeviceID = SenXmlUtils::ToUtf8LC(imei);
       
   147     CleanupStack::Pop(); // ipDeviceID
       
   148 	*/
       
   149 	TPtrC8 imei ;
       
   150 	imei.Set(iManager.IMEI());
       
   151 	ipDeviceID = imei.AllocL();
       
   152 		
       
   153     #else
       
   154     // Return a fake IMEI when working on emulator
       
   155     _LIT8(KEmulatorImei, "012345678901234");
       
   156     ipDeviceID = KEmulatorImei().AllocL();
       
   157     #endif
       
   158 
       
   159     // Device ID is now resolved, fake or not
       
   160     TPtr8 imeiTPtr = ipDeviceID->Des();
       
   161     iIdentity = CSenBaseIdentity::NewL(imeiTPtr);
       
   162 
       
   163     HBufC* file = KIdentityFile().AllocL();
       
   164     CleanupStack::PushL(file);
       
   165     LoadFromL(*file, *iManager.XMLReader());
       
   166 
       
   167     // ValidateDefaultIdentity()
       
   168     ReloadSenSecurityMechanismsL();
       
   169     iObserver = CSenSenSecurityMechanismObserver::NewL(*this);
       
   170     iObserver->Start();
       
   171     CleanupStack::PopAndDestroy(); // file
       
   172     }
       
   173 
       
   174 RFileLogger* CSenBaseIdentityManager::Log()
       
   175     {
       
   176     return iLog;
       
   177     }
       
   178 
       
   179 void CSenBaseIdentityManager::LoadFromL(TDesC& aFile,
       
   180                                                  CSenXmlReader& aReader)
       
   181     {
       
   182     RFs fss;
       
   183     User::LeaveIfError(fss.Connect());
       
   184     CleanupClosePushL(fss);
       
   185     
       
   186     aReader.SetContentHandler(*this);
       
   187     SetReader(aReader);
       
   188 
       
   189     TInt leaveCode(KErrNone);
       
   190 
       
   191 #if defined( EKA2 ) || defined( RD_SECURE_PRIV_DATA )
       
   192     TBuf<KMaxPath> file;
       
   193     fss.CreatePrivatePath(EDriveC);
       
   194     fss.PrivatePath(file);
       
   195     file.Append(aFile);
       
   196     TRAP(leaveCode, aReader.ParseL(fss, file));
       
   197 #else
       
   198     TRAP(leaveCode, aReader.ParseL(fss, aFile));
       
   199 #endif
       
   200 
       
   201 #ifdef _SENDEBUG
       
   202     if(leaveCode == KErrNotFound)
       
   203         {
       
   204         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"- The senidentities.xml does not exist!");
       
   205         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"- New file will be generated.");
       
   206         }
       
   207     else if(leaveCode != KErrNone)
       
   208         {
       
   209         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("- Parsing of senidentities.xml leaved: %d"), leaveCode));
       
   210         }   
       
   211     else
       
   212         {
       
   213         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"- Identity database (senidentities.xml) successfully read.");
       
   214         }
       
   215 #else
       
   216     leaveCode=0; // this is a decision: we are not interested 
       
   217                  // of invalid input in read/parse phase
       
   218 #endif
       
   219 
       
   220     WriteDatabase(); // Always validate by saving
       
   221 
       
   222     CleanupStack::PopAndDestroy(); // fss
       
   223     }
       
   224 
       
   225 EXPORT_C TInt CSenBaseIdentityManager::WriteDatabase()
       
   226     {
       
   227     TInt retVal(KErrNone);
       
   228     TRAP(retVal, WriteConfigurationToL(KIdentityFile));
       
   229 
       
   230     return retVal;
       
   231     }
       
   232 
       
   233 void CSenBaseIdentityManager::StartElementL(
       
   234                                           const TDesC8& aNsUri,
       
   235                                           const TDesC8& aLocalName,
       
   236                                           const TDesC8& aQName,
       
   237                                           const RAttributeArray& aAttributes
       
   238                                                   )
       
   239     {
       
   240     if (aLocalName ==  KIdentityElementLocalName)
       
   241         {
       
   242         delete iIdentity;   // there can be only one at the moment
       
   243         iIdentity = NULL;
       
   244         TPtr8 imei = ipDeviceID->Des();
       
   245         iIdentity = CSenBaseIdentity::NewL(imei);
       
   246         iIdentity->SetAttributesL(aAttributes);
       
   247         iIdentity->SetReader(*Reader());
       
   248         DelegateParsingL(*iIdentity);
       
   249         }
       
   250 //  else if (aLocalName == KUsers)
       
   251 //      {
       
   252 //      }
       
   253     else
       
   254         {
       
   255         CSenBaseFragment::StartElementL(aNsUri, 
       
   256                                     aLocalName, aQName, aAttributes);
       
   257         }
       
   258     }
       
   259 
       
   260 MSenIdentity& CSenBaseIdentityManager::IdentityL()
       
   261     {
       
   262     return (MSenIdentity&)*iIdentity;
       
   263     }
       
   264 
       
   265 TInt CSenBaseIdentityManager::UserNameL(HBufC8*& aUserName)
       
   266     {
       
   267     delete aUserName;
       
   268     aUserName = NULL;
       
   269     aUserName = KUserName().AllocL();
       
   270     return KErrNone;
       
   271     }
       
   272 
       
   273 CSenIdentityProvider* CSenBaseIdentityManager::IdentityProviderL()
       
   274     {
       
   275     return IdentityL().IdentityProviderL();
       
   276     }
       
   277 const RPointerArray<CSenIdentityProvider>& CSenBaseIdentityManager::IdentityProvidersL()
       
   278     {
       
   279     return IdentityL().IdentityProvidersL();
       
   280     }
       
   281 
       
   282 CSenIdentityProvider* CSenBaseIdentityManager::IdentityProviderL(
       
   283                             MSenServiceDescription& aServiceDescription )
       
   284     {
       
   285     return IdentityL().IdentityProviderL( aServiceDescription );
       
   286     }
       
   287 
       
   288 CSenIdentityProvider* CSenBaseIdentityManager::IdentityProviderL(
       
   289                                                         const TDesC8& aURI )
       
   290     {
       
   291     return IdentityL().IdentityProviderL(aURI);
       
   292     }
       
   293 
       
   294 CSenIdentityProvider* CSenBaseIdentityManager::IdentityProviderL(
       
   295                                                 const CDesC8Array& aIdpList,
       
   296                                                 TBool aStrict)
       
   297     {
       
   298     return IdentityL().IdentityProviderL(aIdpList,aStrict);
       
   299     }
       
   300 
       
   301 CSenIdentityProvider* CSenBaseIdentityManager::IdentityProviderL(
       
   302                                 MSenServiceDescription& aServiceDescription,
       
   303                                 const CDesC8Array& aIdpList,
       
   304                                 TBool aStrict)
       
   305     {
       
   306     return IdentityL().IdentityProviderL(aServiceDescription,aIdpList,aStrict);
       
   307     }
       
   308 
       
   309 
       
   310 
       
   311 // Takes ownership of given IdentityProvider(!)
       
   312 TInt CSenBaseIdentityManager::RegisterIdentityProviderL( CSenIdentityProvider* apIdp )
       
   313     {
       
   314     if(!apIdp)
       
   315         {
       
   316         return KErrArgument; // NULL given!
       
   317         }
       
   318 
       
   319     CleanupStack::PushL(apIdp);
       
   320 
       
   321     // try to register into default identity
       
   322     // null check done, above
       
   323     TInt retVal = IdentityL().RegisterIdentityProviderL(apIdp);
       
   324     CleanupStack::Pop(); // apIdp, ownership elsewhere
       
   325 
       
   326     // check success:
       
   327     if ( retVal == KErrNone)
       
   328         {
       
   329         // ownership taken, pop from stack:
       
   330         // try to serialize owned object
       
   331         TInt notifyLeaveCode(KErrNone);
       
   332         TRAP(notifyLeaveCode, iManager.NotifyFrameworksL(KSenEventRegisterIdentityProvider, apIdp));
       
   333         if ( WriteDatabase() != KErrNone)
       
   334             {
       
   335             retVal = KErrWrite;
       
   336             }
       
   337         }
       
   338     return retVal;
       
   339     }
       
   340 
       
   341 TInt CSenBaseIdentityManager::UnregisterIdentityProviderL( CSenIdentityProvider& aIdp )
       
   342     {
       
   343     TInt retVal = IdentityL().UnregisterIdentityProviderL( aIdp );
       
   344 
       
   345     if ( retVal == KErrNone )
       
   346         {
       
   347         TInt notifyLeaveCode(KErrNone);
       
   348         TRAP(notifyLeaveCode, iManager.NotifyFrameworksL(KSenEventUnregisterIdentityProvider, &aIdp));
       
   349         
       
   350         
       
   351 //      if (KErrNone != WriteDatabase() )
       
   352 //          {
       
   353 //          retVal = EFalse;
       
   354 //          }
       
   355 
       
   356         retVal = WriteDatabase();
       
   357         }
       
   358     return retVal;
       
   359     }
       
   360 
       
   361 TBool CSenBaseIdentityManager::AssociateServiceL( const TDesC8& aServiceID,
       
   362                                                   const TDesC8& aProviderID )
       
   363     {
       
   364     TBool retVal = IdentityL().AssociateServiceL(aServiceID, aProviderID);
       
   365     if (retVal)
       
   366         {
       
   367         if (KErrNone != WriteDatabase())
       
   368             {
       
   369             retVal = EFalse;
       
   370             }
       
   371         }
       
   372     return retVal;
       
   373     }
       
   374 
       
   375 TBool CSenBaseIdentityManager::DissociateServiceL(
       
   376                                                    const TDesC8& aServiceID,
       
   377                                                    const TDesC8& aProviderID )
       
   378     {
       
   379     TBool retVal = IdentityL().DissociateServiceL(aServiceID, aProviderID);
       
   380     if (retVal)
       
   381         {
       
   382         if (KErrNone != WriteDatabase())
       
   383             {
       
   384             retVal = EFalse;
       
   385             }
       
   386         }
       
   387     return retVal;
       
   388     }
       
   389 
       
   390 TInt CSenBaseIdentityManager::IdentitiesL(
       
   391                                             CDesC8Array& /* aIdentitiesList */)
       
   392     {
       
   393     return KErrNotSupported; // not yet supported.
       
   394     }
       
   395 
       
   396 
       
   397 // NOTE! This code does not do as documentation states.
       
   398 // Only second line text is shown and
       
   399 // "Ok" button on the left side.
       
   400 // return the value of button pressed (OK or CANCEL)
       
   401 TInt CSenBaseIdentityManager::YesNoQueryL(const TDesC& aLine1,
       
   402                                           const TDesC& aLine2,
       
   403                                           const TDesC& aButton1,
       
   404                                           const TDesC& aButton2)
       
   405     {
       
   406     RNotifier notifier;
       
   407     User::LeaveIfError(notifier.Connect());
       
   408     CleanupClosePushL(notifier);
       
   409 
       
   410     TRequestStatus status;
       
   411     TInt buttonVal = -1;
       
   412 
       
   413 
       
   414     // NOTE! This code does not do as documentation states.
       
   415     // Only second line text is shown and
       
   416     // "Ok" button on the left side.
       
   417 
       
   418 //    notifier.Notify(_L("Line 1 text"), _L("Line2 text"),
       
   419 //        _L("B1Txt"), _L("B2Txt"), buttonVal, status);
       
   420 
       
   421     notifier.Notify(aLine1,
       
   422                     aLine2,
       
   423                     aButton1,
       
   424                     aButton2,
       
   425                     buttonVal,
       
   426                     status);
       
   427 
       
   428 
       
   429     User::WaitForRequest(status);
       
   430     CleanupStack::PopAndDestroy(); // Close notifier
       
   431 
       
   432 #ifdef _SENDEBUG
       
   433     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("User pressed button (%d)"), buttonVal));
       
   434 #endif
       
   435 
       
   436     return buttonVal;
       
   437     }
       
   438 
       
   439 TBool CSenBaseIdentityManager::AllowSavePasswordL()
       
   440     {
       
   441     TBool retVal = EFalse;
       
   442 
       
   443     RNotifier notifier;
       
   444     User::LeaveIfError(notifier.Connect());
       
   445     CleanupClosePushL(notifier);
       
   446 
       
   447     TPckgBuf<TAskSaveDlgRequest>* request = new(ELeave)TPckgBuf<TAskSaveDlgRequest>();
       
   448     CleanupStack::PushL(request);
       
   449 
       
   450     TPckgBuf<TAskSaveDlgResponse>* response = new(ELeave)TPckgBuf<TAskSaveDlgResponse>();
       
   451     CleanupStack::PushL(response);
       
   452 
       
   453     TRequestStatus reqStatus;
       
   454     notifier.StartNotifierAndGetResponse(reqStatus, KSenNotifierPluginUID, *request, *response);
       
   455 
       
   456     User::WaitForRequest(reqStatus);
       
   457     notifier.Close();
       
   458 
       
   459     if(reqStatus.Int() == KErrNone)
       
   460         {
       
   461         if((*response)().OkButtonPressed())
       
   462             {
       
   463             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("User pressed OK Button to allow password saving")));
       
   464             retVal = ETrue;
       
   465             }
       
   466 #ifdef _SENDEBUG
       
   467         else
       
   468             {
       
   469             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("User pressed Cancel Button to deny password saving")));
       
   470             }
       
   471 #endif
       
   472         }
       
   473 #ifdef _SENDEBUG
       
   474     else if(reqStatus.Int() == KErrNotFound)
       
   475         {
       
   476         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("Notifier plug-in for 'Save WS data?' -dialog was not found")));
       
   477         }
       
   478     else
       
   479         {
       
   480         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("'Save WS data?' -dialog returned an error: %d"), 
       
   481                                                         reqStatus.Int()));
       
   482         }
       
   483 #endif
       
   484 
       
   485     CleanupStack::PopAndDestroy(2); // request, response;
       
   486     //CleanupStack::PopAndDestroy(); // close the notifier
       
   487     CleanupStack::Pop(); // notifier
       
   488     return retVal;
       
   489     }
       
   490 
       
   491 TInt CSenBaseIdentityManager::AuthenticationForL(
       
   492                             CSenIdentityProvider& aProvider, 
       
   493                             TPckgBuf<TSenAuthentication>& aResponse)
       
   494     {
       
   495     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CSenBaseIdentityManager::AuthenticationForL");
       
   496 
       
   497     TPtrC8 user = aProvider.AuthzID();
       
   498     if (user == KNullDesC8)
       
   499         {
       
   500         user.Set(aProvider.AdvisoryAuthnID());
       
   501         }
       
   502 
       
   503 
       
   504     if (!iShowPasswordDialog)
       
   505         {
       
   506         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"Not allowed to show password dialog");
       
   507         HBufC8* pDecodedPassword = SenXmlUtils::DecodeHttpCharactersLC(aProvider.Password());
       
   508         TPtrC8 decodedPassword = pDecodedPassword->Des();
       
   509 
       
   510         aResponse().iUsername.Append((const TUint8*)user.Ptr(), user.Size());
       
   511         aResponse().iPassword.Append((const TUint8*)decodedPassword.Ptr(), 
       
   512                                         decodedPassword.Size());
       
   513         CleanupStack::PopAndDestroy(); // delete pDecodedPassword
       
   514         return KErrNone;
       
   515         }
       
   516 
       
   517 
       
   518     RNotifier notifier;
       
   519     User::LeaveIfError(notifier.Connect());
       
   520     CleanupClosePushL(notifier);
       
   521 
       
   522     TPckgBuf<TAuthenticationDlgRequest>* request = 
       
   523                             new(ELeave)TPckgBuf<TAuthenticationDlgRequest>();
       
   524     CleanupStack::PushL(request);
       
   525 
       
   526     // convert username to unicode
       
   527     HBufC* pUserAsUnicode = SenXmlUtils::ToUnicodeLC(user); // push
       
   528     TPtrC username = pUserAsUnicode->Des();
       
   529 
       
   530     // decode password
       
   531     HBufC8* pDecodedPassword = SenXmlUtils::DecodeHttpCharactersLC(aProvider.Password());
       
   532     TPtrC8 decodedPassword = pDecodedPassword->Des();
       
   533     // convert decoded password to unicode
       
   534     HBufC* pPasswordAsUnicode = 
       
   535                 SenXmlUtils::ToUnicodeLC(decodedPassword); // push
       
   536     TPtrC password = pPasswordAsUnicode->Des();
       
   537 
       
   538     // set data to request
       
   539     (*request)().SetData(username, password);
       
   540 
       
   541     CleanupStack::PopAndDestroy(3); // delete pPasswordAsUnicode, pDecodedPassword, pUserAsUnicode
       
   542 
       
   543 
       
   544     TPckgBuf<TAuthenticationDlgResponse>* response = 
       
   545                         new(ELeave)TPckgBuf<TAuthenticationDlgResponse>();
       
   546     CleanupStack::PushL(response);
       
   547 
       
   548     TRequestStatus reqStatus;
       
   549     notifier.StartNotifierAndGetResponse(reqStatus, 
       
   550                             KSenNotifierPluginUID, *request, *response);
       
   551 
       
   552     TBool illegalUsername = EFalse;
       
   553 
       
   554     HBufC8* pUsernameUtf8 = NULL;
       
   555     HBufC8* pPasswordUtf8 = NULL;
       
   556     User::WaitForRequest(reqStatus);
       
   557 
       
   558     notifier.Close();
       
   559 
       
   560     if(reqStatus.Int() == KErrNone)
       
   561         {
       
   562         if((*response)().OkButtonPressed())
       
   563             {
       
   564             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("User pressed OK Button in Password dialog")));
       
   565             pUsernameUtf8 = SenXmlUtils::ToUtf8LC((*response)().Username());
       
   566             pPasswordUtf8 = SenXmlUtils::ToUtf8LC((*response)().Password());
       
   567             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("Username: %S"), pUsernameUtf8));
       
   568             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("Password: %S"), pPasswordUtf8 ));
       
   569             HBufC8* pEncodedUsername = NULL;
       
   570             illegalUsername = 
       
   571                 SenXmlUtils::EncodeHttpCharactersL(*pUsernameUtf8,
       
   572                                                     pEncodedUsername);
       
   573             if (illegalUsername) 
       
   574                 {
       
   575                 TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("Username included illegal characters.")));
       
   576                 delete pEncodedUsername;
       
   577                 pEncodedUsername = NULL;
       
   578                 }
       
   579 #ifdef _SENDEBUG
       
   580             else
       
   581                 {
       
   582                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"Username did NOT include illegal characters.");
       
   583                 }
       
   584 #endif
       
   585        		
       
   586        		//Check if iilegal chars are there then return
       
   587             if (!illegalUsername)
       
   588                 {
       
   589                 
       
   590                 if(user != *pUsernameUtf8)
       
   591                 {
       
   592                 	
       
   593 	                // User modified the username in the dialog prompt(!)
       
   594 	                // We have to save it right away, because there is
       
   595 	                // no in-memory/session based member variables in
       
   596 	                // components calling identity manager (sec mechs and
       
   597 	                // in ID-WSF AS client!
       
   598 	                TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"Username changed. Saving new username.");
       
   599 
       
   600 	                // Use Advisory only if AuthzID is not available
       
   601 	                if (aProvider.AuthzID() == KNullDesC8 
       
   602 	                    && aProvider.AdvisoryAuthnID() != KNullDesC8)
       
   603 	                    {
       
   604 	                    aProvider.SetUserInfoL(aProvider.AuthzID(),
       
   605 	                                            *pUsernameUtf8, 
       
   606 	                                            aProvider.Password());
       
   607 	                    }
       
   608 	                else 
       
   609 	                    {
       
   610 	                    // There was at least AuthzID available, and possibly
       
   611 	                    // also advisory value. Any way, if 
       
   612 
       
   613 	                    
       
   614 	                    // We have to NULL advisory value (set it to KNullDesC8), 
       
   615 	                    // because if both were available, then user was
       
   616 	                    // prompted to allow modification of AuthzID AND 
       
   617 	                    // if advisory was changed in service (is no longer
       
   618 	                    // valid), there would NOT be any way for end-user
       
   619 	                    // to change (remove) it(!)
       
   620 
       
   621 	                    CSenElement& element = aProvider.AsElement();
       
   622 	                    delete element.RemoveElement(KSenIdpAdvisoryAuthnIdLocalname);
       
   623 
       
   624 	                    aProvider.SetUserInfoL(*pUsernameUtf8, 
       
   625 	                                            KNullDesC8,
       
   626 	                                            aProvider.Password());
       
   627 	                    }
       
   628 	                // serialize changed username into sensessions.xml database immediately
       
   629 	                WriteDatabase();
       
   630                 }
       
   631                 
       
   632 	            if (AllowSavePasswordL())
       
   633 	                {
       
   634 	                HBufC8* pEncodedPassword = SenXmlUtils::EncodeHttpCharactersLC(*pPasswordUtf8);
       
   635 #ifdef _SENDEBUG
       
   636                 if(pEncodedPassword)
       
   637                     {
       
   638                     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("Encoded password: %S"), pEncodedPassword));
       
   639                     }
       
   640 #endif
       
   641 					if(pEncodedPassword)
       
   642 						{
       
   643 							aProvider.SetUserInfoL(aProvider.AuthzID(),
       
   644 	                                        aProvider.AdvisoryAuthnID(),
       
   645 	                                        *pEncodedPassword);
       
   646 						}
       
   647 	                WriteDatabase();
       
   648 	                CleanupStack::PopAndDestroy(1); // pEncodedPassword
       
   649 	                }
       
   650                 
       
   651                 }
       
   652 
       
   653             aResponse().iUsername.Zero();
       
   654             aResponse().iPassword.Zero();
       
   655             if (!illegalUsername) 
       
   656             {
       
   657 	            aResponse().iUsername.Copy(pUsernameUtf8->Des());
       
   658 	            aResponse().iPassword.Copy(pPasswordUtf8->Des());
       
   659             }
       
   660 
       
   661             CleanupStack::PopAndDestroy(2); // pPasswordUtf8, pUsernameUtf8
       
   662             }
       
   663         else
       
   664             {
       
   665             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("User pressed Cancel Button in Password dialog")));
       
   666             }
       
   667         }
       
   668     else if(reqStatus.Int() == KErrNotFound)
       
   669         {
       
   670         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("Password dialog plugin notifier impl. was not found")));
       
   671         }
       
   672     else
       
   673         {
       
   674         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8(" Notifier plugin for 'Password' dialog returned an error: %d"), 
       
   675                                                         reqStatus.Int()));
       
   676         }
       
   677 
       
   678     CleanupStack::PopAndDestroy(2); // request, response;
       
   679     CleanupStack::Pop(); // notifier
       
   680 
       
   681     if (illegalUsername) return KErrArgument;
       
   682     return reqStatus.Int();
       
   683     }
       
   684 
       
   685 
       
   686 TInt CSenBaseIdentityManager::WriteConfigurationToL( const TDesC& aFile )
       
   687     {
       
   688     // First, collect everything into MEMORY
       
   689     CBufFlat *pBuf = CBufFlat::NewL(KFLATBUF_SIZE);
       
   690     CleanupStack::PushL(pBuf);
       
   691 
       
   692     RBufWriteStream bufWs(*pBuf);
       
   693     CleanupClosePushL(bufWs);
       
   694 
       
   695     bufWs.WriteL(KUsersStart);
       
   696 
       
   697     if(iIdentity)
       
   698         {
       
   699         iIdentity->WriteAsXMLToL(bufWs);
       
   700         }
       
   701 
       
   702     bufWs.WriteL(KUsersEnd);
       
   703 
       
   704     TPtrC8 p8 = pBuf->Ptr(0);
       
   705 
       
   706     CleanupStack::PopAndDestroy(1); // bufWs
       
   707 
       
   708     // Everything in MEMORY ok, prepare to write into file
       
   709     RFs fss;
       
   710     User::LeaveIfError(fss.Connect());
       
   711     CleanupClosePushL(fss);
       
   712 
       
   713     RFileWriteStream fileOutStream;
       
   714     CleanupClosePushL(fileOutStream);
       
   715 
       
   716 
       
   717     if(!SysUtil::FFSSpaceBelowCriticalLevelL(&fss, p8.Length()) )
       
   718         {
       
   719         // note, this will zero-length the file(!)
       
   720         // it is better to require that 2xfilesize is available and not to
       
   721         // dangerously zero the old file and find out
       
   722         // that there is no space left..
       
   723 
       
   724         //Data caging 2 implementation
       
   725 #if defined( EKA2 ) || defined( RD_SECURE_PRIV_DATA )
       
   726         TBuf<KMaxPath> file;
       
   727         fss.CreatePrivatePath(EDriveC);
       
   728         fss.PrivatePath(file);
       
   729         file.Append(aFile);
       
   730         fileOutStream.Replace(fss, file, EFileWrite);
       
   731 #else
       
   732         fileOutStream.Replace(fss, aFile, EFileWrite);
       
   733 #endif
       
   734         // finally write the UTF-8 into the file. 
       
   735         fileOutStream.WriteL(p8);
       
   736         }
       
   737 
       
   738     CleanupStack::PopAndDestroy(3); // fileOutStream, fss, pBuf
       
   739 
       
   740     return KErrNone;
       
   741     }
       
   742 
       
   743 
       
   744 void CSenBaseIdentityManager::ReloadSenSecurityMechanismsL()
       
   745     {
       
   746     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("Entering CSenXMLDAO::ReloadSenSecurityMechanismsL()..")));
       
   747 
       
   748     delete ipMechanicsNames;
       
   749     ipMechanicsNames = NULL;
       
   750     ipMechanicsNames = CBufFlat::NewL(KFLATBUF_SIZE);
       
   751     RBufWriteStream bufWs(*ipMechanicsNames);
       
   752     CleanupClosePushL(bufWs);
       
   753 
       
   754     iSechMechNames.ResetAndDestroy();
       
   755     iSechMechCues.ResetAndDestroy();
       
   756 
       
   757     REComSession::ListImplementationsL(KUidSechMech, iEcomInfoArray);
       
   758 
       
   759 
       
   760     TInt infoCount(iEcomInfoArray.Count());
       
   761     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel, _L8("%d Security Mechanism ECOM implementations found."),
       
   762                         infoCount));
       
   763 
       
   764     for (TInt i=0; i<infoCount; i++)
       
   765         {
       
   766         CImplementationInformation* pImplImfo = iEcomInfoArray[i];
       
   767         CSenSecurityMechanism* pSechMech =
       
   768             CSenSecurityMechanism::NewL(pImplImfo->DataType(),
       
   769                                         iManager);
       
   770         CleanupStack::PushL(pSechMech);
       
   771         iSechMechNames.Append(pSechMech->Name().AllocL());
       
   772         iSechMechCues.Append(pImplImfo->DataType().AllocL());
       
   773         bufWs.WriteL(pSechMech->Name());
       
   774         CleanupStack::PopAndDestroy(); // pSechMech
       
   775         if (i < (infoCount-1))
       
   776             {
       
   777             bufWs.WriteL(KSpace);
       
   778             }
       
   779         }
       
   780     iEcomInfoArray.ResetAndDestroy();
       
   781     CleanupStack::PopAndDestroy(1); // bufWs
       
   782     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("Leaving CSenXMLDAO::ReloadSenSecurityMechanismsL().. ")));
       
   783 
       
   784     }
       
   785 
       
   786 
       
   787 TPtrC8 CSenBaseIdentityManager::SenSecurityMechanismNames8L()
       
   788     {
       
   789     TPtrC8 p8 = ipMechanicsNames->Ptr(0);
       
   790     return p8;
       
   791     }
       
   792 
       
   793 CSenSecurityMechanism* CSenBaseIdentityManager::MechanismNamedL(
       
   794                                                         const TDesC8& aName)
       
   795     {
       
   796     CSenSecurityMechanism* pSaslMechanism = NULL;
       
   797     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CSenIdWsfPlugin::MechanismNamedL");
       
   798     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(aName));
       
   799 
       
   800     for (TInt i=0; i<iSechMechNames.Count(); i++)
       
   801         {
       
   802         if (*iSechMechNames[i] == aName)
       
   803             {
       
   804             pSaslMechanism = CSenSecurityMechanism::NewL(*iSechMechCues[i], 
       
   805                                                                 iManager);
       
   806             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CSenBaseIdentityManager::MechanismNamedL - successful");
       
   807             break;
       
   808             }
       
   809         }
       
   810     if(!pSaslMechanism)
       
   811         {
       
   812         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CSenBaseIdentityManager::MechanismNamedL - lookup failed!");
       
   813         }
       
   814 
       
   815     return pSaslMechanism;
       
   816     }
       
   817 
       
   818 HBufC8* CSenBaseIdentityManager::EncodeToBase64LC(const TDesC8& aSource)
       
   819     {
       
   820     return iManager.EncodeToBase64LC(aSource);
       
   821     }
       
   822 
       
   823 HBufC8* CSenBaseIdentityManager::DecodeFromBase64LC(const TDesC8& aSource)
       
   824     {
       
   825     return iManager.DecodeFromBase64LC(aSource);
       
   826     }
       
   827 
       
   828 void CSenBaseIdentityManager::SetShowPasswordDialog(const TBool aState)
       
   829     {
       
   830     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CSenBaseIdentityManager::SetShowPasswordDialog");
       
   831     iShowPasswordDialog = aState;
       
   832     }
       
   833 
       
   834 TInt CSenBaseIdentityManager::ContainsIdentityProviderL(
       
   835                                                    TBool& aContains,
       
   836                                                    CSenIdentityProvider& aIDP)
       
   837     {
       
   838     return IdentityL().ContainsIdentityProviderL(aContains, aIDP);
       
   839     }
       
   840     
       
   841 TInt CSenBaseIdentityManager::FindMatchingIdentityProviderL(CSenIdentityProvider &aIdp,
       
   842                                                             CSenIdentityProvider*& apMatch)
       
   843     {
       
   844     return IdentityL().FindMatchingIdentityProviderL(aIdp, apMatch);
       
   845     }
       
   846         
       
   847 TInt CSenBaseIdentityManager::UpdateIdentityProviderL(CSenIdentityProvider& aIdp)
       
   848     {
       
   849     TInt retVal = IdentityL().UpdateIdentityProviderL(aIdp);
       
   850 
       
   851     // check success:
       
   852     if ( retVal == KErrNone)
       
   853         {
       
   854         // ownership taken, pop from stack:
       
   855         // try to serialize owned object
       
   856         if ( WriteDatabase() != KErrNone)
       
   857             {
       
   858             retVal = KErrWrite;
       
   859             }
       
   860         }
       
   861 
       
   862     return retVal;
       
   863     }
       
   864     
       
   865 
       
   866 EXPORT_C void CSenBaseIdentityManager::UpdateTouchIdentityDBL(MSenServiceDescription& asd)
       
   867 	{
       
   868 	CSenIdentityProvider* Idp = NULL;
       
   869 	
       
   870 	Idp = iIdentity->IdentityProviderL(asd);
       
   871 	if(Idp != NULL)
       
   872 		{
       
   873 		TUint32 current_tick(0);
       
   874 		TBuf8<32> tickBuf;
       
   875 				
       
   876 		CSenElement& IdpElem = Idp->AsElement();
       
   877 		current_tick = User::NTickCount();
       
   878 		tickBuf.Num(current_tick);
       
   879 		IdpElem.AddAttrL(KTouch(), tickBuf);
       
   880 		}
       
   881 //	CleanupUnusedIdentityDBL();   
       
   882                                   // (don't de-serialize old items). Serialized 
       
   883 	                              // objects cannot be de-allocated on the fly.
       
   884 	}
       
   885 	
       
   886 void CSenBaseIdentityManager::CleanupUnusedIdentityDBL()
       
   887 	{
       
   888 	TInt count(0);
       
   889 	
       
   890 	RPointerArray<CSenElement> elemList;
       
   891 	CleanupClosePushL(elemList);
       
   892 	
       
   893 	CSenElement& element = iIdentity->AsElement();
       
   894 	element.ElementsL(elemList, KIdentityProvider);
       
   895 	count = elemList.Count(); 
       
   896 	// There can be many Identity Provider elements within Identity element
       
   897 	for(TInt i = 0; i < count; i++)
       
   898 		{
       
   899 		CSenElement* elem = elemList[i];
       
   900 		
       
   901 		const TDesC8* attrValue = elem->AttrValue(KTouch());
       
   902 		if(attrValue != NULL)
       
   903 			{
       
   904 			TUint32 current_tick(0);
       
   905 			TUint32 db_ticks(0);
       
   906 			TUint32 diff_ticks(0);
       
   907 					
       
   908 			TLex8 lex;
       
   909             lex.Assign(*attrValue);
       
   910             lex.Val(db_ticks, EDecimal);
       
   911             
       
   912             current_tick = User::NTickCount();
       
   913             diff_ticks = current_tick - db_ticks;
       
   914             if(KMaxTicks <= diff_ticks || current_tick < db_ticks)
       
   915 				{
       
   916 				TInt endpointCount(0);
       
   917 				_LIT8(KEndpoint, "Endpoint");
       
   918 												
       
   919 				RPointerArray<CSenElement> endpointElemList;
       
   920 				CleanupClosePushL(endpointElemList);
       
   921 				
       
   922 				elem->ElementsL(endpointElemList, KEndpoint);
       
   923 				endpointCount = endpointElemList.Count();
       
   924 				
       
   925 				if(endpointCount > 0)
       
   926 					{
       
   927 					CSenIdentityProvider* pMatch = NULL;
       
   928 					
       
   929 					CSenElement* endpointElem = endpointElemList[0];
       
   930 					TPtrC8 endpoint = endpointElem->Content();
       
   931 					pMatch = IdentityProviderL(endpoint);
       
   932 					if(pMatch != NULL)
       
   933 						{
       
   934 						// Ownership ?
       
   935 						UnregisterIdentityProviderL(*pMatch);
       
   936 						}
       
   937 					}
       
   938 				CleanupStack::PopAndDestroy(&endpointElemList);
       
   939 				}
       
   940 			}
       
   941 		}
       
   942 	CleanupStack::PopAndDestroy(&elemList);
       
   943 	}
       
   944 			
       
   945 // End of file