]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
add product and file entries to database
authorAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 31 Oct 2011 17:34:16 +0000 (18:34 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 28 Nov 2011 13:39:53 +0000 (14:39 +0100)
src/libimcv/plugins/imv_attestation/attest.c
src/libimcv/plugins/imv_attestation/attest_db.c
src/libimcv/plugins/imv_attestation/attest_db.h

index 6b45dafe51a92bb131a59beb66efe10170b9ccc9..51e6ef7cd2e0927a72788b88393ed429b53ef3fe 100644 (file)
@@ -48,7 +48,9 @@ static void do_args(int argc, char *argv[])
                OP_FILES,
                OP_PRODUCTS,
                OP_HASHES,
-       } operation = OP_UNDEF;
+               OP_ADD,
+               OP_DEL,
+       } op = OP_UNDEF;
 
        /* reinit getopt state */
        optind = 0;
@@ -60,9 +62,12 @@ static void do_args(int argc, char *argv[])
                struct option long_opts[] = {
                        { "help", no_argument, NULL, 'h' },
                        { "files", no_argument, NULL, 'f' },
+                       { "add", no_argument, NULL, 'a' },
+                       { "del", no_argument, NULL, 'd' },
                        { "products", no_argument, NULL, 'p' },
                        { "hashes", no_argument, NULL, 'H' },
                        { "directory", required_argument, NULL, 'D' },
+                       { "dir", required_argument, NULL, 'D' },
                        { "file", required_argument, NULL, 'F' },
                        { "product", required_argument, NULL, 'P' },
                        { "sha1", no_argument, NULL, '1' },
@@ -80,31 +85,37 @@ static void do_args(int argc, char *argv[])
                        case EOF:
                                break;
                        case 'h':
-                               operation = OP_USAGE;
+                               op = OP_USAGE;
                                break;
                        case 'f':
-                               operation = OP_FILES;
+                               op = OP_FILES;
                                continue;
                        case 'p':
-                               operation = OP_PRODUCTS;
+                               op = OP_PRODUCTS;
                                continue;
                        case 'H':
-                               operation = OP_HASHES;
+                               op = OP_HASHES;
+                               continue;
+                       case 'a':
+                               op = OP_ADD;
+                               continue;
+                       case 'd':
+                               op = OP_DEL;
                                continue;
                        case 'D':
-                               if (!attest->set_directory(attest, optarg))
+                               if (!attest->set_directory(attest, optarg, op == OP_ADD))
                                {
                                        exit(EXIT_FAILURE);
                                }
                                continue;
                        case 'F':
-                               if (!attest->set_file(attest, optarg))
+                               if (!attest->set_file(attest, optarg, op == OP_ADD))
                                {
                                        exit(EXIT_FAILURE);
                                }
                                continue;
                        case 'P':
-                               if (!attest->set_product(attest, optarg))
+                               if (!attest->set_product(attest, optarg, op == OP_ADD))
                                {
                                        exit(EXIT_FAILURE);
                                }
@@ -140,7 +151,7 @@ static void do_args(int argc, char *argv[])
                break;
        }
 
-       switch (operation)
+       switch (op)
        {
                case OP_USAGE:
                        usage();
@@ -154,6 +165,12 @@ static void do_args(int argc, char *argv[])
                case OP_HASHES:
                        attest->list_hashes(attest);
                        break;
+               case OP_ADD:
+                       attest->add(attest);
+                       break;
+               case OP_DEL:
+                       attest->delete(attest);
+                       break;
                default:
                        usage();
                        exit(EXIT_FAILURE);
index 65295024163d04c0fb000d95f09d0893f5ef2c2a..369ab2ea1c8bf169dbffbc530e893f1d552febc0 100644 (file)
@@ -85,7 +85,7 @@ struct private_attest_db_t {
 };
 
 METHOD(attest_db_t, set_product, bool,
-       private_attest_db_t *this, char *product)
+       private_attest_db_t *this, char *product, bool create)
 {
        enumerator_t *e;
 
@@ -104,12 +104,26 @@ METHOD(attest_db_t, set_product, bool,
                {
                        this->product_set = TRUE;
                }
-               else
-               {
-                       printf("product '%s' not found in database\n", product);
-               }
                e->destroy(e);
        }
+       if (this->product_set)
+       {
+               return TRUE;
+       }
+
+       if (!create)
+       {
+               printf("product '%s' not found in database\n", product);
+       }
+
+       /* Add a new database entry */
+       this->product_set = this->db->execute(this->db, &this->pid,
+                                                                       "INSERT INTO products (name) VALUES (?)",
+                                                                       DB_TEXT, product);
+
+       printf("product '%s' %sinserted into database\n", product,
+                  this->product_set ? "" : "could not be ");
+
        return this->product_set;
 }
 
