]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - src/s_auth.c
Fix an off by one error with zipstats processing
[irc/rqf/shadowircd.git] / src / s_auth.c
index 438bab6cb9dcb94ec277add000a4efdfbf6ed80c..a6143bb1124cfb5b467a74ecc030aa5211319e3e 100644 (file)
 #include "s_conf.h"
 #include "client.h"
 #include "common.h"
-#include "irc_string.h"
-#include "sprintf_irc.h"
+#include "match.h"
 #include "ircd.h"
 #include "numeric.h"
 #include "packet.h"
 #include "res.h"
-#include "s_log.h"
+#include "logger.h"
 #include "s_stats.h"
 #include "send.h"
 #include "hook.h"
 #include "blacklist.h"
 
+struct AuthRequest
+{
+       rb_dlink_node node;
+       struct Client *client;  /* pointer to client struct for request */
+       struct DNSQuery dns_query; /* DNS Query */
+       unsigned int flags;     /* current state of request */
+       rb_fde_t *F;            /* file descriptor for auth queries */
+       time_t timeout;         /* time when query expires */
+       uint16_t lport;
+       uint16_t rport;
+};
+
+/*
+ * flag values for AuthRequest
+ * NAMESPACE: AM_xxx - Authentication Module
+ */
+#define AM_AUTH_CONNECTING   (1 << 0)
+#define AM_AUTH_PENDING      (1 << 1)
+#define AM_DNS_PENDING       (1 << 2)
+
+#define SetDNSPending(x)     ((x)->flags |= AM_DNS_PENDING)
+#define ClearDNSPending(x)   ((x)->flags &= ~AM_DNS_PENDING)
+#define IsDNSPending(x)      ((x)->flags &  AM_DNS_PENDING)
+
+#define SetAuthConnect(x)    ((x)->flags |= AM_AUTH_CONNECTING)
+#define ClearAuthConnect(x)  ((x)->flags &= ~AM_AUTH_CONNECTING)
+#define IsAuthConnect(x)     ((x)->flags &  AM_AUTH_CONNECTING)
+
+#define SetAuthPending(x)    ((x)->flags |= AM_AUTH_PENDING)
+#define ClearAuthPending(x)  ((x)->flags &= AM_AUTH_PENDING)
+#define IsAuthPending(x)     ((x)->flags &  AM_AUTH_PENDING)
+
+#define ClearAuth(x)         ((x)->flags &= ~(AM_AUTH_PENDING | AM_AUTH_CONNECTING))
+#define IsDoingAuth(x)       ((x)->flags &  (AM_AUTH_PENDING | AM_AUTH_CONNECTING))
+
 /*
  * a bit different approach
  * this replaces the original sendheader macros
@@ -199,7 +233,7 @@ auth_dns_callback(void *vptr, struct DNSReply *reply)
                                good = 0;
                        }
                }
-#ifdef IPV6
+#ifdef RB_IPV6
                else if(auth->client->localClient->ip.ss_family == AF_INET6)
                {
                        struct sockaddr_in6 *ip, *ip_fwd;
@@ -222,7 +256,7 @@ auth_dns_callback(void *vptr, struct DNSReply *reply)
 
                 if(good && strlen(reply->h_name) <= HOSTLEN)
                 {
-                        strlcpy(auth->client->host, reply->h_name, sizeof(auth->client->host));
+                        rb_strlcpy(auth->client->host, reply->h_name, sizeof(auth->client->host));
                         sendheader(auth->client, REPORT_FIN_DNS);
                 }
                 else if (strlen(reply->h_name) > HOSTLEN)
@@ -240,7 +274,7 @@ auth_dns_callback(void *vptr, struct DNSReply *reply)
 static void
 auth_error(struct AuthRequest *auth)
 {
-       ++ServerStats->is_abad;
+       ++ServerStats.is_abad;
 
        rb_close(auth->F);
        auth->F = NULL;
@@ -263,7 +297,6 @@ static int
 start_auth_query(struct AuthRequest *auth)
 {
        struct rb_sockaddr_storage localaddr, destaddr;
-       socklen_t locallen = sizeof(struct rb_sockaddr_storage);
        rb_fde_t *F;
        int family;
        
@@ -273,10 +306,8 @@ start_auth_query(struct AuthRequest *auth)
        family = auth->client->localClient->ip.ss_family;
        if((F = rb_socket(family, SOCK_STREAM, 0, "ident")) == NULL)
        {
-               report_error("creating auth stream socket %s:%s",
-                            get_client_name(auth->client, SHOW_IP), 
-                            log_client_name(auth->client, SHOW_IP), errno);
-               ++ServerStats->is_abad;
+               ilog_error("creating auth stream socket");
+               ++ServerStats.is_abad;
                return 0;
        }
 
@@ -301,27 +332,35 @@ start_auth_query(struct AuthRequest *auth)
         * since the ident request must originate from that same address--
         * and machines with multiple IP addresses are common now
         */
