summaryrefslogtreecommitdiffstats
path: root/interfaces-model.lua
blob: b0d3b056ee2ad16fa7b026f91e5724526fad1f4f (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
279
280
281
282
283
284
-- acf model for /etc/network/interfaces 
-- Copyright(c) 2007 N. Angelacos - Licensed under terms of GPL2
module (..., package.seeall)

-- iface is a local (private) table with private methods for managing
-- the interfaces file.  All low-level stuff is done here.  It exposes
-- the iface.tags, file(raw), and array (parsed), as well as a number
-- of functions for managing the interface file. These are in a local
-- table because nobody outside the model should know anything about 
-- the interfaces file (not even where it is - it could be in an LDAP 
-- directory for all we know) The public module functions are defined
-- further below

local iface = {	tags = { "name", "comment", "method", "address", 
			"netmask", "gateway", "hostname", "provider", 
			"pre-up", "up", "down", "post-down" },
		methods = { "dhcp", "static", "ppp", "loopback", "manual" },
		-- lowlevel functions will insert file and array here
		}


-- Lowlevel functions - they do things directly to iface.
iface.read_file = function ()
	local filename = "/etc/network/interfaces" 
	iface.file =  cfe { name=filename, filename=filename }
	local file = io.open(filename)
	local rc = false
	if file then
		iface.file.value = file:read("*a")
		-- make sure it has a terminating \n
		iface.file.value  = string.gsub (iface.file.value, "([^\n])$", "%1\n")
		file:close()
		rc = true
	end
	return rc
end

iface.write_file = function ()
	local rc = false
	local file = io.open ( iface.file.name, "w")
	if file then
		file:write ( iface.file.value )
		file:close()
		rc = true
	end
	return rc
	end

function iface.iface_type ( )
	local f =  {}
	for k,v in pairs(iface.tags) do
		f[v] = cfe { name = v }
	end


	for k,v in pairs ({"comment", "pre-up", "up", "down", "post-down"}) do
		f[v].type ="longtext"
	end
	
	f.method.type = "select"
	f.method.option = iface.methods
	return (f)
end

-- return true or false + the index of iface.array matching "name"
function iface.index (name)
	if name== nil or #name == 0  then
		return true, 0
	end
	
	if iface.array == nil then
		iface.unpack_interfaces ()
	end

	for k,v in ipairs(iface.array) do
		if name == v.name.value then
			return true, k
		end
	end

	return false, 0
end


function iface.append ( self, value, prefix )
	self = self or ""
	-- if we already have some values, then append a newline
	if #self > 0  then self = self .. "\n" end
	-- strip the prefix
	local str = string.gsub(value, "^" .. ( prefix or "" ), "")
	-- and append
	return self .. str 
end

function iface.expand ( self, prefix )
	if #self == 0 then
		return ""
	end
	-- force the string to end in a single linefeed
	self = string.gsub (self, "\r", "")
	self = string.gsub (self, "[\n]*$", "\n")
	local str = ""
	for line in string.gmatch ( self, ".-\n") do
		if #line > 0 then
			str = str .. prefix .. " " .. line
		end
	end
	return str
end

function iface.unpack_interfaces ()
	if iface.array == nil then
		iface.read_file()
		iface.array = {}
	end
	
	-- call it array so we don't have to call it iface.array everywhere
	local array = iface.array
	local count = 0	
	
	array[count] = iface.iface_type()

	for line in string.gmatch ( iface.file.value, ".-\n") do
		-- strip leading spaces, tabs
		line = string.gsub (line, "^[%s]*", "")
		line = string.gsub (line, "\n*$", "")
		-- it can be #, auto, iface, or a parameter
		if string.match(line, "^#") then
			array[count].comment.value = 
				iface.append(array[count].comment.value, line , "#%s*" )
		elseif string.match(line, "^auto") then
			-- do nothing
		elseif string.match(line, "^iface") then
			count = count + 1
			array[count] = iface.iface_type()
			-- iface <name> [inet | ipx] <method> -- we assume inet
			array[count].name.value, 
				array[count].method.value = 
				string.match(line, "%w+%s+(%w+)%s+%w+%s+(%w+)")
		else -- it must be some kind of parameter
			local param, val = 
				string.match(line, "(%S+)%s*(.*)$")
			if (param) then 
				array[count][param].value = 
					iface.append (array[count][param].value, val)
			end
		end
	end

	-- now move the comments to go with the interface
	for n = count,1,-1 do
		array[n].comment.value = array[n-1].comment.value
	end

	return array
end

function iface.pack_interfaces()
	local str = ""
	for n = 1,#iface.array,1 do
		local me = iface.array[n]
		for k,v in pairs (iface.tags) do
			if v == "comment" then
				str = str .. "\n" .. iface.expand ( me[v].value, "#")
		elseif v == "method" then
				str = str .. string.format ("\nauto %s\niface %s inet %s\n",
					me.name.value, me.name.value, 
					me.method.value)
			elseif v == "name" then
				-- nothing
			else
				str = str .. iface.expand( me[v].value, "\t" .. v)
			end
		end
	end
	return str
end

function iface.commit()
	iface.file.value = iface.pack_interfaces()
	return iface.write_file() 
end


function iface.add_after ( name, def )
	-- if the new if.name is already in the table, then fail
	local rc, idx = iface.index(def.name.value)
	if idx > 0  then
		def.name.errtxt = "This interface is already defined"
		return false, def
	end

	-- if the name to insert after doesn't exist, just fail
	rc, idx = iface.index(name)
	if rc == false then
		return false, def
	end

	rc, def = iface.validate (def)
	if rc == false then
		return rc, def
	end
	
	table.insert( iface.array, idx+1, def )
	return iface.commit() , def
end

function iface.read ( name )
	-- if the name is blank, then return an empty def
	local rc, idx = iface.index(name or "")
	if name == nil or #name == 0 or rc == false then
		return rc, iface.iface_type()
	end
	return iface.commit(), iface.array[idx]
end


function iface.update ( def)
	-- if the def by that name doesn't exist, fail
	local rc, idx = iface.index(def.name.value or "" )
	if idx == 0 then
		def.name.errtxt = "This is an invalid interface name"
		return false, def
	end
	
	rc, def = iface.validate ( def )
	if rc == false then
		return rc, def
	end

	iface.array[idx] = def
	return iface.commit(), def
end

function iface.delete (name )
	local rc, idx = iface.index(name or "" )
	if idx == 0 then
		rc = false
	else
		table.remove (iface.array, idx ) 
	end
	return iface.commit()
end


iface.validate  = function ( def )
	if #def.name.value == 0 then
		iface.name.errtxt = "The interface must have a name"
	end
	def.method.errtxt = "Method specified is invalid"
	for k,v in pairs (iface.methods) do
		if def.method.value == v then
			def.method.errtxt = ""
		end
	end
	-- More validation tests go here ---
	--
	local rc = true
	for k,v in pairs(def) do
		if #def[k].errtxt > 0 then result = false end
	end

	return result, def
end

-------------------------------------------------------------------------------
-- Public Methods 
-------------------------------------------------------------------------------

get_all_interfaces	= iface.unpack_interfaces

get_iface_by_name	= iface.read

create_iface_by_name	= iface.add_after

update_iface_by_name	= function (def, name )
	-- make sure name we think we are updating is name we are updating
	def.name.value = name
	return iface.update (def) 
end

delete_iface_by_name	= iface.delete