File: logwatch

package info (click to toggle)
fex 20160919-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,944 kB
  • sloc: perl: 17,699; sh: 346; makefile: 67
file content (239 lines) | stat: -rwxr-xr-x 5,309 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/perl -w

use File::Basename;
use Cwd 'abs_path';
use I18N::Langinfo qw'langinfo CODESET';

# add fex lib
unless ($FEXLIB = $ENV{FEXLIB}) {
  if ($ENV{FEXHOME}) {
    $FEXLIB = $ENV{FEXHOME}.'/lib';
  } elsif (-f '/usr/share/fex/lib/fex.ph') {
    $FEXLIB = '/usr/share/fex/lib';
  } else {
    $FEXLIB = dirname(dirname(abs_path($0))).'/lib';
  }
  $ENV{FEXLIB} = $FEXLIB;
}
die "$0: no $FEXLIB\n" unless -d $FEXLIB;

# import from fex.pp
our (@logdir,$spooldir,$debug);

# load common code, local config : $HOME/lib/fex.ph
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";

$CTYPE = langinfo(CODESET());
binmode(STDOUT,":encoding($CTYPE)");

$log = shift || $logdir[0].'/fexsrv.log';

$ignore = join('|',qw(
  (CONNECT|CONTINUE).*(crawl|msnbot|obertux)
  DISCONNECT:.no.HTTP.request
  GET.*(favicon|robots\.txt)
  GET./organization\.gif
  GET./small_logo\.jpg
  GET./logo\.jpg
  GET./action-fex-camel\.gif
  GET./fup\?showstatus
  GET./FAQ/faq\.css
  GET./FAQ/jquery\.js
  GET.*Arrow\.gif
  GET./apple-touch
  GET./browserconfig\.xml
  User-Agent:.*(Webnote|FeedFetcher|\w+bot|bot/|Website.Watcher|crawler|spider|searchme|Yandex|Slurp|ScoutJet|findlinks|urlmon|nagios)
  User-Agent:.fnb.*quak
  User-Agent:.Google.favicon
  From:.*(msnbot|yandex|googlebot|webcrawler)
  Referer:.*sex.*stream
  Referer:.*stream.*sex
  X-.*prefetch
  X-Purpose:.preview
));

@weed = qw(
  .*keep-alive
  .*no-cache
  Connection:
  Cache-Control:
  Content-Type:
  Accept
  TE:
  UA-CPU:
  Pragma:
  DNT:
  Via:
  profile:
  Upgrade-Insecure-Requests:
  if-modified-since
  Surrogate-Capability
  Proxy-Authorization
  http\.
  Device-Stock
  NOKIA_
  GPRS
  X-Proxy-ID
  X-Moz
  X.Wap
  X-FH
  X-FB
  X-WS
  X-Nokia
  X-UCBrowser
  X-NSN
  X-OperaMini
  x-Device
  x-source-id
  x.up
  X-Behavioral
  X-Do-Not-Track
  X-\S*Via
  x-Mobile
  X-Country
  X-ClickOnceSupport
  X-Newrelic
  X-IMForwards
  X-Clearswift
  X-MDS
  .*:\s*$
);

$/ = "\n\n";
$| = 1;

if (-t STDIN or $ENV{GATEWAY_INTERFACE}) {
  open L,$log or die "$0: $log - $!\n";
  seek L,0,2;
} else {
  *L = *STDIN;
}
# binmode(L,":encoding(UTF-8)");

