Fix missed errors introduced by refactoring (see precedent commit)

This commit is contained in:
Timothée Floure 2018-12-23 16:36:48 +01:00
parent d147c917f2
commit beeca5ce46

View file

@ -2,6 +2,7 @@
require 'toml' require 'toml'
require 'net/ldap' require 'net/ldap'
require 'English'
@configuration_file = '/etc/unipoly-mlmmj-ldap-sync.toml' @configuration_file = '/etc/unipoly-mlmmj-ldap-sync.toml'
@configuration = nil @configuration = nil
@ -15,10 +16,10 @@ def read_configuration(path)
end end
end end
def connect_ldap(conf) def connect_ldap
conn = Net::LDAP.new( conn = Net::LDAP.new(
host: conf['ldap']['host'], host: @configuration['ldap']['host'],
port: conf['ldap']['port'], port: @configuration['ldap']['port'],
auth: { auth: {
method: :simple, method: :simple,
username: @configuration['ldap']['auth']['username'], username: @configuration['ldap']['auth']['username'],
@ -26,26 +27,23 @@ def connect_ldap(conf)
} }
) )
ldap_addr = "#{@configuration['ldap']['host']}:#{@configuration['ldap']['port']}"
begin begin
if conn.bind if conn.bind
conn conn
else else
puts "Failed to authenticate against LDAP server: \ puts "Failed to authenticate against LDAP server: #{ldap_addr}"
#{conf['ldap']['host']}:#{conf['ldap']['port']}"
exit(1) exit(1)
end end
rescue LdapError => raised_error rescue LdapError => raised_error
puts "Failed to contact LDAP server: \ puts "Failed to contact LDAP server: #{ldap_addr}"
#{conf['ldap']['host']}:#{conf['ldap']['port']}"
puts raised_error puts raised_error
exit(1) exit(1)
end end
end end
def get_mlmmj_subscribers(list_name) def get_mlmmj_subscribers(list)
mlmmj_list_binary = @configuration['mlmmj']['list_binary'] mlmmj_list_binary = @configuration['mlmmj']['list_binary']
mlmmj_basepath = @configuration['mlmmj']['basepath']
list = "#{mlmmj_basepath}/#{list_name}@#{@configuration['domain']}"
unless File.executable?(mlmmj_list_binary) unless File.executable?(mlmmj_list_binary)
puts "could not execute #{mlmmj_list_binary}" puts "could not execute #{mlmmj_list_binary}"
@ -94,13 +92,13 @@ def remove_subscriber_from(list_name, list, addr)
# We ignore this action if the configuration file doesn't say we are allowed # We ignore this action if the configuration file doesn't say we are allowed
# to remove addresses from this list. # to remove addresses from this list.
unless conf['lists_remove'].include?(list_name) unless @configuration['lists_remove'].include?(list_name)
puts 'IGNORED' puts 'IGNORED'
return return
end end
# Unsubscribe the address from the list. # Unsubscribe the address from the list.
mlmmj_unsub_binary = conf['mlmmj']['unsub_binary'] mlmmj_unsub_binary = @configuration['mlmmj']['unsub_binary']
`#{mlmmj_unsub_binary} -L #{list} -a #{s} -q -s` `#{mlmmj_unsub_binary} -L #{list} -a #{s} -q -s`
if $CHILD_STATUS.exitstatus.zero? if $CHILD_STATUS.exitstatus.zero?
puts 'OK' puts 'OK'
@ -110,26 +108,28 @@ 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)
mlmmj_basepath = @configuration['mlmmj']['basepath']
list = "#{mlmmj_basepath}/#{list_name}@#{@configuration['domain']}"
# Members are formatted as # Members are formatted as
# 'mail=user@domain.tld,ou=Users,dc=unipoly,dc=epfl,dc=ch': we extract the # 'mail=user@domain.tld,ou=Users,dc=unipoly,dc=epfl,dc=ch': we extract the
# mail address. # mail address.
ldap_members = ldap_group_entry.uniquemember.map do |dn| ldap_members = ldap_group_entry.uniquemember.map do |dn|
/mail=([^,]+),/.match(dn).values_at(1).first.downcase /mail=([^,]+),/.match(dn).values_at(1).first.downcase
end end
puts "Found #{entry.dn} with #{members.size} members" puts "Found #{ldap_group_entry.dn} with #{ldap_members.size} members"
# Extract mail addresses from Mlmmj # Extract mail addresses from Mlmmj
print 'Searchin mlmmj... ' print 'Searchin mlmmj... '
mlmmj_subscribers = get_mlmmj_subscribers(list_name) mlmmj_subscribers = get_mlmmj_subscribers(list)
puts 'OK' puts 'OK'
puts "Found #{entry.cn}@#{@configuration['domain']} with #{subscribers.size} \ puts "Found #{list_name}@#{@configuration['domain']} with\
subscribers" #{mlmmj_subscribers.size} subscribers"
print "\n"
# Add to the subscribers any missing entry # Add to the subscribers any missing entry
# Remove successful matches # Remove successful matches
ldap_members.each do |addr| ldap_members.each do |addr|
add_subscriber_to(list_name, subscribers, list, addr) add_subscriber_to(list_name, mlmmj_subscribers, list, addr)
end end
# Remove remaining "subcribers" (= they did not match with LDAP members) # Remove remaining "subcribers" (= they did not match with LDAP members)
@ -142,7 +142,7 @@ end
def main def main
# Parse configuration, bind to LDAP server # Parse configuration, bind to LDAP server
@configuration = read_configuration(@configuration_file) @configuration = read_configuration(@configuration_file)
conn = connect_ldap(conf) conn = connect_ldap()
domain = @configuration['domain'] domain = @configuration['domain']
basetree = @configuration['ldap']['lists']['basetree'] basetree = @configuration['ldap']['lists']['basetree']
@ -161,7 +161,7 @@ def main
else else
puts 'OK' puts 'OK'
ldap_group_entry = matched_ldap_groups.first ldap_group_entry = matched_ldap_groups.first
sync_list_with(list_name, ldap_group_entry) sync_list(list_name, ldap_group_entry)
end end
end end
end end