-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-ldif.rb
executable file
·50 lines (38 loc) · 1.04 KB
/
generate-ldif.rb
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
#!/usr/bin/env ruby
require 'erb'
require 'net/ldap'
require 'net/ldap/dn'
SERVICES_BLACKLIST = [
'Mon' # conflicts with 'mon'
]
LDIF_HEADER = <<~EOF
dn: dc=brokenbottle,dc=net
objectClass: organization
objectClass: dcObject
dc: brokenbottle
o: brokenbottle
dn: ou=services,dc=brokenbottle,dc=net
objectClass: organizationalUnit
objectClass: top
ou: services
EOF
LDIF_TEMPLATE = <<~EOF
dn: cn=<%= Net::LDAP::DN.escape(names[0]) %>+ipServiceProtocol=<%= proto %>,ou=services,dc=brokenbottle,dc=net
objectClass: ipService
objectClass: top
<% names.each do |name| -%>
cn: <%= Net::LDAP::DN.escape(name) %>
<% end -%>
ipServicePort: <%= port %>
ipServiceProtocol: <%= proto %>
EOF
puts LDIF_HEADER
IO.popen(['/usr/bin/getent', 'services']) do |services|
services.each do |s|
(service, portspec, aliases) = s.split(/\s+/, 3)
(port, proto) = portspec.split('/')
next if SERVICES_BLACKLIST.include? service
names = [service] + aliases.split
puts ERB.new(LDIF_TEMPLATE, 0, '-').result(binding)
end
end