=head1 NAME
Mail::SpamAssassin::Util - utility functions
=head1 DESCRIPTION
A general class for utility functions. Please use this for functions that
stand alone, without requiring a $self object, Portability functions
especially.
NOTE: The functions in this module are to be considered private. Their API may
change at any point, and it's expected that they'll only be used by other
Mail::SpamAssassin modules. (TODO: we should probably revisit this if
it's useful for plugin development.)
=over 4
=cut
package Mail::SpamAssassin::Util;
use strict;
use warnings;
use bytes;
use Mail::SpamAssassin::Logger;
use vars qw (
@ISA @EXPORT
$AM_TAINTED
);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(local_tz base64_decode);
use Mail::SpamAssassin;
use Mail::SpamAssassin::Util::RegistrarBoundaries;
use Config;
use File::Spec;
use Time::Local;
use Sys::Hostname (); use Fcntl;
use POSIX (); use Errno qw(EEXIST);
use constant HAS_MIME_BASE64 => eval { require MIME::Base64; };
use constant RUNNING_ON_WINDOWS => ($^O =~ /^(?:mswin|dos|os2)/oi);
{
my $displayed_path = 0;
sub find_executable_in_env_path {
my ($filename) = @_;
clean_path_in_taint_mode();
if ( !$displayed_path++ ) {
dbg("util: current PATH is: ".join($Config{'path_sep'},File::Spec->path()));
}
foreach my $path (File::Spec->path()) {
my $fname = File::Spec->catfile ($path, $filename);
if ( -f $fname ) {
if (-x $fname) {
dbg("util: executable for $filename was found at $fname");
return $fname;
}
else {
dbg("util: $filename was found at $fname, but isn't executable");
}
}
}
return undef;
}
}
{
my $cleaned_taint_path = 0;
sub clean_path_in_taint_mode {
return if ($cleaned_taint_path++);
return unless am_running_in_taint_mode();
dbg("util: taint mode: deleting unsafe environment variables, resetting PATH");
if (RUNNING_ON_WINDOWS) {
dbg("util: running on Win32, skipping PATH cleaning");
return;
}
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my @path = ();
my @stat;
foreach my $dir (File::Spec->path()) {
next unless $dir;
$dir =~ /^(.+)$/; $dir = File::Spec->canonpath($1);
if (!File::Spec->file_name_is_absolute($dir)) {
dbg("util: PATH included '$dir', which is not absolute, dropping");
next;
}
elsif (!(@stat=stat($dir))) {
dbg("util: PATH included '$dir', which doesn't exist, dropping");
next;
}
elsif (!-d _) {
dbg("util: PATH included '$dir', which isn't a directory, dropping");
next;
}
elsif (($stat[2]&2) != 0) {
dbg("util: PATH included '$dir', which is world writable, dropping");
next;
}
dbg("util: PATH included '$dir', keeping");
push(@path, $dir);
}
$ENV{'PATH'} = join($Config{'path_sep'}, @path);
dbg("util: final PATH set to: ".$ENV{'PATH'});
}
}
sub am_running_in_taint_mode {
return $AM_TAINTED if defined $AM_TAINTED;
if ($] >= 5.008) {
$AM_TAINTED = eval q(no warnings q(syntax); ${^TAINT});
}
else {
my $blank;
for my $d ((File::Spec->curdir, File::Spec->rootdir, File::Spec->tmpdir)) {
opendir(TAINT, $d) || next;
$blank = readdir(TAINT);
closedir(TAINT);
last;
}
if (!(defined $blank && $blank)) {
$blank = join('', values %ENV, $0, @ARGV);
}
$blank = substr($blank, 0, 0);
$AM_TAINTED = not eval { eval "1 || $blank" || 1 };
}
dbg("util: running in taint mode? ". ($AM_TAINTED ? "yes" : "no"));
return $AM_TAINTED;
}
sub am_running_on_windows {
return RUNNING_ON_WINDOWS;
}
sub untaint_file_path {
my ($path) = @_;
return unless defined($path);
return '' if ($path eq '');
my $chars = '-_A-Za-z\xA0-\xFF0-9\.\%\@\=\+\,\/\\\:';
my $re = qr/^\s*([$chars][${chars}~ ]*)$/o;
if ($path =~ $re) {
return $1;
} else {
warn "util: cannot untaint path: \"$path\"\n";
return $path;
}
}
sub untaint_hostname {
my ($host) = @_;
return unless defined($host);
return '' if ($host eq '');
if (length($host) <= 255 && $host =~ /^([a-z\d](?:[a-z\d-]{0,61}[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?)*)$/i) {
return $1;
}
else {
warn "util: cannot untaint hostname: \"$host\"\n";
return $host;
}
}
sub untaint_var {
local ($_) = @_;
return undef unless defined;
unless (ref) {
/^(.*)$/s;
return $1;
}
elsif (ref eq 'ARRAY') {
@{$_} = map { $_ = untaint_var($_) } @{$_};
return @{$_} if wantarray;
}
elsif (ref eq 'HASH') {
while (my ($k, $v) = each %{$_}) {
if (!defined $v && $_ == \%ENV) {
delete ${$_}{$k};
next;
}
${$_}{untaint_var($k)} = untaint_var($v);
}
return %{$_} if wantarray;
}
elsif (ref eq 'SCALAR' or ref eq 'REF') {
${$_} = untaint_var(${$_});
}
else {
warn "util: can't untaint a " . ref($_) . "!\n";
}
return $_;
}
sub taint_var {
my ($v) = @_;
return $v unless defined $v;
my $tainter = substr ($^X."_", 0, 1); $v .= $tainter; chop $v;
return $v;
}
my %TZ = (
'UT' => '+0000',
'UTC' => '+0000',
'NDT' => '-0230',
'AST' => '-0400',
'ADT' => '-0300',
'NST' => '-0330',
'EST' => '-0500',
'EDT' => '-0400',
'CST' => '-0600',
'CDT' => '-0500',
'MST' => '-0700',
'MDT' => '-0600',
'PST' => '-0800',
'PDT' => '-0700',
'HST' => '-1000',
'AKST' => '-0900',
'AKDT' => '-0800',
'HADT' => '-0900',
'HAST' => '-1000',
'GMT' => '+0000',
'BST' => '+0100',
'IST' => '+0100',
'WET' => '+0000',
'WEST' => '+0100',
'CET' => '+0100',
'CEST' => '+0200',
'EET' => '+0200',
'EEST' => '+0300',
'MSK' => '+0300',
'MSD' => '+0400',
'MET' => '+0100',
'MEZ' => '+0100',
'MEST' => '+0200',
'MESZ' => '+0200',
'BRST' => '-0200',
'BRT' => '-0300',
'AEST' => '+1000',
'AEDT' => '+1100',
'ACST' => '+0930',
'ACDT' => '+1030',
'AWST' => '+0800',
'NZST' => '+1200',
'NZDT' => '+1300',
'JST' => '+0900',
'KST' => '+0900',
'HKT' => '+0800',
'SGT' => '+0800',
'PHT' => '+0800',
'IDT' => '+0300',
);
my %MONTH = (jan => 1, feb => 2, mar => 3, apr => 4, may => 5, jun => 6,
jul => 7, aug => 8, sep => 9, oct => 10, nov => 11, dec => 12);
my $LOCALTZ;
sub local_tz {
return $LOCALTZ if defined($LOCALTZ);
my $time = time;
my @g = gmtime($time);
my @t = localtime($time);
my $z = $t[1]-$g[1]+($t[2]-$g[2])*60+($t[7]-$g[7])*1440+($t[5]-$g[5])*525600;
$LOCALTZ = sprintf("%+.2d%.2d", $z/60, $z%60);
return $LOCALTZ;
}
sub parse_rfc822_date {
my ($date) = @_;
local ($_);
my ($yyyy, $mmm, $dd, $hh, $mm, $ss, $mon, $tzoff);
$_ = " $date "; s/, */ /gs; s/\s+/ /gs;
if (s/ (\d+) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) / /i) {
$dd = $1; $mon = lc($2); $yyyy = $3;
} elsif (s/ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) +(\d+) \d+:\d+:\d+ (\d{4}) / /i) {
$dd = $2; $mon = lc($1); $yyyy = $3;
} elsif (s/ (\d+) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{2,3}) / /i) {
$dd = $1; $mon = lc($2); $yyyy = $3;
} else {
dbg("util: time cannot be parsed: $date");
return undef;
}
if (defined $yyyy) {
if (length($yyyy) == 2 && $yyyy < 50) {
$yyyy += 2000;
}
elsif (length($yyyy) != 4) {
$yyyy += 1900;
}
}
if (s/ (\d?\d):(\d\d)(:(\d\d))? / /) {
$hh = $1; $mm = $2; $ss = $4 || 0;
}
if (s/ ([-+]\d{4}) / /) {
$tzoff = $1;
}
elsif (s/\b([A-Z]{2,4}(?:-DST)?)\b/ / && exists $TZ{$1}) {
$tzoff = $TZ{$1};
}
$tzoff ||= '-0000';
if (exists $MONTH{$mon}) {
$mmm = $MONTH{$mon};
}
$hh ||= 0; $mm ||= 0; $ss ||= 0; $dd ||= 0; $mmm ||= 0; $yyyy ||= 0;
if ($ss > 59) {
dbg("util: second after supported range, forcing second to 59: $date");
$ss = 59;
}
if ($mm > 59) {
dbg("util: minute after supported range, forcing minute to 59: $date");
$mm = 59;
}
if ($hh > 23) {
dbg("util: hour after supported range, forcing hour to 23: $date");
$hh = 23;
}
my $max_dd = 31;
if ($mmm == 4 || $mmm == 6 || $mmm == 9 || $mmm == 11) {
$max_dd = 30;
}
elsif ($mmm == 2) {
$max_dd = (!($yyyy % 4) && (($yyyy % 100) || !($yyyy % 400))) ? 29 : 28;
}
if ($dd > $max_dd) {
dbg("util: day is too high, incrementing date to next valid date: $date");
$dd = 1;
$mmm++;
if ($mmm > 12) {
$mmm = 1;
$yyyy++;
}
}
if ($yyyy > 2037) {
dbg("util: year after supported range, forcing year to 2037: $date");
$yyyy = 2037;
}
elsif ($yyyy < 1970) {
dbg("util: year before supported range, forcing year to 1970: $date");
$yyyy = 1971;
}
my $time;
eval { $time = timegm($ss, $mm, $hh, $dd, $mmm-1, $yyyy);
};
if ($@) {
dbg("util: time cannot be parsed: $date, $yyyy-$mmm-$dd $hh:$mm:$ss");
return undef;
}
if ($tzoff =~ /([-+])(\d\d)(\d\d)$/) {
$tzoff = (($2 * 60) + $3) * 60;
if ($1 eq '-') {
$time += $tzoff;
} else {
$time -= $tzoff;
}
}
return $time;
}
sub time_to_rfc822_date {
my($time) = @_;
my @days = qw/Sun Mon Tue Wed Thu Fri Sat/;
my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
my @localtime = localtime($time || time);
$localtime[5]+=1900;
sprintf("%s, %02d %s %4d %02d:%02d:%02d %s", $days[$localtime[6]], $localtime[3],
$months[$localtime[4]], @localtime[5,2,1,0], local_tz());
}
sub wrap {
my $string = shift || '';
my $prefix = shift || '';
my $first = shift || '';
my $length = shift || 77;
my $overflow = shift || 0;
my $break = shift || qr/\s/;
my $sep = "\n";
my @arr = split(/($break)/, $string);
splice @arr, 0, 0, $first if $first;
my $pos = 0;
my $pos_mod = 0;
while ($ my $len = length $arr[$pos];
if ($pos_mod != 0 && $overflow == 0) {
$len += length $arr[$pos+1];
}
if ($len <= $length) {
$arr[$pos] .= splice @arr, $pos+1, 1;
$pos_mod = 1;
}
else {
$arr[$pos] =~ s/\s+$//;
$pos++;
$pos_mod = 0;
splice @arr, $pos, 0, $prefix;
}
}
return join($sep, @arr);
}
sub base64_decode {
local $_ = shift;
my $decoded_length = shift;
s/\s+//g;
if (HAS_MIME_BASE64 && (length($_) % 4 == 0) &&
m|^(?:[A-Za-z0-9+/=]{2,}={0,2})$|s)
{
s/(=+)(?!=*$)/'A' x length($1)/ge;
if (defined $decoded_length) {
$_ = substr $_, 0, 4 * (int($decoded_length/3) + 1);
my $decoded = MIME::Base64::decode_base64($_);
return substr $decoded, 0, $decoded_length;
}
return MIME::Base64::decode_base64($_);
}
tr{A-Za-z0-9+/=}{}cd; s/=+$//; # remove terminating padding
tr{A-Za-z0-9+/=}{ -_`}; # translate to uuencode
s/.$// if (length($_) % 4 == 1); # unpack cannot cope with extra byte
my $length;
my $out = '';
while ($_) {
$length = (length >= 84) ? 84 : length;
$out .= unpack("u", chr(32 + $length * 3/4) . substr($_, 0, $length, ''));
last if (defined $decoded_length && length $out >= $decoded_length);
}
# If only a certain number of bytes are requested, truncate the encoded
# version down to the appropriate size and return the requested bytes
if (defined $decoded_length) {
return substr $out, 0, $decoded_length;
}
return $out;
}
sub qp_decode {
local $_ = shift;
s/\=\r?\n//gs;
s/\=([0-9a-fA-F]{2})/chr(hex($1))/ge;
return $_;
}
sub base64_encode {
local $_ = shift;
if (HAS_MIME_BASE64) {
return MIME::Base64::encode_base64($_);
}
$_ = pack("u57", $_);
s/^.//mg;
tr| -_`|A-Za-z0-9+/A|; s/(A+)$/'=' x length $1/e;
return $_;
}
sub portable_getpwuid {
if (defined &Mail::SpamAssassin::Util::_getpwuid_wrapper) {
return Mail::SpamAssassin::Util::_getpwuid_wrapper(@_);
}
if (!RUNNING_ON_WINDOWS) {
eval ' sub _getpwuid_wrapper { getpwuid($_[0]); } ';
} else {
dbg("util: defining getpwuid() wrapper using 'unknown' as username");
eval ' sub _getpwuid_wrapper { _fake_getpwuid($_[0]); } ';
}
if ($@) {
warn "util: failed to define getpwuid() wrapper: $@\n";
} else {
return Mail::SpamAssassin::Util::_getpwuid_wrapper(@_);
}
}
sub _fake_getpwuid {
return (
'unknown', 'x', $_[0], 0, '', '', '', '/', '', '', );
}
sub extract_ipv4_addr_from_string {
my ($str) = @_;
return unless defined($str);
if ($str =~ /\b(
(?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
(?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
(?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)\.
(?:1\d\d|2[0-4]\d|25[0-5]|\d\d|\d)
)\b/ix)
{
if (defined $1) { return $1; }
}
return;
}
{
my($hostname, $fq_hostname);
sub hostname {
return $hostname if defined($hostname);
clean_path_in_taint_mode();
$hostname = Sys::Hostname::hostname();
return $hostname;
}
sub fq_hostname {
return $fq_hostname if defined($fq_hostname);
$fq_hostname = hostname();
if ($fq_hostname !~ /\./) { my @names = grep(/^\Q${fq_hostname}.\E/o, map { split } (gethostbyname($fq_hostname))[0 .. 1] );
$fq_hostname = $names[0] if (@names); }
return $fq_hostname;
}
}
sub ips_match_in_16_mask {
my ($ipset1, $ipset2) = @_;
my ($b1, $b2);
foreach my $ip1 (@{$ipset1}) {
foreach my $ip2 (@{$ipset2}) {
next unless defined $ip1;
next unless defined $ip2;
next unless ($ip1 =~ /^(\d+\.\d+\.)/); $b1 = $1;
next unless ($ip2 =~ /^(\d+\.\d+\.)/); $b2 = $1;
if ($b1 eq $b2) { return 1; }
}
}
return 0;
}
sub ips_match_in_24_mask {
my ($ipset1, $ipset2) = @_;
my ($b1, $b2);
foreach my $ip1 (@{$ipset1}) {
foreach my $ip2 (@{$ipset2}) {
next unless defined $ip1;
next unless defined $ip2;
next unless ($ip1 =~ /^(\d+\.\d+\.\d+\.)/); $b1 = $1;
next unless ($ip2 =~ /^(\d+\.\d+\.\d+\.)/); $b2 = $1;
if ($b1 eq $b2) { return 1; }
}
}
return 0;
}
sub my_inet_aton { unpack("N", pack("C4", split(/\./, $_[0]))) }
sub parse_content_type {
my $ct = $_[-1] || 'text/plain; charset=us-ascii';
my($boundary) = $ct =~ m!\bboundary\s*=\s*("[^"]+|[^\s";]+(?=[\s;]|$))!i;
# remove double-quotes in boundary (should only be at start and end)
#
$boundary =~ tr/"//d if defined $boundary;
my($charset) = $ct =~ /\bcharset\s*=\s*["']?(.*?)["']?(?:;|$)/i;
my($name) = $ct =~ /\b(?:file)?name\s*=\s*["']?(.*?)["']?(?:;|$)/i;
# Get the actual MIME type out ...
# Note: the header content may not be whitespace unfolded, so make sure the
# REs do /s when appropriate.
# correct:
# Content-type: text/plain; charset=us-ascii
# missing a semi-colon, CT shouldn't have whitespace anyway:
# Content-type: text/plain charset=us-ascii
#
$ct =~ s/^\s+//; # strip leading whitespace
$ct =~ s/;.*$//s; # strip everything after first ';'
$ct =~ s@^([^/]+(?:/[^/\s]*)?).*$@$1@s; # only something/something ...
$ct = lc $ct;
# bug 4298: If at this point we don't have a content-type, assume text/plain;
# also, bug 5399: if the content-type *starts* with "text", and isn't in a
# list of known bad/non-plain formats, do likewise.
if (!$ct ||
($ct =~ /^text\b/ && $ct !~ /^text\/(?:x-vcard|calendar|html)$/))
{
$ct = "text/plain";
}
# strip inappropriate chars (bug 5399: after the text/plain fixup)
$ct =~ tr/\000-\040\177-\377\042\050\051\054\056\072-\077\100\133-\135//d;
# Now that the header has been parsed, return the requested information.
# In scalar context, just the MIME type, in array context the
# four important data parts (type, boundary, charset, and filename).
#
return wantarray ? ($ct,$boundary,$charset,$name) : $ct;
}
###########################################################################
sub url_encode {
my ($url) = @_;
my (@characters) = split(/(\%[0-9a-fA-F]{2})/, $url);
my (@unencoded) = ();
my (@encoded) = ();
foreach (@characters) {
# escaped character set ...
if (/\%[0-9a-fA-F]{2}/) {
# IF it is in the range of 0x00-0x20 or 0x7f-0xff
# or it is one of "<", ">", """, "#", "%",
unless (/(20|7f|[0189a-fA-F][0-9a-fA-F])/i) {
s/\%([2-7][0-9a-fA-F])/sprintf "%c", hex($1)/e;
push(@unencoded, $_);
}
}
else {
s/([\000-\040\177-\377\042\045\074\076])
/push(@encoded, $1) && sprintf "%%%02x", unpack("C",$1)/egx;
}
}
if (wantarray) {
return(join("", @characters), join("", @unencoded), join("", @encoded));
}
else {
return join("", @characters);
}
}
=item $module = first_available_module (@module_list)
Return the name of the first module that can be successfully loaded with
C<require> from the list. Returns C<undef> if none are available.
This is used instead of C<AnyDBM_File> as follows:
my $module = Mail::SpamAssassin::Util::first_available_module
(qw(DB_File GDBM_File NDBM_File SDBM_File));
tie %hash, $module, $path, [... args];
Note that C<SDBM_File> is guaranteed to be present, since it comes
with Perl.
=cut
sub first_available_module {
my (@packages) = @_;
foreach my $mod (@packages) {
if (eval 'require '.$mod.'; 1; ') {
return $mod;
}
}
undef;
}
=item my ($filepath, $filehandle) = secure_tmpfile();
Generates a filename for a temporary file, opens it exclusively and
securely, and returns a filehandle to the open file (opened O_RDWR).
If it cannot open a file after 20 tries, it returns C<undef>.
=cut
sub secure_tmpfile {
my $tmpdir = Mail::SpamAssassin::Util::untaint_file_path(
$ENV{'TMPDIR'} || File::Spec->tmpdir());
if (!$tmpdir) {
warn "util: cannot find a temporary directory, set TMP or TMPDIR in environment";
return;
}
my ($reportfile, $tmpfile);
my $umask = umask 077;
for (my $retries = 20; $retries > 0; $retries--) {
my $suffix = join('', (0..9,'A'..'Z','a'..'z')[rand 62, rand 62, rand 62,
rand 62, rand 62, rand 62]);
$reportfile = File::Spec->catfile($tmpdir,".spamassassin${$}${suffix}tmp");
if (sysopen($tmpfile, $reportfile, O_RDWR|O_CREAT|O_EXCL, 0600)) {
binmode $tmpfile;
last;
}
if ($!{EEXIST}) {
next;
}
warn "util: secure_tmpfile failed to create file '$reportfile': $!\n";
if ($tmpfile) {
close $tmpfile;
}
}
umask $umask;
if (!$tmpfile) {
warn "util: secure_tmpfile failed to create file, giving up";
return; }
return ($reportfile, $tmpfile);
}
=item my ($dirpath) = secure_tmpdir();
Generates a directory for temporary files. Creates it securely and
returns the path to the directory.
If it cannot create a directory after 20 tries, it returns C<undef>.
=cut
sub secure_tmpdir {
my $tmpdir = Mail::SpamAssassin::Util::untaint_file_path(File::Spec->tmpdir());
if (!$tmpdir) {
warn "util: cannot find a temporary directory, set TMP or TMPDIR in environment";
return;
}
my ($reportpath, $tmppath);
my $umask = umask 077;
for (my $retries = 20; $retries > 0; $retries--) {
my $suffix = join('', (0..9,'A'..'Z','a'..'z')[rand 62, rand 62, rand 62,
rand 62, rand 62, rand 62]);
$reportpath = File::Spec->catfile($tmpdir,".spamassassin${$}${suffix}tmp");
if (mkdir $reportpath, 0700) {
$tmppath = $reportpath;
last;
}
if ($!{EEXIST}) {
next;
}
warn "util: secure_tmpdir failed to create file '$reportpath': $!\n";
}
umask $umask;
warn "util: secure_tmpdir failed to create a directory, giving up" if (!$tmppath);
return $tmppath;
}
sub uri_to_domain {
my ($uri) = @_;
return if ($uri =~ /^javascript:/i);
$uri =~ s, $uri =~ s $uri =~ s,^[^/]*\@,,gs;
$uri =~ s,[/\?].*$,,gs;
$uri =~ s,:\d*$,,gs;
return if $uri =~ /\%(?:2[1-9a-fA-F]|[3-6][0-9a-fA-f]|7[0-9a-eA-E])/;
if ($uri !~ /^\d+\.\d+\.\d+\.\d+$/) {
$uri = Mail::SpamAssassin::Util::RegistrarBoundaries::trim_domain($uri);
return unless
(Mail::SpamAssassin::Util::RegistrarBoundaries::is_domain_valid($uri));
}
return lc $uri;
}
sub uri_list_canonify {
my($redirector_patterns, @uris) = @_;
my @nuris = ();
for my $uri (@uris) {
next if $uri =~ /^mailto:/i || $uri =~ /^[^:]*\@/;
$uri =~ s/\n//g;
$uri =~ s/^\s+//;
$uri =~ s/\s+$//;
$uri =~ s/\r//g;
my $nuri = $uri;
$nuri =~ tr@\\@/@;
$nuri =~ s
if ($nuri =~ /\%[0-9a-fA-F]{2}/) {
$nuri = Mail::SpamAssassin::Util::url_encode($nuri);
}
if ($nuri !~ /^[-_a-z0-9]+:/i) {
if ($nuri =~ /^ftp\./) {
$nuri =~ s@^@ftp://@g;
}
else {
$nuri =~ s@^@http://@g;
}
}
$nuri =~ s@^(https?://[^/?]+)\?@$1/?@i;
$nuri =~ s/\&\ $nuri =~ s/\&\
if ($nuri ne $uri) {
push(@nuris, $nuri);
}
if ($nuri =~ m{^(https?://)([^/]+?)((?::\d*)?\/.*)?$}i) {
my($proto, $host, $rest) = ($1,$2,$3);
$rest ||= '';
if ($host =~ tr/\000-\040\200-\377//d) {
push(@nuris, join ('', $proto, $host, $rest));
}
if ($rest =~ m{(https?:/{0,2}.+)$}i) {
push(@uris, $1);
}
else {
foreach (@{$redirector_patterns}) {
if ("$proto$host$rest" =~ $_) {
next unless defined $1;
dbg("uri: parsed uri pattern: $_");
dbg("uri: parsed uri found: $1 in redirector: $proto$host$rest");
push (@uris, $1);
last;
}
}
}
if ($host =~ s/^[^\@]+\@//gs) {
push(@nuris, join ('', $proto, $host, $rest));
}
if ($host =~ s/[^0-9A-Za-z]+$//) {
push(@nuris, join ('', $proto, $host, $rest));
}
if ($host =~ /^
((?:0x[0-9a-f]+|\d+)\.)
((?:0x[0-9a-f]+|\d+)\.)
((?:0x[0-9a-f]+|\d+)\.)
(0x[0-9a-f]+|\d+)
$/ix)
{
my @chunk = ($1,$2,$3,$4);
foreach my $octet (@chunk) {
$octet =~ s/^0x([0-9a-f]+)/sprintf "%d",hex($1)/gei;
$octet =~ s/^0+([1-3][0-7]{0,2}|[4-7][0-7]?)\b/sprintf "%d",oct($1)/ge;
$octet =~ s/^0+//;
}
push(@nuris, join ('', $proto, @chunk, $rest));
}
elsif ($host =~ /^0x[0-9a-f]+$/i) {
$host =~ s/^0x[0-9a-f]*?([0-9a-f]{1,8})$/sprintf "%d",hex($1)/gei;
push(@nuris, join ('', $proto, decode_ulong_to_ip($host), $rest));
}
elsif ($host =~ /^[0-9]+$/) {
push(@nuris, join ('', $proto, decode_ulong_to_ip($host), $rest));
}
}
}
my %uris = map { $_ => 1 } @uris, @nuris;
return keys %uris;
}
sub decode_ulong_to_ip {
return join(".", unpack("CCCC",pack("H*", sprintf "%08lx", $_[0])));
}
sub first_date {
my (@strings) = @_;
foreach my $string (@strings) {
my $time = parse_rfc822_date($string);
return $time if defined($time) && $time;
}
return undef;
}
sub receive_date {
my ($header) = @_;
$header ||= '';
$header =~ s/\n[ \t]+/ /gs;
my @rcvd = ($header =~ /^Received:(.*)/img);
my @local;
my $time;
if (@rcvd) {
if ($rcvd[0] =~ /qmail \d+ invoked by uid \d+/ ||
$rcvd[0] =~ /\bfrom (?:localhost\s|(?:\S+ ){1,2}\S*\b127\.0\.0\.1\b)/)
{
push @local, (shift @rcvd);
}
if (@rcvd && ($rcvd[0] =~ m/\bby localhost with \w+ \(fetchmail-[\d.]+/)) {
push @local, (shift @rcvd);
}
elsif (@local) {
unshift @rcvd, (shift @local);
}
}
if (@rcvd) {
$time = first_date(shift @rcvd);
return $time if defined($time);
}
if (@local) {
$time = first_date(@local);
return $time if defined($time);
}
if ($header =~ /^(?:From|X-From-Line:)\s+(.+)$/im) {
my $string = $1;
$string .= " ".local_tz() unless $string =~ /(?:[-+]\d{4}|\b[A-Z]{2,4}\b)/;
$time = first_date($string);
return $time if defined($time);
}
if (@rcvd) {
$time = first_date(@rcvd);
return $time if defined($time);
}
if ($header =~ /^Resent-Date:\s*(.+)$/im) {
$time = first_date($1);
return $time if defined($time);
}
if ($header =~ /^Date:\s*(.+)$/im) {
$time = first_date($1);
return $time if defined($time);
}
return time;
}
sub setuid_to_euid {
return if (RUNNING_ON_WINDOWS);
my $touid = $>;
if ($< != $touid) {
dbg("util: changing real uid from $< to match effective uid $touid");
$< = $touid;
if ( $< != $touid ) {
dbg("util: initial attempt to change real uid failed, trying BSD workaround");
$> = $<; $< = $touid; $> = $touid; }
if ($< != $touid) {
die "util: setuid $< to $touid failed!";
}
}
}
sub helper_app_pipe_open {
if (RUNNING_ON_WINDOWS) {
return helper_app_pipe_open_windows (@_);
} else {
return helper_app_pipe_open_unix (@_);
}
}
sub helper_app_pipe_open_windows {
my ($fh, $stdinfile, $duperr2out, @cmdline) = @_;
my $cmd = join(' ', @cmdline);
if ($stdinfile) { $cmd .= qq/ < "$stdinfile"/; }
if ($duperr2out) { $cmd .= " 2>&1"; }
return open ($fh, $cmd.'|');
}
sub force_die {
my ($msg) = @_;
eval { warn $msg; };
POSIX::_exit(1); kill('KILL',$$); }
sub helper_app_pipe_open_unix {
my ($fh, $stdinfile, $duperr2out, @cmdline) = @_;
my $pid = open ($fh, '-|');
if (!defined $pid) {
die "util: cannot fork: $!";
}
if ($pid != 0) {
return $pid; }
setuid_to_euid();
dbg("util: setuid: ruid=$< euid=$>");
if (!$stdinfile) { $stdinfile = "/dev/null";
}
my $f = fileno(STDIN);
close STDIN;
if ($f != 0) {
POSIX::close(0);
}
open (STDIN, "<$stdinfile") or force_die "util: cannot open $stdinfile: $!";
if (fileno(STDIN) != 0) {
force_die "util: setuid: oops: fileno(STDIN) [".fileno(STDIN)."] != 0";
}
if ($duperr2out) { my $f = fileno(STDERR);
close STDERR;
if ($f != 2) {
POSIX::close(2);
}
open (STDERR, ">&STDOUT") or force_die "util: dup STDOUT failed: $!";
if (fileno(STDERR) != 2) {
force_die "util: oops: fileno(STDERR) [".fileno(STDERR)."] != 2";
}
}
exec @cmdline;
warn "util: exec failed: $!";
POSIX::_exit(1); kill('KILL',$$); die; }
sub trap_sigalrm_fully {
my ($handler) = @_;
if ($] < 5.008) {
$SIG{ALRM} = $handler;
} else {
POSIX::sigaction POSIX::SIGALRM(), new POSIX::SigAction $handler;
}
}
sub regexp_remove_delimiters {
my ($re) = @_;
my $delim;
if (!defined $re || $re eq '') {
warn "cannot remove delimiters from null regexp";
return undef; }
elsif ($re =~ s/^m{//) { # m{foo/bar}
$delim = '}';
}
elsif ($re =~ s/^m\(//) { # m(foo/bar)
$delim = ')';
}
elsif ($re =~ s/^m<//) { # m<foo/bar>
$delim = '>';
}
elsif ($re =~ s/^m(\W)//) { # m#foo/bar#
$delim = $1;
} else { $re =~ s/^(\W)//; $delim = $1;
}
$re =~ s/\Q${delim}\E([imsx]*)$// or warn "unbalanced re: $re";
my $mods = $1;
if ($mods) {
$re = "(?".$mods.")".$re;
}
return $re;
}
sub make_qr {
my ($re) = @_;
$re = regexp_remove_delimiters($re);
return qr/$re/;
}
sub get_my_locales {
my ($ok_locales) = @_;
my @locales = split(' ', $ok_locales);
my $lang = $ENV{'LC_ALL'};
$lang ||= $ENV{'LANGUAGE'};
$lang ||= $ENV{'LC_MESSAGES'};
$lang ||= $ENV{'LANG'};
push (@locales, $lang) if defined($lang);
return @locales;
}
1;
=back
=cut