Pass a threadsafe ctx into fr_connection_pool create callback
[freeradius.git] / src / modules / rlm_redis / rlm_redis.c
index ca748ad..f07d494 100644 (file)
@@ -21,7 +21,6 @@
  * @copyright 2000,2006  The FreeRADIUS server project
  * @copyright 2011  TekSavvy Solutions <gabe@teksavvy.com>
  */
-#include <freeradius-devel/ident.h>
 
 RCSID("$Id$")
 
@@ -31,22 +30,17 @@ RCSID("$Id$")
 #include "rlm_redis.h"
 
 static const CONF_PARSER module_config[] = {
-       { "hostname", PW_TYPE_STRING_PTR,
-         offsetof(REDIS_INST, hostname), NULL, "127.0.0.1"},
-       { "port", PW_TYPE_INTEGER,
-         offsetof(REDIS_INST, port), NULL, "6379"},
-       { "database", PW_TYPE_INTEGER,
-         offsetof(REDIS_INST, database), NULL, "0"},
-       { "password", PW_TYPE_STRING_PTR,
-         offsetof(REDIS_INST, password), NULL, NULL},
+       { "hostname", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_DEPRECATED, REDIS_INST, hostname), NULL },
+       { "server", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, REDIS_INST, hostname), NULL },
+       { "port", FR_CONF_OFFSET(PW_TYPE_SHORT, REDIS_INST, port), "6379" },
+       { "database", FR_CONF_OFFSET(PW_TYPE_INTEGER, REDIS_INST, database), "0" },
+       { "password", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, REDIS_INST, password), NULL },
 
        { NULL, -1, 0, NULL, NULL} /* end the list */
 };
 
-static int redis_delete_conn(UNUSED void *ctx, void *conn)
+static int _mod_conn_free(REDISSOCK *dissocket)
 {
-       REDISSOCK *dissocket = conn;
-
        redisFree(dissocket->conn);
 
        if (dissocket->reply) {
@@ -54,29 +48,27 @@ static int redis_delete_conn(UNUSED void *ctx, void *conn)
                dissocket->reply = NULL;
        }
 
-       free(dissocket);
-       return 1;
+       return 0;
 }
 
