Use correct macros for log messages with WARNING: ERROR: DEBUG: embedded in the forma...
[freeradius.git] / src / modules / rlm_redis / rlm_redis.c
1 /*
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.
5  *
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.
10  *
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
14  */
15
16 /**
17  * $Id$
18  * @file rlm_redis.c
19  * @brief Driver for the REDIS noSQL key value stores.
20  *
21  * @copyright 2000,2006  The FreeRADIUS server project
22  * @copyright 2011  TekSavvy Solutions <gabe@teksavvy.com>
23  */
24
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29
30 #include "rlm_redis.h"
31
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},
41
42         { NULL, -1, 0, NULL, NULL} /* end the list */
43 };
44
45 static int mod_conn_delete(UNUSED void *instance, void *handle)
46 {
47         REDISSOCK *dissocket = handle;
48
49         redisFree(dissocket->conn);
50
51         if (dissocket->reply) {
52                 freeReplyObject(dissocket->reply);
53                 dissocket->reply = NULL;
54         }
55
56         talloc_free(dissocket);
57         return 1;
58 }
59
60 static void *mod_conn_create(void *ctx)
61 {
62         REDIS_INST *inst = ctx;
63         REDISSOCK *dissocket = NULL;
64         redisContext *conn;
65         char buffer[1024];
66
67         conn = redisConnect(inst->hostname, inst->port);
68         if (conn->err) return NULL;
69
70         if (inst->password) {
71                 redisReply *reply = NULL;
72
73                 snprintf(buffer, sizeof(buffer), "AUTH %s", inst->password);
74
75                 reply = redisCommand(conn, buffer);
76                 if (!reply) {
77                         ERROR("rlm_redis (%s): Failed to run AUTH",
78                                inst->xlat_name);
79                 do_close:
80                         if (reply) freeReplyObject(reply);
81                         redisFree(conn);
82                         return NULL;
83                 }
84
85
86                 switch (reply->type) {
87                 case REDIS_REPLY_STATUS:
88                         if (strcmp(reply->str, "OK") != 0) {
89                                 ERROR("rlm_redis (%s): Failed authentication: reply %s",
90                                        inst->xlat_name, reply->str);
91                                 goto do_close;
92                         }
93                         break;  /* else it's OK */
94
95                 default:
96                         ERROR("rlm_redis (%s): Unexpected reply to AUTH",
97                                inst->xlat_name);
98                         goto do_close;
99                 }
100         }
101
102         if (inst->database) {
103                 redisReply *reply = NULL;
104
105                 snprintf(buffer, sizeof(buffer), "SELECT %d", inst->database);
106
107                 reply = redisCommand(conn, buffer);
108                 if (!reply) {
109                         ERROR("rlm_redis (%s): Failed to run SELECT",
110                                inst->xlat_name);
111                         goto do_close;
112                 }
113
114
115                 switch (reply->type) {
116                 case REDIS_REPLY_STATUS:
117                         if (strcmp(reply->str, "OK") != 0) {
118                                 ERROR("rlm_redis (%s): Failed SELECT %d: reply %s",
119                                        inst->xlat_name, inst->database,
120                                        reply->str);
121                                 goto do_close;
122                         }
123                         break;  /* else it's OK */
124
125                 default:
126                         ERROR("rlm_redis (%s): Unexpected reply to SELECT",
127                                inst->xlat_name);
128                         goto do_close;
129                 }
130         }
131
132         dissocket = talloc_zero(inst, REDISSOCK);
133         dissocket->conn = conn;
134
135         return dissocket;
136 }
137
138 static size_t redis_xlat(void *instance, REQUEST *request,
139                       char const *fmt, char *out, size_t freespace)
140 {
141         REDIS_INST *inst = instance;
142         REDISSOCK *dissocket;
143         size_t ret = 0;
144         char *buffer_ptr;
145         char buffer[21];
146
147         dissocket = fr_connection_get(inst->pool);
148         if (!dissocket) {
149                 ERROR("rlm_redis (%s): redis_get_socket() failed",
150                        inst->xlat_name);
151
152                 return 0;
153         }
154
155         /* Query failed for some reason, release socket and return */
156         if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
157                 goto release;
158         }
159
160         switch (dissocket->reply->type) {
161         case REDIS_REPLY_INTEGER:
162                 buffer_ptr = buffer;
163                 snprintf(buffer_ptr, sizeof(buffer), "%lld",
164                          dissocket->reply->integer);
165
166                 ret = strlen(buffer_ptr);
167                 break;
168
169         case REDIS_REPLY_STATUS:
170         case REDIS_REPLY_STRING:
171                 buffer_ptr = dissocket->reply->str;
172                 ret = dissocket->reply->len;
173                 break;
174
175         default:
176                 buffer_ptr = NULL;
177                 break;
178         }
179
180         if ((ret >= freespace) || (!buffer_ptr)) {
181                 RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
182                        inst->xlat_name);
183                 ret = 0;
184                 goto release;
185         }
186         
187         strlcpy(out, buffer_ptr, freespace);
188
189 release:
190         rlm_redis_finish_query(dissocket);
191         fr_connection_release(inst->pool, dissocket);
192         
193         return ret;
194 }
195
196 /*
197  *      Only free memory we allocated.  The strings allocated via
198  *      cf_section_parse() do not need to be freed.
199  */
200 static int mod_detach(void *instance)
201 {
202         REDIS_INST *inst = instance;
203
204         fr_connection_pool_delete(inst->pool);
205
206         return 0;
207 }
208
209 /*
210  *      Query the redis database
211  */
212 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
213                     char const *query, REQUEST *request)
214 {
215         REDISSOCK *dissocket;
216         int argc;
217         char const *argv[MAX_REDIS_ARGS];
218         char argv_buf[MAX_QUERY_LEN];
219
220         if (!query || !*query || !inst || !dissocket_p) {
221                 return -1;
222         }
223
224         argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, 0,
225                                 sizeof(argv_buf), argv_buf);
226         if (argc <= 0)
227                 return -1;
228
229         dissocket = *dissocket_p;
230
231         DEBUG2("executing %s ...", argv[0]);
232         dissocket->reply = redisCommandArgv(dissocket->conn, argc, argv, NULL);
233
234         if (!dissocket->reply) {
235                 RERROR("%s", dissocket->conn->errstr);
236
237                 dissocket = fr_connection_reconnect(inst->pool, dissocket);
238                 if (!dissocket) {
239                 error:
240                         *dissocket_p = NULL;
241                         return -1;
242                 }
243
244                 dissocket->reply = redisCommand(dissocket->conn, query);
245                 if (!dissocket->reply) {
246                         RERROR("Failed after re-connect", inst->xlat_name);
247                         fr_connection_del(inst->pool, dissocket);
248                         goto error;
249                 }
250
251                 *dissocket_p = dissocket;
252         }
253
254         if (dissocket->reply->type == REDIS_REPLY_ERROR) {
255                 RERROR("Query failed, %s", inst->xlat_name, query);
256                 return -1;
257         }
258
259         return 0;
260 }
261
262 /*
263  *      Clear the redis reply object if any
264  */
265 int rlm_redis_finish_query(REDISSOCK *dissocket)
266 {
267         if (!dissocket || !dissocket->reply) {
268                 return -1;
269         }
270
271         freeReplyObject(dissocket->reply);
272         dissocket->reply = NULL;
273         return 0;
274 }
275
276 static int mod_instantiate(CONF_SECTION *conf, void *instance)
277 {
278         REDIS_INST *inst = instance;
279
280         inst->xlat_name = cf_section_name2(conf);
281
282         if (!inst->xlat_name)
283                 inst->xlat_name = cf_section_name1(conf);
284
285         xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
286
287         inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
288         if (!inst->pool) {
289                 return -1;
290         }
291
292         inst->redis_query = rlm_redis_query;
293         inst->redis_finish_query = rlm_redis_finish_query;
294
295         return 0;
296 }
297
298 module_t rlm_redis = {
299         RLM_MODULE_INIT,
300         "redis",
301         RLM_TYPE_THREAD_SAFE, /* type */
302         sizeof(REDIS_INST),     /* yuck */
303         module_config,
304         mod_instantiate, /* instantiation */
305         mod_detach, /* detach */
306         {
307                 NULL, /* authentication */
308                 NULL, /* authorization */
309                 NULL, /* preaccounting */
310                 NULL, /* accounting */
311                 NULL, /* checksimul */
312                 NULL, /* pre-proxy */
313                 NULL, /* post-proxy */
314                 NULL /* post-auth */
315         },
316 };