]> jfr.im git - solanum.git/blobdiff - authd/provider.c
authd: don't exit() on OPM duplicate scanner errors, just ignore it
[solanum.git] / authd / provider.c
index 8fca1a0845154895f477c78b8558987e869fad5d..7def5714a400139cde552e5a81cce42415879a14 100644 (file)
@@ -48,6 +48,7 @@
 
 #include "stdinc.h"
 #include "rb_dictionary.h"
+#include "rb_lib.h"
 #include "authd.h"
 #include "provider.h"
 #include "notice.h"
 static EVH provider_timeout_event;
 
 rb_dictionary *auth_clients;
-rb_dictionary *auth_providers; /* Referenced by name */
+rb_dlink_list auth_providers;
 
 static rb_dlink_list free_pids;
-static uint32_t pid;
+static uint32_t allocated_pids;
 static struct ev_entry *timeout_ev;
 
 /* Initalise all providers */
@@ -66,7 +67,6 @@ void
 init_providers(void)
 {
        auth_clients = rb_dictionary_create("pending auth clients", rb_uint32cmp);
-       auth_providers = rb_dictionary_create("auth providers", strcmp);
        timeout_ev = rb_event_addish("provider_timeout_event", provider_timeout_event, NULL, 1);
 
        load_provider(&rdns_provider);
@@ -79,27 +79,33 @@ init_providers(void)
 void
 destroy_providers(void)
 {
-       rb_dlink_node *ptr;
+       rb_dlink_node *ptr, *nptr;
        rb_dictionary_iter iter;
        struct auth_client *auth;
-       struct auth_provider *provider;
 
        /* Cancel outstanding connections */
        RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
        {
+               auth_client_ref(auth);
+
                /* TBD - is this the right thing? */
                reject_client(auth, UINT32_MAX, "destroy",
                        "Authentication system is down... try reconnecting in a few seconds");
+
+               auth_client_unref(auth);
        }
 
-       RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
+       RB_DLINK_FOREACH_SAFE(ptr, nptr, auth_providers.head)
        {
+               struct auth_provider *provider = ptr->data;
+
                if(provider->destroy)
                        provider->destroy();
+
+               rb_dlinkDelete(ptr, &auth_providers);
        }
 
        rb_dictionary_destroy(auth_clients, NULL, NULL);
-       rb_dictionary_destroy(auth_providers, NULL, NULL);
        rb_event_delete(timeout_ev);
 }
 
@@ -116,15 +122,13 @@ load_provider(struct auth_provider *provider)
        }
        else
        {
-               if(pid == UINT32_MAX)
+               if(allocated_pids == MAX_PROVIDERS || allocated_pids == UINT32_MAX)
                {
-                       /* If this happens, well, I don't know what to say. Probably a bug.
-                        * In any case, UINT32_MAX is a special sentinel. */
                        warn_opers(L_WARN, "Cannot load additional provider, max reached!");
                        return;
                }
 
-               provider->id = pid++;
+               provider->id = allocated_pids++;
        }
 
        if(provider->opt_handlers != NULL)
@@ -141,7 +145,7 @@ load_provider(struct auth_provider *provider)
        if(provider->init != NULL)
                provider->init();
 
-       rb_dictionary_add(auth_providers, provider->name, provider);
+       rb_dlinkAdd(provider, &provider->node, &auth_providers);
 }
 
 void
@@ -161,71 +165,77 @@ unload_provider(struct auth_provider *provider)
        if(provider->destroy != NULL)
                provider->destroy();
 
-       rb_dictionary_delete(auth_providers, provider->name);
+       rb_dlinkDelete(&provider->node, &auth_providers);
 
        /* Reclaim ID */
        rb_dlinkAddAlloc(RB_UINT_TO_POINTER(provider->id), &free_pids);
 }
 
+void
+auth_client_free(struct auth_client *auth)
+{
+       rb_dictionary_delete(auth_clients, RB_UINT_TO_POINTER(auth->cid));
+       rb_free(auth->data);
+       rb_free(auth);
+}
 
