From ad221c1d338652ee1e426d10a8fe8bea880b0b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Wed, 30 Oct 2019 16:56:30 +0100 Subject: [PATCH] Adapt to gnugen's LDAP tree structure We will eventually support both unipoly and gnugen. --- conf.example.toml | 3 +++ mlmmj-ldap-sync | 24 +++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/conf.example.toml b/conf.example.toml index a90278b..7d59ee0 100644 --- a/conf.example.toml +++ b/conf.example.toml @@ -13,6 +13,9 @@ password = "secret" [ldap.lists] basetree = "ou=Lists,dc=unipoly,dc=epfl,dc=ch" +[ldap.users] +basetree = "ou=Users,dc=unipoly,dc=epfl,dc=ch" + [mlmmj] basepath = "/var/spool/mlmmj" list_binary = "/usr/bin/mlmmj-list" diff --git a/mlmmj-ldap-sync b/mlmmj-ldap-sync index baa3372..261b5f1 100755 --- a/mlmmj-ldap-sync +++ b/mlmmj-ldap-sync @@ -108,16 +108,21 @@ def remove_subscriber_from(list_name, list, addr) end end -def sync_list(list_name, ldap_group_entry) +def sync_list(list_name, ldap_group_entry, user_basetree) mlmmj_basepath = @configuration['mlmmj']['basepath'] list = "#{mlmmj_basepath}/#{list_name}@#{@configuration['domain']}" - # Members are formatted as - # 'mail=user@domain.tld,ou=Users,dc=unipoly,dc=epfl,dc=ch': we extract the - # mail address. - ldap_members = ldap_group_entry.uniquemember.map do |dn| - /mail=([^,]+),/.match(dn).values_at(1).first.downcase + ldap_members = ldap_group_entry.memberuid.map do |uid| + filter = Net::LDAP::Filter.eq('uid', uid) + matched_ldap_users = conn.search(base: user_basetree, filter: filter) + if matched_ldap_users.nil? || matched_ldap_users.empty? + "" + else + matched_ldap_users.first.mail + end end + + ldap_members.select! {|e| not e.empty? } puts "Found #{ldap_group_entry.dn} with #{ldap_members.size} members" # Extract mail addresses from Mlmmj @@ -147,7 +152,8 @@ def main conn = connect_ldap domain = @configuration['domain'] - basetree = @configuration['ldap']['lists']['basetree'] + user_basetree = @configuration['ldap']['user']['basetree'] + list_basetree = @configuration['ldap']['lists']['basetree'] lists = (@configuration['lists_add'] + @configuration['lists_remove']).uniq # Sync Mlmmj lists with LDAP groups @@ -156,14 +162,14 @@ def main print 'Searching LDAP... ' filter = Net::LDAP::Filter.eq('cn', list_name) - matched_ldap_groups = conn.search(base: basetree, filter: filter) + matched_ldap_groups = conn.search(base: list_basetree, filter: filter) if matched_ldap_groups.nil? || matched_ldap_groups.empty? # Could not find matching LDAP group puts 'NOT FOUND' else puts 'OK' ldap_group_entry = matched_ldap_groups.first - sync_list(list_name, ldap_group_entry) + sync_list(list_name, ldap_group_entry, user_basetree) end end end