summaryrefslogtreecommitdiff
path: root/bin/create-fetchmailrc
blob: 11bde0ce344ec480f68738fe5cd87a862a93b79c (plain)
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
#!/usr/bin/perl -w
# this quick hack helps you generate/maintain .fetchmailrc files from
# FS::acct_snarf data.  it is run from a shellcommands export as:
# create-fetchmailrc $username $dir $snarf_machine1 $snarf_username1 $snarf__password1 $snarf_machine2 $snarf_username2 $snarf__password2 ...

use strict;
use POSIX qw( setuid setgid );

my $header = <<END;
# Configuration created by create-fetchmailrc
set postmaster "postmaster"
set bouncemail
set no spambounce
set properties ""
set daemon 240
END

my $username = shift @ARGV or die "no username specified\n";
my $homedir  = shift @ARGV or die "no homedir specified\n";
my $filename = "$homedir/.fetchmailrc";

my $gid = scalar(getgrnam($username)) or die "can't find $username's gid\n";
my $uid = scalar(getpwnam($username)) or die "can't find $username's uid\n";

exit unless $ARGV[0];

open(FETCHMAILRC, ">$filename") or die "can't open $filename: $!\n";
chown $uid, $gid, $filename     or die "can't chown $uid.$gid $filename: $!\n";
chmod 0600, $filename           or die "can't chmod 600 $filename: $!\n";
print FETCHMAILRC $header;

while ($ARGV[0]) {
  my( $s_machine, $s_username, $s_password ) = splice( @ARGV, 0, 3 );
  print FETCHMAILRC <<END;
poll $s_machine
	user '$s_username' there with password '$s_password' is '$username' here
END
}

close FETCHMAILRC;

setgid($gid) or die "can't setgid $gid\n";
setuid($uid) or die "can't setuid $uid\n";
$ENV{HOME} = $homedir;

system(qq(fetchmail -a -K --antispam "550,451" -d 180 -f $filename));