-/* Cancel outstanding providers for a client (if any) and free the auth instance
- * WARNING: do not use auth instance after calling! */
+/* Cancel outstanding providers for a client (if any). */
 void
 cancel_providers(struct auth_client *auth)
 {
-       if(auth->refcount > 0)
+       if(auth->providers_cancelled)
+               return;
+
+       auth->providers_cancelled = true;
+
+       if(auth->providers_active > 0)
        {
-               rb_dictionary_iter iter;
-               struct auth_provider *provider;
+               rb_dlink_node *ptr;
 
-               RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
+               RB_DLINK_FOREACH(ptr, auth_providers.head)
                {
+                       struct auth_provider *provider = ptr->data;
+
                        if(provider->cancel != NULL && is_provider_running(auth, provider->id))
                                /* Cancel if required */
                                provider->cancel(auth);
                }
        }
-
-       rb_dictionary_delete(auth_clients, RB_UINT_TO_POINTER(auth->cid));
-       rb_free(auth->data);
-       rb_free(auth);
 }
 
-/* Provider is done
- * WARNING: do not use auth instance after calling! */
+/* Provider is done */
 void
 provider_done(struct auth_client *auth, uint32_t id)
 {
-       rb_dictionary_iter iter;
-       struct auth_provider *provider;
+       rb_dlink_node *ptr;
 
        lrb_assert(is_provider_running(auth, id));
        lrb_assert(id != UINT32_MAX);
-       lrb_assert(id <= pid);
+       lrb_assert(id < allocated_pids);
 
        set_provider_done(auth, id);
 
-       if(auth->refcount == 0 && !auth->providers_starting)
+       if(auth->providers_active == 0 && !auth->providers_starting)
        {
                /* All done */
-               accept_client(auth, UINT32_MAX);
+               accept_client(auth);
                return;
        }
 
-       RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
+       RB_DLINK_FOREACH(ptr, auth_providers.head)
        {
+               struct auth_provider *provider = ptr->data;
+
                if(provider->completed != NULL && is_provider_running(auth, provider->id))
                        /* Notify pending clients who asked for it */
                        provider->completed(auth, id);
        }
 }
 
