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 /* just allow certain users to create the icon directory if needed. */
284 if (path && (uid_caller == ROOT_UID ||
285 uid_caller == APPFW_UID || uid_caller == uid))
286 _mkdir_for_user(path, uid, gid);
291 API char *getUserPkgParserDBPath(void)
293 return getUserPkgParserDBPathUID(_getuid());
296 API char *getUserPkgParserDBPathUID(uid_t uid)
298 char pkgmgr_parser_db[PATH_MAX];
299 uid_t uid_caller = getuid();
300 gid_t gid = ROOT_UID;
303 db_path = _get_db_path(uid);
304 if (db_path == NULL) {
305 _LOGE("Failed to get db path %d", uid);
308 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
309 "%s/.pkgmgr_parser.db", db_path);
311 if (uid != GLOBAL_USER && uid != ROOT_UID) {
312 tzplatform_set_user(uid);
313 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
314 tzplatform_reset_user();
317 // just allow certain users to create the dbspace directory if needed.
318 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
319 _mkdir_for_user(db_path, uid, gid);
323 return strdup(pkgmgr_parser_db);
326 API char *getUserPkgCertDBPath(void)
328 return getUserPkgCertDBPathUID(_getuid());
331 API char *getUserPkgCertDBPathUID(uid_t uid)
333 char pkgmgr_cert_db[PATH_MAX];
334 uid_t uid_caller = getuid();
335 gid_t gid = ROOT_UID;
338 db_path = _get_db_path(uid);
339 if (db_path == NULL) {
340 _LOGE("Failed to get db path %d", uid);
343 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
344 "%s/.pkgmgr_cert.db", db_path);
346 if (uid != GLOBAL_USER && uid != ROOT_UID) {
347 tzplatform_set_user(uid);
348 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
349 tzplatform_reset_user();
352 // just allow certain users to create the dbspace directory if needed.
353 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
354 _mkdir_for_user(db_path, uid, gid);
358 return strdup(pkgmgr_cert_db);
361 API const char *getUserManifestPath(uid_t uid, bool readonly)
363 const char *path = NULL;
364 uid_t uid_caller = getuid();
365 gid_t gid = ROOT_UID;
367 if (uid != GLOBAL_USER && uid != ROOT_UID) {
368 tzplatform_set_user(uid);
369 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
370 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
371 tzplatform_reset_user();
374 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
376 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
379 /* just allow certain users to create the icon directory if needed. */
380 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
381 _mkdir_for_user(path, uid, gid);
386 int __close_manifest_db(void)
388 if (manifest_db.ref) {
389 if (--manifest_db.ref == 0)
390 sqlite3_close(GET_DB(manifest_db));
396 static const char *parserdb_tables[] = {
397 "package_app_app_category",
399 "package_app_app_control",
400 "package_app_localized_info",
401 "package_app_app_metadata",
402 "package_app_share_allowed",
403 "package_app_app_permission",
404 "package_app_share_request",
406 "package_app_data_control",
407 "package_localized_info",
408 "package_app_icon_section_info",
409 "package_privilege_info",
410 "package_app_image_info",
414 int __open_manifest_db(uid_t uid, bool readonly)
417 char *user_pkg_parser;
420 if (manifest_db.ref) {
425 user_pkg_parser = getUserPkgParserDBPathUID(uid);
426 if (user_pkg_parser == NULL) {
427 _LOGE("Failed to get pkg parser db path - %d", uid);
431 if (access(user_pkg_parser, F_OK) != 0) {
432 _LOGE("Manifest DB does not exists !!");
433 free(user_pkg_parser);
437 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
438 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
440 if (ret != SQLITE_OK) {
441 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
442 free(user_pkg_parser);
448 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
449 parserdb_tables, uid);
450 if (ret != SQLITE_OK) {
451 _LOGE("attach db [%s] failed!\n", user_pkg_parser);
452 free(user_pkg_parser);
457 free(user_pkg_parser);
462 int __close_cert_db(void)
465 if (--cert_db.ref == 0)
466 sqlite3_close_v2(GET_DB(cert_db));
469 _LOGE("Certificate DB is already closed !!\n");
473 static const char *certdb_tables[] = {
474 "package_cert_index_info",
479 int __open_cert_db(uid_t uid, bool readonly)
482 char *user_cert_parser;
490 user_cert_parser = getUserPkgCertDBPathUID(uid);
491 if (user_cert_parser == NULL) {
492 _LOGE("Failed to get pkg cert db path - %d", uid);
496 if (access(user_cert_parser, F_OK) != 0) {
497 _LOGE("Cert DB does not exists !!");
498 free(user_cert_parser);
502 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
503 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
505 if (ret != SQLITE_OK) {
506 _LOGE("connect db [%s] failed!", user_cert_parser);
507 free(user_cert_parser);
512 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
514 if (ret != SQLITE_OK) {
515 _LOGE("attach db [%s] failed!", user_cert_parser);
516 free(user_cert_parser);
521 free(user_cert_parser);
526 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
528 *i = sqlite3_column_int(stmt, idx);
531 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
535 val = (const char *)sqlite3_column_text(stmt, idx);
540 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
542 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
544 sqlite3 *pkgmgr_parser_db = NULL;
548 db_path = getUserPkgParserDBPathUID(uid);
549 if (db_path == NULL) {
550 _LOGE("Failed to get pkg parser db path - %d", uid);
551 return PMINFO_R_ERROR;
554 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
555 SQLITE_OPEN_READWRITE, NULL);
556 if (ret != SQLITE_OK) {
557 _LOGE("connect db failed!");
559 return PMINFO_R_ERROR;
563 /*Begin transaction*/
564 /* Setting Manifest DB */
565 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
566 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
567 _LOGD("Transaction Begin\n");
569 /* pkgcakge_info table */
570 query = sqlite3_mprintf(
571 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
572 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
574 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
575 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
578 /* package_app_info table */
579 query = sqlite3_mprintf(
580 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
581 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
583 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
584 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
586 /*Commit transaction*/
587 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
588 if (ret != SQLITE_OK) {
589 _LOGE("Failed to commit transaction. Rollback now\n");
590 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
591 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
593 _LOGD("Transaction Commit and End\n");
597 sqlite3_close(pkgmgr_parser_db);
602 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
604 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());