2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/types.h>
23 #include <sys/smack.h>
29 #include <pkgmgr_parser_db_queries.h>
30 #include <system_info.h>
32 #include "pkgmgrinfo_internal.h"
33 #include "pkgmgrinfo_private.h"
34 #include "pkgmgrinfo_debug.h"
41 #define APPFW_USER "app_fw"
44 /* TODO: Do not labeling directly */
45 #define DB_LABEL "User::Home"
46 #define SET_SMACK_LABEL(x) \
48 if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
49 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
51 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
54 #define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
56 static const char *__get_cert_db_path(void)
58 return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
61 static int __set_db_permission(const char *path, uid_t uid)
65 char journal_file[BUFSIZE];
69 struct passwd *result;
74 if (getuid() != OWNER_ROOT)
77 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
78 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
81 _LOGE("no such user: %d", uid);
83 _LOGE("getpwuid_r failed: %d", errno);
89 snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
91 files[1] = journal_file;
93 ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
96 _LOGE("no such user: %d", uid);
98 _LOGE("getpwuid_r failed: %d", errno);
102 for (i = 0; i < 2; i++) {
103 fd = open(files[i], O_RDONLY);
105 _LOGE("open %s failed: %d", files[i], errno);
108 ret = fstat(fd, &sb);
110 _LOGE("stat %s failed: %d", files[i], errno);
114 if (S_ISLNK(sb.st_mode)) {
115 _LOGE("%s is symlink!", files[i]);
119 ret = fchown(fd, uid, pwd.pw_gid);
121 _LOGE("fchown %s failed: %d", files[i], errno);
126 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
127 if (!strcmp(path, __get_cert_db_path()))
129 ret = fchmod(fd, mode);
131 _LOGD("fchmod %s failed: %d", files[i], errno);
136 SET_SMACK_LABEL(files[i]);
142 static int __set_db_version(sqlite3 *db) {
143 static const char query_raw[] = "PRAGMA user_version=%Q";
147 char version[PKG_STRING_LEN_MAX] = { 0 };
150 fp = fopen(DB_VERSION_PATH, "r");
152 _LOGE("Failed to open db version file");
156 if (fgets(version, sizeof(version), fp) == NULL) {
157 _LOGE("Failed to get version information");
163 query = sqlite3_mprintf(query_raw, version);
165 _LOGE("Out of memory");
169 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
171 if (ret != SQLITE_OK) {
172 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
176 ret = sqlite3_step(stmt);
177 if (ret != SQLITE_DONE) {
178 _LOGE("sqlite3_step failed: %d", ret);
179 sqlite3_finalize(stmt);
183 sqlite3_finalize(stmt);
188 static int __create_tables(sqlite3 *db, const char **queries)
192 for (i = 0; queries[i] != NULL; i++) {
193 ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
194 if (ret != SQLITE_OK) {
195 _LOGE("exec failed: %s", sqlite3_errmsg(db));
202 static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
204 const char **queries;
206 if (__set_db_version(db))
209 if (strstr(dbpath, ".pkgmgr_parser.db")) {
210 queries = PARSER_INIT_QUERIES;
211 } else if (strstr(dbpath, ".pkgmgr_cert.db")) {
212 queries = CERT_INIT_QUERIES;
214 _LOGE("unexpected dbpath: %s", dbpath);
218 __BEGIN_TRANSACTION(db);
219 __DO_TRANSACTION(db, __create_tables(db, queries));
220 __END_TRANSACTION(db);
222 if (__set_db_permission(dbpath, uid))
223 _LOGE("failed to set db permission");
228 API int pkgmgr_parser_internal_initialize_db(sqlite3 *db, uid_t uid)
232 dbpath = sqlite3_db_filename(db, "main");
233 if (dbpath == NULL) {
234 _LOGE("Fail to get db filename");
238 return __initialize_db(db, dbpath, uid);