for (;;) {
  while (<L>) {
    next if /(^|\n)($ignore)/i;
    s/[\x00-\x08\x0B-\x1F\x1F\x80-\x9F]/_/g;
    s/^\n//;
    foreach $weed (@weed) {
      while (s/\n$weed.*\n/\n/i) {}
    }
    $post = /\nPOST\s/;
    if (/^\n*(CONNECT|CONTINUE).*\s\[([\d_]+)\]/i) { $pid = $2 }
    if (/\n(POST|GET)\s+(\S+)/i) {
      $cgi = $2;
      $cgi =~ s:.*/::;
      $cgi =~ s:\?.*::;
    }
    if (/Content-Length: (\d+)/i) {
      $d = $1;
      while ($d =~ s/(\d)(\d\d\d\b)/$1,$2/) {};
      s/Content-Length: \d+/Content-Length: $d/i;
    }
    s/[\s\n]*$/\n\n/;
    print or exit;
    $from = '';
    if (m:\nGET /fup/(\w{40,}):) {
      $_ = decode_b64($1);
      printf "  FROM=\"%s\"\n\n",$1 if /from=([\w\@.-]+)/;
    } elsif (m:\nGET /fop/(\w+)/:) {
      $dkey = $1;
      my $ddir = "$spooldir/.dkeys/$dkey";
      $_ = readlink $ddir or next;
      (undef,$to,$from) = split('/');
      printf "  FROM=\"%s\"\n",$from;
      printf "  TO=\"%s\"\n",$to;
      $cgi = '';
      if ($comment = slurp("$ddir/comment")) {
        printf "  COMMENT=\"%s\"\n",utf8decode($comment)||'';
      }
      if (not -f "$ddir/data" and $_ = slurp("$ddir/error")) {
        s/\n.*//s;
        print "  ERROR=\"$_\"\n";
      }
      elsif ($size = -s "$ddir/data") {
        printf "  SIZE=%s MB\n",int($size/1024/1024);
      }
      print "\n";
    } elsif (m:\nGET /fup.*skey=(\w+):) {
      read_skey($1);
      print "\n";
    }
    if ($debug and $pid and $post) {
      &read_debug_log;
    };
    $pid = $cgi = '';
  }
  sleep 1;
}


sub read_debug_log {
  my (@log,$log);
  local $/ = "\n";
  local $_;
  # https://rt.cpan.org/Public/Bug/Display.html?id=88592
  # local $^W;
  # no warnings "all";
  no warnings 'utf8';

  for (1..2) {
    sleep 1;
    @log = `ls -rt $logdir[0]/.debug/*_${pid}.$cgi 2>/dev/null`;
    if ($log = $log[-1] and open $log,$log) {
      binmode($log,":utf8");
      while (<$log>) {
        s/\r//;
        s/[^\x09\x20-\xFF]/_/g;
        if (/^Content-Disposition:.*name="FILE".*filename="(.+)"/i) {
          print "  FILE=\"$1\"\n";
        } elsif (/^Content-Disposition:.*name="(\w+)"/i) {
          my $p = uc($1);
          $_ = <$log>;
          my $v = <$log>||'';
          $v =~ s/[\r\n]+//;
          if ($v) {
            my $vv = utf8decode($v)||$v;
            $vv =~ s/[\x00-\x1F]/_/g;
            $vv =~ s/[\x80-\x9F]/_/g;
            printf "  %s=\"%s\"\n",$p,$vv;
            read_akey($v) if $p eq 'AKEY';
            read_skey($v) if $p eq 'SKEY';
          }
        } elsif (/^(Param|Exp): (\w+=".+")/) {
          print "  $2\n";
        }
      }
      close $log;
      print "\n";
      return;
    }
  }
}

sub read_akey {
  my $akey = "$spooldir/.akeys/" . shift;
  if (my $user = readlink($akey)) {
    $user =~ s:../::;
    printf "  USER=\"%s\"\n",$user;
  }
}


sub read_skey {
  my $skey = "$spooldir/.skeys/" . shift;
  if (open $skey,$skey) {
    while (<$skey>) {
      printf "  FROM=\"%s\"\n",$1 if /from=(.+)/;
      printf "  TO=\"%s\"\n",$1   if /to=(.+)/;
    }
    close $skey;
  }
}


sub utf8decode {
  local $_ = shift;
  s/([\xC0-\xDF])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg;
  return $_;
}