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 char *_get_db_path(uid_t uid)
170 db_path = tzplatform_getenv(TZ_SYS_DB);
171 if (db_path == NULL) {
172 _LOGE("Failed to get TZ_SYS_DB path");
176 if (uid == GLOBAL_USER || uid == ROOT_UID)
177 return strdup(db_path);
179 snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
184 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
188 char query[MAX_QUERY_LEN];
190 if (uid != GLOBAL_USER && uid != ROOT_UID) {
191 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
192 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
193 _LOGD("Don't execute query = %s error message = %s\n", query, err);
199 for (i = 0; tables[i]; i++) {
200 if (uid != GLOBAL_USER && uid != ROOT_UID)
201 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
202 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
203 SELECT *,1 AS for_all_users FROM Global.'%s')",
204 tables[i], tables[i], tables[i]);
206 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
207 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
208 tables[i], tables[i]);
209 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
210 _LOGD("Don't execute query = %s error message = %s\n", query, err);
218 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
220 char *error_message = NULL;
221 int ret = sqlite3_exec(db, query, callback, data, &error_message);
222 if (SQLITE_OK != ret) {
223 _LOGE("Don't execute query = %s error message = %s ret = %d\n", query,
225 sqlite3_free(error_message);
228 sqlite3_free(error_message);
232 int _check_create_cert_db(sqlite3 *certdb)
235 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
238 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
241 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL);
244 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL);
247 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
250 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
253 static gid_t _get_gid(const char *name)
260 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
261 if (ret || ge == NULL) {
262 _LOGE("fail to get gid of %s", name);
269 API const char *getIconPath(uid_t uid, bool readonly)
271 const char *path = NULL;
272 uid_t uid_caller = getuid();
273 gid_t gid = ROOT_UID;
275 if (uid != GLOBAL_USER && uid != ROOT_UID) {
276 _LOGD("not supported target user");
281 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
283 path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
285 /* just allow certain users to create the icon directory if needed. */
286 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
287 _mkdir_for_user(path, uid, gid);
292 API char *getUserPkgParserDBPath(void)
294 return getUserPkgParserDBPathUID(_getuid());
297 API char *getUserPkgParserDBPathUID(uid_t uid)
299 char pkgmgr_parser_db[PATH_MAX];
300 uid_t uid_caller = getuid();
301 gid_t gid = ROOT_UID;
304 db_path = _get_db_path(uid);
305 if (db_path == NULL) {
306 _LOGE("Failed to get db path %d", uid);
309 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
310 "%s/.pkgmgr_parser.db", db_path);
312 if (uid != GLOBAL_USER && uid != ROOT_UID) {
313 tzplatform_set_user(uid);
314 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
315 tzplatform_reset_user();
318 // just allow certain users to create the dbspace directory if needed.
319 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
320 _mkdir_for_user(db_path, uid, gid);
324 return strdup(pkgmgr_parser_db);
327 API char *getUserPkgCertDBPath(void)
329 return getUserPkgCertDBPathUID(_getuid());
332 API char *getUserPkgCertDBPathUID(uid_t uid)
334 char pkgmgr_cert_db[PATH_MAX];
335 uid_t uid_caller = getuid();
336 gid_t gid = ROOT_UID;
339 db_path = _get_db_path(uid);
340 if (db_path == NULL) {
341 _LOGE("Failed to get db path %d", uid);
344 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
345 "%s/.pkgmgr_cert.db", db_path);
347 if (uid != GLOBAL_USER && uid != ROOT_UID) {
348 tzplatform_set_user(uid);
349 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
350 tzplatform_reset_user();
353 // just allow certain users to create the dbspace directory if needed.
354 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
355 _mkdir_for_user(db_path, uid, gid);
359 return strdup(pkgmgr_cert_db);
362 API const char *getUserManifestPath(uid_t uid, bool readonly)
364 const char *path = NULL;
365 uid_t uid_caller = getuid();
366 gid_t gid = ROOT_UID;
368 if (uid != GLOBAL_USER && uid != ROOT_UID) {
369 tzplatform_set_user(uid);
370 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
371 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
372 tzplatform_reset_user();
375 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
377 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
380 /* just allow certain users to create the icon directory if needed. */
381 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
382 _mkdir_for_user(path, uid, gid);
387 int __close_manifest_db(void)
389 if (manifest_db.ref) {
390 if (--manifest_db.ref == 0)
391 sqlite3_close(GET_DB(manifest_db));
397 static const char *parserdb_tables[] = {
398 "package_app_app_category",
400 "package_app_app_control",
401 "package_app_localized_info",
402 "package_app_app_metadata",
403 "package_app_share_allowed",
404 "package_app_app_permission",
405 "package_app_share_request",
407 "package_app_data_control",
408 "package_localized_info",
409 "package_app_icon_section_info",
410 "package_privilege_info",
411 "package_app_image_info",
415 int __open_manifest_db(uid_t uid, bool readonly)
418 char *user_pkg_parser;
421 if (manifest_db.ref) {
426 user_pkg_parser = getUserPkgParserDBPathUID(uid);
427 if (user_pkg_parser == NULL) {
428 _LOGE("Failed to get pkg parser db path - %d", uid);
432 if (access(user_pkg_parser, F_OK) != 0) {
433 _LOGE("Manifest DB does not exists !!");
434 free(user_pkg_parser);
438 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
439 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
441 if (ret != SQLITE_OK) {
442 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
443 free(user_pkg_parser);
449 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
450 parserdb_tables, uid);
451 if (ret != SQLITE_OK) {
452 _LOGE("attach db [%s] failed!\n", user_pkg_parser);
453 free(user_pkg_parser);
458 free(user_pkg_parser);
463 int __close_cert_db(void)
466 if (--cert_db.ref == 0)
467 sqlite3_close_v2(GET_DB(cert_db));
470 _LOGE("Certificate DB is already closed !!\n");
474 static const char *certdb_tables[] = {
475 "package_cert_index_info",
480 int __open_cert_db(uid_t uid, bool readonly)
483 char *user_cert_parser;
491 user_cert_parser = getUserPkgCertDBPathUID(uid);
492 if (user_cert_parser == NULL) {
493 _LOGE("Failed to get pkg cert db path - %d", uid);
497 if (access(user_cert_parser, F_OK) != 0) {
498 _LOGE("Cert DB does not exists !!");
499 free(user_cert_parser);
503 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
504 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
506 if (ret != SQLITE_OK) {
507 _LOGE("connect db [%s] failed!", user_cert_parser);
508 free(user_cert_parser);
513 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
515 if (ret != SQLITE_OK) {
516 _LOGE("attach db [%s] failed!", user_cert_parser);
517 free(user_cert_parser);
522 free(user_cert_parser);
527 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
529 *i = sqlite3_column_int(stmt, idx);
532 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
536 val = (const char *)sqlite3_column_text(stmt, idx);
541 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
543 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
545 sqlite3 *pkgmgr_parser_db = NULL;
549 db_path = getUserPkgParserDBPathUID(uid);
550 if (db_path == NULL) {
551 _LOGE("Failed to get pkg parser db path - %d", uid);
552 return PMINFO_R_ERROR;
555 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
556 SQLITE_OPEN_READWRITE, NULL);
557 if (ret != SQLITE_OK) {
558 _LOGE("connect db failed!");
560 return PMINFO_R_ERROR;
564 /*Begin transaction*/
565 /* Setting Manifest DB */
566 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
567 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
568 _LOGD("Transaction Begin\n");
570 /* pkgcakge_info table */
571 query = sqlite3_mprintf(
572 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
573 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
575 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
576 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
579 /* package_app_info table */
580 query = sqlite3_mprintf(
581 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
582 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
584 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
585 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
587 /*Commit transaction*/
588 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
589 if (ret != SQLITE_OK) {
590 _LOGE("Failed to commit transaction. Rollback now\n");
591 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
592 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
594 _LOGD("Transaction Commit and End\n");
598 sqlite3_close(pkgmgr_parser_db);
603 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
605 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());