From c88bc35f1c88d9fbbba6706a4abaad24a1868c98 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Wed, 18 Oct 2006 11:33:32 +0000 Subject: Add hosting ldap git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@190 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- site-ruby/myldap.rb | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 site-ruby/myldap.rb (limited to 'site-ruby') diff --git a/site-ruby/myldap.rb b/site-ruby/myldap.rb new file mode 100644 index 0000000..115d772 --- /dev/null +++ b/site-ruby/myldap.rb @@ -0,0 +1,88 @@ +# +# Copyright (c) 2004 Peter Palfrader +# +# All rights reserved. +# + +class MyLDAP + def initialize(config, use = nil) + @conn = LDAP::Conn.new(config['ldapserver'], config['ldapport']) + @basedn = config['basedn'] + if use + @binddn = config['credentials'][use]['binddn'] + @bindpw = config['credentials'][use]['bindpw'] + else + myconfig = YAML::load( File.open( File.expand_path('~/.noreply.ldap') ) ) + @binddn = myconfig['binddn'] + @bindpw = myconfig['bindpw'] + end + unless @conn.bind(@binddn, @bindpw) + @conn.perror("bind") + end + end + + def add(dn, data) + 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 + -- cgit v1.2.3