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: 15df021d4eaba06d59e4e1887cf6c133c7917e99 $
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: 15df021d4eaba06d59e4e1887cf6c133c7917e99 $")
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 /*
353 * fr_redis_ct_replica() takes a uint8_t replica number.
354 * An index above 256 would truncate, and index 257
355 * would select the first replica instead of no node.
356 */
357 } else if ((idx - 1) > UINT8_MAX) {
358 node = NULL;
359 } else {
360 node = fr_redis_ct_replica(thread->rtcluster, key_slot, (uint8_t) (idx - 1));
361 }
362
363 if (!node) {
364 RDEBUG2("No node available for this key slot");
365 return XLAT_ACTION_DONE;
366 }
367
368 ipaddr = fr_redis_ct_ipaddr(node);
369 if (!ipaddr || (fr_redis_ct_port(&port, node) < 0)) {
370 REDEBUG("Failed retrieving node information");
371 return XLAT_ACTION_FAIL;
372 }
373
374 MEM(vb = fr_value_box_alloc_null(ctx));
375 fr_value_box_asprintf(vb, vb, NULL, false, "%s:%u", ipaddr, port);
377
378 return XLAT_ACTION_DONE;
379}
380
382 { .required = true, .single = true, .type = FR_TYPE_UINT64 }, /* key count */
383 { .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .concat = true, .type = FR_TYPE_STRING }, /* keys and args */
385};
386
387/** Cancellation of Redis Lua load / exec
388 *
389 */
391{
392 rlm_redis_lua_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_lua_xlat_rctx_t);
393
394 RDEBUG2("Forcibly cancelling pending redis lua command");
396}
397
399 UNUSED request_t *request, UNUSED fr_value_box_list_t *in);
400
401/** Process the results of loading a lua script to a redis server
402 *
403 * If the load succeeds, re-enqueue the original EVALSHA command.
404 */
406 request_t *request, UNUSED fr_value_box_list_t *in)
407{
408 rlm_redis_lua_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_lua_xlat_rctx_t);
409
410 if (rctx->action != XLAT_ACTION_DONE) {
411 RPERROR("Failed loading lua script");
412 return rctx->action;
413 }
414
415 /*
416 * Now the script has loaded, re-run the EVALSHA command on the same trunk
417 */
420 return XLAT_ACTION_FAIL;
421 }
422
424}
425
426/** Callback to verify reply to SCRIPT LOAD
427 */
428static void redis_lua_load_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
429{
430 rlm_redis_lua_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_lua_xlat_rctx_t);
431
432 if (reply->type != REDIS_REPLY_STRING) {
433 REDEBUG("Unexpected reply type after loading function");
434 return;
435 }
436
437 if (strcmp(reply->str, xlat_rctx->func->digest) == 0) {
438 RDEBUG3("Script \"%s\" loaded", xlat_rctx->func->name);
439 xlat_rctx->action = XLAT_ACTION_DONE;
440 return;
441 }
442
443 REDEBUG("Function digest %s, does not match calculated digest %s", reply->str, xlat_rctx->func->digest);
444}
445
446/** Callback to convert redis reply to value boxes
447 */
448static void redis_lua_xlat_results(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
449{
450 rlm_redis_lua_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_lua_xlat_rctx_t);
451 fr_value_box_t *vb;
452
453 MEM(vb = fr_value_box_alloc_null(xlat_rctx->ctx));
454 if (fr_redis_reply_to_value_box(xlat_rctx->ctx, vb, reply, FR_TYPE_VOID, NULL, false, false) < 0) {
455 RPERROR("Failed processing reply to %s", fr_redis_command_get_cmd(cmd));
456 return;
457 }
458
459 if (vb->type == FR_TYPE_GROUP) {
460 fr_value_box_t *child_vb = NULL;
461 while ((child_vb = fr_value_box_list_pop_head(&vb->vb_group))) fr_value_box_list_insert_tail(&xlat_rctx->out, child_vb);
462 talloc_free(vb);
463 } else {
464 fr_value_box_list_insert_tail(&xlat_rctx->out, vb);
465 }
466 xlat_rctx->action = XLAT_ACTION_DONE;
467}
468
469/** Process the results from calling a lua script
470 *
471 * Enqueuing SCRIPT LOAD ... if the server reports that the script is missing
472 */
474 request_t *request, UNUSED fr_value_box_list_t *in)
475{
476 rlm_redis_lua_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_lua_xlat_rctx_t);
477 fr_value_box_t *vb = NULL;
478
479 switch (fr_redis_command_set_rcode(rctx->cmds)) {
481 {
483 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
484
485 if (inst->conf.use_cluster_map) fr_redis_ct_map_get(thread->rtcluster, thread->cw,
486 inst->coord_pair_reg, false);
487 }
489
493
497
499 PERROR("Server returned error");
500 return XLAT_ACTION_FAIL;
501
503 {
505 char const **argv;
506 size_t *argv_len;
507
508 RWARN("Script \"%s\" not on the server", rctx->func->name);
509
510 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
511 MEM(argv = talloc_array(cmds, char const *, 3));
512 MEM(argv_len = talloc_array(cmds, size_t, 3));
513
514 argv[0] = "SCRIPT";
515 argv_len[0] = sizeof("SCRIPT") - 1;
516 argv[1] = "LOAD";
517 argv_len[1] = sizeof("LOAD") - 1;
518 argv[2] = rctx->func->body;
519 argv_len[2] = talloc_strlen(rctx->func->body);
520
521 REDIS_XLAT_CMD_SETUP(cmds, 3, argv, argv_len, rctx, false, redis_lua_load_results);
522
524 return XLAT_ACTION_FAIL;
525 }
526
528 }
529 default:
530 break;
531 }
532 if (rctx->action != XLAT_ACTION_DONE) {
533 RPERROR("Failed executing lua script");
534 return rctx->action;
535 }
536 while ((vb = fr_value_box_list_pop_head(&rctx->out))) fr_dcursor_append(out, vb);
537
538 return XLAT_ACTION_DONE;
539}
540
541/** Call a lua function on the redis server
542 *
543 * Lua functions either get uploaded when the trunk connection becomes active or the first
544 * time they get executed.
545 */
547 xlat_ctx_t const *xctx,
548 request_t *request, fr_value_box_list_t *in)
549{
550 rlm_redis_t *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_redis_t);
551 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
553 redis_lua_func_t *func = xlat_inst->func;
554
558
559 char const **argv;
560 size_t *arg_len;
561 size_t argc;
562 fr_value_box_t *key_vb;
563 char *key_count;
564 uint8_t const *key = NULL;
565 size_t key_len = 0;
566
567 argc = fr_value_box_list_num_elements(in);
568 if (argc > MAX_REDIS_ARGS) {
569 REDEBUG("Too many arguments (%zu)", argc);
570 return XLAT_ACTION_FAIL;
571 }
572 argc += 2;
573
575 rctx->ctx = ctx;
576 rctx->action = XLAT_ACTION_FAIL;
577 rctx->func = func;
578 fr_value_box_list_init(&rctx->out);
579
580 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
581 rctx->cmds = cmds;
582
583 MEM(argv = talloc_array(cmds, char const *, argc));
584 MEM(arg_len = talloc_array(cmds, size_t, argc));
585
586 /*
587 * Try EVALSHA first, and if that fails fall back to SCRIPT LOAD
588 */
589 argv[0] = talloc_strdup(argv, "EVALSHA");
590 arg_len[0] = sizeof("EVALSHA") - 1;
591 argv[1] = func->digest;
592 arg_len[1] = sizeof(func->digest) - 1;
593
594 /*
595 * First argument is always the key count
596 */
597 key_vb = fr_value_box_list_pop_head(in);
598 fr_assert(key_vb);
599 arg_len[2] = fr_value_box_aprint(argv, &key_count, key_vb, NULL);
600 if (unlikely(!key_count)) {
601 RPERROR("Failed converting key count to string");
602 return XLAT_ACTION_FAIL;
603 }
604 argv[2] = key_count;
605
606 argc = 3;
608 /*
609 * Fixup null or empty arguments to be
610 * zero length strings so that the position
611 * of subsequent arguments are maintained.
612 */
613 if (!fr_type_is_string(vb->type)) {
614 argv[argc] = "";
615 arg_len[argc++] = 0;
616 continue;
617 }
618
619 argv[argc] = talloc_strdup(argv, vb->vb_strvalue);
620 arg_len[argc++] = vb->vb_length;
621 }
622
623 /*
624 * For eval commands all keys should hash to the same redis instance
625 * so we just use the first key (the arg after the key count).
626 */
627 if (argc > 3) {
628 key = (uint8_t const *)argv[3];
629 key_len = arg_len[3];
630 }
631
632 REDIS_XLAT_CMD_SETUP(cmds, argc, argv, arg_len, rctx, func->read_only, redis_lua_xlat_results);
633
634 rctx->cmd = fr_redis_async_cmd_start(rctx, request, &ret, thread->rtcluster, key, key_len,
635 cmds, func->read_only, NULL);
636
637 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, inst->coord_pair_reg,
638 "Failed enqueing lua command", XLAT_ACTION_FAIL);
639
641}
642
643/** Copies the function configuration into xlat function instance data
644 *
645 */
647{
648 redis_lua_func_inst_t *inst = talloc_get_type_abort(xctx->inst, redis_lua_func_inst_t);
649
650 inst->func = talloc_get_type_abort(xctx->uctx, redis_lua_func_t);
651
652 return 0;
653}
654
656 { .required = true, .concat = true, .type = FR_TYPE_STRING },
657 { .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .concat = true, .type = FR_TYPE_STRING },
659};
660
661/** Callback to convert redis reply to value boxes
662 */
663static void redis_xlat_results(request_t *request, fr_redis_command_t *cmd, redisReply *reply, void *rctx)
664{
665 rlm_redis_xlat_rctx_t *xlat_rctx = talloc_get_type_abort(rctx, rlm_redis_xlat_rctx_t);
666 fr_value_box_t *vb;
667
668 MEM(vb = fr_value_box_alloc_null(xlat_rctx->ctx));
669 if (fr_redis_reply_to_value_box(xlat_rctx->ctx, vb, reply, FR_TYPE_VOID, NULL, false, false) < 0) {
670 RPERROR("Failed processing reply to %s", fr_redis_command_get_cmd(cmd));
671 return;
672 }
673
674 if (vb->type == FR_TYPE_GROUP) {
675 fr_value_box_t *child_vb = NULL;
676 while ((child_vb = fr_value_box_list_pop_head(&vb->vb_group))) fr_value_box_list_insert_tail(&xlat_rctx->out, child_vb);
677 talloc_free(vb);
678 } else {
679 fr_value_box_list_insert_tail(&xlat_rctx->out, vb);
680 }
681 xlat_rctx->action = XLAT_ACTION_DONE;
682}
683
684static xlat_action_t redis_xlat_resume(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx,
685 request_t *request, UNUSED fr_value_box_list_t *in)
686{
687 rlm_redis_xlat_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, rlm_redis_xlat_rctx_t);
688 fr_value_box_t *vb = NULL;
689
690 switch (fr_redis_command_set_rcode(rctx->cmds)) {
692 {
694 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
695
696 if (inst->conf.use_cluster_map) fr_redis_ct_map_get(thread->rtcluster, thread->cw,
697 inst->coord_pair_reg, false);
698 }
700
702 if (rctx->forced_node) goto error;
705
709
711 error:
712 RPERROR("Server returned error");
713 return XLAT_ACTION_FAIL;
714
715 default:
716 break;
717 }
718
719 if (rctx->action != XLAT_ACTION_DONE) {
720 RPERROR("Failed executing Redis command");
721 return rctx->action;
722 }
723
724 while ((vb = fr_value_box_list_pop_head(&rctx->out))) fr_dcursor_append(out, vb);
725
726 return XLAT_ACTION_DONE;
727}
728
729/** Xlat to make calls to redis
730 *
731@verbatim
732%redis(<redis command>)
733@endverbatim
734 *
735 * @ingroup xlat_functions
736 */
738 xlat_ctx_t const *xctx,
739 request_t *request, fr_value_box_list_t *in)
740{
742 rlm_redis_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_redis_thread_t);
743 uint8_t const *key = NULL;
744 size_t key_len = 0;
745
746 fr_value_box_t *first = fr_value_box_list_head(in);
747 fr_sbuff_t sbuff = FR_SBUFF_IN(first->vb_strvalue, first->vb_length);
748
749 int argc = 0;
750 char const **argv;
751 size_t *arg_len;
755 fr_redis_ct_node_t *node = NULL;
756
758 rctx->ctx = ctx;
759 rctx->action = XLAT_ACTION_FAIL;
760 fr_value_box_list_init(&rctx->out);
761
762 if (fr_sbuff_next_if_char(&sbuff, '-')) rctx->read_only = true;
763
764 /*
765 * Hack to allow querying against a specific node for testing
766 */
767 if (fr_sbuff_next_if_char(&sbuff, '@')) {
768 fr_ipaddr_t node_addr;
769 uint16_t port;
771
772 RDEBUG3("Overriding node selection");
773
774 if (fr_inet_pton_port(&node_addr, &port,
775 fr_sbuff_current(&sbuff), fr_sbuff_remaining(&sbuff),
776 AF_UNSPEC, true, true) < 0) {
777 RPEDEBUG("Failed parsing node address");
778 return XLAT_ACTION_FAIL;
779 }
780
781 fr_inet_ntop(buff, sizeof(buff), &node_addr);
783 .hostname = buff,
784 .port = port
785 });
786 if (!node) {
787 RPEDEBUG("Failed locating cluster node");
788 return XLAT_ACTION_FAIL;
789 }
790
791 fr_value_box_list_talloc_free_head(in); /* Remove and free server arg */
792 rctx->forced_node = true;
793 }
794
795 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
796 rctx->cmds = cmds;
797
798 argc = fr_value_box_list_num_elements(in);
799 MEM(argv = talloc_array(cmds, char const *, argc));
800 MEM(arg_len = talloc_array(cmds, size_t, argc));
801
802 argc = 0;
804 if (!fr_type_is_string(vb->type)) {
805 argv[argc] = talloc_strdup(argv, "");
806 arg_len[argc++] = 0;
807 continue;
808 }
809
810 if ((argc == 0) && rctx->read_only && !rctx->forced_node) {
811 argv[argc] = talloc_strndup(argv, vb->vb_strvalue + 1, vb->vb_length - 1);
812 arg_len[argc] = vb->vb_length - 1;
813 } else {
814 argv[argc] = talloc_strndup(argv, vb->vb_strvalue, vb->vb_length);
815 arg_len[argc] = vb->vb_length;
816 }
817 argc++;
818 }
819
820 /*
821 * If we've got multiple arguments, the second one is usually the key.
822 * The Redis docs say commands should be analysed first to get key
823 * positions, but this involves sending them to the server, which is
824 * just as expensive as sending them to the wrong server and receiving
825 * a redirect.
826 */
827 if (argc > 1) {
828 key = (uint8_t const *)argv[1];
829 key_len = arg_len[1];
830 }
831
832 REDIS_XLAT_CMD_SETUP(cmds, argc, argv, arg_len, rctx, rctx->read_only, redis_xlat_results);
833
835 thread->rtcluster, key, key_len, cmds, rctx->read_only, node);
836
837 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, inst->coord_pair_reg,
838 "Failed enqueueing Redis command", XLAT_ACTION_FAIL);
839
841}
842
844 UNUSED redisReply *reply, void *rctx)
845{
846 redis_lua_func_t *func = talloc_get_type_abort(rctx, redis_lua_func_t);
847 DEBUG2("Loaded lua function \"%s\" onto node", func->name);
848}
849
850static void lua_script_load(fr_redis_trunk_t *rtrunk, void *uctx)
851{
852 rlm_redis_thread_t *thread = talloc_get_type_abort(uctx, rlm_redis_thread_t);
854
855 MEM(cmds = fr_redis_command_set_alloc(rtrunk, NULL, NULL, NULL, NULL, true));
856
857 talloc_foreach(thread->inst->lua.funcs, func) {
858 char const **argv;
859 size_t *argv_len;
860
861 MEM(argv = talloc_array(cmds, char const *, 3));
862 MEM(argv_len = talloc_array(cmds, size_t, 3));
863
864 argv[0] = "SCRIPT";
865 argv_len[0] = sizeof("SCRIPT") - 1;
866 argv[1] = "LOAD";
867 argv_len[1] = sizeof("LOAD") - 1;
868 argv[2] = func->body;
869 argv_len[2] = talloc_strlen(func->body);
870
871 if (fr_redis_command_argv_add(cmds, 3, argv, argv_len,
873 talloc_free(cmds);
874 return;
875 }
876 }
877
878 if (redis_command_set_enqueue(rtrunk, cmds) != FR_REDIS_PIPELINE_OK) {
879 ERROR("Failed to enqueue lua function loading");
880 talloc_free(cmds);
881 }
882}
883
884
886{
887 rlm_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_redis_thread_t);
888 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
889
890 if (talloc_array_length(inst->lua.funcs) == 0) {
891 t->rtcluster = fr_redis_ct_alloc(t, mctx->el, &inst->conf, NULL, NULL, false);
892 } else {
893 t->rtcluster = fr_redis_ct_alloc(t, mctx->el, &inst->conf, lua_script_load, t, true);
894 }
895 if (!t->rtcluster) return -1;
896 t->inst = inst;
897
898 return 0;
899}
900
902{
903 rlm_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_redis_thread_t);
904 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
905
906 if (!inst->conf.use_cluster_map) return 0;
907
908 t->cw = fr_coord_attach(t, mctx->el, inst->coord_reg);
909
910 if (!t->cw) {
911 ERROR("Failed to attach to coordinator");
912 return -1;
913 }
914
915 if ((inst->conf.trunk_conf.start == 0) || (fr_schedule_worker_id() != 0)) return 0;
916
917 return fr_redis_ct_map_bootstrap(t->rtcluster, t->cw, inst->coord_pair_reg);
918}
919
921
922static int mod_instantiate(module_inst_ctx_t const *mctx)
923{
924 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
925
926 inst->conf.log_prefix = mctx->mi->name;
927 inst->conf.module_name = mctx->mi->module->name;
928 inst->conf.inst_name = mctx->mi->name;
929
930#ifdef HAVE_REDIS_SSL
931 if (inst->conf.use_tls && !inst->conf.tls) {
932 cf_log_err(mctx->mi->conf, "Missing tls section");
933 return -1;
934 }
935#endif
936
937 if (!inst->conf.use_cluster_map) return 0;
938
939 if (inst->conf.database) {
940 cf_log_err(mctx->mi->conf, "Cannot set Redis database number when cluster in use");
941 return -1;
942 }
943
945 .name = mctx->mi->name,
946 .worker_cb = worker_pair_callbacks,
947 .cb_id = REDIS_COORD_PAIR_CALLBACK_ID,
948 .root = fr_dict_root(dict_redis),
949 .cs = mctx->mi->conf,
950 }
951 );
952 if (!inst->coord_pair_reg) return -1;
953
954 FR_COORD_PAIR_CB_CTX_SET(coord_callbacks, worker_callbacks, inst->coord_pair_reg);
955
957 .name = mctx->mi->name,
958 .coord_cb = coord_callbacks,
959 .worker_cb = worker_callbacks,
960 .mi = mctx->mi
961 });
962
963 if (!inst->coord_reg) return -1;
964
965 return 0;
966}
967
969{
970 rlm_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_redis_thread_t);
971
972 if (!t->cw) return 0;
973
974 fr_coord_detach(t->cw, true);
975 t->cw = NULL;
976 return 0;
977}
978
979static int mod_detach(module_detach_ctx_t const *mctx)
980{
981 rlm_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
982
983 if (!inst->conf.use_cluster_map) return 0;
984
985 fr_coord_deregister(inst->coord_reg);
986 talloc_free(inst->coord_pair_reg);
987 return 0;
988}
989
990static int mod_bootstrap(module_inst_ctx_t const *mctx)
991{
992 rlm_redis_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_redis_t);
993 xlat_t *xlat;
994
995 xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, NULL, redis_xlat, FR_TYPE_VOID);
998
999 /*
1000 * %redis.node(<key>[, idx])
1001 */
1002 if (unlikely((xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, "node", redis_node_xlat, FR_TYPE_STRING)) == NULL)) return -1;
1004
1005 if (unlikely((xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, "remap", redis_remap_xlat, FR_TYPE_STRING)) == NULL)) return -1;
1006
1007 /*
1008 * Loop over the lua functions, registering an xlat
1009 * that'll call that function specifically.
1010 */
1011 talloc_foreach(inst->lua.funcs, func) {
1012 if (unlikely((xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, func->name, redis_lua_func_xlat, FR_TYPE_VOID)) == NULL)) return -1;
1016 }
1017
1018 return 0;
1019}
1020
1021static int mod_load(void)
1022{
1024
1025 return redis_dict_init();
1026}
1027
1028extern module_rlm_t rlm_redis;
1030 .common = {
1031 .magic = MODULE_MAGIC_INIT,
1032 .name = "redis",
1033 .inst_size = sizeof(rlm_redis_t),
1035 .onload = mod_load,
1036 .bootstrap = mod_bootstrap,
1037 .instantiate = mod_instantiate,
1038 .coord_attach = mod_coord_attach,
1039 .coord_detach = mod_coord_detach,
1040 .detach = mod_detach,
1042 .thread_instantiate = mod_thread_instantiate,
1043 }
1044};
#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:712
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:137
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:691
void fr_coord_deregister(fr_coord_reg_t *coord_reg)
De-register a coordinator.
Definition coord.c:172
int fr_coord_detach(fr_coord_worker_t *cw, bool exiting)
Signal a coordinator that a worker wants to detach.
Definition coord.c:652
A coordinator registration.
Definition coord.c:85
The worker end of worker <-> coordinator communication.
Definition coord.c:73
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:737
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:2053
#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:448
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:979
static int mod_load(void)
Definition rlm_redis.c:1021
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:850
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:901
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:1029
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:684
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:646
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:405
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:990
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:428
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:390
#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:885
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:663
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:843
static int mod_coord_detach(module_thread_inst_ctx_t const *mctx)
Definition rlm_redis.c:968
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:546
static xlat_arg_parser_t const redis_args[]
Definition rlm_redis.c:655
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:922
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:381
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:4790
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:4702
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