summaryrefslogtreecommitdiffstats
path: root/dhcp-controller.lua
blob: 9857fa49bc430cd7a0e9c775bbc54016b12be858 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
-- the dhcpd  controller

module (..., package.seeall)

-- Cause an http redirect to our "read" action
-- We use the self.conf table because it already has prefix,controller,etc
-- The redir code is defined in the application error handler (acf-controller)
local list_redir = function (self)
	self.conf.action = "home"
	self.conf.type = "redir"
	error (self.conf)
end

local pvt = {}
mvc= {}
mvc.on_load = function(self, parent)
	-- If they try to run a bogus action, send them to read
	if ( rawget(self.worker, self.conf.action) == nil ) then
		list_redir(self)
	end
	pvt.parent_on_exec = parent.worker.mvc.post_exec
	logit ("dhcpd controller on_load has finished")
end


mvc.pre_exec = function (self) 
	logit ("dhcpd-controller pre_exec activated")
	-- pvt.parent_on_exec ()
end

mvc.post_exec = function ( self )
	logit ("dhcpd-controller post_exec activated")
	return pvt.parent_on_exec()	
end

editnet = function ( self )

	if not self.clientdata.cmd then
		list_redir(self)
	end

	local net = {}
	local option = { script = ENV["SCRIPT_NAME"],
	                 prefix = self.conf.prefix,
	                 controller = self.conf.controller,
	                 action = self.conf.action,
	                 extra = ""
	               }
	         
	if self.clientdata.network then
		if self.clientdata.network == "choose" then
			list_redir(self)
		end
	end

	if self.clientdata.cmd == "update" then
		tmp = self.clientdata
		net = self.model.create_new_net( tmp.name, tmp.defleasetime, tmp.maxleasetime, tmp.gateway,
		          tmp.domainname, tmp.dnssrv1, tmp.dnssrv2, tmp.subnet, tmp.netmask, tmp.leaserangestart,
		          tmp.leaserangeend, tmp.wpad )
	 	errcode, net = self.model.subnet_write( net )
	 	return ( cfe({ option = option, value = net, errcode = errcode }) )
	end

	net = self.model.subnet_read( self.clientdata.network );
	return ( cfe({ option = option, value = net, errcode = { msg="", fields={} }}) )
end

createnet = function ( self )

	if not self.clientdata.cmd then
		list_redir(self)
	end
	
	local option = { script = ENV["SCRIPT_NAME"],
	                 prefix = self.conf.prefix,
	                 controller = self.conf.controller,
	                 action = self.conf.action,
	                 extra = ""
	               }
	               
	if self.clientdata.cmd == "new" then
		net = self.model.create_new_net( "<new>", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil )
		return ( cfe({ option = option, value = net, errcode = { msg = "", fields = nil }}) )
	elseif self.clientdata.cmd == "create" then
		tmp = self.clientdata
		net = self.model.create_new_net( tmp.name, tmp.defleasetime, tmp.maxleasetime, 
		         tmp.gateway, tmp.domainname, tmp.dnssrv1, tmp.dnssrv2, tmp.subnet, 
		         tmp.netmask, tmp.leaserangestart, tmp.leaserangeend, tmp.wpad )
		errcode, net = self.model.subnet_create( net )
		return ( cfe({ option = option, value = net, errcode = errcode }) )
	end
end

home = function ( self )

	local srvctrl = ""
	if self.clientdata.srvcmd then
		srvcmd = self.clientdata.srvcmd
		if srvcmd == "start" or srvcmd == "stop" or srvcmd == "restart" then
			srvctrl = self.model.service_control(self.clientdata.srvcmd)
		end
	end

	local option = { script = ENV["SCRIPT_NAME"],
	                 prefix = self.conf.prefix,
	                 controller = self.conf.controller,
	                 action = self.conf.action,
	                 extra = ""
	               }
	local info = { status = { value = "stopped" }, version = { value = self.model.get_dhcpd_version() }, srvctrl = { value = srvctrl} };
	if self.model.is_running( "dhcpd" ) then
		info.status.value = "running"
	end
	info.subnets = self.model.get_subnets()

	return ( cfe({ option = option, value = "", info = info }) )
end

view = function ( self )

	local filename = ""
	if self.clientdata.conf then
		filename = "/etc/dhcp/dhcpd.conf"
	elseif self.clientdata.leases then
		filename = "/var/lib/dhcpd/dhcpd.leases";
	else 
		list_redir(self)
	end

	local option = { script = ENV["SCRIPT_NAME"],
	                 prefix = self.conf.prefix,
	                 controller = self.conf.controller,
	                 action = self.conf.action,
	                 extra = ""
	               }
	               
	local value = { filename = { value=filename }, 
	                contents = { value=self.model.read_file(filename) } 
	              } 

	return ( cfe({ option = option, value = value }) )
end