Logo Search packages:      
Sourcecode: nepenthes version File versions  Download package

int32_t Nepenthes::run ( int32_t  argc,
char **  argv 
) [virtual]

start nepenthes, using command line arguments.

Parameters:
argc number of arguments.
argv vector containing arguments.
Returns:
0 if the application was shut down poperly, non-null if an error occured.

Definition at line 158 of file Nepenthes.cpp.

References nepenthes::LogManager::addLogger(), changeGroup(), changeRoot(), changeUser(), nepenthes::DialogueFactoryManager::doList(), nepenthes::SubmitManager::doList(), nepenthes::SocketManager::doList(), nepenthes::EventManager::doList(), nepenthes::UploadManager::doList(), nepenthes::DownloadManager::doList(), nepenthes::DNSManager::doList(), nepenthes::ModuleManager::doList(), doLoop(), nepenthes::SubmitManager::Init(), nepenthes::UploadManager::Init(), nepenthes::DownloadManager::Init(), nepenthes::DNSManager::Init(), nepenthes::ModuleManager::Init(), nepenthes::SocketManager::Init(), nepenthes::LogManager::parseTagString(), nepenthes::LogManager::registerTag(), nepenthes::FileLogger::setLogFile(), nepenthes::RingFileLogger::setLogFileFormat(), nepenthes::RingFileLogger::setMaxFiles(), and nepenthes::RingFileLogger::setMaxSize().

