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", PW_TYPE_STRING_PTR,
34 offsetof(REDIS_INST, hostname), NULL, "127.0.0.1"},
35 { "port", PW_TYPE_INTEGER,
36 offsetof(REDIS_INST, port), NULL, "6379"},
37 { "database", PW_TYPE_INTEGER,
38 offsetof(REDIS_INST, database), NULL, "0"},
39 { "password", PW_TYPE_STRING_PTR,
40 offsetof(REDIS_INST, password), NULL, NULL},
42 { NULL, -1, 0, NULL, NULL} /* end the list */
45 static int mod_conn_delete(UNUSED void *ctx, void *conn)
47 REDISSOCK *dissocket = conn;
49 redisFree(dissocket->conn);
51 if (dissocket->reply) {
52 freeReplyObject(dissocket->reply);
53 dissocket->reply = NULL;
56 talloc_free(dissocket);
60 static void *mod_conn_create(void *ctx)
62 REDIS_INST *inst = ctx;
63 REDISSOCK *dissocket = NULL;
67 conn = redisConnect(inst->hostname, inst->port);
68 if (conn->err) return NULL;
71 redisReply *reply = NULL;
73 snprintf(buffer, sizeof(buffer), "AUTH %s", inst->password);
75 reply = redisCommand(conn, buffer);
77 radlog(L_ERR, "rlm_redis (%s): Failed to run AUTH",
80 if (reply) freeReplyObject(reply);
86 switch (reply->type) {
87 case REDIS_REPLY_STATUS:
88 if (strcmp(reply->str, "OK") != 0) {
89 radlog(L_ERR, "rlm_redis (%s): Failed authentication: reply %s",
90 inst->xlat_name, reply->str);
93 break; /* else it's OK */
96 radlog(L_ERR, "rlm_redis (%s): Unexpected reply to AUTH",
102 if (inst->database) {
103 redisReply *reply = NULL;
105 snprintf(buffer, sizeof(buffer), "SELECT %d", inst->database);
107 reply = redisCommand(conn, buffer);
109 radlog(L_ERR, "rlm_redis (%s): Failed to run SELECT",
115 switch (reply->type) {
116 case REDIS_REPLY_STATUS:
117 if (strcmp(reply->str, "OK") != 0) {
118 radlog(L_ERR, "rlm_redis (%s): Failed SELECT %d: reply %s",
119 inst->xlat_name, inst->database,
123 break; /* else it's OK */
126 radlog(L_ERR, "rlm_redis (%s): Unexpected reply to SELECT",
132 dissocket = talloc_zero(inst, REDISSOCK);
133 dissocket->conn = conn;
138 static size_t redis_xlat(void *instance, REQUEST *request,
139 const char *fmt, char *out, size_t freespace)
141 REDIS_INST *inst = instance;
142 REDISSOCK *dissocket;
147 dissocket = fr_connection_get(inst->pool);
149 radlog(L_ERR, "rlm_redis (%s): redis_get_socket() failed",
155 /* Query failed for some reason, release socket and return */
156 if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
160 switch (dissocket->reply->type) {
161 case REDIS_REPLY_INTEGER:
163 snprintf(buffer_ptr, sizeof(buffer), "%lld",
164 dissocket->reply->integer);
166 ret = strlen(buffer_ptr);
169 case REDIS_REPLY_STATUS:
170 case REDIS_REPLY_STRING:
171 buffer_ptr = dissocket->reply->str;
172 ret = dissocket->reply->len;
180 if ((ret >= freespace) || (!buffer_ptr)) {
181 RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
187 strlcpy(out, buffer_ptr, freespace);
190 rlm_redis_finish_query(dissocket);
191 fr_connection_release(inst->pool, dissocket);
197 * Only free memory we allocated. The strings allocated via
198 * cf_section_parse() do not need to be freed.
200 static int mod_detach(void *instance)
202 REDIS_INST *inst = instance;
204 fr_connection_pool_delete(inst->pool);
210 * Query the redis database
212 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
213 const char *query, REQUEST *request)
215 REDISSOCK *dissocket;
217 const char *argv[MAX_REDIS_ARGS];
218 char argv_buf[MAX_QUERY_LEN];
220 if (!query || !*query || !inst || !dissocket_p) {
224 argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, 0,
225 sizeof(argv_buf), argv_buf);
229 dissocket = *dissocket_p;
231 DEBUG2("executing %s ...", argv[0]);
232 dissocket->reply = redisCommandArgv(dissocket->conn, argc, argv, NULL);
234 if (!dissocket->reply) {
235 radlog(L_ERR, "rlm_redis: (%s) REDIS error: %s",
236 inst->xlat_name, dissocket->conn->errstr);
238 dissocket = fr_connection_reconnect(inst->pool, dissocket);
245 dissocket->reply = redisCommand(dissocket->conn, query);
246 if (!dissocket->reply) {
247 radlog(L_ERR, "rlm_redis (%s): failed after re-connect",
249 fr_connection_del(inst->pool, dissocket);
253 *dissocket_p = dissocket;
256 if (dissocket->reply->type == REDIS_REPLY_ERROR) {
257 radlog(L_ERR, "rlm_redis (%s): query failed, %s",
258 inst->xlat_name, query);
266 * Clear the redis reply object if any
268 int rlm_redis_finish_query(REDISSOCK *dissocket)
270 if (!dissocket || !dissocket->reply) {
274 freeReplyObject(dissocket->reply);
275 dissocket->reply = NULL;
279 static int mod_instantiate(CONF_SECTION *conf, void *instance)
281 REDIS_INST *inst = instance;
283 inst->xlat_name = cf_section_name2(conf);
285 if (!inst->xlat_name)
286 inst->xlat_name = cf_section_name1(conf);
288 xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
290 inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
295 inst->redis_query = rlm_redis_query;
296 inst->redis_finish_query = rlm_redis_finish_query;
301 module_t rlm_redis = {
304 RLM_TYPE_THREAD_SAFE, /* type */
305 sizeof(REDIS_INST), /* yuck */
307 mod_instantiate, /* instantiation */
308 mod_detach, /* detach */
310 NULL, /* authentication */
311 NULL, /* authorization */
312 NULL, /* preaccounting */
313 NULL, /* accounting */
314 NULL, /* checksimul */
315 NULL, /* pre-proxy */
316 NULL, /* post-proxy */