1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/pkgmgr_registration.h"
7 #include <manifest_parser/utils/logging.h>
8 #include <pkgmgr_installer.h>
9 #include <pkgmgr_parser_db.h>
10 #include <tzplatform_config.h>
15 namespace bf = boost::filesystem;
19 bool RegisterCertificates(
20 const common_installer::CertificateInfo& cert_info,
21 const std::string& pkgid, uid_t uid) {
22 pkgmgr_instcertinfo_h handle;
23 if (pkgmgr_installer_create_certinfo_set_handle(&handle) < 0) {
24 LOG(ERROR) << "Cannot create pkgmgr_instcertinfo_h";
28 const auto& author_cert = cert_info.author_certificate.get();
30 if (pkgmgr_installer_set_cert_value(handle, PM_SET_AUTHOR_SIGNER_CERT,
31 const_cast<char*>(author_cert->getBase64().c_str())) < 0) {
32 pkgmgr_installer_destroy_certinfo_set_handle(handle);
33 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
38 const auto& author_im_cert = cert_info.author_intermediate_certificate.get();
40 if (pkgmgr_installer_set_cert_value(handle, PM_SET_AUTHOR_INTERMEDIATE_CERT,
41 const_cast<char*>(author_im_cert->getBase64().c_str())) < 0) {
42 pkgmgr_installer_destroy_certinfo_set_handle(handle);
43 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
48 const auto& author_root_cert = cert_info.author_root_certificate.get();
49 if (author_root_cert) {
50 if (pkgmgr_installer_set_cert_value(handle, PM_SET_AUTHOR_ROOT_CERT,
51 const_cast<char*>(author_root_cert->getBase64().c_str())) < 0) {
52 pkgmgr_installer_destroy_certinfo_set_handle(handle);
53 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
58 const auto& dist_cert = cert_info.distributor_certificate.get();
60 if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR_SIGNER_CERT,
61 const_cast<char*>(dist_cert->getBase64().c_str())) < 0) {
62 pkgmgr_installer_destroy_certinfo_set_handle(handle);
63 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
68 const auto& dist_im_cert =
69 cert_info.distributor_intermediate_certificate.get();
71 if (pkgmgr_installer_set_cert_value(handle,
72 PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT,
73 const_cast<char*>(dist_im_cert->getBase64().c_str())) < 0) {
74 pkgmgr_installer_destroy_certinfo_set_handle(handle);
75 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
80 const auto& dist_root_cert = cert_info.distributor_root_certificate.get();
82 if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR_ROOT_CERT,
83 const_cast<char*>(dist_root_cert->getBase64().c_str())) < 0) {
84 pkgmgr_installer_destroy_certinfo_set_handle(handle);
85 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
90 const auto& dist2_cert = cert_info.distributor2_certificate.get();
92 if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR2_SIGNER_CERT,
93 const_cast<char*>(dist2_cert->getBase64().c_str())) < 0) {
94 pkgmgr_installer_destroy_certinfo_set_handle(handle);
95 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
100 const auto& dist2_im_cert =
101 cert_info.distributor2_intermediate_certificate.get();
103 if (pkgmgr_installer_set_cert_value(handle,
104 PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT,
105 const_cast<char*>(dist2_im_cert->getBase64().c_str())) < 0) {
106 pkgmgr_installer_destroy_certinfo_set_handle(handle);
107 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
112 const auto& dist2_root_cert = cert_info.distributor2_root_certificate.get();
113 if (dist2_root_cert) {
114 if (pkgmgr_installer_set_cert_value(handle, PM_SET_DISTRIBUTOR2_ROOT_CERT,
115 const_cast<char*>(dist2_root_cert->getBase64().c_str())) < 0) {
116 pkgmgr_installer_destroy_certinfo_set_handle(handle);
117 LOG(ERROR) << "pkgmgrInstallerSetCertValue fail";
122 if (pkgmgr_installer_save_certinfo(pkgid.c_str(), handle, uid) < 0) {
123 pkgmgr_installer_destroy_certinfo_set_handle(handle);
124 LOG(ERROR) << "Failed to save certificate information";
128 pkgmgr_installer_destroy_certinfo_set_handle(handle);
132 // "preload" : this package was installed at the binary creation.
133 // "system" : this package is "preload" and is not removable.
134 // "update" : this package is "preload" but is updated after first installation.
135 // "removable" : this package can be removed.
136 // "readonly" : this package exists in readonly location.
137 bool AssignPackageTags(manifest_x* manifest,
139 if (!strcmp(manifest->preload, "true")) {
140 manifest->removable = strdup("false");
141 manifest->readonly = strdup("true");
142 manifest->system = strdup("true");
144 manifest->update = strdup("true");
146 manifest->update = strdup("false");
148 manifest->removable = strdup("true");
149 manifest->readonly = strdup("false");
150 manifest->system = strdup("false");
151 manifest->update = strdup("false");
157 } // anonymous namespace
159 namespace common_installer {
161 bool RegisterAppInPkgmgr(manifest_x* manifest,
162 const bf::path& xml_path,
163 const std::string& pkgid,
164 const CertificateInfo& cert_info,
166 RequestMode request_mode,
167 const boost::filesystem::path& tep_path) {
168 // Fill "non-xml" elements
169 if (!tep_path.empty())
170 manifest->tep_name = strdup(tep_path.c_str());
172 if (!AssignPackageTags(manifest, false))
175 int ret = request_mode != RequestMode::GLOBAL ?
176 pkgmgr_parser_process_usr_manifest_x_for_installation(manifest,
177 xml_path.c_str(), uid) :
178 pkgmgr_parser_process_manifest_x_for_installation(manifest,
181 LOG(ERROR) << "Failed to insert manifest into pkgmgr, error code=" << ret;
185 if (!RegisterCertificates(cert_info, pkgid, uid)) {
186 LOG(ERROR) << "Failed to register author certificate";
193 bool UpgradeAppInPkgmgr(manifest_x* manifest,
194 const bf::path& xml_path,
195 const std::string& pkgid,
196 const CertificateInfo& cert_info,
198 RequestMode request_mode) {
199 if (!AssignPackageTags(manifest, true))
202 int ret = request_mode != RequestMode::GLOBAL ?
203 pkgmgr_parser_process_usr_manifest_x_for_upgrade(manifest,
204 xml_path.c_str(), uid) :
205 pkgmgr_parser_process_manifest_x_for_upgrade(manifest, xml_path.c_str());
208 LOG(ERROR) << "Failed to update manifest in pkgmgr, error code=" << ret;
212 (void) pkgmgr_installer_delete_certinfo(pkgid.c_str());
213 if (!RegisterCertificates(cert_info, pkgid, uid))
219 bool UnregisterAppInPkgmgr(manifest_x* manifest,
220 const bf::path& xml_path,
221 const std::string& pkgid,
223 RequestMode request_mode) {
224 int ret = request_mode != RequestMode::GLOBAL ?
225 pkgmgr_parser_process_usr_manifest_x_for_uninstallation(manifest,
226 xml_path.c_str(), uid) :
227 pkgmgr_parser_process_manifest_x_for_uninstallation(manifest,
230 LOG(ERROR) << "Failed to delete manifest from pkgmgr, error code=" << ret;
234 // Certificate info may be not present
235 (void) pkgmgr_installer_delete_certinfo(pkgid.c_str());
240 bool UpdateTepInfoInPkgmgr(const bf::path& tep_path, const std::string& pkgid,
241 uid_t uid, RequestMode request_mode) {
242 int ret = request_mode != RequestMode::GLOBAL ?
243 pkgmgr_parser_usr_update_tep(
244 pkgid.c_str(), tep_path.string().c_str(), uid) :
245 pkgmgr_parser_update_tep(
246 pkgid.c_str(), tep_path.string().c_str());
249 LOG(ERROR) << "Failed to upgrade tep info: " << pkgid;
256 bool DisablePkgInPkgmgr(const std::string& pkgid, uid_t uid,
257 RequestMode request_mode) {
258 int ret = request_mode != RequestMode::GLOBAL ?
259 pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid.c_str(), uid, 1) :
260 pkgmgr_parser_update_pkg_disable_info_in_db(pkgid.c_str(), 1);
262 LOG(ERROR) << "Failed to disable pkg: " << pkgid;
269 bool EnablePkgInPkgmgr(const std::string& pkgid, uid_t uid,
270 RequestMode request_mode) {
271 int ret = request_mode != RequestMode::GLOBAL ?
272 pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid.c_str(), uid, 0) :
273 pkgmgr_parser_update_pkg_disable_info_in_db(pkgid.c_str(), 0);
275 LOG(ERROR) << "Failed to enable pkg: " << pkgid;
282 } // namespace common_installer