summaryrefslogtreecommitdiffstats
path: root/samba-model.lua
blob: 628929dfb6b4decd94595bc47a00064c91358da2 (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
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
require("validator")
require("fs")
require("posix")
require("format")

-- Set variables
local configfile = "/etc/samba/smb.conf"
local confdfile = "/etc/conf.d/samba"
local processname = "smbd"
local packagename = "samba"
local initname = "samba"

local configcontent
local config

local path="PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "

-- ################################################################################
-- LOCAL FUNCTIONS

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service(action)
	return modelfunctions.startstop_service(initname, action)
end

function getstatus()
	return modelfunctions.getstatus(processname, packagename, "Samba Status", initname)
end

function getconfigfile()
	return modelfunctions.getfiledetails(configfile)
end

function setconfigfile(filedetails)
	return modelfunctions.setfiledetails(filedetails, {configfile})
end

function get_join()
	local connect = {}
	connect.domain = cfe({ label="Domain" })
	--connect.style = cfe({ type="select", value="Active Directory", label="Domain Controller type", option={"Active Directory", "NT4-style"} })
	connect.login = cfe({ label="Domain Controller login" })
	connect.password = cfe({ label="Domain Controller password" })

	configcontent = configcontent or fs.read_file(configfile) or ""
	config = config or format.parse_ini_file(configcontent)
	if config and config.global and config.global.workgroup then
		connect.domain.value = config.global.workgroup
	end

	local f = io.popen(path.."net rpc testjoin 2<&1")
	local status = f:read("*a")
	f:close()

	return cfe({ type="group", value=connect, label="Join a Domain", descr=status })
end

function set_join(connect)
	configcontent = configcontent or fs.read_file(configfile) or ""

	configcontent = format.update_ini_file(configcontent, "global", "security", "domain")
	configcontent = format.update_ini_file(configcontent, "global", "workgroup", connect.value.domain.value)
	configcontent = format.update_ini_file(configcontent, "global", "encrypt passwords", "yes")
	configcontent = format.update_ini_file(configcontent, "global", "password server", "*")
	fs.write_file(configfile, configcontent)
	configcontent = nil
	config = nil
	
	local cmd = path
	--if connect.value.style.value == "Active Directory" then
	--	cmd = cmd .. "net ads join"
	--else
		cmd = cmd .. "net rpc join"
	--end
	cmd = cmd .. " -U"..connect.value.login.value.."%"..connect.value.password.value.." 2>&1"
	local f = io.popen(cmd)
	connect.descr = f:read("*a")
	f:close()

	-- the conf.d file doesn't automatically include winbindd
	local content = fs.read_file(confdfile)
	local list = format.parse_ini_file(content, "", "daemon_list")
	if not string.find(list, "winbind") then
		content = format.update_ini_file(content, "", "daemon_list", string.gsub(list, '"$', ' winbind"'))
		fs.write_file(confdfile, content)
	end

	return connect
end