Fix logic of pkgmgrinfo_save_cert_info 95/78595/1
authorSangyoon Jang <s89.jang@samsung.com>
Tue, 5 Jul 2016 08:58:30 +0000 (17:58 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Wed, 6 Jul 2016 07:18:17 +0000 (16:18 +0900)
pkgmgrinfo_save_cert_info now increasing package count(added)
when calling with same pkgid instead of overriding cert info.
Normally overriding cert info is not allowed behavior, it used to
running TCT or testing by inhouse developer.

Change-Id: I648066ae3cfe89888cfd9dad3e8a6a24886883cd
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
parser/pkgmgr_parser_db.c
src/pkgmgrinfo_certinfo.c
src/pkgmgrinfo_db.c

index 2e8da26..4863257 100644 (file)
@@ -314,6 +314,7 @@ sqlite3 *pkgmgr_cert_db;
 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
                                                "CREATE TABLE IF NOT EXISTS package_cert_info( " \
                                                " package TEXT PRIMARY KEY, " \
+                                               " package_count INTEGER, " \
                                                " author_root_cert INTEGER, " \
                                                " author_im_cert INTEGER, " \
                                                " author_signer_cert INTEGER, " \
@@ -324,6 +325,32 @@ sqlite3 *pkgmgr_cert_db;
                                                " dist2_im_cert INTEGER, " \
                                                " dist2_signer_cert INTEGER)"
 
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO \
+                                               "CREATE TRIGGER IF NOT EXISTS update_cert_info " \
+                                               "AFTER UPDATE ON package_cert_info " \
+                                               "WHEN (NEW.package_count = 0) " \
+                                               "BEGIN" \
+                                               " DELETE FROM package_cert_info WHERE package=OLD.package;" \
+                                               "END;"
+
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \
+                                               "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \
+                                               "AFTER UPDATE ON package_cert_info " \
+                                               "WHEN (NEW.package_count = OLD.package_count + 1) " \
+                                               "BEGIN" \
+                                               " UPDATE package_cert_index_info SET" \
+                                               "  cert_ref_count = cert_ref_count - 1" \
+                                               " WHERE cert_id = OLD.author_root_cert" \
+                                               "  OR cert_id = OLD.author_im_cert" \
+                                               "  OR cert_id = OLD.author_signer_cert" \
+                                               "  OR cert_id = OLD.dist_root_cert" \
+                                               "  OR cert_id = OLD.dist_im_cert" \
+                                               "  OR cert_id = OLD.dist_signer_cert" \
+                                               "  OR cert_id = OLD.dist2_root_cert" \
+                                               "  OR cert_id = OLD.dist2_im_cert" \
+                                               "  OR cert_id = OLD.dist2_signer_cert;" \
+                                               "END;"
+
 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
                                                "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
                                                "AFTER DELETE ON package_cert_info " \
@@ -350,16 +377,6 @@ sqlite3 *pkgmgr_cert_db;
                                                " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
                                                "END;"
 
-#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \
-                                               "CREATE TRIGGER IF NOT EXISTS update_%s_info " \
-                                               "AFTER UPDATE ON package_cert_info " \
-                                               "WHEN (OLD.%s IS NOT NULL) " \
-                                               "BEGIN" \
-                                               " UPDATE package_cert_index_info SET" \
-                                               "  cert_ref_count = cert_ref_count - 1" \
-                                               " WHERE cert_id = OLD.%s;" \
-                                               "END;"
-
 static int __insert_application_info(manifest_x *mfx);
 static int __insert_application_appcategory_info(manifest_x *mfx);
 static int __insert_application_appcontrol_info(manifest_x *mfx);
@@ -2381,13 +2398,6 @@ static int __enable_app_splash_screen(const char *appid)
 API int pkgmgr_parser_initialize_db(uid_t uid)
 {
        int ret = -1;
-       int i;
-       char query[MAX_QUERY_LEN];
-       static const char *columns[] = {
-               "author_root_cert", "author_im_cert", "author_signer_cert",
-               "dist_root_cert", "dist_im_cert", "dist_signer_cert",
-               "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert",
-               NULL};
 
        /*Manifest DB*/
        ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
@@ -2492,6 +2502,16 @@ API int pkgmgr_parser_initialize_db(uid_t uid)
                _LOGD("package cert index info DB initialization failed\n");
                return ret;
        }
+       ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO);
+       if (ret == -1) {
+               _LOGD("package cert info DB initialization failed\n");
+               return ret;
+       }
+       ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2);
+       if (ret == -1) {
+               _LOGD("package cert info DB initialization failed\n");
+               return ret;
+       }
        ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO);
        if (ret == -1) {
                _LOGD("package cert info DB initialization failed\n");
@@ -2502,16 +2522,6 @@ API int pkgmgr_parser_initialize_db(uid_t uid)
                _LOGD("package cert index info DB initialization failed\n");
                return ret;
        }
