public:
ConversionState(Converter *cv){ converter = cv; }
virtual ~ConversionState() {};
- virtual bool convert(std::stringstream& data,
- std::ofstream& out)=0;
+ virtual bool convert(std::stringstream& data)=0;
protected:
}
-bool Converter::convert_line(std::stringstream& data, std::ofstream& out)
+bool Converter::convert_line(std::stringstream& data)
{
if ( state )
- return state->convert(data, out);
+ return state->convert(data);
return false;
}
-bool Converter::open_table(std::string name)
+bool Converter::open_table(std::string table_name)
{
Table *t;
// if no open tables, create a top-level table
if (open_tables.size() > 0)
- t = open_tables.top()->open_table(name);
+ t = open_tables.top()->open_table(table_name);
else
- t = data.add_table(name);
+ t = data.add_table(table_name);
open_tables.push(t);
return true;
}
-bool Converter::add_option_to_table(std::string name, std::string val)
+bool Converter::add_option_to_table(std::string option_name, std::string val)
{
Table *t = open_tables.top();
if(t)
{
- t->add_option(name, val);
+ t->add_option(option_name, val);
return true;
}
else
}
-bool Converter::add_option_to_table(std::string name, int val)
+bool Converter::add_option_to_table(std::string option_name, int val)
{
Table *t = open_tables.top();
if(t)
{
- t->add_option(name, val);
+ t->add_option(option_name, val);
return true;
}
else
}
}
-bool Converter::add_option_to_table(std::string name, bool val)
+bool Converter::add_option_to_table(std::string option_name, bool val)
{
Table *t = open_tables.top();
if(t)
{
- t->add_option(name, val);
+ t->add_option(option_name, val);
return true;
}
else
}
}
+void Converter::add_comment_to_table(std::string error_string)
+{
+ if (open_tables.size() > 0)
+ open_tables.top()->add_comment(error_string);
+ else
+ log_error(error_string);
+}
+
+void Converter::add_comment_to_file(std::string comment)
+{
+ data.add_comment(comment);
+}
+
+void Converter::add_comment_to_file(std::string comment, std::stringstream& stream)
+{
+ int pos = stream.tellg();
+ std::ostringstream oss;
+ oss << stream.rdbuf();
+ comment += oss.str();
+ data.add_comment(comment);
+ stream.seekg(pos);
+}
/*******************************
******* PRINTING FOO *********
void Converter::log_error(std::string error_string)
{
- std::cout << "ERROR: Failed to convert:\t" << std::endl;
- std::cout << "\t\t" << error_string << std::endl << std::endl;
+ data.add_error_comment(error_string);
+// std::cout << "ERROR: Failed to convert:\t" << std::endl;
+// std::cout << "\t\t" << error_string << std::endl << std::endl;
}
void Converter::print_line(std::stringstream& in)
Converter();
virtual ~Converter() {};
void reset_state();
- bool convert_line(std::stringstream& data, std::ofstream& out);
+ bool convert_line(std::stringstream& data);
void set_state(ConversionState* c);
bool inline add_variable(std::string name, std::string v){ return data.add_variable(name, v); };
bool add_option_to_table(std::string name, std::string val);
bool add_option_to_table(std::string name, int val);
bool add_option_to_table(std::string name, bool val);
+ void add_comment_to_table(std::string comment);
+ void add_comment_to_file(std::string comment);
+ void add_comment_to_file(std::string comment, std::stringstream& stream);
void log_error(std::string);
void print_line(std::stringstream& in);
delete t;
}
-std::ostream& operator<<( std::ostream &out, const ConversionData &data)
-{
- for (Variable *v : data.vars)
- out << (*v) << std::endl << std::endl;
-
- for (Table *t : data.tables)
- out << (*t) << std::endl << std::endl;
-
- return out;
-}
-
bool ConversionData::add_variable(std::string name, std::string value)
{
for (auto v : vars)
return var->add_value(value);
}
-
Table* ConversionData::add_table(std::string name)
{
Table* t = find_table(tables, name);
return t;
}
+void ConversionData::add_comment(std::string str)
+{
+ // leave at most one blank line between comments
+ if ( !(str.empty() && !comments.empty() && comments.back().empty()) )
+ comments.push_back(std::string(str, 0, 77) + "...");
+}
+
+void ConversionData::add_error_comment(std::string error_string)
+{
+ errors.push_back(std::string(error_string, 0, 77) + "...");
+}
+
+std::ostream& operator<<( std::ostream &out, const ConversionData &data)
+{
+ out << "--[[" << std::endl;
+ out << "--ERRORS:" << std::endl;
+ out << " all of these occured during the attempted conversion:" << std::endl << std::endl;
+
+ for (std::string s : data.errors)
+ out << s << std::endl << std::endl;
+
+ out << "--]]" << std::endl << std::endl << std::endl;
+
+ for (Variable *v : data.vars)
+ out << (*v) << std::endl << std::endl;
+
+ for (Table *t : data.tables)
+ out << (*t) << std::endl << std::endl;
+
+
+ out << "--[[" << std::endl << std::endl;
+ out << "COMMENTS:" << std::endl;
+ out << " these line were originally commented out or empty" << std::endl;
+ out << " in the configuration file." << std::endl;
+ out << std::endl;
+
+ for (std::string s : data.comments)
+ out << s << std::endl;
+
+ out << "--]]" << std::endl;
+
+ return out;
+}
#if 0
bool ConversionData::add_option(std::string name, std::string value)
{
friend std::ostream &operator<<( std::ostream&, const ConversionData &);
bool add_variable(std::string name, std::string value);
Table* add_table(std::string name);
+ void add_comment(std::string comment);
+ void add_error_comment(std::string comment);
#if 0
bool add_option(std::string name, std::string value);
private:
std::vector<Variable*> vars;
std::vector<Table*> tables;
+ std::vector<std::string> comments;
+ std::vector<std::string> errors;
};
switch(o.type)
{
case Option::OptionType::STRING:
- case Option::OptionType::BOOL:
out << '\'' << o.value << "',";
break;
+ case Option::OptionType::BOOL:
case Option::OptionType::INT:
out << o.value << ',';
break;
static inline Table* find_table(std::vector<Table*> vec, std::string name)
{
for( auto *t : vec)
- if(name.compare(t->get_name()))
+ if(!name.compare(t->get_name()))
return t;
return nullptr;
return false;
}
+
+void Table::add_comment(std::string comment)
+{
+ comments.push_back(std::string(comment, 0, 77) + "...");
+}
+
std::ostream &operator<<( std::ostream& out, const Table &t)
{
std::string whitespace = "";
out << whitespace << t.name << " = " << std::endl;
out << whitespace << '{' << std::endl;
+ for(std::string s : t.comments)
+ out << whitespace << " --" << s << std::endl;
+
for (Option* o : t.options)
out << (*o) << std::endl;
bool has_option(std::string name, int val);
bool has_option(std::string name, bool val);
bool has_option(std::string name, std::string val);
+ void add_comment(std::string comment);
friend std::ostream &operator<<( std::ostream&, const Table &);
int depth;
std::vector<Table*> tables;
std::vector<Option*> options;
+ std::vector<std::string> comments;
};
#endif
InitState::InitState(Converter* cv) : ConversionState(cv) {}
-bool InitState::convert(std::stringstream& data_stream, std::ofstream& out)
+bool InitState::convert(std::stringstream& data_stream)
{
std::string keyword;
if( keyword.front() == '#')
{
keyword.erase(keyword.begin());
- std::ostringstream oss;
- oss << data_stream.rdbuf();
- out << "--" << keyword << oss.str() << std::endl;
+// std::ostringstream oss;
+// oss << data_stream.rdbuf();
+// out << "--" << keyword << oss.str() << std::endl;
+ converter->add_comment_to_file(keyword, data_stream);
data_stream.setstate(std::basic_ios<char>::eofbit);
return true;
}
}
}
- out << "--" << data_stream.str() << std::endl;
- data_stream.setstate(std::basic_ios<char>::eofbit);
+// out << "--" << data_stream.str() << std::endl;
+// data_stream.setstate(std::basic_ios<char>::eofbit);
return false;
}
public:
InitState(Converter* cv);
virtual ~InitState() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
};
public:
Config(Converter* cv) : ConversionState(cv) {};
virtual ~Config() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
};
} // namespace
-bool Config::convert(std::stringstream& data_stream, std::ofstream&)
+bool Config::convert(std::stringstream& data_stream)
{
#if 0
std::string keyword;
public:
Include(Converter* cv) : ConversionState(cv) {};
virtual ~Include() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
};
} // namespace
-bool Include::convert(std::stringstream& data_stream, std::ofstream&)
+bool Include::convert(std::stringstream& data_stream)
{
#if 0
std::string keyword;
public:
Output(Converter* cv) : ConversionState(cv) {};
virtual ~Output() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
};
} // namespace
-bool Output::convert(std::stringstream& data_stream, std::ofstream&)
+bool Output::convert(std::stringstream& data_stream)
{
std::string keyword;
public:
Preprocessor(Converter* cv) : ConversionState(cv) {};
virtual ~Preprocessor() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
};
} // namespace
-bool Preprocessor::convert(std::stringstream& data_stream, std::ofstream&)
+bool Preprocessor::convert(std::stringstream& data_stream)
{
std::string keyword;
public:
Suppress(Converter* cv) : ConversionState(cv) {};
virtual ~Suppress() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
};
} // namespace
-bool Suppress::convert(std::stringstream& data_stream, std::ofstream&)
+bool Suppress::convert(std::stringstream& data_stream)
{
#if 0
std::string keyword;
public:
Var(Converter* cv);
virtual ~Var() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
private:
bool first_line;
is_port_list = false;
}
-bool Var::convert(std::stringstream& data_stream, std::ofstream& out)
+bool Var::convert(std::stringstream& data_stream)
{
std::string ports;// converter->print_line(data_stream);
public:
HttpInspect(Converter* cv);
virtual ~HttpInspect() {};
- virtual bool convert(std::stringstream& data, std::ofstream&);
+ virtual bool convert(std::stringstream& data);
private:
+ void add_decode_option(std::string opt_name, int val);
+ bool missing_arg_error(std::string error_string);
+
bool first_line;
bool correct_keyword;
};
} // namespace
-HttpInspect::HttpInspect(Converter* cv) : ConversionState(cv)
+bool HttpInspect::missing_arg_error(std::string arg)
{
- first_line = true;
+ converter->add_comment_to_table("snort.conf missing argument for " + arg);
+ return false;
}
-bool HttpInspect::convert(std::stringstream& data_stream, std::ofstream&)
+HttpInspect::HttpInspect(Converter* cv) : ConversionState(cv)
+{}
+
+bool HttpInspect::convert(std::stringstream& data_stream)
{
std::string keyword;
- std::string value;
+ std::string s_value;
+ int i_value;
+
bool retval = true;;
- if (first_line)
+ if(data_stream >> keyword)
{
- if(data_stream >> keyword)
+ if(keyword.compare("global"))
{
- if(keyword.compare("global"))
- {
- converter->log_error("preprocessor httpinspect: requires the 'global' keyword");
- return false;
- }
+ converter->log_error("preprocessor httpinspect: requires the 'global' keyword");
+ return false;
}
- first_line = false;
- converter->open_table("http_inspect");
}
+ converter->open_table("http_inspect");
{
if(!keyword.compare("compress_depth"))
{
- if(data_stream >> value)
- {
- int c_depth = std::stoi(value);
- converter->add_option_to_table("compress_depth", c_depth);
- }
+ if(data_stream >> i_value)
+ converter->add_option_to_table("compress_depth", i_value);
else
- {
- converter->log_error("unable to find argument for 'preprocessor http_inspect: global compress_depth");
- retval = false;
- }
+ retval = missing_arg_error("compress_depth <int>");
}
+
else if(!keyword.compare("decompress_depth"))
{
- if(data_stream >> value)
- {
- int c_depth = std::stoi(value);
- converter->add_option_to_table("decompress_depth", c_depth);
- }
+ if(data_stream >> i_value)
+ converter->add_option_to_table("decompress_depth", i_value);
else
- {
- converter->log_error("unable to find argument for 'preprocessor http_inspect: global compress_depth");
- retval = false;
- }
+ retval = missing_arg_error("decompress_depth <int>");
}
- else if(!keyword.compare("detect_anomalous_servers")) {}
+
+ else if(!keyword.compare("detect_anomalous_servers"))
+ {
+ converter->add_option_to_table("detect_anomalous_servers", true);
+ }
+
else if(!keyword.compare("iis_unicode_map"))
{
std::string codemap;
- if( (data_stream >> value) &&
- (data_stream >> codemap))
+ if( (data_stream >> s_value) &&
+ (data_stream >> i_value))
{
- int code_map_i = std::stoi(codemap, nullptr);
converter->open_table("unicode_map");
- converter->add_option_to_table("map_file", value);
- converter->add_option_to_table("code_page", code_map_i);
+ converter->add_option_to_table("map_file", s_value);
+ converter->add_option_to_table("code_page", i_value);
converter->close_table();
}
else
{
- converter->log_error("Invalid argument: 'preprocessor http_inspect: global .. unicode_map");
- retval = false;
+ retval = missing_arg_error("iis_unicode_map <filename> <codemap>");
}
}
- else if(!keyword.compare("proxy_alert")) {}
- else if(!keyword.compare("max_gzip_mem")) {}
- else if(!keyword.compare("memcap")) {}
- else if(!keyword.compare("disabled")) {}
+ else if(!keyword.compare("proxy_alert"))
+ {
+ converter->add_option_to_table("proxy_alert", true);
+ }
+
+ else if(!keyword.compare("max_gzip_mem"))
+ {
+ if(data_stream >> i_value)
+ converter->add_option_to_table("max_gzip_mem", i_value);
+ else
+ retval = missing_arg_error("max_gzip_mem <int>");
+ }
+
+ else if(!keyword.compare("memcap"))
+ {
+ if(data_stream >> i_value)
+ converter->add_option_to_table("memcap", i_value);
+ else
+ retval = missing_arg_error("memcap <int>");
+ }
+
+ else if(!keyword.compare("disabled"))
+ {
+ converter->add_comment_to_table("the option 'disabled' is deprecated");
+ }
+
else if(!keyword.compare("b64_decode_depth"))
{
- if(data_stream >> value)
- {
- int c_depth = std::stoi(value);
- converter->add_option_to_table("b64_decode_depth", c_depth);
- }
+ if(data_stream >> i_value)
+ add_decode_option("b64_decode_depth", i_value);
else
- {
- converter->log_error("unable to find argument for 'preprocessor http_inspect: global b64_decode_depth");
- retval = false;
- }
+ retval = missing_arg_error("b64_decode_depth <int>");
}
+
else if(!keyword.compare("bitenc_decode_depth"))
{
- if(data_stream >> value)
- {
- int c_depth = std::stoi(value);
- converter->add_option_to_table("bitenc_decode_depth", c_depth);
- }
+ if(data_stream >> i_value)
+ add_decode_option("bitenc_decode_depth", i_value);
else
- {
- converter->log_error("unable to find argument for 'preprocessor http_inspect: global b64_decode_depth");
- retval = false;
- }
+ retval = missing_arg_error("b64_decode_depth <int>");
}
else if(!keyword.compare("max_mime_mem"))
{
- if(data_stream >> value)
- {
- int c_depth = std::stoi(value);
- converter->add_option_to_table("max_mime_mem", c_depth);
- }
+ if(data_stream >> i_value)
+ add_decode_option("max_mime_mem", i_value);
else
- {
- converter->log_error("unable to find argument for 'preprocessor http_inspect: global b64_decode_depth");
- retval = false;
- }
+ retval = missing_arg_error("max_mime_mem <int>");
}
+
else if(!keyword.compare("qp_decode_depth"))
{
- if(data_stream >> value)
- {
- int c_depth = std::stoi(value);
- converter->add_option_to_table("qp_decode_depth", c_depth);
- }
+ if(data_stream >> i_value)
+ add_decode_option("qp_decode_depth", i_value);
else
- {
- converter->log_error("unable to find argument for 'preprocessor http_inspect: global b64_decode_depth");
- retval = false;
- }
+ retval = missing_arg_error("qp_decode_depth <int>");
}
+
else if(!keyword.compare("uu_decode_depth"))
{
- if(data_stream >> value)
- {
- int c_depth = std::stoi(value);
- converter->add_option_to_table("uu_decode_depth", c_depth);
- }
+ if(data_stream >> i_value)
+ add_decode_option("uu_decode_depth", i_value);
else
- {
- converter->log_error("unable to find argument for 'preprocessor http_inspect: global b64_decode_depth");
- retval = false;
- }
+ retval = missing_arg_error("uu_decode_depth <int>");
}
+
else
{
- converter->log_error("unknown word");
+ converter->log_error("'preprocessor http_inspect: global' --> Invalid argument!!");
+ retval = false;
}
}
}
+void HttpInspect::add_decode_option(std::string opt_name, int val)
+{
+ converter->open_table("decode");
+ converter->add_option_to_table(opt_name, val);
+ converter->close_table();
+}
+
/**************************
******* A P I ***********
**************************/
static bool convert(std::ifstream& in, std::ofstream& out)
{
Converter cv;
- bool skip_line = false;
cv.reset_state();
-// state->inititalize();
-// Converter::reset_state();
+ std::string orig_text;
while(!in.eof())
{
- std::string next_line;
- bool last_line = true;
+ std::string tmp;
+ std::getline(in, tmp);
+ util::ltrim(tmp);
+ orig_text += ' ' + tmp;
+ util::rtrim(orig_text);
- std::getline(in, next_line);
- util::trim(next_line);
-
- if (next_line.empty())
+ if (orig_text.empty())
{
- out << std::endl;
+ cv.add_comment_to_file("");
}
- else if(!skip_line)
+ else
{
- if (next_line.back() == '\\')
+ if (orig_text.front() != '#' && orig_text.back() == '\\')
{
- last_line = false;
- next_line.pop_back();
- util::rtrim(next_line);
+ orig_text.pop_back();
+ util::rtrim(orig_text);
}
-
- std::stringstream data_stream(next_line);
- while(data_stream.tellg() != -1)
+ else
{
- if (!cv.convert_line(data_stream, out))
+ std::stringstream data_stream(orig_text);
+ while(data_stream.tellg() != -1)
{
- cv.log_error(next_line);
- data_stream.setstate(std::basic_ios<char>::eofbit);
- skip_line = true;
+ if (!cv.convert_line(data_stream))
+ {
+ cv.log_error("Failed to entirely convert: " + orig_text);
+// data_stream.setstate(std::basic_ios<char>::eofbit);
+ break;
+ }
}
- }
- }
- if (last_line)
- {
- cv.reset_state();
- skip_line = false;
+ orig_text.clear();
+ cv.reset_state();
+ }
}
}
return true;
}
+
static void show_usage()
{
std::cout << "usage: snort2lua <input_conf_file> <output_lua_file" << std::endl;