@@ -145,7 +159,7 @@ METHOD(attest_db_t, set_pid, bool,
 }
 
 METHOD(attest_db_t, set_file, bool,
-       private_attest_db_t *this, char *file)
+       private_attest_db_t *this, char *file, bool create)
 {
        enumerator_t *e;
 
@@ -164,12 +178,26 @@ METHOD(attest_db_t, set_file, bool,
                {
                        this->file_set = TRUE;
                }
-               else
-               {
-                       printf("file '%s' not found in database\n", file);
-               }
                e->destroy(e);
        }
+       if (this->file_set)
+       {
+               return TRUE;
+       }
+
+       if (!create)
+       {
+               printf("file '%s' not found in database\n", file);
+       }
+
+       /* Add a new database entry */
+       this->file_set = this->db->execute(this->db, &this->fid,
+                                                               "INSERT INTO files (type, path) VALUES (0, ?)",
+                                                               DB_TEXT, file);
+
+       printf("file '%s' %sinserted into database\n", file,
+                  this->file_set ? "" : "could not be ");
+
        return this->file_set;
 }
 
@@ -205,7 +233,7 @@ METHOD(attest_db_t, set_fid, bool,
 }
 
 METHOD(attest_db_t, set_directory, bool,
-       private_attest_db_t *this, char *dir)
+       private_attest_db_t *this, char *dir, bool create)
 {
        enumerator_t *e;
 
@@ -217,7 +245,8 @@ METHOD(attest_db_t, set_directory, bool,
        free(this->dir);
        this->dir = strdup(dir);
 
-       e = this->db->query(this->db, "SELECT id FROM files WHERE path = ?",
+       e = this->db->query(this->db,
+                                               "SELECT id FROM files WHERE type = 1 AND path = ?",
                                                DB_TEXT, dir, DB_INT);
        if (e)
        {
@@ -225,12 +254,26 @@ METHOD(attest_db_t, set_directory, bool,
                {
                        this->dir_set = TRUE;
                }
-               else
-               {
-                       printf("directory '%s' not found in database\n", dir);
-               }
                e->destroy(e);
        }
+       if (this->dir_set)
+       {
+               return TRUE;
+       }
+
+       if (!create)
+       {
+               printf("directory '%s' not found in database\n", dir);
+       }
+
+       /* Add a new database entry */
+       this->dir_set = this->db->execute(this->db, &this->did,
+                                                               "INSERT INTO files (type, path) VALUES (1, ?)",
+                                                               DB_TEXT, dir);
+
+       printf("directory '%s' %sinserted into database\n", dir,
+                  this->dir_set ? "" : "could not be ");
+
        return this->dir_set;
 }
 
@@ -519,6 +562,18 @@ METHOD(attest_db_t, list_hashes, void,
        free(dir);
 }
 
+METHOD(attest_db_t, add, bool,
+       private_attest_db_t *this)
+{
+       return FALSE;
+}
+
+METHOD(attest_db_t, delete, bool,
+       private_attest_db_t *this)
+{
+       return FALSE;
+}
+
 METHOD(attest_db_t, destroy, void,
        private_attest_db_t *this)
 {
@@ -548,6 +603,8 @@ attest_db_t *attest_db_create(char *uri)
                        .list_products = _list_products,
                        .list_files = _list_files,
                        .list_hashes = _list_hashes,
+                       .add = _add,
+                       .delete = _delete,
                        .destroy = _destroy,
                },
                .dir = strdup(""),
index 9c6ba1ab2807e47bef560bf5329e7d685ad8dc12..990297eb20bd694528fa1dc22fa6e25a58c3f638 100644 (file)
@@ -37,9 +37,10 @@ struct attest_db_t {
         * Set software product to be queried
         *
         * @param product               software product
+        * @param create                if TRUE create database entry if it doesn't exist
         * @return                              TRUE if successful
         */
-       bool (*set_product)(attest_db_t *this, char *product);
+       bool (*set_product)(attest_db_t *this, char *product, bool create);
 
        /**
         * Set primary key of the software product to be queried
@@ -53,9 +54,10 @@ struct attest_db_t {
         * Set measurement file to be queried
         *
         * @param file                  measurement file
+        * @param create                if TRUE create database entry if it doesn't exist
         * @return                              TRUE if successful
         */
-       bool (*set_file)(attest_db_t *this, char *file);
+       bool (*set_file)(attest_db_t *this, char *file, bool create);
 
        /**
         * Set primary key of the measurement file to be queried
@@ -69,9 +71,10 @@ struct attest_db_t {
         * Set directory of the measurement file to be queried
         *
         * @param directory             directory containing the measurement file
+        * @param create                if TRUE create database entry if it doesn't exist
         * @return                              TRUE if successful
         */
-       bool (*set_directory)(attest_db_t *this, char *dir);
+       bool (*set_directory)(attest_db_t *this, char *dir, bool create);
 
        /**
         * Set primary key of the directory to be queried
@@ -103,6 +106,16 @@ struct attest_db_t {
         */
        void (*list_hashes)(attest_db_t *this);
 
+       /**
+        * Add an entry to the database
+        */
+       bool (*add)(attest_db_t *this);
+
+       /**
+        * Delete an entry from the database
+        */
+       bool (*delete)(attest_db_t *this);
+
        /**
         * Destroy attest_db_t object
         */