source: nscp/trunk/NSClient++.cpp @ e47106e

Last change on this file since e47106e was 8fcc179, checked in by Michael Medin <michael@…>, 7 years ago

* empty log message *

  • Property mode set to 100644
File size: 21.8 KB
Line 
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
24
25NSClient mainClient;    // Global core instance.
26bool g_bConsoleLog = false;
27//////////////////////////////////////////////////////////////////////////
28// Startup code
29
30/**
31 * Application startup point
32 *
33 * @param argc Argument count
34 * @param argv[] Argument array
35 * @param envp[] Environment array
36 * @return exit status
37 */
38int main(int argc, TCHAR* argv[], TCHAR* envp[])
39{
40        int nRetCode = 0;
41
42        if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) {
43                if ( _stricmp( "install", argv[1]+1 ) == 0 ) {
44                        g_bConsoleLog = true;
45                        try {
46                                serviceControll::Install(SZSERVICENAME, SZSERVICEDISPLAYNAME, SZDEPENDENCIES);
47                        } catch (const serviceControll::SCException& e) {
48                                LOG_MESSAGE_STD("Service installation failed: " + e.error_);
49                                return -1;
50                        }
51                        try {
52                                serviceControll::SetDescription(SZSERVICENAME, SZSERVICEDESCRIPTION);
53                        } catch (const serviceControll::SCException& e) {
54                                LOG_MESSAGE_STD("Couldn't set service description: " + e.error_);
55                        }
56                        LOG_MESSAGE("Service installed!");
57                } else if ( _stricmp( "uninstall", argv[1]+1 ) == 0 ) {
58                        g_bConsoleLog = true;
59                        try {
60                                serviceControll::Uninstall(SZSERVICENAME);
61                        } catch (const serviceControll::SCException& e) {
62                                LOG_MESSAGE_STD("Service deinstallation failed; " + e.error_);
63                                return -1;
64                        }
65                } else if ( _stricmp( "encrypt", argv[1]+1 ) == 0 ) {
66                        g_bConsoleLog = true;
67                        std::string password;
68                        try {
69                                Settings::getInstance()->setFile(mainClient.getBasePath() + "NSC.ini");
70                        } catch (SettingsException e) {
71                                std::cout << "Could not find settings: " << e.getMessage() << std::endl;;
72                                return 1;
73                        }
74                        std::cout << "Enter password to encrypt (has to be a single word): ";
75                        std::cin >> password;
76                        std::string xor_pwd = Encrypt(password);
77                        std::cout << "obfuscated_password=" << xor_pwd << std::endl;
78                        if (password != Decrypt(xor_pwd))
79                                std::cout << "ERROR: Password did not match!" << std::endl;
80                        Settings::destroyInstance();
81                        return 0;
82                } else if ( _stricmp( "start", argv[1]+1 ) == 0 ) {
83                        g_bConsoleLog = true;
84                        serviceControll::Start(SZSERVICENAME);
85                } else if ( _stricmp( "stop", argv[1]+1 ) == 0 ) {
86                        g_bConsoleLog = true;
87                        serviceControll::Stop(SZSERVICENAME);
88                } else if ( _stricmp( "about", argv[1]+1 ) == 0 ) {
89                        g_bConsoleLog = true;
90                        LOG_MESSAGE(SZAPPNAME " (C) Michael Medin");
91                        LOG_MESSAGE("Version " SZVERSION);
92                } else if ( _stricmp( "version", argv[1]+1 ) == 0 ) {
93                        g_bConsoleLog = true;
94                        LOG_MESSAGE(SZAPPNAME " Version: " SZVERSION);
95                } else if ( _stricmp( "test", argv[1]+1 ) == 0 ) {
96#ifdef _DEBUG
97                        /*
98                        strEx::run_test_getToken();
99                        strEx::run_test_replace();
100                        charEx::run_test_getToken();
101                        arrayBuffer::run_testArrayBuffer();
102                        */
103#endif
104                        g_bConsoleLog = true;
105                        if (!mainClient.InitiateService()) {
106                                LOG_ERROR_STD("Service *NOT* started!");
107                                return -1;
108                        }
109                        LOG_MESSAGE_STD("Using settings from: " + Settings::getInstance()->getActiveType());
110                        LOG_MESSAGE("Enter command to inject or exit to terminate...");
111                        std::string s = "";
112                        std::string buff = "";
113                        std::cin >> s;
114                        while (s != "exit") {
115                                if (std::cin.peek() < 15) {
116                                        buff += s;
117                                        strEx::token t = strEx::getToken(buff, ' ');
118                                        std::string msg, perf;
119                                        NSCAPI::nagiosReturn ret = mainClient.inject(t.first, t.second, ' ', msg, perf);
120                                        if (perf.empty())
121                                                std::cout << NSCHelper::translateReturn(ret) << ":" << msg << std::endl;
122                                        else
123                                                std::cout << NSCHelper::translateReturn(ret) << ":" << msg << "|" << perf << std::endl;
124                                        buff = "";
125                                } else {
126                                        buff += s + " ";
127                                }
128                                std::cin >> s;
129                        }
130                        mainClient.TerminateService();
131                        return 0;
132                } else {
133                        LOG_MESSAGE("Usage: -version, -about, -install, -uninstall, -start, -stop, -encrypt");
134                        LOG_MESSAGE("Usage: <ModuleName> <commnd> [arguments]");
135                }
136                return nRetCode;
137        } else if (argc > 2) {
138                g_bConsoleLog = true;
139                mainClient.InitiateService();
140                if (argc>=3)
141                        mainClient.commandLineExec(argv[1], argv[2], argc-3, &argv[3]);
142                else
143                        mainClient.commandLineExec(argv[1], argv[2], 0, NULL);
144                mainClient.TerminateService();
145                return nRetCode;
146        }
147        mainClient.StartServiceCtrlDispatcher();
148        return nRetCode;
149}
150
151//////////////////////////////////////////////////////////////////////////
152// Service functions
153
154/**
155 * Service control handler startup point.
156 * When the program is started as a service this will be the entry point.
157 */
158bool NSClientT::InitiateService(void) {
159        try {
160                Settings::getInstance()->setFile(getBasePath() + "NSC.ini");
161        } catch (SettingsException e) {
162                LOG_ERROR_STD("Could not find settings: " + e.getMessage());
163                return false;
164        }
165        try {
166                simpleSocket::WSAStartup();
167        } catch (simpleSocket::SocketException e) {
168                LOG_ERROR_STD("Uncaught exception: " + e.getMessage());
169                return false;
170        }
171
172        SettingsT::sectionList list = Settings::getInstance()->getSection("modules");
173        for (SettingsT::sectionList::iterator it = list.begin(); it != list.end(); it++) {
174                try {
175                        loadPlugin(getBasePath() + "modules\\" + (*it));
176                } catch(const NSPluginException& e) {
177                        LOG_ERROR_STD("Exception raised: " + e.error_ + " in module: " + e.file_);
178                        return false;
179                }
180        }
181        loadPlugins();
182        return true;
183}
184/**
185 * Service control handler termination point.
186 * When the program is stopped as a service this will be the "exit point".
187 */
188void NSClientT::TerminateService(void) {
189        try {
190                mainClient.unloadPlugins();
191        } catch(NSPluginException *e) {
192                std::cout << "Exception raised: " << e->error_ << " in module: " << e->file_ << std::endl;;
193        }
194        try {
195                simpleSocket::WSACleanup();
196        } catch (simpleSocket::SocketException e) {
197                LOG_ERROR_STD("Uncaught exception: " + e.getMessage());
198        }
199        Settings::destroyInstance();
200}
201
202/**
203 * Forward this to the main service dispatcher helper class
204 * @param dwArgc
205 * @param *lpszArgv
206 */
207void WINAPI NSClientT::service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv) {
208        mainClient.service_main(dwArgc, lpszArgv);
209}
210/**
211 * Forward this to the main service dispatcher helper class
212 * @param dwCtrlCode
213 */
214void WINAPI NSClientT::service_ctrl_dispatch(DWORD dwCtrlCode) {
215        mainClient.service_ctrl(dwCtrlCode);
216}
217
218//////////////////////////////////////////////////////////////////////////
219// Member functions
220
221int NSClientT::commandLineExec(const char* module, const char* command, const unsigned int argLen, char** args) {
222        std::string sModule = module;
223        ReadLock readLock(&m_mutexRW, true, 10000);
224        if (!readLock.IsLocked()) {
225                LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
226                return -1;
227        }
228        for (pluginList::size_type i=0;i<plugins_.size();++i) {
229                NSCPlugin *p = plugins_[i];
230                if (p->getName() == sModule) {
231                        LOG_DEBUG_STD("Found module: " + p->getName() + "...");
232                        try {
233                                return p->commandLineExec(command, argLen, args);
234                        } catch (NSPluginException e) {
235                                LOG_ERROR_STD("Could not execute command: " + e.error_ + " in " + e.file_);
236                        }
237                }
238        }
239        LOG_ERROR("Module not found.");
240        return 0;
241}
242
243/**
244 * Load a list of plug-ins
245 * @param plugins A list with plug-ins (DLL files) to load
246 */
247void NSClientT::addPlugins(const std::list<std::string> plugins) {
248        ReadLock readLock(&m_mutexRW, true, 10000);
249        if (!readLock.IsLocked()) {
250                LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
251                return;
252        }
253        std::list<std::string>::const_iterator it;
254        for (it = plugins.begin(); it != plugins.end(); ++it) {
255                loadPlugin(*it);
256        }
257}
258/**
259 * Unload all plug-ins (in reversed order)
260 */
261void NSClientT::unloadPlugins() {
262        {
263                WriteLock writeLock(&m_mutexRW, true, 10000);
264                if (!writeLock.IsLocked()) {
265                        LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
266                        return;
267                }
268                commandHandlers_.clear();
269                messageHandlers_.clear();
270        }
271        {
272                ReadLock readLock(&m_mutexRW, true, 10000);
273                if (!readLock.IsLocked()) {
274                        LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
275                        return;
276                }
277                for (pluginList::size_type i=plugins_.size();i>0;i--) {
278                        NSCPlugin *p = plugins_[i-1];
279                        LOG_DEBUG_STD("Unloading plugin: " + p->getName() + "...");
280                        p->unload();
281                }
282        }
283        {
284                WriteLock writeLock(&m_mutexRW, true, 10000);
285                if (!writeLock.IsLocked()) {
286                        LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
287                        return;
288                }
289                for (unsigned int i=plugins_.size();i>0;i--) {
290                        NSCPlugin *p = plugins_[i-1];
291                        plugins_[i-1] = NULL;
292                        delete p;
293                }
294                plugins_.clear();
295        }
296}
297
298void NSClientT::loadPlugins() {
299        ReadLock readLock(&m_mutexRW, true, 10000);
300        if (!readLock.IsLocked()) {
301                LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
302                return;
303        }
304        for (pluginList::iterator it=plugins_.begin(); it != plugins_.end(); ++it) {
305                LOG_DEBUG_STD("Loading plugin: " + (*it)->getName() + "...");
306                (*it)->load_plugin();
307        }
308}
309/**
310 * Load a single plug-in using a DLL filename
311 * @param file The DLL file
312 */
313void NSClientT::loadPlugin(const std::string file) {
314        addPlugin(new NSCPlugin(file));
315}
316/**
317 * Load and add a plugin to various internal structures
318 * @param *plugin The plug-ininstance to load. The pointer is managed by the
319 */
320void NSClientT::addPlugin(plugin_type plugin) {
321        plugin->load_dll();
322        {
323                WriteLock writeLock(&m_mutexRW, true, 10000);
324                if (!writeLock.IsLocked()) {
325                        LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
326                        return;
327                }
328                plugins_.insert(plugins_.end(), plugin);
329                if (plugin->hasCommandHandler())
330                        commandHandlers_.insert(commandHandlers_.end(), plugin);
331                if (plugin->hasMessageHandler())
332                        messageHandlers_.insert(messageHandlers_.end(), plugin);
333        }
334
335}
336
337NSCAPI::nagiosReturn NSClientT::inject(std::string command, std::string arguments, char splitter, std::string &msg, std::string & perf) {
338        unsigned int aLen = 0;
339        char ** aBuf = arrayBuffer::split2arrayBuffer(arguments, splitter, aLen);
340        char * mBuf = new char[1024];
341        char * pBuf = new char[1024];
342        NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), aLen, aBuf, mBuf, 1023, pBuf, 1023);
343        arrayBuffer::destroyArrayBuffer(aBuf, aLen);
344        if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) {
345                delete [] mBuf;
346                delete [] pBuf;
347                return ret;
348        }
349        msg = mBuf;
350        perf = pBuf;
351        delete [] mBuf;
352        delete [] pBuf;
353        return ret;
354}
355
356/**
357 * Inject a command into the plug-in stack.
358 *
359 * @param command Command to inject
360 * @param argLen Length of argument buffer
361 * @param **argument Argument buffer
362 * @param *returnMessageBuffer Message buffer
363 * @param returnMessageBufferLen Length of returnMessageBuffer
364 * @param *returnPerfBuffer Performance data buffer
365 * @param returnPerfBufferLen Length of returnPerfBuffer
366 * @return The command status
367 */
368NSCAPI::nagiosReturn NSClientT::injectRAW(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen) {
369        if (logDebug()) {
370                LOG_DEBUG_STD("Injecting: " + (std::string) command + ": " + arrayBuffer::arrayBuffer2string(argument, argLen, ", "));
371        }
372        ReadLock readLock(&m_mutexRW, true, 5000);
373        if (!readLock.IsLocked()) {
374                LOG_ERROR("FATAL ERROR: Could not get read-mutex.");
375                return NSCAPI::returnUNKNOWN;
376        }
377        for (pluginList::size_type i = 0; i < commandHandlers_.size(); i++) {
378                try {
379                        NSCAPI::nagiosReturn c = commandHandlers_[i]->handleCommand(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
380                        switch (c) {
381                                case NSCAPI::returnInvalidBufferLen:
382                                        LOG_ERROR("Return buffer to small to handle this command.");
383                                        return c;
384                                case NSCAPI::returnIgnored:
385                                        break;
386                                case NSCAPI::returnOK:
387                                case NSCAPI::returnWARN:
388                                case NSCAPI::returnCRIT:
389                                case NSCAPI::returnUNKNOWN:
390                                        LOG_DEBUG_STD("Injected Result: " + NSCHelper::translateReturn(c) + "  --  " + (std::string)(returnMessageBuffer));
391                                        LOG_DEBUG_STD("Injected Performance Result: " +(std::string) returnPerfBuffer);
392                                        return c;
393                                default:
394                                        LOG_ERROR_STD("Unknown error from handleCommand: " + strEx::itos(c));
395                                        return c;
396                        }
397                } catch(const NSPluginException& e) {
398                        LOG_ERROR_STD("Exception raised: " + e.error_ + " in module: " + e.file_);
399                        return NSCAPI::returnCRIT;
400                }
401        }
402        LOG_MESSAGE_STD("No handler for command: '" + command + "'");
403        return NSCAPI::returnIgnored;
404}
405
406bool NSClientT::logDebug() {
407        typedef enum status {unknown, debug, nodebug };
408        static status d = unknown;
409        if (d == unknown) {
410                if (Settings::getInstance()->getInt("log", "debug", 0) == 1)
411                        d = debug;
412                else
413                        d = nodebug;
414        }
415        return (d == debug);
416}
417
418/**
419 * Report a message to all logging enabled modules.
420 *
421 * @param msgType Message type
422 * @param file Filename generally __FILE__
423 * @param line  Line number, generally __LINE__
424 * @param message The message as a human readable string.
425 */
426void NSClientT::reportMessage(int msgType, const char* file, const int line, std::string message) {
427        if ((msgType == NSCAPI::debug)&&(!logDebug())) {
428                return;
429        }
430        {
431                ReadLock readLock(&m_mutexRW, true, 5000);
432                if (!readLock.IsLocked()) {
433                        std::cout << "Message was lost as the core was locked..." << std::endl;
434                        return;
435                }
436                MutexLock lock(messageMutex);
437                if (!lock.hasMutex()) {
438                        std::cout << "Message was lost as the core was locked..." << std::endl;
439                        std::cout << message << std::endl;
440                        return;
441                }
442                if (g_bConsoleLog) {
443                        std::string k = "?";
444                        switch (msgType) {
445                        case NSCAPI::critical:
446                                k ="c";
447                                break;
448                        case NSCAPI::warning:
449                                k ="w";
450                                break;
451                        case NSCAPI::error:
452                                k ="e";
453                                break;
454                        case NSCAPI::log:
455                                k ="l";
456                                break;
457                        case NSCAPI::debug:
458                                k ="d";
459                                break;
460                        }
461                        std::cout << k << " " << file << "(" << line << ") " << message << std::endl;
462                }
463                for (pluginList::size_type i = 0; i< messageHandlers_.size(); i++) {
464                        try {
465                                messageHandlers_[i]->handleMessage(msgType, file, line, message.c_str());
466                        } catch(const NSPluginException& e) {
467                                // Here we are pretty much fucked! (as logging this might cause a loop :)
468                                std::cout << "Caught: " << e.error_ << " when trying to log a message..." << std::endl;
469                                std::cout << "This is *really really* bad, now the world is about to end..." << std::endl;
470                        }
471                }
472        }
473}
474std::string NSClientT::getBasePath(void) {
475        MutexLock lock(internalVariables);
476        if (!lock.hasMutex()) {
477                LOG_ERROR("FATAL ERROR: Could not get mutex.");
478                return "FATAL ERROR";
479        }
480        if (!basePath.empty())
481                return basePath;
482        char* buffer = new char[1024];
483        GetModuleFileName(NULL, buffer, 1023);
484        std::string path = buffer;
485        std::string::size_type pos = path.rfind('\\');
486        basePath = path.substr(0, pos) + "\\";
487        delete [] buffer;
488        Settings::getInstance()->setFile(basePath + "NSC.ini");
489        return basePath;
490}
491
492
493NSCAPI::errorReturn NSAPIGetSettingsString(const char* section, const char* key, const char* defaultValue, char* buffer, unsigned int bufLen) {
494        return NSCHelper::wrapReturnString(buffer, bufLen, Settings::getInstance()->getString(section, key, defaultValue), NSCAPI::isSuccess);
495}
496int NSAPIGetSettingsInt(const char* section, const char* key, int defaultValue) {
497        return Settings::getInstance()->getInt(section, key, defaultValue);
498}
499NSCAPI::errorReturn NSAPIGetBasePath(char*buffer, unsigned int bufLen) {
500        return NSCHelper::wrapReturnString(buffer, bufLen, mainClient.getBasePath(), NSCAPI::isSuccess);
501}
502NSCAPI::errorReturn NSAPIGetApplicationName(char*buffer, unsigned int bufLen) {
503        return NSCHelper::wrapReturnString(buffer, bufLen, SZAPPNAME, NSCAPI::isSuccess);
504}
505NSCAPI::errorReturn NSAPIGetApplicationVersionStr(char*buffer, unsigned int bufLen) {
506        return NSCHelper::wrapReturnString(buffer, bufLen, SZVERSION, NSCAPI::isSuccess);
507}
508void NSAPIMessage(int msgType, const char* file, const int line, const char* message) {
509        mainClient.reportMessage(msgType, file, line, message);
510}
511void NSAPIStopServer(void) {
512        serviceControll::Stop(SZSERVICENAME);
513}
514NSCAPI::nagiosReturn NSAPIInject(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen) {
515        return mainClient.injectRAW(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen);
516}
517NSCAPI::errorReturn NSAPIGetSettingsSection(const char* section, char*** aBuffer, unsigned int * bufLen) {
518        unsigned int len = 0;
519        *aBuffer = arrayBuffer::list2arrayBuffer(Settings::getInstance()->getSection(section), len);
520        *bufLen = len;
521        return NSCAPI::isSuccess;
522}
523
524NSCAPI::boolReturn NSAPICheckLogMessages(int messageType) {
525        if (mainClient.logDebug())
526                return NSCAPI::istrue;
527        return NSCAPI::isfalse;
528}
529
530std::string Encrypt(std::string str, unsigned int algorithm) {
531        unsigned int len = 0;
532        NSAPIEncrypt(algorithm, str.c_str(), str.size(), NULL, &len);
533        len+=2;
534        char *buf = new char[len+1];
535        NSCAPI::errorReturn ret = NSAPIEncrypt(algorithm, str.c_str(), str.size(), buf, &len);
536        if (ret == NSCAPI::isSuccess) {
537                std::string ret = buf;
538                delete [] buf;
539                return ret;
540        }
541        return "";
542}
543std::string Decrypt(std::string str, unsigned int algorithm) {
544        unsigned int len = 0;
545        NSAPIDecrypt(algorithm, str.c_str(), str.size(), NULL, &len);
546        len+=2;
547        char *buf = new char[len+1];
548        NSCAPI::errorReturn ret = NSAPIDecrypt(algorithm, str.c_str(), str.size(), buf, &len);
549        if (ret == NSCAPI::isSuccess) {
550                std::string ret = buf;
551                delete [] buf;
552                return ret;
553        }
554        return "";
555}
556
557NSCAPI::errorReturn NSAPIEncrypt(unsigned int algorithm, const char* inBuffer, unsigned int inBufLen, char* outBuf, unsigned int *outBufLen) {
558        if (algorithm != NSCAPI::xor) {
559                LOG_ERROR("Unknown algortihm requested.");
560                return NSCAPI::hasFailed;
561        }
562        std::string s = inBuffer;
563        std::string key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT);
564        char *c = new char[inBufLen+1];
565        strncpy(c, inBuffer, inBufLen);
566        for (unsigned int i=0,j=0;i<inBufLen;i++,j++) {
567                if (j > key.size())
568                        j = 0;
569                c[i] ^= key[j];
570        }
571        unsigned int len = b64::b64_encode(reinterpret_cast<void*>(c), inBufLen, outBuf, *outBufLen);
572        delete [] c;
573        if (outBuf) {
574                if ((len == 0)||(len >= *outBufLen)) {
575                        LOG_ERROR("Invalid out buffer length.");
576                        return NSCAPI::isInvalidBufferLen;
577                }
578                outBuf[len] = 0;
579                *outBufLen = len;
580        } else {
581                *outBufLen = len;
582        }
583        return NSCAPI::isSuccess;
584}
585
586NSCAPI::errorReturn NSAPIDecrypt(unsigned int algorithm, const char* inBuffer, unsigned int inBufLen, char* outBuf, unsigned int *outBufLen) {
587        if (algorithm != NSCAPI::xor) {
588                LOG_ERROR("Unknown algortihm requested.");
589                return NSCAPI::hasFailed;
590        }
591        unsigned int len =  b64::b64_decode(inBuffer, inBufLen, reinterpret_cast<void*>(outBuf), *outBufLen);
592        if (outBuf) {
593                if ((len == 0)||(len >= *outBufLen)) {
594                        LOG_ERROR("Invalid out buffer length.");
595                        return NSCAPI::isInvalidBufferLen;
596                }
597                std::string key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT);
598                for (unsigned int i=0,j=0;i<len;i++,j++) {
599                        if (j > key.size())
600                                j = 0;
601                        outBuf[i] ^= key[j];
602                }
603                outBuf[len] = 0;
604                *outBufLen = len;
605        } else {
606                *outBufLen = len;
607        }
608        return NSCAPI::isSuccess;
609}
610
611NSCAPI::errorReturn NSAPISetSettingsString(const char* section, const char* key, const char* value) {
612        Settings::getInstance()->setString(section, key, value);
613        return NSCAPI::isSuccess;
614}
615NSCAPI::errorReturn NSAPISetSettingsInt(const char* section, const char* key, int value) {
616        Settings::getInstance()->setInt(section, key, value);
617        return NSCAPI::isSuccess;
618}
619NSCAPI::errorReturn NSAPIWriteSettings(int type) {
620        try {
621                if (type == NSCAPI::settings_registry)
622                        Settings::getInstance()->write(REGSettings::getType());
623                else if (type == NSCAPI::settings_inifile)
624                        Settings::getInstance()->write(INISettings::getType());
625                else
626                        Settings::getInstance()->write();
627        } catch (SettingsException e) {
628                LOG_ERROR_STD(e.getMessage());
629                return NSCAPI::hasFailed;
630        }
631        return NSCAPI::isSuccess;
632}
633NSCAPI::errorReturn NSAPIReadSettings(int type) {
634        try {
635                if (type == NSCAPI::settings_registry)
636                        Settings::getInstance()->read(REGSettings::getType());
637                else if (type == NSCAPI::settings_inifile)
638                        Settings::getInstance()->read(INISettings::getType());
639                else
640                        Settings::getInstance()->read();
641        } catch (SettingsException e) {
642                LOG_ERROR_STD(e.getMessage());
643                return NSCAPI::hasFailed;
644        }
645        return NSCAPI::isSuccess;
646}
647NSCAPI::errorReturn NSAPIRehash(int flag) {
648        return NSCAPI::critical;
649}
650
651
652LPVOID NSAPILoader(char*buffer) {
653        if (stricmp(buffer, "NSAPIGetApplicationName") == 0)
654                return &NSAPIGetApplicationName;
655        if (stricmp(buffer, "NSAPIGetApplicationVersionStr") == 0)
656                return &NSAPIGetApplicationVersionStr;
657        if (stricmp(buffer, "NSAPIGetSettingsSection") == 0)
658                return &NSAPIGetSettingsSection;
659        if (stricmp(buffer, "NSAPIGetSettingsString") == 0)
660                return &NSAPIGetSettingsString;
661        if (stricmp(buffer, "NSAPIGetSettingsInt") == 0)
662                return &NSAPIGetSettingsInt;
663        if (stricmp(buffer, "NSAPIMessage") == 0)
664                return &NSAPIMessage;
665        if (stricmp(buffer, "NSAPIStopServer") == 0)
666                return &NSAPIStopServer;
667        if (stricmp(buffer, "NSAPIInject") == 0)
668                return &NSAPIInject;
669        if (stricmp(buffer, "NSAPIGetBasePath") == 0)
670                return &NSAPIGetBasePath;
671        if (stricmp(buffer, "NSAPICheckLogMessages") == 0)
672                return &NSAPICheckLogMessages;
673        if (stricmp(buffer, "NSAPIEncrypt") == 0)
674                return &NSAPIEncrypt;
675        if (stricmp(buffer, "NSAPIDecrypt") == 0)
676                return &NSAPIDecrypt;
677        if (stricmp(buffer, "NSAPISetSettingsString") == 0)
678                return &NSAPISetSettingsString;
679        if (stricmp(buffer, "NSAPISetSettingsInt") == 0)
680                return &NSAPISetSettingsInt;
681        if (stricmp(buffer, "NSAPIWriteSettings") == 0)
682                return &NSAPIWriteSettings;
683        if (stricmp(buffer, "NSAPIReadSettings") == 0)
684                return &NSAPIReadSettings;
685        if (stricmp(buffer, "NSAPIRehash") == 0)
686                return &NSAPIRehash;
687        return NULL;
688}
Note: See TracBrowser for help on using the repository browser.