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"
23 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
24 "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
25 " cert_info TEXT UNIQUE, " \
26 " cert_id INTEGER PRIMARY KEY, " \
27 " cert_ref_count INTEGER NOT NULL)"
29 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
30 "CREATE TABLE IF NOT EXISTS package_cert_info( " \
31 " package TEXT PRIMARY KEY, " \
32 " package_count INTEGER, " \
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_UPDATE_CERT_INFO \
44 "CREATE TRIGGER IF NOT EXISTS update_cert_info " \
45 "AFTER UPDATE ON package_cert_info " \
46 "WHEN (NEW.package_count = 0) " \
48 " DELETE FROM package_cert_info WHERE package=OLD.package;" \
51 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \
52 "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \
53 "AFTER UPDATE ON package_cert_info " \
54 "WHEN (NEW.package_count = OLD.package_count + 1) " \
56 " UPDATE package_cert_index_info SET" \
57 " cert_ref_count = cert_ref_count - 1" \
58 " WHERE cert_id = OLD.author_root_cert" \
59 " OR cert_id = OLD.author_im_cert" \
60 " OR cert_id = OLD.author_signer_cert" \
61 " OR cert_id = OLD.dist_root_cert" \
62 " OR cert_id = OLD.dist_im_cert" \
63 " OR cert_id = OLD.dist_signer_cert" \
64 " OR cert_id = OLD.dist2_root_cert" \
65 " OR cert_id = OLD.dist2_im_cert" \
66 " OR cert_id = OLD.dist2_signer_cert;" \
69 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
70 "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
71 "AFTER DELETE ON package_cert_info " \
73 " UPDATE package_cert_index_info SET" \
74 " cert_ref_count = cert_ref_count - 1" \
75 " WHERE cert_id = OLD.author_root_cert" \
76 " OR cert_id = OLD.author_im_cert" \
77 " OR cert_id = OLD.author_signer_cert" \
78 " OR cert_id = OLD.dist_root_cert" \
79 " OR cert_id = OLD.dist_im_cert" \
80 " OR cert_id = OLD.dist_signer_cert" \
81 " OR cert_id = OLD.dist2_root_cert" \
82 " OR cert_id = OLD.dist2_im_cert" \
83 " OR cert_id = OLD.dist2_signer_cert;" \
86 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
87 "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
88 "AFTER UPDATE ON package_cert_index_info " \
89 "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
90 " WHERE cert_id = OLD.cert_id) = 0) "\
92 " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
95 __thread db_handle manifest_db;
96 __thread db_handle cert_db;
98 typedef int (*sqlite_query_callback)(void *data, int ncols, char **coltxt, char **colname);
100 static int _mkdir_for_user(const char* dir, uid_t uid, gid_t gid)
109 fullpath = strdup(dir);
110 if (fullpath == NULL)
112 subpath = dirname(fullpath);
113 if (strlen(subpath) > 1 && strcmp(subpath, fullpath) != 0) {
114 ret = _mkdir_for_user(fullpath, uid, gid);
121 ret = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
122 if (ret && errno != EEXIST) {
125 } else if (ret && errno == EEXIST) {
130 if (getuid() == ROOT_UID) {
131 fd = open(dir, O_RDONLY);
133 _LOGE("FAIL : open %s : %s", dir,
134 strerror_r(errno, buf, sizeof(buf)));
137 ret = fstat(fd, &sb);
139 _LOGE("FAIL : fstat %s : %s", dir,
140 strerror_r(errno, buf, sizeof(buf)));
144 if (S_ISLNK(sb.st_mode)) {
145 _LOGE("FAIL : %s is symlink!", dir);
149 ret = fchown(fd, uid, gid);
151 _LOGE("FAIL : fchown %s %d.%d, because %s", dir, uid,
152 gid, strerror_r(errno, buf, sizeof(buf)));
164 static char *_get_db_path(uid_t uid)
169 db_path = tzplatform_getenv(TZ_SYS_DB);
170 if (db_path == NULL) {
171 _LOGE("Failed to get TZ_SYS_DB path");
175 if (uid == GLOBAL_USER || uid == ROOT_UID)
176 return strdup(db_path);
178 snprintf(path, sizeof(path), "%s/user/%d", db_path, uid);
183 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
187 char query[MAX_QUERY_LEN];
189 if (uid != GLOBAL_USER && uid != ROOT_UID) {
190 snprintf(query, sizeof(query), "ATTACH DATABASE '%s' AS Global", db);
191 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
192 _LOGD("Don't execute query = %s error message = %s\n", query, err);
198 for (i = 0; tables[i]; i++) {
199 if (uid != GLOBAL_USER && uid != ROOT_UID)
200 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
201 FROM (SELECT *,0 AS for_all_users FROM main.'%s' UNION \
202 SELECT *,1 AS for_all_users FROM Global.'%s')",
203 tables[i], tables[i], tables[i]);
205 snprintf(query, sizeof(query), "CREATE TEMP VIEW '%s' AS SELECT * \
206 FROM (SELECT *,1 AS for_all_users FROM main.'%s')",
207 tables[i], tables[i]);
208 if (SQLITE_OK != sqlite3_exec(handle, query, NULL, NULL, &err)) {
209 _LOGD("Don't execute query = %s error message = %s\n", query, err);
217 static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
219 char *error_message = NULL;
220 int ret = sqlite3_exec(db, query, callback, data, &error_message);
221 if (SQLITE_OK != ret) {
222 _LOGE("Don't execute query = %s error message = %s ret = %d\n", query,
224 sqlite3_free(error_message);
227 sqlite3_free(error_message);
231 int _check_create_cert_db(sqlite3 *certdb)
234 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
237 ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
240 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL);
243 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL);
246 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
249 ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
252 static gid_t _get_gid(const char *name)
259 ret = getgrnam_r(name, &entry, buf, sizeof(buf), &ge);
260 if (ret || ge == NULL) {
261 _LOGE("fail to get gid of %s", name);
268 API const char *getIconPath(uid_t uid, bool readonly)
270 const char *path = NULL;
271 uid_t uid_caller = getuid();
272 gid_t gid = ROOT_UID;
274 if (uid != GLOBAL_USER && uid != ROOT_UID) {
275 _LOGD("not supported target user");
280 path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
282 /* just allow certain users to create the icon directory if needed. */
283 if (path && (uid_caller == ROOT_UID ||
284 uid_caller == APPFW_UID || uid_caller == uid))
285 _mkdir_for_user(path, uid, gid);
290 API char *getUserPkgParserDBPath(void)
292 return getUserPkgParserDBPathUID(_getuid());
295 API char *getUserPkgParserDBPathUID(uid_t uid)
297 char pkgmgr_parser_db[PATH_MAX];
298 uid_t uid_caller = getuid();
299 gid_t gid = ROOT_UID;
302 db_path = _get_db_path(uid);
303 if (db_path == NULL) {
304 _LOGE("Failed to get db path %d", uid);
307 snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
308 "%s/.pkgmgr_parser.db", db_path);
310 if (uid != GLOBAL_USER && uid != ROOT_UID) {
311 tzplatform_set_user(uid);
312 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
313 tzplatform_reset_user();
316 /* just allow certain users to create the dbspace directory if needed. */
317 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
318 _mkdir_for_user(db_path, uid, gid);
322 return strdup(pkgmgr_parser_db);
325 API char *getUserPkgCertDBPath(void)
327 return getUserPkgCertDBPathUID(_getuid());
330 API char *getUserPkgCertDBPathUID(uid_t uid)
332 char pkgmgr_cert_db[PATH_MAX];
333 uid_t uid_caller = getuid();
334 gid_t gid = ROOT_UID;
337 db_path = _get_db_path(uid);
338 if (db_path == NULL) {
339 _LOGE("Failed to get db path %d", uid);
342 snprintf(pkgmgr_cert_db, sizeof(pkgmgr_cert_db),
343 "%s/.pkgmgr_cert.db", db_path);
345 if (uid != GLOBAL_USER && uid != ROOT_UID) {
346 tzplatform_set_user(uid);
347 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
348 tzplatform_reset_user();
351 /* just allow certain users to create the dbspace directory if needed. */
352 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
353 _mkdir_for_user(db_path, uid, gid);
357 return strdup(pkgmgr_cert_db);
360 API const char *getUserManifestPath(uid_t uid, bool readonly)
362 const char *path = NULL;
363 uid_t uid_caller = getuid();
364 gid_t gid = ROOT_UID;
366 if (uid != GLOBAL_USER && uid != ROOT_UID) {
367 tzplatform_set_user(uid);
368 path = tzplatform_mkpath(TZ_USER_PACKAGES, "/");
369 gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
370 tzplatform_reset_user();
373 path = tzplatform_mkpath(TZ_SYS_RO_PACKAGES, "/");
375 path = tzplatform_mkpath(TZ_SYS_RW_PACKAGES, "/");
378 /* just allow certain users to create the icon directory if needed. */
379 if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
380 _mkdir_for_user(path, uid, gid);
385 int __close_manifest_db(void)
387 if (manifest_db.ref) {
388 if (--manifest_db.ref == 0)
389 sqlite3_close(GET_DB(manifest_db));
395 static const char *parserdb_tables[] = {
396 "package_app_app_category",
398 "package_app_app_control",
399 "package_app_localized_info",
400 "package_app_app_metadata",
401 "package_app_share_allowed",
402 "package_app_app_permission",
403 "package_app_share_request",
405 "package_app_data_control",
406 "package_localized_info",
407 "package_app_icon_section_info",
408 "package_privilege_info",
409 "package_app_image_info",
413 int __open_manifest_db(uid_t uid, bool readonly)
416 char *user_pkg_parser;
419 if (manifest_db.ref) {
424 user_pkg_parser = getUserPkgParserDBPathUID(uid);
425 if (user_pkg_parser == NULL) {
426 _LOGE("Failed to get pkg parser db path - %d", uid);
430 if (access(user_pkg_parser, F_OK) != 0) {
431 _LOGE("Manifest DB does not exists !!");
432 free(user_pkg_parser);
436 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
437 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
439 if (ret != SQLITE_OK) {
440 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
441 free(user_pkg_parser);
447 ret = __attach_and_create_view(GET_DB(manifest_db), MANIFEST_DB,
448 parserdb_tables, uid);
449 if (ret != SQLITE_OK) {
450 _LOGE("attach db [%s] failed!\n", user_pkg_parser);
451 free(user_pkg_parser);
456 free(user_pkg_parser);
461 int __close_cert_db(void)
464 if (--cert_db.ref == 0)
465 sqlite3_close_v2(GET_DB(cert_db));
468 _LOGE("Certificate DB is already closed !!\n");
472 static const char *certdb_tables[] = {
473 "package_cert_index_info",
478 int __open_cert_db(uid_t uid, bool readonly)
481 char *user_cert_parser;
489 user_cert_parser = getUserPkgCertDBPathUID(uid);
490 if (user_cert_parser == NULL) {
491 _LOGE("Failed to get pkg cert db path - %d", uid);
495 if (access(user_cert_parser, F_OK) != 0) {
496 _LOGE("Cert DB does not exists !!");
497 free(user_cert_parser);
501 flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
502 ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db),
504 if (ret != SQLITE_OK) {
505 _LOGE("connect db [%s] failed!", user_cert_parser);
506 free(user_cert_parser);
511 ret = __attach_and_create_view(GET_DB(cert_db), CERT_DB,
513 if (ret != SQLITE_OK) {
514 _LOGE("attach db [%s] failed!", user_cert_parser);
515 free(user_cert_parser);
520 free(user_cert_parser);
525 void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
527 *i = sqlite3_column_int(stmt, idx);
530 void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
534 val = (const char *)sqlite3_column_text(stmt, idx);
539 API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
541 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
543 sqlite3 *pkgmgr_parser_db = NULL;
547 db_path = getUserPkgParserDBPathUID(uid);
548 if (db_path == NULL) {
549 _LOGE("Failed to get pkg parser db path - %d", uid);
550 return PMINFO_R_ERROR;
553 ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
554 SQLITE_OPEN_READWRITE, NULL);
555 if (ret != SQLITE_OK) {
556 _LOGE("connect db failed!");
558 return PMINFO_R_ERROR;
562 /*Begin transaction*/
563 /* Setting Manifest DB */
564 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
565 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
566 _LOGD("Transaction Begin\n");
568 /* pkgcakge_info table */
569 query = sqlite3_mprintf(
570 "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
571 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
573 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
574 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
577 /* package_app_info table */
578 query = sqlite3_mprintf(
579 "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
580 location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
582 ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
583 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
585 /*Commit transaction*/
586 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
587 if (ret != SQLITE_OK) {
588 _LOGE("Failed to commit transaction. Rollback now\n");
589 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
590 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
592 _LOGD("Transaction Commit and End\n");
596 sqlite3_close(pkgmgr_parser_db);
601 API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
603 return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());