1 This patch logs raw socket data, to assist debugging when discriminating
2 between server and fetchmail bugs.
4 Apply it to socket.c (works as of 6.3.20) and set the environment
5 variable FETCHMAIL_RAW_LOGFILE to a log file writable by fetchmail. If
6 it's not there, it gets created with mode 0600 (which requires directory
9 The file gets appended to, so you can log into named pipes, character
10 (stream) devices and to the console if you're so inclined.
12 Note 1: any logging failures cause fetchmail to abort() forcefully.
14 Note 2: raw control characters persist in the log and are not filtered
15 out. In doubt use a pager that filters control characters, or use tools
16 such as a binary-capable text edtior, vim's xxd, or hexdump, or od, to
17 view the raw log message.
19 -- Matthias Andree, June 2011
21 diff --git a/socket.c b/socket.c
22 index c8117a5..89847fe 100644
25 @@ -362,6 +362,49 @@ static SSL *_ssl_context[FD_SETSIZE];
27 Note 2: non-printable characters are hex-escaped, so it is safe to use
28 FETCHMAIL_RAW_LOGFILE=/dev/stderr or similar.
30 -- Matthias Andree, August 2011
32 diff --git a/sink.c b/sink.c
33 index 5d92556..ff6208d 100644
36 @@ -649,6 +649,10 @@ int stuffline(struct query *ctl, char *buf)
37 while ((last += strlen(last)) && (last[-1] != '\n'))
40 + if (outlevel >= O_DEBUG && (size_t)(last - buf) != strlen(buf))
41 + report(stdout, GT_("DEBUG: stuffline shipping line with NULs, length=%lu, strlen=%lu\n"), last - buf, strlen(buf));
44 /* fix message lines that have only \n termination (for qmail) */
47 diff --git a/socket.c b/socket.c
48 index e338207..dcaf19d 100644
51 @@ -381,6 +381,49 @@ static SSL *_ssl_context[FD_SETSIZE];
53 static SSL *SSLGetContext( int );
54 #endif /* SSL_ENABLE */
58 +static const char *rawlogfile;
59 +static FILE *rlogstream;
61 +static int SockLog(void) {
62 + static int haveinit;
64 + static int logfd = -1;
68 + if ((rawlogfile = getenv("FETCHMAIL_RAW_LOGFILE"))) {
69 + if ((logfd = open(rawlogfile, O_WRONLY|O_APPEND|O_CREAT, 0600)) == -1) {
70 + report(stderr, GT_("FETCHMAIL_RAW_LOGFILE is set, but opening \"%s\" for appending write failed: %s\n"), rawlogfile, strerror(errno));
73 + if (!(rlogstream = fdopen(logfd, "a"))) {
74 + report(stderr, GT_("FETCHMAIL_RAW_LOGFILE is set, but fdopen(%d) failed: %s\n"), logfd, strerror(errno));
77 + setvbuf(rlogstream, NULL, _IOLBF, 0);
84 +static void LogPrintf(const char *fmt, ...) {
88 + if (!SockLog()) return;
89 + locsav = setlocale(LC_ALL, NULL);
90 + (void)setlocale(LC_ALL, "C");
91 + if (EOF == vfprintf(rlogstream, fmt, va) || EOF == fflush(rlogstream)) {
92 + report(stderr, GT_("FETCHMAIL_RAW_LOGFILE is set, but logging failed: %s\n"), strerror(errno));
95 + (void)setlocale(LC_ALL, locsav);
99 int SockWrite(int sock, const char *buf, int len)
102 @@ -369,6 +412,8 @@ int SockWrite(int sock, const char *buf, int len)
106 + LogPrintf("[>%d-%s count=%04d] %.*s%s", sock, SSLGetContext(sock) ? "crypt" : "plain", len, len, buf, (len < 1 || buf[len - 1] != '\n') ? "\n" : "");
111 @@ -471,6 +516,8 @@ int SockRead(int sock, char *buf, int len)
115 + LogPrintf("[<%d-%s count=%04d] %.*s%s", sock, SSLGetContext(sock) ? "crypt" : "plain", bp - buf, bp - buf, buf, newline ? "" : "\n");