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