{
      bool run=true;
      bool confcheck=false;
      bool filecheck=false;
      bool verbose=false;

      char *filecheckarg =NULL;
      char *confpath = SYSCONFDIR "/nepenthes/nepenthes.conf";
      char *basedir;
      char *workingdir = PREFIX;
      char *chUser = NULL;
      char *chGroup = NULL;
      char *chRoot = NULL;
      const char *consoleTags = 0, *diskTags = 0;


      string flpath;

      string rlpath;    // ringlogger path, gets read from config
      bool ringlog = false;

#ifdef WIN32

#else

      while( 1 )
      {
            int32_t option_index = 0;
            static struct option long_options[] = {
            { "config",             1, 0, 'c' },
                  { "disk-log",           1, 0, 'd' },      // FIXME
                  { "file-check",         1, 0, 'f' },      // FIXME
                  { "group",              1, 0, 'g' },      
                  { "help",               0, 0, 'h' },
                  { "large-help",         0, 0, 'H' },
                  { "info",               0, 0, 'i' },
                  { "check-config",       0, 0, 'k' },
                  { "log",                1, 0, 'l' },      // FIXME
                  { "logging-help", 0, 0, 'L' },      // FIXME
                  { "no-color",           0, 0, 'o' },      // FIXME
                  { "chroot",             1, 0, 'r' }, 
                  { "ringlog",            0, 0, 'R' }, 
                  { "user",               1, 0, 'u' },      
                  { "version",            0, 0, 'V' },
                  { "verbose",            0, 0, 'v' },
                  { "workingdir",   0, 0, 'w' },
                  { 0, 0, 0, 0 }
            };

            int32_t c = getopt_long(argc, argv, "c:d:f:g:hHikl:Lor:Ru:vVw:", long_options, &option_index);
            if (c == -1)
                  break;

            switch (c)
            {

            case 'b':
                  basedir = optarg;
                  break;

            case 'c':
                  confpath = optarg;
                  break;

            case 'd':   // FIXME set disk loglevel
                  diskTags = optarg;
                  break;

            case 'f':
//                fprintf(stderr,"filecheck\n");
                  filecheckarg = optarg;
                  filecheck = true;
                  run=false;
                  break;


            case 'g':
                  chGroup=optarg;
                  printf("Change Group to %s\n",chGroup);
            break;

            case 'h':
                  show_help(false);
                  run=false;
                  break;

            case 'H':
                  show_help(true);
                  run=false;
                  break;

            case 'i':
                  show_info();
                  run=false;
                  break;

            case 'l':   // FIXME set console loglevel
                  consoleTags = optarg;
                  break;

            case 'L':
                  show_loghelp();
                  run=false;
                  break;

            case 'k':
            run = false;
                  confcheck = true;
                  break;

            case 'o':   // FIXME set nocolor on console
                  printf("This feature '%c' is todo\nquitting\n",c);
                  run=false;
                  break;

            case 'r':
                  chRoot = optarg;
                  printf("Change Root to %s \n",chRoot);
                  break;

            case 'R':
                  ringlog = true;
                  printf("Using ringlogger instead of filelogger, rotating logfiles by myself\n");
                  break;

            case 'u':
            chUser = optarg;
                  printf("Change User to %s \n",chUser);
                  break;


            case 'v':
                  printf("DOING VERBOSE\n");
                  verbose = true;
                  break;

            case 'V':
                  show_version();
            run = false;
                  break;

            case 'w':
                  workingdir = optarg;
                  break;

            case '?':
            case ':':
                  exit(0);
                  break;

            default:
                  break;
            }
      }


      if( workingdir && chdir(workingdir) )
      {
            logCrit("Cannot change working diretory to %s: %s.\n", workingdir, strerror(errno));
            return -1;
      }
#endif


      // lookup the userid & groupid we have to switch to
      if ( chUser != NULL )
      {
            if ( changeUser(chUser) == false )
            {
                  run=false;
            }

      }
      if ( chGroup != NULL )
      {
            if ( changeGroup(chGroup) == false)
            {
                  run=false;
            }
      }



      if(run == true || confcheck == true || filecheck == true)
      {
            if (run == true)
            {
            show_logo();
                  show_version();
            }

            m_LogManager        = new LogManager();
            if (filecheck == false || verbose == true )
            {
                  m_LogManager->registerTag(l_crit,   "crit");
                  m_LogManager->registerTag(l_warn,   "warn");
                  m_LogManager->registerTag(l_debug,  "debug");
                  m_LogManager->registerTag(l_info,   "info");
                  m_LogManager->registerTag(l_spam,   "spam");
                  m_LogManager->registerTag(l_net,    "net");
                  m_LogManager->registerTag(l_script, "script");
                  m_LogManager->registerTag(l_shell,  "shell");
                  m_LogManager->registerTag(l_mem,    "mem");
                  m_LogManager->registerTag(l_sc,     "sc");
                  m_LogManager->registerTag(l_dl,     "down");
                  m_LogManager->registerTag(l_mgr,    "mgr");
                  m_LogManager->registerTag(l_hlr,    "handler");
                  m_LogManager->registerTag(l_dia,    "dia");
                  m_LogManager->registerTag(l_sub,    "submit");
                  m_LogManager->registerTag(l_ev,     "event");
                  m_LogManager->registerTag(l_mod,    "module");
                  m_LogManager->registerTag(l_stdtag, "fixme");

                  if( consoleTags )
                        m_LogManager->addLogger(new ConsoleLogger(m_LogManager), m_LogManager->parseTagString(consoleTags));
                  else
                        m_LogManager->addLogger(new ConsoleLogger(m_LogManager), l_all);
            }


            if ( run == true || filecheck == true)
            {
            m_DialogueFactoryManager = new DialogueFactoryManager(this);

                  m_DownloadManager   = new DownloadManager(this);
                  m_EventManager      = new EventManager(this);

#ifdef HAVE_GEOLOCATION
                  m_GeoLocationManager = new GeoLocationManager(this);
#endif 

                  m_UploadManager         = new UploadManager(this);
                  //    m_Lua                   = new Lua
                  m_ModuleManager     = new ModuleManager(this);
                  m_ShellcodeManager  = new ShellcodeManager(this);
                  m_SocketManager     = new SocketManager(this);
                  m_SubmitManager     = new SubmitManager(this);
                  m_Utilities         = new Utilities();
                  m_DNSManager        = new DNSManager(this);
            }
      }


      if ( run == true || confcheck == true || filecheck == true)
      {
        m_Config = new Config;
            logInfo("Trying to load Nepenthes Configuration from %s \n",confpath);
            try
            {
                  m_Config->load(confpath);
                  logInfo("Done loading Nepenthes Configuration from %s \n",confpath);
            } catch ( LoadError e )
            {
                  printf("Unable to load configuration file %s: %s\n", confpath, e.getMessage());
                  run = false;
            } catch ( ParseError e )
            {
                  printf("Parse error in %s on line %d: %s\n", confpath, e.getLine(), e.getMessage());
                  run = false;
            }
            
      }

    if ( run == true )
    {
            if ( m_Config != NULL )
            {
                  


                  if (ringlog == true)
                  {

                        try
                        {
                              rlpath = m_Config->getValString("nepenthes.logmanager.ring_logging_file");
                        } catch ( ... )
                        {
                              logCrit("%s","Could not find nepenthes.logmanager.ring_logging_file in Config\n");
                              run = false;
                        }


                        RingFileLogger *fl = new RingFileLogger(m_LogManager);

                        fl->setLogFileFormat((char *)rlpath.c_str());
                        fl->setMaxFiles(5);
                        fl->setMaxSize(1024 * 1024);

                        if ( diskTags )
                              m_LogManager->addLogger(fl, m_LogManager->parseTagString(diskTags));
                        else
                              m_LogManager->addLogger(fl, l_all);

                  }else
                  {
                        try
                        {
                              flpath = m_Config->getValString("nepenthes.logmanager.file_logging_file");
                        } catch ( ... )
                        {
                              logCrit("%s","Could not find nepenthes.logmanager.file_logging_file in Config\n");
                              run = false;
                        }

                        FileLogger *fl = new FileLogger(m_LogManager);
                        fl->setLogFile(flpath.c_str());
                        if ( diskTags )
                              m_LogManager->addLogger(fl, m_LogManager->parseTagString(diskTags));
                        else
                              m_LogManager->addLogger(fl, l_all);

                  }
            }
      }


      if (run == true || filecheck == true)
      {

            if (filecheck == true)
            {
                  run = true; 
            }

        // socketManager will call WASStartup()
            run = m_SocketManager->Init();

            

            if (run == true )
            {
                  run = m_ModuleManager->Init();
                  m_ModuleManager->doList();
            }

            if (run == true )
            {
                  run = m_DNSManager->Init();
                  m_DNSManager->doList();
            }

#ifdef HAVE_GEOLOCATION
            if (run == true )
            {
                  run = m_GeoLocationManager->Init();
            }
#endif

            if (run == true )
            {
                  run = m_DownloadManager->Init();
                  m_DownloadManager->doList();
            }

            if (run == true )
            {
                  run = m_UploadManager->Init();
                  m_UploadManager->doList();
            }

            if (run == true )
            {
                  m_EventManager->doList();
            }

            if (run == true )
            {
                  m_ShellcodeManager->doList();
            }

            if (run == true )
            {
            m_SocketManager->doList();
            }

            if (run == true )
            {
                  run = m_SubmitManager->Init();
                  m_SubmitManager->doList();
            }

            if (run == true )
            {
                  m_DialogueFactoryManager->doList();
            }


            if (filecheck == true )
            {
                  if (run == true)
                  {
                        run = false; 
                  }else
                  {
                        filecheck = false;
                  }
            }
      }

      if ( chRoot != NULL )
      {
            if ( changeRoot(chRoot) == false )
            {
                  run = false;
            }
      }


      // if we drop priviliges, we have to take care of the logfiles user/group permission
      // if we do not drop privs, make sure the files are ours
      // --common
      if ( run == true )
      {
            if ( ringlog == true )
            {

                  uint16_t numrot=0;
                  for ( numrot = 0;numrot < 5; numrot++ )
                  {
                        char *lp=0;
                        asprintf(&lp,rlpath.c_str(),numrot);

                        struct stat st;
                        int32_t filestat = stat(lp, &st);

                        if ( filestat != 0 )
                        {
                              if ( errno == ENOENT )
                              {
                                    logInfo("logfile %s does not exist yet\n",lp);
                                    continue;
                              } else
                              {
                                    logCrit("Could not access logfile %s '%s'\n",lp, strerror(errno));
                                    run=false;
                              }
                        } else
                        {
                              if ( chown(lp,m_UID, m_GID) != 0 )
                              {
                                    logCrit("Could not chown logfile %s '%s'\n",lp, strerror(errno));
                                    run=false;
                              } else
                              {
                                    char *curUser, *curGroup;
                                    if ( chUser != NULL )
                                    {
                                          curUser = chUser;
                                    } else
                                    {
                                          curUser = getpwuid(geteuid())->pw_name;
                                    }

                                    if ( chGroup != NULL )
                                    {
                                          curGroup = chGroup;
                                    } else
                                    {
                                          curGroup = getgrgid(geteuid())->gr_name;
                                    }

                                    logInfo("Changed logfile %s owner to %i:%i (%s:%s)\n",lp, m_UID,m_GID,curUser,curGroup);
                              }
                        }
                        free(lp);
                  }

            }else
            {
                  struct stat st;
                  int32_t filestat = stat(flpath.c_str(), &st);

                  if ( filestat != 0 )
                  {
                        if ( errno == ENOENT )
                        {
                              logInfo("logfile %s does not exist yet\n",flpath.c_str());
                        } else
                        {
                              logCrit("Could not access logfile %s '%s'\n",flpath.c_str(), strerror(errno));
                              run=false;
                        }
                  } else
                  {
                        if ( chown(flpath.c_str(),m_UID, m_GID) != 0 )
                        {
                              logCrit("Could not chown logfile %s '%s'\n",flpath.c_str(), strerror(errno));
                              run=false;
                        } else
                        {
                              char *curUser, *curGroup;
                              if ( chUser != NULL )
                              {
                                    curUser = chUser;
                              } else
                              {
                                    curUser = getpwuid(geteuid())->pw_name;
                              }

                              if ( chGroup != NULL )
                              {
                                    curGroup = chGroup;
                              } else
                              {
                                    curGroup = getgrgid(geteuid())->gr_name;
                              }

                              logInfo("Changed logfile %s owner to %i:%i (%s:%s)\n",flpath.c_str(), m_UID,m_GID,curUser,curGroup);
                        }
                  }
            }
      }

      // change process group id
      if ( run == true && chGroup != NULL )
      {

            if ( changeGroup() == false )
            {
                  run=false;
            }
      }

      // change process user id
      if ( run == true && chUser != NULL )
      {
            if ( changeUser() == false )
            {
                  run=false;
            }
      }

      if( run == true )
      {
        doLoop();
      }else
      if (filecheck)
      {
            show_version();
            fileCheckMain(filecheckarg,argc,optind,argv);
      }


      if (m_Config != NULL)
      {
      delete m_Config;
      }
    return 0;
}


Generated by  Doxygen 1.6.0   Back to index