summaryrefslogtreecommitdiffstats
path: root/ppp-model.lua
blob: 026053367202b29f16f61678febf27d0800d3d91 (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
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
require("processinfo")
require("fs")
require("validator")

-- Set variables
local processname = "pppd"
local packagename = "ppp"
local papfile = "/etc/ppp/pap-secrets"
local chapfile = "/etc/ppp/chap-secrets"
local peerspath = "/etc/ppp/peers/"

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

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

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

function getstatus()
	local status = {}

	local value, errtxt = processinfo.package_version(packagename)
	status.version = cfe({
		label="Program version",
		value=value,
		errtxt=errtxt,
		name=packagename
	})

	status.status = modelfunctions.getenabled(processname)

	return cfe({ type="group", value=status, label="PPP Status" })
end

function read_papfiledetails()
	return modelfunctions.getfiledetails(papfile)
end

function update_papfiledetails(filedetails)
	return modelfunctions.setfiledetails(filedetails, {papfile})
end

function read_chapfiledetails()
	return modelfunctions.getfiledetails(chapfile)
end

function update_chapfiledetails(filedetails)
	return modelfunctions.setfiledetails(filedetails, {chapfile})
end

function list_peers()
	return cfe({ type="list", value=fs.find_files_as_array(nil, peerspath), label="Peers" })
end

function get_newpeer()
	local newpeer = {}
	newpeer.name = cfe({ label="Name" })
	return cfe({ type="group", value=newpeer, label="New Peer" })
end

function create_peer(newpeer)
	newpeer.errtxt = "Failed to create peer"
	local path = newpeer.value.name.value
	if not string.find(path, "/") then
		path = peerspath .. path
	end

	if validator.is_valid_filename(path, peerspath) then
		if (posix.stat(path)) then
			newpeer.value.name.errtxt = "Peer already exists"
		else
			fs.create_file(path)
			newpeer.errtxt = nil
		end
	else
		newpeer.value.name.errtxt = "Invalid name"
	end

	return newpeer
end

function delete_peer(name)
	local retval = cfe({ value="Failed to delete peer", label="Delete Peer Result", errtxt="Peer not found" })

	for file in fs.find(nil, peerspath) do
		if name == file then
			os.remove(file)
			retval.value = "Peer Deleted"
			retval.errtxt = nil
			break
		end
	end

	return retval
end

function read_peerfile(name)
	return modelfunctions.getfiledetails(name, fs.find_files_as_array(nil, peerspath))
end

function update_peerfile(filedetails)
	return modelfunctions.setfiledetails(filedetails, fs.find_files_as_array(nil, peerspath))
end