Verification of directory contents measurements implemented
[strongswan.git] / src / libimcv / tcg / pts / pts_database.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pts_database.h"
17
18 #include <debug.h>
19 #include <crypto/hashers/hasher.h>
20
21
22 typedef struct private_pts_database_t private_pts_database_t;
23
24 /**
25 * Private data of a pts_database_t object.
26 *
27 */
28 struct private_pts_database_t {
29
30 /**
31 * Public pts_database_t interface.
32 */
33 pts_database_t public;
34
35 /**
36 * database instance
37 */
38 database_t *db;
39
40 };
41
42 METHOD(pts_database_t, create_file_enumerator, enumerator_t*,
43 private_pts_database_t *this, char *product)
44 {
45 enumerator_t *e;
46
47 /* look for all entries belonging to a product in the files table */
48 e = this->db->query(this->db,
49 "SELECT f.id, f.type, f.path FROM files AS f "
50 "JOIN product_file AS pf ON f.id = pf.file "
51 "JOIN products AS p ON p.id = pf.product "
52 "WHERE p.name = ?",
53 DB_TEXT, product, DB_INT, DB_INT, DB_TEXT);
54 return e;
55 }
56
57 METHOD(pts_database_t, is_directory, bool,
58 private_pts_database_t *this, int id, bool *is_directory)
59 {
60 enumerator_t *e;
61 int is_dir;
62
63 /* look for a entry in files table with matching id */
64 e = this->db->query(this->db,
65 "SELECT f.type FROM files AS f "
66 "WHERE f.id = ?",
67 DB_INT, id, DB_INT);
68
69 if (!e)
70 {
71 DBG1(DBG_TNC, "database enumerator failed", id);
72 return FALSE;
73 }
74 if (!e->enumerate(e, &is_dir))
75 {
76 e->destroy(e);
77 DBG1(DBG_TNC, "file entry with given id:%d not found", id);
78 return FALSE;
79 }
80
81 *is_directory = (is_dir == 1) ? TRUE : FALSE;
82 return TRUE;
83 }
84
85
86 METHOD(pts_database_t, create_files_in_dir_enumerator, enumerator_t*,
87 private_pts_database_t *this, int id)
88 {
89 enumerator_t *e;
90
91 /* look for all entries in file_hashes belonging to a same directory*/
92 e = this->db->query(this->db,
93 "SELECT DISTINCT f.path FROM files AS f "
94 "JOIN file_hashes AS fh ON f.id = fh.file "
95 "WHERE fh.directory = ?",
96 DB_INT, id, DB_TEXT);
97 return e;
98 }
99
100 METHOD(pts_database_t, create_file_meas_enumerator, enumerator_t*,
101 private_pts_database_t *this, char *product, int id, pts_meas_algorithms_t algorithm)
102 {
103 enumerator_t *e;
104
105 /* look for all entries belonging to a product and file in file_hashes table */
106 e = this->db->query(this->db,
107 "SELECT fh.hash FROM file_hashes AS fh "
108 "JOIN files AS f ON fh.file = f.id "
109 "JOIN products AS p ON fh.product = p.id "
110 "WHERE p.name = ? AND f.id = ? AND fh.algo = ?",
111 DB_TEXT, product, DB_INT, id, DB_INT, algorithm, DB_BLOB);
112 return e;
113 }
114
115 METHOD(pts_database_t, create_dir_meas_enumerator, enumerator_t*,
116 private_pts_database_t *this, char *product, int id, char *file_name, pts_meas_algorithms_t algorithm)
117 {
118 enumerator_t *e;
119
120 /* look for all entries belonging to a product, file and directory in file_hashes table */
121 e = this->db->query(this->db,
122 "SELECT fh.hash FROM file_hashes AS fh "
123 "JOIN files AS f ON fh.file = f.id "
124 "JOIN products AS p ON fh.product = p.id "
125 "WHERE f.path = ? AND p.name = ? AND fh.directory = ? AND fh.algo = ?",
126 DB_TEXT, file_name, DB_TEXT, product, DB_INT, id, DB_INT, algorithm, DB_BLOB);
127 return e;
128 }
129
130 METHOD(pts_database_t, destroy, void,
131 private_pts_database_t *this)
132 {
133 this->db->destroy(this->db);
134 free(this);
135 }
136
137 /**
138 * See header
139 */
140 pts_database_t *pts_database_create(char *uri)
141 {
142 private_pts_database_t *this;
143
144 INIT(this,
145 .public = {
146 .create_file_enumerator = _create_file_enumerator,
147 .is_directory = _is_directory,
148 .create_files_in_dir_enumerator = _create_files_in_dir_enumerator,
149 .create_file_meas_enumerator = _create_file_meas_enumerator,
150 .create_dir_meas_enumerator = _create_dir_meas_enumerator,
151 .destroy = _destroy,
152 },
153 .db = lib->db->create(lib->db, uri),
154 );
155
156 if (!this->db)
157 {
158 DBG1(DBG_TNC, "failed to connect to PTS file measurement database '%s'",
159 uri);
160 free(this);
161 return NULL;
162 }
163
164 return &this->public;
165 }
166