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
|
local mymodule = {}
-- Load libraries
posix = require("posix")
modelfunctions = require("modelfunctions")
dbmodelfunctions = require("dbmodelfunctions")
fs = require("acf.fs")
format = require("acf.format")
validator = require("acf.validator")
db = require("acf.db")
-- Set variables
local processname = "kamailio"
local packagename = "kamailio"
local baseurl = "/etc/kamailio"
local kamctlrc_file = "/etc/kamailio/kamctlrc"
local dbkam
-- ################################################################################
-- DATABASE FUNCTIONS
local function assert (v, m)
if not v then
m = m or "Assertion failed!"
error(m, 0)
end
return v, m
end
local databasecreate = function()
if not dbkam then
local engine
-- parse the kamctlrc file
local config = format.parse_ini_file(fs.read_file(kamctlrc_file), "") or {}
if not config.DBENGINE then
error("Database engine not specified, please setup one in the config script "..kamctlrc_file)
end
-- create environment object
if config.DBENGINE == "MYSQL" or config.DBENGINE == "mysql" or config.DBENGINE == "MySQL" then
error("MYSQL database not supported")
elseif config.DBENGINE == "PGSQL" or config.DBENGINE == "pgsql" or config.DBENGINE == "postgres" or config.DBENGINE == "postgresql" or config.DBENGINE == "POSTGRESQL" then
engine = db.engine.postgresql
elseif config.DBENGINE == "ORACLE" or config.DBENGINE == "oracle" or config.DBENGINE == "Oracle" then
error("ORACLE database not supported")
elseif config.DBENGINE == "DBTEXT" or config.DBENGINE == "dbtext" or config.DBENGINE == "textdb" then
error("DBTEXT database not supported")
elseif config.DBENGINE == "DB_BERKELEY" or config.DBENGINE == "db_berkeley" or config.DBENGINE == "BERKELEY" or config.DBENGINE == "berkeley" then
error("BERKELEY database not supported")
else
error("Unknown database engine "..config.DBENGINE)
end
-- connect to data source
dbkam = db.create(engine, config.DBNAME or "openser", config.DBRWUSER or "openser", config.DBRWPW or "openserrw", config.DBHOST, config.DBPORT)
end
return dbkam
end
local databaseconnect = function()
if not dbkam then
databasecreate()
end
return dbkam.databaseconnect()
end
local databasedisconnect = function()
local result = dbkam.databasedisconnect()
dbkam = nil
return result
end
local runsqlcommand = function(sql)
mymodule.logevent(sql)
return dbkam.runsqlcommand(sql)
end
local getselectresponse = function(sql)
mymodule.logevent(sql)
return dbkam.getselectresponse(sql)
end
-- ################################################################################
-- LOCAL FUNCTIONS
local is_valid_filename = function(filename)
local dirname = posix.dirname(filename)
return validator.is_valid_filename(filename) and string.match(dirname, baseurl) and not string.match(dirname, "%.%.")
end
local function validate_user(user)
local success = true
if user.value.username.value == "" then
user.value.username.errtxt = "Cannot be empty"
success = false
end
if user.value.password.value == "" then
user.value.password.errtxt = "Cannot be empty"
success = false
end
if user.value.password.value ~= user.value.password_confirm.value then
user.value.password_confirm.errtxt = "Must match password"
success = false
end
return success, user
end
-- ################################################################################
-- PUBLIC FUNCTIONS
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.getstatus()
return modelfunctions.getstatus(processname, packagename, "Kamailio Status")
end
function mymodule.get_filedetails(self, clientdata)
return modelfunctions.getfiledetails(clientdata.filename, is_valid_filename)
end
function mymodule.update_filedetails(self, filedetails)
return modelfunctions.setfiledetails(self, filedetails, is_valid_filename)
end
function mymodule.list_files()
local retval = {}
for file in fs.find(null, baseurl) do
local details = fs.stat(file)
if details.type == "regular" then
details.filename = file
table.insert(retval, details)
end
end
table.sort(retval, function(a,b) return a.filename < b.filename end)
return cfe({ type="structure", value=retval, label="List of Kamailio files" })
end
local function parse_db_show(table)
-- These settings work for Postgres and DBTEXT database
local delimiter = "\'?%s*[,|]%s*\'?"
local results = {}
local errtxt
local f
local res, err = pcall(function()
local connected = databaseconnect()
f = modelfunctions.run_executable({"kamctl", "db", "show", dbkam.escape(table)})
if connected then databasedisconnect() end
end)
if not res and err then
errtxt = err
else
for line in string.gmatch(f, "[^\n]+") do
if #results == 0 and string.match(line, "^ERROR:") then
errtxt = line
results = nil
break
end
if string.match(line, "^[+-]+$") then
results = {}
else
local words = format.string_to_table(line, delimiter)
if words and #words > 0 then
results[#results+1] = words
end
end
end
end
return results, errtxt
end
function mymodule.list_users()
-- Database format: id | username | domain | password | email_address | ha1 | ha1b | rpid
local results = {}
local r, errtxt
r, errtxt = parse_db_show("subscriber")
for i,words in ipairs(r or {}) do
if #words > 1 then
local temp = {username = words[2],
--domain = words[3],
password = words[4],
--email_address = words[5]
}
results[#results+1] = temp
end
end
table.sort(results, function(a,b) return a.username < b.username end)
return cfe({type="list", value=results, label="Kamailio Users", errtxt=errtxt})
end
function mymodule.get_new_user()
local user = {}
user.username = cfe({label="User Name", seq=1})
user.password = cfe({type="password", label="Password", seq=2})
user.password_confirm = cfe({type="password", label="Password (confirm)", seq=3})
--user.email_address = cfe({label="E-mail Address", seq=4})
return cfe({type="group", value=user, label="Kamailio User"})
end
function mymodule.create_new_user(self, user)
local success = validate_user(user)
if success then
local res, err = pcall(function()
local connected = databaseconnect()
user.descr, user.errtxt = modelfunctions.run_executable({"kamctl", "add", dbkam.escape(user.value.username.value), dbkam.escape(user.value.password.value)})
if connected then databasedisconnect() end
end)
if not res and err then
user.errtxt = err
end
else
user.errtxt = "Failed to create new user"
end
return user
end
function mymodule.get_delete_user(self, clientdata)
local user = {}
user.username = cfe({label="User Name"})
return cfe({type="group", value=user, label="Delete User"})
end
function mymodule.delete_user(self, user)
if user.value.username.value == "" then
user.value.username.errtxt = "Invalid username"
user.errtxt = "Failed to delete user"
else
local res, err = pcall(function()
local connected = databaseconnect()
user.descr, user.errtxt = modelfunctions.run_executable({"kamctl", "rm", dbkam.escape(user.value.username.value)})
if connected then databasedisconnect() end
end)
if not res and err then
user.errtxt = err
end
end
return user
end
function mymodule.get_user(self, clientdata)
local user = mymodule.get_new_user()
user.value.username.value = clientdata.username or ""
user.value.username.errtxt = "Invalid user"
user.value.username.readonly = true
local users = mymodule.list_users()
for i,u in ipairs(users.value) do
if u.username == clientdata.username then
user.value.username.errtxt = nil
break
end
end
return user
end
function mymodule.update_user(self, user)
local success = validate_user(user)
if success then
local res, err = pcall(function()
local connected = databaseconnect()
user.descr, user.errtxt = modelfunctions.run_executable({"kamctl", "passwd", dbkam.escape(user.value.username.value), dbkam.escape(user.value.password.value)})
if connected then databasedisconnect() end
end)
if not res and err then
user.errtxt = err
end
else
user.errtxt = "Failed to update user"
end
return user
end
function mymodule.get_create_database(self, clientdata)
return cfe({ type="group", value={}, label="Create Database" })
end
function mymodule.create_database(self, create_db)
create_db.descr, create_db.errtxt = modelfunctions.run_executable({"kamdbctl", "create"}, true, 'y\ny\n')
return create_db
end
function mymodule.get_search_database(self, clientdata)
local errtxt
retval = {}
retval.id = cfe({type="select", value=clientdata.id or "", label="Table.Column", option={}, seq=1})
retval.comparison = cfe({type="select", value=clientdata.comparison or "=", label="Comparison", option={"=", "!=", "~", "!~", "~*", "!*~"}, seq=2})
retval.value = cfe({label="Value", value=clientdata.value or "", descr="Value or SQL regular expression", seq=3})
local res, err = pcall(function()
local connected = databaseconnect()
local tables = dbkam.listtables() or {}
for i,t in ipairs(tables) do
local columns = dbkam.listcolumns(t) or {}
for i,c in ipairs(columns) do
retval.id.option[#retval.id.option + 1] = t.."."..c
end
end
if connected then databasedisconnect() end
end)
if not res and err then
errtxt = err
end
return cfe({type="group", value=retval, label="Database Search", errtxt=errtxt})
end
function mymodule.search_database(self, search)
if modelfunctions.validateselect(search.value.id) and modelfunctions.validateselect(search.value.comparison) then
local res, err = pcall(function()
local connected = databaseconnect()
-- Get the rows from the DB
search.value.result = cfe({type="structure", value={}, label="List of Rows", seq=4 })
local table, column = string.match(search.value.id.value, "^([^.]*)%.(.*)")
if table then
local sql = "SELECT * FROM "..dbkam.escape(table).." WHERE "..dbkam.escape(column)..dbkam.escape(search.value.comparison.value).."'"..dbkam.escape(search.value.value.value).."'"
search.value.result.value = getselectresponse(sql)
end
if connected then databasedisconnect() end
end)
if not res and err then
search.errtxt = err
end
else
search.errtxt = "Failed to search database"
end
return search
end
for n,f in pairs(dbmodelfunctions) do
mymodule[n] = function(...)
return f(databasecreate, ...)
end
end
return mymodule
|