-       for (i = 0; columns[i] != NULL; i++) {
-               snprintf(query, sizeof(query),
-                               QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT,
-                               columns[i], columns[i], columns[i]);
-               ret = __initialize_db(pkgmgr_cert_db, query);
-               if (ret == -1) {
-                       _LOGD("package cert index info DB initialization failed\n");
-                       return ret;
-               }
-       }
 
        if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(GLOBAL_USER), GLOBAL_USER)) {
                _LOGD("Failed to change cert db permission\n");
index 3688488..0bcf967 100644 (file)
@@ -464,11 +464,11 @@ static int _pkginfo_save_cert_info(sqlite3 *db, const char *pkgid,
                char *cert_info[])
 {
        static const char query_insert[] =
-               "INSERT INTO package_cert_info (package,"
+               "INSERT INTO package_cert_info (package, package_count,"
                " author_root_cert, author_im_cert, author_signer_cert,"
                " dist_root_cert, dist_im_cert, dist_signer_cert,"
                " dist2_root_cert, dist2_im_cert, dist2_signer_cert) "
-               "VALUES(?, "
+               "VALUES(?, 1,"
                " (SELECT cert_id FROM package_cert_index_info"
                "  WHERE cert_info=?),"
                " (SELECT cert_id FROM package_cert_index_info"
@@ -488,34 +488,8 @@ static int _pkginfo_save_cert_info(sqlite3 *db, const char *pkgid,
                " (SELECT cert_id FROM package_cert_index_info"
                "  WHERE cert_info=?))";
        static const char query_update[] =
-               "UPDATE package_cert_info SET "
-               " author_root_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               " author_im_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               " author_signer_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               " dist_root_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               " dist_im_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               " dist_signer_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               " dist2_root_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               "dist2_im_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?),"
-               "dist2_signer_cert= "
-               "  (SELECT cert_id FROM package_cert_index_info"
-               "   WHERE cert_info=?) "
+               "UPDATE package_cert_info "
+               "SET package_count = package_count + 1 "
                "WHERE package=?";
        int ret;
        sqlite3_stmt *stmt;
@@ -549,16 +523,7 @@ static int _pkginfo_save_cert_info(sqlite3 *db, const char *pkgid,
                        _LOGE("prepare error: %s", sqlite3_errmsg(db));
                        return PMINFO_R_ERROR;
                }
-               idx = 1;
-               for (i = 0; i < MAX_CERT_TYPE; i++) {
-                       if (sqlite3_bind_text(stmt, idx++, cert_info[i], -1,
-                                       SQLITE_STATIC)) {
-                               _LOGE("bind error: %s", sqlite3_errmsg(db));
-                               sqlite3_finalize(stmt);
-                               return PMINFO_R_ERROR;
-                       }
-               }
-               sqlite3_bind_text(stmt, idx++, pkgid, -1, SQLITE_STATIC);
+               sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_STATIC);
                ret = sqlite3_step(stmt);
                sqlite3_finalize(stmt);
        }
@@ -656,6 +621,7 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha
                sqlite3_close_v2(db);
                return PMINFO_R_ERROR;
        }
