OP_FILES,
OP_PRODUCTS,
OP_HASHES,
- } operation = OP_UNDEF;
+ OP_ADD,
+ OP_DEL,
+ } op = OP_UNDEF;
/* reinit getopt state */
optind = 0;
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' },
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);
}
break;
}
- switch (operation)
+ switch (op)
{
case OP_USAGE:
usage();
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);
};
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;
{
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;
}
}
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;
{
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;
}
}
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;
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)
{
{
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;
}
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)
{
.list_products = _list_products,
.list_files = _list_files,
.list_hashes = _list_hashes,
+ .add = _add,
+ .delete = _delete,
.destroy = _destroy,
},
.dir = strdup(""),
* 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
* 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
* 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
*/
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
*/