aboutsummaryrefslogtreecommitdiffstats
path: root/main/aaudit/aaudit
blob: 6b021427de12eab9f0a490b73cacccff585da09a (plain) (blame)
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
#!/usr/bin/lua5.2

local posix = require 'posix'
local json = require 'cjson'
local lpc = require 'lpc'
local aac = require 'aaudit.common'

local function usage()
	print([[
Usage: aaudit [create|commit] [OPTIONS...]

Options for create:
	-s SERV	Use server SERV
	-d DESC	Description for repository (default: hostname)
	-t ADDR	Specify ADDR as target device (default: local source IP)
	-g GRP	Add in group GRP (can be specified multiple times)

Options for create and commit:
	-m MSG	Specify message for the commit
	-L	Local change (use local 'contact' as change author)
]])
	os.exit(1)
end

local verbose = false
local conf = aac.readconfig() or {}
local req = {}

for ret, optval in posix.getopt(arg, 'vs:d:t:m:Lg:') do
	if ret == 'v' then
		verbose = true
	elseif ret == 's' then
		conf.server = optval
	elseif ret == 'd' then
		conf.description = optval
	elseif ret == 't' then
		conf.target_address = optval
	elseif ret == 'm' then
		req.message = optval
	elseif ret == 'L' then
		req.local_change = true
	elseif ret == 'g' then
		req.groups = req.groups or {}
		table.insert(req.groups, optval)
	else
		usage()
	end
end

if conf.server == nil then
	print("Error: No server configured.")
	usage()
end

req.command = arg[1]
if arg[1] == "create" then
	req.description = conf.description or aac.readfile("/etc/hostname"):gsub("\n","")
	req.ssh_host_key = aac.readfile("/etc/ssh/ssh_host_ecdsa_key.pub")
		or aac.readfile("/etc/ssh/ssh_host_dsa_key.pub")
		or aac.readfile("/etc/ssh/ssh_host_rsa_key.pub")
	aac.writeconfig(conf)
	arg[1] = "commit"
end

req.apkovl_follows = true

if arg[1] ~= "commit" then usage() end

local pid, SW, SR = lpc.run('ssh', '-T', ('%s@%s'):format(conf.user or "aaudit", conf.server))

SW:write(json.encode(req),'\n')
if req.apkovl_follows then
	local APKOVL = io.popen("lbu package -", "rb")
	while true do
		local block = APKOVL:read(2^13)
		if not block then break end
		SW:write(block)
	end
	APKOVL:close()
end
SW:close()

local reply
for line in SR:lines() do
	if line:match("^{") and line:match("}$") then
		reply = json.decode(line)
	elseif verbose then
		print(line)
	end
end
SR:close()

lpc.wait(pid)

if reply then
	reply.msg = reply.msg or "(No message. Server error?)"
	if reply.ok then
		io.write("OK: ",reply.msg,"\n")
	else
		io.write("ERROR: ",reply.msg,"\n")
	end
	if reply.notified then
		io.write("Notified: ",reply.notified,"\n")
	end
else
	io.write("ERROR: No reply received from server\n")
end