mailman3-ldap-sync/unipoly-mlmmj-sync.rb

74 lines
1.8 KiB
Ruby
Raw Normal View History

#!/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 ldap_connect(ldap)
ldap.bind
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|
filter = Net::LDAP::Filter.eq("cn", cn)
match = conn.search(:base => basetree, :filter => filter)
unless (match.size < 1)
entry = match.first
puts "Found: #{entry.dn} with #{entry.uniquemember.size} entries"
mlmmj_list_binary = conf["mlmmj"]["list_binary"]
mlmmj_basepath = conf["mlmmj"]["basepath"]
if (File.executable?(mlmmj_list_binary))
subscribers = %x(#{mlmmj_list_binary} -L #{mlmmj_basepath}/#{cn}@#{domain} -s)
unless ($?.exitstatus == 0)
puts "Got #{subscribers.split("\n").size} subscribers from mlmmj".
else
puts "Failed to get the subscribers of #{cn}@#{domain}"
end
else
puts "Could not execute #{mlmmj_list_binary}"
end
else
dn = "cn=#{cn},#{basetree}"
puts "Unable to find list: #{dn}"
end
end
end
main()