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 " author_root_cert INTEGER, " \
34 " author_im_cert INTEGER, " \
35 " author_signer_cert INTEGER, " \
36 " dist_root_cert INTEGER, " \
37 " dist_im_cert INTEGER, " \
38 " dist_signer_cert INTEGER, " \
39 " dist2_root_cert INTEGER, " \
40 " dist2_im_cert INTEGER, " \
41 " dist2_signer_cert INTEGER)"
43 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
44 "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
45 "AFTER DELETE ON package_cert_info " \
47 " UPDATE package_cert_index_info SET" \
48 " cert_ref_count = cert_ref_count - 1" \
49 " WHERE cert_id = OLD.author_root_cert" \
50 " OR cert_id = OLD.author_im_cert" \
51 " OR cert_id = OLD.author_signer_cert" \
52 " OR cert_id = OLD.dist_root_cert" \
53 " OR cert_id = OLD.dist_im_cert" \
54 " OR cert_id = OLD.dist_signer_cert" \
55 " OR cert_id = OLD.dist2_root_cert" \
56 " OR cert_id = OLD.dist2_im_cert" \
57 " OR cert_id = OLD.dist2_signer_cert;" \
60 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
61 "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
62 "AFTER UPDATE ON package_cert_index_info " \
63 "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
64 " WHERE cert_id = OLD.cert_id) = 0) "\
66 " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
69 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \
70 "CREATE TRIGGER IF NOT EXISTS update_%s_info " \
71 "AFTER UPDATE ON package_cert_info " \
72 "WHEN (OLD.%s IS NOT NULL) " \
74 " UPDATE package_cert_index_info SET" \
75 " cert_ref_count = cert_ref_count - 1" \
76 " WHERE cert_id = OLD.%s;" \
79 __thread db_handle manifest_db;
80 __thread db_handle cert_db;
82 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
84 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
93 fullpath = strdup(dir);
96 subpath = dirname(fullpath);
97 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
98 ret = _mkdir_for_user(fullpath, uid, gid);
105 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
106 if (ret && errno != EEXIST) {
109 } else if (ret && errno == EEXIST) {
114 if (getuid() == ROOT_UID) {
115 fd = open(dir, O_RDONLY);
117 _LOGE("FAIL : open %s : %s", dir,
118 strerror_r(errno, buf, sizeof(buf)));
121 ret = fstat(fd, &sb);
123 _LOGE("FAIL : fstat %s : %s", dir,
124 strerror_r(errno, buf, sizeof(buf)));
128 if (S_ISLNK(sb.st_mode)) {
129 _LOGE("FAIL : %s is symlink!", dir);
133 ret = fchown(fd, uid, gid);
135 _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid,
136 gid, strerror_r(errno, buf, sizeof(buf)));
148 static const char *_get_db_path(uid_t uid) {
149 const char *db_path = NULL;
150 if (uid != GLOBAL_USER && uid != ROOT_UID) {
151 tzplatform_set_user(uid);
152 db_path = tzplatform_getenv(TZ_USER_DB);
153 tzplatform_reset_user();
155 db_path = tzplatform_getenv(TZ_SYS_DB);
160 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
164 char query[MAX_QUERY_LEN];
166 if (uid != GLOBAL_USER && uid != ROOT_UID) {
167 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
168 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
169 _LOGD("Don't execute query = %s error message = %s\n", query, err);
175 for (i = 0; tables[i]; i++) {
176 if (uid != GLOBAL_USER && uid != ROOT_UID)
177 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
178 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
179 SELECT *,1 AS for_all_users FROM Global.'%s')",
180 tables[i], tables[i], tables[i]);
182 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
183 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
184 tables[i], tables[i]);
185 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
186 _LOGD("Don't execute query = %s error message = %s\n", query, err);
194 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
196 char *error_message = NULL;
197 int ret = sqlite3_exec(db, query, callback, data, &error_message);
198 if (SQLITE_OK != ret) {
199 _LOGE("Don't execute query = %s error message = %s ret = %d\n", query,
201 sqlite3_free(error_message);
204 sqlite3_free(error_message);
208 int _check_create_cert_db(sqlite3 *certdb)
212 static const char *columns[] = {
213 "author_root_cert", "author_im_cert", "author_signer_cert",
214 "dist_root_cert", "dist_im_cert", "dist_signer_cert",
215 "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert",
218 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
221 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
224 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
227 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
229 for (i = 0; columns[i] != NULL; i++) {
230 snprintf(buf, sizeof(buf),
231 QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT,
232 columns[i], columns[i], columns[i]);
233 ret = __exec_db_query(certdb, buf, NULL, NULL);
239 static gid_t _get_gid(const char *name)
246 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
247 if (ret || ge == NULL) {
248 _LOGE("fail to get gid of %s", name);
255 API const char *getIconPath(uid_t uid, bool readonly)
257 const char *path = NULL;
258 uid_t uid_caller = getuid();
259 gid_t gid = ROOT_UID;
261 if (uid != GLOBAL_USER && uid != ROOT_UID) {
262 tzplatform_set_user(uid);
263 path = tzplatform_mkpath(TZ_USER_ICONS, "/");
264 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
265 tzplatform_reset_user();
268 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
270 path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
273 // just allow certain users to create the icon directory if needed.
274 if (uid_caller == ROOT_UID || uid_caller == uid)
275 _mkdir_for_user(path, uid, gid);
280 API const char *getUserPkgParserDBPath(void)
282 return getUserPkgParserDBPathUID(_getuid());
285 API const char *getUserPkgParserDBPathUID(uid_t uid)
287 const char *pkgmgr_parser_db = NULL;
288 uid_t uid_caller = getuid();
289 gid_t gid = ROOT_UID;
291 if (uid != GLOBAL_USER && uid != ROOT_UID) {
292 tzplatform_set_user(uid);
293 pkgmgr_parser_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_parser.db");
294 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
295 tzplatform_reset_user();
297 pkgmgr_parser_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
300 // just allow certain users to create the dbspace directory if needed.
301 if (uid_caller == ROOT_UID || uid_caller == uid) {
302 const char *db_path = _get_db_path(uid);
303 _mkdir_for_user(db_path, uid, gid);
306 return pkgmgr_parser_db;
309 API const char *getUserPkgCertDBPath(void)
311 return getUserPkgCertDBPathUID(_getuid());
314 API const char *getUserPkgCertDBPathUID(uid_t uid)
316 const char *pkgmgr_cert_db = NULL;
317 uid_t uid_caller = getuid();
318 gid_t gid = ROOT_UID;
320 if (uid != GLOBAL_USER && uid != ROOT_UID) {
321 tzplatform_set_user(uid);
322 pkgmgr_cert_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_cert.db");
323 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
324 tzplatform_reset_user();
326 pkgmgr_cert_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
329 // just allow certain users to create the dbspace directory if needed.
330 if (uid_caller == ROOT_UID || uid_caller == uid) {
331 const char *db_path = _get_db_path(uid);
332 _mkdir_for_user(db_path, uid, gid);
335 return pkgmgr_cert_db;
338 API const char *getUserManifestPath(uid_t uid, bool readonly)
340 const char *path = NULL;
341 uid_t uid_caller = getuid();
342 gid_t gid = ROOT_UID;
344 if (uid != GLOBAL_USER && uid != ROOT_UID) {
345 tzplatform_set_user(uid);
346 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
347 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
348 tzplatform_reset_user();
351 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
353 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
356 // just allow certain users to create the icon directory if needed.
357 if (uid_caller == ROOT_UID || uid_caller == uid)
358 _mkdir_for_user(path, uid, gid);
363 int __close_manifest_db(void)
365 if (manifest_db.ref) {
366 if (--manifest_db.ref == 0)
367 sqlite3_close(GET_DB(manifest_db));
373 static const char *parserdb_tables[] = {
374 "package_app_app_category",
376 "package_app_app_control",
377 "package_app_localized_info",
378 "package_app_app_metadata",
379 "package_app_share_allowed",
380 "package_app_app_permission",
381 "package_app_share_request",
383 "package_app_data_control",
384 "package_localized_info",
385 "package_app_icon_section_info",
386 "package_privilege_info",
387 "package_app_image_info",
391 int __open_manifest_db(uid_t uid, bool readonly)
394 const char *user_pkg_parser;
397 if (manifest_db.ref) {
402 user_pkg_parser = getUserPkgParserDBPathUID(uid);
403 if (access(user_pkg_parser, F_OK) != 0) {
404 _LOGE("Manifest DB does not exists !!");
408 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
409 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
411 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
415 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
416 parserdb_tables, uid);
417 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
423 int __close_cert_db(void)
426 if (--cert_db.ref == 0)
427 sqlite3_close_v2(GET_DB(cert_db));
430 _LOGE("Certificate DB is already closed !!\n");
434 static const char *certdb_tables[] = {
435 "package_cert_index_info",
440 int __open_cert_db(uid_t uid, bool readonly)
443 const char *user_cert_parser;
451 user_cert_parser = getUserPkgCertDBPathUID(uid);
452 if (access(user_cert_parser, F_OK) != 0) {
453 _LOGE("Cert DB does not exists !!");
457 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
458 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
460 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
464 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
466 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
472 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
474 *i = sqlite3_column_int(stmt, idx);
477 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
481 val = (const char *)sqlite3_column_text(stmt, idx);
486 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
488 /* Should be implemented later */
492 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
495 char query[MAX_QUERY_LEN] = {'\0'};
498 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
501 ret = __open_manifest_db(uid, false);
502 if (ret != SQLITE_OK) {
503 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
504 return PMINFO_R_ERROR;
507 /*Begin transaction*/
508 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
509 if (ret != SQLITE_OK) {
510 _LOGE("Failed to begin transaction\n");
511 __close_manifest_db();
512 return PMINFO_R_ERROR;
514 _LOGD("Transaction Begin\n");
516 memset(query, '\0', MAX_QUERY_LEN);
517 snprintf(query, MAX_QUERY_LEN,
518 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
521 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
522 _LOGE("Don't execute query = %s error message = %s\n", query,
524 sqlite3_free(error_message);
525 return PMINFO_R_ERROR;
527 sqlite3_free(error_message);
529 /*Commit transaction*/
530 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
531 if (ret != SQLITE_OK) {
532 _LOGE("Failed to commit transaction. Rollback now\n");
533 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
534 __close_manifest_db();
535 return PMINFO_R_ERROR;
537 _LOGD("Transaction Commit and End\n");
538 __close_manifest_db();
542 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
544 return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, _getuid());
547 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
550 char query[MAX_QUERY_LEN] = {'\0'};
553 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
555 ret = __open_manifest_db(uid, false);
557 _LOGE("Fail to open manifest DB\n");
558 return PMINFO_R_ERROR;
561 /*Begin transaction*/
562 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
563 if (ret != SQLITE_OK) {
564 _LOGE("Failed to begin transaction\n");
565 __close_manifest_db();
566 return PMINFO_R_ERROR;
568 _LOGD("Transaction Begin\n");
570 memset(query, '\0', MAX_QUERY_LEN);
571 snprintf(query, MAX_QUERY_LEN,
572 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
575 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
576 _LOGE("Don't execute query = %s error message = %s\n", query,
578 sqlite3_free(error_message);
579 return PMINFO_R_ERROR;
582 /*Commit transaction*/
583 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
584 if (ret != SQLITE_OK) {
585 _LOGE("Failed to commit transaction. Rollback now\n");
586 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
587 __close_manifest_db();
588 return PMINFO_R_ERROR;
590 _LOGD("Transaction Commit and End\n");
591 __close_manifest_db();
595 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
597 return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, _getuid());
600 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
604 char query[MAX_QUERY_LEN] = {'\0'};
606 sqlite3 *pkgmgr_parser_db;
608 retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
610 pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
611 val = info->app_info->guestmode_visibility;
613 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
614 SQLITE_OPEN_READWRITE, NULL);
615 if (ret != SQLITE_OK) {
616 _LOGE("DB Open Failed\n");
617 return PMINFO_R_ERROR;
620 /*TODO: Write to DB here*/
622 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
624 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
626 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
627 sqlite3_close(pkgmgr_parser_db);
628 if (ret != SQLITE_OK) {
629 _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
631 return PMINFO_R_ERROR;
637 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
639 return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, _getuid(), status);
642 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
644 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
646 sqlite3 *pkgmgr_parser_db = NULL;
649 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
650 SQLITE_OPEN_READWRITE, NULL);
651 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db failed!");
653 /*Begin transaction*/
654 // Setting Manifest DB
655 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
656 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
657 _LOGD("Transaction Begin\n");
659 // pkgcakge_info table
660 query = sqlite3_mprintf("update package_info set installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
662 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
663 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
666 // package_app_info table
667 query = sqlite3_mprintf("update package_app_info set app_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);
672 /*Commit transaction*/
673 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
674 if (ret != SQLITE_OK) {
675 _LOGE("Failed to commit transaction. Rollback now\n");
676 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
677 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
679 _LOGD("Transaction Commit and End\n");
683 sqlite3_close(pkgmgr_parser_db);
688 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
690 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, _getuid());