Rename invalid WRT_SMACK_LABEL macro to WRT_SMACK_ENABLED
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / task_pkg_info_update.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_pkg_info_update.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task information about package
21  * update
22  */
23 #include "task_pkg_info_update.h"
24
25 #include <unistd.h>
26 #include <string>
27
28 #include <fstream>
29 #include <dpl/wrt-dao-ro/global_config.h>
30 #include <dpl/foreach.h>
31 #include <dpl/sstream.h>
32 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
33 #include <pkgmgr_installer.h>
34 #include <pkgmgr/pkgmgr_parser.h>
35 #include <pkgmgr-info.h>
36 #include <vcore/CryptoHash.h>
37
38 #include <widget_install/job_widget_install.h>
39 #include <widget_install/widget_install_context.h>
40 #include <widget_install/widget_install_errors.h>
41
42 #include <installer_log.h>
43
44 using namespace WrtDB;
45
46 namespace {
47 }
48
49 namespace Jobs {
50 namespace WidgetInstall {
51 TaskPkgInfoUpdate::TaskPkgInfoUpdate(InstallerContext& context) :
52     DPL::TaskDecl<TaskPkgInfoUpdate>(this),
53     m_context(context)
54 {
55     AddStep(&TaskPkgInfoUpdate::StartStep);
56     AddStep(&TaskPkgInfoUpdate::StepPkgInfo);
57     AddStep(&TaskPkgInfoUpdate::StepSetCertiInfo);
58     AddStep(&TaskPkgInfoUpdate::EndStep);
59     AddStep(&TaskPkgInfoUpdate::StepSetEndofInstallation);
60
61     AddAbortStep(&TaskPkgInfoUpdate::StepAbortCertiInfo);
62     AddAbortStep(&TaskPkgInfoUpdate::stepAbortParseManifest);
63 }
64
65 void TaskPkgInfoUpdate::StepPkgInfo()
66 {
67     int code = 0;
68     char* updateTags[3] = {NULL, };
69
70     char preloadTrue[] = "preload=true";
71     char removableTrue[] = "removable=true";
72     char removableFalse[] = "removable=false";
73
74     if (InstallMode::InstallTime::CSC == m_context.mode.installTime) {
75         updateTags[0] = preloadTrue;
76         if (m_context.mode.removable) {
77             updateTags[1] = removableTrue;
78         } else {
79             updateTags[1] = removableFalse;
80         }
81         updateTags[2] = NULL;
82     }
83
84     if (m_context.mode.rootPath == InstallMode::RootPath::RO) {
85         m_manifest += "/usr/share/packages/";
86     } else {
87         m_manifest += "/opt/share/packages/";
88     }
89     m_manifest += DPL::ToUTF8String(m_context.widgetConfig.tzPkgid) + ".xml";
90     _D("manifest file : %s", m_manifest.c_str());
91
92     if (m_context.isUpdateMode || (
93                 m_context.mode.rootPath == InstallMode::RootPath::RO
94                 && m_context.mode.installTime == InstallMode::InstallTime::PRELOAD
95                 && m_context.mode.extension == InstallMode::ExtensionType::DIR)) {
96
97         code = pkgmgr_parser_parse_manifest_for_upgrade(
98                 m_manifest.c_str(), (updateTags[0] == NULL) ? NULL : updateTags);
99
100         if (code != 0) {
101             _E("Manifest parser error: %d", code);
102             ThrowMsg(Exceptions::ManifestInvalid, "Parser returncode: " << code);
103         }
104     } else {
105         code = pkgmgr_parser_parse_manifest_for_installation(
106                 m_manifest.c_str(), (updateTags[0] == NULL) ? NULL : updateTags);
107
108         if (code != 0) {
109             _E("Manifest parser error: %d", code);
110             ThrowMsg(Exceptions::ManifestInvalid, "Parser returncode: " << code);
111         }
112     }
113
114     m_context.job->UpdateProgress(
115             InstallerContext::INSTALL_PKGINFO_UPDATE,
116             "Manifest Update Finished");
117     _D("Manifest parsed");
118 }
119
120 void TaskPkgInfoUpdate::StepSetCertiInfo()
121 {
122     _D("StepSetCertiInfo");
123
124     if (pkgmgr_installer_create_certinfo_set_handle(&m_pkgHandle) < 0) {
125         _E("pkgmgrInstallerCreateCertinfoSetHandle fail");
126         ThrowMsg(Exceptions::SetCertificateInfoFailed,
127                  "Failed to create certificate handle");
128     }
129
130     SetCertiInfo(SIGNATURE_AUTHOR);
131     SetCertiInfo(SIGNATURE_DISTRIBUTOR);
132
133     if ((pkgmgr_installer_save_certinfo(
134              const_cast<char*>(DPL::ToUTF8String(
135                                    m_context.widgetConfig.tzPkgid).c_str()),
136              m_pkgHandle)) < 0)
137     {
138         _E("pkgmgrInstallerSaveCertinfo fail");
139         ThrowMsg(Exceptions::SetCertificateInfoFailed,
140                  "Failed to Installer Save Certinfo");
141     } else {
142         _D("Succeed to save Certinfo");
143     }
144
145     if (pkgmgr_installer_destroy_certinfo_set_handle(m_pkgHandle) < 0) {
146         _E("pkgmgrInstallerDestroyCertinfoSetHandle fail");
147     }
148 }
149
150 void TaskPkgInfoUpdate::SetCertiInfo(int source)
151 {
152     _D("Set CertiInfo to pkgmgr : %d", source);
153     CertificateChainList certificateChainList;
154     m_context.widgetSecurity.getCertificateChainList(certificateChainList,
155             (CertificateSource)source);
156
157     FOREACH(it, certificateChainList)
158     {
159         _D("Insert certinfo to pkgmgr structure");
160
161         ValidationCore::CertificateCollection chain;
162
163         if (false == chain.load(*it)) {
164             _E("Chain is broken");
165             ThrowMsg(Exceptions::SetCertificateInfoFailed,
166                      "Failed to Installer Save Certinfo");
167         }
168
169         if (!chain.sort()) {
170             _E("Chain failed at sorting");
171         }
172
173         ValidationCore::CertificateList list = chain.getCertificateList();
174
175         FOREACH(certIt, list)
176         {
177             pkgmgr_instcert_type instCertType;
178
179             if (source == SIGNATURE_DISTRIBUTOR) {
180                 bool distributor1 = false;
181                 if (!(*certIt)->getCommonName().IsNull()) {
182                     std::string
183                         Name(DPL::ToUTF8String(*(*certIt)->getCommonName()));
184                     std::string tizenStr("Tizen");
185                     if (0 == Name.compare(0, tizenStr.length(), tizenStr)) {
186                         distributor1 = true;
187                     }
188                 }
189
190                 if (distributor1) {
191                     _D("Set SIGNATURE_DISTRIBUTOR");
192                     if ((*certIt)->isRootCert()) {
193                         instCertType = PM_SET_DISTRIBUTOR_ROOT_CERT;
194                     } else {
195                         if ((*certIt)->isCA()) {
196                             instCertType = PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
197                         } else {
198                             instCertType = PM_SET_DISTRIBUTOR_SIGNER_CERT;
199                         }
200                     }
201                 } else {
202                     _D("Set SIGNATURE_DISTRIBUTOR2");
203                     if ((*certIt)->isRootCert()) {
204                         instCertType = PM_SET_DISTRIBUTOR2_ROOT_CERT;
205                     } else {
206                         if ((*certIt)->isCA()) {
207                             instCertType = PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
208                         } else {
209                             instCertType = PM_SET_DISTRIBUTOR2_SIGNER_CERT;
210                         }
211                     }
212                 }
213             } else {
214                 _D("set SIGNATURE_AUTHOR");
215                 if ((*certIt)->isRootCert()) {
216                     instCertType = PM_SET_AUTHOR_ROOT_CERT;
217                 } else {
218                     if ((*certIt)->isCA()) {
219                         instCertType = PM_SET_AUTHOR_INTERMEDIATE_CERT;
220                     } else {
221                         instCertType = PM_SET_AUTHOR_SIGNER_CERT;
222                     }
223                 }
224             }
225             _D("cert type : %d", instCertType);
226             if ((pkgmgr_installer_set_cert_value(
227                      m_pkgHandle,
228                      instCertType,
229                      const_cast<char*>(((*certIt)->getBase64()).c_str()))) < 0)
230             {
231                 _E("pkgmgrInstallerSetCertValue fail");
232                 ThrowMsg(Exceptions::SetCertificateInfoFailed,
233                          "Failed to Set CertValue");
234             }
235         }
236     }
237 }
238
239 void TaskPkgInfoUpdate::StepAbortCertiInfo()
240 {
241     if ((pkgmgr_installer_delete_certinfo(
242              const_cast<char*>(DPL::ToUTF8String(
243                                    m_context.widgetConfig.tzPkgid).c_str()))) <
244         0)
245     {
246         _E("pkgmgr_installer_delete_certinfo fail");
247     }
248 }
249
250 void TaskPkgInfoUpdate::StartStep()
251 {
252     _D("--------- <TaskPkgInfoUpdate> : START ----------");
253 }
254
255 void TaskPkgInfoUpdate::EndStep()
256 {
257     m_context.job->UpdateProgress(
258             InstallerContext::INSTALL_SET_CERTINFO,
259             "Save certinfo to pkgmgr");
260
261     _D("--------- <TaskPkgInfoUpdate> : END ----------");
262 }
263
264 void TaskPkgInfoUpdate::stepAbortParseManifest()
265 {
266     _E("[Parse Manifest] Abroting....");
267
268     int code = pkgmgr_parser_parse_manifest_for_uninstallation(
269             m_manifest.c_str(), NULL);
270
271     if (0 != code) {
272         _W("Manifest parser error: %d", code);
273         ThrowMsg(Exceptions::ManifestInvalid, "Parser returncode: " << code);
274     }
275     int ret = unlink(m_manifest.c_str());
276     if (0 != ret) {
277         _W("No manifest file found: %s", m_manifest.c_str());
278     }
279 }
280
281 void TaskPkgInfoUpdate::StepSetEndofInstallation()
282 {
283     m_context.job->UpdateProgress(
284         InstallerContext::INSTALL_END,
285         "End installation");
286 }
287
288 } //namespace WidgetInstall
289 } //namespace Jobs