]> jfr.im git - solanum.git/blobdiff - authd/providers/rdns.c
authd: misc fixes
[solanum.git] / authd / providers / rdns.c
index 34cd1674c906abf50ab9878a96c4bcd8824aaaa1..e4f2430e3edc2824e29b996ce600a078ee7553c3 100644 (file)
 #include "rb_commio.h"
 #include "authd.h"
 #include "provider.h"
+#include "notice.h"
 #include "res.h"
 #include "dns.h"
 
-struct dns_query
+struct user_query
 {
-       rb_dlink_node node;
-
-       struct auth_client *auth;               /* Our client */
-       struct DNSQuery query;                  /* DNS query */
+       struct dns_query *query;                /* Pending DNS query */
        time_t timeout;                         /* When the request times out */
 };
 
@@ -51,177 +49,157 @@ typedef enum
        REPORT_TOOLONG,
 } dns_message;
 
-static EVH timeout_dns_queries_event;
-static void client_fail(struct dns_query *query, dns_message message);
-static void client_success(struct dns_query *query);
-static void get_dns_answer(void *userdata, struct DNSReply *reply);
+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);
 
-rb_dlink_list queries;
 static struct ev_entry *timeout_ev;
-int timeout = 30;
-
+static EVH timeout_dns_queries_event;
+static int rdns_timeout = 15;
 
-bool client_dns_init(void)
+static void
+dns_answer_callback(const char *res, bool status, query_type type, void *data)
 {
-       timeout_ev = rb_event_addish("timeout_dns_queries_event", timeout_dns_queries_event, NULL, 1);
-       return (timeout_ev != NULL);
+       struct auth_client *auth = data;
+       struct user_query *query = auth->data[PROVIDER_RDNS];
+
+       if(query == NULL || res == NULL || status == false)
+               client_fail(auth, REPORT_FAIL);
+       else if(strlen(res) > HOSTLEN)
+               client_fail(auth, REPORT_TOOLONG);
+       else
+       {
+               rb_strlcpy(auth->hostname, res, HOSTLEN + 1);
+               client_success(auth);
+       }
 }
 
-void client_dns_destroy(void)
+/* Timeout outstanding queries */
+static void
+timeout_dns_queries_event(void *notused)
 {
-       rb_dlink_node *ptr, *nptr;
-       struct dns_query *query;
+       struct auth_client *auth;
+       rb_dictionary_iter iter;
 
-       RB_DLINK_FOREACH_SAFE(ptr, nptr, queries.head)
+       RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
        {
-               client_fail(ptr->data, REPORT_FAIL);
-               rb_dlinkDelete(ptr, &queries);
-               rb_free(ptr);
-       }
+               struct user_query *query = auth->data[PROVIDER_RDNS];
 
-       rb_event_delete(timeout_ev);
+               if(query != NULL && query->timeout < rb_current_time())
+               {
+                       client_fail(auth, REPORT_FAIL);
+                       return;
+               }
+       }
 }
 
-bool client_dns_start(struct auth_client *auth)
+static void
+client_fail(struct auth_client *auth, dns_message report)
 {
-       struct dns_query *query = rb_malloc(sizeof(struct dns_query));
+       struct user_query *query = auth->data[PROVIDER_RDNS];
 
-       query->auth = auth;
-       query->timeout = rb_current_time() + timeout;
+       if(query == NULL)
+               return;
 
-       query->query.ptr = query;
-       query->query.callback = get_dns_answer;
+       rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
 
-       gethost_byaddr(&auth->c_addr, &query->query);
-       notice_client(auth, messages[REPORT_LOOKUP]);
-       set_provider(auth, PROVIDER_RDNS);
-       return true;
+       notice_client(auth->cid, messages[report]);
+       cancel_query(query->query);
+
+       rb_free(query);
+       auth->data[PROVIDER_RDNS] = NULL;
+
+       provider_done(auth, PROVIDER_RDNS);
 }
 
-void client_dns_cancel(struct auth_client *auth)
+static void
+client_success(struct auth_client *auth)
 {
-       rb_dlink_node *ptr;
+       struct user_query *query = auth->data[PROVIDER_RDNS];
 
-       /* Bah, the stupid DNS resolver code doesn't have a cancellation
-        * func... */
-       RB_DLINK_FOREACH(ptr, queries.head)
-       {
-               struct dns_query *query = ptr->data;
+       notice_client(auth->cid, messages[REPORT_FOUND]);
+       cancel_query(query->query);
 
-               if(query->auth == auth)
-               {
-                       /* This will get cleaned up later by the DNS stuff */
-                       client_fail(query, REPORT_FAIL);
-                       return;
-               }
-       }
+       rb_free(query);
+       auth->data[PROVIDER_RDNS] = NULL;
+
+       provider_done(auth, PROVIDER_RDNS);
+}
+
+static bool
+rdns_init(void)
+{
+       timeout_ev = rb_event_addish("timeout_dns_queries_event", timeout_dns_queries_event, NULL, 1);
+       return (timeout_ev != NULL);
 }
 
 static void
