]> jfr.im git - solanum.git/blobdiff - authd/provider.c
authd: fix undefined behaviour
[solanum.git] / authd / provider.c
index 81c8dfc06cf6ee98a06101220e7c32473ff5825d..4e17793ee18bfad189a0fb1c8b1d3ccad262a71d 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/* So the basic design here is to have "authentication providers" that do
- * things like query ident and blacklists and even open proxies.
+/* The basic design here is to have "authentication providers" that do things
+ * like query ident and blacklists and even open proxies.
  *
  * Providers are registered in the auth_providers linked list. It is planned to
  * use a bitmap to store provider ID's later.
  *
- * Providers can either return failure immediately, immediate acceptance, or
- * do work in the background (calling set_provider to signal this).
+ * Providers can either return failure immediately, immediate acceptance, or do
+ * work in the background (calling set_provider to signal this).
  *
- * A dictionary is provided in auth_client for storage of provider-specific data.
+ * Provider-specific data for each client can be kept in an index of the data
+ * struct member (using the provider's ID).
  *
  * All providers must implement at a minimum a perform_provider function. You
  * don't have to implement the others if you don't need them.
@@ -39,6 +40,9 @@
  * should call provider_done. Do NOT call this if you have accepted or rejected
  * the client.
  *
+ * Eventually, stuff like *:line handling will be moved here, but that means we
+ * have to talk to bandb directly first.
+ *
  * --Elizafox, 9 March 2016
  */
 
 rb_dlink_list auth_providers;
 
 /* Clients waiting */
-struct auth_client auth_clients[MAX_CLIENTS];
+rb_dictionary *auth_clients;
 
 /* Load a provider */
 void load_provider(struct auth_provider *provider)
 {
+       if(rb_dlink_list_length(&auth_providers) >= MAX_PROVIDERS)
+       {
+               warn_opers(L_CRIT, "Exceeded maximum level of authd providers (%d max)", MAX_PROVIDERS);
+               return;
+       }
+
        provider->init();
        rb_dlinkAdd(provider, &provider->node, &auth_providers);
 }
@@ -67,6 +77,7 @@ void unload_provider(struct auth_provider *provider)
 /* Initalise all providers */
 void init_providers(void)
 {
+       auth_clients = rb_dictionary_create("pending auth clients", rb_uint32cmp);
        load_provider(&rdns_provider);
        load_provider(&ident_provider);
 }
@@ -75,17 +86,15 @@ void init_providers(void)
 void destroy_providers(void)
 {
        rb_dlink_node *ptr;
+       rb_dictionary_iter iter;
+       struct auth_client *auth;
        struct auth_provider *provider;
 
        /* Cancel outstanding connections */
-       for (size_t i = 0; i < MAX_CLIENTS; i++)
+       RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
        {
-               if(auth_clients[i].cid)
-               {
-                       /* TBD - is this the right thing? */
-                       reject_client(&auth_clients[i], 0,
-                                       "Authentication system is down... try reconnecting in a few seconds");
-               }
+               /* TBD - is this the right thing? */
+               reject_client(auth, 0, "Authentication system is down... try reconnecting in a few seconds");
        }
 
        RB_DLINK_FOREACH(ptr, auth_providers.head)
@@ -107,14 +116,13 @@ void cancel_providers(struct auth_client *auth)
        {
                provider = ptr->data;
 
-               if(provider->cancel && is_provider(auth, provider->id))
+               if(provider->cancel && is_provider_on(auth, provider->id))
                        /* Cancel if required */
                        provider->cancel(auth);
        }
 
-       /* All data should be already destroyed */
-       rb_dictionary_destroy(auth->data, NULL, NULL);
-       auth->data = NULL;
+       rb_dictionary_delete(auth_clients, RB_UINT_TO_POINTER(auth->cid));
+       rb_free(auth);
 }
 
 /* Provider is done */
@@ -123,7 +131,8 @@ void provider_done(struct auth_client *auth, provider_t id)
        rb_dlink_node *ptr;
        struct auth_provider *provider;
 