-/* Reject a client and cancel any outstanding providers
- * WARNING: do not use auth instance after calling! */
+/* Reject a client and cancel any outstanding providers */
 void
 reject_client(struct auth_client *auth, uint32_t id, const char *data, const char *fmt, ...)
 {
-       unsigned int refcount = auth->refcount;
        char buf[BUFSIZE];
        va_list args;
 
@@ -238,7 +248,7 @@ reject_client(struct auth_client *auth, uint32_t id, const char *data, const cha
         * --Elizafox
         */
        rb_helper_write(authd_helper, "R %x %c %s %s %s :%s",
-               auth->cid, auth->data[id].provider->letter,
+               auth->cid, id != UINT32_MAX ? auth->data[id].provider->letter : '*',
                auth->username, auth->hostname,
                data == NULL ? "*" : data, buf);
 
@@ -248,18 +258,11 @@ reject_client(struct auth_client *auth, uint32_t id, const char *data, const cha
        cancel_providers(auth);
 }
 
-/* Accept a client and cancel outstanding providers if any
- * WARNING: do not use auth instance after calling! */
+/* Accept a client and cancel outstanding providers if any */
 void
-accept_client(struct auth_client *auth, uint32_t id)
+accept_client(struct auth_client *auth)
 {
-       unsigned int refcount = auth->refcount;
-
        rb_helper_write(authd_helper, "A %x %s %s", auth->cid, auth->username, auth->hostname);
-
-       if(id != UINT32_MAX)
-               set_provider_done(auth, id);
-
        cancel_providers(auth);
 }
 
@@ -267,21 +270,22 @@ accept_client(struct auth_client *auth, uint32_t id)
 static void
 start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port)
 {
-       struct auth_provider *provider;
-       struct auth_client *auth = rb_malloc(sizeof(struct auth_client));
-       long lcid = strtol(cid, NULL, 16);
-       rb_dictionary_iter iter;
+       struct auth_client *auth;
+       unsigned long long lcid = strtoull(cid, NULL, 16);
+       rb_dlink_node *ptr;
 
-       if(lcid >= UINT32_MAX)
+       if(lcid == 0 || lcid > UINT32_MAX)
                return;
 
+       auth = rb_malloc(sizeof(struct auth_client));
+       auth_client_ref(auth);
        auth->cid = (uint32_t)lcid;
 
        if(rb_dictionary_find(auth_clients, RB_UINT_TO_POINTER(auth->cid)) == NULL)
                rb_dictionary_add(auth_clients, RB_UINT_TO_POINTER(auth->cid), auth);
        else
        {
-               warn_opers(L_CRIT, "provider: duplicate client added via start_auth: %x", auth->cid);
+               warn_opers(L_CRIT, "provider: duplicate client added via start_auth: %s", cid);
                exit(EX_PROVIDER_ERROR);
        }
 
@@ -298,12 +302,13 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_
        rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
        rb_strlcpy(auth->username, "*", sizeof(auth->username));
 
-       auth->data = rb_malloc(rb_dictionary_size(auth_providers) *
-                       sizeof(struct auth_client_data));
+       auth->data = rb_malloc(allocated_pids * sizeof(struct auth_client_data));
 
        auth->providers_starting = true;
-       RB_DICTIONARY_FOREACH(provider, &iter, auth_providers)
+       RB_DLINK_FOREACH(ptr, auth_providers.head)
        {
+               struct auth_provider *provider = ptr->data;
+
                auth->data[provider->id].provider = provider;
 
                lrb_assert(provider->start != NULL);
@@ -311,13 +316,19 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_
                /* Execute providers */
                if(!provider->start(auth))
                        /* Rejected immediately */
-                       return;
+                       goto done;
+
+               if(auth->providers_cancelled)
+                       break;
        }
        auth->providers_starting = false;
 
        /* If no providers are running, accept the client */
-       if(auth->refcount == 0)
-               accept_client(auth, UINT32_MAX);
+       if(auth->providers_active == 0)
+               accept_client(auth);
+
+done:
+       auth_client_unref(auth);
 }
 
 /* Callback for the initiation */
@@ -337,7 +348,7 @@ void
 handle_cancel_connection(int parc, char *parv[])
 {
        struct auth_client *auth;
-       long lcid;
+       unsigned long long lcid;
 
        if(parc < 2)
        {
@@ -345,9 +356,10 @@ handle_cancel_connection(int parc, char *parv[])
                exit(EX_PROVIDER_ERROR);
        }
 
-       if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX)
+       lcid = strtoull(parv[1], NULL, 16);
+       if(lcid == 0 || lcid > UINT32_MAX)
        {
-               warn_opers(L_CRIT, "provider: got a request to cancel a connection that can't exist: %lx", lcid);
+               warn_opers(L_CRIT, "provider: got a request to cancel a connection that can't exist: %s", parv[1]);
                exit(EX_PROVIDER_ERROR);
        }
 
@@ -358,7 +370,9 @@ handle_cancel_connection(int parc, char *parv[])
                return;
        }
 
+       auth_client_ref(auth);
        cancel_providers(auth);
+       auth_client_unref(auth);
 }
 
 static void
@@ -370,11 +384,13 @@ provider_timeout_event(void *notused __unused)
 
        RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
        {
-               rb_dictionary_iter iter2;
-               struct auth_provider *provider;
+               rb_dlink_node *ptr;
 
-               RB_DICTIONARY_FOREACH(provider, &iter2, auth_providers)
+               auth_client_ref(auth);
+
+               RB_DLINK_FOREACH(ptr, auth_providers.head)
                {
+                       struct auth_provider *provider = ptr->data;
                        const time_t timeout = get_provider_timeout(auth, provider->id);
 
                        if(is_provider_running(auth, provider->id) && provider->timeout != NULL &&
@@ -383,5 +399,7 @@ provider_timeout_event(void *notused __unused)
                                provider->timeout(auth);
                        }
                }
+
+               auth_client_unref(auth);
        }
 }