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 #ifdef HAVE_LUA
51 
52 #include <lua.h>
53 #include <lualib.h>
54 #include <lauxlib.h>
55 
56 #include "util-lua.h"
57 #include "util-lua-common.h"
58 #include "util-lua-dns.h"
59 
60 static int DnsGetDnsRrname(lua_State *luastate)
61 {
62  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
63  return LuaCallbackError(luastate, "error: protocol not dns");
64  RSDNSTransaction *tx = LuaStateGetTX(luastate);
65  if (tx == NULL) {
66  return LuaCallbackError(luastate, "internal error: no tx");
67  }
68  return rs_dns_lua_get_rrname(luastate, tx);
69 }
70 
71 static int DnsGetTxid(lua_State *luastate)
72 {
73  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
74  return LuaCallbackError(luastate, "error: protocol not dns");
75  RSDNSTransaction *tx = LuaStateGetTX(luastate);
76  if (tx == NULL) {
77  return LuaCallbackError(luastate, "internal error: no tx");
78  }
79  rs_dns_lua_get_tx_id(luastate, tx);
80  return 1;
81 }
82 
83 static int DnsGetRcode(lua_State *luastate)
84 {
85  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
86  return LuaCallbackError(luastate, "error: protocol not dns");
87  RSDNSTransaction *tx = LuaStateGetTX(luastate);
88  if (tx == NULL) {
89  return LuaCallbackError(luastate, "internal error: no tx");
90  }
91  return rs_dns_lua_get_rcode(luastate, tx);
92 }
93 
94 static int DnsGetRecursionDesired(lua_State *luastate)
95 {
96  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
97  return LuaCallbackError(luastate, "error: protocol not dns");
98  RSDNSTransaction *tx = LuaStateGetTX(luastate);
99  if (tx == NULL) {
100  return LuaCallbackError(luastate, "internal error: no tx");
101  }
102  uint16_t flags = rs_dns_tx_get_response_flags(tx);
103  int recursion_desired = flags & 0x0080 ? 1 : 0;
104  lua_pushboolean(luastate, recursion_desired);
105  return 1;
106 }
107 
108 static int DnsGetQueryTable(lua_State *luastate)
109 {
110  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
111  return LuaCallbackError(luastate, "error: protocol not dns");
112  RSDNSTransaction *tx = LuaStateGetTX(luastate);
113  if (tx == NULL) {
114  return LuaCallbackError(luastate, "internal error: no tx");
115  }
116  return rs_dns_lua_get_query_table(luastate, tx);
117 }
118 
119 static int DnsGetAnswerTable(lua_State *luastate)
120 {
121  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
122  return LuaCallbackError(luastate, "error: protocol not dns");
123  RSDNSTransaction *tx = LuaStateGetTX(luastate);
124  return rs_dns_lua_get_answer_table(luastate, tx);
125 }
126 
127 static int DnsGetAuthorityTable(lua_State *luastate)
128 {
129  if (!(LuaStateNeedProto(luastate, ALPROTO_DNS)))
130  return LuaCallbackError(luastate, "error: protocol not dns");
131  RSDNSTransaction *tx = LuaStateGetTX(luastate);
132  return rs_dns_lua_get_authority_table(luastate, tx);
133 }
134 
135 /** \brief register http lua extensions in a luastate */
136 int LuaRegisterDnsFunctions(lua_State *luastate)
137 {
138  /* registration of the callbacks */
139  lua_pushcfunction(luastate, DnsGetDnsRrname);
140  lua_setglobal(luastate, "DnsGetDnsRrname");
141 
142  lua_pushcfunction(luastate, DnsGetQueryTable);
143  lua_setglobal(luastate, "DnsGetQueries");
144 
145  lua_pushcfunction(luastate, DnsGetAnswerTable);
146  lua_setglobal(luastate, "DnsGetAnswers");
147 
148  lua_pushcfunction(luastate, DnsGetAuthorityTable);
149  lua_setglobal(luastate, "DnsGetAuthorities");
150 
151  lua_pushcfunction(luastate, DnsGetTxid);
152  lua_setglobal(luastate, "DnsGetTxid");
153 
154  lua_pushcfunction(luastate, DnsGetRcode);
155  lua_setglobal(luastate, "DnsGetRcode");
156 
157  lua_pushcfunction(luastate, DnsGetRecursionDesired);
158  lua_setglobal(luastate, "DnsGetRecursionDesired");
159  return 0;
160 }
161 
162 #endif /* HAVE_LUA */
tm-threads.h
util-lua-common.h
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:41
util-lua.h
threads.h
rust.h
util-privs.h
util-unittest.h
util-debug.h
util-print.h
detect.h
pkt-var.h
util-time.h
app-layer-parser.h
conf.h
util-proto-name.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
lua_State
void lua_State
Definition: suricata-common.h:500
util-lua-dns.h
threadvars.h
util-logopenfile.h
util-buffer.h
output.h
app-layer.h