source: nscp/modules/CheckExternalScripts/CheckExternalScripts.cpp @ 60375ed

0.4.00.4.10.4.2
Last change on this file since 60375ed was 60375ed, checked in by Michael Medin <michael@…>, 3 years ago

Fixed *nix compatiblity issues

  • Property mode set to 100644
File size: 9.2 KB
Line 
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
36CheckExternalScripts gCheckExternalScripts;
37
38CheckExternalScripts::CheckExternalScripts() {}
39CheckExternalScripts::~CheckExternalScripts() {}
40
41void CheckExternalScripts::addAllScriptsFrom(std::wstring str_path) {
42        boost::filesystem::wpath path = str_path;
43        if (path.has_relative_path())
44                path = NSCModuleHelper::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
60bool 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 = NSCModuleHelper::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 = NSCModuleHelper::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 = NSCModuleHelper::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 = NSCModuleHelper::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_ = NSCModuleHelper::getBasePath();
105        return true;
106}
107bool CheckExternalScripts::unloadModule() {
108        return true;
109}
110
111
112bool CheckExternalScripts::hasCommandHandler() {
113        return true;
114}
115bool CheckExternalScripts::hasMessageHandler() {
116        return false;
117}
118
119
120NSCAPI::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 NSCModuleHelper::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 (!NSCHelper::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 NSCHelper::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
169NSC_WRAP_DLL();
170NSC_WRAPPERS_MAIN_DEF(gCheckExternalScripts);
171NSC_WRAPPERS_IGNORE_MSG_DEF();
172NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckExternalScripts);
173NSC_WRAPPERS_HANDLE_CONFIGURATION(gCheckExternalScripts);
174
175
176MODULE_SETTINGS_START(CheckExternalScripts, _T("NRPE Listener configuration"), _T("..."))
177
178PAGE(_T("NRPE Listsner configuration"))
179
180ITEM_EDIT_TEXT(_T("port"), _T("This is the port the CheckExternalScripts.dll will listen to."))
181ITEM_MAP_TO(_T("basic_ini_text_mapper"))
182OPTION(_T("section"), _T("NRPE"))
183OPTION(_T("key"), _T("port"))
184OPTION(_T("default"), _T("5666"))
185ITEM_END()
186
187ITEM_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."))
188ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
189OPTION(_T("section"), _T("NRPE"))
190OPTION(_T("key"), _T("allow_arguments"))
191OPTION(_T("default"), _T("false"))
192OPTION(_T("true_value"), _T("1"))
193OPTION(_T("false_value"), _T("0"))
194ITEM_END()
195
196ITEM_CHECK_BOOL(_T("allow_nasty_meta_chars"), _T("This might have security implications (depending on what you do with the options)"))
197ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
198OPTION(_T("section"), _T("NRPE"))
199OPTION(_T("key"), _T("allow_nasty_meta_chars"))
200OPTION(_T("default"), _T("false"))
201OPTION(_T("true_value"), _T("1"))
202OPTION(_T("false_value"), _T("0"))
203ITEM_END()
204
205ITEM_CHECK_BOOL(_T("use_ssl"), _T("This option will enable SSL encryption on the NRPE data socket (this increases security somwhat."))
206ITEM_MAP_TO(_T("basic_ini_bool_mapper"))
207OPTION(_T("section"), _T("NRPE"))
208OPTION(_T("key"), _T("use_ssl"))
209OPTION(_T("default"), _T("true"))
210OPTION(_T("true_value"), _T("1"))
211OPTION(_T("false_value"), _T("0"))
212ITEM_END()
213
214PAGE_END()
215ADVANCED_PAGE(_T("Access configuration"))
216
217ITEM_EDIT_OPTIONAL_LIST(_T("Allow connection from:"), _T("This is the hosts that will be allowed to poll performance data from the NRPE server."))
218OPTION(_T("disabledCaption"), _T("Use global settings (defined previously)"))
219OPTION(_T("enabledCaption"), _T("Specify hosts for NRPE server"))
220OPTION(_T("listCaption"), _T("Add all IP addresses (not hosts) which should be able to connect:"))
221OPTION(_T("separator"), _T(","))
222OPTION(_T("disabled"), _T(""))
223ITEM_MAP_TO(_T("basic_ini_text_mapper"))
224OPTION(_T("section"), _T("NRPE"))
225OPTION(_T("key"), _T("allowed_hosts"))
226OPTION(_T("default"), _T(""))
227ITEM_END()
228
229PAGE_END()
230MODULE_SETTINGS_END()
Note: See TracBrowser for help on using the repository browser.