summaryrefslogtreecommitdiff
path: root/bin/ldap.add.staff
blob: fc744f2ac0d164688c9c342f2cceebaaab96806a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/ruby

#
# Copyright (c) 2004, 2006 Peter Palfrader <peter@palfrader.org>
#
# All rights reserved.
#

require "ldap"
require "optparse"
require "myldap"
require "yaml"

config = YAML::load( File.open( '/etc/noreply/config' ) )

print_usage = false
is_admin = false
username = nil
password = [File.new("/dev/urandom").read(config['module']['staff']['pwlen'])].pack("m").chomp.delete('=')
description = nil

def show_help(parser, code=0, io=STDOUT)
  io.puts parser
  exit(code)
end
ARGV.options do |opts|
  opts.on_tail("-h", "--help", "Display this help screen") { show_help(opts) }
  opts.on("-u", "--username=USERNAME"   , String, "User name")         { |username| }
  opts.on("-p", "--password=PASSWORD"   , String, "Password")          { |password| }
  opts.on("-a", "--admin"               , String, "user is an admin")  { is_admin = true }
  opts.on("-D", "--description=BLA"     , String, "description")       { |description| }
  opts.parse!
end

show_help(ARGV.options, 1, STDERR) if ARGV.length > 0
show_help(ARGV.options, 1, STDERR) unless username

data = {
	'objectclass'	=> ['top', 'organizationalRole', 'simpleSecurityObject'],
	'cn'		=> [username],
	'userPassword'	=> [password]
}
data['description'] = [description] if description

dn = "cn=%s,%sou=staff,%s"%[username, is_admin ? 'ou=admins,' : '', config['basedn']]

puts dn
puts data.to_yaml
puts

MyLDAP.new(config).add(dn, data)