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 | PW_TYPE_DEPRECATED,
34 offsetof(REDIS_INST, hostname), NULL, NULL},
35 { "server", PW_TYPE_STRING_PTR | PW_TYPE_REQUIRED,
36 offsetof(REDIS_INST, hostname), NULL, NULL},
37 { "port", PW_TYPE_INTEGER,
38 offsetof(REDIS_INST, port), NULL, "6379"},
39 { "database", PW_TYPE_INTEGER,
40 offsetof(REDIS_INST, database), NULL, "0"},
41 { "password", PW_TYPE_STRING_PTR,
42 offsetof(REDIS_INST, password), NULL, NULL},
44 { NULL, -1, 0, NULL, NULL} /* end the list */
47 static int mod_conn_delete(UNUSED void *instance, void *handle)
49 REDISSOCK *dissocket = handle;
51 redisFree(dissocket->conn);
53 if (dissocket->reply) {
54 freeReplyObject(dissocket->reply);
55 dissocket->reply = NULL;
58 talloc_free(dissocket);
62 static void *mod_conn_create(void *ctx)
64 REDIS_INST *inst = ctx;
65 REDISSOCK *dissocket = NULL;
69 conn = redisConnect(inst->hostname, inst->port);
70 if (conn->err) return NULL;
73 redisReply *reply = NULL;
75 snprintf(buffer, sizeof(buffer), "AUTH %s", inst->password);
77 reply = redisCommand(conn, buffer);
79 ERROR("rlm_redis (%s): Failed to run AUTH",
82 if (reply) freeReplyObject(reply);
88 switch (reply->type) {
89 case REDIS_REPLY_STATUS:
90 if (strcmp(reply->str, "OK") != 0) {
91 ERROR("rlm_redis (%s): Failed authentication: reply %s",
92 inst->xlat_name, reply->str);
95 break; /* else it's OK */
98 ERROR("rlm_redis (%s): Unexpected reply to AUTH",
104 if (inst->database) {
105 redisReply *reply = NULL;
107 snprintf(buffer, sizeof(buffer), "SELECT %d", inst->database);
109 reply = redisCommand(conn, buffer);
111 ERROR("rlm_redis (%s): Failed to run SELECT",
117 switch (reply->type) {
118 case REDIS_REPLY_STATUS:
119 if (strcmp(reply->str, "OK") != 0) {
120 ERROR("rlm_redis (%s): Failed SELECT %d: reply %s",
121 inst->xlat_name, inst->database,
125 break; /* else it's OK */
128 ERROR("rlm_redis (%s): Unexpected reply to SELECT",
134 dissocket = talloc_zero(inst, REDISSOCK);
135 dissocket->conn = conn;
140 static size_t redis_xlat(void *instance, REQUEST *request,
141 char const *fmt, char *out, size_t freespace)
143 REDIS_INST *inst = instance;
144 REDISSOCK *dissocket;
149 dissocket = fr_connection_get(inst->pool);
151 ERROR("rlm_redis (%s): redis_get_socket() failed",
157 /* Query failed for some reason, release socket and return */
158 if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
162 switch (dissocket->reply->type) {
163 case REDIS_REPLY_INTEGER:
165 snprintf(buffer_ptr, sizeof(buffer), "%lld",
166 dissocket->reply->integer);
168 ret = strlen(buffer_ptr);
171 case REDIS_REPLY_STATUS:
172 case REDIS_REPLY_STRING:
173 buffer_ptr = dissocket->reply->str;
174 ret = dissocket->reply->len;
182 if ((ret >= freespace) || (!buffer_ptr)) {
183 RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
189 strlcpy(out, buffer_ptr, freespace);
192 rlm_redis_finish_query(dissocket);
193 fr_connection_release(inst->pool, dissocket);
199 * Only free memory we allocated. The strings allocated via
200 * cf_section_parse() do not need to be freed.
202 static int mod_detach(void *instance)
204 REDIS_INST *inst = instance;
206 fr_connection_pool_delete(inst->pool);
212 * Query the redis database
214 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
215 char const *query, REQUEST *request)
217 REDISSOCK *dissocket;
219 char *argv[MAX_REDIS_ARGS];
220 char argv_buf[MAX_QUERY_LEN];
222 if (!query || !*query || !inst || !dissocket_p) {
226 argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, false,
227 sizeof(argv_buf), argv_buf);
231 dissocket = *dissocket_p;
233 DEBUG2("executing %s ...", argv[0]);
234 dissocket->reply = redisCommandArgv(dissocket->conn, argc, (char const **)(void **)argv, NULL);
235 if (!dissocket->reply) {
236 RERROR("%s", dissocket->conn->errstr);
238 dissocket = fr_connection_reconnect(inst->pool, dissocket);
245 dissocket->reply = redisCommand(dissocket->conn, query);
246 if (!dissocket->reply) {
247 RERROR("Failed after re-connect", inst->xlat_name);
248 fr_connection_del(inst->pool, dissocket);
252 *dissocket_p = dissocket;
255 if (dissocket->reply->type == REDIS_REPLY_ERROR) {
256 RERROR("Query failed, %s", inst->xlat_name, query);
264 * Clear the redis reply object if any
266 int rlm_redis_finish_query(REDISSOCK *dissocket)
268 if (!dissocket || !dissocket->reply) {
272 freeReplyObject(dissocket->reply);
273 dissocket->reply = NULL;
277 static int mod_instantiate(CONF_SECTION *conf, void *instance)
279 REDIS_INST *inst = instance;
281 inst->xlat_name = cf_section_name2(conf);
283 if (!inst->xlat_name)
284 inst->xlat_name = cf_section_name1(conf);
286 xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
288 inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
293 inst->redis_query = rlm_redis_query;
294 inst->redis_finish_query = rlm_redis_finish_query;
299 module_t rlm_redis = {
302 RLM_TYPE_THREAD_SAFE, /* type */
303 sizeof(REDIS_INST), /* yuck */
305 mod_instantiate, /* instantiation */
306 mod_detach, /* detach */
308 NULL, /* authentication */
309 NULL, /* authorization */
310 NULL, /* preaccounting */
311 NULL, /* accounting */
312 NULL, /* checksimul */
313 NULL, /* pre-proxy */
314 NULL, /* post-proxy */