-       memset(&localaddr, 0, locallen);
-       getsockname(rb_get_fd(auth->client->localClient->F),
-                   (struct sockaddr *) &localaddr, &locallen);
+       localaddr = auth->client->preClient->lip;
        
        /* XXX mangle_mapped_sockaddr((struct sockaddr *)&localaddr); */
-#ifdef IPV6
+#ifdef RB_IPV6
        if(localaddr.ss_family == AF_INET6)
        {
+               auth->lport = ntohs(((struct sockaddr_in6 *)&localaddr)->sin6_port);
                ((struct sockaddr_in6 *)&localaddr)->sin6_port = 0;
-       } else
+       }
+       else
 #endif
-       ((struct sockaddr_in *)&localaddr)->sin_port = 0;
+       {
+               auth->lport = ntohs(((struct sockaddr_in *)&localaddr)->sin_port);
+               ((struct sockaddr_in *)&localaddr)->sin_port = 0;
+       }
 
        destaddr = auth->client->localClient->ip;
-#ifdef IPV6
+#ifdef RB_IPV6
        if(localaddr.ss_family == AF_INET6)
        {
-               ((struct sockaddr_in6 *)&localaddr)->sin6_port = 113;
-       } else
+               auth->rport = ntohs(((struct sockaddr_in6 *)&destaddr)->sin6_port);
+               ((struct sockaddr_in6 *)&destaddr)->sin6_port = htons(113);
+       }
+       else
 #endif
-       ((struct sockaddr_in *)&localaddr)->sin_port = 113;
+       {
+               auth->rport = ntohs(((struct sockaddr_in *)&destaddr)->sin_port);
+               ((struct sockaddr_in *)&destaddr)->sin_port = htons(113);
+       }
        
        auth->F = F;
        SetAuthConnect(auth);
@@ -447,7 +486,7 @@ timeout_auth_queries_event(void *notused)
                        if(IsDoingAuth(auth))
                        {
                                ClearAuth(auth);
-                               ++ServerStats->is_abad;
+                               ++ServerStats.is_abad;
                                sendheader(auth->client, REPORT_FAIL_ID);
                                auth->client->localClient->auth_request = NULL;
                        }
@@ -479,11 +518,7 @@ static void
 auth_connect_callback(rb_fde_t *F, int error, void *data)
 {
        struct AuthRequest *auth = data;
-       struct sockaddr_in us;
-       struct sockaddr_in them;
        char authbuf[32];
-       socklen_t ulen = sizeof(struct sockaddr_in);
-       socklen_t tlen = sizeof(struct sockaddr_in);
 
        /* Check the error */
        if(error != RB_OK)
@@ -493,21 +528,10 @@ auth_connect_callback(rb_fde_t *F, int error, void *data)
                return;
        }
 
-       if(getsockname
-          (rb_get_fd(auth->client->localClient->F), (struct sockaddr *) &us,
-           (socklen_t *) & ulen)
-          || getpeername(rb_get_fd(auth->client->localClient->F),
-                         (struct sockaddr *) &them, (socklen_t *) & tlen))
-       {
-               ilog(L_IOERROR, "auth get{sock,peer}name error for %s:%m",
-                    log_client_name(auth->client, SHOW_IP));
-               auth_error(auth);
-               return;
-       }
        rb_snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n",
-                  (unsigned int) ntohs(them.sin_port), (unsigned int) ntohs(us.sin_port));
+                  auth->rport, auth->lport);
 
-       if(write(rb_get_fd(auth->F), authbuf, strlen(authbuf)) == -1)
+       if(rb_write(auth->F, authbuf, strlen(authbuf)) != strlen(authbuf))
        {
                auth_error(auth);
                return;
@@ -536,7 +560,7 @@ read_auth_reply(rb_fde_t *F, void *data)
        int count;
        char buf[AUTH_BUFSIZ + 1];      /* buffer to read auth reply into */
 
-       len = read(rb_get_fd(F), buf, AUTH_BUFSIZ);
+       len = rb_read(F, buf, AUTH_BUFSIZ);
 
        if(len < 0 && rb_ignore_errno(errno))
        {
@@ -577,14 +601,14 @@ read_auth_reply(rb_fde_t *F, void *data)
 
        if(s == NULL)
        {
-               ++ServerStats->is_abad;
+               ++ServerStats.is_abad;
                strcpy(auth->client->username, "unknown");
                sendheader(auth->client, REPORT_FAIL_ID);
        }
        else
        {
                sendheader(auth->client, REPORT_FIN_ID);
-               ++ServerStats->is_asuc;
+               ++ServerStats.is_asuc;
                SetGotId(auth->client);
        }