The Poor Man's (or Woman's) Intrusion Detection System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

114 lines
3.0 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // Base CLI app classes for TrafficMon tools
  3. // Written by Jonathan A. Foster <jon@jfpossibilities.com>
  4. // Started December 29th, 2021
  5. // Copyright JF Possibilities, Inc. All rights reserved.
  6. //////////////////////////////////////////////////////////////////////
  7. #include <stdexcept>
  8. #include <iostream>
  9. #include <libgen.h>
  10. #include "appbase.h"
  11. //////////////////////////////////////////////////////////////////////
  12. // TrafficMonBaseApp
  13. //////////////////////////////////////////////////////////////////////
  14. cBaseApp &TrafficMonBaseApp::init(int argc, char **argv) {
  15. if(!config) config = new MonitorBaseConf;
  16. return cBaseApp::init(argc, argv);
  17. }
  18. unsigned TrafficMonBaseApp::do_switch(const char *arg) {
  19. if(!arg[1] && *arg=='c') return 1;
  20. return cBaseApp::do_switch(arg);
  21. }
  22. void TrafficMonBaseApp::do_switch_arg(const char *sw, const std::string &val) {
  23. if(!sw[1] && *sw=='c') config->load(val);
  24. }
  25. int TrafficMonBaseApp::main() {
  26. int x;
  27. try {
  28. if(x=cBaseApp::main()) return x; // Parse CLI args
  29. if(!config->traffic_mon.vals.size()) throw CLIerror(
  30. "You need to load a config file with a [Traffic Mon] section"
  31. );
  32. } catch(const CLIerror &e) {
  33. std::cerr << e.what() << "\n\n";
  34. return help();
  35. }
  36. db.open("mysql:user="+qesc(config->traffic_mon.get("db user"))+
  37. ";password="+qesc(config->traffic_mon.get("db password"))+
  38. ";host="+qesc(config->traffic_mon.get("db host"))+
  39. ";database="+qesc(config->traffic_mon.get("db name"))+
  40. ";@opt_reconnect=1");
  41. return 0;
  42. }
  43. TrafficMonBaseApp::~TrafficMonBaseApp() {
  44. if(config) delete(config);
  45. }
  46. //////////////////////////////////////////////////////////////////////
  47. // BlackListBaseApp
  48. //////////////////////////////////////////////////////////////////////
  49. int BlackListBaseApp::help() {
  50. std::cerr << " FORMAT: " << basename(command_args[0]) << " -c {config} [-4 {address}] [-6 {address}]\n"
  51. << '\n'
  52. << "The config file must have a [Traffic Mon] section with the database\n"
  53. << "credentials in it. -4 & -6 set the addresses to pin blocked names to.\n"
  54. << "They default to the 'localhost' address in the respective family. Set\n"
  55. << "to '' to turn off output of that family." << std::endl;
  56. return ExitCode = 1;
  57. }
  58. unsigned BlackListBaseApp::do_switch(const char *arg) {
  59. if(!arg[1] && (*arg=='4' || *arg=='6')) return 1;
  60. return TrafficMonBaseApp::do_switch(arg);
  61. }
  62. void BlackListBaseApp::do_switch_arg(const char *sw, const std::string &val) {
  63. if(!sw[1]) switch(*sw) {
  64. case '4': ipv4 = val; return;
  65. case '6': ipv6 = val; return;
  66. }
  67. TrafficMonBaseApp::do_switch_arg(sw, val);
  68. }
  69. void BlackListBaseApp::do_arg(const char *arg) {
  70. throw CLIerror("Invalid arguments");
  71. }
  72. int BlackListBaseApp::main() {
  73. int x;
  74. if(x=TrafficMonBaseApp::main()) return x; // Parse CLI args, open conf & db
  75. if(ipv4=="" && ipv6=="") {
  76. std::cerr << "All address families turned off. Nothing to do." << std::endl;
  77. return 1;
  78. }
  79. return 0;
  80. }