-get_dns_answer(void *userdata, struct DNSReply *reply)
+rdns_destroy(void)
 {
-       struct dns_query *query = userdata;
-       struct auth_client *auth = query->auth;
-       rb_dlink_node *ptr, *nptr;
-       bool fail = false;
-       dns_message response;
+       struct auth_client *auth;
+       rb_dictionary_iter iter;
 
-       if(reply == NULL || auth == NULL)
+       RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
        {
-               response = REPORT_FAIL;
-               fail = true;
-               goto cleanup;
+               if(auth->data[PROVIDER_RDNS] != NULL)
+                       client_fail(auth, REPORT_FAIL);
        }
 
-       if(!sockcmp(&auth->c_addr, &reply->addr, GET_SS_FAMILY(&auth->c_addr)))
-       {
-               response = REPORT_FAIL;
-               fail = true;
-               goto cleanup;
-       }
+       rb_event_delete(timeout_ev);
+}
 
-       if(strlen(reply->h_name) > HOSTLEN)
-       {
-               /* Ah well. */
-               response = REPORT_TOOLONG;
-               fail = true;
-               goto cleanup;
-       }
+static bool
+rdns_start(struct auth_client *auth)
+{
+       struct user_query *query = rb_malloc(sizeof(struct user_query));
 
-       rb_strlcpy(auth->hostname, reply->h_name, HOSTLEN + 1);
+       query->timeout = rb_current_time() + rdns_timeout;
 
-cleanup:
-       /* Clean us up off the pending queries list */
-       RB_DLINK_FOREACH_SAFE(ptr, nptr, queries.head)
-       {
-               struct dns_query *query_l = ptr->data;
+       auth->data[PROVIDER_RDNS] = query;
 
-               if(query == query_l)
-               {
-                       /* Found */
-                       if(fail)
-                               client_fail(query, response);
-                       else
-                               client_success(query);
-
-                       rb_dlinkDelete(ptr, &queries);
-                       rb_free(query);
-                       return;
-               }
-       }
+       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;
 }
 
-/* Timeout outstanding queries */
-static void timeout_dns_queries_event(void *notused)
+static void
+rdns_cancel(struct auth_client *auth)
 {
-       rb_dlink_node *ptr;
-
-       /* NOTE - we do not delete queries from the list from a timeout, when
-        * the query times out later it will be deleted.
-        */
-       RB_DLINK_FOREACH(ptr, queries.head)
-       {
-               struct dns_query *query = ptr->data;
+       struct user_query *query = auth->data[PROVIDER_RDNS];
 
-               if(query->auth && query->timeout < rb_current_time())
-               {
-                       client_fail(query, REPORT_FAIL);
-                       return;
-               }
-       }
+       if(query != NULL)
+               client_fail(auth, REPORT_FAIL);
 }
 
-static void client_fail(struct dns_query *query, dns_message report)
+static void
+add_conf_dns_timeout(const char *key, int parc, const char **parv)
 {
-       struct auth_client *auth = query->auth;
+       int timeout = atoi(parv[0]);
 
-       if(auth)
+       if(timeout < 0)
        {
-               rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
-               notice_client(auth, messages[report]);
-               provider_done(auth, PROVIDER_RDNS);
-               query->auth = NULL;
+               warn_opers(L_CRIT, "BUG: DNS timeout < 0 (value: %d)", timeout);
+               return;
        }
+
+       rdns_timeout = timeout;
 }
 
-static void client_success(struct dns_query *query)
+struct auth_opts_handler rdns_options[] =
 {
-       struct auth_client *auth = query->auth;
-
-       if(auth)
-       {
-               notice_client(auth, messages[REPORT_FOUND]);
-               provider_done(auth, PROVIDER_RDNS);
-               query->auth = NULL;
-       }
-}
+       { "rdns_timeout", 1, add_conf_dns_timeout },
+       { NULL, 0, NULL },
+};
 
 struct auth_provider rdns_provider =
 {
        .id = PROVIDER_RDNS,
-       .init = client_dns_init,
-       .destroy = client_dns_destroy,
-       .start = client_dns_start,
-       .cancel = client_dns_cancel,
+       .init = rdns_init,
+       .destroy = rdns_destroy,
+       .start = rdns_start,
+       .cancel = rdns_cancel,
        .completed = NULL,
+       .opt_handlers = rdns_options,
 };