Refactor pkgmgr-info
[platform/core/appfw/pkgmgr-info.git] / src / server / initialize_db_internal.cc
1 /*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/smack.h>
22 #include <pwd.h>
23 #include <gio/gio.h>
24 #include <sqlite3.h>
25 #include <pkgmgr_parser_db_queries.h>
26 #include <system_info.h>
27
28 #include <fstream>
29 #include <string_view>
30
31 #include "pkgmgrinfo_internal.h"
32 #include "pkgmgrinfo_private.h"
33 #include "pkgmgrinfo_debug.h"
34
35 namespace {
36
37 constexpr const char DB_LABEL[] = "User::Home";
38 constexpr const char APPFW_USER[] = "app_fw";
39 constexpr const int OWNER_ROOT = 0;
40 constexpr const char DB_VERSION_PATH[] =
41     SYSCONFDIR "/package-manager/pkg_db_version.txt";
42 constexpr const char DB_VERSION_PATH_UNIT_TEST[] =
43     "./pkg_db_version.txt";
44 bool IsUnitTest;
45
46 void SetSmackLabel(const char* x) {
47   if (smack_setlabel(x, DB_LABEL, SMACK_LABEL_ACCESS))
48     _LOGE("failed chsmack -a %s %s", DB_LABEL, x);
49   else
50     _LOGD("chsmack -a %s %s", DB_LABEL, x);
51 }
52
53 const char* GetVersionPath() {
54   if (IsUnitTest)
55     return DB_VERSION_PATH_UNIT_TEST;
56   return DB_VERSION_PATH;
57 }
58
59 const char* GetCertDbPath() {
60   return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
61 }
62
63 int SetDbPermission(const char* path, uid_t uid) {
64   int fd;
65   const char *files[2];
66   char journal_file[BUFSIZE];
67   struct stat sb;
68   mode_t mode;
69   struct passwd pwd;
70   struct passwd *result;
71   char buf[BUFSIZE];
72   int ret;
73   int i;
74
75   if (getuid() != OWNER_ROOT)
76     return 0;
77
78   if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
79     ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
80     if (result == NULL) {
81       if (ret == 0)
82         _LOGE("no such user: %d", uid);
83       else
84         _LOGE("getpwuid_r failed: %d", errno);
85       return -1;
86     }
87     uid = pwd.pw_uid;
88   }
89
90   snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
91   files[0] = path;
92   files[1] = journal_file;
93
94   ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
95   if (result == NULL) {
96     if (ret == 0)
97       _LOGE("no such user: %d", uid);
98     else
99       _LOGE("getpwuid_r failed: %d", errno);
100     return -1;
101   }
102
103   for (i = 0; i < 2; i++) {
104     fd = open(files[i], O_RDONLY);
105     if (fd == -1) {
106       _LOGE("open %s failed: %d", files[i], errno);
107       return -1;
108     }
109     ret = fstat(fd, &sb);
110     if (ret == -1) {
111       _LOGE("stat %s failed: %d", files[i], errno);
112       close(fd);
113       return -1;
114     }
115     if (S_ISLNK(sb.st_mode)) {
116       _LOGE("%s is symlink!", files[i]);
117       close(fd);
118       return -1;
119     }
120     ret = fchown(fd, uid, pwd.pw_gid);
121     if (ret == -1) {
122       _LOGE("fchown %s failed: %d", files[i], errno);
123       close(fd);
124       return -1;
125     }
126
127     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
128     if (!strcmp(path, GetCertDbPath()))
129       mode |= S_IWOTH;
130     ret = fchmod(fd, mode);
131     if (ret == -1) {
132       _LOGD("fchmod %s failed: %d", files[i], errno);
133       close(fd);
134       return -1;
135     }
136     close(fd);
137     SetSmackLabel(files[i]);
138   }
139
140   return 0;
141 }
142
143 int SetDbVersion(const tizen_base::Database& db) {
144   std::ifstream file(GetVersionPath(), std::fstream::in);
145   if (!static_cast<bool>(file)) {
146     _LOGE("Failed to open db version file: %s", GetVersionPath());
147     return -1;
148   }
149
150   std::string version;
151   std::getline(file, version);
152   if (version.empty()) {
153     _LOGE("Failed to get version information");
154     return -1;
155   }
156
157   std::string sql = "PRAGMA user_version=" + version;
158   try {
159     db.OneStepExec({ sql });
160   } catch (const tizen_base::DbException& e) {
161     _LOGE("OneStepExec failed: %s", e.msg());
162     return -1;
163   }
164
165   return 0;
166 }
167
168 int CreateTables(const tizen_base::Database& db, const char** queries) {
169   try {
170     for (int i = 0; queries[i] != nullptr; i++)
171       db.OneStepExec({ queries[i] });
172   } catch (const tizen_base::DbException& e) {
173     _LOGE("OneStepExec failed: %s", e.msg());
174     return -1;
175   }
176
177   return 0;
178 }
179
180 int InitDb(const tizen_base::Database& db, std::string_view dbpath,
181     uid_t uid) {
182   const char** queries;
183
184   if (SetDbVersion(db))
185     return -1;
186
187   if (dbpath.rfind(".pkgmgr_parser.db") != std::string::npos) {
188     queries = PARSER_INIT_QUERIES;
189   } else if (dbpath.rfind(".pkgmgr_cert.db") != std::string::npos) {
190     queries = CERT_INIT_QUERIES;
191   } else {
192     _LOGE("unexpected dbpath: %s", dbpath.data());
193     return -1;
194   }
195
196   auto guard = db.CreateTransactionGuard();
197
198   if (CreateTables(db, queries) != 0)
199     return -1;
200
201   guard.Commit();
202   if (SetDbPermission(dbpath.data(), uid))
203     _LOGE("failed to set db permission");
204
205   return 0;
206 }
207
208 }  // namespace
209
210 namespace pkgmgr_server::internal {
211
212 void SetEnableUnitTest(bool enable) {
213   IsUnitTest = enable;
214 }
215
216 int InitializeDb(const tizen_base::Database& db, uid_t uid) {
217   const char* dbpath = sqlite3_db_filename(db.GetRaw(), "main");
218   if (dbpath == nullptr) {
219     _LOGE("Fail to get db filename");
220     return -1;
221   }
222
223   return InitDb(db, dbpath, uid);
224 }
225
226 }  // namespace pkgmgr_server::internal