#!/usr/local/bin/perl
#
# makevirtual v.0.2
# Matthew N. Dodd <winter@jurai.net>
# May 13, 1996
#
# generate the virtualdomains.db file from the template files
# specified in /etc/virtualdomains.conf
#
# Format for /etc/virtualdomains.conf
#
#	#domain		username	path to config file
#	foo.com		foo		/home/foo
#
# Config file in /home/foo is named $username.email
# Format for $username.email is
#
#	#username	valid email addy to map username to
#	username	username@bar.com
#
# Consistancy checking will be done on the $username.email for
# invalid characters, and for proper format of variables.
# processing will ignore those lines that have invalid
# characters/fields.  Users should run the consistancy 
# checking program on their config files to find errors.
# (note: I have to write this first)
#
# All config files honor comments using the '#' character.
#
# command to make the maps
# cat /etc/virtualdomains | makemap hash /etc/virtualdomains.db

# this must be set to a vaild host that can deliver to
# username in virtualdomains.conf, as we will add 
# a wildcard entry automatically.

$this_host = "intersurf.com";
$SENDMAIL = "/usr/sbin/sendmail";
$m_fullname = "Virtual Domains Subsystem Daemon";
$m_from = "virtual-domains\@intersurf.com";

open(VCONFIG, "/etc/virtualdomains.conf");

while($v_line = <VCONFIG>) {

	chop $v_line;
	if (grep(/^#/, ($v_line))) { next; }

	($udom, $uname, $uconfig) = split(/\s+/, $v_line);

	if (!open(UCONFIG, "$uconfig")) {
		print "\n# *** Unable to open $uconfig ***\n\n"; 
		next;
	}

	$time = 0 ; $mtime = 0;

	$time = time();
	($dev, $inc, $mode, $nlink, $uid, $gid, $rdev, $size,
	$atime, $mtime, $ctime, $blksize, $blocks) = stat(UCONFIG);

	print "#\n# $udom - $uconfig\n#\n";

	if (($mtime + 3600) >= $time) {
		open(MAIL, "| $SENDMAIL -F\"$m_fullname\" -f$m_from $uname\@$this_host");
	} else {
		open(MAIL, "/dev/null");
	}

	print MAIL "To: $uname\@$this_host\n";
	print MAIL "Subject: Virtual Domain Email Configuration Processing Report\n\n";
	print MAIL "Domain: $udom\n";
	print MAIL "Username: $uname\n";
	print MAIL "Config File: $uconfig\n";
	print MAIL "\n";

	$line = 0;
	$errs = 0;
	while($u_line = <UCONFIG>) {

		$line++;

		chop $u_line;
		if (grep(/^#/, ($u_line))) { next; }
	
		($username, $emailaddy) = split(/\s+/, $u_line);

		if (grep(/[`~!\@#\$%^&*()=\[\]{};'\:"|,\/\\?]/, ($username))) {
			print "# >>> ERR-CINVAL: Invalid characters in username '$username'.\n";
			print "# >>> $line : $u_line\n";
			print MAIL ">>> ERR-CINVAL: Invalid characters in username '$username'.\n";
			print MAIL ">>> $line : $u_line\n";
			$errs++;
			next;
		}

		if (!grep(/@/, $emailaddy)) {
			print "# >>> ERR-INVAL: Invalid email address '$emailaddy'.\n";
			print "# >>> $line : $u_line\n";
			print MAIL ">>> ERR-INVAL: Invalid email address '$emailaddy'.\n";
			print MAIL ">>> $line : $u_line\n";
			$errs++;
			next;
		}

		($e_user, $e_dom) = split(/@/, $emailaddy);

		if (grep(/[`~!\@#\$%^&*()=\[\]{};'\:"|,\/\\?]/, ($e_user))) {
			print "# >>> ERR-CINVAL: Invalid characters in username '$e_user'.\n";
			print "# >>> $line : $u_line\n";
			print MAIL ">>> ERR-CINVAL: Invalid characters in username '$e_user'.\n";
			print MAIL ">>> $line : $u_line\n";
			$errs++;
			next;
		}
		if (grep(/[`~!\@#\$%^&*()=\[\]{};'\:"|,\/\\?]/, ($e_dom))) {
			print "# >>> ERR-CINVAL: Invalid characters in domain '$e_dom'.\n";
			print "# >>> $line : $u_line\n";
			print MAIL ">>> ERR-CINVAL: Invalid characters in domain '$e_dom'.\n";
			print MAIL ">>> $line : $u_line\n";
			$errs++;
			next;
		}
		if ($udom eq $e_dom) {
			print "# >>> ERR-LOOP: Possiable address loop.\n";
			print "# \$udom $udom \$e_dom $e_dom\n";
			print "# >>> $line : $u_line\n";
			print MAIL ">>> ERR-LOOP: Possiable address loop.\n";
			print MAIL ">>> $line : $u_line\n";
			$errs++;
			next;
		}
		if (($e_user eq $uname) && ($e_dom eq $this_host)) {
			print "# >>> ERR-UMAP: Unnecessary mapping.\n";
			print "# >>> $line : $u_line\n";
			print MAIL ">>> ERR-UMAP: Unnecessary mapping.\n";
			print MAIL ">>> $line : $u_line\n";
			$errs++;
			next; 
		}

		print "$username\@$udom\t$e_user\@$e_dom\n";
		print MAIL "\nAdding entry : $username\@$udom\t$e_user\@$e_dom\n\n";
	}

	# final entry
	print "$udom\t$uname\@$this_host\n";
	print MAIL "Adding default entry: $udom\t$uname\@$this_host\n";

	# print number of errors found.
	if ($errs > 0) {
		print "# >>> $errs errors found.\n";
	}
	print MAIL ">>> $errs errors found.\n";
	print MAIL "\n";

	close(UCONFIG);
	close(MAIL);
	print "\n";
}

close(VCONFIG);
