2013-06-13 11:33:00 +02:00
/******************************************************************************
* Icinga 2 *
2016-01-12 08:29:59 +01:00
* Copyright ( C ) 2012 - 2016 Icinga Development Team ( https : //www.icinga.org/) *
2013-06-13 11:33:00 +02:00
* *
* This program is free software ; you can redistribute it and / or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation ; either version 2 *
* of the License , or ( at your option ) any later version . *
* *
* This program is distributed in the hope that it will be useful , *
* but WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the *
* GNU General Public License for more details . *
* *
* You should have received a copy of the GNU General Public License *
* along with this program ; if not , write to the Free Software Foundation *
* Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 , USA . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2014-05-25 16:23:35 +02:00
# include "icinga/command.hpp"
2015-03-28 11:04:42 +01:00
# include "icinga/command.tcpp"
2015-02-11 16:08:02 +01:00
# include "icinga/macroprocessor.hpp"
2014-12-18 15:43:01 +01:00
# include "base/exception.hpp"
2015-02-11 13:12:08 +01:00
# include "base/objectlock.hpp"
# include <boost/foreach.hpp>
2013-06-13 11:33:00 +02:00
using namespace icinga ;
2013-11-08 16:07:21 +01:00
REGISTER_TYPE ( Command ) ;
2013-11-08 11:17:46 +01:00
2014-11-30 23:32:13 +01:00
void Command : : Validate ( int types , const ValidationUtils & utils )
2014-07-16 12:58:18 +02:00
{
2014-11-30 23:32:13 +01:00
ObjectImpl < Command > : : Validate ( types , utils ) ;
2014-07-16 12:58:18 +02:00
2014-11-30 23:32:13 +01:00
Dictionary : : Ptr arguments = GetArguments ( ) ;
2015-02-11 13:12:08 +01:00
2014-11-30 23:32:13 +01:00
if ( ! ( types & FAConfig ) )
2015-02-11 13:12:08 +01:00
return ;
2014-11-30 23:32:13 +01:00
if ( arguments ) {
if ( ! GetCommandLine ( ) . IsObjectType < Array > ( ) )
BOOST_THROW_EXCEPTION ( ValidationError ( this , boost : : assign : : list_of ( " command " ) , " Attribute 'command' must be an array if the 'arguments' attribute is set. " ) ) ;
2015-02-11 13:12:08 +01:00
2014-11-30 23:32:13 +01:00
ObjectLock olock ( arguments ) ;
BOOST_FOREACH ( const Dictionary : : Pair & kv , arguments ) {
const Value & arginfo = kv . second ;
Value argval ;
2015-02-11 13:12:08 +01:00
2014-11-30 23:32:13 +01:00
if ( arginfo . IsObjectType < Dictionary > ( ) ) {
Dictionary : : Ptr argdict = arginfo ;
2015-03-12 18:05:58 +01:00
2014-11-30 23:32:13 +01:00
if ( argdict - > Contains ( " value " ) ) {
2015-11-10 07:59:10 +01:00
Value argvalue = argdict - > Get ( " value " ) ;
2014-11-30 23:32:13 +01:00
2015-11-10 07:59:10 +01:00
if ( argvalue . IsString ( ) & & ! MacroProcessor : : ValidateMacroString ( argvalue ) )
2014-11-30 23:32:13 +01:00
BOOST_THROW_EXCEPTION ( ValidationError ( this , boost : : assign : : list_of < String > ( " arguments " ) ( kv . first ) ( " value " ) , " Validation failed: Closing $ not found in macro format string ' " + argvalue + " '. " ) ) ;
2015-03-12 18:05:58 +01:00
}
2014-11-30 23:32:13 +01:00
if ( argdict - > Contains ( " set_if " ) ) {
2015-11-10 07:59:10 +01:00
Value argsetif = argdict - > Get ( " set_if " ) ;
2015-03-12 18:05:58 +01:00
2015-11-10 07:59:10 +01:00
if ( argsetif . IsString ( ) & & ! MacroProcessor : : ValidateMacroString ( argsetif ) )
2014-11-30 23:32:13 +01:00
BOOST_THROW_EXCEPTION ( ValidationError ( this , boost : : assign : : list_of < String > ( " arguments " ) ( kv . first ) ( " set_if " ) , " Closing $ not found in macro format string ' " + argsetif + " '. " ) ) ;
2015-03-12 18:05:58 +01:00
}
2015-11-10 07:59:10 +01:00
} else if ( arginfo . IsString ( ) ) {
if ( ! MacroProcessor : : ValidateMacroString ( arginfo ) )
BOOST_THROW_EXCEPTION ( ValidationError ( this , boost : : assign : : list_of < String > ( " arguments " ) ( kv . first ) , " Closing $ not found in macro format string ' " + arginfo + " '. " ) ) ;
2015-03-12 18:05:58 +01:00
}
2015-02-11 13:12:08 +01:00
}
}
2014-11-30 23:32:13 +01:00
Dictionary : : Ptr env = GetEnv ( ) ;
2015-02-11 13:12:08 +01:00
2014-11-30 23:32:13 +01:00
if ( env ) {
ObjectLock olock ( env ) ;
BOOST_FOREACH ( const Dictionary : : Pair & kv , env ) {
const Value & envval = kv . second ;
2015-02-11 13:12:08 +01:00
2014-11-30 23:32:13 +01:00
if ( ! envval . IsString ( ) | | envval . IsEmpty ( ) )
continue ;
2015-02-11 13:12:08 +01:00
2014-11-30 23:32:13 +01:00
if ( ! MacroProcessor : : ValidateMacroString ( envval ) )
BOOST_THROW_EXCEPTION ( ValidationError ( this , boost : : assign : : list_of < String > ( " env " ) ( kv . first ) , " Closing $ not found in macro format string ' " + envval + " '. " ) ) ;
2015-02-11 13:12:08 +01:00
}
}
}