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