]> jfr.im git - solanum.git/blobdiff - authd/providers/rdns.c
m_kline: check only the added K-line
[solanum.git] / authd / providers / rdns.c
index e5e1f6ddcd29015db0b11ff162641ce3d4a2e041..a0232f60e49c2dad64a507eccc2bd67f973c4668 100644 (file)
 #include "res.h"
 #include "dns.h"
 
+#define SELF_PID (rdns_provider.id)
+
 struct user_query
 {
        struct dns_query *query;                /* Pending DNS query */
-       time_t timeout;                         /* When the request times out */
 };
 
 /* Goinked from old s_auth.c --Elizabeth */
 static const char *messages[] =
 {
        "*** Looking up your hostname...",
-       "*** Found your hostname",
        "*** Couldn't look up your hostname",
        "*** Your hostname is too long, ignoring hostname",
 };
@@ -44,7 +44,6 @@ static const char *messages[] =
 typedef enum
 {
        REPORT_LOOKUP,
-       REPORT_FOUND,
        REPORT_FAIL,
        REPORT_TOOLONG,
 } dns_message;
@@ -53,17 +52,14 @@ static void client_fail(struct auth_client *auth, dns_message message);
 static void client_success(struct auth_client *auth);
 static void dns_answer_callback(const char *res, bool status, query_type type, void *data);
 
-static struct ev_entry *timeout_ev;
-static EVH timeout_dns_queries_event;
-static int rdns_timeout = 15;
+static int rdns_timeout = RDNS_TIMEOUT_DEFAULT;
 
 static void
 dns_answer_callback(const char *res, bool status, query_type type, void *data)
 {
        struct auth_client *auth = data;
-       struct user_query *query = auth->data[PROVIDER_RDNS];
 
-       if(query == NULL || res == NULL || status == false)
+       if(res == NULL || status == false)
                client_fail(auth, REPORT_FAIL);
        else if(strlen(res) > HOSTLEN)
                client_fail(auth, REPORT_TOOLONG);
@@ -74,32 +70,12 @@ dns_answer_callback(const char *res, bool status, query_type type, void *data)
        }
 }
 
-/* Timeout outstanding queries */
-static void
-timeout_dns_queries_event(void *notused)
-{
-       struct auth_client *auth;
-       rb_dictionary_iter iter;
-
-       RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
-       {
-               struct user_query *query = auth->data[PROVIDER_RDNS];
-
-               if(query != NULL && query->timeout < rb_current_time())
-               {
-                       client_fail(auth, REPORT_FAIL);
-                       return;
-               }
-       }
-}
-
 static void
 client_fail(struct auth_client *auth, dns_message report)
 {
-       struct user_query *query = auth->data[PROVIDER_RDNS];
+       struct user_query *query = get_provider_data(auth, SELF_PID);
 
-       if(query == NULL)
-               return;
+       lrb_assert(query != NULL);
 
        rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
 
@@ -107,30 +83,31 @@ client_fail(struct auth_client *auth, dns_message report)
        cancel_query(query->query);
 
        rb_free(query);
-       auth->data[PROVIDER_RDNS] = NULL;
 
-       provider_done(auth, PROVIDER_RDNS);
+       set_provider_data(auth, SELF_PID, NULL);
+       set_provider_timeout_absolute(auth, SELF_PID, 0);
+       provider_done(auth, SELF_PID);
+
+       auth_client_unref(auth);
 }
 
 static void
 client_success(struct auth_client *auth)
 {
-       struct user_query *query = auth->data[PROVIDER_RDNS];
+       struct user_query *query = get_provider_data(auth, SELF_PID);
+
+       lrb_assert(query != NULL);
 
-       notice_client(auth->cid, messages[REPORT_FOUND]);
+       notice_client(auth->cid, "*** Found your hostname: %s", auth->hostname);
        cancel_query(query->query);
 
        rb_free(query);
-       auth->data[PROVIDER_RDNS] = NULL;
 
-       provider_done(auth, PROVIDER_RDNS);
-}
+       set_provider_data(auth, SELF_PID, NULL);
+       set_provider_timeout_absolute(auth, SELF_PID, 0);
+       provider_done(auth, SELF_PID);
 
-static bool
-rdns_init(void)
-{
-       timeout_ev = rb_event_addish("timeout_dns_queries_event", timeout_dns_queries_event, NULL, 1);
-       return (timeout_ev != NULL);
+       auth_client_unref(auth);
 }
 
 static void
@@ -141,11 +118,10 @@ rdns_destroy(void)
 
        RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
        {
-               if(auth->data[PROVIDER_RDNS] != NULL)
+               if(get_provider_data(auth, SELF_PID) != NULL)
                        client_fail(auth, REPORT_FAIL);
+               /* auth is now invalid as we have no reference */
        }
-
-       rb_event_delete(timeout_ev);
 }
 
 static bool
@@ -153,21 +129,21 @@ rdns_start(struct auth_client *auth)
 {
        struct user_query *query = rb_malloc(sizeof(struct user_query));
 
-       query->timeout = rb_current_time() + rdns_timeout;
+       auth_client_ref(auth);
 
-       auth->data[PROVIDER_RDNS] = query;
+       set_provider_data(auth, SELF_PID, query);
+       set_provider_timeout_relative(auth, SELF_PID, rdns_timeout);
 
        query->query = lookup_hostname(auth->c_ip, dns_answer_callback, auth);
 
        notice_client(auth->cid, messages[REPORT_LOOKUP]);
-       set_provider_on(auth, PROVIDER_RDNS);
        return true;
 }
 
 static void
 rdns_cancel(struct auth_client *auth)
 {
-       struct user_query *query = auth->data[PROVIDER_RDNS];
+       struct user_query *query = get_provider_data(auth, SELF_PID);
 
        if(query != NULL)
                client_fail(auth, REPORT_FAIL);
@@ -180,8 +156,8 @@ add_conf_dns_timeout(const char *key, int parc, const char **parv)
 
        if(timeout < 0)
        {
-               warn_opers(L_CRIT, "BUG: DNS timeout < 0 (value: %d)", timeout);
-               return;
+               warn_opers(L_CRIT, "rDNS: DNS timeout < 0 (value: %d)", timeout);
+               exit(EX_PROVIDER_ERROR);
        }
 
        rdns_timeout = timeout;
@@ -189,17 +165,17 @@ add_conf_dns_timeout(const char *key, int parc, const char **parv)
 
 struct auth_opts_handler rdns_options[] =
 {
-       { "dns_timeout", 1, add_conf_dns_timeout },
+       { "rdns_timeout", 1, add_conf_dns_timeout },
        { NULL, 0, NULL },
 };
 
 struct auth_provider rdns_provider =
 {
-       .id = PROVIDER_RDNS,
-       .init = rdns_init,
+       .name = "rdns",
+       .letter = 'R',
        .destroy = rdns_destroy,
        .start = rdns_start,
        .cancel = rdns_cancel,
-       .completed = NULL,
+       .timeout = rdns_cancel,
        .opt_handlers = rdns_options,
 };