-       unset_provider(auth, id);
+       set_provider_off(auth, id);
+       set_provider_done(auth, id);
 
        if(!auth->providers)
        {
@@ -136,7 +145,7 @@ void provider_done(struct auth_client *auth, provider_t id)
        {
                provider = ptr->data;
 
-               if(provider->completed && is_provider(auth, provider->id))
+               if(provider->completed && is_provider_on(auth, provider->id))
                        /* Notify pending clients who asked for it */
                        provider->completed(auth, id);
        }
@@ -145,7 +154,6 @@ void provider_done(struct auth_client *auth, provider_t id)
 /* Reject a client */
 void reject_client(struct auth_client *auth, provider_t id, const char *reason)
 {
-       uint16_t cid = auth->cid;
        char reject;
 
        switch(id)
@@ -159,65 +167,94 @@ void reject_client(struct auth_client *auth, provider_t id, const char *reason)
        case PROVIDER_BLACKLIST:
                reject = 'B';
                break;
-       case PROVIDER_NULL:
        default:
                reject = 'N';
                break;
        }
 
-       /* TODO send back ident */
-       rb_helper_write(authd_helper, "R %x %c :%s", auth->cid, reject, reason);
+       /* We send back username and hostname in case ircd wants to overrule our decision.
+        * In the future this may not be the case.
+        * --Elizafox
+        */
+       rb_helper_write(authd_helper, "R %x %c %s %s :%s", auth->cid, reject, auth->username, auth->hostname, reason);
 
-       unset_provider(auth, id);
+       set_provider_off(auth, id);
        cancel_providers(auth);
-       memset(&auth_clients[cid], 0, sizeof(struct auth_client));
 }
 
 /* Accept a client, cancel outstanding providers if any */
 void accept_client(struct auth_client *auth, provider_t id)
 {
-       uint16_t cid = auth->cid;
+       uint32_t cid = auth->cid;
 
        rb_helper_write(authd_helper, "A %x %s %s", auth->cid, auth->username, auth->hostname);
 
-       unset_provider(auth, id);
+       set_provider_off(auth, id);
        cancel_providers(auth);
-       memset(&auth_clients[cid], 0, sizeof(struct auth_client));
 }
 
 /* Send a notice to a client */
-void notice_client(struct auth_client *auth, const char *notice)
+void notice_client(struct auth_client *auth, const char *fmt, ...)
+{
+       char buf[BUFSIZE];
+       va_list args;
+
+       va_start(args, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, args);
+       va_end(args);
+
+       rb_helper_write(authd_helper, "N %x :%s", auth->cid, buf);
+}
+
+/* Send a warning to the IRC daemon for logging, etc. */
+void warn_opers(notice_level_t level, const char *fmt, ...)
 {
-       rb_helper_write(authd_helper, "N %x :%s", auth->cid, notice);
+       char buf[BUFSIZE];
+       va_list args;
+
+       va_start(args, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, args);
+       va_end(args);
+
+       rb_helper_write(authd_helper, "W %c :%s", level, buf);
 }
 
 /* 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)
 {
        struct auth_provider *provider;
-       struct auth_client *auth;
+       struct auth_client *auth = rb_malloc(sizeof(struct auth_client));
        long lcid = strtol(cid, NULL, 16);
-       char name[20];
        rb_dlink_node *ptr;
 
-       if(lcid >= MAX_CLIENTS)
+       if(lcid >= UINT32_MAX)
                return;
 
-       auth = &auth_clients[lcid];
-       if(auth->cid != 0)
-               /* Shouldn't get here */
-               return;
-
-       auth->cid = (uint16_t)lcid;
+       auth->cid = (uint32_t)lcid;
 
        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);
 
        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);
+
+#ifdef RB_IPV6
+       if(GET_SS_FAMILY(&auth->l_addr) == AF_INET6)
+               ((struct sockaddr_in6 *)&auth->l_addr)->sin6_port = htons(auth->l_port);
+       else
+#endif
+               ((struct sockaddr_in *)&auth->l_addr)->sin_port = htons(auth->l_port);
+
+#ifdef RB_IPV6
+       if(GET_SS_FAMILY(&auth->c_addr) == AF_INET6)
+               ((struct sockaddr_in6 *)&auth->c_addr)->sin6_port = htons(auth->c_port);
+       else
+#endif
+               ((struct sockaddr_in *)&auth->c_addr)->sin_port = htons(auth->c_port);
 
-       snprintf(name, sizeof(name), "%d provider data", auth->cid);
-       auth->data = rb_dictionary_create(name, rb_uint32cmp);
+       rb_dictionary_add(auth_clients, RB_UINT_TO_POINTER(auth->cid), auth);
 
        RB_DLINK_FOREACH(ptr, auth_providers.head)
        {
@@ -241,7 +278,10 @@ static void start_auth(const char *cid, const char *l_ip, const char *l_port, co
 void handle_new_connection(int parc, char *parv[])
 {
        if(parc < 7)
+       {
+               warn_opers(L_CRIT, "BUG: received too few params for new connection (7 expected, got %d)", parc);
                return;
+       }
 
        start_auth(parv[1], parv[2], parv[3], parv[4], parv[5]);
 }