summaryrefslogtreecommitdiffstats
path: root/samba-model.lua
blob: 086b81808166bc8c1d5ea063c70d524f9954d9a9 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
module(..., package.seeall)

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

-- Set variables
local configfile = "/etc/samba/smb.conf"
local confdfile = "/etc/conf.d/samba"
local filelist = {configfile, confdfile}
local processname = "samba"
local packagename = "samba"

local configcontent
local config
local specialsection = {global=1, homes=2, printers=3}

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

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

local function config_to_bool(value)
	if value and (string.lower(value) == "yes" or string.lower(value) == "true" or value == "1") then
		return true
	else
		return false
	end
end

local function validate_share(share)
	local success = true

	if share.value.name.value == "" then
		share.value.name.errtxt = "Cannot be blank"
		success = false
	end
	--share.value.browseable.value
	--share.value.comment.value
	success = modelfunctions.validateselect(share.value.guest) and success
	--share.value.invalid_users.value
	--share.value.path.value
	--share.value.printable.value
	--share.value.read_list.value
	--share.value.writeable.value
	--share.value.username.value
	--share.value.valid_users.value
	--share.value.valid.value
	--share.value.write_list.value
	--share.value.other.value

	return success, share
end

local function write_share(share)
	local lines = {}
	if not share.value.browseable.value then
		lines[#lines+1] = "browseable = no"
	end
	if share.value.comment.value ~= "" then
		lines[#lines+1] = "comment = "..share.value.comment.value
	end
	if share.value.guest.value == "OK" then
		lines[#lines+1] = "guest ok = yes"
	elseif share.value.guest.value == "Only" then
		lines[#lines+1] = "guest ok = yes"
		lines[#lines+1] = "guest only = yes"
	end
	--share.value.invalid_users.value
	if share.value.path.value ~= "" then
		lines[#lines+1] = "path = "..share.value.path.value
	end
	if share.value.printable.value then
		lines[#lines+1] = "printable = yes"
	end
	--share.value.read_list.value
	if share.value.writeable.value then
		lines[#lines+1] = "writeable = yes"
	else
		lines[#lines+1] = "read only = yes"
	end
	--share.value.username.value
	--share.value.valid_users.value
	if not share.value.valid.value then
		lines[#lines+1] = "-valid = no"
	end
	--share.value.write_list.value
	if share.value.other.value ~= "" then
		lines[#lines+1] = share.value.other.value
	end

	configcontent = configcontent or fs.read_file(configfile) or ""
	configcontent = format.set_ini_section(configcontent, share.value.name.value, table.concat(lines, "\n"))
	fs.write_file(configfile, configcontent)
	configcontent = nil
	config = nil
end

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

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

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

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

function listconfigfiles()
	local listed_files = {}
        for i,name in ipairs(filelist) do
		local filedetails = fs.stat(name) or {}
		table.insert ( listed_files , {filename=name, mtime=filedetails.mtime or "---", filesize=filedetails.size or "0"} )
	end
	table.sort(listed_files, function (a,b) return (a.filename < b.filename) end )
	
	return cfe({ type="list", value=listed_files, label="Samba File List" })
end

function getconfigfile(filename)
	return modelfunctions.getfiledetails(filename, filelist)
end

function setconfigfile(self, filedetails)
	return modelfunctions.setfiledetails(self, filedetails, filelist)
end

function get_join()
	local connect = {}
	connect.login = cfe({ label="Domain Controller login", seq=1 })
	connect.password = cfe({ type="password", label="Domain Controller password", seq=2 })

	configcontent = configcontent or fs.read_file(configfile) or ""
	config = config or format.parse_ini_file(configcontent) or {}

	local status = {}
	local errtxt = "Unable to determine join type"
	local join
	if config and config.global and config.global.security then
		if config.global.security:lower() == "ads" then
			status[#status+1] = "Testing AD join"
			errtxt = nil
			join = "ads"
		elseif config.global.security:lower() == "domain" then
			status[#status+1] = "Testing NT4-style join"
			errtxt = nil
			join = "rpc"
		end
	end
	if not errtxt then
		local cmdresult
		cmdresult, errtxt = modelfunctions.run_executable({"net", join, "testjoin"}, true)
		status[#status+1] = cmdresult
	end

	return cfe({ type="group", value=connect, label="Join a Domain", descr=table.concat(status, "\n"), errtxt=errtxt })
end

function set_join(self, connect)
	configcontent = configcontent or fs.read_file(configfile) or ""
	config = config or format.parse_ini_file(configcontent) or {}

	connect.errtxt = "Unable to determine join type"
	local join
	if config and config.global and config.global.security then
		if config.global.security == "ads" then
			connect.errtxt = nil
			join = "ads"
		elseif config.global.security == "domain" then
			connect.errtxt = nil
			join = "rpc"
		end
	end
	if not errtxt then
		connect.descr, connect.errtxt = modelfunctions.run_executable({"net", join, "join", "-U"..connect.value.login.value.."%"..connect.value.password.value}, true)
	end

	return connect
end

function list_shares()
	local shares = {}
	configcontent = configcontent or fs.read_file(configfile) or ""
	config = config or format.parse_ini_file(configcontent) or {}

	for name,section in pairs(config or {}) do
		if not specialsection[name] then
			local temp = {name =name}
			temp.path = section.path or section.directory or ""
			temp.comment = section.comment or ""
			shares[#shares+1] = temp
		end
	end

	return cfe({ type="structure", value=shares, label="Shares" })
end

function read_share(self, clientdata)
	local name = clientdata.name
	local share = {}
	configcontent = configcontent or fs.read_file(configfile) or ""
	config = config or format.parse_ini_file(configcontent) or {}
	name = name or ""
	local sharecfg = config[name] or {}

	share.name = cfe({ value=name, label="Share Name", seq=0 })
	share.browseable = cfe({ type="boolean", value=true, label="Browseable", descr="Controls whether this share is seen in the list of available shares", seq=4 })
		if sharecfg.browseable then share.browseable.value = config_to_bool(sharecfg.browseable)
		elseif sharecfg.browsable then share.browseable.value = config_to_bool(sharecfg.browsable) end
	share.comment = cfe({ value=sharecfg.comment or "", label="Comment", seq=2 })
	share.guest = cfe({ type="select", value="No", label="Guest Access", option={"No", "OK", "Only"}, seq=5 })
		if config_to_bool(sharecfg.public) or config_to_bool(sharecfg["guest ok"]) then
			if config_to_bool(sharecfg["only guest"]) or config_to_bool(sharecfg["guest only"]) then
				share.guest.value = "Only"
			else
				share.guest.value = "OK"
			end
		end
	--share.invalid_users = 
	share.path = cfe({ value=sharecfg.path or sharecfg.directory or "", label="Path", seq=3 })
	share.printable = cfe({ type="boolean", value=config_to_bool(sharecfg.printable) or config_to_bool(sharecfg["print ok"]) or false, label="Printable", seq=6 })
	--share.read_list = 
	share.writeable = cfe({ type="boolean", value=config_to_bool(sharecfg.writeable) or config_to_bool(sharecfg.writable) or (sharecfg["read only"] and not config_to_bool(sharecfg["read only"])) or false, label="Writable", seq=7 })
	--share.username = (same as user and users)
	--share.valid_users =
	share.valid = cfe({ type="boolean", value=not sharecfg["-valid"] or config_to_bool(sharecfg["-valid"]), label="Enabled", seq=1 })
	--share.write_list = 
	share.other = cfe({ type="longtext", value={}, label="Other parameters", seq=8 })
	local reverseparams = {browseable=1, browsable=2, comment=3, public=4, ["guest ok"]=5, ["only guest"]=6, ["guest only"]=7, path=8, directory=9, printable=10, ["print ok"]=11, writeable=12, writable=13, ["read only"]=14, ["-valid"]=15 }
	for name,value in pairs(sharecfg) do
		if not reverseparams[name] then
			table.insert(share.other.value, name.." = "..value)
		end
	end
	share.other.value = table.concat(share.other.value, "\n") or ""

	return cfe({ type="group", value=share, label="Samba Share" })
end

function update_share(self, share)
	local success, share = validate_share(share)

	configcontent = configcontent or fs.read_file(configfile) or ""
	config = config or format.parse_ini_file(configcontent) or {}
	if not config[share.value.name.value] then
		share.value.name.errtxt = "Share not found"
		success = false
	end

	if success then
		write_share(share)
	else
		share.errtxt = "Failed to update share"
	end

	return share
end

function create_share(self, share)
	local success, share = validate_share(share)

	configcontent = configcontent or fs.read_file(configfile) or ""
	config = config or format.parse_ini_file(configcontent) or {}
	if config[share.value.name.value] then
		share.value.name.errtxt = "Share already exists"
		success = false
	end

	if success then
		write_share(share)
	else
		share.errtxt = "Failed to update share"
	end

	return share
end

function get_delete_share(self, clientdata)
	local retval = {}
	retval.name = cfe({ value=clientdata.name or "", label="Share Name" })
	return cfe({ type="group", value=retval, label="Delete Share" })
end

function delete_share(self, delshare)
	local name = delshare.value.name.value
	delshare.ertxt = "Failed to delete share"

	if specialsection[name] then
		delshare.value.name.errtxt = "Share not found"
	else
		configcontent = configcontent or fs.read_file(configfile) or ""
		config = config or format.parse_ini_file(configcontent) or {}
		if config[name] then
			configcontent = format.set_ini_section(configcontent, name, "")
			configcontent = string.gsub(configcontent, "\n%s*%[%s*"..format.escapemagiccharacters(name).."%s*%][^\n]*", "")
			fs.write_file(configfile, configcontent)
			configcontent = nil
			config = nil
			delshare.errtxt = nil
		else
			delshare.value.name.errtxt = "Share not found"
		end
	end

	return delshare
end