#!/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| filter = Net::LDAP::Filter.eq("cn", cn) match = conn.search(:base => basetree, :filter => filter) unless (match.size < 1) entry = match.first members = entry.uniquemember.map { |dn| /mail=([^,]+),/.match(dn).values_at(1).first } puts "Found: #{entry.dn} with #{members.size} entries" mlmmj_list_binary = conf["mlmmj"]["list_binary"] mlmmj_basepath = conf["mlmmj"]["basepath"] if (File.executable?(mlmmj_list_binary)) raw = %x(#{mlmmj_list_binary} -L #{mlmmj_basepath}/#{cn}@#{domain} -s) if ($?.exitstatus == 0) subscribers = raw.split("\n") puts "Got #{subscribers.size} subscribers from mlmmj for #{cn}@#{domain}" members.each do |member| if (subscribers.include?(member)) subscribers.delete(member) else puts "#{member} is to be added to #{cn}" end end print "There are #{subscribers.size} addesses to remove:" subscribers.each { |s| print " " + s} print "\n" 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()