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 static int __get_unzip_size(const char *item, unsigned long long *size)
62 _LOGE("Invalid argument.");
63 return PMINFO_R_ERROR;
66 unzFile uzf = unzOpen64(item);
69 _LOGE("Fail to open item : %s", item);
71 return PMINFO_R_ERROR;
73 ret = unzGoToFirstFile(uzf);
75 _LOGE("error get first zip file ");
78 return PMINFO_R_ERROR;
81 ret = unzOpenCurrentFile(uzf);
83 _LOGE("error unzOpenCurrentFile ");
86 return PMINFO_R_ERROR;
89 unz_file_info fileInfo = {0};
90 char* filename = (char*) calloc(1, BUF_SIZE);
91 ret= unzGetCurrentFileInfo(uzf, &fileInfo, filename, (BUF_SIZE-1), NULL, 0, NULL, 0);
92 *size = (unsigned long long)fileInfo.uncompressed_size + *size;
94 _LOGE("error get current file info");
95 unzCloseCurrentFile(uzf);
104 }while(unzGoToNextFile(uzf) == UNZ_OK);
112 static int __is_default_external_storage()
116 int mmc_status = VCONFKEY_SYSMAN_MMC_REMOVED;
118 ret = vconf_get_int("db/setting/default_memory/install_applications", &storage);
119 retvm_if(ret != 0, PMINFO_R_ERROR, "vconf_get_int(db/setting/default_memory/install_applications) is failed.");
122 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_status);
123 retvm_if(ret != 0, PMINFO_R_ERROR, "vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) is failed.");
125 if((mmc_status == VCONFKEY_SYSMAN_MMC_REMOVED) || (mmc_status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED)) {
126 _LOGD("mmc_status is MMC_REMOVED or NOT_MOUNTED.");
128 _LOGD("mmc_status is MMC_MOUNTED.");
133 return PMINFO_R_ERROR;
136 static void __apply_smack_for_mmc(const char *pkgid)
139 char dirpath[BUF_SIZE] = {'\0'};
141 snprintf(dirpath, BUF_SIZE, "%s/%s/.mmc", OPT_USR_APPS, pkgid);
142 if (access(dirpath, F_OK) != 0) {
143 _LOGE("Cannot access to [%s].", dirpath);
146 memset(dirpath, '\0', BUF_SIZE);
149 snprintf(dirpath, BUF_SIZE, "%s/.mmc/bin", pkgid);
150 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
151 memset(dirpath, '\0', BUF_SIZE);
154 snprintf(dirpath, BUF_SIZE, "%s/.mmc/lib", pkgid);
155 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
156 memset(dirpath, '\0', BUF_SIZE);
158 // [pkgid]/.mmc/lost+found
159 snprintf(dirpath, BUF_SIZE, "%s/.mmc/lost+found", pkgid);
160 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
161 memset(dirpath, '\0', BUF_SIZE);
164 snprintf(dirpath, BUF_SIZE, "%s/.mmc/res", pkgid);
165 _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
166 memset(dirpath, '\0', BUF_SIZE);
171 static int __pre_upgrade_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
175 unsigned long long archive_size_byte = 0;
176 int archive_size_mega = 0;
178 ret = __is_default_external_storage();
180 _LOGD("Upgrade storage is internal.");
183 _LOGD("__pre_upgrade start.");
185 ret = __get_unzip_size(pkgfile, &archive_size_byte);
187 _LOGD("Failed to get uncompressed size.");
188 return PMINFO_R_ERROR;
190 archive_size_mega = archive_size_byte / (1024 * 1024) + 1;
191 _LOGD("Uncompressed size is converted from [%lld]bytes to [%d]Mb.", archive_size_byte, archive_size_mega);
193 *handle = app2ext_init(APP2EXT_SD_CARD);
194 if (*handle == NULL) {
195 _LOGE("@app2ext init failed\n");
196 return PMINFO_R_ERROR;
198 if ((&((*handle)->interface) != NULL) && ((*handle)->interface.pre_upgrade != NULL) && ((*handle)->interface.post_upgrade != NULL) &&
199 ((*handle)->interface.disable != NULL)) {
200 ret = (*handle)->interface.disable(pkgid);
201 if (ret != APP2EXT_SUCCESS) {
202 _LOGE("Unmount ret[%d]", ret);
204 *dir_list = __rpm_populate_dir_list();
205 if (*dir_list == NULL) {
206 _LOGE("@ \nError in populating the directory list\n");
207 return PMINFO_R_ERROR;
209 ret = (*handle)->interface.pre_upgrade(pkgid, *dir_list, archive_size_mega);
210 if (ret == APP2EXT_ERROR_MMC_STATUS) {
211 _LOGE("@app2xt MMC is not here, go internal\n");
212 } else if (ret == APP2EXT_SUCCESS){
213 _LOGD("@pre_upgrade done, go internal\n");
216 _LOGE("@app2xt pre upgrade API failed (%d)\n", ret);
217 return PMINFO_R_ERROR;
220 _LOGE("handle is not proper.");
221 return PMINFO_R_ERROR;
223 _LOGD("__pre_upgrade end.");
228 static int __post_upgrade_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list)
231 int ret = __is_default_external_storage();
233 _LOGD("Upgrade storage is internal.");
236 _LOGD("__post_upgrade start.");
238 /* set smack again for .mmc folder */
239 __apply_smack_for_mmc(pkgid);
240 _LOGD("__apply_smack_for_mmc is completed.");
242 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)) {
243 __rpm_clear_dir_list(dir_list);
244 handle->interface.post_upgrade(pkgid, APP2EXT_STATUS_SUCCESS);
245 app2ext_deinit(handle);
247 _LOGE("handle->interface.post_upgrade is NULL.");
248 return PMINFO_R_ERROR;
250 _LOGD("__post_upgrade end.");
255 static int __pre_install_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
259 unsigned long long archive_size_byte = 0;
260 int archive_size_mega = 0;
262 ret = __is_default_external_storage();
264 _LOGD("Installed storage is internal.");
267 _LOGD("__pre_install start.");
269 ret = __get_unzip_size(pkgfile, &archive_size_byte);
271 _LOGD("Failed to get uncompressed size.");
272 return PMINFO_R_ERROR;
274 archive_size_mega = archive_size_byte / (1024 * 1024) + 1;
275 _LOGD("Uncompressed size is converted from [%lld]bytes to [%d]Mb.", archive_size_byte, archive_size_mega);
277 *handle = app2ext_init(APP2EXT_SD_CARD);
278 if (*handle == NULL) {
279 _LOGE("@app2ext init failed\n");
280 return PMINFO_R_ERROR;
282 if ((&((*handle)->interface) != NULL) && ((*handle)->interface.pre_install != NULL) && ((*handle)->interface.post_install != NULL)
283 && ((*handle)->interface.force_clean != NULL)) {
284 ret = (*handle)->interface.force_clean(pkgid);
285 if (ret != APP2EXT_SUCCESS) {
286 _LOGE("Force clean is failed. pkgid[%s] ret[%d]", pkgid, ret);
287 return PMINFO_R_ERROR;
289 _LOGD("Force clean is OK");
290 *dir_list = __rpm_populate_dir_list();
291 if (*dir_list == NULL) {
292 _LOGE("@ \nError in populating the directory list\n");
293 return PMINFO_R_ERROR;
295 ret = (*handle)->interface.pre_install(pkgid, *dir_list, archive_size_mega);
296 if (ret == APP2EXT_ERROR_MMC_STATUS) {
297 _LOGE("@app2xt MMC is not here, go internal\n");
298 } else if (ret == APP2EXT_SUCCESS){
299 _LOGD("@pre_install done, go internal\n");
302 _LOGE("@app2xt pre install API failed (%d)\n", ret);
303 return PMINFO_R_ERROR;
306 _LOGE("handle is not proper.");
307 return PMINFO_R_ERROR;
309 _LOGD("__pre_install end.");
314 static int __post_install_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list, int install_status)
317 int ret = __is_default_external_storage();
319 _LOGD("Installed storage is internal.");
322 _LOGD("__post_install start.");
324 /* set smack again for .mmc folder */
325 __apply_smack_for_mmc(pkgid);
326 _LOGD("__apply_smack_for_mmc is completed.");
328 if ((handle != NULL) && (handle->interface.post_install != NULL)) {
329 __rpm_clear_dir_list(dir_list);
330 handle->interface.post_install(pkgid, install_status);
331 app2ext_deinit(handle);
333 _LOGE("handle->interface.post_install is NULL.");
334 return PMINFO_R_ERROR;
336 _LOGD("__post_install end.");
341 static void __str_trim(char *input)
343 char *trim_str = input;
348 while (*input != 0) {
349 if (!isspace(*input)) {
360 static char *__find_info_from_xml(const char *manifest, const char *find_info)
362 const char *val = NULL;
364 xmlTextReaderPtr reader;
365 char *info_val = NULL;
368 if(manifest == NULL) {
369 _LOGE("input argument is NULL\n");
373 if(find_info == NULL) {
374 _LOGE("find_info is NULL\n");
378 reader = xmlReaderForFile(manifest, NULL, 0);
381 if (_child_element(reader, -1)) {
382 node = xmlTextReaderConstName(reader);
384 _LOGE("xmlTextReaderConstName value is NULL\n");
388 if (!strcmp(ASCII(node), "manifest")) {
389 ret = _ri_get_attribute(reader,(char*)find_info,&val);
391 _LOGE("error in getting the attribute value");
396 info_val = strdup(val);
397 if(info_val == NULL) {
398 _LOGE("malloc failed!!");
402 _LOGE("unable to create xml reader\n");
406 _LOGE("xmlReaderForFile value is NULL\n");
411 xmlFreeTextReader(reader);
420 static int __coretpk_privilege_func(const char *name, void *user_data)
424 const char *perm[] = {NULL, NULL};
425 const char *ug_pkgid = "ui-gadget::client";
429 _LOGD("privilege = [%s]", name);
430 _ri_privilege_register_package("ui-gadget::client");
432 ret = _ri_privilege_enable_permissions(ug_pkgid, PERM_APP_TYPE_EFL, perm, 1);
433 _LOGE("add ug privilege(%s, %s, %d) done.", ug_pkgid, name, ret);
438 static int __ui_gadget_func(const pkgmgrinfo_appinfo_h handle, void *user_data)
445 char appdir[BUF_SIZE] = {'\0'};
447 ret = pkgmgrinfo_appinfo_is_ui_gadget(handle, &is_ug);
448 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get is_ui_gadget.\n");
453 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
454 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get pkgid\n");
456 _LOGD("@[%s] has ui-gadget", pkgid);
458 /*check bin directory*/
459 snprintf(appdir, BUF_SIZE, "%s/%s/bin", USR_APPS, pkgid);
460 if (access(appdir, F_OK) != 0) {
462 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
464 _LOGL("mkdir()", errno);
470 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
471 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get exec\n");
473 /*make symlink to exec*/
474 const char *ln_argv[] = { "/bin/ln", "-sf", "/usr/bin/ug-client", exec, NULL };
475 ret = _ri_xsystem(ln_argv);
476 retvm_if(ret < 0, RPM_INSTALLER_ERR_INTERNAL, "Failed to exec ln_argv\n");
478 _LOGD("@[%s] success symlink to [/usr/bin/ug-client]", exec);
480 * (bool *) user_data = true;
486 static int __check_updated_system_package(const char *pkgid)
489 bool is_update = false;
490 bool is_system = false;
491 pkgmgrinfo_pkginfo_h pkghandle = NULL;
493 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
494 retvm_if(ret < 0, -1, "pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
496 ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
497 tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_system(%s) failed.", pkgid);
499 ret = pkgmgrinfo_pkginfo_is_update(pkghandle, &is_update);
500 tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_update(%s) failed.", pkgid);
502 if (is_system && is_update) {
503 _LOGD("pkgid=[%s] is updated system package.", pkgid);
506 _LOGD("pkgid=[%s] is not updated system app.", pkgid);
511 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
516 static int __pkg_remove_update(const char *pkgid)
519 char buff[BUF_SIZE] = {'\0'};
520 char rootpath[BUF_SIZE] = {'\0'};
523 _LOGE("pkgid is NULL.");
528 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
530 // remove dir for clean (/opt/usr/apps/[pkgid])
531 snprintf(rootpath, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
532 if (__is_dir(rootpath)) {
533 _rpm_delete_dir(rootpath);
536 // Remove origin rule
537 _ri_privilege_unregister_package(pkgid);
539 // unzip pkg path from factory-reset data
540 memset(rootpath, '\0', BUF_SIZE);
541 snprintf(rootpath, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
542 const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, rootpath, "-d", "/", NULL };
543 ret = _ri_xsystem(pkg_argv);
545 _LOGE("/usr/bin/unzip(%s) failed.", rootpath);
548 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
551 snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
555 memset(buff, '\0', BUF_SIZE);
556 snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
558 _LOGD("manifest = [%s].",buff);
560 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
562 _LOGE("pkgmgr_parser_parse_manifest_for_upgrade(%s) is failed.", pkgid);
565 _LOGD("pkgmgr_parser_parse_manifest_for_upgrade() is ok.");
567 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
569 // apply smack for pkg root path
570 memset(buff, '\0', BUF_SIZE);
571 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
572 // _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
574 // apply smack for defined directory
575 __rpm_apply_smack((char*)pkgid, 0);
578 ret = _ri_apply_privilege((char*)pkgid, 0);
580 _LOGE("_ri_apply_privilege(%s) failed. ret = [%d]", pkgid, ret);
582 _LOGD("_ri_apply_privilege(%s) success.", pkgid);
586 ret = _ri_smack_reload(pkgid, UPGRADE_REQ);
588 _LOGE("_ri_smack_reload(%s) failed.", pkgid);
593 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
595 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
596 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
602 int _coretpk_installer_remove_db_info(const char *pkgid)
605 pkgmgrinfo_pkginfo_h handle = NULL;
607 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
611 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
612 tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_parser_parse_manifest_for_uninstallation is failed, pkgid=[%s]", pkgid);
614 _LOGD("Remove db info is OK.");
617 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
622 int _coretpk_installer_set_smack_label_access(const char *path, const char *label)
624 int res = smack_lsetlabel(path, label, SMACK_LABEL_ACCESS);
627 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", label, res, path);
633 int _coretpk_installer_get_smack_label_access(const char *path, char **label)
635 int res = smack_lgetlabel(path, label, SMACK_LABEL_ACCESS);
638 _LOGE("Error in getting smack ACCESS label failed. result[%d] (path:[%s]))", res, path);
644 int _coretpk_installer_set_smack_label_transmute(const char *path, const char *flag)
646 int res = smack_lsetlabel(path, flag, SMACK_LABEL_TRANSMUTE);
649 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", flag, res, path);
655 int _coretpk_installer_verify_privilege_list(GList *privilege_list, int visibility)
657 char *error_privilege_name = NULL;
661 ret = privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE, privilege_list, visibility, &error_privilege_name);
662 if (ret != PRVMGR_ERR_NONE) {
663 _LOGE("privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) failed. ret = [%d][%s]", ret, error_privilege_name);
664 fprintf(stdout, "\n verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) failed. [%d][%s]\n", ret, error_privilege_name);
666 if (error_privilege_name) {
667 free(error_privilege_name);
670 list = g_list_first(privilege_list);
675 list = g_list_next(list);
677 g_list_free(privilege_list);
678 privilege_list = NULL;
680 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
683 _LOGD("privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
686 list = g_list_first(privilege_list);
691 list = g_list_next(list);
693 g_list_free(privilege_list);
694 privilege_list = NULL;
699 void _coretpk_installer_search_ui_gadget(const char *pkgid)
703 bool is_ug_pkg = false;
704 pkgmgrinfo_pkginfo_h pkghandle = NULL;
706 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
707 retm_if(ret < 0, "@Failed to get the pkginfo handle.");
710 ret = pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ui_gadget_func, &is_ug_pkg);
711 tryvm_if(ret < 0, ret = RPM_INSTALLER_ERR_INTERNAL, "Fail to get applist");
713 /*if there is ug app, apply privilege*/
714 if (is_ug_pkg == true) {
715 ret = pkgmgrinfo_pkginfo_foreach_privilege(pkghandle, __coretpk_privilege_func, NULL);
716 tryvm_if(ret < 0, ret = RPM_INSTALLER_ERR_INTERNAL, "Fail to get privilege list");
720 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
724 int _coretpk_backend_interface(const char *reqcommand, const ri_frontend_cmdline_arg *data)
726 if (reqcommand == NULL || data == NULL) {
727 _LOGE("reqcommand or data is NULL.");
731 if (strncmp(reqcommand, CORETPK_INSTALL, strlen(CORETPK_INSTALL)) == 0) {
732 return _coretpk_installer_prepare_package_install(data->pkgid, data->clientid);
733 } else if (strncmp(reqcommand, CORETPK_UNINSTALL, strlen(CORETPK_UNINSTALL)) == 0) {
734 return _coretpk_installer_prepare_package_uninstall(data->pkgid);
735 } else if (strncmp(reqcommand, CORETPK_DIRECTORY_INSTALL, strlen(CORETPK_DIRECTORY_INSTALL)) == 0) {
736 return _coretpk_installer_prepare_directory_install(data->pkgid, data->clientid);
737 } else if (strncmp(reqcommand, CORETPK_MOVE, strlen(CORETPK_MOVE)) == 0) {
738 return _coretpk_installer_package_move(data->pkgid, data->move_type);
739 } else if (strncmp(reqcommand, CORETPK_REINSTALL, strlen(CORETPK_REINSTALL)) == 0) {
740 return _coretpk_installer_package_reinstall(data->pkgid, data->clientid);
747 static char * _coretpk_installer_get_pkg_path(const char *pkg_path, const char *pkgid)
750 char buff[BUF_SIZE] = {'\0'};
751 char *real_path = NULL;
753 snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
755 if (__is_dir(buff)) break;
756 memset(buff, '\0', BUF_SIZE);
757 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
758 if (__is_dir(buff)) break;
759 memset(buff, '\0', BUF_SIZE);
760 snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
761 if (__is_dir(buff)) break;
762 memset(buff, '\0', BUF_SIZE);
763 snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS, pkgid);
764 if (__is_dir(buff)) break;
769 _LOGE("chdir() failed [%s]\n", strerror(errno));
773 real_path = (char *)malloc(strlen(buff) + 1);
774 if (real_path == NULL) {
775 _LOGE("Malloc failed!\n");
778 memset(real_path, '\0', strlen(buff) + 1);
779 memcpy(real_path, buff, strlen(buff));
785 int _coretpk_installer_verify_signatures(const char *root_path, const char *pkgid, int *visibility)
788 char buff[BUF_SIZE] = {'\0'};
789 const char *pkg_path = root_path;
791 _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
794 // check for signature and certificate
795 pkg_path = _coretpk_installer_get_pkg_path(root_path, pkgid);
796 if (pkg_path == NULL) {
797 _LOGE("pkg_path is NULL.");
802 ret = chdir(root_path);
804 _LOGE("chdir(%s) failed. [%s]", root_path, strerror(errno));
807 // author-signature.xml is mandatory
808 snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
809 if (access(buff, F_OK) == 0) {
810 _LOGD("author-signature.xml is found, path=[%s]", buff);
811 ret = _ri_verify_sig_and_cert(buff, visibility);
813 _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
817 _LOGD("_ri_verify_sig_and_cert succeed, path=[%s]", buff);
819 _LOGE("cannot access xml, path=[%s]", buff);
823 memset(buff, '\0', BUF_SIZE);
825 // signature1.xml is mandatory
826 snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
827 if (access(buff, F_OK) == 0) {
828 _LOGD("signature1.xml is found, path=[%s]", pkg_path);
829 ret = _ri_verify_sig_and_cert(buff, visibility);
831 _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
835 _LOGD("_ri_verify_sig_and_cert() succeed, path=[%s]", buff);
837 _LOGE("cannot access xml, path=[%s]", buff);
841 memset(buff, '\0', BUF_SIZE);
855 char* _coretpk_installer_load_directory(char *directory,char *pkgfile)
859 struct dirent *result;
862 char *pkgname = NULL;
863 char xml_file[MAX_BUF_SIZE] = {'\0'};
865 buf = malloc(BUF_SIZE);
867 _LOGE("malloc() failed.");
871 dir = opendir(directory);
873 _LOGL("opendir()", errno);
878 _LOGD("loading manifest files, directory=[%s]", directory);
880 for (ret = readdir_r(dir, &entry, &result);
881 ret == 0 && result != NULL;
882 ret = readdir_r(dir, &entry, &result)) {
883 char *manifest = NULL;
885 if (!strcmp(entry.d_name, ".") ||
886 !strcmp(entry.d_name, "..")) {
890 manifest = _manifest_to_package(entry.d_name);
892 _LOGE("failed to convert file to xml, file=[%s].", entry.d_name);
896 memset(xml_file,'\0',MAX_BUF_SIZE);
897 snprintf(xml_file,MAX_BUF_SIZE-1,"%s/%s", directory, manifest);
898 _LOGD("manifest=[%s], path=[%s]", manifest, xml_file);
900 ret = _get_package_name_from_xml(xml_file, &pkgname);
901 if (ret != PMINFO_R_OK || pkgname == NULL) {
902 _LOGE("unable to read, xml_file=[%s]", xml_file);
907 if (pkgname[0] != '\0') {
908 snprintf(buf, BUF_SIZE, "%s/%s", directory, manifest);
927 pkginfo *_coretpk_installer_get_pkgfile_info(char *pkgfile)
929 pkginfo *info = NULL;
931 char cwd[BUF_SIZE] = {'\0'};
932 char buff[BUF_SIZE] = {'\0'};
933 char manifest[BUF_SIZE] = { '\0'};
936 char *tmp_pkgid = NULL;
937 char *tmp_version = NULL;
939 temp = getcwd(cwd, BUF_SIZE);
940 if ((temp == NULL) || (cwd[0] == '\0')) {
941 _LOGL("getcwd()", errno);
945 ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_755);
947 if (access(TEMP_DIR, F_OK) == 0) {
948 _rpm_delete_dir(TEMP_DIR);
949 ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_644);
951 _LOGL("mkdir()", errno);
955 _LOGL("access()", errno);
960 ret = chdir(TEMP_DIR);
962 _LOGL("chdir()", errno);
966 _LOGD("switched to %s", TEMP_DIR);
968 snprintf(manifest, BUF_SIZE, "%s", CORETPK_XML);
969 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, manifest, "-d", TEMP_DIR, NULL };
970 ret = _ri_xsystem(unzip_argv);
972 _LOGE("cannot find manifest in the package.");
973 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
977 char* manifestpath = _coretpk_installer_load_directory(TEMP_DIR, pkgfile);
978 if (manifestpath != NULL) {
979 strcpy(buff, manifestpath);
983 if (buff[0] == '\0') {
984 _LOGE("cannot find manifest in the package.");
988 _LOGD("manifest file=[%s]",buff);
990 info = calloc(1, sizeof(pkginfo));
992 _LOGE("calloc() failed.");
996 tmp_pkgid = __find_info_from_xml(buff, "package");
997 if (tmp_pkgid != NULL) {
998 strncpy(info->package_name, tmp_pkgid, sizeof(info->package_name) - 1);
1001 _LOGE("can not get pkgid");
1005 tmp_version = __find_info_from_xml(buff, "version");
1006 if (tmp_version != NULL) {
1007 strncpy(info->version, tmp_version, sizeof(info->version) - 1);
1010 _LOGE("can not get version");
1014 _LOGD("pkgid=[%s], version=[%s]", info->package_name, info->version);
1017 _rpm_delete_dir(TEMP_DIR);
1021 _LOGL("chdir()", errno);
1027 int _coretpk_installer_convert_manifest(char *manifestfilepath, char *pkgid, char* clientid)
1030 char rwmanifest[BUF_SIZE] = {'\0'};
1032 if (clientid != NULL) {
1033 _LOGD("client package id=[%s]", clientid);
1037 if (strstr(manifestfilepath, OPT_USR_APPS)) {
1038 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
1039 const char *rw_xml_argv[] = { CORETPK_RW_XML_CONVERTER, manifestfilepath, rwmanifest, pkgid, clientid, NULL };
1040 ret = _ri_xsystem(rw_xml_argv);
1042 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
1043 const char *ro_xml_argv[] = { CORETPK_RO_XML_CONVERTER, manifestfilepath, rwmanifest, pkgid, clientid, NULL };
1044 ret = _ri_xsystem(ro_xml_argv);
1047 _LOGD("pkgid=[%s]", pkgid);
1048 _LOGD("tizen-manifest=[%s]", manifestfilepath);
1049 _LOGD("converted manifest=[%s]", rwmanifest);
1052 if (ret == INCLUDE_ABSOLUTE_PATH) {
1053 _LOGE("path of exec or icon can not be started with absolute path.");
1055 _LOGL("converting the manifest file", errno);
1057 ret = RPM_INSTALLER_ERR_INTERNAL;
1061 _LOGD("manifest is converted successfully");
1067 int _coretpk_installer_get_configuration_value(char *value)
1069 char buffer[BUF_SIZE] = {'\0'};
1075 if (access(CORETPK_CONFIG_PATH, F_OK) != 0) {
1076 /* if there is no ini file, signature has to be checked */
1080 fi = fopen(CORETPK_CONFIG_PATH, "r");
1082 _LOGL("fopen()", errno);
1086 while (fgets(buffer, BUF_SIZE, fi) != NULL) {
1087 /* buffer will be like signature=off\n\0*/
1088 if (strncmp(buffer, value, strlen(value)) == 0) {
1089 len = strlen(buffer);
1090 /*remove newline character*/
1091 buffer[len - 1] = '\0';
1092 p = strchr(buffer, '=');
1095 if (strcmp(p, "on") == 0) {
1110 int _coretpk_installer_apply_file_policy(char *filepath)
1114 if (access(filepath, F_OK) == 0) {
1116 ret = chmod(filepath, FILE_PERMISSION_644);
1118 _LOGL("chmod()", errno);
1121 _LOGE("skip! empty filepath=[%s]", filepath);
1127 int _coretpk_installer_apply_directory_policy(char *dirpath, int mode, bool appowner)
1131 struct dirent entry;
1132 struct dirent *result;
1133 char fullpath[BUF_SIZE] = {'\0'};
1135 if (access(dirpath, F_OK) != 0) {
1136 _LOGE("skip! empty dirpath=[%s]", dirpath);
1140 dir = opendir(dirpath);
1142 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1147 ret = _coretpk_installer_change_mode(dirpath, DIRECTORY_PERMISSION_755);
1149 _LOGE("_coretpk_installer_change_mode is failed, dirpath=[%s]", dirpath);
1152 for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
1153 if (strcmp(entry.d_name, ".") == 0) {
1154 snprintf(fullpath, BUF_SIZE, "%s/", dirpath);
1155 if (appowner == true) {
1156 _coretpk_installer_change_directory_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1158 ret = _coretpk_installer_change_mode(fullpath, DIRECTORY_PERMISSION_755);
1160 _LOGE("_coretpk_installer_change_mode is failed, fullpath=[%s]", fullpath);
1163 } else if (strcmp(entry.d_name, "..") == 0) {
1168 if (entry.d_type == DT_DIR) {
1169 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1172 if (appowner == true) {
1173 ret = _coretpk_installer_change_directory_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1175 _LOGE("_coretpk_installer_change_directory_owner failed, fullpath=[%s]", fullpath);
1180 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1182 // permission(input mode)
1183 ret = _coretpk_installer_change_mode(fullpath, mode);
1185 _LOGE("_coretpk_installer_change_mode failed, fullpath=[%s]", fullpath);
1189 if (appowner == true) {
1190 ret = _coretpk_installer_change_file_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1192 _LOGE("_coretpk_installer_change_file_owner failed, fullpath=[%s]", fullpath);
1198 if (entry.d_type == DT_DIR) {
1199 ret = _coretpk_installer_apply_directory_policy(fullpath, mode, appowner);
1201 _LOGE("_coretpk_installer_apply_directory_policy failed, fullpath=[%s]", fullpath);
1204 memset(fullpath, '\0', BUF_SIZE);
1212 int _coretpk_installer_make_directory_for_ext(char *pkgid)
1214 char ext_pkg_base_path[BUF_SIZE] = {0, };
1215 char temp_path[BUF_SIZE] = {0, };
1216 char pkg_shared_data_path[BUF_SIZE] = {0, };
1217 char *shared_data_label = NULL;
1220 if (access(OPT_STORAGE_SDCARD, F_OK) != 0) {
1221 _LOGL("There is no OPT_STORAGE_SDCARD", errno);
1226 if (access(OPT_STORAGE_SDCARD_APP_ROOT, F_OK) != 0) {
1228 res = mkdir(OPT_STORAGE_SDCARD_APP_ROOT, DIRECTORY_PERMISSION_755);
1230 _LOGL("mkdir()", errno);
1236 snprintf(ext_pkg_base_path, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
1237 res = mkdir(ext_pkg_base_path, 0500);
1238 if (res == -1 && errno != EEXIST)
1240 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1244 res = _coretpk_installer_set_smack_label_access(ext_pkg_base_path, "_");
1247 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1252 memset(temp_path, 0, BUF_SIZE);
1253 strcpy(temp_path, ext_pkg_base_path);
1254 strncat(temp_path, "/data", strlen("/data"));
1255 res = mkdir(temp_path, 0700);
1256 if (res == -1 && errno != EEXIST)
1258 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1261 res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1264 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1269 memset(temp_path, 0, BUF_SIZE);
1270 strcpy(temp_path, ext_pkg_base_path);
1271 strncat(temp_path, "/cache", strlen("/cache"));
1272 res = mkdir(temp_path, 0700);
1273 if (res == -1 && errno != EEXIST)
1275 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1278 res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1281 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1286 memset(temp_path, 0, BUF_SIZE);
1287 strcpy(temp_path, ext_pkg_base_path);
1288 strncat(temp_path, "/shared", strlen("/shared"));
1289 res = mkdir(temp_path, 0500);
1290 if (res == -1 && errno != EEXIST)
1292 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1295 res = _coretpk_installer_set_smack_label_access(temp_path, "_");
1298 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1302 snprintf(pkg_shared_data_path, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid , "shared/data");
1304 res = access(pkg_shared_data_path, F_OK);
1307 _LOGD("Exist shared/data folder (path:[%s])", pkg_shared_data_path);
1308 res = _coretpk_installer_get_smack_label_access(pkg_shared_data_path, &shared_data_label);
1311 _LOGE("_coretpk_installer_get_smack_label_access() is failed.");
1316 strncat(temp_path, "/data", strlen("/data"));
1317 res = mkdir(temp_path, 0705);
1318 if (res == -1 && errno != EEXIST)
1320 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1324 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1327 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1331 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1334 _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1339 memset(temp_path, 0, BUF_SIZE);
1340 strcpy(temp_path, ext_pkg_base_path);
1341 strncat(temp_path, "/shared", strlen("/shared"));
1342 strncat(temp_path, "/cache", strlen("/cache"));
1343 res = mkdir(temp_path, 0700);
1344 if (res == -1 && errno != EEXIST)
1346 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1349 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1352 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1355 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1358 _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1363 else if (res == -1 && errno == ENOENT)
1365 _LOGD("Directory dose not exist. path: %s, errno: %d (%s)",
1366 pkg_shared_data_path, errno, strerror(errno));
1371 _LOGE("access() failed. path: %s, errno: %d (%s)",
1372 pkg_shared_data_path, errno, strerror(errno));
1380 int _coretpk_installer_make_directory(char *pkgid)
1383 char appdir[BUF_SIZE] = {'\0'};
1384 char rootfile[BUF_SIZE] = {'\0'};
1385 char *groupid = NULL;
1388 if (pkgid == NULL) {
1389 _LOGE("pkgid is NULL.");
1394 memset(appdir, '\0', BUF_SIZE);
1395 snprintf(appdir, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
1396 if (access(appdir, F_OK) != 0) {
1398 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1400 _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1403 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1405 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1410 memset(appdir, '\0', BUF_SIZE);
1411 snprintf(appdir, BUF_SIZE, "%s/%s/bin", OPT_USR_APPS, pkgid);
1412 if (access(appdir, F_OK) != 0) {
1413 memset(appdir, '\0', BUF_SIZE);
1414 snprintf(appdir, BUF_SIZE, "%s/%s/bin", USR_APPS, pkgid);
1415 if (access(appdir, F_OK) != 0) {
1416 _LOGE("[%s] is not existed.", appdir);
1420 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1422 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1427 memset(appdir, '\0', BUF_SIZE);
1428 snprintf(appdir, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgid);
1429 if (access(appdir, F_OK) != 0) {
1431 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1433 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1436 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1438 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1443 memset(appdir, '\0', BUF_SIZE);
1444 snprintf(appdir, BUF_SIZE, "%s/%s/lib", OPT_USR_APPS, pkgid);
1445 if (access(appdir, F_OK) != 0) {
1446 memset(appdir, '\0', BUF_SIZE);
1447 snprintf(appdir, BUF_SIZE, "%s/%s/lib", USR_APPS, pkgid);
1449 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1451 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1456 memset(appdir, '\0', BUF_SIZE);
1457 snprintf(appdir, BUF_SIZE, "%s/%s/res", OPT_USR_APPS, pkgid);
1458 if (access(appdir, F_OK) != 0) {
1459 memset(appdir, '\0', BUF_SIZE);
1460 snprintf(appdir, BUF_SIZE, "%s/%s/res", USR_APPS, pkgid);
1462 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1464 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1469 memset(appdir, '\0', BUF_SIZE);
1470 snprintf(appdir, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgid);
1471 if (access(appdir, F_OK) != 0) {
1473 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1475 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1478 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1480 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1485 memset(appdir, '\0', BUF_SIZE);
1486 snprintf(appdir, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgid);
1487 if (access(appdir, F_OK) != 0) {
1488 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1490 _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1493 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1495 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1498 memset(appdir, '\0', BUF_SIZE);
1499 snprintf(appdir, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgid);
1500 if (access(appdir, F_OK) != 0) {
1501 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1503 _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1506 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1508 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1513 memset(appdir, '\0', BUF_SIZE);
1514 snprintf(appdir, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
1515 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1517 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1522 memset(appdir, '\0', BUF_SIZE);
1523 snprintf(appdir, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgid);
1524 if (access(appdir, F_OK) != 0) {
1525 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1527 _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1530 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1532 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1537 memset(appdir, '\0', BUF_SIZE);
1538 snprintf(appdir, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
1539 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1541 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1544 memset(appdir, '\0', BUF_SIZE);
1545 snprintf(appdir, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgid);
1546 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1548 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1553 memset(appdir, '\0', BUF_SIZE);
1554 snprintf(appdir, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
1555 if (access(appdir, F_OK) != 0) {
1556 ret = _coretpk_installer_get_group_id(pkgid, &groupid);
1558 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1560 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1565 ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1567 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1571 // [pkgid]/tizen-manifest.xml
1572 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
1573 ret = _coretpk_installer_apply_file_policy(rootfile);
1575 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1578 memset(rootfile, '\0', BUF_SIZE);
1579 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, CORETPK_XML);
1580 ret = _coretpk_installer_apply_file_policy(rootfile);
1582 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1586 // [pkgid]/author-signature.xml
1587 memset(rootfile, '\0', BUF_SIZE);
1588 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1589 ret = _coretpk_installer_apply_file_policy(rootfile);
1591 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1594 memset(rootfile, '\0', BUF_SIZE);
1595 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1596 ret = _coretpk_installer_apply_file_policy(rootfile);
1598 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1602 // [pkgid]/signature1.xml
1603 memset(rootfile, '\0', BUF_SIZE);
1604 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SIGNATURE1_XML);
1605 ret = _coretpk_installer_apply_file_policy(rootfile);
1607 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1610 memset(rootfile, '\0', BUF_SIZE);
1611 snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, SIGNATURE1_XML);
1612 ret = _coretpk_installer_apply_file_policy(rootfile);
1614 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1618 // /opt/share/packages/[pkgid].xml
1619 memset(rootfile, '\0', BUF_SIZE);
1620 snprintf(rootfile, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
1621 ret = _coretpk_installer_apply_file_policy(rootfile);
1623 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1626 memset(rootfile, '\0', BUF_SIZE);
1627 snprintf(rootfile, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
1628 ret = _coretpk_installer_apply_file_policy(rootfile);
1630 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1634 // for external storage
1635 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
1636 int ret = _coretpk_installer_make_directory_for_ext(pkgid);
1638 _LOGE("_coretpk_installer_make_directory_for_ext() failed, pkgid=[%s]", pkgid);
1646 int _coretpk_installer_change_mode(char *path, int mode)
1650 ret = chmod(path, mode);
1652 _LOGL("chmod()", errno);
1659 int _coretpk_installer_change_file_owner(char *path, int ownerid, int groupid)
1663 if (access(path, F_OK) == 0) {
1664 ret = chown(path, ownerid, groupid);
1666 _LOGL("chown()", errno);
1674 int _coretpk_installer_change_directory_owner(char *dirpath, int ownerid, int groupid)
1678 if (__is_dir(dirpath)) {
1679 ret = chown(dirpath, ownerid, groupid);
1681 _LOGL("chown()", errno);
1689 void _coretpk_installer_set_privilege_setup_path_for_ext(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1691 char path[BUF_SIZE] = {'\0'};
1693 snprintf(path, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, dirpath);
1694 if (access(path, F_OK) == 0) {
1695 _ri_privilege_setup_path(pkgid, path, type, label);
1699 void _coretpk_installer_set_privilege_setup_path(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1701 char path[BUF_SIZE] = {'\0'};
1703 snprintf(path, BUF_SIZE, "%s/%s", USR_APPS, dirpath);
1704 if (access(path, F_OK) == 0) {
1705 _ri_privilege_setup_path(pkgid, path, type, label);
1707 memset(path, '\0', BUF_SIZE);
1709 snprintf(path, BUF_SIZE, "%s/%s", OPT_USR_APPS, dirpath);
1710 if (access(path, F_OK) == 0) {
1711 _ri_privilege_setup_path(pkgid, path, type, label);
1715 int _coretpk_installer_get_group_id(char *pkgid, char **result)
1718 const char *value = NULL;
1719 char author_signature[BUF_SIZE] = {'\0'};
1720 char *e_rootcert = NULL;
1721 char *d_rootcert = NULL;
1723 unsigned char hashout[BUF_SIZE] = {'\0'};
1724 unsigned int h_size = 0;
1727 pkgmgrinfo_certinfo_h handle = NULL;
1729 snprintf(author_signature, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1730 if (access(author_signature, F_OK) != 0) {
1731 _LOGE("[%s] is not found.", author_signature);
1733 memset(author_signature, '\0', BUF_SIZE);
1734 snprintf(author_signature, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1735 if (access(author_signature, F_OK) != 0) {
1736 _LOGE("[%s] is not found.", author_signature);
1739 _LOGE("author_signature=[%s]", author_signature);
1743 ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
1745 _LOGE("failed to get cert info.");
1749 ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle);
1751 _LOGE("failed to load cert info.");
1755 /*get root certificate*/
1756 ret = pkgmgrinfo_pkginfo_get_cert_value(handle, PMINFO_AUTHOR_SIGNER_CERT, &value);
1757 if (ret < 0 || value == NULL) {
1758 _LOGE("failed to get cert value.");
1763 d_rootcert = (char *)g_base64_decode(value, &d_size);
1764 if (d_rootcert == NULL) {
1765 _LOGE("failed to execute decode.");
1770 EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
1772 _LOGE("@Failed to get hash.");
1777 e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
1778 if (e_rootcert == NULL) {
1779 _LOGE("failed to execute encode.");
1782 e_size = strlen(e_rootcert);
1783 _LOGD("encoding done, len=[%d]", e_size);
1786 for (length = e_size; length >= 0; --length) {
1787 if (e_rootcert[length] == '/') {
1788 e_rootcert[length] = '#';
1792 *result = e_rootcert;
1801 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
1807 int _coretpk_installer_apply_smack_for_ext(char *pkgname)
1810 char dirpath[BUF_SIZE] = {'\0'};
1813 snprintf(dirpath, BUF_SIZE, "%s", pkgname);
1814 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1815 memset(dirpath, '\0', BUF_SIZE);
1818 snprintf(dirpath, BUF_SIZE, "%s/data", pkgname);
1819 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1820 memset(dirpath, '\0', BUF_SIZE);
1823 snprintf(dirpath, BUF_SIZE, "%s/cache", pkgname);
1824 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1825 memset(dirpath, '\0', BUF_SIZE);
1828 snprintf(dirpath, BUF_SIZE, "%s/shared", pkgname);
1829 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1830 memset(dirpath, '\0', BUF_SIZE);
1832 // [pkgid]/shared/data
1833 snprintf(dirpath, BUF_SIZE, "%s/shared/data", pkgname);
1834 _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PUBLIC_RO, NULL);
1839 int _coretpk_installer_apply_smack(char *pkgname, int flag)
1842 char dirpath[BUF_SIZE] = {'\0'};
1843 char manifest[BUF_SIZE] = {'\0'};
1844 char *groupid = NULL;
1845 char *shared_data_label = NULL;
1847 _ri_privilege_register_package(pkgname);
1850 snprintf(dirpath, BUF_SIZE, "%s", pkgname);
1851 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1852 memset(dirpath, '\0', BUF_SIZE);
1855 snprintf(dirpath, BUF_SIZE, "%s/shared", pkgname);
1856 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1857 memset(dirpath, '\0', BUF_SIZE);
1860 snprintf(dirpath, BUF_SIZE, "%s/shared/res", pkgname);
1861 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1862 memset(dirpath, '\0', BUF_SIZE);
1865 snprintf(dirpath, BUF_SIZE, "%s/shared/data", pkgname);
1866 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PUBLIC_RO, NULL);
1867 memset(dirpath, '\0', BUF_SIZE);
1870 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
1871 ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
1873 memset(dirpath, '\0', BUF_SIZE);
1874 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgname);
1875 ret = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
1877 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1879 ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
1881 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1886 memset(dirpath, '\0', BUF_SIZE);
1887 snprintf(dirpath, BUF_SIZE, "%s/shared/trusted", pkgname);
1888 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
1889 ret = _coretpk_installer_get_group_id(pkgname, &groupid);
1891 LOGD("groupid = [%s] for shared/trusted.", groupid);
1892 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
1896 LOGE("_coretpk_installer_get_group_id(%s) failed.", pkgname);
1900 memset(dirpath, '\0', BUF_SIZE);
1903 snprintf(dirpath, BUF_SIZE, "%s/bin", pkgname);
1904 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1905 memset(dirpath, '\0', BUF_SIZE);
1908 snprintf(dirpath, BUF_SIZE, "%s/data", pkgname);
1909 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1910 memset(dirpath, '\0', BUF_SIZE);
1913 snprintf(dirpath, BUF_SIZE, "%s/lib", pkgname);
1914 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1915 memset(dirpath, '\0', BUF_SIZE);
1918 snprintf(dirpath, BUF_SIZE, "%s/res", pkgname);
1919 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1920 memset(dirpath, '\0', BUF_SIZE);
1923 snprintf(dirpath, BUF_SIZE, "%s/cache", pkgname);
1924 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1925 memset(dirpath, '\0', BUF_SIZE);
1927 // tizen-manifest.xml
1928 snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, CORETPK_XML);
1929 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1930 memset(dirpath, '\0', BUF_SIZE);
1932 // author-signature.xml
1933 snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, AUTHOR_SIGNATURE_XML);
1934 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1935 memset(dirpath, '\0', BUF_SIZE);
1938 snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, SIGNATURE1_XML);
1939 _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1940 memset(dirpath, '\0', BUF_SIZE);
1943 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgname);
1944 if (access(manifest, F_OK) == 0) {
1945 _ri_privilege_setup_path(pkgname, manifest, APP_PATH_PRIVATE, pkgname);
1949 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
1950 if (access(OPT_STORAGE_SDCARD, F_OK) == 0) {
1951 ret = _coretpk_installer_apply_smack_for_ext(pkgname);
1953 _LOGE("_coretpk_installer_apply_smack_for_ext(%s) failed.", pkgname);
1962 static char * __getprivilege(const char* pBuf)
1964 const char* pKey = "<privilege>";
1965 const char* p = NULL;
1966 const char* pStart = NULL;
1967 const char* pEnd = NULL;
1969 p = strstr(pBuf, pKey);
1973 pStart = p + strlen(pKey);
1974 pEnd = strchr(pStart, '<');
1978 size_t len = pEnd - pStart;
1982 char *pRes = (char*)malloc(len + 1);
1984 _LOGE("malloc failed!!");
1987 strncpy(pRes, pStart, len);
1993 int _coretpk_installer_apply_privilege(char *pkgid, char *pkgPath, int apiVisibility)
1998 char *find_str = NULL;
1999 char buf[BUF_SIZE] = {0};
2000 char manifest[BUF_SIZE] = {'\0'};
2001 const char *perm[] = {NULL, NULL};
2002 int apptype = PERM_APP_TYPE_EFL;
2004 if (apiVisibility & CERT_SVC_VISIBILITY_PLATFORM) {
2005 _LOGD("VISIBILITY_PLATFORM!");
2006 apptype = PERM_APP_TYPE_EFL_PLATFORM;
2007 } else if ((apiVisibility & CERT_SVC_VISIBILITY_PARTNER) ||
2008 (apiVisibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
2009 (apiVisibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
2010 _LOGD("VISIBILITY_PARTNER!");
2011 apptype = PERM_APP_TYPE_EFL_PARTNER;
2014 snprintf(manifest, BUF_SIZE, "%s/%s", pkgPath, CORETPK_XML);
2015 _LOGD("pkgid = [%s], manifest = [%s]", pkgid, manifest);
2017 fp = fopen(manifest, "r");
2019 _LOGE("Fail get : %s\n", manifest);
2023 while (fgets(buf, BUF_SIZE, fp) != NULL) {
2026 if (strstr(buf, "<privilege>")) {
2027 find_str = __getprivilege(buf);
2028 if (find_str != NULL) {
2029 _LOGD("privilege = [%s]", find_str);
2032 ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2034 _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", pkgid, apptype);
2036 _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", pkgid, apptype);
2042 _LOGD("find_str is null.");
2046 memset(buf, 0x00, BUF_SIZE);
2055 int _coretpk_installer_package_install(char *pkgfile, char *pkgid, char *clientid)
2058 char buff[BUF_SIZE] = {'\0'};
2059 char manifest[BUF_SIZE] = {'\0'};
2060 char cwd[BUF_SIZE] = {'\0'};
2062 char rwmanifest[BUF_SIZE] = {'\0'};
2065 /* for external installation */
2066 app2ext_handle *handle = NULL;
2067 GList *dir_list = NULL;
2068 int install_status = APP2EXT_STATUS_SUCCESS;
2071 if (pkgfile == NULL || pkgid == NULL) {
2072 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2073 return RPM_INSTALLER_ERR_WRONG_PARAM;
2076 /*send event for start*/
2077 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "install");
2078 _LOGD("[#]start : _coretpk_installer_package_install[%s]", pkgid);
2080 /*send event for install_percent*/
2081 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2083 snprintf(buff, BUF_SIZE, "%s", OPT_USR_APPS);
2084 const char *mkdir_argv[] = { "/bin/mkdir", "-p", buff, NULL };
2085 ret = _ri_xsystem(mkdir_argv);
2087 _LOGE("Failed to make usr application dir.");
2090 /*If the directory which will be installed exists, remove it.*/
2091 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2092 if (__is_dir(buff)) {
2093 _rpm_delete_dir(buff);
2097 ret = __pre_install_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2099 _LOGE("__pre_install_for_mmc is failed.");
2103 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, "-d", buff, NULL };
2104 ret = _ri_xsystem(unzip_argv);
2106 _LOGE("failed to unzip for path=[%s], ret=[%d]", buff, ret);
2109 _LOGD("unzip is done successfully, path=[%s]", buff);
2112 temp = getcwd(cwd, BUF_SIZE);
2113 if ((temp == NULL) || (cwd[0] == '\0')) {
2114 _LOGL("getcwd()", errno);
2115 ret = RPM_INSTALLER_ERR_INTERNAL;
2118 _LOGD("current working directory, path=[%s]", cwd);
2123 _LOGL("chdir()", errno);
2124 ret = RPM_INSTALLER_ERR_INTERNAL;
2128 /*check for signature and certificate*/
2129 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2130 ret = _coretpk_installer_verify_signatures(buff, pkgid, &visibility);
2132 _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
2133 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2136 _LOGD("signature and certificate are verified successfully.");
2142 _LOGL("chdir()", errno);
2143 ret = RPM_INSTALLER_ERR_INTERNAL;
2147 /*convert manifest and copy the file to /opt/share/packages*/
2148 snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
2149 ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
2151 _LOGE("failed to convert the manifest.");
2152 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2155 _LOGD("manifest is converted successfully.");
2157 if (strstr(pkgfile, ".wgt") != NULL) {
2158 _LOGD("wgt file=[%s]", pkgfile);
2160 if (strstr(manifest, OPT_USR_APPS)) {
2161 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2162 const char *rw_xml_category[] = { CORETPK_CATEGORY_CONVERTER, rwmanifest, NULL };
2163 ret = _ri_xsystem(rw_xml_category);
2167 /*check the manifest file.*/
2168 memset(manifest, '\0', sizeof(manifest));
2169 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2170 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
2171 ret = pkgmgr_parser_check_manifest_validation(manifest);
2173 _LOGE("invalid manifest file");
2174 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2178 /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
2179 ret = pkgmgr_parser_parse_usr_manifest_for_installation(manifest, getuid(), NULL);
2181 _LOGE("failed to parse the manifest.");
2182 ret = RPM_INSTALLER_ERR_INTERNAL;
2185 _LOGD("manifest parsing done successfully.");
2188 _coretpk_installer_search_ui_gadget(pkgid);
2190 /*send event for install_percent*/
2191 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2193 /*register cert info*/
2194 _ri_register_cert(pkgid);
2197 ret = _coretpk_installer_make_directory(pkgid);
2199 _LOGE("failed to make the directory.");
2203 /*apply smack to app dir*/
2204 ret = _coretpk_installer_apply_smack(pkgid, 1);
2206 _LOGE("failed to apply the smack.");
2210 /*apply smack by privilege*/
2211 ret = _ri_apply_privilege(pkgid, visibility);
2213 _LOGE("failed to apply permission, ret=[%d]", ret);
2215 _LOGD("permission applying done successfully.");
2217 // Check privilege and visibility
2218 if (privilege_list) {
2219 ret = _coretpk_installer_verify_privilege_list(privilege_list, visibility);
2223 _LOGD("_coretpk_installer_verify_privilege_list done.");
2229 ret = _ri_smack_reload(pkgid, REQUEST_TYPE_INSTALL);
2231 _LOGD("failed to reload the smack.");
2235 /*send event for install_percent*/
2236 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2238 ret = RPM_INSTALLER_SUCCESS;
2243 install_status = APP2EXT_STATUS_FAILED;
2245 _LOGD("install status is [%d].", install_status);
2246 if (__post_install_for_mmc(handle, pkgid, dir_list, install_status) < 0) {
2247 _LOGE("__post_install_for_mmc is failed.");
2252 _LOGD("_coretpk_installer_package_install is done.");
2253 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2256 ret = _coretpk_installer_remove_db_info(pkgid);
2258 _LOGE("_coretpk_installer_remove_db_info is failed.");
2261 /*remove xml(/opt/share/packages/pkgid.xml)*/
2262 if (access(manifest, F_OK) == 0) {
2263 (void)remove(manifest);
2266 /*remove app dir(/opt/usr/apps/pkgid)*/
2267 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2268 if (__is_dir(buff)) {
2269 _rpm_delete_dir(buff);
2272 /*remove ext app dir(/opt/storage/sdcard/apps/pkgid)*/
2273 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
2274 char extpath[BUF_SIZE] = {'\0'};
2275 snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
2276 if (__is_dir(extpath)) {
2277 _rpm_delete_dir(extpath);
2281 char *errorstr = NULL;
2282 _ri_error_no_to_string(ret, &errorstr);
2283 _ri_broadcast_status_notification(pkgid, "coretpk", "error", errorstr);
2286 _LOGE("_coretpk_installer_package_install is failed.");
2287 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2293 int _coretpk_installer_package_uninstall(const char *pkgid)
2296 int update_system = 0;
2298 update_system = __check_updated_system_package(pkgid);
2300 if (update_system == 1) {
2301 _LOGD("start remove_update, pkgid=[%s]", pkgid);
2302 ret = __pkg_remove_update(pkgid);
2304 _LOGD("start uninstall, pkgid=[%s]", pkgid);
2305 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2309 _LOGE("uninstallation is failed, pkgid=[%s], update_system=[%d]", pkgid, update_system);
2311 _LOGD("uninstallation is done successfully, pkgid=[%s]", pkgid);
2317 int _coretpk_installer_package_upgrade(char *pkgfile, char *pkgid, char *clientid)
2320 char buff[BUF_SIZE] = {'\0'};
2321 char manifest[BUF_SIZE] = { '\0'};
2322 char cwd[BUF_SIZE] = {'\0'};
2323 char rwmanifest[BUF_SIZE] = {'\0'};
2324 pkgmgrinfo_pkginfo_h pkghandle = NULL;
2328 /* for external upgrade */
2329 app2ext_handle *handle = NULL;
2330 GList *dir_list = NULL;
2333 if (pkgfile == NULL || pkgid == NULL) {
2334 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2335 return RPM_INSTALLER_ERR_WRONG_PARAM;
2338 /*send event for start*/
2339 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
2341 /*terminate running app*/
2342 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2344 _LOGE("failed to get the pkginfo handle.");
2345 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2348 pkgmgrinfo_appinfo_get_list(pkghandle, PMINFO_UI_APP, __ri_check_running_app, NULL);
2349 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2351 /*remove dir for clean*/
2352 __ri_remove_updated_dir(pkgid);
2355 ret = __pre_upgrade_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2357 _LOGE("__pre_upgrade_for_mmc is failed.");
2361 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2362 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, "-d", buff, NULL };
2363 ret = _ri_xsystem(unzip_argv);
2365 _LOGE("failed to unzip for [%s, %d].", buff, ret);
2368 _LOGD("#unzip[%s] success.", buff);
2370 /*send event for install_percent*/
2371 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2374 temp = getcwd(cwd, BUF_SIZE);
2375 if ((temp == NULL) || (cwd[0] == '\0')) {
2376 _LOGL("getcwd()", errno);
2377 ret = RPM_INSTALLER_ERR_INTERNAL;
2380 _LOGD("#Current working directory is %s.", cwd);
2385 _LOGL("chdir()", errno);
2386 ret = RPM_INSTALLER_ERR_INTERNAL;
2390 /*check for signature and certificate*/
2391 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2392 ret = _coretpk_installer_verify_signatures(buff, pkgid, &visibility);
2394 _LOGE("@Failed to verify signature and certificate[%s].", pkgid);
2395 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2398 _LOGD("#signature and certificate verifying success");
2404 _LOGL("chdir()", errno);
2405 ret = RPM_INSTALLER_ERR_INTERNAL;
2409 /*convert manifest and copy the file to /opt/share/packages*/
2410 snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
2411 ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
2413 _LOGE("@Failed to convert the manifest.");
2414 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2417 _LOGD("#manifest converting success");
2419 if (strstr(pkgfile, ".wgt") != NULL) {
2420 _LOGD("wgt file = [%s]", pkgfile);
2422 if (strstr(manifest, OPT_USR_APPS)) {
2423 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2424 const char *rw_xml_category[] = { CORETPK_CATEGORY_CONVERTER, rwmanifest, NULL };
2425 ret = _ri_xsystem(rw_xml_category);
2429 /*check the manifest file.*/
2430 memset(manifest, '\0', sizeof(manifest));
2431 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2432 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
2433 ret = pkgmgr_parser_check_manifest_validation(manifest);
2435 _LOGE("@invalid manifest file");
2436 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2440 /*send event for install_percent*/
2441 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2443 /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
2444 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
2446 _LOGE("@parsing manifest failed.");
2447 ret = RPM_INSTALLER_ERR_INTERNAL;
2450 _LOGD("#parsing manifest success.");
2453 _coretpk_installer_search_ui_gadget(pkgid);
2455 /*unregister cert info*/
2456 _ri_unregister_cert(pkgid);
2458 /*register cert info*/
2459 _ri_register_cert(pkgid);
2462 ret = _coretpk_installer_make_directory(pkgid);
2464 _LOGE("@Failed to make the directory");
2468 // Remove origin rule
2469 _ri_privilege_unregister_package(pkgid);
2471 /*apply smack to app dir*/
2472 ret = _coretpk_installer_apply_smack(pkgid, 1);
2474 _LOGE("@Failed to apply the smack.");
2478 /*apply smack by privilege*/
2479 ret = _ri_apply_privilege(pkgid, visibility);
2481 _LOGE("@Failed to apply permission[%d].", ret);
2483 _LOGD("#permission applying success.");
2485 // Check privilege and visibility
2486 if (privilege_list) {
2487 ret = _coretpk_installer_verify_privilege_list(privilege_list, visibility);
2491 _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
2497 ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
2499 _LOGE("@Failed to reload the smack.");
2503 /*send event for install_percent*/
2504 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2505 ret = RPM_INSTALLER_SUCCESS;
2509 if (__post_upgrade_for_mmc(handle, pkgid, dir_list) < 0) {
2510 _LOGE("__post_upgrade_for_mmc is failed.");
2515 _LOGD("[#]end : _coretpk_installer_package_upgrade");
2516 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2519 /*TODO:need to add recovery logic*/
2521 char *errorstr = NULL;
2522 _ri_error_no_to_string(ret, &errorstr);
2523 _ri_broadcast_status_notification(pkgid, "coretpk", "error", errorstr);
2526 _LOGE("[@]end : _coretpk_installer_package_upgrade");
2527 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2533 char* _coretpk_installer_get_pkgid_from_directory_path(char *dirpath)
2535 char* subpath = strrchr(dirpath, '/');
2539 int _coretpk_installer_directory_install(char *dirpath, char *clientid)
2542 char manifest[BUF_SIZE] = {'\0'};
2543 char cwd[BUF_SIZE] = {'\0'};
2549 if (dirpath == NULL) {
2550 _LOGE("dirpath is NULL.");
2551 return RPM_INSTALLER_ERR_WRONG_PARAM;
2554 _LOGD("directory_install start: dirpath = [%s]", dirpath);
2557 temp = getcwd(cwd, BUF_SIZE);
2558 if ((temp == NULL) || (cwd[0] == '\0')) {
2559 _LOGE("getcwd() failed. [%d][%s]", errno, strerror(errno));
2560 ret = RPM_INSTALLER_ERR_INTERNAL;
2564 _LOGD("Current working directory is [%s].", cwd);
2567 ret = chdir(dirpath);
2569 _LOGE("chdir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
2570 ret = RPM_INSTALLER_ERR_INTERNAL;
2574 // check for signature and certificate
2575 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2576 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
2577 _LOGD("pkgid=[%s]", pkgid);
2579 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
2581 _LOGE("_coretpk_installer_verify_signatures(%s, %s) failed.", dirpath, pkgid);
2582 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2585 _LOGD("verify_signatures(%s, %s) succeed!", dirpath, pkgid);
2591 _LOGE("chdir(%s) failed. [%d][%s]", cwd, errno, strerror(errno));
2592 ret = RPM_INSTALLER_ERR_INTERNAL;
2596 // convert manifest and copy the file to /usr/share/packages
2597 snprintf(manifest, BUF_SIZE, "%s/%s", dirpath, CORETPK_XML);
2598 if (pkgid == NULL) {
2599 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
2600 _LOGD("pkgid = [%s]", pkgid);
2603 ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
2605 _LOGE("_coretpk_installer_convert_manifest() failed. manifest = [%s], pkgid = [%s]", manifest, pkgid);
2606 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2609 _LOGD("convert_manifest(%s, %s) succeed!", manifest, pkgid);
2611 // check the manifest file
2612 memset(manifest, '\0', sizeof(manifest));
2613 if (strstr(dirpath, OPT_USR_APPS)) {
2614 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2616 snprintf(manifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2618 _LOGD("manifest = [%s]", manifest);
2620 // compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)
2621 ret = pkgmgr_parser_check_manifest_validation(manifest);
2623 _LOGE("pkgmgr_parser_check_manifest_validation(%s) failed.", manifest);
2624 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2628 // Parse the manifest to get install location and size. If installation fails, remove manifest info from DB
2629 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2631 _LOGE("pkgmgr_parser_parse_manifest_for_installation(%s) failed.", manifest);
2632 ret = RPM_INSTALLER_ERR_INTERNAL;
2635 _LOGD("parse_manifest(%s) succeed!", manifest);
2637 // register cert info
2638 _ri_register_cert(pkgid);
2641 ret = _coretpk_installer_make_directory(pkgid);
2643 _LOGE("_coretpk_installer_make_directory(%s) failed.", pkgid);
2646 _LOGD("make_directory(%s) succeed!", pkgid);
2648 // apply smack to app dir
2649 ret = _coretpk_installer_apply_smack(pkgid, 1);
2651 _LOGE("_coretpk_installer_apply_smack(%s) failed.", pkgid);
2653 _LOGD("apply_smack(%s) succeed!", pkgid);
2655 // apply smack by privilege
2656 ret = _ri_apply_privilege(pkgid, visibility);
2658 _LOGE("_ri_apply_privilege(%s, %d) failed. ret = [%d]", pkgid, visibility, ret);
2660 _LOGD("apply_privilege(%s, %d) succeed!", pkgid, visibility);
2662 ret = RPM_INSTALLER_SUCCESS;
2665 _LOGD("directory_install end: dirpath = [%s], ret = [%d]", dirpath, ret);
2670 int _coretpk_installer_prepare_package_install(char *pkgfile, char *clientid)
2673 pkginfo *info = NULL;
2674 pkginfo *dbinfo = NULL;
2679 info = _coretpk_installer_get_pkgfile_info(pkgfile);
2680 if (info == NULL || (strlen(info->package_name) == 0)) {
2681 _LOGE("failed to get the pkg info.");
2682 ret = RPM_INSTALLER_ERR_INTERNAL;
2686 pkgid = strdup(info->package_name);
2687 if (pkgid == NULL) {
2688 _LOGE("strdup() failed.");
2689 ret = RPM_INSTALLER_ERR_INTERNAL;
2693 dbinfo = _rpm_installer_get_pkgname_info(info->package_name);
2695 if (dbinfo == NULL) {
2696 /*package is not installed. Go for installation.*/
2697 _LOGD("start to install");
2698 ret = _coretpk_installer_package_install(pkgfile, pkgid, clientid);
2699 } else if (strcmp(info->version, dbinfo->version) > 0) {
2701 _LOGD("start to upgrade");
2702 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2703 } else if (strcmp(info->version, dbinfo->version) < 0) {
2705 _LOGD("start to downgrade");
2706 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2708 /*same package. Reinstall it. Manifest should be parsed again */
2709 _LOGD("start to reinstall");
2710 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2714 _LOGE("result=[%d]", ret);
2741 _ri_broadcast_status_notification("Invalid package", "invalid", "start", "install");
2742 _ri_broadcast_status_notification("Invalid package", "invalid", "end", "fail");
2747 int _coretpk_installer_prepare_package_uninstall(const char *pkgid)
2749 if (pkgid == NULL) {
2750 _LOGE("pkgid is NULL.");
2751 return RPM_INSTALLER_ERR_WRONG_PARAM;
2754 _LOGD("pkgid=[%s]", pkgid);
2757 pkginfo *dbinfo = NULL;
2759 dbinfo = _rpm_installer_get_pkgname_info(pkgid);
2760 if (dbinfo == NULL) {
2761 _LOGE("[%s] is not installed.", pkgid);
2762 return RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
2765 ret = _coretpk_installer_package_uninstall(pkgid);
2767 _LOGE("_coretpk_installer_package_uninstall() failed, pkgid=[%s], ret=[%d]", pkgid, ret);
2769 _LOGD("_coretpk_installer_package_uninstall() is done successfully, pkgid=[%s]", pkgid);
2780 int _coretpk_installer_prepare_directory_install(char *dirpath, char *clientid)
2784 ret = _coretpk_installer_directory_install(dirpath, clientid);
2785 _LOGD("path=[%s], result=[%d]", dirpath, ret);
2790 int _coretpk_installer_package_move(char* pkgid, int move_type)
2793 app2ext_handle *hdl = NULL;
2796 GList *dir_list = NULL;
2797 pkgmgrinfo_pkginfo_h pkghandle = NULL;
2799 _ri_broadcast_status_notification(pkgid, "coretpk", "start", "move");
2800 _LOGD("[#]start : _coretpk_installer_package_move[%s][%d]", pkgid, move_type);
2802 if (move_type == PM_MOVE_TO_INTERNAL) {
2803 movetype = APP2EXT_MOVE_TO_PHONE;
2804 } else if (move_type == PM_MOVE_TO_SDCARD) {
2805 movetype = APP2EXT_MOVE_TO_EXT;
2807 ret = RPM_INSTALLER_ERR_WRONG_PARAM;
2811 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2813 _LOGE("@Failed to get the pkginfo handle.");
2814 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2818 /* Terminate the running instance of app */
2819 pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
2820 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2822 hdl = app2ext_init(APP2EXT_SD_CARD);
2823 if ((hdl != NULL) && (hdl->interface.move != NULL)) {
2824 dir_list = __rpm_populate_dir_list();
2825 if (dir_list == NULL) {
2826 _LOGE("@Failed to get the populate directory.");
2827 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
2831 ret = hdl->interface.move(pkgid, dir_list, movetype);
2832 __rpm_clear_dir_list(dir_list);
2834 _LOGE("@Failed to move app.");
2835 ret = RPM_INSTALLER_ERR_INTERNAL;
2838 if(move_type == PM_MOVE_TO_INTERNAL) {
2839 _LOGD("#updating the installed storage from external to internal");
2840 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
2842 _LOGD("#updating the installed storage from internal to external");
2843 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
2846 if (ret != PMINFO_R_OK) {
2847 _LOGE("@Failed to udpate the installed storage.");
2848 ret = RPM_INSTALLER_ERR_INTERNAL;
2854 _LOGE("@Failed to get app2ext handle.");
2855 ret = RPM_INSTALLER_ERR_INTERNAL;
2860 app2ext_deinit(hdl);
2864 _LOGD("[#]end : _coretpk_installer_package_move");
2865 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2867 _LOGE("[@]end : _coretpk_installer_package_move");
2868 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2876 int _coretpk_installer_copy_file( const char *src_path, const char *dst_path)
2881 unsigned char temp_buf[8192] = {'\0',};
2882 size_t size_of_uchar = sizeof(unsigned char);
2883 size_t size_of_temp_buf = sizeof(temp_buf);
2885 src = fopen(src_path, "r");
2887 _LOGE("@Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
2891 dst = fopen(dst_path, "w");
2893 /*No such file or directory*/
2894 if (errno == ENOENT) {
2895 /*make the path of parent dir for the data*/
2896 char *path = strdup(dst_path);
2897 char *p = strrchr(path, '/');
2905 int idx = strlen(path) - strlen(p);
2908 /*make the parent dir*/
2909 const char *mkdir_argv[] = { "/bin/mkdir", "-p", path, NULL };
2910 ret = _ri_xsystem(mkdir_argv);
2912 _LOGE("Failed to make parent dir.");
2915 _LOGD("#[%s] is created.", path);
2919 dst = fopen(dst_path, "w");
2921 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
2926 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
2932 while (!feof(src)) {
2933 rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
2934 fwrite( temp_buf, size_of_uchar, rc, dst);
2948 int _coretpk_installer_handle_rds_data(char *pkgid, GList *delete, GList *add, GList *modify, int *updatexml)
2952 char handledata[BUF_SIZE] = {'\0'};
2953 char srcfile[BUF_SIZE] = {'\0'};
2954 char destfile[BUF_SIZE] = {'\0'};
2957 if (delete != NULL) {
2958 list = g_list_first(delete);
2960 char *data = (char *)list->data;
2961 if (!strcasestr(data, RDS_DELTA_DELETE)) {
2962 snprintf(handledata, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
2964 const char *delete_argv[] = { "/bin/rm", "-rf", handledata, NULL };
2965 ret = _ri_xsystem(delete_argv);
2967 _LOGD("#[delete] success : %s", data);
2969 _LOGD("#[delete] fail : %s", data);
2971 memset(handledata, '\0', sizeof(handledata));
2974 list = g_list_next(list);
2977 _LOGD("#There is no deleted data.");
2982 list = g_list_first(add);
2984 char *data = (char *)list->data;
2985 if (!strcasestr(data, RDS_DELTA_ADD)) {
2986 snprintf(srcfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, data);
2987 snprintf(destfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
2989 if (__is_dir((char *)srcfile)) {
2990 const char *mkdir_argv[] = { "/bin/mkdir", "-p", destfile, NULL };
2991 _ri_xsystem(mkdir_argv);
2992 _LOGD("#[%s] is created.", destfile);
2994 ret =_coretpk_installer_copy_file(srcfile, destfile);
2996 _LOGD("#[add] success : %s", data);
2998 _LOGD("#[add] fail : %s", data);
3001 memset(srcfile, '\0', sizeof(srcfile));
3002 memset(destfile, '\0', sizeof(destfile));
3005 list = g_list_next(list);
3008 _LOGD("#There is no added data.");
3012 if (modify != NULL) {
3013 list = g_list_first(modify);
3015 char *data = (char *)list->data;
3016 if (!strcasestr(data, RDS_DELTA_MODIFY)) {
3017 /*If XML is modified, the checking codes for xml has to be executed.*/
3018 if (strcmp(data, CORETPK_XML) == 0) {
3022 snprintf(srcfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, data);
3023 snprintf(destfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
3025 ret =_coretpk_installer_copy_file(srcfile, destfile);
3027 _LOGD("#[modify] success : %s", data);
3029 _LOGD("#[modify] fail : %s", data);
3032 memset(srcfile, '\0', sizeof(srcfile));
3033 memset(destfile, '\0', sizeof(destfile));
3035 list = g_list_next(list);
3038 _LOGD("#There is no modified data.");
3044 int _coretpk_installer_read_rds_file(char *pkgid, char *rdsfile, int *updatexml)
3047 int state = RDS_STATE_NONE;
3049 char buffer[BUF_SIZE] = {'\0'};
3052 GList *delete_list = NULL;
3053 GList *add_list = NULL;
3054 GList *modify_list = NULL;
3056 if (access(rdsfile, F_OK) != 0) {
3057 _LOGL("access()", errno);
3061 fi = fopen(rdsfile, "r");
3063 _LOGL("fopen()", errno);
3067 while (fgets(buffer, BUF_SIZE, fi) != NULL) {
3068 buffer[strlen(buffer) - 1] = '\0';
3071 if (buffer[0] == '#') {
3072 if (strcasestr(buffer, RDS_DELTA_DELETE)) {
3073 state = RDS_STATE_DELETE;
3074 } else if (strcasestr(buffer, RDS_DELTA_ADD)) {
3075 state = RDS_STATE_ADD;
3076 } else if (strcasestr(buffer, RDS_DELTA_MODIFY)) {
3077 state = RDS_STATE_MODIFY;
3079 state = RDS_STATE_NONE;
3083 if (state == RDS_STATE_NONE) {
3084 _LOGE("Unknown RDS State, INSTALLER_RDS_STATE_NONE");
3088 /*make rds data list*/
3090 case RDS_STATE_DELETE:
3091 _LOGD("RDS_STATE_DELETE data : %s", buffer);
3092 delete_list = g_list_append(delete_list, g_strdup(buffer));
3096 _LOGD("RDS_STATE_ADD data : %s", buffer);
3097 add_list = g_list_append(add_list, g_strdup(buffer));
3100 case RDS_STATE_MODIFY:
3101 _LOGD("RDS_STATE_MODIFY data : %s", buffer);
3102 modify_list = g_list_append(modify_list, g_strdup(buffer));
3107 ret = _coretpk_installer_handle_rds_data(pkgid, delete_list, add_list, modify_list, updatexml);
3109 _LOGE("@Failed to handle rds data.");
3112 if (delete_list != NULL) {
3113 g_list_free(delete_list);
3115 if (add_list != NULL) {
3116 g_list_free(add_list);
3118 if (modify_list != NULL) {
3119 g_list_free(modify_list);
3126 int _coretpk_installer_package_reinstall(char *pkgid, char *clientid)
3129 char manifest[BUF_SIZE] = {'\0'};
3130 char rdsfile[BUF_SIZE] = {'\0'};
3131 char dirpath[BUF_SIZE] = {'\0'};
3132 char cwd[BUF_SIZE] = {'\0'};
3138 if (pkgid == NULL) {
3139 _LOGE("@The input param[pkgid] is NULL.");
3140 return RPM_INSTALLER_ERR_WRONG_PARAM;
3143 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "start", "update");
3144 _LOGD("[#]start : _coretpk_installer_package_reinstall[%s]", pkgid);
3146 snprintf(rdsfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, RDS_DELTA_FILE);
3147 ret = _coretpk_installer_read_rds_file(pkgid, rdsfile, &updatexml);
3149 _LOGE("@Failed to read the rds file.");
3150 ret = RPM_INSTALLER_ERR_INTERNAL;
3153 _LOGD("#RDS file reading success");
3155 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "30");
3158 temp = getcwd(cwd, BUF_SIZE);
3159 if ((temp == NULL) || (cwd[0] == '\0')) {
3160 _LOGL("getcwd()", errno);
3161 ret = RPM_INSTALLER_ERR_INTERNAL;
3164 _LOGD("#Current working directory is %s.", cwd);
3167 snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3168 ret = chdir(dirpath);
3170 _LOGL("chdir()", errno);
3171 ret = RPM_INSTALLER_ERR_INTERNAL;
3175 /*check for signature and certificate*/
3176 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
3178 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
3179 _LOGD("pkgid[%s]", pkgid);
3181 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
3183 _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
3184 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3187 _LOGD("signature and certificate verifying success");
3193 _LOGL("chdir()", errno);
3194 ret = RPM_INSTALLER_ERR_INTERNAL;
3199 /*convert manifest and copy the file to /opt/share/packages*/
3200 snprintf(manifest, BUF_SIZE, "%s/%s", dirpath, CORETPK_XML);
3201 if (pkgid == NULL) {
3202 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
3203 _LOGD("pkgid[%s]", pkgid);
3205 ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
3207 _LOGE("@Failed to convert the manifest.");
3208 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3211 _LOGD("#manifest converting success");
3213 /*check the manifest file.*/
3214 memset(manifest, '\0', sizeof(manifest));
3215 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
3216 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
3217 ret = pkgmgr_parser_check_manifest_validation(manifest);
3219 _LOGE("@invalid manifest file");
3220 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3224 /*Parse the manifest to get install location and size. If failed, remove manifest info from DB.*/
3225 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
3227 _LOGE("@Failed to parse the manifest.");
3228 ret = RPM_INSTALLER_ERR_INTERNAL;
3231 _LOGD("#manifest parsing success");
3234 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "60");
3236 /*register cert info*/
3237 _ri_register_cert(pkgid);
3240 ret = _coretpk_installer_make_directory(pkgid);
3242 _LOGE("@Failed to make directory");
3246 _ri_privilege_unregister_package(pkgid);
3248 /*apply smack to app dir*/
3249 ret = _coretpk_installer_apply_smack(pkgid, 1);
3251 _LOGE("@Failed to apply smack.");
3255 /*apply smack by privilege*/
3256 ret = _ri_apply_privilege(pkgid, visibility);
3258 _LOGE("@Failed to apply permission[%d].", ret);
3260 _LOGD("#permission applying success.");
3262 // Check privilege and visibility
3263 if (privilege_list) {
3264 ret = _coretpk_installer_verify_privilege_list(privilege_list, visibility);
3268 _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
3274 ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
3276 _LOGE("@Failed to reload the smack.");
3280 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "100");
3281 ret = RPM_INSTALLER_SUCCESS;
3285 _LOGD("[#]end : _coretpk_installer_package_reinstall");
3286 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "ok");
3289 ret = _coretpk_installer_remove_db_info(pkgid);
3291 _LOGE("_coretpk_installer_remove_db_info is failed.");
3294 /*remove xml(/opt/share/packages/pkgid.xml)*/
3295 if (access(manifest, F_OK) == 0) {
3296 (void)remove(manifest);
3299 /*remove app dir(/opt/usr/apps/pkgid)*/
3300 if (__is_dir(dirpath)) {
3301 _rpm_delete_dir(dirpath);
3304 _LOGE("[@]end : _coretpk_installer_package_reinstall");
3305 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "fail");
3311 int _coretpk_installer_csc_install(char *path_str, char *remove_str)
3314 pkginfo *info = NULL;
3315 char buff[BUF_SIZE] = {'\0'};
3316 char manifest[BUF_SIZE] = {'\0'};
3317 char cwd[BUF_SIZE] = {'\0'};
3319 char *csc_tags[3] = {NULL, };
3323 if (path_str == NULL || remove_str == NULL) {
3324 _LOGE("@The input param[pkgfile or pkgid] is NULL.");
3325 return RPM_INSTALLER_ERR_WRONG_PARAM;
3328 _LOGD("[##]csc-core : start csc_install[path=%s]", path_str);
3330 info = _coretpk_installer_get_pkgfile_info(path_str);
3331 if (info == NULL || (strlen(info->package_name) == 0)) {
3332 _LOGE("[@@]end : _coretpk_installer_prepare_package_install: failed to get the pkg info.");
3333 ret = RPM_INSTALLER_ERR_INTERNAL;
3337 _LOGD("[##]csc-core : get pkgid [%s]", info->package_name);
3339 /*If the directory which will be installed exists, remove it.*/
3340 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, info->package_name);
3341 if (__is_dir(buff)) {
3342 _rpm_delete_dir(buff);
3345 _LOGD("[##]csc-core : real path [%s]", buff);
3347 const char *unzip_argv[] = { "/usr/bin/unzip", "-o", path_str, "-d", buff, NULL };
3348 ret = _ri_xsystem(unzip_argv);
3350 _LOGE("@Failed to unzip for [%s, %d].", buff, ret);
3354 _LOGD("[##]csc-core : unzip success[%s]", buff);
3357 temp = getcwd(cwd, BUF_SIZE);
3358 if ((temp == NULL) || (cwd[0] == '\0')) {
3359 _LOGL("getcwd()", errno);
3360 ret = RPM_INSTALLER_ERR_INTERNAL;
3367 _LOGL("chdir()", errno);
3368 ret = RPM_INSTALLER_ERR_INTERNAL;
3372 _LOGD("[##]csc-core : check signature");
3374 /*check for signature and certificate*/
3375 if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
3376 ret = _coretpk_installer_verify_signatures(buff, info->package_name, &visibility);
3378 _LOGE("@Failed to verify signature and certificate[%s].", info->package_name);
3379 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3382 _LOGD("[##]csc-core : signature verify success[%s]", buff);
3388 _LOGL("chdir()", errno);
3389 ret = RPM_INSTALLER_ERR_INTERNAL;
3393 /*convert manifest and copy the file to /opt/share/packages*/
3394 snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, info->package_name, CORETPK_XML);
3395 ret = _coretpk_installer_convert_manifest(manifest, info->package_name, NULL);
3397 _LOGE("@Failed to convert the manifest.");
3398 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3402 _LOGD("[##]csc-core : manifest converting success");
3404 /*check the manifest file.*/
3405 memset(manifest, '\0', sizeof(manifest));
3406 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, info->package_name);
3407 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
3408 ret = pkgmgr_parser_check_manifest_validation(manifest);
3410 _LOGE("@invalid manifest file");
3411 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3415 _LOGD("[##]csc-core : manifest validation success");
3417 /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
3418 if (strcmp(remove_str,"true")==0)
3419 csc_tags[0] = "removable=true";
3421 csc_tags[0] = "removable=false";
3423 csc_tags[1] = "preload=true";
3426 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, csc_tags);
3428 _LOGE("@Failed to parse the manifest.");
3429 ret = RPM_INSTALLER_ERR_INTERNAL;
3433 _LOGD("[##]csc-core : register manifest success");
3435 /*register cert info*/
3436 _ri_register_cert(info->package_name);
3439 ret = _coretpk_installer_make_directory(info->package_name);
3441 _LOGE("@Failed to make the directory");
3445 _LOGD("[##]csc-core : make directory success");
3447 /*apply smack to app dir*/
3448 ret = _coretpk_installer_apply_smack(info->package_name, 1);
3450 _LOGE("@Failed to apply the smack.");
3454 _LOGD("[##]csc-core : apply_smack success");
3456 /*apply smack by privilege*/
3457 ret = _ri_apply_privilege(info->package_name, visibility);
3459 _LOGE("@Failed to apply permission[%d].", ret);
3462 _LOGD("[##]csc-core : apply_privilege success");
3466 ret = _ri_smack_reload(info->package_name, REQUEST_TYPE_INSTALL);
3468 _LOGD("@Failed to reload the smack.");
3472 _LOGD("[##]csc-core : smack_reload success");
3474 ret = RPM_INSTALLER_SUCCESS;
3478 _LOGD("[##]csc-core : finish csc core success");
3480 /*remove xml(/opt/share/packages/pkgid.xml)*/
3481 if (access(manifest, F_OK) == 0) {
3482 (void)remove(manifest);
3485 /*remove app dir(/opt/usr/apps/pkgid)*/
3486 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, info->package_name);
3487 if (__is_dir(buff)) {
3488 _rpm_delete_dir(buff);
3490 _LOGD("[##]csc-core : finish csc core fail");