diff options
author | Peter Palfrader <peter@palfrader.org> | 2006-10-18 11:33:32 +0000 |
---|---|---|
committer | weasel <weasel@bc3d92e2-beff-0310-a7cd-cc87d7ac0ede> | 2006-10-18 11:33:32 +0000 |
commit | c88bc35f1c88d9fbbba6706a4abaad24a1868c98 (patch) | |
tree | 487c31421b2f92e6e76bcf946500187b6b014e91 /site-ruby |
Add hosting ldap
git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@190 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede
Diffstat (limited to 'site-ruby')
-rw-r--r-- | site-ruby/myldap.rb | 88 |
1 files changed, 88 insertions, 0 deletions
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 <peter@palfrader.org> +# +# 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 + |