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
|
module(..., package.seeall)
-- Load libraries
require("posix")
require("modelfunctions")
require("fs")
require("format")
require("validator")
-- Set variables
local processname = "kamailio"
local packagename = "kamailio"
local baseurl = "/etc/kamailio"
local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "
-- ################################################################################
-- 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 startstop_service(action)
return modelfunctions.startstop_service(processname, action)
end
function getstatus()
return modelfunctions.getstatus(processname, packagename, "Kamailio Status")
end
function get_filedetails(filename)
return modelfunctions.getfiledetails(filename, is_valid_filename)
end
function update_filedetails(filedetails)
return modelfunctions.setfiledetails(filedetails, is_valid_filename)
end
function 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
function list_users()
-- Database format: id | username | domain | password | email_address | ha1 | ha1b | rpid
local cmd = path .. "kamctl db show subscriber"
local f = io.popen(cmd)
-- These settings work for Postgres database
local skiplines = 2
local delimiter = "%s*|%s*"
local dbengine = format.parse_ini_file(fs.read_file("/etc/kamailio/kamctlrc") or "", "", "DBENGINE")
if dbengine == "DBTEXT" then
skiplines = 0
delimiter = "\'?%s*,%s*\'?"
end
local results = {}
for line in f:lines() do
if skiplines > 0 then
skiplines = skiplines-1
else
local words = format.string_to_table(line, delimiter)
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
end
f:close()
table.sort(results, function(a,b) return a.username < b.username end)
return cfe({type="list", value=results, label="Kamailio Users"})
end
function get_new_user()
local user = {}
user.username = cfe({label="User Name"})
user.password = cfe({label="Password"})
user.password_confirm = cfe({label="Password (confirm)"})
--user.email_address = cfe({label="E-mail Address"})
return cfe({type="group", value=user, label="Kamailio User"})
end
function create_new_user(user)
local success = validate_user(user)
if success then
local cmd = path .. "kamctl add "..format.escapespecialcharacters(user.value.username.value).." "..format.escapespecialcharacters(user.value.password.value)
--if user.value.email_address.value ~= "" then
-- cmd = cmd.." "..format.escapespecialcharacters(user.value.email_address.value)
--end
local f = io.popen(cmd)
user.descr = f:read("*a")
f:close()
else
user.errtxt = "Failed to create new user"
end
return user
end
function delete_user(username)
local cmd = path .. "kamctl rm "..format.escapespecialcharacters(username)
local f = io.popen(cmd)
local result = f:read("*a")
f:close()
return cfe({value=result, label="Delete User Result"})
end
function get_user(username)
local user = get_new_user()
user.value.username.value = username
user.value.username.errtxt = "Invalid user"
local users = list_users()
for i,u in ipairs(users.value) do
if u.username == username then
user.value.username.errtxt = nil
break
end
end
return user
end
function update_user(user)
local success = validate_user(user)
if success then
local cmd = path .. "kamctl passwd "..format.escapespecialcharacters(user.value.username.value).." "..format.escapespecialcharacters(user.value.password.value)
local f = io.popen(cmd)
user.descr = f:read("*a")
f:close()
else
user.errtxt = "Failed to update user"
end
return user
end
|