-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplexLDAP.rb
More file actions
87 lines (73 loc) · 2.13 KB
/
Copy pathplexLDAP.rb
File metadata and controls
87 lines (73 loc) · 2.13 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'net/ldap'
require 'yaml'
require 'logger'
require_relative 'plexTv.rb'
class PlexLDAP
def initialize
begin
config = YAML.load_file('config.yaml')
rescue Errno::ENOENT => e
abort('Configuration file not found. Exiting...')
end
begin
$logger = Logger.new('plexldap.log')
rescue Errno::ENOENT => e
abort('Log file not found. Exiting...')
end
$ldap = Net::LDAP.new
$ldap.host = config['ldap']['server']
$ldap.port = config['ldap']['port']
$ldap.base = config['ldap']['base']
$ldap.auth(config['ldap']['bind_user'], config['ldap']['bind_password'])
begin
$ldap.bind
rescue Errno::ENOENT => e
$logger.error('Cannot bind to LDAP server')
end
$plexTv = PlexTv.new(config)
end
def createUsers
plex_users = $plexTv.get('/pms/friends/all')
if plex_users.nil? || plex_users.empty?
$logger.error("No Plex friends found.")
else
plex_users['MediaContainer']['User'].each do | user |
if user['username'] == 'Delightful Demon'
dn = "cn=#{user['username']},ou=users,dc=felannisport,dc=com"
attr = {
:cn => user['username'],
:givenname => user['username'],
:gidnumber => "501",
:homedirectory => "/home/users/#{user['username']}",
:sn => user['username'],
:loginshell => "/sbin/nologin",
:objectClass => ["inetOrgPerson","posixAccount","top"],
:uid => user['username'],
:mail => user['email'],
:uidNumber => getNextUIDNumber.to_s,
}
if $ldap.add(:dn => dn, :attributes => attr)
$logger.info("Account #{user['username']} successfully added!")
end
end
end
end
end
def getNextUIDNumber
uidNumber = 0
search_filter = Net::LDAP::Filter.eq('objectClass', 'inetOrgPerson')
$ldap.search(:filter => search_filter, :return_result => false) do |entry|
if entry['uidnumber'][0].to_i > uidNumber
uidNumber = entry['uidnumber'][0].to_i
end
if uidNumber != 0
return uidNumber += 1
end
end
end
end
def main
plexldap = PlexLDAP.new
plexldap.createUsers
end
main()