00001 ---------------------------------------------------------------------------
00002 -- Copyright (C) 2014-2016 Cisco and/or its affiliates. All rights reserved.
00003 --
00004 -- This program is free software; you can redistribute it and/or modify it
00005 -- under the terms of the GNU General Public License Version 2 as published
00006 -- by the Free Software Foundation. You may not use, modify or distribute
00007 -- this program under any other version of the GNU General Public License.
00008 --
00009 -- This program is distributed in the hope that it will be useful, but
00010 -- WITHOUT ANY WARRANTY; without even the implied warranty of
00011 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012 -- General Public License for more details.
00013 --
00014 -- You should have received a copy of the GNU General Public License along
00015 -- with this program; if not, write to the Free Software Foundation, Inc.,
00016 -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 ---------------------------------------------------------------------------
00018 -- snort_config.lua author Russ Combs <rucombs@cisco.com>
00019
00020 ---------------------------------------------------------------------------
00021 -- Snort uses this to configure Lua settings into C++
00022 ---------------------------------------------------------------------------
00023
00024 ffi = require("ffi")
00025
00026 ffi.cdef[[
00027 bool open_table(const char*, int);
00028 void close_table(const char*, int);
00029 bool set_bool(const char*, bool);
00030 bool set_number(const char*, double);
00031 bool set_string(const char*, const char*);
00032 bool set_alias(const char*, const char*);
00033 ]]
00034
00035 function include(file)
00036 dofile(file)
00037 end
00038
00039 function snort_traverse(tab, fqn)
00040 local key, val
00041
00042 for key,val in pairs(tab) do
00043 -- skip Lua reserved symbols
00044 if ( string.sub(key, 1, 1) ~= '_' ) then
00045 if ( type(val) == 'string' ) then
00046 snort_set(fqn, key, val)
00047 end
00048 end
00049 end
00050
00051 for key,val in pairs(tab) do
00052 -- skip Lua reserved symbols
00053 if ( string.sub(key, 1, 1) ~= '_' ) then
00054 if ( type(val) ~= 'string' ) then
00055 snort_set(fqn, key, val)
00056 end
00057 end
00058 end
00059 end
00060
00061 function snort_set(fqn, key, val)
00062 local name
00063 local idx = 0
00064 local what = type(val)
00065
00066 if ( not fqn ) then
00067 name = key
00068
00069 elseif ( type(key) == 'number' ) then
00070 name = fqn
00071 idx = key
00072
00073 else
00074 name = fqn .. '.' .. key
00075 end
00076
00077 if ( what == 'boolean' ) then
00078 ffi.C.set_bool(name, val)
00079
00080 elseif ( what == 'number' ) then
00081 ffi.C.set_number(name, val)
00082
00083 elseif ( what == 'string' ) then
00084 ffi.C.set_string(name, val)
00085
00086 elseif ( what == 'table' ) then
00087 if ( ffi.C.open_table(name, idx) ) then
00088 snort_traverse(val, name)
00089 ffi.C.close_table(name, idx)
00090 end
00091 end
00092 end
00093
00094 function load_aliases()
00095 for i,v in ipairs(binder) do
00096 if ( v.use and type(v.use) == "table" ) then
00097 if ( v.use.name and v.use.type ) then
00098 ffi.C.set_alias(v.use.name, v.use.type)
00099 tab = _G[v.use.name]
00100
00101 if ( tab ) then
00102 snort_set(nil, v.use.name, _G[v.use.name])
00103 end
00104 end
00105 end
00106 end
00107 end
00108
00109 function snort_config(tab)
00110 snort_traverse(tab)
00111
00112 if ( binder and type(binder) == 'table' ) then
00113 load_aliases()
00114 end
00115 end
00116
END OF CODE