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",
313 "package_app_app_svc",
315 "package_app_data_control",
316 "package_localized_info",
317 "package_app_icon_section_info",
318 "package_privilege_info",
319 "package_app_image_info",
323 int __open_manifest_db(uid_t uid, bool readonly)
326 const char *user_pkg_parser;
329 if (manifest_db.ref) {
334 user_pkg_parser = getUserPkgParserDBPathUID(uid);
335 if (access(user_pkg_parser, F_OK) != 0) {
336 _LOGE("Manifest DB does not exists !!");
340 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
341 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
343 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n",
347 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
348 parserdb_tables, uid);
349 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n",
355 int __close_cert_db(void)
358 if (--cert_db.ref == 0)
359 sqlite3_close_v2(GET_DB(cert_db));
362 _LOGE("Certificate DB is already closed !!\n");
366 static const char *certdb_tables[] = {
367 "package_cert_index_info",
372 int __open_cert_db(uid_t uid, bool readonly)
375 const char *user_cert_parser;
383 user_cert_parser = getUserPkgCertDBPathUID(uid);
384 if (access(user_cert_parser, F_OK) != 0) {
385 _LOGE("Cert DB does not exists !!");
389 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
390 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
392 retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!",
396 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
398 retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!",
404 void _save_column_str(sqlite3_stmt *stmt, int idx, const char **str)
408 val = (const char *)sqlite3_column_text(stmt, idx);
409 /* get string when the result is not null */
410 if (val && (strcasecmp(val, "(null)") != 0) && (strcmp(val, "") != 0))
414 API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
416 /* Should be implemented later */
420 API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
423 char query[MAX_QUERY_LEN] = {'\0'};
426 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
429 ret = __open_manifest_db(uid, false);
430 if (ret != SQLITE_OK) {
431 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
432 return PMINFO_R_ERROR;
435 /*Begin transaction*/
436 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
437 if (ret != SQLITE_OK) {
438 _LOGE("Failed to begin transaction\n");
439 __close_manifest_db();
440 return PMINFO_R_ERROR;
442 _LOGD("Transaction Begin\n");
444 memset(query, '\0', MAX_QUERY_LEN);
445 snprintf(query, MAX_QUERY_LEN,
446 "update package_app_info set app_enabled='%s' where app_id='%s'", enabled?"true":"false", appid);
449 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
450 _LOGE("Don't execute query = %s error message = %s\n", query,
452 sqlite3_free(error_message);
453 return PMINFO_R_ERROR;
455 sqlite3_free(error_message);
457 /*Commit transaction*/
458 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
459 if (ret != SQLITE_OK) {
460 _LOGE("Failed to commit transaction. Rollback now\n");
461 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
462 __close_manifest_db();
463 return PMINFO_R_ERROR;
465 _LOGD("Transaction Commit and End\n");
466 __close_manifest_db();
470 API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
472 return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, GLOBAL_USER);
475 API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
478 char query[MAX_QUERY_LEN] = {'\0'};
481 retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
483 ret = __open_manifest_db(uid, false);
485 _LOGE("Fail to open manifest DB\n");
486 return PMINFO_R_ERROR;
489 /*Begin transaction*/
490 ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
491 if (ret != SQLITE_OK) {
492 _LOGE("Failed to begin transaction\n");
493 __close_manifest_db();
494 return PMINFO_R_ERROR;
496 _LOGD("Transaction Begin\n");
498 memset(query, '\0', MAX_QUERY_LEN);
499 snprintf(query, MAX_QUERY_LEN,
500 "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
503 sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
504 _LOGE("Don't execute query = %s error message = %s\n", query,
506 sqlite3_free(error_message);
507 return PMINFO_R_ERROR;
510 /*Commit transaction*/
511 ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
512 if (ret != SQLITE_OK) {
513 _LOGE("Failed to commit transaction. Rollback now\n");
514 sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
515 __close_manifest_db();
516 return PMINFO_R_ERROR;
518 _LOGD("Transaction Commit and End\n");
519 __close_manifest_db();
523 API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
525 return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, GLOBAL_USER);
528 API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
532 char query[MAX_QUERY_LEN] = {'\0'};
534 sqlite3 *pkgmgr_parser_db;
536 retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
538 pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
539 val = info->uiapp_info->guestmode_visibility;
541 ret = db_util_open_with_options(getUserPkgParserDBPathUID(uid), &pkgmgr_parser_db,
542 SQLITE_OPEN_READWRITE, NULL);
543 if (ret != SQLITE_OK) {
544 _LOGE("DB Open Failed\n");
545 return PMINFO_R_ERROR;
548 /*TODO: Write to DB here*/
550 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->uiapp_info->appid);
552 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->uiapp_info->appid);
554 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
555 sqlite3_close(pkgmgr_parser_db);
556 if (ret != SQLITE_OK) {
557 _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
559 return PMINFO_R_ERROR;
565 API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
567 return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, GLOBAL_USER, status);