Pass a threadsafe ctx into fr_connection_pool create callback
[freeradius.git] / src / modules / rlm_smsotp / rlm_smsotp.c
index e9f75aa..4aecd19 100644 (file)
@@ -28,27 +28,27 @@ RCSID("$Id$")
 #include <sys/un.h>
 
 typedef struct rlm_smsotp_t {
-       char            *socket;
-       char            *challenge;
-       char            *authtype;
+       char const      *socket;
+       char const      *challenge;
+       char const      *authtype;
        fr_connection_pool_t *pool;
 } rlm_smsotp_t;
 
 static const CONF_PARSER module_config[] = {
-       { "socket", PW_TYPE_STRING_PTR,
-         offsetof(rlm_smsotp_t, socket),
-         NULL, "/var/run/smsotp_socket" },
-       { "challenge_message", PW_TYPE_STRING_PTR,
-         offsetof(rlm_smsotp_t, challenge), NULL, "Enter Mobile PIN" },
-       { "challenge_type", PW_TYPE_STRING_PTR,
-         offsetof(rlm_smsotp_t, authtype),
-         NULL, "smsotp-reply" },
+       { "socket", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_smsotp_t, socket), "/var/run/smsotp_socket" },
+       { "challenge_message", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_smsotp_t, challenge), "Enter Mobile PIN" },
+       { "challenge_type", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_smsotp_t, authtype), "smsotp-reply" },
 
        { NULL, -1, 0, NULL, NULL }             /* end the list */
 };
 
+static int _mod_conn_free(int *fdp)
+{
+       close(*fdp);
+       return 0;
+}
 
-static void *mod_conn_create(void *instance)
+static void *mod_conn_create(TALLOC_CTX *ctx, void *instance)
 {
        int fd;
        struct sockaddr_un sa;
@@ -72,22 +72,13 @@ static void *mod_conn_create(void *instance)
                return NULL;
        }
 
-       fdp = talloc_zero(instance, int);
+       fdp = talloc_zero(ctx, int);
+       talloc_set_destructor(fdp, _mod_conn_free);
        *fdp = fd;
 
        return fdp;
 }
 
-static int mod_conn_delete(UNUSED void *instance, void *handle)
-{
-       int *fdp = handle;
-
-       close(*fdp);
-       talloc_free(fdp);
-       return 0;
-}
-
-
 /*
  * Full read with logging, and close on failure.
  * Returns nread on success, 0 on EOF, -1 on other failures.
@@ -181,7 +172,7 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
        /*
         *      Initialize the socket pool.
         */
-       inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, mod_conn_delete, NULL);
+       inst->pool = fr_connection_pool_init(conf, inst, mod_conn_create, NULL, NULL, NULL);
        if (!inst->pool) {
                return -1;
        }
@@ -192,7 +183,7 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
 /*
  *     Authenticate the user with the given password.
  */
-static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
+static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
 {
        rlm_smsotp_t *inst = instance;
        VALUE_PAIR *state;
@@ -203,10 +194,7 @@ static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
        char output[1000];
 
        fdp = fr_connection_get(inst->pool);
-       if (!fdp) {
-               REDEBUG("Failed to get handle from connection pool");
-               return RLM_MODULE_FAIL;
-       }
+       if (!fdp) return RLM_MODULE_FAIL;
 
        /* Get greeting */
        bufsize = read_all(fdp, buffer, sizeof(buffer));
@@ -292,7 +280,7 @@ static rlm_rcode_t mod_authenticate(void *instance, REQUEST *request)
         *  The server will take care of sending it to the user.
         */
        request->reply->code = PW_CODE_ACCESS_CHALLENGE;
-       DEBUG("rlm_smsotp: Sending Access-Challenge.");
+       DEBUG("rlm_smsotp: Sending Access-Challenge");
 
        rcode = RLM_MODULE_HANDLED;
 
@@ -307,7 +295,7 @@ done:
  *     from the database. The authentication code only needs to check
  *     the password, the rest is done here.
  */
-static rlm_rcode_t mod_authorize(UNUSED void *instance, UNUSED REQUEST *request)
+static rlm_rcode_t CC_HINT(nonnull) mod_authorize(UNUSED void *instance, UNUSED REQUEST *request)
 {
        VALUE_PAIR *state;
        rlm_smsotp_t *inst = instance;