aboutsummaryrefslogtreecommitdiffstats
path: root/src/sw-collector/sw_collector_db.c
blob: 6f14818e68506e6bd3da8cb591908c851293abef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (C) 2017 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "sw_collector_db.h"

#include "swima/swima_event.h"

typedef struct private_sw_collector_db_t private_sw_collector_db_t;

/**
 * Private data of an sw_collector_db_t object.
 */
struct private_sw_collector_db_t {

	/**
	 * Public members of sw_collector_db_state_t
	 */
	sw_collector_db_t public;

	/**
	 * Epoch
	 */
	uint32_t epoch;

	/**
	 * Event ID of last event stored in database
	 */
	uint32_t last_eid;

	/**
	 * Software collector database
	 */
	database_t *db;

};

METHOD(sw_collector_db_t, add_event, uint32_t,
	private_sw_collector_db_t *this, char *timestamp)
{
	uint32_t eid = 0;

	if (this->db->execute(this->db, &eid,
			"INSERT INTO events (epoch, timestamp) VALUES (?, ?)",
			 DB_UINT, this->epoch, DB_TEXT, timestamp) != 1)
	{
		DBG1(DBG_IMC, "unable to insert event into database");
		return 0;
	}

	return eid;
}

METHOD(sw_collector_db_t, get_last_event, bool,
	private_sw_collector_db_t *this, uint32_t *eid, uint32_t *epoch,
	char **last_time)
{
	char *timestamp;
	enumerator_t *e;

	e = this->db->query(this->db,
			"SELECT id, epoch, timestamp FROM events ORDER BY timestamp DESC",
			DB_UINT, DB_UINT, DB_TEXT);
	if (!e)
	{
		DBG1(DBG_IMC, "database query for event failed");
		return FALSE;
	}
	if (e->enumerate(e, eid, epoch, &timestamp))
	{
		if (last_time)
		{
			*last_time = strdup(timestamp);
		}
	}
	else
	{
		*eid = 0;
	}
	e->destroy(e);

	return TRUE;
}

METHOD(sw_collector_db_t, add_sw_event, bool,
	private_sw_collector_db_t *this, uint32_t eid, uint32_t sw_id,
	uint8_t action)
{
	if (this->db->execute(this->db, NULL,
			"INSERT INTO sw_events (eid, sw_id, action) VALUES (?, ?, ?)",
			 DB_UINT, eid, DB_UINT, sw_id, DB_UINT, action) != 1)
	{
		DBG1(DBG_IMC, "unable to insert sw_event into database");
		return FALSE;
	}

	return TRUE;
}

METHOD(sw_collector_db_t, set_sw_id, uint32_t,
	private_sw_collector_db_t *this, char *name,  char *package, char *version,
	uint8_t source, bool installed)
{
	uint32_t sw_id;

	if (this->db->execute(this->db, &sw_id,
			"INSERT INTO sw_identifiers "
			"(name, package, version, source, installed) VALUES (?, ?, ?, ?, ?)",
			 DB_TEXT, name, DB_TEXT, package, DB_TEXT, version, DB_UINT, source,
			 DB_UINT, installed) != 1)
	{
		DBG1(DBG_IMC, "unable to insert sw_id into database");
		return 0;
	}

	return sw_id;
}

METHOD(sw_collector_db_t, get_sw_id, uint32_t,
	private_sw_collector_db_t *this, char *name, char **package, char **version,
	uint8_t *source, bool *installed)
{
	char *sw_package, *sw_version;
	uint32_t sw_id = 0, sw_source, sw_installed;
	enumerator_t *e;

	/* Does software identifier already exist in database? */
	e = this->db->query(this->db,
			"SELECT id, package, version, source, installed "
			"FROM sw_identifiers WHERE name = ?",
			DB_TEXT, name, DB_UINT, DB_TEXT, DB_TEXT, DB_UINT, DB_UINT);
	if (!e)
	{
		DBG1(DBG_IMC, "database query for sw_identifier failed");
		return 0;
	}
	if (e->enumerate(e, &sw_id, &sw_package, &sw_version, &sw_source,
						&sw_installed))
	{
		if (package)
		{
			*package = strdup(sw_package);
		}
		if (version)
		{
			*version = strdup(sw_version);
		}
		if (source)
		{
			*source = sw_source;
		}
		if (installed)
		{
			*installed = sw_installed;
		}
	}
	e->destroy(e);

	return sw_id;
}

METHOD(sw_collector_db_t, get_sw_id_count, uint32_t,
	private_sw_collector_db_t *this, sw_collector_db_query_t type)
{
	uint32_t count, installed;
	enumerator_t *e;

	if (type == SW_QUERY_ALL)
	{
		e = this->db->query(this->db,
			"SELECT COUNT(installed) FROM sw_identifiers", DB_UINT);
	}
	else
	{
		installed = (type == SW_QUERY_INSTALLED);
		e = this->db->query(this->db,
			"SELECT COUNT(installed) FROM sw_identifiers WHERE installed = ?",
			 DB_UINT, installed, DB_UINT);
	}

	if (!e)
	{
		DBG1(DBG_IMC, "database query for sw_identifier count failed");
		return 0;
	}
	if (!e->enumerate(e, &count))
	{
		count = 0;
	}
	e->destroy(e);

	return count;
}

