summaryrefslogtreecommitdiffstats
path: root/dhcp-controller.lua
blob: cf9d3070187d84e3d160fa84ea55a23201261539 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
-- 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

dep = function( self )

	-- do the dependancy check
        msg = self.model.dep_check()
        
        -- make sure nobody accidentally calls this action
        if msg == nil then
        	self.conf.type = "redir"
        	self.conf.action = "home"
        	error (self.conf)
        end
        
        -- go ahead
	return ( cfe ({ msg = msg }) )
end

help = function( self )

	return ( cfe ({ option = "" }) )
end

delnet = function( self )

	local net = {}

	if not self.clientdata.cmd then
		list_redir(self)
	end
	if not self.clientdata.network 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 == "delete" then
		if self.clientdata.confirm == "yes" then
			msg = self.model.subnet_delete( self.clientdata.network )
			list_redir(self)
		else 
			net = self.model.subnet_read( self.clientdata.network );
		        return ( cfe({ option = option, value = net, msg = msg }) )
		end
	end
end

settings = function( self )

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

	local settings = {}
	local option = { script = ENV["SCRIPT_NAME"],
	                 prefix = self.conf.prefix,
	                 controller = self.conf.controller,
	                 action = self.conf.action,
	                 extra = ""
	               }

	if self.clientdata.cmd == "update" then
		tmp = self.clientdata
		settings = self.model.create_new_settings( tmp.defleasetime, tmp.maxleasetime, tmp.domainname )
		errcode, settings = self.model.update_settings( settings )
		return ( cfe ({ option = option, value = settings, errcode = errcode }))
	else
		settings = self.model.read_settings()
		return ( cfe ({ option = option, value = settings, errcode = { msg = "", fields={} }}) )
	end
end

editnet = function ( self )

	if not self.clientdata.cmd then
		list_redir(self)
	else
		if self.clientdata.cmd == "back" then
			list_redir(self)
		end
	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
		dynamicx = tmp.dynamichosts
		advancedx = tmp.advanced
		if tmp.unknownclients == "allow" then
			dynamicx = tmp.dynamicx
		end
		if tmp.useadvanced ~= "use" then
			advancedx = tmp.advancedx
		end
		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, tmp.statichosts, tmp.unknownclients, dynamicx, advancedx, tmp.useadvanced )
	 	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

editspc = 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 == "update" then
		msg = ""
		fields = {}
		tmp = self.clientdata
		errmsg = self.model.validate_dynamichosts( tmp.dynamic )
		if #errmsg > 0 then
			msg = errmsg
			table.insert(fields, "dynamichosts")
		end
		value = self.model.advglobal_update( tmp.preconfig, tmp.postconfig, tmp.dynamic )
		return ( cfe({ option = option, value = value, errcode = { msg=msg, fields=fields }}) )
	end
		               
	value = self.model.advglobal_read()
	return ( cfe({ option = option, value = value, 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, 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, "", tmp.unknownclients, "", "", "" )
		errcode, net = self.model.subnet_create( net )
		if #errcode.msg == 0 then
			self.conf.type = "redir"
			self.conf.action = "home"
			error (self.conf)
		else 
			return ( cfe({ option = option, value = net, errcode = errcode }) )
		end
	end
end

home = function ( self )

	-- dependancy check for neccessary libs/packages et al.
	msg = self.model.dep_check()
	if msg ~= nil then
		self.conf.type = "redir"
		self.conf.action = "dep"
		error(self.conf)
	end

	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
	
	if self.clientdata.cmd == "generate" then
		genmsg = self.model.config_generate()
	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 = "", genmsg = genmsg, 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