summaryrefslogtreecommitdiffstats
path: root/openldap-model.lua
blob: 88391711c4a0f0d5865aa56adcddb33dcd76818e (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
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
88
89
90
91
92
93
94
95
96
97
98
99
local mymodule = {}

modelfunctions = require("modelfunctions")
posix = require("posix")
format = require("acf.format")
fs = require("acf.fs")
processinfo = require("acf.processinfo")
validator = require("acf.validator")
date = require("acf.date")

local processname = "slapd"
local packagename = "openldap"
local configfile = "/etc/openldap/slapd.conf"

function mymodule.set_processname(p)
	processname = p
	configfile = "/etc/openldap/"..processname..".conf"
end

local function log_content( f )
		return fs.read_file(f) or ""
end

function mymodule.getstatus()
	return modelfunctions.getstatus(processname, packagename, "OpenLDAP Status")
end

function mymodule.get_startstop(self, clientdata)       
        return modelfunctions.get_startstop(processname)
end

function mymodule.startstop_service(self, startstop, action)        
        return modelfunctions.startstop_service(startstop, action)
end

function mymodule.get_logfile(f)
	local config = log_content(configfile)
	return cfe({ value=config.log or "", label="Log file" })
end

function mymodule.get_filecontent()
	return modelfunctions.getfiledetails(configfile)
end

function mymodule.update_filecontent(self, filedetails)
	return modelfunctions.setfiledetails(self, filedetails, {configfile})
end

function mymodule.get_ldapsearch(self, ldapdata)
	local cnf =  {}
	cnf = parse_config(configfile)
        local result = {}
        local cmd = {"ldapsearch", "-x", "-L", "ldap://127.0.0.1", "-b", "dc=teste,dc=local"}
        local file = modelfunctions.run_executable(cmd)
	
	result.file = cfe({type="select", option={"/etc/openldap/slapd.conf", "/etc/openldap/ldap.conf"}, label="File"})
	result.version= cfe({label="Version", description="LDIF file format version"})
	result.basedn = cfe({label="Root DN"})
	
        for line in string.gmatch(file, "([^\n]+)") do
                if string.match(line, "version") then
                        --local cn = string.match(line, "version: (%d)")
                        --local entry = string.match(line, "^result(%w+)") or "Entrada"
                        --result.value = cfe({value=cn})
			result.version.value = string.match(line, "version: (%d)")
			result.version.readonly = true
			result.basedn.value = cnf.suffix or ""
                end
        end

        return cfe({type="group", value=result, label="Ldap Search Result"})
end


function parse_config(configfile)
	local file = io.open (configfile)
	local confs = {}
	
	if (file) then
		local alllines = file:read("*all")
		for line in string.gmatch(alllines, "([^\n]+)") do
			-- Ignoring lines starting by #
			if string.match(line,"^%a") then
				for key, value in string.gmatch(line,"(%w*)%s(.*)") do
					if key then
						confs[key]  = string.gsub(value,"%s*","")
					end
				end
			end
		end
	end
	
	file:close()
	
	return confs
end


return mymodule