From f31d568517f93f9a3747f06374ff793e65837b5f Mon Sep 17 00:00:00 2001 From: Sangyoon Jang Date: Thu, 16 Jul 2015 11:00:34 +0900 Subject: [PATCH] Add create and initialize db api add api: create and initialize db set smack label "System::Shared" to db file Change-Id: I0f187a0f074d8ac430ca804bc2c121bbe48509c0 Signed-off-by: Sangyoon Jang --- parser/pkgmgr_parser_db.c | 91 ++++++++++++++++++++++++++++---------- parser/pkgmgr_parser_db.h | 1 + parser/pkgmgr_parser_internal.h | 2 +- parser/pkgmgr_parser_resource_db.c | 2 +- src/pkgmgr-info.c | 2 +- 5 files changed, 71 insertions(+), 27 deletions(-) diff --git a/parser/pkgmgr_parser_db.c b/parser/pkgmgr_parser_db.c index d1f2d7c..8632c8d 100644 --- a/parser/pkgmgr_parser_db.c +++ b/parser/pkgmgr_parser_db.c @@ -52,9 +52,13 @@ #define BUFSIZE 4096 #define OWNER_ROOT 0 -#define SET_SMACK_LABEL(x,uid) \ - if(smack_setlabel((x), (((uid) == GLOBAL_USER)?"*":"User"), SMACK_LABEL_ACCESS)) _LOGE("failed chsmack -a \"User/*\" %s", x); \ - else _LOGD("chsmack -a \"User/*\" %s", x); +#define SET_SMACK_LABEL(x) \ +do { \ + if (smack_setlabel((x), "System::Shared", SMACK_LABEL_ACCESS)) \ + _LOGE("failed chsmack -a \"System::Shared\" %s", x); \ + else \ + _LOGD("chsmack -a \"System::Shared\" %s", x); \ +} while (0) sqlite3 *pkgmgr_parser_db; sqlite3 *pkgmgr_cert_db; @@ -301,6 +305,7 @@ static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, static void __preserve_guestmode_visibility_value(manifest_x *mfx); static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname); static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path); +static int __parserdb_change_perm(const char *db_file, uid_t uid); static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname) { @@ -2050,7 +2055,7 @@ static int __update_preload_condition_in_db() return ret; } -API int pkgmgr_parser_initialize_db(void) +API int pkgmgr_parser_initialize_db(uid_t uid) { int ret = -1; /*Manifest DB*/ @@ -2152,10 +2157,17 @@ API int pkgmgr_parser_initialize_db(void) return ret; } + if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) { + _LOGD("Failed to change cert db permission\n"); + } + if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) { + _LOGD("Failed to change parser db permission\n"); + } + return 0; } -static int parserdb_change_perm(const char *db_file, uid_t uid) +static int __parserdb_change_perm(const char *db_file, uid_t uid) { char buf[BUFSIZE]; char journal_file[BUFSIZE]; @@ -2166,14 +2178,16 @@ static int parserdb_change_perm(const char *db_file, uid_t uid) files[1] = journal_file; files[2] = NULL; - if(db_file == NULL) + if (db_file == NULL) return -1; - if(getuid() != OWNER_ROOT) //At this time we should be root to apply this - return 0; + if (getuid() != OWNER_ROOT) //At this time we should be root to apply this + return 0; snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal"); - userinfo = getpwuid(uid); - if (!userinfo) { + if (uid == OWNER_ROOT) + uid = GLOBAL_USER; + userinfo = getpwuid(uid); + if (!userinfo) { _LOGE("FAIL: user %d doesn't exist", uid); return -1; } @@ -2181,23 +2195,58 @@ static int parserdb_change_perm(const char *db_file, uid_t uid) for (i = 0; files[i]; i++) { ret = chown(files[i], uid, userinfo->pw_gid); - SET_SMACK_LABEL(files[i],uid) if (ret == -1) { - strerror_r(errno, buf, sizeof(buf)); - _LOGD("FAIL : chown %s %d.%d, because %s", db_file, uid, userinfo->pw_gid, buf); + if (strerror_r(errno, buf, sizeof(buf))) + strcpy(buf, ""); + _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid, + userinfo->pw_gid, buf); return -1; } ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); if (ret == -1) { - strerror_r(errno, buf, sizeof(buf)); - _LOGD("FAIL : chmod %s 0664, because %s", db_file, buf); + if (strerror_r(errno, buf, sizeof(buf))) + strcpy(buf, ""); + _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf); return -1; } + SET_SMACK_LABEL(files[i]); } return 0; } +API int pkgmgr_parser_create_and_initialize_db(uid_t uid) +{ + int ret; + + if (getuid() != OWNER_ROOT) { + _LOGE("Only root user is allowed"); + return -1; + } + + if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) { + _LOGE("Manifest db for user %d is already exists", uid); + return -1; + } + + if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) { + _LOGE("Cert db for user %d is already exists", uid); + return -1; + } + + ret = pkgmgr_parser_check_and_create_db(uid); + if (ret < 0) + return -1; + ret = pkgmgr_parser_initialize_db(uid); + if (ret < 0) { + pkgmgr_parser_close_db(); + return -1; + } + pkgmgr_parser_close_db(); + + return 0; +} + API int pkgmgr_parser_check_and_create_db(uid_t uid) { int ret = -1; @@ -2214,12 +2263,6 @@ API int pkgmgr_parser_check_and_create_db(uid_t uid) _LOGD("Cert DB creation Failed\n"); return -1; } - if( 0 != parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) { - _LOGD("Failed to change cert db permission\n"); - } - if( 0 != parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) { - _LOGD("Failed to change parser db permission\n"); - } return 0; } @@ -2243,7 +2286,7 @@ API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx) _LOGD("Failed to open DB\n"); return ret; } - ret = pkgmgr_parser_initialize_db(); + ret = pkgmgr_parser_initialize_db(GLOBAL_USER); if (ret == -1) goto err; /*Begin transaction*/ @@ -2287,7 +2330,7 @@ API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) _LOGD("Failed to open DB\n"); return ret; } - ret = pkgmgr_parser_initialize_db(); + ret = pkgmgr_parser_initialize_db(uid); if (ret == -1) goto err; /*Begin transaction*/ @@ -2330,7 +2373,7 @@ API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) _LOGD("Failed to open DB\n"); return ret; } - ret = pkgmgr_parser_initialize_db(); + ret = pkgmgr_parser_initialize_db(uid); if (ret == -1) goto err; /*Preserve guest mode visibility*/ diff --git a/parser/pkgmgr_parser_db.h b/parser/pkgmgr_parser_db.h index c6a333b..6a9496f 100644 --- a/parser/pkgmgr_parser_db.h +++ b/parser/pkgmgr_parser_db.h @@ -136,6 +136,7 @@ int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid); int pkgmgr_parser_update_preload_info_in_db(); int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid); +int pkgmgr_parser_create_and_initialize_db(uid_t uid); /** @} */ diff --git a/parser/pkgmgr_parser_internal.h b/parser/pkgmgr_parser_internal.h index ee69d39..9e8021e 100644 --- a/parser/pkgmgr_parser_internal.h +++ b/parser/pkgmgr_parser_internal.h @@ -32,6 +32,6 @@ #endif int pkgmgr_parser_check_and_create_db(uid_t uid); -int pkgmgr_parser_initialize_db(void); +int pkgmgr_parser_initialize_db(uid_t uid); #endif /* __PKGMGR_PARSER_INTERNAL_H__ */ diff --git a/parser/pkgmgr_parser_resource_db.c b/parser/pkgmgr_parser_resource_db.c index 1c0dec3..8db3c27 100755 --- a/parser/pkgmgr_parser_resource_db.c +++ b/parser/pkgmgr_parser_resource_db.c @@ -243,7 +243,7 @@ int pkgmgr_parser_resource_db_save(const char *package, resource_data_t *data) ret = pkgmgr_parser_check_and_create_db(getuid()); if (ret == 0) - ret = pkgmgr_parser_initialize_db(); + ret = pkgmgr_parser_initialize_db(getuid()); if (ret < 0) { _LOGE("db initialization failed"); goto catch; diff --git a/src/pkgmgr-info.c b/src/pkgmgr-info.c index 3a7c264..38ef3b8 100644 --- a/src/pkgmgr-info.c +++ b/src/pkgmgr-info.c @@ -807,7 +807,7 @@ static int __open_manifest_db(uid_t uid) return -1; } - if (pkgmgr_parser_initialize_db()) { + if (pkgmgr_parser_initialize_db(uid)) { _LOGE("initialize db failed"); return -1; } -- 2.7.4