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