00001 #include "otsdaq-core/WebUsersUtilities/RemoteWebUsers.h"
00002
00003 #include "otsdaq-core/SOAPUtilities/SOAPParameters.h"
00004 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
00005 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
00006 #include "otsdaq-core/XmlUtilities/HttpXmlDocument.h"
00007 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00008 #include "otsdaq-core/CgiDataUtilities/CgiDataUtilities.h"
00009 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00010 #include "otsdaq-core/SupervisorDescriptorInfo/SupervisorDescriptorInfo.h"
00011
00012 #include <cstdlib>
00013 #include <cstdio>
00014 #include <vector>
00015
00016
00017 using namespace ots;
00018
00019 #undef __MF_SUBJECT__
00020 #define __MF_SUBJECT__ "RemoteWebUsers"
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 RemoteWebUsers::RemoteWebUsers(xdaq::Application* application)
00090 : SOAPMessenger (application)
00091 {
00092 ActiveUserLastUpdateTime_ = 0;
00093 ActiveUserList_ = "";
00094 }
00095
00096
00097
00098
00099 bool RemoteWebUsers::xmlLoginGateway(
00100 cgicc::Cgicc &cgi,
00101 std::ostringstream *out,
00102 HttpXmlDocument *xmldoc,
00103 const SupervisorDescriptorInfo &theSupervisorsDescriptorInfo,
00104 uint8_t *userPermissions,
00105 const bool refresh,
00106 const uint8_t permissionsThreshold,
00107 const bool checkLock,
00108 const bool lockRequired,
00109 std::string *userWithLock,
00110 std::string *userName,
00111 std::string *displayName,
00112 uint64_t *activeSessionIndex)
00113 {
00114
00115 if(userPermissions) *userPermissions = 0;
00116 if(userWithLock) *userWithLock = "";
00117 if(userName) *userName = "";
00118 if(displayName) *displayName = "";
00119 if(activeSessionIndex) *activeSessionIndex = -1;
00120
00121 const std::string ip = cgi.getEnvironment().getRemoteAddr();
00122
00123
00124
00125
00126
00127
00128
00129 const xdaq::ApplicationDescriptor* gatewaySupervisor;
00130
00131 SOAPParameters parameters;
00132 xoap::MessageReference retMsg;
00133
00134
00135
00136
00137 std::string cookieCode = CgiDataUtilities::getOrPostData(cgi,"CookieCode");
00138
00139
00140
00141
00142
00144
00145 gatewaySupervisor = theSupervisorsDescriptorInfo.getSupervisorDescriptor();
00146 if(!gatewaySupervisor)
00147 {
00148
00149 std::string sequence = CgiDataUtilities::getOrPostData(cgi,"sequence");
00150
00151 if(!sequence.length())
00152 {
00153 __COUT__ << "Invalid attempt." << std::endl;
00154 *out << RemoteWebUsers::REQ_NO_LOGIN_RESPONSE;
00155 return false;
00156 }
00157
00158
00159
00160 gatewaySupervisor = theSupervisorsDescriptorInfo.getWizardDescriptor();
00161 if(!gatewaySupervisor)
00162 {
00163 *out << RemoteWebUsers::REQ_NO_LOGIN_RESPONSE;
00164 return false;
00165 }
00166
00167 parameters.addParameter("sequence",sequence);
00168 retMsg = SOAPMessenger::sendWithSOAPReply(gatewaySupervisor,
00169 "SupervisorSequenceCheck", parameters);
00170 parameters.clear();
00171 parameters.addParameter("Permissions");
00172 receive(retMsg, parameters);
00173
00174 uint8_t tmpUserPermissions_;
00175 sscanf(parameters.getValue("Permissions").c_str(),"%hhu",&tmpUserPermissions_);
00176
00177 if(userPermissions) *userPermissions = tmpUserPermissions_;
00178
00179 if(tmpUserPermissions_ < permissionsThreshold)
00180 {
00181 *out << RemoteWebUsers::REQ_NO_LOGIN_RESPONSE;
00182 __COUT__ << "User has insufficient permissions: " << tmpUserPermissions_ << "<" <<
00183 permissionsThreshold << std::endl;
00184 return false;
00185 }
00186
00187 if(userWithLock) *userWithLock = "admin";
00188 if(userName) *userName = "admin";
00189 if(displayName) *displayName = "Admin";
00190 if(activeSessionIndex) *activeSessionIndex = 0;
00191
00192 return true;
00193 }
00194
00195
00196
00197 parameters.clear();
00198 parameters.addParameter("CookieCode",cookieCode);
00199 parameters.addParameter("RefreshOption",refresh?"1":"0");
00200
00201 retMsg = SOAPMessenger::sendWithSOAPReply(gatewaySupervisor,
00202 "SupervisorCookieCheck", parameters);
00203
00204 parameters.clear();
00205 parameters.addParameter("CookieCode");
00206 parameters.addParameter("Permissions");
00207 parameters.addParameter("UserWithLock");
00208 receive(retMsg, parameters);
00209 tmpUserWithLock_ = parameters.getValue("UserWithLock");
00210 sscanf(parameters.getValue("Permissions").c_str(),"%hhu",&tmpUserPermissions_);
00211 if(userWithLock) *userWithLock = tmpUserWithLock_;
00212 if(userPermissions) *userPermissions = tmpUserPermissions_;
00213
00214 cookieCode = parameters.getValue("CookieCode");
00215
00216
00217
00218 if(cookieCode.length() != COOKIE_CODE_LENGTH)
00219 {
00220 *out << RemoteWebUsers::REQ_NO_LOGIN_RESPONSE;
00221 return false;
00222 }
00223
00224 if(tmpUserPermissions_ < permissionsThreshold)
00225 {
00226 *out << RemoteWebUsers::REQ_NO_PERMISSION_RESPONSE;
00227 __COUT__ << "User has insufficient permissions: " << tmpUserPermissions_ << "<" <<
00228 permissionsThreshold << std::endl;
00229 return false;
00230 }
00231
00232 if(xmldoc)
00233 xmldoc->setHeader(cookieCode);
00234
00235 if(!userName && !displayName && !activeSessionIndex && !checkLock && !lockRequired)
00236 return true;
00237
00238
00239
00240
00242
00243 parameters.clear();
00244 parameters.addParameter("CookieCode",cookieCode);
00245 retMsg = SOAPMessenger::sendWithSOAPReply(gatewaySupervisor,
00246 "SupervisorGetUserInfo", parameters);
00247
00248 parameters.clear();
00249 parameters.addParameter("Username");
00250 parameters.addParameter("DisplayName");
00251 parameters.addParameter("ActiveSessionIndex");
00252 receive(retMsg, parameters);
00253 std::string tmpUserName = parameters.getValue("Username");
00254 if(userName) *userName = tmpUserName;
00255 if(displayName) *displayName = parameters.getValue("DisplayName");
00256 if(activeSessionIndex) *activeSessionIndex = strtoul(parameters.getValue("ActiveSessionIndex").c_str(),0,0);
00257
00258 if(checkLock && tmpUserWithLock_ != "" && tmpUserWithLock_ != tmpUserName)
00259 {
00260 *out << RemoteWebUsers::REQ_USER_LOCKOUT_RESPONSE;
00261 __COUT__ << "User " << tmpUserName << " is locked out. " << tmpUserWithLock_ << " has lock." << std::endl;
00262 return false;
00263 }
00264
00265 if(lockRequired && tmpUserWithLock_ != tmpUserName)
00266 {
00267 *out << RemoteWebUsers::REQ_LOCK_REQUIRED_RESPONSE;
00268 __COUT__ << "User " << tmpUserName << " must have lock to proceed. (" << tmpUserWithLock_ << " has lock.)" << std::endl;
00269 return false;
00270 }
00271
00272 return true;
00273 }
00274
00275
00276
00277
00278 bool RemoteWebUsers::isWizardMode(const SupervisorDescriptorInfo& theSupervisorsDescriptorInfo)
00279 {
00280 return theSupervisorsDescriptorInfo.getWizardDescriptor()?true:false;
00281 }
00282
00283
00284
00285
00286
00287 std::string RemoteWebUsers::getActiveUserList(const xdaq::ApplicationDescriptor* supervisorDescriptor)
00288 {
00289
00290 if(1 || time(0) - ActiveUserLastUpdateTime_ > ACTIVE_USERS_UPDATE_THRESHOLD)
00291 {
00292
00293 __COUT__ << "Need to update " << std::endl;
00294
00295 xoap::MessageReference retMsg = ots::SOAPMessenger::sendWithSOAPReply(supervisorDescriptor,"SupervisorGetActiveUsers");
00296
00297
00298 SOAPParameters retParameters("UserList");
00299 receive(retMsg, retParameters);
00300
00301 ActiveUserLastUpdateTime_ = time(0);
00302 return (ActiveUserList_ = retParameters.getValue("UserList"));
00303 }
00304 else
00305 return ActiveUserList_;
00306 }
00307
00308
00309
00310
00311
00312
00313 std::pair<std::string , ConfigurationGroupKey> RemoteWebUsers::getLastConfigGroup(
00314 const xdaq::ApplicationDescriptor* supervisorDescriptor,
00315 const std::string &actionOfLastGroup,
00316 std::string &actionTimeString)
00317 {
00318 actionTimeString = "";
00319 xoap::MessageReference retMsg = ots::SOAPMessenger::sendWithSOAPReply(
00320 supervisorDescriptor,"SupervisorLastConfigGroupRequest",
00321 SOAPParameters("ActionOfLastGroup",actionOfLastGroup));
00322
00323
00324 SOAPParameters retParameters;
00325 retParameters.addParameter("GroupName");
00326 retParameters.addParameter("GroupKey");
00327 retParameters.addParameter("GroupAction");
00328 retParameters.addParameter("GroupActionTime");
00329 receive(retMsg, retParameters);
00330
00331 std::pair<std::string , ConfigurationGroupKey> theGroup;
00332 if(retParameters.getValue("GroupAction") != actionOfLastGroup)
00333 {
00334 __COUT_WARN__ << "Returned group action '" << retParameters.getValue("GroupAction") <<
00335 "' does not match requested group action '" << actionOfLastGroup << ".'" << std::endl;
00336 return theGroup;
00337 }
00338
00339
00340 theGroup.first = retParameters.getValue("GroupName");
00341 theGroup.second = strtol(retParameters.getValue("GroupKey").c_str(),0,0);
00342 actionTimeString = retParameters.getValue("GroupActionTime");
00343 return theGroup;
00344 }
00345
00346
00347
00348
00349
00350
00351 bool RemoteWebUsers::getUserInfoForCookie(const xdaq::ApplicationDescriptor* supervisorDescriptor,
00352 std::string &cookieCode, std::string *userName, std::string *displayName, uint64_t *activeSessionIndex)
00353 {
00354 __COUT__ << std::endl;
00355 if(cookieCode.length() != COOKIE_CODE_LENGTH) return false;
00356
00357
00358
00359 xoap::MessageReference retMsg = SOAPMessenger::sendWithSOAPReply(supervisorDescriptor, "SupervisorGetUserInfo", SOAPParameters("CookieCode",cookieCode));
00360
00361 SOAPParameters retParameters;
00362 retParameters.addParameter("Username");
00363 retParameters.addParameter("DisplayName");
00364 retParameters.addParameter("ActiveSessionIndex");
00365 receive(retMsg, retParameters);
00366 if(userName) *userName = retParameters.getValue("Username");
00367 if(displayName) *displayName = retParameters.getValue("DisplayName");
00368 if(activeSessionIndex) *activeSessionIndex = strtoul(retParameters.getValue("ActiveSessionIndex").c_str(),0,0);
00369
00370 __COUT__ << "userName " << *userName << std::endl;
00371
00372 return true;
00373 }
00374
00375
00376
00377
00378 bool RemoteWebUsers::cookieCodeIsActiveForRequest(const xdaq::ApplicationDescriptor* supervisorDescriptor,
00379 std::string &cookieCode, uint8_t *userPermissions, std::string ip, bool refresh, std::string *userWithLock)
00380 {
00381
00382 if(cookieCode.length() != COOKIE_CODE_LENGTH) return false;
00383
00384
00385
00386 SOAPParameters parameters;
00387 parameters.addParameter("CookieCode",cookieCode);
00388 parameters.addParameter("RefreshOption",refresh?"1":"0");
00389
00390
00391 xoap::MessageReference retMsg = SOAPMessenger::sendWithSOAPReply(supervisorDescriptor, "SupervisorCookieCheck", parameters);
00392
00393
00394 SOAPParameters retParameters;
00395 retParameters.addParameter("CookieCode");
00396 retParameters.addParameter("Permissions");
00397 retParameters.addParameter("UserWithLock");
00398 receive(retMsg, retParameters);
00399
00400
00401
00402 if(userWithLock) *userWithLock = retParameters.getValue("UserWithLock");
00403 if(userPermissions) sscanf(retParameters.getValue("Permissions").c_str(),"%hhu",userPermissions);
00404
00405 cookieCode = retParameters.getValue("CookieCode");
00406
00407 return cookieCode.length() == COOKIE_CODE_LENGTH;
00408 }
00409
00410
00411
00412
00413 void RemoteWebUsers::sendSystemMessage(const xdaq::ApplicationDescriptor* supervisorDescriptor, const std::string& toUser, const std::string& msg)
00414 {
00415 SOAPParameters parameters;
00416 parameters.addParameter("ToUser" , toUser);
00417 parameters.addParameter("Message", msg);
00418
00419 xoap::MessageReference retMsg = SOAPMessenger::sendWithSOAPReply(supervisorDescriptor, "SupervisorSystemMessage",parameters);
00420 }
00421
00422
00423
00424
00425
00426 void RemoteWebUsers::makeSystemLogbookEntry(const xdaq::ApplicationDescriptor* supervisorDescriptor, const std::string& entryText)
00427 {
00428 SOAPParameters parameters;
00429 parameters.addParameter("EntryText", entryText);
00430
00431 xoap::MessageReference retMsg = SOAPMessenger::sendWithSOAPReply(supervisorDescriptor, "SupervisorSystemLogbookEntry",parameters);
00432 }
00433