From b79a215211eeadcf3ee140dd5f9f58445ba73a26 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Thu, 31 Oct 2019 11:49:46 +0100 Subject: [PATCH] Add support for domain configuration in zmldapsync --- zmldapsync/README.md | 9 ++++++++ zmldapsync/zmldapsync.pl | 50 +++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/zmldapsync/README.md b/zmldapsync/README.md index b3d4d2b..7738441 100644 --- a/zmldapsync/README.md +++ b/zmldapsync/README.md @@ -15,6 +15,7 @@ The goals are : A few other features are included, like : * Creating domains * Creating / removing domain aliases + * Configure domain attributes All from a simple yaml configuration file @@ -152,6 +153,14 @@ domains: domain_aliases: - mail.corp2.com - corp4.net + # A list of attr which will be set using zmprov modifyDomain. The key is the Zimbra attr in LDAP, as you can get/set with zmprov + additional_domain_attrs: + zimbraVirtualHostname: + - zm.corep.com + - mail.corp2.com + zimbraPublicServicePort: 443 + zimbraPublicServiceProtocol: https + zimbraPublicServiceHostname: mail.corp2.com ``` ## Command line diff --git a/zmldapsync/zmldapsync.pl b/zmldapsync/zmldapsync.pl index 5c3ea37..3c2bf3a 100644 --- a/zmldapsync/zmldapsync.pl +++ b/zmldapsync/zmldapsync.pl @@ -130,7 +130,11 @@ DOMAIN: foreach my $domain ( keys $conf->{domains} ) { } # Get LDAP entry representing the domain - my $domain_entry = ldap2hashref( $zim_domain_search, 'zimbraDomainName' )->{$domain}; + my $domain_entry = ldap2hashref( + $zim_domain_search, + 'zimbraDomainName', + [ 'zimbraVirtualHostname' ] + )->{$domain}; # Check if auth is set to ad or ldap if ( not defined $domain_entry->{zimbraAuthMech} or @@ -166,6 +170,33 @@ DOMAIN: foreach my $domain ( keys $conf->{domains} ) { } } + # Domain configuration, as defined in the conf + if (defined $conf->{domains}->{$domain}->{zimbra}->{additional_domain_attrs} ) { + my $attr_mod = ''; + foreach my $attr (keys $conf->{domains}->{$domain}->{zimbra}->{additional_domain_attrs} ) { + if ( ref $conf->{domains}->{$domain}->{zimbra}->{additional_domain_attrs}->{$attr} eq 'ARRAY' ) { + my $attr_diff = Array::Diff->diff( + $domain_entry->{$attr} || [], + $conf->{domains}->{$domain}->{zimbra}->{additional_domain_attrs}->{$attr} + ); + foreach ( @{ $attr_diff->added } ) { + $attr_mod .= " +$attr " . zim_attr_value($_); + } + foreach ( @{ $attr_diff->deleted } ) { + $attr_mod .= " -$attr " . zim_attr_value($_); + } + } else { + if ( ($domain_entry->{$attr} || '') ne $conf->{domains}->{$domain}->{zimbra}->{additional_domain_attrs}->{$attr} ) { + $attr_mod .= " $attr " . zim_attr_value( $conf->{domains}->{$domain}->{zimbra}->{additional_domain_attrs}->{$attr} ); + } + } + } + if ($attr_mod ne ''){ + log_info( "Domain $domain configuration must be updated ($attr_mod)" ); + send_zmprov_cmd( "modifyDomain $domain $attr_mod" ); + } + } + log_verbose( "Trying to connect to " . join( ' or ', @{ $conf->{domains}->{$domain}->{ldap}->{servers} } ) ); @@ -873,16 +904,6 @@ sub search_zim_domain { # Search in Zimbra LDAP if the required domain exists my $zim_domain_search = $zim_ldap->ldap->search( filter => "(&(objectClass=zimbraDomain)(zimbraDomainName=$dom)(!(zimbraDomainAliasTargetId=*)))", - attrs => [ - 'zimbraDomainName', - 'zimbraDomainType', - 'zimbraId', - 'zimbraAuthMechAdmin', - 'zimbraAuthMech', - 'zimbraAuthLdapSearchBindDn', - 'zimbraAuthLdapSearchBindPassword', - 'zimbraAuthLdapSearchFilter' - ] ); return $zim_domain_search; } @@ -1036,9 +1057,10 @@ sub get_default_conf { } $defaults->{zimbra} = { - create_if_missing => 0, - setup_ldap_auth => 0, - domain_aliases => undef, + create_if_missing => 0, + setup_ldap_auth => 0, + domain_aliases => undef, + additional_domain_attrs => {} }; # If some attribute mapping is defined in the provided conf