9 #include <linux/limits.h>
13 #include "pkgmgr-server.h"
16 #define RUN_DIR "/run/user"
19 #define DB_DIR "/var/lib/package-manager"
21 #define RESTRICTION_CONF ".package_manager_restriction_mode"
24 static int __set_mode(int cur, int mode)
29 static int __unset_mode(int cur, int mode)
34 static char *__get_conf_file_path(uid_t uid)
38 snprintf(buf, sizeof(buf), "%s/%d/%s", RUN_DIR, uid, RESTRICTION_CONF);
43 static int __set_restriction_mode(uid_t uid, int mode)
50 conf_path = __get_conf_file_path(uid);
51 if (conf_path == NULL) {
52 ERR("failed to get conf path");
56 fd = open(conf_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
58 ERR("failed to open conf file: %d", errno);
63 len = read(fd, &cur, sizeof(int));
65 ERR("failed to read conf file: %d", errno);
71 mode = __set_mode(cur, mode);
73 lseek(fd, 0, SEEK_SET);
74 len = write(fd, &mode, sizeof(int));
76 ERR("failed to write conf file: %d", errno);
88 static int __unset_restriction_mode(uid_t uid, int mode)
95 conf_path = __get_conf_file_path(uid);
96 if (conf_path == NULL) {
97 ERR("failed to get conf path");
101 if (access(conf_path, F_OK) != 0) {
106 fd = open(conf_path, O_RDWR, 0);
108 ERR("failed to open conf file(%s): %d", conf_path, errno);
113 len = read(fd, &cur, sizeof(int));
115 ERR("failed to read conf file: %d", errno);
121 mode = __unset_mode(cur, mode);
123 lseek(fd, 0, SEEK_SET);
124 len = write(fd, &mode, sizeof(int));
126 ERR("failed to write conf file: %d", errno);
138 static int __get_restriction_mode(uid_t uid, int *mode)
145 conf_path = __get_conf_file_path(uid);
146 if (conf_path == NULL)
149 if (access(conf_path, F_OK) != 0) {
155 fd = open(conf_path, O_RDONLY, 0);
157 ERR("failed to open conf file(%s): %d", conf_path, errno);
162 len = read(fd, &cur, sizeof(int));
164 ERR("failed to read conf file: %d", errno);
178 static const char *__get_db_path(void)
180 return DB_DIR"/restriction.db";
183 static sqlite3 *__open_db(void)
189 path = __get_db_path();
191 ERR("get db path error");
195 ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
196 if (ret != SQLITE_OK) {
197 ERR("open db error: %d", ret);
209 static int __update_pkg_restriction_mode(uid_t uid, const char *pkgid, int mode,
212 static const char query[] =
213 "INSERT OR REPLACE INTO restriction (uid, pkgid, mode) "
214 "VALUES(?, ?, (COALESCE("
215 " (SELECT mode FROM restriction WHERE uid=? AND pkgid=?), 0) ";
225 snprintf(buf, sizeof(buf), "%s%s", query,
226 type == TYPE_SET ? "| ?))" : "& ~?))");
228 ret = sqlite3_prepare_v2(db, buf, strlen(buf), &stmt, NULL);
229 if (ret != SQLITE_OK) {
230 ERR("prepare error: %s", sqlite3_errmsg(db));
231 sqlite3_close_v2(db);
235 sqlite3_bind_int(stmt, 1, uid);
236 sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
237 sqlite3_bind_int(stmt, 3, uid);
238 sqlite3_bind_text(stmt, 4, pkgid, -1, SQLITE_STATIC);
239 sqlite3_bind_int(stmt, 5, mode);
241 ret = sqlite3_step(stmt);
242 sqlite3_finalize(stmt);
243 if (ret != SQLITE_DONE) {
244 ERR("step error: %s", sqlite3_errmsg(db));
245 sqlite3_close_v2(db);
249 sqlite3_close_v2(db);
254 static int __get_pkg_restriction_mode(uid_t uid, const char *pkgid, int *mode)
256 static const char query[] =
258 " (SELECT mode FROM restriction WHERE uid=? AND pkgid=?), 0)";
267 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
268 if (ret != SQLITE_OK) {
269 ERR("prepare error: %s", sqlite3_errmsg(db));
270 sqlite3_close_v2(db);
274 sqlite3_bind_int(stmt, 1, uid);
275 sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_STATIC);
277 ret = sqlite3_step(stmt);
278 if (ret != SQLITE_ROW) {
279 ERR("step error: %s", sqlite3_errmsg(db));
280 sqlite3_finalize(stmt);
281 sqlite3_close_v2(db);
285 *mode = sqlite3_column_int(stmt, 0);
287 sqlite3_finalize(stmt);
288 sqlite3_close_v2(db);
293 int _set_restriction_mode(uid_t uid, const char *pkgid, int mode)
295 if (pkgid && strlen(pkgid))
296 return __update_pkg_restriction_mode(uid, pkgid, mode,
299 return __set_restriction_mode(uid, mode);
302 int _unset_restriction_mode(uid_t uid, const char *pkgid, int mode)
304 if (pkgid && strlen(pkgid))
305 return __update_pkg_restriction_mode(uid, pkgid, mode,
308 return __unset_restriction_mode(uid, mode);
311 int _get_restriction_mode(uid_t uid, const char *pkgid, int *mode)
313 if (pkgid && strlen(pkgid))
314 return __get_pkg_restriction_mode(uid, pkgid, mode);
316 return __get_restriction_mode(uid, mode);