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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
module(..., package.seeall)
-- Load libraries
require("modelfunctions")
require("validator")
require("fs")
require("posix")
require("format")
-- Set variables
local configfile = "/etc/tcpproxy.conf"
local processname = "tcpproxy"
local packagename = "tcpproxy"
local smtppackagename = "rxmtp"
local smtpdirectory = "/etc/rxmtp/"
local keywords = { "standalone", "port", "interface", "rotate", "server", "uid", "gid", "user", "exec", "acp", "logname", "setenv", "timeout", "writefile" }
local config
-- ################################################################################
-- LOCAL FUNCTIONS
local function parseconfigfile(file)
file = file or ""
local retval = {}
for line in string.gmatch(file, "([^\n]+)\n?") do
line = string.gsub(line, "#.*$", "")
if line and line ~= "" then
table.insert(retval, {})
for word in string.gmatch(line, "%S+") do
table.insert(retval[#retval], word)
end
end
end
return retval
end
local function getsmtpconfig(config)
-- parse the TCP proxy config file for smtp entries
local retval = {}
local port25 = false
local currentint
for i,entry in ipairs(config) do
if entry[1] == "port" then
if entry[2] == "25" then
port25 = true
currentint = nil
else
port25 = false
currentint = nil
end
elseif port25 then
if entry[1] == "interface" then
currentint = entry[2]
elseif entry[1] == "exec" and string.find(entry[2], "rxmtp$") then
if currentint then
table.insert(retval, {ipaddr=currentint, cmd=table.concat(entry, " ", 2)})
else
-- bad config - exec command without interface
end
end
end
end
return retval
end
local function setsmtpcmd(ipaddr, cmd)
if cmd then cmd = "exec "..cmd end
local file = fs.read_file(configfile) or ""
local inport25 = false
local ininterface = false
local done = false
local lines = {}
for line in string.gmatch(file, "([^\n]*)\n?") do
if not done then
if ininterface and string.find(line, "^%s*exec%s") then
-- We found the line, replace it
line = cmd
done = true
elseif ininterface and string.find(line, "^%s*interface%s") then
-- We're leaving the interface and we haven't written line yet
if cmd then
if string.find(lines[#lines], "^%s*$") then lines[#lines] = nil end
lines[#lines + 1] = cmd
lines[#lines + 1] = ""
end
done = true
elseif inport25 and string.find(line, "^%s*interface%s+"..ipaddr) then
ininterface = true
elseif inport25 and string.find(line, "^%s*port%s") then
-- we're leaving port 25 and we haven't written line yet
if cmd then
lines[#lines + 1] = "interface "..ipaddr
lines[#lines + 1] = cmd
lines[#lines + 1] = ""
end
done = true
elseif string.find(line, "^%s*port%s+25") then
inport25 = true
end
end
lines[#lines + 1] = line
end
if not done and cmd then
if not inport25 then lines[#lines + 1] = "port 25" end
if not ininterface then lines[#lines + 1] = "interface "..ipaddr end
lines[#lines + 1] = cmd
end
fs.write_file(configfile, string.gsub(table.concat(lines, "\n"), "\n+$", ""))
end
local function parsesmtpcmd(exec)
-- parse the command line into a table
exec = exec or ""
local options = format.opts_to_table(exec) or {}
local server = string.match(exec, "rxmtp.*%s(%S+)%s*$")
if server and not string.match(server, "^%-") then
local test = string.match(exec, "rxmtp.*%s(%S+)%s+%S+%s*$")
if test then
local optionswithvalues = {["-l"]=1, ["-o"]=1, ["-r"]=1, ['-s']=1, ['-t']=1, ['-x']=1, ['-X']=1}
if not optionswithvalues[test] then
options[""] = server
end
else
options[""] = server
end
end
return options
end
local function validatesmtpentry(entry)
local success = true
if string.find(entry.value.ipaddr.value, "[^%w.]") then
entry.value.ipaddr.errtxt = "Invalid interface / address"
success = false
end
if string.find(entry.value.server.value, "[^%w.-]") and not fs.is_file(entry.value.server.value) then
entry.value.server.errtxt = "Cannot find server binary"
success = false
end
if string.find(entry.value.domain.value, "[^%w.-]") then
entry.value.domain.errtxt = "Invalid domain"
success = false
end
if entry.value.optionalserver.value ~= "" then
if string.find(string.match(entry.value.optionalserver.value, "^[^:]+"), "[^%w.-]") then
entry.value.optionalserver.errtxt = "Invalid server"
success = false
elseif string.match(entry.value.optionalserver.value, ":(.*)") and not validator.is_port(string.match(entry.value.optionalserver.value, ":(.*)")) then
entry.value.optionalserver.errtxt = "Invalid port"
success = false
elseif entry.value.domain.value == "" then
entry.value.optionalserver.errtxt = "Cannot define server without Custom Domain"
success = false
end
elseif entry.value.domain.value ~= "" then
entry.value.optionalserver.errtxt = "Must define server if Custom Domain defined"
success = false
end
success = modelfunctions.validateselect(entry.value.optionalrewritelist) and success
if entry.value.domain.value == "" and entry.value.optionalrewritelist.value ~= "" then
entry.value.optionalrewritelist.errtxt = "Cannot define rewrite list without Custom Domain"
success = false
end
success = modelfunctions.validateselect(entry.value.senderlistfile) and success
success = modelfunctions.validateselect(entry.value.rcptlistfile) and success
return success, entry
end
local function getsmtpcmd(ipaddr)
config = config or parseconfigfile(fs.read_file(configfile) or "")
local smtpconfig = getsmtpconfig(config)
local exec = ""
for i,entry in ipairs(smtpconfig) do
if entry.ipaddr == ipaddr then
exec = entry.cmd
break
end
end
return exec
end
-- ################################################################################
-- PUBLIC FUNCTIONS
function startstop_service(action)
return modelfunctions.startstop_service(processname, action)
end
function getstatus()
return modelfunctions.getstatus(processname, packagename, "TCP Proxy Status")
end
function getconfigfile()
return modelfunctions.getfiledetails(configfile)
end
function setconfigfile(filedetails)
return modelfunctions.setfiledetails(filedetails, {configfile})
end
function getsmtpstatus()
local value, errtxt = processinfo.package_version(smtppackagename)
local version = cfe({ value=value, label="Program version", errtxt=errtxt })
return cfe({ type="group", value={version=version, entries=entries}, label="SMTP Proxy Status" })
end
function listsmtpentries(self)
local entries = cfe({ type="structure", value={}, label="SMTP Command Entries" })
if self then
local interfacescontroller = self:new("alpine-baselayout/interfaces")
local interfaces = interfacescontroller.model:get_addresses()
interfacescontroller:destroy()
-- add in entries for interfaces (w/o ipaddr)
local interface
for i,entry in ipairs(interfaces.value) do
if interface ~= entry.interface then
interface = entry.interface
table.insert(entries.value, {interface=interface})
end
table.insert(entries.value, entry)
end
end
local reverseaddress = {}
for i,int in ipairs(entries.value) do reverseaddress[int.ipaddr or int.interface] = i end
config = config or parseconfigfile(fs.read_file(configfile) or "")
local smtpconfig = getsmtpconfig(config)
for i,int in ipairs(smtpconfig) do
local pos = reverseaddress[int.ipaddr]
if pos then
entries.value[pos].cmd = int.cmd
else
if not validator.is_ipv4(int.ipaddr) then
int.interface = int.ipaddr
int.ipaddr = nil
end
table.insert(entries.value, int)
end
end
return entries
end
function readsmtpentry(ipaddr)
local exec = getsmtpcmd(ipaddr)
local listfiles = listsmtpfiles()
table.insert(listfiles.value, 1, "")
local ipaddrcfe = cfe({ value=ipaddr, label="Interface / IP Address" })
local server = cfe({ label="SMTP Server", descr="Domain name / address of different machine or local binary" })
local addressonly = cfe({ type="boolean", value=false, label="Address Only", descr="Removes everything in the from field except address" })
local domain = cfe({ label="Custom Domain" })
local optionalserver = cfe({ label="Custom Domain Server", descr="Use this SMTP server[:port] for e-mails to Custom Domain" })
local optionalrewritelist = cfe({ type="select", label="Custom Domain Rewrite List", descr="File with a list for sender rewriting for Custom Domain", option=listfiles.value })
local senderlistfile = cfe({ type="select", label="Sender List File", descr="File with a list for sender rewriting (and valid senders)", option=listfiles.value })
local rcptlistfile = cfe({ type="select", label="Recipient List File", descr="File with a list for recipient rewriting (and valid recipients)", option=listfiles.value })
if exec and exec ~= "" then
local options = parsesmtpcmd(exec)
server.value = options[""] or server.value
addressonly.value = (options["-b"] ~= nil)
if options["-o"] then
domain.value = string.match(options["-o"], "^[^=]*") or ""
optionalserver.value = string.match(options["-o"], "=([^=]*)") or ""
optionalrewritelist.value = string.match(options["-o"], "=.*=(.*)") or ""
end
senderlistfile.value = options["-x"] or senderlistfile.value
rcptlistfile.value = options["-X"] or rcptlistfile.value
end
return cfe({ type="group", value={ipaddr=ipaddrcfe, server=server, addressonly=addressonly, domain=domain, optionalserver=optionalserver, optionalrewritelist=optionalrewritelist, senderlistfile=senderlistfile, rcptlistfile=rcptlistfile}, label="SMTP Proxy Entry" })
end
function updatesmtpentry(entry)
local success, entry = validatesmtpentry(entry)
if success then
local options = parsesmtpcmd(getsmtpcmd(entry.value.ipaddr.value))
options[""] = entry.value.server.value
if entry.value.addressonly.value then options["-b"] = "" else options["-b"] = nil end
if entry.value.domain.value == "" then
options["-o"] = nil
else
options["-o"] = entry.value.domain.value .. "=" .. entry.value.optionalserver.value
end
if entry.value.optionalrewritelist.value ~= "" then
options["-o"] = options["-o"] .. "=" .. entry.value.optionalrewritelist.value
end
if entry.value.senderlistfile.value ~= "" then
options["-x"] = entry.value.senderlistfile.value
else
options["-x"] = nil
end
if entry.value.rcptlistfile.value ~= "" then
options["-X"] = entry.value.rcptlistfile.value
else
options["-X"] = nil
end
local exec = {"/usr/sbin/rxmtp"}
for option,value in pairs(options) do
if option ~= "" then
if value ~= "" then
exec[#exec + 1] = option .. " " .. value
else
exec[#exec + 1] = option
end
end
end
if options[""] ~= "" then
exec[#exec + 1] = options[""]
end
setsmtpcmd(entry.value.ipaddr.value, table.concat(exec, " "))
else
entry.errtxt = "Failed to set SMTP Proxy Entry"
end
return entry
end
function delsmtpentry(ipaddr)
setsmtpcmd(ipaddr, nil)
return cfe({ value="Deleted SMTP Proxy Entry", label="Delete SMTP Entry result" })
end
function listsmtpfiles()
local retval = cfe({ type="list", value={}, label="SMTP Proxy Files" })
if not fs.is_dir(smtpdirectory) then posix.mkdir(smtpdirectory) end
for file in posix.files(smtpdirectory) do
if fs.is_file(smtpdirectory .. file) then
table.insert(retval.value, smtpdirectory .. file)
end
end
return retval
end
function getnewsmtpfile()
local filename = cfe({ label="File Name", descr="Must be in "..smtpdirectory })
return cfe({ type="group", value={filename=filename}, label="SMTP Proxy File" })
end
function createsmtpfile(filedetails)
local success = true
if not validator.is_valid_filename(filedetails.value.filename.value, smtpdirectory) then
success = false
filedetails.value.filename.errtxt = "Invalid filename"
else
if not fs.is_dir(smtpdirectory) then posix.mkdir(smtpdirectory) end
if posix.stat(filedetails.value.filename.value) then
success = false
filedetails.value.filename.errtxt = "Filename already exists"
end
end
if success then
fs.create_file(filedetails.value.filename.value)
else
filedetails.errtxt = "Failed to Create File"
end
return filedetails
end
function readsmtpfile(filename)
return modelfunctions.getfiledetails(filename, function(filename) return validator.is_valid_filename(filename, smtpdirectory) end)
end
function updatesmtpfile(filedetails)
return modelfunctions.setfiledetails(filedetails, function(filename) return validator.is_valid_filename(filename, smtpdirectory) end)
end
function delsmtpfile(filename)
local retval = cfe({ value="Deleted SMTP Proxy File", label="Delete SMTP File result" })
if validator.is_valid_filename(filename, smtpdirectory) and fs.is_file(filename) then
os.remove(filename)
else
retval.value = "Failed to delete SMTP Proxy File - invalid filename"
end
return retval
end
|