The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
rlm_redis.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: ce9b8c4a26e48aebc6fb3146a19959bb4fe56504 $
19 * @file rlm_redis.c
20 * @brief Driver for the Redis noSQL key value store.
21 *
22 * @author Gabriel Blanchard
23 *
24 * @copyright 2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25 * @copyright 2011 TekSavvy Solutions (gabe@teksavvy.com)
26 * @copyright 2000,2006,2015 The FreeRADIUS server project
27 */
28
29RCSID("$Id: ce9b8c4a26e48aebc6fb3146a19959bb4fe56504 $")
30
31#include <assert.h>
32#include <stdint.h>
33
34#include <freeradius-devel/redis/base.h>
35#include <freeradius-devel/redis/cluster_async.h>
36
37#include <freeradius-devel/server/modpriv.h>
38#include <freeradius-devel/server/module_rlm.h>
39#include <freeradius-devel/io/coord_pair.h>
40
41#include <freeradius-devel/unlang/xlat_func.h>
42
43#include <freeradius-devel/util/base16.h>
44#include <freeradius-devel/util/debug.h>
45#include <freeradius-devel/util/types.h>
46
47static fr_dict_t const *dict_redis;
48
51 { .out = &dict_redis, .proto = "redis" },
53};
54
55/** A lua function or stored procedure we make available as an xlat
56 *
57 */
58typedef struct {
59 char const *name; //!< Friendly name for the function. Used to register the equivalent xlat.
60 char digest[(SHA1_DIGEST_LENGTH * 2) + 1]; //!< pre-computed hash of lua code.
61 char const *body; //!< the actual lua code.
62 bool read_only; //!< Function has no side effects
64
65/** Instance of a redis lua func xlat
66 *
67 */
68typedef struct {
69 redis_lua_func_t *func; //!< Function configuration.
71
72
73typedef struct {
74 redis_lua_func_t **funcs; //!< Array of functions to register.
75
77
78/** rlm_redis module instance
79 *
80 */
81typedef struct {
82 fr_redis_conf_t conf; //!< Connection parameters for the Redis server.
83 //!< Must be first field in this struct.
84
85
86 rlm_redis_lua_t lua; //!< Array of functions to register.
87
88 fr_coord_reg_t *coord_reg; //!< Coordinator registration.
89 fr_coord_pair_reg_t *coord_pair_reg; //!< Coord pair registration.
91
92typedef struct {
93 rlm_redis_t const *inst; //!< Module instance.
94 fr_redis_ct_t *rtcluster; //!< Per thread Redis cluster.
95 fr_coord_worker_t *cw; //!< Coord-worker for fetching cluster map.
97
98/** Resume context for redis lua xlat
99 */
100typedef struct {
101 redis_lua_func_t const *func; //!< Lua function
102 TALLOC_CTX *ctx; //!< Context to allocate boxes.
103 fr_value_box_list_t out; //!< List to store boxes in callback.
104 xlat_action_t action; //!< Xlat action set in callback.
105 fr_redis_command_set_t *cmds; //!< Command set for this xlat.
106 fr_redis_async_cmd_t *cmd; //!< Async command context.
108
109/** Resume context for redis xlat
110 */
111typedef struct {
112 bool read_only; //!< Should the xlat be run read only.
113 bool forced_node; //!< Was the xlat called with a specific node.
114 TALLOC_CTX *ctx; //!< Context to allocate boxes.
115 fr_value_box_list_t out; //!< List to store boxes in callback.
116 xlat_action_t action; //!< Xlat action set in callback.
117 fr_redis_command_set_t *cmds; //!< Command set for this xlat.
118 fr_redis_async_cmd_t *cmd; //!< Async command context.
120
121#define REDIS_XLAT_CMD_SETUP(_cmds, _argc, _argv, _arg_len, _rctx, _read_only, _func) \
122if (_read_only && \
123 (fr_redis_command_literal_add(_cmds, "READONLY", redis_xlat_status_check, _rctx) != FR_REDIS_PIPELINE_OK)) \
124 return XLAT_ACTION_FAIL; \
125if (fr_redis_command_argv_add(_cmds, _argc, _argv, _arg_len, _func, _rctx) != FR_REDIS_PIPELINE_OK) \
126 return XLAT_ACTION_FAIL; \
127if (_read_only && \
128 (fr_redis_command_literal_add(_cmds, "READWRITE", redis_xlat_status_check, _rctx) != FR_REDIS_PIPELINE_OK)) \
129 return XLAT_ACTION_FAIL
130
131static int lua_func_body_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
132
135 { FR_CONF_OFFSET("read_only", redis_lua_func_t, read_only) },
137};
138
141 .subcs_type = "redis_lua_func_t", .name2 = CF_IDENT_ANY },
143};
144
150
151/** Do basic processing for a lua function body and compute its sha1 hash
152 *
153 */
154static int lua_func_body_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
155{
156 int ret;
157 redis_lua_func_t *func = talloc_get_type_abort(parent, redis_lua_func_t);
158 char const *body;
159 fr_sha1_ctx sha1_ctx;
161
162 /*
163 * Get the function name from name2
164 * of the enclosing function section.
165 */
167 if (unlikely(!func->name)) {
168 cf_log_err(cf_parent(ci), "functions must be declared as \"function <name> {\"");
169 return -1;
170 }
171
172 /*
173 * Perform normal string parsing first
174 */
175 if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
176 body = *((char **)out);
177
178 fr_sha1_init(&sha1_ctx);
179 fr_sha1_update(&sha1_ctx, (uint8_t const *)body, talloc_strlen(body));
180 fr_sha1_final(digest, &sha1_ctx);
181 fr_base16_encode(&FR_SBUFF_OUT(func->digest, sizeof(func->digest)), &FR_DBUFF_TMP(digest, sizeof(digest)));
182
183 if (DEBUG_ENABLED3) cf_log_debug(ci, "sha1 hash of function is %pV", fr_box_strvalue_len(func->digest, sizeof(func->digest) - 1));
184
185 return 0;
186}
187
188/** Callback to check redis replied with "OK" when expected.
189 */
190static void redis_xlat_status_check(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
191{
192 rlm_redis_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_xlat_rctx_t);
193
194 if (reply->type != REDIS_REPLY_STATUS) {
195 RWARN("Did not receive expected redis status reply");
196 xlat_rctx->action = XLAT_ACTION_FAIL;
197 return;
198 }
199
200 if (strcmp(reply->str, "OK") != 0) {
201 RERROR("Running \"%s\" returned %s", fr_redis_command_get_cmd(cmd), reply->str);
202 xlat_rctx->action = XLAT_ACTION_FAIL;
203 }
204}
205
206/** Callback to check redis replied with "PONG" when expected.
207 */
208static void redis_xlat_ping_check(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
209{
210 rlm_redis_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_xlat_rctx_t);
211
212 if (reply->type != REDIS_REPLY_STATUS) {
213 RWARN("Did not receive expected redis status reply");
214 return;
215 }
216
217 if (strcmp(reply->str, "PONG") != 0) {
218 RERROR("Running \"%s\" returned %s", fr_redis_command_get_cmd(cmd), reply->str);
219 return;
220 }
221 xlat_rctx->action = XLAT_ACTION_DONE;
222}
223
224/** Common cancellation for redis xlats
225 *
226 */
228{
229 rlm_redis_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_xlat_rctx_t);
230
231 RDEBUG2("Forcibly cancelling pending redis command");
233}
234
236 request_t *request, UNUSED fr_value_box_list_t *in)
237{
238 rlm_redis_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_xlat_rctx_t);
239 fr_value_box_t *vb = NULL;
240
241 if (rctx->action != XLAT_ACTION_DONE) {
242 RPERROR("PING after cluster remap failed");
243 return rctx->action;
244 }
245
246 MEM(vb = fr_value_box_alloc_null(ctx));
247 switch (fr_redis_command_set_rcode(rctx->cmds)) {
249 fr_value_box_strdup(vb, vb, NULL, "success", false);
250 break;
251 default:
252 fr_value_box_strdup(vb, vb, NULL, "fail", false);
253 }
254
256
257 return XLAT_ACTION_DONE;
258}
259
260/** Force a redis cluster remap
261 *
262@verbatim
263%redis.remap()
264@endverbatim
265 *
266 * @ingroup xlat_functions
267 */
269 xlat_ctx_t const *xctx,
270 request_t *request, UNUSED fr_value_box_list_t *in)
271{
273 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
274
278
279 if (!inst->conf.use_cluster_map) {
280 RWARN("Cluster map not in use");
281 return XLAT_ACTION_DONE;
282 }
283
284 if (fr_redis_ct_map_get(thread->rtcluster, thread->cw,
285 inst->coord_pair_reg, true) == REDIS_ASYNC_RCODE_ERROR) {
286 RPEDEBUG("Failed to initiate cluster remap");
287 return XLAT_ACTION_FAIL;
288 }
289
290 /*
291 * Since cluster remap is out of band, using the coordinator thread, queue up
292 * a "PING" command which will run after the remap.
293 * In addition, if the cluster map has not been previously fetched, this will
294 * bootstrap the cluster map fetching.
295 * The xlat will return after the remap has completed.
296 */
298 rctx->ctx = ctx;
299 rctx->action = XLAT_ACTION_FAIL;
300
301 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
302 rctx->cmds = cmds;
304 talloc_free(rctx);
305 return XLAT_ACTION_FAIL;
306 }
307
309 thread->rtcluster, NULL, 0, cmds, rctx->read_only, NULL);
310
311 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, inst->coord_pair_reg,
312 "Failed to enqueue redis PING", XLAT_ACTION_FAIL);
313
315}
316
318 { .required = true, .single = true, .type = FR_TYPE_STRING },
319 { .single = true, .type = FR_TYPE_UINT32 },
321};
322
323/** Return the node that is currently servicing a particular key
324 *
325@verbatim
326%redis.node(<key>[, <index>])
327@endverbatim
328 *
329 * @ingroup xlat_functions
330 */
332 xlat_ctx_t const *xctx,
333 request_t *request, fr_value_box_list_t *in)
334{
335 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
336
337 fr_redis_ct_key_slot_t const *key_slot;
338 fr_redis_ct_node_t const *node;
339 char const *ipaddr;
340 uint16_t port;
341
342 unsigned long idx = 0;
343 fr_value_box_t *vb, *key, *idx_vb;
344 XLAT_ARGS(in, &key, &idx_vb);
345
346 if (idx_vb) idx = idx_vb->vb_uint32;
347
348 key_slot = fr_redis_ct_slot_by_key(thread->rtcluster, request, (uint8_t const *)key->vb_strvalue,
349 key->vb_length);
350 if (idx == 0) {
351 node = fr_redis_ct_master(thread->rtcluster, key_slot);
352 } else if ((idx - 1) > UINT8_MAX) {
353 node = NULL;
354 } else {
355 node = fr_redis_ct_replica(thread->rtcluster, key_slot, (uint8_t) (idx - 1));
356 }
357
358 if (!node) {
359 RDEBUG2("No node available for this key slot");
360 return XLAT_ACTION_DONE;
361 }
362
363 ipaddr = fr_redis_ct_ipaddr(node);
364 if (!ipaddr || (fr_redis_ct_port(&port, node) < 0)) {
365 REDEBUG("Failed retrieving node information");
366 return XLAT_ACTION_FAIL;
367 }
368
369 MEM(vb = fr_value_box_alloc_null(ctx));
370 fr_value_box_asprintf(vb, vb, NULL, false, "%s:%u", ipaddr, port);
372
373 return XLAT_ACTION_DONE;
374}
375
377 { .required = true, .single = true, .type = FR_TYPE_UINT64 }, /* key count */
378 { .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .concat = true, .type = FR_TYPE_STRING }, /* keys and args */
380};
381
382/** Cancellation of Redis Lua load / exec
383 *
384 */
386{
387 rlm_redis_lua_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_lua_xlat_rctx_t);
388
389 RDEBUG2("Forcibly cancelling pending redis lua command");
391}
392
394 UNUSED request_t *request, UNUSED fr_value_box_list_t *in);
395
396/** Process the results of loading a lua script to a redis server
397 *
398 * If the load succeeds, re-enqueue the original EVALSHA command.
399 */
401 request_t *request, UNUSED fr_value_box_list_t *in)
402{
403 rlm_redis_lua_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_lua_xlat_rctx_t);
404
405 if (rctx->action != XLAT_ACTION_DONE) {
406 RPERROR("Failed loading lua script");
407 return rctx->action;
408 }
409
410 /*
411 * Now the script has loaded, re-run the EVALSHA command on the same trunk
412 */
415 return XLAT_ACTION_FAIL;
416 }
417
419}
420
421/** Callback to verify reply to SCRIPT LOAD
422 */
423static void redis_lua_load_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
424{
425 rlm_redis_lua_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_lua_xlat_rctx_t);
426
427 if (reply->type != REDIS_REPLY_STRING) {
428 REDEBUG("Unexpected reply type after loading function");
429 return;
430 }
431
432 if (strcmp(reply->str, xlat_rctx->func->digest) == 0) {
433 RDEBUG3("Script \"%s\" loaded", xlat_rctx->func->name);
434 xlat_rctx->action = XLAT_ACTION_DONE;
435 return;
436 }
437
438 REDEBUG("Function digest %s, does not match calculated digest %s", reply->str, xlat_rctx->func->digest);
439}
440
441/** Callback to convert redis reply to value boxes
442 */
443static void redis_lua_xlat_results(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
444{
445 rlm_redis_lua_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_lua_xlat_rctx_t);
446 fr_value_box_t *vb;
447
448 MEM(vb = fr_value_box_alloc_null(xlat_rctx->ctx));
449 if (fr_redis_reply_to_value_box(xlat_rctx->ctx, vb, reply, FR_TYPE_VOID, NULL, false, false) < 0) {
450 RPERROR("Failed processing reply to %s", fr_redis_command_get_cmd(cmd));
451 return;
452 }
453
454 if (vb->type == FR_TYPE_GROUP) {
455 fr_value_box_t *child_vb = NULL;
456 while ((child_vb = fr_value_box_list_pop_head(&vb->vb_group))) fr_value_box_list_insert_tail(&xlat_rctx->out, child_vb);
457 talloc_free(vb);
458 } else {
459 fr_value_box_list_insert_tail(&xlat_rctx->out, vb);
460 }
461 xlat_rctx->action = XLAT_ACTION_DONE;
462}
463
464/** Process the results from calling a lua script
465 *
466 * Enqueuing SCRIPT LOAD ... if the server reports that the script is missing
467 */
469 request_t *request, UNUSED fr_value_box_list_t *in)
470{
471 rlm_redis_lua_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_lua_xlat_rctx_t);
472 fr_value_box_t *vb = NULL;
473
474 switch (fr_redis_command_set_rcode(rctx->cmds)) {
476 {
478 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
479
480 if (inst->conf.use_cluster_map) fr_redis_ct_map_get(thread->rtcluster, thread->cw,
481 inst->coord_pair_reg, false);
482 }
484
488
492
494 PERROR("Server returned error");
495 return XLAT_ACTION_FAIL;
496
498 {
500 char const **argv;
501 size_t *argv_len;
502
503 RWARN("Script \"%s\" not on the server", rctx->func->name);
504
505 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
506 MEM(argv = talloc_array(cmds, char const *, 3));
507 MEM(argv_len = talloc_array(cmds, size_t, 3));
508
509 argv[0] = "SCRIPT";
510 argv_len[0] = sizeof("SCRIPT") - 1;
511 argv[1] = "LOAD";
512 argv_len[1] = sizeof("LOAD") - 1;
513 argv[2] = rctx->func->body;
514 argv_len[2] = talloc_strlen(rctx->func->body);
515
516 REDIS_XLAT_CMD_SETUP(cmds, 3, argv, argv_len, rctx, false, redis_lua_load_results);
517
519 return XLAT_ACTION_FAIL;
520 }
521
523 }
524 default:
525 break;
526 }
527 if (rctx->action != XLAT_ACTION_DONE) {
528 RPERROR("Failed executing lua script");
529 return rctx->action;
530 }
531 while ((vb = fr_value_box_list_pop_head(&rctx->out))) fr_dcursor_append(out, vb);
532
533 return XLAT_ACTION_DONE;
534}
535
536/** Call a lua function on the redis server
537 *
538 * Lua functions either get uploaded when the trunk connection becomes active or the first
539 * time they get executed.
540 */
542 xlat_ctx_t const *xctx,
543 request_t *request, fr_value_box_list_t *in)
544{
545 rlm_redis_t *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_redis_t);
546 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
548 redis_lua_func_t *func = xlat_inst->func;
549
553
554 char const **argv;
555 size_t *arg_len;
556 size_t argc;
557 fr_value_box_t *key_vb;
558 char *key_count;
559 uint8_t const *key = NULL;
560 size_t key_len = 0;
561
562 argc = fr_value_box_list_num_elements(in);
563 if (argc > MAX_REDIS_ARGS) {
564 REDEBUG("Too many arguments (%zu)", argc);
565 return XLAT_ACTION_FAIL;
566 }
567 argc += 2;
568
570 rctx->ctx = ctx;
571 rctx->action = XLAT_ACTION_FAIL;
572 rctx->func = func;
573 fr_value_box_list_init(&rctx->out);
574
575 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
576 rctx->cmds = cmds;
577
578 MEM(argv = talloc_array(cmds, char const *, argc));
579 MEM(arg_len = talloc_array(cmds, size_t, argc));
580
581 /*
582 * Try EVALSHA first, and if that fails fall back to SCRIPT LOAD
583 */
584 argv[0] = talloc_strdup(argv, "EVALSHA");
585 arg_len[0] = sizeof("EVALSHA") - 1;
586 argv[1] = func->digest;
587 arg_len[1] = sizeof(func->digest) - 1;
588
589 /*
590 * First argument is always the key count
591 */
592 key_vb = fr_value_box_list_pop_head(in);
593 fr_assert(key_vb);
594 arg_len[2] = fr_value_box_aprint(argv, &key_count, key_vb, NULL);
595 if (unlikely(!key_count)) {
596 RPERROR("Failed converting key count to string");
597 return XLAT_ACTION_FAIL;
598 }
599 argv[2] = key_count;
600
601 argc = 3;
603 /*
604 * Fixup null or empty arguments to be
605 * zero length strings so that the position
606 * of subsequent arguments are maintained.
607 */
608 if (!fr_type_is_string(vb->type)) {
609 argv[argc] = "";
610 arg_len[argc++] = 0;
611 continue;
612 }
613
614 argv[argc] = talloc_strdup(argv, vb->vb_strvalue);
615 arg_len[argc++] = vb->vb_length;
616 }
617
618 /*
619 * For eval commands all keys should hash to the same redis instance
620 * so we just use the first key (the arg after the key count).
621 */
622 if (argc > 3) {
623 key = (uint8_t const *)argv[3];
624 key_len = arg_len[3];
625 }
626
627 REDIS_XLAT_CMD_SETUP(cmds, argc, argv, arg_len, rctx, func->read_only, redis_lua_xlat_results);
628
629 rctx->cmd = fr_redis_async_cmd_start(rctx, request, &ret, thread->rtcluster, key, key_len,
630 cmds, func->read_only, NULL);
631
632 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, inst->coord_pair_reg,
633 "Failed enqueing lua command", XLAT_ACTION_FAIL);
634
636}
637
638/** Copies the function configuration into xlat function instance data
639 *
640 */
642{
643 redis_lua_func_inst_t *inst = talloc_get_type_abort(xctx->inst, redis_lua_func_inst_t);
644
645 inst->func = talloc_get_type_abort(xctx->uctx, redis_lua_func_t);
646
647 return 0;
648}
649
651 { .required = true, .concat = true, .type = FR_TYPE_STRING },
652 { .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .concat = true, .type = FR_TYPE_STRING },
654};
655
656/** Callback to convert redis reply to value boxes
657 */
658static void redis_xlat_results(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
659{
660 rlm_redis_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_xlat_rctx_t);
661 fr_value_box_t *vb;
662
663 MEM(vb = fr_value_box_alloc_null(xlat_rctx->ctx));
664 if (fr_redis_reply_to_value_box(xlat_rctx->ctx, vb, reply, FR_TYPE_VOID, NULL, false, false) < 0) {
665 RPERROR("Failed processing reply to %s", fr_redis_command_get_cmd(cmd));
666 return;
667 }
668
669 if (vb->type == FR_TYPE_GROUP) {
670 fr_value_box_t *child_vb = NULL;
671 while ((child_vb = fr_value_box_list_pop_head(&vb->vb_group))) fr_value_box_list_insert_tail(&xlat_rctx->out, child_vb);
672 talloc_free(vb);
673 } else {
674 fr_value_box_list_insert_tail(&xlat_rctx->out, vb);
675 }
676 xlat_rctx->action = XLAT_ACTION_DONE;
677}
678
679static xlat_action_t redis_xlat_resume(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx,
680 request_t *request, UNUSED fr_value_box_list_t *in)
681{
682 rlm_redis_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_xlat_rctx_t);
683 fr_value_box_t *vb = NULL;
684
685 switch (fr_redis_command_set_rcode(rctx->cmds)) {
687 {
689 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
690
691 if (inst->conf.use_cluster_map) fr_redis_ct_map_get(thread->rtcluster, thread->cw,
692 inst->coord_pair_reg, false);
693 }
695
697 if (rctx->forced_node) goto error;
700
704
706 error:
707 RPERROR("Server returned error");
708 return XLAT_ACTION_FAIL;
709
710 default:
711 break;
712 }
713
714 if (rctx->action != XLAT_ACTION_DONE) {
715 RPERROR("Failed executing Redis command");
716 return rctx->action;
717 }
718
719 while ((vb = fr_value_box_list_pop_head(&rctx->out))) fr_dcursor_append(out, vb);
720
721 return XLAT_ACTION_DONE;
722}
723
724/** Xlat to make calls to redis
725 *
726@verbatim
727%redis(<redis command>)
728@endverbatim
729 *
730 * @ingroup xlat_functions
731 */
733 xlat_ctx_t const *xctx,
734 request_t *request, fr_value_box_list_t *in)
735{
737 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
738 uint8_t const *key = NULL;
739 size_t key_len = 0;
740
741 fr_value_box_t *first = fr_value_box_list_head(in);
742 fr_sbuff_t sbuff = FR_SBUFF_IN(first->vb_strvalue, first->vb_length);
743
744 int argc = 0;
745 char const **argv;
746 size_t *arg_len;
750 fr_redis_ct_node_t *node = NULL;
751
753 rctx->ctx = ctx;
754 rctx->action = XLAT_ACTION_FAIL;
755 fr_value_box_list_init(&rctx->out);
756
757 if (fr_sbuff_next_if_char(&sbuff, '-')) rctx->read_only = true;
758
759 /*
760 * Hack to allow querying against a specific node for testing
761 */
762 if (fr_sbuff_next_if_char(&sbuff, '@')) {
763 fr_ipaddr_t node_addr;
764 uint16_t port;
766
767 RDEBUG3("Overriding node selection");
768
769 if (fr_inet_pton_port(&node_addr, &port,
770 fr_sbuff_current(&sbuff), fr_sbuff_remaining(&sbuff),
771 AF_UNSPEC, true, true) < 0) {
772 RPEDEBUG("Failed parsing node address");
773 return XLAT_ACTION_FAIL;
774 }
775
776 fr_inet_ntop(buff, sizeof(buff), &node_addr);
778 .hostname = buff,
779 .port = port
780 });
781 if (!node) {
782 RPEDEBUG("Failed locating cluster node");
783 return XLAT_ACTION_FAIL;
784 }
785
786 fr_value_box_list_talloc_free_head(in); /* Remove and free server arg */
787 rctx->forced_node = true;
788 }
789
790 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
791 rctx->cmds = cmds;
792
793 argc = fr_value_box_list_num_elements(in);
794 MEM(argv = talloc_array(cmds, char const *, argc));
795 MEM(arg_len = talloc_array(cmds, size_t, argc));
796
797 argc = 0;
799 if (!fr_type_is_string(vb->type)) {
800 argv[argc] = talloc_strdup(argv, "");
801 arg_len[argc++] = 0;
802 continue;
803 }
804
805 if ((argc == 0) && rctx->read_only && !rctx->forced_node) {
806 argv[argc] = talloc_strndup(argv, vb->vb_strvalue + 1, vb->vb_length - 1);
807 arg_len[argc] = vb->vb_length - 1;
808 } else {
809 argv[argc] = talloc_strndup(argv, vb->vb_strvalue, vb->vb_length);
810 arg_len[argc] = vb->vb_length;
811 }
812 argc++;
813 }
814
815 /*
816 * If we've got multiple arguments, the second one is usually the key.
817 * The Redis docs say commands should be analysed first to get key
818 * positions, but this involves sending them to the server, which is
819 * just as expensive as sending them to the wrong server and receiving
820 * a redirect.
821 */
822 if (argc > 1) {
823 key = (uint8_t const *)argv[1];
824 key_len = arg_len[1];
825 }
826
827 REDIS_XLAT_CMD_SETUP(cmds, argc, argv, arg_len, rctx, rctx->read_only, redis_xlat_results);
828
830 thread->rtcluster, key, key_len, cmds, rctx->read_only, node);
831
832 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, inst->coord_pair_reg,
833 "Failed enqueueing Redis command", XLAT_ACTION_FAIL);
834
836}
837
839 UNUSED redisReply *reply, void *rctx)
840{
841 redis_lua_func_t *func = talloc_get_type_abort(rctx, redis_lua_func_t);
842 DEBUG2("Loaded lua function \"%s\" onto node", func->name);
843}
844
845static void lua_script_load(fr_redis_trunk_t *rtrunk, void *uctx)
846{
847 rlm_redis_thread_t *thread = talloc_get_type_abort(uctx, rlm_redis_thread_t);
849
850 MEM(cmds = fr_redis_command_set_alloc(rtrunk, NULL, NULL, NULL, NULL, true));
851
852 talloc_foreach(thread->inst->lua.funcs, func) {
853 char const **argv;
854 size_t *argv_len;
855
856 MEM(argv = talloc_array(cmds, char const *, 3));
857 MEM(argv_len = talloc_array(cmds, size_t, 3));
858
859 argv[0] = "SCRIPT";
860 argv_len[0] = sizeof("SCRIPT") - 1;
861 argv[1] = "LOAD";
862 argv_len[1] = sizeof("LOAD") - 1;
863 argv[2] = func->body;
864 argv_len[2] = talloc_strlen(func->body);
865
866 if (fr_redis_command_argv_add(cmds, 3, argv, argv_len,
868 talloc_free(cmds);
869 return;
870 }
871 }
872
873 if (redis_command_set_enqueue(rtrunk, cmds) != FR_REDIS_PIPELINE_OK) {
874 ERROR("Failed to enqueue lua function loading");
875 talloc_free(cmds);
876 }
877}
878
879
881{
882 rlm_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_redis_thread_t);
883 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
884
885 if (talloc_array_length(inst->lua.funcs) == 0) {
886 t->rtcluster = fr_redis_ct_alloc(t, mctx->el, &inst->conf, NULL, NULL, false);
887 } else {
888 t->rtcluster = fr_redis_ct_alloc(t, mctx->el, &inst->conf, lua_script_load, t, true);
889 }
890 if (!t->rtcluster) return -1;
891 t->inst = inst;
892
893 return 0;
894}
895
897{
898 rlm_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_redis_thread_t);
899 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
900
901 if (!inst->conf.use_cluster_map) return 0;
902
903 t->cw = fr_coord_attach(t, mctx->el, inst->coord_reg);
904
905 if (!t->cw) {
906 ERROR("Failed to attach to coordinator");
907 return -1;
908 }
909
910 if ((inst->conf.trunk_conf.start == 0) || (fr_schedule_worker_id() != 0)) return 0;
911
912 return fr_redis_ct_map_bootstrap(t->rtcluster, t->cw, inst->coord_pair_reg);
913}
914
916
917static int mod_instantiate(module_inst_ctx_t const *mctx)
918{
919 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
920
921 inst->conf.log_prefix = mctx->mi->name;
922 inst->conf.module_name = mctx->mi->module->name;
923 inst->conf.inst_name = mctx->mi->name;
924
925#ifdef HAVE_REDIS_SSL
926 if (inst->conf.use_tls && !inst->conf.tls) {
927 cf_log_err(mctx->mi->conf, "Missing tls section");
928 return -1;
929 }
930#endif
931
932 if (!inst->conf.use_cluster_map) return 0;
933
934 if (inst->conf.database) {
935 cf_log_err(mctx->mi->conf, "Cannot set Redis database number when cluster in use");
936 return -1;
937 }
938
940 .name = mctx->mi->name,
941 .worker_cb = worker_pair_callbacks,
942 .cb_id = REDIS_COORD_PAIR_CALLBACK_ID,
943 .root = fr_dict_root(dict_redis),
944 .cs = mctx->mi->conf,
945 }
946 );
947 if (!inst->coord_pair_reg) return -1;
948
949 FR_COORD_PAIR_CB_CTX_SET(coord_callbacks, worker_callbacks, inst->coord_pair_reg);
950
952 .name = mctx->mi->name,
953 .coord_cb = coord_callbacks,
954 .worker_cb = worker_callbacks,
955 .mi = mctx->mi
956 });
957
958 if (!inst->coord_reg) return -1;
959
960 return 0;
961}
962
964{
965 rlm_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_redis_thread_t);
966
967 if (!t->cw) return 0;
968
969 fr_coord_detach(t->cw, true);
970 t->cw = NULL;
971 return 0;
972}
973
974static int mod_detach(module_detach_ctx_t const *mctx)
975{
976 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
977
978 if (!inst->conf.use_cluster_map) return 0;
979
980 fr_coord_deregister(inst->coord_reg);
981 talloc_free(inst->coord_pair_reg);
982 return 0;
983}
984
985static int mod_bootstrap(module_inst_ctx_t const *mctx)
986{
987 rlm_redis_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
988 xlat_t *xlat;
989
990 xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, NULL, redis_xlat, FR_TYPE_VOID);
993
994 /*
995 * %redis.node(<key>[, idx])
996 */
997 if (unlikely((xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, "node", redis_node_xlat, FR_TYPE_STRING)) == NULL)) return -1;
999
1000 if (unlikely((xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, "remap", redis_remap_xlat, FR_TYPE_STRING)) == NULL)) return -1;
1001
1002 /*
1003 * Loop over the lua functions, registering an xlat
1004 * that'll call that function specifically.
1005 */
1006 talloc_foreach(inst->lua.funcs, func) {
1007 if (unlikely((xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, func->name, redis_lua_func_xlat, FR_TYPE_VOID)) == NULL)) return -1;
1011 }
1012
1013 return 0;
1014}
1015
1016static int mod_load(void)
1017{
1019
1020 return redis_dict_init();
1021}
1022
1023extern module_rlm_t rlm_redis;
1025 .common = {
1026 .magic = MODULE_MAGIC_INIT,
1027 .name = "redis",
1028 .inst_size = sizeof(rlm_redis_t),
1030 .onload = mod_load,
1031 .bootstrap = mod_bootstrap,
1032 .instantiate = mod_instantiate,
1033 .coord_attach = mod_coord_attach,
1034 .coord_detach = mod_coord_detach,
1035 .detach = mod_detach,
1037 .thread_instantiate = mod_thread_instantiate,
1038 }
1039};
#define fr_base16_encode(_out, _in)
Definition base16.h:71
static uint8_t request[4]
static uint8_t reply[4]
#define RCSID(id)
Definition build.h:603
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define unlikely(_x)
Definition build.h:486
#define UNUSED
Definition build.h:415
int cf_pair_parse_value(TALLOC_CTX *ctx, void *out, UNUSED void *base, CONF_ITEM *ci, conf_parser_t const *rule)
Parses a CONF_PAIR into a C data type.
Definition cf_parse.c:252
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:722
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:666
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_SUBSECTION_ALLOC_MULTI(_name, _flags, _struct, _field, _subcs)
conf_parser_t which allocates one struct for each instance of a subsection
Definition cf_parse.h:403
char const * name2
Second identifier for CONF_SECTION.
Definition cf_parse.h:651
@ CONF_FLAG_OK_MISSING
OK if it's missing.
Definition cf_parse.h:491
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:309
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
Common header for all CONF_* types.
Definition cf_priv.h:54
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1405
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:671
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:340
#define cf_parent(_cf)
Definition cf_util.h:115
#define cf_log_debug(_cf, _fmt,...)
Definition cf_util.h:343
#define CF_IDENT_ANY
Definition cf_util.h:80
int fr_redis_ct_map_bootstrap(fr_redis_ct_t *rtcluster, fr_coord_worker_t *cw, fr_coord_pair_reg_t *coord_pair_reg)
Initiate bootstrapping of the cluster map.
fr_redis_ct_t * fr_redis_ct_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, fr_redis_conf_t *conf, fr_redis_trunk_active_t active, void *active_uctx, bool active_oneshot)
Allocate per-thread, per-cluster instance.
fr_redis_async_cmd_t * fr_redis_async_cmd_start(TALLOC_CTX *ctx, request_t *request, fr_redis_async_rcode_t *rcode, fr_redis_ct_t *rtcluster, uint8_t const *key, size_t key_len, fr_redis_command_set_t *cmds, bool read_only, fr_redis_ct_node_t *node)
Start running a command set on an async redis cluster.
fr_redis_async_rcode_t fr_redis_ct_map_get(fr_redis_ct_t *rtcluster, fr_coord_worker_t *cw, fr_coord_pair_reg_t *coord_pair_reg, bool force)
Initiate updating of the cluster map.
void fr_redis_async_cmd_cancel(fr_redis_async_cmd_t *cmd)
Cancel a Redis async command.
fr_redis_ct_node_t * fr_redis_ct_node_by_addr(fr_redis_ct_t *rtcluster, fr_redis_io_conf_t *ioconf)
fr_redis_async_rcode_t fr_redis_async_cmd_resend(fr_redis_async_cmd_t *cmd)
Re-submit a redis async command set.
fr_redis_ct_node_t const * fr_redis_ct_replica(fr_redis_ct_t *rtcluster, fr_redis_ct_key_slot_t const *key_slot, uint8_t replica_num)
Return the replica node that would be used for a particular key slot.
fr_redis_trunk_t * fr_redis_async_cmd_trunk(fr_redis_async_cmd_t *cmd)
Fetch the redis trunk a command is associated with.
fr_redis_ct_key_slot_t const * fr_redis_ct_slot_by_key(fr_redis_ct_t *rtcluster, request_t *request, uint8_t const *key, size_t key_len)
Resolve key to key slot.
fr_redis_async_rcode_t fr_redis_async_cmd_redirect(fr_redis_async_cmd_t *cmd)
Re-submit a redis async command set on a different node.
int fr_redis_ct_port(uint16_t *out, fr_redis_ct_node_t const *node)
Return the port of a particular node.
fr_redis_ct_node_t const * fr_redis_ct_master(fr_redis_ct_t *rtcluster, fr_redis_ct_key_slot_t const *key_slot)
Return the master node that would be used for a particular key slot.
char const * fr_redis_ct_ipaddr(fr_redis_ct_node_t const *node)
Return the ipaddr of a particular node.
Structure for holding the state of an async redis command set.
Thread local state for a cluster.
#define REDIS_ASYNC_COORD_CALLBACKS(_thread_type)
#define REDIS_ASYNC_START_RCODE_PROCESS(_rcode, _cluster, _cw, _coord_pair_reg, _error_msg, _error_ret)
Convenience macro to reduce boilerplate.
fr_coord_reg_t * fr_coord_register(fr_coord_reg_ctx_t *reg_ctx)
Register a coordinator.
Definition coord.c:145
fr_coord_worker_t * fr_coord_attach(TALLOC_CTX *ctx, fr_event_list_t *el, fr_coord_reg_t *coord_reg)
Attach a worker to a coordinator.
Definition coord.c:689
void fr_coord_deregister(fr_coord_reg_t *coord_reg)
De-register a coordinator.
Definition coord.c:180
int fr_coord_detach(fr_coord_worker_t *cw, bool exiting)
Signal a coordinator that a worker wants to detach.
Definition coord.c:650
A coordinator registration.
Definition coord.c:93
The worker end of worker <-> coordinator communication.
Definition coord.c:82
fr_coord_pair_reg_t * fr_coord_pair_register(fr_coord_pair_reg_ctx_t *reg_ctx)
Register a set of callbacks for pair list based coordinator messages.
Definition coord_pair.c:113
struct fr_coord_pair_reg_s fr_coord_pair_reg_t
Definition coord_pair.h:32
#define FR_COORD_PAIR_CB_CTX_SET(_in_cb, _out_cb, _reg)
Set up ctx on pair list callbacks.
Definition coord_pair.h:93
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:556
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2717
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
static xlat_action_t redis_remap_xlat(TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Force a redis cluster remap.
Definition rlm_redis.c:268
static xlat_action_t redis_node_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Return the node that is currently servicing a particular key.
Definition rlm_redis.c:331
static xlat_action_t redis_xlat(TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Xlat to make calls to redis.
Definition rlm_redis.c:732
talloc_free(hp)
int fr_inet_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_t inlen, int af, bool resolve, bool mask)
Parses IPv4/6 address + port, to fr_ipaddr_t and integer (port)
Definition inet.c:944
char * fr_inet_ntop(char out[static FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t const *addr)
Print the address portion of a fr_ipaddr_t.
Definition inet.c:1025
#define FR_IPADDR_STRLEN
Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.
Definition inet.h:89
IPv4/6 prefix.
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:2067
#define PERROR(_fmt,...)
Definition log.h:233
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RWARN(fmt,...)
Definition log.h:314
#define RERROR(fmt,...)
Definition log.h:315
#define RPERROR(fmt,...)
Definition log.h:319
#define RPEDEBUG(fmt,...)
Definition log.h:393
#define DEBUG_ENABLED3
True if global debug level 1-3 messages are enabled.
Definition log.h:264
unsigned short uint16_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_GROUP
A grouping of other attributes.
unsigned char uint8_t
#define UINT8_MAX
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
void * thread
Thread specific instance data.
Definition module_ctx.h:43
fr_event_list_t * el
Event list to register any IO handlers and timers against.
Definition module_ctx.h:68
module_instance_t * mi
Module instance to detach.
Definition module_ctx.h:57
void * thread
Thread instance data.
Definition module_ctx.h:67
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:64
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for detach calls.
Definition module_ctx.h:56
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
xlat_t * module_rlm_xlat_register(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, char const *name, xlat_func_t func, fr_type_t return_type)
Definition module_rlm.c:234
module_t common
Common fields presented by all modules.
Definition module_rlm.h:39
char const * fr_redis_command_get_cmd(fr_redis_command_t *cmd)
Definition pipeline.c:1025
fr_redis_async_rcode_t fr_redis_command_set_rcode(fr_redis_command_set_t *cmds)
Extract the rcode from a command set.
Definition pipeline.c:1040
fr_redis_pipeline_status_t redis_command_set_enqueue(fr_redis_trunk_t *rtrunk, fr_redis_command_set_t *cmds)
Enqueue a command set on a specific trunk.
Definition pipeline.c:535
int fr_redis_command_set_reset(fr_redis_command_set_t *cmds)
Reset a command set to it's state before enqueuing.
Definition pipeline.c:1065
fr_redis_command_set_t * fr_redis_command_set_alloc(TALLOC_CTX *ctx, request_t *request, fr_redis_command_set_complete_t complete, fr_redis_command_set_fail_t fail, void *rctx, bool autofree)
Allocate a new command set.
Definition pipeline.c:252
fr_redis_pipeline_status_t fr_redis_command_argv_add(fr_redis_command_set_t *cmds, size_t argc, char const **argv, size_t *argv_len, fr_redis_command_complete_t complete, void *rctx)
Add a command with arguments to the command set.
Definition pipeline.c:439
fr_redis_pipeline_status_t fr_redis_command_literal_add(fr_redis_command_set_t *cmds, char const *cmd_str, fr_redis_command_complete_t complete, void *rctx)
Add a literal command to the command set.
Definition pipeline.c:404
Represents a single command.
Definition pipeline.c:62
Represents a collection of pipelined commands.
Definition pipeline.c:94
@ FR_REDIS_PIPELINE_OK
No failure.
Definition pipeline.h:44
static const conf_parser_t config[]
Definition base.c:162
#define fr_assert(_expr)
Definition rad_assert.h:37
#define REDEBUG(fmt,...)
#define RDEBUG2(fmt,...)
#define DEBUG2(fmt,...)
int fr_redis_reply_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, redisReply *reply, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, bool box_error, bool shallow))
Convert a string or integer type to fr_value_box_t of specified type.
Definition redis.c:222
int redis_dict_init(void)
Load the Redis dictionaries.
Definition redis.c:136
#define REDIS_COMMON_CONFIG
Definition base.h:171
#define MAX_REDIS_ARGS
Definition base.h:55
void fr_redis_version_print(void)
Print the version of libhiredis the server was built against.
Definition redis.c:107
fr_redis_async_rcode_t
Definition base.h:90
@ REDIS_ASYNC_RCODE_MOVE
Attempt operation on an alternative node with remap.
Definition base.h:98
@ REDIS_ASYNC_RCODE_ERROR
Unrecoverable error.
Definition base.h:92
@ REDIS_ASYNC_RCODE_ASK
Attempt operation on an alternative node.
Definition base.h:97
@ REDIS_ASYNC_RCODE_TRY_AGAIN
Try the operation again.
Definition base.h:96
@ REDIS_ASYNC_RCODE_NO_SCRIPT
Script doesn't exist.
Definition base.h:99
@ REDIS_ASYNC_RCODE_SUCCESS
Operation was successful.
Definition base.h:91
Configuration parameters for a redis connection.
Definition base.h:124
static void redis_lua_xlat_results(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
Callback to convert redis reply to value boxes.
Definition rlm_redis.c:443
fr_dict_autoload_t rlm_redis_dict[]
Definition rlm_redis.c:50
static int mod_detach(module_detach_ctx_t const *mctx)
Definition rlm_redis.c:974
static int mod_load(void)
Definition rlm_redis.c:1016
fr_redis_async_cmd_t * cmd
Async command context.
Definition rlm_redis.c:118
rlm_redis_lua_t lua
Array of functions to register.
Definition rlm_redis.c:86
static void lua_script_load(fr_redis_trunk_t *rtrunk, void *uctx)
Definition rlm_redis.c:845
static xlat_action_t redis_remap_xlat_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Definition rlm_redis.c:235
static int mod_coord_attach(module_thread_inst_ctx_t const *mctx)
Definition rlm_redis.c:896
static conf_parser_t module_lua[]
Definition rlm_redis.c:139
TALLOC_CTX * ctx
Context to allocate boxes.
Definition rlm_redis.c:114
redis_lua_func_t const * func
Lua function.
Definition rlm_redis.c:101
static void redis_xlat_ping_check(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
Callback to check redis replied with "PONG" when expected.
Definition rlm_redis.c:208
TALLOC_CTX * ctx
Context to allocate boxes.
Definition rlm_redis.c:102
xlat_action_t action
Xlat action set in callback.
Definition rlm_redis.c:116
module_rlm_t rlm_redis
Definition rlm_redis.c:1024
static xlat_action_t redis_xlat_resume(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Definition rlm_redis.c:679
bool read_only
Should the xlat be run read only.
Definition rlm_redis.c:112
fr_redis_conf_t conf
Connection parameters for the Redis server.
Definition rlm_redis.c:82
static int redis_lua_func_instantiate(xlat_inst_ctx_t const *xctx)
Copies the function configuration into xlat function instance data.
Definition rlm_redis.c:641
fr_redis_command_set_t * cmds
Command set for this xlat.
Definition rlm_redis.c:105
fr_value_box_list_t out
List to store boxes in callback.
Definition rlm_redis.c:103
redis_lua_func_t ** funcs
Array of functions to register.
Definition rlm_redis.c:74
fr_coord_reg_t * coord_reg
Coordinator registration.
Definition rlm_redis.c:88
static xlat_action_t redis_lua_load_resume(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Process the results of loading a lua script to a redis server.
Definition rlm_redis.c:400
rlm_redis_t const * inst
Module instance.
Definition rlm_redis.c:93
char digest[(SHA1_DIGEST_LENGTH *2)+1]
pre-computed hash of lua code.
Definition rlm_redis.c:60
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Definition rlm_redis.c:985
static void redis_lua_load_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
Callback to verify reply to SCRIPT LOAD.
Definition rlm_redis.c:423
redis_lua_func_t * func
Function configuration.
Definition rlm_redis.c:69
static xlat_action_t redis_lua_func_resume(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
static conf_parser_t module_lua_func[]
Definition rlm_redis.c:133
static void redis_xlat_cancel(xlat_ctx_t const *xctx, request_t *request, UNUSED fr_signal_t action)
Common cancellation for redis xlats.
Definition rlm_redis.c:227
char const * body
the actual lua code.
Definition rlm_redis.c:61
fr_redis_command_set_t * cmds
Command set for this xlat.
Definition rlm_redis.c:117
char const * name
Friendly name for the function. Used to register the equivalent xlat.
Definition rlm_redis.c:59
static fr_dict_t const * dict_redis
Definition rlm_redis.c:47
fr_coord_pair_reg_t * coord_pair_reg
Coord pair registration.
Definition rlm_redis.c:89
static void redis_lua_cancel(xlat_ctx_t const *xctx, request_t *request, UNUSED fr_signal_t action)
Cancellation of Redis Lua load / exec.
Definition rlm_redis.c:385
#define REDIS_XLAT_CMD_SETUP(_cmds, _argc, _argv, _arg_len, _rctx, _read_only, _func)
Definition rlm_redis.c:121
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
Definition rlm_redis.c:880
fr_coord_worker_t * cw
Coord-worker for fetching cluster map.
Definition rlm_redis.c:95
static xlat_arg_parser_t const redis_node_xlat_args[]
Definition rlm_redis.c:317
static void redis_xlat_results(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
Callback to convert redis reply to value boxes.
Definition rlm_redis.c:658
fr_value_box_list_t out
List to store boxes in callback.
Definition rlm_redis.c:115
static void lua_script_load_results(UNUSED request_t *request, UNUSED fr_redis_command_t *cmd, UNUSED redisReply *reply, void *rctx)
Definition rlm_redis.c:838
static int mod_coord_detach(module_thread_inst_ctx_t const *mctx)
Definition rlm_redis.c:963
bool forced_node
Was the xlat called with a specific node.
Definition rlm_redis.c:113
static void redis_xlat_status_check(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
Callback to check redis replied with "OK" when expected.
Definition rlm_redis.c:190
static xlat_action_t redis_lua_func_xlat(TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Call a lua function on the redis server.
Definition rlm_redis.c:541
static xlat_arg_parser_t const redis_args[]
Definition rlm_redis.c:650
static int lua_func_body_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Do basic processing for a lua function body and compute its sha1 hash.
Definition rlm_redis.c:154
xlat_action_t action
Xlat action set in callback.
Definition rlm_redis.c:104
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition rlm_redis.c:917
fr_redis_async_cmd_t * cmd
Async command context.
Definition rlm_redis.c:106
static conf_parser_t module_config[]
Definition rlm_redis.c:145
fr_redis_ct_t * rtcluster
Per thread Redis cluster.
Definition rlm_redis.c:94
static xlat_arg_parser_t const redis_lua_func_args[]
Definition rlm_redis.c:376
bool read_only
Function has no side effects.
Definition rlm_redis.c:62
Instance of a redis lua func xlat.
Definition rlm_redis.c:68
A lua function or stored procedure we make available as an xlat.
Definition rlm_redis.c:58
Resume context for redis lua xlat.
Definition rlm_redis.c:100
rlm_redis module instance
Definition rlm_redis.c:81
Resume context for redis xlat.
Definition rlm_redis.c:111
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2247
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
int fr_schedule_worker_id(void)
Return the worker id for the current thread.
Definition schedule.c:111
#define MODULE_THREAD_INST(_ctype)
Definition module.h:261
char const * name
Instance name e.g. user_database.
Definition module.h:361
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:355
size_t inst_size
Size of the module's instance data.
Definition module.h:213
void * data
Module's instance data.
Definition module.h:297
void * boot
Data allocated during the boostrap phase.
Definition module.h:300
void fr_sha1_init(fr_sha1_ctx *context)
Definition sha1.c:93
void fr_sha1_final(uint8_t digest[static SHA1_DIGEST_LENGTH], fr_sha1_ctx *context)
Definition sha1.c:141
void fr_sha1_update(fr_sha1_ctx *context, uint8_t const *in, size_t len)
Definition sha1.c:105
#define SHA1_DIGEST_LENGTH
Definition sha1.h:29
#define FR_SIGNAL_ALLOW(_signals)
Signal mask which blocks every signal except the ones in _signals.
Definition signal.h:58
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
eap_aka_sim_process_conf_t * inst
#define talloc_get_type_abort_const
Definition talloc.h:117
#define talloc_strndup(_ctx, _str, _len)
Definition talloc.h:150
#define talloc_foreach(_array, _iter)
Iterate over a talloced array of elements.
Definition talloc.h:79
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
xlat_action_t unlang_xlat_yield(request_t *request, xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition xlat.c:543
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
@ XLAT_ARG_VARIADIC_EMPTY_KEEP
Empty argument groups are left alone, and either passed through as empty groups or null boxes.
Definition xlat.h:127
#define XLAT_ARGS(_list,...)
Populate local variables with value boxes from the input list.
Definition xlat.h:373
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:136
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:160
Definition for a single argument consumed by an xlat function.
Definition xlat.h:135
static fr_slen_t parent
Definition pair.h:864
#define fr_type_is_string(_x)
Definition types.h:348
int fr_value_box_asprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt,...)
Print a formatted string using our internal printf wrapper and assign it to a value box.
Definition value.c:4851
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4763
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_slen_t fr_value_box_aprint(TALLOC_CTX *ctx, char **out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules) 1(fr_value_box_print
#define fr_box_strvalue_len(_val, _len)
Definition value.h:340
static fr_sbuff_err_t char ** out
Definition value.h:1061
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:679
#define fr_value_box_list_foreach(_list_head, _iter)
Definition value.h:253
void * rctx
Resume context.
Definition xlat_ctx.h:54
void const * inst
xlat instance data.
Definition xlat_ctx.h:50
void * uctx
Passed to the registration function.
Definition xlat_ctx.h:66
module_ctx_t const * mctx
Synthesised module calling ctx.
Definition xlat_ctx.h:52
void * inst
xlat instance data to populate.
Definition xlat_ctx.h:63
An xlat calling ctx.
Definition xlat_ctx.h:49
An xlat instantiation ctx.
Definition xlat_ctx.h:62
void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags)
Specify flags that alter the xlat's behaviour.
Definition xlat_func.c:401
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:374
#define xlat_func_instantiate_set(_xlat, _instantiate, _inst_struct, _detach, _uctx)
Set a callback for global instantiation of xlat functions.
Definition xlat_func.h:95
@ XLAT_FUNC_FLAG_MODULE_STATUS
Definition xlat_func.h:40