New synchronization script: fetch values from LDAP and mlmmj

This commit is contained in:
Timothée Floure 2018-06-28 15:45:14 +02:00
parent 8fccf87a94
commit b26a4ba565
3 changed files with 94 additions and 0 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# mlmmj-ldap-sync
Allows to sync LDAP groups with mlmmj lists.

18
conf.example.toml Normal file
View File

@ -0,0 +1,18 @@
domain = "unipoly.ch"
lists = [ "membres"]
[ldap]
host = "ldap.gnugen.ch"
port = 389
[ldap.auth]
username = "cn=unipoly-mlmmj,ou=Services,dc=unipoly,dc=epfl,dc=ch"
password = "secret"
[ldap.lists]
basetree = "ou=Lists,dc=unipoly,dc=epfl,dc=ch"
[mlmmj]
basepath = "/var/spool/mlmmj"
list_binary = "/usr/bin/mlmmj-list"

73
unipoly-mlmmj-sync.rb Executable file
View File

@ -0,0 +1,73 @@
#!/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()