summaryrefslogtreecommitdiff
path: root/cgi/command.cgi
blob: d81ef085365e2e32735a4bc72edcc4e09c534d4f (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
#!/usr/bin/env lua5.1

local gitano = require "gitano"
local gall = require "gall"
local luxio = require "luxio"
local sio = require "luxio.simple"
local sp = require "luxio.subprocess"

local stream = sio.stderr

function url_decode(str)
  str = string.gsub (str, "+", " ")
  str = string.gsub (str, "%%(%x%x)",
      function(h) return string.char(tonumber(h,16)) end)
  str = string.gsub (str, "\r\n", "\n")
  return str
end

function run_command(cmd, parsed_cmdline, user, config, env)
   gitano.log.debug("Welcome to " .. config.global.site_name)
   gitano.log.debug("Running:")
   for i = 1, #parsed_cmdline do
      gitano.log.debug(" => " .. parsed_cmdline[i])
   end
   gitano.log.debug("")
   gitano.log.debug("On behalf of " .. user .. " using key " .. env["GITANO_KEYTAG"])

   local how, why = cmd.run(config, repo, parsed_cmdline, env)

   if how ~= "exit" or why ~= 0 then
      gitano.log.critical("Error running sub-process:",
                ("%s (%d)"):format(how, why))
      gitano.log.fatal("Unable to continue")
   else
      gitano.log.syslog.info(cmdline, "completed successfully")
   end
end

local query_string = url_decode(os.getenv("QUERY_STRING"))
local cmdline = query_string

stream:write('cmdline: ' .. cmdline)

local _, e = string.find(query_string, "cmd=")
cmdline = string.sub(query_string, e + 1, #query_string)

local user = os.getenv("REMOTE_USER")

local authorized, cmd, parsed_cmdline, config, env =
   gitano.auth.is_authorized(user, cmdline)

if authorized then
   run_command(cmd, parsed_cmdline, user, config, env)
else
   io.write("Status: 403 Forbidden\r\n\r\n")
end