Fixed encrypted resource list to db
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_encrypt_resource.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    task_ecnrypt_resource.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task encrypt resource
21  */
22 #include "task_encrypt_resource.h"
23
24 #include <unistd.h>
25 #include <string>
26 #include <sys/stat.h>
27 #include <dirent.h>
28
29 #include <dpl/log/log.h>
30 #include <dpl/errno_string.h>
31 #include <dpl/foreach.h>
32 #include <dpl/wrt-dao-ro/global_config.h>
33
34 #include <widget_install/job_widget_install.h>
35 #include <widget_install/widget_install_context.h>
36 #include <widget_install/widget_install_errors.h>
37
38 using namespace WrtDB;
39 using namespace WRTEncryptor;
40
41 namespace {
42 std::set<std::string>& getSupportedForEncryption()
43 {
44     static std::set<std::string> encryptSet;
45     if (encryptSet.empty()) {
46         encryptSet.insert(".html");
47         encryptSet.insert(".css");
48         encryptSet.insert(".js");
49     }
50     return encryptSet;
51 }
52
53 bool isSupportedForEncryption(std::string file)
54 {
55     size_t foundKey = file.rfind(".");
56     if (std::string::npos != foundKey) {
57         std::string mimeType = file.substr(foundKey);
58         return getSupportedForEncryption().count(mimeType) > 0;
59     }
60     return false;
61 }
62 }
63
64 namespace Jobs {
65 namespace WidgetInstall {
66 TaskEncryptResource::TaskEncryptResource(InstallerContext& context) :
67     DPL::TaskDecl<TaskEncryptResource>(this),
68     m_context(context)
69 {
70     AddStep(&TaskEncryptResource::StepEncryptResource);
71 }
72
73 void TaskEncryptResource::StepEncryptResource()
74 {
75     LogDebug("Step Encrypt resource");
76     m_resEnc = new ResourceEncryptor;
77     m_resEnc->CreateEncryptionKey(DPL::ToUTF8String(*m_context.
78                 widgetConfig.pkgname));
79
80     EncryptDirectory(m_context.locations->getTemporaryRootDir());
81 }
82
83 void TaskEncryptResource::EncryptDirectory(std::string path)
84 {
85     DIR* pkgDir = opendir(path.c_str());
86     if (!pkgDir) {
87         LogDebug("Package directory doesn't exist");
88         ThrowMsg(Exceptions::InternalError, "Error occurs during read \
89                 directory");
90     }
91
92     struct dirent* dirent;
93     struct stat statInfo;
94     do {
95         if ((dirent = readdir(pkgDir))) {
96             std::string dirName = dirent->d_name;
97             std::string absFileName = path + "/" + dirName;
98             if (stat(absFileName.c_str(), &statInfo) != 0) {
99                 ThrowMsg(Exceptions::InternalError, "Error occurs read file");
100             }
101
102             if (S_ISDIR(statInfo.st_mode)) {
103                 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
104                             "..") == 0) {
105                     continue;
106                 }
107                 EncryptDirectory(absFileName);
108             } else {
109                 if (isSupportedForEncryption(absFileName)) {
110                     EncryptFile(absFileName);
111                 }
112             }
113         }
114     }
115     while(dirent);
116     if (closedir(pkgDir)) {
117         LogError("Fail to close directory : " << path);
118     }
119 }
120
121 void TaskEncryptResource::EncryptFile(const std::string &fileName)
122 {
123     Try
124     {
125         LogDebug("Need to ecnrypt file Name " << fileName);
126         std::string encFile = fileName + ".enc";
127
128         struct stat buf;
129         int ret = stat(fileName.c_str(), &buf);
130         if(ret == 0) {
131             size_t fileSize = buf.st_size;
132
133             FILE* resFp = fopen(fileName.c_str(), "r");
134             if ( NULL == resFp) {
135                 LogDebug("Couldnot open file : " << fileName);
136                 return;
137             }
138
139             int blockSize = m_resEnc->GetBlockSize(fileSize);
140             LogDebug("Get block size : " << blockSize);
141
142             unsigned char readBuf[fileSize];
143             unsigned char outEncBuf[blockSize];
144             memset(readBuf, 0, fileSize);
145             memset(outEncBuf, 0, blockSize);
146
147             fread(readBuf, sizeof(unsigned char), fileSize, resFp);
148
149             m_resEnc->EncryptChunk(readBuf, outEncBuf, fileSize);
150
151             FILE* encFp = fopen(encFile.c_str(), "w");
152             if (NULL == encFp) {
153                 LogError("Failed to open ecryption file");
154                 return;
155             }
156             fwrite(outEncBuf, sizeof(unsigned char), blockSize, encFp);
157
158             fclose(resFp);
159             fclose(encFp);
160
161             LogDebug("Success to encrypt file");
162             LogDebug("Remove unecrypted file : " << fileName);
163
164             unlink(fileName.c_str());
165             if ((rename(encFile.c_str(), fileName.c_str())) != 0) {
166                 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
167             }
168
169             std::string realPath = fileName;
170             realPath.replace(0, m_context.locations->getTemporaryRootDir().length(),
171                     m_context.locations->getSourceDir());
172
173             WrtDB::EncryptedFileInfo info;
174             info.fileName = DPL::FromUTF8String(realPath);
175             info.fileSize = fileSize;
176
177             m_context.widgetConfig.encryptedFiles.insert(info);
178         }
179     }
180     Catch(ResourceEncryptor::Exception::Base)
181     {
182         ReThrowMsg(Exceptions::ExtractFileFailed, fileName);
183     }
184 }
185 } //namespace WidgetInstall
186 } //namespace Jobs