Tizen 2.4.0 rev3 SDK Public Release
[framework/base/rpm-installer.git] / backend / src / coretpk / coretpk-installer.c
1 /*
2  * coretpk-installer
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE
23 #endif
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <dirent.h>
28 #include <unistd.h>
29 #include <glib.h>
30 #include <openssl/sha.h>
31 #include <openssl/evp.h>
32 #include <vconf.h>
33 #include <unzip.h>
34 #include <sys/smack.h>
35 #include <ctype.h>
36
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>
44
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"
52
53 extern pkgmgr_installer *pi;
54 extern GList *privilege_list;
55
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);
58
59 int __coretpk_patch_padded_api_version(const char *api_version, char **pad_api_version)
60 {
61         char *pad_version = NULL;
62         char *ptr_fw = NULL;
63         char *ptr_bw = NULL;
64
65         pad_version = strdup(api_version);
66         if (pad_version == NULL) {
67                 _LOGE("out of memory");
68                 return RPM_INSTALLER_ERR_NOT_ENOUGH_MEMORY;
69         }
70
71         ptr_fw = strchr(pad_version, '.');
72         ptr_bw = strrchr(pad_version, '.');
73
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);
77                 }
78         }
79
80         *pad_api_version = pad_version;
81
82         return RPM_INSTALLER_SUCCESS;
83 }
84
85 static int __coretpk_compare_with_platform_version(const char *api_version)
86 {
87         char *current_version = NULL;
88         char *platform_version = NULL;
89         int ret = 0;
90         int result = 0;
91
92         if (!api_version) {
93                 _LOGE("Invalid parameter");
94                 return RPM_INSTALLER_ERR_WRONG_PARAM;
95         }
96
97         if (strlen(api_version) == 0) {
98                 _LOGD("No api-version, assume same with platform api-version");
99                 return RPM_INSTALLER_SUCCESS;
100         }
101
102         ret = __coretpk_patch_padded_api_version(api_version, &current_version);
103         if (ret != RPM_INSTALLER_SUCCESS) {
104                 return RPM_INSTALLER_ERR_WRONG_PARAM;
105         }
106
107         ret = __coretpk_patch_padded_api_version(TIZEN_VERSION, &platform_version);
108         if (ret != RPM_INSTALLER_SUCCESS) {
109                 return RPM_INSTALLER_ERR_WRONG_PARAM;
110         }
111
112         _LOGD("platform_version(%s) vs. current_version(%s)", platform_version, current_version);
113
114         result = strverscmp(platform_version, current_version);
115         if (result < 0) {
116                 ret = RPM_INSTALLER_ERR_NOT_SUPPORTED_API_VERSION;
117         } else {
118                 ret = RPM_INSTALLER_SUCCESS;
119         }
120
121         FREE_AND_NULL(current_version);
122         FREE_AND_NULL(platform_version);
123
124         return ret;
125 }
126
127 static int __get_unzip_size(const char *item, unsigned long long *size)
128 {
129         if (!item || !size) {
130                 _LOGE("Invalid argument.");
131                 return PMINFO_R_ERROR;
132         }
133         int ret = 0;
134         unzFile uzf = unzOpen64(item);
135         if(uzf== NULL)
136         {
137                 _LOGE("Fail to open item : %s", item);
138                 *size = 0;
139                 return PMINFO_R_ERROR;
140         } else {
141                 ret = unzGoToFirstFile(uzf);
142                 if(ret != UNZ_OK) {
143                         _LOGE("error get first zip file ");
144                         unzClose(uzf);
145                         *size = 0;
146                         return PMINFO_R_ERROR;
147                 } else {
148                         do{
149                                 ret = unzOpenCurrentFile(uzf);
150                                 if(ret != UNZ_OK) {
151                                         _LOGE("error unzOpenCurrentFile ");
152                                         unzClose(uzf);
153                                         *size = 0;
154                                         return PMINFO_R_ERROR;
155                                 }
156
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;
161                                 if(ret != UNZ_OK) {
162                                         _LOGE("error get current file info");
163                                         unzCloseCurrentFile(uzf);
164                                         *size = 0;
165                                         break;
166                                 }
167
168                                 if (filename) {
169                                         free(filename);
170                                         filename = NULL;
171                                 }
172                         }while(unzGoToNextFile(uzf) == UNZ_OK);
173                 }
174         }
175         unzClose(uzf);
176
177         return PMINFO_R_OK;
178 }
179
180 static int __is_default_external_storage()
181 {
182         int ret = 0;
183         int storage = 0;
184         int mmc_status = VCONFKEY_SYSMAN_MMC_REMOVED;
185
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.");
188
189         if (storage == 1) {
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.");
192
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.");
195                 } else {
196                         _LOGD("mmc_status is MMC_MOUNTED.");
197                         return PMINFO_R_OK;
198                 }
199         }
200
201         return PMINFO_R_ERROR;
202 }
203
204 static void __apply_smack_for_mmc(const char *pkgid)
205 {
206         char dirpath[BUF_SIZE] = {'\0'};
207
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);
211                 return;
212         }
213         memset(dirpath, '\0', BUF_SIZE);
214
215         // [pkgid]/.mmc/bin
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);
219
220         // [pkgid]/.mmc/lib
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);
224
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);
229
230         // [pkgid]/.mmc/res
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);
234
235         return;
236 }
237
238 static int __pre_upgrade_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
239 {
240         int ret = 0;
241         unsigned long long archive_size_byte = 0;
242         int archive_size_mega = 0;
243
244         ret = __is_default_external_storage();
245         if (ret < 0) {
246                 _LOGD("Upgrade storage is internal.");
247                 return 0;
248         }
249         _LOGD("__pre_upgrade start.");
250
251         ret = __get_unzip_size(pkgfile, &archive_size_byte);
252         if (ret < 0) {
253                 _LOGD("Failed to get uncompressed size.");
254                 return PMINFO_R_ERROR;
255         }
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);
258
259         *handle = app2ext_init(APP2EXT_SD_CARD);
260         if (*handle == NULL) {
261                 _LOGE("@app2ext init failed\n");
262                 return PMINFO_R_ERROR;
263         }
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);
269                 }
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;
274                 }
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");
280                 }
281                 else {
282                         _LOGE("@app2xt pre upgrade API failed (%d)\n", ret);
283                         return PMINFO_R_ERROR;
284                 }
285         } else {
286                 _LOGE("handle is not proper.");
287                 return PMINFO_R_ERROR;
288         }
289         _LOGD("__pre_upgrade end.");
290         return PMINFO_R_OK;
291 }
292
293 static int __post_upgrade_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list)
294 {
295         int ret = __is_default_external_storage();
296         if (ret != 0) {
297                 _LOGD("Upgrade storage is internal.");
298                 return 0;
299         }
300         _LOGD("__post_upgrade start.");
301
302         /* set smack again for .mmc folder */
303         __apply_smack_for_mmc(pkgid);
304         _LOGD("__apply_smack_for_mmc is completed.");
305
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);
310         } else {
311                 _LOGE("handle->interface.post_upgrade is NULL.");
312                 return PMINFO_R_ERROR;
313         }
314         _LOGD("__post_upgrade end.");
315         return PMINFO_R_OK;
316 }
317
318 static int __pre_install_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
319 {
320         int ret = 0;
321         unsigned long long archive_size_byte = 0;
322         int archive_size_mega = 0;
323
324         ret = __is_default_external_storage();
325         if (ret != 0) {
326                 _LOGD("Installed storage is internal.");
327                 return 0;
328         }
329         _LOGD("__pre_install start.");
330
331         ret = __get_unzip_size(pkgfile, &archive_size_byte);
332         if (ret < 0) {
333                 _LOGD("Failed to get uncompressed size.");
334                 return PMINFO_R_ERROR;
335         }
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);
338
339         *handle = app2ext_init(APP2EXT_SD_CARD);
340         if (*handle == NULL) {
341                 _LOGE("@app2ext init failed\n");
342                 return PMINFO_R_ERROR;
343         }
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;
350                 }
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;
356                 }
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");
362                 }
363                 else {
364                         _LOGE("@app2xt pre install API failed (%d)\n", ret);
365                         return PMINFO_R_ERROR;
366                 }
367         } else {
368                 _LOGE("handle is not proper.");
369                 return PMINFO_R_ERROR;
370         }
371         _LOGD("__pre_install end.");
372         return PMINFO_R_OK;
373 }
374
375 static int __post_install_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list, int install_status)
376 {
377         int ret = __is_default_external_storage();
378         if (ret != 0) {
379                 _LOGD("Installed storage is internal.");
380                 return 0;
381         }
382         _LOGD("__post_install start.");
383
384         /* set smack again for .mmc folder */
385         __apply_smack_for_mmc(pkgid);
386         _LOGD("__apply_smack_for_mmc is completed.");
387
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);
392         } else {
393                 _LOGE("handle->interface.post_install is NULL.");
394                 return PMINFO_R_ERROR;
395         }
396         _LOGD("__post_install end.");
397         return PMINFO_R_OK;
398 }
399
400 static void __str_trim(char *input)
401 {
402         char *trim_str = input;
403
404         if (input == NULL)
405                 return;
406
407         while (*input != 0) {
408                 if (!isspace(*input)) {
409                         *trim_str = *input;
410                         trim_str++;
411                 }
412                 input++;
413         }
414
415         *trim_str = 0;
416         return;
417 }
418
419 static char *__find_info_from_xml(const char *manifest, const char *find_info)
420 {
421         const char *val = NULL;
422         const xmlChar *node;
423         xmlTextReaderPtr reader;
424         char *info_val = NULL;
425         int ret = -1;
426
427         if(manifest == NULL) {
428                 _LOGE("input argument is NULL\n");
429                 return NULL;
430         }
431
432         if(find_info == NULL) {
433                 _LOGE("find_info is NULL\n");
434                 return NULL;
435         }
436
437         reader = xmlReaderForFile(manifest, NULL, 0);
438
439         if (reader) {
440                 if (_child_element(reader, -1)) {
441                         node = xmlTextReaderConstName(reader);
442                         if (!node) {
443                                 _LOGE("xmlTextReaderConstName value is NULL\n");
444                                 goto end;
445                         }
446
447                         if (!strcmp(ASCII(node), "manifest")) {
448                                 ret = _ri_get_attribute(reader,(char*)find_info,&val);
449                                 if(ret != 0){
450                                         _LOGE("error in getting the attribute value");
451                                         goto end;
452                                 }
453
454                                 if(val) {
455                                         info_val = strdup(val);
456                                         if(info_val == NULL) {
457                                                 _LOGE("malloc failed!!");
458                                         }
459                                 }
460                         } else {
461                                 _LOGE("unable to create xml reader\n");
462                         }
463                 }
464         } else {
465                 _LOGE("xmlReaderForFile value is NULL\n");
466         }
467
468 end:
469         if (reader) {
470                 xmlFreeTextReader(reader);
471         }
472
473         if(val)
474                 free((void*)val);
475
476         return info_val;
477 }
478
479 static int __coretpk_privilege_func(const char *name, void *user_data)
480 {
481         int ret = 0;
482         const char *perm[] = {NULL, NULL};
483         const char *ug_pkgid = "ui-gadget::client";
484
485         perm[0] = name;
486
487         _LOGD("privilege = [%s]", name);
488         _ri_privilege_register_package("ui-gadget::client");
489
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);
492
493         return ret;
494 }
495
496 static int __ui_gadget_func(const pkgmgrinfo_appinfo_h handle, void *user_data)
497 {
498         int ret = 0;
499         bool is_ug = 0;
500         char *pkgid = NULL;
501         char *exec = NULL;
502         char appdir[BUF_SIZE] = {'\0'};
503
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");
506
507         if (is_ug == true) {
508
509                 /*get pkgid*/
510                 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
511                 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get pkgid\n");
512
513                 _LOGD("@[%s] has ui-gadget", pkgid);
514
515                 /*check bin directory*/
516                 snprintf(appdir, BUF_SIZE, "%s/%s/bin", USR_APPS, pkgid);
517                 if (access(appdir, F_OK) != 0) {
518                         /*permission(755)*/
519                         ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
520                         if (ret < 0) {
521                                 _LOGL("mkdir()", errno);
522                                 return -1;
523                         }
524                 }
525
526                 /*get exec*/
527                 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
528                 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get exec\n");
529
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");
534
535                 _LOGD("@[%s] success symlink to [/usr/bin/ug-client]", exec);
536
537                 * (bool *) user_data = true;
538         }
539
540         return 0;
541 }
542
543 static int __check_updated_system_package(const char *pkgid)
544 {
545         int ret = 0;
546         bool is_update = false;
547         bool is_system = false;
548         pkgmgrinfo_pkginfo_h pkghandle = NULL;
549
550         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
551         retvm_if(ret < 0, -1, "pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
552
553         ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
554         tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_system(%s) failed.", pkgid);
555
556         ret = pkgmgrinfo_pkginfo_is_update(pkghandle, &is_update);
557         tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_update(%s) failed.", pkgid);
558
559         if (is_system && is_update) {
560                 _LOGD("pkgid=[%s] is updated system package.", pkgid);
561                 ret = 1;
562         } else {
563                 _LOGD("pkgid=[%s] is not updated system app.", pkgid);
564                 ret = -1;
565         }
566
567 catch:
568         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
569
570         return ret;
571 }
572
573 static int __pkg_remove_update(const char *pkgid)
574 {
575         int ret = 0;
576         char buff[BUF_SIZE] = {'\0'};
577         char rootpath[BUF_SIZE] = {'\0'};
578
579         if (pkgid == NULL) {
580                 _LOGE("pkgid is NULL.");
581                 return -1;
582         }
583
584         // start
585         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
586
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);
591         }
592
593         // Remove origin rule
594         _ri_privilege_unregister_package(pkgid);
595
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);
601         if (ret != 0) {
602                 _LOGE("/usr/bin/unzip(%s) failed.", rootpath);
603         }
604
605         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
606
607         // remove opt xml
608         snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
609         (void)remove(buff);
610
611         // updated usr xml
612         memset(buff, '\0', BUF_SIZE);
613         snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
614
615         _LOGD("manifest = [%s].",buff);
616
617         ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
618         if (ret < 0) {
619                 _LOGE("pkgmgr_parser_parse_manifest_for_upgrade(%s) is failed.", pkgid);
620                 return ret;
621         }
622         _LOGD("pkgmgr_parser_parse_manifest_for_upgrade() is ok.");
623
624         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
625
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);
630
631         // apply smack for defined directory
632         __rpm_apply_smack((char*)pkgid, 0);
633
634         // apply privilege
635         ret = _ri_apply_privilege((char*)pkgid, 0);
636         if (ret != 0) {
637                 _LOGE("_ri_apply_privilege(%s) failed. ret = [%d]", pkgid, ret);
638         } else {
639                 _LOGD("_ri_apply_privilege(%s) success.", pkgid);
640         }
641
642         // reload smack
643         ret = _ri_smack_reload(pkgid, UPGRADE_REQ);
644         if (ret != 0) {
645                 _LOGE("_ri_smack_reload(%s) failed.", pkgid);
646         }
647
648         // finish
649         if (ret != 0) {
650                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
651         } else {
652                 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
653                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
654         }
655
656         return ret;
657 }
658
659 int _coretpk_installer_remove_db_info(const char *pkgid)
660 {
661         int ret = 0;
662         pkgmgrinfo_pkginfo_h handle = NULL;
663
664         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
665         if (ret < 0) {
666                 return PMINFO_R_OK;
667         }
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);
670
671         _LOGD("Remove db info is OK.");
672
673 catch:
674         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
675
676         return ret;
677 }
678
679 int _coretpk_installer_set_smack_label_access(const char *path, const char *label)
680 {
681         int res = smack_lsetlabel(path, label, SMACK_LABEL_ACCESS);
682         if (res != 0)
683         {
684                 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", label, res, path);
685                 return -1;
686         }
687         return 0;
688 }
689
690 int _coretpk_installer_get_smack_label_access(const char *path, char **label)
691 {
692         int res = smack_lgetlabel(path, label, SMACK_LABEL_ACCESS);
693         if (res != 0)
694         {
695                 _LOGE("Error in getting smack ACCESS label failed. result[%d] (path:[%s]))", res, path);
696                 return -1;
697         }
698         return 0;
699 }
700
701 int _coretpk_installer_set_smack_label_transmute(const char *path, const char *flag)
702 {
703         int res = smack_lsetlabel(path, flag, SMACK_LABEL_TRANSMUTE);
704         if (res != 0)
705         {
706                 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", flag, res, path);
707                 return -1;
708         }
709         return 0;
710 }
711
712 int _coretpk_installer_verify_privilege_list(const char *pkg_id, GList *privilege_list, int visibility, const char *api_version)
713 {
714         char *error_privilege_name = NULL;
715         GList *list = NULL;
716         int ret = 0;
717         char buf[BUF_SIZE] = { '\0' };
718
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);
723
724                 if (!error_privilege_name)
725                         error_privilege_name = strdup("Unidentified privilege error");
726
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;
733                 else {
734                         _LOGE("Unidentified privilege error : [%s]", error_privilege_name);
735                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
736                 }
737
738                 snprintf(buf, BUF_SIZE - 1, "%d:%s", ret, error_privilege_name);
739                 _ri_broadcast_privilege_notification(pkg_id, "coretpk", "error", buf);
740         } else
741                 _LOGD("privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
742
743 err:
744
745         if (error_privilege_name)
746                 free(error_privilege_name);
747
748         list = g_list_first(privilege_list);
749         while (list) {
750                 if (list->data) {
751                         free(list->data);
752                 }
753                 list = g_list_next(list);
754         }
755         g_list_free(privilege_list);
756         privilege_list = NULL;
757
758         return ret;
759 }
760
761 void _coretpk_installer_search_ui_gadget(const char *pkgid)
762 {
763         int ret = 0;
764         bool is_ug_pkg = false;
765         pkgmgrinfo_pkginfo_h pkghandle = NULL;
766
767         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
768         retm_if(ret < 0, "@Failed to get the pkginfo handle.");
769
770         /* search ug app */
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");
773
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");
778         }
779
780 catch :
781         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
782 }
783
784 int _coretpk_backend_interface(const char *reqcommand, const ri_frontend_cmdline_arg *data)
785 {
786         if (reqcommand == NULL || data == NULL) {
787                 _LOGE("reqcommand or data is NULL.");
788                 return -1;
789         }
790
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);
801         } else {
802                 return -1;
803         }
804 }
805
806 #if 0
807 static char * _coretpk_installer_get_pkg_path(const char *pkg_path, const char *pkgid)
808 {
809         int ret = 0;
810         char buff[BUF_SIZE] = {'\0'};
811         char *real_path = NULL;
812
813         snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
814         do {
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;
825         } while (0);
826
827         ret = chdir(buff);
828         if (ret != 0) {
829                 _LOGE("chdir() failed [%s]\n", strerror(errno));
830                 return NULL;
831         }
832
833         real_path = (char *)malloc(strlen(buff) + 1);
834         if (real_path == NULL) {
835                 _LOGE("Malloc failed!\n");
836                 return NULL;
837         }
838         memset(real_path, '\0', strlen(buff) + 1);
839         memcpy(real_path, buff, strlen(buff));
840
841         return real_path;
842 }
843 #endif
844
845 int _coretpk_installer_verify_signatures(const char *root_path, const char *pkgid, int *visibility)
846 {
847         int ret = 0;
848         char buff[BUF_SIZE] = {'\0'};
849         const char *pkg_path = root_path;
850
851         _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
852
853 #if 0
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.");
858                 return 0;
859         }
860 #endif
861
862         ret = chdir(root_path);
863         if (ret != 0) {
864                 _LOGE("chdir(%s) failed. [%s]", root_path, strerror(errno));
865         }
866
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);
872                 if (ret) {
873                         _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
874                         ret = -1;
875                         goto end;
876                 }
877                 _LOGD("_ri_verify_sig_and_cert succeed, path=[%s]", buff);
878         } else {
879                 _LOGE("cannot access xml, path=[%s]", buff);
880                 ret = -1;
881                 goto end;
882         }
883         memset(buff, '\0', BUF_SIZE);
884
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);
890                 if (ret) {
891                         _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
892                         ret = -1;
893                         goto end;
894                 }
895                 _LOGD("_ri_verify_sig_and_cert() succeed, path=[%s]", buff);
896         } else {
897                 _LOGE("cannot access xml, path=[%s]", buff);
898                 ret = -1;
899                 goto end;
900         }
901         memset(buff, '\0', BUF_SIZE);
902         ret = 0;
903
904 end:
905 #if 0
906         if(pkg_path){
907                 free(pkg_path);
908                 pkg_path = NULL;
909         }
910 #endif
911
912         return ret;
913 }
914
915 char* _coretpk_installer_load_directory(char *directory,char *pkgfile)
916 {
917         DIR *dir;
918         struct dirent entry;
919         struct dirent *result;
920         int ret = 0;
921         char *buf = NULL;
922         char *pkgname = NULL;
923         char xml_file[MAX_BUF_SIZE] = {'\0'};
924
925         buf = malloc(BUF_SIZE);
926         if (buf == NULL) {
927                 _LOGE("malloc() failed.");
928                 return NULL;
929         }
930
931         dir = opendir(directory);
932         if (!dir) {
933                 _LOGL("opendir()", errno);
934                 free(buf);
935                 return NULL;
936         }
937
938         _LOGD("loading manifest files, directory=[%s]", directory);
939
940         for (ret = readdir_r(dir, &entry, &result);
941                         ret == 0 && result != NULL;
942                         ret = readdir_r(dir, &entry, &result)) {
943                 char *manifest = NULL;
944
945                 if (!strcmp(entry.d_name, ".") ||
946                         !strcmp(entry.d_name, "..")) {
947                         continue;
948                 }
949
950                 manifest = _manifest_to_package(entry.d_name);
951                 if (!manifest) {
952                         _LOGE("failed to convert file to xml, file=[%s].", entry.d_name);
953                         continue;
954                 }
955
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);
959
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);
963                         free(manifest);
964                         continue;
965                 }
966
967                 if (pkgname[0] != '\0') {
968                         snprintf(buf, BUF_SIZE, "%s/%s", directory, manifest);
969                         free(manifest);
970                         break;
971                 }
972
973                 free(manifest);
974         }
975
976         closedir(dir);
977
978         if (pkgname) {
979                 free(pkgname);
980                 pkgname = NULL;
981         }
982
983
984         return buf;
985 }
986
987 pkginfo *_coretpk_installer_get_pkg_info(char *manifest)
988 {
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'};
994
995         if (manifest == NULL || strlen(manifest) >= BUF_SIZE) {
996                 _LOGE("invalid parameter");
997                 goto err;
998         }
999
1000         info = calloc(1, sizeof(pkginfo));
1001         if (info == NULL) {
1002                 _LOGE("calloc() failed.");
1003                 goto err;
1004         }
1005
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);
1010                 free(tmp_pkgid);
1011         } else {
1012                 _LOGE("can not get pkgid");
1013                 goto err;
1014         }
1015
1016         tmp_version = __find_info_from_xml(buff, "version");
1017         if (tmp_version != NULL) {
1018                 strncpy(info->version, tmp_version, sizeof(info->version) - 1);
1019                 free(tmp_version);
1020         } else {
1021                 _LOGE("can not get version");
1022                 goto err;
1023         }
1024
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);
1029         } else {
1030                 _LOGE("can not get api-version");
1031         }
1032
1033         _LOGD("pkgid=[%s], version=[%s], api-version=[%s]", info->package_name, info->version, info->api_version);
1034
1035 err:
1036         return info;
1037 }
1038
1039 pkginfo *_coretpk_installer_get_pkgfile_info(char *pkgfile)
1040 {
1041         pkginfo *info = NULL;
1042         int ret = 0;
1043         char cwd[BUF_SIZE] = {'\0'};
1044         char buff[BUF_SIZE] = {'\0'};
1045         char manifest[BUF_SIZE] = { '\0'};
1046         char *temp = NULL;
1047
1048         char *tmp_pkgid = NULL;
1049         char *tmp_version = NULL;
1050         char *tmp_api_version = NULL;
1051
1052         temp = getcwd(cwd, BUF_SIZE);
1053         if ((temp == NULL) || (cwd[0] == '\0')) {
1054                 _LOGL("getcwd()", errno);
1055                 return NULL;
1056         }
1057
1058         ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_644);
1059         if (ret < 0) {
1060                 if (access(TEMP_DIR, F_OK) == 0) {
1061                         _rpm_delete_dir(TEMP_DIR);
1062                         ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_644);
1063                         if (ret < 0) {
1064                                 _LOGL("mkdir()", errno);
1065                                 return NULL;
1066                         }
1067                 } else {
1068                         _LOGL("access()", errno);
1069                         return NULL;
1070                 }
1071         }
1072
1073         ret = chdir(TEMP_DIR);
1074         if (ret != 0) {
1075                 _LOGL("chdir()", errno);
1076                 goto err;
1077         }
1078
1079         _LOGD("switched to %s", TEMP_DIR);
1080
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);
1084         if (ret != 0) {
1085                 _LOGE("cannot find manifest in the package.");
1086                 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
1087                 goto err;
1088         }
1089
1090         char* manifestpath = _coretpk_installer_load_directory(TEMP_DIR, pkgfile);
1091         if (manifestpath != NULL && strlen(manifestpath) < BUF_SIZE) {
1092                 strcpy(buff, manifestpath);
1093                 free(manifestpath);
1094         }
1095
1096         if (buff[0] == '\0') {
1097                 _LOGE("cannot find manifest in the package.");
1098                 goto err;
1099         }
1100
1101         _LOGD("manifest file=[%s]",buff);
1102
1103         info = calloc(1, sizeof(pkginfo));
1104         if (info == NULL) {
1105                 _LOGE("calloc() failed.");
1106                 goto err;
1107         }
1108
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);
1112                 free(tmp_pkgid);
1113         } else {
1114                 _LOGE("can not get pkgid");
1115                 goto err;
1116         }
1117
1118         tmp_version = __find_info_from_xml(buff, "version");
1119         if (tmp_version != NULL) {
1120                 strncpy(info->version, tmp_version, sizeof(info->version) - 1);
1121                 free(tmp_version);
1122         } else {
1123                 _LOGE("can not get version");
1124                 goto err;
1125         }
1126
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);
1131         } else {
1132                 _LOGE("can not get api-version");
1133         }
1134
1135         _LOGD("pkgid=[%s], version=[%s], api-version=[%s]", info->package_name, info->version, info->api_version);
1136
1137         info->is_widget = _coretpk_parser_is_widget(buff);
1138
1139 err:
1140         _rpm_delete_dir(TEMP_DIR);
1141
1142         ret = chdir(cwd);
1143         if (ret != 0) {
1144                 _LOGL("chdir()", errno);
1145         }
1146
1147         return info;
1148 }
1149
1150 int _coretpk_installer_get_configuration_value(char *value)
1151 {
1152         char buffer[BUF_SIZE] = {'\0'};
1153         char *p = NULL;
1154         FILE *fi = NULL;
1155         int len = 0;
1156         int ret = 0;
1157
1158         if (access(CORETPK_CONFIG_PATH, F_OK) != 0) {
1159                 /* if there is no ini file, signature has to be checked */
1160                 return 1;
1161         }
1162
1163         fi = fopen(CORETPK_CONFIG_PATH, "r");
1164         if (fi == NULL) {
1165                 _LOGL("fopen()", errno);
1166                 return 0;
1167         }
1168
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, '=');
1176                         if (p) {
1177                                 p++;
1178                                 if (strcmp(p, "on") == 0) {
1179                                         ret = 1;
1180                                 } else {
1181                                         ret = 0;
1182                                 }
1183                         }
1184                 } else {
1185                         continue;
1186                 }
1187         }
1188
1189         fclose(fi);
1190         return ret;
1191 }
1192
1193 int _coretpk_installer_apply_file_policy(char *filepath)
1194 {
1195         int ret = 0;
1196
1197         if (access(filepath, F_OK) == 0) {
1198                 /*permission(644)*/
1199                 ret = chmod(filepath, FILE_PERMISSION_644);
1200                 if (ret != 0) {
1201                         _LOGL("chmod()", errno);
1202                 }
1203         } else {
1204                 _LOGE("skip! empty filepath=[%s]", filepath);
1205         }
1206
1207         return 0;
1208 }
1209
1210 int _coretpk_installer_apply_directory_policy(char *dirpath, int mode, bool appowner)
1211 {
1212         int ret = 0;
1213         DIR *dir;
1214         struct dirent entry;
1215         struct dirent *result;
1216         char fullpath[BUF_SIZE] = {'\0'};
1217
1218         if (access(dirpath, F_OK) != 0) {
1219                 _LOGE("skip! empty dirpath=[%s]", dirpath);
1220                 return 0;
1221         }
1222
1223         dir = opendir(dirpath);
1224         if (!dir) {
1225                 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1226                 return -1;
1227         }
1228
1229         // permission(755)
1230         ret = _coretpk_installer_change_mode(dirpath, DIRECTORY_PERMISSION_755);
1231         if (ret != 0) {
1232                 _LOGE("_coretpk_installer_change_mode is failed, dirpath=[%s]", dirpath);
1233         }
1234
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);
1240                         }
1241                         ret = _coretpk_installer_change_mode(fullpath, DIRECTORY_PERMISSION_755);
1242                         if (ret != 0) {
1243                                 _LOGE("_coretpk_installer_change_mode is failed, fullpath=[%s]", fullpath);
1244                         }
1245                         continue;
1246                 } else if (strcmp(entry.d_name, "..") == 0) {
1247                         continue;
1248                 }
1249
1250                 // sub dir
1251                 if (entry.d_type == DT_DIR) {
1252                         snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1253
1254                         // owner:group
1255                         if (appowner == true) {
1256                                 ret = _coretpk_installer_change_directory_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1257                                 if (ret != 0) {
1258                                         _LOGE("_coretpk_installer_change_directory_owner failed, fullpath=[%s]", fullpath);
1259                                 }
1260                         }
1261                 // sub file
1262                 } else {
1263                         snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1264
1265                         // permission(input mode)
1266                         ret = _coretpk_installer_change_mode(fullpath, mode);
1267                         if (ret != 0) {
1268                                 _LOGE("_coretpk_installer_change_mode failed, fullpath=[%s]", fullpath);
1269                         }
1270
1271                         // owner:group
1272                         if (appowner == true) {
1273                                 ret = _coretpk_installer_change_file_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1274                                 if (ret != 0) {
1275                                         _LOGE("_coretpk_installer_change_file_owner failed, fullpath=[%s]", fullpath);
1276                                 }
1277                         }
1278                 }
1279
1280                 // find next dir
1281                 if (entry.d_type == DT_DIR) {
1282                         ret = _coretpk_installer_apply_directory_policy(fullpath, mode, appowner);
1283                         if(ret != 0 ){
1284                                 _LOGE("_coretpk_installer_apply_directory_policy failed, fullpath=[%s]", fullpath);
1285                         }
1286                 }
1287                 memset(fullpath, '\0', BUF_SIZE);
1288         }
1289
1290         closedir(dir);
1291
1292         return ret;
1293 }
1294
1295 int _coretpk_installer_make_directory_for_ext(char *pkgid)
1296 {
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;
1301         int res = 0;
1302
1303         if (access(OPT_STORAGE_SDCARD, F_OK) != 0) {
1304                 _LOGL("There is no OPT_STORAGE_SDCARD", errno);
1305                 return -1;
1306         }
1307
1308         /*pkg root path*/
1309         if (access(OPT_STORAGE_SDCARD_APP_ROOT, F_OK) != 0) {
1310                 /*permission(755)*/
1311                 res = mkdir(OPT_STORAGE_SDCARD_APP_ROOT, DIRECTORY_PERMISSION_755);
1312                 if (res < 0) {
1313                         _LOGL("mkdir()", errno);
1314                         return -1;
1315                         }
1316         }
1317
1318         /*app root path*/
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)
1322         {
1323                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1324                 return -1;
1325         }
1326
1327         res = _coretpk_installer_set_smack_label_access(ext_pkg_base_path, "_");
1328         if (res != 0)
1329         {
1330                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1331                 return -1;
1332         }
1333
1334         //data
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)
1340         {
1341                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1342                 return -1;
1343         }
1344         res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1345         if (res != 0)
1346         {
1347                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1348                 return -1;
1349         }
1350
1351         //cache
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)
1357         {
1358                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1359                 return -1;
1360         }
1361         res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1362         if (res != 0)
1363         {
1364                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1365                 return -1;
1366         }
1367
1368         //shared
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)
1374         {
1375                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1376                 return -1;
1377         }
1378         res = _coretpk_installer_set_smack_label_access(temp_path, "_");
1379         if (res != 0)
1380         {
1381                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1382                 return -1;
1383         }
1384
1385         snprintf(pkg_shared_data_path, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid , "shared/data");
1386
1387         res = access(pkg_shared_data_path, F_OK);
1388         if (res == 0)
1389         {
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);
1392                 if (res != 0)
1393                 {
1394                         _LOGE("_coretpk_installer_get_smack_label_access() is failed.");
1395                         return -1;
1396                 }
1397
1398                 //shared/data
1399                 strncat(temp_path, "/data", strlen("/data"));
1400                 res = mkdir(temp_path, 0705);
1401                 if (res == -1 && errno != EEXIST)
1402                 {
1403                         _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1404                         return -1;
1405                 }
1406
1407                 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1408                 if (res != 0)
1409                 {
1410                         _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1411                         return -1;
1412                 }
1413
1414                 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1415                 if (res != 0)
1416                 {
1417                         _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1418 //                      return -1;
1419                 }
1420
1421                 //shared/cache
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)
1428                 {
1429                         _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1430                         return -1;
1431                 }
1432                 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1433                 if (res != 0)
1434                 {
1435                         _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1436                         return -1;
1437                 }
1438                 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1439                 if (res != 0)
1440                 {
1441                         _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1442 //                      return -1;
1443                 }
1444
1445         }
1446         else if (res == -1 && errno == ENOENT)
1447         {
1448                 _LOGD("Directory dose not exist. path: %s, errno: %d (%s)",
1449                                 pkg_shared_data_path, errno, strerror(errno));
1450                 return 0;
1451         }
1452         else
1453         {
1454                 _LOGE("access() failed. path: %s, errno: %d (%s)",
1455                                 pkg_shared_data_path, errno, strerror(errno));
1456                 return -1;
1457         }
1458
1459         return 0;
1460 }
1461
1462
1463 int _coretpk_installer_make_directory(char *pkgid)
1464 {
1465         int ret = 0;
1466         char appdir[BUF_SIZE] = {'\0'};
1467         char rootfile[BUF_SIZE] = {'\0'};
1468         char *groupid = NULL;
1469
1470         // check param
1471         if (pkgid == NULL) {
1472                 _LOGE("pkgid is NULL.");
1473                 return -1;
1474         }
1475
1476         // root
1477         memset(appdir, '\0', BUF_SIZE);
1478         snprintf(appdir, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
1479         if (access(appdir, F_OK) != 0) {
1480                 // permission(755)
1481                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1482                 if (ret < 0) {
1483                         _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1484                 }
1485         }
1486         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1487         if (ret != 0) {
1488                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1489                 return -1;
1490         }
1491
1492         // bin
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);
1500                         return -1;
1501                 }
1502         }
1503         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1504         if (ret != 0) {
1505                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1506                 return -1;
1507         }
1508
1509         // data
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) {
1513                 // permission(755)
1514                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1515                 if (ret < 0) {
1516                         _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1517                 }
1518         }
1519         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1520         if (ret != 0) {
1521                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1522                 return -1;
1523         }
1524
1525         //lib
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);
1531         }
1532         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1533         if (ret != 0) {
1534                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1535                 return -1;
1536         }
1537
1538         // res
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);
1544         }
1545         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1546         if (ret != 0) {
1547                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1548                 return -1;
1549         }
1550
1551         // cache
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) {
1555                 // permission(755)
1556                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1557                 if (ret < 0) {
1558                         _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1559                 }
1560         }
1561         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1562         if (ret != 0) {
1563                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1564                 return -1;
1565         }
1566
1567         // shared
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);
1572                 if (ret < 0) {
1573                         _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1574                 }
1575         }
1576         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1577         if (ret != 0) {
1578                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1579                 return -1;
1580         }
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);
1585                 if (ret < 0) {
1586                         _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1587                 }
1588         }
1589         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1590         if (ret != 0) {
1591                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1592                 return -1;
1593         }
1594
1595         // shared/data
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);
1599         if (ret != 0) {
1600                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1601                 return -1;
1602         }
1603
1604         // shared/cache
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);
1609                 if (ret < 0) {
1610                         _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1611                 }
1612         }
1613         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1614         if (ret != 0) {
1615                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1616                 return -1;
1617         }
1618
1619         // shared/res
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);
1623         if (ret != 0) {
1624                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1625                 return -1;
1626         }
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);
1630         if (ret != 0) {
1631                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1632                 return -1;
1633         }
1634
1635         // shared/trusted
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);
1640                 if (ret == 0) {
1641                         ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1642                         if (ret < 0) {
1643                                 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1644                         }
1645                         free(groupid);
1646                 }
1647         }
1648         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1649         if (ret != 0) {
1650                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1651                 return -1;
1652         }
1653
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);
1657         if (ret != 0) {
1658                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1659                 return -1;
1660         }
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);
1664         if (ret != 0) {
1665                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1666                 return -1;
1667         }
1668
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);
1673         if (ret != 0) {
1674                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1675                 return -1;
1676         }
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);
1680         if (ret != 0) {
1681                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1682                 return -1;
1683         }
1684
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);
1689         if (ret != 0) {
1690                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1691                 return -1;
1692         }
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);
1696         if (ret != 0) {
1697                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1698                 return -1;
1699         }
1700
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);
1705         if (ret != 0) {
1706                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1707                 return -1;
1708         }
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);
1712         if (ret != 0) {
1713                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1714                 return -1;
1715         }
1716
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);
1720                 if (ret != 0) {
1721                         _LOGE("_coretpk_installer_make_directory_for_ext() failed, pkgid=[%s]", pkgid);
1722                         return -1;
1723                 }
1724         }
1725
1726         return ret;
1727 }
1728
1729 int _coretpk_installer_change_mode(char *path, int mode)
1730 {
1731         int ret = 0;
1732
1733         ret = chmod(path, mode);
1734         if (ret != 0) {
1735                 _LOGL("chmod()", errno);
1736                 return -1;
1737         }
1738
1739         return ret;
1740 }
1741
1742 int _coretpk_installer_change_file_owner(char *path, int ownerid, int groupid)
1743 {
1744         int ret = 0;
1745
1746         if (access(path, F_OK) == 0) {
1747                 ret = chown(path, ownerid, groupid);
1748                 if (ret != 0) {
1749                         _LOGL("chown()", errno);
1750                         return -1;
1751                 }
1752         }
1753
1754         return ret;
1755 }
1756
1757 int _coretpk_installer_change_directory_owner(char *dirpath, int ownerid, int groupid)
1758 {
1759         int ret = 0;
1760
1761         if (__is_dir(dirpath)) {
1762                 ret = chown(dirpath, ownerid, groupid);
1763                 if (ret != 0) {
1764                         _LOGL("chown()", errno);
1765                         return -1;
1766                 }
1767         }
1768
1769         return ret;
1770 }
1771
1772 void _coretpk_installer_set_privilege_setup_path_for_ext(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1773 {
1774         char path[BUF_SIZE] = {'\0'};
1775
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);
1779         }
1780 }
1781
1782 void _coretpk_installer_set_privilege_setup_path(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1783 {
1784         char path[BUF_SIZE] = {'\0'};
1785
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);
1789         }
1790         memset(path, '\0', BUF_SIZE);
1791
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);
1795         }
1796 }
1797
1798 int _coretpk_installer_get_group_id(char *pkgid, char **result)
1799 {
1800         int ret = 0;
1801         const char *value = NULL;
1802         char author_signature[BUF_SIZE] = {'\0'};
1803         char *e_rootcert = NULL;
1804         char *d_rootcert = NULL;
1805         gsize d_size = 0;
1806         unsigned char hashout[BUF_SIZE] = {'\0'};
1807         unsigned int h_size = 0;
1808         int e_size = 0;
1809         int length = 0;
1810         pkgmgrinfo_certinfo_h handle = NULL;
1811
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);
1815
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);
1820                         return -1;
1821                 } else {
1822                         _LOGE("author_signature=[%s]", author_signature);
1823                 }
1824         }
1825
1826         ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
1827         if (ret < 0) {
1828                 _LOGE("failed to get cert info.");
1829                 goto err;
1830         }
1831
1832         ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle);
1833         if (ret < 0) {
1834                 _LOGE("failed to load cert info.");
1835                 goto err;
1836         }
1837
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.");
1842                 goto err;
1843         }
1844
1845         /*decode cert*/
1846         d_rootcert = (char *)g_base64_decode(value, &d_size);
1847         if (d_rootcert == NULL) {
1848                 _LOGE("failed to execute decode.");
1849                 goto err;
1850         }
1851
1852         /*hash*/
1853         EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
1854         if (h_size <= 0) {
1855                 _LOGE("@Failed to get hash.");
1856                 goto err;
1857         }
1858
1859         /*encode cert*/
1860         e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
1861         if (e_rootcert == NULL) {
1862                 _LOGE("failed to execute encode.");
1863                 goto err;
1864         }
1865         e_size = strlen(e_rootcert);
1866         _LOGD("encoding done, len=[%d]", e_size);
1867
1868         /*replace / to #*/
1869         for (length = e_size; length >= 0; --length) {
1870                 if (e_rootcert[length] == '/') {
1871                         e_rootcert[length] = '#';
1872                 }
1873         }
1874
1875         *result = e_rootcert;
1876
1877 err:
1878         if (d_rootcert) {
1879                 free(d_rootcert);
1880         }
1881
1882         /*destroy cert*/
1883         if (handle) {
1884                 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
1885         }
1886
1887         return ret;
1888 }
1889
1890 int _coretpk_installer_apply_smack_for_ext(char *pkgname)
1891 {
1892         int ret = 0;
1893         char dirpath[BUF_SIZE] = {'\0'};
1894
1895         // approot
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);
1899
1900         // [pkgid]/data
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);
1904
1905         // [pkgid]/cache
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);
1909
1910         // [pkgid]/shared
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);
1914
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);
1918
1919         return ret;
1920 }
1921
1922 int _coretpk_installer_apply_smack(char *pkgname, int flag)
1923 {
1924         int ret = 0;
1925         char dirpath[BUF_SIZE] = {'\0'};
1926         char manifest[BUF_SIZE] = {'\0'};
1927         char *groupid = NULL;
1928         char *shared_data_label = NULL;
1929
1930         _ri_privilege_register_package(pkgname);
1931
1932         // app root
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);
1936
1937         // shared
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);
1941
1942         // shared/res
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);
1946
1947         // shared/data
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);
1951
1952         // shared/cache
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);
1955         if (ret == 0) {
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);
1959                 if (ret != 0) {
1960                         _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1961                 }
1962                 ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
1963                 if (ret != 0) {
1964                         _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1965                 }
1966         }
1967
1968         // shared/trusted
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);
1973                 if (ret == 0) {
1974                         LOGD("groupid = [%s] for shared/trusted.", groupid);
1975                         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
1976                         if (groupid)
1977                                 free(groupid);
1978                 } else {
1979                         LOGE("_coretpk_installer_get_group_id(%s) failed.", pkgname);
1980                         return -1;
1981                 }
1982         }
1983         memset(dirpath, '\0', BUF_SIZE);
1984
1985         // bin
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);
1989
1990         // data
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);
1994
1995         // lib
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);
1999
2000         // res
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);
2004
2005         // cache
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);
2009
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);
2014
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);
2019
2020         // signature1.xml
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);
2024
2025         // [pkgid].xml
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);
2029         }
2030
2031         // external storage
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);
2035                         if (ret != 0) {
2036                                 _LOGE("_coretpk_installer_apply_smack_for_ext(%s) failed.", pkgname);
2037                                 return -1;
2038                         }
2039                 }
2040         }
2041
2042         return ret;
2043 }
2044
2045 static char * __getprivilege(const char* pBuf)
2046 {
2047         const char* pKey = "<privilege>";
2048         const char* p = NULL;
2049         const char* pStart = NULL;
2050         const char* pEnd = NULL;
2051
2052         p = strstr(pBuf, pKey);
2053         if (p == NULL)
2054                 return NULL;
2055
2056         pStart = p + strlen(pKey);
2057         pEnd = strchr(pStart, '<');
2058         if (pEnd == NULL)
2059                 return NULL;
2060
2061         size_t len = pEnd - pStart;
2062         if (len <= 0)
2063                 return NULL;
2064
2065         char *pRes = (char*)malloc(len + 1);
2066         if(pRes == NULL){
2067                 _LOGE("malloc failed!!");
2068                 return NULL;
2069         }
2070         strncpy(pRes, pStart, len);
2071         pRes[len] = 0;
2072
2073         return pRes;
2074 }
2075
2076 int _coretpk_installer_apply_privilege(char *pkgid, char *pkgPath, int apiVisibility)
2077 {
2078         int ret = 0;
2079         FILE *fp = NULL;
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;
2086
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;
2095         }
2096
2097         snprintf(manifest, BUF_SIZE, "%s/%s", pkgPath, CORETPK_XML);
2098         _LOGD("pkgid = [%s], manifest = [%s]", pkgid, manifest);
2099
2100         fp = fopen(manifest, "r");
2101         if (fp == NULL) {
2102                 _LOGE("Fail get : %s\n", manifest);
2103                 return -1;
2104         }
2105
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);
2109                 if (ret != 0) {
2110                         _LOGE("failed to set pkg version for privilege, ret[%d]", ret);
2111                         return -1;
2112                 } else
2113                         _LOGD("api-version fot privilege has done successfully.");
2114                 free(tmp_api_version);
2115         } else {
2116                 _LOGE("failed to get api version from native app manifest");
2117                 if (fp)
2118                         fclose(fp);
2119                 return -1;
2120         }
2121
2122         /*reload privilege*/
2123         _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2124
2125         while (fgets(buf, BUF_SIZE, fp) != NULL) {
2126                 __str_trim(buf);
2127
2128                 if (strstr(buf, "<privilege>")) {
2129                         find_str = __getprivilege(buf);
2130                         if (find_str !=  NULL) {
2131                                 _LOGD("privilege = [%s]", find_str);
2132                                 perm[0] = find_str;
2133
2134                                 ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2135                                 if(ret < 0) {
2136                                         _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", pkgid, apptype);
2137                                 } else {
2138                                         _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", pkgid, apptype);
2139                                 }
2140
2141                                 free(find_str);
2142                                 find_str = NULL;
2143                         } else {
2144                                 _LOGD("find_str is null.");
2145                         }
2146                 }
2147
2148                 memset(buf, 0x00, BUF_SIZE);
2149         }
2150
2151         if (fp != NULL)
2152                 fclose(fp);
2153
2154         return 0;
2155 }
2156
2157 int _coretpk_installer_package_install(char *pkgfile, char *pkgid, char *clientid)
2158 {
2159         int ret = 0;
2160         char buff[BUF_SIZE] = {'\0'};
2161         char manifest[BUF_SIZE] = {'\0'};
2162         char cwd[BUF_SIZE] = {'\0'};
2163         char *temp = NULL;
2164         char rwmanifest[BUF_SIZE] = {'\0'};
2165         int visibility = 0;
2166         pkginfo *info = NULL;
2167
2168         /* for external installation */
2169         app2ext_handle *handle = NULL;
2170         GList *dir_list = NULL;
2171         int install_status = APP2EXT_STATUS_SUCCESS;
2172
2173         /*check param*/
2174         if (pkgfile == NULL || pkgid == NULL) {
2175                 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2176                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2177         }
2178
2179         /*send event for start*/
2180         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "install");
2181         _LOGD("[#]start : _coretpk_installer_package_install[%s]", pkgid);
2182
2183         /*get pkginfo*/
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;
2188                 goto err;
2189         }
2190
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);
2197                 }
2198                 ret = RPM_INSTALLER_ERR_INTERNAL;
2199                 goto err;
2200         }
2201
2202         /*send event for install_percent*/
2203         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2204
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);
2209         }
2210
2211         /* pre_install */
2212         ret = __pre_install_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2213         if (ret < 0) {
2214                 _LOGE("__pre_install_for_mmc is failed.");
2215                 goto err;
2216         }
2217
2218         const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, "-d", buff, NULL };
2219         ret = _ri_xsystem(unzip_argv);
2220         if (ret != 0) {
2221                 _LOGE("failed to unzip for path=[%s], ret=[%d]", buff, ret);
2222                 goto err;
2223         }
2224         _LOGD("unzip is done successfully, path=[%s]", buff);
2225
2226         /*getcwd*/
2227         temp = getcwd(cwd, BUF_SIZE);
2228         if ((temp == NULL) || (cwd[0] == '\0')) {
2229                 _LOGL("getcwd()", errno);
2230                 ret = RPM_INSTALLER_ERR_INTERNAL;
2231                 goto err;
2232         }
2233         _LOGD("current working directory, path=[%s]", cwd);
2234
2235         /*change dir*/
2236         ret = chdir(buff);
2237         if (ret != 0) {
2238                 _LOGL("chdir()", errno);
2239                 ret = RPM_INSTALLER_ERR_INTERNAL;
2240                 goto err;
2241         }
2242
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);
2246                 if (ret < 0) {
2247                         _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
2248                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2249                         goto err;
2250                 }
2251                 _LOGD("signature and certificate are verified successfully.");
2252         }
2253
2254         /*chdir*/
2255         ret = chdir(cwd);
2256         if (ret != 0) {
2257                 _LOGL("chdir()", errno);
2258                 ret = RPM_INSTALLER_ERR_INTERNAL;
2259                 goto err;
2260         }
2261
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);
2265         if (ret != 0) {
2266                 _LOGE("failed to convert the manifest.");
2267                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2268                 goto err;
2269         }
2270         _LOGD("manifest is converted successfully.");
2271
2272         if (strstr(pkgfile, ".wgt") != NULL) {
2273                 _LOGD("wgt file=[%s]", pkgfile);
2274
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);
2279                 }
2280         }
2281
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);
2287         if(ret < 0) {
2288                 _LOGE("invalid manifest file");
2289                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2290                 goto err;
2291         }
2292
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);
2295         if (ret < 0) {
2296                 _LOGE("failed to parse the manifest.");
2297                 ret = RPM_INSTALLER_ERR_INTERNAL;
2298                 goto err;
2299         }
2300         _LOGD("manifest parsing done successfully.");
2301
2302         /*search_ug_app*/
2303         _coretpk_installer_search_ui_gadget(pkgid);
2304
2305         /*send event for install_percent*/
2306         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2307
2308         /*register cert info*/
2309         _ri_register_cert(pkgid);
2310
2311         /*make directory*/
2312         ret = _coretpk_installer_make_directory(pkgid);
2313         if (ret != 0) {
2314                 _LOGE("failed to make the directory.");
2315                 goto err;
2316         }
2317
2318         /*apply smack to app dir*/
2319         ret = _coretpk_installer_apply_smack(pkgid, 1);
2320         if (ret != 0) {
2321                 _LOGE("failed to apply the smack.");
2322                 goto err;
2323         }
2324
2325         if (strlen(info->api_version) != 0) {
2326                 ret = _ri_privilege_set_package_version(pkgid, info->api_version);
2327                 if (ret != 0)
2328                         _LOGE("failed to set pkg version for privilege, ret[%d]", ret);
2329                 else
2330                         _LOGD("api-version fot privilege has done successfully.");
2331         } else {
2332                 _LOGD("failed to retrieve api_version of package");
2333         }
2334
2335         /*apply smack by privilege*/
2336         ret = _ri_apply_privilege(pkgid, visibility);
2337         if (ret != 0) {
2338                 _LOGE("failed to apply permission, ret=[%d]", ret);
2339         }
2340         _LOGD("permission applying done successfully.");
2341
2342         // Check privilege and visibility
2343         if (privilege_list) {
2344                 ret = _coretpk_installer_verify_privilege_list(pkgid, privilege_list, visibility, info->api_version);
2345                 if (ret != 0) {
2346                         goto err;
2347                 } else {
2348                         _LOGD("_coretpk_installer_verify_privilege_list done.");
2349                 }
2350         }
2351
2352 #if 0
2353         /*reload smack*/
2354         ret = _ri_smack_reload(pkgid, REQUEST_TYPE_INSTALL);
2355         if (ret != 0) {
2356                 _LOGD("failed to reload the smack.");
2357         }
2358 #endif
2359
2360         /*send event for install_percent*/
2361         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2362
2363         ret = RPM_INSTALLER_SUCCESS;
2364
2365 err:
2366         /* post_install */
2367         if (ret != 0) {
2368                 install_status = APP2EXT_STATUS_FAILED;
2369         }
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.");
2373                 ret = -1;
2374         }
2375
2376         if (info != NULL) {
2377                 free(info);
2378                 info = NULL;
2379         }
2380
2381         if (ret == 0) {
2382                 _LOGD("_coretpk_installer_package_install is done.");
2383                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2384         } else {
2385                 /*remove db info*/
2386                 if (_coretpk_installer_remove_db_info(pkgid) < 0) {
2387                         _LOGE("_coretpk_installer_remove_db_info is failed.");
2388                 }
2389
2390                 /*remove xml(/opt/share/packages/pkgid.xml)*/
2391                 if (access(manifest, F_OK) == 0) {
2392                         (void)remove(manifest);
2393                 }
2394
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);
2399                 }
2400
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);
2407                         }
2408                 }
2409
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);
2414                 }
2415                 sleep(2);
2416
2417                 _LOGE("_coretpk_installer_package_install is failed.");
2418                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2419         }
2420
2421         return ret;
2422 }
2423
2424 int _coretpk_installer_package_uninstall(const char *pkgid)
2425 {
2426         int ret = 0;
2427         int update_system = 0;
2428
2429         update_system = __check_updated_system_package(pkgid);
2430
2431         if (update_system == 1) {
2432                 _LOGD("start remove_update, pkgid=[%s]", pkgid);
2433                 ret = __pkg_remove_update(pkgid);
2434         } else {
2435                 _LOGD("start uninstall, pkgid=[%s]", pkgid);
2436                 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2437         }
2438
2439         if (ret < 0) {
2440                 _LOGE("uninstallation is failed, pkgid=[%s], update_system=[%d]", pkgid, update_system);
2441         } else {
2442                 _LOGD("uninstallation is done successfully, pkgid=[%s]", pkgid);
2443         }
2444
2445         return ret;
2446 }
2447
2448 int _coretpk_installer_package_upgrade(char *pkgfile, char *pkgid, char *clientid)
2449 {
2450         int ret = 0;
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;
2456         char *temp = NULL;
2457         int visibility = 0;
2458         pkginfo *info = NULL;
2459
2460         /* for external upgrade */
2461         app2ext_handle *handle = NULL;
2462         GList *dir_list = NULL;
2463
2464         /*check param*/
2465         if (pkgfile == NULL || pkgid == NULL) {
2466                 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2467                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2468         }
2469
2470         /*send event for start*/
2471         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
2472
2473         /*get pkginfo*/
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;
2478                 goto err;
2479         }
2480
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);
2487                 }
2488                 ret = RPM_INSTALLER_ERR_INTERNAL;
2489                 goto err;
2490         }
2491
2492         /*terminate running app*/
2493         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2494         if (ret < 0) {
2495                 _LOGE("failed to get the pkginfo handle.");
2496                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2497                 goto err;
2498         }
2499         pkgmgrinfo_appinfo_get_list(pkghandle, PMINFO_UI_APP, __ri_check_running_app, NULL);
2500         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2501
2502         /*remove dir for clean*/
2503         __ri_remove_updated_dir(pkgid);
2504
2505         /* pre_upgrade */
2506         ret = __pre_upgrade_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2507         if (ret < 0) {
2508                 _LOGE("__pre_upgrade_for_mmc is failed.");
2509                 goto err;
2510         }
2511
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);
2515         if (ret != 0) {
2516                 _LOGE("failed to unzip for [%s, %d].", buff, ret);
2517                 goto err;
2518         }
2519         _LOGD("#unzip[%s] success.", buff);
2520
2521         /*send event for install_percent*/
2522         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2523
2524         /*getcwd*/
2525         temp = getcwd(cwd, BUF_SIZE);
2526         if ((temp == NULL) || (cwd[0] == '\0')) {
2527                 _LOGL("getcwd()", errno);
2528                 ret = RPM_INSTALLER_ERR_INTERNAL;
2529                 goto err;
2530         }
2531         _LOGD("#Current working directory is %s.", cwd);
2532
2533         /*change dir*/
2534         ret = chdir(buff);
2535         if (ret != 0) {
2536                 _LOGL("chdir()", errno);
2537                 ret = RPM_INSTALLER_ERR_INTERNAL;
2538                 goto err;
2539         }
2540
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);
2544                 if (ret < 0) {
2545                         _LOGE("@Failed to verify signature and certificate[%s].", pkgid);
2546                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2547                         goto err;
2548                 }
2549                 _LOGD("#signature and certificate verifying success");
2550         }
2551
2552         /*chdir*/
2553         ret = chdir(cwd);
2554         if (ret != 0) {
2555                 _LOGL("chdir()", errno);
2556                 ret = RPM_INSTALLER_ERR_INTERNAL;
2557                 goto err;
2558         }
2559
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);
2563         if (ret != 0) {
2564                 _LOGE("@Failed to convert the manifest.");
2565                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2566                 goto err;
2567         }
2568         _LOGD("#manifest converting success");
2569
2570         if (strstr(pkgfile, ".wgt") != NULL) {
2571                 _LOGD("wgt file = [%s]", pkgfile);
2572
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);
2577                 }
2578         }
2579
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);
2585         if(ret < 0) {
2586                 _LOGE("@invalid manifest file");
2587                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2588                 goto err;
2589         }
2590
2591         /*send event for install_percent*/
2592         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2593
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);
2596         if (ret < 0) {
2597                 _LOGE("@parsing manifest failed.");
2598                 ret = RPM_INSTALLER_ERR_INTERNAL;
2599                 goto err;
2600         }
2601         _LOGD("#parsing manifest success.");
2602
2603         /*search_ug_app*/
2604         _coretpk_installer_search_ui_gadget(pkgid);
2605
2606         /*unregister cert info*/
2607         _ri_unregister_cert(pkgid);
2608
2609         /*register cert info*/
2610         _ri_register_cert(pkgid);
2611
2612         /*make directory*/
2613         ret = _coretpk_installer_make_directory(pkgid);
2614         if (ret != 0) {
2615                 _LOGE("@Failed to make the directory");
2616                 goto err;
2617         }
2618
2619         // Remove origin rule
2620         _ri_privilege_unregister_package(pkgid);
2621
2622         /*apply smack to app dir*/
2623         ret = _coretpk_installer_apply_smack(pkgid, 1);
2624         if (ret != 0) {
2625                 _LOGE("@Failed to apply the smack.");
2626                 goto err;
2627         }
2628
2629         /*apply smack by privilege*/
2630         ret = _ri_apply_privilege(pkgid, visibility);
2631         if (ret != 0) {
2632                 _LOGE("@Failed to apply permission[%d].", ret);
2633         }
2634         _LOGD("#permission applying success.");
2635
2636         // Check privilege and visibility
2637         if (privilege_list) {
2638                 ret = _coretpk_installer_verify_privilege_list(pkgid, privilege_list, visibility, info->api_version);
2639                 if (ret != 0) {
2640                         goto err;
2641                 } else {
2642                         _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
2643                 }
2644         }
2645
2646 #if 0
2647         /*reload smack*/
2648         ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
2649         if (ret != 0) {
2650                 _LOGE("@Failed to reload the smack.");
2651         }
2652 #endif
2653
2654         /*send event for install_percent*/
2655         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2656         ret = RPM_INSTALLER_SUCCESS;
2657
2658 err:
2659         /* post_upgrade */
2660         if (__post_upgrade_for_mmc(handle, pkgid, dir_list) < 0) {
2661                 _LOGE("__post_upgrade_for_mmc is failed.");
2662                 ret = -1;
2663         }
2664
2665         if (info != NULL) {
2666                 free(info);
2667                 info = NULL;
2668         }
2669
2670         if (ret == 0) {
2671                 _LOGD("[#]end : _coretpk_installer_package_upgrade");
2672                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2673         } else {
2674
2675                 /*TODO:need to add recovery logic*/
2676
2677                 char *errorstr = NULL;
2678                 _ri_error_no_to_string(ret, &errorstr);
2679                 _ri_broadcast_status_notification(pkgid, "coretpk", "error", errorstr);
2680                 sleep(2);
2681
2682                 _LOGE("[@]end : _coretpk_installer_package_upgrade");
2683                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2684         }
2685
2686         return ret;
2687 }
2688
2689 char* _coretpk_installer_get_pkgid_from_directory_path(char *dirpath)
2690 {
2691         char* subpath = strrchr(dirpath, '/');
2692         return subpath + 1;
2693 }
2694
2695 int _coretpk_installer_directory_install(char *dirpath, char *clientid)
2696 {
2697         int ret = 0;
2698         char manifest[BUF_SIZE] = {'\0'};
2699         char cwd[BUF_SIZE] = {'\0'};
2700         char *temp = NULL;
2701         char *pkgid = NULL;
2702         int visibility = 0;
2703
2704         // check param
2705         if (dirpath == NULL) {
2706                 _LOGE("dirpath is NULL.");
2707                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2708         }
2709
2710         _LOGD("directory_install start: dirpath = [%s]", dirpath);
2711
2712         // getcwd
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;
2717                 goto err;
2718         }
2719
2720         _LOGD("Current working directory is [%s].", cwd);
2721
2722         // change dir
2723         ret = chdir(dirpath);
2724         if (ret != 0) {
2725                 _LOGE("chdir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
2726                 ret = RPM_INSTALLER_ERR_INTERNAL;
2727                 goto err;
2728         }
2729
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);
2734
2735                 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
2736                 if (ret < 0) {
2737                         _LOGE("_coretpk_installer_verify_signatures(%s, %s) failed.", dirpath, pkgid);
2738                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2739                         goto err;
2740                 }
2741                 _LOGD("verify_signatures(%s, %s) succeed!", dirpath, pkgid);
2742         }
2743
2744   // chdir
2745         ret = chdir(cwd);
2746         if (ret != 0) {
2747                 _LOGE("chdir(%s) failed. [%d][%s]", cwd, errno, strerror(errno));
2748                 ret = RPM_INSTALLER_ERR_INTERNAL;
2749                 goto err;
2750         }
2751
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);
2757         }
2758
2759         ret = _coretpk_parser_convert_manifest(manifest, pkgid, clientid, false, visibility, NULL);
2760         if (ret != 0) {
2761                 _LOGE("_coretpk_parser_convert_manifest() failed. manifest = [%s], pkgid = [%s]", manifest, pkgid);
2762                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2763                 goto err;
2764         }
2765         _LOGD("convert_manifest(%s, %s) succeed!", manifest, pkgid);
2766
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);
2771         } else {
2772                 snprintf(manifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2773         }
2774         _LOGD("manifest = [%s]", manifest);
2775
2776         // compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)
2777         ret = pkgmgr_parser_check_manifest_validation(manifest);
2778         if(ret < 0) {
2779                 _LOGE("pkgmgr_parser_check_manifest_validation(%s) failed.", manifest);
2780                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2781                 goto err;
2782         }
2783
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);
2786         if (ret < 0) {
2787                 _LOGE("pkgmgr_parser_parse_manifest_for_installation(%s) failed.", manifest);
2788                 ret = RPM_INSTALLER_ERR_INTERNAL;
2789                 goto err;
2790         }
2791         _LOGD("parse_manifest(%s) succeed!", manifest);
2792
2793         // register cert info
2794         _ri_register_cert(pkgid);
2795
2796         // make directory
2797         ret = _coretpk_installer_make_directory(pkgid);
2798         if (ret != 0) {
2799                 _LOGE("_coretpk_installer_make_directory(%s) failed.", pkgid);
2800                 goto err;
2801         }
2802         _LOGD("make_directory(%s) succeed!", pkgid);
2803
2804         // apply smack to app dir
2805         ret = _coretpk_installer_apply_smack(pkgid, 1);
2806         if (ret != 0) {
2807                 _LOGE("_coretpk_installer_apply_smack(%s) failed.", pkgid);
2808         }
2809         _LOGD("apply_smack(%s) succeed!", pkgid);
2810
2811         // apply smack by privilege
2812         ret = _ri_apply_privilege(pkgid, visibility);
2813         if (ret != 0) {
2814                 _LOGE("_ri_apply_privilege(%s, %d) failed. ret = [%d]", pkgid, visibility, ret);
2815         }
2816         _LOGD("apply_privilege(%s, %d) succeed!", pkgid, visibility);
2817
2818         ret = RPM_INSTALLER_SUCCESS;
2819
2820 err:
2821         _LOGD("directory_install end: dirpath = [%s], ret = [%d]", dirpath, ret);
2822
2823         return ret;
2824 }
2825
2826 int _coretpk_installer_prepare_package_install(char *pkgfile, char *clientid)
2827 {
2828         int ret = 0;
2829         pkginfo *info = NULL;
2830         pkginfo *dbinfo = NULL;
2831         char *pkgid = NULL;
2832
2833         _LOGD("start");
2834
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;
2839                 goto err;
2840         }
2841
2842         pkgid = strdup(info->package_name);
2843         if (pkgid == NULL) {
2844                 _LOGE("strdup() failed.");
2845                 ret = RPM_INSTALLER_ERR_INTERNAL;
2846                 goto err;
2847         }
2848
2849         dbinfo = _rpm_installer_get_pkgname_info(info->package_name);
2850
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) {
2856                 /*upgrade */
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) {
2860                 /*downgrade*/
2861                 _LOGD("start to downgrade");
2862                 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2863         } else {
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);
2867         }
2868
2869         if (ret != 0) {
2870                 _LOGE("result=[%d]", ret);
2871         } else {
2872                 _LOGD("success");
2873         }
2874
2875         if (info) {
2876                 free(info);
2877                 info = NULL;
2878         }
2879         if (dbinfo) {
2880                 free(dbinfo);
2881                 dbinfo = NULL;
2882         }
2883
2884         if (pkgid) {
2885                 free(pkgid);
2886                 pkgid = NULL;
2887         }
2888
2889         return ret;
2890
2891 err:
2892         if (info) {
2893                 free(info);
2894                 info = NULL;
2895         }
2896
2897         _ri_broadcast_status_notification("Invalid package", "invalid", "start", "install");
2898         _ri_broadcast_status_notification("Invalid package", "invalid", "end", "fail");
2899
2900         return ret;
2901 }
2902
2903 int _coretpk_installer_prepare_package_uninstall(const char *pkgid)
2904 {
2905         if (pkgid == NULL) {
2906                 _LOGE("pkgid is NULL.");
2907                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2908         }
2909
2910         _LOGD("pkgid=[%s]", pkgid);
2911
2912         int ret = 0;
2913         pkginfo *dbinfo = NULL;
2914
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;
2919         }
2920
2921         ret = _coretpk_installer_package_uninstall(pkgid);
2922         if (ret != 0) {
2923                 _LOGE("_coretpk_installer_package_uninstall() failed, pkgid=[%s], ret=[%d]", pkgid, ret);
2924         } else {
2925                 _LOGD("_coretpk_installer_package_uninstall() is done successfully, pkgid=[%s]", pkgid);
2926         }
2927
2928         if (dbinfo) {
2929                 free(dbinfo);
2930                 dbinfo = NULL;
2931         }
2932
2933         return ret;
2934 }
2935
2936 int _coretpk_installer_prepare_directory_install(char *dirpath, char *clientid)
2937 {
2938         int ret = 0;
2939
2940         ret = _coretpk_installer_directory_install(dirpath, clientid);
2941         _LOGD("path=[%s], result=[%d]", dirpath, ret);
2942
2943         return ret;
2944 }
2945
2946 int _coretpk_installer_package_move(char* pkgid, int move_type)
2947 {
2948         app2ext_handle *hdl = NULL;
2949         int ret = 0;
2950         int movetype = -1;
2951         GList *dir_list = NULL;
2952         pkgmgrinfo_pkginfo_h pkghandle = NULL;
2953
2954         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "move");
2955         _LOGD("[#]start : _coretpk_installer_package_move[%s][%d]", pkgid, move_type);
2956
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;
2961         } else {
2962                 ret = RPM_INSTALLER_ERR_WRONG_PARAM;
2963                 goto err;
2964         }
2965
2966         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2967         if (ret < 0) {
2968                 _LOGE("@Failed to get the pkginfo handle.");
2969                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2970                 goto err;
2971         }
2972
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);
2976
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;
2983                         goto err;
2984                 }
2985
2986                 ret = hdl->interface.move(pkgid, dir_list, movetype);
2987                 __rpm_clear_dir_list(dir_list);
2988                 if (ret != 0) {
2989                         _LOGE("@Failed to move app.");
2990                         ret = RPM_INSTALLER_ERR_INTERNAL;
2991                         goto err;
2992                 } else {
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);
2996                         } else {
2997                                 _LOGD("#updating the installed storage from internal to external");
2998                                 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
2999                         }
3000
3001                         if (ret != PMINFO_R_OK) {
3002                                 _LOGE("@Failed to udpate the installed storage.");
3003                                 ret = RPM_INSTALLER_ERR_INTERNAL;
3004                                 goto err;
3005                         }
3006                 }
3007
3008         } else {
3009                 _LOGE("@Failed to get app2ext handle.");
3010                 ret = RPM_INSTALLER_ERR_INTERNAL;
3011         }
3012
3013 err:
3014         if (hdl != NULL) {
3015                 app2ext_deinit(hdl);
3016         }
3017
3018         if (ret == 0) {
3019                 _LOGD("[#]end : _coretpk_installer_package_move");
3020                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
3021         } else {
3022                 _LOGE("[@]end : _coretpk_installer_package_move");
3023                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
3024         }
3025
3026         return ret;
3027 }
3028
3029 int _coretpk_installer_copy_file( const char *src_path, const char *dst_path)
3030 {
3031         int ret = 0;
3032         FILE *src, *dst;
3033         int rc = 0;
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);
3037
3038     src = fopen(src_path, "r");
3039     if (src == NULL) {
3040                 _LOGE("@Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
3041         return  -1;
3042     }
3043
3044     dst = fopen(dst_path, "w");
3045     if (dst == NULL) {
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);
3050                         if (!path) {
3051                                 _LOGE("Memory allocation failed");
3052                                 goto err;
3053                         }
3054                         char *p = strrchr(path, '/');
3055                         if (p) {
3056                                 p++;
3057                         } else {
3058                                 ret = -1;
3059                                 free(path);
3060                                 goto err;
3061                         }
3062                         int idx = strlen(path) - strlen(p);
3063                         path[idx] = '\0';
3064
3065                         /*make the parent dir*/
3066                         const char *mkdir_argv[] = { "/bin/mkdir", "-p", path, NULL };
3067                         ret = _ri_xsystem(mkdir_argv);
3068                         if (ret != 0) {
3069                                 _LOGE("Failed to make parent dir.");
3070                         }
3071
3072                         _LOGD("#[%s] is created.", path);
3073                         free(path);
3074
3075                         /*open the file*/
3076                         dst = fopen(dst_path, "w");
3077                         if (dst == NULL) {
3078                                 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
3079                                 ret = -1;
3080                                 goto err;
3081                         }
3082         } else {
3083                         _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
3084                         ret = -1;
3085                         goto err;
3086         }
3087     }
3088
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);
3092     }
3093
3094  err:
3095          if (src) {
3096                  fclose(src);
3097          }
3098          if (dst) {
3099                  fclose(dst);
3100          }
3101
3102     return  ret;
3103 }
3104
3105 int _coretpk_installer_handle_rds_data(char *pkgid, GList *delete, GList *add, GList *modify, int *updatexml)
3106 {
3107         int ret = 0;
3108         GList *list = NULL;
3109         char handledata[BUF_SIZE] = {'\0'};
3110         char srcfile[BUF_SIZE] = {'\0'};
3111         char destfile[BUF_SIZE] = {'\0'};
3112
3113         /*delete*/
3114         if (delete != NULL) {
3115                 list = g_list_first(delete);
3116                 while (list) {
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);
3120
3121                                 const char *delete_argv[] = { "/bin/rm", "-rf", handledata, NULL };
3122                                 ret = _ri_xsystem(delete_argv);
3123                                 if (ret == 0) {
3124                                         _LOGD("#[delete] success : %s", data);
3125                                 } else {
3126                                         _LOGD("#[delete] fail : %s", data);
3127                                 }
3128                                 memset(handledata, '\0', sizeof(handledata));
3129                         }
3130
3131                         list = g_list_next(list);
3132                 }
3133         } else {
3134                 _LOGD("#There is no deleted data.");
3135         }
3136
3137         /*add*/
3138         if (add != NULL) {
3139                 list = g_list_first(add);
3140                 while (list) {
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);
3145
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);
3150                                 } else {
3151                                         ret =_coretpk_installer_copy_file(srcfile, destfile);
3152                                         if (ret == 0) {
3153                                                 _LOGD("#[add] success : %s", data);
3154                                         } else {
3155                                                 _LOGD("#[add] fail : %s", data);
3156                                         }
3157                                 }
3158                                 memset(srcfile, '\0', sizeof(srcfile));
3159                                 memset(destfile, '\0', sizeof(destfile));
3160                         }
3161
3162                         list = g_list_next(list);
3163                 }
3164         } else {
3165                 _LOGD("#There is no added data.");
3166         }
3167
3168         /*modify*/
3169         if (modify != NULL) {
3170                 list = g_list_first(modify);
3171                 while (list) {
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) {
3176                                         *updatexml = 1;
3177                                 }
3178
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);
3181
3182                                 ret =_coretpk_installer_copy_file(srcfile, destfile);
3183                                 if (ret == 0) {
3184                                         _LOGD("#[modify] success : %s", data);
3185                                 } else {
3186                                         _LOGD("#[modify] fail : %s", data);
3187                                 }
3188
3189                                 memset(srcfile, '\0', sizeof(srcfile));
3190                                 memset(destfile, '\0', sizeof(destfile));
3191                         }
3192                         list = g_list_next(list);
3193                 }
3194         } else {
3195                 _LOGD("#There is no modified data.");
3196         }
3197
3198         return ret;
3199 }
3200
3201 int _coretpk_installer_read_rds_file(char *pkgid, char *rdsfile, int *updatexml)
3202 {
3203         int ret = 0;
3204         int state = RDS_STATE_NONE;
3205
3206         char buffer[BUF_SIZE] = {'\0'};
3207         FILE *fi = NULL;
3208
3209         GList *delete_list = NULL;
3210         GList *add_list = NULL;
3211         GList *modify_list = NULL;
3212
3213         if (access(rdsfile, F_OK) != 0) {
3214                 _LOGL("access()", errno);
3215                 return -1;
3216         }
3217
3218         fi = fopen(rdsfile, "r");
3219         if (fi == NULL) {
3220                 _LOGL("fopen()", errno);
3221                 return -1;
3222         }
3223
3224         while (fgets(buffer, BUF_SIZE, fi) != NULL) {
3225                 buffer[strlen(buffer) - 1] = '\0';
3226
3227                 /*check rds state*/
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;
3235                         } else {
3236                                 state = RDS_STATE_NONE;
3237                         }
3238                 }
3239
3240                 if (state == RDS_STATE_NONE) {
3241                         _LOGE("Unknown RDS State, INSTALLER_RDS_STATE_NONE");
3242                         continue;
3243                 }
3244
3245                 /*make rds data list*/
3246                 switch (state) {
3247                         case RDS_STATE_DELETE:
3248                                 _LOGD("RDS_STATE_DELETE data : %s", buffer);
3249                                 delete_list = g_list_append(delete_list, g_strdup(buffer));
3250                                 break;
3251
3252                         case RDS_STATE_ADD:
3253                                 _LOGD("RDS_STATE_ADD data : %s", buffer);
3254                                 add_list = g_list_append(add_list, g_strdup(buffer));
3255                                 break;
3256
3257                         case RDS_STATE_MODIFY:
3258                                 _LOGD("RDS_STATE_MODIFY data : %s", buffer);
3259                                 modify_list = g_list_append(modify_list, g_strdup(buffer));
3260                                 break;
3261                 }
3262         }
3263
3264         ret = _coretpk_installer_handle_rds_data(pkgid, delete_list, add_list, modify_list, updatexml);
3265         if (ret != 0) {
3266                 _LOGE("@Failed to handle rds data.");
3267         }
3268
3269         if (delete_list != NULL) {
3270                 g_list_free(delete_list);
3271         }
3272         if (add_list != NULL) {
3273                 g_list_free(add_list);
3274         }
3275         if (modify_list != NULL) {
3276                 g_list_free(modify_list);
3277         }
3278
3279         fclose(fi);
3280         return ret;
3281 }
3282
3283 int _coretpk_installer_package_reinstall(char *pkgid, char *clientid)
3284 {
3285         int ret = 0;
3286         char manifest[BUF_SIZE] = {'\0'};
3287         char rdsfile[BUF_SIZE] = {'\0'};
3288         char dirpath[BUF_SIZE] = {'\0'};
3289         char cwd[BUF_SIZE] = {'\0'};
3290         char *temp = NULL;
3291         int updatexml = 0;
3292         int visibility = 0;
3293         pkginfo *info = NULL;
3294
3295         /*check param*/
3296         if (pkgid == NULL) {
3297                 _LOGE("@The input param[pkgid] is NULL.");
3298                 return RPM_INSTALLER_ERR_WRONG_PARAM;
3299         }
3300
3301         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "start", "update");
3302         _LOGD("[#]start : _coretpk_installer_package_reinstall[%s]", pkgid);
3303
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);
3306         if (ret != 0) {
3307                 _LOGE("@Failed to read the rds file.");
3308                 ret = RPM_INSTALLER_ERR_INTERNAL;
3309                 goto err;
3310         }
3311         _LOGD("#RDS file reading success");
3312
3313         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "30");
3314
3315         /*getcwd*/
3316         temp = getcwd(cwd, BUF_SIZE);
3317         if ((temp == NULL) || (cwd[0] == '\0')) {
3318                 _LOGL("getcwd()", errno);
3319                 ret = RPM_INSTALLER_ERR_INTERNAL;
3320                 goto err;
3321         }
3322         _LOGD("#Current working directory is %s.", cwd);
3323
3324         /*change dir*/
3325         snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3326         ret = chdir(dirpath);
3327         if (ret != 0) {
3328                 _LOGL("chdir()", errno);
3329                 ret = RPM_INSTALLER_ERR_INTERNAL;
3330                 goto err;
3331         }
3332
3333         /*check for signature and certificate*/
3334         if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
3335
3336                 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
3337                 _LOGD("pkgid[%s]", pkgid);
3338
3339                 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
3340                 if (ret < 0) {
3341                         _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
3342                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3343                         goto err;
3344                 }
3345                 _LOGD("signature and certificate verifying success");
3346         }
3347
3348         /*chdir*/
3349         ret = chdir(cwd);
3350         if (ret != 0) {
3351                 _LOGL("chdir()", errno);
3352                 ret = RPM_INSTALLER_ERR_INTERNAL;
3353                 goto err;
3354         }
3355
3356         if (updatexml) {
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);
3362                 }
3363                 ret = _coretpk_parser_convert_manifest(manifest, pkgid, clientid, false, visibility, NULL);
3364                 if (ret != 0) {
3365                         _LOGE("@Failed to convert the manifest.");
3366                         ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3367                         goto err;
3368                 }
3369                 _LOGD("#manifest converting success");
3370
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);
3376                 if(ret < 0) {
3377                         _LOGE("@invalid manifest file");
3378                         ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3379                         goto err;
3380                 }
3381
3382                 /*get pkginfo*/
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;
3387                         goto err;
3388                 }
3389
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);
3396                         }
3397                         ret = RPM_INSTALLER_ERR_INTERNAL;
3398                         goto err;
3399                 }
3400
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);
3403                 if (ret < 0) {
3404                         _LOGE("@Failed to parse the manifest.");
3405                         ret = RPM_INSTALLER_ERR_INTERNAL;
3406                         goto err;
3407                 }
3408                 _LOGD("#manifest parsing success");
3409         }
3410
3411         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "60");
3412
3413         /*register cert info*/
3414         _ri_register_cert(pkgid);
3415
3416         /*make directory*/
3417         ret = _coretpk_installer_make_directory(pkgid);
3418         if (ret != 0) {
3419                 _LOGE("@Failed to make directory");
3420                 goto err;
3421         }
3422
3423         _ri_privilege_unregister_package(pkgid);
3424
3425         /*apply smack to app dir*/
3426         ret = _coretpk_installer_apply_smack(pkgid, 1);
3427         if (ret != 0) {
3428                 _LOGE("@Failed to apply smack.");
3429                 goto err;
3430         }
3431
3432         /*apply smack by privilege*/
3433         ret = _ri_apply_privilege(pkgid, visibility);
3434         if (ret != 0) {
3435                 _LOGE("@Failed to apply permission[%d].", ret);
3436         }
3437         _LOGD("#permission applying success.");
3438
3439         // Check privilege and visibility
3440         if (privilege_list) {
3441                 ret = _coretpk_installer_verify_privilege_list(pkgid, privilege_list, visibility, info->api_version);
3442                 if (ret != 0) {
3443                         goto err;
3444                 } else {
3445                         _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
3446                 }
3447         }
3448
3449 #if 0
3450         /*reload smack*/
3451         ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
3452         if (ret != 0) {
3453                 _LOGE("@Failed to reload the smack.");
3454         }
3455 #endif
3456
3457         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "100");
3458         ret = RPM_INSTALLER_SUCCESS;
3459
3460 err:
3461
3462         if (info != NULL) {
3463                 free(info);
3464                 info = NULL;
3465         }
3466
3467         if (ret == 0) {
3468                 _LOGD("[#]end : _coretpk_installer_package_reinstall");
3469                 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "ok");
3470         } else {
3471                 /*remove db info*/
3472                 ret = _coretpk_installer_remove_db_info(pkgid);
3473                 if (ret < 0) {
3474                         _LOGE("_coretpk_installer_remove_db_info is failed.");
3475                 }
3476
3477                 /*remove xml(/opt/share/packages/pkgid.xml)*/
3478                 if (access(manifest, F_OK) == 0) {
3479                         (void)remove(manifest);
3480                 }
3481
3482                 /*remove app dir(/opt/usr/apps/pkgid)*/
3483                 if (__is_dir(dirpath)) {
3484                         _rpm_delete_dir(dirpath);
3485                 }
3486
3487                 _LOGE("[@]end : _coretpk_installer_package_reinstall");
3488                 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "fail");
3489         }
3490
3491         return ret;
3492 }
3493
3494 int _coretpk_installer_csc_install(char *path_str, char *remove_str)
3495 {
3496         int ret = 0;
3497         pkginfo *info = NULL;
3498         char buff[BUF_SIZE] = {'\0'};
3499         char manifest[BUF_SIZE] = {'\0'};
3500         char cwd[BUF_SIZE] = {'\0'};
3501         char *temp = NULL;
3502         char *csc_tags[3] = {NULL, };
3503         int visibility = 0;
3504
3505         /*check param*/
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;
3509         }
3510
3511         _LOGD("[##]csc-core : start csc_install[path=%s]", path_str);
3512
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;
3517                 goto err;
3518         }
3519
3520         _LOGD("[##]csc-core : get pkgid [%s]", info->package_name);
3521
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);
3526         }
3527
3528         _LOGD("[##]csc-core : real path [%s]", buff);
3529
3530         const char *unzip_argv[] = { "/usr/bin/unzip", "-o", path_str, "-d", buff, NULL };
3531         ret = _ri_xsystem(unzip_argv);
3532         if (ret != 0) {
3533                 _LOGE("@Failed to unzip for [%s, %d].", buff, ret);
3534                 goto err;
3535         }
3536
3537         _LOGD("[##]csc-core : unzip success[%s]", buff);
3538
3539         /*getcwd*/
3540         temp = getcwd(cwd, BUF_SIZE);
3541         if ((temp == NULL) || (cwd[0] == '\0')) {
3542                 _LOGL("getcwd()", errno);
3543                 ret = RPM_INSTALLER_ERR_INTERNAL;
3544                 goto err;
3545         }
3546
3547         /*change dir*/
3548         ret = chdir(buff);
3549         if (ret != 0) {
3550                 _LOGL("chdir()", errno);
3551                 ret = RPM_INSTALLER_ERR_INTERNAL;
3552                 goto err;
3553         }
3554
3555         _LOGD("[##]csc-core : check signature");
3556
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);
3560                 if (ret < 0) {
3561                         _LOGE("@Failed to verify signature and certificate[%s].", info->package_name);
3562                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3563                         goto err;
3564                 }
3565                 _LOGD("[##]csc-core : signature verify success[%s]", buff);
3566         }
3567
3568    /*chdir*/
3569         ret = chdir(cwd);
3570         if (ret != 0) {
3571                 _LOGL("chdir()", errno);
3572                 ret = RPM_INSTALLER_ERR_INTERNAL;
3573                 goto err;
3574         }
3575
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);
3579         if (ret != 0) {
3580                 _LOGE("@Failed to convert the manifest.");
3581                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3582                 goto err;
3583         }
3584
3585         _LOGD("[##]csc-core : manifest converting success");
3586
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);
3592         if(ret < 0) {
3593                 _LOGE("@invalid manifest file");
3594                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3595                 goto err;
3596         }
3597
3598         _LOGD("[##]csc-core : manifest validation success");
3599
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";
3603         else
3604                 csc_tags[0] = "removable=false";
3605
3606         csc_tags[1] = "preload=true";
3607         csc_tags[2] = NULL;
3608
3609         ret = pkgmgr_parser_parse_manifest_for_installation(manifest, csc_tags);
3610         if (ret < 0) {
3611                 _LOGE("@Failed to parse the manifest.");
3612                 ret = RPM_INSTALLER_ERR_INTERNAL;
3613                 goto err;
3614         }
3615
3616         _LOGD("[##]csc-core : register manifest success");
3617
3618         /*register cert info*/
3619         _ri_register_cert(info->package_name);
3620
3621         /*make directory*/
3622         ret = _coretpk_installer_make_directory(info->package_name);
3623         if (ret != 0) {
3624                 _LOGE("@Failed to make the directory");
3625                 goto err;
3626         }
3627
3628         _LOGD("[##]csc-core : make directory success");
3629
3630         /*apply smack to app dir*/
3631         ret = _coretpk_installer_apply_smack(info->package_name, 1);
3632         if (ret != 0) {
3633                 _LOGE("@Failed to apply the smack.");
3634                 goto err;
3635         }
3636
3637         _LOGD("[##]csc-core : apply_smack success");
3638
3639         /*apply smack by privilege*/
3640         ret = _ri_apply_privilege(info->package_name, visibility);
3641         if (ret != 0) {
3642                 _LOGE("@Failed to apply permission[%d].", ret);
3643         }
3644
3645         _LOGD("[##]csc-core : apply_privilege success");
3646
3647 #if 0
3648         /*reload smack*/
3649         ret = _ri_smack_reload(info->package_name, REQUEST_TYPE_INSTALL);
3650         if (ret != 0) {
3651                 _LOGD("@Failed to reload the smack.");
3652         }
3653 #endif
3654
3655         _LOGD("[##]csc-core : smack_reload success");
3656
3657         ret = RPM_INSTALLER_SUCCESS;
3658
3659 err:
3660         if (ret == 0) {
3661                 _LOGD("[##]csc-core : finish csc core success");
3662         } else {
3663                 /*remove xml(/opt/share/packages/pkgid.xml)*/
3664                 if (access(manifest, F_OK) == 0) {
3665                         (void)remove(manifest);
3666                 }
3667
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);
3672                 }
3673                 _LOGD("[##]csc-core : finish csc core fail");
3674
3675         }
3676
3677         if (info) {
3678                 free(info);
3679                 info = NULL;
3680         }
3681
3682         return ret;
3683 }