-static void *redis_create_conn(void *ctx)
+static void *mod_conn_create(TALLOC_CTX *ctx, void *instance)
 {
-       REDIS_INST *inst = ctx;
+       REDIS_INST *inst = instance;
        REDISSOCK *dissocket = NULL;
        redisContext *conn;
+       redisReply *reply = NULL;
        char buffer[1024];
 
        conn = redisConnect(inst->hostname, inst->port);
        if (conn->err) return NULL;
 
        if (inst->password) {
-               redisReply *reply = NULL;
-
                snprintf(buffer, sizeof(buffer), "AUTH %s", inst->password);
 
                reply = redisCommand(conn, buffer);
                if (!reply) {
-                       radlog(L_ERR, "rlm_redis (%s): Failed to run AUTH",
-                              inst->xlat_name);
+                       ERROR("rlm_redis (%s): Failed to run AUTH", inst->xlat_name);
+
                do_close:
                        if (reply) freeReplyObject(reply);
                        redisFree(conn);
@@ -87,27 +79,25 @@ static void *redis_create_conn(void *ctx)
                switch (reply->type) {
                case REDIS_REPLY_STATUS:
                        if (strcmp(reply->str, "OK") != 0) {
-                               radlog(L_ERR, "rlm_redis (%s): Failed authentication: reply %s",
+                               ERROR("rlm_redis (%s): Failed authentication: reply %s",
                                       inst->xlat_name, reply->str);
                                goto do_close;
                        }
                        break;  /* else it's OK */
 
                default:
-                       radlog(L_ERR, "rlm_redis (%s): Unexpected reply to AUTH",
+                       ERROR("rlm_redis (%s): Unexpected reply to AUTH",
                               inst->xlat_name);
                        goto do_close;
                }
        }
 
        if (inst->database) {
-               redisReply *reply = NULL;
-
                snprintf(buffer, sizeof(buffer), "SELECT %d", inst->database);
 
                reply = redisCommand(conn, buffer);
                if (!reply) {
-                       radlog(L_ERR, "rlm_redis (%s): Failed to run SELECT",
+                       ERROR("rlm_redis (%s): Failed to run SELECT",
                               inst->xlat_name);
                        goto do_close;
                }
@@ -116,7 +106,7 @@ static void *redis_create_conn(void *ctx)
                switch (reply->type) {
                case REDIS_REPLY_STATUS:
                        if (strcmp(reply->str, "OK") != 0) {
-                               radlog(L_ERR, "rlm_redis (%s): Failed SELECT %d: reply %s",
+                               ERROR("rlm_redis (%s): Failed SELECT %d: reply %s",
                                       inst->xlat_name, inst->database,
                                       reply->str);
                                goto do_close;
@@ -124,21 +114,20 @@ static void *redis_create_conn(void *ctx)
                        break;  /* else it's OK */
 
                default:
-                       radlog(L_ERR, "rlm_redis (%s): Unexpected reply to SELECT",
+                       ERROR("rlm_redis (%s): Unexpected reply to SELECT",
                               inst->xlat_name);
                        goto do_close;
                }
        }
 
-       dissocket = rad_malloc(sizeof(*dissocket));
-       memset(dissocket, 0, sizeof(*dissocket));
+       dissocket = talloc_zero(ctx, REDISSOCK);
        dissocket->conn = conn;
+       talloc_set_destructor(dissocket, _mod_conn_free);
 
        return dissocket;
 }
 
-static size_t redis_xlat(void *instance, REQUEST *request,
-                     const char *fmt, char *out, size_t freespace)
+static ssize_t redis_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
 {
        REDIS_INST *inst = instance;
        REDISSOCK *dissocket;
@@ -147,12 +136,7 @@ static size_t redis_xlat(void *instance, REQUEST *request,
        char buffer[21];
 
        dissocket = fr_connection_get(inst->pool);
-       if (!dissocket) {
-               radlog(L_ERR, "rlm_redis (%s): redis_get_socket() failed",
-                      inst->xlat_name);
-
-               return 0;
-       }
+       if (!dissocket) return -1;
 
        /* Query failed for some reason, release socket and return */
        if (rlm_redis_query(&dissocket, inst, fmt, request) < 0) {
@@ -179,19 +163,19 @@ static size_t redis_xlat(void *instance, REQUEST *request,
                break;
        }
 
-       if ((ret >= freespace) || (buffer_ptr == NULL)) {
+       if ((ret >= freespace) || (!buffer_ptr)) {
                RDEBUG("rlm_redis (%s): Can't write result, insufficient space or unsupported result\n",
                       inst->xlat_name);
-               ret = 0;
+               ret = -1;
                goto release;
        }
-       
+
        strlcpy(out, buffer_ptr, freespace);
 
 release:
        rlm_redis_finish_query(dissocket);
        fr_connection_release(inst->pool, dissocket);
-       
+
        return ret;
 }
 
@@ -199,16 +183,12 @@ release:
  *     Only free memory we allocated.  The strings allocated via
  *     cf_section_parse() do not need to be freed.
  */
-static int redis_detach(void *instance)
+static int mod_detach(void *instance)
 {
        REDIS_INST *inst = instance;
 
        fr_connection_pool_delete(inst->pool);
 
-       if (inst->xlat_name) {
-               xlat_unregister(inst->xlat_name, redis_xlat, instance);
-       }
-
        return 0;
 }
 
@@ -216,18 +196,18 @@ static int redis_detach(void *instance)
  *     Query the redis database
  */
 int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
-                   const char *query, REQUEST *request)
+                   char const *query, REQUEST *request)
 {
        REDISSOCK *dissocket;
        int argc;
-       const char *argv[MAX_REDIS_ARGS];
+       char *argv[MAX_REDIS_ARGS];
        char argv_buf[MAX_QUERY_LEN];
 
        if (!query || !*query || !inst || !dissocket_p) {
                return -1;
        }
 
-       argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, 0,
+       argc = rad_expand_xlat(request, query, MAX_REDIS_ARGS, argv, false,
                                sizeof(argv_buf), argv_buf);
        if (argc <= 0)
                return -1;
@@ -235,11 +215,9 @@ int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
        dissocket = *dissocket_p;
 
        DEBUG2("executing %s ...", argv[0]);
-       dissocket->reply = redisCommandArgv(dissocket->conn, argc, argv, NULL);
-
+       dissocket->reply = redisCommandArgv(dissocket->conn, argc, (char const **)(void **)argv, NULL);
        if (!dissocket->reply) {
-               radlog(L_ERR, "rlm_redis: (%s) REDIS error: %s",
-                      inst->xlat_name, dissocket->conn->errstr);
+               RERROR("%s", dissocket->conn->errstr);
 
                dissocket = fr_connection_reconnect(inst->pool, dissocket);
                if (!dissocket) {
@@ -250,8 +228,7 @@ int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
 
                dissocket->reply = redisCommand(dissocket->conn, query);
                if (!dissocket->reply) {
-                       radlog(L_ERR, "rlm_redis (%s): failed after re-connect",
-                              inst->xlat_name);
+                       RERROR("Failed after re-connect");
                        fr_connection_del(inst->pool, dissocket);
                        goto error;
                }
@@ -260,8 +237,7 @@ int rlm_redis_query(REDISSOCK **dissocket_p, REDIS_INST *inst,
        }
 
        if (dissocket->reply->type == REDIS_REPLY_ERROR) {
-               radlog(L_ERR, "rlm_redis (%s): query failed, %s",
-                      inst->xlat_name, query);
+               RERROR("Query failed, %s", query);
                return -1;
        }
 
@@ -282,34 +258,25 @@ int rlm_redis_finish_query(REDISSOCK *dissocket)
        return 0;
 }
 
-static int redis_instantiate(CONF_SECTION *conf, void **instance)
+static int mod_instantiate(CONF_SECTION *conf, void *instance)
 {
-       REDIS_INST *inst;
-
-       /*
-        *      Set up a storage area for instance data
-        */
-       *instance = inst = talloc_zero(conf, REDIS_INST);
-       if (!inst) return -1;
-
-       /*
-        *      If the configuration parameters can't be parsed, then
-        *      fail.
-        */
-       if (cf_section_parse(conf, inst, module_config) < 0) {
-               return -1;
+       static bool version_done;
+
+       REDIS_INST *inst = instance;
+
+       if (!version_done) {
+               version_done = true;
+
+               INFO("rlm_redis: libhiredis version: %i.%i.%i", HIREDIS_MAJOR, HIREDIS_MINOR, HIREDIS_PATCH);
        }
 
        inst->xlat_name = cf_section_name2(conf);
 
-       if (!inst->xlat_name)
-               inst->xlat_name = cf_section_name1(conf);
+       if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
 
-       xlat_register(inst->xlat_name, redis_xlat, inst);
+       xlat_register(inst->xlat_name, redis_xlat, NULL, inst); /* FIXME! */
 
-       inst->pool = fr_connection_pool_init(conf, inst,
-                                            redis_create_conn, NULL,
-                                            redis_delete_conn);
+       inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, NULL, NULL);
        if (!inst->pool) {
                return -1;
        }
@@ -324,8 +291,10 @@ module_t rlm_redis = {
        RLM_MODULE_INIT,
        "redis",
        RLM_TYPE_THREAD_SAFE, /* type */
-       redis_instantiate, /* instantiation */
-       redis_detach, /* detach */
+       sizeof(REDIS_INST),     /* yuck */
+       module_config,
+       mod_instantiate, /* instantiation */
+       mod_detach, /* detach */
        {
                NULL, /* authentication */
                NULL, /* authorization */