Adapt to gnugen's LDAP tree structure

We will eventually support both unipoly and gnugen.
This commit is contained in:
Timothée Floure 2019-10-30 16:56:30 +01:00
parent 3a66832fac
commit ad221c1d33
2 changed files with 18 additions and 9 deletions

View File

@ -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"

View File

@ -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