Changeset b7ed6ac in nscp
- Timestamp:
- 03/21/08 18:15:51 (5 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2, stable
- Children:
- 367bf20
- Parents:
- dd02c15
- Files:
-
- 7 edited
-
changelog (modified) (2 diffs)
-
include/EnumProcess.cpp (modified) (10 diffs)
-
include/EnumProcess.h (modified) (7 diffs)
-
include/error.hpp (modified) (1 diff)
-
include/filter_framework.hpp (modified) (1 diff)
-
modules/CheckSystem/CheckSystem.cpp (modified) (13 diffs)
-
modules/LUAScript/script_wrapper.hpp (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
changelog
rdd02c15 rb7ed6ac 6 6 * "The message is blocked by User Interface Privilege Isolation, Administrative applications that need to see it can allow it through by calling ChangeWindowMessageFilter after making sure the necessary security precautions are in place. " 7 7 8 2007-03-20 8 2008-03-21 9 + Added command line support for process checks 10 New option: cmdLine will toggle so full command lines are used instead of just process names. 11 + Added regular expression matching to process checks 12 New option: match=regexp (match=strings is the default and "old" way) 13 + Added substring matching to process checks 14 New option: match=substr (match=strings is the default and "old" way) 15 This is *NOT* case blind so might be hard to use, plan to add case blindness to it in the future. 16 : Sample command: check_nt ... -v PROCSTATE -l cmdLine,match=regexp,.*exp.* -d SHOWALL 17 * Ohh yeah... it is 2008 this year... not 2007, fixed a few entries in the changelog :) 18 - BREAKING CHANGE! -- Removed TOOLHELPER API as PSAPI is simpler and toolhel is really only usefull on w9x (which I dont oficcaly support) 19 20 2008-03-20 9 21 + Added host-lookupos for NSCA server (#149) 10 22 + Added option (cache_hostname=1|0) to cache the NSCA host name (Ie. only lookup once) … … 13 25 Added option debug_skip_data_collection to simulate this (just for kicks) 14 26 15 200 7-03-1827 2008-03-18 16 28 * Added some more error mesages to the NSCA module 17 29 * Added support for srguments to LUA module. 18 30 syntax: function debug (command, args) -- args is a table with all arguments 19 31 20 200 7-03-11 MickeM32 2008-03-11 MickeM 21 33 ! 0.3.1 Released 22 34 -
include/EnumProcess.cpp
r978bd31 rb7ed6ac 34 34 { 35 35 lpString = new TCHAR[MAX_FILENAME+1]; 36 m_hProcessSnap = INVALID_HANDLE_VALUE;37 m_hModuleSnap = INVALID_HANDLE_VALUE;38 36 39 37 PSAPI = ::LoadLibrary(_TEXT("PSAPI")); … … 55 53 } 56 54 57 TOOLHELP = ::LoadLibrary(_TEXT("Kernel32"));58 if (TOOLHELP)59 {60 // Setup variables61 m_pe.dwSize = sizeof(m_pe);62 m_me.dwSize = sizeof(m_me);63 // Find ToolHelp functions64 #ifdef UNICODE65 FCreateToolhelp32Snapshot = (PFCreateToolhelp32Snapshot)::GetProcAddress(TOOLHELP, "CreateToolhelp32Snapshot");66 FProcess32First = (PFProcess32First)::GetProcAddress(TOOLHELP, "Process32FirstW");67 FProcess32Next = (PFProcess32Next)::GetProcAddress(TOOLHELP, "Process32NextW");68 FModule32First = (PFModule32First)::GetProcAddress(TOOLHELP, "Module32FirstW");69 FModule32Next = (PFModule32Next)::GetProcAddress(TOOLHELP, "Module32NextW");70 #else71 FCreateToolhelp32Snapshot = (PFCreateToolhelp32Snapshot)::GetProcAddress(TOOLHELP, "CreateToolhelp32SnapshotA");72 FProcess32First = (PFProcess32First)::GetProcAddress(TOOLHELP, "Process32FirstA");73 FProcess32Next = (PFProcess32Next)::GetProcAddress(TOOLHELP, "Process32NextA");74 FModule32First = (PFModule32First)::GetProcAddress(TOOLHELP, "Module32FirstA");75 FModule32Next = (PFModule32Next)::GetProcAddress(TOOLHELP, "Module32NextA");76 #endif77 }78 79 55 // Find the preferred method of enumeration 80 56 m_method = ENUM_METHOD::NONE; 81 57 int method = GetAvailableMethods(); 82 58 if (method == (method|ENUM_METHOD::PSAPI)) m_method = ENUM_METHOD::PSAPI; 83 if (method == (method|ENUM_METHOD::TOOLHELP)) m_method = ENUM_METHOD::TOOLHELP;84 if (method == (method|ENUM_METHOD::PROC16)) m_method += ENUM_METHOD::PROC16;85 59 86 60 } … … 92 66 if (m_pModules) {delete[] m_pModules;} 93 67 if (PSAPI) FreeLibrary(PSAPI); 94 if (TOOLHELP) FreeLibrary(TOOLHELP); 95 if (INVALID_HANDLE_VALUE != m_hProcessSnap) ::CloseHandle(m_hProcessSnap); 96 if (INVALID_HANDLE_VALUE != m_hModuleSnap) ::CloseHandle(m_hModuleSnap); 97 } 98 99 100 101 int CEnumProcess::GetAvailableMethods() 102 { 68 } 69 70 71 72 int CEnumProcess::GetAvailableMethods() { 103 73 int res = 0; 104 74 // Does all psapi functions exist? 105 75 if (PSAPI&&FEnumProcesses&&FEnumProcessModules&&FGetModuleFileNameEx) 106 76 res += ENUM_METHOD::PSAPI; 107 // How about Toolhelp?108 if (TOOLHELP&&FCreateToolhelp32Snapshot&&FProcess32Next&&FProcess32Next&&FModule32First&&FModule32Next)109 res += ENUM_METHOD::TOOLHELP;110 111 77 return res; 112 78 } 113 79 114 int CEnumProcess::SetMethod(int method) 115 { 80 int CEnumProcess::SetMethod(int method) { 116 81 int avail = GetAvailableMethods(); 117 118 if (method != ENUM_METHOD::PROC16 && avail == (method|avail)) 82 if (avail == (method|avail)) 119 83 m_method = method; 120 121 84 return m_method; 122 85 } … … 131 94 BOOL CEnumProcess::GetProcessFirst(CEnumProcess::CProcessEntry *pEntry) 132 95 { 133 if (ENUM_METHOD::NONE == m_method) return FALSE; 134 135 if ((ENUM_METHOD::TOOLHELP|m_method) == m_method) 136 // Use ToolHelp functions 137 // ---------------------- 138 { 139 m_hProcessSnap = FCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 140 if (INVALID_HANDLE_VALUE == m_hProcessSnap) return FALSE; 141 if (!FProcess32First(m_hProcessSnap, &m_pe)) return FALSE; 142 pEntry->dwPID = m_pe.th32ProcessID; 143 pEntry->sFilename, m_pe.szExeFile; 144 } 145 else 96 if (ENUM_METHOD::NONE == m_method) { 97 return FALSE; 98 } else if ((ENUM_METHOD::PSAPI|m_method) == m_method) { 146 99 // Use PSAPI functions 147 100 // ---------------------- 148 {149 101 if (m_pProcesses) {delete[] m_pProcesses;} 150 102 m_pProcesses = new DWORD[m_MAX_COUNT]; … … 163 115 m_cProcesses = cbNeeded/sizeof(DWORD); 164 116 return FillPStructPSAPI(*m_pProcesses, pEntry); 165 } 166 117 } else { 118 return FALSE; 119 } 167 120 return TRUE; 168 121 } … … 173 126 { 174 127 if (ENUM_METHOD::NONE == m_method) return FALSE; 175 pEntry->hTask16 = 0;176 177 128 178 129 // Use ToolHelp functions 179 130 // ---------------------- 180 if ((ENUM_METHOD::TOOLHELP|m_method) == m_method) 181 { 182 if (!FProcess32Next(m_hProcessSnap, &m_pe)) return FALSE; 183 pEntry->dwPID = m_pe.th32ProcessID; 184 pEntry->sFilename = m_pe.szExeFile; 185 } 186 else 131 if ((ENUM_METHOD::PSAPI|m_method) == m_method) { 187 132 // Use PSAPI functions 188 133 // ---------------------- 189 {190 134 if (--m_cProcesses <= 0) return FALSE; 191 135 FillPStructPSAPI(*++m_pCurrentP, pEntry); 192 } 193 136 } else { 137 return FALSE; 138 } 194 139 return TRUE; 195 140 } … … 199 144 { 200 145 if (ENUM_METHOD::NONE == m_method) return FALSE; 201 // Use ToolHelp functions 202 // ---------------------- 203 if ((ENUM_METHOD::TOOLHELP|m_method) == m_method) 204 { 205 if (INVALID_HANDLE_VALUE != m_hModuleSnap) ::CloseHandle(m_hModuleSnap); 206 m_hModuleSnap = FCreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID); 207 208 if(!FModule32First(m_hModuleSnap, &m_me)) return FALSE; 209 210 pEntry->pLoadBase = m_me.modBaseAddr; 211 pEntry->sFilename = m_me.szExePath; 212 pEntry->pPreferredBase = GetModulePreferredBase(dwPID, m_me.modBaseAddr); 213 return TRUE; 214 } 215 else 146 if ((ENUM_METHOD::PSAPI|m_method) == m_method) { 216 147 // Use PSAPI functions 217 148 // ---------------------- 218 {219 149 if (m_pModules) {delete[] m_pModules;} 220 150 m_pModules = new HMODULE[m_MAX_COUNT]; … … 240 170 } 241 171 return FALSE; 172 } else { 173 return FALSE; 242 174 } 243 175 } … … 247 179 { 248 180 if (ENUM_METHOD::NONE == m_method) return FALSE; 249 250 // Use ToolHelp functions 251 // ---------------------- 252 if ((ENUM_METHOD::TOOLHELP|m_method) == m_method) 253 { 254 if(!FModule32Next(m_hModuleSnap, &m_me)) return FALSE; 255 256 pEntry->pLoadBase = m_me.modBaseAddr; 257 pEntry->sFilename = m_me.szExePath; 258 pEntry->pPreferredBase = GetModulePreferredBase(dwPID, m_me.modBaseAddr); 259 return TRUE; 260 } 261 else 181 if ((ENUM_METHOD::PSAPI|m_method) == m_method) { 262 182 // Use PSAPI functions 263 183 // ---------------------- 264 {265 184 if (--m_cModules <= 0) return FALSE; 266 185 return FillMStructPSAPI(dwPID, *++m_pCurrentM, pEntry); 267 } 268 269 } 270 186 } else { 187 return FALSE; 188 } 189 190 } 191 192 193 BOOL CEnumProcess::EnableTokenPrivilege (LPTSTR privilege) 194 { 195 HANDLE hToken; 196 TOKEN_PRIVILEGES token_privileges; 197 DWORD dwSize; 198 ZeroMemory (&token_privileges, sizeof (token_privileges)); 199 token_privileges.PrivilegeCount = 1; 200 if ( !OpenProcessToken (GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken)) 201 return FALSE; 202 if (!LookupPrivilegeValue ( NULL, privilege, &token_privileges.Privileges[0].Luid)) 203 { 204 CloseHandle (hToken); 205 return FALSE; 206 } 207 208 token_privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 209 if (!AdjustTokenPrivileges ( hToken, FALSE, &token_privileges, 0, NULL, &dwSize)) 210 { 211 CloseHandle (hToken); 212 return FALSE; 213 } 214 CloseHandle (hToken); 215 return TRUE; 216 } 217 218 // Process data block is found in an NT machine. 219 // on an Intel system at 0x00020000 which is the 32 220 // memory page. At offset 0x0498 is what I believe to be 221 // the process' startup directory which is followed by 222 // the system's PATH. Next is process full command 223 // followed by the exe name. 224 #define PROCESS_DATA_BLOCK_ADDRESS (LPVOID)0x00020498 225 // align pointer 226 #define ALIGNMENT(x) ( (x & 0xFFFFFFFC) ? (x & 0xFFFFFFFC) + sizeof(DWORD) : x ) 227 228 std::wstring CEnumProcess::GetCommandLine(HANDLE hProcess) 229 { 230 SYSTEM_INFO sysinfo; 231 GetSystemInfo (&sysinfo); 232 233 MEMORY_BASIC_INFORMATION mbi; 234 if (VirtualQueryEx (hProcess, PROCESS_DATA_BLOCK_ADDRESS, &mbi, sizeof(mbi) ) == 0) 235 throw EnumProcException(_T("VirtualQueryEx failed"), GetLastError()); 236 LPBYTE lpBuffer = (LPBYTE)malloc (sysinfo.dwPageSize); 237 if (lpBuffer == NULL) 238 throw EnumProcException(_T("Failed to allocate buffer")); 239 DWORD dwBytesRead; 240 if (!ReadProcessMemory( hProcess, mbi.BaseAddress, (LPVOID)lpBuffer, sysinfo.dwPageSize, &dwBytesRead)) { 241 free(lpBuffer); 242 throw EnumProcException(_T("ReadProcessMemory failed"), GetLastError()); 243 } 244 LPBYTE lpPos = lpPos = lpBuffer + ((DWORD)PROCESS_DATA_BLOCK_ADDRESS - (DWORD)mbi.BaseAddress); 245 246 // Skip programs current directory and path 247 lpPos += (wcslen((LPWSTR)lpPos) + 1) * sizeof(WCHAR); 248 249 // Aligned on a DWORD boundary skip it, and copy the next string into 250 // buffer and null terminate it. 251 lpPos = (LPBYTE)ALIGNMENT((DWORD)lpPos); 252 lpPos += (wcslen((LPWSTR)lpPos) + 1) * sizeof(WCHAR); 253 254 // Sometimes there is an extra \0 here 255 /* 256 if ( *lpPos == '\0' ) 257 lpPos += sizeof(WCHAR); 258 */ 259 260 DWORD nStrLength = (wcslen((LPWSTR)lpPos) + 1) * sizeof(WCHAR); 261 WCHAR *buffer = new TCHAR[nStrLength+2]; 262 buffer[0] = L'\0'; 263 if(nStrLength > sizeof(WCHAR)) { 264 wcsncpy(buffer, (LPWSTR)lpPos, nStrLength); 265 buffer[nStrLength] = L'\0'; 266 } 267 free(lpBuffer); 268 std::wstring ret = buffer; 269 delete [] buffer; 270 return ret; 271 } 271 272 272 273 … … 274 275 { 275 276 pEntry->dwPID = dwPID; 276 277 277 // Open process to get filename 278 HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPID); 279 if (hProc) 280 { 281 HMODULE hMod; 282 DWORD size; 283 // Get the first module (the process itself) 284 if( FEnumProcessModules(hProc, &hMod, sizeof(hMod), &size) ) 285 { 286 //Get filename 287 288 if( !FGetModuleFileNameEx( hProc, hMod, lpString, MAX_FILENAME) ) { 289 pEntry->sFilename = _T("N/A (error)"); 290 } else { 291 std::wstring path = lpString; 292 std::wstring::size_type pos = path.find_last_of(_T("\\")); 293 if (pos != std::wstring::npos) { 294 path = path.substr(++pos); 295 } 296 pEntry->sFilename = path; 278 bool bCmdLine = pEntry->getCommandLine(); 279 DWORD openArgs = PROCESS_QUERY_INFORMATION|PROCESS_VM_READ; 280 if (bCmdLine) 281 openArgs |= PROCESS_VM_OPERATION; 282 HANDLE hProc = OpenProcess(openArgs, FALSE, dwPID); 283 if (!hProc) { 284 pEntry->filename = _T("N/A (security restriction)"); 285 return TRUE; 286 } 287 if (bCmdLine) { 288 try { 289 pEntry->command_line = GetCommandLine(hProc); 290 } catch (EnumProcException &e) { 291 pEntry->command_line = _T("ERROR: " + e.getMessage();); 292 } catch (...) { 293 pEntry->command_line = _T("ERROR: Failed to get CommandLine."); 294 } 295 } 296 HMODULE hMod; 297 DWORD size; 298 // Get the first module (the process itself) 299 if( FEnumProcessModules(hProc, &hMod, sizeof(hMod), &size) ) { 300 //Get filename 301 //GetModuleFileNameEx 302 303 if( !FGetModuleFileNameEx( hProc, hMod, lpString, MAX_FILENAME) ) { 304 pEntry->filename = _T("N/A (error)"); 305 } else { 306 std::wstring path = lpString; 307 std::wstring::size_type pos = path.find_last_of(_T("\\")); 308 if (pos != std::wstring::npos) { 309 path = path.substr(++pos); 297 310 } 298 } 299 CloseHandle(hProc); 300 } 301 else 302 pEntry->sFilename = _T("N/A (security restriction)"); 303 304 return TRUE; 311 pEntry->filename = path; 312 } 313 } 314 CloseHandle(hProc); 305 315 } 306 316 -
include/EnumProcess.h
r978bd31 rb7ed6ac 22 22 23 23 #include <psapi.h> 24 #include <tlhelp32.h>25 24 #include <string> 25 #include <error.hpp> 26 26 27 27 … … 30 30 const int NONE = 0x0; 31 31 const int PSAPI = 0x1; 32 const int TOOLHELP= 0x2;33 const int PROC16 = 0x4;34 32 } 35 33 … … 41 39 typedef BOOL (WINAPI *PFEnumProcessModules)(HANDLE hProcess, HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded); 42 40 typedef DWORD (WINAPI *PFGetModuleFileNameEx)(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize); 43 44 //Functions loaded from Kernel3245 typedef HANDLE (WINAPI *PFCreateToolhelp32Snapshot)(DWORD dwFlags, DWORD th32ProcessID);46 typedef BOOL (WINAPI *PFProcess32First)(HANDLE hSnapshot, LPPROCESSENTRY32W lppe);47 typedef BOOL (WINAPI *PFProcess32Next)(HANDLE hSnapshot, LPPROCESSENTRY32W lppe);48 typedef BOOL (WINAPI *PFModule32First)(HANDLE hSnapshot, LPMODULEENTRY32W lpme);49 typedef BOOL (WINAPI *PFModule32Next)(HANDLE hSnapshot, LPMODULEENTRY32W lpme);50 41 #else 51 42 // Functions loaded from PSAPI … … 53 44 typedef BOOL (WINAPI *PFEnumProcessModules)(HANDLE hProcess, HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded); 54 45 typedef DWORD (WINAPI *PFGetModuleFileNameEx)(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize); 55 56 //Functions loaded from Kernel3257 typedef HANDLE (WINAPI *PFCreateToolhelp32Snapshot)(DWORD dwFlags, DWORD th32ProcessID);58 typedef BOOL (WINAPI *PFProcess32First)(HANDLE hSnapshot, LPPROCESSENTRY32 lppe);59 typedef BOOL (WINAPI *PFProcess32Next)(HANDLE hSnapshot, LPPROCESSENTRY32 lppe);60 typedef BOOL (WINAPI *PFModule32First)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);61 typedef BOOL (WINAPI *PFModule32Next)(HANDLE hSnapshot, LPMODULEENTRY32 lpme);62 46 #endif 63 47 … … 66 50 public: 67 51 52 class EnumProcException { 53 std::wstring error_; 54 public: 55 EnumProcException(std::wstring error) : error_(error) {} 56 EnumProcException(std::wstring error, DWORD code) : error_(error) { 57 error_ += _T(":" ) + error::format::from_system(code); 58 } 59 std::wstring getMessage() const { 60 return error_; 61 } 62 }; 63 68 64 struct CProcessEntry 69 65 { 70 std::wstring sFilename; 66 static const int fill_filename = 0x1; 67 static const int fill_command_line = 0x2; 68 DWORD fill; 69 std::wstring filename; 70 std::wstring command_line; 71 71 DWORD dwPID; 72 WORD hTask16;73 // Constructors/Destructors74 CProcessEntry( ) : dwPID(0), hTask16(0) {}75 CProcessEntry( CProcessEntry &e) : dwPID(e.dwPID), hTask16(e.hTask16), sFilename(e.sFilename) {}72 // Constructors/Destructor 73 CProcessEntry() : dwPID(0), fill(0) {} 74 CProcessEntry(DWORD toFill) : dwPID(0), fill(toFill) {} 75 CProcessEntry(const CProcessEntry &e) : dwPID(e.dwPID), fill(e.fill), filename(e.filename), command_line(e.command_line) {} 76 76 virtual ~CProcessEntry() {} 77 bool getCommandLine() const { return fill&fill_command_line!=0; } 78 bool getFilename() const { return fill&fill_filename!=0; } 77 79 }; 78 80 … … 95 97 BOOL GetProcessNext(CProcessEntry *pEntry); 96 98 BOOL GetProcessFirst(CProcessEntry* pEntry); 99 BOOL EnableTokenPrivilege(LPTSTR privilege); 100 std::wstring GetCommandLine(HANDLE hProcess); 97 101 98 102 int GetAvailableMethods(); … … 120 124 BOOL FillPStructPSAPI(DWORD pid, CProcessEntry* pEntry); 121 125 BOOL FillMStructPSAPI(DWORD dwPID, HMODULE mMod, CModuleEntry* pEntry); 122 123 // ToolHelp related members124 HANDLE m_hProcessSnap, m_hModuleSnap;125 HMODULE TOOLHELP; //Handle to the module (Kernel32)126 #ifdef UNICODE127 PROCESSENTRY32W m_pe;128 MODULEENTRY32W m_me;129 #else130 PROCESSENTRY32 m_pe;131 MODULEENTRY32 m_me;132 #endif133 // ToolHelp related functions134 PFCreateToolhelp32Snapshot FCreateToolhelp32Snapshot;135 PFProcess32First FProcess32First;136 PFProcess32Next FProcess32Next;137 PFModule32First FModule32First;138 PFModule32Next FModule32Next;139 126 LPTSTR lpString; 140 141 127 }; 142 128 -
include/error.hpp
r047516e rb7ed6ac 3 3 #include <string> 4 4 #include <windows.h> 5 #include <strEx.h> 5 6 6 7 namespace error { -
include/filter_framework.hpp
r7f596ce rb7ed6ac 126 126 } catch (const boost::bad_expression e) { 127 127 throw handler_exception(_T("Invalid syntax in regular expression:") + str); 128 } catch (...) { 129 throw handler_exception(_T("Invalid syntax in regular expression:") + str); 128 130 } 129 131 } -
modules/CheckSystem/CheckSystem.cpp
rdd02c15 rb7ed6ac 30 30 #include <set> 31 31 #include <sysinfo.h> 32 #ifndef NO_BOOST_DEP 33 #include <boost/regex.hpp> 34 #endif 32 35 33 36 CheckSystem gCheckSystem; … … 68 71 if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_AUTO) { 69 72 OSVERSIONINFO osVer = systemInfo::getOSVersion(); 73 /* 70 74 if (systemInfo::isBelowNT4(osVer)) { 71 75 NSC_DEBUG_MSG_STD(_T("Autodetected NT4<, using PSAPI process enumeration.")); … … 84 88 } 85 89 } else { 90 */ 86 91 NSC_DEBUG_MSG_STD(_T("Autodetected failed, using PSAPI process enumeration.")); 87 92 processMethod_ = ENUM_METHOD::PSAPI; … … 92 97 NSC_LOG_ERROR_STD(_T("Try this URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=3d1fbaed-d122-45cf-9d46-1cae384097ac")); 93 98 } 94 }99 //} 95 100 } else if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) { 96 101 NSC_DEBUG_MSG_STD(_T("Using PSAPI method.")); … … 101 106 } 102 107 } else { 103 NSC_DEBUG_MSG_STD(_T("Using TOOLHELP method.")); 104 if (method == (method|ENUM_METHOD::TOOLHELP)) { 105 processMethod_ = ENUM_METHOD::TOOLHELP; 106 } else { 107 NSC_LOG_ERROR_STD(_T("TOOLHELP method not avalible, check ") C_SYSTEM_ENUMPROC_METHOD _T(" option.")); 108 } 108 NSC_LOG_ERROR_STD(_T("TOOLHELP method has been removed sine we dont really want to support w9x ") C_SYSTEM_ENUMPROC_METHOD _T(".")); 109 109 } 110 110 try { … … 751 751 } 752 752 typedef struct NSPROCDATA__ { 753 NSPROCDATA__() : count(0) {}754 NSPROCDATA__(const NSPROCDATA__ &other) {755 count = other.count;756 entry = other.entry;757 }758 759 753 unsigned int count; 760 754 CEnumProcess::CProcessEntry entry; 755 std::wstring key; 756 757 NSPROCDATA__() : count(0) {} 758 NSPROCDATA__(const NSPROCDATA__ &other) : count(other.count), entry(other.entry), key(other.key) {} 761 759 } NSPROCDATA; 762 760 typedef std::map<std::wstring,NSPROCDATA,strEx::case_blind_string_compare> NSPROCLST; … … 765 763 * @return a hash_map with all running processes 766 764 */ 767 NSPROCLST GetProcessList(int processMethod )765 NSPROCLST GetProcessList(int processMethod, bool getCmdLines) 768 766 { 769 767 NSPROCLST ret; … … 777 775 return ret; 778 776 } 779 CEnumProcess::CProcessEntry entry; 777 int toFill = CEnumProcess::CProcessEntry::fill_filename; 778 if (getCmdLines) 779 toFill |= CEnumProcess::CProcessEntry::fill_command_line; 780 CEnumProcess::CProcessEntry entry(toFill); 780 781 for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) ) { 781 NSPROCLST::iterator it = ret.find(entry.sFilename); 782 std::wstring key; 783 if (getCmdLines) 784 key = entry.command_line; 785 else 786 key = entry.filename; 787 NSPROCLST::iterator it = ret.find(key); 782 788 if (it == ret.end()) { 783 ret[entry.sFilename].entry = entry; 784 ret[entry.sFilename].count = 1; 789 ret[key].entry = entry; 790 ret[key].count = 1; 791 ret[key].key = key; 785 792 } else 786 793 (*it).second.count++; … … 812 819 StateConatiner tmpObject; 813 820 bool bPerfData = true; 821 bool useCmdLine = false; 822 typedef enum { 823 match_string, match_substring, match_regexp 824 } match_type; 825 match_type match = match_string; 826 827 814 828 815 829 tmpObject.data = _T("uptime"); … … 822 836 MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 823 837 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 838 MAP_OPTIONS_BOOL_TRUE(_T("cmdLine"), useCmdLine) 839 MAP_OPTIONS_MODE(_T("match"), _T("string"), match, match_string) 840 MAP_OPTIONS_MODE(_T("match"), _T("regexp"), match, match_regexp) 841 MAP_OPTIONS_MODE(_T("match"), _T("substr"), match, match_substring) 842 MAP_OPTIONS_MODE(_T("match"), _T("substring"), match, match_substring) 824 843 MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) 825 844 else if (p2.first == _T("Proc")) { … … 840 859 MAP_OPTIONS_END() 841 860 842 843 861 NSPROCLST runningProcs; 844 862 try { 845 runningProcs = GetProcessList(processMethod_ );863 runningProcs = GetProcessList(processMethod_, useCmdLine); 846 864 } catch (TCHAR *c) { 847 865 NSC_LOG_ERROR_STD(_T("ERROR: ") + c); … … 851 869 852 870 for (std::list<StateConatiner>::iterator it = list.begin(); it != list.end(); ++it) { 853 NSPROCLST::iterator proc = runningProcs.find((*it).data); 871 NSPROCLST::iterator proc; 872 if (match == match_string) { 873 proc = runningProcs.find((*it).data); 874 } else if (match == match_substring) { 875 for (proc=runningProcs.begin();proc!=runningProcs.end();++proc) { 876 if ((*proc).first.find((*it).data) != std::wstring::npos) 877 break; 878 } 879 #ifndef NO_BOOST_DEP 880 } else if (match == match_regexp) { 881 try { 882 boost::wregex filter((*it).data,boost::regex::icase); 883 for (proc=runningProcs.begin();proc!=runningProcs.end();++proc) { 884 std::wstring value = (*proc).first; 885 if (boost::regex_match(value, filter)) 886 break; 887 } 888 } catch (const boost::bad_expression e) { 889 NSC_LOG_ERROR_STD(_T("Failed to compile regular expression: ") + (*proc).first); 890 msg = _T("Failed to compile regular expression: ") + (*proc).first; 891 return NSCAPI::returnUNKNOWN; 892 } catch (...) { 893 NSC_LOG_ERROR_STD(_T("Failed to compile regular expression: ") + (*proc).first); 894 msg = _T("Failed to compile regular expression: ") + (*proc).first; 895 return NSCAPI::returnUNKNOWN; 896 } 897 #endif 898 } else { 899 NSC_LOG_ERROR_STD(_T("Unsupported mode for: ") + (*proc).first); 900 msg = _T("Unsupported mode for: ") + (*proc).first; 901 return NSCAPI::returnUNKNOWN; 902 } 854 903 bool bFound = (proc != runningProcs.end()); 855 std::wstring tmp;856 TNtServiceInfo info;857 904 if (bNSClient) { 858 905 if (bFound && (*it).showAll()) { 859 906 if (!msg.empty()) msg += _T(" - "); 860 msg += (* it).data+ _T(": Running");907 msg += (*proc).first + _T(": Running"); 861 908 } else if (bFound) { 862 909 } else { … … 873 920 value.count = 0; 874 921 value.state = checkHolders::state_stopped; 922 } 923 if (bFound && (*it).alias.empty()) { 924 (*it).alias = (*proc).first; 875 925 } 876 926 (*it).perfData = bPerfData; -
modules/LUAScript/script_wrapper.hpp
rd76af81 rb7ed6ac 45 45 return strEx::string_to_wstring(s); 46 46 } 47 48 49 50 51 class Account { 52 lua_Number m_balance; 53 public: 54 static const char className[]; 55 static Luna<Account>::RegType methods[]; 56 57 Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } 58 int inject(lua_State *L) { 59 m_balance += luaL_checknumber(L, 1); return 0; 60 } 61 int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } 62 int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; } 63 ~Account() { printf("deleted Account (%p)\n", this); } 64 }; 65 66 const char Account::className[] = "Account"; 67 68 #define method(class, name) {#name, &class::name} 69 70 Luna<Account>::RegType Account::methods[] = { 71 method(Account, inject), 72 method(Account, withdraw), 73 method(Account, balance), 74 {0,0} 75 }; 47 typedef std::pair<std::wstring,int> where_type; 48 where_type where(lua_State *L, int level = 1) { 49 lua_Debug ar; 50 if (lua_getstack(L, level, &ar)) { /* check function at level */ 51 lua_getinfo(L, "Sl", &ar); /* get info about it */ 52 if (ar.currentline > 0) { /* is there info? */ 53 return where_type(s2w(ar.short_src), ar.currentline); 54 } 55 } 56 return where_type(_T("unknown"),0); 57 } 76 58 std::wstring extract_string(lua_State *L) { 77 59 return strEx::string_to_wstring(lua_tostring( L, lua_gettop( L ) )); … … 120 102 lua_pushstring(L, strEx::wstring_to_string(_T("unknown")).c_str()); 121 103 } 122 123 static int inject(lua_State *L) { 124 int nargs = lua_gettop( L ); 125 unsigned int argLen = nargs-1; 126 arrayBuffer::arrayBuffer arguments = arrayBuffer::createArrayBuffer(argLen); 127 for (unsigned int i=argLen;i>0;i--) { 128 std::wstring arg = extract_string(L); 129 arrayBuffer::set(arguments, argLen, i-1, arg); 130 lua_pop(L, 1); 131 } 132 std::wstring command = extract_string(L); 133 lua_pop(L, 1); 134 135 std::wstring msg; 136 std::wstring perf; 137 NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectCommand(command.c_str(), argLen, arguments, msg, perf); 138 push_code(L, ret); 139 lua_pushstring(L, strEx::wstring_to_string(msg).c_str()); 140 lua_pushstring(L, strEx::wstring_to_string(perf).c_str()); 141 return 3; 104 void push_string(lua_State *L, std::wstring s) { 105 lua_pushstring(L, strEx::wstring_to_string(s).c_str()); 142 106 } 143 107 … … 198 162 199 163 }; 164 class nsclient_wrapper { 165 public: 166 167 static int execute (lua_State *L) { 168 try { 169 int nargs = lua_gettop( L ); 170 if (nargs == 0) { 171 return luaL_error(L, "nscp.execute requires atleast 1 argument!"); 172 } 173 unsigned int argLen = nargs-1; 174 arrayBuffer::arrayBuffer arguments = arrayBuffer::createArrayBuffer(argLen); 175 for (unsigned int i=argLen;i>0;i--) { 176 std::wstring arg = extract_string(L); 177 arrayBuffer::set(arguments, argLen, i-1, arg); 178 lua_pop(L, 1); 179 } 180 std::wstring command = extract_string(L); 181 lua_pop(L, 1); 182 std::wstring msg; 183 std::wstring perf; 184 NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectCommand(command.c_str(), argLen, arguments, msg, perf); 185 push_code(L, ret); 186 lua_pushstring(L, strEx::wstring_to_string(msg).c_str()); 187 lua_pushstring(L, strEx::wstring_to_string(perf).c_str()); 188 return 3; 189 } catch (...) { 190 return luaL_error(L, "Unknown exception in: nscp.execute"); 191 } 192 } 193 194 static int register_command(lua_State *L) { 195 try { 196 lua_handler *handler = lua_manager::get_handler(L); 197 lua_script *script = lua_manager::get_script(L); 198 int nargs = lua_gettop( L ); 199 if (nargs != 2) 200 return luaL_error(L, "Incorrect syntax: nscp.register(<key>, <function>);"); 201 handler->register_command(script, pop_string(L), pop_string(L)); 202 return 0; 203 } catch (LUAException e) { 204 return luaL_error(L, std::string("Error in nscp.register: " + w2s(e.getMessage())).c_str()); 205 } catch (...) { 206 return luaL_error(L, "Unknown exception in: nscp.register"); 207 } 208 } 209 210 static int getSetting (lua_State *L) { 211 int nargs = lua_gettop( L ); 212 if (nargs < 2 || nargs > 3) 213 return luaL_error(L, "Incorrect syntax: nscp.getSetting(<section>, <key>[, <default value>]);"); 214 std::wstring v; 215 if (nargs > 2) 216 v = pop_string(L); 217 std::wstring k = pop_string(L); 218 std::wstring s = pop_string(L); 219 push_string(L, NSCModuleHelper::getSettingsString(s, k, v)); 220 return 1; 221 } 222 static int getSection (lua_State *L) { 223 NSC_DEBUG_MSG_STD(_T("LUA::setSettings")); 224 return 0; 225 } 226 static int info (lua_State *L) { 227 return log_any(L, NSCAPI::log); 228 } 229 static int error (lua_State *L) { 230 return log_any(L, NSCAPI::error); 231 } 232 static int log_any(lua_State *L, int mode) { 233 where_type w = where(L); 234 int nargs = lua_gettop( L ); 235 std::wstring str; 236 for (int i=0;i<nargs;i++) { 237 str += pop_string(L); 238 } 239 NSCModuleHelper::Message(mode, w.first, w.second, str); 240 return 0; 241 } 242 243 static const luaL_Reg my_funcs[]; 244 245 static int luaopen(lua_State *L) { 246 luaL_register(L, "nscp", my_funcs); 247 return 1; 248 } 249 250 251 }; 252 const luaL_Reg nsclient_wrapper::my_funcs[] = { 253 {"execute", execute}, 254 {"info", info}, 255 {"print", info}, 256 {"error", error}, 257 {"register", register_command}, 258 {"getSetting", getSetting}, 259 {"getSection", getSection}, 260 {NULL, NULL} 261 }; 262 200 263 lua_manager::handler_type lua_manager::handlers; 201 264 lua_manager::script_type lua_manager::scripts; 202 265 double lua_manager::last_value = 0; 203 266 char lua_manager::handler_key[] = "registry.key.handler"; 204 char lua_manager::script_key[] = "registry.key.sctrip"; 205 206 static int register_command(lua_State *L) { 207 try { 208 lua_handler *handler = lua_manager::get_handler(L); 209 lua_script *script = lua_manager::get_script(L); 210 int nargs = lua_gettop( L ); 211 if (nargs < 2) { 212 return luaL_error(L, "Missing argument for register_command! usage: register_command(<key>, <function>);"); 213 } 214 if (nargs > 2) { 215 return luaL_error(L, "To many arguments for register_command! usage: register_command(<key>, <function>);"); 216 } 217 handler->register_command(script, pop_string(L), pop_string(L)); 218 return 0; 219 } catch (LUAException e) { 220 return luaL_error(L, std::string("Error: " + w2s(e.getMessage())).c_str()); 221 } catch (...) { 222 return luaL_error(L, "Unknown exception in: register_command"); 223 } 224 } 267 char lua_manager::script_key[] = "registry.key.script"; 268 225 269 class lua_script { 226 270 Lua_State L; … … 232 276 void load() { 233 277 luaL_openlibs(L); 278 nsclient_wrapper::luaopen(L); 234 279 //Luna<Account>::Register(L); 235 lua_register(L, "inject", inject); 236 lua_register(L, "register_command", register_command); 280 //lua_register(L, "register_command", register_command); 237 281 238 282 if (luaL_loadfile(L, strEx::wstring_to_string(script_).c_str()) != 0) {
Note: See TracChangeset
for help on using the changeset viewer.








