2019-02-25 08:48:22 -05:00
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2015-10-22 06:02:15 -04:00
# include "remote/infohandler.hpp"
# include "remote/httputility.hpp"
2016-08-09 02:44:53 -04:00
# include "base/application.hpp"
2015-10-22 06:02:15 -04:00
using namespace icinga ;
2015-10-22 07:29:06 -04:00
REGISTER_URLHANDLER ( " / " , InfoHandler ) ;
2015-10-22 06:02:15 -04:00
2019-02-15 04:33:01 -05:00
bool InfoHandler : : HandleRequest (
2025-06-11 04:28:16 -04:00
const WaitGroup : : Ptr & ,
2026-01-22 06:41:21 -05:00
const HttpApiRequest & request ,
HttpApiResponse & response ,
2026-01-23 07:31:01 -05:00
boost : : asio : : yield_context &
2019-02-15 04:33:01 -05:00
)
2015-10-22 06:02:15 -04:00
{
2019-02-15 04:33:01 -05:00
namespace http = boost : : beast : : http ;
2025-07-23 03:37:05 -04:00
auto url = request . Url ( ) ;
auto user = request . User ( ) ;
auto params = request . Params ( ) ;
2019-02-15 04:33:01 -05:00
if ( url - > GetPath ( ) . size ( ) > 2 )
2015-10-22 06:02:15 -04:00
return false ;
2019-02-15 04:33:01 -05:00
if ( request . method ( ) ! = http : : verb : : get )
2015-10-22 06:02:15 -04:00
return false ;
2019-02-15 04:33:01 -05:00
if ( url - > GetPath ( ) . empty ( ) ) {
response . result ( http : : status : : found ) ;
response . set ( http : : field : : location , " /v1 " ) ;
2015-10-22 07:29:06 -04:00
return true ;
}
2019-02-15 04:33:01 -05:00
if ( url - > GetPath ( ) [ 0 ] ! = " v1 " | | url - > GetPath ( ) . size ( ) ! = 1 )
2015-10-22 07:29:06 -04:00
return false ;
2019-02-15 04:33:01 -05:00
response . result ( http : : status : : ok ) ;
2015-10-22 06:02:15 -04:00
2016-05-11 04:28:44 -04:00
std : : vector < String > permInfo ;
2015-10-22 06:02:15 -04:00
Array : : Ptr permissions = user - > GetPermissions ( ) ;
2016-05-11 04:28:44 -04:00
2015-10-22 06:02:15 -04:00
if ( permissions ) {
ObjectLock olock ( permissions ) ;
2016-08-25 00:19:44 -04:00
for ( const Value & permission : permissions ) {
2015-10-22 06:02:15 -04:00
String name ;
2016-05-11 04:28:44 -04:00
bool hasFilter = false ;
2015-10-22 06:02:15 -04:00
if ( permission . IsObjectType < Dictionary > ( ) ) {
Dictionary : : Ptr dpermission = permission ;
name = dpermission - > Get ( " permission " ) ;
2016-05-11 04:28:44 -04:00
hasFilter = dpermission - > Contains ( " filter " ) ;
2015-10-22 06:02:15 -04:00
} else
name = permission ;
2016-05-11 04:28:44 -04:00
if ( hasFilter )
name + = " (filtered) " ;
2017-11-30 02:19:58 -05:00
permInfo . emplace_back ( std : : move ( name ) ) ;
2015-10-22 06:02:15 -04:00
}
}
2019-02-15 04:33:01 -05:00
if ( request [ http : : field : : accept ] = = " application/json " ) {
2018-01-11 05:17:38 -05:00
Dictionary : : Ptr result1 = new Dictionary ( {
{ " user " , user - > GetName ( ) } ,
{ " permissions " , Array : : FromVector ( permInfo ) } ,
{ " version " , Application : : GetAppVersion ( ) } ,
2019-08-27 10:47:07 -04:00
{ " info " , " More information about API requests is available in the documentation at https://icinga.com/docs/icinga2/latest/ " }
2018-01-11 05:17:38 -05:00
} ) ;
Dictionary : : Ptr result = new Dictionary ( {
{ " results " , new Array ( { result1 } ) }
} ) ;
2016-05-11 04:28:44 -04:00
2017-12-20 09:31:05 -05:00
HttpUtility : : SendJsonBody ( response , params , result ) ;
2016-05-11 04:28:44 -04:00
} else {
2019-02-15 04:33:01 -05:00
response . set ( http : : field : : content_type , " text/html " ) ;
2016-05-11 04:28:44 -04:00
2025-07-23 03:37:05 -04:00
auto & body = response . body ( ) ;
body < < " <html><head><title>Icinga 2</title></head><h1>Hello from Icinga 2 (Version: "
< < Application : : GetAppVersion ( ) < < " )!</h1> "
< < " <p>You are authenticated as <b> " < < user - > GetName ( ) < < " </b>. " ;
2016-05-11 04:28:44 -04:00
if ( ! permInfo . empty ( ) ) {
2025-07-23 03:37:05 -04:00
body < < " Your user has the following permissions:</p> <ul> " ;
2016-05-11 04:28:44 -04:00
2016-08-25 00:19:44 -04:00
for ( const String & perm : permInfo ) {
2025-07-23 03:37:05 -04:00
body < < " <li> " < < perm < < " </li> " ;
2016-05-11 04:28:44 -04:00
}
2025-07-23 03:37:05 -04:00
body < < " </ul> " ;
2016-05-11 04:28:44 -04:00
} else
2025-07-23 03:37:05 -04:00
body < < " Your user does not have any permissions.</p> " ;
2016-05-11 04:28:44 -04:00
2025-07-23 03:37:05 -04:00
body < < R " (<p>More information about API requests is available in the <a href= " https : //icinga.com/docs/icinga2/latest/" target="_blank">documentation</a>.</p></html>)";
2016-05-11 04:28:44 -04:00
}
2015-10-22 06:02:15 -04:00
return true ;
}