This file is indexed.

/usr/share/perl5/Mail/SpamAssassin/AutoWhitelist.pm is in spamassassin 3.4.2-1~deb9u1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at:
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>

=head1 NAME

Mail::SpamAssassin::AutoWhitelist - auto-whitelist handler for SpamAssassin

=head1 SYNOPSIS

  (see Mail::SpamAssassin)


=head1 DESCRIPTION

Mail::SpamAssassin is a module to identify spam using text analysis and
several internet-based realtime blacklists.

This class is used internally by SpamAssassin to manage the automatic
whitelisting functionality.  Please refer to the C<Mail::SpamAssassin>
documentation for public interfaces.

=head1 METHODS

=over 4

=cut

package Mail::SpamAssassin::AutoWhitelist;

use strict;
use warnings;
# use bytes;
use re 'taint';

use NetAddr::IP 4.000;

use Mail::SpamAssassin;
use Mail::SpamAssassin::Logger;
use Mail::SpamAssassin::Util qw(untaint_var);

our @ISA = qw();

###########################################################################

sub new {
  my $class = shift;
  $class = ref($class) || $class;
  my ($main, $msg) = @_;

  my $conf = $main->{conf};
  my $self = {
    main          => $main,
    factor        => $conf->{auto_whitelist_factor},
    ipv4_mask_len => $conf->{auto_whitelist_ipv4_mask_len},
    ipv6_mask_len => $conf->{auto_whitelist_ipv6_mask_len},
  };

  my $factory;
  if ($main->{pers_addr_list_factory}) {
    $factory = $main->{pers_addr_list_factory};
  }
  else {
    my $type = $conf->{auto_whitelist_factory};
    if ($type =~ /^([_A-Za-z0-9:]+)$/) {
      $type = untaint_var($type);
      eval '
  	    require '.$type.';
            $factory = '.$type.'->new();
            1;
           '
      or do {
	my $eval_stat = $@ ne '' ? $@ : "errno=$!";  chomp $eval_stat;
	warn "auto-whitelist: $eval_stat\n";
	undef $factory;
      };
      $main->set_persistent_address_list_factory($factory) if $factory;
    }
    else {
      warn "auto-whitelist: illegal auto_whitelist_factory setting\n";
    }
  }

  if (!defined $factory) {
    $self->{checker} = undef;
  } else {
    $self->{checker} = $factory->new_checker($self->{main});
  }

  bless ($self, $class);
  $self;
}

###########################################################################

=item $meanscore = awl->check_address($addr, $originating_ip, $signedby);

This method will return the mean score of all messages associated with the
given address, or undef if the address hasn't been seen before.

If B<$originating_ip> is supplied, it will be used in the lookup.

=cut

sub check_address {
  my ($self, $addr, $origip, $signedby) = @_;

  if (!defined $self->{checker}) {
    return;		# no factory defined; we can't check
  }

  $self->{entry} = undef;

  my $fulladdr = $self->pack_addr ($addr, $origip);
  my $entry = $self->{checker}->get_addr_entry ($fulladdr, $signedby);
  $self->{entry} = $entry;

  if (!$entry->{count}) {
    # no entry found
    if (defined $origip) {
      # try upgrading a default entry (probably from "add-addr-to-foo")
      my $noipaddr = $self->pack_addr ($addr, undef);
      my $noipent = $self->{checker}->get_addr_entry ($noipaddr, undef);

      if (defined $noipent->{count} && $noipent->{count} > 0) {
	dbg("auto-whitelist: found entry w/o IP address for $addr: replacing with $origip");
	$self->{checker}->remove_entry($noipent);
        # Now assign proper entry the count and totscore values of the
        # no-IP entry instead of assigning the whole value to avoid
        # wiping out any information added to the previous entry.
	$entry->{count} = $noipent->{count};
	$entry->{totscore} = $noipent->{totscore};
      }
    }
  }

  if ($entry->{count} < 0 ||
      $entry->{count} != $entry->{count} ||  # test for NaN
      $entry->{totscore} != $entry->{totscore})
  {
    warn "auto-whitelist: resetting bad data for ($addr, $origip), ".
         "count: $entry->{count}, totscore: $entry->{totscore}\n";
    $entry->{count} = $entry->{totscore} = 0;
  }

  return !$entry->{count} ? undef : $entry->{totscore} / $entry->{count};
}

###########################################################################

=item awl->count();

This method will return the count of messages used in determining the
whitelist correction.

=cut

sub count {
  my $self = shift;
  return $self->{entry}->{count};
}


###########################################################################

=item awl->add_score($score);

This method will add half the score to the current entry.  Half the
score is used, so that repeated use of the same From and IP address
combination will gradually reduce the score.

=cut

