Skip to content

Commit 18de267

Browse files
committed
let webem session manager also track script running hosts
1 parent 9f24b8a commit 18de267

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

webserver/cWebem.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,7 @@ static void GetURICommandParameter(const std::string &uri, std::string &cmdparam
14581458
bool cWebemRequestHandler::CheckAuthentication(WebEmSession & session, const request& req, reply& rep)
14591459
{
14601460
session.rights = -1; // no rights
1461+
session.id = "";
14611462

14621463
if (myWebem->m_userpasswords.size() == 0)
14631464
{
@@ -1584,7 +1585,7 @@ bool cWebemRequestHandler::CheckAuthentication(WebEmSession & session, const req
15841585
}
15851586
}
15861587

1587-
if (session.rights == 2)
1588+
if ((session.rights == 2) && (session.id.empty()))
15881589
{
15891590
session.isnew = true;
15901591
return true;
@@ -1994,14 +1995,44 @@ void cWebemRequestHandler::handle_request(const request& req, reply& rep)
19941995
// Set timeout to make session in use
19951996
session.timeout = mytime(NULL) + SHORT_SESSION_TIMEOUT;
19961997

1997-
if (session.isnew == true) {
1998-
bool isJson = (req.uri.find("json.htm") != std::string::npos);
1999-
if (isJson && (session.remote_host == "127.0.0.1"))
1998+
if ((session.isnew == true) && (session.rights == 2) && (req.uri.find("json.htm") != std::string::npos))
1999+
{
2000+
// client is possibly a script that does not send cookies - see if we have the IP address registered as a session ID
2001+
WebEmSession* memSession = myWebem->GetSession(session.remote_host);
2002+
time_t now = mytime(NULL);
2003+
if (memSession != NULL)
20002004
{
2001-
// never create sessions for script connections that originate from localhost
2002-
return;
2005+
if (memSession->expires < now)
2006+
{
2007+
myWebem->RemoveSession(session.remote_host);
2008+
}
2009+
else
2010+
{
2011+
session.isnew = false;
2012+
if (memSession->expires - (SHORT_SESSION_TIMEOUT / 2) < now)
2013+
{
2014+
memSession->expires = now + SHORT_SESSION_TIMEOUT;
2015+
2016+
// unsure about the point of the forced removal of 'live' sessions and restore from
2017+
// database but these 'fake' sessions are memory only and can't be restored that way.
2018+
// Should I do a RemoveSession() followed by a AddSession()?
2019+
// For now: keep 'timeout' in sync with 'expires'
2020+
memSession->timeout = memSession->expires;
2021+
}
2022+
}
2023+
}
2024+
2025+
if (session.isnew == true) {
2026+
// register a 'fake' IP based session so we can reference that if the client returns here
2027+
session.id = session.remote_host;
2028+
session.rights = -1; // predictable session ID must have no rights
2029+
session.expires = session.timeout;
2030+
myWebem->AddSession(session);
2031+
session.rights = 2; // restore session rights
20032032
}
2033+
}
20042034

2035+
if (session.isnew == true) {
20052036
_log.Log(LOG_STATUS,"Incoming connection from: %s", session.remote_host.c_str());
20062037
// Create a new session ID
20072038
session.id = generateSessionID();
@@ -2012,11 +2043,8 @@ void cWebemRequestHandler::handle_request(const request& req, reply& rep)
20122043
}
20132044
session.auth_token = generateAuthToken(session, req); // do it after expires to save it also
20142045
session.isnew = false;
2015-
//GB3 Todo: need sane way to keep track of scripts running on other hosts
2016-
if (!isJson)
2017-
myWebem->AddSession(session);
2046+
myWebem->AddSession(session);
20182047
send_cookie(rep, session);
2019-
20202048
} else if (session.forcelogin == true) {
20212049
#ifdef DEBUG_WWW
20222050
_log.Log(LOG_STATUS, "[web:%s] Logout : remove session %s", myWebem->GetPort().c_str(), session.id.c_str());

0 commit comments

Comments
 (0)