| 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 <time.h>
|
|---|
| 24 |
|
|---|
| 25 | #include <settings/macros.h>
|
|---|
| 26 | #include <msvc_wrappers.h>
|
|---|
| 27 | #include <config.h>
|
|---|
| 28 | #include <strEx.h>
|
|---|
| 29 | #include <file_helpers.hpp>
|
|---|
| 30 | #include <file_helpers.hpp>
|
|---|
| 31 |
|
|---|
| 32 | #include <boost/regex.hpp>
|
|---|
| 33 | #include <boost/filesystem.hpp>
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | CheckExternalScripts gCheckExternalScripts;
|
|---|
| 37 |
|
|---|
| 38 | CheckExternalScripts::CheckExternalScripts() {}
|
|---|
| 39 | CheckExternalScripts::~CheckExternalScripts() {}
|
|---|
| 40 |
|
|---|
| 41 | void CheckExternalScripts::addAllScriptsFrom(std::wstring str_path) {
|
|---|
| 42 | boost::filesystem::wpath path = str_path;
|
|---|
| 43 | if (path.has_relative_path())
|
|---|
| 44 | path = GET_CORE()->getBasePath() / path;
|
|---|
| 45 | file_helpers::patterns::pattern_type split_path = file_helpers::patterns::split_pattern(path);
|
|---|
| 46 | if (!boost::filesystem::is_directory(split_path.first))
|
|---|
| 47 | NSC_LOG_ERROR_STD(_T("Path was not found: ") + split_path.first.string());
|
|---|
| 48 |
|
|---|
| 49 | boost::wregex pattern(split_path.second);
|
|---|
| 50 | boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end
|
|---|
| 51 | for ( boost::filesystem::wdirectory_iterator itr( split_path.first ); itr != end_itr; ++itr ) {
|
|---|
| 52 | if ( !is_directory(itr->status()) ) {
|
|---|
| 53 | std::wstring name = itr->path().leaf();
|
|---|
| 54 | if (regex_match(name, pattern))
|
|---|
| 55 | addCommand(name, (split_path.first / name).string(), _T(""));
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | bool CheckExternalScripts::loadModule(NSCAPI::moduleLoadMode mode) {
|
|---|
| 61 | SETTINGS_REG_PATH(external_scripts::SECTION);
|
|---|
| 62 | SETTINGS_REG_PATH(external_scripts::SCRIPT_SECTION);
|
|---|
| 63 | SETTINGS_REG_PATH(external_scripts::ALIAS_SECTION);
|
|---|
| 64 | SETTINGS_REG_KEY_I(external_scripts::TIMEOUT);
|
|---|
| 65 | SETTINGS_REG_KEY_S(external_scripts::SCRIPT_PATH);
|
|---|
| 66 | SETTINGS_REG_KEY_B(external_scripts::ALLOW_ARGS);
|
|---|
| 67 | SETTINGS_REG_KEY_B(external_scripts::ALLOW_NASTY);
|
|---|
| 68 |
|
|---|
| 69 | timeout = SETTINGS_GET_INT(external_scripts::TIMEOUT);
|
|---|
| 70 | scriptDirectory_ = SETTINGS_GET_STRING(external_scripts::SCRIPT_PATH);
|
|---|
| 71 | allowArgs_ = SETTINGS_GET_BOOL(nrpe::ALLOW_ARGS);
|
|---|
| 72 | allowNasty_ = SETTINGS_GET_BOOL(nrpe::ALLOW_NASTY);
|
|---|
| 73 | std::list<std::wstring>::const_iterator it;
|
|---|
| 74 | std::list<std::wstring> commands = GET_CORE()->getSettingsSection(setting_keys::external_scripts::SCRIPT_SECTION_PATH);
|
|---|
| 75 | for (it = commands.begin(); it != commands.end(); ++it) {
|
|---|
| 76 | if ((*it).empty())
|
|---|
| 77 | continue;
|
|---|
| 78 | NSC_DEBUG_MSG_STD(_T("Looking under: ") + setting_keys::external_scripts::SCRIPT_SECTION_PATH + _T(", ") + (*it));
|
|---|
| 79 | std::wstring s = GET_CORE()->getSettingsString(setting_keys::external_scripts::SCRIPT_SECTION_PATH, (*it), _T(""));
|
|---|
| 80 | if (s.empty()) {
|
|---|
| 81 | NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it));
|
|---|
| 82 | } else {
|
|---|
| 83 | strEx::token tok = strEx::getToken(s, ' ', true);
|
|---|
| 84 | addCommand((*it).c_str(), tok.first, tok.second);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | commands = GET_CORE()->getSettingsSection(setting_keys::external_scripts::ALIAS_SECTION_PATH);
|
|---|
| 89 | for (it = commands.begin(); it != commands.end(); ++it) {
|
|---|
| 90 | if ((*it).empty())
|
|---|
| 91 | continue;
|
|---|
| 92 | std::wstring s = GET_CORE()->getSettingsString(setting_keys::external_scripts::ALIAS_SECTION_PATH, (*it), _T(""));
|
|---|
| 93 | if (s.empty()) {
|
|---|
| 94 | NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it));
|
|---|
| 95 | } else {
|
|---|
| 96 | strEx::token tok = strEx::getToken(s, ' ', true);
|
|---|
| 97 | addAlias((*it).c_str(), tok.first, tok.second);
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | if (!scriptDirectory_.empty()) {
|
|---|
| 102 | addAllScriptsFrom(scriptDirectory_);
|
|---|
| 103 | }
|
|---|
| 104 | root_ = GET_CORE()->getBasePath();
|
|---|
| 105 | return true;
|
|---|
| 106 | }
|
|---|
| 107 | bool CheckExternalScripts::unloadModule() {
|
|---|
| 108 | return true;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | bool CheckExternalScripts::hasCommandHandler() {
|
|---|
| 113 | return true;
|
|---|
| 114 | }
|
|---|
| 115 | bool CheckExternalScripts::hasMessageHandler() {
|
|---|
| 116 | return false;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | NSCAPI::nagiosReturn CheckExternalScripts::handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf) {
|
|---|
| 121 | std::wstring cmd = command.c_str();
|
|---|
| 122 | boost::to_lower(cmd);
|
|---|
| 123 | command_list::const_iterator cit = commands.find(cmd);
|
|---|
| 124 | bool isAlias = false;
|
|---|
| 125 | if (cit == commands.end()) {
|
|---|
| 126 | cit = alias.find(cmd);
|
|---|
| 127 | if (cit == alias.end())
|
|---|
| 128 | return NSCAPI::returnIgnored;
|
|---|
| 129 | isAlias = true;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | const command_data cd = (*cit).second;
|
|---|
| 133 | std::wstring args = cd.arguments;
|
|---|
| 134 | if (isAlias || allowArgs_) {
|
|---|
| 135 | int i=1;
|
|---|
| 136 | BOOST_FOREACH(std::wstring str, arguments) {
|
|---|
| 137 | if (isAlias || allowNasty_) {
|
|---|
| 138 | if (str.find_first_of(NASTY_METACHARS) != std::wstring::npos) {
|
|---|
| 139 | NSC_LOG_ERROR(_T("Request string contained illegal metachars!"));
|
|---|
| 140 | return NSCAPI::returnIgnored;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | strEx::replace(args, _T("$ARG") + strEx::itos(i) + _T("$"), str);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | if (isAlias) {
|
|---|
| 147 | return GET_CORE()->InjectSplitAndCommand(cd.command, args, ' ', message, perf, true);
|
|---|
| 148 | } else {
|
|---|
| 149 | int result = process::executeProcess(process::exec_arguments(root_, cd.command + _T(" ") + args, timeout), message, perf);
|
|---|
| 150 | if (!nscapi::plugin_helper::isNagiosReturnCode(result)) {
|
|---|
| 151 | NSC_LOG_ERROR_STD(_T("The command (") + cd.command + _T(") returned an invalid return code: ") + strEx::itos(result));
|
|---|
| 152 | return NSCAPI::returnUNKNOWN;
|
|---|
| 153 | }
|
|---|
| 154 | return nscapi::plugin_helper::int2nagios(result);
|
|---|
| 155 | /*
|
|---|
| 156 | } else if (cd.type == script_dir) {
|
|---|
| 157 | std::wstring args = arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" "));
|
|---|
| 158 | std::wstring cmd = scriptDirectory_ + command.c_str() + _T(" ") +args;
|
|---|
| 159 | return executeNRPECommand(cmd, message, perf);
|
|---|
| 160 | } else {
|
|---|
| 161 | NSC_LOG_ERROR_STD(_T("Unknown script type: ") + command.c_str());
|
|---|
| 162 | return NSCAPI::critical;
|
|---|
| 163 | */
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | NSC_WRAP_DLL();
|
|---|
| 170 | NSC_WRAPPERS_MAIN_DEF(gCheckExternalScripts);
|
|---|
| 171 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 172 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckExternalScripts);
|
|---|
| 173 |
|
|---|