27 RCSID(
"$Id: dc9e3b4cb17048dd0f6316fe90111e94433853d9 $")
29 #include <freeradius-devel/radiusd.h>
31 #include <libcouchbase/couchbase.h>
32 #include "../rlm_json/json.h"
45 if (error != LCB_SUCCESS) {
47 ERROR(
"rlm_couchbase: (stats_callback) %s (0x%x)", lcb_strerror(instance, error), error);
63 lcb_error_t error,
const lcb_store_resp_t *resp)
65 if (error != LCB_SUCCESS) {
67 ERROR(
"rlm_couchbase: (store_callback) %s (0x%x)", lcb_strerror(instance, error), error);
87 const char *bytes = resp->v.v0.bytes;
88 lcb_size_t nbytes = resp->v.v0.nbytes;
94 if (bytes && nbytes > 1) {
96 DEBUG(
"rlm_couchbase: (get_callback) got %zu bytes", nbytes);
98 c->
jobj = json_tokener_parse_ex(c->
jtok, bytes, nbytes);
101 case json_tokener_continue:
103 if (c->
jobj != NULL) {
104 ERROR(
"rlm_couchbase: (get_callback) object not null on continue!");
107 case json_tokener_success:
112 ERROR(
"rlm_couchbase: (get_callback) json parsing error: %s",
121 DEBUG(
"rlm_couchbase: (get_callback) key does not exist");
126 ERROR(
"rlm_couchbase: (get_callback) %s (0x%x)", lcb_strerror(instance, error), error);
140 lcb_error_t error,
const lcb_http_resp_t *resp)
145 const char *bytes = resp->v.v0.bytes;
146 lcb_size_t nbytes = resp->v.v0.nbytes;
152 if (bytes && nbytes > 1) {
154 DEBUG(
"rlm_couchbase: (http_data_callback) got %zu bytes", nbytes);
156 c->
jobj = json_tokener_parse_ex(c->
jtok, bytes, nbytes);
159 case json_tokener_continue:
161 if (c->
jobj != NULL) {
162 ERROR(
"rlm_couchbase: (http_data_callback) object not null on continue!");
165 case json_tokener_success:
170 ERROR(
"rlm_couchbase: (http_data_callback) json parsing error: %s",
179 ERROR(
"rlm_couchbase: (http_data_callback) %s (0x%x)", lcb_strerror(instance, error), error);
202 struct lcb_create_st options;
205 memset(&options, 0,
sizeof(options));
208 options.v.v0.host = host;
209 options.v.v0.bucket = bucket;
212 if (bucket != NULL && pass != NULL) {
213 options.v.v0.user = bucket;
214 options.v.v0.passwd = pass;
218 error = lcb_create(instance, &options);
219 if (error != LCB_SUCCESS)
return error;
221 error = lcb_cntl(*instance, LCB_CNTL_SET, LCB_CNTL_CONFIGURATION_TIMEOUT, &timeout);
222 if (error != LCB_SUCCESS)
return error;
225 error = lcb_connect(*instance);
226 if (error != LCB_SUCCESS)
return error;
250 lcb_server_stats_cmd_t cmd;
251 const lcb_server_stats_cmd_t *commands[1];
255 memset(&cmd, 0,
sizeof(cmd));
258 cmd.v.v0.name =
"tap";
259 cmd.v.v0.nname = strlen(cmd.v.v0.name);
262 if ((error = lcb_server_stats(instance, cookie, 1, commands)) == LCB_SUCCESS) {
281 lcb_error_t
couchbase_set_key(lcb_t instance,
const char *key,
const char *document,
int expire)
285 const lcb_store_cmd_t *commands[1];
289 memset(&cmd, 0,
sizeof(cmd));
293 cmd.v.v0.nkey = strlen(cmd.v.v0.key);
294 cmd.v.v0.bytes = document;
295 cmd.v.v0.nbytes = strlen(cmd.v.v0.bytes);
296 cmd.v.v0.exptime = expire;
297 cmd.v.v0.operation = LCB_SET;
300 if ((error = lcb_store(instance, NULL, 1, commands)) == LCB_SUCCESS) {
325 const lcb_get_cmd_t *commands[1];
329 memset(&cmd, 0,
sizeof(cmd));
333 cmd.v.v0.nkey = strlen(cmd.v.v0.key);
339 c->
jerr = json_tokener_success;
342 c->
jtok = json_tokener_new();
345 DEBUG3(
"rlm_couchbase: fetching document %s", key);
348 if ((error = lcb_get(instance, c, 1, commands)) == LCB_SUCCESS) {
354 json_tokener_free(c->
jtok);
377 const lcb_http_cmd_t *commands;
380 memset(&cmd, 0,
sizeof(cmd));
383 cmd.v.v0.path = path;
384 cmd.v.v0.npath = strlen(cmd.v.v0.path);
385 cmd.v.v0.body = post;
386 cmd.v.v0.nbody = post ? strlen(post) : 0;
387 cmd.v.v0.method = post ? LCB_HTTP_METHOD_POST : LCB_HTTP_METHOD_GET;
388 cmd.v.v0.chunked = 1;
389 cmd.v.v0.content_type =
"application/json";
395 c->
jerr = json_tokener_success;
398 c->
jtok = json_tokener_new();
401 DEBUG3(
"rlm_couchbase: fetching view %s", path);
404 if ((error = lcb_make_http_request(instance, c, LCB_HTTP_TYPE_VIEW, commands, NULL)) == LCB_SUCCESS) {
410 json_tokener_free(c->
jtok);
const void * cdata
Constant pointer to cookie payload (cookie_t).
void couchbase_store_callback(lcb_t instance, const void *cookie, lcb_storage_t operation, lcb_error_t error, const lcb_store_resp_t *resp)
Couchbase callback for store (write) operations.
void * data
Non-constant pointer to data payload (cookie_t).
lcb_error_t couchbase_set_key(lcb_t instance, const char *key, const char *document, int expire)
Store a document by key in Couchbase.
void couchbase_stat_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_server_stat_resp_t *resp)
Couchbase callback for cluster statistics requests.
lcb_error_t couchbase_server_stats(lcb_t instance, const void *cookie)
Request Couchbase server statistics.
lcb_error_t couchbase_get_key(lcb_t instance, const void *cookie, const char *key)
Retrieve a document by key from Couchbase.
Couchbase wrapper function prototypes and datatypes.
void couchbase_http_data_callback(lcb_http_request_t request, lcb_t instance, const void *cookie, lcb_error_t error, const lcb_http_resp_t *resp)
Couchbase callback for http (view) operations.
void couchbase_get_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_get_resp_t *resp)
Couchbase callback for get (read) operations.
json_tokener * jtok
JSON tokener objects handled by the json-c library.
Union of constant and non-constant pointers.
json_object * jobj
JSON objects handled by the json-c library.
const char * json_tokener_error_desc(enum json_tokener_error jerr)
lcb_error_t couchbase_init_connection(lcb_t *instance, const char *host, const char *bucket, const char *pass, lcb_uint32_t timeout)
Initialize a Couchbase connection instance.
enum json_tokener_error jerr
Error values produced by the json-c library.
lcb_error_t couchbase_query_view(lcb_t instance, const void *cookie, const char *path, const char *post)
Query a Couchbase design document view.
Information relating to the parsing of Couchbase document payloads.
enum json_tokener_error json_tokener_get_error(json_tokener *tok)