sub add_score {
  my ($self,$score) = @_;

  if (!defined $self->{checker}) {
    return;		# no factory defined; we can't check
  }
  if ($score != $score) {
    warn "auto-whitelist: attempt to add a $score to AWL entry ignored\n";
    return;		# don't try to add a NaN
  }

  $self->{entry}->{count} ||= 0;
  $self->{checker}->add_score($self->{entry}, $score);
}

###########################################################################

=item awl->add_known_good_address($addr);

This method will add a score of -100 to the given address -- effectively
"bootstrapping" the address as being one that should be whitelisted.

=cut

sub add_known_good_address {
  my ($self, $addr, $signedby) = @_;

  return $self->modify_address($addr, -100, $signedby);
}


###########################################################################

=item awl->add_known_bad_address($addr);

This method will add a score of 100 to the given address -- effectively
"bootstrapping" the address as being one that should be blacklisted.

=cut

sub add_known_bad_address {
  my ($self, $addr, $signedby) = @_;

  return $self->modify_address($addr, 100, $signedby);
}

###########################################################################

sub remove_address {
  my ($self, $addr, $signedby) = @_;

  return $self->modify_address($addr, undef, $signedby);
}

###########################################################################

sub modify_address {
  my ($self, $addr, $score, $signedby) = @_;

  if (!defined $self->{checker}) {
    return;		# no factory defined; we can't check
  }

  my $fulladdr = $self->pack_addr ($addr, undef);
  my $entry = $self->{checker}->get_addr_entry ($fulladdr, $signedby);

  # remove any old entries (will remove per-ip entries as well)
  # always call this regardless, as the current entry may have 0
  # scores, but the per-ip one may have more
  $self->{checker}->remove_entry($entry);

  # remove address only, no new score to add
  if (!defined $score)  { return 1; }
  if ($score != $score) { return 1; }  # don't try to add a NaN

  # else add score. get a new entry first
  $entry = $self->{checker}->get_addr_entry ($fulladdr, $signedby);
  $self->{checker}->add_score($entry, $score);

  return 1;
}

###########################################################################

sub finish {
  my $self = shift;

  return  if !defined $self->{checker};
  $self->{checker}->finish();
}

###########################################################################

sub ip_to_awl_key {
  my ($self, $origip) = @_;

  my $result;
  local $1;
  if (!defined $origip) {
    # could not find an IP address to use
  } elsif ($origip =~ /^ (\d{1,3} \. \d{1,3}) \. \d{1,3} \. \d{1,3} $/xs) {
    my $mask_len = $self->{ipv4_mask_len};
    $mask_len = 16  if !defined $mask_len;
    # handle the default and easy cases manually
    if ($mask_len == 32) {
      $result = $origip;
    } elsif ($mask_len == 16) {
      $result = $1;
    } else {
      my $origip_obj = NetAddr::IP->new($origip . '/' . $mask_len);
      if (!defined $origip_obj) {  # invalid IPv4 address
        dbg("auto-whitelist: bad IPv4 address $origip");
      } else {
        $result = $origip_obj->network->addr;
        $result =~s/(\.0){1,3}\z//;  # truncate zero tail
      }
    }
  } elsif ($origip =~ /:/ &&  # triage
           $origip =~
           /^ [0-9a-f]{0,4} (?: : [0-9a-f]{0,4} | \. [0-9]{1,3} ){2,9} $/xsi) {
    # looks like an IPv6 address
    my $mask_len = $self->{ipv6_mask_len};
    $mask_len = 48  if !defined $mask_len;
    my $origip_obj = NetAddr::IP->new6($origip . '/' . $mask_len);
    if (!defined $origip_obj) {  # invalid IPv6 address
      dbg("auto-whitelist: bad IPv6 address $origip");
    } elsif (NetAddr::IP->can('full6')) {  # since NetAddr::IP 4.010
      $result = $origip_obj->network->full6;  # string in a canonical form
      $result =~ s/(:0000){1,7}\z/::/;        # compress zero tail
    }
  } else {
    dbg("auto-whitelist: bad IP address $origip");
  }
  if (defined $result && length($result) > 39) {  # just in case, keep under
    $result = substr($result,0,39);               # the awl.ip field size
  }
  if (defined $result) {
    dbg("auto-whitelist: IP masking %s -> %s", $origip,$result);
  }
  return $result;
}

###########################################################################

sub pack_addr {
  my ($self, $addr, $origip) = @_;

  $addr = lc $addr;
  $addr =~ s/[\000\;\'\"\!\|]/_/gs;	# paranoia

  if (defined $origip) {
    $origip = $self->ip_to_awl_key($origip);
  }
  if (!defined $origip) {
    # could not find an IP address to use, could be localhost mail
    # or from the user running "add-addr-to-*".
    $origip = 'none';
  }
  return $addr . "|ip=" . $origip;
}

###########################################################################

1;

=back

=cut