From 8d796f898128855aa17cd06a0c04e97a65d5fbc9 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 12 Sep 2013 08:12:11 +0000 Subject: [PATCH] Add ncat_main.c facilities for -L. Currently still with the ugly o.script name. --- ncat/ncat_core.c | 1 + ncat/ncat_core.h | 1 + ncat/ncat_lua.c | 3 ++- ncat/ncat_lua.h | 2 +- ncat/ncat_main.c | 29 ++++++++++++++++++++++++++--- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ncat/ncat_core.c b/ncat/ncat_core.c index e2b71686b..1a2bcfd03 100644 --- a/ncat/ncat_core.c +++ b/ncat/ncat_core.c @@ -201,6 +201,7 @@ void options_init(void) o.cmdexec = NULL; o.execmode = EXEC_PLAIN; + o.script = 0; o.proxy_auth = NULL; o.proxytype = NULL; diff --git a/ncat/ncat_core.h b/ncat/ncat_core.h index b19e43652..868197a07 100644 --- a/ncat/ncat_core.h +++ b/ncat/ncat_core.h @@ -195,6 +195,7 @@ struct options { /* When execmode == EXEC_LUA, cmdexec is the name of the file to run. */ char *cmdexec; enum exec_mode execmode; + int script; char *proxy_auth; char *proxytype; diff --git a/ncat/ncat_lua.c b/ncat/ncat_lua.c index f90b40f6e..be7425b02 100644 --- a/ncat/ncat_lua.c +++ b/ncat/ncat_lua.c @@ -165,9 +165,10 @@ static int traceback (lua_State *L) return 1; } -void lua_setup(char *cmdexec) +void lua_setup(char *cmdexec, int script) { ncat_assert(cmdexec != NULL); + ncat_assert(script == 0); //TODO lua_State **L = &luaexec_L; diff --git a/ncat/ncat_lua.h b/ncat/ncat_lua.h index c0a0f4336..55750d42b 100644 --- a/ncat/ncat_lua.h +++ b/ncat/ncat_lua.h @@ -142,6 +142,6 @@ extern int error_handler_idx; void lua_report(lua_State *L, char *prefix, int panic); void dump_stack(lua_State *L, char* title); -void lua_setup(char *cmdexec); +void lua_setup(char *cmdexec, int script); #endif diff --git a/ncat/ncat_main.c b/ncat/ncat_main.c index 5af89f25a..ecea2af23 100644 --- a/ncat/ncat_main.c +++ b/ncat/ncat_main.c @@ -149,6 +149,10 @@ #ifdef HAVE_LUA #include "ncat_lua.h" #include "ncat_lua_exec.h" + +char** filter_filenames = NULL; +int filter_filenames_no = 0; + #endif static int ncat_connect_mode(void); @@ -278,6 +282,7 @@ int main(int argc, char *argv[]) #ifdef HAVE_LUA {"lua-exec", required_argument, NULL, 0}, {"lua-exec-internal",required_argument, NULL, 0}, + {"load-lua-socket-file", required_argument, NULL, 'L'}, #endif {"max-conns", required_argument, NULL, 'm'}, {"help", no_argument, NULL, 'h'}, @@ -332,7 +337,7 @@ int main(int argc, char *argv[]) while (1) { /* handle command line arguments */ int option_index; - int c = getopt_long(argc, argv, "46UCc:e:g:G:i:km:hp:d:lo:x:ts:uvw:n", + int c = getopt_long(argc, argv, "46UCc:e:g:G:i:km:hp:d:lo:x:ts:uvw:nL:", long_options, &option_index); /* That's the end of the options. */ @@ -545,9 +550,15 @@ int main(int argc, char *argv[]) #endif ncat_assert(argc == 3); o.cmdexec = argv[2]; - lua_setup(o.cmdexec); + lua_setup(o.cmdexec, 0); lua_run(); } + break; + case 'L': + o.script = 1; + filter_filenames_no++; + filter_filenames = (char **)safe_realloc(filter_filenames, sizeof(char *) * filter_filenames_no); + filter_filenames[filter_filenames_no-1] = Strdup(optarg); #endif break; case 'h': @@ -567,6 +578,8 @@ int main(int argc, char *argv[]) " -e, --exec Executes the given command\n" #ifdef HAVE_LUA " --lua-exec Executes the given Lua script\n" +//This line is clearly too long. The option name, IMHO, too. +" -L --load-lua-socket-file Applies the Lua filter script\n" #endif " -g hop1[,hop2,...] Loose source routing hop points (8 max)\n" " -G Loose source routing hop pointer (4, 8, 12, ...)\n" @@ -869,8 +882,18 @@ connection brokering should work."); set_lf_mode(); #ifdef HAVE_LUA + if (o.script) { + int i; + for (i = 0; i < filter_filenames_no; i++) { + if (o.verbose) + loguser("Applying Lua filter: %s\n", filter_filenames[i]); + lua_setup(filter_filenames[i], 1); + free(filter_filenames[i]); + } + free(filter_filenames); + } if (o.execmode == EXEC_LUA) - lua_setup(o.cmdexec); + lua_setup(o.cmdexec, 0); #endif if (o.listen)