| 1 | /////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // NSClient++ Base Service |
|---|
| 3 | // |
|---|
| 4 | // Copyright (c) 2004 MySolutions NORDIC (http://www.medin.name) |
|---|
| 5 | // |
|---|
| 6 | // Date: 2004-03-13 |
|---|
| 7 | // Author: Michael Medin (michael@medin.name) |
|---|
| 8 | // |
|---|
| 9 | // Part of this file is based on work by Bruno Vais (bvais@usa.net) |
|---|
| 10 | // |
|---|
| 11 | // This software is provided "AS IS", without a warranty of any kind. |
|---|
| 12 | // You are free to use/modify this code but leave this header intact. |
|---|
| 13 | // |
|---|
| 14 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 15 | #include "stdafx.h" |
|---|
| 16 | #include <winsvc.h> |
|---|
| 17 | #include "NSClient++.h" |
|---|
| 18 | #include <Settings.h> |
|---|
| 19 | #include <charEx.h> |
|---|
| 20 | #include <Socket.h> |
|---|
| 21 | #include <b64/b64.h> |
|---|
| 22 | #include <config.h> |
|---|
| 23 | #include <msvc_wrappers.h> |
|---|
| 24 | #include <Userenv.h> |
|---|
| 25 | #include <remote_processes.hpp> |
|---|
| 26 | #include <Lmcons.h> |
|---|
| 27 | //#ifdef DEBUG |
|---|
| 28 | #include <assert.h> |
|---|
| 29 | //#endif |
|---|
| 30 | |
|---|
| 31 | NSClient mainClient(SZSERVICENAME); // Global core instance. |
|---|
| 32 | bool g_bConsoleLog = false; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | class tray_starter { |
|---|
| 36 | struct start_block { |
|---|
| 37 | std::wstring cmd; |
|---|
| 38 | std::wstring cmd_line; |
|---|
| 39 | DWORD sessionId; |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | public: |
|---|
| 43 | static LPVOID init(DWORD dwSessionId, std::wstring exe, std::wstring cmdline) { |
|---|
| 44 | start_block *sb = new start_block; |
|---|
| 45 | sb->cmd = exe; |
|---|
| 46 | sb->cmd_line = cmdline; |
|---|
| 47 | sb->sessionId = dwSessionId; |
|---|
| 48 | return sb; |
|---|
| 49 | } |
|---|
| 50 | DWORD threadProc(LPVOID lpParameter) { |
|---|
| 51 | start_block* param = static_cast<start_block*>(lpParameter); |
|---|
| 52 | DWORD dwSessionId = param->sessionId; |
|---|
| 53 | std::wstring cmd = param->cmd; |
|---|
| 54 | std::wstring cmdline = param->cmd_line; |
|---|
| 55 | delete param; |
|---|
| 56 | for (int i=0;i<10;i++) { |
|---|
| 57 | Sleep(1000); |
|---|
| 58 | if (startTrayHelper(dwSessionId, cmd, cmdline, false)) |
|---|
| 59 | break; |
|---|
| 60 | } |
|---|
| 61 | return 0; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | static bool start(DWORD dwSessionId) { |
|---|
| 65 | std::wstring program = mainClient.getBasePath() + _T("\\") + Settings::getInstance()->getString(NSCLIENT_SECTION_TITLE, NSCLIENT_SETTINGS_SYSTRAY_EXE, NSCLIENT_SETTINGS_SYSTRAY_EXE_DEFAULT); |
|---|
| 66 | std::wstring cmdln = _T("\"") + program + _T("\" -channel __") + strEx::itos(dwSessionId) + _T("__"); |
|---|
| 67 | return tray_starter::startTrayHelper(dwSessionId, program, cmdln); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | static bool startTrayHelper(DWORD dwSessionId, std::wstring exe, std::wstring cmdline, bool startThread = true) { |
|---|
| 71 | HANDLE hToken = NULL; |
|---|
| 72 | if (!remote_processes::GetSessionUserToken(dwSessionId, &hToken)) { |
|---|
| 73 | LOG_ERROR_STD(_T("Failed to query user token: ") + error::lookup::last_error()); |
|---|
| 74 | return false; |
|---|
| 75 | } else { |
|---|
| 76 | STARTUPINFO StartUPInfo; |
|---|
| 77 | PROCESS_INFORMATION ProcessInfo; |
|---|
| 78 | |
|---|
| 79 | ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); |
|---|
| 80 | ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); |
|---|
| 81 | StartUPInfo.wShowWindow = SW_HIDE; |
|---|
| 82 | StartUPInfo.lpDesktop = L"Winsta0\\Default"; |
|---|
| 83 | StartUPInfo.cb = sizeof(STARTUPINFO); |
|---|
| 84 | |
|---|
| 85 | wchar_t *buffer = new wchar_t[cmdline.size()+10]; |
|---|
| 86 | wcscpy(buffer, cmdline.c_str()); |
|---|
| 87 | LOG_MESSAGE_STD(_T("Running: ") + exe); |
|---|
| 88 | LOG_MESSAGE_STD(_T("Running: ") + cmdline); |
|---|
| 89 | |
|---|
| 90 | LPVOID pEnv =NULL; |
|---|
| 91 | DWORD dwCreationFlags = CREATE_NO_WINDOW; //0; //DETACHED_PROCESS |
|---|
| 92 | |
|---|
| 93 | if(CreateEnvironmentBlock(&pEnv,hToken,TRUE)) { |
|---|
| 94 | dwCreationFlags|=CREATE_UNICODE_ENVIRONMENT; |
|---|
| 95 | } else { |
|---|
| 96 | LOG_ERROR_STD(_T("Failed to create enviornment: ") + error::lookup::last_error()); |
|---|
| 97 | pEnv=NULL; |
|---|
| 98 | } |
|---|
| 99 | /* |
|---|
| 100 | LOG_ERROR_STD(_T("Impersonating user: ")); |
|---|
| 101 | if (!ImpersonateLoggedOnUser(hToken)) { |
|---|
| 102 | LOG_ERROR_STD(_T("Failed to impersonate the user: ") + error::lookup::last_error()); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | wchar_t pszUname[UNLEN + 1]; |
|---|
| 106 | ZeroMemory(pszUname,sizeof(pszUname)); |
|---|
| 107 | DWORD dwSize = UNLEN; |
|---|
| 108 | if (!GetUserName(pszUname,&dwSize)) { |
|---|
| 109 | DWORD dwErr = GetLastError(); |
|---|
| 110 | if (!RevertToSelf()) |
|---|
| 111 | LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); |
|---|
| 112 | LOG_ERROR_STD(_T("Failed to get username: ") + error::format::from_system(dwErr)); |
|---|
| 113 | return false; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | PROFILEINFO info; |
|---|
| 118 | info.dwSize = sizeof(PROFILEINFO); |
|---|
| 119 | info.lpUserName = pszUname; |
|---|
| 120 | if (!LoadUserProfile(hToken, &info)) { |
|---|
| 121 | DWORD dwErr = GetLastError(); |
|---|
| 122 | if (!RevertToSelf()) |
|---|
| 123 | LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); |
|---|
| 124 | LOG_ERROR_STD(_T("Failed to get username: ") + error::format::from_system(dwErr)); |
|---|
| 125 | return false; |
|---|
| 126 | } |
|---|
| 127 | */ |
|---|
| 128 | if (!CreateProcessAsUser(hToken, exe.c_str(), buffer, NULL, NULL, FALSE, dwCreationFlags, pEnv, NULL, &StartUPInfo, &ProcessInfo)) { |
|---|
| 129 | DWORD dwErr = GetLastError(); |
|---|
| 130 | delete [] buffer; |
|---|
| 131 | /* |
|---|
| 132 | if (!RevertToSelf()) { |
|---|
| 133 | LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); |
|---|
| 134 | } |
|---|
| 135 | */ |
|---|
| 136 | if (startThread && dwErr == ERROR_PIPE_NOT_CONNECTED) { |
|---|
| 137 | LOG_MESSAGE(_T("Failed to start trayhelper: starting a background thread to do it instead...")); |
|---|
| 138 | Thread<tray_starter> *pThread = new Thread<tray_starter>(_T("tray-starter-thread")); |
|---|
| 139 | pThread->createThread(tray_starter::init(dwSessionId, exe, cmdline)); |
|---|
| 140 | return false; |
|---|
| 141 | } else if (dwErr == ERROR_PIPE_NOT_CONNECTED) { |
|---|
| 142 | LOG_ERROR_STD(_T("Thread failed to start trayhelper (will try again): ") + error::format::from_system(dwErr)); |
|---|
| 143 | return false; |
|---|
| 144 | } else { |
|---|
| 145 | LOG_ERROR_STD(_T("Failed to start trayhelper: ") + error::format::from_system(dwErr)); |
|---|
| 146 | return true; |
|---|
| 147 | } |
|---|
| 148 | } else { |
|---|
| 149 | delete [] buffer; |
|---|
| 150 | /* |
|---|
| 151 | if (!RevertToSelf()) { |
|---|
| 152 | LOG_ERROR_STD(_T("Failed to revert to self: ") + error::lookup::last_error()); |
|---|
| 153 | } |
|---|
| 154 | */ |
|---|
| 155 | LOG_MESSAGE_STD(_T("Started tray in other user session: ") + strEx::itos(dwSessionId)); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | CloseHandle(hToken); |
|---|
| 160 | return true; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | void display(std::wstring title, std::wstring message) { |
|---|
| 168 | ::MessageBox(NULL, message.c_str(), title.c_str(), MB_OK|MB_ICONERROR); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Application startup point |
|---|
| 173 | * |
|---|
| 174 | * @param argc Argument count |
|---|
| 175 | * @param argv[] Argument array |
|---|
| 176 | * @param envp[] Environment array |
|---|
| 177 | * @return exit status |
|---|
| 178 | */ |
|---|
| 179 | int wmain(int argc, TCHAR* argv[], TCHAR* envp[]) |
|---|
| 180 | { |
|---|
| 181 | srand( (unsigned)time( NULL ) ); |
|---|
| 182 | int nRetCode = 0; |
|---|
| 183 | if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { |
|---|
| 184 | if ( _wcsicmp( _T("install"), argv[1]+1 ) == 0 ) { |
|---|
| 185 | bool bGui = false; |
|---|
| 186 | bool bStart = false; |
|---|
| 187 | std::wstring service_name, service_description; |
|---|
| 188 | for (int i=2;i<argc;i++) { |
|---|
| 189 | if (_wcsicmp( _T("gui"), argv[i]) == 0) { |
|---|
| 190 | bGui = true; |
|---|
| 191 | } else if (_wcsicmp( _T("start"), argv[i]) == 0) { |
|---|
| 192 | bStart = true; |
|---|
| 193 | } else { |
|---|
| 194 | if (service_name.empty()) |
|---|
| 195 | service_name = argv[i]; |
|---|
| 196 | else { |
|---|
| 197 | if (!service_description.empty()) |
|---|
| 198 | service_description += _T(" "); |
|---|
| 199 | service_description += argv[i]; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | if (service_name.empty()) |
|---|
| 204 | service_name = SZSERVICENAME; |
|---|
| 205 | if (service_description.empty()) |
|---|
| 206 | service_description = SZSERVICEDISPLAYNAME; |
|---|
| 207 | g_bConsoleLog = true; |
|---|
| 208 | try { |
|---|
| 209 | serviceControll::Install(service_name.c_str(), service_description.c_str(), SZDEPENDENCIES); |
|---|
| 210 | if (bStart) |
|---|
| 211 | serviceControll::Start(service_name); |
|---|
| 212 | } catch (const serviceControll::SCException& e) { |
|---|
| 213 | if (bGui) |
|---|
| 214 | display(_T("Error installing"), _T("Service installation failed; ") + e.error_); |
|---|
| 215 | LOG_ERROR_STD(_T("Service installation failed: ") + e.error_); |
|---|
| 216 | return -1; |
|---|
| 217 | } |
|---|
| 218 | try { |
|---|
| 219 | serviceControll::SetDescription(service_name, service_description); |
|---|
| 220 | } catch (const serviceControll::SCException& e) { |
|---|
| 221 | if (bGui) |
|---|
| 222 | display(_T("Error installing"), _T("Service installation failed; ") + e.error_); |
|---|
| 223 | LOG_MESSAGE_STD(_T("Couldn't set service description: ") + e.error_); |
|---|
| 224 | } |
|---|
| 225 | if (bGui) |
|---|
| 226 | display(_T("Service installed"), _T("Service installed successfully!")); |
|---|
| 227 | LOG_MESSAGE(_T("Service installed!")); |
|---|
| 228 | return 0; |
|---|
| 229 | } else if ( _wcsicmp( _T("uninstall"), argv[1]+1 ) == 0 ) { |
|---|
| 230 | bool bGui = false; |
|---|
| 231 | bool bStop = false; |
|---|
| 232 | std::wstring service_name; |
|---|
| 233 | for (int i=2;i<argc;i++) { |
|---|
| 234 | if (_wcsicmp( _T("gui"), argv[i]) == 0) { |
|---|
| 235 | bGui = true; |
|---|
| 236 | } else if (_wcsicmp( _T("stop"), argv[i]) == 0) { |
|---|
| 237 | bStop = true; |
|---|
| 238 | } else { |
|---|
| 239 | service_name = argv[i]; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | if (service_name.empty()) |
|---|
| 243 | service_name = SZSERVICENAME; |
|---|
| 244 | g_bConsoleLog = true; |
|---|
| 245 | try { |
|---|
| 246 | if (bStop) |
|---|
| 247 | serviceControll::Stop(service_name); |
|---|
| 248 | } catch (const serviceControll::SCException& e) { |
|---|
| 249 | LOG_MESSAGE_STD(_T("Failed to stop service (") + service_name + _T(") failed; ") + e.error_); |
|---|
| 250 | } |
|---|
| 251 | try { |
|---|
| 252 | serviceControll::Uninstall(service_name); |
|---|
| 253 | } catch (const serviceControll::SCException& e) { |
|---|
| 254 | if (bGui) |
|---|
| 255 | display(_T("Error uninstalling"), _T("Service de-installation (") + service_name + _T(") failed; ") + e.error_ + _T("\nMaybe the service was not previously installed properly?")); |
|---|
| 256 | LOG_ERROR_STD(_T("Service deinstallation failed; ") + e.error_); |
|---|
| 257 | return 0; |
|---|
| 258 | } |
|---|
| 259 | if (bGui) |
|---|
| 260 | display(_T("Service uninstalled"), _T("Service uninstalled successfully!")); |
|---|
| 261 | LOG_MESSAGE(_T("Service uninstalled!")); |
|---|
| 262 | return 0; |
|---|
| 263 | } else if ( _wcsicmp( _T("encrypt"), argv[1]+1 ) == 0 ) { |
|---|
| 264 | g_bConsoleLog = true; |
|---|
| 265 | std::wstring password; |
|---|
| 266 | try { |
|---|
| 267 | Settings::getInstance()->setFile(mainClient.getBasePath(), _T("NSC.ini")); |
|---|
| 268 | } catch (SettingsException e) { |
|---|
| 269 | std::wcout << _T("Could not find settings: ") << e.getMessage() << std::endl;; |
|---|
| 270 | return 1; |
|---|
| 271 | } |
|---|
| 272 | std::wcout << _T("Enter password to encrypt (has to be a single word): "); |
|---|
| 273 | std::wcin >> password; |
|---|
| 274 | std::wstring xor_pwd = Encrypt(password); |
|---|
| 275 | std::wcout << _T("obfuscated_password=") << xor_pwd << std::endl; |
|---|
| 276 | std::wstring outPasswd = Decrypt(xor_pwd); |
|---|
| 277 | if (password != outPasswd) |
|---|
| 278 | std::wcout << _T("ERROR: Password did not match: ") << outPasswd<< std::endl; |
|---|
| 279 | Settings::destroyInstance(); |
|---|
| 280 | return 0; |
|---|
| 281 | } else if ( _wcsicmp( _T("start"), argv[1]+1 ) == 0 ) { |
|---|
| 282 | g_bConsoleLog = true; |
|---|
| 283 | bool bGui = false; |
|---|
| 284 | std::wstring service_name; |
|---|
| 285 | for (int i=2;i<argc;i++) { |
|---|
| 286 | if (_wcsicmp( _T("gui"), argv[i]) == 0) { |
|---|
| 287 | bGui = true; |
|---|
| 288 | } else { |
|---|
| 289 | service_name = argv[i]; |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | if (service_name.empty()) |
|---|
| 293 | service_name = SZSERVICENAME; |
|---|
| 294 | try { |
|---|
| 295 | serviceControll::Start(service_name.c_str()); |
|---|
| 296 | } catch (const serviceControll::SCException& e) { |
|---|
| 297 | if (bGui) |
|---|
| 298 | display(_T("Service failed to start"), e.error_); |
|---|
| 299 | LOG_MESSAGE_STD(_T("Service failed to start: ") + e.error_); |
|---|
| 300 | return -1; |
|---|
| 301 | } |
|---|
| 302 | } else if ( _wcsicmp( _T("stop"), argv[1]+1 ) == 0 ) { |
|---|
| 303 | g_bConsoleLog = true; |
|---|
| 304 | bool bGui = false; |
|---|
| 305 | std::wstring service_name; |
|---|
| 306 | for (int i=2;i<argc;i++) { |
|---|
| 307 | if (_wcsicmp( _T("gui"), argv[i]) == 0) { |
|---|
| 308 | bGui = true; |
|---|
| 309 | } else { |
|---|
| 310 | service_name = argv[i]; |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | if (service_name.empty()) |
|---|
| 314 | service_name = SZSERVICENAME; |
|---|
| 315 | try { |
|---|
| 316 | serviceControll::Stop(service_name.c_str()); |
|---|
| 317 | } catch (const serviceControll::SCException& e) { |
|---|
| 318 | if (bGui) |
|---|
| 319 | display(_T("Service failed to stop"), e.error_); |
|---|
| 320 | LOG_MESSAGE_STD(_T("Service failed to stop: ") + e.error_); |
|---|
| 321 | return -1; |
|---|
| 322 | } |
|---|
| 323 | } else if ( _wcsicmp( _T("about"), argv[1]+1 ) == 0 ) { |
|---|
| 324 | g_bConsoleLog = true; |
|---|
| 325 | LOG_MESSAGE(SZAPPNAME _T(" (C) Michael Medin - michael<at>medin<dot>name")); |
|---|
| 326 | LOG_MESSAGE(_T("Version: ") SZVERSION); |
|---|
| 327 | LOG_MESSAGE(_T("Architecture: ") SZARCH); |
|---|
| 328 | |
|---|
| 329 | std::wstring pluginPath = mainClient.getBasePath() + _T("modules\\"); |
|---|
| 330 | LOG_MESSAGE_STD(_T("Looking at plugins in: ") + pluginPath); |
|---|
| 331 | |
|---|
| 332 | WIN32_FIND_DATA wfd; |
|---|
| 333 | HANDLE hFind = FindFirstFile((pluginPath + _T("*.dll")).c_str(), &wfd); |
|---|
| 334 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 335 | do { |
|---|
| 336 | std::wstring file = wfd.cFileName; |
|---|
| 337 | NSCPlugin *plugin = new NSCPlugin(pluginPath + _T("\\") + file); |
|---|
| 338 | std::wstring name = _T("<unknown>"); |
|---|
| 339 | std::wstring description = _T("<unknown>"); |
|---|
| 340 | try { |
|---|
| 341 | plugin->load_dll(); |
|---|
| 342 | name = plugin->getName(); |
|---|
| 343 | description = plugin->getDescription(); |
|---|
| 344 | } catch(const NSPluginException& e) { |
|---|
| 345 | LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); |
|---|
| 346 | } catch (std::exception e) { |
|---|
| 347 | LOG_ERROR_STD(_T("exception loading plugin: ") + strEx::string_to_wstring(e.what())); |
|---|
| 348 | } catch (...) { |
|---|
| 349 | LOG_ERROR_STD(_T("Unknown exception loading plugin")); |
|---|
| 350 | } |
|---|
| 351 | LOG_MESSAGE_STD(_T("* ") + name + _T(" (") + file + _T(")")); |
|---|
| 352 | std::list<std::wstring> list = strEx::splitEx(description, _T("\n")); |
|---|
| 353 | for (std::list<std::wstring>::const_iterator cit = list.begin(); cit != list.end(); ++cit) { |
|---|
| 354 | LOG_MESSAGE_STD(_T(" ") + *cit); |
|---|
| 355 | } |
|---|
| 356 | } while (FindNextFile(hFind, &wfd)); |
|---|
| 357 | } else { |
|---|
| 358 | LOG_CRITICAL(_T("No plugin was found!")); |
|---|
| 359 | } |
|---|
| 360 | FindClose(hFind); |
|---|
| 361 | } else if ( _wcsicmp( _T("version"), argv[1]+1 ) == 0 ) { |
|---|
| 362 | g_bConsoleLog = true; |
|---|
| 363 | LOG_MESSAGE(SZAPPNAME _T(" Version: ") SZVERSION _T(", Plattform: ") SZARCH); |
|---|
| 364 | } else if ( _wcsicmp( _T("d"), argv[1]+1 ) == 0 ) { |
|---|
| 365 | // Run command from command line (like NRPE) but with debug enabled |
|---|
| 366 | g_bConsoleLog = true; |
|---|
| 367 | mainClient.enableDebug(true); |
|---|
| 368 | mainClient.initCore(true); |
|---|
| 369 | std::wstring command, args, msg, perf; |
|---|
| 370 | if (argc > 2) |
|---|
| 371 | command = argv[2]; |
|---|
| 372 | for (int i=3;i<argc;i++) { |
|---|
| 373 | if (i!=3) args += _T(" "); |
|---|
| 374 | args += argv[i]; |
|---|
| 375 | } |
|---|
| 376 | nRetCode = mainClient.inject(command, args, L' ', true, msg, perf); |
|---|
| 377 | std::wcout << msg << _T("|") << perf << std::endl; |
|---|
| 378 | mainClient.exitCore(true); |
|---|
| 379 | return nRetCode; |
|---|
| 380 | } else if ( _wcsicmp( _T("c"), argv[1]+1 ) == 0 ) { |
|---|
| 381 | // Run command from command line (like NRPE) |
|---|
| 382 | g_bConsoleLog = true; |
|---|
| 383 | mainClient.enableDebug(false); |
|---|
| 384 | mainClient.initCore(true); |
|---|
| 385 | std::wstring command, args, msg, perf; |
|---|
| 386 | if (argc > 2) |
|---|
| 387 | command = argv[2]; |
|---|
| 388 | for (int i=3;i<argc;i++) { |
|---|
| 389 | if (i!=3) args += _T(" "); |
|---|
| 390 | args += argv[i]; |
|---|
| 391 | } |
|---|
| 392 | nRetCode = mainClient.inject(command, args, L' ', true, msg, perf); |
|---|
| 393 | std::wcout << msg << _T("|") << perf << std::endl; |
|---|
| 394 | mainClient.exitCore(true); |
|---|
| 395 | return nRetCode; |
|---|
| 396 | } else if ( _wcsicmp( _T("noboot"), argv[1]+1 ) == 0 ) { |
|---|
| 397 | g_bConsoleLog = true; |
|---|
| 398 | mainClient.enableDebug(false); |
|---|
| 399 | mainClient.initCore(false); |
|---|
| 400 | int nRetCode = -1; |
|---|
| 401 | if (argc>=4) |
|---|
| 402 | nRetCode = mainClient.commandLineExec(argv[2], argv[3], argc-4, &argv[4]); |
|---|
| 403 | else if (argc>=3) |
|---|
| 404 | nRetCode = mainClient.commandLineExec(argv[2], argv[3], 0, NULL); |
|---|
| 405 | mainClient.exitCore(false); |
|---|
| 406 | return nRetCode; |
|---|
| 407 | } else if ( _wcsicmp( _T("svc"), argv[1]+1 ) == 0 ) { |
|---|
| 408 | g_bConsoleLog = true; |
|---|
| 409 | try { |
|---|
| 410 | std::wstring exe = serviceControll::get_exe_path(SZSERVICENAME); |
|---|
| 411 | LOG_MESSAGE_STD(_T("The Service uses: ") + exe); |
|---|
| 412 | } catch (const serviceControll::SCException& e) { |
|---|
| 413 | LOG_ERROR_STD(_T("Failed to find service: ") + e.error_); |
|---|
| 414 | } |
|---|
| 415 | } else if ( _wcsicmp( _T("test"), argv[1]+1 ) == 0 ) { |
|---|
| 416 | bool server = false; |
|---|
| 417 | if (argc > 2 && _wcsicmp( _T("server"), argv[2] ) == 0 ) { |
|---|
| 418 | server = true; |
|---|
| 419 | } |
|---|
| 420 | std::wcout << "Launching test mode - " << (server?_T("server mode"):_T("client mode")) << std::endl; |
|---|
| 421 | LOG_MESSAGE_STD(_T("Booting: " SZSERVICEDISPLAYNAME )); |
|---|
| 422 | try { |
|---|
| 423 | if (serviceControll::isStarted(SZSERVICENAME)) { |
|---|
| 424 | std::wcerr << "Service seems to be started, this is probably not a good idea..." << std::endl; |
|---|
| 425 | } |
|---|
| 426 | } catch (const serviceControll::SCException& e) { |
|---|
| 427 | e;// Empty by design |
|---|
| 428 | } |
|---|
| 429 | g_bConsoleLog = true; |
|---|
| 430 | mainClient.enableDebug(true); |
|---|
| 431 | if (!mainClient.initCore(true)) { |
|---|
| 432 | LOG_ERROR_STD(_T("Service *NOT* started!")); |
|---|
| 433 | return -1; |
|---|
| 434 | } |
|---|
| 435 | // if (server) |
|---|
| 436 | // mainClient.startTrayIcon(0); |
|---|
| 437 | LOG_MESSAGE_STD(_T("Using settings from: ") + Settings::getInstance()->getActiveType()); |
|---|
| 438 | LOG_MESSAGE(_T("Enter command to inject or exit to terminate...")); |
|---|
| 439 | |
|---|
| 440 | std::wstring s = _T(""); |
|---|
| 441 | std::wstring buff = _T(""); |
|---|
| 442 | while (true) { |
|---|
| 443 | std::wcin >> s; |
|---|
| 444 | if (s == _T("exit")) { |
|---|
| 445 | std::wcout << _T("Exiting...") << std::endl; |
|---|
| 446 | break; |
|---|
| 447 | } else if (s == _T("off") && buff == _T("debug ")) { |
|---|
| 448 | std::wcout << _T("Setting debug log off...") << std::endl; |
|---|
| 449 | mainClient.enableDebug(false); |
|---|
| 450 | } else if (s == _T("on") && buff == _T("debug ")) { |
|---|
| 451 | std::wcout << _T("Setting debug log on...") << std::endl; |
|---|
| 452 | mainClient.enableDebug(true); |
|---|
| 453 | } else if (s == _T("reattach")) { |
|---|
| 454 | std::wcout << _T("Reattaching to session 0") << std::endl; |
|---|
| 455 | mainClient.startTrayIcon(0); |
|---|
| 456 | //#ifdef DEBUG |
|---|
| 457 | } else if (s == _T("assert")) { |
|---|
| 458 | assert(false); |
|---|
| 459 | //#endif |
|---|
| 460 | } else if (std::cin.peek() < 15) { |
|---|
| 461 | buff += s; |
|---|
| 462 | strEx::token t = strEx::getToken(buff, ' '); |
|---|
| 463 | std::wstring msg, perf; |
|---|
| 464 | NSCAPI::nagiosReturn ret = mainClient.inject(t.first, t.second, ' ', true, msg, perf); |
|---|
| 465 | if (ret == NSCAPI::returnIgnored) { |
|---|
| 466 | std::wcout << _T("No handler for command: ") << t.first << std::endl; |
|---|
| 467 | } else { |
|---|
| 468 | std::wcout << NSCHelper::translateReturn(ret) << _T(":"); |
|---|
| 469 | std::cout << strEx::wstring_to_string(msg); |
|---|
| 470 | if (!perf.empty()) |
|---|
| 471 | std::cout << "|" << strEx::wstring_to_string(perf); |
|---|
| 472 | std::wcout << std::endl; |
|---|
| 473 | } |
|---|
| 474 | buff = _T(""); |
|---|
| 475 | } else { |
|---|
| 476 | buff += s + _T(" "); |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | mainClient.exitCore(true); |
|---|
| 480 | return 0; |
|---|
| 481 | } else { |
|---|
| 482 | std::wcerr << _T("Usage: -version, -about, -install, -uninstall, -start, -stop, -encrypt") << std::endl; |
|---|
| 483 | std::wcerr << _T("Usage: [-noboot] <ModuleName> <commnd> [arguments]") << std::endl; |
|---|
| 484 | return -1; |
|---|
| 485 | } |
|---|
| 486 | return nRetCode; |
|---|
| 487 | } else if (argc > 2) { |
|---|
| 488 | g_bConsoleLog = true; |
|---|
| 489 | mainClient.initCore(true); |
|---|
| 490 | if (argc>=3) |
|---|
| 491 | nRetCode = mainClient.commandLineExec(argv[1], argv[2], argc-3, &argv[3]); |
|---|
| 492 | else |
|---|
| 493 | nRetCode = mainClient.commandLineExec(argv[1], argv[2], 0, NULL); |
|---|
| 494 | mainClient.exitCore(true); |
|---|
| 495 | return nRetCode; |
|---|
| 496 | } else if (argc > 1) { |
|---|
| 497 | g_bConsoleLog = true; |
|---|
| 498 | mainClient.enableDebug(true); |
|---|
| 499 | std::wcerr << _T("Invalid command line argument: ") << argv[1] << std::endl; |
|---|
| 500 | std::wcerr << _T("Usage: -version, -about, -install, -uninstall, -start, -stop, -encrypt") << std::endl; |
|---|
| 501 | std::wcerr << _T("Usage: [-noboot] <ModuleName> <commnd> [arguments]") << std::endl; |
|---|
| 502 | return -1; |
|---|
| 503 | } |
|---|
| 504 | std::wcout << _T("Running as service...") << std::endl; |
|---|
| 505 | if (!mainClient.StartServiceCtrlDispatcher()) { |
|---|
| 506 | LOG_MESSAGE(_T("We failed to start the service")); |
|---|
| 507 | } |
|---|
| 508 | return nRetCode; |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | void NSClientT::session_error(std::wstring file, unsigned int line, std::wstring msg) { |
|---|
| 512 | NSAPIMessage(NSCAPI::error, file.c_str(), line, msg.c_str()); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | void NSClientT::session_info(std::wstring file, unsigned int line, std::wstring msg) { |
|---|
| 516 | NSAPIMessage(NSCAPI::log, file.c_str(), line, msg.c_str()); |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 523 | // Service functions |
|---|
| 524 | |
|---|
| 525 | /** |
|---|
| 526 | * Initialize the program |
|---|
| 527 | * @param boot true if we shall boot all plugins |
|---|
| 528 | * @param attachIfPossible is true we will attach to a running instance. |
|---|
| 529 | * @return success |
|---|
| 530 | * @author mickem |
|---|
| 531 | */ |
|---|
| 532 | bool NSClientT::initCore(bool boot) { |
|---|
| 533 | LOG_DEBUG(_T("Attempting to start NSCLient++ - " SZVERSION)); |
|---|
| 534 | try { |
|---|
| 535 | Settings::getInstance()->setFile(getBasePath(), _T("NSC.ini")); |
|---|
| 536 | if (debug_ == log_debug) { |
|---|
| 537 | Settings::getInstance()->setInt(_T("log"), _T("debug"), 1); |
|---|
| 538 | } |
|---|
| 539 | if (enable_shared_session_) |
|---|
| 540 | Settings::getInstance()->setInt(MAIN_SECTION_TITLE, MAIN_SHARED_SESSION, 1); |
|---|
| 541 | enable_shared_session_ = Settings::getInstance()->getInt(MAIN_SECTION_TITLE, MAIN_SHARED_SESSION, MAIN_SHARED_SESSION_DEFAULT)==1; |
|---|
| 542 | } catch (SettingsException e) { |
|---|
| 543 | LOG_ERROR_STD(_T("Could not find settings: ") + e.getMessage()); |
|---|
| 544 | return false; |
|---|
| 545 | } catch (...) { |
|---|
| 546 | LOG_ERROR_STD(_T("Unknown exception reading settings...")); |
|---|
| 547 | return false; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | if (enable_shared_session_) { |
|---|
| 551 | LOG_MESSAGE_STD(_T("Enabling shared session...")); |
|---|
| 552 | if (boot) { |
|---|
| 553 | LOG_MESSAGE_STD(_T("Starting shared session...")); |
|---|
| 554 | try { |
|---|
| 555 | shared_server_.reset(new nsclient_session::shared_server_session(this)); |
|---|
| 556 | if (!shared_server_->session_exists()) { |
|---|
| 557 | shared_server_->create_new_session(); |
|---|
| 558 | } else { |
|---|
| 559 | LOG_ERROR_STD(_T("Session already exists cant create a new one!")); |
|---|
| 560 | } |
|---|
| 561 | startTrayIcons(); |
|---|
| 562 | } catch (nsclient_session::session_exception e) { |
|---|
| 563 | LOG_ERROR_STD(_T("Failed to create new session: ") + e.what()); |
|---|
| 564 | shared_server_.reset(NULL); |
|---|
| 565 | } catch (...) { |
|---|
| 566 | LOG_ERROR_STD(_T("Failed to create new session: Unknown exception")); |
|---|
| 567 | shared_server_.reset(NULL); |
|---|
| 568 | } |
|---|
| 569 | } else { |
|---|
| 570 | LOG_MESSAGE_STD(_T("Attaching to shared session...")); |
|---|
| 571 | try { |
|---|
| 572 | std::wstring id = _T("_attached_") + strEx::itos(GetCurrentProcessId()) + _T("_"); |
|---|
| 573 | shared_client_.reset(new nsclient_session::shared_client_session(id, this)); |
|---|
| 574 | if (shared_client_->session_exists()) { |
|---|
| 575 | shared_client_->attach_to_session(id); |
|---|
| 576 | } else { |
|---|
| 577 | LOG_ERROR_STD(_T("No session was found cant attach!")); |
|---|
| 578 | } |
|---|
| 579 | LOG_ERROR_STD(_T("Session is: ") + shared_client_->get_client_id()); |
|---|
| 580 | } catch (nsclient_session::session_exception e) { |
|---|
| 581 | LOG_ERROR_STD(_T("Failed to attach to session: ") + e.what()); |
|---|
| 582 | shared_client_.reset(NULL); |
|---|
| 583 | } catch (...) { |
|---|
| 584 | LOG_ERROR_STD(_T("Failed to attach to session: Unknown exception")); |
|---|
| 585 | shared_client_.reset(NULL); |
|---|
| 586 | } |
|---|
| 587 | } |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | try { |
|---|
| 591 | simpleSocket::WSAStartup(); |
|---|
| 592 | } catch (simpleSocket::SocketException e) { |
|---|
| 593 | LOG_ERROR_STD(_T("Socket exception: ") + e.getMessage()); |
|---|
| 594 | return false; |
|---|
| 595 | } catch (...) { |
|---|
| 596 | LOG_ERROR_STD(_T("Unknown exception iniating socket...")); |
|---|
| 597 | return false; |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | try { |
|---|
| 601 | com_helper_.initialize(); |
|---|
| 602 | } catch (com_helper::com_exception e) { |
|---|
| 603 | LOG_ERROR_STD(_T("COM exception: ") + e.getMessage()); |
|---|
| 604 | return false; |
|---|
| 605 | } catch (...) { |
|---|
| 606 | LOG_ERROR_STD(_T("Unknown exception iniating COM...")); |
|---|
| 607 | return false; |
|---|
| 608 | } |
|---|
| 609 | if (boot) { |
|---|
| 610 | try { |
|---|
| 611 | settings_base::sectionList list = Settings::getInstance()->getSection(_T("modules")); |
|---|
| 612 | for (settings_base::sectionList::iterator it = list.begin(); it != list.end(); it++) { |
|---|
| 613 | try { |
|---|
| 614 | loadPlugin(getBasePath() + _T("modules\\") + (*it)); |
|---|
| 615 | } catch(const NSPluginException& e) { |
|---|
| 616 | LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); |
|---|
| 617 | //return false; |
|---|
| 618 | } catch (std::exception e) { |
|---|
| 619 | LOG_ERROR_STD(_T("exception loading plugin: ") + (*it) + strEx::string_to_wstring(e.what())); |
|---|
| 620 | return false; |
|---|
| 621 | } catch (...) { |
|---|
| 622 | LOG_ERROR_STD(_T("Unknown exception loading plugin: ") + (*it)); |
|---|
| 623 | return false; |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| 626 | } catch (SettingsException e) { |
|---|
| 627 | LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); |
|---|
| 628 | } catch (...) { |
|---|
| 629 | LOG_ERROR_STD(_T("Unknown exception when loading settings")); |
|---|
| 630 | return false; |
|---|
| 631 | } |
|---|
| 632 | try { |
|---|
| 633 | loadPlugins(); |
|---|
| 634 | } catch (...) { |
|---|
| 635 | LOG_ERROR_STD(_T("Unknown exception loading plugins")); |
|---|
| 636 | return false; |
|---|
| 637 | } |
|---|
| 638 | LOG_DEBUG_STD(_T("NSCLient++ - " SZVERSION) + _T(" Started!")); |
|---|
| 639 | } |
|---|
| 640 | return true; |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | /** |
|---|
| 644 | * Service control handler startup point. |
|---|
| 645 | * When the program is started as a service this will be the entry point. |
|---|
| 646 | */ |
|---|
| 647 | bool NSClientT::InitiateService() { |
|---|
| 648 | if (!initCore(true)) |
|---|
| 649 | return false; |
|---|
| 650 | /* |
|---|
| 651 | DWORD dwSessionId = remote_processes::getActiveSessionId(); |
|---|
| 652 | if (dwSessionId != 0xFFFFFFFF) |
|---|
| 653 | tray_starter::start(dwSessionId); |
|---|
| 654 | else |
|---|
| 655 | LOG_ERROR_STD(_T("Failed to start tray helper:" ) + error::lookup::last_error()); |
|---|
| 656 | */ |
|---|
| 657 | return true; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | void NSClientT::startTrayIcons() { |
|---|
| 661 | if (shared_server_.get() == NULL) { |
|---|
| 662 | LOG_MESSAGE_STD(_T("No master session so tray icons not started")); |
|---|
| 663 | return; |
|---|
| 664 | } |
|---|
| 665 | remote_processes::PWTS_SESSION_INFO list; |
|---|
| 666 | DWORD count; |
|---|
| 667 | if (!remote_processes::_WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE , 0, 1, &list, &count)) { |
|---|
| 668 | LOG_ERROR_STD(_T("Failed to enumerate sessions:" ) + error::lookup::last_error()); |
|---|
| 669 | } else { |
|---|
| 670 | LOG_DEBUG_STD(_T("Found ") + strEx::itos(count) + _T(" sessions")); |
|---|
| 671 | for (DWORD i=0;i<count;i++) { |
|---|
| 672 | LOG_DEBUG_STD(_T("Found session: ") + strEx::itos(list[i].SessionId) + _T(" state: ") + strEx::itos(list[i].State)); |
|---|
| 673 | if (list[i].State == remote_processes::_WTS_CONNECTSTATE_CLASS::WTSActive) { |
|---|
| 674 | startTrayIcon(list[i].SessionId); |
|---|
| 675 | } |
|---|
| 676 | } |
|---|
| 677 | } |
|---|
| 678 | } |
|---|
| 679 | void NSClientT::startTrayIcon(DWORD dwSessionId) { |
|---|
| 680 | if (shared_server_.get() == NULL) { |
|---|
| 681 | LOG_MESSAGE_STD(_T("No master session so tray icons not started")); |
|---|
| 682 | return; |
|---|
| 683 | } |
|---|
| 684 | if (!shared_server_->re_attach_client(dwSessionId)) { |
|---|
| 685 | if (!tray_starter::start(dwSessionId)) { |
|---|
| 686 | LOG_ERROR_STD(_T("Failed to start session (") + strEx::itos(dwSessionId) + _T("): " ) + error::lookup::last_error()); |
|---|
| 687 | } |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | bool NSClientT::exitCore(bool boot) { |
|---|
| 692 | plugins_loaded_ = false; |
|---|
| 693 | LOG_DEBUG(_T("Attempting to stop NSCLient++ - " SZVERSION)); |
|---|
| 694 | if (boot) { |
|---|
| 695 | try { |
|---|
| 696 | LOG_DEBUG_STD(_T("Stopping: NON Message Handling Plugins")); |
|---|
| 697 | mainClient.unloadPlugins(false); |
|---|
| 698 | } catch(NSPluginException e) { |
|---|
| 699 | LOG_ERROR_STD(_T("Exception raised when unloading non msg plguins: ") + e.error_ + _T(" in module: ") + e.file_); |
|---|
| 700 | } catch(...) { |
|---|
| 701 | LOG_ERROR_STD(_T("Unknown exception raised when unloading non msg plugins")); |
|---|
| 702 | } |
|---|
| 703 | } |
|---|
| 704 | LOG_DEBUG_STD(_T("Stopping: COM helper")); |
|---|
| 705 | try { |
|---|
| 706 | com_helper_.unInitialize(); |
|---|
| 707 | } catch (com_helper::com_exception e) { |
|---|
| 708 | LOG_ERROR_STD(_T("COM exception: ") + e.getMessage()); |
|---|
| 709 | } catch (...) { |
|---|
| 710 | LOG_ERROR_STD(_T("Unknown exception uniniating COM...")); |
|---|
| 711 | } |
|---|
| 712 | LOG_DEBUG_STD(_T("Stopping: Socket Helpers")); |
|---|
| 713 | try { |
|---|
| 714 | simpleSocket::WSACleanup(); |
|---|
| 715 | } catch (simpleSocket::SocketException e) { |
|---|
| 716 | LOG_ERROR_STD(_T("Socket exception: ") + e.getMessage()); |
|---|
| 717 | } catch (...) { |
|---|
| 718 | LOG_ERROR_STD(_T("Unknown exception uniniating socket...")); |
|---|
| 719 | } |
|---|
| 720 | LOG_DEBUG_STD(_T("Stopping: Settings instance")); |
|---|
| 721 | Settings::destroyInstance(); |
|---|
| 722 | try { |
|---|
| 723 | if (shared_client_.get() != NULL) { |
|---|
| 724 | LOG_DEBUG_STD(_T("Stopping: shared client")); |
|---|
| 725 | shared_client_->set_handler(NULL); |
|---|
| 726 | shared_client_->close_session(); |
|---|
| 727 | } |
|---|
| 728 | } catch(nsclient_session::session_exception &e) { |
|---|
| 729 | LOG_ERROR_STD(_T("Exception closing shared client session: ") + e.what()); |
|---|
| 730 | } catch(...) { |
|---|
| 731 | LOG_ERROR_STD(_T("Exception closing shared client session: Unknown exception!")); |
|---|
| 732 | } |
|---|
| 733 | try { |
|---|
| 734 | if (shared_server_.get() != NULL) { |
|---|
| 735 | LOG_DEBUG_STD(_T("Stopping: shared server")); |
|---|
| 736 | shared_server_->set_handler(NULL); |
|---|
| 737 | shared_server_->close_session(); |
|---|
| 738 | } |
|---|
| 739 | } catch(...) { |
|---|
| 740 | LOG_ERROR_STD(_T("UNknown exception raised: When closing shared session")); |
|---|
| 741 | } |
|---|
| 742 | if (boot) { |
|---|
| 743 | try { |
|---|
| 744 | LOG_DEBUG_STD(_T("Stopping: Message handling Plugins")); |
|---|
| 745 | mainClient.unloadPlugins(true); |
|---|
| 746 | } catch(NSPluginException e) { |
|---|
| 747 | LOG_ERROR_STD(_T("Exception raised when unloading msg plugins: ") + e.error_ + _T(" in module: ") + e.file_); |
|---|
| 748 | } catch(...) { |
|---|
| 749 | LOG_ERROR_STD(_T("UNknown exception raised: When stopping message plguins")); |
|---|
| 750 | } |
|---|
| 751 | LOG_DEBUG_STD(_T("NSCLient++ - " SZVERSION) + _T(" Stopped succcessfully")); |
|---|
| 752 | } |
|---|
| 753 | return true; |
|---|
| 754 | } |
|---|
| 755 | /** |
|---|
| 756 | * Service control handler termination point. |
|---|
| 757 | * When the program is stopped as a service this will be the "exit point". |
|---|
| 758 | */ |
|---|
| 759 | void NSClientT::TerminateService(void) { |
|---|
| 760 | exitCore(true); |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | /** |
|---|
| 764 | * Forward this to the main service dispatcher helper class |
|---|
| 765 | * @param dwArgc |
|---|
| 766 | * @param *lpszArgv |
|---|
| 767 | */ |
|---|
| 768 | void WINAPI NSClientT::service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv) { |
|---|
| 769 | try { |
|---|
| 770 | mainClient.service_main(dwArgc, lpszArgv); |
|---|
| 771 | } catch (service_helper::service_exception e) { |
|---|
| 772 | LOG_ERROR_STD(_T("Unknown service error: ") + e.what()); |
|---|
| 773 | } catch (...) { |
|---|
| 774 | LOG_ERROR_STD(_T("Unknown service error!")); |
|---|
| 775 | } |
|---|
| 776 | } |
|---|
| 777 | DWORD WINAPI NSClientT::service_ctrl_dispatch_ex(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) { |
|---|
| 778 | return mainClient.service_ctrl_ex(dwControl, dwEventType, lpEventData, lpContext); |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | /** |
|---|
| 782 | * Forward this to the main service dispatcher helper class |
|---|
| 783 | * @param dwCtrlCode |
|---|
| 784 | */ |
|---|
| 785 | void WINAPI NSClientT::service_ctrl_dispatch(DWORD dwCtrlCode) { |
|---|
| 786 | mainClient.service_ctrl_ex(dwCtrlCode, NULL, NULL, NULL); |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | void NSClientT::service_on_session_changed(DWORD dwSessionId, bool logon, DWORD dwEventType) { |
|---|
| 791 | if (shared_server_.get() == NULL) { |
|---|
| 792 | LOG_DEBUG_STD(_T("No shared session: ignoring change event!")); |
|---|
| 793 | return; |
|---|
| 794 | } |
|---|
| 795 | LOG_DEBUG_STD(_T("Got session change: ") + strEx::itos(dwSessionId)); |
|---|
| 796 | if (!logon) { |
|---|
| 797 | LOG_DEBUG_STD(_T("Not a logon event: ") + strEx::itos(dwEventType)); |
|---|
| 798 | return; |
|---|
| 799 | } |
|---|
| 800 | tray_starter::start(dwSessionId); |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 805 | // Member functions |
|---|
| 806 | |
|---|
| 807 | int NSClientT::commandLineExec(const TCHAR* module, const TCHAR* command, const unsigned int argLen, TCHAR** args) { |
|---|
| 808 | std::wstring sModule = module; |
|---|
| 809 | std::wstring moduleList = _T(""); |
|---|
| 810 | { |
|---|
| 811 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 812 | if (!readLock.IsLocked()) { |
|---|
| 813 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 814 | return -1; |
|---|
| 815 | } |
|---|
| 816 | for (pluginList::size_type i=0;i<plugins_.size();++i) { |
|---|
| 817 | NSCPlugin *p = plugins_[i]; |
|---|
| 818 | if (!moduleList.empty()) |
|---|
| 819 | moduleList += _T(", "); |
|---|
| 820 | moduleList += p->getModule(); |
|---|
| 821 | if (p->getModule() == sModule) { |
|---|
| 822 | LOG_DEBUG_STD(_T("Found module: ") + p->getName() + _T("...")); |
|---|
| 823 | try { |
|---|
| 824 | return p->commandLineExec(command, argLen, args); |
|---|
| 825 | } catch (NSPluginException e) { |
|---|
| 826 | LOG_ERROR_STD(_T("Could not execute command: ") + e.error_ + _T(" in ") + e.file_); |
|---|
| 827 | return -1; |
|---|
| 828 | } |
|---|
| 829 | } |
|---|
| 830 | } |
|---|
| 831 | } |
|---|
| 832 | try { |
|---|
| 833 | plugin_type plugin = loadPlugin(getBasePath() + _T("modules\\") + module); |
|---|
| 834 | LOG_DEBUG_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); |
|---|
| 835 | plugin->load_plugin(); |
|---|
| 836 | return plugin->commandLineExec(command, argLen, args); |
|---|
| 837 | } catch (NSPluginException e) { |
|---|
| 838 | LOG_MESSAGE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); |
|---|
| 839 | } |
|---|
| 840 | try { |
|---|
| 841 | plugin_type plugin = loadPlugin(getBasePath() + _T("modules\\") + module + _T(".dll")); |
|---|
| 842 | LOG_DEBUG_STD(_T("Loading plugin: ") + plugin->getName() + _T("...")); |
|---|
| 843 | plugin->load_plugin(); |
|---|
| 844 | return plugin->commandLineExec(command, argLen, args); |
|---|
| 845 | } catch (NSPluginException e) { |
|---|
| 846 | LOG_MESSAGE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); |
|---|
| 847 | } |
|---|
| 848 | LOG_ERROR_STD(_T("Module not found: ") + module + _T(" available modules are: ") + moduleList); |
|---|
| 849 | return 0; |
|---|
| 850 | } |
|---|
| 851 | |
|---|
| 852 | /** |
|---|
| 853 | * Load a list of plug-ins |
|---|
| 854 | * @param plugins A list with plug-ins (DLL files) to load |
|---|
| 855 | */ |
|---|
| 856 | void NSClientT::addPlugins(const std::list<std::wstring> plugins) { |
|---|
| 857 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 858 | if (!readLock.IsLocked()) { |
|---|
| 859 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 860 | return; |
|---|
| 861 | } |
|---|
| 862 | std::list<std::wstring>::const_iterator it; |
|---|
| 863 | for (it = plugins.begin(); it != plugins.end(); ++it) { |
|---|
| 864 | loadPlugin(*it); |
|---|
| 865 | } |
|---|
| 866 | } |
|---|
| 867 | /** |
|---|
| 868 | * Unload all plug-ins (in reversed order) |
|---|
| 869 | */ |
|---|
| 870 | void NSClientT::unloadPlugins(bool unloadLoggers) { |
|---|
| 871 | { |
|---|
| 872 | WriteLock writeLock(&m_mutexRW, true, 10000); |
|---|
| 873 | if (!writeLock.IsLocked()) { |
|---|
| 874 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 875 | return; |
|---|
| 876 | } |
|---|
| 877 | commandHandlers_.clear(); |
|---|
| 878 | if (unloadLoggers) |
|---|
| 879 | messageHandlers_.clear(); |
|---|
| 880 | } |
|---|
| 881 | { |
|---|
| 882 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 883 | if (!readLock.IsLocked()) { |
|---|
| 884 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 885 | return; |
|---|
| 886 | } |
|---|
| 887 | for (pluginList::reverse_iterator it = plugins_.rbegin(); it != plugins_.rend(); ++it) { |
|---|
| 888 | NSCPlugin *p = *it; |
|---|
| 889 | if (p == NULL) |
|---|
| 890 | continue; |
|---|
| 891 | try { |
|---|
| 892 | if (unloadLoggers || !p->hasMessageHandler()) { |
|---|
| 893 | LOG_DEBUG_STD(_T("Unloading plugin: ") + p->getModule() + _T("...")); |
|---|
| 894 | p->unload(); |
|---|
| 895 | } else { |
|---|
| 896 | LOG_DEBUG_STD(_T("Skipping log plugin: ") + p->getModule() + _T("...")); |
|---|
| 897 | } |
|---|
| 898 | } catch(NSPluginException e) { |
|---|
| 899 | LOG_ERROR_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); |
|---|
| 900 | } catch(...) { |
|---|
| 901 | LOG_ERROR_STD(_T("Unknown exception raised when unloading plugin")); |
|---|
| 902 | } |
|---|
| 903 | } |
|---|
| 904 | } |
|---|
| 905 | { |
|---|
| 906 | WriteLock writeLock(&m_mutexRW, true, 10000); |
|---|
| 907 | if (!writeLock.IsLocked()) { |
|---|
| 908 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 909 | return; |
|---|
| 910 | } |
|---|
| 911 | for (pluginList::iterator it = plugins_.begin(); it != plugins_.end();) { |
|---|
| 912 | NSCPlugin *p = (*it); |
|---|
| 913 | try { |
|---|
| 914 | if (p != NULL && (unloadLoggers|| !p->isLoaded())) { |
|---|
| 915 | it = plugins_.erase(it); |
|---|
| 916 | delete p; |
|---|
| 917 | continue; |
|---|
| 918 | } |
|---|
| 919 | } catch(NSPluginException e) { |
|---|
| 920 | LOG_ERROR_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); |
|---|
| 921 | } catch(...) { |
|---|
| 922 | LOG_ERROR_STD(_T("Unknown exception raised when unloading plugin")); |
|---|
| 923 | } |
|---|
| 924 | it++; |
|---|
| 925 | } |
|---|
| 926 | } |
|---|
| 927 | } |
|---|
| 928 | |
|---|
| 929 | void NSClientT::loadPlugins() { |
|---|
| 930 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 931 | if (!readLock.IsLocked()) { |
|---|
| 932 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 933 | return; |
|---|
| 934 | } |
|---|
| 935 | for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { |
|---|
| 936 | LOG_DEBUG_STD(_T("Loading plugin: ") + (*it)->getName() + _T("...")); |
|---|
| 937 | try { |
|---|
| 938 | (*it)->load_plugin(); |
|---|
| 939 | ++it; |
|---|
| 940 | } catch(NSPluginException e) { |
|---|
| 941 | it = plugins_.erase(it); |
|---|
| 942 | LOG_ERROR_STD(_T("Exception raised when loading plugin: ") + e.error_ + _T(" in module: ") + e.file_ + _T(" plugin has been removed.")); |
|---|
| 943 | } catch(...) { |
|---|
| 944 | it = plugins_.erase(it); |
|---|
| 945 | LOG_ERROR_STD(_T("Unknown exception raised when unloading plugin plugin has been removed")); |
|---|
| 946 | } |
|---|
| 947 | } |
|---|
| 948 | plugins_loaded_ = true; |
|---|
| 949 | } |
|---|
| 950 | /** |
|---|
| 951 | * Load a single plug-in using a DLL filename |
|---|
| 952 | * @param file The DLL file |
|---|
| 953 | */ |
|---|
| 954 | NSClientT::plugin_type NSClientT::loadPlugin(const std::wstring file) { |
|---|
| 955 | return addPlugin(new NSCPlugin(file)); |
|---|
| 956 | } |
|---|
| 957 | /** |
|---|
| 958 | * Load and add a plugin to various internal structures |
|---|
| 959 | * @param *plugin The plug-ininstance to load. The pointer is managed by the |
|---|
| 960 | */ |
|---|
| 961 | NSClientT::plugin_type NSClientT::addPlugin(plugin_type plugin) { |
|---|
| 962 | plugin->load_dll(); |
|---|
| 963 | { |
|---|
| 964 | WriteLock writeLock(&m_mutexRW, true, 10000); |
|---|
| 965 | if (!writeLock.IsLocked()) { |
|---|
| 966 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 967 | return plugin; |
|---|
| 968 | } |
|---|
| 969 | plugins_.insert(plugins_.end(), plugin); |
|---|
| 970 | if (plugin->hasCommandHandler()) |
|---|
| 971 | commandHandlers_.insert(commandHandlers_.end(), plugin); |
|---|
| 972 | if (plugin->hasMessageHandler()) |
|---|
| 973 | messageHandlers_.insert(messageHandlers_.end(), plugin); |
|---|
| 974 | } |
|---|
| 975 | return plugin; |
|---|
| 976 | } |
|---|
| 977 | |
|---|
| 978 | |
|---|
| 979 | std::wstring NSClientT::describeCommand(std::wstring command) { |
|---|
| 980 | ReadLock readLock(&m_mutexRWcmdDescriptions, true, 5000); |
|---|
| 981 | if (!readLock.IsLocked()) { |
|---|
| 982 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex when trying to get command list.")); |
|---|
| 983 | return _T("Failed to get mutex when describing command: ") + command; |
|---|
| 984 | } |
|---|
| 985 | cmdMap::const_iterator cit = cmdDescriptions_.find(command); |
|---|
| 986 | if (cit == cmdDescriptions_.end()) |
|---|
| 987 | return _T("Command not found: ") + command + _T(", maybe it has not been register?"); |
|---|
| 988 | return (*cit).second; |
|---|
| 989 | } |
|---|
| 990 | std::list<std::wstring> NSClientT::getAllCommandNames() { |
|---|
| 991 | std::list<std::wstring> lst; |
|---|
| 992 | ReadLock readLock(&m_mutexRWcmdDescriptions, true, 5000); |
|---|
| 993 | if (!readLock.IsLocked()) { |
|---|
| 994 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex when trying to get command list.")); |
|---|
| 995 | return lst; |
|---|
| 996 | } |
|---|
| 997 | for (cmdMap::const_iterator cit = cmdDescriptions_.begin(); cit != cmdDescriptions_.end(); ++cit) { |
|---|
| 998 | lst.push_back((*cit).first); |
|---|
| 999 | } |
|---|
| 1000 | return lst; |
|---|
| 1001 | } |
|---|
| 1002 | void NSClientT::registerCommand(std::wstring cmd, std::wstring desc) { |
|---|
| 1003 | WriteLock writeLock(&m_mutexRWcmdDescriptions, true, 10000); |
|---|
| 1004 | if (!writeLock.IsLocked()) { |
|---|
| 1005 | LOG_ERROR_STD(_T("FATAL ERROR: Failed to describe command:") + cmd); |
|---|
| 1006 | return; |
|---|
| 1007 | } |
|---|
| 1008 | cmdDescriptions_[cmd] = desc; |
|---|
| 1009 | } |
|---|
| 1010 | |
|---|
| 1011 | unsigned int NSClientT::getBufferLength() { |
|---|
| 1012 | static unsigned int len = 0; |
|---|
| 1013 | if (len == 0) { |
|---|
| 1014 | try { |
|---|
| 1015 | len = Settings::getInstance()->getInt(MAIN_SECTION_TITLE, MAIN_STRING_LENGTH, MAIN_STRING_LENGTH_DEFAULT); |
|---|
| 1016 | } catch (SettingsException &e) { |
|---|
| 1017 | LOG_DEBUG_STD(_T("Failed to get length: ") + e.getMessage()); |
|---|
| 1018 | return MAIN_STRING_LENGTH_DEFAULT; |
|---|
| 1019 | } catch (...) { |
|---|
| 1020 | LOG_ERROR(_T("Failed to get length: :(")); |
|---|
| 1021 | return MAIN_STRING_LENGTH_DEFAULT; |
|---|
| 1022 | } |
|---|
| 1023 | } |
|---|
| 1024 | return len; |
|---|
| 1025 | } |
|---|
| 1026 | |
|---|
| 1027 | NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { |
|---|
| 1028 | if (shared_client_.get() != NULL && shared_client_->hasMaster()) { |
|---|
| 1029 | try { |
|---|
| 1030 | return shared_client_->inject(command, arguments, splitter, escape, msg, perf); |
|---|
| 1031 | } catch (nsclient_session::session_exception &e) { |
|---|
| 1032 | LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); |
|---|
| 1033 | return NSCAPI::returnCRIT; |
|---|
| 1034 | } catch (...) { |
|---|
| 1035 | LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); |
|---|
| 1036 | return NSCAPI::returnCRIT; |
|---|
| 1037 | } |
|---|
| 1038 | } else { |
|---|
| 1039 | unsigned int aLen = 0; |
|---|
| 1040 | TCHAR ** aBuf = arrayBuffer::split2arrayBuffer(arguments, splitter, aLen, escape); |
|---|
| 1041 | unsigned int buf_len = getBufferLength(); |
|---|
| 1042 | TCHAR * mBuf = new TCHAR[buf_len+1]; mBuf[0] = '\0'; |
|---|
| 1043 | TCHAR * pBuf = new TCHAR[buf_len+1]; pBuf[0] = '\0'; |
|---|
| 1044 | NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), aLen, aBuf, mBuf, buf_len, pBuf, buf_len); |
|---|
| 1045 | arrayBuffer::destroyArrayBuffer(aBuf, aLen); |
|---|
| 1046 | if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { |
|---|
| 1047 | delete [] mBuf; |
|---|
| 1048 | delete [] pBuf; |
|---|
| 1049 | return ret; |
|---|
| 1050 | } |
|---|
| 1051 | msg = mBuf; |
|---|
| 1052 | perf = pBuf; |
|---|
| 1053 | delete [] mBuf; |
|---|
| 1054 | delete [] pBuf; |
|---|
| 1055 | return ret; |
|---|
| 1056 | } |
|---|
| 1057 | } |
|---|
| 1058 | |
|---|
| 1059 | /** |
|---|
| 1060 | * Inject a command into the plug-in stack. |
|---|
| 1061 | * |
|---|
| 1062 | * @param command Command to inject |
|---|
| 1063 | * @param argLen Length of argument buffer |
|---|
| 1064 | * @param **argument Argument buffer |
|---|
| 1065 | * @param *returnMessageBuffer Message buffer |
|---|
| 1066 | * @param returnMessageBufferLen Length of returnMessageBuffer |
|---|
| 1067 | * @param *returnPerfBuffer Performance data buffer |
|---|
| 1068 | * @param returnPerfBufferLen Length of returnPerfBuffer |
|---|
| 1069 | * @return The command status |
|---|
| 1070 | */ |
|---|
| 1071 | NSCAPI::nagiosReturn NSClientT::injectRAW(const TCHAR* command, const unsigned int argLen, TCHAR **argument, TCHAR *returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR *returnPerfBuffer, unsigned int returnPerfBufferLen) { |
|---|
| 1072 | if (logDebug()) { |
|---|
| 1073 | LOG_DEBUG_STD(_T("Injecting: ") + (std::wstring) command + _T(": ") + arrayBuffer::arrayBuffer2string(argument, argLen, _T(", "))); |
|---|
| 1074 | } |
|---|
| 1075 | if (shared_client_.get() != NULL && shared_client_->hasMaster()) { |
|---|
| 1076 | try { |
|---|
| 1077 | std::wstring msg, perf; |
|---|
| 1078 | int returnCode = shared_client_->inject(command, arrayBuffer::arrayBuffer2string(argument, argLen, _T(" ")), L' ', true, msg, perf); |
|---|
| 1079 | NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, msg, returnCode); |
|---|
| 1080 | return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, perf, returnCode); |
|---|
| 1081 | } catch (nsclient_session::session_exception &e) { |
|---|
| 1082 | LOG_ERROR_STD(_T("Failed to inject remote command: ") + e.what()); |
|---|
| 1083 | int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: ") + e.what(), NSCAPI::returnCRIT); |
|---|
| 1084 | return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); |
|---|
| 1085 | } catch (...) { |
|---|
| 1086 | LOG_ERROR_STD(_T("Failed to inject remote command: Unknown exception")); |
|---|
| 1087 | int returnCode = NSCHelper::wrapReturnString(returnMessageBuffer, returnMessageBufferLen, _T("Failed to inject remote command: + e.what()"), NSCAPI::returnCRIT); |
|---|
| 1088 | return NSCHelper::wrapReturnString(returnPerfBuffer, returnPerfBufferLen, _T(""), returnCode); |
|---|
| 1089 | } |
|---|
| 1090 | } else { |
|---|
| 1091 | ReadLock readLock(&m_mutexRW, true, 5000); |
|---|
| 1092 | if (!readLock.IsLocked()) { |
|---|
| 1093 | LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); |
|---|
| 1094 | return NSCAPI::returnUNKNOWN; |
|---|
| 1095 | } |
|---|
| 1096 | for (pluginList::size_type i = 0; i < commandHandlers_.size(); i++) { |
|---|
| 1097 | try { |
|---|
| 1098 | NSCAPI::nagiosReturn c = commandHandlers_[i]->handleCommand(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); |
|---|
| 1099 | switch (c) { |
|---|
| 1100 | case NSCAPI::returnInvalidBufferLen: |
|---|
| 1101 | LOG_ERROR(_T("UNKNOWN: Return buffer to small to handle this command.")); |
|---|
| 1102 | return c; |
|---|
| 1103 | case NSCAPI::returnIgnored: |
|---|
| 1104 | break; |
|---|
| 1105 | case NSCAPI::returnOK: |
|---|
| 1106 | case NSCAPI::returnWARN: |
|---|
| 1107 | case NSCAPI::returnCRIT: |
|---|
| 1108 | case NSCAPI::returnUNKNOWN: |
|---|
| 1109 | LOG_DEBUG_STD(_T("Injected Result: ") + NSCHelper::translateReturn(c) + _T(" '") + (std::wstring)(returnMessageBuffer) + _T("'")); |
|---|
| 1110 | LOG_DEBUG_STD(_T("Injected Performance Result: '") +(std::wstring)(returnPerfBuffer) + _T("'")); |
|---|
| 1111 | return c; |
|---|
| 1112 | default: |
|---|
| 1113 | LOG_ERROR_STD(_T("Unknown error from handleCommand: ") + strEx::itos(c) + _T(" the injected command was: ") + (std::wstring)command); |
|---|
| 1114 | return c; |
|---|
| 1115 | } |
|---|
| 1116 | } catch(const NSPluginException& e) { |
|---|
| 1117 | LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); |
|---|
| 1118 | return NSCAPI::returnCRIT; |
|---|
| 1119 | } catch(...) { |
|---|
| 1120 | LOG_ERROR_STD(_T("Unknown exception raised in module")); |
|---|
| 1121 | return NSCAPI::returnCRIT; |
|---|
| 1122 | } |
|---|
| 1123 | } |
|---|
| 1124 | LOG_MESSAGE_STD(_T("No handler for command: '") + command + _T("'")); |
|---|
| 1125 | return NSCAPI::returnIgnored; |
|---|
| 1126 | } |
|---|
| 1127 | } |
|---|
| 1128 | |
|---|
| 1129 | bool NSClientT::logDebug() { |
|---|
| 1130 | if (debug_ == log_unknown) { |
|---|
| 1131 | try { |
|---|
| 1132 | if (Settings::getInstance()->getInt(_T("log"), _T("debug"), 0) == 1) |
|---|
| 1133 | debug_ = log_debug; |
|---|
| 1134 | else |
|---|
| 1135 | debug_ = log_nodebug; |
|---|
| 1136 | } catch (SettingsException e) { |
|---|
| 1137 | return true; |
|---|
| 1138 | } |
|---|
| 1139 | } |
|---|
| 1140 | return (debug_ == log_debug); |
|---|
| 1141 | } |
|---|
| 1142 | void NSClientT::enableDebug(bool debug) { |
|---|
| 1143 | if (debug) { |
|---|
| 1144 | debug_ = log_debug; |
|---|
| 1145 | LOG_DEBUG(_T("Enabling debug mode...")); |
|---|
| 1146 | } |
|---|
| 1147 | else |
|---|
| 1148 | debug_ = log_nodebug; |
|---|
| 1149 | } |
|---|
| 1150 | |
|---|
| 1151 | |
|---|
| 1152 | void log_broken_message(std::wstring msg) { |
|---|
| 1153 | OutputDebugString(msg.c_str()); |
|---|
| 1154 | std::wcout << msg << std::endl; |
|---|
| 1155 | } |
|---|
| 1156 | /** |
|---|
| 1157 | * Report a message to all logging enabled modules. |
|---|
| 1158 | * |
|---|
| 1159 | * @param msgType Message type |
|---|
| 1160 | * @param file Filename generally __FILE__ |
|---|
| 1161 | * @param line Line number, generally __LINE__ |
|---|
| 1162 | * @param message The message as a human readable string. |
|---|
| 1163 | */ |
|---|
| 1164 | void NSClientT::reportMessage(int msgType, const TCHAR* file, const int line, std::wstring message) { |
|---|
| 1165 | if ((msgType == NSCAPI::debug)&&(!logDebug())) { |
|---|
| 1166 | return; |
|---|
| 1167 | } |
|---|
| 1168 | if (shared_server_.get() != NULL && shared_server_->hasClients()) { |
|---|
| 1169 | try { |
|---|
| 1170 | shared_server_->sendLogMessageToClients(msgType, file, line, message); |
|---|
| 1171 | } catch (nsclient_session::session_exception e) { |
|---|
| 1172 | log_broken_message(_T("Failed to send message to clients: ") + e.what()); |
|---|
| 1173 | } |
|---|
| 1174 | } |
|---|
| 1175 | std::wstring file_stl = file; |
|---|
| 1176 | std::wstring::size_type pos = file_stl.find_last_of(_T("\\")); |
|---|
| 1177 | if (pos != std::wstring::npos) |
|---|
| 1178 | file_stl = file_stl.substr(pos); |
|---|
| 1179 | { |
|---|
| 1180 | ReadLock readLock(&m_mutexRW, true, 5000); |
|---|
| 1181 | if (!readLock.IsLocked()) { |
|---|
| 1182 | log_broken_message(_T("Message was lost as the (mutexRW) core was locked: ") + message); |
|---|
| 1183 | return; |
|---|
| 1184 | } |
|---|
| 1185 | MutexLock lock(messageMutex); |
|---|
| 1186 | if (!lock.hasMutex()) { |
|---|
| 1187 | log_broken_message(_T("Message was lost as the core was locked: ") + message); |
|---|
| 1188 | return; |
|---|
| 1189 | } |
|---|
| 1190 | if (g_bConsoleLog) { |
|---|
| 1191 | std::string k = "?"; |
|---|
| 1192 | switch (msgType) { |
|---|
| 1193 | case NSCAPI::critical: |
|---|
| 1194 | k ="c"; |
|---|
| 1195 | break; |
|---|
| 1196 | case NSCAPI::warning: |
|---|
| 1197 | k ="w"; |
|---|
| 1198 | break; |
|---|
| 1199 | case NSCAPI::error: |
|---|
| 1200 | k ="e"; |
|---|
| 1201 | break; |
|---|
| 1202 | case NSCAPI::log: |
|---|
| 1203 | k ="l"; |
|---|
| 1204 | break; |
|---|
| 1205 | case NSCAPI::debug: |
|---|
| 1206 | k ="d"; |
|---|
| 1207 | break; |
|---|
| 1208 | } |
|---|
| 1209 | std::cout << k << " " << strEx::wstring_to_string(file_stl) << "(" << line << ") " << strEx::wstring_to_string(message) << std::endl; |
|---|
| 1210 | } |
|---|
| 1211 | if (!plugins_loaded_) { |
|---|
| 1212 | OutputDebugString(message.c_str()); |
|---|
| 1213 | log_cache_.push_back(cached_log_entry(msgType, file, line, message)); |
|---|
| 1214 | } else { |
|---|
| 1215 | if (log_cache_.size() > 0) { |
|---|
| 1216 | for (log_cache_type::const_iterator cit=log_cache_.begin();cit!=log_cache_.end();++cit) { |
|---|
| 1217 | for (pluginList::size_type i = 0; i< messageHandlers_.size(); i++) { |
|---|
| 1218 | try { |
|---|
| 1219 | messageHandlers_[i]->handleMessage((*cit).msgType, (_T("CACHE") + (*cit).file).c_str(), (*cit).line, (*cit).message.c_str()); |
|---|
| 1220 | } catch(const NSPluginException& e) { |
|---|
| 1221 | log_broken_message(_T("Caught: ") + e.error_ + _T(" when trying to log a message...")); |
|---|
| 1222 | return; |
|---|
| 1223 | } catch(...) { |
|---|
| 1224 | log_broken_message(_T("Caught: Unknown Exception when trying to log a message...")); |
|---|
| 1225 | return; |
|---|
| 1226 | } |
|---|
| 1227 | } |
|---|
| 1228 | } |
|---|
| 1229 | log_cache_.clear(); |
|---|
| 1230 | } |
|---|
| 1231 | for (pluginList::size_type i = 0; i< messageHandlers_.size(); i++) { |
|---|
| 1232 | try { |
|---|
| 1233 | messageHandlers_[i]->handleMessage(msgType, file, line, message.c_str()); |
|---|
| 1234 | } catch(const NSPluginException& e) { |
|---|
| 1235 | log_broken_message(_T("Caught: ") + e.error_ + _T(" when trying to log a message...")); |
|---|
| 1236 | return; |
|---|
| 1237 | } catch(...) { |
|---|
| 1238 | log_broken_message(_T("Caught: Unknown Exception when trying to log a message...")); |
|---|
| 1239 | return; |
|---|
| 1240 | } |
|---|
| 1241 | } |
|---|
| 1242 | } |
|---|
| 1243 | } |
|---|
| 1244 | } |
|---|
| 1245 | std::wstring NSClientT::getBasePath(void) { |
|---|
| 1246 | MutexLock lock(internalVariables); |
|---|
| 1247 | if (!lock.hasMutex()) { |
|---|
| 1248 | LOG_ERROR(_T("FATAL ERROR: Could not get mutex.")); |
|---|
| 1249 | return _T("FATAL ERROR"); |
|---|
| 1250 | } |
|---|
| 1251 | if (!basePath.empty()) |
|---|
| 1252 | return basePath; |
|---|
| 1253 | unsigned int buf_len = 4096; |
|---|
| 1254 | TCHAR* buffer = new TCHAR[buf_len+1]; |
|---|
| 1255 | GetModuleFileName(NULL, buffer, buf_len); |
|---|
| 1256 | std::wstring path = buffer; |
|---|
| 1257 | std::wstring::size_type pos = path.rfind('\\'); |
|---|
| 1258 | basePath = path.substr(0, pos) + _T("\\"); |
|---|
| 1259 | delete [] buffer; |
|---|
| 1260 | try { |
|---|
| 1261 | Settings::getInstance()->setFile(basePath, _T("NSC.ini")); |
|---|
| 1262 | } catch (SettingsException e) { |
|---|
| 1263 | LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); |
|---|
| 1264 | } |
|---|
| 1265 | return basePath; |
|---|
| 1266 | } |
|---|
| 1267 | |
|---|
| 1268 | |
|---|
| 1269 | NSCAPI::errorReturn NSAPIGetSettingsString(const TCHAR* section, const TCHAR* key, const TCHAR* defaultValue, TCHAR* buffer, unsigned int bufLen) { |
|---|
| 1270 | try { |
|---|
| 1271 | return NSCHelper::wrapReturnString(buffer, bufLen, Settings::getInstance()->getString(section, key, defaultValue), NSCAPI::isSuccess); |
|---|
| 1272 | } catch (...) { |
|---|
| 1273 | LOG_ERROR_STD(_T("Failed to getString: ") + key); |
|---|
| 1274 | return NSCAPI::hasFailed; |
|---|
| 1275 | } |
|---|
| 1276 | } |
|---|
| 1277 | int NSAPIGetSettingsInt(const TCHAR* section, const TCHAR* key, int defaultValue) { |
|---|
| 1278 | try { |
|---|
| 1279 | return Settings::getInstance()->getInt(section, key, defaultValue); |
|---|
| 1280 | } catch (SettingsException e) { |
|---|
| 1281 | LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); |
|---|
| 1282 | return defaultValue; |
|---|
| 1283 | } |
|---|
| 1284 | } |
|---|
| 1285 | NSCAPI::errorReturn NSAPIGetBasePath(TCHAR*buffer, unsigned int bufLen) { |
|---|
| 1286 | return NSCHelper::wrapReturnString(buffer, bufLen, mainClient.getBasePath(), NSCAPI::isSuccess); |
|---|
| 1287 | } |
|---|
| 1288 | NSCAPI::errorReturn NSAPIGetApplicationName(TCHAR*buffer, unsigned int bufLen) { |
|---|
| 1289 | return NSCHelper::wrapReturnString(buffer, bufLen, SZAPPNAME, NSCAPI::isSuccess); |
|---|
| 1290 | } |
|---|
| 1291 | NSCAPI::errorReturn NSAPIGetApplicationVersionStr(TCHAR*buffer, unsigned int bufLen) { |
|---|
| 1292 | return NSCHelper::wrapReturnString(buffer, bufLen, SZVERSION, NSCAPI::isSuccess); |
|---|
| 1293 | } |
|---|
| 1294 | void NSAPIMessage(int msgType, const TCHAR* file, const int line, const TCHAR* message) { |
|---|
| 1295 | mainClient.reportMessage(msgType, file, line, message); |
|---|
| 1296 | } |
|---|
| 1297 | void NSAPIStopServer(void) { |
|---|
| 1298 | serviceControll::StopNoWait(SZSERVICENAME); |
|---|
| 1299 | } |
|---|
| 1300 | NSCAPI::nagiosReturn NSAPIInject(const TCHAR* command, const unsigned int argLen, TCHAR **argument, TCHAR *returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR *returnPerfBuffer, unsigned int returnPerfBufferLen) { |
|---|
| 1301 | return mainClient.injectRAW(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); |
|---|
| 1302 | } |
|---|
| 1303 | NSCAPI::errorReturn NSAPIGetSettingsSection(const TCHAR* section, TCHAR*** aBuffer, unsigned int * bufLen) { |
|---|
| 1304 | try { |
|---|
| 1305 | unsigned int len = 0; |
|---|
| 1306 | *aBuffer = arrayBuffer::list2arrayBuffer(Settings::getInstance()->getSection(section), len); |
|---|
| 1307 | *bufLen = len; |
|---|
| 1308 | return NSCAPI::isSuccess; |
|---|
| 1309 | } catch (...) { |
|---|
| 1310 | LOG_ERROR_STD(_T("Failed to getSection: ") + section); |
|---|
| 1311 | return NSCAPI::hasFailed; |
|---|
| 1312 | } |
|---|
| 1313 | } |
|---|
| 1314 | NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(TCHAR*** aBuffer, unsigned int * bufLen) { |
|---|
| 1315 | arrayBuffer::destroyArrayBuffer(*aBuffer, *bufLen); |
|---|
| 1316 | *bufLen = 0; |
|---|
| 1317 | *aBuffer = NULL; |
|---|
| 1318 | return NSCAPI::isSuccess; |
|---|
| 1319 | } |
|---|
| 1320 | |
|---|
| 1321 | NSCAPI::boolReturn NSAPICheckLogMessages(int messageType) { |
|---|
| 1322 | if (mainClient.logDebug()) |
|---|
| 1323 | return NSCAPI::istrue; |
|---|
| 1324 | return NSCAPI::isfalse; |
|---|
| 1325 | } |
|---|
| 1326 | |
|---|
| 1327 | std::wstring Encrypt(std::wstring str, unsigned int algorithm) { |
|---|
| 1328 | unsigned int len = 0; |
|---|
| 1329 | NSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len); |
|---|
| 1330 | len+=2; |
|---|
| 1331 | TCHAR *buf = new TCHAR[len+1]; |
|---|
| 1332 | NSCAPI::errorReturn ret = NSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len); |
|---|
| 1333 | if (ret == NSCAPI::isSuccess) { |
|---|
| 1334 | std::wstring ret = buf; |
|---|
| 1335 | delete [] buf; |
|---|
| 1336 | return ret; |
|---|
| 1337 | } |
|---|
| 1338 | return _T(""); |
|---|
| 1339 | } |
|---|
| 1340 | std::wstring Decrypt(std::wstring str, unsigned int algorithm) { |
|---|
| 1341 | unsigned int len = 0; |
|---|
| 1342 | NSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len); |
|---|
| 1343 | len+=2; |
|---|
| 1344 | TCHAR *buf = new TCHAR[len+1]; |
|---|
| 1345 | NSCAPI::errorReturn ret = NSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len); |
|---|
| 1346 | if (ret == NSCAPI::isSuccess) { |
|---|
| 1347 | std::wstring ret = buf; |
|---|
| 1348 | delete [] buf; |
|---|
| 1349 | return ret; |
|---|
| 1350 | } |
|---|
| 1351 | return _T(""); |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | NSCAPI::errorReturn NSAPIEncrypt(unsigned int algorithm, const TCHAR* inBuffer, unsigned int inBufLen, TCHAR* outBuf, unsigned int *outBufLen) { |
|---|
| 1355 | if (algorithm != NSCAPI::xor) { |
|---|
| 1356 | LOG_ERROR(_T("Unknown algortihm requested.")); |
|---|
| 1357 | return NSCAPI::hasFailed; |
|---|
| 1358 | } |
|---|
| 1359 | std::wstring key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT); |
|---|
| 1360 | int tcharInBufLen = 0; |
|---|
| 1361 | char *c = charEx::tchar_to_char(inBuffer, inBufLen, tcharInBufLen); |
|---|
| 1362 | std::wstring::size_type j=0; |
|---|
| 1363 | for (int i=0;i<tcharInBufLen;i++,j++) { |
|---|
| 1364 | if (j > key.size()) |
|---|
| 1365 | j = 0; |
|---|
| 1366 | c[i] ^= key[j]; |
|---|
| 1367 | } |
|---|
| 1368 | size_t cOutBufLen = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, NULL, NULL); |
|---|
| 1369 | if (!outBuf) { |
|---|
| 1370 | *outBufLen = static_cast<unsigned int>(cOutBufLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work |
|---|
| 1371 | return NSCAPI::isSuccess; |
|---|
| 1372 | } |
|---|
| 1373 | char *cOutBuf = new char[cOutBufLen+1]; |
|---|
| 1374 | size_t len = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, cOutBuf, cOutBufLen); |
|---|
| 1375 | delete [] c; |
|---|
| 1376 | if (len == 0) { |
|---|
| 1377 | LOG_ERROR(_T("Invalid out buffer length.")); |
|---|
| 1378 | return NSCAPI::isInvalidBufferLen; |
|---|
| 1379 | } |
|---|
| 1380 | int realOutLen; |
|---|
| 1381 | TCHAR *realOut = charEx::char_to_tchar(cOutBuf, cOutBufLen, realOutLen); |
|---|
| 1382 | if (static_cast<unsigned int>(realOutLen) >= *outBufLen) { |
|---|
| 1383 | LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated.")); |
|---|
| 1384 | return NSCAPI::isInvalidBufferLen; |
|---|
| 1385 | } |
|---|
| 1386 | wcsncpy_s(outBuf, *outBufLen, realOut, realOutLen); |
|---|
| 1387 | delete [] realOut; |
|---|
| 1388 | outBuf[realOutLen] = 0; |
|---|
| 1389 | *outBufLen = static_cast<unsigned int>(realOutLen); |
|---|
| 1390 | return NSCAPI::isSuccess; |
|---|
| 1391 | } |
|---|
| 1392 | |
|---|
| 1393 | NSCAPI::errorReturn NSAPIDecrypt(unsigned int algorithm, const TCHAR* inBuffer, unsigned int inBufLen, TCHAR* outBuf, unsigned int *outBufLen) { |
|---|
| 1394 | if (algorithm != NSCAPI::xor) { |
|---|
| 1395 | LOG_ERROR(_T("Unknown algortihm requested.")); |
|---|
| 1396 | return NSCAPI::hasFailed; |
|---|
| 1397 | } |
|---|
| 1398 | int inBufLenC = 0; |
|---|
| 1399 | char *inBufferC = charEx::tchar_to_char(inBuffer, inBufLen, inBufLenC); |
|---|
| 1400 | size_t cOutLen = b64::b64_decode(inBufferC, inBufLenC, NULL, NULL); |
|---|
| 1401 | if (!outBuf) { |
|---|
| 1402 | *outBufLen = static_cast<unsigned int>(cOutLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work |
|---|
| 1403 | return NSCAPI::isSuccess; |
|---|
| 1404 | } |
|---|
| 1405 | char *cOutBuf = new char[cOutLen+1]; |
|---|
| 1406 | size_t len = b64::b64_decode(inBufferC, inBufLenC, reinterpret_cast<void*>(cOutBuf), cOutLen); |
|---|
| 1407 | delete [] inBufferC; |
|---|
| 1408 | if (len == 0) { |
|---|
| 1409 | LOG_ERROR(_T("Invalid out buffer length.")); |
|---|
| 1410 | return NSCAPI::isInvalidBufferLen; |
|---|
| 1411 | } |
|---|
| 1412 | int realOutLen; |
|---|
| 1413 | |
|---|
| 1414 | std::wstring key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT); |
|---|
| 1415 | std::wstring::size_type j=0; |
|---|
| 1416 | for (int i=0;i<cOutLen;i++,j++) { |
|---|
| 1417 | if (j > key.size()) |
|---|
| 1418 | j = 0; |
|---|
| 1419 | cOutBuf[i] ^= key[j]; |
|---|
| 1420 | } |
|---|
| 1421 | |
|---|
| 1422 | TCHAR *realOut = charEx::char_to_tchar(cOutBuf, cOutLen, realOutLen); |
|---|
| 1423 | if (static_cast<unsigned int>(realOutLen) >= *outBufLen) { |
|---|
| 1424 | LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated.")); |
|---|
| 1425 | return NSCAPI::isInvalidBufferLen; |
|---|
| 1426 | } |
|---|
| 1427 | wcsncpy_s(outBuf, *outBufLen, realOut, realOutLen); |
|---|
| 1428 | delete [] realOut; |
|---|
| 1429 | outBuf[realOutLen] = 0; |
|---|
| 1430 | *outBufLen = static_cast<unsigned int>(realOutLen); |
|---|
| 1431 | return NSCAPI::isSuccess; |
|---|
| 1432 | } |
|---|
| 1433 | |
|---|
| 1434 | NSCAPI::errorReturn NSAPISetSettingsString(const TCHAR* section, const TCHAR* key, const TCHAR* value) { |
|---|
| 1435 | try { |
|---|
| 1436 | Settings::getInstance()->setString(section, key, value); |
|---|
| 1437 | } catch (...) { |
|---|
| 1438 | LOG_ERROR_STD(_T("Failed to setString: ") + key); |
|---|
| 1439 | return NSCAPI::hasFailed; |
|---|
| 1440 | } |
|---|
| 1441 | return NSCAPI::isSuccess; |
|---|
| 1442 | } |
|---|
| 1443 | NSCAPI::errorReturn NSAPISetSettingsInt(const TCHAR* section, const TCHAR* key, int value) { |
|---|
| 1444 | try { |
|---|
| 1445 | Settings::getInstance()->setInt(section, key, value); |
|---|
| 1446 | } catch (...) { |
|---|
| 1447 | LOG_ERROR_STD(_T("Failed to setInt: ") + key); |
|---|
| 1448 | return NSCAPI::hasFailed; |
|---|
| 1449 | } |
|---|
| 1450 | return NSCAPI::isSuccess; |
|---|
| 1451 | } |
|---|
| 1452 | NSCAPI::errorReturn NSAPIWriteSettings(int type) { |
|---|
| 1453 | try { |
|---|
| 1454 | if (type == NSCAPI::settings_registry) |
|---|
| 1455 | Settings::getInstance()->write(REGSettings::getType()); |
|---|
| 1456 | else if (type == NSCAPI::settings_inifile) |
|---|
| 1457 | Settings::getInstance()->write(INISettings::getType()); |
|---|
| 1458 | else |
|---|
| 1459 | Settings::getInstance()->write(); |
|---|
| 1460 | } catch (SettingsException e) { |
|---|
| 1461 | LOG_ERROR_STD(_T("Failed to write settings: ") + e.getMessage()); |
|---|
| 1462 | return NSCAPI::hasFailed; |
|---|
| 1463 | } catch (...) { |
|---|
| 1464 | LOG_ERROR_STD(_T("Failed to write settings")); |
|---|
| 1465 | return NSCAPI::hasFailed; |
|---|
| 1466 | } |
|---|
| 1467 | return NSCAPI::isSuccess; |
|---|
| 1468 | } |
|---|
| 1469 | NSCAPI::errorReturn NSAPIReadSettings(int type) { |
|---|
| 1470 | try { |
|---|
| 1471 | if (type == NSCAPI::settings_registry) |
|---|
| 1472 | Settings::getInstance()->read(REGSettings::getType()); |
|---|
| 1473 | else if (type == NSCAPI::settings_inifile) |
|---|
| 1474 | Settings::getInstance()->read(INISettings::getType()); |
|---|
| 1475 | else |
|---|
| 1476 | Settings::getInstance()->read(); |
|---|
| 1477 | } catch (SettingsException e) { |
|---|
| 1478 | LOG_ERROR_STD(_T("Failed to read settings: ") + e.getMessage()); |
|---|
| 1479 | return NSCAPI::hasFailed; |
|---|
| 1480 | } catch (...) { |
|---|
| 1481 | LOG_ERROR_STD(_T("Failed to read settings")); |
|---|
| 1482 | return NSCAPI::hasFailed; |
|---|
| 1483 | } |
|---|
| 1484 | return NSCAPI::isSuccess; |
|---|
| 1485 | } |
|---|
| 1486 | NSCAPI::errorReturn NSAPIRehash(int flag) { |
|---|
| 1487 | return NSCAPI::hasFailed; |
|---|
| 1488 | } |
|---|
| 1489 | NSCAPI::errorReturn NSAPIDescribeCommand(const TCHAR* command, TCHAR* buffer, unsigned int bufLen) { |
|---|
| 1490 | return NSCHelper::wrapReturnString(buffer, bufLen, mainClient.describeCommand(command), NSCAPI::isSuccess); |
|---|
| 1491 | } |
|---|
| 1492 | NSCAPI::errorReturn NSAPIGetAllCommandNames(arrayBuffer::arrayBuffer* aBuffer, unsigned int *bufLen) { |
|---|
| 1493 | unsigned int len = 0; |
|---|
| 1494 | *aBuffer = arrayBuffer::list2arrayBuffer(mainClient.getAllCommandNames(), len); |
|---|
| 1495 | *bufLen = len; |
|---|
| 1496 | return NSCAPI::isSuccess; |
|---|
| 1497 | } |
|---|
| 1498 | NSCAPI::errorReturn NSAPIReleaseAllCommandNamessBuffer(TCHAR*** aBuffer, unsigned int * bufLen) { |
|---|
| 1499 | arrayBuffer::destroyArrayBuffer(*aBuffer, *bufLen); |
|---|
| 1500 | *bufLen = 0; |
|---|
| 1501 | *aBuffer = NULL; |
|---|
| 1502 | return NSCAPI::isSuccess; |
|---|
| 1503 | } |
|---|
| 1504 | NSCAPI::errorReturn NSAPIRegisterCommand(const TCHAR* cmd,const TCHAR* desc) { |
|---|
| 1505 | mainClient.registerCommand(cmd, desc); |
|---|
| 1506 | return NSCAPI::isSuccess; |
|---|
| 1507 | } |
|---|
| 1508 | |
|---|
| 1509 | |
|---|
| 1510 | LPVOID NSAPILoader(TCHAR*buffer) { |
|---|
| 1511 | if (_wcsicmp(buffer, _T("NSAPIGetApplicationName")) == 0) |
|---|
| 1512 | return &NSAPIGetApplicationName; |
|---|
| 1513 | if (_wcsicmp(buffer, _T("NSAPIGetApplicationVersionStr")) == 0) |
|---|
| 1514 | return &NSAPIGetApplicationVersionStr; |
|---|
| 1515 | if (_wcsicmp(buffer, _T("NSAPIGetSettingsString")) == 0) |
|---|
| 1516 | return &NSAPIGetSettingsString; |
|---|
| 1517 | if (_wcsicmp(buffer, _T("NSAPIGetSettingsSection")) == 0) |
|---|
| 1518 | return &NSAPIGetSettingsSection; |
|---|
| 1519 | if (_wcsicmp(buffer, _T("NSAPIReleaseSettingsSectionBuffer")) == 0) |
|---|
| 1520 | return &NSAPIReleaseSettingsSectionBuffer; |
|---|
| 1521 | if (_wcsicmp(buffer, _T("NSAPIGetSettingsInt")) == 0) |
|---|
| 1522 | return &NSAPIGetSettingsInt; |
|---|
| 1523 | if (_wcsicmp(buffer, _T("NSAPIMessage")) == 0) |
|---|
| 1524 | return &NSAPIMessage; |
|---|
| 1525 | if (_wcsicmp(buffer, _T("NSAPIStopServer")) == 0) |
|---|
| 1526 | return &NSAPIStopServer; |
|---|
| 1527 | if (_wcsicmp(buffer, _T("NSAPIInject")) == 0) |
|---|
| 1528 | return &NSAPIInject; |
|---|
| 1529 | if (_wcsicmp(buffer, _T("NSAPIGetBasePath")) == 0) |
|---|
| 1530 | return &NSAPIGetBasePath; |
|---|
| 1531 | if (_wcsicmp(buffer, _T("NSAPICheckLogMessages")) == 0) |
|---|
| 1532 | return &NSAPICheckLogMessages; |
|---|
| 1533 | if (_wcsicmp(buffer, _T("NSAPIEncrypt")) == 0) |
|---|
| 1534 | return &NSAPIEncrypt; |
|---|
| 1535 | if (_wcsicmp(buffer, _T("NSAPIDecrypt")) == 0) |
|---|
| 1536 | return &NSAPIDecrypt; |
|---|
| 1537 | if (_wcsicmp(buffer, _T("NSAPISetSettingsString")) == 0) |
|---|
| 1538 | return &NSAPISetSettingsString; |
|---|
| 1539 | if (_wcsicmp(buffer, _T("NSAPISetSettingsInt")) == 0) |
|---|
| 1540 | return &NSAPISetSettingsInt; |
|---|
| 1541 | if (_wcsicmp(buffer, _T("NSAPIWriteSettings")) == 0) |
|---|
| 1542 | return &NSAPIWriteSettings; |
|---|
| 1543 | if (_wcsicmp(buffer, _T("NSAPIReadSettings")) == 0) |
|---|
| 1544 | return &NSAPIReadSettings; |
|---|
| 1545 | if (_wcsicmp(buffer, _T("NSAPIRehash")) == 0) |
|---|
| 1546 | return &NSAPIRehash; |
|---|
| 1547 | if (_wcsicmp(buffer, _T("NSAPIDescribeCommand")) == 0) |
|---|
| 1548 | return &NSAPIDescribeCommand; |
|---|
| 1549 | if (_wcsicmp(buffer, _T("NSAPIGetAllCommandNames")) == 0) |
|---|
| 1550 | return &NSAPIGetAllCommandNames; |
|---|
| 1551 | if (_wcsicmp(buffer, _T("NSAPIReleaseAllCommandNamessBuffer")) == 0) |
|---|
| 1552 | return &NSAPIReleaseAllCommandNamessBuffer; |
|---|
| 1553 | if (_wcsicmp(buffer, _T("NSAPIRegisterCommand")) == 0) |
|---|
| 1554 | return &NSAPIRegisterCommand; |
|---|
| 1555 | |
|---|
| 1556 | return NULL; |
|---|
| 1557 | } |
|---|