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 ssize_t redis_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
142 REDIS_INST *inst = instance;
143 REDISSOCK *dissocket;
148 dissocket = fr_connection_get(inst->pool);
150 ERROR("rlm_redis (%s): redis_get_socket() failed",
156 /* Query failed for some reason, release socket and return */
157 if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
161 switch (dissocket->reply->type) {
162 case REDIS_REPLY_INTEGER:
164 snprintf(buffer_ptr, sizeof(buffer), "%lld",
165 dissocket->reply->integer);
167 ret = strlen(buffer_ptr);
170 case REDIS_REPLY_STATUS:
171 case REDIS_REPLY_STRING:
172 buffer_ptr = dissocket->reply->str;
173 ret = dissocket->reply->len;
181 if ((ret >= freespace) || (!buffer_ptr)) {
182 RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
188 strlcpy(out, buffer_ptr, freespace);
191 rlm_redis_finish_query(dissocket);
192 fr_connection_release(inst->pool, dissocket);
198 * Only free memory we allocated. The strings allocated via
199 * cf_section_parse() do not need to be freed.
201 static int mod_detach(void *instance)
203 REDIS_INST *inst = instance;
205 fr_connection_pool_delete(inst->pool);
211 * Query the redis database
213 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
214 char const *query, REQUEST *request)
216 REDISSOCK *dissocket;
218 char *argv[MAX_REDIS_ARGS];
219 char argv_buf[MAX_QUERY_LEN];
221 if (!query || !*query || !inst || !dissocket_p) {
225 argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, false,
226 sizeof(argv_buf), argv_buf);
230 dissocket = *dissocket_p;
232 DEBUG2("executing %s ...", argv[0]);
233 dissocket->reply = redisCommandArgv(dissocket->conn, argc, (char const **)(void **)argv, NULL);
234 if (!dissocket->reply) {
235 RERROR("%s", dissocket->conn->errstr);
237 dissocket = fr_connection_reconnect(inst->pool, dissocket);
244 dissocket->reply = redisCommand(dissocket->conn, query);
245 if (!dissocket->reply) {
246 RERROR("Failed after re-connect");
247 fr_connection_del(inst->pool, dissocket);
251 *dissocket_p = dissocket;
254 if (dissocket->reply->type == REDIS_REPLY_ERROR) {
255 RERROR("Query failed, %s", query);
263 * Clear the redis reply object if any
265 int rlm_redis_finish_query(REDISSOCK *dissocket)
267 if (!dissocket || !dissocket->reply) {
271 freeReplyObject(dissocket->reply);
272 dissocket->reply = NULL;
276 static int mod_instantiate(CONF_SECTION *conf, void *instance)
278 REDIS_INST *inst = instance;
280 inst->xlat_name = cf_section_name2(conf);
282 if (!inst->xlat_name)
283 inst->xlat_name = cf_section_name1(conf);
285 xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
287 inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
292 inst->redis_query = rlm_redis_query;
293 inst->redis_finish_query = rlm_redis_finish_query;
298 module_t rlm_redis = {
301 RLM_TYPE_THREAD_SAFE, /* type */
302 sizeof(REDIS_INST), /* yuck */
304 mod_instantiate, /* instantiation */
305 mod_detach, /* detach */
307 NULL, /* authentication */
308 NULL, /* authorization */
309 NULL, /* preaccounting */
310 NULL, /* accounting */
311 NULL, /* checksimul */
312 NULL, /* pre-proxy */
313 NULL, /* post-proxy */