inst->xlat_name not needed for debug messages in rlm_redis
[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 ssize_t redis_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
141 {
142         REDIS_INST *inst = instance;
143         REDISSOCK *dissocket;
144         size_t ret = 0;
145         char *buffer_ptr;
146         char buffer[21];
147
148         dissocket = fr_connection_get(inst->pool);
149         if (!dissocket) {
150                 ERROR("rlm_redis (%s): redis_get_socket() failed",
151                        inst->xlat_name);
152
153                 return -1;
154         }
155
156         /* Query failed for some reason, release socket and return */
157         if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
158                 goto release;
159         }
160
161         switch (dissocket->reply->type) {
162         case REDIS_REPLY_INTEGER:
163                 buffer_ptr = buffer;
164                 snprintf(buffer_ptr, sizeof(buffer), "%lld",
165                          dissocket->reply->integer);
166
167                 ret = strlen(buffer_ptr);
168                 break;
169
170         case REDIS_REPLY_STATUS:
171         case REDIS_REPLY_STRING:
172                 buffer_ptr = dissocket->reply->str;
173                 ret = dissocket->reply->len;
174                 break;
175
176         default:
177                 buffer_ptr = NULL;
178                 break;
179         }
180
181         if ((ret >= freespace) || (!buffer_ptr)) {
182                 RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
183                        inst->xlat_name);
184                 ret = -1;
185                 goto release;
186         }
187         
188         strlcpy(out, buffer_ptr, freespace);
189
190 release:
191         rlm_redis_finish_query(dissocket);
192         fr_connection_release(inst->pool, dissocket);
193         
194         return ret;
195 }
196
197 /*
198  *      Only free memory we allocated.  The strings allocated via
199  *      cf_section_parse() do not need to be freed.
200  */
201 static int mod_detach(void *instance)
202 {
203         REDIS_INST *inst = instance;
204
205         fr_connection_pool_delete(inst->pool);
206
207         return 0;
208 }
209
210 /*
211  *      Query the redis database
212  */
213 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
214                     char const *query, REQUEST *request)
215 {
216         REDISSOCK *dissocket;
217         int argc;
218         char *argv[MAX_REDIS_ARGS];
219         char argv_buf[MAX_QUERY_LEN];
220
221         if (!query || !*query || !inst || !dissocket_p) {
222                 return -1;
223         }
224
225         argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, false,
226                                 sizeof(argv_buf), argv_buf);
227         if (argc <= 0)
228                 return -1;
229
230         dissocket = *dissocket_p;
231
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);
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");
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", 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 };