#!/usr/bin/perl
#
# Copyright (C) 2001 Caldera International, Inc.  All Rights Reserved.
#
# domaincreate
#
# create a mail domain
#

$DEBUG	= 0;				# must be first statement

BEGIN {
	use File::Basename();
	$newdir = File::Basename::dirname($0);
	unshift (@INC, "$newdir");
}

use 		msglib;

sub Usage {
	print STDERR msgi18nmsg(1); # usage message
	exit(1);
}

#
# main
#

msgi18ninit(16); # set 16 is msgdomaincreate

$argc = @ARGV;
if ($argc < 1) {
	Usage;
}
$optresult = GetOptions ("domain=s" => \$vdomain,
			 "description=s" => \$description);

argcheck("domain", $vdomain);
argcheck("description", $description);

if (!$optresult) {
	Usage;
}

if (!$vdomain) {
	Usage;
}

if ($description) {
	$attrs = ['ou' => "$vdomain",
		  'description' => $description,
		  'objectClass' => ['top','organizationalUnit','msgDomain'],
		 ];
} else {
	$attrs = ['ou' => "$vdomain",
		  'objectClass' => ['top','organizationalUnit','msgDomain'],
		 ];
}

# Get defaults for LDAP
parse_conf;

$ldap   = Net::LDAP->new($ldaphost) || die msgi18nmsg(2) . "$@";
$result = $ldap->bind("$msgadmindn", password => "$msgadminpw", version => 3);

if ( $result->code ) {
	#
	# if we've got an error... record it
	#
	print getLDAPerrorString("bind",$result) . "\n";
	exit(1);
}

# make sure the domain doesn't exist
$mesg = $ldap->search(
		base 	=> $ldapsuffix,
		scope 	=> "one",
		filter 	=> fbuild("(ou=%1)", $vdomain),
		attrs => ['dn'],
);

if($mesg->code) {
	print getLDAPerrorString("search",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

$entry = $mesg->entry(0);
if ($entry) {
	print STDERR msgi18nmsg(3, $vdomain); # $vdomain already exists, or name in use
	$ldap->unbind;
	exit(1);
}

$mesg = $ldap->add(dn => "ou=$vdomain,$ldapsuffix", attr => $attrs);

if( $mesg->code ) {
	print getLDAPerrorString("add",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

# create the MSG Users and MSG Groups container nodes
$usersattrs = ['ou' => "MSG Users",
	       'objectClass' => ['top','organizationalUnit'],
	      ];

$mesg = $ldap->add(dn => "ou=MSG Users,ou=$vdomain,$ldapsuffix",
		   attr => $usersattrs);

if( $mesg->code ) {
	print getLDAPerrorString("add",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

$groupsattrs = ['ou' => "MSG Groups",
	        'objectClass' => ['top','organizationalUnit'],
	       ];

$mesg = $ldap->add(dn => "ou=MSG Groups,ou=$vdomain,$ldapsuffix",
		   attr => $groupsattrs);

if( $mesg->code ) {
	print getLDAPerrorString("add",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

# now make the MAILER-DAEMON and postmaster aliases.
# these are special and point to the admin group.

$dn = "cn=Postmaster,ou=MSG Groups,ou=$vdomain,$ldapsuffix";
$aliasattrs = [
	objectClass => [ "top", "msgGroup" ],
	cn => "Postmaster",
	mail => "postmaster\@$vdomain",
	owner => "uid=msguser,$ldapsuffix",
	member => "cn=MSG Administrators,$ldapsuffix",
	"${msgldapprefix}MsgMgrpJoinable" => "FALSE",
	ou => "$vdomain",
	description => "Postmaster alias: cannot be deleted",
	];

$mesg = $ldap->add(
	dn => $dn,
	attr => $aliasattrs);

if( $mesg->code ) {
	print getLDAPerrorString("add",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

$dn = "cn=MAILER-DAEMON,ou=MSG Groups,ou=$vdomain,$ldapsuffix";
$aliasattrs = [
	objectClass => [ "top", "msgGroup" ],
	cn => "MAILER-DAEMON",
	mail => "MAILER-DAEMON\@$vdomain",
	owner => "uid=msguser,$ldapsuffix",
	member => "cn=MSG Administrators,$ldapsuffix",
	"${msgldapprefix}MsgMgrpJoinable" => "FALSE",
	ou => "$vdomain",
	description => "MAILER-DAEMON alias: cannot be deleted",
	];

$mesg = $ldap->add(
	dn => $dn,
	attr => $aliasattrs);

if( $mesg->code ) {
	print getLDAPerrorString("add",$mesg) . "\n";
	$ldap->unbind;
	exit(1);
}

$ldap->unbind;

$ret = do_rootread("/etc/postfix/transport", \@lines);
if ($ret == 0) {
	print STDERR msgi18nmsg(5); # Cannot read /etc/postfix/transport
	exit(1);
}

@olines = ();
$oline = 0;

$found_dn = 0;
foreach(@lines) {

	if(/^$vdomain\s.*/) {
		$found_dn = 1;
	}
	$olines[$oline++] = $_;
}
if (! $found_dn ) {
	$olines[$oline++] = "$vdomain lmtp:unix:/var/imap/socket/lmtp\n";
}

do_rootwrite("/etc/postfix/transport", \@olines);

$cmd = "|$msgbin/msgroot --run=postmap --args=/etc/postfix/transport";
$ret = do_cmd($cmd);
if ($ret == 0) {
        # service action $service $action failed
  	die msgi18nsmsg(23, 1, "postmap", "/etc/postfix/transport");
}

# now fixup the mydestination parameter

$ret = do_rootread("/etc/postfix/main.cf", \@lines);
if ($ret == 0) {
	print STDERR msgi18nmsg(4); # Cannot read /etc/postfix/main.cf
	exit(1);
}

@olines = ();
$oline = 0;

foreach (@lines) {

	if (/^mydestination\s*=\s*/) {
		chomp($_);
		$_ = "$_, $vdomain\n";
	}
	$olines[$oline++] = $_;
}

do_rootwrite("/etc/postfix/main.cf", \@olines);

do_rootserviceaction("postfix", "reload");

exit(0);
