-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.lua
79 lines (63 loc) · 1.71 KB
/
location.lua
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
#!/usr/bin/lua
local uci = require("uci").cursor()
local password = ''
salt_path = 'salt.txt'
output = 'location.txt'
dry = true
function exec_silent(command)
local p = assert(io.popen(command, 'r'))
local result = p:read('*all')
p:close()
return result
end
function sha512(input)
-- it's important to escape single quotes!
local result = exec_silent("echo -n '"..input.."' | sha512sum")
-- remove stdin from output
result = result:gsub('-', '')
-- remove whitespace from output
result = result:gsub(' ', '')
result = result:gsub('\n', '')
return result
end
function readSalt()
return exec_silent('cat '..salt_path)
end
function genSalt()
-- I think /dev/random should be more secure
return exec_silent('dd if=/dev/random bs=1024 count=1 2>/dev/null | md5sum -b | head -c 32 > '..salt_path)
end
function checkSalt()
-- This function check's wheter the salt already exists
return io.open(salt_path, 'r') ~= nil
end
if not checkSalt() then
genSalt()
io.write('Content-type: text/html\n\n')
io.write('wrote salt.')
os.exit()
end
input = io.read()
input = input:gmatch('[^,]+')
lat = input()
lon = input()
hash = input()
salt = readSalt()
test = lat..lon..password..salt
io.write('Content-type: text/html\n\n')
io.write('lat='..lat..' lon='..lon..' hash='..hash..'\n')
if sha512(test) == hash then
io.write('access=OK\n')
genSalt()
exec_silent("echo 'lat="..lat.." lon="..lon.."' > "..output)
if not dry then
local sname = uci:get_first("gluon-node-info", "location")
uci:set("gluon-node-info", sname, "latitude", lat)
uci:set("gluon-node-info", sname, "longitude", lon)
uci:save("gluon-node-info")
uci:commit("gluon-node-info")
end
io.write('status=DONE\n')
else
io.write('access=DENIED\n')
end