summaryrefslogtreecommitdiff
path: root/src/debug_shell.lua
blob: e470a8e9d6cd5f43ad1415c681949a82f736d8e6 (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
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
--
--  anylike
--
--  anylike is an IKEv2 Implementation written in Lua and C. It's main 
--  design goal is to provide anytun and uanytun or any other SATP 
--  implementation with a key exchange mechanism but it should also be 
--  possible to use anylike as key exchange daemon for IPSec security 
--  associations. The use of Lua guarantees that anylike is easily 
--  portable to many platforms including very small ones like wireless 
--  routers.
--  
--
--  Copyright (C) 2007-2008 Markus Grueneis <gimpf@anylike.org>
--                          Christian Pointner <equinox@anylike.org>
--
--  This file is part of anylike.
--
--  anylike is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  any later version.
--
--  anylike is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with anylike. If not, see <http://www.gnu.org/licenses/>.
--

socket = require("socket")

debug_shell = {}

debug_shell.socks = {};

debug_shell.init = function(host, port) 
   local ip, err = socket.dns.toip(host)
   if(ip == nil) then
      log.printf(log.ERROR, "debug shell: can't resolve %s: %s", host, err)
      return nil
   end

   debug_shell.socks[1], err = socket.tcp()
   if(debug_shell.socks[1] == nil) then
      log.printf(log.ERROR, "debug shell: can't create tcp socket")
      return nil
   end

   debug_shell.socks[1]:setoption('reuseaddr', true);
      
   local ret, err = debug_shell.socks[1]:bind(ip, port)
   if(ret == nil) then
      log.printf(log.ERROR, "debug shell: bind(%s,%s) failed: %s", ip, port, err)
      return nil
   end

   local ret, err = debug_shell.socks[1]:listen()
   if(ret == nil) then
      log.printf(log.ERROR, "debug shell: listen() failed: %s", ip, port, err)
      return nil
   end

   debug_shell.socks[1]:settimeout(0);
   debug_shell.buffer = "";

   log.printf(log.WARNING, "debug shell: listening on %s:%s (this is a huge security risk)", ip, port);

   return debug_shell.socks[1]
end          

debug_shell.close = function()
   debug_shell.socks[1]:close()
   if(debug_shell.socks[2]) then debug_shell.socks[2]:close() end
end

debug_shell.handle = function(sock)
   local ret = 0
   if(sock == debug_shell.socks[1]) then
      local newclient, err = debug_shell.socks[1]:accept()
      if(newclient == nil) then
         log.printf(log.ERROR, "debug shell: accept() failed: %s", newclient, err)
      end
      local ip, port = newclient:getpeername();
      if(debug_shell.socks[2]) then
         log.printf(log.INFO, "debug shell: refusing connection from %s:%s, already connected", ip, port)
         newclient:close();
      else
         log.printf(log.INFO, "debug shell: connection from %s:%s accepted", ip, port)
         debug_shell.socks[2] = newclient
         debug_shell.socks[2]:settimeout(0);
         debug_shell.socks[2]:setoption('tcp-nodelay', true);
      end
   else 
      local data, err, partial = debug_shell.socks[2]:receive('*l')
      if(data == nil) then
         if(err == 'closed') then 
            log.printf(log.INFO, "debug shell: connection closed by peer", ip, port)
            debug_shell.socks[2]:close()
            debug_shell.socks[2] = nil
         elseif(err == 'timeout') then
            debug_shell.buffer = debug_shell.buffer .. partial
         else
            log.printf(log.INFO, "debug shell: connection error: %s", err)
            debug_shell.socks[2]:close()
            debug_shell.socks[2] = nil
         end
      else
         debug_shell.buffer = debug_shell.buffer .. data
         ret = debug_shell.exec_cmd()
         debug_shell.buffer = ""
      end
   end

   return ret
end

debug_shell.exec_cmd = function()
   log.printf(log.DEBUG, "debug shell: received string: '%s'", debug_shell.buffer)

   local luacode = string.match(debug_shell.buffer, "^!(.*)$");
   if(luacode and luacode ~= "") then
      local chunk = loadstring(luacode)
      if(chunk) then 
         log.printf(log.DEBUG, "debug shell: calling lua command: '%s'", luacode)
         local results = { pcall(function() chunk() end) }
         if(results[1]) then
            for i,result in ipairs(results) do
               if(i ~= 1) then
                  debug_shell.socks[2]:send(string.format("#%d: %s\n", i, tostring(result)))
               end
            end
         else
            debug_shell.socks[2]:send("lua call failed\n")
         end
      else
         debug_shell.socks[2]:send("syntax error\n")
      end
   else
      local cmd, sep, param = string.match(debug_shell.buffer, "^(%w+)(.?)(.*)$");
      
      if(cmd == 'quit') then
         if(sep and sep ~= "") then return 0 end
         log.printf(log.NOTICE, "debug shell: quit command received")
         return 2
      elseif(cmd == 'ping') then
         if(sep == ' ') then debug_shell.socks[2]:send("pong " .. (param or "") .. "\n")
         else debug_shell.socks[2]:send("pong\n") end
      else 
         debug_shell.socks[2]:send("unknown command\n")
      end
   end
   
   return 0
end