tizen 2.3.1 release
[framework/base/rpm-installer.git] / backend / src / rpm / rpm-installer.c
1 /*
2  * rpm-installer
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25 #endif
26 #define __USE_GNU
27 #include <sys/wait.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <wait.h>
32 #include <stdio.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <dirent.h>
38 #include <ctype.h>              /* for isspace () */
39 #include <wctype.h>             /* for towlower() */
40 #include <vconf.h>
41 #include <cert-service.h>
42 #include <libxml/parser.h>
43 #include <libxml/tree.h>
44 #include <libxml/xmlmemory.h>
45 #include <sqlite3.h>
46 #include <db-util.h>
47 #include <sys/xattr.h>
48
49 #ifndef XMLSEC_NO_XSLT
50 #include <libxslt/xslt.h>
51 #include <libxslt/security.h>
52 #endif
53
54 #include <xmlsec/xmlsec.h>
55 #include <xmlsec/xmltree.h>
56 #include <xmlsec/xmldsig.h>
57 #include <xmlsec/crypto.h>
58 #include <xmlsec/errors.h>
59 #include <pkgmgr-info.h>
60 #include <pkgmgr_parser.h>
61 #include <package-manager.h>
62 #include <privilege-control.h>
63 #include <app_manager.h>
64 #include <aul.h>
65 #include <dlfcn.h>
66 #define APP2EXT_ENABLE
67 #ifdef APP2EXT_ENABLE
68 #include <app2ext_interface.h>
69 #endif
70
71 #include "rpm-installer-util.h"
72 #include "rpm-installer-signature.h"
73 #include "rpm-installer.h"
74 #include "rpm-frontend.h"
75 #include "rpm-installer-type.h"
76 #include "coretpk-installer-internal.h"
77
78
79 enum rpm_sig_type {
80         SIG_AUTH,
81         SIG_DIST1,
82         SIG_DIST2,
83 };
84
85 enum rpm_sig_sub_type {
86         SIG_SIGNER,
87         SIG_INTERMEDIATE,
88         SIG_ROOT,
89 };
90
91 typedef struct cert_chain_t {
92         int cert_type;
93         char *cert_value;
94 } cert_chain;
95
96 cert_chain list[MAX_CERT_NUM];
97 GList *privilege_list;
98
99 #define PKGMGR_DB               "/opt/dbspace/.pkgmgr_parser.db"
100 #define EXT_APPDATA_PRIVILEGE_NAME      "http://tizen.org/privilege/externalstorage.appdata"
101
102 extern char *gpkgname;
103 extern int sig_enable;
104
105 static void __rpm_process_line(char *line);
106 static void __rpm_perform_read(int fd);
107 static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca);
108 static xmlSecKeysMngrPtr __ri_load_trusted_certs(char *files, int files_size);
109 static int __ri_verify_file(xmlSecKeysMngrPtr mngr, const char *sigxmlfile);
110 static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value);
111 static void __ri_free_cert_chain(void);
112 static char *__ri_get_cert_from_file(const char *file);
113 static int __privilege_func(const char *name, void *user_data);
114 static char *__ri_get_str(const char* str, const char* pKey);
115 static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
116                                                                         const char* errorObject, const char* errorSubject, int reason, const char* msg);
117
118 int _ri_set_group_id(const char *pkgid, const char *groupid);
119
120 static void __str_trim(char *input)
121 {
122         char *trim_str = input;
123
124         if (input == NULL)
125                 return;
126
127         while (*input != 0) {
128                 if (!isspace(*input)) {
129                         *trim_str = *input;
130                         trim_str++;
131                 }
132                 input++;
133         }
134
135         *trim_str = 0;
136         return;
137 }
138
139 static int __ri_check_pkgid_for_deactivation(const char *pkgid)
140 {
141         FILE *fp = NULL;
142         char deactivation_str[FILENAME_MAX] = { 0 };
143         char *deactivation_pkgid = NULL;
144         char *deactivation_state = NULL;
145
146         fp = fopen(DEACTIVATION_PKGID_LIST, "r");
147         if (fp == NULL) {
148                 _LOGE("fopen fail\n");
149                 return 0;
150         }
151
152         while (fgets(deactivation_str, sizeof(deactivation_str), fp) != NULL) {
153                 __str_trim(deactivation_str);
154
155                 deactivation_pkgid = __ri_get_str(deactivation_str, TOKEN_PKGID_STR);
156                 if(deactivation_pkgid == NULL)
157                         continue;
158
159                 deactivation_state = __ri_get_str(deactivation_str, TOKEN_STATE_STR);
160                 if(deactivation_state == NULL) {
161                         free(deactivation_pkgid);
162                         continue;
163                 }
164
165                 if ((strcmp(deactivation_pkgid, pkgid) == 0) && (strcmp(deactivation_state, "off") == 0)) {
166                         fclose(fp);
167                         free(deactivation_pkgid);
168                         free(deactivation_state);
169                         _LOGE("Find pkgid[%s] form deactivation list.\n", pkgid);
170                         return -1;
171                 }
172
173                 free(deactivation_pkgid);
174                 free(deactivation_state);
175                 memset(deactivation_str, 0x00, sizeof(deactivation_str));
176         }
177
178         if (fp != NULL)
179                 fclose(fp);
180
181         return 0;
182
183 }
184
185 static int __ri_get_op_type(char *op_str)
186 {
187         if (strcmp(op_str,"install")==0)
188                 return INSTALL_REQ;
189         else if (strcmp(op_str,"update")==0)
190                 return UPGRADE_REQ;
191         else if (strcmp(op_str,"uninstall")==0)
192                 return UNINSTALL_REQ;
193         else
194                 return -1;
195 }
196
197 static char *__ri_get_str(const char* str, const char* pKey)
198 {
199         const char* p = NULL;
200         const char* pStart = NULL;
201         const char* pEnd = NULL;
202
203         if (str == NULL)
204                 return NULL;
205
206         char *pBuf = strdup(str);
207         if(!pBuf){
208                 _LOGE("Malloc failed !");
209                 return NULL;
210         }
211
212         p = strstr(pBuf, pKey);
213         if (p == NULL){
214                 if(pBuf){
215                         free(pBuf);
216                         pBuf = NULL;
217                 }
218                 return NULL;
219         }
220
221         pStart = p + strlen(pKey);
222         pEnd = strchr(pStart, SEPERATOR_END);
223         if (pEnd == NULL){
224                 if(pBuf){
225                         free(pBuf);
226                         pBuf = NULL;
227                 }
228                 return NULL;
229         }
230         size_t len = pEnd - pStart;
231         if (len <= 0){
232                 if(pBuf){
233                         free(pBuf);
234                         pBuf = NULL;
235                 }
236                 return NULL;
237         }
238         char *pRes = (char*)malloc(len + 1);
239         if(!pRes){
240                 if(pBuf){
241                         free(pBuf);
242                         pBuf = NULL;
243                 }
244                 _LOGE("Malloc failed!");
245                 return NULL;
246         }
247         strncpy(pRes, pStart, len);
248         pRes[len] = 0;
249
250         if(pBuf){
251                 free(pBuf);
252                 pBuf = NULL;
253         }
254
255         return pRes;
256 }
257
258 static int __ri_init_csc_xml(char *xml_path, char *removable)
259 {
260         int ret = 0;
261         char* csc_tags[3] = {NULL, };
262
263         if (strcmp(removable,"true")==0)
264                 csc_tags[0] = "removable=true";
265         else
266                 csc_tags[0] = "removable=false";
267
268         csc_tags[1] = "preload=true";
269         csc_tags[2] = NULL;
270
271         ret = pkgmgr_parser_parse_manifest_for_installation(xml_path, csc_tags);
272
273         return ret;
274 }
275
276 static int __ri_create_cert_chain(int sigtype, int sigsubtype, char *value)
277 {
278         if (value == NULL)
279                 return -1;
280         // _LOGD("Push in list [%d] [%d] [%s]", sigtype, sigsubtype, value);
281         switch (sigtype) {
282         case SIG_AUTH:
283                 switch (sigsubtype) {
284                 case SIG_SIGNER:
285                         list[PMINFO_SET_AUTHOR_SIGNER_CERT].cert_type = PMINFO_SET_AUTHOR_SIGNER_CERT;
286                         list[PMINFO_SET_AUTHOR_SIGNER_CERT].cert_value = strdup(value);
287                         break;
288                 case SIG_INTERMEDIATE:
289                         list[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT].cert_type = PMINFO_SET_AUTHOR_INTERMEDIATE_CERT;
290                         list[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT].cert_value = strdup(value);
291                         break;
292                 case SIG_ROOT:
293                         /*value is already a mallocd pointer*/
294                         list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_type = PMINFO_SET_AUTHOR_ROOT_CERT;
295                         list[PMINFO_SET_AUTHOR_ROOT_CERT].cert_value = value;
296                         break;
297                 default:
298                         break;
299                 }
300                 break;
301         case SIG_DIST1:
302                 switch (sigsubtype) {
303                 case SIG_SIGNER:
304                         list[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_SIGNER_CERT;
305                         list[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT].cert_value = strdup(value);
306                         break;
307                 case SIG_INTERMEDIATE:
308                         list[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT;
309                         list[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT].cert_value = strdup(value);
310                         break;
311                 case SIG_ROOT:
312                         /*value is already a mallocd pointer*/
313                         list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR_ROOT_CERT;
314                         list[PMINFO_SET_DISTRIBUTOR_ROOT_CERT].cert_value = value;
315                         break;
316                 default:
317                         break;
318                 }
319                 break;
320         case SIG_DIST2:
321                 switch (sigsubtype) {
322                 case SIG_SIGNER:
323                         list[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT;
324                         list[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT].cert_value = strdup(value);
325                         break;
326                 case SIG_INTERMEDIATE:
327                         list[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT;
328                         list[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT].cert_value = strdup(value);
329                         break;
330                 case SIG_ROOT:
331                         /*value is already a mallocd pointer*/
332                         list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_type = PMINFO_SET_DISTRIBUTOR2_ROOT_CERT;
333                         list[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT].cert_value = value;
334                         break;
335                 default:
336                         break;
337                 }
338                 break;
339         default:
340                 break;
341         }
342         return 0;
343 }
344
345 static void __ri_free_cert_chain()
346 {
347         int i = 0;
348         for (i = 0; i < MAX_CERT_NUM; i++) {
349                 if (list[i].cert_value)
350                         free(list[i].cert_value);
351         }
352 }
353
354 static void __ri_xmlsec_debug_print(const char* file, int line, const char* func,
355                                    const char* errorObject, const char* errorSubject, int reason, const char* msg)
356 {
357         char total[BUF_SIZE];
358         snprintf(total, sizeof(total), "[%s(%d)] : [%s] : [%s] : [%s]", func, line, errorObject, errorSubject, msg);
359         if(reason != 256) {
360                 fprintf(stderr, "## [validate error]: %s\n", total);
361                 _LOGE("%s",total);
362         } else {
363                 _LOGE("%s",total);
364         }
365 }
366
367 static int __ri_verify_file(xmlSecKeysMngrPtr sec_key_mngr, const char *sigxmlfile)
368 {
369         xmlDocPtr doc = NULL;
370         xmlNodePtr node = NULL;
371         xmlSecDSigCtxPtr dsigCtx = NULL;
372         int res = -1;
373         if (sigxmlfile == NULL)
374                 return -1;
375         if (sec_key_mngr == NULL)
376                 return -1;
377
378         /* set error callback to xmlsec1 */
379         xmlSecErrorsSetCallback(__ri_xmlsec_debug_print);
380
381         /* load file */
382         doc = xmlParseFile(sigxmlfile);
383         if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)) {
384                 _LOGE("unable to parse file \"%s\"\n", sigxmlfile);
385                 goto err;
386         }
387         /* find start node */
388         node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeSignature, xmlSecDSigNs);
389         if (node == NULL) {
390                 _LOGE("start node not found in \"%s\"\n", sigxmlfile);
391                 goto err;
392         }
393         /* create signature context */
394         dsigCtx = xmlSecDSigCtxCreate(sec_key_mngr);
395         if (dsigCtx == NULL) {
396                 _LOGE("failed to create signature context\n");
397                 goto err;
398         }
399         /* Verify signature */
400         if (xmlSecDSigCtxVerify(dsigCtx, node) < 0) {
401                 _LOGE("failed to verify signature\n");
402                 goto err;
403         }
404         /* print verification result to stdout */
405         if (dsigCtx->status == xmlSecDSigStatusSucceeded) {
406                 res = 0;
407                 _LOGD("valid signature");
408         } else {
409                 res = -1;
410                 _LOGD("invalid signature");
411         }
412
413 err:
414         /* cleanup */
415         if(dsigCtx != NULL) {
416                 xmlSecDSigCtxDestroy(dsigCtx);
417         }
418         if(doc != NULL) {
419                 xmlFreeDoc(doc);
420         }
421         return res;
422 }
423
424 static xmlSecKeysMngrPtr __ri_load_trusted_certs(char *files, int files_size)
425 {
426         xmlSecKeysMngrPtr sec_key_mngr;
427         if (files == NULL)
428                 return NULL;
429         if (files_size < 0)
430                 return NULL;
431         sec_key_mngr = xmlSecKeysMngrCreate();
432         if (sec_key_mngr == NULL) {
433                 _LOGE("failed to create keys manager.\n");
434                 return NULL;
435         }
436         if (xmlSecCryptoAppDefaultKeysMngrInit(sec_key_mngr) < 0) {
437                 _LOGE("failed to initialize keys manager.\n");
438                 xmlSecKeysMngrDestroy(sec_key_mngr);
439                 return NULL;
440         }
441         /* load trusted cert */
442         if (xmlSecCryptoAppKeysMngrCertLoad(sec_key_mngr, files, xmlSecKeyDataFormatPem, xmlSecKeyDataTypeTrusted) < 0) {
443                 _LOGE("failed to load pem certificate from \"%s\"\n", files);
444                 xmlSecKeysMngrDestroy(sec_key_mngr);
445                 return NULL;
446         }
447         return sec_key_mngr;
448 }
449
450 static int __ri_xmlsec_verify_signature(const char *sigxmlfile, char *rootca)
451 {
452         int ret = 0;
453         xmlSecKeysMngrPtr sec_key_mngr = NULL;
454         xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
455         xmlSubstituteEntitiesDefault(1);
456
457 #ifndef XMLSEC_NO_XSLT
458         xmlIndentTreeOutput = 1;
459         xsltSecurityPrefsPtr sec_prefs = xsltNewSecurityPrefs();
460         xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_WRITE_FILE, xsltSecurityForbid);
461         xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_READ_FILE, xsltSecurityForbid);
462         xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_CREATE_DIRECTORY, xsltSecurityForbid);
463         xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_WRITE_NETWORK, xsltSecurityForbid);
464         xsltSetSecurityPrefs(sec_prefs,  XSLT_SECPREF_READ_NETWORK, xsltSecurityForbid);
465         xsltSetDefaultSecurityPrefs(sec_prefs);
466 #endif
467
468         ret = xmlSecInit();
469         if (ret < 0) {
470                 _LOGE("xmlsec initialization failed [%d]\n", ret);
471                 goto end;
472         }
473         ret = xmlSecCheckVersion();
474         if (ret != 1) {
475                 _LOGE("Incompatible version of loaded xmlsec library [%d]\n", ret);
476                 goto end;
477         }
478 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
479         ret = xmlSecCryptoDLLoadLibrary(BAD_CAST "openssl");
480         if (ret < 0) {
481                 _LOGE("unable to load openssl library [%d]\n", ret);
482                 goto end;
483         }
484 #endif
485
486         ret = xmlSecCryptoAppInit(NULL);
487         if (ret < 0) {
488                 _LOGE("crypto initialization failed [%d]\n", ret);
489                 goto end;
490         }
491         ret = xmlSecCryptoInit();
492         if (ret < 0) {
493                 _LOGE("xmlsec-crypto initialization failed [%d]\n", ret);
494                 goto end;
495         }
496
497         sec_key_mngr = __ri_load_trusted_certs(rootca, 1);
498         if (sec_key_mngr == NULL) {
499                 _LOGE("loading of trusted certs failed\n");
500                 ret = -1;
501                 goto end;
502         }
503
504         if (__ri_verify_file(sec_key_mngr, sigxmlfile) < 0) {
505                 ret = -1;
506         }
507
508 end:
509         if (sec_key_mngr)
510                 xmlSecKeysMngrDestroy(sec_key_mngr);
511         xmlSecCryptoShutdown();
512         xmlSecCryptoAppShutdown();
513         xmlSecShutdown();
514 #ifndef XMLSEC_NO_XSLT
515         xsltFreeSecurityPrefs(sec_prefs);
516         xsltCleanupGlobals();
517 #endif
518         return ret;
519 }
520
521 int _rpm_installer_get_group_id(char *pkgid, char **result)
522 {
523         int ret = 0;
524         const char *value = NULL;
525         char author_signature[BUF_SIZE] = {'\0'};
526         char *e_rootcert = NULL;
527         char *d_rootcert = NULL;
528         gsize d_size = 0;
529         unsigned char hashout[BUF_SIZE] = {'\0'};
530         unsigned int h_size = 0;
531         int e_size = 0;
532         int length = 0;
533         pkgmgrinfo_certinfo_h handle = NULL;
534
535         snprintf(author_signature, BUF_SIZE, "%s/%s/%s", USR_APPS, pkgid, AUTHOR_SIGNATURE_XML);
536         if (access(author_signature, F_OK) != 0) {
537                 _LOGE("[%s] isn't existed.", author_signature);
538                 return -1;
539         }
540
541         ret = pkgmgrinfo_pkginfo_create_certinfo(&handle);
542         if (ret < 0) {
543                 _LOGE("pkgmgrinfo_pkginfo_create_certinfo(%s) failed.", pkgid);
544                 goto err;
545         }
546
547         ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, handle);
548         if (ret < 0) {
549                 _LOGE("pkgmgrinfo_pkginfo_load_certinfo(%s) failed.", pkgid);
550                 goto err;
551         }
552
553         // get root certificate
554         ret = pkgmgrinfo_pkginfo_get_cert_value(handle, PMINFO_AUTHOR_SIGNER_CERT, &value);
555         if (ret < 0 || value == NULL) {
556                 _LOGE("pkgmgrinfo_pkginfo_get_cert_value(%s) failed. [%d]", pkgid, ret);
557                 goto err;
558         }
559
560         // decode cert
561         d_rootcert = (char *)g_base64_decode(value, &d_size);
562         if (d_rootcert == NULL) {
563                 _LOGE("g_base64_decode() failed.");
564                 goto err;
565         }
566         _LOGD("g_base64_decode() succeed, d_size=[%d]", d_size);
567
568         // hash
569         EVP_Digest(d_rootcert, d_size, hashout, &h_size, EVP_sha1(), NULL);
570         if (h_size <= 0) {
571                 _LOGE("EVP_Digest(hash) failed.");
572                 goto err;
573         }
574         _LOGD("EVP_Digest() succeed, h_size=[%d]", h_size);
575
576         // encode cert
577         e_rootcert = g_base64_encode((const guchar *)hashout, h_size);
578         if (e_rootcert == NULL) {
579                 _LOGE("g_base64_encode() failed.");
580                 goto err;
581         }
582         e_size = strlen(e_rootcert);
583         _LOGD("g_base64_encode() succeed, e_size=[%d]", e_size);
584
585         // replace / to #
586         for (length = e_size; length >= 0; --length) {
587                 if (e_rootcert[length] == '/') {
588                         e_rootcert[length] = '#';
589                 }
590         }
591
592         *result = e_rootcert;
593
594 err:
595         if (d_rootcert) {
596                 free(d_rootcert);
597         }
598
599         // destroy cert
600         if (handle) {
601                 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
602         }
603
604         return ret;
605 }
606
607 void __rpm_apply_smack(char *pkgname, int flag)
608 {
609         int ret = -1;
610         char dirpath[BUF_SIZE] = {'\0'};
611         char* groupid = NULL;
612         char *shared_data_label = NULL;
613
614         /*execute privilege APIs. The APIs should not fail*/
615         _ri_privilege_register_package(pkgname);
616
617         /*home dir. Dont setup path but change smack access to "_" */
618         snprintf(dirpath, BUF_SIZE, "%s/%s", USR_APPS, pkgname);
619         if (__is_dir(dirpath))
620                 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
621         memset(dirpath, '\0', BUF_SIZE);
622         snprintf(dirpath, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgname);
623         if (__is_dir(dirpath))
624                 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
625
626         // data
627         memset(dirpath, '\0', BUF_SIZE);
628         snprintf(dirpath, BUF_SIZE, "%s/%s/data", OPT_USR_APPS, pkgname);
629         if (__is_dir(dirpath))
630                 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
631
632         // cache
633         memset(dirpath, '\0', BUF_SIZE);
634         snprintf(dirpath, BUF_SIZE, "%s/%s/cache", OPT_USR_APPS, pkgname);
635         if (!__is_dir(dirpath)) {
636                 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
637                 if (ret < 0) {
638                         _LOGE("directory making is failed.\n");
639                 }
640         }
641         ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
642         if(ret != 0){
643                 _LOGE("chown failed!! [%s]",strerror(errno));
644         }
645         _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_PRIVATE, pkgname);
646
647         //shared
648         memset(dirpath, '\0', BUF_SIZE);
649         snprintf(dirpath, BUF_SIZE, "%s/%s/shared", USR_APPS, pkgname);
650         if (__is_dir(dirpath))
651                 _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
652         memset(dirpath, '\0', BUF_SIZE);
653         snprintf(dirpath, BUF_SIZE, "%s/%s/shared", OPT_USR_APPS, pkgname);
654         if (!__is_dir(dirpath))
655                 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
656                 if (ret < 0) {
657                         _LOGE("directory making is failed.\n");
658                 }
659         _ri_privilege_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
660         memset(dirpath, '\0', BUF_SIZE);
661
662         //shared/res
663         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", USR_APPS, pkgname);
664         if (__is_dir(dirpath))
665                 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
666         memset(dirpath, '\0', BUF_SIZE);
667
668         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/res", OPT_USR_APPS, pkgname);
669         if (__is_dir(dirpath))
670                 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_ANY_LABEL, "_");
671         memset(dirpath, '\0', BUF_SIZE);
672
673         //shared/data
674         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", USR_APPS, pkgname);
675         if (__is_dir(dirpath)) {
676                 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
677                 if(ret != 0){
678                         _LOGE("chown failed!! [%s]",strerror(errno));
679                 }
680                 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
681         }
682         memset(dirpath, '\0', BUF_SIZE);
683
684         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/data", OPT_USR_APPS, pkgname);
685         if (__is_dir(dirpath)) {
686                 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
687                 if(ret != 0){
688                         _LOGE("chown failed!! [%s]",strerror(errno));
689                 }
690                 _ri_privilege_setup_path(pkgname, dirpath, PERM_APP_PATH_PUBLIC, NULL);
691         }
692
693         // shared/cache
694         ret = _coretpk_installer_get_smack_label_access(dirpath, &shared_data_label);
695         if (ret != 0) {
696                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
697         }
698         memset(dirpath, '\0', BUF_SIZE);
699         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/cache", OPT_USR_APPS, pkgname);
700         if (!__is_dir(dirpath)) {
701                 ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
702                 if (ret < 0) {
703                         _LOGE("directory making is failed.\n");
704                 }
705         }
706         ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
707         if(ret != 0){
708                 _LOGE("chown failed!! [%s]",strerror(errno));
709         }
710         ret  = _coretpk_installer_set_smack_label_access(dirpath, shared_data_label);
711         if (ret != 0) {
712                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
713         }
714         ret = _coretpk_installer_set_smack_label_transmute(dirpath, "1");
715         if (ret != 0) {
716                 _LOGE("_coretpk_installer_apply_directory_policy() failed, appdir=[%s], ret=[%d]", dirpath, ret);
717         }
718
719         /*/shared/trusted/*/
720         memset(dirpath, '\0', BUF_SIZE);
721         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", USR_APPS, pkgname);
722         if (__is_dir(dirpath)) {
723                 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
724                 if(ret != 0){
725                         _LOGE("chown failed!! [%s]",strerror(errno));
726                 }
727
728                 ret = _rpm_installer_get_group_id(pkgname, &groupid);
729                 if (ret == 0) {
730                         _LOGD("group id for trusted directory is [%s]", groupid);
731                         _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
732                         FREE_AND_NULL(groupid);
733                 }
734         }
735         memset(dirpath, '\0', BUF_SIZE);
736
737         snprintf(dirpath, BUF_SIZE, "%s/%s/shared/trusted", OPT_USR_APPS, pkgname);
738
739         _LOGD("dirpath [%s]", dirpath);
740
741         ret = _rpm_installer_get_group_id(pkgname, &groupid);
742         if (ret == 0) {
743                 if (__is_dir(dirpath) != 1) {
744                         _LOGE("dont have [%s]", dirpath);
745
746                         ret = mkdir(dirpath, DIRECTORY_PERMISSION_755);
747                         if (ret == -1 && errno != EEXIST) {
748                                 _LOGE("mkdir failed!! [%s]",strerror(errno));
749                         }
750                 }
751
752                 ret = chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
753                 if(ret != 0){
754                         _LOGE("chown failed!! [%s]",strerror(errno));
755                 }
756
757                 _LOGD("group id for trusted directory is [%s]", groupid);
758                 _ri_privilege_setup_path(pkgname, dirpath, APP_PATH_GROUP_RW, groupid);
759                 _ri_set_group_id(pkgname, groupid);
760
761                 FREE_AND_NULL(groupid);
762         }
763 }
764
765 int __is_dir(char *dirname)
766 {
767         struct stat stFileInfo;
768         if(dirname == NULL) {
769                 _LOGE("dirname is null\n");
770                 return -1;
771         }
772
773         (void)stat(dirname, &stFileInfo);
774
775         if (S_ISDIR(stFileInfo.st_mode)) {
776                 return 1;
777         }
778         return 0;
779 }
780
781 static void __rpm_process_line(char *line)
782 {
783         char *tok = NULL;
784         tok = strtok(line, " ");
785         if (tok) {
786                 if (!strncmp(tok, "%%", 2)) {
787                         tok = strtok(NULL, " ");
788                         if (tok) {
789                                 _LOGD("Install percentage is %s\n",
790                                        tok);
791                                 _ri_broadcast_status_notification(gpkgname,
792                                                                   "rpm",
793                                                                   "install_percent",
794                                                                   tok);
795                                 _ri_stat_cb(gpkgname, "install_percent", tok);
796                         }
797                         return;
798                 }
799         }
800         return;
801 }
802
803 static void __rpm_perform_read(int fd)
804 {
805         char *buf_ptr = NULL;
806         char *tmp_ptr = NULL;
807         int size = 0;
808         static char buffer[BUF_SIZE] = { 0, };
809         static int buffer_position;
810
811         size = read(fd, &buffer[buffer_position],
812                     sizeof(buffer) - buffer_position);
813         buffer_position += size;
814         if (size <= 0)
815                 return;
816
817         /* Process each line of the recieved buffer */
818         buf_ptr = tmp_ptr = buffer;
819         while ((tmp_ptr = (char *)memchr(buf_ptr, '\n',
820                                          buffer + buffer_position - buf_ptr)) !=
821                NULL) {
822                 *tmp_ptr = 0;
823                 __rpm_process_line(buf_ptr);
824                 /* move to next line and continue */
825                 buf_ptr = tmp_ptr + 1;
826         }
827
828         /*move the remaining bits at the start of the buffer
829            and update the buffer position */
830         buf_ptr = (char *)memrchr(buffer, 0, buffer_position);
831         if (buf_ptr == NULL)
832                 return;
833
834         /* we have processed till the last \n which has now become
835            0x0. So we increase the pointer to next position */
836         buf_ptr++;
837
838         memmove(buffer, buf_ptr, buf_ptr - buffer);
839         buffer_position = buffer + buffer_position - buf_ptr;
840 }
841
842
843 int _rpm_xsystem(const char *argv[])
844 {
845         int err = 0;
846         int status = 0;
847         pid_t pid;
848         int pipefd[2];
849         int result = 0;
850         int fd = 0;
851
852         if (pipe(pipefd) == -1) {
853                 _LOGE("pipe creation failed\n");
854                 return -1;
855         }
856         /*Read progress info via pipe */
857         pid = fork();
858
859         switch (pid) {
860         case -1:
861                 _LOGE("fork failed\n");
862                 return -1;
863         case 0:
864                 /* child */
865                 {
866                         close(pipefd[0]);
867                         close(1);
868                         close(2);
869                         fd = dup(pipefd[1]);
870                         if (fd < 0) {
871                                 _LOGE("dup failed\n");
872                                 _exit(100);
873                         }
874
875                         result = dup(pipefd[1]);
876                         if (result < 0) {
877                                 _LOGE("dup failed\n");
878                                 _exit(100);
879                         }
880
881                         if (execvp(argv[0], (char *const *)argv) == -1) {
882                                 _LOGE("execvp failed\n");
883                         }
884                         _exit(100);
885                 }
886         default:
887                 /* parent */
888                 break;
889         }
890
891         close(pipefd[1]);
892
893         while ((err = waitpid(pid, &status, WNOHANG)) != pid) {
894                 if (err < 0) {
895                         if (errno == EINTR)
896                                 continue;
897                         _LOGE("waitpid failed\n");
898                         close(pipefd[0]);
899                         return -1;
900                 }
901
902                 int select_ret;
903                 fd_set rfds;
904                 struct timespec tv;
905                 FD_ZERO(&rfds);
906                 FD_SET(pipefd[0], &rfds);
907                 tv.tv_sec = 1;
908                 tv.tv_nsec = 0;
909                 select_ret =
910                     pselect(pipefd[0] + 1, &rfds, NULL, NULL, &tv, NULL);
911                 if (select_ret == 0)
912                         continue;
913
914                 else if (select_ret < 0 && errno == EINTR)
915                         continue;
916                 else if (select_ret < 0) {
917                         _LOGE("select() returned error\n");
918                         continue;
919                 }
920                 if (FD_ISSET(pipefd[0], &rfds))
921                         __rpm_perform_read(pipefd[0]);
922         }
923
924         close(pipefd[0]);
925         /* Check for an error code. */
926         if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) {
927
928                 if (WIFSIGNALED(status) != 0 && WTERMSIG(status) == SIGSEGV) {
929                         printf
930                             ("Sub-process %s received a segmentation fault. \n",
931                              argv[0]);
932                 } else if (WIFEXITED(status) != 0) {
933                         printf("Sub-process %s returned an error code (%u)\n",
934                                argv[0], WEXITSTATUS(status));
935                 } else {
936                         printf("Sub-process %s exited unexpectedly\n", argv[0]);
937                 }
938         }
939         return WEXITSTATUS(status);
940 }
941
942 void __rpm_clear_dir_list(GList* dir_list)
943 {
944         GList *list = NULL;
945         app2ext_dir_details* dir_detail = NULL;
946         if (dir_list) {
947                 list = g_list_first(dir_list);
948                 while (list) {
949                         dir_detail = (app2ext_dir_details *)list->data;
950                         if (dir_detail && dir_detail->name) {
951                                 free(dir_detail->name);
952                         }
953                         g_free(list->data);
954                         list = g_list_next(list);
955                 }
956                 g_list_free(dir_list);
957         }
958 }
959
960 GList * __rpm_populate_dir_list()
961 {
962         GList *dir_list = NULL;
963         GList *list = NULL;
964         app2ext_dir_details* dir_detail = NULL;
965         int i;
966         char pkg_ro_content_rpm[3][5] = { "bin", "res", "lib" };
967
968
969         for (i=0; i<3; i++) {
970                 dir_detail = (app2ext_dir_details*) calloc(1, sizeof(app2ext_dir_details));
971                 if (dir_detail == NULL) {
972                         printf("\nMemory allocation failed\n");
973                         goto FINISH_OFF;
974                 }
975                 dir_detail->name = (char*) calloc(1, sizeof(char)*(strlen(pkg_ro_content_rpm[i])+2));
976                 if (dir_detail->name == NULL) {
977                         printf("\nMemory allocation failed\n");
978                         free(dir_detail);
979                         goto FINISH_OFF;
980                 }
981                 snprintf(dir_detail->name, (strlen(pkg_ro_content_rpm[i])+1), "%s", pkg_ro_content_rpm[i]);
982                 dir_detail->type = APP2EXT_DIR_RO;
983                 dir_list = g_list_append(dir_list, dir_detail);
984         }
985         if (dir_list) {
986                 list = g_list_first(dir_list);
987                 while (list) {
988                         dir_detail = (app2ext_dir_details *)list->data;
989                         list = g_list_next(list);
990                 }
991         }
992         return dir_list;
993 FINISH_OFF:
994         if (dir_list) {
995                 list = g_list_first(dir_list);
996                 while (list) {
997                         dir_detail = (app2ext_dir_details *)list->data;
998                         if (dir_detail && dir_detail->name) {
999                                 free(dir_detail->name);
1000                         }
1001                         g_free(list->data);
1002                         list = g_list_next(list);
1003                 }
1004                 g_list_free(dir_list);
1005         }
1006         return NULL;
1007 }
1008
1009 static char *__ri_get_cert_from_file(const char *file)
1010 {
1011         FILE *fp_cert = NULL;
1012         int certlen = 0;
1013         char *certbuf = NULL;
1014         char *startcert = NULL;
1015         char *endcert = NULL;
1016         int certwrite = 0;
1017         char *cert = NULL;
1018         int i = 0;
1019         int ch = 0;
1020         int error = 0;
1021
1022         if(!(fp_cert = fopen(file, "r"))) {
1023                 _LOGE("[ERR][%s] Fail to open file, [%s]\n", __func__, file);
1024                 return NULL;
1025         }
1026
1027         fseek(fp_cert, 0L, SEEK_END);
1028
1029         certlen = ftell(fp_cert);
1030         if (certlen < 0) {
1031                 _LOGE("[ERR][%s] Fail to find EOF\n", __func__);
1032                 error = 1;
1033                 goto err;
1034         }
1035
1036         fseek(fp_cert, 0L, SEEK_SET);
1037
1038         if(!(certbuf = (char*)malloc(sizeof(char) * (int)certlen))) {
1039                 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1040                 error = 1;
1041                 goto err;
1042         }
1043         memset(certbuf, 0x00, (int)certlen);
1044
1045         i = 0;
1046         while((ch = fgetc(fp_cert)) != EOF) {
1047                 if(ch != '\n') {
1048                         certbuf[i] = ch;
1049                         i++;
1050                 }
1051         }
1052         certbuf[i] = '\0';
1053
1054         startcert = strstr(certbuf, "-----BEGIN CERTIFICATE-----") + strlen("-----BEGIN CERTIFICATE-----");
1055         endcert = strstr(certbuf, "-----END CERTIFICATE-----");
1056         certwrite = (int)endcert - (int)startcert;
1057
1058         cert = (char*)malloc(sizeof(char) * (certwrite+2));
1059         if (cert == NULL) {
1060                 _LOGE("[ERR][%s] Fail to allocate memory\n", __func__);
1061                 error = 1;
1062                 goto err;
1063         }
1064         memset(cert, 0x00, certwrite+2);
1065         snprintf(cert, certwrite+1, "%s", startcert);
1066         _LOGD("Root CA, len=[%d]\n%s", certwrite, cert);
1067
1068 err:
1069         if (certbuf)
1070                 free(certbuf);
1071         fclose(fp_cert);
1072         if (error)
1073                 return NULL;
1074         else
1075                 return cert;
1076 }
1077
1078 static int __privilege_func(const char *name, void *user_data)
1079 {
1080         int ret = 0;
1081         const char *perm[] = {NULL, NULL};
1082         perm[0] = name;
1083         int apptype = PERM_APP_TYPE_EFL;
1084         privilegeinfo *info = (privilegeinfo*)user_data;
1085
1086         _LOGD("package_id = [%s], privilege = [%s]", info->package_id, name);
1087         privilege_list = g_list_append(privilege_list, strdup((char*)name));
1088
1089         if (info->visibility & CERT_SVC_VISIBILITY_PLATFORM) {
1090                 _LOGD("VISIBILITY_PLATFORM!");
1091                 apptype = PERM_APP_TYPE_EFL_PLATFORM;
1092         } else if ((info->visibility & CERT_SVC_VISIBILITY_PARTNER) ||
1093                         (info->visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
1094                         (info->visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
1095                 _LOGD("VISIBILITY_PARTNER!");
1096                 apptype = PERM_APP_TYPE_EFL_PARTNER;
1097         }
1098
1099         ret = _ri_privilege_enable_permissions((char *)info->package_id, apptype, perm, 1);
1100         if(ret < 0) {
1101                 _LOGE("_ri_privilege_enable_permissions(%s, %d) failed.", (char *)info->package_id, apptype);
1102                 return -1;
1103         }
1104
1105         if (strcmp(name, EXT_APPDATA_PRIVILEGE_NAME) == 0) {
1106                 _LOGD("it is EXT_APPDATA_PRIVILEGE_NAME");
1107                 if(_coretpk_installer_make_directory_for_ext((char *)info->package_id) < 0) {
1108                         _LOGE("make_directory_for_ext failed.");
1109                 }
1110         }
1111
1112         _LOGD("_ri_privilege_enable_permissions(%s, %d) succeed.", (char *)info->package_id, apptype);
1113         return ret;
1114 }
1115
1116 char *__strlwr(char *str)
1117 {
1118         int i = 0;
1119
1120         while(*(str+i) != '\0'){
1121                 if(*(str+i) >= 65 || *(str+i)<= 90) {
1122                         *(str+i) = towlower(*(str+i));
1123                 }
1124                 i++;
1125         }
1126         return str;
1127 }
1128
1129 static int __ri_find_xml(char *pkgid, char **rpm_xml)
1130 {
1131         DIR *dir;
1132         struct dirent entry, *result;
1133         int ret = 0;
1134         char buf[BUF_SIZE];
1135
1136         dir = opendir(USR_SHARE_PACKAGES);
1137         if (!dir) {
1138                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
1139                         _LOGE("fota-info : Failed to access the [%s] because %s", USR_SHARE_PACKAGES, buf);
1140                 return -1;
1141         }
1142
1143         for (ret = readdir_r(dir, &entry, &result);
1144                         ret == 0 && result != NULL;
1145                         ret = readdir_r(dir, &entry, &result)) {
1146                 char *manifest = NULL;
1147
1148                 if (entry.d_name[0] == '.') continue;
1149
1150                 manifest = _manifest_to_package(entry.d_name);
1151                 if (!manifest) {
1152                         _LOGE("fota-info : Failed to convert file to xml[%s]", entry.d_name);
1153                         continue;
1154                 }
1155
1156                 if (strstr(manifest, __strlwr(pkgid))) {
1157                         snprintf(buf, sizeof(buf), "%s/%s", USR_SHARE_PACKAGES, manifest);
1158                         _LOGD("fota-info : pkgid[%s] find xml[%s]\n", pkgid, buf);
1159                         *rpm_xml = strdup(buf);
1160                         closedir(dir);
1161                         free(manifest);
1162                         return 0;
1163                 }
1164
1165                 free(manifest);
1166         }
1167
1168         _LOGE("fota-info : Failed to find xml for pkgid[%s]", pkgid);
1169         closedir(dir);
1170         return 0;
1171 }
1172
1173 static int __ri_install_fota(char *pkgid)
1174 {
1175         int ret = 0;
1176         char *manifest = NULL;
1177         char *temp[] = {"fota=true", NULL};
1178
1179         _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
1180
1181         /*if pkgid is one of deactivation, it does not need to install*/
1182         ret = __ri_check_pkgid_for_deactivation(pkgid);
1183         if (ret < 0) {
1184                 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1185                 return ret;
1186         }
1187
1188         /*get manifest*/
1189         manifest = pkgmgr_parser_get_manifest_file(pkgid);
1190         if (manifest == NULL) {
1191                 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1192                 ret = -1;
1193                 goto end;
1194         }
1195         _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1196
1197         ret = pkgmgr_parser_parse_manifest_for_installation(manifest, temp);
1198         if (ret < 0) {
1199                 _LOGE("fota-info : installation fail[manifest=%s]\n", manifest);
1200                 ret = -1;
1201                 goto end;
1202         }
1203         _LOGD("fota-info : pkgid[%s] installation success\n", pkgid);
1204
1205         __rpm_apply_smack(pkgid,0);
1206         __ri_make_directory(pkgid);
1207
1208         ret = _ri_apply_privilege(pkgid, 0);
1209         if (ret < 0) {
1210                 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1211                 ret = -1;
1212                 goto end;
1213         }
1214         _LOGD("fota-info : pkgid[%s] apply smack success\n", pkgid);
1215
1216 end:
1217         if (manifest){
1218                 free(manifest);
1219                 manifest = NULL;
1220         }
1221
1222         return ret;
1223 }
1224
1225 static int __ri_upgrade_fota(char *pkgid)
1226 {
1227         _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
1228
1229         int ret = 0;
1230         char *manifest = NULL;
1231         char *temp[] = {"fota=true", NULL};
1232
1233         /*if pkgid is one of deactivation, it does not need to upgrade*/
1234         ret = __ri_check_pkgid_for_deactivation(pkgid);
1235         if (ret < 0) {
1236                 _LOGE("fota-info : pkgid[%s] for deactivation dont need to install.\n", pkgid);
1237                 return ret;
1238         }
1239
1240         /*get manifest*/
1241         manifest = pkgmgr_parser_get_manifest_file(pkgid);
1242         if (manifest == NULL) {
1243                 _LOGE("fota-info : dont have manefest[pkgid=%s]\n", pkgid);
1244                 ret = -1;
1245                 goto end;
1246         }
1247         _LOGD("fota-info : pkgid[%s] has manefest[%s]\n", pkgid, manifest);
1248         if (access(manifest, F_OK) != 0) {
1249                 _LOGE("fota-info : can not access[manifest=%s]\n", manifest);
1250                 free(manifest);
1251                 manifest = NULL;
1252
1253                 /*find xml*/
1254                 ret = __ri_find_xml(pkgid, &manifest);
1255                 if (ret < 0) {
1256                         _LOGE("fota-info : can not find xml[pkgid=%s]\n", pkgid);
1257                         ret = -1;
1258                         goto end;
1259                 }
1260         }
1261
1262         if (manifest == NULL) {
1263                 return -1;
1264         }
1265
1266         ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, temp);
1267         if (ret < 0) {
1268                 _LOGE("fota-info : upgrade fail[manifest=%s]\n", manifest);
1269                 ret = -1;
1270                 goto end;
1271         }
1272
1273         __rpm_apply_smack(pkgid,0);
1274         __ri_make_directory(pkgid);
1275
1276         ret = _ri_apply_privilege(pkgid, 0);
1277         if (ret < 0) {
1278                 _LOGE("fota-info : _ri_apply_privilege fail[pkgid=%s]\n", pkgid);
1279         }
1280
1281         _LOGD("fota-info : pkgid[%s] upgrade success\n", pkgid);
1282
1283 end:
1284         if (manifest)
1285                 free(manifest);
1286
1287         return ret;
1288 }
1289
1290 static int __ri_uninstall_fota(char *pkgid)
1291 {
1292         _LOGD("fota-info : pkgid[%s] start uninstallation\n", pkgid);
1293
1294         int ret = 0;
1295
1296         ret = _ri_privilege_unregister_package(pkgid);
1297         if (ret < 0) {
1298                 _LOGE("fota-info : _ri_privilege_unregister_package fail[pkgid=%s]\n", pkgid);
1299         }
1300
1301         ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1302         if (ret < 0) {
1303                 _LOGE("fota-info : uninstall fail[manifest=%s]\n", pkgid);
1304         }
1305
1306         _LOGD("fota-info : pkgid[%s] uninstall success\n", pkgid);
1307
1308         return ret;
1309 }
1310
1311 static char * __getvalue(const char* pBuf, const char* pKey)
1312 {
1313         const char* p = NULL;
1314         const char* pStart = NULL;
1315         const char* pEnd = NULL;
1316
1317         p = strstr(pBuf, pKey);
1318         if (p == NULL)
1319                 return NULL;
1320
1321         pStart = p + strlen(pKey) + 1;
1322         pEnd = strchr(pStart, SEPERATOR_START);
1323         if (pEnd == NULL)
1324                 return NULL;
1325
1326         size_t len = pEnd - pStart;
1327         if (len <= 0)
1328                 return NULL;
1329
1330         char *pRes = (char*)malloc(len + 1);
1331         if(pRes == NULL){
1332                 _LOGE("@malloc failed");
1333                 return NULL;
1334         }
1335         strncpy(pRes, pStart, len);
1336         pRes[len] = 0;
1337
1338         return pRes;
1339 }
1340
1341 static char *__find_rpm_pkgid(const char* manifest)
1342 {
1343         FILE *fp = NULL;
1344         char buf[BUF_SIZE] = {0};
1345         char *pkgid = NULL;
1346
1347         fp = fopen(manifest, "r");
1348         if (fp == NULL) {
1349                 _LOGE("csc-info : Fail get : %s\n", manifest);
1350                 return NULL;
1351         }
1352
1353         while (fgets(buf, BUF_SIZE, fp) != NULL) {
1354                 __str_trim(buf);
1355                 pkgid = __getvalue(buf, TOKEN_PACKAGE_STR);
1356                 if (pkgid !=  NULL) {
1357                         fclose(fp);
1358                         return pkgid;
1359                 }
1360                 memset(buf, 0x00, BUF_SIZE);
1361         }
1362
1363         if (fp != NULL)
1364                 fclose(fp);
1365
1366         return NULL;
1367 }
1368
1369 static int __copy_file( const char *src_path, const char *dst_path)
1370 {
1371         FILE            *src, *dst;
1372         int             rc = 0;
1373         unsigned char   temp_buf[8192] = {'\0',};
1374         size_t          size_of_uchar = sizeof( unsigned char);
1375         size_t          size_of_temp_buf = sizeof( temp_buf);
1376
1377     src = fopen(src_path, "r");
1378     if( src == NULL) {
1379                 _LOGE("Failed to open(). path=%s, E:%d(%s)", src_path, errno, strerror(errno));
1380         return  -1;
1381     }
1382
1383     dst = fopen(dst_path, "w");
1384     if( dst == NULL) {
1385                 _LOGE("Failed to open dst file. file=%s, E:%d(%s)", dst_path, errno, strerror(errno));
1386         fclose(src);
1387         return  -1;
1388     }
1389
1390     while(!feof(src)) {
1391         rc = fread( temp_buf, size_of_uchar, size_of_temp_buf, src);
1392         fwrite( temp_buf, size_of_uchar, rc, dst);
1393     }
1394
1395     fclose( src);
1396     fclose( dst);
1397     return  0;
1398 }
1399
1400 static int __ri_install_csc(char *path_str, char *remove_str)
1401 {
1402         int ret = 0;
1403
1404         char *pkgid = NULL;
1405         char delims[] = "/";
1406         char* token = NULL;
1407         char argv[BUF_SIZE] = {'\0'};
1408         char xml_name[BUF_SIZE] = {'\0'};
1409         char src_file[BUF_SIZE] = {'\0'};
1410         char dest_file[BUF_SIZE] = {'\0'};
1411
1412         snprintf(src_file, sizeof(src_file), "%s", path_str);
1413
1414         /*get pkgid from path str*/
1415         pkgid = __find_rpm_pkgid(path_str);
1416         if (pkgid == NULL) {
1417                 _LOGE("csc-info : fail to find pkgid\n");
1418                 return -1;
1419         }
1420         _LOGD("csc-info : find pkgid=[%s] for installation\n", pkgid);
1421
1422         /*find xml name*/
1423         token = strtok(path_str, delims);
1424         while(token)
1425         {
1426                 memset(xml_name, 0x00, sizeof(xml_name));
1427                 if (strlen(token) < BUF_SIZE)
1428                         strcat(xml_name, token);
1429                 else
1430                         _LOGE("tokenized value too long[%s]", token);
1431
1432                 token = strtok(NULL, delims);
1433         }
1434         _LOGD("csc-info : xml name = %s\n", xml_name);
1435
1436         /*copy xml to /opt/share/packages*/
1437         snprintf(dest_file, sizeof(dest_file), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1438         ret = __copy_file(src_file, dest_file);
1439         if (ret != 0) {
1440                 _LOGE("csc-info : xml copy fail(%d)\n", ret);
1441         } else {
1442                 _LOGE("csc-info : xml copy success to [%s] \n", dest_file);
1443         }
1444
1445         /*remove old pkg info*/
1446         ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1447         if (ret < 0) {
1448                 _LOGD("csc-info : fail remove old pkg info\n");
1449         } else {
1450                 _LOGD("csc-info : success remove old pkg info\n");
1451         }
1452
1453         /*insert new pkg info*/
1454         memset(argv, 0x00, sizeof(argv));
1455         snprintf(argv, sizeof(argv), "%s/%s", OPT_SHARE_PACKAGES, xml_name);
1456         ret = __ri_init_csc_xml(argv, remove_str);
1457         if (ret < 0) {
1458                 _LOGD("csc-info : fail insert db\n");
1459         } else {
1460                 _LOGD("csc-info : success xml name = %s\n", xml_name);
1461         }
1462         if(pkgid){
1463                 free(pkgid);
1464                 pkgid = NULL;
1465         }
1466
1467         return 0;
1468 }
1469
1470 static int __ri_uninstall_csc(char *pkgid)
1471 {
1472         /*remove old pkg info*/
1473         int ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
1474         if (ret < 0) {
1475                 _LOGD("csc-info : fail remove old pkg info\n");
1476         } else {
1477                 _LOGD("csc-info : success remove old pkg info\n");
1478         }
1479
1480         return 0;
1481 }
1482
1483
1484 static int __get_size_from_xml(const char *manifest, int *size)
1485 {
1486         const char *val = NULL;
1487         const xmlChar *node;
1488         xmlTextReaderPtr reader;
1489         int ret = PMINFO_R_OK;
1490
1491         if(manifest == NULL) {
1492                 _LOGE("Input argument is NULL\n");
1493                 return PMINFO_R_ERROR;
1494         }
1495
1496         if(size == NULL) {
1497                 _LOGE("Argument supplied to hold return value is NULL\n");
1498                 return PMINFO_R_ERROR;
1499         }
1500
1501         reader = xmlReaderForFile(manifest, NULL, 0);
1502
1503         if (reader){
1504                 if (_child_element(reader, -1)) {
1505                         node = xmlTextReaderConstName(reader);
1506                         if (!node) {
1507                                 _LOGE("xmlTextReaderConstName value is NULL\n");
1508                                 ret = PMINFO_R_ERROR;
1509                                 goto end;
1510                         }
1511
1512                         if (!strcmp(ASCII(node), "manifest")) {
1513                                 ret = _ri_get_attribute(reader,"size",&val);
1514                                 if(ret != 0){
1515                                         _LOGE("@Error in getting the attribute value");
1516                                         ret = PMINFO_R_ERROR;
1517                                         goto end;
1518                                 }
1519                                 if (val) {
1520                                         *size = atoi(val);
1521                                         free((void*)val);
1522                                 } else {
1523                                         *size = 0;
1524                                         _LOGE("package size is not specified\n");
1525                                         ret =  PMINFO_R_ERROR;
1526                                         goto end;
1527                                 }
1528                         } else {
1529                                 _LOGE("Unable to create xml reader\n");
1530                                 ret = PMINFO_R_ERROR;
1531                                 goto end;
1532                         }
1533                 }
1534         } else {
1535                 _LOGE("xmlReaderForFile value is NULL\n");
1536                 ret =  PMINFO_R_ERROR;
1537         }
1538
1539
1540 end:
1541         xmlFreeTextReader(reader);
1542         return PMINFO_R_OK;
1543 }
1544
1545 static int __get_location_from_xml(const char *manifest, pkgmgrinfo_install_location *location)
1546 {
1547         const char *val = NULL;
1548         const xmlChar *node;
1549         xmlTextReaderPtr reader;
1550         int ret = -1;
1551
1552         if(manifest == NULL) {
1553                 _LOGE("Input argument is NULL\n");
1554                 return PMINFO_R_ERROR;
1555         }
1556
1557         if(location == NULL) {
1558                 _LOGE("Argument supplied to hold return value is NULL\n");
1559                 return PMINFO_R_ERROR;
1560         }
1561
1562         reader = xmlReaderForFile(manifest, NULL, 0);
1563
1564         if (reader){
1565                 if ( _child_element(reader, -1)) {
1566                         node = xmlTextReaderConstName(reader);
1567                         if (!node) {
1568                                 _LOGE("xmlTextReaderConstName value is NULL\n");
1569                                 xmlFreeTextReader(reader);
1570                                 return PMINFO_R_ERROR;
1571                         }
1572
1573                         if (!strcmp(ASCII(node), "manifest")) {
1574                                 ret = _ri_get_attribute(reader,"install-location",&val);
1575                                 if(ret != 0){
1576                                         _LOGE("@Error in getting the attribute value");
1577                                         xmlFreeTextReader(reader);
1578                                         return PMINFO_R_ERROR;
1579                                 }
1580
1581                                 if (val) {
1582                                         if (strcmp(val, "internal-only") == 0)
1583                                                 *location = PMINFO_INSTALL_LOCATION_INTERNAL_ONLY;
1584                                         else if (strcmp(val, "prefer-external") == 0)
1585                                                 *location = PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL;
1586                                         else
1587                                                 *location = PMINFO_INSTALL_LOCATION_AUTO;
1588                                         free((void*)val);
1589                                 }
1590                         } else {
1591                                 _LOGE("Unable to create xml reader\n");
1592                                 xmlFreeTextReader(reader);
1593                                 return PMINFO_R_ERROR;
1594                         }
1595                 }
1596         } else {
1597                 _LOGE("xmlReaderForFile value is NULL\n");
1598                 return PMINFO_R_ERROR;
1599         }
1600
1601         xmlFreeTextReader(reader);
1602
1603         return PMINFO_R_OK;
1604 }
1605
1606 static char *__get_pkg_path(const char *pkg_path, const char *pkgid)
1607 {
1608         int ret = 0;
1609         char buff[BUF_SIZE] = {'\0'};
1610         char *real_path = NULL;
1611
1612         snprintf(buff, BUF_SIZE, "%s/%s", pkg_path, pkgid);
1613         do {
1614                 if (__is_dir(buff)) break;
1615                 memset(buff, '\0', BUF_SIZE);
1616                 snprintf(buff, BUF_SIZE, "%s/%s", USR_APPS,pkgid);
1617                 if (__is_dir(buff)) break;
1618                 memset(buff, '\0', BUF_SIZE);
1619                 snprintf(buff, BUF_SIZE, "/opt/apps/%s", pkgid);
1620                 if (__is_dir(buff)) break;
1621                 memset(buff, '\0', BUF_SIZE);
1622                 snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS,pkgid);
1623                 if (__is_dir(buff)) break;
1624         } while (0);
1625
1626         ret = chdir(buff);
1627         if (ret != 0) {
1628                 _LOGE("chdir(%s) failed. [%s]", buff, strerror(errno));
1629                 return NULL;
1630         }
1631
1632         real_path = (char *)malloc(strlen(buff) + 1);
1633         if (real_path == NULL) {
1634                 _LOGE("malloc() failed.");
1635                 return NULL;
1636         }
1637         memset(real_path, '\0', strlen(buff) + 1);
1638         memcpy(real_path, buff, strlen(buff));
1639
1640         return real_path;
1641 }
1642
1643 static int __set_label_for_pkg_dir(const char *dirpath, const char* label)
1644 {
1645         int ret = 0;
1646         DIR *dir;
1647         struct dirent entry;
1648         struct dirent *result;
1649         char fullpath[BUF_SIZE] = {'\0'};
1650
1651         if (access(dirpath, F_OK) != 0) {
1652                 _LOGE("skip! empty dirpath = [%s]", dirpath);
1653                 return 0;
1654         }
1655
1656         dir = opendir(dirpath);
1657         if (!dir) {
1658                 _LOGE("opendir(%s) failed. [%d][%s]", dirpath, errno, strerror(errno));
1659                 return -1;
1660         }
1661
1662         if(lsetxattr(dirpath, "security.SMACK64", label, strlen(label), 0)) {
1663                 _LOGE("error(%d) in setting smack label",errno);
1664         }
1665
1666         for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)){
1667                 if (!strcmp(entry.d_name, ".") ||
1668                         !strcmp(entry.d_name, "..")) {
1669                         continue;
1670                 }
1671
1672                 // sub
1673                 snprintf(fullpath, BUF_SIZE, "%s/%s", dirpath, entry.d_name);
1674                 if(lsetxattr(fullpath, "security.SMACK64", label, strlen(label), 0)) {
1675                         _LOGE("error(%d) in setting smack label",errno);
1676                 }
1677
1678
1679                 // find next dir
1680                 if (entry.d_type == DT_DIR) {
1681                         ret = __set_label_for_pkg_dir(fullpath,label);
1682                         if(ret != 0 ){
1683                                 _LOGE("_coretpk_installer_apply_directory_policy(%s) failed.", fullpath);
1684                         }
1685                 }
1686                 memset(fullpath, '\0', BUF_SIZE);
1687         }
1688
1689         closedir(dir);
1690
1691         return ret;
1692
1693 }
1694
1695 void _ri_soft_reset(char *pkgid)
1696 {
1697         int ret = 0;
1698         char dirpath[PKG_STRING_LEN_MAX] = {'\0'};
1699
1700         //home
1701         snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s", OPT_USR_APPS, pkgid);
1702         if (access(dirpath, F_OK)==0) {
1703                 __set_label_for_pkg_dir(dirpath, "_");
1704         }
1705
1706         // data
1707         memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1708         snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/data", OPT_USR_APPS, pkgid);
1709         if (access(dirpath, F_OK)==0) {
1710                 __set_label_for_pkg_dir(dirpath, pkgid);
1711         }
1712
1713         //shared
1714         memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1715         snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared", OPT_USR_APPS, pkgid);
1716         if (access(dirpath, F_OK)==0) {
1717                 __set_label_for_pkg_dir(dirpath, "_");
1718         }
1719
1720         //shared/res
1721         memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1722         snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/res", OPT_USR_APPS, pkgid);
1723         if (access(dirpath, F_OK)==0) {
1724                 __set_label_for_pkg_dir(dirpath, "_");
1725         }
1726
1727         //shared/data
1728         memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1729         snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/data", OPT_USR_APPS, pkgid);
1730         if (access(dirpath, F_OK)==0) {
1731                 __set_label_for_pkg_dir(dirpath, "_");
1732         }
1733
1734         //shared/trusted
1735         memset(dirpath, '\0', PKG_STRING_LEN_MAX);
1736         snprintf(dirpath, PKG_STRING_LEN_MAX, "%s/%s/shared/trusted", OPT_USR_APPS, pkgid);
1737         if (access(dirpath, F_OK)==0) {
1738                 pkgmgrinfo_pkginfo_h handle = NULL;
1739                 char *groupid = NULL;
1740                 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
1741                 if (ret != PMINFO_R_OK)
1742                         return;
1743                 ret = pkgmgrinfo_pkginfo_get_groupid(handle, &groupid);
1744                 if (ret == PMINFO_R_OK) {
1745                         _ri_privilege_setup_path(pkgid, dirpath, APP_PATH_GROUP_RW, groupid);
1746                 }
1747                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1748         }
1749
1750         return;
1751 }
1752
1753 void _ri_register_cert(const char *pkgid)
1754 {
1755         int error = 0;
1756         pkgmgrinfo_instcertinfo_h handle = NULL;
1757         int i = 0;
1758         /* create Handle*/
1759         error = pkgmgrinfo_create_certinfo_set_handle(&handle);
1760         if (error != 0) {
1761                 _LOGE("Cert handle creation failed. Err:%d", error);
1762                 __ri_free_cert_chain();
1763                 return;
1764         }
1765
1766         if (list[SIG_AUTH].cert_value == NULL) {
1767                 _LOGE("pkgid[%s] dont have SIG_AUTH.cert_value ", pkgid);
1768                 goto err;
1769         }
1770
1771         for (i = 0; i < MAX_CERT_NUM; i++) {
1772
1773                 if (list[i].cert_value) {
1774                         error = pkgmgrinfo_set_cert_value(handle, list[i].cert_type, list[i].cert_value);
1775                         if (error != 0) {
1776                                 _LOGE("pkgmgrinfo_set_cert_value failed. cert type:%d. Err:%d", list[i].cert_type, error);
1777                                 goto err;
1778                         }
1779                 }
1780         }
1781         /* Save the certificates in cert DB*/
1782         error = pkgmgrinfo_save_certinfo(pkgid, handle);
1783         if (error != 0) {
1784                 _LOGE("pkgmgrinfo_save_certinfo failed. Err:%d", error);
1785                 goto err;
1786         }
1787 err:
1788         if (handle)
1789                 pkgmgrinfo_destroy_certinfo_set_handle(handle);
1790         __ri_free_cert_chain();
1791 }
1792
1793 void _ri_unregister_cert(const char *pkgid)
1794 {
1795         int error = 0;
1796         /* Delete the certifictes from cert DB*/
1797         error = pkgmgrinfo_delete_certinfo(pkgid);
1798         if (error != 0) {
1799                 _LOGE("pkgmgrinfo_delete_certinfo failed. Err:%d", error);
1800                 return;
1801         }
1802 }
1803
1804 int _ri_verify_sig_and_cert(const char *sigfile, int *visibility)
1805 {
1806         char certval[MAX_BUFF_LEN] = {'\0'};
1807         int err = 0;
1808         int validity = 0;
1809         int i = 0;
1810         int j= 0;
1811         int ret = RPM_INSTALLER_SUCCESS;
1812         char *crt = NULL;
1813         signature_x *signx = NULL;
1814         struct keyinfo_x *keyinfo = NULL;
1815         struct x509data_x *x509data = NULL;
1816         CERT_CONTEXT *ctx = NULL;
1817         int sigtype = 0;
1818
1819         ctx = cert_svc_cert_context_init();
1820         if (ctx == NULL) {
1821                 _LOGE("cert_svc_cert_context_init() failed.");
1822                 return RPM_INSTALLER_ERR_INTERNAL;
1823         }
1824
1825         if (strstr(sigfile, AUTHOR_SIGNATURE_XML))
1826                 sigtype = SIG_AUTH;
1827         else if (strstr(sigfile, SIGNATURE1_XML))
1828                 sigtype = SIG_DIST1;
1829         else if (strstr(sigfile, SIGNATURE2_XML))
1830                 sigtype = SIG_DIST2;
1831         else {
1832                 _LOGE("Unsupported signature type! [%s]", sigfile);
1833                 cert_svc_cert_context_final(ctx);
1834                 return RPM_INSTALLER_ERR_INTERNAL;
1835         }
1836
1837         signx = _ri_process_signature_xml(sigfile);
1838         if (signx == NULL) {
1839                 _LOGE("_ri_process_signature_xml(%s) failed.", sigfile);
1840                 ret = RPM_INSTALLER_ERR_INTERNAL;
1841                 goto end;
1842         }
1843
1844         keyinfo = signx->keyinfo;
1845         if ((keyinfo == NULL) || (keyinfo->x509data == NULL) || (keyinfo->x509data->x509certificate == NULL)) {
1846                 _LOGE("keyinfo is invalid. [%s]", sigfile);
1847                 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1848                 goto end;
1849         }
1850
1851         x509data = keyinfo->x509data;
1852         x509certificate_x *cert = x509data->x509certificate;
1853
1854         // First cert is Signer certificate
1855         if (cert->text != NULL) {
1856                 for (i = 0; i <= (int)strlen(cert->text); i++) {
1857                         if (cert->text[i] != '\n') {
1858                                 certval[j++] = cert->text[i];
1859                         }
1860                 }
1861                 certval[j] = '\0';
1862
1863                 err = cert_svc_load_buf_to_context(ctx, (unsigned char*)certval);
1864                 if (err != 0) {
1865                         _LOGE("cert_svc_load_buf_to_context() failed. cert = [%s], err = [%d]", certval, err);
1866                         ret = RPM_INSTALLER_ERR_INTERNAL;
1867                         goto end;
1868                 }
1869                 // _LOGD("SIG_SIGNER certval = [%d][%s]", strlen(certval), certval);
1870
1871                 err = __ri_create_cert_chain(sigtype, SIG_SIGNER, certval);
1872                 if (err) {
1873                         _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1874                         __ri_free_cert_chain();
1875                         ret = RPM_INSTALLER_ERR_INTERNAL;
1876                         goto end;
1877                 }
1878         }
1879
1880         // Second cert is Intermediate certificate
1881         cert = cert->next;
1882         if (cert->text != NULL) {
1883                 memset(certval, 0x00, MAX_BUFF_LEN);
1884                 j = 0;
1885                 for (i = 0; i <= (int)strlen(cert->text); i++) {
1886                         if (cert->text[i] != '\n') {
1887                                 certval[j++] = cert->text[i];
1888                         }
1889                 }
1890                 certval[j] = '\0';
1891
1892                 if (cert->text != NULL) {
1893                         err = cert_svc_push_buf_into_context(ctx, (unsigned char*)certval);
1894                         if (err != 0) {
1895                                 _LOGE("cert_svc_push_buf_into_context() failed. cert = [%s], err = [%d]", certval, err);
1896                                 ret = RPM_INSTALLER_ERR_INTERNAL;
1897                                 goto end;
1898                         }
1899                 }
1900                 // _LOGD("SIG_INTERMEDIATE certval = [%d][%s]", strlen(certval), certval);
1901
1902                 err = __ri_create_cert_chain(sigtype, SIG_INTERMEDIATE, certval);
1903                 if (err) {
1904                         _LOGE("__ri_create_cert_chain() failed. sigtype = [%d], certval = [%s]", sigtype, certval);
1905                         __ri_free_cert_chain();
1906                         ret = RPM_INSTALLER_ERR_INTERNAL;
1907                         goto end;
1908                 }
1909         } else {
1910                 _LOGE("Invalid CertChain! (cert->text is NULL.)");
1911                 ret = RPM_INSTALLER_ERR_CERT_INVALID;
1912                 goto end;
1913         }
1914
1915         err = cert_svc_verify_certificate(ctx, &validity);
1916         if (err != 0) {
1917                 _LOGE("cert_svc_verify_certificate() failed.");
1918                 ret = RPM_INSTALLER_ERR_INTERNAL;
1919                 goto end;
1920         }
1921         _LOGD("cert_svc_verify() is done successfully. validity=[%d]", validity);
1922
1923         if (validity == 0) {
1924                 _LOGE("Certificate Invalid/Expired (validity == 0)");
1925                 ret = RPM_INSTALLER_ERR_CERTCHAIN_VERIFICATION_FAILED;
1926                 goto end;
1927         }
1928
1929         // verify signature
1930         // For reference validation, we should be in TEMP_DIR/usr/apps/<pkgid>
1931         if (ctx->fileNames && ctx->fileNames->filename) {
1932                 _LOGD("Root CA cert path=[%s]", ctx->fileNames->filename);
1933
1934                 err = __ri_xmlsec_verify_signature(sigfile, ctx->fileNames->filename);
1935                 if (err < 0) {
1936                         _LOGE("__ri_xmlsec_verify_signature(%s) failed.", sigfile);
1937                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1938                         goto end;
1939                 }
1940
1941                 crt = __ri_get_cert_from_file(ctx->fileNames->filename);
1942                 err = __ri_create_cert_chain(sigtype, SIG_ROOT, crt);
1943                 if (err) {
1944                         _LOGE("__ri_create_cert_chain(%d) failed.", sigtype);
1945                         __ri_free_cert_chain();
1946                         ret = RPM_INSTALLER_ERR_INTERNAL;
1947                         goto end;
1948                 }
1949
1950                 err = cert_svc_get_visibility(ctx, visibility);
1951                 if (err != 0) {
1952                         _LOGE("cert_svc_get_visibility() failed. err = [%d]", err);
1953                         ret = RPM_INSTALLER_ERR_SIG_VERIFICATION_FAILED;
1954                         goto end;
1955                 }
1956                 _LOGD("cert_svc_get_visibility() returns visibility=[%d]", *visibility);
1957         } else {
1958                 _LOGE("No Root CA cert found. Signature validation failed.");
1959                 ret = RPM_INSTALLER_ERR_ROOT_CERT_NOT_FOUND;
1960                 goto end;
1961         }
1962
1963         ret = 0;
1964
1965 end:
1966         cert_svc_cert_context_final(ctx);
1967         ctx = NULL;
1968         _ri_free_signature_xml(signx);
1969         signx = NULL;
1970         return ret;
1971 }
1972
1973 int _ri_verify_signatures(const char *root_path, const char *pkgid)
1974 {
1975         int ret = 0;
1976         char buff[BUF_SIZE] = {'\0'};
1977         char *pkg_path = NULL;
1978         int visibility = 0;
1979
1980         _LOGD("root_path=[%s], pkgid=[%s]", root_path, pkgid);
1981
1982         // check for signature and certificate
1983         pkg_path = __get_pkg_path(root_path, pkgid);
1984         if (pkg_path == NULL) {
1985                 _LOGE("__get_pkg_path(%s, %s) failed.", root_path, pkgid);
1986                 return 0;
1987         }
1988
1989         _LOGD("switched to pkg_path=[%s]", pkg_path);
1990
1991         // author-signature.xml is mandatory
1992         snprintf(buff, BUF_SIZE, "%s/author-signature.xml", pkg_path);
1993         if (access(buff, F_OK) == 0) {
1994                 _LOGD("author-signature.xml, path=[%s]", buff);
1995                 ret = _ri_verify_sig_and_cert(buff, &visibility);
1996                 if (ret) {
1997                         _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
1998                         ret = -1;
1999                         goto end;
2000                 }
2001                 _LOGD("------------------------------------------------------");
2002                 _LOGD("signature is verified successfully");
2003                 _LOGD("path=[%s]", buff);
2004                 _LOGD("------------------------------------------------------");
2005         }
2006         memset(buff, '\0', BUF_SIZE);
2007
2008         // signature2.xml is optional
2009         snprintf(buff, BUF_SIZE, "%s/signature2.xml", pkg_path);
2010         if (access(buff, F_OK) == 0) {
2011                 _LOGD("signature2.xml found. [%s]", pkg_path);
2012                 ret = _ri_verify_sig_and_cert(buff, &visibility);
2013                 if (ret) {
2014                         _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2015                         ret = -1;
2016                         goto end;
2017                 }
2018                 _LOGD("_ri_verify_sig_and_cert(%s) succeed.", buff);
2019         }
2020         memset(buff, '\0', BUF_SIZE);
2021
2022         // signature1.xml is mandatory
2023         snprintf(buff, BUF_SIZE, "%s/signature1.xml", pkg_path);
2024         if (access(buff, F_OK) == 0) {
2025                 _LOGD("signature1.xml, path=[%s]", buff);
2026                 ret = _ri_verify_sig_and_cert(buff, &visibility);
2027                 if (ret) {
2028                         _LOGE("_ri_verify_sig_and_cert(%s) failed.", buff);
2029                         ret = -1;
2030                         goto end;
2031                 }
2032                 _LOGD("------------------------------------------------------");
2033                 _LOGD("signature is verified successfully");
2034                 _LOGD("path=[%s]", buff);
2035                 _LOGD("------------------------------------------------------");
2036         }
2037         memset(buff, '\0', BUF_SIZE);
2038
2039         ret = 0;
2040
2041 end :
2042         if(pkg_path){
2043                 free(pkg_path);
2044                 pkg_path = NULL;
2045         }
2046
2047         if ((ret != 0) && (sig_enable == 0)) {
2048                 _LOGD("_ri_verify_signatures(%s, %s) failed, but it's ok for config.", root_path, pkgid);
2049                 ret = 0;
2050         }
2051
2052         return ret;
2053 }
2054
2055 void _ri_apply_smack(char *pkgname, int flag)
2056 {
2057         __rpm_apply_smack(pkgname, flag);
2058 }
2059
2060 int _ri_apply_privilege(char *pkgid, int visibility)
2061 {
2062         int ret = -1;
2063         pkgmgrinfo_pkginfo_h handle = NULL;
2064         privilegeinfo info;
2065         int apptype = PERM_APP_TYPE_EFL;
2066
2067         if (!pkgid) {
2068                 _LOGE("pkgid is NULL");
2069                 return -1;
2070         }
2071
2072         memset(&info, '\0', sizeof(info));
2073
2074         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
2075         if (ret != PMINFO_R_OK)
2076                 return -1;
2077
2078         if (strlen(pkgid) < PKG_MAX_LEN)
2079                 strcpy(info.package_id, pkgid);
2080         else {
2081                 _LOGE("pkgid too long[%s]", pkgid);
2082                 if (handle)
2083                         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2084                 return -1;
2085         }
2086
2087         info.visibility = visibility;
2088
2089         ret = pkgmgrinfo_pkginfo_foreach_privilege(handle, __privilege_func, (void *)&info);
2090         if (ret != PMINFO_R_OK) {
2091                 _LOGE("pkgmgrinfo_pkginfo_get_pkgid failed\n");
2092                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2093                 return -1;
2094         }
2095         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
2096
2097         if (visibility & CERT_SVC_VISIBILITY_PLATFORM) {
2098                 _LOGD("VISIBILITY_PLATFORM!");
2099                 apptype = PERM_APP_TYPE_EFL_PLATFORM;
2100         } else if ((visibility & CERT_SVC_VISIBILITY_PARTNER) ||
2101                         (visibility & CERT_SVC_VISIBILITY_PARTNER_OPERATOR) ||
2102                         (visibility & CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER)) {
2103                 _LOGD("VISIBILITY_PARTNER!");
2104                 apptype = PERM_APP_TYPE_EFL_PARTNER;
2105         }
2106
2107         /*reload privilege*/
2108         const char *perm[] = {NULL, NULL};
2109         ret = _ri_privilege_enable_permissions(pkgid, apptype, perm, 1);
2110
2111         return 0;
2112 }
2113
2114 int _ri_set_group_id(const char *pkgid, const char *groupid)
2115 {
2116         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
2117         retvm_if(groupid == NULL, PMINFO_R_EINVAL, "groupid is NULL\n");
2118         int ret = -1;
2119         sqlite3 *pkginfo_db = NULL;
2120         char *query = NULL;
2121
2122         /*open db*/
2123         ret = db_util_open(PKGMGR_DB, &pkginfo_db, 0);
2124         retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_DB);
2125
2126         /*Begin transaction*/
2127         ret = sqlite3_exec(pkginfo_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2128         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
2129         _LOGD("Transaction Begin\n");
2130
2131         query = sqlite3_mprintf("update package_info set package_reserve3=%Q where package=%Q", groupid, pkgid);
2132
2133         ret = sqlite3_exec(pkginfo_db, query, NULL, NULL, NULL);
2134         tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2135
2136         /*Commit transaction*/
2137         ret = sqlite3_exec(pkginfo_db, "COMMIT", NULL, NULL, NULL);
2138         if (ret != SQLITE_OK) {
2139                 _LOGE("Failed to commit transaction. Rollback now\n");
2140                 ret = sqlite3_exec(pkginfo_db, "ROLLBACK", NULL, NULL, NULL);
2141                 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
2142         }
2143         _LOGD("Transaction Commit and End\n");
2144
2145         ret = PMINFO_R_OK;
2146 catch:
2147         sqlite3_free(query);
2148         sqlite3_close(pkginfo_db);
2149         return ret;
2150 }
2151
2152 static int __ri_install_fota_for_rw(char *pkgid)
2153 {
2154         int ret = 0;
2155         int home_dir = 1;
2156         char buff[BUF_SIZE] = {'\0'};
2157
2158         _LOGD("fota-info : pkgid[%s] start installation\n", pkgid);
2159
2160         /*unzip pkg path from factoryrest data*/
2161         snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2162         const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2163         ret = _ri_xsystem(pkg_argv);
2164         if (ret != 0) {
2165                 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2166                 return ret;
2167         }
2168
2169         _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2170
2171         /*unzip manifest from factoryrest data*/
2172         memset(buff, '\0', BUF_SIZE);
2173         snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2174
2175         const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2176         ret = _ri_xsystem(xml_argv);
2177         if (ret != 0) {
2178                 _LOGE("fota-info : xml_argv fail for pkgid[%s] .\n", pkgid);
2179                 return ret;
2180         }
2181
2182         _LOGD("fota-info : xml_argv[%s] is success\n", pkgid);
2183
2184         /*get updated manifest path*/
2185         memset(buff, '\0', BUF_SIZE);
2186         snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2187         _LOGE("Manifest name is %s\n", buff);
2188
2189         /*apply smack for manifest*/
2190         _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2191
2192         /*register manifest*/
2193         char* fota_tags[3] = {NULL, };
2194         fota_tags[0] = "removable=true";
2195         fota_tags[1] = "preload=true";
2196         fota_tags[2] = NULL;
2197
2198         ret = pkgmgr_parser_parse_manifest_for_installation(buff, fota_tags);
2199         if (ret < 0) {
2200                 _LOGE("Parsing Manifest Failed\n");
2201                 ret = -1;
2202                 goto err;
2203         } else {
2204                 _LOGD("Parsing Manifest Success\n");
2205         }
2206
2207         /*Register cert info*/
2208         _ri_register_cert(pkgid);
2209
2210         /*apply smack for pkg root path*/
2211         memset(buff, '\0', BUF_SIZE);
2212         snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2213         _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2214
2215         /*apply smack for defined directory*/
2216         __rpm_apply_smack(pkgid, home_dir);
2217
2218         /*apply privilege*/
2219         ret = _ri_apply_privilege(pkgid, 0);
2220         if (ret != 0) {
2221                 _LOGE("apply perm failed with err(%d)\n", ret);
2222         } else {
2223                 _LOGD("apply perm success\n");
2224         }
2225
2226 err:
2227
2228         return ret;
2229 }
2230
2231 static int __ri_upgrade_fota_for_rw(char *pkgid)
2232 {
2233         int ret = 0;
2234         int home_dir = 1;
2235         char buff[BUF_SIZE] = {'\0'};
2236
2237         _LOGD("fota-info : pkgid[%s] start upgrade\n", pkgid);
2238
2239         /*unzip pkg dir from factoryrest data*/
2240         snprintf(buff, BUF_SIZE, "opt/usr/apps/%s/*", pkgid);
2241         const char *pkg_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2242         ret = _ri_xsystem(pkg_argv);
2243         if (ret != 0) {
2244                 _LOGE("fota-info : unzip root path[%s] is fail .\n", buff);
2245                 return ret;
2246         }
2247
2248         _LOGD("fota-info : unzip root path[%s] is success\n", buff);
2249
2250         /*unzip manifest from factoryrest data*/
2251         memset(buff, '\0', BUF_SIZE);
2252         snprintf(buff, BUF_SIZE, "opt/share/packages/%s.xml", pkgid);
2253         const char *xml_argv[] = { "/usr/bin/unzip", "-oX", OPT_ZIP_FILE, buff, "-d", "/", NULL };
2254         ret = _ri_xsystem(xml_argv);
2255         if (ret != 0) {
2256                 _LOGE("fota-info : unzip manifest[%s] is fail .\n", buff);
2257                 return ret;
2258         }
2259
2260         _LOGD("fota-info : unzip manifest[%s] is success\n", buff);
2261
2262         /*get updated manifest path*/
2263         memset(buff, '\0', BUF_SIZE);
2264         snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2265         _LOGE("Manifest name is %s\n", buff);
2266
2267         /*apply smack for manifest*/
2268         _ri_privilege_change_smack_label(buff, pkgid, 0);/*0 is SMACK_LABEL_ACCESS*/
2269
2270         /*register manifest*/
2271         ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
2272         if (ret < 0) {
2273                 _LOGE("Parsing Manifest Failed\n");
2274                 ret = -1;
2275                 goto err;
2276         } else {
2277                 _LOGD("Parsing Manifest Success\n");
2278         }
2279
2280         /*Register new cert info*/
2281         _ri_unregister_cert(pkgid);
2282         _ri_register_cert(pkgid);
2283
2284         /*apply smack for pkg root path*/
2285         memset(buff, '\0', BUF_SIZE);
2286         snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2287         _ri_privilege_setup_path(pkgid, buff, PERM_APP_PATH_ANY_LABEL, pkgid);
2288
2289         /*apply smack for defined directory*/
2290         __rpm_apply_smack(pkgid, home_dir);
2291
2292         /*apply privilege*/
2293         ret = _ri_apply_privilege(pkgid, 0);
2294         if (ret != 0) {
2295                 _LOGE("apply perm failed with err(%d)\n", ret);
2296         } else {
2297                 _LOGD("apply perm success\n");
2298         }
2299
2300 err:
2301         return ret;
2302 }
2303
2304 static int __ri_uninstall_fota_for_rw(char *pkgid)
2305 {
2306         int ret = 0;
2307         char buff[BUF_SIZE] = {'\0'};
2308
2309         _LOGD("fota-info : pkgid[%s] start uninstall\n", pkgid);
2310
2311         /*del root path dir*/
2312         snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
2313
2314         if (__is_dir(buff)) {
2315                 _rpm_delete_dir(buff);
2316         }
2317
2318         /*del manifest*/
2319         memset(buff, '\0', BUF_SIZE);
2320         snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2321         (void)remove(buff);
2322
2323         /*del db info*/
2324         ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
2325         if (ret < 0) {
2326                 _LOGE("Parsing Manifest Failed\n");
2327         }
2328
2329         /*execute privilege APIs*/
2330         _ri_privilege_revoke_permissions(pkgid);
2331         _ri_privilege_unregister_package(pkgid);
2332
2333         /*Unregister cert info*/
2334         _ri_unregister_cert(pkgid);
2335
2336         return 0;
2337 }
2338
2339 /**
2340  * callback for the pkgmgrinfo_appinfo_get_list used in _rpm_uninstall_pkg()
2341  */
2342 int __ri_check_running_app(const pkgmgrinfo_appinfo_h handle, void *user_data)
2343 {
2344         int ret = 0;
2345         bool isRunning = 0;
2346         char *appid = NULL;
2347         app_context_h appCtx = NULL;
2348
2349         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2350         if (ret < 0) {
2351                 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2352                 return ret;
2353         }
2354
2355         ret = app_manager_is_running(appid, &isRunning);
2356         if (ret < 0) {
2357                 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2358                 return ret;
2359         }
2360         _LOGE("app[%s] , running state[%d].\n", appid, isRunning);
2361
2362         if (isRunning) {
2363                 ret = app_manager_get_app_context(appid, &appCtx);
2364                 if (ret < 0) {
2365                         _LOGE("Failed to execute app_manager_get_app_context[%d].\n", ret);
2366                         return ret;
2367                 }
2368
2369                 ret = app_manager_terminate_app(appCtx);
2370                 if (ret < 0) {
2371                         _LOGE("Failed to execute app_manager_terminate_app[%d].\n", ret);
2372                         app_context_destroy(appCtx);
2373                         return ret;
2374                 }
2375
2376                 int i = 0;
2377                 for (i = 0; i < TERMINATE_RETRY_COUNT; i++) {
2378                         ret = app_manager_is_running(appid, &isRunning);
2379                         if (ret < 0) {
2380                                 _LOGE("Failed to execute app_manager_is_running[%d].\n", ret);
2381                                 app_context_destroy(appCtx);
2382                                 return ret;
2383                         }
2384
2385                         if (!isRunning) {
2386                                 _LOGD("App(%s) is terminated.\n", appid);
2387                                 break;
2388                         } else {
2389                                 _LOGD("App(%s) is not terminated yet. wait count=[%d].\n", appid, i);
2390                                 usleep(100000);
2391                         }
2392                 }
2393
2394                 ret = app_context_destroy(appCtx);
2395                 if (ret < 0) {
2396                         _LOGE("Failed to execute app_context_destroy[%d].\n", ret);
2397                         return ret;
2398                 }
2399         }
2400
2401         return ret;
2402 }
2403
2404 int __ri_change_dir(char *dirname)
2405 {
2406         int ret = 0;
2407
2408         ret = mkdir(dirname, 0644);
2409         if (ret < 0) {
2410                 if (access(dirname, F_OK) == 0) {
2411                         _rpm_delete_dir(dirname);
2412                         ret = mkdir(dirname, 0644);
2413                         if (ret < 0) {
2414                                 _LOGE("mkdir(%s) failed\n", dirname);
2415                                 return -1;
2416                         }
2417                 } else {
2418                         _LOGE("can not access[%s]\n", dirname);
2419                         return -1;
2420                 }
2421         }
2422
2423         ret = chdir(dirname);
2424         if (ret != 0) {
2425                 _LOGE("chdir(%s) failed [%s]\n", dirname, strerror(errno));
2426                 return -1;
2427         }
2428         return 0;
2429 }
2430
2431 int _ri_smack_reload(const char *pkgid, rpm_request_type request_type)
2432 {
2433         int ret = 0;
2434         char *op_type = NULL;
2435
2436         switch (request_type) {
2437                 case INSTALL_REQ:
2438                         op_type = strdup("install");
2439                         break;
2440
2441                 case UPGRADE_REQ:
2442                         op_type = strdup("update");
2443                         break;
2444
2445                 case UNINSTALL_REQ:
2446                         op_type = strdup("uninstall");
2447                         break;
2448
2449                 default:
2450                         break;
2451         }
2452
2453         if(op_type == NULL) {
2454                 _LOGE("@Failed to reload smack. request_type not matched[pkgid=%s, op=%s]", pkgid, op_type);
2455                 return -1;
2456         }
2457
2458         const char *smack_argv[] = { "/usr/bin/smack_reload.sh", op_type, pkgid, NULL };
2459         ret = _ri_xsystem(smack_argv);
2460         if (ret != 0) {
2461                 _LOGE("@Failed to reload smack[pkgid=%s, op=%s].", pkgid, op_type);
2462         } else {
2463                 _LOGD("#success: smack reload[pkgid=%s, op=%s]", pkgid, op_type);
2464         }
2465         if(op_type){
2466                 free(op_type);
2467                 op_type = NULL;
2468         }
2469         return ret;
2470 }
2471
2472 int _ri_smack_reload_all(void)
2473 {
2474         int ret = 0;
2475
2476         const char *smack_argv[] = { "/usr/bin/smackload-fast", NULL};
2477         ret = _ri_xsystem(smack_argv);
2478         if (ret != 0) {
2479                 _LOGE("@Failed to reload all smack : %d", errno);
2480         } else {
2481                 _LOGD("#success: smack reload all");
2482         }
2483
2484         return ret;
2485 }
2486
2487 void __ri_remove_updated_dir(const char *pkgid)
2488 {
2489         char path_buf[BUF_SIZE] = {'\0'};
2490
2491         // check pkgid
2492         if (pkgid == NULL) {
2493                 _LOGE("pkgid is NULL.");
2494                 return;
2495         }
2496
2497         // remove bin dir
2498         snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, BIN_DIR_STR);
2499         if (__is_dir(path_buf)) {
2500                 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2501                 _rpm_delete_dir(path_buf);
2502         }
2503
2504         // remove res dir
2505         memset(path_buf, '\0', BUF_SIZE);
2506         snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, RES_DIR_STR);
2507         if (__is_dir(path_buf)) {
2508                 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2509                 _rpm_delete_dir(path_buf);
2510         }
2511
2512         // remove shared/res dir
2513         memset(path_buf, '\0', BUF_SIZE);
2514         snprintf(path_buf, BUF_SIZE, "%s/%s/%s", OPT_USR_APPS, pkgid, SHARED_RES_DIR_STR);
2515         if (__is_dir(path_buf)) {
2516                 _LOGE("@pkgid[%s] need to clean dir[%s]\n", pkgid, path_buf);
2517                 _rpm_delete_dir(path_buf);
2518         }
2519 }
2520
2521 static int __metadata_func(const char *key, const char *value, void *user_data)
2522 {
2523         int ret = 0;
2524         bool isRunning = 0;
2525
2526         if (key == NULL) {
2527                 _LOGE("key is null\n");
2528                 return -1;
2529         }
2530         if (value == NULL) {
2531                 _LOGE("value is null\n");
2532                 return -1;
2533         }
2534         if (user_data == NULL) {
2535                 _LOGE("user_data is null\n");
2536                 return -1;
2537         }
2538
2539         if ((strcmp(key, "launch-on-attach") == 0) && (strcmp(value, "true") == 0)) {
2540                 _LOGE("consumer[%s] : launch-on-attach is true \n", (char *)user_data);
2541
2542                 ret = app_manager_is_running((char *)user_data, &isRunning);
2543                 if (ret < 0) {
2544                         _LOGE("Failed to execute app_manager_is_running[%s].\n", (char *)user_data);
2545                         return ret;
2546                 }
2547
2548                 if (isRunning) {
2549                         _LOGE("consumer[%s] is already launched \n", (char *)user_data);
2550                 } else {
2551                         usleep(100 * 1000); /* 100ms sleep for infomation ready*/
2552                         ret = aul_launch_app((char *)user_data, NULL);
2553                         if (ret == AUL_R_ERROR) {
2554                                 _LOGE("consumer[%s] launch fail, sleep and retry  launch_app\n", (char *)user_data);
2555                                 usleep(100 * 1000);     /* 100ms sleep for infomation ready*/
2556                                 aul_launch_app((char *)user_data, NULL);
2557                         }
2558                         _LOGE("consumer[%s] is launched !!!! \n", (char *)user_data);
2559                 }
2560         }
2561         return 0;
2562 }
2563
2564 static int __ri_find_svcapp(const pkgmgrinfo_appinfo_h handle, void *user_data)
2565 {
2566         int ret = 0;
2567         char *appid = NULL;
2568         char *component_type = NULL;
2569
2570         ret = pkgmgrinfo_appinfo_get_component_type(handle, &component_type);
2571         if (ret != PMINFO_R_OK) {
2572                 _LOGE("@Failed to get component_type\n");
2573                 return -1;
2574         }
2575
2576         if (strcmp(component_type, "svcapp") == 0) {
2577                 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2578                 if (ret != PMINFO_R_OK) {
2579                         _LOGE("@Failed to get appid\n");
2580                         return -1;
2581                 }
2582                 _LOGE("@find consumer[%s], check metadata for launch\n", appid);
2583
2584                 ret = pkgmgrinfo_appinfo_foreach_metadata(handle, __metadata_func, (void *)appid);
2585                 if (ret != PMINFO_R_OK) {
2586                         _LOGE("@Failed to get foreach_metadata\n");
2587                         return -1;
2588                 }
2589         }
2590
2591         return 0;
2592 }
2593
2594 static void __ri_launch_consumer(const char *pkgid)
2595 {
2596         int ret = 0;
2597         pkgmgrinfo_pkginfo_h pkghandle = NULL;
2598
2599         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
2600         if (ret < 0) {
2601                 _LOGE("@Failed to get pkginfo handle [%s]\n", pkgid);
2602                 return;
2603         }
2604
2605         ret = pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_find_svcapp, NULL);
2606         if (ret < 0) {
2607                 _LOGE("@Failed to get appinfo_get_list [%s]\n", pkgid);
2608                 return;
2609         }
2610         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
2611 }
2612
2613 static int __ail_change_info(int op, const char *appid)
2614 {
2615         void *lib_handle = NULL;
2616         int (*ail_desktop_operation) (const char *);
2617         char *aop = NULL;
2618         int ret = 0;
2619
2620         if ((lib_handle = dlopen(LIBAIL_PATH, RTLD_LAZY)) == NULL) {
2621                 _LOGE("dlopen is failed LIBAIL_PATH[%s]\n", LIBAIL_PATH);
2622                 goto END;
2623         }
2624
2625
2626         switch (op) {
2627                 case 0:
2628                         aop  = "ail_desktop_add";
2629                         break;
2630                 case 1:
2631                         aop  = "ail_desktop_update";
2632                         break;
2633                 case 2:
2634                         aop  = "ail_desktop_remove";
2635                         break;
2636                 case 3:
2637                         aop  = "ail_desktop_clean";
2638                         break;
2639                 case 4:
2640                         aop  = "ail_desktop_fota";
2641                         break;
2642                 default:
2643                         goto END;
2644                         break;
2645         }
2646
2647         if ((ail_desktop_operation =
2648              dlsym(lib_handle, aop)) == NULL || dlerror() != NULL) {
2649                 _LOGE("can not find symbol \n");
2650
2651                 goto END;
2652         }
2653
2654         ret = ail_desktop_operation(appid);
2655
2656 END:
2657         if (lib_handle)
2658                 dlclose(lib_handle);
2659
2660         return ret;
2661 }
2662
2663 static int __ri_update_ail_info(const pkgmgrinfo_appinfo_h handle, void *user_data)
2664 {
2665         int ret = 0;
2666         char *appid = NULL;
2667
2668         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
2669         if (ret < 0) {
2670                 _LOGE("Failed to execute pkgmgrinfo_appinfo_get_appid[%d].\n", ret);
2671                 return ret;
2672         }
2673
2674         ret = __ail_change_info(AIL_INSTALL, appid);
2675         if (ret < 0) {
2676                 _LOGE("Failed to execute __ail_change_info[%s].\n", appid);
2677         }
2678
2679         return ret;
2680 }
2681
2682 static int __child_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
2683 {
2684         int ret = 0;
2685         char *pkgid = NULL;
2686
2687         ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
2688         if(ret < 0) {
2689                 _LOGE("get_pkgid failed\n");
2690                 return ret;
2691         }
2692
2693         _LOGD("@child pkgid is [%s] for uninstallation", pkgid);
2694
2695         ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
2696         if(ret != 0) {
2697                 _LOGE("uninstall pkg(%s) failed\n",pkgid );
2698         }
2699
2700         return ret;
2701 }
2702
2703 static void __uninstall_child_package_by_mother_pkgid(const char *pkgid)
2704 {
2705         int ret = 0;
2706         pkgmgrinfo_pkginfo_filter_h handle = NULL;
2707
2708         ret = pkgmgrinfo_pkginfo_filter_create(&handle);
2709         if (ret != 0) {
2710                 _LOGE("filter_create failed for (%s)\n", pkgid);
2711                 return;
2712         }
2713
2714         ret = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID, pkgid);
2715         if (ret < 0) {
2716                 _LOGE("PMINFO_PKGINFO_PROP_PACKAGE_STORECLIENT_ID add failed\n");
2717                 goto end;
2718         }
2719
2720         ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __child_list_cb, NULL);
2721         if (ret < 0) {
2722                 _LOGE("foreach_pkginfo failed\n");
2723         }
2724
2725 end:
2726         pkgmgrinfo_pkginfo_filter_destroy(handle);
2727 }
2728
2729 int _rpm_install_pkg_with_dbpath(char *pkgfilepath, char *pkgid, char *clientid)
2730 {
2731         int ret = 0;
2732         char manifest[BUF_SIZE] = { '\0'};
2733         char resultxml[BUF_SIZE] = {'\0'};
2734         char cwd[BUF_SIZE] = {'\0'};
2735         int home_dir = 0;
2736         char *temp = NULL;
2737 #ifdef APP2EXT_ENABLE
2738         app2ext_handle *handle = NULL;
2739         GList *dir_list = NULL;
2740         pkgmgrinfo_install_location location = 1;
2741         int size = -1;
2742         unsigned long rpm_size = 0;
2743 #endif
2744         /*send event for start*/
2745         _ri_broadcast_status_notification(pkgid, "rpm", "start", "install");
2746         _LOGD("[#]start : _rpm_install_pkg_with_dbpath");
2747
2748         /*getcwd*/
2749         temp = getcwd(cwd, BUF_SIZE);
2750         if ((temp == NULL) ||(cwd[0] == '\0')) {
2751                 _LOGE("@failed to get the current directory info.");
2752                 ret = RPM_INSTALLER_ERR_INTERNAL;
2753                 goto err;
2754         }
2755         _LOGD("#current working directory is %s", cwd);
2756
2757         /*change dir*/
2758         ret = __ri_change_dir(TEMP_DIR);
2759         if (ret == -1) {
2760                 _LOGE("@failed to change directory.");
2761                 ret = RPM_INSTALLER_ERR_INTERNAL;
2762                 goto err;
2763         }
2764         _LOGD("#switched to %s", TEMP_DIR);
2765
2766         /*run cpio script*/
2767         const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
2768         ret = _ri_xsystem(cpio_argv);
2769
2770         /*get manifext.xml path*/
2771         snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
2772         _LOGD("#manifest name is %s", manifest);
2773
2774         if (access(manifest, F_OK)) {
2775                 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
2776
2777                 memset(manifest, '\0', sizeof(manifest));
2778                 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
2779                 _LOGD("#manifest name is %s", manifest);
2780
2781                 if (access(manifest, F_OK)) {
2782                         _LOGE("@can not find manifest.xml in the pkg.");
2783                         ret = RPM_INSTALLER_ERR_NO_MANIFEST;
2784                         goto err;
2785                 } else {
2786                         home_dir = 0;
2787                 }
2788
2789 #if 0   //disable "copy ro-xml to rw-xml", because of some bug
2790                 snprintf(srcpath, BUF_SIZE, "%s", manifest);
2791                 memset(manifest, '\0', sizeof(manifest));
2792                 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
2793
2794                 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
2795                 ret = _ri_xsystem(xml_update_argv);
2796 #endif
2797         } else {
2798                 home_dir = 1;
2799         }
2800         /*send event for install_percent*/
2801         _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
2802
2803         /*check manifest.xml validation*/
2804         ret = pkgmgr_parser_check_manifest_validation(manifest);
2805         if(ret < 0) {
2806                 _LOGE("@invalid manifest");
2807                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
2808                 goto err;
2809         }
2810
2811         /*check for signature and certificate*/
2812         ret = _ri_verify_signatures(TEMP_DIR, pkgid);
2813     if (ret < 0) {
2814         _LOGE("@signature and certificate failed(%s).", pkgid);
2815                 ret = RPM_INSTALLER_ERR_SIG_INVALID;
2816                 goto err;
2817     }
2818     _LOGD("#_ri_verify_signatures success.");
2819
2820     /*chdir*/
2821         ret = chdir(cwd);
2822         if (ret != 0) {
2823                 _LOGE("@failed to change directory(%s)(%s)", cwd, strerror(errno));
2824                 ret = RPM_INSTALLER_ERR_INTERNAL;
2825                 goto err;
2826         }
2827
2828 #ifdef APP2EXT_ENABLE
2829         ret = __get_location_from_xml(manifest, &location);
2830         if (ret < 0) {
2831                 _LOGE("@Failed to get install location\n");
2832                 ret = RPM_INSTALLER_ERR_INTERNAL;
2833                 goto err;
2834         } else {
2835                 if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
2836                         _LOGD("#Install: external storage location");
2837
2838                         /*Get the rpm's size from rpm header*/
2839                         rpm_size = _ri_calculate_rpm_size(pkgfilepath);
2840                         if(rpm_size != 0){
2841                                 rpm_size = rpm_size/(1024*1024);      //rpm size in MB
2842                                 _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
2843
2844                                 /*Add margin to the rpm size*/
2845                                 rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
2846                                 _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
2847                         }else{
2848                                 _LOGE("@Failed to get size from rpm header\n");
2849                                 ret = RPM_INSTALLER_ERR_INTERNAL;
2850                                 goto err;
2851                         }
2852
2853                         /*
2854                         Get the size from the manifest file.
2855                         */
2856                         ret = __get_size_from_xml(manifest, &size);
2857                         if (ret != PMINFO_R_OK) {
2858                                 size = rpm_size;
2859                                 _LOGD(" #rpm size is %d MB",size);
2860                         }else{
2861                                 size = size>rpm_size?size:rpm_size;
2862                                 _LOGD("#rpm size is %d MB",size);
2863                         }
2864                 }
2865         }
2866
2867         if ((location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) && size > 0) {
2868                 handle = app2ext_init(APP2EXT_SD_CARD);
2869                 if (handle == NULL) {
2870                         _LOGE("@app2ext init failed\n");
2871                         ret = RPM_INSTALLER_ERR_INTERNAL;
2872                         goto err;
2873                 }
2874                 if ((&(handle->interface) != NULL) && (handle->interface.pre_install != NULL) && (handle->interface.post_install != NULL)){
2875                         dir_list = __rpm_populate_dir_list();
2876                         if (dir_list == NULL) {
2877                                 _LOGE("@ \nError in populating the directory list\n");
2878                                 app2ext_deinit(handle);
2879                                 ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
2880                                 goto err;
2881                         }
2882                         ret = handle->interface.pre_install(gpkgname, dir_list, size);
2883                         if (ret == APP2EXT_ERROR_MMC_STATUS) {
2884                                 _LOGE("@app2xt MMC is not here, go internal\n");
2885                         } else if (ret == APP2EXT_SUCCESS){
2886                                 _LOGE("@pre_install done, go internal\n");
2887                         }
2888                         else {
2889                                 _LOGE("@app2xt pre install API failed (%d)\n", ret);
2890                                 __rpm_clear_dir_list(dir_list);
2891                                 handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2892                                 app2ext_deinit(handle);
2893                                 ret = RPM_INSTALLER_ERR_INTERNAL;
2894                                 goto err;
2895                         }
2896                 }
2897         }
2898 #endif
2899
2900
2901         /*run script*/
2902         if (home_dir == 0) {
2903 #if 0   //disable "INSTALL_SCRIPT_WITH_DBPATH_RO", because of some bug
2904                 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
2905                 ret = _ri_xsystem(argv);
2906 #endif
2907                 const char *argv[] = { INSTALL_SCRIPT, pkgfilepath, NULL };
2908                 ret = _ri_xsystem(argv);
2909         } else {
2910                 const char *argv[] = { INSTALL_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
2911                 ret = _ri_xsystem(argv);
2912         }
2913         if (ret != 0) {
2914                 _LOGE("@failed to install the pkg(%d).", ret);
2915 #ifdef APP2EXT_ENABLE
2916                 if ((handle != NULL) && (handle->interface.post_install != NULL)){
2917                         __rpm_clear_dir_list(dir_list);
2918                         handle->interface.post_install(gpkgname, APP2EXT_STATUS_FAILED);
2919                         app2ext_deinit(handle);
2920                 }
2921 #endif
2922                 goto err;
2923         }
2924         _LOGD("#install success.");
2925
2926         /*write the storeclient-id to manifest.xml*/
2927         if (clientid != NULL) {
2928                 if (home_dir == 0) {
2929                         snprintf(resultxml, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
2930                 } else {
2931                         snprintf(resultxml, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
2932                 }
2933
2934                 const char *convert_argv[] = { RPM_UPDATE_XML, manifest, clientid, resultxml, NULL };
2935                 ret = _ri_xsystem(convert_argv);
2936                 if (ret != 0) {
2937                         _LOGE("@Failed to convert the manifest.xml");
2938                         goto err;
2939                 }
2940
2941                 _LOGD("#client id[%s], input manifest:[%s], dest manifest:[%s]", clientid, manifest, resultxml);
2942         }
2943
2944         /*send event for install_percent*/
2945         _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
2946
2947 #ifdef APP2EXT_ENABLE
2948         if ((handle != NULL) && (handle->interface.post_install != NULL)){
2949                 __rpm_clear_dir_list(dir_list);
2950                 handle->interface.post_install(gpkgname, APP2EXT_STATUS_SUCCESS);
2951                 app2ext_deinit(handle);
2952         }
2953 #endif
2954
2955         /*Parse the manifest to get install location and size. If installation fails, remove manifest info from DB*/
2956         if (clientid != NULL) {
2957                 ret = pkgmgr_parser_parse_manifest_for_installation(resultxml, NULL);
2958         } else {
2959                 ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
2960         }
2961         if (ret < 0) {
2962                 _LOGE("@failed to parse the manifest.");
2963                 ret = RPM_INSTALLER_ERR_INTERNAL;
2964                 goto err;
2965         }
2966         _LOGD("#manifest parsing success");
2967
2968         /*register cert info*/
2969         _ri_register_cert(pkgid);
2970
2971         /*search_ug_app*/
2972         _coretpk_installer_search_ui_gadget(pkgid);
2973
2974         /*apply smack to shared dir*/
2975         __rpm_apply_smack(pkgid, 1);
2976
2977         /*apply smack by privilege*/
2978         ret = _ri_apply_privilege(pkgid, 0);
2979         if (ret != 0) {
2980                 _LOGE("@failed to apply permission(%d).", ret);
2981         }
2982         _LOGD("#permission applying success.");
2983
2984         /*reload smack*/
2985         ret = _ri_smack_reload_all();
2986         if (ret != 0) {
2987                 _LOGD("@failed to reload_all the smack.");
2988         }
2989
2990         /*send event for install_percent*/
2991         _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
2992
2993 err:
2994         _rpm_delete_dir(TEMP_DIR);
2995         _rpm_delete_dir(TEMP_DBPATH);
2996
2997          if (ret == RPM_INSTALLER_SUCCESS) {
2998                  _LOGD("[#]end : _rpm_install_pkg_with_dbpath");
2999                  __ri_launch_consumer(pkgid);
3000                 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3001                 _ri_stat_cb(pkgid, "end", "ok");
3002         } else {
3003                 _LOGE("[@]end : _rpm_install_pkg_with_dbpath");
3004                 /*remove db info*/
3005                 ret = _coretpk_installer_remove_db_info(pkgid);
3006                 if (ret < 0) {
3007                         _LOGE("_coretpk_installer_remove_db_info is failed.");
3008                 }
3009
3010                 char *errstr = NULL;
3011                 _ri_error_no_to_string(ret, &errstr);
3012                 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3013                 _ri_stat_cb(pkgid, "error", errstr);
3014                 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3015                 sleep(2);
3016                 _ri_stat_cb(pkgid, "end", "fail");
3017                 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3018         }
3019
3020         return ret;
3021 }
3022
3023 int _rpm_upgrade_pkg_with_dbpath(char *pkgfilepath, char *pkgid)
3024 {
3025         int ret = 0;
3026         char manifest[BUF_SIZE] = { '\0'};
3027         char cwd[BUF_SIZE] = {'\0'};
3028         int home_dir = 0;
3029         pkgmgrinfo_pkginfo_h pkghandle;
3030         char *temp = NULL;
3031 #ifdef APP2EXT_ENABLE
3032         app2ext_handle *handle = NULL;
3033         GList *dir_list = NULL;
3034         pkgmgrinfo_installed_storage location = 1;
3035         int size = -1;
3036         unsigned long rpm_size = 0;
3037 #endif
3038         _ri_broadcast_status_notification(pkgid, "rpm", "start", "update");
3039         _LOGD("[#]start : _rpm_upgrade_pkg_with_dbpath");
3040
3041         /*terminate running app*/
3042         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3043         if (ret < 0) {
3044                 _LOGE("@failed to get pkginfo handle");
3045                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3046                 goto err;
3047         }
3048         pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3049         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3050
3051         /*getcwd*/
3052         temp = getcwd(cwd, BUF_SIZE);
3053         if ((temp == NULL) ||(cwd[0] == '\0')) {
3054                 _LOGE("@getcwd() failed.");
3055                 ret = RPM_INSTALLER_ERR_INTERNAL;
3056                 goto err;
3057         }
3058         _LOGD("#current working directory is %s.", cwd);
3059
3060         /*change dir*/
3061         ret = __ri_change_dir(TEMP_DIR);
3062         if (ret == -1) {
3063                 _LOGE("@change dir failed.");
3064                 ret = RPM_INSTALLER_ERR_INTERNAL;
3065                 goto err;
3066         }
3067         _LOGD("#switched to %s", TEMP_DIR);
3068
3069         /*run cpio script*/
3070         const char *cpio_argv[] = { CPIO_SCRIPT, pkgfilepath, NULL };
3071         ret = _ri_xsystem(cpio_argv);
3072
3073         /*get manifext.xml path*/
3074         snprintf(manifest, BUF_SIZE, "%s/opt/share/packages/%s.xml", TEMP_DIR, pkgid);
3075         _LOGD("#manifest name is %s.", manifest);
3076
3077         if (access(manifest, F_OK)) {
3078                 _LOGD("#there is no RW manifest.xml. check RO manifest.xml.");
3079
3080                 memset(manifest, '\0', sizeof(manifest));
3081                 snprintf(manifest, BUF_SIZE, "%s/usr/share/packages/%s.xml", TEMP_DIR, pkgid);
3082                 _LOGD("#manifest name is %s.", manifest);
3083
3084                 if (access(manifest, F_OK)) {
3085                         _LOGE("@can not find manifest.xml in the pkg.");
3086                         ret = RPM_INSTALLER_ERR_NO_MANIFEST;
3087                         goto err;
3088                 } else {
3089                         home_dir = 0;
3090                 }
3091
3092 #if 0   //disable "copy ro-xml to rw-xml", because of some bug
3093                 snprintf(srcpath, BUF_SIZE, "%s", manifest);
3094                 memset(manifest, '\0', sizeof(manifest));
3095                 snprintf(manifest, BUF_SIZE, "%s/%s.xml", MANIFEST_RW_DIRECTORY, pkgid);
3096
3097                 const char *xml_update_argv[] = { CPIO_SCRIPT_UPDATE_XML, srcpath, manifest, NULL };
3098                 ret = _ri_xsystem(xml_update_argv);
3099 #endif
3100         } else {
3101                 home_dir = 1;
3102         }
3103
3104         /*send event for install_percent*/
3105         _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "30");
3106
3107         /*check manifest.xml validation*/
3108         ret = pkgmgr_parser_check_manifest_validation(manifest);
3109         if(ret < 0) {
3110                 _LOGE("@invalid manifest");
3111                 ret = RPM_INSTALLER_ERR_INVALID_MANIFEST;
3112                 goto err;
3113         }
3114
3115         /*check for signature and certificate*/
3116         ret = _ri_verify_signatures(TEMP_DIR, pkgid);
3117     if (ret < 0) {
3118                 _LOGE("@signature and certificate failed(%s).", pkgid);
3119                 ret = RPM_INSTALLER_ERR_SIG_INVALID;
3120                 goto err;
3121     }
3122     _LOGD("#_ri_verify_signatures success.");
3123
3124     /*chdir*/
3125         ret = chdir(cwd);
3126         if (ret != 0) {
3127                 _LOGE("@chdir(%s) failed(%s).", cwd, strerror(errno));
3128                 ret = RPM_INSTALLER_ERR_INTERNAL;
3129                 goto err;
3130         }
3131
3132     /*remove dir for clean*/
3133         __ri_remove_updated_dir(pkgid);
3134
3135         _LOGD("#Preserve the smack file");
3136         /*Preserve the smack rule file */
3137         ret = __ri_copy_smack_rule_file(UPGRADE_REQ,pkgid,0);
3138         if(ret != RPM_INSTALLER_SUCCESS)
3139                 goto err;
3140 #ifdef APP2EXT_ENABLE
3141         ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname, &pkghandle);
3142         if (ret < 0) {
3143                 _LOGE("Failed to get pkginfo handle\n");
3144                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3145                 goto err;
3146         } else {
3147                 ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
3148                 if (ret < 0) {
3149                         _LOGE("Failed to get install location\n");
3150                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3151                         ret = RPM_INSTALLER_ERR_INTERNAL;
3152                         goto err;
3153                 } else {
3154                         if (location == PMINFO_EXTERNAL_STORAGE) {
3155                                 /*Get the rpm's size from rpm header*/
3156                                 rpm_size = _ri_calculate_rpm_size(pkgfilepath);
3157                                 if(rpm_size != 0){
3158                                         rpm_size = rpm_size/(1024*1024);      //rpm size in MB
3159                                         _LOGD("#Rpm file(%s) size is %lu MB",pkgfilepath,rpm_size);
3160
3161                                         /*Add margin to the rpm size*/
3162                                         rpm_size = rpm_size + RPM_SIZE_MARGIN(rpm_size);
3163                                         _LOGD("#Rpm file (%s) size after margin is %lu MB",pkgfilepath,rpm_size);
3164                                 }else{
3165                                         _LOGE("@Failed to get size from rpm header\n");
3166                                         ret = RPM_INSTALLER_ERR_INTERNAL;
3167                                         goto err;
3168                                 }
3169
3170                                 /*
3171                                 Get the size from the manifest file.
3172                                 */
3173                                 ret = __get_size_from_xml(manifest, &size);
3174                                 if (ret != PMINFO_R_OK) {
3175                                         size = rpm_size;
3176                                         _LOGD(" #rpm size is %d",size);
3177                                 }else{
3178                                         size = size>rpm_size?size:rpm_size;
3179                                         _LOGD("#rpm size is %d",size);
3180                                 }
3181                         }
3182                 }
3183                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3184                 if ((location == PMINFO_EXTERNAL_STORAGE) && size > 0) {
3185                         handle = app2ext_init(APP2EXT_SD_CARD);
3186                         if (handle == NULL) {
3187                                 _LOGE("app2ext init failed\n");
3188                                 ret = RPM_INSTALLER_ERR_INTERNAL;
3189                                 goto err;
3190                         }
3191                         if ((&(handle->interface) != NULL) && (handle->interface.pre_upgrade != NULL) && (handle->interface.post_upgrade != NULL)){
3192                                 dir_list = __rpm_populate_dir_list();
3193                                 if (dir_list == NULL) {
3194                                         _LOGE("\nError in populating the directory list\n");
3195                                         ret = RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
3196                                         app2ext_deinit(handle);
3197                                         goto err;
3198                                 }
3199                                 ret = handle->interface.pre_upgrade(gpkgname, dir_list, size);
3200                                 if (ret == APP2EXT_ERROR_MMC_STATUS ) {
3201                                         _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3202                                 }else if(ret == APP2EXT_SUCCESS){
3203                                         _LOGE("pre upgrade done, go internal");
3204                                 }else {
3205                                         _LOGE("app2xt pre upgrade API failed (%d)\n", ret);
3206                                         __rpm_clear_dir_list(dir_list);
3207                                         handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3208                                         ret = RPM_INSTALLER_ERR_INTERNAL;
3209                                         app2ext_deinit(handle);
3210                                         goto err;
3211                                 }
3212                         }
3213                 }
3214         }
3215 #endif
3216
3217         /*run script*/
3218         if (home_dir == 0) {
3219 #if 0   //disable "UPGRADE_SCRIPT_WITH_DBPATH_RO", because of some bug
3220                 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RO, pkgfilepath, NULL };
3221                 ret = _ri_xsystem(argv);
3222 #endif
3223                 const char *argv[] = { UPGRADE_SCRIPT, pkgfilepath, NULL };
3224                 ret = _ri_xsystem(argv);
3225         } else {
3226                 const char *argv[] = { UPGRADE_SCRIPT_WITH_DBPATH_RW, pkgfilepath, NULL };
3227                 ret = _ri_xsystem(argv);
3228         }
3229         if (ret != 0) {
3230                 _LOGE("@upgrade complete with error(%d)", ret);
3231 #ifdef APP2EXT_ENABLE
3232                 if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3233                         __rpm_clear_dir_list(dir_list);
3234                         handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_FAILED);
3235                         app2ext_deinit(handle);
3236                 }
3237 #endif
3238                 goto err;
3239         }
3240         _LOGD("#upgrade script success.");
3241
3242         /*send event for install_percent*/
3243         _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "60");
3244
3245         /*Parse the manifest to get install location and size. If fails, remove manifest info from DB.*/
3246         ret = pkgmgr_parser_parse_manifest_for_upgrade(manifest, NULL);
3247         if (ret < 0) {
3248                 _LOGE("@parsing manifest failed.");
3249                 ret = RPM_INSTALLER_ERR_INTERNAL;
3250                 goto err;
3251         }
3252         _LOGD("#parsing manifest success.");
3253
3254         /*unregister cert info*/
3255         _ri_unregister_cert(pkgid);
3256
3257         /*register cert info*/
3258         _ri_register_cert(pkgid);
3259
3260 #ifdef APP2EXT_ENABLE
3261         if ((handle != NULL) && (handle->interface.post_upgrade != NULL)){
3262                 __rpm_clear_dir_list(dir_list);
3263                 handle->interface.post_upgrade(gpkgname, APP2EXT_STATUS_SUCCESS);
3264                 app2ext_deinit(handle);
3265         }
3266 #endif
3267
3268         /*search_ug_app*/
3269         _coretpk_installer_search_ui_gadget(pkgid);
3270
3271         /*apply smack to shared dir*/
3272         __rpm_apply_smack(pkgid, 1);
3273
3274         /*apply smack by privilege*/
3275         ret = _ri_apply_privilege(pkgid, 0);
3276         if (ret != 0) {
3277                 _LOGE("@apply perm failed with err(%d)", ret);
3278         }
3279         _LOGD("#apply perm success.");
3280
3281         /*reload smack*/
3282         ret = _ri_smack_reload_all();
3283         if (ret != 0) {
3284                 _LOGD("_ri_smack_reload_all failed.");
3285         }
3286
3287         /*send event for install_percent*/
3288         _ri_broadcast_status_notification(pkgid, "rpm", "install_percent", "100");
3289
3290 err:
3291         _rpm_delete_dir(TEMP_DIR);
3292         _rpm_delete_dir(TEMP_DBPATH);
3293
3294          if (ret == RPM_INSTALLER_SUCCESS) {
3295                  _LOGD("[#]end : _rpm_upgrade_pkg_with_dbpath");
3296                  __ri_launch_consumer(pkgid);
3297                 _ri_broadcast_status_notification(pkgid, "rpm", "end", "ok");
3298                 _ri_stat_cb(pkgid, "end", "ok");
3299         } else {
3300                  _LOGE("[@]end : _rpm_upgrade_pkg_with_dbpath");
3301                 char *errstr = NULL;
3302                 _ri_error_no_to_string(ret, &errstr);
3303                 _ri_broadcast_status_notification(pkgid, "rpm", "error", errstr);
3304                 _ri_stat_cb(pkgid, "error", errstr);
3305                 _ri_broadcast_status_notification(pkgid, "rpm", "end", "fail");
3306                 sleep(2);
3307                 _ri_stat_cb(pkgid, "end", "fail");
3308                 _LOGE("install failed with err(%d) (%s)\n", ret, errstr);
3309         }
3310
3311         return ret;
3312 }
3313
3314 int _rpm_uninstall_pkg_with_dbpath(const char *pkgid, bool is_system)
3315 {
3316         if (pkgid == NULL) {
3317                 _LOGE("pkgid is NULL.");
3318                 return -1;
3319         }
3320
3321         int ret = 0;
3322         char buff[BUF_SIZE] = {'\0'};
3323         char tizen_manifest[BUF_SIZE] = {'\0'};
3324         pkgmgrinfo_pkginfo_h pkghandle = NULL;
3325         bool mother_package = false;
3326         bool coretpk = false;
3327
3328         #ifdef APP2EXT_ENABLE
3329         app2ext_handle *handle = NULL;
3330         pkgmgrinfo_installed_storage location = 1;
3331         #endif
3332
3333         _LOGD("pkgid=[%s], is_system=[%d]", pkgid, is_system);
3334
3335         snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3336         if (access(tizen_manifest, R_OK) == 0) {
3337                 coretpk = true;
3338                 _LOGD("[%s] is existed.", tizen_manifest);
3339         }
3340
3341         // send start event
3342         if (is_system)
3343                 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "update");
3344         else
3345                 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "start", "uninstall");
3346
3347         // terminate running app
3348         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3349         if (ret < 0) {
3350                 _LOGE("pkgmgrinfo_pkginfo_get_pkginfo(%s) failed.", pkgid);
3351                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3352                 goto end;
3353         }
3354         pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3355
3356         // If package is mother package, then uninstall child package
3357         pkgmgrinfo_pkginfo_is_mother_package(pkghandle, &mother_package);
3358         if (mother_package == true) {
3359                 _LOGD("[%s] is mother package", pkgid);
3360                 __uninstall_child_package_by_mother_pkgid(pkgid);
3361         }
3362         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3363
3364         snprintf(tizen_manifest, BUF_SIZE, "%s/%s/tizen-manifest.xml", OPT_USR_APPS, pkgid);
3365         if (access(tizen_manifest, R_OK) == 0) {
3366                 coretpk = true;
3367                 _LOGD("[%s] is existing.", tizen_manifest);
3368         }
3369
3370         // del root path dir
3371         snprintf(buff, BUF_SIZE, "%s/%s", OPT_USR_APPS, pkgid);
3372         if (__is_dir(buff)) {
3373                 _rpm_delete_dir(buff);
3374         }
3375
3376         // del root path dir for ext
3377         if (_coretpk_installer_get_configuration_value(INI_VALUE_MAKE_EXT_DIRECTORY)) {
3378                 char extpath[BUF_SIZE] = {'\0'};
3379                 snprintf(extpath, BUF_SIZE, "%s/%s", OPT_STORAGE_SDCARD_APP_ROOT, pkgid);
3380                 if (__is_dir(extpath)) {
3381                         _rpm_delete_dir(extpath);
3382                 }
3383         }
3384
3385         // del manifest
3386         memset(buff, '\0', BUF_SIZE);
3387         snprintf(buff, BUF_SIZE, "%s/%s.xml", OPT_SHARE_PACKAGES, pkgid);
3388         (void)remove(buff);
3389
3390         // check system pkg, if pkg is system pkg, need to update xml on USR_SHARE_PACKAGES
3391         if (is_system) {
3392                 memset(buff, '\0', BUF_SIZE);
3393                 snprintf(buff, BUF_SIZE, "%s/%s.xml", USR_SHARE_PACKAGES, pkgid);
3394                 _LOGE("manifest for upgrade, path=[%s]",buff);
3395                 ret = pkgmgr_parser_parse_manifest_for_upgrade(buff, NULL);
3396                 if (ret < 0) {
3397                         _LOGE("parsing manifest failed.");
3398                 }
3399                 goto end;
3400         } else {
3401 #ifdef APP2EXT_ENABLE
3402                 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3403                 if (ret < 0) {
3404                         _LOGE("failed to get pkginfo handle");
3405                         ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3406                         goto end;
3407                 }
3408                 ret = pkgmgrinfo_pkginfo_get_installed_storage(pkghandle, &location);
3409                 if (ret < 0) {
3410                         _LOGE("failed to get install location\n");
3411                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3412                         return RPM_INSTALLER_ERR_INTERNAL;
3413                 }
3414
3415                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3416                 if (location == PMINFO_EXTERNAL_STORAGE) {
3417                         handle = app2ext_init(APP2EXT_SD_CARD);
3418                         if (handle == NULL) {
3419                                 _LOGE("app2ext init failed\n");
3420                                 return RPM_INSTALLER_ERR_INTERNAL;
3421                         }
3422                         if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) && (handle->interface.post_uninstall != NULL) &&
3423                                         (handle->interface.disable != NULL)){
3424                                 ret = handle->interface.disable(pkgid);
3425                                 if (ret != APP2EXT_SUCCESS) {
3426                                         _LOGE("Unmount ret[%d]", ret);
3427                                 }
3428                                 ret = app2ext_get_app_location(pkgid);
3429                                 if (ret == APP2EXT_INTERNAL_MEM){
3430                                         _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
3431                                 }
3432                                 else {
3433                                         ret = handle->interface.pre_uninstall(pkgid);
3434                                         if (ret == APP2EXT_ERROR_MMC_STATUS ) {
3435                                                 _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3436                                         }else if (ret == APP2EXT_SUCCESS){
3437                                                 _LOGE("pre uninstall done, go to internal");
3438                                         }else {
3439                                                 _LOGE("app2xt pre uninstall API failed (%d)\n", ret);
3440                                                 handle->interface.post_uninstall(pkgid);
3441                                                 app2ext_deinit(handle);
3442                                                 return RPM_INSTALLER_ERR_INTERNAL;
3443                                         }
3444                                 }
3445                         }
3446                 }
3447 #endif
3448
3449                 // del db info
3450                 ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
3451                 if (ret < 0) {
3452                         _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation() failed, pkgid=[%s]", pkgid);
3453                 }
3454
3455 #ifdef APP2EXT_ENABLE
3456                 if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3457                         handle->interface.post_uninstall(pkgid);
3458                         app2ext_deinit(handle);
3459                 }
3460 #endif
3461
3462         }
3463
3464         // execute privilege APIs
3465         _ri_privilege_revoke_permissions(pkgid);
3466         _ri_privilege_unregister_package(pkgid);
3467
3468         // Unregister cert info
3469         _ri_unregister_cert(pkgid);
3470
3471 end:
3472         // Restore the old smack file
3473         if (coretpk == false) {
3474                 ret = __ri_copy_smack_rule_file(UNINSTALL_REQ,pkgid,is_system);
3475                 if (ret != RPM_INSTALLER_SUCCESS){
3476                         _LOGD("smack restore failed");
3477                 }else{
3478                         /*reload smack*/
3479                         ret = _ri_smack_reload_all();
3480                         if (ret != 0) {
3481                                 _LOGD("_ri_smack_reload_all failed.");
3482                         }
3483                 }
3484         }
3485
3486         if (ret != 0) {
3487                 _LOGE("failed, ret=[%d]", ret);
3488                 char *errstr = NULL;
3489                 _ri_error_no_to_string(ret, &errstr);
3490                 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "error",  errstr);
3491                 _ri_stat_cb(pkgid, "error", errstr);
3492                 sleep(2);
3493                 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end",  "fail");
3494                 _ri_stat_cb(pkgid, "end", "fail");
3495                 _LOGE("remove failed with err(%d) (%s)\n", ret, errstr);
3496         } else {
3497                 _LOGE("success");
3498                 _ri_broadcast_status_notification(pkgid, coretpk ? "coretpk" : "rpm", "end", "ok");
3499                 _ri_stat_cb(pkgid, "end", "ok");
3500         }
3501
3502         return ret;
3503 }
3504
3505 int _rpm_uninstall_pkg(char *pkgid)
3506 {
3507         int ret = 0;
3508         int err = 0;
3509         bool is_update = 0;
3510         bool is_system = 0;
3511         bool is_removable = 0;
3512         char buff[BUF_SIZE] = {'\0'};
3513         pkgmgrinfo_install_location location = 1;
3514 #ifdef APP2EXT_ENABLE
3515         app2ext_handle *handle = NULL;
3516 #endif
3517         char *manifest = NULL;
3518         pkgmgrinfo_pkginfo_h pkghandle;
3519         const char *argv[] = { UNINSTALL_SCRIPT, pkgid, NULL };
3520
3521         _LOGD("start : _rpm_uninstall_pkg\n");
3522
3523         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkghandle);
3524         if (ret < 0) {
3525                 _LOGE("Failed to get pkginfo handle\n");
3526                 return RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3527         }
3528         /*terminate running app*/
3529         pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3530
3531         ret = pkgmgrinfo_pkginfo_is_system(pkghandle, &is_system);
3532         if (ret < 0) {
3533                 _LOGE("pkgmgrinfo_pkginfo_is_system failed.\n");
3534                 return RPM_INSTALLER_ERR_INTERNAL;
3535         }
3536         if (is_system) {
3537                 ret = pkgmgrinfo_pkginfo_is_update(pkghandle, &is_update);
3538                 if (ret < 0) {
3539                         _LOGE("pkgmgrinfo_pkginfo_is_system failed.\n");
3540                         return RPM_INSTALLER_ERR_INTERNAL;
3541                 }
3542                 if (is_update) {
3543                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3544                         /*updated and system pkg need to "remove-update"*/
3545                         _LOGD("Remove Update[%s]",pkgid);
3546                         ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 1);
3547                         if (ret < 0) {
3548                                 _LOGE("uninstall_pkg_with_dbpath for system, is_update fail\n");
3549                         }
3550                         return 0;
3551                 }
3552         } else {
3553                 pkgmgrinfo_pkginfo_is_removable(pkghandle, &is_removable);
3554                 if (is_removable) {
3555                         /*non-system and can be removable,  it should be deleted*/
3556                         _LOGD("Delete Package [%s]",pkgid);
3557                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3558                         ret = _rpm_uninstall_pkg_with_dbpath(pkgid, 0);
3559                         if (ret < 0) {
3560                                 _LOGE("uninstall_pkg_with_dbpath for non-system, is_remove fail\n");
3561                         }
3562                         return 0;
3563                 }
3564         }
3565
3566         _ri_broadcast_status_notification(pkgid, "rpm", "start", "uninstall");
3567
3568 #ifdef APP2EXT_ENABLE
3569         ret = pkgmgrinfo_pkginfo_get_install_location(pkghandle, &location);
3570         if (ret < 0) {
3571                 _LOGE("Failed to get install location\n");
3572                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3573                 return RPM_INSTALLER_ERR_INTERNAL;
3574         }
3575
3576         pkgmgrinfo_appinfo_get_list(pkghandle, PM_UI_APP, __ri_check_running_app, NULL);
3577
3578         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3579         if (location == PMINFO_INSTALL_LOCATION_PREFER_EXTERNAL) {
3580                 handle = app2ext_init(APP2EXT_SD_CARD);
3581                 if (handle == NULL) {
3582                         _LOGE("app2ext init failed\n");
3583                         return RPM_INSTALLER_ERR_INTERNAL;
3584                 }
3585                 if ((&(handle->interface) != NULL) && (handle->interface.pre_uninstall != NULL) && (handle->interface.post_uninstall != NULL)){
3586                         ret = app2ext_get_app_location(pkgid);
3587                         if (ret == APP2EXT_INTERNAL_MEM){
3588                                         _LOGE("app2xt APP is not in MMC, go internal (%d)\n", ret);
3589                         }
3590                         else {
3591                                 ret = handle->interface.pre_uninstall(pkgid);
3592                                 if (ret == APP2EXT_ERROR_MMC_STATUS || ret == APP2EXT_SUCCESS ) {
3593                                         _LOGE("app2xt MMC is not here, go internal (%d)\n", ret);
3594                                 }
3595                                 else {
3596                                         _LOGE("app2xt pre uninstall API failed (%d)\n", ret);
3597                                         handle->interface.post_uninstall(pkgid);
3598                                         app2ext_deinit(handle);
3599                                         return RPM_INSTALLER_ERR_INTERNAL;
3600                                 }
3601                         }
3602                 }
3603         }
3604 #endif
3605
3606 #ifdef PRE_CHECK_FOR_MANIFEST
3607         /*Manifest info should be removed first because after installation manifest
3608         file is uninstalled. If uninstallation fails, we need to re-insert manifest info for consistency*/
3609         manifest = pkgmgr_parser_get_manifest_file(pkgid);
3610         if (manifest == NULL) {
3611                 _LOGE("manifest name is NULL\n");
3612                 app2ext_deinit(handle);
3613                 return RPM_INSTALLER_ERR_NO_MANIFEST;
3614         }
3615         _LOGD("manifest name is %s\n", manifest);
3616         ret = pkgmgr_parser_parse_manifest_for_uninstallation(manifest, NULL);
3617         if (ret < 0) {
3618                 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
3619         }
3620
3621 #endif
3622
3623         ret = _rpm_xsystem(argv);
3624         if (ret != 0) {
3625                 _LOGE("uninstall failed with error(%d)\n", ret);
3626                 #ifdef PRE_CHECK_FOR_MANIFEST
3627                 err = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
3628                 if (err < 0) {
3629                         _LOGE("Parsing Manifest Failed\n");
3630                 }
3631                 if (manifest) {
3632                         free(manifest);
3633                         manifest = NULL;
3634                 }
3635                 #endif
3636                 #ifdef APP2EXT_ENABLE
3637                 if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3638                         handle->interface.post_uninstall(pkgid);
3639                         app2ext_deinit(handle);
3640                 }
3641                 #endif
3642                 return ret;
3643         }
3644
3645 #ifdef APP2EXT_ENABLE
3646         if ((handle != NULL) && (handle->interface.post_uninstall != NULL)){
3647                 handle->interface.post_uninstall(pkgid);
3648                 app2ext_deinit(handle);
3649         }
3650 #endif
3651
3652 #ifdef PRE_CHECK_FOR_MANIFEST
3653         if (manifest) {
3654                 free(manifest);
3655                 manifest = NULL;
3656         }
3657 #endif
3658         /* Uninstallation Success. Remove the installation time key from vconf*/
3659         snprintf(buff, BUF_SIZE, "db/app-info/%s/installed-time", pkgid);
3660         err = vconf_unset(buff);
3661         if (err) {
3662                 _LOGE("unset installation time failed\n");
3663         }
3664         /*execute privilege APIs*/
3665         _ri_privilege_revoke_permissions(pkgid);
3666         _ri_privilege_unregister_package(pkgid);
3667         /*Unregister cert info*/
3668         _ri_unregister_cert(gpkgname);
3669
3670         _LOGD("end : _rpm_uninstall_pkg(%d)\n", ret);
3671         return ret;
3672 }
3673
3674 int _rpm_install_corexml(char *pkgfilepath, char *pkgid)
3675 {
3676         int ret = 0;
3677         /*validate signature and certifictae*/
3678         ret = _ri_verify_signatures(USR_APPS, pkgid);
3679     if (ret < 0) {
3680                 _LOGE("_ri_verify_signatures Failed : %s\n", pkgid);
3681                 ret = RPM_INSTALLER_ERR_SIG_INVALID;
3682                 goto err;
3683     }
3684
3685         /* check : given pkgid is deactivation*/
3686         ret = __ri_check_pkgid_for_deactivation(pkgid);
3687         if (ret < 0) {
3688                 _LOGE("pkgid[%s] for deactivation dont need to install.\n", pkgid);
3689                 goto err;
3690         }
3691
3692         /* Parse and insert manifest in DB*/
3693     ret = pkgmgr_parser_parse_manifest_for_installation(pkgfilepath, NULL);
3694     if (ret < 0) {
3695                 _LOGD("Installing Manifest Failed : %s\n", pkgfilepath);
3696                 ret = RPM_INSTALLER_ERR_PACKAGE_NOT_INSTALLED;
3697                 goto err;
3698     }
3699
3700     // _ri_register_cert has __ri_free_cert_chain.
3701     _ri_register_cert(pkgid);
3702
3703         /*search_ug_app*/
3704         _coretpk_installer_search_ui_gadget(pkgid);
3705
3706         ret = RPM_INSTALLER_SUCCESS;
3707
3708 err:
3709         if (ret != 0) {
3710                 __ri_free_cert_chain();
3711         }
3712
3713         return ret;
3714
3715 }
3716
3717 int _rpm_move_pkg(char *pkgid, int move_type)
3718 {
3719         app2ext_handle *hdl = NULL;
3720         int ret = 0;
3721         int movetype = -1;
3722         GList *dir_list = NULL;
3723         pkgmgrinfo_pkginfo_h pkghandle = NULL;
3724
3725         if (move_type == PM_MOVE_TO_INTERNAL)
3726                 movetype = APP2EXT_MOVE_TO_PHONE;
3727         else if (move_type == PM_MOVE_TO_SDCARD)
3728                 movetype = APP2EXT_MOVE_TO_EXT;
3729         else
3730                 return RPM_INSTALLER_ERR_WRONG_PARAM;
3731
3732         ret = pkgmgrinfo_pkginfo_get_pkginfo(gpkgname,&pkghandle);
3733         if(ret < 0){
3734                 _LOGE("@failed to get the pkginfo handle!!");
3735                 ret = RPM_INSTALLER_ERR_PKG_NOT_FOUND;
3736                 return ret;
3737         }
3738         /* Terminate the running instance of app */
3739         pkgmgrinfo_appinfo_get_list(pkghandle,PM_UI_APP,__ri_check_running_app,NULL);
3740         pkgmgrinfo_pkginfo_destroy_pkginfo(pkghandle);
3741         hdl = app2ext_init(APP2EXT_SD_CARD);
3742         if ((hdl != NULL) && (hdl->interface.move != NULL)){
3743                 dir_list = __rpm_populate_dir_list();
3744                 if (dir_list == NULL) {
3745                         _LOGE("\nError in populating the directory list\n");
3746                         return RPM_INSTALLER_ERR_RPM_SCRIPT_WRONG_ARGS;
3747                 }
3748                 ret = hdl->interface.move(pkgid, dir_list, movetype);
3749                 __rpm_clear_dir_list(dir_list);
3750                 if (ret != 0) {
3751                         _LOGE("Failed to move app\n");
3752                         return RPM_INSTALLER_ERR_INTERNAL;
3753                 }else{
3754                         if(move_type == PM_MOVE_TO_INTERNAL){
3755                                 _LOGD("#updating the installed storage from external to internal");
3756                                 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_INTERNAL);
3757                         }
3758                         else{
3759                                 _LOGD("#updating the installed storage from internal to external");
3760                                 ret = pkgmgrinfo_pkginfo_set_installed_storage(pkgid, INSTALL_EXTERNAL);
3761                         }
3762
3763                         if(ret != PMINFO_R_OK){
3764                                 _LOGE("@failed to udpate the installed storage");
3765                                 return RPM_INSTALLER_ERR_INTERNAL;
3766                         }
3767                 }
3768                 app2ext_deinit(hdl);
3769                 return RPM_INSTALLER_SUCCESS;
3770         } else {
3771                 _LOGE("Failed to get app2ext handle\n");
3772                 return RPM_INSTALLER_ERR_INTERNAL;
3773         }
3774 }
3775
3776 int _rpm_process_cscxml(char *csc_script)
3777 {
3778         int ret = 0;
3779         int op_type = 0;
3780
3781         char *path_str = NULL;
3782         char *op_str = NULL;
3783         char *remove_str = NULL;
3784         char csc_str[BUF_SIZE] = {'\0'};
3785         snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
3786
3787         /*get params from csc script*/
3788         path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
3789         op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3790         remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
3791         if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
3792                 _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
3793                 goto end;
3794         }
3795         _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
3796
3797         /*get operation type*/
3798         op_type = __ri_get_op_type(op_str);
3799         if(op_type < 0){
3800                 _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
3801                 goto end;
3802         }
3803
3804         switch (op_type) {
3805                 case INSTALL_REQ:
3806                         ret = __ri_install_csc(path_str, remove_str);
3807                         break;
3808
3809                 case UPGRADE_REQ:
3810                         ret = __ri_install_csc(path_str, remove_str);
3811                         break;
3812
3813                 case UNINSTALL_REQ:
3814                         ret = __ri_uninstall_csc(path_str);
3815                         break;
3816
3817                 default:
3818                         break;
3819         }
3820
3821         if (ret < 0)
3822                 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",path_str, op_type);
3823
3824 end:
3825         if(path_str)
3826                 free(path_str);
3827         if(op_str)
3828                 free(op_str);
3829         if(remove_str)
3830                 free(remove_str);
3831
3832         return ret;
3833 }
3834
3835 int _rpm_process_csc_coretpk(char *csc_script)
3836 {
3837         int ret = 0;
3838         int op_type = 0;
3839
3840         char *path_str = NULL;
3841         char *op_str = NULL;
3842         char *remove_str = NULL;
3843         char csc_str[BUF_SIZE] = {'\0'};
3844         snprintf(csc_str, BUF_SIZE - 1, "%s:", csc_script);
3845
3846         /*get params from csc script*/
3847         path_str = __ri_get_str(csc_str, TOKEN_PATH_STR);
3848         op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3849         remove_str = __ri_get_str(csc_str, TOKEN_REMOVE_STR);
3850         if((path_str == NULL) || (op_str == NULL) || (remove_str == NULL)){
3851                 _LOGE("csc-info : input param is null[%s, %s, %s]\n", path_str, op_str, remove_str);
3852                 goto end;
3853         }
3854         _LOGD("csc-info : path=%s, op=%s, remove=%s\n", path_str, op_str, remove_str);
3855
3856         /*get operation type*/
3857         op_type = __ri_get_op_type(op_str);
3858         if(op_type < 0){
3859                 _LOGE("csc-info : operation error[%s, %s]\n", path_str, op_str);
3860                 goto end;
3861         }
3862
3863         switch (op_type) {
3864                 case INSTALL_REQ:
3865                         ret = _coretpk_installer_csc_install(path_str, remove_str);
3866                         break;
3867
3868                 case UPGRADE_REQ:
3869                         ret = _coretpk_installer_csc_install(path_str, remove_str);
3870                         break;
3871
3872                 case UNINSTALL_REQ:
3873                         ret = __ri_uninstall_csc(path_str);
3874                         break;
3875
3876                 default:
3877                         break;
3878         }
3879
3880         if (ret < 0)
3881                 _LOGE("csc-info : csc fail [pkgid=%s, operation=%d]\n",path_str, op_type);
3882
3883 end:
3884         if(path_str)
3885                 free(path_str);
3886         if(op_str)
3887                 free(op_str);
3888         if(remove_str)
3889                 free(remove_str);
3890
3891         return ret;
3892 }
3893
3894 int _rpm_process_fota(char *fota_script)
3895 {
3896         int ret = 0;
3897         int op_type = 0;
3898         char *pkgid = NULL;
3899         char *op_str = NULL;
3900
3901         char csc_str[BUF_SIZE] = {'\0'};
3902         snprintf(csc_str, BUF_SIZE - 1, "%s:", fota_script);
3903
3904         /*get params from fota script*/
3905         pkgid = __ri_get_str(csc_str, TOKEN_PATH_STR);
3906         op_str = __ri_get_str(csc_str, TOKEN_OPERATION_STR);
3907         if((pkgid == NULL) || (op_str == NULL)){
3908                 _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
3909                 goto end;
3910         }
3911         _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
3912
3913         /*get operation type*/
3914         op_type = __ri_get_op_type(op_str);
3915         if(op_type < 0){
3916                 _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
3917                 goto end;
3918         }
3919
3920         switch (op_type) {
3921                 case INSTALL_REQ:
3922                         ret = __ri_install_fota(pkgid);
3923                         break;
3924
3925                 case UPGRADE_REQ:
3926                         ret = __ri_upgrade_fota(pkgid);
3927                         break;
3928
3929                 case UNINSTALL_REQ:
3930                         ret = __ri_uninstall_fota(pkgid);
3931                         break;
3932
3933                 default:
3934                         break;
3935         }
3936
3937         if (ret < 0)
3938                 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
3939
3940 end:
3941         if(pkgid)
3942                 free(pkgid);
3943         if(op_str)
3944                 free(op_str);
3945
3946         return ret;
3947 }
3948
3949 int _rpm_process_fota_for_rw(char *fota_script)
3950 {
3951         int ret = 0;
3952         int op_type = 0;
3953         char *pkgid = NULL;
3954         char *op_str = NULL;
3955
3956         char fota_str[BUF_SIZE] = {'\0'};
3957         snprintf(fota_str, BUF_SIZE - 1, "%s:", fota_script);
3958
3959         /*get params from fota script*/
3960         pkgid = __ri_get_str(fota_str, TOKEN_PATH_STR);
3961         op_str = __ri_get_str(fota_str, TOKEN_OPERATION_STR);
3962         if((pkgid == NULL) || (op_str == NULL)){
3963                 _LOGE("fota-info : input param is null[%s, %s]\n", pkgid, op_str);
3964                 goto end;
3965         }
3966         _LOGD("fota-info : path=%s, op=%s\n", pkgid, op_str);
3967
3968         /*get operation type*/
3969         op_type = __ri_get_op_type(op_str);
3970         if(op_type < 0){
3971                 _LOGE("fota-info : operation error[%s, %s]\n", pkgid, op_str);
3972                 goto end;
3973         }
3974
3975         switch (op_type) {
3976                 case INSTALL_REQ:
3977                         ret = __ri_install_fota_for_rw(pkgid);
3978                         break;
3979
3980                 case UPGRADE_REQ:
3981                         ret = __ri_upgrade_fota_for_rw(pkgid);
3982                         break;
3983
3984                 case UNINSTALL_REQ:
3985                         ret = __ri_uninstall_fota_for_rw(pkgid);
3986                         break;
3987
3988                 default:
3989                         break;
3990         }
3991
3992         if (ret < 0)
3993                 _LOGE("fota-info : Fota fail [pkgid=%s, operation=%d]\n",pkgid, op_type);
3994
3995 end:
3996         if(pkgid)
3997                 free(pkgid);
3998         if(op_str)
3999                 free(op_str);
4000
4001         sync();
4002
4003         return ret;
4004 }
4005
4006 int _rpm_process_enable(char *pkgid)
4007 {
4008         int ret = 0;
4009         char *manifest = NULL;
4010         pkgmgrinfo_pkginfo_h handle;
4011         bool is_system = 0;
4012
4013         _LOGE("start :: pkgid[%s] enable process\n",pkgid);
4014
4015         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
4016         if ((ret == 0) && (handle != NULL)) {
4017                 _LOGE("pkg[%s] is already installed.", pkgid);
4018                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4019                 return 0;
4020         }
4021
4022         manifest = pkgmgr_parser_get_manifest_file(pkgid);
4023         if (manifest == NULL) {
4024                 _LOGE("Failed to fetch package manifest file\n");
4025                 return -1;
4026         }
4027
4028         _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_INSTALL_EVENT_STR);
4029
4030         ret = pkgmgr_parser_parse_manifest_for_installation(manifest, NULL);
4031         free(manifest);
4032         if (ret < 0) {
4033                 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
4034                 _LOGE("insert in db failed\n");
4035                 return -1;
4036         }
4037
4038         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
4039         if (ret < 0) {
4040                 _LOGE("insert in db failed\n");
4041         } else {
4042                 ret = pkgmgrinfo_pkginfo_is_system(handle, &is_system);
4043                 if (is_system) {
4044                         pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_update_ail_info, NULL);
4045                 }
4046                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4047         }
4048
4049         /*delete disabled pkg info from backup db table*/
4050         pkgmgr_parser_delete_disabled_pkg(pkgid, NULL);
4051
4052         _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
4053
4054         _LOGE("end :: pkgid[%s] enable process\n",pkgid);
4055
4056         return 0;
4057 }
4058
4059 int _rpm_process_disable(char *pkgid)
4060 {
4061         int ret = 0;
4062         pkgmgrinfo_pkginfo_h handle;
4063
4064         _LOGE("start :: pkgid[%s] disable process\n",pkgid);
4065
4066         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
4067         if ((ret < 0) || (handle == NULL)) {
4068                 _LOGE("pkgid[%s] is already disabled\n", pkgid);
4069                 return 0;
4070         }
4071
4072         _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_START_KEY_STR, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR);
4073
4074         pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, __ri_check_running_app, NULL);
4075         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
4076
4077         ret = pkgmgr_parser_parse_manifest_for_uninstallation(pkgid, NULL);
4078         if (ret < 0) {
4079                 _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_FAIL_EVENT_STR);
4080                 _LOGE("pkgmgr_parser_parse_manifest_for_uninstallation failed.\n");
4081                 return -1;
4082         }
4083
4084         /*save disabled pkg info to backup db table*/
4085         pkgmgr_parser_insert_disabled_pkg(pkgid, NULL);
4086
4087         _ri_broadcast_status_notification(pkgid, "rpm", PKGMGR_INSTALLER_END_KEY_STR, PKGMGR_INSTALLER_OK_EVENT_STR);
4088
4089         _LOGE("end :: pkgid[%s] disable process\n",pkgid);
4090         return 0;
4091 }
4092
4093 int _rpm_process_enabled_list(const char *enabled_list)
4094 {
4095         char* token = NULL;
4096         char delims[] = ":";
4097         char pkgid[MAX_BUF_SIZE] = {'\0'};
4098         char pkgid_list[MAX_BUF_SIZE] = {'\0'};
4099
4100         if (enabled_list == NULL)
4101                 return -1;
4102
4103         snprintf(pkgid_list, MAX_BUF_SIZE, "%s", enabled_list);
4104         token = strtok(pkgid_list, delims);
4105
4106         while(token)
4107         {
4108                 memset(pkgid, 0x00, sizeof(pkgid));
4109                 if (strlen(token) < MAX_BUF_SIZE)
4110                         strcat(pkgid, token);
4111                 else
4112                         _LOGE("pkgid too long[%s]", token);
4113
4114
4115                 _rpm_process_enable(pkgid);
4116
4117                 token = strtok(NULL, delims);
4118         }
4119
4120         return 0;
4121 }
4122
4123 int _rpm_process_disabled_list(const char *disabled_list)
4124 {
4125         char* token = NULL;
4126         char delims[] = ":";
4127         char pkgid[MAX_BUF_SIZE] = {'\0'};
4128         char pkgid_list[MAX_BUF_SIZE] = {'\0'};
4129
4130         if (disabled_list == NULL)
4131                 return -1;
4132
4133         snprintf(pkgid_list, MAX_BUF_SIZE, "%s", disabled_list);
4134         token = strtok(pkgid_list, delims);
4135
4136         while(token)
4137         {
4138                 memset(pkgid, 0x00, sizeof(pkgid));
4139                 if (strlen(token) < MAX_BUF_SIZE)
4140                         strncat(pkgid, token, strlen(token));
4141                 else
4142                         _LOGE("pkgid too long[%s]", token);
4143
4144                 _rpm_process_disable(pkgid);
4145
4146                 token = strtok(NULL, delims);
4147         }
4148
4149         return 0;
4150 }
4151
4152 int __ri_copy_smack_rule_file(int op, const char *pkgname, int is_system)
4153 {
4154
4155         mode_t mode = DIR_PERMS;
4156         int ret = RPM_INSTALLER_SUCCESS;
4157         char src[PATH_MAX]={0};
4158         char dest[PATH_MAX]={0};
4159
4160         switch(op)
4161                 {
4162                 case UNINSTALL_REQ:
4163                         /*
4164                         For downloadable native app, restore the smack file.
4165                         Otherwise, remove the stored smack file.
4166                         */
4167                         snprintf(dest,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
4168                         snprintf(src,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
4169                         _LOGD("#src:[%s] dest:[%s]",src,dest);
4170
4171                         if(!is_system){
4172                                 if(!access(src,F_OK)){
4173                                         ret = remove(src);
4174                                         if(!ret){
4175                                                 _LOGD("#File [%s] deleted.",src);
4176                                         }else{
4177                                                 _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
4178                                                 ret = RPM_INSTALLER_ERR_INTERNAL;
4179                                                 goto end;
4180                                         }
4181                                 }
4182                                 if(!access(dest,F_OK)){
4183                                         ret = remove(dest);
4184                                         if(!ret){
4185                                                 _LOGD("#File [%s] deleted.",dest);
4186                                         }else{
4187                                                 _LOGE("@Unable to delete the file [%s], error:(%s)",dest,strerror(errno));
4188                                                 ret = RPM_INSTALLER_ERR_INTERNAL;
4189                                                 goto end;
4190                                         }
4191                                 }
4192                         }else{
4193                                 _LOGD("#Restore smack files for uninstallation [%s]",pkgname);
4194                                 if(!access(src,F_OK)){
4195                                         _LOGD("#Copying [%s] to [%s]",src,dest);
4196                                         ret = __copy_file(src,dest);
4197                                         if(!ret){
4198                                                 ret = remove(src);
4199                                                 if(!ret){
4200                                                         _LOGD("#File [%s] deleted.",src);
4201                                                 }else{
4202                                                         _LOGE("@Unable to delete the file [%s], error:(%s)",src,strerror(errno));
4203                                                         ret = RPM_INSTALLER_ERR_INTERNAL;
4204                                                         goto end;
4205                                                 }
4206                                         }else{
4207                                                 _LOGE("@Copy Failed!!");
4208                                                 ret = RPM_INSTALLER_ERR_INTERNAL;
4209                                                 goto end;
4210                                         }
4211                                 }else{
4212                                         _LOGE("@ %s.rule file is not preserved",pkgname);
4213                                 }
4214                         }
4215                         break;
4216
4217                 case UPGRADE_REQ:
4218
4219                         _LOGD("#Preserve the smack file for upgrade [%s]",pkgname);
4220
4221                         /* Apply the new smack file and preserve the old smack rule file if it is not preserved.*/
4222                         snprintf(src,PATH_MAX-1,"%s%s.rule",SMACK_RULES_ALT_PATH,pkgname);
4223                         snprintf(dest,PATH_MAX-1,"%s%s.rule",DIR_RPM_WGT_SMACK_RULE_OPT,pkgname);
4224
4225                         _LOGD("#src[%s] dest[%s]",src,dest);
4226
4227                         /* Create the directory if not exist to preserve the smack files */
4228                         if(mkdir(DIR_RPM_WGT_SMACK_RULE_OPT,mode) == 0 || errno == EEXIST){
4229                                 if((access(src,F_OK) == 0) && (access(dest,F_OK) != 0)){
4230                                         ret = __copy_file(src,dest);
4231                                 }else{
4232                                         _LOGD("#Smack file is already preserved");
4233                                 }
4234                         }else{
4235                                 _LOGE("@Temporary folder creation failed");
4236                                 ret = RPM_INSTALLER_ERR_INTERNAL;
4237                         }
4238                         break;
4239                 default:
4240                         _LOGE("@Unsupported Operation\n");
4241                         ret = RPM_INSTALLER_ERR_INTERNAL;
4242                 }
4243
4244         end:
4245                 return ret;
4246
4247
4248 }