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 "create table if not exists package_cert_index_info " \
24 "(cert_info text not null, " \
26 "cert_ref_count integer, " \
27 "PRIMARY KEY(cert_id)) "
29 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO "create table if not exists package_cert_info " \
30 "(package text not null, " \
31 "author_root_cert integer, " \
32 "author_im_cert integer, " \
33 "author_signer_cert integer, " \
34 "dist_root_cert integer, " \
35 "dist_im_cert integer, " \
36 "dist_signer_cert integer, " \
37 "dist2_root_cert integer, " \
38 "dist2_im_cert integer, " \
39 "dist2_signer_cert integer, " \
40 "PRIMARY KEY(package)) "
41 __thread db_handle manifest_db;
42 __thread db_handle cert_db;
44 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
46 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
52 fullpath = strdup(dir);
55 subpath = dirname(fullpath);
56 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
57 ret = _mkdir_for_user(fullpath, uid, gid);
64 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
65 if (ret && errno != EEXIST) {
68 } else if (ret && errno == EEXIST) {
73 if (getuid() == ROOT_UID) {
74 ret = chown(dir, uid, gid);
76 _LOGE("FAIL : chown %s %d.%d, because %s", dir, uid,
77 gid, strerror(errno));
85 static const char *_get_db_path(uid_t uid) {
86 const char *db_path = NULL;
87 if (uid != GLOBAL_USER && uid != ROOT_UID) {
88 tzplatform_set_user(uid);
89 db_path = tzplatform_getenv(TZ_USER_DB);
90 tzplatform_reset_user();
92 db_path = tzplatform_getenv(TZ_SYS_DB);
97 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
101 char query[MAX_QUERY_LEN];
103 if (uid != GLOBAL_USER && uid != ROOT_UID) {
104 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
105 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
106 _LOGD("Don't execute query = %s error message = %s\n", query, err);
112 for (i = 0; tables[i]; i++) {
113 if (uid != GLOBAL_USER && uid != ROOT_UID)
114 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
115 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
116 SELECT *,1 AS for_all_users FROM Global.'%s')",
117 tables[i], tables[i], tables[i]);
119 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
120 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
121 tables[i], tables[i]);
122 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
123 _LOGD("Don't execute query = %s error message = %s\n", query, err);
131 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
133 char *error_message = NULL;
134 int ret = sqlite3_exec(db, query, callback, data, &error_message);
135 if (SQLITE_OK != ret) {
136 _LOGE("Don't execute query = %s error message = %s ret = %d\n", query,
138 sqlite3_free(error_message);
141 sqlite3_free(error_message);
145 int _check_create_cert_db(sqlite3 *certdb)
148 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
151 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
154 static gid_t _get_gid(const char *name)
161 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
162 if (ret || ge == NULL) {
163 _LOGE("fail to get gid of %s", name);
170 API const char *getIconPath(uid_t uid)
172 const char *path = NULL;
173 uid_t uid_caller = getuid();
174 gid_t gid = ROOT_UID;
176 if (uid != GLOBAL_USER && uid != ROOT_UID) {
177 tzplatform_set_user(uid);
178 path = tzplatform_mkpath(TZ_USER_ICONS, "/");
179 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
180 tzplatform_reset_user();
182 path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
185 // just allow certain users to create the icon directory if needed.
186 if (uid_caller == ROOT_UID || uid_caller == uid)
187 _mkdir_for_user(path, uid, gid);
192 API const char *getUserPkgParserDBPath(void)
194 return getUserPkgParserDBPathUID(GLOBAL_USER);
197 API const char *getUserPkgParserDBPathUID(uid_t uid)
199 const char *pkgmgr_parser_db = NULL;
200 uid_t uid_caller = getuid();
201 gid_t gid = ROOT_UID;
203 if (uid != GLOBAL_USER && uid != ROOT_UID) {
204 tzplatform_set_user(uid);
205 pkgmgr_parser_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_parser.db");
206 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
207 tzplatform_reset_user();
209 pkgmgr_parser_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
212 // just allow certain users to create the dbspace directory if needed.
213 if (uid_caller == ROOT_UID || uid_caller == uid) {
214 const char *db_path = _get_db_path(uid);
215 _mkdir_for_user(db_path, uid, gid);
218 return pkgmgr_parser_db;
221 API const char *getUserPkgCertDBPath(void)
223 return getUserPkgCertDBPathUID(GLOBAL_USER);
226 API const char *getUserPkgCertDBPathUID(uid_t uid)
228 const char *pkgmgr_cert_db = NULL;
229 uid_t uid_caller = getuid();
230 gid_t gid = ROOT_UID;
232 if (uid != GLOBAL_USER && uid != ROOT_UID) {
233 tzplatform_set_user(uid);
234 pkgmgr_cert_db = tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_cert.db");
235 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
236 tzplatform_reset_user();
238 pkgmgr_cert_db = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
241 // just allow certain users to create the dbspace directory if needed.
242 if (uid_caller == ROOT_UID || uid_caller == uid) {
243 const char *db_path = _get_db_path(uid);
244 _mkdir_for_user(db_path, uid, gid);
247 return pkgmgr_cert_db;
250 API const char *getUserDesktopPath(uid_t uid)
252 const char *path = NULL;
253 uid_t uid_caller = getuid();
254 gid_t gid = ROOT_UID;
256 if (uid != GLOBAL_USER && uid != ROOT_UID) {
257 tzplatform_set_user(uid);
258 path = tzplatform_mkpath(TZ_USER_DESKTOP, "/");
259 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
260 tzplatform_reset_user();
262 path = tzplatform_mkpath(TZ_SYS_RW_DESKTOP_APP, "/");
265 // just allow certain users to create the icon directory if needed.
266 if (uid_caller == ROOT_UID || uid_caller == uid)
267 _mkdir_for_user(path, uid, gid);
272 API const char *getUserManifestPath(uid_t uid)
274 const char *path = NULL;
275 uid_t uid_caller = getuid();
276 gid_t gid = ROOT_UID;
278 if (uid != GLOBAL_USER && uid != ROOT_UID) {
279 tzplatform_set_user(uid);
280 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
281 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
282 tzplatform_reset_user();
284 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
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 int __close_manifest_db(void)
296 if (manifest_db.ref) {
297 if (--manifest_db.ref == 0)
298 sqlite3_close(GET_DB(manifest_db));
304 static const char *parserdb_tables[] = {
305 "package_app_app_category",
307 "package_app_app_control",
308 "package_app_localized_info",
309 "package_app_app_metadata",
310 "package_app_share_allowed",
311 "package_app_app_permission",
312 "package_app_share_request",
314 "package_app_data_control",
315 "package_localized_info",
316 "package_app_icon_section_info",
317 "package_privilege_info",
318 "package_app_image_info",
322 int __open_manifest_db(uid_t uid, bool readonly)
325 const char *user_pkg_parser;
328 if (manifest_db.ref) {
333 user_pkg_parser = getUserPkgParserDBPathUID(uid);
334 if (access(user_pkg_parser, F_OK) != 0) {
335 _LOGE("Manifest DB does not exists !!");
339 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
340 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
342 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
346 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
347 parserdb_tables, uid);
348 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
354 int __close_cert_db(void)
357 if (--cert_db.ref == 0)
358 sqlite3_close_v2(GET_DB(cert_db));
361 _LOGE("Certificate DB is already closed !!\n");
365 static const char *certdb_tables[] = {
366 "package_cert_index_info",
371 int __open_cert_db(uid_t uid, bool readonly)
374 const char *user_cert_parser;
382 user_cert_parser = getUserPkgCertDBPathUID(uid);
383 if (access(user_cert_parser, F_OK) != 0) {
384 _LOGE("Cert DB does not exists !!");
388 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
389 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
391 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
395 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
397 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
403 void _save_column_str(sqlite3_stmt *stmt, int idx, const char **str)
407 val = (const char *)sqlite3_column_text(stmt, idx);
412 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
414 /* Should be implemented later */
418 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
421 char query[MAX_QUERY_LEN] = {'\0'};
424 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
427 ret = __open_manifest_db(uid, false);
428 if (ret != SQLITE_OK) {
429 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
430 return PMINFO_R_ERROR;
433 /*Begin transaction*/
434 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
435 if (ret != SQLITE_OK) {
436 _LOGE("Failed to begin transaction\n");
437 __close_manifest_db();
438 return PMINFO_R_ERROR;
440 _LOGD("Transaction Begin\n");
442 memset(query, '\0', MAX_QUERY_LEN);
443 snprintf(query, MAX_QUERY_LEN,
444 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
447 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
448 _LOGE("Don't execute query = %s error message = %s\n", query,
450 sqlite3_free(error_message);
451 return PMINFO_R_ERROR;
453 sqlite3_free(error_message);
455 /*Commit transaction*/
456 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
457 if (ret != SQLITE_OK) {
458 _LOGE("Failed to commit transaction. Rollback now\n");
459 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
460 __close_manifest_db();
461 return PMINFO_R_ERROR;
463 _LOGD("Transaction Commit and End\n");
464 __close_manifest_db();
468 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
470 return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, GLOBAL_USER);
473 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
476 char query[MAX_QUERY_LEN] = {'\0'};
479 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
481 ret = __open_manifest_db(uid, false);
483 _LOGE("Fail to open manifest DB\n");
484 return PMINFO_R_ERROR;
487 /*Begin transaction*/
488 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
489 if (ret != SQLITE_OK) {
490 _LOGE("Failed to begin transaction\n");
491 __close_manifest_db();
492 return PMINFO_R_ERROR;
494 _LOGD("Transaction Begin\n");
496 memset(query, '\0', MAX_QUERY_LEN);
497 snprintf(query, MAX_QUERY_LEN,
498 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
501 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
502 _LOGE("Don't execute query = %s error message = %s\n", query,
504 sqlite3_free(error_message);
505 return PMINFO_R_ERROR;
508 /*Commit transaction*/
509 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
510 if (ret != SQLITE_OK) {
511 _LOGE("Failed to commit transaction. Rollback now\n");
512 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
513 __close_manifest_db();
514 return PMINFO_R_ERROR;
516 _LOGD("Transaction Commit and End\n");
517 __close_manifest_db();
521 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
523 return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, GLOBAL_USER);
526 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
530 char query[MAX_QUERY_LEN] = {'\0'};
532 sqlite3 *pkgmgr_parser_db;
534 retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
536 pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
537 val = info->app_info->guestmode_visibility;
539 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
540 SQLITE_OPEN_READWRITE, NULL);
541 if (ret != SQLITE_OK) {
542 _LOGE("DB Open Failed\n");
543 return PMINFO_R_ERROR;
546 /*TODO: Write to DB here*/
548 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
550 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
552 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
553 sqlite3_close(pkgmgr_parser_db);
554 if (ret != SQLITE_OK) {
555 _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
557 return PMINFO_R_ERROR;
563 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
565 return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, GLOBAL_USER, status);
568 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
570 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
573 sqlite3 *pkgmgr_parser_db = NULL;
576 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
577 SQLITE_OPEN_READWRITE, NULL);
578 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db failed!");
580 /*Begin transaction*/
581 // Setting Manifest DB
582 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
583 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
584 _LOGD("Transaction Begin\n");
586 // pkgcakge_info table
587 query = sqlite3_mprintf("update package_info set installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
589 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
590 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
593 // package_app_info table
594 query = sqlite3_mprintf("update package_app_info set app_installed_storage=%Q where package=%Q", location?"installed_external":"installed_internal", pkgid);
596 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
597 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
599 /*Commit transaction*/
600 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
601 if (ret != SQLITE_OK) {
602 _LOGE("Failed to commit transaction. Rollback now\n");
603 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
604 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
606 _LOGD("Transaction Commit and End\n");
610 sqlite3_close(pkgmgr_parser_db);
615 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
617 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, GLOBAL_USER);