2 * Copyright 2017 IoT.bzh
4 * author: Loïc Collignon <loic.collignon@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <sys/types.h>
25 #include <linux/limits.h>
26 #include <json-c/json.h>
29 #define AFB_BINDING_VERSION 2
30 #include <afb/afb-binding.h>
38 #define DBFILE "/ll-database-binding.db"
39 #define USERNAME "agl"
40 #define APPNAME "firefox"
42 // ----- Globals -----
44 char database_file[MAX_PATH];
46 // ----- Binding's declarations -----
47 int ll_database_binding_init();
48 void verb_read(struct afb_req req);
49 void verb_update(struct afb_req req);
50 void verb_delete(struct afb_req req);
52 void verb_insert(struct afb_req req);
53 void verb_update(struct afb_req req);
54 void verb_delete(struct afb_req req);
55 void verb_read(struct afb_req req);
57 // ----- Binding's implementations -----
60 * @brief Initialize the binding.
61 * @return Exit code, zero if success.
63 int ll_database_binding_init()
66 struct passwd* result;
71 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
72 if (bufsize == -1 || bufsize > MAX_PATH) bufsize = MAX_PATH;
74 ret = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
77 if (ret == 0) AFB_ERROR("User not found");
78 else AFB_ERROR("getpwuid_r failed with %d code", ret);
79 return ret ? ret : -1;
82 memset(database_file, 0, MAX_PATH);
83 strcat(database_file, result->pw_dir);
84 strcat(database_file, DBFILE);
86 AFB_INFO("The database file is '%s'", database_file);
88 if ((ret = db_create(&database, NULL, 0)) != 0)
90 AFB_ERROR("Failed to create database: %s.", db_strerror(ret));
94 if ((ret = database->open(database, NULL, database_file, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
96 AFB_ERROR("Failed to open the '%s' database: %s.", database_file, db_strerror(ret));
97 database->close(database, 0);
105 * @brief Handle the @c read verb.
106 * @param[in] req The query.
108 void verb_insert(struct afb_req req)
118 struct json_object* args;
119 struct json_object* item;
121 args = afb_req_json(req);
125 afb_req_fail(req, "No argument provided.", NULL);
129 if (!json_object_object_get_ex(args, "key", &item) || !item) tag = NULL;
130 else tag = json_object_get_string(item);
132 if (!tag || !strlen(tag))
134 afb_req_fail(req, "No tag provided.", NULL);
138 if (!json_object_object_get_ex(args, "value", &item) || !item) value = NULL;
139 else value = json_object_get_string(item);
141 if (!value || !strlen(value))
143 afb_req_fail(req, "No value provided.", NULL);
147 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
148 strcpy(rkey, USERNAME);
150 strcat(rkey, APPNAME);
154 AFB_INFO("insert: key=%s, value=%s", rkey, value);
156 memset(&key, 0, sizeof(key));
157 memset(&data, 0, sizeof(data));
160 key.size = strlen(rkey);
162 data.data = (void*)value;
163 data.size = strlen(value);
165 if ((ret = database->put(database, NULL, &key, &data, DB_NOOVERWRITE)) == 0)
166 afb_req_success_f(req, NULL, "db success: insertion %s=%s.", (char*)key.data, (char*)data.data);
168 afb_req_fail_f(req, "Failed to insert datas.", "db fail: insertion : %s=%s - %s", (char*)key.data, (char*)data.data, db_strerror(ret));
172 void verb_update(struct afb_req req)
182 struct json_object* args;
183 struct json_object* item;
185 args = afb_req_json(req);
186 // username should be get from identity binding
187 // application should be get from smack
188 // tag should be get using get_json_string(args, "tag");
192 afb_req_fail(req, "No argument provided.", NULL);
196 if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
197 else tag = json_object_get_string(item);
199 if (!tag || !strlen(tag))
201 afb_req_fail(req, "No tag provided.", NULL);
205 if (!json_object_object_get_ex(args, "value", &item) || !item) value = NULL;
206 else value = json_object_get_string(item);
208 if (!value || !strlen(value))
210 afb_req_fail(req, "No value provided.", NULL);
214 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
215 strcpy(rkey, USERNAME);
217 strcat(rkey, APPNAME);
221 AFB_INFO("update: key=%s, value=%s", rkey, value);
223 memset(&key, 0, sizeof(key));
224 memset(&data, 0, sizeof(data));
227 key.size = strlen(rkey);
229 data.data = (void*)value;
230 data.size = strlen(value);
232 if ((ret = database->put(database, NULL, &key, &data, 0)) == 0)
233 afb_req_success_f(req, NULL, "db success: update %s=%s.", (char*)key.data, (char*)data.data);
235 afb_req_fail_f(req, "Failed to update datas.", "db fail: update %s=%s - %s", (char*)key.data, (char*)data.data, db_strerror(ret));
239 void verb_delete(struct afb_req req)
247 struct json_object* args;
248 struct json_object* item;
250 args = afb_req_json(req);
254 afb_req_fail(req, "No argument provided.", NULL);
258 if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
259 else tag = json_object_get_string(item);
261 if (!tag || !strlen(tag))
263 afb_req_fail(req, "No tag provided.", NULL);
267 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
268 strcpy(rkey, USERNAME);
270 strcat(rkey, APPNAME);
274 AFB_INFO("delete: key=%s", rkey);
276 memset(&key, 0, sizeof(key));
279 key.size = strlen(rkey);
281 if ((ret = database->del(database, NULL, &key, 0)) == 0)
282 afb_req_success_f(req, NULL, "db success: delete %s.", (char *)key.data);
284 afb_req_fail_f(req, "Failed to delete datas.", "db fail: delete %s - %s", (char*)key.data, db_strerror(ret));
288 void verb_read(struct afb_req req)
299 struct json_object* args;
300 struct json_object* item;
301 struct json_object* result;
302 struct json_object* val;
304 args = afb_req_json(req);
308 afb_req_fail(req, "No argument provided.", NULL);
312 if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
313 else tag = json_object_get_string(item);
315 if (!tag || !strlen(tag))
317 afb_req_fail(req, "No tag provided.", NULL);
321 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
322 strcpy(rkey, USERNAME);
324 strcat(rkey, APPNAME);
328 AFB_INFO("update: key=%s, value=%s", rkey, value);
330 memset(&key, 0, sizeof(key));
331 memset(&data, 0, sizeof(data));
332 memset(&value, 0, 4096);
335 key.size = strlen(rkey);
339 data.flags = DB_DBT_USERMEM;
341 if ((ret = database->get(database, NULL, &key, &data, 0)) == 0)
343 result = json_object_new_object();
344 val = json_tokener_parse((char*)data.data);
345 json_object_object_add(result, "value", val ? val : json_object_new_string((char*)data.data));
347 afb_req_success_f(req, result, "db success: read %s=%s.", (char*)key.data, (char*)data.data);
350 afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", (char*)key.data, db_strerror(ret));
354 // ----- Binding's configuration -----
355 static const struct afb_auth ll_database_binding_auths[] = {
358 static const afb_verb_v2 ll_database_binding_verbs[]= {
359 REGISTER_VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2),
360 REGISTER_VERB(update, NULL, NULL, AFB_SESSION_NONE_V2),
361 REGISTER_VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2),
362 REGISTER_VERB(read, NULL, NULL, AFB_SESSION_NONE_V2),
366 const struct afb_binding_v2 afbBindingV2 = {
367 .api = "ll-database",
368 .specification = NULL,
369 .verbs = ll_database_binding_verbs,
371 .init = ll_database_binding_init,