#!/usr/bin/perl -w
use 5.006;
use Getopt::Std;
use File::Basename;
use Socket;
use IO::Socket::INET;
use Digest::MD5 'md5_hex';
$ENV{PATH} .= ':/sbin:/usr/sbin';
$usage = "usage: $0 [-p port] [IP-address]\n";
$xinetd = '/etc/xinetd.d/fex';
umask 022;
if ($<) {
die "you must be root to install F*EX\n";
}
goto INSTALL if $0 =~ /upgrade$/;
$fex = 'fex.rus.uni-stuttgart.de';
if (system("host $fex >/dev/null") != 0) {
die "host $fex is not resolvable - check /etc/resolv.conf\n";
}
if (`uname` =~ /^SunOS/) {
die "Solaris is currently not supported. "
."Please contact framstag\@rus.uni-stuttgart.de for details.\n";
}
$opt_p = 80;
if (open $xinetd,$xinetd) {
while (<$xinetd>) {
if (/^\s*port\s*=\s*(\d+)/) {
$opt_p = $fexport = $1;
}
if (/^\s*bind\s*=\s*([\d.]+)/) {
$fexip = $ip = $1;
}
}
close $xinetd;
}
getopts('p:') or die $usage;
$arg = shift;
if ($arg and -f "locale/$arg/lib/fup.pl") {
exec 'locale/translate',$arg;
} else {
$ip = $arg || $fexip || 0;
}
# if (not $ip and open P,"ifconfig 2>/dev/null |") {
if (not $ip and open P,'host $(hostname)|') {
$guessed_ip = 0;
while (
) {
if (/(\d+\.\d+\.\d+\.\d+)/) {
$guessed_ip = $1;
last;
}
}
close P;
print "Your IP [$guessed_ip] : ";
chomp($ip = );
$ip ||= $guessed_ip;
}
$ip =~ /^\d+\.\d+\.\d+\.\d+$/ or die $usage;
($hostname) = gethostbyaddr(gethostbyname($ip),AF_INET);
die "cannot find hostname for IP $ip\n" unless $hostname;
print "checking prerequisites\n";
if (`which xinetd` =~ m{^/}) {
print "found xinetd\n";
} else {
print "xinetd executable NOT found\n";
$premiss++;
}
foreach (qw'/usr/lib/sendmail /usr/sbin/sendmail') {
if (-x) {
$sendmail = $_;
print "found $sendmail\n";
last;
}
}
unless ($sendmail) {
print "sendmail NOT found\n";
$premiss++;
}
if ($premiss) {
print "installation aborted, nothing has been touched yet\n";
print "what now? ==> see doc/installation\n";
exit 1;
}
unless ($fexport) {
$SH = IO::Socket::INET->new(
PeerAddr => $ip,
PeerPort => $opt_p,
Proto => 'tcp',
);
if ($SH) {
print "There is already a tcp-service running on $ip:$opt_p !\n";
print "Select another port for F*EX by running $0 -p OTHERPORT $ip\n";
print "or an alternative IP-address by running $0 OTHERADDRESS\n";
exit 5;
}
}
print "prerequisites checked, ok\n";
unless (getpwnam('fex')) {
print "creating user fex\n";
system 'useradd -s /bin/bash -c "File EXchange" -m fex';
exit $? if $?;
}
if (open F,'/etc/passwd') {
while () {
$fexbash = $_ if /^fex:.*\/bash/;
}
close F;
}
unless ($fexbash) {
die "no bash login shell for user fex\n";
}
INSTALL:
umask 077;
@FEX = getpwnam('fex') or die "no user fex\n";
$FEXHOME = $FEX[7];
die "no HOME directory for user fex\n" unless -d $FEXHOME;
print "Installing:\n";
@save = (
"lib/fex.ph",
"lib/fup.pl",
"lib/reactivation.txt",
"etc/mime.types",
"htdocs/index.html",
"htdocs/robots.txt",
"htdocs/FAQ/local.faq",
);
foreach $s (@save) {
$f = "$FEXHOME/$s";
if (-e $f) {
$fs = $f.'_save';
rename $f,$fs and print "$f --> $fs\n";
}
}
cpav(qw'bin cgi-bin lib etc htdocs doc',$FEXHOME);
unlink "$FEXHOME/doc/License";
unlink "$FEXHOME/htdocs/License";
$hl = "$FEXHOME/htdocs/locale";
unless (-d $hl) { mkdir $hl or die "$0: cannot mkdir $hl - $!\n" }
if (-d "$FEXHOME/spool") {
warn "checking spool ...\n";
&convert_spool;
system "chown -R fex $spooldir/";
} else {
$newinstall = $FEXHOME;
chmod 0700,$FEXHOME;
mkdir "$FEXHOME/spool",0700 or die "cannot mkdir $FEXHOME/spool - $!\n";
mkdir "$FEXHOME/spool/.error",0700;
system "chown -R fex $FEXHOME/spool";
}
foreach $s (@save) {
$f = "$FEXHOME/$s";
$fs = $f.'_save';
$fn = $f.'_new';
if (-e $fs) {
system "rm -rf $fn";
rename $f,$fn and print "$f --> $fn\n";
rename $fs,$f and print "$fs --> $f\n";
}
}
system(qw'perl -p -i -e',
's:href="/?FAQ.html":href="/FAQ/FAQ.html":',
"$FEXHOME/lib/fup.pl"
);
$fph = "$FEXHOME/lib/fex.ph";
open $fph,$fph or die "cannot read $fph - $!\n";
while (<$fph>) {
s/'MYHOSTNAME.MYDOMAIN'/'$hostname'/;
$conf .= $_;
}
close $fph;
eval $conf;
$spooldir ||= "$FEXHOME/spool";
$fid = "$FEXHOME/.fex/id";
$aa = "$spooldir/$admin/@";
if ($newinstall) {
print "\n";
for (;;) {
print "Server hostname [$hostname] : ";
$_ = ;
s/\s//g;
$hostname = $_ if $_;
last if gethostbyname($hostname);
print "No DNS for $hostname\n";
}
for (;;) {
print "F*EX admin [$admin] : ";
$_ = ;
s/\s//g;
$admin = $_ if $_;
last if $admin =~ /.\@./;
print "admin must be a valid email address!\n";
}
while (not $admin_pw) {
print "F*EX admin password: ";
$admin_pw = ;
$admin_pw =~ s/\s//g;
}
mkfid();
print "(admin password is in $aa)\n";
$conf =~ s/^\s*\$hostname\s*=.*/\$hostname = '$hostname';/m;
$conf =~ s/^\s*\$admin\s*=.*/\$admin = '$admin';/m;
} else {
if ($admin_pw) {
print "\nFound old \$admin_pw in $fph !\n";
print "This is no longer supported for security reason.\n";
if (open $aa,$aa) {
$_ = <$aa>||'';
chomp;
close $aa;
if ($_ ne $admin_pw) {
print "\nYou have to delete \$admin_pw in $fph and run\n";
print "$FEXHOME/bin/fac -u $admin $admin_pw\n";
print "\nThen rerun $0\n";
exit 2;
}
}
mkfid();
print "\$admin_pw is transfered to auth-ID in $aa\n\n";
$conf =~ s/^\s*(\$admin_pw)\s*=.*/# $1 is now auth_ID of user \$admin/m;
}
}
sub mkfid {
my $ad = dirname($aa);
mkdir $ad;
open $aa,'>',$aa or die "$0: cannot create $aa - $!\n";
print {$aa} "$admin_pw\n";
close $aa;
my $fd = dirname($fid);
mkdir $fd;
rename $fid,$fid.'_save';
open $fid,'>',$fid or die "$0: cannot create $fid - $!\n";
print {$fid} "$hostname:$opt_p\n";
print {$fid} "$admin\n";
print {$fid} "$admin_pw\n";
close $fid;
chmod 0700,$fd;
system "chown -R fex $fd $ad";
}
open $fph,">$fph.new" or die "$0: cannot write $fph.new - $!\n";
print {$fph} $conf;
close $fph;
system "chown fex $fph.new";
rename "$fph.new",$fph or die "$0: cannot rename $fph.new to $fph - $!\n";
do $fph or die "$0: error in new $fph - $!\n";
rename "locale/deutsch","locale/german" if -d "locale/deutsch";
rename "locale/espanol","locale/spanish" if -d "locale/espanol";
if (@locales = glob "locale/*/lib/fup.pl") {
foreach (@locales) {
m{locale/(.+?)/} and $locale = $1;
if (-f "$FEXHOME/$_") {
system 'locale/translate',$locale;
system "chown -R fex $FEXHOME/locale/$locale";
$hl = "$FEXHOME/htdocs/locale/$locale";
symlink "$FEXHOME/locale/$locale/htdocs",$hl unless -l $hl;
} else {
push @nlocales,"./install $1\n";
}
}
if (@nlocales) {
if (glob "$FEXHOME/locale/*/lib/fup.pl") {
print "\nTo install another localized version, type:\n";
} else {
print "\nTo install a localized version, type:\n";
}
print @nlocales;
}
}
$fph = "$FEXHOME/lib/fex.ph";
do $fph;
unless (-f $xinetd) {
my $xc = '/etc/xinetd.conf';
if (open $xc,$xc) {
while (<$xc>) {
if (/^\s*only_from/) {
print "WARNING: found \"only_from\" in $xc : fexsrv is restricted!\n";
}
}
close $xc;
}
if (-d '/etc/xinetd.d') {
unless (-f $xinetd) {
open $xinetd,">$xinetd" or die "cannot write $xinetd - $!\n";
open F,'etc/xinetd_fex' or die "cannot read etc/xinetd_fex - $!\n";
while () {
s/FEXHOME/$FEXHOME/;
s/PORT/$opt_p/;
s/ADDRESS/$ip/;
print {$xinetd} $_;
}
close F;
close $xinetd;
system qw'/etc/init.d/xinetd restart';
print "WARNING: cannot restart xinetd\n" if $?;
}
} else {
print "WARNING: No /etc/xinetd.d found.\n";
print "WARNING: You have to install etc/xinetd_fex manually.\n";
}
$crontab = `crontab -u fex -l 2>/dev/null`;
if ($crontab !~ /fex_cleanup/) {
open $crontab,">fex.cron" or die "cannot create fex.cron - $!\n";
print {$crontab} $crontab,"\n";
print {$crontab} " 3 3 * * * exec $FEXHOME/bin/fex_cleanup\n";
close $crontab;
system qw(crontab -u fex fex.cron);
}
system "chown -R fex:root $FEXHOME $FEXHOME/spool/";
system "chmod -R go-r $FEXHOME/lib $FEXHOME/cgi-bin $FEXHOME/spool/";
print "\n";
print "Now check configuration file $FEXHOME/lib/fex.ph and run\n";
print "$FEXHOME/bin/fac for further configuration and user management.\n";
print "(You can do this as user \"fex\")\n";
} else {
system "chmod -R go-r $FEXHOME/lib $FEXHOME/cgi-bin";
print "\n";
print "F*EX update installed.\n";
print "You can inform your users about the new features with:\n";
print "$FEXHOME/bin/fexwall 'new F*EX features on $hostname' ".
"< $FEXHOME/doc/newfeatures\n";
}
if (@local_rdomains and not @local_rhosts) {
print "\nWARNING:\n";
print "In $fph you have @local_rdomains but not @local_rhosts!\n";
print "Selfregistrating of external users will not work!\n";
print "See ${fph}_new/\n";
}
if (`$sendmail -h 2>&1` =~ /exim/ and
`grep trusted_users /etc/exim4/exim4.conf 2>/dev/null` !~ /\bfex\b/) {
print "\nWARNING:\n";
print "$sendmail is exim\n";
print "You MUST set in your exim4.conf:\n";
print "trusted_users = mail : uucp : fex\n";
}
exit;
sub convert_spool {
my ($f,$d,$to,$from,$link);
local $) = $FEX[3];
local $> = $FEX[2];
our ($spooldir,$skeydir,$gkeydir);
$ENV{FEXLIB} = $FEXLIB = "$FEXHOME/lib";
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
# User --> user@maildomain
if ($mdomain) {
foreach $f (glob "$spooldir/.dkeys/*") {
if ($link = readlink $f) {
(undef,$to,$from,$file) = split('/',$link);
if ($file) {
$to .= '@'.$mdomain if $to !~ /@/;
$from .= '@'.$mdomain if $from !~ /@/;
if ($link ne "../$to/$from/$file") {
symlink "../$to/$from/$file",$f;
}
}
}
}
}
# fix spool layout: FROM and TO must have domains and must be lower case
foreach $d ((glob "$spooldir/*/*"),(glob "$spooldir/*")) {
if (not -l $d and -d $d and $d =~ m:(.+)/(.+):) {
$p = $1;
$b = $2;
if ($b !~ /^@/ and $b !~ /^[A-Z_-]+$/) {
if ($mdomain and $b !~ /@/) {
rename $d,sprintf("%s/%s@%s",$p,lc($b),$mdomain);
} elsif ($b ne lc($b)) {
rename $d,sprintf("%s/%s",$p,lc($b));
}
}
}
}
# split auth-ID and subuser file: @ --> @ @SUBUSER
foreach my $u (glob "$spooldir/*@*") {
next if -f "$u/\@SUBUSER";
open my $idf,"$u/\@" or next;
$id = <$idf>;
if (defined ($su = <$idf>) and $su =~ /\w/
and open my $suf,">$u/\@SUBUSER") {
print {$suf} $su;
while (defined ($su = <$idf>)) { print {$suf} $su }
close $suf;
close $idf;
if (open my $idf,">$u/\@") {
print {$idf} $id;
close $idf;
}
}
}
# create new SKEYs
foreach my $sf (glob "$spooldir/*/\@SUBUSER") {
$user = (split '/',$sf)[-2];
if (open $sf,$sf) {
while (<$sf>) {
s/#.*//;
if (/(.+\@.+):(.+)/) {
($subuser,$id) = ($1,$2);
next if $subuser =~ /\*/;
$skey = md5_hex("$user:$subuser:$id");
if (open $skey,'>',"$skeydir/$skey") {
print {$skey} "from=$subuser\n",
"to=$user\n",
"id=$id\n";
close $skey;
}
mkdirp("$spooldir/$subuser/\@MAINUSER");
symlink $skey,"$spooldir/$subuser/\@MAINUSER/$user";
}
}
}
close $sf;
}
# create new GKEYs
foreach my $gf (glob "$spooldir/*/\@GROUP/*") {
next unless -f $gf;
$group = (split '/',$gf)[-1];
$user = (split '/',$gf)[-3];
if (open $gf,$gf) {
while (<$gf>) {
s/#.*//;
if (/(.+\@.+):(.+)/) {
($gm,$id) = ($1,$2);
$gkey = md5_hex("$user:$group:$gm:$id");
if (open $gkey,'>',"$gkeydir/$gkey") {
print {$gkey} "from=$gm\n",
"to=\@$group\n",
"user=$user\n",
"id=$id\n";
close $gkey;
}
mkdirp("$spooldir/$gm/\@GROUP");
symlink "../../$user/\@GROUP/$group","$spooldir/$gm/\@GROUP/$group";
}
}
}
close $gf;
}
}
sub cpav {
my $dd = pop @_;
local *P;
die "cpav: $dd is not a directory" unless -d $dd;
open P,"tar cf - @_ | su -c 'cd $dd; umask 022; tar xvf - 2>&1' fex |"
or die "cpav: cannot tar - $!\n";
while () {
chomp;
print "$_ --> $dd/$_\n" unless /\/$/;
}
close P;
}