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