# # Copyright (c) 2004 Peter Palfrader # # All rights reserved. # class MyLDAP def initialize(config, use = nil) @conn = LDAP::Conn.new(config['ldapserver'], config['ldapport']) @conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @basedn = config['basedn'] if use unless config['credentials'][use] throw "No credentials for #{use} found." end unless config['credentials'][use]['anon'] @binddn = config['credentials'][use]['binddn'] @bindpw = config['credentials'][use]['bindpw'] end elsif File.exists? File.expand_path('~/.noreply.ldap') myconfig = YAML::load( File.open( File.expand_path('~/.noreply.ldap') ) ) @binddn = myconfig['binddn'] @bindpw = myconfig['bindpw'] end if @binddn and @bindpw unless @conn.bind(@binddn, @bindpw) @conn.perror("bind") end end end def add(dn, data) puts "dn: #{dn}" data.each_pair{ |key, value| value.each { |v| puts "#{key}: #{v}" } } puts begin entry = data.map{ |key, value| LDAP.mod(LDAP::LDAP_MOD_ADD, key, value) } @conn.add(dn, entry) rescue LDAP::ResultError @conn.perror("add") return false end @conn.perror("add") return true end def conn() return @conn end def verify_client(client) begin clients = @conn.search2(@basedn, LDAP::LDAP_SCOPE_SUBTREE, '(&(objectclass=tnClient)(o='+client+'))') rescue LDAP::ResultError => msg $stderr.print(msg) exit 1 end if clients.length != 1 STDERR.puts "Found %s clients with o=%s"%[clients.length, client] exit 1 end return clients.pop end def verify_local_domains_exist(addresses) domains = addresses.collect{ |a| a =~ /@(.*)/ domain = $1 unless domain STDERR.puts "%s is no email address"%[a] exit 1 end domain }.uniq domains.each { |d| begin doms = @conn.search2(@basedn, LDAP::LDAP_SCOPE_SUBTREE, '(&(objectclass=tnMailDomain)(tnMailDomainname='+d+'))') rescue LDAP::ResultError => msg $stderr.print(msg) exit 1 end if doms.length != 1 STDERR.puts "Found %s tnMailDomains with tnMailDomainname=%s"%[doms.length, d] exit 1 end puts "Domain %s: check"%[d] } end end