2018-06-28 15:45:14 +02:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'toml'
|
|
|
|
require 'net/ldap'
|
|
|
|
|
|
|
|
@configuration_file = "conf.example.toml"
|
|
|
|
|
|
|
|
def read_configuration(path)
|
|
|
|
TOML.load_file(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect_ldap(conf)
|
|
|
|
conn = Net::LDAP.new(
|
|
|
|
:host => conf["ldap"]["host"],
|
|
|
|
:port => conf["ldap"]["port"],
|
|
|
|
:auth => {
|
|
|
|
:method => :simple,
|
|
|
|
:username => conf["ldap"]["auth"]["username"],
|
|
|
|
:password => conf["ldap"]["auth"]["password"]
|
|
|
|
})
|
|
|
|
|
|
|
|
begin
|
|
|
|
if conn.bind
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
puts "Failed to authenticate against LDAP server: \
|
|
|
|
#{conf["ldap"]["host"]}:#{conf["ldap"]["port"]}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
puts "Failed to contact LDAP server: \
|
|
|
|
#{conf["ldap"]["host"]}:#{conf["ldap"]["port"]}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def main
|
|
|
|
conf = read_configuration(@configuration_file)
|
|
|
|
conn = connect_ldap(conf)
|
|
|
|
|
|
|
|
domain = conf["domain"]
|
|
|
|
basetree = conf["ldap"]["lists"]["basetree"]
|
|
|
|
conf["lists"].each do |cn|
|
2018-06-28 17:44:58 +02:00
|
|
|
puts ">> Processing list #{cn}@#{domain}"
|
|
|
|
|
2018-06-28 15:45:14 +02:00
|
|
|
filter = Net::LDAP::Filter.eq("cn", cn)
|
2018-06-28 17:44:58 +02:00
|
|
|
|
|
|
|
print "Searching LDAP... "
|
2018-06-28 15:45:14 +02:00
|
|
|
match = conn.search(:base => basetree, :filter => filter)
|
2018-06-28 17:44:58 +02:00
|
|
|
if (match.size > 0)
|
2018-06-28 15:45:14 +02:00
|
|
|
entry = match.first
|
2018-06-28 17:44:58 +02:00
|
|
|
members = entry.uniquemember.map do |dn|
|
2018-06-28 17:53:31 +02:00
|
|
|
/mail=([^,]+),/.match(dn).values_at(1).first.downcase
|
2018-06-28 17:44:58 +02:00
|
|
|
end
|
2018-06-28 15:45:14 +02:00
|
|
|
|
2018-06-28 17:44:58 +02:00
|
|
|
puts "OK"
|
|
|
|
puts "Found #{entry.dn} with #{members.size} members"
|
|
|
|
|
|
|
|
print "Searchin mlmmj... "
|
2018-06-28 17:53:31 +02:00
|
|
|
mlmmj_list_binary = conf["mlmmj"]["list_binary"]
|
|
|
|
mlmmj_basepath = conf["mlmmj"]["basepath"]
|
|
|
|
list = "#{mlmmj_basepath}/#{cn}@#{domain}"
|
2018-06-28 15:45:14 +02:00
|
|
|
if (File.executable?(mlmmj_list_binary))
|
2018-06-28 17:44:58 +02:00
|
|
|
raw = %x(#{mlmmj_list_binary} -L #{list} -s)
|
|
|
|
|
2018-06-28 17:23:42 +02:00
|
|
|
if ($?.exitstatus == 0)
|
2018-06-28 17:53:31 +02:00
|
|
|
puts "OK"
|
2018-06-28 17:23:42 +02:00
|
|
|
subscribers = raw.split("\n")
|
2018-06-28 17:44:58 +02:00
|
|
|
puts "Found #{cn}@#{domain} with #{subscribers.size} subscribers"
|
|
|
|
print "\n"
|
|
|
|
|
|
|
|
# Add to the subscribers any missing entry
|
|
|
|
# Remove successful matches
|
|
|
|
members.each do |m|
|
|
|
|
if (subscribers.include?(m))
|
|
|
|
subscribers.delete(m)
|
|
|
|
else
|
2018-06-28 17:53:31 +02:00
|
|
|
print "Adding #{m}... "
|
2018-06-28 17:44:58 +02:00
|
|
|
mlmmj_sub_binary = conf["mlmmj"]["sub_binary"]
|
|
|
|
%x(#{mlmmj_sub_binary} -L #{list} -a #{m} -q -f -s)
|
|
|
|
if ($?.exitstatus == 0)
|
|
|
|
puts "OK"
|
|
|
|
else
|
|
|
|
puts "Failed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-28 17:23:42 +02:00
|
|
|
|
2018-06-28 17:44:58 +02:00
|
|
|
# Remove remaining "subcribers" (= they did not match with members)
|
|
|
|
subscribers.each do |s|
|
2018-06-28 17:53:31 +02:00
|
|
|
print "Removing #{s}... "
|
2018-06-28 17:44:58 +02:00
|
|
|
mlmmj_unsub_binary = conf["mlmmj"]["unsub_binary"]
|
2018-06-28 17:53:31 +02:00
|
|
|
%x(#{mlmmj_unsub_binary} -L #{list} -a #{s} -q -s)
|
2018-06-28 17:44:58 +02:00
|
|
|
if ($?.exitstatus == 0)
|
|
|
|
puts "OK"
|
2018-06-28 17:23:42 +02:00
|
|
|
else
|
2018-06-28 17:44:58 +02:00
|
|
|
puts "Failed"
|
2018-06-28 17:23:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
print "\n"
|
2018-06-28 15:45:14 +02:00
|
|
|
else
|
2018-06-28 17:44:58 +02:00
|
|
|
puts "FAIL"
|
2018-06-28 15:45:14 +02:00
|
|
|
end
|
|
|
|
else
|
2018-06-28 17:44:58 +02:00
|
|
|
puts "could not execute #{mlmmj_list_binary}"
|
2018-06-28 15:45:14 +02:00
|
|
|
end
|
|
|
|
else
|
2018-06-28 17:44:58 +02:00
|
|
|
puts "FAIL"
|
2018-06-28 15:45:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
main()
|