| 1 | /**************************************************************************
|
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> *
|
|---|
| 3 | * *
|
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp *
|
|---|
| 5 | * *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by *
|
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or *
|
|---|
| 9 | * (at your option) any later version. *
|
|---|
| 10 | * *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|---|
| 14 | * GNU General Public License for more details. *
|
|---|
| 15 | * *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License *
|
|---|
| 17 | * along with this program; if not, write to the *
|
|---|
| 18 | * Free Software Foundation, Inc., *
|
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|---|
| 20 | ***************************************************************************/
|
|---|
| 21 | #include "stdafx.h"
|
|---|
| 22 | #include "CheckExternalScripts.h"
|
|---|
| 23 | #include <strEx.h>
|
|---|
| 24 | #include <time.h>
|
|---|
| 25 | #include <config.h>
|
|---|
| 26 | #include <msvc_wrappers.h>
|
|---|
| 27 |
|
|---|
| 28 | CheckExternalScripts gCheckExternalScripts;
|
|---|
| 29 |
|
|---|
| 30 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|---|
| 31 | {
|
|---|
| 32 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call);
|
|---|
| 33 | return TRUE;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | CheckExternalScripts::CheckExternalScripts() {}
|
|---|
| 37 | CheckExternalScripts::~CheckExternalScripts() {}
|
|---|
| 38 |
|
|---|
| 39 | void CheckExternalScripts::addAllScriptsFrom(std::wstring path) {
|
|---|
| 40 | std::wstring baseDir;
|
|---|
| 41 | std::wstring::size_type pos = path.find_last_of('*');
|
|---|
| 42 | if (pos == std::wstring::npos) {
|
|---|
| 43 | path += _T("*.*");
|
|---|
| 44 | }
|
|---|
| 45 | WIN32_FIND_DATA wfd;
|
|---|
| 46 | HANDLE hFind = FindFirstFile(path.c_str(), &wfd);
|
|---|
| 47 | if (hFind != INVALID_HANDLE_VALUE) {
|
|---|
| 48 | do {
|
|---|
| 49 | if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
|
|---|
| 50 | addCommand(wfd.cFileName);
|
|---|
| 51 | }
|
|---|
| 52 | } while (FindNextFile(hFind, &wfd));
|
|---|
| 53 | } else {
|
|---|
| 54 | NSC_LOG_ERROR_STD(_T("No scripts found in path: ") + path);
|
|---|
| 55 | return;
|
|---|
| 56 | }
|
|---|
| 57 | FindClose(hFind);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | bool CheckExternalScripts::loadModule() {
|
|---|
| 61 | timeout = NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_TIMEOUT ,EXTSCRIPT_SETTINGS_TIMEOUT_DEFAULT);
|
|---|
| 62 | scriptDirectory_ = NSCModuleHelper::getSettingsString(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_SCRIPTDIR ,EXTSCRIPT_SETTINGS_SCRIPTDIR_DEFAULT);
|
|---|
| 63 | std::list<std::wstring>::const_iterator it;
|
|---|
| 64 | std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(EXTSCRIPT_SCRIPT_SECTION_TITLE);
|
|---|
| 65 | for (it = commands.begin(); it != commands.end(); ++it) {
|
|---|
| 66 | if ((*it).empty())
|
|---|
| 67 | continue;
|
|---|
| 68 | std::wstring s = NSCModuleHelper::getSettingsString(EXTSCRIPT_SCRIPT_SECTION_TITLE, (*it), _T(""));
|
|---|
| 69 | if (s.empty()) {
|
|---|
| 70 | NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it));
|
|---|
| 71 | } else {
|
|---|
| 72 | strEx::token tok = strEx::getToken(s, ' ', true);
|
|---|
| 73 | addCommand((*it).c_str(), tok.first, tok.second);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | commands = NSCModuleHelper::getSettingsSection(EXTSCRIPT_ALIAS_SECTION_TITLE);
|
|---|
| 78 | for (it = commands.begin(); it != commands.end(); ++it) {
|
|---|
| 79 | if ((*it).empty())
|
|---|
| 80 | continue;
|
|---|
| 81 | std::wstring s = NSCModuleHelper::getSettingsString(EXTSCRIPT_ALIAS_SECTION_TITLE, (*it), _T(""));
|
|---|
| 82 | if (s.empty()) {
|
|---|
| 83 | NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it));
|
|---|
| 84 | } else {
|
|---|
| 85 | strEx::token tok = strEx::getToken(s, ' ', true);
|
|---|
| 86 | addAlias((*it).c_str(), tok.first, tok.second);
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | if (!scriptDirectory_.empty()) {
|
|---|
| 91 | addAllScriptsFrom(scriptDirectory_);
|
|---|
| 92 | }
|
|---|
| 93 | root_ = NSCModuleHelper::getBasePath();
|
|---|
| 94 | return true;
|
|---|
| 95 | }
|
|---|
| 96 | bool CheckExternalScripts::unloadModule() {
|
|---|
| 97 | return true;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | bool CheckExternalScripts::hasCommandHandler() {
|
|---|
| 102 | return true;
|
|---|
| 103 | }
|
|---|
| 104 | bool CheckExternalScripts::hasMessageHandler() {
|
|---|
| 105 | return false;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | NSCAPI::nagiosReturn CheckExternalScripts::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) {
|
|---|
| 110 | command_list::const_iterator cit = commands.find(command);
|
|---|
| 111 | bool isAlias = false;
|
|---|
| 112 | if (cit == commands.end()) {
|
|---|
| 113 | cit = alias.find(command);
|
|---|
| 114 | if (cit == alias.end())
|
|---|
| 115 | return NSCAPI::returnIgnored;
|
|---|
| 116 | isAlias = true;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | const command_data cd = (*cit).second;
|
|---|
| 120 | std::wstring args = cd.arguments;
|
|---|
| 121 | if (isAlias || NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_ALLOW_ARGUMENTS, EXTSCRIPT_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 1) {
|
|---|
| 122 | arrayBuffer::arrayList arr = arrayBuffer::arrayBuffer2list(argLen, char_args);
|
|---|
| 123 | arrayBuffer::arrayList::const_iterator cit2 = arr.begin();
|
|---|
| 124 | int i=1;
|
|---|
| 125 |
|
|---|
| 126 | for (;cit2!=arr.end();cit2++,i++) {
|
|---|
| 127 | if (isAlias || NSCModuleHelper::getSettingsInt(EXTSCRIPT_SECTION_TITLE, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META, EXTSCRIPT_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) {
|
|---|
| 128 | if ((*cit2).find_first_of(NASTY_METACHARS) != std::wstring::npos) {
|
|---|
| 129 | NSC_LOG_ERROR(_T("Request string contained illegal metachars!"));
|
|---|
| 130 | return NSCAPI::returnIgnored;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | strEx::replace(args, _T("$ARG") + strEx::itos(i) + _T("$"), (*cit2));
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | if (isAlias) {
|
|---|
| 137 | return NSCModuleHelper::InjectSplitAndCommand(cd.command, args, ' ', message, perf, true);
|
|---|
| 138 | } else {
|
|---|
| 139 | int result = process::executeProcess(root_, cd.command + _T(" ") + args, message, perf, timeout);
|
|---|
| 140 | if (!NSCHelper::isNagiosReturnCode(result)) {
|
|---|
| 141 | NSC_LOG_ERROR_STD(_T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result));
|
|---|
| 142 | return NSCAPI::returnUNKNOWN;
|
|---|
| 143 | }
|
|---|
| 144 | return NSCHelper::int2nagios(result);
|
|---|
| 145 | /*
|
|---|
| 146 | } else if (cd.type == script_dir) {
|
|---|
| 147 | std::wstring args = arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" "));
|
|---|
| 148 | std::wstring cmd = scriptDirectory_ + command.c_str() + _T(" ") +args;
|
|---|
| 149 | return executeNRPECommand(cmd, message, perf);
|
|---|
| 150 | } else {
|
|---|
| 151 | NSC_LOG_ERROR_STD(_T("Unknown script type: ") + command.c_str());
|
|---|
| 152 | return NSCAPI::critical;
|
|---|
| 153 | */
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | NSC_WRAPPERS_MAIN_DEF(gCheckExternalScripts);
|
|---|
| 160 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 161 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckExternalScripts);
|
|---|
| 162 | NSC_WRAPPERS_HANDLE_CONFIGURATION(gCheckExternalScripts);
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 | MODULE_SETTINGS_START(CheckExternalScripts, _T("NRPE Listener configuration"), _T("..."))
|
|---|
| 166 |
|
|---|
| 167 | PAGE(_T("NRPE Listsner configuration"))
|
|---|
| 168 |
|
|---|
| 169 | ITEM_EDIT_TEXT(_T("port"), _T("This is the port the CheckExternalScripts.dll will listen to."))
|
|---|
| 170 | ITEM_MAP_TO(_T("basic_ini_text_mapper"))
|
|---|
| 171 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 172 | OPTION(_T("key"), _T("port"))
|
|---|
| 173 | OPTION(_T("default"), _T("5666"))
|
|---|
| 174 | ITEM_END()
|
|---|
| 175 |
|
|---|
| 176 | ITEM_CHECK_BOOL(_T("allow_arguments"), _T("This option determines whether or not the NRPE daemon will allow clients to specify arguments to commands that are executed."))
|
|---|
| 177 | ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
|
|---|
| 178 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 179 | OPTION(_T("key"), _T("allow_arguments"))
|
|---|
| 180 | OPTION(_T("default"), _T("false"))
|
|---|
| 181 | OPTION(_T("true_value"), _T("1"))
|
|---|
| 182 | OPTION(_T("false_value"), _T("0"))
|
|---|
| 183 | ITEM_END()
|
|---|
| 184 |
|
|---|
| 185 | ITEM_CHECK_BOOL(_T("allow_nasty_meta_chars"), _T("This might have security implications (depending on what you do with the options)"))
|
|---|
| 186 | ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
|
|---|
| 187 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 188 | OPTION(_T("key"), _T("allow_nasty_meta_chars"))
|
|---|
| 189 | OPTION(_T("default"), _T("false"))
|
|---|
| 190 | OPTION(_T("true_value"), _T("1"))
|
|---|
| 191 | OPTION(_T("false_value"), _T("0"))
|
|---|
| 192 | ITEM_END()
|
|---|
| 193 |
|
|---|
| 194 | ITEM_CHECK_BOOL(_T("use_ssl"), _T("This option will enable SSL encryption on the NRPE data socket (this increases security somwhat."))
|
|---|
| 195 | ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
|
|---|
| 196 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 197 | OPTION(_T("key"), _T("use_ssl"))
|
|---|
| 198 | OPTION(_T("default"), _T("true"))
|
|---|
| 199 | OPTION(_T("true_value"), _T("1"))
|
|---|
| 200 | OPTION(_T("false_value"), _T("0"))
|
|---|
| 201 | ITEM_END()
|
|---|
| 202 |
|
|---|
| 203 | PAGE_END()
|
|---|
| 204 | ADVANCED_PAGE(_T("Access configuration"))
|
|---|
| 205 |
|
|---|
| 206 | ITEM_EDIT_OPTIONAL_LIST(_T("Allow connection from:"), _T("This is the hosts that will be allowed to poll performance data from the NRPE server."))
|
|---|
| 207 | OPTION(_T("disabledCaption"), _T("Use global settings (defined previously)"))
|
|---|
| 208 | OPTION(_T("enabledCaption"), _T("Specify hosts for NRPE server"))
|
|---|
| 209 | OPTION(_T("listCaption"), _T("Add all IP addresses (not hosts) which should be able to connect:"))
|
|---|
| 210 | OPTION(_T("separator"), _T(","))
|
|---|
| 211 | OPTION(_T("disabled"), _T(""))
|
|---|
| 212 | ITEM_MAP_TO(_T("basic_ini_text_mapper"))
|
|---|
| 213 | OPTION(_T("section"), _T("NRPE"))
|
|---|
| 214 | OPTION(_T("key"), _T("allowed_hosts"))
|
|---|
| 215 | OPTION(_T("default"), _T(""))
|
|---|
| 216 | ITEM_END()
|
|---|
| 217 |
|
|---|
| 218 | PAGE_END()
|
|---|
| 219 | MODULE_SETTINGS_END()
|
|---|