2015-10-22 12:02:15 +02:00
/******************************************************************************
* Icinga 2 *
2017-01-10 15:54:22 +01:00
* Copyright ( C ) 2012 - 2017 Icinga Development Team ( https : //www.icinga.com/) *
2015-10-22 12:02:15 +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 . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# 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
2016-05-10 15:16:35 +02:00
bool InfoHandler : : HandleRequest ( const ApiUser : : Ptr & user , HttpRequest & request , HttpResponse & response , const Dictionary : : Ptr & params )
2015-10-22 12:02:15 +02:00
{
2015-10-22 13:29:06 +02:00
if ( request . RequestUrl - > GetPath ( ) . size ( ) > 2 )
2015-10-22 12:02:15 +02:00
return false ;
if ( request . RequestMethod ! = " GET " )
return false ;
2015-10-22 13:29:06 +02:00
if ( request . RequestUrl - > GetPath ( ) . empty ( ) ) {
response . SetStatus ( 302 , " Found " ) ;
response . AddHeader ( " Location " , " /v1 " ) ;
return true ;
}
2015-10-28 07:21:18 +01:00
if ( request . RequestUrl - > GetPath ( ) [ 0 ] ! = " v1 " | | request . RequestUrl - > GetPath ( ) . size ( ) ! = 1 )
2015-10-22 13:29:06 +02:00
return false ;
2015-10-22 12:02:15 +02:00
response . SetStatus ( 200 , " OK " ) ;
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) " ;
permInfo . push_back ( name ) ;
2015-10-22 12:02:15 +02:00
}
}
2016-05-11 10:28:44 +02:00
if ( request . Headers - > Get ( " accept " ) = = " application/json " ) {
Dictionary : : Ptr result1 = new Dictionary ( ) ;
result1 - > Set ( " user " , user - > GetName ( ) ) ;
result1 - > Set ( " permissions " , Array : : FromVector ( permInfo ) ) ;
2016-08-09 08:44:53 +02:00
result1 - > Set ( " version " , Application : : GetAppVersion ( ) ) ;
2017-04-06 22:20:20 +02:00
result1 - > Set ( " info " , " More information about API requests is available in the documentation at https://docs.icinga.com/icinga2/latest. " ) ;
2016-05-11 10:28:44 +02:00
Array : : Ptr results = new Array ( ) ;
results - > Add ( result1 ) ;
2015-10-22 12:02:15 +02:00
2016-05-11 10:28:44 +02:00
Dictionary : : Ptr result = new Dictionary ( ) ;
result - > Set ( " results " , results ) ;
HttpUtility : : SendJsonBody ( response , result ) ;
} else {
response . AddHeader ( " Content-Type " , " text/html " ) ;
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> " ;
2017-04-06 22:20:20 +02:00
body + = " <p>More information about API requests is available in the <a href= \" https://docs.icinga.com/icinga2/latest \" target= \" _blank \" >documentation</a>.</p></html> " ;
2016-05-11 10:28:44 +02:00
response . WriteBody ( body . CStr ( ) , body . GetLength ( ) ) ;
}
2015-10-22 12:02:15 +02:00
return true ;
}