#!/usr/bin/perl   

########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

use vars qw/$majordomo_user $majordomo_ldir $majordomo_home $mta $min_uid/;

use strict;

sub checkAndCreateD{
  my ($path,$uid,$gid,$mod)=@_;
  unless(-d $path){
	$mod = 0755 unless $mod; 
	mkdir($path,$mod) or &soft_error("mlf||scripts_dir_create||#1187||$path");
	chown $uid,$gid,$path;
  }
}

sub checkAndCreateF{
  my ($path,$uid,$gid,$mod)=@_;
  unless(-e $path){
	if (open (FILE,">$path")){
	  close (FILE);
	  	
	  chmod ($mod,$path) if $mod;
	  chown $uid,$gid,$path;
	}
	else {
	  die "Can't open file '$path' for writing : $!\n";
	}  
  }
}

sub createMajoDomainTree {
  
  my($domain_id,$majordomo_uid,$majordomo_gid)=@_;
  
  my ($login,$pass);
  
  unless($majordomo_uid && $majordomo_gid){
	($login,$pass,$majordomo_uid,$majordomo_gid)=getpwnam($majordomo_user) 
	  or die("User $majordomo_user not in passwd file");
  }
  
  $domain_id =~ /(\d+)/;
  $domain_id =$1;

#
# get $majordomo_ldir from confixx_main.conf
#
  
  if ( $domain_id>0 && -d $majordomo_ldir){
	my ($mlists_dir);
	$mlists_dir = "$majordomo_ldir/$domain_id";
	
	&checkAndCreateD ($mlists_dir,$majordomo_uid,$majordomo_gid);
	&checkAndCreateD ("$mlists_dir/lists",$majordomo_uid,$majordomo_gid);
	&checkAndCreateD ("$mlists_dir/archive",$majordomo_uid,$majordomo_gid);
	&checkAndCreateD ("$mlists_dir/digest_work",$majordomo_uid,$majordomo_gid);
	&checkAndCreateF ("$mlists_dir/Log",$majordomo_uid,$majordomo_gid);
	
	return $mlists_dir;
  }
  else {
	return '';
  }	  
}

sub createMajoListPaths {
  my ($login,$pass);
  
  my($domain_dir,$mlist_name,$majordomo_uid,$majordomo_gid)=@_;
  unless($majordomo_uid && $majordomo_gid){
	($login,$pass,$majordomo_uid,$majordomo_gid)=getpwnam($majordomo_user) 
	  or die("User $majordomo_user not in passwd file");
  }
  
  die("$domain_dir is not a directory\n") unless (-d $domain_dir); 

	my $mlist_path = "$domain_dir/lists/$mlist_name";

  &checkAndCreateD ("$mlist_path.archive",$majordomo_uid,$majordomo_gid);
  &checkAndCreateD ("$domain_dir/digest_work/$mlist_name-digest",$majordomo_uid,$majordomo_gid);
  
  &checkAndCreateF ($mlist_path,$majordomo_uid,$majordomo_gid,0644);
  &checkAndCreateF ("$mlist_path.info",$majordomo_uid,$majordomo_gid,0664);
	  
}


