Use consistent names for the connection functions
[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 *ctx, void *conn)
46 {
47         REDISSOCK *dissocket = conn;
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                         radlog(L_ERR, "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                                 radlog(L_ERR, "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                         radlog(L_ERR, "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                         radlog(L_ERR, "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                                 radlog(L_ERR, "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                         radlog(L_ERR, "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                       const char *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                 radlog(L_ERR, "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                     const char *query, REQUEST *request)
214 {
215         REDISSOCK *dissocket;
216         int argc;
217         const char *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                 radlog(L_ERR, "rlm_redis: (%s) REDIS error: %s",
236                        inst->xlat_name, dissocket->conn->errstr);
237
238                 dissocket = fr_connection_reconnect(inst->pool, dissocket);
239                 if (!dissocket) {
240                 error:
241                         *dissocket_p = NULL;
242                         return -1;
243                 }
244
245                 dissocket->reply = redisCommand(dissocket->conn, query);
246                 if (!dissocket->reply) {
247                         radlog(L_ERR, "rlm_redis (%s): failed after re-connect",
248                                inst->xlat_name);
249                         fr_connection_del(inst->pool, dissocket);
250                         goto error;
251                 }
252
253                 *dissocket_p = dissocket;
254         }
255
256         if (dissocket->reply->type == REDIS_REPLY_ERROR) {
257                 radlog(L_ERR, "rlm_redis (%s): query failed, %s",
258                        inst->xlat_name, query);
259                 return -1;
260         }
261
262         return 0;
263 }
264
265 /*
266  *      Clear the redis reply object if any
267  */
268 int rlm_redis_finish_query(REDISSOCK *dissocket)
269 {
270         if (!dissocket || !dissocket->reply) {
271                 return -1;
272         }
273
274         freeReplyObject(dissocket->reply);
275         dissocket->reply = NULL;
276         return 0;
277 }
278
279 static int mod_instantiate(CONF_SECTION *conf, void *instance)
280 {
281         REDIS_INST *inst = instance;
282
283         inst->xlat_name = cf_section_name2(conf);
284
285         if (!inst->xlat_name)
286                 inst->xlat_name = cf_section_name1(conf);
287
288         xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
289
290         inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
291         if (!inst->pool) {
292                 return -1;
293         }
294
295         inst->redis_query = rlm_redis_query;
296         inst->redis_finish_query = rlm_redis_finish_query;
297
298         return 0;
299 }
300
301 module_t rlm_redis = {
302         RLM_MODULE_INIT,
303         "redis",
304         RLM_TYPE_THREAD_SAFE, /* type */
305         sizeof(REDIS_INST),     /* yuck */
306         module_config,
307         mod_instantiate, /* instantiation */
308         mod_detach, /* detach */
309         {
310                 NULL, /* authentication */
311                 NULL, /* authorization */
312                 NULL, /* preaccounting */
313                 NULL, /* accounting */
314                 NULL, /* checksimul */
315                 NULL, /* pre-proxy */
316                 NULL, /* post-proxy */
317                 NULL /* post-auth */
318         },
319 };