15 #include <tzplatform_config.h>
18 #include "pkgmgr-info.h"
19 #include "pkgmgrinfo_debug.h"
20 #include "pkgmgrinfo_private.h"
21 #include "pkgmgr_parser.h"
22 #include "pkgmgr_parser_internal.h"
24 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
25 "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
26 " cert_info TEXT UNIQUE, " \
27 " cert_id INTEGER PRIMARY KEY, " \
28 " cert_ref_count INTEGER NOT NULL)"
30 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
31 "CREATE TABLE IF NOT EXISTS package_cert_info( " \
32 " package TEXT PRIMARY KEY, " \
33 " package_count INTEGER, " \
34 " author_root_cert INTEGER, " \
35 " author_im_cert INTEGER, " \
36 " author_signer_cert INTEGER, " \
37 " dist_root_cert INTEGER, " \
38 " dist_im_cert INTEGER, " \
39 " dist_signer_cert INTEGER, " \
40 " dist2_root_cert INTEGER, " \
41 " dist2_im_cert INTEGER, " \
42 " dist2_signer_cert INTEGER)"
44 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO \
45 "CREATE TRIGGER IF NOT EXISTS update_cert_info " \
46 "AFTER UPDATE ON package_cert_info " \
47 "WHEN (NEW.package_count = 0) " \
49 " DELETE FROM package_cert_info WHERE package=OLD.package;" \
52 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \
53 "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \
54 "AFTER UPDATE ON package_cert_info " \
55 "WHEN (NEW.package_count = OLD.package_count + 1) " \
57 " UPDATE package_cert_index_info SET" \
58 " cert_ref_count = cert_ref_count - 1" \
59 " WHERE cert_id = OLD.author_root_cert" \
60 " OR cert_id = OLD.author_im_cert" \
61 " OR cert_id = OLD.author_signer_cert" \
62 " OR cert_id = OLD.dist_root_cert" \
63 " OR cert_id = OLD.dist_im_cert" \
64 " OR cert_id = OLD.dist_signer_cert" \
65 " OR cert_id = OLD.dist2_root_cert" \
66 " OR cert_id = OLD.dist2_im_cert" \
67 " OR cert_id = OLD.dist2_signer_cert;" \
70 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
71 "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
72 "AFTER DELETE ON package_cert_info " \
74 " UPDATE package_cert_index_info SET" \
75 " cert_ref_count = cert_ref_count - 1" \
76 " WHERE cert_id = OLD.author_root_cert" \
77 " OR cert_id = OLD.author_im_cert" \
78 " OR cert_id = OLD.author_signer_cert" \
79 " OR cert_id = OLD.dist_root_cert" \
80 " OR cert_id = OLD.dist_im_cert" \
81 " OR cert_id = OLD.dist_signer_cert" \
82 " OR cert_id = OLD.dist2_root_cert" \
83 " OR cert_id = OLD.dist2_im_cert" \
84 " OR cert_id = OLD.dist2_signer_cert;" \
87 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
88 "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
89 "AFTER UPDATE ON package_cert_index_info " \
90 "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
91 " WHERE cert_id = OLD.cert_id) = 0) "\
93 " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
96 __thread db_handle manifest_db;
97 __thread db_handle cert_db;
99 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
101 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
110 fullpath = strdup(dir);
111 if (fullpath == NULL)
113 subpath = dirname(fullpath);
114 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
115 ret = _mkdir_for_user(fullpath, uid, gid);
122 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
123 if (ret && errno != EEXIST) {
126 } else if (ret && errno == EEXIST) {
131 if (getuid() == ROOT_UID) {
132 fd = open(dir, O_RDONLY);
134 _LOGE("FAIL : open %s : %s", dir,
135 strerror_r(errno, buf, sizeof(buf)));
138 ret = fstat(fd, &sb);
140 _LOGE("FAIL : fstat %s : %s", dir,
141 strerror_r(errno, buf, sizeof(buf)));
145 if (S_ISLNK(sb.st_mode)) {
146 _LOGE("FAIL : %s is symlink!", dir);
150 ret = fchown(fd, uid, gid);
152 _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid,
153 gid, strerror_r(errno, buf, sizeof(buf)));
165 static char *_get_db_path(uid_t uid)
170 db_path = tzplatform_getenv(TZ_SYS_DB);
171 if (db_path == NULL) {
172 _LOGE("Failed to get TZ_SYS_DB path");
176 if (uid == GLOBAL_USER || uid == ROOT_UID)
177 return strdup(db_path);
179 snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
184 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
188 char query[MAX_QUERY_LEN];
190 if (uid != GLOBAL_USER && uid != ROOT_UID) {
191 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
192 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
193 _LOGD("Don't execute query = %s error message = %s\n", query, err);
199 for (i = 0; tables[i]; i++) {
200 if (uid != GLOBAL_USER && uid != ROOT_UID)
201 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
202 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
203 SELECT *,1 AS for_all_users FROM Global.'%s')",
204 tables[i], tables[i], tables[i]);
206 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
207 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
208 tables[i], tables[i]);
209 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
210 _LOGD("Don't execute query = %s error message = %s\n", query, err);
218 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
220 char *error_message = NULL;
221 int ret = sqlite3_exec(db, query, callback, data, &error_message);
222 if (SQLITE_OK != ret) {
223 _LOGE("Don't execute query = %s error message = %s ret = %d\n", query,
225 sqlite3_free(error_message);
228 sqlite3_free(error_message);
232 int _check_create_cert_db(sqlite3 *certdb)
235 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
238 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
241 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL);
244 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL);
247 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
250 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
253 static gid_t _get_gid(const char *name)
260 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
261 if (ret || ge == NULL) {
262 _LOGE("fail to get gid of %s", name);
269 API const char *getIconPath(uid_t uid, bool readonly)
271 const char *path = NULL;
272 uid_t uid_caller = getuid();
273 gid_t gid = ROOT_UID;
275 if (uid != GLOBAL_USER && uid != ROOT_UID) {
276 _LOGD("not supported target user");
281 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
283 path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
285 /* just allow certain users to create the icon directory if needed. */
286 if (uid_caller == ROOT_UID || uid_caller == uid)
287 _mkdir_for_user(path, uid, gid);
292 API char *getUserPkgParserDBPath(void)
294 return getUserPkgParserDBPathUID(_getuid());
297 API char *getUserPkgParserDBPathUID(uid_t uid)
299 char pkgmgr_parser_db[PATH_MAX];
300 uid_t uid_caller = getuid();
301 gid_t gid = ROOT_UID;
304 db_path = _get_db_path(uid);
305 if (db_path == NULL) {
306 _LOGE("Failed to get db path %d", uid);
309 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
310 "%s/.pkgmgr_parser.db", db_path);
312 if (uid != GLOBAL_USER && uid != ROOT_UID) {
313 tzplatform_set_user(uid);
314 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
315 tzplatform_reset_user();
318 // just allow certain users to create the dbspace directory if needed.
319 if (uid_caller == ROOT_UID || uid_caller == uid)
320 _mkdir_for_user(db_path, uid, gid);
324 return strdup(pkgmgr_parser_db);
327 API char *getUserPkgCertDBPath(void)
329 return getUserPkgCertDBPathUID(_getuid());
332 API char *getUserPkgCertDBPathUID(uid_t uid)
334 char pkgmgr_cert_db[PATH_MAX];
335 uid_t uid_caller = getuid();
336 gid_t gid = ROOT_UID;
339 db_path = _get_db_path(uid);
340 if (db_path == NULL) {
341 _LOGE("Failed to get db path %d", uid);
344 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
345 "%s/.pkgmgr_cert.db", db_path);
347 if (uid != GLOBAL_USER && uid != ROOT_UID) {
348 tzplatform_set_user(uid);
349 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
350 tzplatform_reset_user();
353 // just allow certain users to create the dbspace directory if needed.
354 if (uid_caller == ROOT_UID || uid_caller == uid)
355 _mkdir_for_user(db_path, uid, gid);
359 return strdup(pkgmgr_cert_db);
362 API const char *getUserManifestPath(uid_t uid, bool readonly)
364 const char *path = NULL;
365 uid_t uid_caller = getuid();
366 gid_t gid = ROOT_UID;
368 if (uid != GLOBAL_USER && uid != ROOT_UID) {
369 tzplatform_set_user(uid);
370 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
371 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
372 tzplatform_reset_user();
375 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
377 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
380 /* just allow certain users to create the icon directory if needed. */
381 if (uid_caller == ROOT_UID || uid_caller == uid)
382 _mkdir_for_user(path, uid, gid);
387 int __close_manifest_db(void)
389 if (manifest_db.ref) {
390 if (--manifest_db.ref == 0)
391 sqlite3_close(GET_DB(manifest_db));
397 static const char *parserdb_tables[] = {
398 "package_app_app_category",
400 "package_app_app_control",
401 "package_app_localized_info",
402 "package_app_app_metadata",
403 "package_app_share_allowed",
404 "package_app_app_permission",
405 "package_app_share_request",
407 "package_app_data_control",
408 "package_localized_info",
409 "package_app_icon_section_info",
410 "package_privilege_info",
411 "package_app_image_info",
415 int __open_manifest_db(uid_t uid, bool readonly)
418 char *user_pkg_parser;
421 if (manifest_db.ref) {
426 user_pkg_parser = getUserPkgParserDBPathUID(uid);
427 if (user_pkg_parser == NULL) {
428 _LOGE("Failed to get pkg parser db path - %d", uid);
432 if (access(user_pkg_parser, F_OK) != 0) {
433 _LOGE("Manifest DB does not exists !!");
434 free(user_pkg_parser);
438 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
439 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
441 if (ret != SQLITE_OK) {
442 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
443 free(user_pkg_parser);
449 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
450 parserdb_tables, uid);
451 if (ret != SQLITE_OK) {
452 _LOGE("attach db [%s] failed!\n", user_pkg_parser);
453 free(user_pkg_parser);
458 free(user_pkg_parser);
463 int __close_cert_db(void)
466 if (--cert_db.ref == 0)
467 sqlite3_close_v2(GET_DB(cert_db));
470 _LOGE("Certificate DB is already closed !!\n");
474 static const char *certdb_tables[] = {
475 "package_cert_index_info",
480 int __open_cert_db(uid_t uid, bool readonly)
483 char *user_cert_parser;
491 user_cert_parser = getUserPkgCertDBPathUID(uid);
492 if (user_cert_parser == NULL) {
493 _LOGE("Failed to get pkg cert db path - %d", uid);
497 if (access(user_cert_parser, F_OK) != 0) {
498 _LOGE("Cert DB does not exists !!");
499 free(user_cert_parser);
503 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
504 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
506 if (ret != SQLITE_OK) {
507 _LOGE("connect db [%s] failed!", user_cert_parser);
508 free(user_cert_parser);
513 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
515 if (ret != SQLITE_OK) {
516 _LOGE("attach db [%s] failed!", user_cert_parser);
517 free(user_cert_parser);
522 free(user_cert_parser);
527 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
529 *i = sqlite3_column_int(stmt, idx);
532 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
536 val = (const char *)sqlite3_column_text(stmt, idx);
541 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
543 /* Should be implemented later */
547 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
550 char query[MAX_QUERY_LEN] = {'\0'};
553 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
556 ret = __open_manifest_db(uid, false);
557 if (ret != SQLITE_OK) {
558 _LOGE("connect db [%d] failed!\n", uid);
559 return PMINFO_R_ERROR;
562 /*Begin transaction*/
563 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
564 if (ret != SQLITE_OK) {
565 _LOGE("Failed to begin transaction\n");
566 __close_manifest_db();
567 return PMINFO_R_ERROR;
569 _LOGD("Transaction Begin\n");
571 memset(query, '\0', MAX_QUERY_LEN);
572 snprintf(query, MAX_QUERY_LEN,
573 "update package_app_info set app_enabled='%s' where app_id='%s'",
574 enabled ? "true" : "false", appid);
577 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
578 _LOGE("Don't execute query = %s error message = %s\n", query,
580 sqlite3_free(error_message);
581 return PMINFO_R_ERROR;
583 sqlite3_free(error_message);
585 /*Commit transaction*/
586 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
587 if (ret != SQLITE_OK) {
588 _LOGE("Failed to commit transaction. Rollback now\n");
589 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
590 __close_manifest_db();
591 return PMINFO_R_ERROR;
593 _LOGD("Transaction Commit and End\n");
594 __close_manifest_db();
598 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
600 return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, _getuid());
603 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
606 char query[MAX_QUERY_LEN] = {'\0'};
609 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
611 ret = __open_manifest_db(uid, false);
613 _LOGE("Fail to open manifest DB\n");
614 return PMINFO_R_ERROR;
617 /*Begin transaction*/
618 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
619 if (ret != SQLITE_OK) {
620 _LOGE("Failed to begin transaction\n");
621 __close_manifest_db();
622 return PMINFO_R_ERROR;
624 _LOGD("Transaction Begin\n");
626 memset(query, '\0', MAX_QUERY_LEN);
627 snprintf(query, MAX_QUERY_LEN,
628 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
631 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
632 _LOGE("Don't execute query = %s error message = %s\n", query,
634 sqlite3_free(error_message);
635 return PMINFO_R_ERROR;
638 /*Commit transaction*/
639 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
640 if (ret != SQLITE_OK) {
641 _LOGE("Failed to commit transaction. Rollback now\n");
642 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
643 __close_manifest_db();
644 return PMINFO_R_ERROR;
646 _LOGD("Transaction Commit and End\n");
647 __close_manifest_db();
651 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
653 return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, _getuid());
656 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
660 char query[MAX_QUERY_LEN] = {'\0'};
662 sqlite3 *pkgmgr_parser_db;
665 retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
667 pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
668 val = info->app_info->guestmode_visibility;
670 db_path = getUserPkgParserDBPathUID(uid);
671 if (db_path == NULL) {
672 _LOGE("Failed to get pkg parser db path - %d", uid);
673 return PMINFO_R_ERROR;
676 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
677 SQLITE_OPEN_READWRITE, NULL);
678 if (ret != SQLITE_OK) {
679 _LOGE("DB Open Failed\n");
681 return PMINFO_R_ERROR;
685 /*TODO: Write to DB here*/
687 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
689 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
691 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
692 sqlite3_close(pkgmgr_parser_db);
693 if (ret != SQLITE_OK) {
694 _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
696 return PMINFO_R_ERROR;
702 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
704 return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, _getuid(), status);
707 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
709 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
711 sqlite3 *pkgmgr_parser_db = NULL;
715 db_path = getUserPkgParserDBPathUID(uid);
716 if (db_path == NULL) {
717 _LOGE("Failed to get pkg parser db path - %d", uid);
718 return PMINFO_R_ERROR;
721 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
722 SQLITE_OPEN_READWRITE, NULL);
723 if (ret != SQLITE_OK) {
724 _LOGE("connect db failed!");
726 return PMINFO_R_ERROR;
730 /*Begin transaction*/
731 /* Setting Manifest DB */
732 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
733 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
734 _LOGD("Transaction Begin\n");
736 /* pkgcakge_info table */
737 query = sqlite3_mprintf(
738 "update package_info set installed_storage=%Q where package=%Q",
739 location ? "installed_external" : "installed_internal", pkgid);
741 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
742 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
745 /* package_app_info table */
746 query = sqlite3_mprintf(
747 "update package_app_info set app_installed_storage=%Q where package=%Q",
748 location ? "installed_external" : "installed_internal", pkgid);
750 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
751 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
753 /*Commit transaction*/
754 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
755 if (ret != SQLITE_OK) {
756 _LOGE("Failed to commit transaction. Rollback now\n");
757 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
758 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
760 _LOGD("Transaction Commit and End\n");
764 sqlite3_close(pkgmgr_parser_db);
769 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
771 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, _getuid());