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