+
        if (_pkginfo_save_cert_info(db, pkgid, info->cert_info)) {
                _LOGE("failed to save cert info, rollback now");
                sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
@@ -700,7 +666,9 @@ API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
 static int _pkginfo_delete_certinfo(sqlite3 *db, const char *pkgid)
 {
        static const char query[] =
-               "DELETE FROM package_cert_info WHERE package=?";
+               "UPDATE package_cert_info "
+               "SET package_count = package_count - 1 "
+               "WHERE package=?";
        int ret;
        sqlite3_stmt *stmt;
 
index 3aca7de..ff23b29 100644 (file)
@@ -30,6 +30,7 @@
 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
        "CREATE TABLE IF NOT EXISTS package_cert_info( " \
        " package TEXT PRIMARY KEY, " \
+       " package_count INTEGER, " \
        " author_root_cert INTEGER, " \
        " author_im_cert INTEGER, " \
        " author_signer_cert INTEGER, " \
        " dist2_im_cert INTEGER, " \
        " dist2_signer_cert INTEGER)"
 
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO \
+       "CREATE TRIGGER IF NOT EXISTS update_cert_info " \
+       "AFTER UPDATE ON package_cert_info " \
+       "WHEN (NEW.package_count = 0) " \
+       "BEGIN" \
+       " DELETE FROM package_cert_info WHERE package=OLD.package;" \
+       "END;"
+
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \
+       "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \
+       "AFTER UPDATE ON package_cert_info " \
+       "WHEN (NEW.package_count = OLD.package_count + 1) " \
+       "BEGIN" \
+       " UPDATE package_cert_index_info SET" \
+       "  cert_ref_count = cert_ref_count - 1" \
+       " WHERE cert_id = OLD.author_root_cert" \
+       "  OR cert_id = OLD.author_im_cert" \
+       "  OR cert_id = OLD.author_signer_cert" \
+       "  OR cert_id = OLD.dist_root_cert" \
+       "  OR cert_id = OLD.dist_im_cert" \
+       "  OR cert_id = OLD.dist_signer_cert" \
+       "  OR cert_id = OLD.dist2_root_cert" \
+       "  OR cert_id = OLD.dist2_im_cert" \
+       "  OR cert_id = OLD.dist2_signer_cert;" \
+       "END;"
+
 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
        "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
        "AFTER DELETE ON package_cert_info " \
        " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
        "END;"
 
-#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \
-       "CREATE TRIGGER IF NOT EXISTS update_%s_info " \
-       "AFTER UPDATE ON package_cert_info " \
-       "WHEN (OLD.%s IS NOT NULL) " \
-       "BEGIN" \
-       " UPDATE package_cert_index_info SET" \
-       "  cert_ref_count = cert_ref_count - 1" \
-       " WHERE cert_id = OLD.%s;" \
-       "END;"
-
 __thread db_handle manifest_db;
 __thread db_handle cert_db;
 
@@ -207,13 +224,6 @@ static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callb
 
 int _check_create_cert_db(sqlite3 *certdb)
 {
-       int i;
-       char buf[BUFSIZE];
-       static const char *columns[] = {
-               "author_root_cert", "author_im_cert", "author_signer_cert",
-               "dist_root_cert", "dist_im_cert", "dist_signer_cert",
-               "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert",
-               NULL};
        int ret = 0;
        ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
        if (ret < 0)
@@ -221,19 +231,16 @@ int _check_create_cert_db(sqlite3 *certdb)
        ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
        if (ret < 0)
                return ret;
+       ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL);
+       if (ret < 0)
+               return ret;
+       ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL);
+       if (ret < 0)
+               return ret;
        ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
        if (ret < 0)
                return ret;
        ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL);
-
-       for (i = 0; columns[i] != NULL; i++) {
-               snprintf(buf, sizeof(buf),
-                               QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT,
-                               columns[i], columns[i], columns[i]);
-               ret = __exec_db_query(certdb, buf, NULL, NULL);
-               if (ret < 0)
-                       return ret;
-       }
        return ret;
 }
 static gid_t _get_gid(const char *name)