METHOD(sw_collector_db_t, update_sw_id, bool,
	private_sw_collector_db_t *this, uint32_t sw_id, char *name, char *version,
	bool installed)
{
	int res;

	if (name && version)
	{
		res = this->db->execute(this->db, NULL,
			"UPDATE sw_identifiers SET name = ?, version = ?, installed = ? "
			"WHERE id = ?", DB_TEXT, name, DB_TEXT, version, DB_UINT, installed,
			 DB_UINT, sw_id);
	}
	else
	{
		res = this->db->execute(this->db, NULL,
			"UPDATE sw_identifiers SET installed = ? WHERE id = ?",
			 DB_UINT, installed, DB_UINT, sw_id);
	}
	if (res != 1)
	{
		DBG1(DBG_IMC, "unable to update software identifier in database");
		return FALSE;
	}
	return TRUE;
}

METHOD(sw_collector_db_t, update_package, int,
	private_sw_collector_db_t *this, char *package_filter, char *package)
{
	int count;

	count = this->db->execute(this->db, NULL,
			"UPDATE sw_identifiers SET package = ? WHERE package LIKE ?",
			 DB_TEXT, package, DB_TEXT, package_filter);
	if (count < 0)
	{
		DBG1(DBG_IMC, "unable to update package name in database");
	}

	return count;
}

METHOD(sw_collector_db_t, create_sw_enumerator, enumerator_t*,
	private_sw_collector_db_t *this, sw_collector_db_query_t type, char *package)
{
	enumerator_t *e;
	u_int installed;

	if (type == SW_QUERY_ALL)
	{
		if (package)
		{
			e = this->db->query(this->db,
				"SELECT id, name, package, version, installed "
				"FROM sw_identifiers WHERE package = ? ORDER BY name ASC",
				 DB_TEXT, package, DB_UINT, DB_TEXT, DB_TEXT, DB_TEXT, DB_UINT);
		}
		else
		{
			e = this->db->query(this->db,
				"SELECT id, name, package, version, installed "
				"FROM sw_identifiers ORDER BY name ASC",
				 DB_UINT, DB_TEXT, DB_TEXT, DB_TEXT, DB_UINT);
		}
	}
	else
	{
		installed = (type == SW_QUERY_INSTALLED);

		if (package)
		{
			e = this->db->query(this->db,
				"SELECT id, name, package, version, installed "
				"FROM sw_identifiers WHERE package = ? AND installed = ? "
				"ORDER BY name ASC", DB_TEXT, package, DB_UINT, installed,
				 DB_UINT, DB_TEXT, DB_TEXT, DB_TEXT, DB_UINT);
		}
		else
		{
			e = this->db->query(this->db,
				"SELECT id, name, package, version, installed "
				"FROM sw_identifiers WHERE installed = ? ORDER BY name ASC",
				 DB_UINT, installed, DB_UINT, DB_TEXT, DB_TEXT, DB_TEXT, DB_UINT);
		}
	}
	if (!e)
	{
		DBG1(DBG_IMC, "database query for sw_identifier count failed");
		return NULL;
	}

	return e;
}

METHOD(sw_collector_db_t, destroy, void,
	private_sw_collector_db_t *this)
{
	this->db->destroy(this->db);
	free(this);
}

/**
 * Described in header.
 */
sw_collector_db_t *sw_collector_db_create(char *uri)
{
	private_sw_collector_db_t *this;
	uint32_t first_eid, last_eid;
	char *first_time;

	INIT(this,
		.public = {
			.add_event = _add_event,
			.get_last_event = _get_last_event,
			.add_sw_event = _add_sw_event,
			.set_sw_id = _set_sw_id,
			.get_sw_id = _get_sw_id,
			.get_sw_id_count = _get_sw_id_count,
			.update_sw_id = _update_sw_id,
			.update_package = _update_package,
			.create_sw_enumerator = _create_sw_enumerator,
			.destroy = _destroy,
		},
		.db = lib->db->create(lib->db, uri),
	);

	if (!this->db)
	{
		DBG1(DBG_IMC, "opening database URI '%s' failed", uri);
		return NULL;
	}

	/* Retrieve last event in database */
	if (!get_last_event(this, &last_eid, &this->epoch, NULL))
	{
		destroy(this);
		return NULL;
	}

	/* Create random epoch and first event if no events exist yet */
	if (!last_eid)
	{
		rng_t *rng;

		rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
		if (!rng ||
			!rng->get_bytes(rng, sizeof(uint32_t), (uint8_t*)&this->epoch))
		{
			DESTROY_IF(rng);
			destroy(this);
			DBG1(DBG_IMC, "generating random epoch value failed");
			return NULL;
		}
		rng->destroy(rng);

		/* strongTNC workaround - limit epoch to 31 bit unsigned integer */
		this->epoch &= 0x7fffffff;

		/* Create first event when the OS was installed */
		first_time = lib->settings->get_str(lib->settings,
						"sw-collector.first_time", "0000-00-00T00:00:00Z");
		first_eid = add_event(this, first_time);
		if (!first_eid)
		{
			destroy(this);
			return NULL;
		}
		DBG0(DBG_IMC, "First-Date: %s, eid = %u, epoch = %u",
					   first_time, first_eid, this->epoch);
	}

	return &this->public;
}