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>
24 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
31 #include "rlm_redis.h"
33 static const CONF_PARSER module_config[] = {
34 { "hostname", PW_TYPE_STRING_PTR,
35 offsetof(REDIS_INST, hostname), NULL, "127.0.0.1"},
36 { "port", PW_TYPE_INTEGER,
37 offsetof(REDIS_INST, port), NULL, "6379"},
38 { "database", PW_TYPE_INTEGER,
39 offsetof(REDIS_INST, database), NULL, "0"},
40 { "password", PW_TYPE_STRING_PTR,
41 offsetof(REDIS_INST, password), NULL, NULL},
43 { NULL, -1, 0, NULL, NULL} /* end the list */
46 static int redis_delete_conn(UNUSED void *ctx, void *conn)
48 REDISSOCK *dissocket = conn;
50 redisFree(dissocket->conn);
52 if (dissocket->reply) {
53 freeReplyObject(dissocket->reply);
54 dissocket->reply = NULL;
61 static void *redis_create_conn(void *ctx)
63 REDIS_INST *inst = ctx;
64 REDISSOCK *dissocket = NULL;
68 conn = redisConnect(inst->hostname, inst->port);
69 if (conn->err) return NULL;
72 redisReply *reply = NULL;
74 snprintf(buffer, sizeof(buffer), "AUTH %s", inst->password);
76 reply = redisCommand(conn, buffer);
78 radlog(L_ERR, "rlm_redis (%s): Failed to run AUTH",
81 if (reply) freeReplyObject(reply);
87 switch (reply->type) {
88 case REDIS_REPLY_STATUS:
89 if (strcmp(reply->str, "OK") != 0) {
90 radlog(L_ERR, "rlm_redis (%s): Failed authentication: reply %s",
91 inst->xlat_name, reply->str);
94 break; /* else it's OK */
97 radlog(L_ERR, "rlm_redis (%s): Unexpected reply to AUTH",
103 if (inst->database) {
104 redisReply *reply = NULL;
106 snprintf(buffer, sizeof(buffer), "SELECT %d", inst->database);
108 reply = redisCommand(conn, buffer);
110 radlog(L_ERR, "rlm_redis (%s): Failed to run SELECT",
116 switch (reply->type) {
117 case REDIS_REPLY_STATUS:
118 if (strcmp(reply->str, "OK") != 0) {
119 radlog(L_ERR, "rlm_redis (%s): Failed SELECT %d: reply %s",
120 inst->xlat_name, inst->database,
124 break; /* else it's OK */
127 radlog(L_ERR, "rlm_redis (%s): Unexpected reply to SELECT",
133 dissocket = rad_malloc(sizeof(*dissocket));
134 memset(dissocket, 0, sizeof(*dissocket));
135 dissocket->conn = conn;
140 static size_t redis_escape_func(UNUSED REQUEST *request,
141 char *out, size_t outlen, const char *in, UNUSED void *arg)
148 * Non-printable characters get replaced with their
149 * mime-encoded equivalents.
151 if ((*in <= 32) || (*in == '\\')) {
153 * Only 3 or less bytes available.
159 snprintf(out, outlen, "=%02X", (unsigned char) in[0]);
168 * Only one byte left.
188 static size_t redis_xlat(void *instance, REQUEST *request,
189 const char *fmt, char *out, size_t freespace)
191 REDIS_INST *inst = instance;
192 REDISSOCK *dissocket;
196 char querystr[MAX_QUERY_LEN];
198 if (!radius_xlat(querystr, sizeof(querystr), fmt, request,
199 redis_escape_func, inst)) {
200 radlog(L_ERR, "rlm_redis (%s): xlat failed.",
206 dissocket = fr_connection_get(inst->pool);
208 radlog(L_ERR, "rlm_redis (%s): redis_get_socket() failed",
214 /* Query failed for some reason, release socket and return */
215 if (rlm_redis_query(&dissocket, inst, querystr) < 0) {
219 switch (dissocket->reply->type) {
220 case REDIS_REPLY_INTEGER:
222 snprintf(buffer_ptr, sizeof(buffer), "%lld",
223 dissocket->reply->integer);
225 ret = strlen(buffer_ptr);
228 case REDIS_REPLY_STATUS:
229 case REDIS_REPLY_STRING:
230 buffer_ptr = dissocket->reply->str;
231 ret = dissocket->reply->len;
239 if ((ret >= freespace) || (buffer_ptr == NULL)) {
240 RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
246 strlcpy(out, buffer_ptr, freespace);
249 rlm_redis_finish_query(dissocket);
250 fr_connection_release(inst->pool, dissocket);
256 * Only free memory we allocated. The strings allocated via
257 * cf_section_parse() do not need to be freed.
259 static int redis_detach(void *instance)
261 REDIS_INST *inst = instance;
263 fr_connection_pool_delete(inst->pool);
265 if (inst->xlat_name) {
266 xlat_unregister(inst->xlat_name, redis_xlat, instance);
273 * Query the redis database
275 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst, char *query)
277 REDISSOCK *dissocket;
279 if (!query || !*query || !inst || !dissocket_p) {
283 dissocket = *dissocket_p;
285 DEBUG2("executing query %s", query);
286 dissocket->reply = redisCommand(dissocket->conn, query);
288 if (!dissocket->reply) {
289 radlog(L_ERR, "rlm_redis: (%s) REDIS error: %s",
290 inst->xlat_name, dissocket->conn->errstr);
292 dissocket = fr_connection_reconnect(inst->pool, dissocket);
299 dissocket->reply = redisCommand(dissocket->conn, query);
300 if (!dissocket->reply) {
301 radlog(L_ERR, "rlm_redis (%s): failed after re-connect",
303 fr_connection_del(inst->pool, dissocket);
307 *dissocket_p = dissocket;
310 if (dissocket->reply->type == REDIS_REPLY_ERROR) {
311 radlog(L_ERR, "rlm_redis (%s): query failed, %s",
312 inst->xlat_name, query);
320 * Clear the redis reply object if any
322 int rlm_redis_finish_query(REDISSOCK *dissocket)
324 if (!dissocket || !dissocket->reply) {
328 freeReplyObject(dissocket->reply);
329 dissocket->reply = NULL;
333 static int redis_instantiate(CONF_SECTION *conf, void **instance)
336 const char *xlat_name;
339 * Set up a storage area for instance data
341 *instance = inst = talloc_zero(conf, REDIS_INST);
342 if (!inst) return -1;
345 * If the configuration parameters can't be parsed, then
348 if (cf_section_parse(conf, inst, module_config) < 0) {
352 xlat_name = cf_section_name2(conf);
355 xlat_name = cf_section_name1(conf);
357 xlat_register(inst->xlat_name, redis_xlat, inst);
359 inst->pool = fr_connection_pool_init(conf, inst,
360 redis_create_conn, NULL,
366 inst->redis_query = rlm_redis_query;
367 inst->redis_finish_query = rlm_redis_finish_query;
368 inst->redis_escape_func = redis_escape_func;
373 module_t rlm_redis = {
376 RLM_TYPE_THREAD_SAFE, /* type */
377 redis_instantiate, /* instantiation */
378 redis_detach, /* detach */
380 NULL, /* authentication */
381 NULL, /* authorization */
382 NULL, /* preaccounting */
383 NULL, /* accounting */
384 NULL, /* checksimul */
385 NULL, /* pre-proxy */
386 NULL, /* post-proxy */