]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 3391: forwarded_for log functionality broken
authorAmos Jeffries <squid3@treenet.co.nz>
Fri, 16 Dec 2011 11:29:40 +0000 (00:29 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Fri, 16 Dec 2011 11:29:40 +0000 (00:29 +1300)
src/AccessLogEntry.cc [new file with mode: 0644]
src/AccessLogEntry.h
src/Makefile.am
src/format/Format.cc
src/log/FormatHttpdCombined.cc
src/log/FormatHttpdCommon.cc
src/log/FormatSquidNative.cc
src/log/FormatSquidReferer.cc
src/log/FormatSquidUseragent.cc

diff --git a/src/AccessLogEntry.cc b/src/AccessLogEntry.cc
new file mode 100644 (file)
index 0000000..96b4410
--- /dev/null
@@ -0,0 +1,19 @@
+#include "config.h"
+#include "AccessLogEntry.h"
+#include "HttpRequest.h"
+
+void
+AccessLogEntry::getLogClientIp(char *buf, size_t bufsz) const
+{
+#if FOLLOW_X_FORWARDED_FOR
+    if (Config.onoff.log_uses_indirect_client && request)
+        request->indirect_client_addr.NtoA(buf, bufsz);
+    else
+#endif
+        if (tcpClient != NULL)
+            tcpClient->remote.NtoA(buf, bufsz);
+        else if (cache.caddr.IsNoAddr()) // e.g., ICAP OPTIONS lack client
+            strncpy(buf, "-", 1);
+        else
+            cache.caddr.NtoA(buf, bufsz);
+}
index 5c2d80616dfb731fbd4cb3e2ed2227667f76ae5d..687d4a444bff054395d96af644202c1842cdee7c 100644 (file)
@@ -52,6 +52,11 @@ public:
     AccessLogEntry() : url(NULL), tcpClient(), reply(NULL), request(NULL),
             adapted_request(NULL) {}
 
+    /// Fetch the client IP log string into the given buffer.
+    /// Knows about several alternate locations of the IP
+    /// including indirect forwarded-for IP if configured to log that
+    void getLogClientIp(char *buf, size_t bufsz) const;
+
     const char *url;
 
     /// TCP/IP level details about the client connection
index 7c3ffabe305d1739eadbd2faf43f7cdfa33845ab..d6de297721d69e42f288f6400fb2121b67c064d5 100644 (file)
@@ -253,6 +253,7 @@ libsquid_la_SOURCES = \
 
 squid_SOURCES = \
        $(ACL_REGISTRATION_SOURCES) \
+       AccessLogEntry.cc \
        AccessLogEntry.h \
        AsyncEngine.cc \
        AsyncEngine.h \
@@ -1260,6 +1261,7 @@ tests_testBoilerplate_DEPENDENCIES = \
 
 ## Tests of the CacheManager module.
 tests_testCacheManager_SOURCES = \
+       AccessLogEntry.cc \
        $(ACL_REGISTRATION_SOURCES) \
        debug.cc \
        HttpParser.cc \
@@ -1598,6 +1600,7 @@ tests_testDiskIO_DEPENDENCIES = \
 
 ## Tests of the Even module.
 tests_testEvent_SOURCES = \
+       AccessLogEntry.cc \
        $(ACL_REGISTRATION_SOURCES) \
        BodyPipe.cc \
        CacheDigest.cc \
@@ -1791,6 +1794,7 @@ tests_testEvent_DEPENDENCIES = \
 
 ## Tests of the EventLoop module.
 tests_testEventLoop_SOURCES = \
+       AccessLogEntry.cc \
        $(ACL_REGISTRATION_SOURCES) \
        BodyPipe.cc \
        CacheDigest.cc \
@@ -1982,6 +1986,7 @@ tests_testEventLoop_DEPENDENCIES = \
        $(SQUID_CPPUNIT_LA)
 
 tests_test_http_range_SOURCES = \
+       AccessLogEntry.cc \
        $(ACL_REGISTRATION_SOURCES) \
        BodyPipe.cc \
        cache_cf.cc \
@@ -2203,6 +2208,7 @@ tests_testHttpParser_DEPENDENCIES = \
 
 ## Tests of the HttpRequest module.
 tests_testHttpRequest_SOURCES = \
+       AccessLogEntry.cc \
        $(ACL_REGISTRATION_SOURCES) \
        HttpParser.cc \
        HttpParser.h \
@@ -3121,6 +3127,7 @@ tests_testNull_DEPENDENCIES = \
 ## Tests of the URL module.
 ## TODO: Trim this down once the insanity is over.
 tests_testURL_SOURCES = \
+       AccessLogEntry.cc \
        $(ACL_REGISTRATION_SOURCES) \
        BodyPipe.cc \
        cache_cf.cc \
index 4c6f780260f38a134b0d0132079b6b153efb6397..6344c4cfe269ea9309270971442bdc7b0b36885b 100644 (file)
@@ -315,10 +315,8 @@ Format::Format::assemble(MemBuf &mb, AccessLogEntry *al, int logSequenceNumber)
             break;
 
         case LFT_CLIENT_IP_ADDRESS:
-            if (al->cache.caddr.IsNoAddr()) // e.g., ICAP OPTIONS lack client
-                out = "-";
-            else
-                out = al->cache.caddr.NtoA(tmp,1024);
+            al->getLogClientIp(tmp, sizeof(tmp));
+            out = tmp;
             break;
 
         case LFT_CLIENT_FQDN:
index 72bc6daba0f2a410d8ccaa00c2cc6bc7af53793f..50fb3d646ab8ca3b3a565467a80c60e9f0bbb1f6 100644 (file)
@@ -44,8 +44,6 @@
 void
 Log::Format::HttpdCombined(AccessLogEntry * al, Logfile * logfile)
 {
-    char clientip[MAX_IPSTRLEN];
-
     const char *user_ident = ::Format::QuoteUrlEncodeUsername(al->cache.rfc931);
 
     const char *user_auth = ::Format::QuoteUrlEncodeUsername(al->cache.authuser);
@@ -58,8 +56,11 @@ Log::Format::HttpdCombined(AccessLogEntry * al, Logfile * logfile)
     if (!agent || *agent == '\0')
         agent = "-";
 
+    char clientip[MAX_IPSTRLEN];
+    al->getLogClientIp(clientip, MAX_IPSTRLEN);
+
     logfilePrintf(logfile, "%s %s %s [%s] \"%s %s %s/%d.%d\" %d %"PRId64" \"%s\" \"%s\" %s%s:%s%s",
-                  al->cache.caddr.NtoA(clientip,MAX_IPSTRLEN),
+                  clientip,
                   user_ident ? user_ident : dash_str,
                   user_auth ? user_auth : dash_str,
                   Time::FormatHttpd(squid_curtime),
index 3dce6a5ae8774fb459ea5105d4d4bff0de07072a..d5c16fc90323c5fe509726403f16714973eee597 100644 (file)
 void
 Log::Format::HttpdCommon(AccessLogEntry * al, Logfile * logfile)
 {
-    char clientip[MAX_IPSTRLEN];
     const char *user_auth = ::Format::QuoteUrlEncodeUsername(al->cache.authuser);
     const char *user_ident = ::Format::QuoteUrlEncodeUsername(al->cache.rfc931);
 
+    char clientip[MAX_IPSTRLEN];
+    al->getLogClientIp(clientip, MAX_IPSTRLEN);
+
     logfilePrintf(logfile, "%s %s %s [%s] \"%s %s %s/%d.%d\" %d %"PRId64" %s%s:%s%s",
-                  al->cache.caddr.NtoA(clientip,MAX_IPSTRLEN),
+                  clientip,
                   user_ident ? user_ident : dash_str,
                   user_auth ? user_auth : dash_str,
                   Time::FormatHttpd(squid_curtime),
index 76d31071947d4c82d51df36e51cfc5c6a7c0f5c7..21ed037148577482db99461af2772cd282c9c6aa 100644 (file)
@@ -62,10 +62,7 @@ Log::Format::SquidNative(AccessLogEntry * al, Logfile * logfile)
         safe_free(user);
 
     char clientip[MAX_IPSTRLEN];
-    if (al->tcpClient != NULL)
-        al->tcpClient->remote.NtoA(clientip, sizeof(clientip));
-    else
-        al->cache.caddr.NtoA(clientip, sizeof(clientip));
+    al->getLogClientIp(clientip, MAX_IPSTRLEN);
 
     logfilePrintf(logfile, "%9ld.%03d %6d %s %s%s/%03d %"PRId64" %s %s %s %s%s/%s %s%s",
                   (long int) current_time.tv_sec,
index 585187dd654b8f205c41696505de52e1600cb2bf..6932b9cdbb0cadae44e6c322c8c0109d16795cf4 100644 (file)
@@ -50,11 +50,12 @@ Log::Format::SquidReferer(AccessLogEntry *al, Logfile *logfile)
         return;
 
     char clientip[MAX_IPSTRLEN];
+    al->getLogClientIp(clientip, MAX_IPSTRLEN);
 
     logfilePrintf(logfile, "%9ld.%03d %s %s %s\n",
                   (long int) current_time.tv_sec,
                   (int) current_time.tv_usec / 1000,
-                  al->cache.caddr.NtoA(clientip, MAX_IPSTRLEN),
+                  clientip,
                   referer,
                   al->url ? al->url : "-");
 }
index ce9f676a1a74323b309057be4cbaa74bbf1bd539..dc1ae23a5bf778ad92f38b3f6b88b24e29b770ec 100644 (file)
 void
 Log::Format::SquidUserAgent(AccessLogEntry * al, Logfile * logfile)
 {
-    char clientip[MAX_IPSTRLEN];
-
     const char *agent = al->request->header.getStr(HDR_USER_AGENT);
 
     // do not log unless there is something to be displayed.
     if (!agent || *agent == '\0')
         return;
 
+    char clientip[MAX_IPSTRLEN];
+    al->getLogClientIp(clientip, MAX_IPSTRLEN);
+
     logfilePrintf(logfile, "%s [%s] \"%s\"\n",
-                  al->cache.caddr.NtoA(clientip,MAX_IPSTRLEN),
+                  clientip,
                   Time::FormatHttpd(squid_curtime),
                   agent);
 }