Commit d7a3f878c984040f478064f7ddabe0bfbfa13d8e

Authored by Georg GH. Hopp
Committed by Georg Hopp
1 parent f4221819

initial checkin - nothings final

This is the first checkin for some experiments with net-ldap
to create an admin tool for my ldap database (user, groups, domains,
mail-aliases and mail-accounts)
The final tool should make it ease to add, modify and delete each of
these with either a terminal- or a webinterface.
  1 +.*.swp
  1 +require 'SystemData'
  2 +
  3 +class Group < SystemData
  4 + attr_accessor :name, :gid, :members
  5 +
  6 + def initialize(args = {})
  7 + super(args)
  8 +
  9 + @name = args[:name]
  10 + @gid = args[:gid]
  11 + @members = args[:members]
  12 + @members = Array.new if @members.nil?
  13 + end
  14 +end
  1 +require 'SystemData'
  2 +
  3 +class MailAccount < SystemData
  4 + attr_accessor :mail, :mailbox
  5 +
  6 + def initialize(args = {})
  7 + super(args)
  8 +
  9 + @mail = args[:mail]
  10 + @home = args[:home]
  11 + @mailbox = args[:mailbox]
  12 + end
  13 +
  14 + def site
  15 + @mail.sub(/.*@/, '')
  16 + end
  17 +end
  1 +require 'SystemData'
  2 +
  3 +class MailAlias < SystemData
  4 + attr_accessor :mail, :maildrop, :user
  5 +
  6 + def initialize(args = {})
  7 + super(args)
  8 +
  9 + @mail = args[:mail]
  10 + @maildrop = args[:maildrop]
  11 + @user = args[:user]
  12 + end
  13 +
  14 + def site
  15 + @mail.sub(/.*@/, '')
  16 + end
  17 +end
  1 +require 'SystemData'
  2 +
  3 +class Site < SystemData
  4 + attr_accessor :name, :mailAccounts, :mailAliases
  5 +
  6 + def initialize(args = {})
  7 + @name = args[:name]
  8 + end
  9 +end
  1 +class SystemData
  2 + attr_reader :id
  3 +
  4 + def initialize(args = {})
  5 + @backend = args[:backend]
  6 + @id = args[:id]
  7 + end
  8 +
  9 + def save
  10 + if @id
  11 + @backend.update(self)
  12 + else
  13 + @backend.insert(self)
  14 + end
  15 + end
  16 +end
  1 +require 'User'
  2 +require 'Group'
  3 +require 'Site'
  4 +require 'MailAlias'
  5 +require 'MailAccount'
  6 +
  7 +class SystemDataBackend
  8 +
  9 + def initialize(backend)
  10 + @backend = backend
  11 + @data = Hash.new
  12 + end
  13 +
  14 + def users
  15 + load(:user)
  16 + end
  17 +
  18 + def groups
  19 + load(:group)
  20 + end
  21 +
  22 + def sites
  23 + load(:site)
  24 + end
  25 +
  26 + def mailAliases
  27 + load(:mailAlias)
  28 + end
  29 +
  30 + def mailAccounts
  31 + load(:mailAccount)
  32 + end
  33 +
  34 + def userByName(name)
  35 + users.find{|user| user.name == name}
  36 + end
  37 +
  38 + def groupByName(name)
  39 + groups.find{|group| group.name == name}
  40 + end
  41 +
  42 + def siteByName(name)
  43 + sites.find{|site| site.name == name}
  44 + end
  45 +
  46 + def usersInGroup(group)
  47 + case
  48 + when group.members.instance_of?(Array):
  49 + Hash[group.members.map{|uid| [uid, userByName(uid)] if userByName(uid)}]
  50 + when goup.members.nil?:
  51 + {}
  52 + else
  53 + {group.members => userByName(group.members)} if userByName(group.members)
  54 + end
  55 + end
  56 +
  57 + def mailAliasesBySite(site)
  58 + mailAliases.find_all{|mail| mail.site == site.name}
  59 + end
  60 +
  61 + def mailAccountsBySite(site)
  62 + mailAccounts.find_all{|mail| mail.site == site.name}
  63 + end
  64 +
  65 + private
  66 +
  67 + def load(kind)
  68 + load!(kind) if ! @data[kind]
  69 + @data[kind]
  70 + end
  71 +
  72 + def load!(kind)
  73 + @data[kind] = Array.new
  74 + class_str = kind.to_s
  75 + class_str[0] = class_str[0..0].capitalize
  76 +
  77 + @backend.load(kind) do |data|
  78 + @data[kind].push(
  79 + eval(class_str + '.new(data)')
  80 + )
  81 + end
  82 + end
  83 +
  84 +end
  1 +require 'active_support/secure_random'
  2 +require 'net/ldap'
  3 +
  4 +class SystemDataBackendLdap
  5 +
  6 + LDAP_USER_MAP = {
  7 + :uid => :name,
  8 + :userpassword => :pass,
  9 + :uidnumber => :uid,
  10 + :gidnumber => :gid,
  11 + :loginshell => :shell,
  12 + :homedirectory => :home
  13 + }
  14 +
  15 + LDAP_GROUP_MAP = {
  16 + :cn => :name,
  17 + :gidnumber => :gid,
  18 + :memberuid => :members
  19 + }
  20 +
  21 + LDAP_SITE_MAP = {:o => :name}
  22 +
  23 + LDAP_MAP = {
  24 + :user => LDAP_USER_MAP,
  25 + :group => LDAP_GROUP_MAP,
  26 + :site => LDAP_SITE_MAP
  27 + }
  28 +
  29 + LDAP_FILTER = {
  30 + :user => Net::LDAP::Filter::eq('objectClass', 'posixAccount'),
  31 + :group => Net::LDAP::Filter::eq('objectClass', 'posixGroup'),
  32 + :site => Net::LDAP::Filter::eq('objectClass', 'organization') &
  33 + (~Net::LDAP::Filter::eq('o', 'hosting')),
  34 + :mailAlias => Net::LDAP::Filter::eq('objectClass', 'mailAlias'),
  35 + :mailAccount => Net::LDAP::Filter::eq('objectClass', 'mailAccount')
  36 + }
  37 +
  38 +
  39 + def initialize(host, port, baseDn, args={})
  40 + @baseDn = baseDn
  41 + @systemDn = 'o=system,' + @baseDn
  42 + @hostingDn = 'o=hosting,' + @baseDn
  43 +
  44 + @systemDn = args[:systemDn] if args[:systemDn]
  45 + @hostingDn = args[:hostingDn] if args[:hostingDn]
  46 +
  47 + @ldap = Net::LDAP.new(:host => host, :port => port)
  48 + @ldapData = Hash.new
  49 + end
  50 +
  51 + def load!(kind)
  52 + @ldapData[kind] = @ldap.search(
  53 + :base => ldapBase(kind),
  54 + :filter => LDAP_FILTER[kind]
  55 + )
  56 + end
  57 +
  58 + def load(kind)
  59 + load!(kind) if ! @ldapData[kind]
  60 +
  61 + @ldapData[kind].each do |data|
  62 + map = { :dn => :id }
  63 + map.merge!(LDAP_MAP[kind]) if LDAP_MAP[kind]
  64 +
  65 + ydata = { :backend => self }
  66 +
  67 + data.each do |key,value|
  68 + ydata.merge!({ map[key] || key => value.size==1?value[0]:value.to_a })
  69 + end
  70 +
  71 + yield ydata
  72 + end
  73 + end
  74 +
  75 + private
  76 +
  77 + def ldapBase(kind)
  78 + case(kind)
  79 + when :user, :group: @systemDn
  80 + when :site, :mailAlias, :mailAccount: @hostingDn
  81 + end
  82 + end
  83 +
  84 +end
  1 +require 'SystemData'
  2 +
  3 +class User < SystemData
  4 + attr_accessor :name, :pass, :uid, :gid, :shell, :home
  5 +
  6 + def initialize(args = {})
  7 + super(args)
  8 +
  9 + @name = args[:name]
  10 + @pass = args[:pass]
  11 + @uid = args[:uid]
  12 + @gid = args[:gid]
  13 + @shell = args[:shell]
  14 + @home = args[:home]
  15 + end
  16 +end
  1 +require 'SystemDataBackend'
  2 +require 'SystemDataBackendLdap'
  3 +
  4 +backend = SystemDataBackend.new(
  5 + SystemDataBackendLdap.new('host.one.virtual', 389, 'dc=weird-web-workers,dc=org')
  6 +)
  7 +
  8 +backend.users.each do |user|
  9 + puts 'user: '+user.name+','+user.uid+','+user.gid+','+user.home
  10 +end
  11 +
  12 +puts
  13 +
  14 +backend.groups.each do |group|
  15 + members = backend.usersInGroup(group)
  16 +
  17 + puts 'group: '+group.name+','+group.gid
  18 + puts ' members:'
  19 + group.members.each do |name|
  20 + print ' ' + name + ': '
  21 + print members[name].uid + ', ' + members[name].home if members[name]
  22 + puts
  23 + end
  24 + puts
  25 +end
  26 +
  27 +backend.sites.each do |site|
  28 + puts 'site: ' + site.name
  29 + puts ' MailAliases:'
  30 + backend.mailAliasesBySite(site).each{|account| puts ' ' + account.mail}
  31 + puts
  32 + puts ' MailAccounts:'
  33 + backend.mailAccountsBySite(site).each{|account| puts ' ' + account.mail}
  34 + puts
  35 +end
  36 +
  37 +backend.mailAliases.each do |mailAlias|
  38 + puts 'mailAlias: '+mailAlias.mail.inspect+','+mailAlias.maildrop.inspect
  39 +end
  40 +
  41 +puts
  42 +
  43 +backend.mailAccounts.each do |mailAccount|
  44 + puts 'mailAccount: '+mailAccount.mail.inspect+','+mailAccount.mailbox.inspect
  45 +end
  46 +
Please register or login to post a comment