vpnui/vpnpolins/src/vpnbundlehandler.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2007 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:   Utility class for .VPN bundle content analysis
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32std.h>
       
    21 
       
    22 #include "vpnbundlehandler.h"
       
    23 #include "policyinstaller_constants.h"
       
    24 #include "logvpncommon.h"
       
    25 
       
    26 
       
    27 CVpnBundleHandler* CVpnBundleHandler::NewL(const TDesC& aBundleDir) 
       
    28     {
       
    29     LOG_("-> CVpnBundleHandler::NewL()");
       
    30     
       
    31     CVpnBundleHandler* self = new (ELeave) CVpnBundleHandler(aBundleDir);
       
    32     CleanupStack::PushL(self);
       
    33     self->ConstructL();
       
    34     CleanupStack::Pop(self);
       
    35     
       
    36     LOG_("<- CVpnBundleHandler::NewL()");
       
    37     
       
    38     return self;
       
    39     }
       
    40 
       
    41 CVpnBundleHandler* CVpnBundleHandler::NewLC(const TDesC& aBundleDir) 
       
    42     {
       
    43     LOG_("-> CVpnBundleHandler::NewLC()");
       
    44     
       
    45     CVpnBundleHandler* self = new (ELeave) CVpnBundleHandler(aBundleDir);
       
    46     CleanupStack::PushL(self);
       
    47     self->ConstructL();
       
    48     
       
    49     LOG_("<- CVpnBundleHandler::NewLC()");
       
    50     
       
    51     return self;
       
    52     }
       
    53 
       
    54 CVpnBundleHandler::CVpnBundleHandler(const TDesC& aBundleDir) : iBundleFileDir(aBundleDir) 
       
    55     {
       
    56     }
       
    57 
       
    58 CVpnBundleHandler::~CVpnBundleHandler() 
       
    59     {
       
    60     LOG_("-> CVpnBundleHandler::~CVpnBundleHandler()");
       
    61 
       
    62     delete iPKCS12FileName;
       
    63     delete iCommandFileName;
       
    64     iFileServer.Close();    
       
    65 
       
    66     LOG_("<- CVpnBundleHandler::~CVpnBundleHandler()");
       
    67     }
       
    68 
       
    69 void CVpnBundleHandler::ConstructL() 
       
    70     {
       
    71     LOG_("-> CVpnBundleHandler::ConstructL()");
       
    72 
       
    73     User::LeaveIfError(iFileServer.Connect());
       
    74 
       
    75     LOG_("<- CVpnBundleHandler::ConstructL()");
       
    76     }
       
    77 
       
    78 void CVpnBundleHandler::AnalyzeBundleContentsL() 
       
    79     {
       
    80     LOG_("-> CVpnBundleHandler::AnalyzeBundleContentsL()");
       
    81 
       
    82     // Check whether a command file exists. Leave if more than one exist.
       
    83     LocateCommandFileL();
       
    84 
       
    85     // Check whether a PKCS#12 file exists. Leave if more than one exist.
       
    86     LocatePKCS12FileL();
       
    87 
       
    88     LOG_("<- CVpnBundleHandler::AnalyzeBundleContentsL()");
       
    89     }
       
    90 
       
    91 TBool CVpnBundleHandler::CommandFileExists() const
       
    92     {
       
    93     LOG_("-> CVpnBundleHandler::CommandFileExists()");
       
    94 
       
    95     TBool ret(EFalse);
       
    96     if (iCommandFileName)
       
    97         {
       
    98         ret = ETrue;
       
    99         }
       
   100 
       
   101     LOG_1("<- CVpnBundleHandler::CommandFileExists() ret: %d", ret);
       
   102 
       
   103     return ret;
       
   104     }
       
   105 
       
   106 TBool CVpnBundleHandler::PKCS12FileExists() const
       
   107     {
       
   108     LOG_("-> CVpnBundleHandler::PKCS12FileExists() ");
       
   109 
       
   110     TBool ret(EFalse);
       
   111     if (iPKCS12FileName)
       
   112         {
       
   113         ret = ETrue;
       
   114         }
       
   115 
       
   116     LOG_1("<- CVpnBundleHandler::PKCS12FileExists() ret: %d", ret);
       
   117 
       
   118     return ret;
       
   119     }
       
   120 
       
   121 HBufC8* CVpnBundleHandler::ExtractPKCS12DataL() 
       
   122     {
       
   123     LOG_("-> CVpnBundleHandler::ExtractPKCS12DataL()");
       
   124 
       
   125     TInt dataLen(0);
       
   126     HBufC8* ret(NULL);
       
   127 
       
   128     ASSERT(iPKCS12FileName);
       
   129 
       
   130     // Read binary PKCS#12 data from file and into an 8bit descr
       
   131     RFile file;
       
   132     CleanupClosePushL(file);
       
   133 
       
   134     LOG_(" Opening command file for reading");
       
   135     User::LeaveIfError(file.Open(iFileServer, 
       
   136                                  *iPKCS12FileName, 
       
   137                                  EFileStream|EFileRead));
       
   138     User::LeaveIfError(file.Size(dataLen));
       
   139 
       
   140     LOG_1(" File data length: %d bytes", dataLen);
       
   141 
       
   142     ret = HBufC8::NewLC(dataLen);
       
   143     TPtr8 dataptr = ret->Des();
       
   144 
       
   145     LOG_1(" Data length before read: %d B", dataptr.Length());
       
   146 
       
   147     LOG_(" Reading...");
       
   148     User::LeaveIfError(file.Read(dataptr));
       
   149 
       
   150     LOG_1(" Data length after read: %d B", dataptr.Length());
       
   151 
       
   152     CleanupStack::Pop(ret); // control transferred
       
   153 
       
   154     LOG_(" Closing file");
       
   155     CleanupStack::PopAndDestroy(); // file
       
   156 
       
   157     LOG_1("<- CVpnBundleHandler::ExtractPKCS12DataL() len: %d", dataLen);
       
   158 
       
   159     return ret;
       
   160     }
       
   161 
       
   162 HBufC8* CVpnBundleHandler::ExtractCommandFileDataL() 
       
   163     {
       
   164     LOG_("-> CVpnBundleHandler::ExtractCommandFileDataL()");
       
   165 
       
   166     TInt dataLen(0);
       
   167     HBufC8* ret(NULL);
       
   168 
       
   169 
       
   170     ASSERT(iCommandFileName);
       
   171 
       
   172     // Read ASCII cmd file data from file and into an 8bit descr
       
   173     RFile file;
       
   174     CleanupClosePushL(file);
       
   175 
       
   176     LOG_(" Opening command file for reading");
       
   177     User::LeaveIfError(file.Open(iFileServer, 
       
   178                                  *iCommandFileName, 
       
   179                                  EFileStreamText|EFileRead));
       
   180     User::LeaveIfError(file.Size(dataLen));
       
   181 
       
   182     LOG_1(" File data length: %d bytes", dataLen);
       
   183 
       
   184     ret = HBufC8::NewLC(dataLen);
       
   185     TPtr8 dataptr = ret->Des();
       
   186 
       
   187     LOG_1(" Data length before read: %d B", dataptr.Length());
       
   188 
       
   189     LOG_(" Reading...");
       
   190     User::LeaveIfError(file.Read(dataptr));
       
   191 
       
   192     LOG_1(" Data length after read: %d B", dataptr.Length());
       
   193 
       
   194     CleanupStack::Pop(ret); // control transferred
       
   195     LOG_(" Closing file");
       
   196     CleanupStack::PopAndDestroy(); // file
       
   197 
       
   198     LOG_1("<- CVpnBundleHandler::ExtractCommandFileDataL() len: %d", dataLen);
       
   199     
       
   200     return ret;
       
   201     }
       
   202 
       
   203 /**
       
   204  * Find PKCS#12 file from the temp dir the .vpn file was extracted to.
       
   205  */
       
   206 void CVpnBundleHandler::LocateCommandFileL() 
       
   207     {
       
   208     LOG_("-> CVpnBundleHandler::LocateCommandFileL()");
       
   209 
       
   210     TFindFile fileFinder(iFileServer);
       
   211     CDir* fileList(NULL);
       
   212 
       
   213 
       
   214 
       
   215     LOG_1(" Search argument: '%S'", &(KCommandFilePattern()));
       
   216     LOG_1(" Search path: '%S'", &iBundleFileDir);
       
   217 
       
   218     // Find the command file using the pattern .p12
       
   219     TInt ret = fileFinder.FindWildByDir(KCommandFilePattern, 
       
   220                                         iBundleFileDir, fileList);
       
   221 
       
   222     if (ret == KErrNone)
       
   223         {
       
   224         CleanupStack::PushL(fileList);
       
   225 
       
   226         // Only allow one or less command files in the VPN bundle
       
   227         if (fileList->Count() > 1) 
       
   228             {
       
   229             LOG_("<- CVpnBundleHandler::LocateCommandFileL() LEAVE: multiple command files");
       
   230             User::Leave(KErrArgument);
       
   231             }
       
   232 
       
   233         // At the moment, there may be exactly 0 or 1 command files,
       
   234         // so the loop isn't strictly necessary. However,
       
   235         // in the future it may be deemed necessary to support 
       
   236         // multiple command files.
       
   237         for (TInt i = 0; i < fileList->Count(); i++)
       
   238             {
       
   239             LOG_1(" Processing cmdfile number %d", (i+1));
       
   240             TParse fileNameParser;
       
   241             fileNameParser.Set((*fileList)[i].iName, NULL, NULL);
       
   242 
       
   243             LOG_1(" Found cmdfile: '%S'", &(fileNameParser.FullName()));
       
   244             if (iCommandFileName) 
       
   245                 {
       
   246                 delete iCommandFileName;
       
   247                 iCommandFileName = NULL;
       
   248                 }
       
   249 
       
   250             // Allocate heap for the filename
       
   251             iCommandFileName = HBufC::NewL(fileNameParser.FullName().Length() + 
       
   252                                            iBundleFileDir.Length());
       
   253             //Concatenate the path + filename
       
   254             iCommandFileName->Des().Append(iBundleFileDir);
       
   255             iCommandFileName->Des().Append(fileNameParser.FullName());
       
   256             }
       
   257 
       
   258         CleanupStack::PopAndDestroy(fileList);
       
   259         }
       
   260     else if (ret != KErrNotFound) 
       
   261         {
       
   262         User::Leave(ret);
       
   263         }
       
   264 
       
   265     LOG_("<- CVpnBundleHandler::LocateCommandFileL()");
       
   266     }
       
   267 
       
   268 /**
       
   269  * Find PKCS#12 file from the temp dir the .vpn file was extracted to.
       
   270  */
       
   271 void CVpnBundleHandler::LocatePKCS12FileL() 
       
   272     {
       
   273     LOG_("-> CVpnBundleHandler::LocatePKCS12FileL()");
       
   274 
       
   275     TFindFile fileFinder(iFileServer);
       
   276     CDir* fileList(NULL);
       
   277 
       
   278     LOG_1(" Search argument: '%S'", &(KPKCS12Pattern()));
       
   279     LOG_1(" Search path: '%S'", &iBundleFileDir);
       
   280 
       
   281     // Find the PCKS#12 file using the pattern .p12
       
   282     TInt ret = fileFinder.FindWildByDir(KPKCS12Pattern, 
       
   283                                         iBundleFileDir, 
       
   284                                         fileList);
       
   285 
       
   286     if (ret == KErrNone)
       
   287         {
       
   288         CleanupStack::PushL(fileList);
       
   289 
       
   290         // Only allow one or less command files in the VPN bundle
       
   291         if (fileList->Count() > 1) 
       
   292             {
       
   293             LOG_("<- CVpnBundleHandler::LocatePKCS12FileL() LEAVE: multiple p12 files");
       
   294             User::Leave(KErrArgument);
       
   295             }
       
   296 
       
   297         // At the moment, there may be exactly 0 or 1 p12 files,
       
   298         // so the loop isn't strictly necessary. However,
       
   299         // in the future it may be deemed necessary to support 
       
   300         // multiple p12 files.
       
   301         for (TInt i = 0; i < fileList->Count(); i++)
       
   302             {
       
   303             LOG_1(" Processing p12 number %d", (i+1));
       
   304             TParse fileNameParser;
       
   305             fileNameParser.Set((*fileList)[i].iName, NULL, NULL);
       
   306 
       
   307             LOG_1(" Found p12: '%S'", &(fileNameParser.FullName()));
       
   308             if (iPKCS12FileName) 
       
   309                 {
       
   310                 delete iPKCS12FileName;
       
   311                 iPKCS12FileName = NULL;
       
   312                 }
       
   313 
       
   314             // Allocate heap for the filename
       
   315             iPKCS12FileName = HBufC::NewL(fileNameParser.FullName().Length() + 
       
   316                                            iBundleFileDir.Length());
       
   317             //Concatenate the path + filename
       
   318             iPKCS12FileName->Des().Append(iBundleFileDir);
       
   319             iPKCS12FileName->Des().Append(fileNameParser.FullName());
       
   320             }
       
   321 
       
   322         CleanupStack::PopAndDestroy(fileList);
       
   323         }
       
   324     else if (ret != KErrNotFound) 
       
   325         {
       
   326         User::Leave(ret);
       
   327         }
       
   328 
       
   329     LOG_("<- CVpnBundleHandler::LocatePKCS12FileL()");
       
   330     }
       
   331 
       
   332