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 tzplatform_set_user(uid);
277 path = tzplatform_mkpath(TZ_USER_ICONS, "/");
278 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
279 tzplatform_reset_user();
282 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
284 path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
287 /* just allow certain users to create the icon directory if needed. */
288 if (uid_caller == ROOT_UID || uid_caller == uid)
289 _mkdir_for_user(path, uid, gid);
294 API char *getUserPkgParserDBPath(void)
296 return getUserPkgParserDBPathUID(_getuid());
299 API char *getUserPkgParserDBPathUID(uid_t uid)
301 char pkgmgr_parser_db[PATH_MAX];
302 uid_t uid_caller = getuid();
303 gid_t gid = ROOT_UID;
306 db_path = _get_db_path(uid);
307 if (db_path == NULL) {
308 _LOGE("Failed to get db path %d", uid);
311 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
312 "%s/.pkgmgr_parser.db", db_path);
314 if (uid != GLOBAL_USER && uid != ROOT_UID) {
315 tzplatform_set_user(uid);
316 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
317 tzplatform_reset_user();
320 // just allow certain users to create the dbspace directory if needed.
321 if (uid_caller == ROOT_UID || uid_caller == uid)
322 _mkdir_for_user(db_path, uid, gid);
326 return strdup(pkgmgr_parser_db);
329 API char *getUserPkgCertDBPath(void)
331 return getUserPkgCertDBPathUID(_getuid());
334 API char *getUserPkgCertDBPathUID(uid_t uid)
336 char pkgmgr_cert_db[PATH_MAX];
337 uid_t uid_caller = getuid();
338 gid_t gid = ROOT_UID;
341 db_path = _get_db_path(uid);
342 if (db_path == NULL) {
343 _LOGE("Failed to get db path %d", uid);
346 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
347 "%s/.pkgmgr_cert.db", db_path);
349 if (uid != GLOBAL_USER && uid != ROOT_UID) {
350 tzplatform_set_user(uid);
351 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
352 tzplatform_reset_user();
355 // just allow certain users to create the dbspace directory if needed.
356 if (uid_caller == ROOT_UID || uid_caller == uid)
357 _mkdir_for_user(db_path, uid, gid);
361 return strdup(pkgmgr_cert_db);
364 API const char *getUserManifestPath(uid_t uid, bool readonly)
366 const char *path = NULL;
367 uid_t uid_caller = getuid();
368 gid_t gid = ROOT_UID;
370 if (uid != GLOBAL_USER && uid != ROOT_UID) {
371 tzplatform_set_user(uid);
372 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
373 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
374 tzplatform_reset_user();
377 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
379 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
382 /* just allow certain users to create the icon directory if needed. */
383 if (uid_caller == ROOT_UID || uid_caller == uid)
384 _mkdir_for_user(path, uid, gid);
389 int __close_manifest_db(void)
391 if (manifest_db.ref) {
392 if (--manifest_db.ref == 0)
393 sqlite3_close(GET_DB(manifest_db));
399 static const char *parserdb_tables[] = {
400 "package_app_app_category",
402 "package_app_app_control",
403 "package_app_localized_info",
404 "package_app_app_metadata",
405 "package_app_share_allowed",
406 "package_app_app_permission",
407 "package_app_share_request",
409 "package_app_data_control",
410 "package_localized_info",
411 "package_app_icon_section_info",
412 "package_privilege_info",
413 "package_app_image_info",
417 int __open_manifest_db(uid_t uid, bool readonly)
420 char *user_pkg_parser;
423 if (manifest_db.ref) {
428 user_pkg_parser = getUserPkgParserDBPathUID(uid);
429 if (user_pkg_parser == NULL) {
430 _LOGE("Failed to get pkg parser db path - %d", uid);
434 if (access(user_pkg_parser, F_OK) != 0) {
435 _LOGE("Manifest DB does not exists !!");
436 free(user_pkg_parser);
440 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
441 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
443 if (ret != SQLITE_OK) {
444 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
445 free(user_pkg_parser);
451 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
452 parserdb_tables, uid);
453 if (ret != SQLITE_OK) {
454 _LOGE("attach db [%s] failed!\n", user_pkg_parser);
455 free(user_pkg_parser);
460 free(user_pkg_parser);
465 int __close_cert_db(void)
468 if (--cert_db.ref == 0)
469 sqlite3_close_v2(GET_DB(cert_db));
472 _LOGE("Certificate DB is already closed !!\n");
476 static const char *certdb_tables[] = {
477 "package_cert_index_info",
482 int __open_cert_db(uid_t uid, bool readonly)
485 char *user_cert_parser;
493 user_cert_parser = getUserPkgCertDBPathUID(uid);
494 if (user_cert_parser == NULL) {
495 _LOGE("Failed to get pkg cert db path - %d", uid);
499 if (access(user_cert_parser, F_OK) != 0) {
500 _LOGE("Cert DB does not exists !!");
501 free(user_cert_parser);
505 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
506 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
508 if (ret != SQLITE_OK) {
509 _LOGE("connect db [%s] failed!", user_cert_parser);
510 free(user_cert_parser);
515 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
517 if (ret != SQLITE_OK) {
518 _LOGE("attach db [%s] failed!", user_cert_parser);
519 free(user_cert_parser);
524 free(user_cert_parser);
529 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
531 *i = sqlite3_column_int(stmt, idx);
534 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
538 val = (const char *)sqlite3_column_text(stmt, idx);
543 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
545 /* Should be implemented later */
549 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
552 char query[MAX_QUERY_LEN] = {'\0'};
555 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
558 ret = __open_manifest_db(uid, false);
559 if (ret != SQLITE_OK) {
560 _LOGE("connect db [%d] failed!\n", uid);
561 return PMINFO_R_ERROR;
564 /*Begin transaction*/
565 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
566 if (ret != SQLITE_OK) {
567 _LOGE("Failed to begin transaction\n");
568 __close_manifest_db();
569 return PMINFO_R_ERROR;
571 _LOGD("Transaction Begin\n");
573 memset(query, '\0', MAX_QUERY_LEN);
574 snprintf(query, MAX_QUERY_LEN,
575 "update package_app_info set app_enabled='%s' where app_id='%s'",
576 enabled ? "true" : "false", appid);
579 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
580 _LOGE("Don't execute query = %s error message = %s\n", query,
582 sqlite3_free(error_message);
583 return PMINFO_R_ERROR;
585 sqlite3_free(error_message);
587 /*Commit transaction*/
588 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
589 if (ret != SQLITE_OK) {
590 _LOGE("Failed to commit transaction. Rollback now\n");
591 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
592 __close_manifest_db();
593 return PMINFO_R_ERROR;
595 _LOGD("Transaction Commit and End\n");
596 __close_manifest_db();
600 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
602 return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, _getuid());
605 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
608 char query[MAX_QUERY_LEN] = {'\0'};
611 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
613 ret = __open_manifest_db(uid, false);
615 _LOGE("Fail to open manifest DB\n");
616 return PMINFO_R_ERROR;
619 /*Begin transaction*/
620 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
621 if (ret != SQLITE_OK) {
622 _LOGE("Failed to begin transaction\n");
623 __close_manifest_db();
624 return PMINFO_R_ERROR;
626 _LOGD("Transaction Begin\n");
628 memset(query, '\0', MAX_QUERY_LEN);
629 snprintf(query, MAX_QUERY_LEN,
630 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
633 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
634 _LOGE("Don't execute query = %s error message = %s\n", query,
636 sqlite3_free(error_message);
637 return PMINFO_R_ERROR;
640 /*Commit transaction*/
641 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
642 if (ret != SQLITE_OK) {
643 _LOGE("Failed to commit transaction. Rollback now\n");
644 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
645 __close_manifest_db();
646 return PMINFO_R_ERROR;
648 _LOGD("Transaction Commit and End\n");
649 __close_manifest_db();
653 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
655 return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, _getuid());
658 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
662 char query[MAX_QUERY_LEN] = {'\0'};
664 sqlite3 *pkgmgr_parser_db;
667 retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
669 pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
670 val = info->app_info->guestmode_visibility;
672 db_path = getUserPkgParserDBPathUID(uid);
673 if (db_path == NULL) {
674 _LOGE("Failed to get pkg parser db path - %d", uid);
675 return PMINFO_R_ERROR;
678 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
679 SQLITE_OPEN_READWRITE, NULL);
680 if (ret != SQLITE_OK) {
681 _LOGE("DB Open Failed\n");
683 return PMINFO_R_ERROR;
687 /*TODO: Write to DB here*/
689 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
691 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
693 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
694 sqlite3_close(pkgmgr_parser_db);
695 if (ret != SQLITE_OK) {
696 _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
698 return PMINFO_R_ERROR;
704 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
706 return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, _getuid(), status);
709 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
711 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
713 sqlite3 *pkgmgr_parser_db = NULL;
717 db_path = getUserPkgParserDBPathUID(uid);
718 if (db_path == NULL) {
719 _LOGE("Failed to get pkg parser db path - %d", uid);
720 return PMINFO_R_ERROR;
723 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
724 SQLITE_OPEN_READWRITE, NULL);
725 if (ret != SQLITE_OK) {
726 _LOGE("connect db failed!");
728 return PMINFO_R_ERROR;
732 /*Begin transaction*/
733 /* Setting Manifest DB */
734 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
735 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
736 _LOGD("Transaction Begin\n");
738 /* pkgcakge_info table */
739 query = sqlite3_mprintf(
740 "update package_info set installed_storage=%Q where package=%Q",
741 location ? "installed_external" : "installed_internal", pkgid);
743 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
744 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
747 /* package_app_info table */
748 query = sqlite3_mprintf(
749 "update package_app_info set app_installed_storage=%Q where package=%Q",
750 location ? "installed_external" : "installed_internal", pkgid);
752 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
753 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
755 /*Commit transaction*/
756 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
757 if (ret != SQLITE_OK) {
758 _LOGE("Failed to commit transaction. Rollback now\n");
759 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
760 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
762 _LOGD("Transaction Commit and End\n");
766 sqlite3_close(pkgmgr_parser_db);
771 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
773 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, _getuid());