#!/usr/bin/perl -w
#-----------------------------------------------------------------------------
#
#  POPular -- A POP3 server and proxy for large mail systems
#
#  $Id: check-setup,v 1.3 2001/03/23 12:19:12 sqrt Exp $
#
#  http://www.remote.org/jochen/mail/popular/
#
#-----------------------------------------------------------------------------
#
#  Copyright (C) 1999-2001  Jochen Topf <jochen@remote.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
#
#-----------------------------------------------------------------------------
#
#  Usage: check-setup <options>
#
#  -p           check for pproxy setup
#  -s           check for server setup
#  -c <dir>     configuration directory
#  -u <user>    user
#  -g <group>   group
#
#-----------------------------------------------------------------------------

use strict;
use Getopt::Std;

my %opts;
getopts('psc:u:g:', \%opts);

my $PROXY  = $opts{'p'};
my $SERVER = $opts{'s'};
my $CFGDIR = $opts{'c'};
my $USER   = $opts{'u'};
my $GROUP  = $opts{'g'};


#-----------------------------------------------------------------------------
if (! $PROXY && ! $SERVER) {
  print <<"EOF";
Do you want to run a POPular proxy on this machine? (Y/n)
EOF
  my $answer = <STDIN>;
  if ($answer =~ /^y?$/i) {
    $PROXY = 1;
  }
  print <<"EOF";
Do you want to run a POPular server on this machine? (Y/n)
EOF
  $answer = <STDIN>;
  if ($answer =~ /^y?$/i) {
    $SERVER = 1;
  }
}

if (! $PROXY && ! $SERVER) {
  print <<"EOF";
Running neither the server nor the proxy doesn't make sense.
EOF
  exit 1;
}

if (! $CFGDIR) {
  print <<"EOF";
What is the main configuration directory? ('/etc/popular')
EOF
  $CFGDIR = <STDIN>;
  chomp $CFGDIR;
  $CFGDIR = '/etc/popular' if ($CFGDIR eq '');
}

if (! $USER) {
  print <<"EOF";
What user should be used to run the POPular programs? ('pop')
EOF
  $USER = <STDIN>;
  chomp $USER;
  $USER = 'pop' if ($USER eq '');
  if ($USER eq 'root') {
    print "User 'root' is not allowed\n";
    exit 1;
  }
}

if (! $GROUP) {
  print <<"EOF";
What group should be used to run the POPular programs? ('pop')
EOF
  $GROUP = <STDIN>;
  chomp $GROUP;
  $GROUP = 'pop' if ($GROUP eq '');
  if ($GROUP eq 'root') {
    print "Group 'root' is not allowed\n";
    exit 1;
  }
}

#-----------------------------------------------------------------------------

my $proxservopts = $PROXY ? " -p" : "" . $SERVER ? " -s" : "";
print <<"EOF";
#-----------------------------------------------------------------------------
# This program is now configured as follows:
#
EOF
  print "# Check for proxy setup ......... ", $PROXY ? "Yes" : "No", "\n";
  print "# Check for server setup ........ ", $SERVER ? "Yes" : "No", "\n";
print <<"EOF";
# Main configuration directory .. $CFGDIR
# POPular user .................. $USER
# POPular group ................. $GROUP
#
# To get the same settings the next time you can start the program as follows:

$0$proxservopts -c $CFGDIR -u $USER -g $GROUP

#-----------------------------------------------------------------------------
# This program will now check your setup and recommend changes if problems are
# found. Please execute all commands given as user 'root'.
#-----------------------------------------------------------------------------
EOF

#-----------------------------------------------------------------------------

check_dir($CFGDIR, 'root', 0, 'root', 0, '0755');

my ($PIDFILE, $STATEFILE, $DBDIR);

if ($PROXY) {
  open(PPROXYRC, "$CFGDIR/pproxy.rc") or die("Can't open $CFGDIR/pproxy.rc: $!");
  print "# Found $CFGDIR/pproxy.rc\n";
  while (<PPROXYRC>) {
    next if (/^$/);
    next if (/^#/);
    next if (/^set backlog [0-9]{1,3}$/);
    next if (/^set capadir [0-9a-zA-Z_.\/-]+$/);
    next if (/^set checkport [0-9]{1,5}$/);
    next if (/^set checktimeout [0-9]{1,2}$/);
    next if (/^set defaultns [0-9a-zA-Z_.-]+$/);
    next if (/^set fallback [0-9a-zA-Z_.-]+$/);
    next if (/^set logfile [0-9a-zA-Z_.\/-]+$/);
    next if (/^set maxlocalload [0-9]{1,4}$/);
    next if (/^set maxsession [0-9]{1,5}$/);
    next if (/^set pdmdir [0-9a-zA-Z_.\/-]+$/);
    if (/^set pidfile (.*)$/) {
      $PIDFILE = $1;
      next;
    }
    next if (/^set readtimeout [0-9]{1,5}$/);
    if (/^set sockfile (.*)$/) {
      $DBDIR = $1;
      next;
    }
    if (/^set statefile (.*)$/) {
      $STATEFILE = $1;
      next;
    }
    next if (/^backend /);
    next if (/^vserv /);
    print <<"EOF";
# I don't understand the following line in your pproxy.rc. This might or might
# not be an error:
# $_
EOF
  }
}

exit 0;


#-----------------------------------------------------------------------------
#
#  check_dir($filename, $user, $fuid, $group, $fgid, $fmode)
#
#-----------------------------------------------------------------------------
sub check_dir {
  my ($filename, $user, $fuid, $group, $fgid, $fmode) = @_;

  my @stats = stat($filename);

  if (! defined @stats) {
    print <<"EOF";
# Directory '$filename' doesn't exist. Create it with:
mkdir $filename
chown $user $filename
chgrp $group $filename
chmod $fmode $filename
EOF
    exit 1;
  }

  my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
	$blksize,$blocks) = @stats;

  my $pmode = $mode &   07777;
  my $type  = $mode & 0170000;

  if ($type != 040000) {
    print <<"EOF";
# '$filename' is not a directory
EOF
    exit 1;
  }

  if ($pmode != oct($fmode)) {
    print <<"EOF";
# '$filename' has the wrong permissions. Fix this with:
chmod $fmode $filename
EOF
    exit 1;
  }

  if ($uid != $fuid) {
    print <<"EOF";
# '$filename' has the wrong owner. Fix this with:
chown $user $filename
EOF
    exit 1;
  }

  if ($gid != $fgid) {
    print <<"EOF";
# '$filename' has the wrong group. Fix this with:
chgrp $group $filename
EOF
    exit 1;
  }
}


#-- THE END ------------------------------------------------------------------


syntax highlighted by Code2HTML, v. 0.9.1