Changeset 70f2d7b in nscp


Ignore:
Timestamp:
03/29/06 22:19:32 (7 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
76aafc4
Parents:
aabbd97
Message:

Updated with reg settings (take 1)

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • NSClient++.cpp

    rf42280d r70f2d7b  
    2020#include <Socket.h> 
    2121#include <b64/b64.h> 
     22#include <config.h> 
    2223 
    2324 
     
    6566      g_bConsoleLog = true; 
    6667      std::string password; 
    67       Settings::getInstance()->setFile(mainClient.getBasePath() + "NSC.ini"); 
     68      try { 
     69        Settings::getInstance()->setFile(mainClient.getBasePath() + "NSC.ini"); 
     70      } catch (SettingsException e) { 
     71        std::cout << "Could not find settings: " << e.getMessage() << std::endl;; 
     72        return 1; 
     73      } 
    6874      std::cout << "Enter password to encrypt (has to be a single word): "; 
    6975      std::cin >> password; 
     
    97103#endif 
    98104      g_bConsoleLog = true; 
    99       mainClient.InitiateService(); 
     105      if (!mainClient.InitiateService()) { 
     106        LOG_ERROR_STD("Service *NOT* started!"); 
     107        return -1; 
     108      } 
     109      LOG_MESSAGE_STD("Using settings from: " + Settings::getInstance()->getActiveType()); 
    100110      LOG_MESSAGE("Enter command to inject or exit to terminate..."); 
    101111      std::string s = ""; 
     
    146156 * When the program is started as a service this will be the entry point. 
    147157 */ 
    148 void NSClientT::InitiateService(void) { 
    149   Settings::getInstance()->setFile(getBasePath() + "NSC.ini"); 
    150  
     158bool NSClientT::InitiateService(void) { 
     159  try { 
     160    Settings::getInstance()->setFile(getBasePath() + "NSC.ini"); 
     161  } catch (SettingsException e) { 
     162    LOG_ERROR_STD("Could not find settings: " + e.getMessage()); 
     163    return false; 
     164  } 
    151165  try { 
    152166    simpleSocket::WSAStartup(); 
    153167  } catch (simpleSocket::SocketException e) { 
    154168    LOG_ERROR_STD("Uncaught exception: " + e.getMessage()); 
     169    return false; 
    155170  } 
    156171 
     
    161176    } catch(const NSPluginException& e) { 
    162177      LOG_ERROR_STD("Exception raised: " + e.error_ + " in module: " + e.file_); 
     178      return false; 
    163179    } 
    164180  } 
    165181  loadPlugins(); 
     182  return true; 
    166183} 
    167184/** 
  • NSClient++.h

    r6817602 r70f2d7b  
    5353 
    5454  // Service helper functions 
    55   void InitiateService(void); 
     55  bool InitiateService(void); 
    5656  void TerminateService(void); 
    5757  static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv); 
  • NSClient++.sln

    r6817602 r70f2d7b  
    119119    {BD93F0C3-E342-4D68-9717-FCAC2E7189AA}.Distribution.Build.0 = Debug|Win32 
    120120    {BD93F0C3-E342-4D68-9717-FCAC2E7189AA}.Release.ActiveCfg = Release|Win32 
    121     {BD93F0C3-E342-4D68-9717-FCAC2E7189AA}.Release.Build.0 = Release|Win32 
    122121    {E6E588AB-EFEF-481C-9AF7-DCDCB95CFF45}.Debug.ActiveCfg = Debug|Win32 
    123122    {E6E588AB-EFEF-481C-9AF7-DCDCB95CFF45}.Debug.Build.0 = Debug|Win32 
  • include/INISettings.h

    raabbd97 r70f2d7b  
    2525  { 
    2626  } 
     27  std::string getActiveType() { 
     28    return "INI-file"; 
     29  } 
    2730 
    2831  static bool hasSettings(std::string file) { 
    29     std::cout << GetPrivateProfileInt(MAIN_SECTION_TITLE, MAIN_USEFILE, MAIN_USEFILE_DEFAULT, file.c_str()) << ":" << file << std::endl; 
    3032    return GetPrivateProfileInt(MAIN_SECTION_TITLE, MAIN_USEFILE, MAIN_USEFILE_DEFAULT, file.c_str()) == 1; 
    3133  } 
  • include/REGSettings.h

    raabbd97 r70f2d7b  
    66#define BUFF_LEN 4096 
    77 
     8#include <iostream> 
    89class REGSettings : public TSettings 
    910{ 
     
    1920 
    2021  static bool hasSettings() { 
    21     // @todo 
    22     return false; 
     22    return getInt_(NS_HKEY_ROOT, NS_REG_ROOT, "use_reg", 0) == 1; 
     23  } 
     24 
     25  std::string getActiveType() { 
     26    return "registry"; 
    2327  } 
    2428 
    2529  sectionList getSections(unsigned int bufferLength = BUFF_LEN) { 
    26     sectionList ret; 
    27     return ret; 
     30    return getSubKeys_(NS_HKEY_ROOT, NS_REG_ROOT); 
    2831  } 
    2932 
     
    3437  */ 
    3538  sectionList getSection(std::string section, unsigned int bufferLength = BUFF_LEN) { 
    36     sectionList ret; 
    37     return ret; 
     39    return getValues_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str()); 
    3840  } 
    3941  /** 
     
    4547  */ 
    4648  std::string getString(std::string section, std::string key, std::string defaultValue = "") const { 
    47     std::string ret; 
    48     return ret; 
     49    return getString_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str(), key.c_str(), defaultValue); 
    4950  } 
    5051 
     
    6061  */ 
    6162  int getInt(std::string section, std::string key, int defaultValue = 0) { 
    62     return 0; 
     63    return getInt_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str(), key.c_str(), defaultValue); 
    6364  } 
    6465  void setInt(std::string section, std::string key, int value) { 
    6566  } 
     67 
     68 
     69  static std::string getString_(HKEY hKey, LPCTSTR lpszPath, LPCTSTR lpszKey, std::string def) { 
     70    std::string ret = def; 
     71    HKEY hTemp; 
     72    if (RegOpenKeyEx(hKey, lpszPath, 0, KEY_QUERY_VALUE, &hTemp) != ERROR_SUCCESS) { 
     73      return def; 
     74    } 
     75    DWORD type; 
     76    DWORD cbData = 1024; 
     77    BYTE *bData = new BYTE[cbData]; 
     78    BOOL bRet = RegQueryValueEx(hTemp, lpszKey, NULL, &type, bData, &cbData); 
     79    if (type != REG_SZ) { 
     80      bRet = false; 
     81    } 
     82    RegCloseKey(hTemp); 
     83    if (bRet) { 
     84      ret = (LPCTSTR)bData; 
     85    } 
     86    delete [] bData; 
     87    return ret; 
     88  } 
     89  static DWORD getInt_(HKEY hKey, LPCTSTR lpszPath, LPCTSTR lpszKey, DWORD def) { 
     90    DWORD ret = def; 
     91    LONG bRet; 
     92    HKEY hTemp; 
     93    if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) { 
     94      return def; 
     95    } 
     96    DWORD type; 
     97    DWORD cbData = 1024; 
     98    BYTE *bData = new BYTE[sizeof(DWORD)]; 
     99    bRet = RegQueryValueEx(hTemp, lpszKey, NULL, &type, bData, &cbData); 
     100    if (type != REG_DWORD) { 
     101      bRet = -1; 
     102    } 
     103    RegCloseKey(hTemp); 
     104    if (bRet == ERROR_SUCCESS) { 
     105      ret = (DWORD)*bData; 
     106    } 
     107    delete [] bData; 
     108    return ret; 
     109  } 
     110  static sectionList getValues_(HKEY hKey, LPCTSTR lpszPath) { 
     111    sectionList ret; 
     112    LONG bRet; 
     113    HKEY hTemp; 
     114    if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) { 
     115      return ret; 
     116    } 
     117    DWORD    cValues=0; 
     118    DWORD    cMaxValLen; 
     119    // Get the class name and the value count.  
     120    bRet = RegQueryInfoKey(hTemp,NULL,NULL,NULL,NULL,NULL,NULL,&cValues,&cMaxValLen,NULL,NULL,NULL); 
     121    if ((bRet == ERROR_SUCCESS)&&(cValues>0)) { 
     122      TCHAR *lpValueName = new TCHAR[cMaxValLen+1]; 
     123      for (unsigned int i=0; i<cValues; i++) { 
     124        DWORD len = cMaxValLen; 
     125        bRet = RegEnumValue(hKey, i, lpValueName, &len, NULL, NULL, NULL, NULL); 
     126        if (bRet == ERROR_SUCCESS) { 
     127          ret.push_back(std::string(lpValueName)); 
     128        } 
     129      } 
     130      delete [] lpValueName; 
     131    } 
     132    return ret; 
     133  } 
     134  static sectionList getSubKeys_(HKEY hKey, LPCTSTR lpszPath) { 
     135    sectionList ret; 
     136    LONG bRet; 
     137    HKEY hTemp; 
     138    if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) { 
     139      return ret; 
     140    } 
     141    DWORD    cSubKeys=0; 
     142    DWORD    cMaxKeyLen; 
     143    // Get the class name and the value count.  
     144    bRet = RegQueryInfoKey(hTemp,NULL,NULL,NULL,&cSubKeys,&cMaxKeyLen,NULL,NULL,NULL,NULL,NULL,NULL); 
     145    if ((bRet == ERROR_SUCCESS)&&(cSubKeys>0)) { 
     146      TCHAR *lpValueName = new TCHAR[cMaxKeyLen+1]; 
     147      for (unsigned int i=0; i<cSubKeys; i++) { 
     148        DWORD len = cMaxKeyLen; 
     149        bRet = RegEnumKey(hKey, i, lpValueName, len); 
     150        if (bRet == ERROR_SUCCESS) { 
     151          ret.push_back(std::string(lpValueName)); 
     152        } 
     153      } 
     154      delete [] lpValueName; 
     155    } 
     156    return ret; 
     157  } 
    66158}; 
  • include/Settings.h

    raabbd97 r70f2d7b  
    1010 
    1111class SettingsException { 
     12private: 
     13  std::string err; 
    1214public: 
    13   SettingsException(std::string str) {} 
     15  SettingsException(std::string str) : err(str) {} 
     16  std::string getMessage() { 
     17    return err; 
     18  } 
    1419 
    1520}; 
     
    4146      delete settingsManager; 
    4247  } 
     48  std::string getActiveType() { 
     49    if (!settingsManager) { 
     50      return ""; 
     51    } return settingsManager->getActiveType(); 
     52  } 
    4353 
    4454  /** 
  • include/TSettings.h

    raabbd97 r70f2d7b  
    1616  { 
    1717  } 
    18  
     18  virtual std::string getActiveType() = 0; 
    1919  virtual sectionList getSections(unsigned int bufferLength = BUFF_LEN) = 0; 
    2020  virtual sectionList getSection(std::string section, unsigned int bufferLength = BUFF_LEN) = 0; 
  • include/config.h

    raabbd97 r70f2d7b  
    105105#define MAIN_ALLOWED_HOSTS "allowed_hosts" 
    106106#define MAIN_ALLOWED_HOSTS_DEFAULT "127.0.0.1" 
     107 
     108 
     109// Main Registry ROOT 
     110#define NS_HKEY_ROOT HKEY_LOCAL_MACHINE 
     111#define NS_REG_ROOT "SOFTWARE\\NSClient++" 
Note: See TracChangeset for help on using the changeset viewer.