14 #include <tzplatform_config.h>
17 #include "pkgmgr-info.h"
18 #include "pkgmgrinfo_debug.h"
19 #include "pkgmgrinfo_private.h"
20 #include "pkgmgr_parser.h"
21 #include "pkgmgr_parser_internal.h"
23 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
24 "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
25 " cert_info TEXT UNIQUE, " \
26 " cert_id INTEGER PRIMARY KEY, " \
27 " cert_ref_count INTEGER NOT NULL)"
29 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
30 "CREATE TABLE IF NOT EXISTS package_cert_info( " \
31 " package TEXT PRIMARY KEY, " \
32 " author_root_cert INTEGER, " \
33 " author_im_cert INTEGER, " \
34 " author_signer_cert INTEGER, " \
35 " dist_root_cert INTEGER, " \
36 " dist_im_cert INTEGER, " \
37 " dist_signer_cert INTEGER, " \
38 " dist2_root_cert INTEGER, " \
39 " dist2_im_cert INTEGER, " \
40 " dist2_signer_cert INTEGER)"
42 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
43 "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
44 "AFTER DELETE ON package_cert_info " \
46 " UPDATE package_cert_index_info SET" \
47 " cert_ref_count = cert_ref_count - 1" \
48 " WHERE cert_id = OLD.author_root_cert" \
49 " OR cert_id = OLD.author_im_cert" \
50 " OR cert_id = OLD.author_signer_cert" \
51 " OR cert_id = OLD.dist_root_cert" \
52 " OR cert_id = OLD.dist_im_cert" \
53 " OR cert_id = OLD.dist_signer_cert" \
54 " OR cert_id = OLD.dist2_root_cert" \
55 " OR cert_id = OLD.dist2_im_cert" \
56 " OR cert_id = OLD.dist2_signer_cert;" \
59 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
60 "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
61 "AFTER UPDATE ON package_cert_index_info " \
62 "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
63 " WHERE cert_id = OLD.cert_id) = 0) "\
65 " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
68 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \
69 "CREATE TRIGGER IF NOT EXISTS update_%s_info " \
70 "AFTER UPDATE ON package_cert_info " \
71 "WHEN (OLD.%s IS NOT NULL) " \
73 " UPDATE package_cert_index_info SET" \
74 " cert_ref_count = cert_ref_count - 1" \
75 " WHERE cert_id = OLD.%s;" \
78 __thread db_handle manifest_db;
79 __thread db_handle cert_db;
81 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
83 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
90 fullpath = strdup(dir);
93 subpath = dirname(fullpath);
94 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
95 ret = _mkdir_for_user(fullpath, uid, gid);
102 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
103 if (ret && errno != EEXIST) {
106 } else if (ret && errno == EEXIST) {
111 if (getuid() == ROOT_UID) {
112 ret = chown(dir, uid, gid);
114 _LOGE("FAIL : chown %s %d.%d, because %s", dir, uid,
115 gid, strerror_r(errno, buf, sizeof(buf)));
123 static const char *_get_db_path(uid_t uid) {
124 const char *db_path = NULL;
125 if (uid != GLOBAL_USER && uid != ROOT_UID) {
126 tzplatform_set_user(uid);
127 db_path = tzplatform_getenv(TZ_USER_DB);
128 tzplatform_reset_user();
130 db_path = tzplatform_getenv(TZ_SYS_DB);
135 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
139 char query[MAX_QUERY_LEN];
141 if (uid != GLOBAL_USER && uid != ROOT_UID) {
142 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
143 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
144 _LOGD("Don't execute query = %s error message = %s\n", query, err);
150 for (i = 0; tables[i]; i++) {
151 if (uid != GLOBAL_USER && uid != ROOT_UID)
152 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
153 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
154 SELECT *,1 AS for_all_users FROM Global.'%s')",
155 tables[i], tables[i], tables[i]);
157 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
158 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
159 tables[i], tables[i]);
160 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
161 _LOGD("Don't execute query = %s error message = %s\n", query, err);
169 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
171 char *error_message = NULL;
172 int ret = sqlite3_exec(db, query, callback, data, &error_message);
173 if (SQLITE_OK != ret) {
174 _LOGE("Don't execute query = %s error message = %s ret = %d\n", query,
176 sqlite3_free(error_message);
179 sqlite3_free(error_message);
183 int _check_create_cert_db(sqlite3 *certdb)
187 static const char *columns[] = {
188 "author_root_cert", "author_im_cert", "author_signer_cert",
189 "dist_root_cert", "dist_im_cert", "dist_signer_cert",
190 "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert",
193 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
196 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
199 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
202 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
204 for (i = 0; columns[i] != NULL; i++) {
205 snprintf(buf, sizeof(buf),
206 QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT,
207 columns[i], columns[i], columns[i]);
208 ret = __exec_db_query(certdb, buf, NULL, NULL);
214 static gid_t _get_gid(const char *name)
221 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
222 if (ret || ge == NULL) {
223 _LOGE("fail to get gid of %s", name);
230 API const char *getIconPath(uid_t uid, bool readonly)
232 const char *path = NULL;
233 uid_t uid_caller = getuid();
234 gid_t gid = ROOT_UID;
236 if (uid != GLOBAL_USER && uid != ROOT_UID) {
237 tzplatform_set_user(uid);
238 path = tzplatform_mkpath(TZ_USER_ICONS, "/");
239 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
240 tzplatform_reset_user();
243 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
245 path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
248 // just allow certain users to create the icon directory if needed.
249 if (uid_caller == ROOT_UID || uid_caller == uid)
250 _mkdir_for_user(path, uid, gid);
255 API const char *getUserPkgParserDBPath(void)
257 return getUserPkgParserDBPathUID(_getuid());
260 API const char *getUserPkgParserDBPathUID(uid_t uid)
262 const char *pkgmgr_parser_db = NULL;
263 uid_t uid_caller = getuid();
264 gid_t gid = ROOT_UID;
266 if (uid != GLOBAL_USER && uid != ROOT_UID) {
267 tzplatform_set_user(uid);
268 pkgmgr_parser_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_parser.db");
269 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
270 tzplatform_reset_user();
272 pkgmgr_parser_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
275 // just allow certain users to create the dbspace directory if needed.
276 if (uid_caller == ROOT_UID || uid_caller == uid) {
277 const char *db_path = _get_db_path(uid);
278 _mkdir_for_user(db_path, uid, gid);
281 return pkgmgr_parser_db;
284 API const char *getUserPkgCertDBPath(void)
286 return getUserPkgCertDBPathUID(_getuid());
289 API const char *getUserPkgCertDBPathUID(uid_t uid)
291 const char *pkgmgr_cert_db = NULL;
292 uid_t uid_caller = getuid();
293 gid_t gid = ROOT_UID;
295 if (uid != GLOBAL_USER && uid != ROOT_UID) {
296 tzplatform_set_user(uid);
297 pkgmgr_cert_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_cert.db");
298 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
299 tzplatform_reset_user();
301 pkgmgr_cert_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
304 // just allow certain users to create the dbspace directory if needed.
305 if (uid_caller == ROOT_UID || uid_caller == uid) {
306 const char *db_path = _get_db_path(uid);
307 _mkdir_for_user(db_path, uid, gid);
310 return pkgmgr_cert_db;
313 API const char *getUserManifestPath(uid_t uid, bool readonly)
315 const char *path = NULL;
316 uid_t uid_caller = getuid();
317 gid_t gid = ROOT_UID;
319 if (uid != GLOBAL_USER && uid != ROOT_UID) {
320 tzplatform_set_user(uid);
321 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
322 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
323 tzplatform_reset_user();
326 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
328 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
331 // just allow certain users to create the icon directory if needed.
332 if (uid_caller == ROOT_UID || uid_caller == uid)
333 _mkdir_for_user(path, uid, gid);
338 int __close_manifest_db(void)
340 if (manifest_db.ref) {
341 if (--manifest_db.ref == 0)
342 sqlite3_close(GET_DB(manifest_db));
348 static const char *parserdb_tables[] = {
349 "package_app_app_category",
351 "package_app_app_control",
352 "package_app_localized_info",
353 "package_app_app_metadata",
354 "package_app_share_allowed",
355 "package_app_app_permission",
356 "package_app_share_request",
358 "package_app_data_control",
359 "package_localized_info",
360 "package_app_icon_section_info",
361 "package_privilege_info",
362 "package_app_image_info",
366 int __open_manifest_db(uid_t uid, bool readonly)
369 const char *user_pkg_parser;
372 if (manifest_db.ref) {
377 user_pkg_parser = getUserPkgParserDBPathUID(uid);
378 if (access(user_pkg_parser, F_OK) != 0) {
379 _LOGE("Manifest DB does not exists !!");
383 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
384 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
386 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
390 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
391 parserdb_tables, uid);
392 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
398 int __close_cert_db(void)
401 if (--cert_db.ref == 0)
402 sqlite3_close_v2(GET_DB(cert_db));
405 _LOGE("Certificate DB is already closed !!\n");
409 static const char *certdb_tables[] = {
410 "package_cert_index_info",
415 int __open_cert_db(uid_t uid, bool readonly)
418 const char *user_cert_parser;
426 user_cert_parser = getUserPkgCertDBPathUID(uid);
427 if (access(user_cert_parser, F_OK) != 0) {
428 _LOGE("Cert DB does not exists !!");
432 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
433 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
435 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
439 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
441 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
447 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
449 *i = sqlite3_column_int(stmt, idx);
452 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
456 val = (const char *)sqlite3_column_text(stmt, idx);
461 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
463 /* Should be implemented later */
467 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
470 char query[MAX_QUERY_LEN] = {'\0'};
473 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
476 ret = __open_manifest_db(uid, false);
477 if (ret != SQLITE_OK) {
478 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
479 return PMINFO_R_ERROR;
482 /*Begin transaction*/
483 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
484 if (ret != SQLITE_OK) {
485 _LOGE("Failed to begin transaction\n");
486 __close_manifest_db();
487 return PMINFO_R_ERROR;
489 _LOGD("Transaction Begin\n");
491 memset(query, '\0', MAX_QUERY_LEN);
492 snprintf(query, MAX_QUERY_LEN,
493 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
496 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
497 _LOGE("Don't execute query = %s error message = %s\n", query,
499 sqlite3_free(error_message);
500 return PMINFO_R_ERROR;
502 sqlite3_free(error_message);
504 /*Commit transaction*/
505 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
506 if (ret != SQLITE_OK) {
507 _LOGE("Failed to commit transaction. Rollback now\n");
508 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
509 __close_manifest_db();
510 return PMINFO_R_ERROR;
512 _LOGD("Transaction Commit and End\n");
513 __close_manifest_db();
517 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
519 return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, _getuid());
522 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
525 char query[MAX_QUERY_LEN] = {'\0'};
528 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
530 ret = __open_manifest_db(uid, false);
532 _LOGE("Fail to open manifest DB\n");
533 return PMINFO_R_ERROR;
536 /*Begin transaction*/
537 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
538 if (ret != SQLITE_OK) {
539 _LOGE("Failed to begin transaction\n");
540 __close_manifest_db();
541 return PMINFO_R_ERROR;
543 _LOGD("Transaction Begin\n");
545 memset(query, '\0', MAX_QUERY_LEN);
546 snprintf(query, MAX_QUERY_LEN,
547 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
550 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
551 _LOGE("Don't execute query = %s error message = %s\n", query,
553 sqlite3_free(error_message);
554 return PMINFO_R_ERROR;
557 /*Commit transaction*/
558 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
559 if (ret != SQLITE_OK) {
560 _LOGE("Failed to commit transaction. Rollback now\n");
561 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
562 __close_manifest_db();
563 return PMINFO_R_ERROR;
565 _LOGD("Transaction Commit and End\n");
566 __close_manifest_db();
570 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
572 return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, _getuid());
575 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
579 char query[MAX_QUERY_LEN] = {'\0'};
581 sqlite3 *pkgmgr_parser_db;
583 retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
585 pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
586 val = info->app_info->guestmode_visibility;
588 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
589 SQLITE_OPEN_READWRITE, NULL);
590 if (ret != SQLITE_OK) {
591 _LOGE("DB Open Failed\n");
592 return PMINFO_R_ERROR;
595 /*TODO: Write to DB here*/
597 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
599 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
601 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
602 sqlite3_close(pkgmgr_parser_db);
603 if (ret != SQLITE_OK) {
604 _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
606 return PMINFO_R_ERROR;
612 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
614 return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, _getuid(), status);
617 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
619 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
621 sqlite3 *pkgmgr_parser_db = NULL;
624 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
625 SQLITE_OPEN_READWRITE, NULL);
626 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db failed!");
628 /*Begin transaction*/
629 // Setting Manifest DB
630 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
631 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
632 _LOGD("Transaction Begin\n");
634 // pkgcakge_info table
635 query = sqlite3_mprintf("update package_info set installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
637 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
638 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
641 // package_app_info table
642 query = sqlite3_mprintf("update package_app_info set app_installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
644 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
645 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
647 /*Commit transaction*/
648 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
649 if (ret != SQLITE_OK) {
650 _LOGE("Failed to commit transaction. Rollback now\n");
651 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
652 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
654 _LOGD("Transaction Commit and End\n");
658 sqlite3_close(pkgmgr_parser_db);
663 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
665 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, _getuid());