aboutsummaryrefslogtreecommitdiffstats
path: root/ll-database-binding/src/ll-database-binding.c
blob: 5b63fa912ad3d758f62830f9d6691ae4d7de0f9d (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright 2017 IoT.bzh
 *
 * author: Loïc Collignon <loic.collignon@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <linux/limits.h>
#include <json-c/json.h>
#include <db.h>

#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>

#include "utils.h"

#ifndef MAX_PATH
#define MAX_PATH 1024
#endif

#define DBFILE		"/ll-database-binding.db"
#define USERNAME	"agl"
#define APPNAME		"firefox"

// ----- Globals -----
static DB*		database;
static char	database_file[MAX_PATH];

// ----- Binding's declarations -----
static int ll_database_binding_init();
static void verb_insert(struct afb_req req);
static void verb_update(struct afb_req req);
static void verb_delete(struct afb_req req);
static void verb_read(struct afb_req req);

// ----- Binding's implementations -----

/**
 * @brief Initialize the binding.
 * @return Exit code, zero if success.
 */
static int ll_database_binding_init()
{
	struct passwd pwd;
	struct passwd* result;
	char buf[MAX_PATH];
	size_t bufsize;
	int ret;

	bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
	if (bufsize == -1 || bufsize > MAX_PATH) bufsize = MAX_PATH;

	ret = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
	if (result == NULL)
	{
		if (ret == 0) AFB_ERROR("User not found");
		else AFB_ERROR("getpwuid_r failed with %d code", ret);
		return -1;
	}

	memset(database_file, 0, MAX_PATH);
	strcat(database_file, result->pw_dir);
	strcat(database_file, DBFILE);

	AFB_INFO("The database file is '%s'", database_file);

	if ((ret = db_create(&database, NULL, 0)) != 0)
	{
		AFB_ERROR("Failed to create database: %s.", db_strerror(ret));
		return -1;
	}

	if ((ret = database->open(database, NULL, database_file, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
	{
		AFB_ERROR("Failed to open the '%s' database: %s.", database_file, db_strerror(ret));
		database->close(database, 0);
		return -1;
	}

	return 0;
}

/**
 * Returns the database key for the 'req'
 */
static int get_key(struct afb_req req, DBT *key)
{
	char *appid, *data;
	const char *jkey;

	size_t ljkey, lappid, size;

	struct json_object* args;
	struct json_object* item;

	/* get the key */
	args = afb_req_json(req);
	if (!json_object_object_get_ex(args, "key", &item))
	{
		afb_req_fail(req, "no-key", NULL);
		return -1;
	}
	if (!item
	 || !(jkey = json_object_get_string(item))
	 || !(ljkey = strlen(jkey)))
	{
		afb_req_fail(req, "bad-key", NULL);
		return -1;
	}

	/* get the appid */
	appid = afb_req_get_application_id(req);
	if (!appid)
	{
		afb_req_fail(req, "bad-context", NULL);
		return -1;
	}

	/* make the db-key */
	lappid = strlen(appid);
	size = lappid + ljkey + 2;
	data = realloc(appid, size);
	if (!data)
	{
		free(appid);
		afb_req_fail(req, "out-of-memory", NULL);
		return -1;
	}
	data[lappid] = ':';
	memcpy(&data[lappid + 1], jkey, ljkey + 1);

	/* return the key */
	key->data = data;
	key->size = (uint32_t)size;
	return 0;
}

/**
 * @brief Handle the @c read verb.
 * @param[in] req The query.
 */
static void verb_insert(struct afb_req req)
{
	int ret;
	DBT key;
	DBT data;

	const char* value;

	struct json_object* args;
	struct json_object* item;

	args = afb_req_json(req);

	if (!json_object_object_get_ex(args, "value", &item) || !item) value = NULL;
	else value = json_object_get_string(item);

	if (!value || !strlen(value))
	{
		afb_req_fail(req, "No value provided.", NULL);
		return;
	}

	if (get_key(req, &key))
		return;

	AFB_INFO("insert: key=%s, value=%s", (char*)key.data, value);

	data.data = (void*)value;
	data.size = (uint32_t)strlen(value);

	if ((ret = database->put(database, NULL, &key, &data, DB_NOOVERWRITE)) == 0)
		afb_req_success_f(req, NULL, "db success: insertion %s=%s.", (char*)key.data, (char*)data.data);
	else
		afb_req_fail_f(req, "Failed to insert datas.", "db fail: insertion : %s=%s - %s", (char*)key.data, (char*)data.data, db_strerror(ret));
	free(key.data);
}

static void verb_update(struct afb_req req)
{
	DBT key;
	DBT data;
	int ret;

	char* rkey;
	const char* tag;
	const char* value;

	struct json_object* args;
	struct json_object* item;

	args = afb_req_json(req);
	// username should be get from identity binding
	// application should be get from smack
	// tag should be get using get_json_string(args, "tag");

	if (!args)
	{
		afb_req_fail(req, "No argument provided.", NULL);
		return;
	}

	if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
	else tag = json_object_get_string(item);

	if (!tag || !strlen(tag))
	{
		afb_req_fail(req, "No tag provided.", NULL);
		return;
	}

	if (!json_object_object_get_ex(args, "value", &item) || !item) value = NULL;
	else value = json_object_get_string(item);

	if (!value || !strlen(value))
	{
		afb_req_fail(req, "No value provided.", NULL);
		return;
	}

	rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
	strcpy(rkey, USERNAME);
	strcat(rkey, ":");
	strcat(rkey, APPNAME);
	strcat(rkey, ":");
	strcat(rkey, tag);

	AFB_INFO("update: key=%s, value=%s", rkey, value);

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));

	key.data = rkey;
	key.size = (uint32_t)strlen(rkey);

	data.data = (void*)value;
	data.size = (uint32_t)strlen(value);

	if ((ret = database->put(database, NULL, &key, &data, 0)) == 0)
		afb_req_success_f(req, NULL, "db success: update %s=%s.", (char*)key.data, (char*)data.data);
	else
		afb_req_fail_f(req, "Failed to update datas.", "db fail: update %s=%s - %s", (char*)key.data, (char*)data.data, db_strerror(ret));
	free(rkey);
}

static void verb_delete(struct afb_req req)
{
	DBT key;
	int ret;

	char* rkey;
	const char* tag;

	struct json_object* args;
	struct json_object* item;

	args = afb_req_json(req);

	if (!args)
	{
		afb_req_fail(req, "No argument provided.", NULL);
		return;
	}

	if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
	else tag = json_object_get_string(item);

	if (!tag || !strlen(tag))
	{
		afb_req_fail(req, "No tag provided.", NULL);
		return;
	}

	rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
	strcpy(rkey, USERNAME);
	strcat(rkey, ":");
	strcat(rkey, APPNAME);
	strcat(rkey, ":");
	strcat(rkey, tag);

	AFB_INFO("delete: key=%s", rkey);

	memset(&key, 0, sizeof(key));

	key.data = rkey;
	key.size = (uint32_t)strlen(rkey);

	if ((ret = database->del(database, NULL, &key, 0)) == 0)
		afb_req_success_f(req, NULL, "db success: delete %s.", (char *)key.data);
	else
		afb_req_fail_f(req, "Failed to delete datas.", "db fail: delete %s - %s", (char*)key.data, db_strerror(ret));
	free(rkey);
}

static void verb_read(struct afb_req req)
{
	DBT key;
	DBT data;
	int ret;

	char* rkey;
	const char* tag;
	char value[4096];

	struct json_object* args;
	struct json_object* item;
	struct json_object* result;
	struct json_object* val;

	args = afb_req_json(req);

	if (!args)
	{
		afb_req_fail(req, "No argument provided.", NULL);
		return;
	}

	if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
	else tag = json_object_get_string(item);

	if (!tag || !strlen(tag))
	{
		afb_req_fail(req, "No tag provided.", NULL);
		return;
	}

	rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
	strcpy(rkey, USERNAME);
	strcat(rkey, ":");
	strcat(rkey, APPNAME);
	strcat(rkey, ":");
	strcat(rkey, tag);

	AFB_INFO("update: key=%s, value=%s", rkey, value);

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	memset(&value, 0, 4096);

	key.data = rkey;
	key.size = (uint32_t)strlen(rkey);

	data.data = value;
	data.ulen = 4096;
	data.flags = DB_DBT_USERMEM;

	if ((ret = database->get(database, NULL, &key, &data, 0)) == 0)
	{
		result = json_object_new_object();
		val = json_tokener_parse((char*)data.data);
		json_object_object_add(result, "value", val ? val : json_object_new_string((char*)data.data));

		afb_req_success_f(req, result, "db success: read %s=%s.", (char*)key.data, (char*)data.data);
	}
	else
		afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", (char*)key.data, db_strerror(ret));
	free(rkey);
}

// ----- Binding's configuration -----
/*
static const struct afb_auth ll_database_binding_auths[] = {
};
*/

static const afb_verb_v2 ll_database_binding_verbs[]= {
		REGISTER_VERB(insert,	NULL, NULL, AFB_SESSION_NONE_V2),
		REGISTER_VERB(update,	NULL, NULL, AFB_SESSION_NONE_V2),
		REGISTER_VERB(delete,	NULL, NULL, AFB_SESSION_NONE_V2),
		REGISTER_VERB(read,		NULL, NULL, AFB_SESSION_NONE_V2),
        { .verb = NULL}
};

const struct afb_binding_v2 afbBindingV2 = {
                .api = "ll-database",
                .specification = NULL,
                .verbs = ll_database_binding_verbs,
                .preinit = NULL,
                .init = ll_database_binding_init,
                .onevent = NULL,
                .noconcurrency = 0
};