]> jfr.im git - solanum.git/blobdiff - ircd/authd.c
authd: more minor cleanups
[solanum.git] / ircd / authd.c
index a4bacbb041dc017929dc953bf0b1494b2df41dc6..861821aa667bc8bdc635a00660c4a44508f4a9b3 100644 (file)
@@ -109,6 +109,41 @@ start_authd(void)
        return 0;
 }
 
+static inline uint32_t
+str_to_cid(const char *str)
+{
+       long lcid = strtol(str, NULL, 16);
+
+       if(lcid > UINT32_MAX || lcid <= 0)
+       {
+               iwarn("authd sent us back a bad client ID: %lx", lcid);
+               restart_authd();
+               return 0;
+       }
+
+       return (uint32_t)lcid;
+}
+
+static inline struct Client *
+cid_to_client(uint32_t cid, bool delete)
+{
+       struct Client *client_p;
+
+       if(delete)
+               client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid));
+       else
+               client_p = rb_dictionary_retrieve(cid_clients, RB_UINT_TO_POINTER(cid));
+
+       if(client_p == NULL)
+       {
+               iwarn("authd sent us back a bad client ID: %ux", cid);
+               restart_authd();
+               return NULL;
+       }
+
+       return client_p;
+}
+
 static void
 parse_authd_reply(rb_helper * helper)
 {
@@ -116,7 +151,6 @@ parse_authd_reply(rb_helper * helper)
        int parc;
        char authdBuf[READBUF_SIZE];
        char *parv[MAXPARA + 1];
-       long lcid;
        uint32_t cid;
        struct Client *client_p;
 
@@ -134,24 +168,14 @@ parse_authd_reply(rb_helper * helper)
                                return;
                        }
 
-                       if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
-                       {
-                               iwarn("authd sent us back a bad client ID: %ld", lcid);
-                               restart_authd();
+                       if((cid = str_to_cid(parv[1])) == 0)
                                return;
-                       }
-
-                       cid = (uint32_t)lcid;
 
                        /* cid to uid (retrieve and delete) */
-                       if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
-                       {
-                               iwarn("authd sent us back an unknown client ID %x", cid);
-                               restart_authd();
+                       if((client_p = cid_to_client(cid, true)) == NULL)
                                return;
-                       }
 
-                       authd_decide_client(client_p, parv[2], parv[3], true, '\0', NULL, NULL);
+                       authd_accept_client(client_p, parv[2], parv[3]);
                        break;
                case 'R':       /* Reject client */
                        if(parc != 7)
@@ -161,24 +185,14 @@ parse_authd_reply(rb_helper * helper)
                                return;
                        }
 
-                       if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
-                       {
-                               iwarn("authd sent us back a bad client ID %ld", lcid);
-                               restart_authd();
+                       if((cid = str_to_cid(parv[1])) == 0)
                                return;
-                       }
-
-                       cid = (uint32_t)lcid;
 
                        /* cid to uid (retrieve and delete) */
-                       if((client_p = rb_dictionary_delete(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
-                       {
-                               iwarn("authd sent us back an unknown client ID %x", cid);
-                               restart_authd();
+                       if((client_p = cid_to_client(cid, true)) == NULL)
                                return;
-                       }
 
-                       authd_decide_client(client_p, parv[3], parv[4], false, toupper(*parv[2]), parv[5], parv[6]);
+                       authd_reject_client(client_p, parv[3], parv[4], toupper(*parv[2]), parv[5], parv[6]);
                        break;
                case 'N':       /* Notice to client */
                        if(parc != 3)
@@ -188,22 +202,12 @@ parse_authd_reply(rb_helper * helper)
                                return;
                        }
 
-                       if((lcid = strtol(parv[1], NULL, 16)) > UINT32_MAX || lcid < 0)
-                       {
-                               iwarn("authd sent us back a bad client ID %ld", lcid);
-                               restart_authd();
+                       if((cid = str_to_cid(parv[1])) == 0)
                                return;
-                       }
-
-                       cid = (uint32_t)lcid;
 
                        /* cid to uid */
-                       if((client_p = rb_dictionary_retrieve(cid_clients, RB_UINT_TO_POINTER(cid))) == NULL)
-                       {
-                               iwarn("authd sent us back an unknown client ID %x", cid);
-                               restart_authd();
+                       if((client_p = cid_to_client(cid, false)) == NULL)
                                return;
-                       }
 
                        sendto_one_notice(client_p, ":%s", parv[2]);
                        break;
@@ -398,7 +402,7 @@ authd_initiate_client(struct Client *client_p)
  *
  * After this point authd no longer "owns" the client.
  */
-void
+static inline void
 authd_decide_client(struct Client *client_p, const char *ident, const char *host, bool accept, char cause, const char *data, const char *reason)
 {
        if(client_p->preClient == NULL || client_p->preClient->authd_cid == 0)
@@ -435,6 +439,20 @@ authd_decide_client(struct Client *client_p, const char *ident, const char *host
        read_packet(client_p->localClient->F, client_p);
 }
 
+/* Convenience function to accept client */
+void
+authd_accept_client(struct Client *client_p, const char *ident, const char *host)
+{
+       authd_decide_client(client_p, ident, host, true, '\0', NULL, NULL);
+}
+
+/* Convenience function to reject client */
+void
+authd_reject_client(struct Client *client_p, const char *ident, const char *host, char cause, const char *data, const char *reason)
+{
+       authd_decide_client(client_p, ident, host, false, cause, data, reason);
+}
+
 void
 authd_abort_client(struct Client *client_p)
 {
@@ -450,7 +468,7 @@ authd_abort_client(struct Client *client_p)
                rb_helper_write(authd_helper, "E %x", client_p->preClient->authd_cid);
 
        /* XXX should we blindly allow like this? */
-       authd_decide_client(client_p, "*", "*", true, '\0', NULL, NULL);
+       authd_accept_client(client_p, "*", "*");
        client_p->preClient->authd_cid = 0;
 }
 
@@ -563,21 +581,22 @@ ident_check_enable(bool enabled)
 }
 
 /* Create an OPM listener */
-bool
-create_opm_listener(struct rb_sockaddr_storage *addr)
+void
+create_opm_listener(const char *ip, uint16_t port)
 {
-       char addrbuf[HOSTIPLEN];
-
-       if(!rb_inet_ntop_sock((struct sockaddr *)addr, addrbuf, sizeof(addrbuf)))
-               return false;
+       rb_helper_write(authd_helper, "O opm_listener %s %hu", ip, port);
+}
 
-       if(addrbuf[0] == ':')
-       {
-               /* Reformat for authd sending */
-               memmove(addrbuf + 1, addrbuf, sizeof(addrbuf) - 1);
-               addrbuf[0] = '0';
-       }
+/* Disable all OPM scans */
+void
+opm_check_enable(bool enabled)
+{
+       rb_helper_write(authd_helper, "O opm_enable %d", enabled ? 1 : 0);
+}
 
-       rb_helper_write(authd_helper, "O opm_listener %s %hu", addrbuf, ntohs(GET_SS_PORT(addr)));
-       return true;
+/* Create an OPM proxy scan */
+void
+create_opm_proxy_scan(const char *scan, uint16_t port)
+{
+       rb_helper_write(authd_helper, "O opm_scanner %s %hu", scan, port);
 }