summaryrefslogtreecommitdiffstats
path: root/ppp-model.lua
blob: ff7a8cc471cc7f4c3728ec3aeaaf04761046b5b0 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
processinfo = require("acf.processinfo")
fs = require("acf.fs")
validator = require("acf.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 = cfe({ label = "Program status", name=processname })
	local t = processinfo.pidof(processname)
	if (t) and (#t > 0) then
		status.status.value = "Started"
	else
		status.status.value = "Stopped"
	end

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

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

function update_papfiledetails(self, filedetails)
	local retval = modelfunctions.setfiledetails(self, filedetails, {papfile})
	posix.chmod(papfile, "rw-------")
	return retval
end

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

function update_chapfiledetails(self, filedetails)
	local retval = modelfunctions.setfiledetails(self, filedetails, {chapfile})
	posix.chmod(chapfile, "rw-------")
	return retval
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(self, 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 get_delete_peer(self, clientdata)
	retval = {}
	retval.name = cfe({ value=clientdata.name or "", label="Name" })
	return cfe({ type="group", value=retval, label="Delete Peer File" })
end

function delete_peer(self, delpeer)
	delpeer.errtxt = "Failed to delete peer"
	delpeer.value.name.errtxt="Peer not found"

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

	return delpeer
end

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

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