]> jfr.im git - solanum.git/blobdiff - authd/provider.c
m_stats: don't ratelimit with no server argument
[solanum.git] / authd / provider.c
index 3b64c55a116f39d343e5c76201d3c89f15523e4f..cf2607ada729eb44695181d2a20b0cb6b62edd71 100644 (file)
@@ -59,9 +59,32 @@ rb_dictionary *auth_clients;
 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;
 
+/* Set a provider's raw status */
+static inline void
+set_provider_status(struct auth_client *auth, uint32_t provider, provider_status_t status)
+{
+       auth->data[provider].status = status;
+}
+
+/* Set the provider as running */
+static inline void
+set_provider_running(struct auth_client *auth, uint32_t provider)
+{
+       auth->providers_active++;
+       set_provider_status(auth, provider, PROVIDER_STATUS_RUNNING);
+}
+
+/* Provider is no longer operating on this auth client */
+static inline void
+set_provider_done(struct auth_client *auth, uint32_t provider)
+{
+       set_provider_status(auth, provider, PROVIDER_STATUS_DONE);
+       auth->providers_active--;
+}
+
 /* Initalise all providers */
 void
 init_providers(void)
@@ -69,10 +92,13 @@ init_providers(void)
        auth_clients = rb_dictionary_create("pending auth clients", rb_uint32cmp);
        timeout_ev = rb_event_addish("provider_timeout_event", provider_timeout_event, NULL, 1);
 
-       load_provider(&rdns_provider);
-       load_provider(&ident_provider);
+       /* FIXME must be started before rdns/ident to receive completion notification from them */
        load_provider(&blacklist_provider);
        load_provider(&opm_provider);
+
+       /* FIXME must be started after blacklist/opm in case of early completion notifications */
+       load_provider(&rdns_provider);
+       load_provider(&ident_provider);
 }
 
 /* Terminate all providers */
@@ -82,14 +108,17 @@ destroy_providers(void)
        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_DLINK_FOREACH_SAFE(ptr, nptr, auth_providers.head)
@@ -119,15 +148,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)
@@ -187,7 +214,7 @@ cancel_providers(struct auth_client *auth)
 
        auth->providers_cancelled = true;
 
-       if(auth->refcount > 0)
+       if(auth->providers_active > 0)
        {
                rb_dlink_node *ptr;
 
@@ -202,8 +229,7 @@ cancel_providers(struct auth_client *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)
 {
@@ -211,14 +237,14 @@ provider_done(struct auth_client *auth, uint32_t id)
 
        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;
        }
 
@@ -232,12 +258,10 @@ provider_done(struct auth_client *auth, uint32_t 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;
 
@@ -250,7 +274,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);
 
@@ -260,24 +284,17 @@ 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);
 }
 
 /* Begin authenticating user */
 static void
-start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port)
+start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port, const char *protocol)
 {
        struct auth_client *auth;
        unsigned long long lcid = strtoull(cid, NULL, 16);
@@ -287,6 +304,7 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_
                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)
@@ -297,24 +315,22 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_
                exit(EX_PROVIDER_ERROR);
        }
 
+       auth->protocol = strtoull(protocol, NULL, 16);
+
        rb_strlcpy(auth->l_ip, l_ip, sizeof(auth->l_ip));
        auth->l_port = (uint16_t)atoi(l_port);  /* should be safe */
-       (void) rb_inet_pton_sock(l_ip, (struct sockaddr *)&auth->l_addr);
+       (void) rb_inet_pton_sock(l_ip, &auth->l_addr);
        SET_SS_PORT(&auth->l_addr, htons(auth->l_port));
 
        rb_strlcpy(auth->c_ip, c_ip, sizeof(auth->c_ip));
        auth->c_port = (uint16_t)atoi(c_port);
-       (void) rb_inet_pton_sock(c_ip, (struct sockaddr *)&auth->c_addr);
+       (void) rb_inet_pton_sock(c_ip, &auth->c_addr);
        SET_SS_PORT(&auth->c_addr, htons(auth->c_port));
 
        rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
        rb_strlcpy(auth->username, "*", sizeof(auth->username));
 
-       /* FIXME this is unsafe, the list being allocated needs to
-        * support the maximum PID allocated, not just the current
-        * length of auth_providers */
-       auth->data = rb_malloc(rb_dlink_list_length(&auth_providers) *
-                       sizeof(struct auth_client_data));
+       auth->data = rb_malloc(allocated_pids * sizeof(struct auth_client_data));
 
        auth->providers_starting = true;
        RB_DLINK_FOREACH(ptr, auth_providers.head)
@@ -326,9 +342,10 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_
                lrb_assert(provider->start != NULL);
 
                /* Execute providers */
+               set_provider_running(auth, provider->id);
                if(!provider->start(auth))
                        /* Rejected immediately */
-                       return;
+                       goto done;
 
                if(auth->providers_cancelled)
                        break;
@@ -336,21 +353,23 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_
        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 */
 void
 handle_new_connection(int parc, char *parv[])
 {
-       if(parc < 6)
-       {
+       if (parc < 6) {
                warn_opers(L_CRIT, "provider: received too few params for new connection (6 expected, got %d)", parc);
                exit(EX_PROVIDER_ERROR);
        }
 
-       start_auth(parv[1], parv[2], parv[3], parv[4], parv[5]);
+       start_auth(parv[1], parv[2], parv[3], parv[4], parv[5], parc > 6 ? parv[6] : "0");
 }
 
 void
@@ -379,7 +398,9 @@ handle_cancel_connection(int parc, char *parv[])
                return;
        }
 
+       auth_client_ref(auth);
        cancel_providers(auth);
+       auth_client_unref(auth);
 }
 
 static void
@@ -393,6 +414,8 @@ provider_timeout_event(void *notused __unused)
        {
                rb_dlink_node *ptr;
 
+               auth_client_ref(auth);
+
                RB_DLINK_FOREACH(ptr, auth_providers.head)
                {
                        struct auth_provider *provider = ptr->data;
@@ -404,5 +427,7 @@ provider_timeout_event(void *notused __unused)
                                provider->timeout(auth);
                        }
                }
+
+               auth_client_unref(auth);
        }
 }