module(..., package.seeall) -- Load libraries require("modelfunctions") require("fs") 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 = 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 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