aboutsummaryrefslogtreecommitdiffstats
path: root/src/libimcv/plugins/imc_swima/sw_collector/sw_collector_db.c
blob: e3c8b008e4dd58529a6db82e20e39eac855ad427 (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
/*
 * 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, get_sw_id, uint32_t,
	private_sw_collector_db_t *this, char *package, char *version, char *name,
	uint8_t source, bool installed, bool check)
{
	uint32_t sw_id = 0, status;
	enumerator_t *e;

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

	if (sw_id)
	{
		if (status == installed)
		{
			if (!check)
			{
				DBG1(DBG_IMC, "  Warning: sw_id %u is already %s", sw_id,
					 status ? "installed" : "deleted");
			}
			return sw_id;
		}
		if (check)
		{
			DBG1(DBG_IMC, "  Warning: sw_id %u is %s", sw_id,
				 status ? "installed" : "deleted");
		}

		/* Change installation status */
		if (this->db->execute(this->db, NULL,
				"UPDATE sw_identifiers SET installed = ? WHERE id = ?",
				 DB_UINT, installed, DB_UINT, sw_id) != 1)
		{
			DBG1(DBG_IMC, "unable to update sw_id status in database");
			return 0;
		}
	}
	else
	{
		/* Create new software identifier */
		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;
		}

		if (check || !installed)
		{
			add_sw_event(this, 1, sw_id, SWIMA_EVENT_ACTION_CREATION);
		}
	}

	return sw_id;
}

METHOD(sw_collector_db_t, get_sw_id_count, uint32_t,
	private_sw_collector_db_t *this, bool installed_only)
{
	uint32_t count;
	enumerator_t *e;

	if (installed_only)
	{
		e = this->db->query(this->db,
			"SELECT COUNT(installed) FROM sw_identifiers WHERE installed = 1 ",
			 DB_UINT);
	}
	else
	{
		e = this->db->query(this->db,
			"SELECT COUNT(installed) FROM sw_identifiers", 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, create_sw_enumerator, enumerator_t*,
	private_sw_collector_db_t *this, bool installed_only)
{
	enumerator_t *e;

	if (installed_only)
	{
		e = this->db->query(this->db,
				"SELECT name, package, version, installed FROM sw_identifiers "
				"WHERE installed = 1 ORDER BY name ASC",
				 DB_TEXT, DB_TEXT, DB_TEXT, DB_UINT);
	}
	else
	{
		e = this->db->query(this->db,
				"SELECT name, package, version, installed FROM sw_identifiers "
				"ORDER BY name ASC", 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,
			.get_sw_id = _get_sw_id,
			.get_sw_id_count = _get_sw_id_count,
			.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);

		/* 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;
}