1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
|
package Kolab::Conf;
## COPYRIGHT
## ---------
##
## See AUTHORS file
##
##
## LICENSE
## -------
##
## 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, 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 can view the GNU General Public License, online, at the GNU
## Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
##
## $Revision: 1.4.2.1 $
use 5.008;
use strict;
use warnings;
use IO::File;
use File::Copy;
use File::Temp;
use File::stat;
use Kolab;
use Kolab::Util;
use Kolab::LDAP;
require Exporter;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = (
'all' => [ qw(
&buildPostfixTransportMap
&buildCyrusGroups
&buildLDAPReplicas
&rebuildTemplates
&checkPermissions
) ]
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
my %templates = ();
my %ownership = ();
my %permissions = ();
my %templatehaschanged = ();
my %haschanged = ();
my %commentchar = ();
sub fixup {
my $file = shift;
my $ownership = shift;
my $perm = shift;
(my $owner, my $group) = split(/:/, $ownership, 2);
my $uid = (getpwnam($owner))[2];
my $gid = (getgrnam($group))[2];
Kolab::log('T', sprintf("Changing permissions of %s to 0%o", $file, $perm ), KOLAB_DEBUG );
if( chmod($perm, $file) != 1 ) {
Kolab::log('T', "Unable to change permissions of `$file' to ".sprintf("0%o",$perm) . ": $!", KOLAB_ERROR);
exit(1);
}
Kolab::log('T', "Changing owner of $file to $owner:$group ($uid:$gid)", KOLAB_DEBUG );
if( chown($uid,$gid,$file) != 1 ) {
Kolab::log('T', "Unable to change ownership of `$file' to $uid:$gid: $!", KOLAB_ERROR);
exit(1);
}
}
sub printWarning {
my $stream = shift;
my $templateFile = shift;
my $cc = shift;
$templateFile = "" if (!defined $templateFile);
$cc = "#" if (!defined $cc);
# Different warnings during bootstrapping and regular configuration
# $Kolab::config{"bootstrap_config"} = "true";
if ((defined $Kolab::config{"bootstrap_config"}) &&
($Kolab::config{"bootstrap_config"} eq "true")) {
print $stream "$cc=================================================================\n";
print $stream "$cc This is a preliminary version of this configuration file and\n";
print $stream "$cc only used for bootstrapping. If you see this warning in your\n";
print $stream "$cc configuration after bootstrapping the Kolab Server\n";
print $stream "$cc SOMETHING WENT VERY WRONG !!!\n";
print $stream "$cc=================================================================\n";
} else {
print $stream "$cc=================================================================\n";
print $stream "$cc THIS FILE IS AUTOMATICALLY WRITTEN BY THE KOLAB CONFIG BACKEND.\n";
print $stream "$cc MANUAL CHANGES ARE LOST UNLESS MADE IN THE TEMPLATE FILE:\n";
print $stream "$cc\n";
print $stream "$cc $templateFile\n";
print $stream "$cc\n";
print $stream "$cc Changes can be activated by running ".$Kolab::config{'kolabconf_script'}."\n";
print $stream "$cc=================================================================\n";
}
}
sub build {
my $tmpl = shift;
my $cfg = shift;
my $owner = shift;
my $perm = shift;
my $cchr = shift; # comment character
my $oldcfg = $cfg . '.old';
my $templatedir = $Kolab::config{"templatedir"};
my %special_templates = (
"$templatedir/transport.template" => 1,
"$templatedir/virtual.template" => 1,
"$templatedir/imapd.group.template" => 1,
"$templatedir/slapd.access.template" => 1,
"$templatedir/slapd.replicas.template" => 1
);
my $oldmask = umask 077;
#creating the config file is changing it
if (! -f $cfg) {
$templatehaschanged{$tmpl} = 1;
Kolab::log('T', "`$cfg' creation detected", KOLAB_DEBUG );
}
copy($cfg, $oldcfg);
#chown($Kolab::config{'kolab_uid'}, $Kolab::config{'kolab_gid'}, $oldcfg);
# To avoid warnings, the backup files must be owned by root
chown(0, 0, $oldcfg);
umask $oldmask;
#chmod(0600, $oldcfg) if ($oldcfg =~ /openldap/);
Kolab::log('T', "Creating new configuration file `$cfg' from template `$tmpl'", KOLAB_DEBUG );
#print STDERR "Creating new configuration file `$cfg' from template `$tmpl'\n";
my $template;
if (!($template = IO::File->new($tmpl, 'r'))) {
Kolab::log('T', "Unable to open template file `$tmpl': $!", KOLAB_ERROR);
# Error, fail gracefully
return;
}
my $config;
if (!($config = new File::Temp( TEMPLATE => 'tmpXXXXX',
DIR => $Kolab::config{"kolabdir"},
SUFFIX => '.kolabtmp',
UNLINK => 0 ))) {
Kolab::log('T', "Unable to open configuration file `$cfg': $!", KOLAB_ERROR);
exit(1);
}
#Kolab::log('T', "Using temporary file '".$config->filename."'", KOLAB_DEBUG );
my $skip = 0;
my $keep = 0;
while (<$template>) {
#Eat the meta data sections
if (/^KOLAB_META_START$/) {
my $found_end;
while (!$found_end) {
$_ = <$template>;
$found_end = /^KOLAB_META_END$/;
}
$_ = <$template>;
}
if (/\@{3}if\s+exists\(\s*(\S+?)\s*\)\@{3}/) {
# @@@if exists(/full/path/to/file)@@@
# also possible: @@@if exists( /full/path/to/file )@@@
if (-f $1) {
# Keep text if searched file or symbolic link exists.
$keep = 1;
} else {
# Skip text
$skip++;
$keep = 0;
}
} elsif (/\@{3}if\s+(\S+?)\@{3}/) {
# @@@if some_variable@@@
# The some_variable is a key in the $Kolab::config hash and has
# its value set to either 'false' or 'true'
if ($Kolab::config{$1} && lc($Kolab::config{$1}) ne "false" ) {
# Keep text
$keep = 1;
} else {
# Skip text
$skip++;
$keep = 0;
}
s/\@{3}if (\S+?)\@{3}\n?//;
} elsif (/\@{3}else\@{3}/) {
if( $keep == 0 ) {
# Now keep
$keep = 1;
$skip--;
} else {
# Now skip
$keep = 0;
$skip++;
}
s/\@{3}else\@{3}\n?//;
} elsif (/\@{3}endif\@{3}/) {
($skip > 0) && $skip--;
s/\@{3}endif\@{3}\n?//;
} elsif (/\@{3}warning\@{3}/) {
printWarning($config, $tmpl, $cchr);
} else {
while (/\@{3}([^\s\@]+?)(\|(.+?)\((.*)\))?\@{3}/) {
my $attr = $1;
my $fct = $3;
my $args = $4;
#print STDERR "attr=\"$attr\", fct=\"$fct\", args=\"$args\"\n";
if ($Kolab::config{$attr}) {
my $val = "";
if( !$fct ) {
if (ref $Kolab::config{$attr} eq "ARRAY") {
$val = $Kolab::config{$attr}->[0];
} else {
$val = $Kolab::config{$attr};
}
} else {
# Modifier functions
SWITCH: {
# Join function
$fct eq 'join' && do {
if (ref $Kolab::config{$attr} eq "ARRAY") {
my @vals = @{$Kolab::config{$attr}} ;
# We want to make sure subdomain.domain.tld comes before domain.tld
my @length_sorted_vals = sort {length $b cmp length $a} @vals;
$val = join ($args, @length_sorted_vals) ;
} else {
$val = $Kolab::config{$attr};
}
last SWITCH;
};
# Quote function
$fct eq 'quote' && do {
# slapd.conf compatible quoting
$val = $Kolab::config{$attr};
$val =~ s/"/\"/g;
$val = '"'.$val.'"';
last SWITCH;
}
}
}
s/\@{3}([^\s\@]+?)(\|.+?)?\@{3}/$val/;
last if ( $val eq "\@\@\@$attr\@\@\@" ); # prevent endless loop
} else {
# Only warn the user in case we are not skipping the section
($skip == 0) && Kolab::log('T', "No configuration variable corresponding to `$1' exists", KOLAB_WARN);
s/\@{3}([^\s\@]+?)\@{3}//;
}
}
($skip == 0) && print $config $_;
}
}
$template->close;
$config->close;
move($config->filename, $cfg) || Kolab::log('T', "Error moving configfile to $cfg, error: $!", KOLAB_ERROR );
fixup( $cfg, $owner, $perm );
#chown($Kolab::config{'kolab_uid'}, $Kolab::config{'kolab_gid'}, $cfg);
#chmod(0600, $cfg) if ($cfg =~ /openldap/);
if (-f $oldcfg && !defined $special_templates{$tmpl} ) {
my $rc = `diff -q $cfg $oldcfg`;
chomp($rc);
if ($rc) {
if ($cfg =~ /postfix/) {
$haschanged{'postfix'} = 1;
} elsif ($cfg =~ /saslauthd/) {
$haschanged{'saslauthd'} = 1;
} elsif ($cfg =~ /apache/) {
$haschanged{'apache'} = 1;
} elsif ($cfg =~ /openldap/ || $cfg =~ /slapd/) {
$haschanged{'slapd'} = 1;
} elsif ($cfg =~ /(imapd|cyrus)/) {
$haschanged{'imapd'} = 1;
} elsif ($cfg =~ /amavisd/) {
$haschanged{'amavisd'} = 1;
} elsif ($cfg =~ /clamav/) {
$haschanged{'clamav'} = 1;
#} elsif ($cfg =~ /example/) {
} else {
Kolab::log('T', "`$cfg' change detected ", KOLAB_DEBUG );
}
$templatehaschanged{$tmpl} = 1;
Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG );
}
}
Kolab::log('T', "Finished creating configuration file `$cfg'", KOLAB_DEBUG );
}
sub buildPostfixTransportMap
{
buildPostfixMap( 'transport' );
}
sub buildPostfixVirtualMap
{
buildPostfixMap( 'virtual' );
}
sub buildPostfixMap
{
my $map = shift;
Kolab::log('T', "Building Postfix $map map", KOLAB_DEBUG);
my $templatedir = $Kolab::config{"templatedir"};
my $keytemplate = "$templatedir/$map.template";
my $cfg = $templates{$keytemplate};
my $oldcfg = $cfg . '.old';
#my $oldmask = umask 077;
#copy($cfg, $oldcfg);
#chown($Kolab::config{'kolab_uid'}, $Kolab::config{'kolab_gid'}, $oldcfg);
#umask $oldmask;
#delete $templates{$keytemplate};
my $transport;
if (!($transport = IO::File->new($cfg, 'a'))) {
Kolab::log('T', "Unable to create Postfix $map map: $!", KOLAB_ERROR);
exit(1);
}
my $ldap = Kolab::LDAP::create(
$Kolab::config{'ldap_ip'},
$Kolab::config{'ldap_port'},
$Kolab::config{'bind_dn'},
$Kolab::config{'bind_pw'}
);
my $mesg = $ldap->search(
base => 'k=kolab,'.$Kolab::config{'base_dn'},
scope => 'base',
filter => '(objectclass=*)'
);
if ($mesg->code) {
Kolab::log('T', "Unable to locate Postfix $map map entries in LDAP", KOLAB_ERROR);
exit(1);
}
my $ldapobject;
if ($mesg->code <= 0) {
foreach $ldapobject ($mesg->entries) {
my $routes = $ldapobject->get_value("postfix-$map", asref => 1);
foreach (@$routes) {
$_ = trim($_);
Kolab::log('T', "Adding entry `$_' to $map");
print $transport $_ . "\n";
}
}
} else {
Kolab::log('T', "No Postfix $map map entries found");
}
Kolab::LDAP::destroy($ldap);
$transport->close;
# FIXME: bad way of doing things...
#system("chown root:root @emailserver_confdir@/*");
fixup( $cfg, $ownership{$cfg}, $permissions{$cfg});
system("$Kolab::config{'postmapping'}/$map");
if (-f $oldcfg) {
my $rc = `diff -q $cfg $oldcfg`;
chomp($rc);
if ($rc) {
Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG);
$haschanged{'postfix'} = 1;
}
} else {
$haschanged{'postfix'} = 1;
}
Kolab::log('T', 'Finished building Postfix $map map', KOLAB_DEBUG);
}
sub buildCyrusGroups
{
Kolab::log('T', 'Building Cyrus groups', KOLAB_DEBUG);
my $templatedir = $Kolab::config{"templatedir"};
my $keytemplate = "$templatedir/imapd.group.template";
my $cfg = $templates{$keytemplate};
my $oldcfg = $cfg . '.old';
#delete $templates{$keytemplate};
#my $oldmask = umask 077;
#copy($cfg, $oldcfg);
#chown($Kolab::config{'kolab_uid'}, $Kolab::config{'kolab_gid'}, $oldcfg);
#umask $oldmask;
my $groupconf;
if (!($groupconf = IO::File->new($cfg, 'a'))) {
Kolab::log('T', "Unable to open configuration file `$cfg': $!", KOLAB_ERROR);
exit(1);
}
my $ldap = Kolab::LDAP::create(
$Kolab::config{'ldap_ip'},
$Kolab::config{'ldap_port'},
$Kolab::config{'bind_dn'},
$Kolab::config{'bind_pw'}
);
my $mesg = $ldap->search(
base => $Kolab::config{'base_dn'},
scope => 'sub',
filter => '(&(mail=*)(objectclass=kolabgroupofnames))'
);
if ($mesg->code) {
Kolab::log('T', 'Unable to locate Cyrus groups in LDAP', KOLAB_ERROR);
exit(1);
}
my $ldapobject;
my $count = 60000;
if ($mesg->code <= 0) {
foreach $ldapobject ($mesg->entries) {
#my $group = $ldapobject->get_value('cn') . '@'.join('.',reverse(@dn)) . ":*:$count:";
my $group = lc($ldapobject->get_value('mail')).":*:$count:";
my $userlist = $ldapobject->get_value('member', asref => 1);
foreach (@$userlist) {
my $uid = $_;
my $umesg = $ldap->search( base => $uid,
scope => 'base',
filter => '(objectClass=*)' );
if ( $umesg && $umesg->code() <= 0 && $umesg->count() == 1 ) {
my $mail;
($mail = $umesg->entry(0)->get_value('mail')) or
($mail = $umesg->entry(0)->get_value('uid'));
$group .= lc($mail).',';
}
}
$group =~ s/,$//;
print $groupconf $group . "\n";
Kolab::log('T', "Adding cyrus group `$group'");
$count++;
}
} else {
Kolab::log('T', 'No Cyrus groups found');
}
$groupconf->close;
Kolab::LDAP::destroy($ldap);
fixup( $cfg, $ownership{$cfg}, $permissions{$cfg});
Kolab::log('T', 'Finished building Cyrus groups', KOLAB_DEBUG );
}
sub buildLDAPAccess
{
Kolab::log('T', 'Building LDAP access file', KOLAB_DEBUG);
my $templatedir = $Kolab::config{"templatedir"};
my $keytemplate = "$templatedir/slapd.access.template";
if( ! -f $keytemplate ) {
Kolab::log('T', "No LDAP access file `$keytemplate', skipping", KOLAB_DEBUG);
return;
}
my $cfg = $templates{$keytemplate};
my $oldcfg = $cfg . '.old';
my $access;
if (!($access = IO::File->new($cfg, 'a'))) {
Kolab::log('T', "Unable to open configuration file `$cfg': $!", KOLAB_ERROR);
exit(1);
}
my $global_acl = <<'EOS';
# Domain specific access
access to filter=(&(objectClass=kolabInetOrgPerson)(mail=*@@@@domain@@@)(|(!(alias=*))(alias=*@@@@domain@@@)))
by group/kolabGroupOfNames="cn=@@@domain@@@,cn=domains,cn=internal,@@@base_dn@@@" write
by * break
access to filter=(&(objectClass=kolabGroupOfNames)(mail=*@@@@domain@@@))
by group/kolabGroupOfNames="cn=@@@domain@@@,cn=domains,cn=internal,@@@base_dn@@@" write
by * break
access to filter=(&(objectClass=kolabSharedFolder)(cn=*@@@@domain@@@))
by group/kolabGroupOfNames="cn=@@@domain@@@,cn=domains,cn=internal,@@@base_dn@@@" write
by * break
EOS
my $dom_acl1 = << 'EOS';
# Access to domain groups
access to dn.children="cn=domains,cn=internal,@@@base_dn@@@"
by group/kolabGroupOfNames="cn=admin,cn=internal,@@@base_dn@@@" write
by group/kolabGroupOfNames="cn=maintainer,cn=internal,@@@base_dn@@@" write
by dn="cn=nobody,cn=internal,@@@base_dn@@@" read
EOS
my $dom_acl2 = << 'EOS';
by group/kolabGroupOfNames="cn=@@@domain@@@,cn=domains,cn=internal,@@@base_dn@@@" read
EOS
my $dom_acl3 = << 'EOS';
by * search stop
EOS
my $str;
my $domain;
my @domains;
if( ref($Kolab::config{'postfix-mydestination'}) eq 'ARRAY' ) {
@domains = @{$Kolab::config{'postfix-mydestination'}};
} else {
@domains =( $Kolab::config{'postfix-mydestination'} );
}
($str = $dom_acl1) =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;
print $access $str;
foreach $domain (@domains) {
($str = $dom_acl2) =~ s/\@{3}domain\@{3}/$domain/g;
$str =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;
print $access $str;
}
($str = $dom_acl3) =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;
print $access $str;
foreach $domain (@domains) {
($str = $global_acl) =~ s/\@{3}domain\@{3}/$domain/g;
$str =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;
print $access $str;
Kolab::log('T', "Adding acl for domain '$str'");
}
$access->close;
if (-f $oldcfg) {
my $rc = `diff -q $cfg $oldcfg`;
chomp($rc);
if ($rc) {
Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG);
$haschanged{'slapd'} = 1;
}
} else {
$haschanged{'slapd'} = 1;
}
fixup( $cfg, $ownership{$cfg}, $permissions{$cfg});
Kolab::log('T', 'Finished building LDAP access file', KOLAB_DEBUG );
}
sub buildLDAPReplicas
{
Kolab::log('T', 'Building LDAP replicas', KOLAB_DEBUG);
my $templatedir = $Kolab::config{"templatedir"};
my $keytemplate = "$templatedir/slapd.replicas.template";
if( ! -f $keytemplate ) {
Kolab::log('T', "No LDAP replicas `$keytemplate', skipping", KOLAB_DEBUG);
return;
}
my $cfg = $templates{$keytemplate};
my $oldcfg = $cfg . '.old';
my $repl;
if (!($repl = IO::File->new($cfg, 'a'))) {
Kolab::log('T', "Unable to open configuration file `$cfg': $!", KOLAB_ERROR);
exit(1);
}
# directory_mode syncrepl is supported from openldap-2.3.x and beyond
if ($Kolab::config{'directory_mode'} eq "syncrepl") {
if ( $Kolab::config{'is_master'} eq "false" ) {
# Output a syncrepl statement for database synchronisation
print $repl "syncrepl rid=0 \n"
." provider=".$Kolab::config{"ldap_master_uri"}."\n"
." type=refreshAndPersist\n"
." retry=\"60 10 300 +\"\n"
." searchbase=\"".$Kolab::config{'base_dn'}."\"\n"
." scope=sub\n"
." schemachecking=on\n"
." binddn=\"".$Kolab::config{"bind_dn"}."\"\n"
." credentials=\"".$Kolab::config{"bind_pw"}."\"\n"
." bindmethod=simple\n";
}
} else {
if( $Kolab::config{'is_master'} eq "true" ) {
# Master setup
my @kh;
if( ref $Kolab::config{'kolabhost'} eq 'ARRAY' ) {
@kh = @{$Kolab::config{'kolabhost'}};
} else {
@kh = ( $Kolab::config{'kolabhost'} );
}
for my $h ( @kh ) {
next if lc($h) eq lc($Kolab::config{'fqdnhostname'});
print $repl "replica uri=ldaps://$h\n"
." binddn=\"".$Kolab::config{'bind_dn'}."\"\n"
." bindmethod=simple credentials=".$Kolab::config{'bind_pw'}."\n\n";
}
} else {
# Slave setup
# Output an update dn statement instead
print $repl "updatedn ".$Kolab::config{'bind_dn'}."\n";
print $repl "updateref ".$Kolab::config{'ldap_master_uri'}."\n";
}
}
$repl->close;
fixup( $cfg, $ownership{$cfg}, $permissions{$cfg});
if (-f $oldcfg) {
my $rc = `diff -q $cfg $oldcfg`;
chomp($rc);
if ($rc) {
Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG);
$haschanged{'slapd'} = 1;
}
} else {
$haschanged{'slapd'} = 1;
}
Kolab::log('T', 'Finished building LDAP replicas', KOLAB_DEBUG);
}
sub replaceMetaVar
{
my $var = shift;
while ($var =~ /\@{3}([^\s\@]+?)\@{3}/) {
my $attr = $1;
if ($Kolab::config{$attr}) {
my $val = $Kolab::config{$attr};
$var =~ s/\@{3}([^\s\@]+?)\@{3}/$val/;
} else {
Kolab::log('T', "No configuration variable corresponding to `$1' exists", KOLAB_WARN);
}
}
return $var;
}
sub loadMetaTemplates
{
my $templatedir = shift;
my ($tref, $pref, $oref, $cmdref, $ccharref) = @_;
Kolab::log('T', 'Collecting template files', KOLAB_DEBUG );
opendir(DIR, $templatedir) or Kolab::log('T', 'Given templatedir $templatedir does not exist!', KOLAB_ERROR );
my @metatemplates = grep { /\.template$/ } readdir (DIR);
closedir(DIR);
foreach my $template (@metatemplates) {
my $runonchange = undef;
my $commentchar = undef;
#Open each file and check for the META
if (open (TEMPLATE, "$templatedir/$template" )) {
my $line = <TEMPLATE>;
if ($line =~ /^KOLAB_META_START$/) {
Kolab::log('T', 'Processing META template :'.$template, KOLAB_DEBUG );
my ($found_end, $target, $permissions, $ownership);
while (<TEMPLATE>) {
$line = $_;
if (!$found_end) {
$found_end = $line =~ /^KOLAB_META_END$/;
if (!$found_end && $line) {
my ($key,$value) = split(/=/,$line);
chomp($value);
Kolab::log('T', 'META Key: '.$key.' Value: '.$value, KOLAB_DEBUG );
if ($key =~ /^TARGET$/) {
$target = replaceMetaVar($value);
Kolab::log('T', 'META Target '.$target, KOLAB_DEBUG );
} elsif ($key =~ /^PERMISSIONS$/) {
$permissions = replaceMetaVar($value);
Kolab::log('T', 'META Permissions '.$permissions, KOLAB_DEBUG );
} elsif ($key =~ /^OWNERSHIP$/) {
$ownership = replaceMetaVar($value);
Kolab::log('T', 'META Ownership '.$ownership, KOLAB_DEBUG );
} elsif ($key =~ /^RUNONCHANGE$/) {
$runonchange = replaceMetaVar($value);
Kolab::log('T', 'META Cmd to execute '.$runonchange, KOLAB_DEBUG );
} elsif ($key =~ /^COMMENT_CHAR$/) {
$commentchar = replaceMetaVar($value);
Kolab::log('T', 'META CommentChar to use: '.$commentchar, KOLAB_DEBUG );
} else {
Kolab::log('T', 'incorrect META key "'.$key.'" in: '.$template, KOLAB_WARN );
}
}
}
}
if ($found_end && $target && $permissions && $ownership) {
Kolab::log('T', 'All mandatory fields populated in '.$template, KOLAB_DEBUG );
$$tref{$templatedir . "/" . $template} = $target;
$$oref{$target} = $ownership;
$permissions = oct($permissions);
$$pref{$target} = $permissions;
my $runcmdtemplate = $templatedir."/".$template;
$$cmdref{$runcmdtemplate} = $runonchange if (defined $runonchange);
$$ccharref{$target} = $commentchar if (defined $commentchar);
}
}
} else {
Kolab::log('T', 'Could not open template file: '. $template, KOLAB_WARN);
}
}
}
sub rebuildTemplates
{
my $key;
my $value;
my $section="";
my %runonchange;
my $templatedir = $Kolab::config{"templatedir"};
Kolab::log('T', 'Regenerating configuration files', KOLAB_DEBUG );
Kolab::log('T', 'Loading meta-template data', KOLAB_DEBUG );
loadMetaTemplates( $templatedir, \%templates, \%permissions, \%ownership, \%runonchange, \%commentchar );
my $cfg;
my $tpl;
foreach $tpl (keys %templates) {
$cfg = $templates{$tpl};
#print STDOUT "Rebuilding $tpl => $cfg\n";
build($tpl, $cfg, $ownership{$cfg}, $permissions{$cfg}, $commentchar{$cfg});
}
buildPostfixTransportMap;
buildPostfixVirtualMap;
buildLDAPAccess;
buildLDAPReplicas;
buildCyrusGroups;
Kolab::log('T', 'Finished regenerating configuration files', KOLAB_DEBUG );
foreach $key (keys %runonchange) {
if (defined $templatehaschanged{$key})
{
Kolab::log('T', 'Actioning RUNONCHANGE for '.$key, KOLAB_DEBUG );
my $cmd = $runonchange{$key};
# $cmd can contain:
# - /usr/sbin/postmap: should always be executed
# - openpkg rc imapd restart (in openpkg distribution)
# - kolabsrv rc post reload (in other distributions)
# The commands with ' rc ' may only be executed when reloading is not
# prohibited by the user with the "-n" option.
if ($Kolab::do_reload || $cmd !~ / rc \S+ re(start|load)/) {
Kolab::log('T', 'Executing command: '.$cmd, KOLAB_DEBUG );
system($cmd);
} else {
Kolab::log('T', 'Reload not allowed, not executing command: '.$cmd, KOLAB_DEBUG );
}
system($cmd);
Kolab::log('T', 'Executing command', KOLAB_DEBUG );
}
}
}
sub bootstrapConfig
{
my $templatedir = $Kolab::config{"templatedir"};
# FIXME: it would be better if the templates can be given as an
# argument to this function.
my @templ = ("$templatedir/slapd.access.template",
"$templatedir/slapd.conf.template",
"$templatedir/slapd.replicas.template");
my %runonchange;
loadMetaTemplates( $templatedir, \%templates, \%permissions, \%ownership, \%runonchange, \%commentchar );
my $cfg;
my $out;
foreach my $tpl (@templ) {
$cfg = $templates{$tpl};
# print STDOUT "Rebuilding $tpl => $cfg\n";
build($tpl, $cfg, $ownership{$cfg}, $permissions{$cfg}, $commentchar{$cfg});
}
}
sub checkPermissions {
my $key;
my $value;
my $templatedir = $Kolab::config{"templatedir"};
Kolab::log('T', 'Checking generated config file permissions and ownership', KOLAB_DEBUG );
loadMetaTemplates( $templatedir, \%templates, \%permissions, \%ownership );
my $ok = 1;
foreach $key (keys %templates) {
my $tpl = $templates{$key};
if (-r $tpl) {
my $st = stat($tpl);
my $owner = getpwuid($st->uid).':'.getgrgid($st->gid);
if( ( ($st->mode & 07777) != $permissions{$tpl}) ||
($owner ne $ownership{$tpl}) ) {
my $str = 'File '.$tpl.' has the wrong persmissions/owner. Found '
.sprintf("%lo", $st->mode&07777).' '.$owner.', expected '
.sprintf("%lo",$permissions{$tpl}).' '.$ownership{$tpl};
print( "$str\n" );
Kolab::log('T', $str, KOLAB_ERROR );
$ok = 0;
}
} else {
my $str = "File $tpl does not exist";
print "$str\n";
Kolab::log('T', "$str", KOLAB_ERROR );
}
}
return $ok;
}
sub reload
{
if ($haschanged{'slapd'}) {
&Kolab::log('K', 'Stopping OpenLDAP...');
system("invoke-rc.d slapd stop");
&Kolab::log('K', 'Deleting old slapd config...');
system("rm -rf $Kolab::config{'ldapserver_confdir'}/slapd.d");
&Kolab::log('K', 'Converting slapd config... (most errors here can be ignored)');
system("mkdir $Kolab::config{'ldapserver_confdir'}/slapd.d");
system("slaptest -f $Kolab::config{'ldapserver_confdir'}/slapd.conf -F $Kolab::config{'ldapserver_confdir'}/slapd.d");
system("chown -R openldap $Kolab::config{'ldapserver_confdir'}/slapd.d");
system("chgrp -R openldap $Kolab::config{'ldapserver_confdir'}/slapd.d");
&Kolab::log('K', 'Starting OpenLDAP...');
system("invoke-rc.d slapd start");
}
if ($haschanged{'saslauthd'}) {
&Kolab::log('K', 'Restarting SASLAuthd...');
system("invoke-rc.d saslauthd restart");
}
if ($haschanged{'apache'}) {
&Kolab::log('K', 'Reloading Apache...');
system("invoke-rc.d apache2 graceful");
}
if ($haschanged{'postfix'}) {
&Kolab::log('K', 'Reloading Postfix...');
system("invoke-rc.d postfix reload");
}
if ($haschanged{'imapd'}) {
&Kolab::log('K', 'Restarting imapd...');
# Would it be enough with a reload here? /steffen
system("invoke-rc.d kolab-cyrus restart");
}
%Kolab::Conf::haschanged = ();
&Kolab::log('K', 'Reload finished');
}
1;
__END__
=head1 NAME
Kolab::Conf - Perl extension for Kolab template generation
=head1 ABSTRACT
Kolab::Conf handles the generation of template files, used by
kolabconf.
=head1 COPYRIGHT AND AUTHORS
Stuart Bingë and others (see AUTHORS file)
=head1 LICENSE
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, 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 can view the GNU General Public License, online, at the GNU
Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
=cut
|