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_insert(struct afb_req req);
49 void verb_update(struct afb_req req);
50 void verb_delete(struct afb_req req);
51 void verb_read(struct afb_req req);
53 // ----- Binding's implementations -----
56 * @brief Initialize the binding.
57 * @return Exit code, zero if success.
59 int ll_database_binding_init()
62 struct passwd* result;
67 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
68 if (bufsize == -1 || bufsize > MAX_PATH) bufsize = MAX_PATH;
70 ret = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
73 if (ret == 0) AFB_ERROR("User not found");
74 else AFB_ERROR("getpwuid_r failed with %d code", ret);
78 memset(database_file, 0, MAX_PATH);
79 strcat(database_file, result->pw_dir);
80 strcat(database_file, DBFILE);
82 AFB_INFO("The database file is '%s'", database_file);
84 if ((ret = db_create(&database, NULL, 0)) != 0)
86 AFB_ERROR("Failed to create database: %s.", db_strerror(ret));
90 if ((ret = database->open(database, NULL, database_file, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
92 AFB_ERROR("Failed to open the '%s' database: %s.", database_file, db_strerror(ret));
93 database->close(database, 0);
101 * @brief Handle the @c read verb.
102 * @param[in] req The query.
104 void verb_insert(struct afb_req req)
114 struct json_object* args;
115 struct json_object* item;
117 args = afb_req_json(req);
121 afb_req_fail(req, "No argument provided.", NULL);
125 if (!json_object_object_get_ex(args, "key", &item) || !item) tag = NULL;
126 else tag = json_object_get_string(item);
128 if (!tag || !strlen(tag))
130 afb_req_fail(req, "No tag provided.", NULL);
134 if (!json_object_object_get_ex(args, "value", &item) || !item) value = NULL;
135 else value = json_object_get_string(item);
137 if (!value || !strlen(value))
139 afb_req_fail(req, "No value provided.", NULL);
143 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
144 strcpy(rkey, USERNAME);
146 strcat(rkey, APPNAME);
150 AFB_INFO("insert: key=%s, value=%s", rkey, value);
152 memset(&key, 0, sizeof(key));
153 memset(&data, 0, sizeof(data));
156 key.size = (uint32_t)strlen(rkey);
158 data.data = (void*)value;
159 data.size = (uint32_t)strlen(value);
161 if ((ret = database->put(database, NULL, &key, &data, DB_NOOVERWRITE)) == 0)
162 afb_req_success_f(req, NULL, "db success: insertion %s=%s.", (char*)key.data, (char*)data.data);
164 afb_req_fail_f(req, "Failed to insert datas.", "db fail: insertion : %s=%s - %s", (char*)key.data, (char*)data.data, db_strerror(ret));
168 void verb_update(struct afb_req req)
178 struct json_object* args;
179 struct json_object* item;
181 args = afb_req_json(req);
182 // username should be get from identity binding
183 // application should be get from smack
184 // tag should be get using get_json_string(args, "tag");
188 afb_req_fail(req, "No argument provided.", NULL);
192 if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
193 else tag = json_object_get_string(item);
195 if (!tag || !strlen(tag))
197 afb_req_fail(req, "No tag provided.", NULL);
201 if (!json_object_object_get_ex(args, "value", &item) || !item) value = NULL;
202 else value = json_object_get_string(item);
204 if (!value || !strlen(value))
206 afb_req_fail(req, "No value provided.", NULL);
210 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
211 strcpy(rkey, USERNAME);
213 strcat(rkey, APPNAME);
217 AFB_INFO("update: key=%s, value=%s", rkey, value);
219 memset(&key, 0, sizeof(key));
220 memset(&data, 0, sizeof(data));
223 key.size = (uint32_t)strlen(rkey);
225 data.data = (void*)value;
226 data.size = (uint32_t)strlen(value);
228 if ((ret = database->put(database, NULL, &key, &data, 0)) == 0)
229 afb_req_success_f(req, NULL, "db success: update %s=%s.", (char*)key.data, (char*)data.data);
231 afb_req_fail_f(req, "Failed to update datas.", "db fail: update %s=%s - %s", (char*)key.data, (char*)data.data, db_strerror(ret));
235 void verb_delete(struct afb_req req)
243 struct json_object* args;
244 struct json_object* item;
246 args = afb_req_json(req);
250 afb_req_fail(req, "No argument provided.", NULL);
254 if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
255 else tag = json_object_get_string(item);
257 if (!tag || !strlen(tag))
259 afb_req_fail(req, "No tag provided.", NULL);
263 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
264 strcpy(rkey, USERNAME);
266 strcat(rkey, APPNAME);
270 AFB_INFO("delete: key=%s", rkey);
272 memset(&key, 0, sizeof(key));
275 key.size = (uint32_t)strlen(rkey);
277 if ((ret = database->del(database, NULL, &key, 0)) == 0)
278 afb_req_success_f(req, NULL, "db success: delete %s.", (char *)key.data);
280 afb_req_fail_f(req, "Failed to delete datas.", "db fail: delete %s - %s", (char*)key.data, db_strerror(ret));
284 void verb_read(struct afb_req req)
294 struct json_object* args;
295 struct json_object* item;
296 struct json_object* result;
297 struct json_object* val;
299 args = afb_req_json(req);
303 afb_req_fail(req, "No argument provided.", NULL);
307 if (!json_object_object_get_ex(args, "tag", &item) || !item) tag = NULL;
308 else tag = json_object_get_string(item);
310 if (!tag || !strlen(tag))
312 afb_req_fail(req, "No tag provided.", NULL);
316 rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
317 strcpy(rkey, USERNAME);
319 strcat(rkey, APPNAME);
323 AFB_INFO("update: key=%s, value=%s", rkey, value);
325 memset(&key, 0, sizeof(key));
326 memset(&data, 0, sizeof(data));
327 memset(&value, 0, 4096);
330 key.size = (uint32_t)strlen(rkey);
334 data.flags = DB_DBT_USERMEM;
336 if ((ret = database->get(database, NULL, &key, &data, 0)) == 0)
338 result = json_object_new_object();
339 val = json_tokener_parse((char*)data.data);
340 json_object_object_add(result, "value", val ? val : json_object_new_string((char*)data.data));
342 afb_req_success_f(req, result, "db success: read %s=%s.", (char*)key.data, (char*)data.data);
345 afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", (char*)key.data, db_strerror(ret));
349 // ----- Binding's configuration -----
351 static const struct afb_auth ll_database_binding_auths[] = {
355 static const afb_verb_v2 ll_database_binding_verbs[]= {
356 REGISTER_VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2),
357 REGISTER_VERB(update, NULL, NULL, AFB_SESSION_NONE_V2),
358 REGISTER_VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2),
359 REGISTER_VERB(read, NULL, NULL, AFB_SESSION_NONE_V2),
363 const struct afb_binding_v2 afbBindingV2 = {
364 .api = "ll-database",
365 .specification = NULL,
366 .verbs = ll_database_binding_verbs,
368 .init = ll_database_binding_init,