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 const char *_get_db_path(uid_t uid) {
166 const char *db_path = NULL;
167 if (uid != GLOBAL_USER && uid != ROOT_UID) {
168 tzplatform_set_user(uid);
169 db_path = tzplatform_getenv(TZ_USER_DB);
170 tzplatform_reset_user();
172 db_path = tzplatform_getenv(TZ_SYS_DB);
177 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
181 char query[MAX_QUERY_LEN];
183 if (uid != GLOBAL_USER && uid != ROOT_UID) {
184 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
185 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
186 _LOGD("Don't execute query = %s error message = %s\n", query, err);
192 for (i = 0; tables[i]; i++) {
193 if (uid != GLOBAL_USER && uid != ROOT_UID)
194 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
195 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
196 SELECT *,1 AS for_all_users FROM Global.'%s')",
197 tables[i], tables[i], tables[i]);
199 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
200 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
201 tables[i], tables[i]);
202 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
203 _LOGD("Don't execute query = %s error message = %s\n", query, err);
211 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
213 char *error_message = NULL;
214 int ret = sqlite3_exec(db, query, callback, data, &error_message);
215 if (SQLITE_OK != ret) {
216 _LOGE("Don't execute query = %s error message = %s ret = %d\n", query,
218 sqlite3_free(error_message);
221 sqlite3_free(error_message);
225 int _check_create_cert_db(sqlite3 *certdb)
228 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
231 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
234 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL);
237 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL);
240 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
243 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
246 static gid_t _get_gid(const char *name)
253 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
254 if (ret || ge == NULL) {
255 _LOGE("fail to get gid of %s", name);
262 API const char *getIconPath(uid_t uid, bool readonly)
264 const char *path = NULL;
265 uid_t uid_caller = getuid();
266 gid_t gid = ROOT_UID;
268 if (uid != GLOBAL_USER && uid != ROOT_UID) {
269 tzplatform_set_user(uid);
270 path = tzplatform_mkpath(TZ_USER_ICONS, "/");
271 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
272 tzplatform_reset_user();
275 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
277 path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
280 // just allow certain users to create the icon directory if needed.
281 if (uid_caller == ROOT_UID || uid_caller == uid)
282 _mkdir_for_user(path, uid, gid);
287 API const char *getUserPkgParserDBPath(void)
289 return getUserPkgParserDBPathUID(_getuid());
292 API const char *getUserPkgParserDBPathUID(uid_t uid)
294 const char *pkgmgr_parser_db = NULL;
295 uid_t uid_caller = getuid();
296 gid_t gid = ROOT_UID;
298 if (uid != GLOBAL_USER && uid != ROOT_UID) {
299 tzplatform_set_user(uid);
300 pkgmgr_parser_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_parser.db");
301 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
302 tzplatform_reset_user();
304 pkgmgr_parser_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
307 // just allow certain users to create the dbspace directory if needed.
308 if (uid_caller == ROOT_UID || uid_caller == uid) {
309 const char *db_path = _get_db_path(uid);
310 _mkdir_for_user(db_path, uid, gid);
313 return pkgmgr_parser_db;
316 API const char *getUserPkgCertDBPath(void)
318 return getUserPkgCertDBPathUID(_getuid());
321 API const char *getUserPkgCertDBPathUID(uid_t uid)
323 const char *pkgmgr_cert_db = NULL;
324 uid_t uid_caller = getuid();
325 gid_t gid = ROOT_UID;
327 if (uid != GLOBAL_USER && uid != ROOT_UID) {
328 tzplatform_set_user(uid);
329 pkgmgr_cert_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_cert.db");
330 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
331 tzplatform_reset_user();
333 pkgmgr_cert_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
336 // just allow certain users to create the dbspace directory if needed.
337 if (uid_caller == ROOT_UID || uid_caller == uid) {
338 const char *db_path = _get_db_path(uid);
339 _mkdir_for_user(db_path, uid, gid);
342 return pkgmgr_cert_db;
345 API const char *getUserManifestPath(uid_t uid, bool readonly)
347 const char *path = NULL;
348 uid_t uid_caller = getuid();
349 gid_t gid = ROOT_UID;
351 if (uid != GLOBAL_USER && uid != ROOT_UID) {
352 tzplatform_set_user(uid);
353 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
354 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
355 tzplatform_reset_user();
358 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
360 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
363 // just allow certain users to create the icon directory if needed.
364 if (uid_caller == ROOT_UID || uid_caller == uid)
365 _mkdir_for_user(path, uid, gid);
370 int __close_manifest_db(void)
372 if (manifest_db.ref) {
373 if (--manifest_db.ref == 0)
374 sqlite3_close(GET_DB(manifest_db));
380 static const char *parserdb_tables[] = {
381 "package_app_app_category",
383 "package_app_app_control",
384 "package_app_localized_info",
385 "package_app_app_metadata",
386 "package_app_share_allowed",
387 "package_app_app_permission",
388 "package_app_share_request",
390 "package_app_data_control",
391 "package_localized_info",
392 "package_app_icon_section_info",
393 "package_privilege_info",
394 "package_app_image_info",
398 int __open_manifest_db(uid_t uid, bool readonly)
401 const char *user_pkg_parser;
404 if (manifest_db.ref) {
409 user_pkg_parser = getUserPkgParserDBPathUID(uid);
410 if (access(user_pkg_parser, F_OK) != 0) {
411 _LOGE("Manifest DB does not exists !!");
415 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
416 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
418 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
422 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
423 parserdb_tables, uid);
424 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
430 int __close_cert_db(void)
433 if (--cert_db.ref == 0)
434 sqlite3_close_v2(GET_DB(cert_db));
437 _LOGE("Certificate DB is already closed !!\n");
441 static const char *certdb_tables[] = {
442 "package_cert_index_info",
447 int __open_cert_db(uid_t uid, bool readonly)
450 const char *user_cert_parser;
458 user_cert_parser = getUserPkgCertDBPathUID(uid);
459 if (access(user_cert_parser, F_OK) != 0) {
460 _LOGE("Cert DB does not exists !!");
464 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
465 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
467 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
471 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
473 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
479 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
481 *i = sqlite3_column_int(stmt, idx);
484 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
488 val = (const char *)sqlite3_column_text(stmt, idx);
493 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
495 /* Should be implemented later */
499 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
502 char query[MAX_QUERY_LEN] = {'\0'};
505 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
508 ret = __open_manifest_db(uid, false);
509 if (ret != SQLITE_OK) {
510 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
511 return PMINFO_R_ERROR;
514 /*Begin transaction*/
515 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
516 if (ret != SQLITE_OK) {
517 _LOGE("Failed to begin transaction\n");
518 __close_manifest_db();
519 return PMINFO_R_ERROR;
521 _LOGD("Transaction Begin\n");
523 memset(query, '\0', MAX_QUERY_LEN);
524 snprintf(query, MAX_QUERY_LEN,
525 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
528 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
529 _LOGE("Don't execute query = %s error message = %s\n", query,
531 sqlite3_free(error_message);
532 return PMINFO_R_ERROR;
534 sqlite3_free(error_message);
536 /*Commit transaction*/
537 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
538 if (ret != SQLITE_OK) {
539 _LOGE("Failed to commit transaction. Rollback now\n");
540 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
541 __close_manifest_db();
542 return PMINFO_R_ERROR;
544 _LOGD("Transaction Commit and End\n");
545 __close_manifest_db();
549 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
551 return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, _getuid());
554 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
557 char query[MAX_QUERY_LEN] = {'\0'};
560 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
562 ret = __open_manifest_db(uid, false);
564 _LOGE("Fail to open manifest DB\n");
565 return PMINFO_R_ERROR;
568 /*Begin transaction*/
569 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
570 if (ret != SQLITE_OK) {
571 _LOGE("Failed to begin transaction\n");
572 __close_manifest_db();
573 return PMINFO_R_ERROR;
575 _LOGD("Transaction Begin\n");
577 memset(query, '\0', MAX_QUERY_LEN);
578 snprintf(query, MAX_QUERY_LEN,
579 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
582 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
583 _LOGE("Don't execute query = %s error message = %s\n", query,
585 sqlite3_free(error_message);
586 return PMINFO_R_ERROR;
589 /*Commit transaction*/
590 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
591 if (ret != SQLITE_OK) {
592 _LOGE("Failed to commit transaction. Rollback now\n");
593 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
594 __close_manifest_db();
595 return PMINFO_R_ERROR;
597 _LOGD("Transaction Commit and End\n");
598 __close_manifest_db();
602 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
604 return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, _getuid());
607 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
611 char query[MAX_QUERY_LEN] = {'\0'};
613 sqlite3 *pkgmgr_parser_db;
615 retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
617 pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
618 val = info->app_info->guestmode_visibility;
620 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
621 SQLITE_OPEN_READWRITE, NULL);
622 if (ret != SQLITE_OK) {
623 _LOGE("DB Open Failed\n");
624 return PMINFO_R_ERROR;
627 /*TODO: Write to DB here*/
629 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
631 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
633 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
634 sqlite3_close(pkgmgr_parser_db);
635 if (ret != SQLITE_OK) {
636 _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
638 return PMINFO_R_ERROR;
644 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
646 return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, _getuid(), status);
649 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
651 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
653 sqlite3 *pkgmgr_parser_db = NULL;
656 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
657 SQLITE_OPEN_READWRITE, NULL);
658 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db failed!");
660 /*Begin transaction*/
661 // Setting Manifest DB
662 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
663 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
664 _LOGD("Transaction Begin\n");
666 // pkgcakge_info table
667 query = sqlite3_mprintf("update package_info set installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
669 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
670 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
673 // package_app_info table
674 query = sqlite3_mprintf("update package_app_info set app_installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
676 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
677 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
679 /*Commit transaction*/
680 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
681 if (ret != SQLITE_OK) {
682 _LOGE("Failed to commit transaction. Rollback now\n");
683 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
684 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
686 _LOGD("Transaction Commit and End\n");
690 sqlite3_close(pkgmgr_parser_db);
695 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
697 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, _getuid());