Fix db recovery 96/227196/4
authorJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 10 Mar 2020 09:06:44 +0000 (18:06 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Thu, 12 Mar 2020 10:00:43 +0000 (19:00 +0900)
- Adds service condition.
- Fix db creation tool to initialize certificate db properly.
- Does not check if retrieved uid is not regular user.
- Remove unnecessary code from db recovery tool.
- Add conditions to check user database only.
- Initialize local flag to consider multi-user environment.

Change-Id: I2c817743eb21ed04bd56092ff259e5e361d849cb
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
tool/pkg-db-creator.c
tool/pkg-db-recovery.c
tool/pkg-db-recovery.service

index b5bfb95..59b6345 100644 (file)
@@ -131,7 +131,7 @@ int main(int argc, char *argv[])
 
        }
 
-       if (uid == 0) {
+       if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
                ret = pkgmgr_parser_initialize_cert_db();
                if (ret != 0) {
                        LOGE("failed to create cert db for uid [%d], err[%d]", uid, ret);
index 932fd21..f97b096 100644 (file)
@@ -192,128 +192,6 @@ err:
        return false;
 }
 
-static bool __change_owner(const char *files[2], uid_t uid)
-{
-       int ret;
-       int i;
-       int fd;
-       struct passwd pwd;
-       struct passwd *result;
-       char buf[BUFSIZE];
-       struct stat sb;
-       mode_t mode;
-
-       if (uid == OWNER_ROOT) {
-               ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
-               if (result == NULL) {
-                       if (ret == 0)
-                               LOGE("no such user: %d", uid);
-                       else
-                               LOGE("getpwnam_r failed: %d", errno);
-                       return false;
-               }
-               uid = pwd.pw_uid;
-       }
-
-       ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
-       if (result == NULL) {
-               if (ret == 0)
-                       LOGE("no such user: %d", uid);
-               else
-                       LOGE("getpwuid_r failed: %d", errno);
-               return false;
-       }
-
-       for (i = 0; i < 2; i++) {
-               fd = open(files[i], O_RDONLY);
-               if (fd == -1) {
-                       LOGE("open %s failed: %d", files[i], errno);
-                       return false;
-               }
-               ret = fstat(fd, &sb);
-               if (ret == -1) {
-                       LOGE("stat %s failed: %d", files[i], errno);
-                       close(fd);
-                       return false;
-               }
-               if (S_ISLNK(sb.st_mode)) {
-                       LOGE("%s is symlink!", files[i]);
-                       close(fd);
-                       return false;
-               }
-               ret = fchown(fd, uid, pwd.pw_gid);
-               if (ret == -1) {
-                       LOGE("fchown %s failed: %d", files[i], errno);
-                       close(fd);
-                       return false;
-               }
-
-               mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
-               if (strstr(files[0], PKGMGR_CERT_DB_FILE) != NULL)
-                       mode |= S_IWOTH;
-               ret = fchmod(fd, mode);
-               if (ret == -1) {
-                       LOGE("fchmod %s failed: %d", files[i], errno);
-                       close(fd);
-                       return false;;
-               }
-               close(fd);
-       }
-
-       return true;
-}
-
-static void __change_permission(uid_t uid)
-{
-       const char *files[2];
-       char *parser_dbpath = NULL;
-       char journal_file[PATH_MAX];
-       GList *tmp_list;
-       user_info *tmp_info;
-
-       if (uid < REGULAR_USER) {
-               files[0] = PKGMGR_PARSER_DB_FILE;
-               snprintf(journal_file, sizeof(journal_file),
-                               "%s-journal", PKGMGR_PARSER_DB_FILE);
-               files[1] = journal_file;
-
-               if (!__change_owner(files, uid)) {
-                       LOGE("Failed to change ownership");
-                       return;
-               }
-
-               files[0] = PKGMGR_CERT_DB_FILE;
-               snprintf(journal_file, sizeof(journal_file),
-                               "%s-journal", PKGMGR_CERT_DB_FILE);
-               files[1] = journal_file;
-               if (!__change_owner(files, uid)) {
-                       LOGE("Failed to change ownership");
-                       return;
-               }
-       } else {
-               for (tmp_list = user_info_list; tmp_list != NULL;
-                               tmp_list = g_list_next(tmp_list)) {
-                       tmp_info = (user_info *)tmp_list->data;
-                       if (!tmp_info)
-                               continue;
-
-                       if (tmp_info->uid == uid) {
-                               parser_dbpath = tmp_info->db_path;
-                               break;
-                       }
-               }
-               files[0] = parser_dbpath;
-               snprintf(journal_file, sizeof(journal_file),
-                               "%s-journal", parser_dbpath);
-               files[1] = journal_file;
-
-               if (!__change_owner(files, uid)) {
-                       LOGE("Failed to change ownership");
-                       return;
-               }
-       }
-}
-
 static void _xsystem(const char *argv[])
 {
        int status = 0;
@@ -366,7 +244,6 @@ static void __initdb(uid_t uid)
 
        __create_need_to_recovery_file(uid);
        __create_db(uid);
-       __change_permission(uid);
        snprintf(uid_string, sizeof(uid_string), "%d", (int)uid);
        _xsystem((uid > REGULAR_USER) ? initdb_rw : initdb_ro);
        __remove_need_to_recovery_file(uid);
@@ -381,7 +258,7 @@ static void __initdb_all()
        for (tmp_list = user_info_list;
                        tmp_list != NULL; tmp_list = g_list_next(tmp_list)) {
                tmp_info = (user_info *)tmp_list->data;
-               if (!tmp_info || tmp_info->uid < REGULAR_USER)
+               if (!tmp_info)
                        continue;
                __initdb(tmp_info->uid);
        }
@@ -391,10 +268,11 @@ static void __check_user_db()
 {
        GList *tmp_list = NULL;
        user_info *tmp_info;
-       bool need_recovery = false;
+       bool need_recovery;
 
        for (tmp_list = user_info_list;
                        tmp_list != NULL; tmp_list = g_list_next(tmp_list)) {
+               need_recovery = false;
                tmp_info = (user_info *)tmp_list->data;
                if (!tmp_info)
                        continue;
@@ -456,17 +334,16 @@ static void _get_user_list()
                if (!strcmp(".", ent->d_name) || !strcmp("..", ent->d_name) ||
                                !S_ISDIR(stats.st_mode))
                        continue;
+               uid = (uid_t)atoi(ent->d_name);
+               if (uid < REGULAR_USER)
+                       continue;
+
                info = calloc(1, sizeof(user_info));
                if (!info) {
                        closedir(dir);
                        return;
                }
 
-               uid = (uid_t)atoi(ent->d_name);
-               if (!uid) {
-                       free(info);
-                       continue;
-               }
                info->uid = uid;
                info->db_path = __get_dbpath(uid);
                user_info_list = g_list_append(user_info_list, info);
index fd64c5e..a316f41 100644 (file)
@@ -2,6 +2,7 @@
 Description=Package DB Recovery Service
 DefaultDependencies=no
 Before=ac.service
+After=systemd-tmpfiles-setup.service local-fs.target
 
 [Service]
 Type=oneshot