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