Update source from tizen 2.3
[platform/core/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 static int __get_unzip_size(const char *item, unsigned long long *size)
60 {
61         if (!item || !size) {
62                 _LOGE("Invalid argument.");
63                 return PMINFO_R_ERROR;
64         }
65         int ret = 0;
66         unzFile uzf = unzOpen64(item);
67         if(uzf== NULL)
68         {
69                 _LOGE("Fail to open item : %s", item);
70                 *size = 0;
71                 return PMINFO_R_ERROR;
72         } else {
73                 ret = unzGoToFirstFile(uzf);
74                 if(ret != UNZ_OK) {
75                         _LOGE("error get first zip file ");
76                         unzClose(uzf);
77                         *size = 0;
78                         return PMINFO_R_ERROR;
79                 } else {
80                         do{
81                                 ret = unzOpenCurrentFile(uzf);
82                                 if(ret != UNZ_OK) {
83                                         _LOGE("error unzOpenCurrentFile ");
84                                         unzClose(uzf);
85                                         *size = 0;
86                                         return PMINFO_R_ERROR;
87                                 }
88
89                                 unz_file_info fileInfo = {0};
90                                 char* filename = (char*) calloc(1, BUF_SIZE);
91                                 ret= unzGetCurrentFileInfo(uzf, &fileInfo, filename, (BUF_SIZE-1), NULL, 0, NULL, 0);
92                                 *size = (unsigned long long)fileInfo.uncompressed_size + *size;
93                                 if(ret != UNZ_OK) {
94                                         _LOGE("error get current file info");
95                                         unzCloseCurrentFile(uzf);
96                                         *size = 0;
97                                         break;
98                                 }
99
100                                 if (filename) {
101                                         free(filename);
102                                         filename = NULL;
103                                 }
104                         }while(unzGoToNextFile(uzf) == UNZ_OK);
105                 }
106         }
107         unzClose(uzf);
108
109         return PMINFO_R_OK;
110 }
111
112 static int __is_default_external_storage()
113 {
114         int ret = 0;
115         int storage = 0;
116         int mmc_status = VCONFKEY_SYSMAN_MMC_REMOVED;
117
118         ret = vconf_get_int("db/setting/default_memory/install_applications", &storage);
119         retvm_if(ret != 0, PMINFO_R_ERROR, "vconf_get_int(db/setting/default_memory/install_applications) is failed.");
120
121         if (storage == 1) {
122                 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_status);
123                 retvm_if(ret != 0, PMINFO_R_ERROR, "vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS) is failed.");
124
125                 if((mmc_status == VCONFKEY_SYSMAN_MMC_REMOVED) || (mmc_status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED)) {
126                         _LOGD("mmc_status is MMC_REMOVED or NOT_MOUNTED.");
127                 } else {
128                         _LOGD("mmc_status is MMC_MOUNTED.");
129                         return PMINFO_R_OK;
130                 }
131         }
132
133         return PMINFO_R_ERROR;
134 }
135
136 static void __apply_smack_for_mmc(const char *pkgid)
137 {
138 #if 0
139         char dirpath[BUF_SIZE] = {'\0'};
140
141         snprintf(dirpath, BUF_SIZE, "%s/%s/.mmc", OPT_USR_APPS, pkgid);
142         if (access(dirpath, F_OK) != 0) {
143                 _LOGE("Cannot access to [%s].", dirpath);
144                 return;
145         }
146         memset(dirpath, '\0', BUF_SIZE);
147
148         // [pkgid]/.mmc/bin
149         snprintf(dirpath, BUF_SIZE, "%s/.mmc/bin", pkgid);
150         _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
151         memset(dirpath, '\0', BUF_SIZE);
152
153         // [pkgid]/.mmc/lib
154         snprintf(dirpath, BUF_SIZE, "%s/.mmc/lib", pkgid);
155         _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
156         memset(dirpath, '\0', BUF_SIZE);
157
158         // [pkgid]/.mmc/lost+found
159         snprintf(dirpath, BUF_SIZE, "%s/.mmc/lost+found", pkgid);
160         _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
161         memset(dirpath, '\0', BUF_SIZE);
162
163         // [pkgid]/.mmc/res
164         snprintf(dirpath, BUF_SIZE, "%s/.mmc/res", pkgid);
165         _coretpk_installer_set_privilege_setup_path((char*)pkgid, dirpath, APP_PATH_PRIVATE, (char*)pkgid);
166         memset(dirpath, '\0', BUF_SIZE);
167 #endif
168         return;
169 }
170
171 static int __pre_upgrade_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
172 {
173 #if 0
174         int ret = 0;
175         unsigned long long archive_size_byte = 0;
176         int archive_size_mega = 0;
177
178         ret = __is_default_external_storage();
179         if (ret < 0) {
180                 _LOGD("Upgrade storage is internal.");
181                 return 0;
182         }
183         _LOGD("__pre_upgrade start.");
184
185         ret = __get_unzip_size(pkgfile, &archive_size_byte);
186         if (ret < 0) {
187                 _LOGD("Failed to get uncompressed size.");
188                 return PMINFO_R_ERROR;
189         }
190         archive_size_mega = archive_size_byte / (1024 * 1024) + 1;
191         _LOGD("Uncompressed size is converted from [%lld]bytes to [%d]Mb.", archive_size_byte, archive_size_mega);
192
193         *handle = app2ext_init(APP2EXT_SD_CARD);
194         if (*handle == NULL) {
195                 _LOGE("@app2ext init failed\n");
196                 return PMINFO_R_ERROR;
197         }
198         if ((&((*handle)->interface) != NULL) && ((*handle)->interface.pre_upgrade != NULL) && ((*handle)->interface.post_upgrade != NULL) &&
199                         ((*handle)->interface.disable != NULL)) {
200                 ret = (*handle)->interface.disable(pkgid);
201                 if (ret != APP2EXT_SUCCESS) {
202                         _LOGE("Unmount ret[%d]", ret);
203                 }
204                 *dir_list = __rpm_populate_dir_list();
205                 if (*dir_list == NULL) {
206                         _LOGE("@ \nError in populating the directory list\n");
207                         return PMINFO_R_ERROR;
208                 }
209                 ret = (*handle)->interface.pre_upgrade(pkgid, *dir_list, archive_size_mega);
210                 if (ret == APP2EXT_ERROR_MMC_STATUS) {
211                         _LOGE("@app2xt MMC is not here, go internal\n");
212                 } else if (ret == APP2EXT_SUCCESS){
213                         _LOGD("@pre_upgrade done, go internal\n");
214                 }
215                 else {
216                         _LOGE("@app2xt pre upgrade API failed (%d)\n", ret);
217                         return PMINFO_R_ERROR;
218                 }
219         } else {
220                 _LOGE("handle is not proper.");
221                 return PMINFO_R_ERROR;
222         }
223         _LOGD("__pre_upgrade end.");
224 #endif
225         return PMINFO_R_OK;
226 }
227
228 static int __post_upgrade_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list)
229 {
230 #if 0
231         int ret = __is_default_external_storage();
232         if (ret != 0) {
233                 _LOGD("Upgrade storage is internal.");
234                 return 0;
235         }
236         _LOGD("__post_upgrade start.");
237
238         /* set smack again for .mmc folder */
239         __apply_smack_for_mmc(pkgid);
240         _LOGD("__apply_smack_for_mmc is completed.");
241
242         if ((handle != NULL) && (handle->interface.post_upgrade != NULL)) {
243                 __rpm_clear_dir_list(dir_list);
244                 handle->interface.post_upgrade(pkgid, APP2EXT_STATUS_SUCCESS);
245                 app2ext_deinit(handle);
246         } else {
247                 _LOGE("handle->interface.post_upgrade is NULL.");
248                 return PMINFO_R_ERROR;
249         }
250         _LOGD("__post_upgrade end.");
251 #endif
252         return PMINFO_R_OK;
253 }
254
255 static int __pre_install_for_mmc(const char *pkgid, const char *pkgfile, GList **dir_list, app2ext_handle **handle)
256 {
257 #if 0
258         int ret = 0;
259         unsigned long long archive_size_byte = 0;
260         int archive_size_mega = 0;
261
262         ret = __is_default_external_storage();
263         if (ret != 0) {
264                 _LOGD("Installed storage is internal.");
265                 return 0;
266         }
267         _LOGD("__pre_install start.");
268
269         ret = __get_unzip_size(pkgfile, &archive_size_byte);
270         if (ret < 0) {
271                 _LOGD("Failed to get uncompressed size.");
272                 return PMINFO_R_ERROR;
273         }
274         archive_size_mega = archive_size_byte / (1024 * 1024) + 1;
275         _LOGD("Uncompressed size is converted from [%lld]bytes to [%d]Mb.", archive_size_byte, archive_size_mega);
276
277         *handle = app2ext_init(APP2EXT_SD_CARD);
278         if (*handle == NULL) {
279                 _LOGE("@app2ext init failed\n");
280                 return PMINFO_R_ERROR;
281         }
282         if ((&((*handle)->interface) != NULL) && ((*handle)->interface.pre_install != NULL) && ((*handle)->interface.post_install != NULL)
283                         && ((*handle)->interface.force_clean != NULL)) {
284                 ret = (*handle)->interface.force_clean(pkgid);
285                 if (ret != APP2EXT_SUCCESS) {
286                         _LOGE("Force clean is failed. pkgid[%s] ret[%d]", pkgid, ret);
287                         return PMINFO_R_ERROR;
288                 }
289                 _LOGD("Force clean is OK");
290                 *dir_list = __rpm_populate_dir_list();
291                 if (*dir_list == NULL) {
292                         _LOGE("@ \nError in populating the directory list\n");
293                         return PMINFO_R_ERROR;
294                 }
295                 ret = (*handle)->interface.pre_install(pkgid, *dir_list, archive_size_mega);
296                 if (ret == APP2EXT_ERROR_MMC_STATUS) {
297                         _LOGE("@app2xt MMC is not here, go internal\n");
298                 } else if (ret == APP2EXT_SUCCESS){
299                         _LOGD("@pre_install done, go internal\n");
300                 }
301                 else {
302                         _LOGE("@app2xt pre install API failed (%d)\n", ret);
303                         return PMINFO_R_ERROR;
304                 }
305         } else {
306                 _LOGE("handle is not proper.");
307                 return PMINFO_R_ERROR;
308         }
309         _LOGD("__pre_install end.");
310 #endif
311         return PMINFO_R_OK;
312 }
313
314 static int __post_install_for_mmc(app2ext_handle *handle, const char *pkgid, GList *dir_list, int install_status)
315 {
316 #if 0
317         int ret = __is_default_external_storage();
318         if (ret != 0) {
319                 _LOGD("Installed storage is internal.");
320                 return 0;
321         }
322         _LOGD("__post_install start.");
323
324         /* set smack again for .mmc folder */
325         __apply_smack_for_mmc(pkgid);
326         _LOGD("__apply_smack_for_mmc is completed.");
327
328         if ((handle != NULL) && (handle->interface.post_install != NULL)) {
329                 __rpm_clear_dir_list(dir_list);
330                 handle->interface.post_install(pkgid, install_status);
331                 app2ext_deinit(handle);
332         } else {
333                 _LOGE("handle->interface.post_install is NULL.");
334                 return PMINFO_R_ERROR;
335         }
336         _LOGD("__post_install end.");
337 #endif
338         return PMINFO_R_OK;
339 }
340
341 static void __str_trim(char *input)
342 {
343         char *trim_str = input;
344
345         if (input == NULL)
346                 return;
347
348         while (*input != 0) {
349                 if (!isspace(*input)) {
350                         *trim_str = *input;
351                         trim_str++;
352                 }
353                 input++;
354         }
355
356         *trim_str = 0;
357         return;
358 }
359
360 static char *__find_info_from_xml(const char *manifest, const char *find_info)
361 {
362         const char *val = NULL;
363         const xmlChar *node;
364         xmlTextReaderPtr reader;
365         char *info_val = NULL;
366         int ret = -1;
367
368         if(manifest == NULL) {
369                 _LOGE("input argument is NULL\n");
370                 return NULL;
371         }
372
373         if(find_info == NULL) {
374                 _LOGE("find_info is NULL\n");
375                 return NULL;
376         }
377
378         reader = xmlReaderForFile(manifest, NULL, 0);
379
380         if (reader) {
381                 if (_child_element(reader, -1)) {
382                         node = xmlTextReaderConstName(reader);
383                         if (!node) {
384                                 _LOGE("xmlTextReaderConstName value is NULL\n");
385                                 goto end;
386                         }
387
388                         if (!strcmp(ASCII(node), "manifest")) {
389                                 ret = _ri_get_attribute(reader,(char*)find_info,&val);
390                                 if(ret != 0){
391                                         _LOGE("error in getting the attribute value");
392                                         goto end;
393                                 }
394
395                                 if(val) {
396                                         info_val = strdup(val);
397                                         if(info_val == NULL) {
398                                                 _LOGE("malloc failed!!");
399                                         }
400                                 }
401                         } else {
402                                 _LOGE("unable to create xml reader\n");
403                         }
404                 }
405         } else {
406                 _LOGE("xmlReaderForFile value is NULL\n");
407         }
408
409 end:
410         if (reader) {
411                 xmlFreeTextReader(reader);
412         }
413
414         if(val)
415                 free((void*)val);
416
417         return info_val;
418 }
419
420 static int __coretpk_privilege_func(const char *name, void *user_data)
421 {
422         int ret = 0;
423 #if 0
424         const char *perm[] = {NULL, NULL};
425         const char *ug_pkgid = "ui-gadget::client";
426
427         perm[0] = name;
428
429         _LOGD("privilege = [%s]", name);
430         _ri_privilege_register_package("ui-gadget::client");
431
432         ret = _ri_privilege_enable_permissions(ug_pkgid, PERM_APP_TYPE_EFL, perm, 1);
433         _LOGE("add ug privilege(%s, %s, %d) done.", ug_pkgid, name, ret);
434 #endif
435         return ret;
436 }
437
438 static int __ui_gadget_func(const pkgmgrinfo_appinfo_h handle, void *user_data)
439 {
440 #if 0
441         int ret = 0;
442         bool is_ug = 0;
443         char *pkgid = NULL;
444         char *exec = NULL;
445         char appdir[BUF_SIZE] = {'\0'};
446
447         ret = pkgmgrinfo_appinfo_is_ui_gadget(handle, &is_ug);
448         retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get is_ui_gadget.\n");
449
450         if (is_ug == true) {
451
452                 /*get pkgid*/
453                 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
454                 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get pkgid\n");
455
456                 _LOGD("@[%s] has ui-gadget", pkgid);
457
458                 /*check bin directory*/
459                 snprintf(appdir, BUF_SIZE, "%s/%s/bin", USR_APPS, pkgid);
460                 if (access(appdir, F_OK) != 0) {
461                         /*permission(755)*/
462                         ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
463                         if (ret < 0) {
464                                 _LOGL("mkdir()", errno);
465                                 return -1;
466                         }
467                 }
468
469                 /*get exec*/
470                 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
471                 retvm_if(ret < 0, RPM_INSTALLER_ERR_PKG_NOT_FOUND, "Failed to get exec\n");
472
473                 /*make symlink to exec*/
474                 const char *ln_argv[] = { "/bin/ln", "-sf", "/usr/bin/ug-client", exec, NULL };
475                 ret = _ri_xsystem(ln_argv);
476                 retvm_if(ret < 0, RPM_INSTALLER_ERR_INTERNAL, "Failed to exec ln_argv\n");
477
478                 _LOGD("@[%s] success symlink to [/usr/bin/ug-client]", exec);
479
480                 * (bool *) user_data = true;
481         }
482 #endif
483         return 0;
484 }
485
486 static int __check_updated_system_package(const char *pkgid)
487 {
488         int ret = 0;
489         bool is_update = false;
490         bool is_system = false;
491         pkgmgrinfo_pkginfo_h pkghandle = NULL;
492
493         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
494         retvm_if(ret < 0, -1, "pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
495
496         ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
497         tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_system(%s) failed.", pkgid);
498
499         ret = pkgmgrinfo_pkginfo_is_update(pkghandle, &is_update);
500         tryvm_if(ret < 0, ret = -1, "pkgmgrinfo_pkginfo_is_update(%s) failed.", pkgid);
501
502         if (is_system && is_update) {
503                 _LOGD("pkgid=[%s] is updated system package.", pkgid);
504                 ret = 1;
505         } else {
506                 _LOGD("pkgid=[%s] is not updated system app.", pkgid);
507                 ret = -1;
508         }
509
510 catch:
511         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
512
513         return ret;
514 }
515
516 static int __pkg_remove_update(const char *pkgid)
517 {
518         int ret = 0;
519         char buff[BUF_SIZE] = {'\0'};
520         char rootpath[BUF_SIZE] = {'\0'};
521
522         if (pkgid == NULL) {
523                 _LOGE("pkgid is NULL.");
524                 return -1;
525         }
526
527         // start
528         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
529
530         // remove dir for clean (/opt/usr/apps/[pkgid])
531         snprintf(rootpath, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
532         if (__is_dir(rootpath)) {
533                 _rpm_delete_dir(rootpath);
534         }
535
536         // Remove origin rule
537         _ri_privilege_unregister_package(pkgid);
538
539         // unzip pkg path from factory-reset data
540         memset(rootpath, '\0', BUF_SIZE);
541         snprintf(rootpath, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
542         const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, rootpath, "-d", "/", NULL };
543         ret = _ri_xsystem(pkg_argv);
544         if (ret != 0) {
545                 _LOGE("/usr/bin/unzip(%s) failed.", rootpath);
546         }
547
548         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
549
550         // remove opt xml
551         snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
552         (void)remove(buff);
553
554         // updated usr xml
555         memset(buff, '\0', BUF_SIZE);
556         snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
557
558         _LOGD("manifest = [%s].",buff);
559
560         ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
561         if (ret < 0) {
562                 _LOGE("pkgmgr_parser_parse_manifest_for_upgrade(%s) is failed.", pkgid);
563                 return ret;
564         }
565         _LOGD("pkgmgr_parser_parse_manifest_for_upgrade() is ok.");
566
567         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
568
569         // apply smack for pkg root path
570         memset(buff, '\0', BUF_SIZE);
571         snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
572 //      _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
573
574         // apply smack for defined directory
575         __rpm_apply_smack((char*)pkgid, 0);
576
577         // apply privilege
578         ret = _ri_apply_privilege((char*)pkgid, 0);
579         if (ret != 0) {
580                 _LOGE("_ri_apply_privilege(%s) failed. ret = [%d]", pkgid, ret);
581         } else {
582                 _LOGD("_ri_apply_privilege(%s) success.", pkgid);
583         }
584
585         // reload smack
586         ret = _ri_smack_reload(pkgid, UPGRADE_REQ);
587         if (ret != 0) {
588                 _LOGE("_ri_smack_reload(%s) failed.", pkgid);
589         }
590
591         // finish
592         if (ret != 0) {
593                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
594         } else {
595                 _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
596                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
597         }
598
599         return ret;
600 }
601
602 int _coretpk_installer_remove_db_info(const char *pkgid)
603 {
604         int ret = 0;
605         pkgmgrinfo_pkginfo_h handle = NULL;
606
607         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
608         if (ret < 0) {
609                 return PMINFO_R_OK;
610         }
611         ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
612         tryvm_if(ret < 0, ret = PMINFO_R_ERROR, "pkgmgr_parser_parse_manifest_for_uninstallation is failed, pkgid=[%s]", pkgid);
613
614         _LOGD("Remove db info is OK.");
615
616 catch:
617         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
618
619         return ret;
620 }
621
622 int _coretpk_installer_set_smack_label_access(const char *path, const char *label)
623 {
624         int res = smack_lsetlabel(path, label, SMACK_LABEL_ACCESS);
625         if (res != 0)
626         {
627                 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", label, res, path);
628                 return -1;
629         }
630         return 0;
631 }
632
633 int _coretpk_installer_get_smack_label_access(const char *path, char **label)
634 {
635         int res = smack_lgetlabel(path, label, SMACK_LABEL_ACCESS);
636         if (res != 0)
637         {
638                 _LOGE("Error in getting smack ACCESS label failed. result[%d] (path:[%s]))", res, path);
639                 return -1;
640         }
641         return 0;
642 }
643
644 int _coretpk_installer_set_smack_label_transmute(const char *path, const char *flag)
645 {
646         int res = smack_lsetlabel(path, flag, SMACK_LABEL_TRANSMUTE);
647         if (res != 0)
648         {
649                 _LOGE("smack set label(%s) failed[%d] (path:[%s]))", flag, res, path);
650                 return -1;
651         }
652         return 0;
653 }
654
655 int _coretpk_installer_verify_privilege_list(GList *privilege_list, int visibility)
656 {
657         char *error_privilege_name = NULL;
658         GList *list = NULL;
659         int ret = 0;
660 #if 0
661         ret = privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE, privilege_list, visibility, &error_privilege_name);
662         if (ret != PRVMGR_ERR_NONE) {
663                 _LOGE("privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) failed. ret = [%d][%s]", ret, error_privilege_name);
664                 fprintf(stdout, "\n verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) failed. [%d][%s]\n", ret, error_privilege_name);
665
666                 if (error_privilege_name) {
667                         free(error_privilege_name);
668                 }
669
670                 list = g_list_first(privilege_list);
671                 while (list) {
672                         if (list->data) {
673                                 free(list->data);
674                         }
675                         list = g_list_next(list);
676                 }
677                 g_list_free(privilege_list);
678                 privilege_list = NULL;
679
680                 ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
681                 return ret;
682         } else {
683                 _LOGD("privilege_manager_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
684         }
685
686         list = g_list_first(privilege_list);
687         while (list) {
688                 if (list->data) {
689                         free(list->data);
690                 }
691                 list = g_list_next(list);
692         }
693         g_list_free(privilege_list);
694         privilege_list = NULL;
695 #endif
696         return ret;
697 }
698
699 void _coretpk_installer_search_ui_gadget(const char *pkgid)
700 {
701 #if 0
702         int ret = 0;
703         bool is_ug_pkg = false;
704         pkgmgrinfo_pkginfo_h pkghandle = NULL;
705
706         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
707         retm_if(ret < 0, "@Failed to get the pkginfo handle.");
708
709         /* search ug app */
710         ret = pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ui_gadget_func, &is_ug_pkg);
711         tryvm_if(ret < 0, ret = RPM_INSTALLER_ERR_INTERNAL, "Fail to get applist");
712
713         /*if there is ug app,  apply privilege*/
714         if (is_ug_pkg == true) {
715                 ret = pkgmgrinfo_pkginfo_foreach_privilege(pkghandle, __coretpk_privilege_func, NULL);
716                 tryvm_if(ret < 0, ret = RPM_INSTALLER_ERR_INTERNAL, "Fail to get privilege list");
717         }
718
719 catch :
720         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
721 #endif
722 }
723
724 int _coretpk_backend_interface(const char *reqcommand, const ri_frontend_cmdline_arg *data)
725 {
726         if (reqcommand == NULL || data == NULL) {
727                 _LOGE("reqcommand or data is NULL.");
728                 return -1;
729         }
730
731         if (strncmp(reqcommand, CORETPK_INSTALL, strlen(CORETPK_INSTALL)) == 0) {
732                 return _coretpk_installer_prepare_package_install(data->pkgid, data->clientid);
733         }  else if (strncmp(reqcommand, CORETPK_UNINSTALL, strlen(CORETPK_UNINSTALL)) == 0) {
734                 return _coretpk_installer_prepare_package_uninstall(data->pkgid);
735         } else if (strncmp(reqcommand, CORETPK_DIRECTORY_INSTALL, strlen(CORETPK_DIRECTORY_INSTALL)) == 0) {
736                 return _coretpk_installer_prepare_directory_install(data->pkgid, data->clientid);
737         } else if (strncmp(reqcommand, CORETPK_MOVE, strlen(CORETPK_MOVE)) == 0) {
738                 return _coretpk_installer_package_move(data->pkgid, data->move_type);
739         } else if (strncmp(reqcommand, CORETPK_REINSTALL, strlen(CORETPK_REINSTALL)) == 0) {
740                 return _coretpk_installer_package_reinstall(data->pkgid, data->clientid);
741         } else {
742                 return -1;
743         }
744 }
745
746 #if 0
747 static char * _coretpk_installer_get_pkg_path(const char *pkg_path, const char *pkgid)
748 {
749         int ret = 0;
750         char buff[BUF_SIZE] = {'\0'};
751         char *real_path = NULL;
752
753         snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
754         do {
755                 if (__is_dir(buff)) break;
756                 memset(buff, '\0', BUF_SIZE);
757                 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
758                 if (__is_dir(buff)) break;
759                 memset(buff, '\0', BUF_SIZE);
760                 snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
761                 if (__is_dir(buff)) break;
762                 memset(buff, '\0', BUF_SIZE);
763                 snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS, pkgid);
764                 if (__is_dir(buff)) break;
765         } while (0);
766
767         ret = chdir(buff);
768         if (ret != 0) {
769                 _LOGE("chdir() failed [%s]\n", strerror(errno));
770                 return NULL;
771         }
772
773         real_path = (char *)malloc(strlen(buff) + 1);
774         if (real_path == NULL) {
775                 _LOGE("Malloc failed!\n");
776                 return NULL;
777         }
778         memset(real_path, '\0', strlen(buff) + 1);
779         memcpy(real_path, buff, strlen(buff));
780
781         return real_path;
782 }
783 #endif
784
785 int _coretpk_installer_verify_signatures(const char *root_path, const char *pkgid, int *visibility)
786 {
787         int ret = 0;
788         char buff[BUF_SIZE] = {'\0'};
789         const char *pkg_path = root_path;
790
791         _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
792
793 #if 0
794         // check for signature and certificate
795         pkg_path = _coretpk_installer_get_pkg_path(root_path, pkgid);
796         if (pkg_path == NULL) {
797                 _LOGE("pkg_path is NULL.");
798                 return 0;
799         }
800 #endif
801
802         ret = chdir(root_path);
803         if (ret != 0) {
804                 _LOGE("chdir(%s) failed. [%s]", root_path, strerror(errno));
805         }
806
807         // author-signature.xml is mandatory
808         snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
809         if (access(buff, F_OK) == 0) {
810                 _LOGD("author-signature.xml is found, path=[%s]", buff);
811                 ret = _ri_verify_sig_and_cert(buff, visibility);
812                 if (ret) {
813                         _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
814                         ret = -1;
815                         goto end;
816                 }
817                 _LOGD("_ri_verify_sig_and_cert succeed, path=[%s]", buff);
818         } else {
819                 _LOGE("cannot access xml, path=[%s]", buff);
820                 ret = -1;
821                 goto end;
822         }
823         memset(buff, '\0', BUF_SIZE);
824
825         // signature1.xml is mandatory
826         snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
827         if (access(buff, F_OK) == 0) {
828                 _LOGD("signature1.xml is found, path=[%s]", pkg_path);
829                 ret = _ri_verify_sig_and_cert(buff, visibility);
830                 if (ret) {
831                         _LOGE("_ri_verify_sig_and_cert() failed, path=[%s]", buff);
832                         ret = -1;
833                         goto end;
834                 }
835                 _LOGD("_ri_verify_sig_and_cert() succeed, path=[%s]", buff);
836         } else {
837                 _LOGE("cannot access xml, path=[%s]", buff);
838                 ret = -1;
839                 goto end;
840         }
841         memset(buff, '\0', BUF_SIZE);
842         ret = 0;
843
844 end:
845 #if 0
846         if(pkg_path){
847                 free(pkg_path);
848                 pkg_path = NULL;
849         }
850 #endif
851
852         return ret;
853 }
854
855 char* _coretpk_installer_load_directory(char *directory,char *pkgfile)
856 {
857         DIR *dir;
858         struct dirent entry;
859         struct dirent *result;
860         int ret = 0;
861         char *buf = NULL;
862         char *pkgname = NULL;
863         char xml_file[MAX_BUF_SIZE] = {'\0'};
864
865         buf = malloc(BUF_SIZE);
866         if (buf == NULL) {
867                 _LOGE("malloc() failed.");
868                 return NULL;
869         }
870
871         dir = opendir(directory);
872         if (!dir) {
873                 _LOGL("opendir()", errno);
874                 free(buf);
875                 return NULL;
876         }
877
878         _LOGD("loading manifest files, directory=[%s]", directory);
879
880         for (ret = readdir_r(dir, &entry, &result);
881                         ret == 0 && result != NULL;
882                         ret = readdir_r(dir, &entry, &result)) {
883                 char *manifest = NULL;
884
885                 if (!strcmp(entry.d_name, ".") ||
886                         !strcmp(entry.d_name, "..")) {
887                         continue;
888                 }
889
890                 manifest = _manifest_to_package(entry.d_name);
891                 if (!manifest) {
892                         _LOGE("failed to convert file to xml, file=[%s].", entry.d_name);
893                         continue;
894                 }
895
896                 memset(xml_file,'\0',MAX_BUF_SIZE);
897                 snprintf(xml_file,MAX_BUF_SIZE-1,"%s/%s", directory, manifest);
898                 _LOGD("manifest=[%s], path=[%s]", manifest, xml_file);
899
900                 ret = _get_package_name_from_xml(xml_file, &pkgname);
901                 if (ret != PMINFO_R_OK || pkgname == NULL) {
902                         _LOGE("unable to read, xml_file=[%s]", xml_file);
903                         free(manifest);
904                         continue;
905                 }
906
907                 if (pkgname[0] != '\0') {
908                         snprintf(buf, BUF_SIZE, "%s/%s", directory, manifest);
909                         free(manifest);
910                         break;
911                 }
912
913                 free(manifest);
914         }
915
916         closedir(dir);
917
918         if (pkgname) {
919                 free(pkgname);
920                 pkgname = NULL;
921         }
922
923
924         return buf;
925 }
926
927 pkginfo *_coretpk_installer_get_pkgfile_info(char *pkgfile)
928 {
929         pkginfo *info = NULL;
930         int ret = 0;
931         char cwd[BUF_SIZE] = {'\0'};
932         char buff[BUF_SIZE] = {'\0'};
933         char manifest[BUF_SIZE] = { '\0'};
934         char *temp = NULL;
935
936         char *tmp_pkgid = NULL;
937         char *tmp_version = NULL;
938
939         temp = getcwd(cwd, BUF_SIZE);
940         if ((temp == NULL) || (cwd[0] == '\0')) {
941                 _LOGL("getcwd()", errno);
942                 return NULL;
943         }
944
945         ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_755);
946         if (ret < 0) {
947                 if (access(TEMP_DIR, F_OK) == 0) {
948                         _rpm_delete_dir(TEMP_DIR);
949                         ret = mkdir(TEMP_DIR, DIRECTORY_PERMISSION_644);
950                         if (ret < 0) {
951                                 _LOGL("mkdir()", errno);
952                                 return NULL;
953                         }
954                 } else {
955                         _LOGL("access()", errno);
956                         return NULL;
957                 }
958         }
959
960         ret = chdir(TEMP_DIR);
961         if (ret != 0) {
962                 _LOGL("chdir()", errno);
963                 goto err;
964         }
965
966         _LOGD("switched to %s", TEMP_DIR);
967
968         snprintf(manifest, BUF_SIZE, "%s", CORETPK_XML);
969         const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, manifest, "-d", TEMP_DIR, NULL };
970         ret = _ri_xsystem(unzip_argv);
971         if (ret != 0) {
972                 _LOGE("cannot find manifest in the package.");
973                 ret = RPM_INSTALLER_ERR_NO_MANIFEST;
974                 goto err;
975         }
976
977         char* manifestpath = _coretpk_installer_load_directory(TEMP_DIR, pkgfile);
978         if (manifestpath != NULL) {
979                 strcpy(buff, manifestpath);
980                 free(manifestpath);
981         }
982
983         if (buff[0] == '\0') {
984                 _LOGE("cannot find manifest in the package.");
985                 goto err;
986         }
987
988         _LOGD("manifest file=[%s]",buff);
989
990         info = calloc(1, sizeof(pkginfo));
991         if (info == NULL) {
992                 _LOGE("calloc() failed.");
993                 goto err;
994         }
995
996         tmp_pkgid = __find_info_from_xml(buff, "package");
997         if (tmp_pkgid != NULL) {
998                 strncpy(info->package_name, tmp_pkgid, sizeof(info->package_name) - 1);
999                 free(tmp_pkgid);
1000         } else {
1001                 _LOGE("can not get pkgid");
1002                 goto err;
1003         }
1004
1005         tmp_version = __find_info_from_xml(buff, "version");
1006         if (tmp_version != NULL) {
1007                 strncpy(info->version, tmp_version, sizeof(info->version) - 1);
1008                 free(tmp_version);
1009         } else {
1010                 _LOGE("can not get version");
1011                 goto err;
1012         }
1013
1014         _LOGD("pkgid=[%s], version=[%s]", info->package_name, info->version);
1015
1016 err:
1017         _rpm_delete_dir(TEMP_DIR);
1018
1019         ret = chdir(cwd);
1020         if (ret != 0) {
1021                 _LOGL("chdir()", errno);
1022         }
1023
1024         return info;
1025 }
1026
1027 int _coretpk_installer_convert_manifest(char *manifestfilepath, char *pkgid, char* clientid)
1028 {
1029         int ret = 0;
1030         char rwmanifest[BUF_SIZE] = {'\0'};
1031
1032         if (clientid != NULL) {
1033                 _LOGD("client package id=[%s]", clientid);
1034         }
1035
1036         /*run script*/
1037         if (strstr(manifestfilepath, OPT_USR_APPS)) {
1038                 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
1039                 const char *rw_xml_argv[] = { CORETPK_RW_XML_CONVERTER, manifestfilepath, rwmanifest, pkgid, clientid, NULL };
1040                 ret = _ri_xsystem(rw_xml_argv);
1041         } else {
1042                 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
1043                 const char *ro_xml_argv[] = { CORETPK_RO_XML_CONVERTER, manifestfilepath, rwmanifest, pkgid, clientid, NULL };
1044                 ret = _ri_xsystem(ro_xml_argv);
1045         }
1046
1047         _LOGD("pkgid=[%s]", pkgid);
1048         _LOGD("tizen-manifest=[%s]", manifestfilepath);
1049         _LOGD("converted manifest=[%s]", rwmanifest);
1050
1051         if (ret != 0) {
1052                 if (ret == INCLUDE_ABSOLUTE_PATH) {
1053                         _LOGE("path of exec or icon can not be started with absolute path.");
1054                 } else {
1055                         _LOGL("converting the manifest file", errno);
1056                 }
1057                 ret = RPM_INSTALLER_ERR_INTERNAL;
1058                 goto err;
1059         }
1060
1061         _LOGD("manifest is converted successfully");
1062
1063 err:
1064         return ret;
1065 }
1066
1067 int _coretpk_installer_get_configuration_value(char *value)
1068 {
1069         char buffer[BUF_SIZE] = {'\0'};
1070         char *p = NULL;
1071         FILE *fi = NULL;
1072         int len = 0;
1073         int ret = 0;
1074
1075         if (access(CORETPK_CONFIG_PATH, F_OK) != 0) {
1076                 /* if there is no ini file, signature has to be checked */
1077                 return 1;
1078         }
1079
1080         fi = fopen(CORETPK_CONFIG_PATH, "r");
1081         if (fi == NULL) {
1082                 _LOGL("fopen()", errno);
1083                 return 0;
1084         }
1085
1086         while (fgets(buffer, BUF_SIZE, fi) != NULL) {
1087                 /* buffer will be like signature=off\n\0*/
1088                 if (strncmp(buffer, value, strlen(value)) == 0) {
1089                         len = strlen(buffer);
1090                         /*remove newline character*/
1091                         buffer[len - 1] = '\0';
1092                         p = strchr(buffer, '=');
1093                         if (p) {
1094                                 p++;
1095                                 if (strcmp(p, "on") == 0) {
1096                                         ret = 1;
1097                                 } else {
1098                                         ret = 0;
1099                                 }
1100                         }
1101                 } else {
1102                         continue;
1103                 }
1104         }
1105
1106         fclose(fi);
1107         return ret;
1108 }
1109
1110 int _coretpk_installer_apply_file_policy(char *filepath)
1111 {
1112         int ret = 0;
1113
1114         if (access(filepath, F_OK) == 0) {
1115                 /*permission(644)*/
1116                 ret = chmod(filepath, FILE_PERMISSION_644);
1117                 if (ret != 0) {
1118                         _LOGL("chmod()", errno);
1119                 }
1120         } else {
1121                 _LOGE("skip! empty filepath=[%s]", filepath);
1122         }
1123
1124         return 0;
1125 }
1126
1127 int _coretpk_installer_apply_directory_policy(char *dirpath, int mode, bool appowner)
1128 {
1129         int ret = 0;
1130         DIR *dir;
1131         struct dirent entry;
1132         struct dirent *result;
1133         char fullpath[BUF_SIZE] = {'\0'};
1134
1135         if (access(dirpath, F_OK) != 0) {
1136                 _LOGE("skip! empty dirpath=[%s]", dirpath);
1137                 return 0;
1138         }
1139
1140         dir = opendir(dirpath);
1141         if (!dir) {
1142                 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1143                 return -1;
1144         }
1145
1146         // permission(755)
1147         ret = _coretpk_installer_change_mode(dirpath, DIRECTORY_PERMISSION_755);
1148         if (ret != 0) {
1149                 _LOGE("_coretpk_installer_change_mode is failed, dirpath=[%s]", dirpath);
1150         }
1151
1152         for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
1153                 if (strcmp(entry.d_name, ".") == 0) {
1154                         snprintf(fullpath, BUF_SIZE, "%s/", dirpath);
1155                         if (appowner == true) {
1156                                 _coretpk_installer_change_directory_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1157                         }
1158                         ret = _coretpk_installer_change_mode(fullpath, DIRECTORY_PERMISSION_755);
1159                         if (ret != 0) {
1160                                 _LOGE("_coretpk_installer_change_mode is failed, fullpath=[%s]", fullpath);
1161                         }
1162                         continue;
1163                 } else if (strcmp(entry.d_name, "..") == 0) {
1164                         continue;
1165                 }
1166
1167                 // sub dir
1168                 if (entry.d_type == DT_DIR) {
1169                         snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1170
1171                         // owner:group
1172                         if (appowner == true) {
1173                                 ret = _coretpk_installer_change_directory_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1174                                 if (ret != 0) {
1175                                         _LOGE("_coretpk_installer_change_directory_owner failed, fullpath=[%s]", fullpath);
1176                                 }
1177                         }
1178                 // sub file
1179                 } else {
1180                         snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1181
1182                         // permission(input mode)
1183                         ret = _coretpk_installer_change_mode(fullpath, mode);
1184                         if (ret != 0) {
1185                                 _LOGE("_coretpk_installer_change_mode failed, fullpath=[%s]", fullpath);
1186                         }
1187
1188                         // owner:group
1189                         if (appowner == true) {
1190                                 ret = _coretpk_installer_change_file_owner(fullpath, APP_OWNER_ID, APP_GROUP_ID);
1191                                 if (ret != 0) {
1192                                         _LOGE("_coretpk_installer_change_file_owner failed, fullpath=[%s]", fullpath);
1193                                 }
1194                         }
1195                 }
1196
1197                 // find next dir
1198                 if (entry.d_type == DT_DIR) {
1199                         ret = _coretpk_installer_apply_directory_policy(fullpath, mode, appowner);
1200                         if(ret != 0 ){
1201                                 _LOGE("_coretpk_installer_apply_directory_policy failed, fullpath=[%s]", fullpath);
1202                         }
1203                 }
1204                 memset(fullpath, '\0', BUF_SIZE);
1205         }
1206
1207         closedir(dir);
1208
1209         return ret;
1210 }
1211
1212 int _coretpk_installer_make_directory_for_ext(char *pkgid)
1213 {
1214         char ext_pkg_base_path[BUF_SIZE] = {0, };
1215         char temp_path[BUF_SIZE] = {0, };
1216         char pkg_shared_data_path[BUF_SIZE] = {0, };
1217         char *shared_data_label = NULL;
1218         int res = 0;
1219
1220         if (access(OPT_STORAGE_SDCARD, F_OK) != 0) {
1221                 _LOGL("There is no OPT_STORAGE_SDCARD", errno);
1222                 return -1;
1223         }
1224
1225         /*pkg root path*/
1226         if (access(OPT_STORAGE_SDCARD_APP_ROOT, F_OK) != 0) {
1227                 /*permission(755)*/
1228                 res = mkdir(OPT_STORAGE_SDCARD_APP_ROOT, DIRECTORY_PERMISSION_755);
1229                 if (res < 0) {
1230                         _LOGL("mkdir()", errno);
1231                         return -1;
1232                         }
1233         }
1234
1235         /*app root path*/
1236         snprintf(ext_pkg_base_path, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
1237         res = mkdir(ext_pkg_base_path, 0500);
1238         if (res == -1 && errno != EEXIST)
1239         {
1240                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1241                 return -1;
1242         }
1243
1244         res = _coretpk_installer_set_smack_label_access(ext_pkg_base_path, "_");
1245         if (res != 0)
1246         {
1247                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1248                 return -1;
1249         }
1250
1251         //data
1252         memset(temp_path, 0, BUF_SIZE);
1253         strcpy(temp_path, ext_pkg_base_path);
1254         strncat(temp_path, "/data", strlen("/data"));
1255         res = mkdir(temp_path, 0700);
1256         if (res == -1 && errno != EEXIST)
1257         {
1258                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1259                 return -1;
1260         }
1261         res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1262         if (res != 0)
1263         {
1264                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1265                 return -1;
1266         }
1267
1268         //cache
1269         memset(temp_path, 0, BUF_SIZE);
1270         strcpy(temp_path, ext_pkg_base_path);
1271         strncat(temp_path, "/cache", strlen("/cache"));
1272         res = mkdir(temp_path, 0700);
1273         if (res == -1 && errno != EEXIST)
1274         {
1275                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1276                 return -1;
1277         }
1278         res = _coretpk_installer_set_smack_label_access(temp_path, pkgid);
1279         if (res != 0)
1280         {
1281                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1282                 return -1;
1283         }
1284
1285         //shared
1286         memset(temp_path, 0, BUF_SIZE);
1287         strcpy(temp_path, ext_pkg_base_path);
1288         strncat(temp_path, "/shared", strlen("/shared"));
1289         res = mkdir(temp_path, 0500);
1290         if (res == -1 && errno != EEXIST)
1291         {
1292                 _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1293                 return -1;
1294         }
1295         res = _coretpk_installer_set_smack_label_access(temp_path, "_");
1296         if (res != 0)
1297         {
1298                 _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1299                 return -1;
1300         }
1301
1302         snprintf(pkg_shared_data_path, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid , "shared/data");
1303
1304         res = access(pkg_shared_data_path, F_OK);
1305         if (res == 0)
1306         {
1307                 _LOGD("Exist shared/data folder (path:[%s])", pkg_shared_data_path);
1308                 res = _coretpk_installer_get_smack_label_access(pkg_shared_data_path, &shared_data_label);
1309                 if (res != 0)
1310                 {
1311                         _LOGE("_coretpk_installer_get_smack_label_access() is failed.");
1312                         return -1;
1313                 }
1314
1315                 //shared/data
1316                 strncat(temp_path, "/data", strlen("/data"));
1317                 res = mkdir(temp_path, 0705);
1318                 if (res == -1 && errno != EEXIST)
1319                 {
1320                         _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1321                         return -1;
1322                 }
1323
1324                 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1325                 if (res != 0)
1326                 {
1327                         _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1328                         return -1;
1329                 }
1330
1331                 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1332                 if (res != 0)
1333                 {
1334                         _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1335 //                      return -1;
1336                 }
1337
1338                 //shared/cache
1339                 memset(temp_path, 0, BUF_SIZE);
1340                 strcpy(temp_path, ext_pkg_base_path);
1341                 strncat(temp_path, "/shared", strlen("/shared"));
1342                 strncat(temp_path, "/cache", strlen("/cache"));
1343                 res = mkdir(temp_path, 0700);
1344                 if (res == -1 && errno != EEXIST)
1345                 {
1346                         _LOGE("mkdir() is failed. error = [%d] strerror = [%s]", errno, strerror(errno));
1347                         return -1;
1348                 }
1349                 res = _coretpk_installer_set_smack_label_access(temp_path, shared_data_label);
1350                 if (res != 0)
1351                 {
1352                         _LOGE("_coretpk_installer_set_smack_label_access() is failed.");
1353                         return -1;
1354                 }
1355                 res = _coretpk_installer_set_smack_label_transmute(temp_path, "1");
1356                 if (res != 0)
1357                 {
1358                         _LOGE("_coretpk_installer_set_smack_label_transmute() is failed.");
1359 //                      return -1;
1360                 }
1361
1362         }
1363         else if (res == -1 && errno == ENOENT)
1364         {
1365                 _LOGD("Directory dose not exist. path: %s, errno: %d (%s)",
1366                                 pkg_shared_data_path, errno, strerror(errno));
1367                 return 0;
1368         }
1369         else
1370         {
1371                 _LOGE("access() failed. path: %s, errno: %d (%s)",
1372                                 pkg_shared_data_path, errno, strerror(errno));
1373                 return -1;
1374         }
1375
1376         return 0;
1377 }
1378
1379
1380 int _coretpk_installer_make_directory(char *pkgid)
1381 {
1382         int ret = 0;
1383         char appdir[BUF_SIZE] = {'\0'};
1384         char rootfile[BUF_SIZE] = {'\0'};
1385         char *groupid = NULL;
1386
1387         // check param
1388         if (pkgid == NULL) {
1389                 _LOGE("pkgid is NULL.");
1390                 return -1;
1391         }
1392
1393         // root
1394         memset(appdir, '\0', BUF_SIZE);
1395         snprintf(appdir, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
1396         if (access(appdir, F_OK) != 0) {
1397                 // permission(755)
1398                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1399                 if (ret < 0) {
1400                         _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1401                 }
1402         }
1403         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1404         if (ret != 0) {
1405                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1406                 return -1;
1407         }
1408
1409         // bin
1410         memset(appdir, '\0', BUF_SIZE);
1411         snprintf(appdir, BUF_SIZE, "%s/%s/bin", OPT_USR_APPS, pkgid);
1412         if (access(appdir, F_OK) != 0) {
1413                 memset(appdir, '\0', BUF_SIZE);
1414                 snprintf(appdir, BUF_SIZE, "%s/%s/bin", USR_APPS, pkgid);
1415                 if (access(appdir, F_OK) != 0) {
1416                         _LOGE("[%s] is not existed.", appdir);
1417                         return -1;
1418                 }
1419         }
1420         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1421         if (ret != 0) {
1422                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1423                 return -1;
1424         }
1425
1426         // data
1427         memset(appdir, '\0', BUF_SIZE);
1428         snprintf(appdir, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgid);
1429         if (access(appdir, F_OK) != 0) {
1430                 // permission(755)
1431                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1432                 if (ret < 0) {
1433                         _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1434                 }
1435         }
1436         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1437         if (ret != 0) {
1438                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1439                 return -1;
1440         }
1441
1442         //lib
1443         memset(appdir, '\0', BUF_SIZE);
1444         snprintf(appdir, BUF_SIZE, "%s/%s/lib", OPT_USR_APPS, pkgid);
1445         if (access(appdir, F_OK) != 0) {
1446                 memset(appdir, '\0', BUF_SIZE);
1447                 snprintf(appdir, BUF_SIZE, "%s/%s/lib", USR_APPS, pkgid);
1448         }
1449         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE | PERM_EXECUTE, false);
1450         if (ret != 0) {
1451                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1452                 return -1;
1453         }
1454
1455         // res
1456         memset(appdir, '\0', BUF_SIZE);
1457         snprintf(appdir, BUF_SIZE, "%s/%s/res", OPT_USR_APPS, pkgid);
1458         if (access(appdir, F_OK) != 0) {
1459                 memset(appdir, '\0', BUF_SIZE);
1460                 snprintf(appdir, BUF_SIZE, "%s/%s/res", USR_APPS, pkgid);
1461         }
1462         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1463         if (ret != 0) {
1464                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1465                 return -1;
1466         }
1467
1468         // cache
1469         memset(appdir, '\0', BUF_SIZE);
1470         snprintf(appdir, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgid);
1471         if (access(appdir, F_OK) != 0) {
1472                 // permission(755)
1473                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1474                 if (ret < 0) {
1475                         _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1476                 }
1477         }
1478         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1479         if (ret != 0) {
1480                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1481                 return -1;
1482         }
1483
1484         // shared
1485         memset(appdir, '\0', BUF_SIZE);
1486         snprintf(appdir, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgid);
1487         if (access(appdir, F_OK) != 0) {
1488                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1489                 if (ret < 0) {
1490                         _LOGE("mkdir(%s) failed. [%d][%s]", appdir, errno, strerror(errno));
1491                 }
1492         }
1493         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1494         if (ret != 0) {
1495                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1496                 return -1;
1497         }
1498         memset(appdir, '\0', BUF_SIZE);
1499         snprintf(appdir, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgid);
1500         if (access(appdir, F_OK) != 0) {
1501                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1502                 if (ret < 0) {
1503                         _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1504                 }
1505         }
1506         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1507         if (ret != 0) {
1508                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1509                 return -1;
1510         }
1511
1512         // shared/data
1513         memset(appdir, '\0', BUF_SIZE);
1514         snprintf(appdir, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
1515         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1516         if (ret != 0) {
1517                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1518                 return -1;
1519         }
1520
1521         // shared/cache
1522         memset(appdir, '\0', BUF_SIZE);
1523         snprintf(appdir, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgid);
1524         if (access(appdir, F_OK) != 0) {
1525                 ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1526                 if (ret < 0) {
1527                         _LOGE("mkdir failed. appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1528                 }
1529         }
1530         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1531         if (ret != 0) {
1532                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1533                 return -1;
1534         }
1535
1536         // shared/res
1537         memset(appdir, '\0', BUF_SIZE);
1538         snprintf(appdir, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
1539         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1540         if (ret != 0) {
1541                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1542                 return -1;
1543         }
1544         memset(appdir, '\0', BUF_SIZE);
1545         snprintf(appdir, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgid);
1546         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, false);
1547         if (ret != 0) {
1548                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1549                 return -1;
1550         }
1551
1552         // shared/trusted
1553         memset(appdir, '\0', BUF_SIZE);
1554         snprintf(appdir, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
1555         if (access(appdir, F_OK) != 0) {
1556                 ret = _coretpk_installer_get_group_id(pkgid, &groupid);
1557                 if (ret == 0) {
1558                         ret = mkdir(appdir, DIRECTORY_PERMISSION_755);
1559                         if (ret < 0) {
1560                                 _LOGE("mkdir failed, appdir=[%s], errno=[%d][%s]", appdir, errno, strerror(errno));
1561                         }
1562                         free(groupid);
1563                 }
1564         }
1565         ret = _coretpk_installer_apply_directory_policy(appdir, PERM_BASE, true);
1566         if (ret != 0) {
1567                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", appdir, ret);
1568                 return -1;
1569         }
1570
1571         // [pkgid]/tizen-manifest.xml
1572         snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
1573         ret = _coretpk_installer_apply_file_policy(rootfile);
1574         if (ret != 0) {
1575                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1576                 return -1;
1577         }
1578         memset(rootfile, '\0', BUF_SIZE);
1579         snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, CORETPK_XML);
1580         ret = _coretpk_installer_apply_file_policy(rootfile);
1581         if (ret != 0) {
1582                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1583                 return -1;
1584         }
1585
1586         // [pkgid]/author-signature.xml
1587         memset(rootfile, '\0', BUF_SIZE);
1588         snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1589         ret = _coretpk_installer_apply_file_policy(rootfile);
1590         if (ret != 0) {
1591                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1592                 return -1;
1593         }
1594         memset(rootfile, '\0', BUF_SIZE);
1595         snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1596         ret = _coretpk_installer_apply_file_policy(rootfile);
1597         if (ret != 0) {
1598                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1599                 return -1;
1600         }
1601
1602         // [pkgid]/signature1.xml
1603         memset(rootfile, '\0', BUF_SIZE);
1604         snprintf(rootfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SIGNATURE1_XML);
1605         ret = _coretpk_installer_apply_file_policy(rootfile);
1606         if (ret != 0) {
1607                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1608                 return -1;
1609         }
1610         memset(rootfile, '\0', BUF_SIZE);
1611         snprintf(rootfile, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, SIGNATURE1_XML);
1612         ret = _coretpk_installer_apply_file_policy(rootfile);
1613         if (ret != 0) {
1614                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1615                 return -1;
1616         }
1617
1618         // /opt/share/packages/[pkgid].xml
1619         memset(rootfile, '\0', BUF_SIZE);
1620         snprintf(rootfile, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
1621         ret = _coretpk_installer_apply_file_policy(rootfile);
1622         if (ret != 0) {
1623                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1624                 return -1;
1625         }
1626         memset(rootfile, '\0', BUF_SIZE);
1627         snprintf(rootfile, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
1628         ret = _coretpk_installer_apply_file_policy(rootfile);
1629         if (ret != 0) {
1630                 _LOGE("_coretpk_installer_apply_file_policy() failed, rootfile=[%s]", rootfile);
1631                 return -1;
1632         }
1633 /*
1634         // for external storage
1635         if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
1636                 int ret = _coretpk_installer_make_directory_for_ext(pkgid);
1637                 if (ret != 0) {
1638                         _LOGE("_coretpk_installer_make_directory_for_ext() failed, pkgid=[%s]", pkgid);
1639                         return -1;
1640                 }
1641         }
1642 */
1643         return ret;
1644 }
1645
1646 int _coretpk_installer_change_mode(char *path, int mode)
1647 {
1648         int ret = 0;
1649
1650         ret = chmod(path, mode);
1651         if (ret != 0) {
1652                 _LOGL("chmod()", errno);
1653                 return -1;
1654         }
1655
1656         return ret;
1657 }
1658
1659 int _coretpk_installer_change_file_owner(char *path, int ownerid, int groupid)
1660 {
1661         int ret = 0;
1662
1663         if (access(path, F_OK) == 0) {
1664                 ret = chown(path, ownerid, groupid);
1665                 if (ret != 0) {
1666                         _LOGL("chown()", errno);
1667                         return -1;
1668                 }
1669         }
1670
1671         return ret;
1672 }
1673
1674 int _coretpk_installer_change_directory_owner(char *dirpath, int ownerid, int groupid)
1675 {
1676         int ret = 0;
1677
1678         if (__is_dir(dirpath)) {
1679                 ret = chown(dirpath, ownerid, groupid);
1680                 if (ret != 0) {
1681                         _LOGL("chown()", errno);
1682                         return -1;
1683                 }
1684         }
1685
1686         return ret;
1687 }
1688
1689 void _coretpk_installer_set_privilege_setup_path_for_ext(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1690 {
1691         char path[BUF_SIZE] = {'\0'};
1692
1693         snprintf(path, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, dirpath);
1694         if (access(path, F_OK) == 0) {
1695                 _ri_privilege_setup_path(pkgid, path, type, label);
1696         }
1697 }
1698
1699 void _coretpk_installer_set_privilege_setup_path(char *pkgid, char *dirpath, app_path_type_t type, char *label)
1700 {
1701         char path[BUF_SIZE] = {'\0'};
1702
1703         snprintf(path, BUF_SIZE, "%s/%s", USR_APPS, dirpath);
1704         if (access(path, F_OK) == 0) {
1705                 _ri_privilege_setup_path(pkgid, path, type, label);
1706         }
1707         memset(path, '\0', BUF_SIZE);
1708
1709         snprintf(path, BUF_SIZE, "%s/%s", OPT_USR_APPS, dirpath);
1710         if (access(path, F_OK) == 0) {
1711                 _ri_privilege_setup_path(pkgid, path, type, label);
1712         }
1713 }
1714
1715 int _coretpk_installer_get_group_id(char *pkgid, char **result)
1716 {
1717         int ret = 0;
1718         const char *value = NULL;
1719         char author_signature[BUF_SIZE] = {'\0'};
1720         char *e_rootcert = NULL;
1721         char *d_rootcert = NULL;
1722         gsize d_size = 0;
1723         unsigned char hashout[BUF_SIZE] = {'\0'};
1724         unsigned int h_size = 0;
1725         int e_size = 0;
1726         int length = 0;
1727         pkgmgrinfo_certinfo_h handle = NULL;
1728
1729         snprintf(author_signature, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1730         if (access(author_signature, F_OK) != 0) {
1731                 _LOGE("[%s] is not found.", author_signature);
1732
1733                 memset(author_signature, '\0', BUF_SIZE);
1734                 snprintf(author_signature, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
1735                 if (access(author_signature, F_OK) != 0) {
1736                         _LOGE("[%s] is not found.", author_signature);
1737                         return -1;
1738                 } else {
1739                         _LOGE("author_signature=[%s]", author_signature);
1740                 }
1741         }
1742
1743         ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
1744         if (ret < 0) {
1745                 _LOGE("failed to get cert info.");
1746                 goto err;
1747         }
1748
1749         ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle);
1750         if (ret < 0) {
1751                 _LOGE("failed to load cert info.");
1752                 goto err;
1753         }
1754
1755         /*get root certificate*/
1756         ret = pkgmgrinfo_pkginfo_get_cert_value(handle, PMINFO_AUTHOR_SIGNER_CERT, &value);
1757         if (ret < 0 || value == NULL) {
1758                 _LOGE("failed to get cert value.");
1759                 goto err;
1760         }
1761
1762         /*decode cert*/
1763         d_rootcert = (char *)g_base64_decode(value, &d_size);
1764         if (d_rootcert == NULL) {
1765                 _LOGE("failed to execute decode.");
1766                 goto err;
1767         }
1768
1769         /*hash*/
1770         EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
1771         if (h_size <= 0) {
1772                 _LOGE("@Failed to get hash.");
1773                 goto err;
1774         }
1775
1776         /*encode cert*/
1777         e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
1778         if (e_rootcert == NULL) {
1779                 _LOGE("failed to execute encode.");
1780                 goto err;
1781         }
1782         e_size = strlen(e_rootcert);
1783         _LOGD("encoding done, len=[%d]", e_size);
1784
1785         /*replace / to #*/
1786         for (length = e_size; length >= 0; --length) {
1787                 if (e_rootcert[length] == '/') {
1788                         e_rootcert[length] = '#';
1789                 }
1790         }
1791
1792         *result = e_rootcert;
1793
1794 err:
1795         if (d_rootcert) {
1796                 free(d_rootcert);
1797         }
1798
1799         /*destroy cert*/
1800         if (handle) {
1801                 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
1802         }
1803
1804         return ret;
1805 }
1806
1807 int _coretpk_installer_apply_smack_for_ext(char *pkgname)
1808 {
1809         int ret = 0;
1810         char dirpath[BUF_SIZE] = {'\0'};
1811
1812         // approot
1813         snprintf(dirpath, BUF_SIZE, "%s", pkgname);
1814         _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1815         memset(dirpath, '\0', BUF_SIZE);
1816
1817         // [pkgid]/data
1818         snprintf(dirpath, BUF_SIZE, "%s/data", pkgname);
1819         _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1820         memset(dirpath, '\0', BUF_SIZE);
1821
1822         // [pkgid]/cache
1823         snprintf(dirpath, BUF_SIZE, "%s/cache", pkgname);
1824         _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1825         memset(dirpath, '\0', BUF_SIZE);
1826
1827         // [pkgid]/shared
1828         snprintf(dirpath, BUF_SIZE, "%s/shared", pkgname);
1829         _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1830         memset(dirpath, '\0', BUF_SIZE);
1831
1832         // [pkgid]/shared/data
1833         snprintf(dirpath, BUF_SIZE, "%s/shared/data", pkgname);
1834         _coretpk_installer_set_privilege_setup_path_for_ext(pkgname, dirpath, APP_PATH_PUBLIC_RO, NULL);
1835
1836         return ret;
1837 }
1838
1839 int _coretpk_installer_apply_smack(char *pkgname, int flag)
1840 {
1841         int ret = 0;
1842         char dirpath[BUF_SIZE] = {'\0'};
1843         char manifest[BUF_SIZE] = {'\0'};
1844         char *groupid = NULL;
1845         char *shared_data_label = NULL;
1846
1847         _ri_privilege_register_package(pkgname);
1848
1849         // app root
1850         snprintf(dirpath, BUF_SIZE, "%s", pkgname);
1851         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1852         memset(dirpath, '\0', BUF_SIZE);
1853
1854         // shared
1855         snprintf(dirpath, BUF_SIZE, "%s/shared", pkgname);
1856         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1857         memset(dirpath, '\0', BUF_SIZE);
1858
1859         // shared/res
1860         snprintf(dirpath, BUF_SIZE, "%s/shared/res", pkgname);
1861         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_ANY_LABEL, "_");
1862         memset(dirpath, '\0', BUF_SIZE);
1863
1864         // shared/data
1865         snprintf(dirpath, BUF_SIZE, "%s/shared/data", pkgname);
1866         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PUBLIC_RO, NULL);
1867         memset(dirpath, '\0', BUF_SIZE);
1868
1869         // shared/cache
1870         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
1871         ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
1872         if (ret == 0) {
1873                 memset(dirpath, '\0', BUF_SIZE);
1874                 snprintf(dirpath, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgname);
1875                 ret  = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
1876                 if (ret != 0) {
1877                         _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1878                 }
1879                 ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
1880                 if (ret != 0) {
1881                         _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
1882                 }
1883         }
1884
1885         // shared/trusted
1886         memset(dirpath, '\0', BUF_SIZE);
1887         snprintf(dirpath, BUF_SIZE, "%s/shared/trusted", pkgname);
1888         if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
1889                 ret = _coretpk_installer_get_group_id(pkgname, &groupid);
1890                 if (ret == 0) {
1891                         LOGD("groupid = [%s] for shared/trusted.", groupid);
1892                         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
1893                         if (groupid)
1894                                 free(groupid);
1895                 } else {
1896                         LOGE("_coretpk_installer_get_group_id(%s) failed.", pkgname);
1897                         return -1;
1898                 }
1899         }
1900         memset(dirpath, '\0', BUF_SIZE);
1901
1902         // bin
1903         snprintf(dirpath, BUF_SIZE, "%s/bin", pkgname);
1904         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1905         memset(dirpath, '\0', BUF_SIZE);
1906
1907         // data
1908         snprintf(dirpath, BUF_SIZE, "%s/data", pkgname);
1909         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1910         memset(dirpath, '\0', BUF_SIZE);
1911
1912         // lib
1913         snprintf(dirpath, BUF_SIZE, "%s/lib", pkgname);
1914         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1915         memset(dirpath, '\0', BUF_SIZE);
1916
1917         // res
1918         snprintf(dirpath, BUF_SIZE, "%s/res", pkgname);
1919         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1920         memset(dirpath, '\0', BUF_SIZE);
1921
1922         // cache
1923         snprintf(dirpath, BUF_SIZE, "%s/cache", pkgname);
1924         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1925         memset(dirpath, '\0', BUF_SIZE);
1926
1927         // tizen-manifest.xml
1928         snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, CORETPK_XML);
1929         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1930         memset(dirpath, '\0', BUF_SIZE);
1931
1932         // author-signature.xml
1933         snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, AUTHOR_SIGNATURE_XML);
1934         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1935         memset(dirpath, '\0', BUF_SIZE);
1936
1937         // signature1.xml
1938         snprintf(dirpath, BUF_SIZE, "%s/%s", pkgname, SIGNATURE1_XML);
1939         _coretpk_installer_set_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
1940         memset(dirpath, '\0', BUF_SIZE);
1941
1942         // [pkgid].xml
1943         snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgname);
1944         if (access(manifest, F_OK) == 0) {
1945                 _ri_privilege_setup_path(pkgname, manifest, APP_PATH_PRIVATE, pkgname);
1946         }
1947
1948         // external storage
1949         if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
1950                 if (access(OPT_STORAGE_SDCARD, F_OK) == 0) {
1951                         ret = _coretpk_installer_apply_smack_for_ext(pkgname);
1952                         if (ret != 0) {
1953                                 _LOGE("_coretpk_installer_apply_smack_for_ext(%s) failed.", pkgname);
1954                                 return -1;
1955                         }
1956                 }
1957         }
1958
1959         return ret;
1960 }
1961
1962 static char * __getprivilege(const char* pBuf)
1963 {
1964         const char* pKey = "<privilege>";
1965         const char* p = NULL;
1966         const char* pStart = NULL;
1967         const char* pEnd = NULL;
1968
1969         p = strstr(pBuf, pKey);
1970         if (p == NULL)
1971                 return NULL;
1972
1973         pStart = p + strlen(pKey);
1974         pEnd = strchr(pStart, '<');
1975         if (pEnd == NULL)
1976                 return NULL;
1977
1978         size_t len = pEnd - pStart;
1979         if (len <= 0)
1980                 return NULL;
1981
1982         char *pRes = (char*)malloc(len + 1);
1983         if(pRes == NULL){
1984                 _LOGE("malloc failed!!");
1985                 return NULL;
1986         }
1987         strncpy(pRes, pStart, len);
1988         pRes[len] = 0;
1989
1990         return pRes;
1991 }
1992
1993 int _coretpk_installer_apply_privilege(char *pkgid, char *pkgPath, int apiVisibility)
1994 {
1995 #if 0
1996         int ret = 0;
1997         FILE *fp = NULL;
1998         char *find_str = NULL;
1999         char buf[BUF_SIZE] = {0};
2000         char manifest[BUF_SIZE] = {'\0'};
2001         const char *perm[] = {NULL, NULL};
2002         int apptype = PERM_APP_TYPE_EFL;
2003
2004         if (apiVisibility & CERT_SVC_VISIBILITY_PLATFORM) {
2005                 _LOGD("VISIBILITY_PLATFORM!");
2006                 apptype = PERM_APP_TYPE_EFL_PLATFORM;
2007         } else if ((apiVisibility & CERT_SVC_VISIBILITY_PARTNER) ||
2008                         (apiVisibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
2009                         (apiVisibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
2010                 _LOGD("VISIBILITY_PARTNER!");
2011                 apptype = PERM_APP_TYPE_EFL_PARTNER;
2012         }
2013
2014         snprintf(manifest, BUF_SIZE, "%s/%s", pkgPath, CORETPK_XML);
2015         _LOGD("pkgid = [%s], manifest = [%s]", pkgid, manifest);
2016
2017         fp = fopen(manifest, "r");
2018         if (fp == NULL) {
2019                 _LOGE("Fail get : %s\n", manifest);
2020                 return -1;
2021         }
2022
2023         while (fgets(buf, BUF_SIZE, fp) != NULL) {
2024                 __str_trim(buf);
2025
2026                 if (strstr(buf, "<privilege>")) {
2027                         find_str = __getprivilege(buf);
2028                         if (find_str !=  NULL) {
2029                                 _LOGD("privilege = [%s]", find_str);
2030                                 perm[0] = find_str;
2031
2032                                 ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2033                                 if(ret < 0) {
2034                                         _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", pkgid, apptype);
2035                                 } else {
2036                                         _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", pkgid, apptype);
2037                                 }
2038
2039                                 free(find_str);
2040                                 find_str = NULL;
2041                         } else {
2042                                 _LOGD("find_str is null.");
2043                         }
2044                 }
2045
2046                 memset(buf, 0x00, BUF_SIZE);
2047         }
2048
2049         if (fp != NULL)
2050                 fclose(fp);
2051 #endif
2052         return 0;
2053 }
2054
2055 int _coretpk_installer_package_install(char *pkgfile, char *pkgid, char *clientid)
2056 {
2057         int ret = 0;
2058         char buff[BUF_SIZE] = {'\0'};
2059         char manifest[BUF_SIZE] = {'\0'};
2060         char cwd[BUF_SIZE] = {'\0'};
2061         char *temp = NULL;
2062         char rwmanifest[BUF_SIZE] = {'\0'};
2063         int visibility = 0;
2064
2065         /* for external installation */
2066         app2ext_handle *handle = NULL;
2067         GList *dir_list = NULL;
2068         int install_status = APP2EXT_STATUS_SUCCESS;
2069
2070         /*check param*/
2071         if (pkgfile == NULL || pkgid == NULL) {
2072                 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2073                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2074         }
2075
2076         /*send event for start*/
2077         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "install");
2078         _LOGD("[#]start : _coretpk_installer_package_install[%s]", pkgid);
2079
2080         /*send event for install_percent*/
2081         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2082
2083         snprintf(buff, BUF_SIZE, "%s", OPT_USR_APPS);
2084         const char *mkdir_argv[] = { "/bin/mkdir", "-p", buff, NULL };
2085         ret = _ri_xsystem(mkdir_argv);
2086         if (ret != 0) {
2087                 _LOGE("Failed to make usr application dir.");
2088         }
2089
2090         /*If the directory which will be installed exists, remove it.*/
2091         snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2092         if (__is_dir(buff)) {
2093                 _rpm_delete_dir(buff);
2094         }
2095
2096         /* pre_install */
2097         ret = __pre_install_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2098         if (ret < 0) {
2099                 _LOGE("__pre_install_for_mmc is failed.");
2100                 goto err;
2101         }
2102
2103         const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, "-d", buff, NULL };
2104         ret = _ri_xsystem(unzip_argv);
2105         if (ret != 0) {
2106                 _LOGE("failed to unzip for path=[%s], ret=[%d]", buff, ret);
2107                 goto err;
2108         }
2109         _LOGD("unzip is done successfully, path=[%s]", buff);
2110
2111         /*getcwd*/
2112         temp = getcwd(cwd, BUF_SIZE);
2113         if ((temp == NULL) || (cwd[0] == '\0')) {
2114                 _LOGL("getcwd()", errno);
2115                 ret = RPM_INSTALLER_ERR_INTERNAL;
2116                 goto err;
2117         }
2118         _LOGD("current working directory, path=[%s]", cwd);
2119
2120         /*change dir*/
2121         ret = chdir(buff);
2122         if (ret != 0) {
2123                 _LOGL("chdir()", errno);
2124                 ret = RPM_INSTALLER_ERR_INTERNAL;
2125                 goto err;
2126         }
2127
2128         /*check for signature and certificate*/
2129         if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2130                 ret = _coretpk_installer_verify_signatures(buff, pkgid, &visibility);
2131                 if (ret < 0) {
2132                         _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
2133                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2134                         goto err;
2135                 }
2136                 _LOGD("signature and certificate are verified successfully.");
2137         }
2138
2139    /*chdir*/
2140         ret = chdir(cwd);
2141         if (ret != 0) {
2142                 _LOGL("chdir()", errno);
2143                 ret = RPM_INSTALLER_ERR_INTERNAL;
2144                 goto err;
2145         }
2146
2147         /*convert manifest and copy the file to /opt/share/packages*/
2148         snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
2149         ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
2150         if (ret != 0) {
2151                 _LOGE("failed to convert the manifest.");
2152                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2153                 goto err;
2154         }
2155         _LOGD("manifest is converted successfully.");
2156
2157         if (strstr(pkgfile, ".wgt") != NULL) {
2158                 _LOGD("wgt file=[%s]", pkgfile);
2159
2160                 if (strstr(manifest, OPT_USR_APPS)) {
2161                                 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2162                                 const char *rw_xml_category[] = { CORETPK_CATEGORY_CONVERTER, rwmanifest, NULL };
2163                                 ret = _ri_xsystem(rw_xml_category);
2164                 }
2165         }
2166
2167         /*check the manifest file.*/
2168         memset(manifest, '\0', sizeof(manifest));
2169         snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2170         /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
2171         ret = pkgmgr_parser_check_manifest_validation(manifest);
2172         if(ret < 0) {
2173                 _LOGE("invalid manifest file");
2174                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2175                 goto err;
2176         }
2177
2178         /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
2179         ret = pkgmgr_parser_parse_usr_manifest_for_installation(manifest, getuid(), NULL);
2180         if (ret < 0) {
2181                 _LOGE("failed to parse the manifest.");
2182                 ret = RPM_INSTALLER_ERR_INTERNAL;
2183                 goto err;
2184         }
2185         _LOGD("manifest parsing done successfully.");
2186
2187         /*search_ug_app*/
2188         _coretpk_installer_search_ui_gadget(pkgid);
2189
2190         /*send event for install_percent*/
2191         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2192
2193         /*register cert info*/
2194         _ri_register_cert(pkgid);
2195
2196         /*make directory*/
2197         ret = _coretpk_installer_make_directory(pkgid);
2198         if (ret != 0) {
2199                 _LOGE("failed to make the directory.");
2200                 goto err;
2201         }
2202 #if 0
2203         /*apply smack to app dir*/
2204         ret = _coretpk_installer_apply_smack(pkgid, 1);
2205         if (ret != 0) {
2206                 _LOGE("failed to apply the smack.");
2207                 goto err;
2208         }
2209
2210         /*apply smack by privilege*/
2211         ret = _ri_apply_privilege(pkgid, visibility);
2212         if (ret != 0) {
2213                 _LOGE("failed to apply permission, ret=[%d]", ret);
2214         }
2215         _LOGD("permission applying done successfully.");
2216
2217         // Check privilege and visibility
2218         if (privilege_list) {
2219                 ret = _coretpk_installer_verify_privilege_list(privilege_list, visibility);
2220                 if (ret != 0) {
2221                         goto err;
2222                 } else {
2223                         _LOGD("_coretpk_installer_verify_privilege_list done.");
2224                 }
2225         }
2226 #endif
2227 #if 0
2228         /*reload smack*/
2229         ret = _ri_smack_reload(pkgid, REQUEST_TYPE_INSTALL);
2230         if (ret != 0) {
2231                 _LOGD("failed to reload the smack.");
2232         }
2233 #endif
2234
2235         /*send event for install_percent*/
2236         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2237
2238         ret = RPM_INSTALLER_SUCCESS;
2239
2240 err:
2241         /* post_install */
2242         if (ret != 0) {
2243                 install_status = APP2EXT_STATUS_FAILED;
2244         }
2245         _LOGD("install status is [%d].", install_status);
2246         if (__post_install_for_mmc(handle, pkgid, dir_list, install_status)  < 0) {
2247                 _LOGE("__post_install_for_mmc is failed.");
2248                 ret = -1;
2249         }
2250
2251         if (ret == 0) {
2252                 _LOGD("_coretpk_installer_package_install is done.");
2253                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2254         } else {
2255                 /*remove db info*/
2256                 ret = _coretpk_installer_remove_db_info(pkgid);
2257                 if (ret < 0) {
2258                         _LOGE("_coretpk_installer_remove_db_info is failed.");
2259                 }
2260
2261                 /*remove xml(/opt/share/packages/pkgid.xml)*/
2262                 if (access(manifest, F_OK) == 0) {
2263                         (void)remove(manifest);
2264                 }
2265
2266                 /*remove app dir(/opt/usr/apps/pkgid)*/
2267                 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2268                 if (__is_dir(buff)) {
2269                         _rpm_delete_dir(buff);
2270                 }
2271
2272                 /*remove ext app dir(/opt/storage/sdcard/apps/pkgid)*/
2273                 if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
2274                         char extpath[BUF_SIZE] = {'\0'};
2275                         snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
2276                         if (__is_dir(extpath)) {
2277                                 _rpm_delete_dir(extpath);
2278                         }
2279                 }
2280
2281                 char *errorstr = NULL;
2282                 _ri_error_no_to_string(ret, &errorstr);
2283                 _ri_broadcast_status_notification(pkgid, "coretpk", "error", errorstr);
2284                 sleep(2);
2285
2286                 _LOGE("_coretpk_installer_package_install is failed.");
2287                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2288         }
2289
2290         return ret;
2291 }
2292
2293 int _coretpk_installer_package_uninstall(const char *pkgid)
2294 {
2295         int ret = 0;
2296         int update_system = 0;
2297
2298         update_system = __check_updated_system_package(pkgid);
2299
2300         if (update_system == 1) {
2301                 _LOGD("start remove_update, pkgid=[%s]", pkgid);
2302                 ret = __pkg_remove_update(pkgid);
2303         } else {
2304                 _LOGD("start uninstall, pkgid=[%s]", pkgid);
2305                 ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2306         }
2307
2308         if (ret < 0) {
2309                 _LOGE("uninstallation is failed, pkgid=[%s], update_system=[%d]", pkgid, update_system);
2310         } else {
2311                 _LOGD("uninstallation is done successfully, pkgid=[%s]", pkgid);
2312         }
2313
2314         return ret;
2315 }
2316
2317 int _coretpk_installer_package_upgrade(char *pkgfile, char *pkgid, char *clientid)
2318 {
2319         int ret = 0;
2320         char buff[BUF_SIZE] = {'\0'};
2321         char manifest[BUF_SIZE] = { '\0'};
2322         char cwd[BUF_SIZE] = {'\0'};
2323         char rwmanifest[BUF_SIZE] = {'\0'};
2324         pkgmgrinfo_pkginfo_h pkghandle = NULL;
2325         char *temp = NULL;
2326         int visibility = 0;
2327
2328         /* for external upgrade */
2329         app2ext_handle *handle = NULL;
2330         GList *dir_list = NULL;
2331
2332         /*check param*/
2333         if (pkgfile == NULL || pkgid == NULL) {
2334                 _LOGE("invalid input parameter, pkgfile or pkgid is NULL.");
2335                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2336         }
2337
2338         /*send event for start*/
2339         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "update");
2340
2341         /*terminate running app*/
2342         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2343         if (ret < 0) {
2344                 _LOGE("failed to get the pkginfo handle.");
2345                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2346                 goto err;
2347         }
2348         pkgmgrinfo_appinfo_get_list(pkghandle, PMINFO_UI_APP, __ri_check_running_app, NULL);
2349         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2350
2351     /*remove dir for clean*/
2352         __ri_remove_updated_dir(pkgid);
2353
2354         /* pre_upgrade */
2355         ret = __pre_upgrade_for_mmc(pkgid, pkgfile, &dir_list, &handle);
2356         if (ret < 0) {
2357                 _LOGE("__pre_upgrade_for_mmc is failed.");
2358                 goto err;
2359         }
2360
2361         snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, pkgid);
2362         const char *unzip_argv[] = { "/usr/bin/unzip", "-o", pkgfile, "-d", buff, NULL };
2363         ret = _ri_xsystem(unzip_argv);
2364         if (ret != 0) {
2365                 _LOGE("failed to unzip for [%s, %d].", buff, ret);
2366                 goto err;
2367         }
2368         _LOGD("#unzip[%s] success.", buff);
2369
2370         /*send event for install_percent*/
2371         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "30");
2372
2373         /*getcwd*/
2374         temp = getcwd(cwd, BUF_SIZE);
2375         if ((temp == NULL) || (cwd[0] == '\0')) {
2376                 _LOGL("getcwd()", errno);
2377                 ret = RPM_INSTALLER_ERR_INTERNAL;
2378                 goto err;
2379         }
2380         _LOGD("#Current working directory is %s.", cwd);
2381
2382         /*change dir*/
2383         ret = chdir(buff);
2384         if (ret != 0) {
2385                 _LOGL("chdir()", errno);
2386                 ret = RPM_INSTALLER_ERR_INTERNAL;
2387                 goto err;
2388         }
2389
2390         /*check for signature and certificate*/
2391         if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2392                 ret = _coretpk_installer_verify_signatures(buff, pkgid, &visibility);
2393                 if (ret < 0) {
2394                         _LOGE("@Failed to verify signature and certificate[%s].", pkgid);
2395                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2396                         goto err;
2397                 }
2398                 _LOGD("#signature and certificate verifying success");
2399         }
2400
2401    /*chdir*/
2402         ret = chdir(cwd);
2403         if (ret != 0) {
2404                 _LOGL("chdir()", errno);
2405                 ret = RPM_INSTALLER_ERR_INTERNAL;
2406                 goto err;
2407         }
2408
2409         /*convert manifest and copy the file to /opt/share/packages*/
2410         snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, CORETPK_XML);
2411         ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
2412         if (ret != 0) {
2413                 _LOGE("@Failed to convert the manifest.");
2414                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2415                 goto err;
2416         }
2417         _LOGD("#manifest converting success");
2418
2419         if (strstr(pkgfile, ".wgt") != NULL) {
2420                 _LOGD("wgt file = [%s]", pkgfile);
2421
2422                 if (strstr(manifest, OPT_USR_APPS)) {
2423                                 snprintf(rwmanifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2424                                 const char *rw_xml_category[] = { CORETPK_CATEGORY_CONVERTER, rwmanifest, NULL };
2425                                 ret = _ri_xsystem(rw_xml_category);
2426                 }
2427         }
2428
2429         /*check the manifest file.*/
2430         memset(manifest, '\0', sizeof(manifest));
2431         snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2432         /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
2433         ret = pkgmgr_parser_check_manifest_validation(manifest);
2434         if(ret < 0) {
2435                 _LOGE("@invalid manifest file");
2436                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2437                 goto err;
2438         }
2439
2440         /*send event for install_percent*/
2441         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "60");
2442
2443         /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
2444         ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
2445         if (ret < 0) {
2446                 _LOGE("@parsing manifest failed.");
2447                 ret = RPM_INSTALLER_ERR_INTERNAL;
2448                 goto err;
2449         }
2450         _LOGD("#parsing manifest success.");
2451
2452         /*search_ug_app*/
2453         _coretpk_installer_search_ui_gadget(pkgid);
2454
2455         /*unregister cert info*/
2456         _ri_unregister_cert(pkgid);
2457
2458         /*register cert info*/
2459         _ri_register_cert(pkgid);
2460
2461         /*make directory*/
2462         ret = _coretpk_installer_make_directory(pkgid);
2463         if (ret != 0) {
2464                 _LOGE("@Failed to make the directory");
2465                 goto err;
2466         }
2467
2468         // Remove origin rule
2469         _ri_privilege_unregister_package(pkgid);
2470
2471         /*apply smack to app dir*/
2472         ret = _coretpk_installer_apply_smack(pkgid, 1);
2473         if (ret != 0) {
2474                 _LOGE("@Failed to apply the smack.");
2475                 goto err;
2476         }
2477
2478         /*apply smack by privilege*/
2479         ret = _ri_apply_privilege(pkgid, visibility);
2480         if (ret != 0) {
2481                 _LOGE("@Failed to apply permission[%d].", ret);
2482         }
2483         _LOGD("#permission applying success.");
2484
2485         // Check privilege and visibility
2486         if (privilege_list) {
2487                 ret = _coretpk_installer_verify_privilege_list(privilege_list, visibility);
2488                 if (ret != 0) {
2489                         goto err;
2490                 } else {
2491                         _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
2492                 }
2493         }
2494
2495 #if 0
2496         /*reload smack*/
2497         ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
2498         if (ret != 0) {
2499                 _LOGE("@Failed to reload the smack.");
2500         }
2501 #endif
2502
2503         /*send event for install_percent*/
2504         _ri_broadcast_status_notification(pkgid, "coretpk", "install_percent", "100");
2505         ret = RPM_INSTALLER_SUCCESS;
2506
2507 err:
2508         /* post_upgrade */
2509         if (__post_upgrade_for_mmc(handle, pkgid, dir_list) < 0) {
2510                 _LOGE("__post_upgrade_for_mmc is failed.");
2511                 ret = -1;
2512         }
2513
2514         if (ret == 0) {
2515                 _LOGD("[#]end : _coretpk_installer_package_upgrade");
2516                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2517         } else {
2518
2519                 /*TODO:need to add recovery logic*/
2520
2521                 char *errorstr = NULL;
2522                 _ri_error_no_to_string(ret, &errorstr);
2523                 _ri_broadcast_status_notification(pkgid, "coretpk", "error", errorstr);
2524                 sleep(2);
2525
2526                 _LOGE("[@]end : _coretpk_installer_package_upgrade");
2527                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2528         }
2529
2530         return ret;
2531 }
2532
2533 char* _coretpk_installer_get_pkgid_from_directory_path(char *dirpath)
2534 {
2535         char* subpath = strrchr(dirpath, '/');
2536         return subpath + 1;
2537 }
2538
2539 int _coretpk_installer_directory_install(char *dirpath, char *clientid)
2540 {
2541         int ret = 0;
2542         char manifest[BUF_SIZE] = {'\0'};
2543         char cwd[BUF_SIZE] = {'\0'};
2544         char *temp = NULL;
2545         char *pkgid = NULL;
2546         int visibility = 0;
2547
2548         // check param
2549         if (dirpath == NULL) {
2550                 _LOGE("dirpath is NULL.");
2551                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2552         }
2553
2554         _LOGD("directory_install start: dirpath = [%s]", dirpath);
2555
2556         // getcwd
2557         temp = getcwd(cwd, BUF_SIZE);
2558         if ((temp == NULL) || (cwd[0] == '\0')) {
2559                 _LOGE("getcwd() failed. [%d][%s]", errno, strerror(errno));
2560                 ret = RPM_INSTALLER_ERR_INTERNAL;
2561                 goto err;
2562         }
2563
2564         _LOGD("Current working directory is [%s].", cwd);
2565
2566         // change dir
2567         ret = chdir(dirpath);
2568         if (ret != 0) {
2569                 _LOGE("chdir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
2570                 ret = RPM_INSTALLER_ERR_INTERNAL;
2571                 goto err;
2572         }
2573
2574         // check for signature and certificate
2575         if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
2576                 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
2577                 _LOGD("pkgid=[%s]", pkgid);
2578
2579                 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
2580                 if (ret < 0) {
2581                         _LOGE("_coretpk_installer_verify_signatures(%s, %s) failed.", dirpath, pkgid);
2582                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
2583                         goto err;
2584                 }
2585                 _LOGD("verify_signatures(%s, %s) succeed!", dirpath, pkgid);
2586         }
2587
2588   // chdir
2589         ret = chdir(cwd);
2590         if (ret != 0) {
2591                 _LOGE("chdir(%s) failed. [%d][%s]", cwd, errno, strerror(errno));
2592                 ret = RPM_INSTALLER_ERR_INTERNAL;
2593                 goto err;
2594         }
2595
2596         // convert manifest and copy the file to /usr/share/packages
2597         snprintf(manifest, BUF_SIZE, "%s/%s", dirpath, CORETPK_XML);
2598         if (pkgid == NULL) {
2599                 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
2600                 _LOGD("pkgid = [%s]", pkgid);
2601         }
2602
2603         ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
2604         if (ret != 0) {
2605                 _LOGE("_coretpk_installer_convert_manifest() failed. manifest = [%s], pkgid = [%s]", manifest, pkgid);
2606                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2607                 goto err;
2608         }
2609         _LOGD("convert_manifest(%s, %s) succeed!", manifest, pkgid);
2610
2611         // check the manifest file
2612         memset(manifest, '\0', sizeof(manifest));
2613         if (strstr(dirpath, OPT_USR_APPS)) {
2614                 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2615         } else {
2616                 snprintf(manifest, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2617         }
2618         _LOGD("manifest = [%s]", manifest);
2619
2620         // compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)
2621         ret = pkgmgr_parser_check_manifest_validation(manifest);
2622         if(ret < 0) {
2623                 _LOGE("pkgmgr_parser_check_manifest_validation(%s) failed.", manifest);
2624                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2625                 goto err;
2626         }
2627
2628         // Parse the manifest to get install location and size. If installation fails, remove manifest info from DB
2629         ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2630         if (ret < 0) {
2631                 _LOGE("pkgmgr_parser_parse_manifest_for_installation(%s) failed.", manifest);
2632                 ret = RPM_INSTALLER_ERR_INTERNAL;
2633                 goto err;
2634         }
2635         _LOGD("parse_manifest(%s) succeed!", manifest);
2636
2637         // register cert info
2638         _ri_register_cert(pkgid);
2639
2640         // make directory
2641         ret = _coretpk_installer_make_directory(pkgid);
2642         if (ret != 0) {
2643                 _LOGE("_coretpk_installer_make_directory(%s) failed.", pkgid);
2644                 goto err;
2645         }
2646         _LOGD("make_directory(%s) succeed!", pkgid);
2647
2648         // apply smack to app dir
2649         ret = _coretpk_installer_apply_smack(pkgid, 1);
2650         if (ret != 0) {
2651                 _LOGE("_coretpk_installer_apply_smack(%s) failed.", pkgid);
2652         }
2653         _LOGD("apply_smack(%s) succeed!", pkgid);
2654
2655         // apply smack by privilege
2656         ret = _ri_apply_privilege(pkgid, visibility);
2657         if (ret != 0) {
2658                 _LOGE("_ri_apply_privilege(%s, %d) failed. ret = [%d]", pkgid, visibility, ret);
2659         }
2660         _LOGD("apply_privilege(%s, %d) succeed!", pkgid, visibility);
2661
2662         ret = RPM_INSTALLER_SUCCESS;
2663
2664 err:
2665         _LOGD("directory_install end: dirpath = [%s], ret = [%d]", dirpath, ret);
2666
2667         return ret;
2668 }
2669
2670 int _coretpk_installer_prepare_package_install(char *pkgfile, char *clientid)
2671 {
2672         int ret = 0;
2673         pkginfo *info = NULL;
2674         pkginfo *dbinfo = NULL;
2675         char *pkgid = NULL;
2676
2677         _LOGD("start");
2678
2679         info = _coretpk_installer_get_pkgfile_info(pkgfile);
2680         if (info == NULL || (strlen(info->package_name) == 0)) {
2681                 _LOGE("failed to get the pkg info.");
2682                 ret = RPM_INSTALLER_ERR_INTERNAL;
2683                 goto err;
2684         }
2685
2686         pkgid = strdup(info->package_name);
2687         if (pkgid == NULL) {
2688                 _LOGE("strdup() failed.");
2689                 ret = RPM_INSTALLER_ERR_INTERNAL;
2690                 goto err;
2691         }
2692
2693         dbinfo = _rpm_installer_get_pkgname_info(info->package_name);
2694
2695         if (dbinfo == NULL) {
2696                 /*package is not installed. Go for installation.*/
2697                 _LOGD("start to install");
2698                 ret = _coretpk_installer_package_install(pkgfile, pkgid, clientid);
2699         } else if (strcmp(info->version, dbinfo->version) > 0) {
2700                 /*upgrade */
2701                 _LOGD("start to upgrade");
2702                 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2703         } else if (strcmp(info->version, dbinfo->version) < 0) {
2704                 /*downgrade*/
2705                 _LOGD("start to downgrade");
2706                 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2707         } else {
2708                 /*same package. Reinstall it. Manifest should be parsed again */
2709                 _LOGD("start to reinstall");
2710                 ret = _coretpk_installer_package_upgrade(pkgfile, info->package_name, clientid);
2711         }
2712
2713         if (ret != 0) {
2714                 _LOGE("result=[%d]", ret);
2715         } else {
2716                 _LOGD("success");
2717         }
2718
2719         if (info) {
2720                 free(info);
2721                 info = NULL;
2722         }
2723         if (dbinfo) {
2724                 free(dbinfo);
2725                 dbinfo = NULL;
2726         }
2727
2728         if (pkgid) {
2729                 free(pkgid);
2730                 pkgid = NULL;
2731         }
2732
2733         return ret;
2734
2735 err:
2736         if (info) {
2737                 free(info);
2738                 info = NULL;
2739         }
2740
2741         _ri_broadcast_status_notification("Invalid package", "invalid", "start", "install");
2742         _ri_broadcast_status_notification("Invalid package", "invalid", "end", "fail");
2743
2744         return ret;
2745 }
2746
2747 int _coretpk_installer_prepare_package_uninstall(const char *pkgid)
2748 {
2749         if (pkgid == NULL) {
2750                 _LOGE("pkgid is NULL.");
2751                 return RPM_INSTALLER_ERR_WRONG_PARAM;
2752         }
2753
2754         _LOGD("pkgid=[%s]", pkgid);
2755
2756         int ret = 0;
2757         pkginfo *dbinfo = NULL;
2758
2759         dbinfo = _rpm_installer_get_pkgname_info(pkgid);
2760         if (dbinfo == NULL) {
2761                 _LOGE("[%s] is not installed.", pkgid);
2762                 return RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
2763         }
2764
2765         ret = _coretpk_installer_package_uninstall(pkgid);
2766         if (ret != 0) {
2767                 _LOGE("_coretpk_installer_package_uninstall() failed, pkgid=[%s], ret=[%d]", pkgid, ret);
2768         } else {
2769                 _LOGD("_coretpk_installer_package_uninstall() is done successfully, pkgid=[%s]", pkgid);
2770         }
2771
2772         if (dbinfo) {
2773                 free(dbinfo);
2774                 dbinfo = NULL;
2775         }
2776
2777         return ret;
2778 }
2779
2780 int _coretpk_installer_prepare_directory_install(char *dirpath, char *clientid)
2781 {
2782         int ret = 0;
2783
2784         ret = _coretpk_installer_directory_install(dirpath, clientid);
2785         _LOGD("path=[%s], result=[%d]", dirpath, ret);
2786
2787         return ret;
2788 }
2789
2790 int _coretpk_installer_package_move(char* pkgid, int move_type)
2791 {
2792 #if 0
2793         app2ext_handle *hdl = NULL;
2794         int ret = 0;
2795         int movetype = -1;
2796         GList *dir_list = NULL;
2797         pkgmgrinfo_pkginfo_h pkghandle = NULL;
2798
2799         _ri_broadcast_status_notification(pkgid, "coretpk", "start", "move");
2800         _LOGD("[#]start : _coretpk_installer_package_move[%s][%d]", pkgid, move_type);
2801
2802         if (move_type == PM_MOVE_TO_INTERNAL) {
2803                 movetype = APP2EXT_MOVE_TO_PHONE;
2804         } else if (move_type == PM_MOVE_TO_SDCARD) {
2805                 movetype = APP2EXT_MOVE_TO_EXT;
2806         } else {
2807                 ret = RPM_INSTALLER_ERR_WRONG_PARAM;
2808                 goto err;
2809         }
2810
2811         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2812         if (ret < 0) {
2813                 _LOGE("@Failed to get the pkginfo handle.");
2814                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
2815                 goto err;
2816         }
2817
2818         /* Terminate the running instance of app */
2819         pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
2820         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2821
2822         hdl = app2ext_init(APP2EXT_SD_CARD);
2823         if ((hdl != NULL) && (hdl->interface.move != NULL)) {
2824                 dir_list = __rpm_populate_dir_list();
2825                 if (dir_list == NULL) {
2826                         _LOGE("@Failed to get the populate directory.");
2827                         ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
2828                         goto err;
2829                 }
2830
2831                 ret = hdl->interface.move(pkgid, dir_list, movetype);
2832                 __rpm_clear_dir_list(dir_list);
2833                 if (ret != 0) {
2834                         _LOGE("@Failed to move app.");
2835                         ret = RPM_INSTALLER_ERR_INTERNAL;
2836                         goto err;
2837                 } else {
2838                         if(move_type == PM_MOVE_TO_INTERNAL) {
2839                                 _LOGD("#updating the installed storage from external to internal");
2840                                 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
2841                         } else {
2842                                 _LOGD("#updating the installed storage from internal to external");
2843                                 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
2844                         }
2845
2846                         if (ret != PMINFO_R_OK) {
2847                                 _LOGE("@Failed to udpate the installed storage.");
2848                                 ret = RPM_INSTALLER_ERR_INTERNAL;
2849                                 goto err;
2850                         }
2851                 }
2852
2853         } else {
2854                 _LOGE("@Failed to get app2ext handle.");
2855                 ret = RPM_INSTALLER_ERR_INTERNAL;
2856         }
2857
2858 err:
2859         if (hdl != NULL) {
2860                 app2ext_deinit(hdl);
2861         }
2862
2863         if (ret == 0) {
2864                 _LOGD("[#]end : _coretpk_installer_package_move");
2865                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "ok");
2866         } else {
2867                 _LOGE("[@]end : _coretpk_installer_package_move");
2868                 _ri_broadcast_status_notification(pkgid, "coretpk", "end", "fail");
2869         }
2870
2871         return ret;
2872 #endif
2873         return 0;
2874 }
2875
2876 int _coretpk_installer_copy_file( const char *src_path, const char *dst_path)
2877 {
2878         int ret = 0;
2879         FILE *src, *dst;
2880         int rc = 0;
2881         unsigned char temp_buf[8192] = {'\0',};
2882         size_t size_of_uchar = sizeof(unsigned char);
2883         size_t size_of_temp_buf = sizeof(temp_buf);
2884
2885     src = fopen(src_path, "r");
2886     if (src == NULL) {
2887                 _LOGE("@Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
2888         return  -1;
2889     }
2890
2891     dst = fopen(dst_path, "w");
2892     if (dst == NULL) {
2893         /*No such file or directory*/
2894         if (errno == ENOENT) {
2895                 /*make the path of parent dir for the data*/
2896                         char *path = strdup(dst_path);
2897                         char *p = strrchr(path, '/');
2898                         if (p) {
2899                                 p++;
2900                         } else {
2901                                 ret = -1;
2902                                 free(path);
2903                                 goto err;
2904                         }
2905                         int idx = strlen(path) - strlen(p);
2906                         path[idx] = '\0';
2907
2908                         /*make the parent dir*/
2909                         const char *mkdir_argv[] = { "/bin/mkdir", "-p", path, NULL };
2910                         ret = _ri_xsystem(mkdir_argv);
2911                         if (ret != 0) {
2912                                 _LOGE("Failed to make parent dir.");
2913                         }
2914
2915                         _LOGD("#[%s] is created.", path);
2916                         free(path);
2917
2918                         /*open the file*/
2919                         dst = fopen(dst_path, "w");
2920                         if (dst == NULL) {
2921                                 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
2922                                 ret = -1;
2923                                 goto err;
2924                         }
2925         } else {
2926                         _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
2927                         ret = -1;
2928                         goto err;
2929         }
2930     }
2931
2932     while (!feof(src)) {
2933         rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
2934         fwrite( temp_buf, size_of_uchar, rc, dst);
2935     }
2936
2937  err:
2938          if (src) {
2939                  fclose(src);
2940          }
2941          if (dst) {
2942                  fclose(dst);
2943          }
2944
2945     return  ret;
2946 }
2947
2948 int _coretpk_installer_handle_rds_data(char *pkgid, GList *delete, GList *add, GList *modify, int *updatexml)
2949 {
2950         int ret = 0;
2951         GList *list = NULL;
2952         char handledata[BUF_SIZE] = {'\0'};
2953         char srcfile[BUF_SIZE] = {'\0'};
2954         char destfile[BUF_SIZE] = {'\0'};
2955
2956         /*delete*/
2957         if (delete != NULL) {
2958                 list = g_list_first(delete);
2959                 while (list) {
2960                         char *data = (char *)list->data;
2961                         if (!strcasestr(data, RDS_DELTA_DELETE)) {
2962                                 snprintf(handledata, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
2963
2964                                 const char *delete_argv[] = { "/bin/rm", "-rf", handledata, NULL };
2965                                 ret = _ri_xsystem(delete_argv);
2966                                 if (ret == 0) {
2967                                         _LOGD("#[delete] success : %s", data);
2968                                 } else {
2969                                         _LOGD("#[delete] fail : %s", data);
2970                                 }
2971                                 memset(handledata, '\0', sizeof(handledata));
2972                         }
2973
2974                         list = g_list_next(list);
2975                 }
2976         } else {
2977                 _LOGD("#There is no deleted data.");
2978         }
2979
2980         /*add*/
2981         if (add != NULL) {
2982                 list = g_list_first(add);
2983                 while (list) {
2984                         char *data = (char *)list->data;
2985                         if (!strcasestr(data, RDS_DELTA_ADD)) {
2986                                 snprintf(srcfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, data);
2987                                 snprintf(destfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
2988
2989                                 if (__is_dir((char *)srcfile)) {
2990                                         const char *mkdir_argv[] = { "/bin/mkdir", "-p", destfile, NULL };
2991                                         _ri_xsystem(mkdir_argv);
2992                                         _LOGD("#[%s] is created.", destfile);
2993                                 } else {
2994                                         ret =_coretpk_installer_copy_file(srcfile, destfile);
2995                                         if (ret == 0) {
2996                                                 _LOGD("#[add] success : %s", data);
2997                                         } else {
2998                                                 _LOGD("#[add] fail : %s", data);
2999                                         }
3000                                 }
3001                                 memset(srcfile, '\0', sizeof(srcfile));
3002                                 memset(destfile, '\0', sizeof(destfile));
3003                         }
3004
3005                         list = g_list_next(list);
3006                 }
3007         } else {
3008                 _LOGD("#There is no added data.");
3009         }
3010
3011         /*modify*/
3012         if (modify != NULL) {
3013                 list = g_list_first(modify);
3014                 while (list) {
3015                         char *data = (char *)list->data;
3016                         if (!strcasestr(data, RDS_DELTA_MODIFY)) {
3017                                 /*If XML is modified, the checking codes for xml has to be executed.*/
3018                                 if (strcmp(data, CORETPK_XML) == 0) {
3019                                         *updatexml = 1;
3020                                 }
3021
3022                                 snprintf(srcfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, data);
3023                                 snprintf(destfile, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, data);
3024
3025                                 ret =_coretpk_installer_copy_file(srcfile, destfile);
3026                                 if (ret == 0) {
3027                                         _LOGD("#[modify] success : %s", data);
3028                                 } else {
3029                                         _LOGD("#[modify] fail : %s", data);
3030                                 }
3031
3032                                 memset(srcfile, '\0', sizeof(srcfile));
3033                                 memset(destfile, '\0', sizeof(destfile));
3034                         }
3035                         list = g_list_next(list);
3036                 }
3037         } else {
3038                 _LOGD("#There is no modified data.");
3039         }
3040
3041         return ret;
3042 }
3043
3044 int _coretpk_installer_read_rds_file(char *pkgid, char *rdsfile, int *updatexml)
3045 {
3046         int ret = 0;
3047         int state = RDS_STATE_NONE;
3048
3049         char buffer[BUF_SIZE] = {'\0'};
3050         FILE *fi = NULL;
3051
3052         GList *delete_list = NULL;
3053         GList *add_list = NULL;
3054         GList *modify_list = NULL;
3055
3056         if (access(rdsfile, F_OK) != 0) {
3057                 _LOGL("access()", errno);
3058                 return -1;
3059         }
3060
3061         fi = fopen(rdsfile, "r");
3062         if (fi == NULL) {
3063                 _LOGL("fopen()", errno);
3064                 return -1;
3065         }
3066
3067         while (fgets(buffer, BUF_SIZE, fi) != NULL) {
3068                 buffer[strlen(buffer) - 1] = '\0';
3069
3070                 /*check rds state*/
3071                 if (buffer[0] == '#') {
3072                         if (strcasestr(buffer, RDS_DELTA_DELETE)) {
3073                                 state = RDS_STATE_DELETE;
3074                         } else if (strcasestr(buffer, RDS_DELTA_ADD)) {
3075                                 state = RDS_STATE_ADD;
3076                         } else if (strcasestr(buffer, RDS_DELTA_MODIFY)) {
3077                                 state = RDS_STATE_MODIFY;
3078                         } else {
3079                                 state = RDS_STATE_NONE;
3080                         }
3081                 }
3082
3083                 if (state == RDS_STATE_NONE) {
3084                         _LOGE("Unknown RDS State, INSTALLER_RDS_STATE_NONE");
3085                         continue;
3086                 }
3087
3088                 /*make rds data list*/
3089                 switch (state) {
3090                         case RDS_STATE_DELETE:
3091                                 _LOGD("RDS_STATE_DELETE data : %s", buffer);
3092                                 delete_list = g_list_append(delete_list, g_strdup(buffer));
3093                                 break;
3094
3095                         case RDS_STATE_ADD:
3096                                 _LOGD("RDS_STATE_ADD data : %s", buffer);
3097                                 add_list = g_list_append(add_list, g_strdup(buffer));
3098                                 break;
3099
3100                         case RDS_STATE_MODIFY:
3101                                 _LOGD("RDS_STATE_MODIFY data : %s", buffer);
3102                                 modify_list = g_list_append(modify_list, g_strdup(buffer));
3103                                 break;
3104                 }
3105         }
3106
3107         ret = _coretpk_installer_handle_rds_data(pkgid, delete_list, add_list, modify_list, updatexml);
3108         if (ret != 0) {
3109                 _LOGE("@Failed to handle rds data.");
3110         }
3111
3112         if (delete_list != NULL) {
3113                 g_list_free(delete_list);
3114         }
3115         if (add_list != NULL) {
3116                 g_list_free(add_list);
3117         }
3118         if (modify_list != NULL) {
3119                 g_list_free(modify_list);
3120         }
3121
3122         fclose(fi);
3123         return ret;
3124 }
3125
3126 int _coretpk_installer_package_reinstall(char *pkgid, char *clientid)
3127 {
3128         int ret = 0;
3129         char manifest[BUF_SIZE] = {'\0'};
3130         char rdsfile[BUF_SIZE] = {'\0'};
3131         char dirpath[BUF_SIZE] = {'\0'};
3132         char cwd[BUF_SIZE] = {'\0'};
3133         char *temp = NULL;
3134         int updatexml = 0;
3135         int visibility = 0;
3136
3137         /*check param*/
3138         if (pkgid == NULL) {
3139                 _LOGE("@The input param[pkgid] is NULL.");
3140                 return RPM_INSTALLER_ERR_WRONG_PARAM;
3141         }
3142
3143         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "start", "update");
3144         _LOGD("[#]start : _coretpk_installer_package_reinstall[%s]", pkgid);
3145
3146         snprintf(rdsfile, BUF_SIZE, "%s/tmp/%s/%s", OPT_USR_APPS, pkgid, RDS_DELTA_FILE);
3147         ret = _coretpk_installer_read_rds_file(pkgid, rdsfile, &updatexml);
3148         if (ret != 0) {
3149                 _LOGE("@Failed to read the rds file.");
3150                 ret = RPM_INSTALLER_ERR_INTERNAL;
3151                 goto err;
3152         }
3153         _LOGD("#RDS file reading success");
3154
3155         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "30");
3156
3157         /*getcwd*/
3158         temp = getcwd(cwd, BUF_SIZE);
3159         if ((temp == NULL) || (cwd[0] == '\0')) {
3160                 _LOGL("getcwd()", errno);
3161                 ret = RPM_INSTALLER_ERR_INTERNAL;
3162                 goto err;
3163         }
3164         _LOGD("#Current working directory is %s.", cwd);
3165
3166         /*change dir*/
3167         snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3168         ret = chdir(dirpath);
3169         if (ret != 0) {
3170                 _LOGL("chdir()", errno);
3171                 ret = RPM_INSTALLER_ERR_INTERNAL;
3172                 goto err;
3173         }
3174
3175         /*check for signature and certificate*/
3176         if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
3177
3178                 pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
3179                 _LOGD("pkgid[%s]", pkgid);
3180
3181                 ret = _coretpk_installer_verify_signatures(dirpath, pkgid, &visibility);
3182                 if (ret < 0) {
3183                         _LOGE("failed to verify signature and certificate, pkgid=[%s].", pkgid);
3184                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3185                         goto err;
3186                 }
3187                 _LOGD("signature and certificate verifying success");
3188         }
3189
3190    /*chdir*/
3191         ret = chdir(cwd);
3192         if (ret != 0) {
3193                 _LOGL("chdir()", errno);
3194                 ret = RPM_INSTALLER_ERR_INTERNAL;
3195                 goto err;
3196         }
3197
3198         if (updatexml) {
3199                 /*convert manifest and copy the file to /opt/share/packages*/
3200                 snprintf(manifest, BUF_SIZE, "%s/%s", dirpath, CORETPK_XML);
3201                 if (pkgid == NULL) {
3202                         pkgid = _coretpk_installer_get_pkgid_from_directory_path(dirpath);
3203                         _LOGD("pkgid[%s]", pkgid);
3204                 }
3205                 ret = _coretpk_installer_convert_manifest(manifest, pkgid, clientid);
3206                 if (ret != 0) {
3207                         _LOGE("@Failed to convert the manifest.");
3208                         ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3209                         goto err;
3210                 }
3211                 _LOGD("#manifest converting success");
3212
3213                 /*check the manifest file.*/
3214                 memset(manifest, '\0', sizeof(manifest));
3215                 snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
3216                 /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
3217                 ret = pkgmgr_parser_check_manifest_validation(manifest);
3218                 if(ret < 0) {
3219                         _LOGE("@invalid manifest file");
3220                         ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3221                         goto err;
3222                 }
3223
3224                 /*Parse the manifest to get install location and size. If failed, remove manifest info from DB.*/
3225                 ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
3226                 if (ret < 0) {
3227                         _LOGE("@Failed to parse the manifest.");
3228                         ret = RPM_INSTALLER_ERR_INTERNAL;
3229                         goto err;
3230                 }
3231                 _LOGD("#manifest parsing success");
3232         }
3233
3234         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "60");
3235
3236         /*register cert info*/
3237         _ri_register_cert(pkgid);
3238
3239         /*make directory*/
3240         ret = _coretpk_installer_make_directory(pkgid);
3241         if (ret != 0) {
3242                 _LOGE("@Failed to make directory");
3243                 goto err;
3244         }
3245
3246         _ri_privilege_unregister_package(pkgid);
3247
3248         /*apply smack to app dir*/
3249         ret = _coretpk_installer_apply_smack(pkgid, 1);
3250         if (ret != 0) {
3251                 _LOGE("@Failed to apply smack.");
3252                 goto err;
3253         }
3254
3255         /*apply smack by privilege*/
3256         ret = _ri_apply_privilege(pkgid, visibility);
3257         if (ret != 0) {
3258                 _LOGE("@Failed to apply permission[%d].", ret);
3259         }
3260         _LOGD("#permission applying success.");
3261
3262         // Check privilege and visibility
3263         if (privilege_list) {
3264                 ret = _coretpk_installer_verify_privilege_list(privilege_list, visibility);
3265                 if (ret != 0) {
3266                         goto err;
3267                 } else {
3268                         _LOGD("_coretpk_installer_verify_privilege_list(PRVMGR_PACKAGE_TYPE_CORE) is ok.");
3269                 }
3270         }
3271
3272 #if 0
3273         /*reload smack*/
3274         ret = _ri_smack_reload(pkgid, REQUEST_TYPE_UPGRADE);
3275         if (ret != 0) {
3276                 _LOGE("@Failed to reload the smack.");
3277         }
3278 #endif
3279
3280         pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "install_percent", "100");
3281         ret = RPM_INSTALLER_SUCCESS;
3282
3283 err:
3284         if (ret == 0) {
3285                 _LOGD("[#]end : _coretpk_installer_package_reinstall");
3286                 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "ok");
3287         } else {
3288                 /*remove db info*/
3289                 ret = _coretpk_installer_remove_db_info(pkgid);
3290                 if (ret < 0) {
3291                         _LOGE("_coretpk_installer_remove_db_info is failed.");
3292                 }
3293
3294                 /*remove xml(/opt/share/packages/pkgid.xml)*/
3295                 if (access(manifest, F_OK) == 0) {
3296                         (void)remove(manifest);
3297                 }
3298
3299                 /*remove app dir(/opt/usr/apps/pkgid)*/
3300                 if (__is_dir(dirpath)) {
3301                         _rpm_delete_dir(dirpath);
3302                 }
3303
3304                 _LOGE("[@]end : _coretpk_installer_package_reinstall");
3305                 pkgmgr_installer_send_signal(pi, "coretpk", pkgid, "end", "fail");
3306         }
3307
3308         return ret;
3309 }
3310
3311 int _coretpk_installer_csc_install(char *path_str, char *remove_str)
3312 {
3313         int ret = 0;
3314         pkginfo *info = NULL;
3315         char buff[BUF_SIZE] = {'\0'};
3316         char manifest[BUF_SIZE] = {'\0'};
3317         char cwd[BUF_SIZE] = {'\0'};
3318         char *temp = NULL;
3319         char *csc_tags[3] = {NULL, };
3320         int visibility = 0;
3321
3322         /*check param*/
3323         if (path_str == NULL || remove_str == NULL) {
3324                 _LOGE("@The input param[pkgfile or pkgid] is NULL.");
3325                 return RPM_INSTALLER_ERR_WRONG_PARAM;
3326         }
3327
3328         _LOGD("[##]csc-core : start csc_install[path=%s]", path_str);
3329
3330         info = _coretpk_installer_get_pkgfile_info(path_str);
3331         if (info == NULL || (strlen(info->package_name) == 0)) {
3332                 _LOGE("[@@]end : _coretpk_installer_prepare_package_install: failed to get the pkg info.");
3333                 ret = RPM_INSTALLER_ERR_INTERNAL;
3334                 goto err;
3335         }
3336
3337         _LOGD("[##]csc-core : get pkgid [%s]", info->package_name);
3338
3339         /*If the directory which will be installed exists, remove it.*/
3340         snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, info->package_name);
3341         if (__is_dir(buff)) {
3342                 _rpm_delete_dir(buff);
3343         }
3344
3345         _LOGD("[##]csc-core : real path [%s]", buff);
3346
3347         const char *unzip_argv[] = { "/usr/bin/unzip", "-o", path_str, "-d", buff, NULL };
3348         ret = _ri_xsystem(unzip_argv);
3349         if (ret != 0) {
3350                 _LOGE("@Failed to unzip for [%s, %d].", buff, ret);
3351                 goto err;
3352         }
3353
3354         _LOGD("[##]csc-core : unzip success[%s]", buff);
3355
3356         /*getcwd*/
3357         temp = getcwd(cwd, BUF_SIZE);
3358         if ((temp == NULL) || (cwd[0] == '\0')) {
3359                 _LOGL("getcwd()", errno);
3360                 ret = RPM_INSTALLER_ERR_INTERNAL;
3361                 goto err;
3362         }
3363
3364         /*change dir*/
3365         ret = chdir(buff);
3366         if (ret != 0) {
3367                 _LOGL("chdir()", errno);
3368                 ret = RPM_INSTALLER_ERR_INTERNAL;
3369                 goto err;
3370         }
3371
3372         _LOGD("[##]csc-core : check signature");
3373
3374         /*check for signature and certificate*/
3375         if (_coretpk_installer_get_configuration_value(INI_VALUE_SIGNATURE)) {
3376                 ret = _coretpk_installer_verify_signatures(buff, info->package_name, &visibility);
3377                 if (ret < 0) {
3378                         _LOGE("@Failed to verify signature and certificate[%s].", info->package_name);
3379                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
3380                         goto err;
3381                 }
3382                 _LOGD("[##]csc-core : signature verify success[%s]", buff);
3383         }
3384
3385    /*chdir*/
3386         ret = chdir(cwd);
3387         if (ret != 0) {
3388                 _LOGL("chdir()", errno);
3389                 ret = RPM_INSTALLER_ERR_INTERNAL;
3390                 goto err;
3391         }
3392
3393         /*convert manifest and copy the file to /opt/share/packages*/
3394         snprintf(manifest, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, info->package_name, CORETPK_XML);
3395         ret = _coretpk_installer_convert_manifest(manifest, info->package_name, NULL);
3396         if (ret != 0) {
3397                 _LOGE("@Failed to convert the manifest.");
3398                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3399                 goto err;
3400         }
3401
3402         _LOGD("[##]csc-core : manifest converting success");
3403
3404         /*check the manifest file.*/
3405         memset(manifest, '\0', sizeof(manifest));
3406         snprintf(manifest, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, info->package_name);
3407         /*compare manifest.xml with schema file(/usr/etc/package-manager/preload/manifest.xsd)*/
3408         ret = pkgmgr_parser_check_manifest_validation(manifest);
3409         if(ret < 0) {
3410                 _LOGE("@invalid manifest file");
3411                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3412                 goto err;
3413         }
3414
3415         _LOGD("[##]csc-core : manifest validation success");
3416
3417         /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
3418         if (strcmp(remove_str,"true")==0)
3419                 csc_tags[0] = "removable=true";
3420         else
3421                 csc_tags[0] = "removable=false";
3422
3423         csc_tags[1] = "preload=true";
3424         csc_tags[2] = NULL;
3425
3426         ret = pkgmgr_parser_parse_manifest_for_installation(manifest, csc_tags);
3427         if (ret < 0) {
3428                 _LOGE("@Failed to parse the manifest.");
3429                 ret = RPM_INSTALLER_ERR_INTERNAL;
3430                 goto err;
3431         }
3432
3433         _LOGD("[##]csc-core : register manifest success");
3434
3435         /*register cert info*/
3436         _ri_register_cert(info->package_name);
3437
3438         /*make directory*/
3439         ret = _coretpk_installer_make_directory(info->package_name);
3440         if (ret != 0) {
3441                 _LOGE("@Failed to make the directory");
3442                 goto err;
3443         }
3444
3445         _LOGD("[##]csc-core : make directory success");
3446
3447         /*apply smack to app dir*/
3448         ret = _coretpk_installer_apply_smack(info->package_name, 1);
3449         if (ret != 0) {
3450                 _LOGE("@Failed to apply the smack.");
3451                 goto err;
3452         }
3453
3454         _LOGD("[##]csc-core : apply_smack success");
3455
3456         /*apply smack by privilege*/
3457         ret = _ri_apply_privilege(info->package_name, visibility);
3458         if (ret != 0) {
3459                 _LOGE("@Failed to apply permission[%d].", ret);
3460         }
3461
3462         _LOGD("[##]csc-core : apply_privilege success");
3463
3464 #if 0
3465         /*reload smack*/
3466         ret = _ri_smack_reload(info->package_name, REQUEST_TYPE_INSTALL);
3467         if (ret != 0) {
3468                 _LOGD("@Failed to reload the smack.");
3469         }
3470 #endif
3471
3472         _LOGD("[##]csc-core : smack_reload success");
3473
3474         ret = RPM_INSTALLER_SUCCESS;
3475
3476 err:
3477         if (ret == 0) {
3478                 _LOGD("[##]csc-core : finish csc core success");
3479         } else {
3480                 /*remove xml(/opt/share/packages/pkgid.xml)*/
3481                 if (access(manifest, F_OK) == 0) {
3482                         (void)remove(manifest);
3483                 }
3484
3485                 /*remove app dir(/opt/usr/apps/pkgid)*/
3486                 snprintf(buff, BUF_SIZE, "%s/%s/", OPT_USR_APPS, info->package_name);
3487                 if (__is_dir(buff)) {
3488                         _rpm_delete_dir(buff);
3489                 }
3490                 _LOGD("[##]csc-core : finish csc core fail");
3491
3492         }
3493
3494         if (info) {
3495                 free(info);
3496                 info = NULL;
3497         }
3498
3499         return ret;
3500 }