suricata
util-lua-dns.c
Go to the documentation of this file.
1 /* Copyright (C) 2014 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 
19 /**
20  * \file
21  *
22  * \author Eric Leblond <eric@regit.org>
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-print.h"
36 #include "util-unittest.h"
37 
38 #include "util-debug.h"
39 
40 #include "output.h"
41 #include "app-layer.h"
42 #include "app-layer-parser.h"
43 #include "util-privs.h"
44 #include "util-buffer.h"
45 #include "util-proto-name.h"
46 #include "util-logopenfile.h"
47 #include "util-time.h"
48 #include "rust.h"
49 
50 #include "lua.h"
51 #include "lualib.h"
52 #include "lauxlib.h"
53 
54 #include "util-lua.h"
55 #include "util-lua-common.h"
56 #include "util-lua-dns.h"
57 
58 static int DnsGetDnsRrname(lua_State *luastate)
59 {
60  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
61  return LuaCallbackError(luastate, "error: protocol not dns");
62  RSDNSTransaction *tx = LuaStateGetTX(luastate);
63  if (tx == NULL) {
64  return LuaCallbackError(luastate, "internal error: no tx");
65  }
66  return SCDnsLuaGetRrname(luastate, tx);
67 }
68 
69 static int DnsGetTxid(lua_State *luastate)
70 {
71  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
72  return LuaCallbackError(luastate, "error: protocol not dns");
73  RSDNSTransaction *tx = LuaStateGetTX(luastate);
74  if (tx == NULL) {
75  return LuaCallbackError(luastate, "internal error: no tx");
76  }
77  SCDnsLuaGetTxId(luastate, tx);
78  return 1;
79 }
80 
81 static int DnsGetRcode(lua_State *luastate)
82 {
83  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
84  return LuaCallbackError(luastate, "error: protocol not dns");
85  RSDNSTransaction *tx = LuaStateGetTX(luastate);
86  if (tx == NULL) {
87  return LuaCallbackError(luastate, "internal error: no tx");
88  }
89  return SCDnsLuaGetRcode(luastate, tx);
90 }
91 
92 static int DnsGetRecursionDesired(lua_State *luastate)
93 {
94  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
95  return LuaCallbackError(luastate, "error: protocol not dns");
96  RSDNSTransaction *tx = LuaStateGetTX(luastate);
97  if (tx == NULL) {
98  return LuaCallbackError(luastate, "internal error: no tx");
99  }
100  uint16_t flags = SCDnsTxGetResponseFlags(tx);
101  int recursion_desired = flags & 0x0080 ? 1 : 0;
102  lua_pushboolean(luastate, recursion_desired);
103  return 1;
104 }
105 
106 static int DnsGetQueryTable(lua_State *luastate)
107 {
108  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
109  return LuaCallbackError(luastate, "error: protocol not dns");
110  RSDNSTransaction *tx = LuaStateGetTX(luastate);
111  if (tx == NULL) {
112  return LuaCallbackError(luastate, "internal error: no tx");
113  }
114  return SCDnsLuaGetQueryTable(luastate, tx);
115 }
116 
117 static int DnsGetAnswerTable(lua_State *luastate)
118 {
119  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
120  return LuaCallbackError(luastate, "error: protocol not dns");
121  RSDNSTransaction *tx = LuaStateGetTX(luastate);
122  return SCDnsLuaGetAnswerTable(luastate, tx);
123 }
124 
125 static int DnsGetAuthorityTable(lua_State *luastate)
126 {
127  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
128  return LuaCallbackError(luastate, "error: protocol not dns");
129  RSDNSTransaction *tx = LuaStateGetTX(luastate);
130  return SCDnsLuaGetAuthorityTable(luastate, tx);
131 }
132 
133 /** \brief register http lua extensions in a luastate */
135 {
136  /* registration of the callbacks */
137  lua_pushcfunction(luastate, DnsGetDnsRrname);
138  lua_setglobal(luastate, "DnsGetDnsRrname");
139 
140  lua_pushcfunction(luastate, DnsGetQueryTable);
141  lua_setglobal(luastate, "DnsGetQueries");
142 
143  lua_pushcfunction(luastate, DnsGetAnswerTable);
144  lua_setglobal(luastate, "DnsGetAnswers");
145 
146  lua_pushcfunction(luastate, DnsGetAuthorityTable);
147  lua_setglobal(luastate, "DnsGetAuthorities");
148 
149  lua_pushcfunction(luastate, DnsGetTxid);
150  lua_setglobal(luastate, "DnsGetTxid");
151 
152  lua_pushcfunction(luastate, DnsGetRcode);
153  lua_setglobal(luastate, "DnsGetRcode");
154 
155  lua_pushcfunction(luastate, DnsGetRecursionDesired);
156  lua_setglobal(luastate, "DnsGetRecursionDesired");
157  return 0;
158 }
tm-threads.h
util-lua-common.h
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:41
util-lua.h
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:59
threads.h
LuaRegisterDnsFunctions
int LuaRegisterDnsFunctions(lua_State *luastate)
register http lua extensions in a luastate
Definition: util-lua-dns.c:134
rust.h
util-privs.h
util-unittest.h
lua_State
struct lua_State lua_State
Definition: suricata-common.h:506
util-debug.h
util-print.h
detect.h
pkt-var.h
util-time.h
app-layer-parser.h
conf.h
util-proto-name.h
LuaStateGetTX
void * LuaStateGetTX(lua_State *luastate)
get tx pointer from the lua state
Definition: util-lua.c:134
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
util-lua-dns.h
threadvars.h
util-logopenfile.h
util-buffer.h
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:995
output.h
app-layer.h