summaryrefslogtreecommitdiffstats
path: root/nse_utility.cc
blob: 0ccd2d7f33a24326df0dc30c7a1f0b21b701e5a2 (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
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>

#include "Target.h"
#include "portlist.h"

#include "nse_main.h"
#include "nse_utility.h"

#ifndef IPPROTO_SCTP
#include "libnetutil/netutil.h"
#endif

int nseU_checkinteger (lua_State *L, int arg)
{
  lua_Number n = luaL_checknumber(L, arg);
  int i;
  if (!lua_numbertointeger(floor(n), &i)) {
    return luaL_error(L, "Number cannot be converted to an integer");
  }
  return i;
}

int nseU_traceback (lua_State *L)
{
  if (lua_isstring(L, 1))
    luaL_traceback(L, L, lua_tostring(L, 1), 1);
  return 1;
}

int nseU_placeholder (lua_State *L)
{
  lua_pushnil(L);
  return lua_error(L);
}

size_t nseU_tablen (lua_State *L, int idx)
{
  size_t len = 0;
  idx = lua_absindex(L, idx);

  for (lua_pushnil(L); lua_next(L, idx); lua_pop(L, 1))
    len++;

  return len;
}

void nseU_setsfield (lua_State *L, int idx, const char *field, const char *what)
{
  idx = lua_absindex(L, idx);
  lua_pushstring(L, what); /* what can be NULL */
  lua_setfield(L, idx, field);
}

void nseU_setnfield (lua_State *L, int idx, const char *field, lua_Number n)
{
  idx = lua_absindex(L, idx);
  lua_pushnumber(L, n);
  lua_setfield(L, idx, field);
}

void nseU_setifield (lua_State *L, int idx, const char *field, lua_Integer i)
{
  idx = lua_absindex(L, idx);
  lua_pushinteger(L, i);
  lua_setfield(L, idx, field);
}

void nseU_setbfield (lua_State *L, int idx, const char *field, int b)
{
  idx = lua_absindex(L, idx);
  lua_pushboolean(L, b);
  lua_setfield(L, idx, field);
}

void nseU_setpfield (lua_State *L, int idx, const char *field, void * p)
{
  idx = lua_absindex(L, idx);
  lua_pushlightuserdata(L, p);
  lua_setfield(L, idx, field);
}

void nseU_appendfstr (lua_State *L, int idx, const char *fmt, ...)
{
  va_list va;
  idx = lua_absindex(L, idx);
  va_start(va, fmt);
  lua_pushvfstring(L, fmt, va);
  va_end(va);
  lua_rawseti(L, idx, lua_rawlen(L, idx)+1);
}

int nseU_success (lua_State *L)
{
  lua_pushboolean(L, true);
  return 1;
}

int nseU_safeerror (lua_State *L, const char *fmt, ...)
{
  va_list va;
  lua_pushboolean(L, false);
  va_start(va, fmt);
  lua_pushvfstring(L, fmt, va);
  va_end(va);
  return 2;
}

void nseU_weaktable (lua_State *L, int narr, int nrec, const char *mode)
{
  lua_createtable(L, narr, nrec);
  lua_createtable(L, 0, 1);
  lua_pushstring(L, mode);
  lua_setfield(L, -2, "__mode");
  lua_setmetatable(L, -2);
}

void nseU_typeerror (lua_State *L, int idx, const char *type)
{
  const char *msg = lua_pushfstring(L, "%s expected, got %s", type, luaL_typename(L, idx));
  luaL_argerror(L, idx, msg);
}

void *nseU_checkudata (lua_State *L, int idx, int upvalue, const char *name)
{
  idx = lua_absindex(L, idx);

  lua_getmetatable(L, idx);
  if (!(lua_isuserdata(L, idx) && lua_rawequal(L, -1, upvalue)))
    nseU_typeerror(L, idx, name);
  lua_pop(L, 1);
  return lua_touserdata(L, idx);
}

void nseU_checktarget (lua_State *L, int idx, const char **address, const char **targetname)
{
  idx = lua_absindex(L, idx);
  if (lua_istable(L, idx)) {
    lua_getfield(L, idx, "ip");
    *address = lua_tostring(L, -1);
    lua_getfield(L, idx, "targetname");
    *targetname = lua_tostring(L, -1);
    if (*address == NULL && *targetname == NULL)
      luaL_argerror(L, idx, "host table lacks 'ip' or 'targetname' fields");
    *address = *address ? *address : *targetname;
    lua_pop(L, 2); /* no point replacing idx, need 2 only have 1 */
  } else {
    *address = *targetname = luaL_checkstring(L, idx);
  }
}

void nseU_opttarget (lua_State *L, int idx, const char **address, const char **targetname)
{
  if (lua_isnoneornil(L, idx)) {
    *address = NULL;
    *targetname = NULL;
    return;
  } else {
    return nseU_checktarget(L, idx, address, targetname);
  }
}

uint16_t nseU_checkport (lua_State *L, int idx, const char **protocol)
{
  uint16_t port;
  idx = lua_absindex(L, idx);

  if (lua_istable(L, idx)) {
    lua_getfield(L, idx, "number");
    if (!lua_isinteger(L, -1))
      luaL_argerror(L, idx, "port table lacks integer 'number' field");
    port = (uint16_t) lua_tointeger(L, -1);
    lua_getfield(L, idx, "protocol");
    if (lua_isstring(L, -1))
      *protocol = lua_tostring(L, -1);
    lua_pop(L, 2);
  } else {
    port = (uint16_t) luaL_checkinteger(L, idx);
  }
  return port;
}

Target *nseU_gettarget (lua_State *L, int idx)
{
  int top = lua_gettop(L);
  Target *target;
  idx = lua_absindex(L, idx);
  luaL_checktype(L, idx, LUA_TTABLE);
  lua_getfield(L, idx, "_Target");
  lua_getfield(L, idx, "targetname");
  lua_getfield(L, idx, "ip");
  if (!(lua_isstring(L, -2) || lua_isstring(L, -1)))
    luaL_error(L, "host table does not have a 'ip' or 'targetname' field");
  if (lua_islightuserdata(L, -3)) /* _Target */
  {
    lua_pop(L, 2); /* pop ip and targetname, leaving _Target on top */
    goto done;
  }
  /* IP is preferred to targetname because it is more unique. Really, though, a
   * user can scan the same IP or targetname multiple times, and NSE will get
   * all mixed up. */
  if (lua_isstring(L, -1)) /* ip */
  {
    nse_gettarget(L, -1); /* use ip */
    if (lua_islightuserdata(L, -1))
      goto done;
    else
      lua_pop(L, 1);
  }
  if (lua_isstring(L, -2)) /* targetname */
    nse_gettarget(L, -2); /* use targetname */
  if (!lua_islightuserdata(L, -1))
    luaL_argerror(L, 1, "host is not being processed right now");
done:
  target = (Target *) lua_touserdata(L, -1);
  lua_settop(L, top); /* reset stack */
  return target;
}

Port *nseU_getport (lua_State *L, Target *target, Port *port, int idx)
{
  Port *p = NULL;
  int portno, protocol;
  idx = lua_absindex(L, idx);
  luaL_checktype(L, idx, LUA_TTABLE);
  lua_getfield(L, idx, "number");
  if (!lua_isinteger(L, -1))
    luaL_error(L, "port 'number' field must be an integer");
  lua_getfield(L, idx, "protocol");
  if (!lua_isstring(L, -1))
    luaL_error(L, "port 'protocol' field must be a string");
  portno = (int) lua_tointeger(L, -2);
  protocol = strcmp(lua_tostring(L, -1), "tcp") == 0 ? IPPROTO_TCP :
             strcmp(lua_tostring(L, -1), "udp") == 0 ? IPPROTO_UDP :
             strcmp(lua_tostring(L, -1), "sctp") == 0 ? IPPROTO_SCTP :
             luaL_error(L, "port 'protocol' field must be \"udp\", \"sctp\" or \"tcp\"");
  while ((p = target->ports.nextPort(p, port, protocol, PORT_UNKNOWN)) != NULL)
    if (p->portno == portno)
      break;
  lua_pop(L, 2);
  return p;
}