sub createMajoConf {
  my ($login,$pass);
  my ($domain_dir,$domain_name,$owner_passwd,$majordomo_uid,$majordomo_gid)=@_;

  unless($majordomo_uid && $majordomo_gid){
	($login,$pass,$majordomo_uid,$majordomo_gid)=getpwnam($majordomo_user) 
	  or die("User $majordomo_user not in passwd file");
  }

  my (@conf,$line);

#
# get $majordomo_home from confix_main.conf
#
  open (FILE,"< $majordomo_home/majordomo.cf") or die "Can't open '$majordomo_home/majordomo.cf' : $!\n"; 
  @conf=<FILE>;
  close (FILE);  
 
  open(FILE,"> $domain_dir/majordomo.cf");
  
  foreach $line (@conf) {
#
# edit conf-file
#  
		unless ( ($line =~ /^#/)|| ($line =~ /^\s*$/)){
			if ($line =~/\$whereami\s*=/){
				$line="\$whereami = '$domain_name';\n";  

			} elsif ( $line =~ /^\s*\$whoami\s*=/ ) {
				$line="\$whoami = 'majordomo\@$domain_name';\n";

			} elsif ( $line =~ /^\s*\$whoami_owner\s*=/ ) {
				$line="\$whoami_owner = 'majordomo-owner\@$domain_name';\n";

			} elsif ( $line =~/^\s*\$listdir\s*=/ ) {
				$line="\$listdir = '$domain_dir/lists';\n";

			} elsif ( $line =~/^\s*\$digest_work_dir\s*=/ ) {
				$line="\$digest_work_dir = '$domain_dir/digest_work';\n";

			} elsif ( $line =~/^\s*\$log\s*=/ ) {
				$line="\$log = '$domain_dir/Log';\n";

			} elsif ( $line =~/^\s*\$admin_passwd\s*=/ ) {
				$line="\$admin_passwd = '$owner_passwd';\n";
			
			} elsif ($line =~ /^\s*\$homedir\s*=/ ) {
				$line="\$homedir = '$majordomo_home';\n";
			}

	  
#	  elsif($line =~/^\s*\$sendmail_command\s*=/){
#		$line="\$sendmail_command = '$bin_sendmail';\n"; 
#	  }
#		elsif(/\$TMPDIR\s*=/){
# 		$TMPDIR = $ENV{'TMPDIR'} || "$majordomo_home/tmp";
#	  $line="\$TMPDIR = $TMPDIR;"; 
#		}
		}
		print FILE $line;
  }
  
	close (FILE);   
	undef @conf;
	chown ($majordomo_uid, $majordomo_gid, "$domain_dir/majordomo.cf");
	if ($mta eq 'qmail'){
		chmod 0740,"$domain_dir/majordomo.cf";
	} else {
		chmod 0640,"$domain_dir/majordomo.cf";
	}

#
# creat majocool_default.cf
#

	if ( open (FILE,"< $majordomo_home/majorcool_default.cf") ) { 
	  @conf=<FILE>;
	  close (FILE);  
 
	  open(FILE,"> $domain_dir/majorcool_default.cf");
	  my $inModules=0;
	  my $mod;
	  foreach $line (@conf) {
#
# edit conf-file
#  
		unless ( ($line =~ /^#/ ) || ( $line =~ /^\s*$/ ) ) {
    	  if ( $inModules ) {
			if ( $line =~ /\);/ ) {
			  $inModules=0;

			} elsif ( $line=~/^\s*'(create|rename|delete)'\s*,\s*(\d)\s*/ ) {
			  if ( $2 ) {
				$mod=$1;
				$line="\t'$1', 0, ";
				if ($mod eq 'create'){
				  $line.='  # request/create new list - disable';

				} elsif ( $mod eq 'rename' ) {
				  $line.='  # request/rename old list - disable';

				} elsif ( $mod eq 'delete' ) {
				  $line.='  # request/delete old list - disable';
				}
						
				$line.="\n";
						
			  }
			}
    	  } else {
			if ($line =~/^\s*\$sitename\s*=/ ) {
			  $line="\$sitename = '$domain_name';\n";  

			} elsif ( $line =~/^\s*\$sitehome\s*=/ ) {
			  $line="\$sitehome = 'http://$domain_name';\n";
			} elsif ( $line =~/^\s*\$cache\s*=/ ) {
			  $line="\$cache = '$domain_dir/.majordomo_keys';\n";

			} elsif ( $line =~ /%modules\s*=\s*\(/ ) {
			  $inModules=1;
			}
		  }
		}
		print FILE $line;
	  }
  
	  close (FILE);   
	  chown ($majordomo_uid, $majordomo_gid, "$domain_dir/majorcool_default.cf");
	} else {
	  warn "Can't open '$majordomo_home/majorcool_default.cf' : $!\n"; 
	}
}

sub indexMajoLists {
#
# Creat MajoCool index
#  
  my ($ptrMajoHomes,$majordomo_uid, $majordomo_gid)=@_;

  my ($login,$pass,$majoDir,$majoCoolIndexer);

	unless($majordomo_uid && $majordomo_gid){
		($login,$pass,$majordomo_uid,$majordomo_gid)=getpwnam($majordomo_user) 
			or die("User $majordomo_user not in passwd file");
	}

  if (-d $majordomo_home){
		$majoCoolIndexer="$majordomo_home/mj_key_cache";
		if (-x $majoCoolIndexer ) {
			foreach $majoDir (@$ptrMajoHomes){
				if (-e "$majoDir/majordomo.cf"){
					system( "$majoCoolIndexer -C $majoDir/majordomo.cf -K $majoDir/.majordomo_keys >/dev/null");
					chown ($majordomo_uid, $majordomo_gid, "$majoDir/.majordomo_keys");
				}
			}
		}
	}
}

sub checkMajoListConf {
#
# Create config file for maillist
#  
  my($ptrListsInfo,$majordomo_uid, $majordomo_gid)=@_;

  my($login,$pass);

  unless($majordomo_uid && $majordomo_gid){
		($login,$pass,$majordomo_uid,$majordomo_gid)=getpwnam($majordomo_user) 
			or die("User $majordomo_user not in passwd file");
  }
  my($key,$pathConfig,$owner_passwd);
  my(@conf,$line);
  foreach $key (keys %$ptrListsInfo){
	($pathConfig,$owner_passwd)=@{$ptrListsInfo->{$key}};
	if (-e $pathConfig){
	  $owner_passwd = 'confixx' unless ($owner_passwd);
	  open(FILE,"<$pathConfig");
	  @conf=<FILE>;
	  close(FILE);  
	  open(FILE,">$pathConfig.temp");
	  foreach $line (@conf){
		unless (($line =~ /^#/) || ($line =~ /^\s*$/)){
		  if ($line =~/^\s*admin_passwd\s*=/){
			$line="admin_passwd = $owner_passwd\n";  
		  }
		}
		print FILE $line;
	  }
	  close(FILE);
	
	  unlink $pathConfig;
	  rename "$pathConfig.temp",$pathConfig;
	  
	  chown( $majordomo_uid,$majordomo_gid,$pathConfig);
	  chmod( 0640, $pathConfig );        
	}
  } 
}

sub delMajoDomain {
  my($domain_id)=@_;
  if ( $domain_id>0 && -d $majordomo_ldir){
	my ($domain_dir) = "$majordomo_ldir/$domain_id";
	if ( -d $domain_dir){
	  rmtree($domain_dir);
	}
  }
}

sub delMajoListFiles {
  my($domain_id,$mlist_name)=@_;

  if ( $domain_id>0 && -d $majordomo_ldir){
	my ($domain_dir) = "$majordomo_ldir/$domain_id";
	if ( -d $domain_dir && $mlist_name){

	  &delFiles("$domain_dir/lists",$mlist_name);
	  &delFiles("$domain_dir/archive",$mlist_name);
	  
	  rmtree("$domain_dir/lists/$mlist_name.archives");
	  rmtree("$domain_dir/digest_work/$mlist_name-digest");
	}
  }
}

sub delFiles{
  my ($dir,$prefix)=@_;
  my ($file,$path);
  if ( -d $dir){
	opendir (DIR,$dir) or die "Can't opendir $dir: $!\n";
	while (defined ($file=readdir DIR)){
	  $path="$dir/$file";
	  if ( -f $path && ( $file eq $prefix || $file=~/^$prefix\./)){
		unlink $path;
	  }
	}
	closedir (DIR);
  }
}

sub checkQmailForwards{

	my(%hash,$uid,$account,$home,$ruid,$rgid,$homeFW,%changeFW,
		 @addFW,@listFW,$ptrArr,$ret,$rhome);

	unless($::mta eq 'qmail'){
		return $ret;
	}

	$homeFW = "$::user_homeDir/forward";
	unless(-d $homeFW){
    mkdir($homeFW, 0755);
		chmod 0755,$homeFW;
  }

	for (my $i=0; $i<10; $i++){
		$uid = $min_uid-45+$i;
		$account = "confixxfw_$i";
		$home = "$homeFW/$account";
		if ( ($ruid,$rgid,$rhome) = (getpwnam($account))[2,3,7] ) {
			if($rhome eq $home){
				$uid = $ruid;
				if($rgid != $::mta_gid){
					$changeFW{$account} = [$uid,$home];
				}
			} else {
				$changeFW{$account} = [$uid,$home];
			}
		}else{
			push @addFW,[$account,$uid,$home];
		}
		push @listFW,[$account,$uid,$home];
	}
	$ret = 0;
	if ( keys %changeFW ) {
		my(@passwd,@shadow,$line);

#
# edit /etc/passwd
#
		if( open ( PASSWD, "</etc/passwd" ) ) {
			@passwd = <PASSWD>;
			close ( PASSWD );
		}
		for ( my $i=0; $i<@passwd; $i++ ) {
			($account)=split(/:/,$passwd[$i],2);
			if ( exists ( $changeFW{$account} ) ) {
				($uid,$home) = @{$changeFW{$account}};
				$passwd[$i] = "$account:x:$uid\:$::mta_gid\::$home:/bin/false\n";
			}
		}

		if( open ( PASSWD, ">/etc/passwd" ) ) {
			print PASSWD @passwd;
			close ( PASSWD );
			$ret += @passwd;
		}
#
# end edit /etc/passwd
#
	}

	if ( @addFW ) {
#
# add new accounts
#
		if ( open ( PASSWD, ">>/etc/passwd" ) ) {
			if ( open ( SHADOW, ">>/etc/shadow" ) ) {
				my $days_since_1970 = int(time() / 86400);
				foreach $ptrArr (@addFW) {
					($account,$uid,$home) = @{$ptrArr};
					print SHADOW "$account:!:$days_since_1970:0:99999::::\n";
					print PASSWD "$account:x:$uid\:$::mta_gid\::$home:/bin/false\n";
					$ret++;
				}
				close ( SHADOW );
			}
			close ( PASSWD );
		}
#
# end add new accounts
#
	}

#
# check ownership of forward's home
#
	foreach $ptrArr (@listFW) {
		($account,$uid,$home) = @{$ptrArr};
		if( -d $home ){
			($ruid,$rgid) = (stat( $home ))[4,5];
			unless( ( $uid == $ruid ) && ( $::mta_gid == $rgid ) ) {
				chown ( $uid,$::mta_gid,$home );
				chmod ( 0755, $home );
			}
		} else {
			mkdir ( $home, 0755 );
			chown ( $uid,$::mta_gid,$home );
			chmod ( 0755, $home );
		}
	}

	return $ret;

}

1;
