4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
30 #include <openssl/sha.h>
31 #include <openssl/evp.h>
34 #include <sys/smack.h>
37 #include <pkgmgr-info.h>
38 #include <pkgmgr_parser.h>
39 #include <privilege-control.h>
40 #include <privilege_manager.h>
41 #include <app_manager.h>
42 #include <app2ext_interface.h>
43 #include <package-manager.h>
45 #include "coretpk-installer-internal.h"
46 #include "coretpk-installer-type.h"
47 /*use rpm-installer exceptions*/
48 #include "rpm-installer-util.h"
49 /*because the logic of coretpk and rpm are similar, use rpm functions.*/
50 #include "rpm-installer.h"
51 #include "rpm-installer-type.h"
53 extern pkgmgr_installer *pi;
54 extern GList *privilege_list;
56 int _coretpk_installer_get_group_id(char *pkgid, char **result);
57 void _coretpk_installer_set_privilege_setup_path(char *pkgid, char *dirpath, app_path_type_t type, char *label);
59 int __coretpk_patch_padded_api_version(const char *api_version, char **pad_api_version)
61 char *pad_version = NULL;
65 pad_version = strdup(api_version);
66 if (pad_version == NULL) {
67 _LOGE("out of memory");
68 return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
71 ptr_fw = strchr(pad_version, '.');
72 ptr_bw = strrchr(pad_version, '.');
74 if (ptr_fw && ptr_bw) {
75 if (ptr_fw == ptr_bw) {
76 pad_version = strncat(pad_version, ".0", BUF_SIZE - strlen(pad_version) - 1);
80 *pad_api_version = pad_version;
82 return RPM_INSTALLER_SUCCESS;
85 static int __coretpk_compare_with_platform_version(const char *api_version)
87 char *current_version = NULL;
88 char *platform_version = NULL;
93 _LOGE("Invalid parameter");
94 return RPM_INSTALLER_ERR_WRONG_PARAM;
97 if (strlen(api_version) == 0) {
98 _LOGD("No api-version, assume same with platform api-version");
99 return RPM_INSTALLER_SUCCESS;
102 ret = __coretpk_patch_padded_api_version(api_version, ¤t_version);
103 if (ret != RPM_INSTALLER_SUCCESS) {
104 return RPM_INSTALLER_ERR_WRONG_PARAM;
107 ret = __coretpk_patch_padded_api_version(TIZEN_VERSION, &platform_version);
108 if (ret != RPM_INSTALLER_SUCCESS) {
109 return RPM_INSTALLER_ERR_WRONG_PARAM;
112 _LOGD("platform_version(%s) vs. current_version(%s)", platform_version, current_version);
114 result = strverscmp(platform_version, current_version);
116 ret = RPM_INSTALLER_ERR_NOT_SUPPORTED_API_VERSION;
118 ret = RPM_INSTALLER_SUCCESS;
121 FREE_AND_NULL(current_version);
122 FREE_AND_NULL(platform_version);
127 static int __get_unzip_size(const char *item, unsigned long long *size)
129 if (!item || !size) {
130 _LOGE("Invalid argument.");
131 return PMINFO_R_ERROR;
134 unzFile uzf = unzOpen64(item);
137 _LOGE("Fail to open item : %s", item);
139 return PMINFO_R_ERROR;
141 ret = unzGoToFirstFile(uzf);
143 _LOGE("error get first zip file ");
146 return PMINFO_R_ERROR;
149 ret = unzOpenCurrentFile(uzf);
151 _LOGE("error unzOpenCurrentFile ");
154 return PMINFO_R_ERROR;
157 unz_file_info fileInfo = {0};
158 char* filename = (char*) calloc(1, BUF_SIZE);
159 ret= unzGetCurrentFileInfo(uzf, &fileInfo, filename, (BUF_SIZE-1), NULL, 0, NULL, 0);
160 *size = (unsigned long long)fileInfo.uncompressed_size + *size;
162 _LOGE("error get current file info");
163 unzCloseCurrentFile(uzf);
172 }while(unzGoToNextFile(uzf) == UNZ_OK);
180 static int __is_default_external_storage()
184 int mmc_status = VCONFKEY_SYSMAN_MMC_REMOVED;
186 ret = vconf_get_int("db/setting/default_memory/install_applications", &storage);
187 retvm_if(ret != 0, PMINFO_R_ERROR, "vconf_get_int(db/setting/default_memory/install_applications) is failed.");
190 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_status);
191 retvm_if(ret != 0, PMINFO_R_ERROR, "vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) is failed.");
193 if((mmc_status == VCONFKEY_SYSMAN_MMC_REMOVED) || (mmc_status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED)) {
194 _LOGD("mmc_status is MMC_REMOVED or NOT_MOUNTED.");
196 _LOGD("mmc_status is MMC_MOUNTED.");
201 return PMINFO_R_ERROR;
204 static void __apply_smack_for_mmc(const char *pkgid)
206 char dirpath[BUF_SIZE] = {'\0'};
208 snprintf(dirpath, BUF_SIZE, "%s/%s/.mmc", OPT_USR_APPS, pkgid);
209 if (access(dirpath, F_OK) != 0) {
210 _LOGE("Cannot access to [%s].", dirpath);
213 memset(dirpath, '\0', BUF_SIZE);
216 snprintf(dirpath, BUF_SIZE, "%s/.mmc/bin", pkgid);
217 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
218 memset(dirpath, '\0', BUF_SIZE);
221 snprintf(dirpath, BUF_SIZE, "%s/.mmc/lib", pkgid);
222 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
223 memset(dirpath, '\0', BUF_SIZE);
225 // [pkgid]/.mmc/lost+found
226 snprintf(dirpath, BUF_SIZE, "%s/.mmc/lost+found", pkgid);
227 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
228 memset(dirpath, '\0', BUF_SIZE);
231 snprintf(dirpath, BUF_SIZE, "%s/.mmc/res", pkgid);
232 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
233 memset(dirpath, '\0', BUF_SIZE);
238 static int __pre_upgrade_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
241 unsigned long long archive_size_byte = 0;
242 int archive_size_mega = 0;
244 ret = __is_default_external_storage();
246 _LOGD("Upgrade storage is internal.");
249 _LOGD("__pre_upgrade start.");
251 ret = __get_unzip_size(pkgfile, &archive_size_byte);
253 _LOGD("Failed to get uncompressed size.");
254 return PMINFO_R_ERROR;
256 archive_size_mega = archive_size_byte / (1024 * 1024) + 1;
257 _LOGD("Uncompressed size is converted from [%lld]bytes to [%d]Mb.", archive_size_byte, archive_size_mega);
259 *handle = app2ext_init(APP2EXT_SD_CARD);
260 if (*handle == NULL) {
261 _LOGE("@app2ext init failed\n");
262 return PMINFO_R_ERROR;
264 if ((&((*handle)->interface) != NULL) && ((*handle)->interface.pre_upgrade != NULL) && ((*handle)->interface.post_upgrade != NULL) &&
265 ((*handle)->interface.disable != NULL)) {
266 ret = (*handle)->interface.disable(pkgid);
267 if (ret != APP2EXT_SUCCESS) {
268 _LOGE("Unmount ret[%d]", ret);
270 *dir_list = __rpm_populate_dir_list();
271 if (*dir_list == NULL) {
272 _LOGE("@ \nError in populating the directory list\n");
273 return PMINFO_R_ERROR;
275 ret = (*handle)->interface.pre_upgrade(pkgid, *dir_list, archive_size_mega);
276 if (ret == APP2EXT_ERROR_MMC_STATUS) {
277 _LOGE("@app2xt MMC is not here, go internal\n");
278 } else if (ret == APP2EXT_SUCCESS){
279 _LOGD("@pre_upgrade done, go internal\n");
282 _LOGE("@app2xt pre upgrade API failed (%d)\n", ret);
283 return PMINFO_R_ERROR;
286 _LOGE("handle is not proper.");
287 return PMINFO_R_ERROR;
289 _LOGD("__pre_upgrade end.");
293 static int __post_upgrade_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list)
295 int ret = __is_default_external_storage();
297 _LOGD("Upgrade storage is internal.");
300 _LOGD("__post_upgrade start.");
302 /* set smack again for .mmc folder */
303 __apply_smack_for_mmc(pkgid);
304 _LOGD("__apply_smack_for_mmc is completed.");
306 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)) {
307 __rpm_clear_dir_list(dir_list);
308 handle->interface.post_upgrade(pkgid, APP2EXT_STATUS_SUCCESS);
309 app2ext_deinit(handle);
311 _LOGE("handle->interface.post_upgrade is NULL.");
312 return PMINFO_R_ERROR;
314 _LOGD("__post_upgrade end.");
318 static int __pre_install_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
321 unsigned long long archive_size_byte = 0;
322 int archive_size_mega = 0;
324 ret = __is_default_external_storage();
326 _LOGD("Installed storage is internal.");
329 _LOGD("__pre_install start.");
331 ret = __get_unzip_size(pkgfile, &archive_size_byte);
333 _LOGD("Failed to get uncompressed size.");
334 return PMINFO_R_ERROR;
336 archive_size_mega = archive_size_byte / (1024 * 1024) + 1;
337 _LOGD("Uncompressed size is converted from [%lld]bytes to [%d]Mb.", archive_size_byte, archive_size_mega);
339 *handle = app2ext_init(APP2EXT_SD_CARD);
340 if (*handle == NULL) {
341 _LOGE("@app2ext init failed\n");
342 return PMINFO_R_ERROR;
344 if ((&((*handle)->interface) != NULL) && ((*handle)->interface.pre_install != NULL) && ((*handle)->interface.post_install != NULL)
345 && ((*handle)->interface.force_clean != NULL)) {
346 ret = (*handle)->interface.force_clean(pkgid);
347 if (ret != APP2EXT_SUCCESS) {
348 _LOGE("Force clean is failed. pkgid[%s] ret[%d]", pkgid, ret);
349 return PMINFO_R_ERROR;
351 _LOGD("Force clean is OK");
352 *dir_list = __rpm_populate_dir_list();
353 if (*dir_list == NULL) {
354 _LOGE("@ \nError in populating the directory list\n");
355 return PMINFO_R_ERROR;
357 ret = (*handle)->interface.pre_install(pkgid, *dir_list, archive_size_mega);
358 if (ret == APP2EXT_ERROR_MMC_STATUS) {
359 _LOGE("@app2xt MMC is not here, go internal\n");
360 } else if (ret == APP2EXT_SUCCESS){
361 _LOGD("@pre_install done, go internal\n");
364 _LOGE("@app2xt pre install API failed (%d)\n", ret);
365 return PMINFO_R_ERROR;
368 _LOGE("handle is not proper.");
369 return PMINFO_R_ERROR;
371 _LOGD("__pre_install end.");
375 static int __post_install_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list, int install_status)
377 int ret = __is_default_external_storage();
379 _LOGD("Installed storage is internal.");
382 _LOGD("__post_install start.");
384 /* set smack again for .mmc folder */
385 __apply_smack_for_mmc(pkgid);
386 _LOGD("__apply_smack_for_mmc is completed.");
388 if ((handle != NULL) && (handle->interface.post_install != NULL)) {
389 __rpm_clear_dir_list(dir_list);
390 handle->interface.post_install(pkgid, install_status);
391 app2ext_deinit(handle);
393 _LOGE("handle->interface.post_install is NULL.");
394 return PMINFO_R_ERROR;
396 _LOGD("__post_install end.");
400 static void __str_trim(char *input)
402 char *trim_str = input;
407 while (*input != 0) {
408 if (!isspace(*input)) {
419 static char *__find_info_from_xml(const char *manifest, const char *find_info)
421 const char *val = NULL;
423 xmlTextReaderPtr reader;
424 char *info_val = NULL;
427 if(manifest == NULL) {
428 _LOGE("input argument is NULL\n");
432 if(find_info == NULL) {
433 _LOGE("find_info is NULL\n");
437 reader = xmlReaderForFile(manifest, NULL, 0);
440 if (_child_element(reader, -1)) {
441 node = xmlTextReaderConstName(reader);
443 _LOGE("xmlTextReaderConstName value is NULL\n");
447 if (!strcmp(ASCII(node), "manifest")) {
448 ret = _ri_get_attribute(reader,(char*)find_info,&val);
450 _LOGE("error in getting the attribute value");
455 info_val = strdup(val);
456 if(info_val == NULL) {
457 _LOGE("malloc failed!!");
461 _LOGE("unable to create xml reader\n");
465 _LOGE("xmlReaderForFile value is NULL\n");
470 xmlFreeTextReader(reader);
479 static int __coretpk_privilege_func(const char *name, void *user_data)
482 const char *perm[] = {NULL, NULL};
483 const char *ug_pkgid = "ui-gadget::client";
487 _LOGD("privilege = [%s]", name);
488 _ri_privilege_register_package("ui-gadget::client");
490 ret = _ri_privilege_enable_permissions(ug_pkgid, PERM_APP_TYPE_EFL, perm, 1);
491 _LOGE("add ug privilege(%s, %s, %d) done.", ug_pkgid, name, ret);
496 static int __ui_gadget_func(const pkgmgrinfo_appinfo_h handle, void *user_data)
502 char appdir[BUF_SIZE] = {'\0'};
504 ret = pkgmgrinfo_appinfo_is_ui_gadget(handle, &is_ug);
505 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get is_ui_gadget.\n");
510 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
511 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get pkgid\n");
513 _LOGD("@[%s] has ui-gadget", pkgid);
515 /*check bin directory*/
516 snprintf(appdir, BUF_SIZE, "%s/%s/bin", USR_APPS, pkgid);
517 if (access(appdir, F_OK) != 0) {
519 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
521 _LOGL("mkdir()", errno);
527 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
528 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get exec\n");
530 /*make symlink to exec*/
531 const char *ln_argv[] = { "/bin/ln", "-sf", "/usr/bin/ug-client", exec, NULL };
532 ret = _ri_xsystem(ln_argv);
533 retvm_if(ret < 0, RPM_INSTALLER_ERR_INTERNAL, "Failed to exec ln_argv\n");
535 _LOGD("@[%s] success symlink to [/usr/bin/ug-client]", exec);
537 * (bool *) user_data = true;
543 static int __check_updated_system_package(const char *pkgid)
546 bool is_update = false;
547 bool is_system = false;
548 pkgmgrinfo_pkginfo_h pkghandle = NULL;
550 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
551 retvm_if(ret < 0, -1, "pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
553 ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
554 tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_system(%s) failed.", pkgid);
556 ret = pkgmgrinfo_pkginfo_is_update(pkghandle, &is_update);
557 tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_update(%s) failed.", pkgid);
559 if (is_system && is_update) {
560 _LOGD("pkgid=[%s] is updated system package.", pkgid);
563 _LOGD("pkgid=[%s] is not updated system app.", pkgid);
568 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
573 static int __pkg_remove_update(const char *pkgid)
576 char buff[BUF_SIZE] = {'\0'};
577 char rootpath[BUF_SIZE] = {'\0'};
580 _LOGE("pkgid is NULL.");
585 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
587 // remove dir for clean (/opt/usr/apps/[pkgid])
588 snprintf(rootpath, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
589 if (__is_dir(rootpath)) {
590 _rpm_delete_dir(rootpath);
593 // Remove origin rule
594 _ri_privilege_unregister_package(pkgid);
596 // unzip pkg path from factory-reset data
597 memset(rootpath, '\0', BUF_SIZE);
598 snprintf(rootpath, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
599 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, rootpath, "-d", "/", NULL };
600 ret = _ri_xsystem(pkg_argv);
602 _LOGE("/usr/bin/unzip(%s) failed.", rootpath);
605 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
608 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
612 memset(buff, '\0', BUF_SIZE);
613 snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
615 _LOGD("manifest = [%s].",buff);
617 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
619 _LOGE("pkgmgr_parser_parse_manifest_for_upgrade(%s) is failed.", pkgid);
622 _LOGD("pkgmgr_parser_parse_manifest_for_upgrade() is ok.");
624 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
626 // apply smack for pkg root path
627 memset(buff, '\0', BUF_SIZE);
628 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
629 _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
631 // apply smack for defined directory
632 __rpm_apply_smack((char*)pkgid, 0);
635 ret = _ri_apply_privilege((char*)pkgid, 0);
637 _LOGE("_ri_apply_privilege(%s) failed. ret = [%d]", pkgid, ret);
639 _LOGD("_ri_apply_privilege(%s) success.", pkgid);
643 ret = _ri_smack_reload(pkgid, UPGRADE_REQ);
645 _LOGE("_ri_smack_reload(%s) failed.", pkgid);
650 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
652 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
653 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
659 int _coretpk_installer_remove_db_info(const char *pkgid)
662 pkgmgrinfo_pkginfo_h handle = NULL;
664 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
668 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
669 tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_parser_parse_manifest_for_uninstallation is failed, pkgid=[%s]", pkgid);
671 _LOGD("Remove db info is OK.");
674 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
679 int _coretpk_installer_set_smack_label_access(const char *path, const char *label)
681 int res = smack_lsetlabel(path, label, SMACK_LABEL_ACCESS);
684 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", label, res, path);
690 int _coretpk_installer_get_smack_label_access(const char *path, char **label)
692 int res = smack_lgetlabel(path, label, SMACK_LABEL_ACCESS);
695 _LOGE("Error in getting smack ACCESS label failed. result[%d] (path:[%s]))", res, path);
701 int _coretpk_installer_set_smack_label_transmute(const char *path, const char *flag)
703 int res = smack_lsetlabel(path, flag, SMACK_LABEL_TRANSMUTE);
706 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", flag, res, path);
712 int _coretpk_installer_verify_privilege_list(const char *pkg_id, GList *privilege_list, int visibility, const char *api_version)
714 char *error_privilege_name = NULL;
717 char buf[BUF_SIZE] = { '\0' };
719 ret = privilege_manager_verify_privilege(api_version, PRVMGR_PACKAGE_TYPE_CORE, privilege_list, visibility, &error_privilege_name);
720 if (ret != PRVMGR_ERR_NONE) {
721 _LOGE("privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) failed. ret = [%d][%s]", ret, error_privilege_name);
722 fprintf(stdout, "\n verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) failed. [%d][%s]\n", ret, error_privilege_name);
724 if (!error_privilege_name)
725 error_privilege_name = strdup("Unidentified privilege error");
727 if (strstr(error_privilege_name, "[DEPRECATED_PRIVILEGE]") != NULL)
728 ret = RPM_INSTALLER_ERR_PRIVILEGE_USING_LEGACY_FAILED;
729 else if (strstr(error_privilege_name, "[NO_EXIST_PRIVILEGE]") != NULL)
730 ret = RPM_INSTALLER_ERR_PRIVILEGE_UNKNOWN;
731 else if (strstr(error_privilege_name, "[MISMATCHED_PRIVILEGE_LEVEL]") != NULL)
732 ret = RPM_INSTALLER_ERR_PRIVILEGE_UNAUTHORIZED;
734 _LOGE("Unidentified privilege error : [%s]", error_privilege_name);
735 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
738 snprintf(buf, BUF_SIZE - 1, "%d:%s", ret, error_privilege_name);
739 _ri_broadcast_privilege_notification(pkg_id, "coretpk", "error", buf);
741 _LOGD("privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
745 if (error_privilege_name)
746 free(error_privilege_name);
748 list = g_list_first(privilege_list);
753 list = g_list_next(list);
755 g_list_free(privilege_list);
756 privilege_list = NULL;
761 void _coretpk_installer_search_ui_gadget(const char *pkgid)
764 bool is_ug_pkg = false;
765 pkgmgrinfo_pkginfo_h pkghandle = NULL;
767 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
768 retm_if(ret < 0, "@Failed to get the pkginfo handle.");
771 ret = pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ui_gadget_func, &is_ug_pkg);
772 tryvm_if(ret < 0, ret = RPM_INSTALLER_ERR_INTERNAL, "Fail to get applist");
774 /*if there is ug app, apply privilege*/
775 if (is_ug_pkg == true) {
776 ret = pkgmgrinfo_pkginfo_foreach_privilege(pkghandle, __coretpk_privilege_func, NULL);
777 tryvm_if(ret < 0, ret = RPM_INSTALLER_ERR_INTERNAL, "Fail to get privilege list");
781 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
784 int _coretpk_backend_interface(const char *reqcommand, const ri_frontend_cmdline_arg *data)
786 if (reqcommand == NULL || data == NULL) {
787 _LOGE("reqcommand or data is NULL.");
791 if (strncmp(reqcommand, CORETPK_INSTALL, strlen(CORETPK_INSTALL)) == 0) {
792 return _coretpk_installer_prepare_package_install(data->pkgid, data->clientid);
793 } else if (strncmp(reqcommand, CORETPK_UNINSTALL, strlen(CORETPK_UNINSTALL)) == 0) {
794 return _coretpk_installer_prepare_package_uninstall(data->pkgid);
795 } else if (strncmp(reqcommand, CORETPK_DIRECTORY_INSTALL, strlen(CORETPK_DIRECTORY_INSTALL)) == 0) {
796 return _coretpk_installer_prepare_directory_install(data->pkgid, data->clientid);
797 } else if (strncmp(reqcommand, CORETPK_MOVE, strlen(CORETPK_MOVE)) == 0) {
798 return _coretpk_installer_package_move(data->pkgid, data->move_type);
799 } else if (strncmp(reqcommand, CORETPK_REINSTALL, strlen(CORETPK_REINSTALL)) == 0) {
800 return _coretpk_installer_package_reinstall(data->pkgid, data->clientid);
807 static char * _coretpk_installer_get_pkg_path(const char *pkg_path, const char *pkgid)
810 char buff[BUF_SIZE] = {'\0'};
811 char *real_path = NULL;
813 snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
815 if (__is_dir(buff)) break;
816 memset(buff, '\0', BUF_SIZE);
817 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
818 if (__is_dir(buff)) break;
819 memset(buff, '\0', BUF_SIZE);
820 snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
821 if (__is_dir(buff)) break;
822 memset(buff, '\0', BUF_SIZE);
823 snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS, pkgid);
824 if (__is_dir(buff)) break;
829 _LOGE("chdir() failed [%s]\n", strerror(errno));
833 real_path = (char *)malloc(strlen(buff) + 1);
834 if (real_path == NULL) {
835 _LOGE("Malloc failed!\n");
838 memset(real_path, '\0', strlen(buff) + 1);
839 memcpy(real_path, buff, strlen(buff));
845 int _coretpk_installer_verify_signatures(const char *root_path, const char *pkgid, int *visibility)
848 char buff[BUF_SIZE] = {'\0'};
849 const char *pkg_path = root_path;
851 _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
854 // check for signature and certificate
855 pkg_path = _coretpk_installer_get_pkg_path(root_path, pkgid);
856 if (pkg_path == NULL) {
857 _LOGE("pkg_path is NULL.");
862 ret = chdir(root_path);
864 _LOGE("chdir(%s) failed. [%s]", root_path, strerror(errno));
867 // author-signature.xml is mandatory
868 snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
869 if (access(buff, F_OK) == 0) {
870 _LOGD("author-signature.xml is found, path=[%s]", buff);
871 ret = _ri_verify_sig_and_cert(buff, visibility);
873 _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
877 _LOGD("_ri_verify_sig_and_cert succeed, path=[%s]", buff);
879 _LOGE("cannot access xml, path=[%s]", buff);
883 memset(buff, '\0', BUF_SIZE);
885 // signature1.xml is mandatory
886 snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
887 if (access(buff, F_OK) == 0) {
888 _LOGD("signature1.xml is found, path=[%s]", pkg_path);
889 ret = _ri_verify_sig_and_cert(buff, visibility);
891 _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
895 _LOGD("_ri_verify_sig_and_cert() succeed, path=[%s]", buff);
897 _LOGE("cannot access xml, path=[%s]", buff);
901 memset(buff, '\0', BUF_SIZE);
915 char* _coretpk_installer_load_directory(char *directory,char *pkgfile)
919 struct dirent *result;
922 char *pkgname = NULL;
923 char xml_file[MAX_BUF_SIZE] = {'\0'};
925 buf = malloc(BUF_SIZE);
927 _LOGE("malloc() failed.");
931 dir = opendir(directory);
933 _LOGL("opendir()", errno);
938 _LOGD("loading manifest files, directory=[%s]", directory);
940 for (ret = readdir_r(dir, &entry, &result);
941 ret == 0 && result != NULL;
942 ret = readdir_r(dir, &entry, &result)) {
943 char *manifest = NULL;
945 if (!strcmp(entry.d_name, ".") ||
946 !strcmp(entry.d_name, "..")) {
950 manifest = _manifest_to_package(entry.d_name);
952 _LOGE("failed to convert file to xml, file=[%s].", entry.d_name);
956 memset(xml_file,'\0',MAX_BUF_SIZE);
957 snprintf(xml_file,MAX_BUF_SIZE-1,"%s/%s", directory, manifest);
958 _LOGD("manifest=[%s], path=[%s]", manifest, xml_file);
960 ret = _get_package_name_from_xml(xml_file, &pkgname);
961 if (ret != PMINFO_R_OK || pkgname == NULL) {
962 _LOGE("unable to read, xml_file=[%s]", xml_file);
967 if (pkgname[0] != '\0') {
968 snprintf(buf, BUF_SIZE, "%s/%s", directory, manifest);
987 pkginfo *_coretpk_installer_get_pkg_info(char *manifest)
989 pkginfo *info = NULL;
990 char *tmp_pkgid = NULL;
991 char *tmp_version = NULL;
992 char *tmp_api_version = NULL;
993 char buff[BUF_SIZE] = {'\0'};
995 if (manifest == NULL || strlen(manifest) >= BUF_SIZE) {
996 _LOGE("invalid parameter");
1000 info = calloc(1, sizeof(pkginfo));
1002 _LOGE("calloc() failed.");
1006 strcpy(buff, manifest);
1007 tmp_pkgid = __find_info_from_xml(buff, "package");
1008 if (tmp_pkgid != NULL) {
1009 strncpy(info->package_name, tmp_pkgid, sizeof(info->package_name) - 1);
1012 _LOGE("can not get pkgid");
1016 tmp_version = __find_info_from_xml(buff, "version");
1017 if (tmp_version != NULL) {
1018 strncpy(info->version, tmp_version, sizeof(info->version) - 1);
1021 _LOGE("can not get version");
1025 tmp_api_version = __find_info_from_xml(buff, "api-version");
1026 if (tmp_version != NULL) {
1027 strncpy(info->api_version, tmp_api_version, sizeof(info->api_version) - 1);
1028 free(tmp_api_version);
1030 _LOGE("can not get api-version");
1033 _LOGD("pkgid=[%s], version=[%s], api-version=[%s]", info->package_name, info->version, info->api_version);
1039 pkginfo *_coretpk_installer_get_pkgfile_info(char *pkgfile)
1041 pkginfo *info = NULL;
1043 char cwd[BUF_SIZE] = {'\0'};
1044 char buff[BUF_SIZE] = {'\0'};
1045 char manifest[BUF_SIZE] = { '\0'};
1048 char *tmp_pkgid = NULL;
1049 char *tmp_version = NULL;
1050 char *tmp_api_version = NULL;
1052 temp = getcwd(cwd, BUF_SIZE);
1053 if ((temp == NULL) || (cwd[0] == '\0')) {
1054 _LOGL("getcwd()", errno);
1058 ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_644);
1060 if (access(TEMP_DIR, F_OK) == 0) {
1061 _rpm_delete_dir(TEMP_DIR);
1062 ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_644);
1064 _LOGL("mkdir()", errno);
1068 _LOGL("access()", errno);
1073 ret = chdir(TEMP_DIR);
1075 _LOGL("chdir()", errno);
1079 _LOGD("switched to %s", TEMP_DIR);
1081 snprintf(manifest, BUF_SIZE, "%s", CORETPK_XML);
1082 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, manifest, "-d", TEMP_DIR, NULL };
1083 ret = _ri_xsystem(unzip_argv);
1085 _LOGE("cannot find manifest in the package.");
1086 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
1090 char* manifestpath = _coretpk_installer_load_directory(TEMP_DIR, pkgfile);
1091 if (manifestpath != NULL && strlen(manifestpath) < BUF_SIZE) {
1092 strcpy(buff, manifestpath);
1096 if (buff[0] == '\0') {
1097 _LOGE("cannot find manifest in the package.");
1101 _LOGD("manifest file=[%s]",buff);
1103 info = calloc(1, sizeof(pkginfo));
1105 _LOGE("calloc() failed.");
1109 tmp_pkgid = __find_info_from_xml(buff, "package");
1110 if (tmp_pkgid != NULL) {
1111 strncpy(info->package_name, tmp_pkgid, sizeof(info->package_name) - 1);
1114 _LOGE("can not get pkgid");
1118 tmp_version = __find_info_from_xml(buff, "version");
1119 if (tmp_version != NULL) {
1120 strncpy(info->version, tmp_version, sizeof(info->version) - 1);
1123 _LOGE("can not get version");
1127 tmp_api_version = __find_info_from_xml(buff, "api-version");
1128 if (tmp_version != NULL) {
1129 strncpy(info->api_version, tmp_api_version, sizeof(info->api_version) - 1);
1130 free(tmp_api_version);
1132 _LOGE("can not get api-version");
1135 _LOGD("pkgid=[%s], version=[%s], api-version=[%s]", info->package_name, info->version, info->api_version);
1137 info->is_widget = _coretpk_parser_is_widget(buff);
1140 _rpm_delete_dir(TEMP_DIR);
1144 _LOGL("chdir()", errno);
1150 int _coretpk_installer_get_configuration_value(char *value)
1152 char buffer[BUF_SIZE] = {'\0'};
1158 if (access(CORETPK_CONFIG_PATH, F_OK) != 0) {
1159 /* if there is no ini file, signature has to be checked */
1163 fi = fopen(CORETPK_CONFIG_PATH, "r");
1165 _LOGL("fopen()", errno);
1169 while (fgets(buffer, BUF_SIZE, fi) != NULL) {
1170 /* buffer will be like signature=off\n\0*/
1171 if (strncmp(buffer, value, strlen(value)) == 0) {
1172 len = strlen(buffer);
1173 /*remove newline character*/
1174 buffer[len - 1] = '\0';
1175 p = strchr(buffer, '=');
1178 if (strcmp(p, "on") == 0) {
1193 int _coretpk_installer_apply_file_policy(char *filepath)
1197 if (access(filepath, F_OK) == 0) {
1199 ret = chmod(filepath, FILE_PERMISSION_644);
1201 _LOGL("chmod()", errno);
1204 _LOGE("skip! empty filepath=[%s]", filepath);
1210 int _coretpk_installer_apply_directory_policy(char *dirpath, int mode, bool appowner)
1214 struct dirent entry;
1215 struct dirent *result;
1216 char fullpath[BUF_SIZE] = {'\0'};
1218 if (access(dirpath, F_OK) != 0) {
1219 _LOGE("skip! empty dirpath=[%s]", dirpath);
1223 dir = opendir(dirpath);
1225 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1230 ret = _coretpk_installer_change_mode(dirpath, DIRECTORY_PERMISSION_755);
1232 _LOGE("_coretpk_installer_change_mode is failed, dirpath=[%s]", dirpath);
1235 for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
1236 if (strcmp(entry.d_name, ".") == 0) {
1237 snprintf(fullpath, BUF_SIZE, "%s/", dirpath);
1238 if (appowner == true) {
1239 _coretpk_installer_change_directory_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1241 ret = _coretpk_installer_change_mode(fullpath, DIRECTORY_PERMISSION_755);
1243 _LOGE("_coretpk_installer_change_mode is failed, fullpath=[%s]", fullpath);
1246 } else if (strcmp(entry.d_name, "..") == 0) {
1251 if (entry.d_type == DT_DIR) {
1252 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1255 if (appowner == true) {
1256 ret = _coretpk_installer_change_directory_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1258 _LOGE("_coretpk_installer_change_directory_owner failed, fullpath=[%s]", fullpath);
1263 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1265 // permission(input mode)
1266 ret = _coretpk_installer_change_mode(fullpath, mode);
1268 _LOGE("_coretpk_installer_change_mode failed, fullpath=[%s]", fullpath);
1272 if (appowner == true) {
1273 ret = _coretpk_installer_change_file_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1275 _LOGE("_coretpk_installer_change_file_owner failed, fullpath=[%s]", fullpath);
1281 if (entry.d_type == DT_DIR) {
1282 ret = _coretpk_installer_apply_directory_policy(fullpath, mode, appowner);
1284 _LOGE("_coretpk_installer_apply_directory_policy failed, fullpath=[%s]", fullpath);
1287 memset(fullpath, '\0', BUF_SIZE);
1295 int _coretpk_installer_make_directory_for_ext(char *pkgid)
1297 char ext_pkg_base_path[BUF_SIZE] = {0, };
1298 char temp_path[BUF_SIZE] = {0, };
1299 char pkg_shared_data_path[BUF_SIZE] = {0, };
1300 char *shared_data_label = NULL;
1303 if (access(OPT_STORAGE_SDCARD, F_OK) != 0) {
1304 _LOGL("There is no OPT_STORAGE_SDCARD", errno);
1309 if (access(OPT_STORAGE_SDCARD_APP_ROOT, F_OK) != 0) {
1311 res = mkdir(OPT_STORAGE_SDCARD_APP_ROOT, DIRECTORY_PERMISSION_755);
1313 _LOGL("mkdir()", errno);
1319 snprintf(ext_pkg_base_path, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
1320 res = mkdir(ext_pkg_base_path, 0500);
1321 if (res == -1 && errno != EEXIST)
1323 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1327 res = _coretpk_installer_set_smack_label_access(ext_pkg_base_path, "_");
1330 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1335 memset(temp_path, 0, BUF_SIZE);
1336 strcpy(temp_path, ext_pkg_base_path);
1337 strncat(temp_path, "/data", strlen("/data"));
1338 res = mkdir(temp_path, 0700);
1339 if (res == -1 && errno != EEXIST)
1341 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1344 res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1347 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1352 memset(temp_path, 0, BUF_SIZE);
1353 strcpy(temp_path, ext_pkg_base_path);
1354 strncat(temp_path, "/cache", strlen("/cache"));
1355 res = mkdir(temp_path, 0700);
1356 if (res == -1 && errno != EEXIST)
1358 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1361 res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1364 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1369 memset(temp_path, 0, BUF_SIZE);
1370 strcpy(temp_path, ext_pkg_base_path);
1371 strncat(temp_path, "/shared", strlen("/shared"));
1372 res = mkdir(temp_path, 0500);
1373 if (res == -1 && errno != EEXIST)
1375 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1378 res = _coretpk_installer_set_smack_label_access(temp_path, "_");
1381 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1385 snprintf(pkg_shared_data_path, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid , "shared/data");
1387 res = access(pkg_shared_data_path, F_OK);
1390 _LOGD("Exist shared/data folder (path:[%s])", pkg_shared_data_path);
1391 res = _coretpk_installer_get_smack_label_access(pkg_shared_data_path, &shared_data_label);
1394 _LOGE("_coretpk_installer_get_smack_label_access() is failed.");
1399 strncat(temp_path, "/data", strlen("/data"));
1400 res = mkdir(temp_path, 0705);
1401 if (res == -1 && errno != EEXIST)
1403 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1407 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1410 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1414 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1417 _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1422 memset(temp_path, 0, BUF_SIZE);
1423 strcpy(temp_path, ext_pkg_base_path);
1424 strncat(temp_path, "/shared", strlen("/shared"));
1425 strncat(temp_path, "/cache", strlen("/cache"));
1426 res = mkdir(temp_path, 0700);
1427 if (res == -1 && errno != EEXIST)
1429 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1432 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1435 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1438 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1441 _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1446 else if (res == -1 && errno == ENOENT)
1448 _LOGD("Directory dose not exist. path: %s, errno: %d (%s)",
1449 pkg_shared_data_path, errno, strerror(errno));
1454 _LOGE("access() failed. path: %s, errno: %d (%s)",
1455 pkg_shared_data_path, errno, strerror(errno));
1463 int _coretpk_installer_make_directory(char *pkgid)
1466 char appdir[BUF_SIZE] = {'\0'};
1467 char rootfile[BUF_SIZE] = {'\0'};
1468 char *groupid = NULL;
1471 if (pkgid == NULL) {
1472 _LOGE("pkgid is NULL.");
1477 memset(appdir, '\0', BUF_SIZE);
1478 snprintf(appdir, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
1479 if (access(appdir, F_OK) != 0) {
1481 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1483 _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1486 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1488 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1493 memset(appdir, '\0', BUF_SIZE);
1494 snprintf(appdir, BUF_SIZE, "%s/%s/bin", OPT_USR_APPS, pkgid);
1495 if (access(appdir, F_OK) != 0) {
1496 memset(appdir, '\0', BUF_SIZE);
1497 snprintf(appdir, BUF_SIZE, "%s/%s/bin", USR_APPS, pkgid);
1498 if (access(appdir, F_OK) != 0) {
1499 _LOGE("[%s] is not existed.", appdir);
1503 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1505 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1510 memset(appdir, '\0', BUF_SIZE);
1511 snprintf(appdir, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgid);
1512 if (access(appdir, F_OK) != 0) {
1514 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1516 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1519 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1521 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1526 memset(appdir, '\0', BUF_SIZE);
1527 snprintf(appdir, BUF_SIZE, "%s/%s/lib", OPT_USR_APPS, pkgid);
1528 if (access(appdir, F_OK) != 0) {
1529 memset(appdir, '\0', BUF_SIZE);
1530 snprintf(appdir, BUF_SIZE, "%s/%s/lib", USR_APPS, pkgid);
1532 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1534 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1539 memset(appdir, '\0', BUF_SIZE);
1540 snprintf(appdir, BUF_SIZE, "%s/%s/res", OPT_USR_APPS, pkgid);
1541 if (access(appdir, F_OK) != 0) {
1542 memset(appdir, '\0', BUF_SIZE);
1543 snprintf(appdir, BUF_SIZE, "%s/%s/res", USR_APPS, pkgid);
1545 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1547 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1552 memset(appdir, '\0', BUF_SIZE);
1553 snprintf(appdir, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgid);
1554 if (access(appdir, F_OK) != 0) {
1556 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1558 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1561 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1563 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1568 memset(appdir, '\0', BUF_SIZE);
1569 snprintf(appdir, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgid);
1570 if (access(appdir, F_OK) != 0) {
1571 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1573 _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1576 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1578 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1581 memset(appdir, '\0', BUF_SIZE);
1582 snprintf(appdir, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgid);
1583 if (access(appdir, F_OK) != 0) {
1584 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1586 _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1589 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1591 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1596 memset(appdir, '\0', BUF_SIZE);
1597 snprintf(appdir, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
1598 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1600 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1605 memset(appdir, '\0', BUF_SIZE);
1606 snprintf(appdir, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgid);
1607 if (access(appdir, F_OK) != 0) {
1608 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1610 _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1613 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1615 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1620 memset(appdir, '\0', BUF_SIZE);
1621 snprintf(appdir, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
1622 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1624 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1627 memset(appdir, '\0', BUF_SIZE);
1628 snprintf(appdir, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgid);
1629 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1631 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1636 memset(appdir, '\0', BUF_SIZE);
1637 snprintf(appdir, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
1638 if (access(appdir, F_OK) != 0) {
1639 ret = _coretpk_installer_get_group_id(pkgid, &groupid);
1641 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1643 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1648 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1650 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1654 // [pkgid]/tizen-manifest.xml
1655 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
1656 ret = _coretpk_installer_apply_file_policy(rootfile);
1658 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1661 memset(rootfile, '\0', BUF_SIZE);
1662 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, CORETPK_XML);
1663 ret = _coretpk_installer_apply_file_policy(rootfile);
1665 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1669 // [pkgid]/author-signature.xml
1670 memset(rootfile, '\0', BUF_SIZE);
1671 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1672 ret = _coretpk_installer_apply_file_policy(rootfile);
1674 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1677 memset(rootfile, '\0', BUF_SIZE);
1678 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1679 ret = _coretpk_installer_apply_file_policy(rootfile);
1681 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1685 // [pkgid]/signature1.xml
1686 memset(rootfile, '\0', BUF_SIZE);
1687 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SIGNATURE1_XML);
1688 ret = _coretpk_installer_apply_file_policy(rootfile);
1690 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1693 memset(rootfile, '\0', BUF_SIZE);
1694 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, SIGNATURE1_XML);
1695 ret = _coretpk_installer_apply_file_policy(rootfile);
1697 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1701 // /opt/share/packages/[pkgid].xml
1702 memset(rootfile, '\0', BUF_SIZE);
1703 snprintf(rootfile, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
1704 ret = _coretpk_installer_apply_file_policy(rootfile);
1706 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1709 memset(rootfile, '\0', BUF_SIZE);
1710 snprintf(rootfile, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
1711 ret = _coretpk_installer_apply_file_policy(rootfile);
1713 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1717 // for external storage
1718 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
1719 int ret = _coretpk_installer_make_directory_for_ext(pkgid);
1721 _LOGE("_coretpk_installer_make_directory_for_ext() failed, pkgid=[%s]", pkgid);
1729 int _coretpk_installer_change_mode(char *path, int mode)
1733 ret = chmod(path, mode);
1735 _LOGL("chmod()", errno);
1742 int _coretpk_installer_change_file_owner(char *path, int ownerid, int groupid)
1746 if (access(path, F_OK) == 0) {
1747 ret = chown(path, ownerid, groupid);
1749 _LOGL("chown()", errno);
1757 int _coretpk_installer_change_directory_owner(char *dirpath, int ownerid, int groupid)
1761 if (__is_dir(dirpath)) {
1762 ret = chown(dirpath, ownerid, groupid);
1764 _LOGL("chown()", errno);
1772 void _coretpk_installer_set_privilege_setup_path_for_ext(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1774 char path[BUF_SIZE] = {'\0'};
1776 snprintf(path, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, dirpath);
1777 if (access(path, F_OK) == 0) {
1778 _ri_privilege_setup_path(pkgid, path, type, label);
1782 void _coretpk_installer_set_privilege_setup_path(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1784 char path[BUF_SIZE] = {'\0'};
1786 snprintf(path, BUF_SIZE, "%s/%s", USR_APPS, dirpath);
1787 if (access(path, F_OK) == 0) {
1788 _ri_privilege_setup_path(pkgid, path, type, label);
1790 memset(path, '\0', BUF_SIZE);
1792 snprintf(path, BUF_SIZE, "%s/%s", OPT_USR_APPS, dirpath);
1793 if (access(path, F_OK) == 0) {
1794 _ri_privilege_setup_path(pkgid, path, type, label);
1798 int _coretpk_installer_get_group_id(char *pkgid, char **result)
1801 const char *value = NULL;
1802 char author_signature[BUF_SIZE] = {'\0'};
1803 char *e_rootcert = NULL;
1804 char *d_rootcert = NULL;
1806 unsigned char hashout[BUF_SIZE] = {'\0'};
1807 unsigned int h_size = 0;
1810 pkgmgrinfo_certinfo_h handle = NULL;
1812 snprintf(author_signature, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1813 if (access(author_signature, F_OK) != 0) {
1814 _LOGE("[%s] is not found.", author_signature);
1816 memset(author_signature, '\0', BUF_SIZE);
1817 snprintf(author_signature, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1818 if (access(author_signature, F_OK) != 0) {
1819 _LOGE("[%s] is not found.", author_signature);
1822 _LOGE("author_signature=[%s]", author_signature);
1826 ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
1828 _LOGE("failed to get cert info.");
1832 ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle);
1834 _LOGE("failed to load cert info.");
1838 /*get root certificate*/
1839 ret = pkgmgrinfo_pkginfo_get_cert_value(handle, PMINFO_AUTHOR_SIGNER_CERT, &value);
1840 if (ret < 0 || value == NULL) {
1841 _LOGE("failed to get cert value.");
1846 d_rootcert = (char *)g_base64_decode(value, &d_size);
1847 if (d_rootcert == NULL) {
1848 _LOGE("failed to execute decode.");
1853 EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
1855 _LOGE("@Failed to get hash.");
1860 e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
1861 if (e_rootcert == NULL) {
1862 _LOGE("failed to execute encode.");
1865 e_size = strlen(e_rootcert);
1866 _LOGD("encoding done, len=[%d]", e_size);
1869 for (length = e_size; length >= 0; --length) {
1870 if (e_rootcert[length] == '/') {
1871 e_rootcert[length] = '#';
1875 *result = e_rootcert;
1884 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
1890 int _coretpk_installer_apply_smack_for_ext(char *pkgname)
1893 char dirpath[BUF_SIZE] = {'\0'};
1896 snprintf(dirpath, BUF_SIZE, "%s", pkgname);
1897 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1898 memset(dirpath, '\0', BUF_SIZE);
1901 snprintf(dirpath, BUF_SIZE, "%s/data", pkgname);
1902 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1903 memset(dirpath, '\0', BUF_SIZE);
1906 snprintf(dirpath, BUF_SIZE, "%s/cache", pkgname);
1907 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1908 memset(dirpath, '\0', BUF_SIZE);
1911 snprintf(dirpath, BUF_SIZE, "%s/shared", pkgname);
1912 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1913 memset(dirpath, '\0', BUF_SIZE);
1915 // [pkgid]/shared/data
1916 snprintf(dirpath, BUF_SIZE, "%s/shared/data", pkgname);
1917 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PUBLIC_RO, NULL);
1922 int _coretpk_installer_apply_smack(char *pkgname, int flag)
1925 char dirpath[BUF_SIZE] = {'\0'};
1926 char manifest[BUF_SIZE] = {'\0'};
1927 char *groupid = NULL;
1928 char *shared_data_label = NULL;
1930 _ri_privilege_register_package(pkgname);
1933 snprintf(dirpath, BUF_SIZE, "%s", pkgname);
1934 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1935 memset(dirpath, '\0', BUF_SIZE);
1938 snprintf(dirpath, BUF_SIZE, "%s/shared", pkgname);
1939 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1940 memset(dirpath, '\0', BUF_SIZE);
1943 snprintf(dirpath, BUF_SIZE, "%s/shared/res", pkgname);
1944 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1945 memset(dirpath, '\0', BUF_SIZE);
1948 snprintf(dirpath, BUF_SIZE, "%s/shared/data", pkgname);
1949 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PUBLIC_RO, NULL);
1950 memset(dirpath, '\0', BUF_SIZE);
1953 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
1954 ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
1956 memset(dirpath, '\0', BUF_SIZE);
1957 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgname);
1958 ret = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
1960 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1962 ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
1964 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1969 memset(dirpath, '\0', BUF_SIZE);
1970 snprintf(dirpath, BUF_SIZE, "%s/shared/trusted", pkgname);
1971 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
1972 ret = _coretpk_installer_get_group_id(pkgname, &groupid);
1974 LOGD("groupid = [%s] for shared/trusted.", groupid);
1975 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
1979 LOGE("_coretpk_installer_get_group_id(%s) failed.", pkgname);
1983 memset(dirpath, '\0', BUF_SIZE);
1986 snprintf(dirpath, BUF_SIZE, "%s/bin", pkgname);
1987 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1988 memset(dirpath, '\0', BUF_SIZE);
1991 snprintf(dirpath, BUF_SIZE, "%s/data", pkgname);
1992 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1993 memset(dirpath, '\0', BUF_SIZE);
1996 snprintf(dirpath, BUF_SIZE, "%s/lib", pkgname);
1997 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1998 memset(dirpath, '\0', BUF_SIZE);
2001 snprintf(dirpath, BUF_SIZE, "%s/res", pkgname);
2002 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
2003 memset(dirpath, '\0', BUF_SIZE);
2006 snprintf(dirpath, BUF_SIZE, "%s/cache", pkgname);
2007 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
2008 memset(dirpath, '\0', BUF_SIZE);
2010 // tizen-manifest.xml
2011 snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, CORETPK_XML);
2012 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
2013 memset(dirpath, '\0', BUF_SIZE);
2015 // author-signature.xml
2016 snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, AUTHOR_SIGNATURE_XML);
2017 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
2018 memset(dirpath, '\0', BUF_SIZE);
2021 snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, SIGNATURE1_XML);
2022 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
2023 memset(dirpath, '\0', BUF_SIZE);
2026 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgname);
2027 if (access(manifest, F_OK) == 0) {
2028 _ri_privilege_setup_path(pkgname, manifest, APP_PATH_PRIVATE, pkgname);
2032 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
2033 if (access(OPT_STORAGE_SDCARD, F_OK) == 0) {
2034 ret = _coretpk_installer_apply_smack_for_ext(pkgname);
2036 _LOGE("_coretpk_installer_apply_smack_for_ext(%s) failed.", pkgname);
2045 static char * __getprivilege(const char* pBuf)
2047 const char* pKey = "<privilege>";
2048 const char* p = NULL;
2049 const char* pStart = NULL;
2050 const char* pEnd = NULL;
2052 p = strstr(pBuf, pKey);
2056 pStart = p + strlen(pKey);
2057 pEnd = strchr(pStart, '<');
2061 size_t len = pEnd - pStart;
2065 char *pRes = (char*)malloc(len + 1);
2067 _LOGE("malloc failed!!");
2070 strncpy(pRes, pStart, len);
2076 int _coretpk_installer_apply_privilege(char *pkgid, char *pkgPath, int apiVisibility)
2080 char *find_str = NULL;
2081 char buf[BUF_SIZE] = {0};
2082 char manifest[BUF_SIZE] = {'\0'};
2083 const char *perm[] = {NULL, NULL};
2084 int apptype = PERM_APP_TYPE_EFL;
2085 char *tmp_api_version = NULL;
2087 if (apiVisibility & CERT_SVC_VISIBILITY_PLATFORM) {
2088 _LOGD("VISIBILITY_PLATFORM!");
2089 apptype = PERM_APP_TYPE_EFL_PLATFORM;
2090 } else if ((apiVisibility & CERT_SVC_VISIBILITY_PARTNER) ||
2091 (apiVisibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
2092 (apiVisibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
2093 _LOGD("VISIBILITY_PARTNER!");
2094 apptype = PERM_APP_TYPE_EFL_PARTNER;
2097 snprintf(manifest, BUF_SIZE, "%s/%s", pkgPath, CORETPK_XML);
2098 _LOGD("pkgid = [%s], manifest = [%s]", pkgid, manifest);
2100 fp = fopen(manifest, "r");
2102 _LOGE("Fail get : %s\n", manifest);
2106 tmp_api_version = __find_info_from_xml(manifest, "api-version");
2107 if (tmp_api_version) {
2108 ret = _ri_privilege_set_package_version(pkgid, tmp_api_version);
2110 _LOGE("failed to set pkg version for privilege, ret[%d]", ret);
2113 _LOGD("api-version fot privilege has done successfully.");
2114 free(tmp_api_version);
2116 _LOGE("failed to get api version from native app manifest");
2122 /*reload privilege*/
2123 _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2125 while (fgets(buf, BUF_SIZE, fp) != NULL) {
2128 if (strstr(buf, "<privilege>")) {
2129 find_str = __getprivilege(buf);
2130 if (find_str != NULL) {
2131 _LOGD("privilege = [%s]", find_str);
2134 ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2136 _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", pkgid, apptype);
2138 _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", pkgid, apptype);
2144 _LOGD("find_str is null.");
2148 memset(buf, 0x00, BUF_SIZE);
2157 int _coretpk_installer_package_install(char *pkgfile, char *pkgid, char *clientid)
2160 char buff[BUF_SIZE] = {'\0'};
2161 char manifest[BUF_SIZE] = {'\0'};
2162 char cwd[BUF_SIZE] = {'\0'};
2164 char rwmanifest[BUF_SIZE] = {'\0'};
2166 pkginfo *info = NULL;
2168 /* for external installation */
2169 app2ext_handle *handle = NULL;
2170 GList *dir_list = NULL;
2171 int install_status = APP2EXT_STATUS_SUCCESS;
2174 if (pkgfile == NULL || pkgid == NULL) {
2175 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2176 return RPM_INSTALLER_ERR_WRONG_PARAM;
2179 /*send event for start*/
2180 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "install");
2181 _LOGD("[#]start : _coretpk_installer_package_install[%s]", pkgid);
2184 info = _coretpk_installer_get_pkgfile_info(pkgfile);
2185 if (info == NULL || (strlen(info->package_name) == 0)) {
2186 _LOGE("failed to get pkg info");
2187 ret = RPM_INSTALLER_ERR_INTERNAL;
2191 /*compare package's api version with platform version*/
2192 ret = __coretpk_compare_with_platform_version(info->api_version);
2193 if (ret != RPM_INSTALLER_SUCCESS) {
2194 if (ret == RPM_INSTALLER_ERR_NOT_SUPPORTED_API_VERSION) {
2195 _LOGE("Unable to install. Platform version[%s] < Package version[%s]",
2196 TIZEN_VERSION, info->api_version);
2198 ret = RPM_INSTALLER_ERR_INTERNAL;
2202 /*send event for install_percent*/
2203 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2205 /*If the directory which will be installed exists, remove it.*/
2206 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2207 if (__is_dir(buff)) {
2208 _rpm_delete_dir(buff);
2212 ret = __pre_install_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2214 _LOGE("__pre_install_for_mmc is failed.");
2218 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, "-d", buff, NULL };
2219 ret = _ri_xsystem(unzip_argv);
2221 _LOGE("failed to unzip for path=[%s], ret=[%d]", buff, ret);
2224 _LOGD("unzip is done successfully, path=[%s]", buff);
2227 temp = getcwd(cwd, BUF_SIZE);
2228 if ((temp == NULL) || (cwd[0] == '\0')) {
2229 _LOGL("getcwd()", errno);
2230 ret = RPM_INSTALLER_ERR_INTERNAL;
2233 _LOGD("current working directory, path=[%s]", cwd);
2238 _LOGL("chdir()", errno);
2239 ret = RPM_INSTALLER_ERR_INTERNAL;
2243 /*check for signature and certificate*/
2244 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2245 ret = _coretpk_installer_verify_signatures(buff, pkgid, &visibility);
2247 _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
2248 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2251 _LOGD("signature and certificate are verified successfully.");
2257 _LOGL("chdir()", errno);
2258 ret = RPM_INSTALLER_ERR_INTERNAL;
2262 /*convert manifest and copy the file to /opt/share/packages*/
2263 snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
2264 ret = _coretpk_parser_convert_manifest(manifest, pkgid, clientid, false, visibility, NULL);
2266 _LOGE("failed to convert the manifest.");
2267 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2270 _LOGD("manifest is converted successfully.");
2272 if (strstr(pkgfile, ".wgt") != NULL) {
2273 _LOGD("wgt file=[%s]", pkgfile);
2275 if (strstr(manifest, OPT_USR_APPS)) {
2276 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2277 const char *rw_xml_category[] = { CORETPK_CATEGORY_CONVERTER, rwmanifest, NULL };
2278 ret = _ri_xsystem(rw_xml_category);
2282 /*check the manifest file.*/
2283 memset(manifest, '\0', sizeof(manifest));
2284 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2285 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
2286 ret = pkgmgr_parser_check_manifest_validation(manifest);
2288 _LOGE("invalid manifest file");
2289 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2293 /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
2294 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2296 _LOGE("failed to parse the manifest.");
2297 ret = RPM_INSTALLER_ERR_INTERNAL;
2300 _LOGD("manifest parsing done successfully.");
2303 _coretpk_installer_search_ui_gadget(pkgid);
2305 /*send event for install_percent*/
2306 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2308 /*register cert info*/
2309 _ri_register_cert(pkgid);
2312 ret = _coretpk_installer_make_directory(pkgid);
2314 _LOGE("failed to make the directory.");
2318 /*apply smack to app dir*/
2319 ret = _coretpk_installer_apply_smack(pkgid, 1);
2321 _LOGE("failed to apply the smack.");
2325 if (strlen(info->api_version) != 0) {
2326 ret = _ri_privilege_set_package_version(pkgid, info->api_version);
2328 _LOGE("failed to set pkg version for privilege, ret[%d]", ret);
2330 _LOGD("api-version fot privilege has done successfully.");
2332 _LOGD("failed to retrieve api_version of package");
2335 /*apply smack by privilege*/
2336 ret = _ri_apply_privilege(pkgid, visibility);
2338 _LOGE("failed to apply permission, ret=[%d]", ret);
2340 _LOGD("permission applying done successfully.");
2342 // Check privilege and visibility
2343 if (privilege_list) {
2344 ret = _coretpk_installer_verify_privilege_list(pkgid, privilege_list, visibility, info->api_version);
2348 _LOGD("_coretpk_installer_verify_privilege_list done.");
2354 ret = _ri_smack_reload(pkgid, REQUEST_TYPE_INSTALL);
2356 _LOGD("failed to reload the smack.");
2360 /*send event for install_percent*/
2361 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2363 ret = RPM_INSTALLER_SUCCESS;
2368 install_status = APP2EXT_STATUS_FAILED;
2370 _LOGD("install status is [%d].", install_status);
2371 if (__post_install_for_mmc(handle, pkgid, dir_list, install_status) < 0) {
2372 _LOGE("__post_install_for_mmc is failed.");
2382 _LOGD("_coretpk_installer_package_install is done.");
2383 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2386 if (_coretpk_installer_remove_db_info(pkgid) < 0) {
2387 _LOGE("_coretpk_installer_remove_db_info is failed.");
2390 /*remove xml(/opt/share/packages/pkgid.xml)*/
2391 if (access(manifest, F_OK) == 0) {
2392 (void)remove(manifest);
2395 /*remove app dir(/opt/usr/apps/pkgid)*/
2396 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2397 if (__is_dir(buff)) {
2398 _rpm_delete_dir(buff);
2401 /*remove ext app dir(/opt/storage/sdcard/apps/pkgid)*/
2402 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
2403 char extpath[BUF_SIZE] = {'\0'};
2404 snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
2405 if (__is_dir(extpath)) {
2406 _rpm_delete_dir(extpath);
2410 char *errorstr = NULL;
2411 if (ret < RPM_INSTALLER_ERR_PRIVILEGE_UNAUTHORIZED || ret > RPM_INSTALLER_ERR_PRIVILEGE_USING_LEGACY_FAILED) {
2412 _ri_error_no_to_string(ret, &errorstr);
2413 _ri_broadcast_status_notification(pkgid, "coretpk", "error", errorstr);
2417 _LOGE("_coretpk_installer_package_install is failed.");
2418 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2424 int _coretpk_installer_package_uninstall(const char *pkgid)
2427 int update_system = 0;
2429 update_system = __check_updated_system_package(pkgid);
2431 if (update_system == 1) {
2432 _LOGD("start remove_update, pkgid=[%s]", pkgid);
2433 ret = __pkg_remove_update(pkgid);
2435 _LOGD("start uninstall, pkgid=[%s]", pkgid);
2436 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2440 _LOGE("uninstallation is failed, pkgid=[%s], update_system=[%d]", pkgid, update_system);
2442 _LOGD("uninstallation is done successfully, pkgid=[%s]", pkgid);
2448 int _coretpk_installer_package_upgrade(char *pkgfile, char *pkgid, char *clientid)
2451 char buff[BUF_SIZE] = {'\0'};
2452 char manifest[BUF_SIZE] = { '\0'};
2453 char cwd[BUF_SIZE] = {'\0'};
2454 char rwmanifest[BUF_SIZE] = {'\0'};
2455 pkgmgrinfo_pkginfo_h pkghandle = NULL;
2458 pkginfo *info = NULL;
2460 /* for external upgrade */
2461 app2ext_handle *handle = NULL;
2462 GList *dir_list = NULL;
2465 if (pkgfile == NULL || pkgid == NULL) {
2466 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2467 return RPM_INSTALLER_ERR_WRONG_PARAM;
2470 /*send event for start*/
2471 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
2474 info = _coretpk_installer_get_pkgfile_info(pkgfile);
2475 if (info == NULL || strlen(info->package_name) == 0) {
2476 _LOGE("failed to get pkg info");
2477 ret = RPM_INSTALLER_ERR_INTERNAL;
2481 /*compare package's api version with platform version*/
2482 ret = __coretpk_compare_with_platform_version(info->api_version);
2483 if (ret != RPM_INSTALLER_SUCCESS) {
2484 if (ret == RPM_INSTALLER_ERR_NOT_SUPPORTED_API_VERSION) {
2485 _LOGE("Unable to install. Platform version[%s] < Package version[%s]",
2486 TIZEN_VERSION, info->api_version);
2488 ret = RPM_INSTALLER_ERR_INTERNAL;
2492 /*terminate running app*/
2493 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2495 _LOGE("failed to get the pkginfo handle.");
2496 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2499 pkgmgrinfo_appinfo_get_list(pkghandle, PMINFO_UI_APP, __ri_check_running_app, NULL);
2500 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2502 /*remove dir for clean*/
2503 __ri_remove_updated_dir(pkgid);
2506 ret = __pre_upgrade_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2508 _LOGE("__pre_upgrade_for_mmc is failed.");
2512 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2513 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, "-d", buff, NULL };
2514 ret = _ri_xsystem(unzip_argv);
2516 _LOGE("failed to unzip for [%s, %d].", buff, ret);
2519 _LOGD("#unzip[%s] success.", buff);
2521 /*send event for install_percent*/
2522 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2525 temp = getcwd(cwd, BUF_SIZE);
2526 if ((temp == NULL) || (cwd[0] == '\0')) {
2527 _LOGL("getcwd()", errno);
2528 ret = RPM_INSTALLER_ERR_INTERNAL;
2531 _LOGD("#Current working directory is %s.", cwd);
2536 _LOGL("chdir()", errno);
2537 ret = RPM_INSTALLER_ERR_INTERNAL;
2541 /*check for signature and certificate*/
2542 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2543 ret = _coretpk_installer_verify_signatures(buff, pkgid, &visibility);
2545 _LOGE("@Failed to verify signature and certificate[%s].", pkgid);
2546 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2549 _LOGD("#signature and certificate verifying success");
2555 _LOGL("chdir()", errno);
2556 ret = RPM_INSTALLER_ERR_INTERNAL;
2560 /*convert manifest and copy the file to /opt/share/packages*/
2561 snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
2562 ret = _coretpk_parser_convert_manifest(manifest, pkgid, clientid, false, visibility, NULL);
2564 _LOGE("@Failed to convert the manifest.");
2565 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2568 _LOGD("#manifest converting success");
2570 if (strstr(pkgfile, ".wgt") != NULL) {
2571 _LOGD("wgt file = [%s]", pkgfile);
2573 if (strstr(manifest, OPT_USR_APPS)) {
2574 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2575 const char *rw_xml_category[] = { CORETPK_CATEGORY_CONVERTER, rwmanifest, NULL };
2576 ret = _ri_xsystem(rw_xml_category);
2580 /*check the manifest file.*/
2581 memset(manifest, '\0', sizeof(manifest));
2582 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2583 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
2584 ret = pkgmgr_parser_check_manifest_validation(manifest);
2586 _LOGE("@invalid manifest file");
2587 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2591 /*send event for install_percent*/
2592 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2594 /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
2595 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
2597 _LOGE("@parsing manifest failed.");
2598 ret = RPM_INSTALLER_ERR_INTERNAL;
2601 _LOGD("#parsing manifest success.");
2604 _coretpk_installer_search_ui_gadget(pkgid);
2606 /*unregister cert info*/
2607 _ri_unregister_cert(pkgid);
2609 /*register cert info*/
2610 _ri_register_cert(pkgid);
2613 ret = _coretpk_installer_make_directory(pkgid);
2615 _LOGE("@Failed to make the directory");
2619 // Remove origin rule
2620 _ri_privilege_unregister_package(pkgid);
2622 /*apply smack to app dir*/
2623 ret = _coretpk_installer_apply_smack(pkgid, 1);
2625 _LOGE("@Failed to apply the smack.");
2629 /*apply smack by privilege*/
2630 ret = _ri_apply_privilege(pkgid, visibility);
2632 _LOGE("@Failed to apply permission[%d].", ret);
2634 _LOGD("#permission applying success.");
2636 // Check privilege and visibility
2637 if (privilege_list) {
2638 ret = _coretpk_installer_verify_privilege_list(pkgid, privilege_list, visibility, info->api_version);
2642 _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
2648 ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
2650 _LOGE("@Failed to reload the smack.");
2654 /*send event for install_percent*/
2655 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2656 ret = RPM_INSTALLER_SUCCESS;
2660 if (__post_upgrade_for_mmc(handle, pkgid, dir_list) < 0) {
2661 _LOGE("__post_upgrade_for_mmc is failed.");
2671 _LOGD("[#]end : _coretpk_installer_package_upgrade");
2672 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2675 /*TODO:need to add recovery logic*/
2677 char *errorstr = NULL;
2678 _ri_error_no_to_string(ret, &errorstr);
2679 _ri_broadcast_status_notification(pkgid, "coretpk", "error", errorstr);
2682 _LOGE("[@]end : _coretpk_installer_package_upgrade");
2683 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2689 char* _coretpk_installer_get_pkgid_from_directory_path(char *dirpath)
2691 char* subpath = strrchr(dirpath, '/');
2695 int _coretpk_installer_directory_install(char *dirpath, char *clientid)
2698 char manifest[BUF_SIZE] = {'\0'};
2699 char cwd[BUF_SIZE] = {'\0'};
2705 if (dirpath == NULL) {
2706 _LOGE("dirpath is NULL.");
2707 return RPM_INSTALLER_ERR_WRONG_PARAM;
2710 _LOGD("directory_install start: dirpath = [%s]", dirpath);
2713 temp = getcwd(cwd, BUF_SIZE);
2714 if ((temp == NULL) || (cwd[0] == '\0')) {
2715 _LOGE("getcwd() failed. [%d][%s]", errno, strerror(errno));
2716 ret = RPM_INSTALLER_ERR_INTERNAL;
2720 _LOGD("Current working directory is [%s].", cwd);
2723 ret = chdir(dirpath);
2725 _LOGE("chdir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
2726 ret = RPM_INSTALLER_ERR_INTERNAL;
2730 // check for signature and certificate
2731 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2732 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
2733 _LOGD("pkgid=[%s]", pkgid);
2735 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
2737 _LOGE("_coretpk_installer_verify_signatures(%s, %s) failed.", dirpath, pkgid);
2738 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2741 _LOGD("verify_signatures(%s, %s) succeed!", dirpath, pkgid);
2747 _LOGE("chdir(%s) failed. [%d][%s]", cwd, errno, strerror(errno));
2748 ret = RPM_INSTALLER_ERR_INTERNAL;
2752 // convert manifest and copy the file to /usr/share/packages
2753 snprintf(manifest, BUF_SIZE, "%s/%s", dirpath, CORETPK_XML);
2754 if (pkgid == NULL) {
2755 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
2756 _LOGD("pkgid = [%s]", pkgid);
2759 ret = _coretpk_parser_convert_manifest(manifest, pkgid, clientid, false, visibility, NULL);
2761 _LOGE("_coretpk_parser_convert_manifest() failed. manifest = [%s], pkgid = [%s]", manifest, pkgid);
2762 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2765 _LOGD("convert_manifest(%s, %s) succeed!", manifest, pkgid);
2767 // check the manifest file
2768 memset(manifest, '\0', sizeof(manifest));
2769 if (strstr(dirpath, OPT_USR_APPS)) {
2770 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2772 snprintf(manifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2774 _LOGD("manifest = [%s]", manifest);
2776 // compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)
2777 ret = pkgmgr_parser_check_manifest_validation(manifest);
2779 _LOGE("pkgmgr_parser_check_manifest_validation(%s) failed.", manifest);
2780 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2784 // Parse the manifest to get install location and size. If installation fails, remove manifest info from DB
2785 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2787 _LOGE("pkgmgr_parser_parse_manifest_for_installation(%s) failed.", manifest);
2788 ret = RPM_INSTALLER_ERR_INTERNAL;
2791 _LOGD("parse_manifest(%s) succeed!", manifest);
2793 // register cert info
2794 _ri_register_cert(pkgid);
2797 ret = _coretpk_installer_make_directory(pkgid);
2799 _LOGE("_coretpk_installer_make_directory(%s) failed.", pkgid);
2802 _LOGD("make_directory(%s) succeed!", pkgid);
2804 // apply smack to app dir
2805 ret = _coretpk_installer_apply_smack(pkgid, 1);
2807 _LOGE("_coretpk_installer_apply_smack(%s) failed.", pkgid);
2809 _LOGD("apply_smack(%s) succeed!", pkgid);
2811 // apply smack by privilege
2812 ret = _ri_apply_privilege(pkgid, visibility);
2814 _LOGE("_ri_apply_privilege(%s, %d) failed. ret = [%d]", pkgid, visibility, ret);
2816 _LOGD("apply_privilege(%s, %d) succeed!", pkgid, visibility);
2818 ret = RPM_INSTALLER_SUCCESS;
2821 _LOGD("directory_install end: dirpath = [%s], ret = [%d]", dirpath, ret);
2826 int _coretpk_installer_prepare_package_install(char *pkgfile, char *clientid)
2829 pkginfo *info = NULL;
2830 pkginfo *dbinfo = NULL;
2835 info = _coretpk_installer_get_pkgfile_info(pkgfile);
2836 if (info == NULL || (strlen(info->package_name) == 0)) {
2837 _LOGE("failed to get the pkg info.");
2838 ret = RPM_INSTALLER_ERR_INTERNAL;
2842 pkgid = strdup(info->package_name);
2843 if (pkgid == NULL) {
2844 _LOGE("strdup() failed.");
2845 ret = RPM_INSTALLER_ERR_INTERNAL;
2849 dbinfo = _rpm_installer_get_pkgname_info(info->package_name);
2851 if (dbinfo == NULL) {
2852 /*package is not installed. Go for installation.*/
2853 _LOGD("start to install");
2854 ret = _coretpk_installer_package_install(pkgfile, pkgid, clientid);
2855 } else if (strcmp(info->version, dbinfo->version) > 0) {
2857 _LOGD("start to upgrade");
2858 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2859 } else if (strcmp(info->version, dbinfo->version) < 0) {
2861 _LOGD("start to downgrade");
2862 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2864 /*same package. Reinstall it. Manifest should be parsed again */
2865 _LOGD("start to reinstall");
2866 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2870 _LOGE("result=[%d]", ret);
2897 _ri_broadcast_status_notification("Invalid package", "invalid", "start", "install");
2898 _ri_broadcast_status_notification("Invalid package", "invalid", "end", "fail");
2903 int _coretpk_installer_prepare_package_uninstall(const char *pkgid)
2905 if (pkgid == NULL) {
2906 _LOGE("pkgid is NULL.");
2907 return RPM_INSTALLER_ERR_WRONG_PARAM;
2910 _LOGD("pkgid=[%s]", pkgid);
2913 pkginfo *dbinfo = NULL;
2915 dbinfo = _rpm_installer_get_pkgname_info(pkgid);
2916 if (dbinfo == NULL) {
2917 _LOGE("[%s] is not installed.", pkgid);
2918 return RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
2921 ret = _coretpk_installer_package_uninstall(pkgid);
2923 _LOGE("_coretpk_installer_package_uninstall() failed, pkgid=[%s], ret=[%d]", pkgid, ret);
2925 _LOGD("_coretpk_installer_package_uninstall() is done successfully, pkgid=[%s]", pkgid);
2936 int _coretpk_installer_prepare_directory_install(char *dirpath, char *clientid)
2940 ret = _coretpk_installer_directory_install(dirpath, clientid);
2941 _LOGD("path=[%s], result=[%d]", dirpath, ret);
2946 int _coretpk_installer_package_move(char* pkgid, int move_type)
2948 app2ext_handle *hdl = NULL;
2951 GList *dir_list = NULL;
2952 pkgmgrinfo_pkginfo_h pkghandle = NULL;
2954 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "move");
2955 _LOGD("[#]start : _coretpk_installer_package_move[%s][%d]", pkgid, move_type);
2957 if (move_type == PM_MOVE_TO_INTERNAL) {
2958 movetype = APP2EXT_MOVE_TO_PHONE;
2959 } else if (move_type == PM_MOVE_TO_SDCARD) {
2960 movetype = APP2EXT_MOVE_TO_EXT;
2962 ret = RPM_INSTALLER_ERR_WRONG_PARAM;
2966 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2968 _LOGE("@Failed to get the pkginfo handle.");
2969 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2973 /* Terminate the running instance of app */
2974 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
2975 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2977 hdl = app2ext_init(APP2EXT_SD_CARD);
2978 if ((hdl != NULL) && (hdl->interface.move != NULL)) {
2979 dir_list = __rpm_populate_dir_list();
2980 if (dir_list == NULL) {
2981 _LOGE("@Failed to get the populate directory.");
2982 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
2986 ret = hdl->interface.move(pkgid, dir_list, movetype);
2987 __rpm_clear_dir_list(dir_list);
2989 _LOGE("@Failed to move app.");
2990 ret = RPM_INSTALLER_ERR_INTERNAL;
2993 if(move_type == PM_MOVE_TO_INTERNAL) {
2994 _LOGD("#updating the installed storage from external to internal");
2995 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
2997 _LOGD("#updating the installed storage from internal to external");
2998 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
3001 if (ret != PMINFO_R_OK) {
3002 _LOGE("@Failed to udpate the installed storage.");
3003 ret = RPM_INSTALLER_ERR_INTERNAL;
3009 _LOGE("@Failed to get app2ext handle.");
3010 ret = RPM_INSTALLER_ERR_INTERNAL;
3015 app2ext_deinit(hdl);
3019 _LOGD("[#]end : _coretpk_installer_package_move");
3020 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
3022 _LOGE("[@]end : _coretpk_installer_package_move");
3023 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
3029 int _coretpk_installer_copy_file( const char *src_path, const char *dst_path)
3034 unsigned char temp_buf[8192] = {'\0',};
3035 size_t size_of_uchar = sizeof(unsigned char);
3036 size_t size_of_temp_buf = sizeof(temp_buf);
3038 src = fopen(src_path, "r");
3040 _LOGE("@Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
3044 dst = fopen(dst_path, "w");
3046 /*No such file or directory*/
3047 if (errno == ENOENT) {
3048 /*make the path of parent dir for the data*/
3049 char *path = strdup(dst_path);
3051 _LOGE("Memory allocation failed");
3054 char *p = strrchr(path, '/');
3062 int idx = strlen(path) - strlen(p);
3065 /*make the parent dir*/
3066 const char *mkdir_argv[] = { "/bin/mkdir", "-p", path, NULL };
3067 ret = _ri_xsystem(mkdir_argv);
3069 _LOGE("Failed to make parent dir.");
3072 _LOGD("#[%s] is created.", path);
3076 dst = fopen(dst_path, "w");
3078 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
3083 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
3089 while (!feof(src)) {
3090 rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
3091 fwrite( temp_buf, size_of_uchar, rc, dst);
3105 int _coretpk_installer_handle_rds_data(char *pkgid, GList *delete, GList *add, GList *modify, int *updatexml)
3109 char handledata[BUF_SIZE] = {'\0'};
3110 char srcfile[BUF_SIZE] = {'\0'};
3111 char destfile[BUF_SIZE] = {'\0'};
3114 if (delete != NULL) {
3115 list = g_list_first(delete);
3117 char *data = (char *)list->data;
3118 if (!strcasestr(data, RDS_DELTA_DELETE)) {
3119 snprintf(handledata, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
3121 const char *delete_argv[] = { "/bin/rm", "-rf", handledata, NULL };
3122 ret = _ri_xsystem(delete_argv);
3124 _LOGD("#[delete] success : %s", data);
3126 _LOGD("#[delete] fail : %s", data);
3128 memset(handledata, '\0', sizeof(handledata));
3131 list = g_list_next(list);
3134 _LOGD("#There is no deleted data.");
3139 list = g_list_first(add);
3141 char *data = (char *)list->data;
3142 if (!strcasestr(data, RDS_DELTA_ADD)) {
3143 snprintf(srcfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, data);
3144 snprintf(destfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
3146 if (__is_dir((char *)srcfile)) {
3147 const char *mkdir_argv[] = { "/bin/mkdir", "-p", destfile, NULL };
3148 _ri_xsystem(mkdir_argv);
3149 _LOGD("#[%s] is created.", destfile);
3151 ret =_coretpk_installer_copy_file(srcfile, destfile);
3153 _LOGD("#[add] success : %s", data);
3155 _LOGD("#[add] fail : %s", data);
3158 memset(srcfile, '\0', sizeof(srcfile));
3159 memset(destfile, '\0', sizeof(destfile));
3162 list = g_list_next(list);
3165 _LOGD("#There is no added data.");
3169 if (modify != NULL) {
3170 list = g_list_first(modify);
3172 char *data = (char *)list->data;
3173 if (!strcasestr(data, RDS_DELTA_MODIFY)) {
3174 /*If XML is modified, the checking codes for xml has to be executed.*/
3175 if (strcmp(data, CORETPK_XML) == 0) {
3179 snprintf(srcfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, data);
3180 snprintf(destfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
3182 ret =_coretpk_installer_copy_file(srcfile, destfile);
3184 _LOGD("#[modify] success : %s", data);
3186 _LOGD("#[modify] fail : %s", data);
3189 memset(srcfile, '\0', sizeof(srcfile));
3190 memset(destfile, '\0', sizeof(destfile));
3192 list = g_list_next(list);
3195 _LOGD("#There is no modified data.");
3201 int _coretpk_installer_read_rds_file(char *pkgid, char *rdsfile, int *updatexml)
3204 int state = RDS_STATE_NONE;
3206 char buffer[BUF_SIZE] = {'\0'};
3209 GList *delete_list = NULL;
3210 GList *add_list = NULL;
3211 GList *modify_list = NULL;
3213 if (access(rdsfile, F_OK) != 0) {
3214 _LOGL("access()", errno);
3218 fi = fopen(rdsfile, "r");
3220 _LOGL("fopen()", errno);
3224 while (fgets(buffer, BUF_SIZE, fi) != NULL) {
3225 buffer[strlen(buffer) - 1] = '\0';
3228 if (buffer[0] == '#') {
3229 if (strcasestr(buffer, RDS_DELTA_DELETE)) {
3230 state = RDS_STATE_DELETE;
3231 } else if (strcasestr(buffer, RDS_DELTA_ADD)) {
3232 state = RDS_STATE_ADD;
3233 } else if (strcasestr(buffer, RDS_DELTA_MODIFY)) {
3234 state = RDS_STATE_MODIFY;
3236 state = RDS_STATE_NONE;
3240 if (state == RDS_STATE_NONE) {
3241 _LOGE("Unknown RDS State, INSTALLER_RDS_STATE_NONE");
3245 /*make rds data list*/
3247 case RDS_STATE_DELETE:
3248 _LOGD("RDS_STATE_DELETE data : %s", buffer);
3249 delete_list = g_list_append(delete_list, g_strdup(buffer));
3253 _LOGD("RDS_STATE_ADD data : %s", buffer);
3254 add_list = g_list_append(add_list, g_strdup(buffer));
3257 case RDS_STATE_MODIFY:
3258 _LOGD("RDS_STATE_MODIFY data : %s", buffer);
3259 modify_list = g_list_append(modify_list, g_strdup(buffer));
3264 ret = _coretpk_installer_handle_rds_data(pkgid, delete_list, add_list, modify_list, updatexml);
3266 _LOGE("@Failed to handle rds data.");
3269 if (delete_list != NULL) {
3270 g_list_free(delete_list);
3272 if (add_list != NULL) {
3273 g_list_free(add_list);
3275 if (modify_list != NULL) {
3276 g_list_free(modify_list);
3283 int _coretpk_installer_package_reinstall(char *pkgid, char *clientid)
3286 char manifest[BUF_SIZE] = {'\0'};
3287 char rdsfile[BUF_SIZE] = {'\0'};
3288 char dirpath[BUF_SIZE] = {'\0'};
3289 char cwd[BUF_SIZE] = {'\0'};
3293 pkginfo *info = NULL;
3296 if (pkgid == NULL) {
3297 _LOGE("@The input param[pkgid] is NULL.");
3298 return RPM_INSTALLER_ERR_WRONG_PARAM;
3301 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "start", "update");
3302 _LOGD("[#]start : _coretpk_installer_package_reinstall[%s]", pkgid);
3304 snprintf(rdsfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, RDS_DELTA_FILE);
3305 ret = _coretpk_installer_read_rds_file(pkgid, rdsfile, &updatexml);
3307 _LOGE("@Failed to read the rds file.");
3308 ret = RPM_INSTALLER_ERR_INTERNAL;
3311 _LOGD("#RDS file reading success");
3313 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "30");
3316 temp = getcwd(cwd, BUF_SIZE);
3317 if ((temp == NULL) || (cwd[0] == '\0')) {
3318 _LOGL("getcwd()", errno);
3319 ret = RPM_INSTALLER_ERR_INTERNAL;
3322 _LOGD("#Current working directory is %s.", cwd);
3325 snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3326 ret = chdir(dirpath);
3328 _LOGL("chdir()", errno);
3329 ret = RPM_INSTALLER_ERR_INTERNAL;
3333 /*check for signature and certificate*/
3334 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
3336 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
3337 _LOGD("pkgid[%s]", pkgid);
3339 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
3341 _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
3342 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3345 _LOGD("signature and certificate verifying success");
3351 _LOGL("chdir()", errno);
3352 ret = RPM_INSTALLER_ERR_INTERNAL;
3357 /*convert manifest and copy the file to /opt/share/packages*/
3358 snprintf(manifest, BUF_SIZE, "%s/%s", dirpath, CORETPK_XML);
3359 if (pkgid == NULL) {
3360 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
3361 _LOGD("pkgid[%s]", pkgid);
3363 ret = _coretpk_parser_convert_manifest(manifest, pkgid, clientid, false, visibility, NULL);
3365 _LOGE("@Failed to convert the manifest.");
3366 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3369 _LOGD("#manifest converting success");
3371 /*check the manifest file.*/
3372 memset(manifest, '\0', sizeof(manifest));
3373 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
3374 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
3375 ret = pkgmgr_parser_check_manifest_validation(manifest);
3377 _LOGE("@invalid manifest file");
3378 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3383 info = _coretpk_installer_get_pkg_info(manifest);
3384 if (info == NULL || strlen(info->package_name) == 0) {
3385 _LOGE("failed to get pkg info");
3386 ret = RPM_INSTALLER_ERR_INTERNAL;
3390 /*compare package's api version with platform version*/
3391 ret = __coretpk_compare_with_platform_version(info->api_version);
3392 if (ret != RPM_INSTALLER_SUCCESS) {
3393 if (ret == RPM_INSTALLER_ERR_NOT_SUPPORTED_API_VERSION) {
3394 _LOGE("Unable to install. Platform version[%s] < Package version[%s]",
3395 TIZEN_VERSION, info->api_version);
3397 ret = RPM_INSTALLER_ERR_INTERNAL;
3401 /*Parse the manifest to get install location and size. If failed, remove manifest info from DB.*/
3402 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
3404 _LOGE("@Failed to parse the manifest.");
3405 ret = RPM_INSTALLER_ERR_INTERNAL;
3408 _LOGD("#manifest parsing success");
3411 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "60");
3413 /*register cert info*/
3414 _ri_register_cert(pkgid);
3417 ret = _coretpk_installer_make_directory(pkgid);
3419 _LOGE("@Failed to make directory");
3423 _ri_privilege_unregister_package(pkgid);
3425 /*apply smack to app dir*/
3426 ret = _coretpk_installer_apply_smack(pkgid, 1);
3428 _LOGE("@Failed to apply smack.");
3432 /*apply smack by privilege*/
3433 ret = _ri_apply_privilege(pkgid, visibility);
3435 _LOGE("@Failed to apply permission[%d].", ret);
3437 _LOGD("#permission applying success.");
3439 // Check privilege and visibility
3440 if (privilege_list) {
3441 ret = _coretpk_installer_verify_privilege_list(pkgid, privilege_list, visibility, info->api_version);
3445 _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
3451 ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
3453 _LOGE("@Failed to reload the smack.");
3457 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "100");
3458 ret = RPM_INSTALLER_SUCCESS;
3468 _LOGD("[#]end : _coretpk_installer_package_reinstall");
3469 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "ok");
3472 ret = _coretpk_installer_remove_db_info(pkgid);
3474 _LOGE("_coretpk_installer_remove_db_info is failed.");
3477 /*remove xml(/opt/share/packages/pkgid.xml)*/
3478 if (access(manifest, F_OK) == 0) {
3479 (void)remove(manifest);
3482 /*remove app dir(/opt/usr/apps/pkgid)*/
3483 if (__is_dir(dirpath)) {
3484 _rpm_delete_dir(dirpath);
3487 _LOGE("[@]end : _coretpk_installer_package_reinstall");
3488 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "fail");
3494 int _coretpk_installer_csc_install(char *path_str, char *remove_str)
3497 pkginfo *info = NULL;
3498 char buff[BUF_SIZE] = {'\0'};
3499 char manifest[BUF_SIZE] = {'\0'};
3500 char cwd[BUF_SIZE] = {'\0'};
3502 char *csc_tags[3] = {NULL, };
3506 if (path_str == NULL || remove_str == NULL) {
3507 _LOGE("@The input param[pkgfile or pkgid] is NULL.");
3508 return RPM_INSTALLER_ERR_WRONG_PARAM;
3511 _LOGD("[##]csc-core : start csc_install[path=%s]", path_str);
3513 info = _coretpk_installer_get_pkgfile_info(path_str);
3514 if (info == NULL || (strlen(info->package_name) == 0)) {
3515 _LOGE("[@@]end : _coretpk_installer_prepare_package_install: failed to get the pkg info.");
3516 ret = RPM_INSTALLER_ERR_INTERNAL;
3520 _LOGD("[##]csc-core : get pkgid [%s]", info->package_name);
3522 /*If the directory which will be installed exists, remove it.*/
3523 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, info->package_name);
3524 if (__is_dir(buff)) {
3525 _rpm_delete_dir(buff);
3528 _LOGD("[##]csc-core : real path [%s]", buff);
3530 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", path_str, "-d", buff, NULL };
3531 ret = _ri_xsystem(unzip_argv);
3533 _LOGE("@Failed to unzip for [%s, %d].", buff, ret);
3537 _LOGD("[##]csc-core : unzip success[%s]", buff);
3540 temp = getcwd(cwd, BUF_SIZE);
3541 if ((temp == NULL) || (cwd[0] == '\0')) {
3542 _LOGL("getcwd()", errno);
3543 ret = RPM_INSTALLER_ERR_INTERNAL;
3550 _LOGL("chdir()", errno);
3551 ret = RPM_INSTALLER_ERR_INTERNAL;
3555 _LOGD("[##]csc-core : check signature");
3557 /*check for signature and certificate*/
3558 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
3559 ret = _coretpk_installer_verify_signatures(buff, info->package_name, &visibility);
3561 _LOGE("@Failed to verify signature and certificate[%s].", info->package_name);
3562 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3565 _LOGD("[##]csc-core : signature verify success[%s]", buff);
3571 _LOGL("chdir()", errno);
3572 ret = RPM_INSTALLER_ERR_INTERNAL;
3576 /*convert manifest and copy the file to /opt/share/packages*/
3577 snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, info->package_name, CORETPK_XML);
3578 ret = _coretpk_parser_convert_manifest(manifest, info->package_name, NULL, false, visibility, NULL);
3580 _LOGE("@Failed to convert the manifest.");
3581 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3585 _LOGD("[##]csc-core : manifest converting success");
3587 /*check the manifest file.*/
3588 memset(manifest, '\0', sizeof(manifest));
3589 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, info->package_name);
3590 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
3591 ret = pkgmgr_parser_check_manifest_validation(manifest);
3593 _LOGE("@invalid manifest file");
3594 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3598 _LOGD("[##]csc-core : manifest validation success");
3600 /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
3601 if (strcmp(remove_str,"true")==0)
3602 csc_tags[0] = "removable=true";
3604 csc_tags[0] = "removable=false";
3606 csc_tags[1] = "preload=true";
3609 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, csc_tags);
3611 _LOGE("@Failed to parse the manifest.");
3612 ret = RPM_INSTALLER_ERR_INTERNAL;
3616 _LOGD("[##]csc-core : register manifest success");
3618 /*register cert info*/
3619 _ri_register_cert(info->package_name);
3622 ret = _coretpk_installer_make_directory(info->package_name);
3624 _LOGE("@Failed to make the directory");
3628 _LOGD("[##]csc-core : make directory success");
3630 /*apply smack to app dir*/
3631 ret = _coretpk_installer_apply_smack(info->package_name, 1);
3633 _LOGE("@Failed to apply the smack.");
3637 _LOGD("[##]csc-core : apply_smack success");
3639 /*apply smack by privilege*/
3640 ret = _ri_apply_privilege(info->package_name, visibility);
3642 _LOGE("@Failed to apply permission[%d].", ret);
3645 _LOGD("[##]csc-core : apply_privilege success");
3649 ret = _ri_smack_reload(info->package_name, REQUEST_TYPE_INSTALL);
3651 _LOGD("@Failed to reload the smack.");
3655 _LOGD("[##]csc-core : smack_reload success");
3657 ret = RPM_INSTALLER_SUCCESS;
3661 _LOGD("[##]csc-core : finish csc core success");
3663 /*remove xml(/opt/share/packages/pkgid.xml)*/
3664 if (access(manifest, F_OK) == 0) {
3665 (void)remove(manifest);
3668 /*remove app dir(/opt/usr/apps/pkgid)*/
3669 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, info->package_name);
3670 if (__is_dir(buff)) {
3671 _rpm_delete_dir(buff);
3673 _LOGD("[##]csc-core : finish csc core fail");