]> Pileus Git - ~andy/fetchmail/blob - contrib/rawlog.patch
Fix up merge.
[~andy/fetchmail] / contrib / rawlog.patch
1 This patch logs raw socket data, to assist debugging when discriminating
2 between server and fetchmail bugs.
3
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
7 write permission).
8
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.
11
12 Note 1: any logging failures cause fetchmail to abort() forcefully.
13
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.
18
19 -- Matthias Andree, June 2011
20
21 diff --git a/socket.c b/socket.c
22 index c8117a5..89847fe 100644
23 --- a/socket.c
24 +++ b/socket.c
25 @@ -362,6 +362,49 @@ static     SSL *_ssl_context[FD_SETSIZE];
26  static SSL     *SSLGetContext( int );
27  #endif /* SSL_ENABLE */
28  
29 +#include <fcntl.h>
30 +
31 +static const char *rawlogfile;
32 +static FILE *rlogstream;
33 +
34 +static int SockLog(void) {
35 +    static int haveinit;
36 +    static int wantlog;
37 +    static int logfd = -1;
38 +
39 +    if (!haveinit) {
40 +       haveinit = 1;
41 +       if ((rawlogfile = getenv("FETCHMAIL_RAW_LOGFILE"))) {
42 +           if ((logfd = open(rawlogfile, O_WRONLY|O_APPEND|O_CREAT, 0600)) == -1) {
43 +               report(stderr, GT_("FETCHMAIL_RAW_LOGFILE is set, but opening \"%s\" for appending write failed: %s\n"), rawlogfile, strerror(errno));
44 +               abort();
45 +           }
46 +           if (!(rlogstream = fdopen(logfd, "a"))) {
47 +               report(stderr, GT_("FETCHMAIL_RAW_LOGFILE is set, but fdopen(%d) failed: %s\n"), logfd, strerror(errno));
48 +               abort();
49 +           }
50 +           setvbuf(rlogstream, NULL, _IOLBF, 0);
51 +           wantlog = 1;
52 +       }
53 +    }
54 +    return wantlog;
55 +}
56 +
57 +static void LogPrintf(const char *fmt, ...) {
58 +    va_list va;
59 +    const char *locsav;
60 +    va_start(va, fmt);
61 +    if (!SockLog()) return;
62 +    locsav = setlocale(LC_ALL, NULL);
63 +    (void)setlocale(LC_ALL, "C");
64 +    if (EOF == vfprintf(rlogstream, fmt, va) || EOF == fflush(rlogstream)) {
65 +       report(stderr, GT_("FETCHMAIL_RAW_LOGFILE is set, but logging failed: %s\n"), strerror(errno));
66 +       abort();
67 +    }
68 +    (void)setlocale(LC_ALL, locsav);
69 +    va_end(va);
70 +}
71 +
72  int SockWrite(int sock, const char *buf, int len)
73  {
74      int n, wrlen = 0;
75 @@ -369,6 +412,8 @@ int SockWrite(int sock, const char *buf, int len)
76      SSL *ssl;
77  #endif
78  
79 +    LogPrintf("[>%d-%s count=%04d] %.*s%s", sock, SSLGetContext(sock) ? "crypt" : "plain", len, len, buf, (len < 1 || buf[len - 1] != '\n') ? "\n" : "");
80 +
81      while (len)
82      {
83  #ifdef SSL_ENABLE
84 @@ -471,6 +516,8 @@ int SockRead(int sock, char *buf, int len)
85             (!newline && len);
86      *bp = '\0';
87  
88 +    LogPrintf("[<%d-%s count=%04d] %.*s%s", sock, SSLGetContext(sock) ? "crypt" : "plain", bp - buf, bp - buf, buf, newline ? "" : "\n");
89 +
90      return bp - buf;
91  }
92