2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2 if the
4 * License as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * @brief Driver for the REDIS noSQL key value stores.
21 * @copyright 2000,2006 The FreeRADIUS server project
22 * @copyright 2011 TekSavvy Solutions <gabe@teksavvy.com>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
30 #include "rlm_redis.h"
32 static const CONF_PARSER module_config[] = {
33 { "hostname", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_DEPRECATED, REDIS_INST, hostname), NULL },
34 { "server", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, REDIS_INST, hostname), NULL },
35 { "port", FR_CONF_OFFSET(PW_TYPE_SHORT, REDIS_INST, port), "6379" },
36 { "database", FR_CONF_OFFSET(PW_TYPE_INTEGER, REDIS_INST, database), "0" },
37 { "password", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, REDIS_INST, password), NULL },
39 { NULL, -1, 0, NULL, NULL} /* end the list */
42 static int mod_conn_delete(UNUSED void *instance, void *handle)
44 REDISSOCK *dissocket = handle;
46 redisFree(dissocket->conn);
48 if (dissocket->reply) {
49 freeReplyObject(dissocket->reply);
50 dissocket->reply = NULL;
53 talloc_free(dissocket);
57 static void *mod_conn_create(void *ctx)
59 REDIS_INST *inst = ctx;
60 REDISSOCK *dissocket = NULL;
62 redisReply *reply = NULL;
65 conn = redisConnect(inst->hostname, inst->port);
66 if (conn->err) return NULL;
69 snprintf(buffer, sizeof(buffer), "AUTH %s", inst->password);
71 reply = redisCommand(conn, buffer);
73 ERROR("rlm_redis (%s): Failed to run AUTH",
76 if (reply) freeReplyObject(reply);
82 switch (reply->type) {
83 case REDIS_REPLY_STATUS:
84 if (strcmp(reply->str, "OK") != 0) {
85 ERROR("rlm_redis (%s): Failed authentication: reply %s",
86 inst->xlat_name, reply->str);
89 break; /* else it's OK */
92 ERROR("rlm_redis (%s): Unexpected reply to AUTH",
99 snprintf(buffer, sizeof(buffer), "SELECT %d", inst->database);
101 reply = redisCommand(conn, buffer);
103 ERROR("rlm_redis (%s): Failed to run SELECT",
109 switch (reply->type) {
110 case REDIS_REPLY_STATUS:
111 if (strcmp(reply->str, "OK") != 0) {
112 ERROR("rlm_redis (%s): Failed SELECT %d: reply %s",
113 inst->xlat_name, inst->database,
117 break; /* else it's OK */
120 ERROR("rlm_redis (%s): Unexpected reply to SELECT",
126 dissocket = talloc_zero(inst, REDISSOCK);
127 dissocket->conn = conn;
132 static ssize_t redis_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
134 REDIS_INST *inst = instance;
135 REDISSOCK *dissocket;
140 dissocket = fr_connection_get(inst->pool);
141 if (!dissocket) return -1;
143 /* Query failed for some reason, release socket and return */
144 if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
148 switch (dissocket->reply->type) {
149 case REDIS_REPLY_INTEGER:
151 snprintf(buffer_ptr, sizeof(buffer), "%lld",
152 dissocket->reply->integer);
154 ret = strlen(buffer_ptr);
157 case REDIS_REPLY_STATUS:
158 case REDIS_REPLY_STRING:
159 buffer_ptr = dissocket->reply->str;
160 ret = dissocket->reply->len;
168 if ((ret >= freespace) || (!buffer_ptr)) {
169 RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
175 strlcpy(out, buffer_ptr, freespace);
178 rlm_redis_finish_query(dissocket);
179 fr_connection_release(inst->pool, dissocket);
185 * Only free memory we allocated. The strings allocated via
186 * cf_section_parse() do not need to be freed.
188 static int mod_detach(void *instance)
190 REDIS_INST *inst = instance;
192 fr_connection_pool_delete(inst->pool);
198 * Query the redis database
200 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
201 char const *query, REQUEST *request)
203 REDISSOCK *dissocket;
205 char *argv[MAX_REDIS_ARGS];
206 char argv_buf[MAX_QUERY_LEN];
208 if (!query || !*query || !inst || !dissocket_p) {
212 argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, false,
213 sizeof(argv_buf), argv_buf);
217 dissocket = *dissocket_p;
219 DEBUG2("executing %s ...", argv[0]);
220 dissocket->reply = redisCommandArgv(dissocket->conn, argc, (char const **)(void **)argv, NULL);
221 if (!dissocket->reply) {
222 RERROR("%s", dissocket->conn->errstr);
224 dissocket = fr_connection_reconnect(inst->pool, dissocket);
231 dissocket->reply = redisCommand(dissocket->conn, query);
232 if (!dissocket->reply) {
233 RERROR("Failed after re-connect");
234 fr_connection_del(inst->pool, dissocket);
238 *dissocket_p = dissocket;
241 if (dissocket->reply->type == REDIS_REPLY_ERROR) {
242 RERROR("Query failed, %s", query);
250 * Clear the redis reply object if any
252 int rlm_redis_finish_query(REDISSOCK *dissocket)
254 if (!dissocket || !dissocket->reply) {
258 freeReplyObject(dissocket->reply);
259 dissocket->reply = NULL;
263 static int mod_instantiate(CONF_SECTION *conf, void *instance)
265 static bool version_done;
267 REDIS_INST *inst = instance;
272 INFO("rlm_redis: libhiredis version: %i.%i.%i", HIREDIS_MAJOR, HIREDIS_MINOR, HIREDIS_PATCH);
275 inst->xlat_name = cf_section_name2(conf);
277 if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
279 xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
281 inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
286 inst->redis_query = rlm_redis_query;
287 inst->redis_finish_query = rlm_redis_finish_query;
292 module_t rlm_redis = {
295 RLM_TYPE_THREAD_SAFE, /* type */
296 sizeof(REDIS_INST), /* yuck */
298 mod_instantiate, /* instantiation */
299 mod_detach, /* detach */
301 NULL, /* authentication */
302 NULL, /* authorization */
303 NULL, /* preaccounting */
304 NULL, /* accounting */
305 NULL, /* checksimul */
306 NULL, /* pre-proxy */
307 NULL, /* post-proxy */