From b26a4ba56550372789b0f452402ced1797f5d72b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 28 Jun 2018 15:45:14 +0200 Subject: [PATCH] New synchronization script: fetch values from LDAP and mlmmj --- README.md | 3 ++ conf.example.toml | 18 +++++++++++ unipoly-mlmmj-sync.rb | 73 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 README.md create mode 100644 conf.example.toml create mode 100755 unipoly-mlmmj-sync.rb diff --git a/README.md b/README.md new file mode 100644 index 0000000..1249f9e --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# mlmmj-ldap-sync + +Allows to sync LDAP groups with mlmmj lists. diff --git a/conf.example.toml b/conf.example.toml new file mode 100644 index 0000000..3e63855 --- /dev/null +++ b/conf.example.toml @@ -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" + diff --git a/unipoly-mlmmj-sync.rb b/unipoly-mlmmj-sync.rb new file mode 100755 index 0000000..9df06a5 --- /dev/null +++ b/unipoly-mlmmj-sync.rb @@ -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()