From f46409bf74e42de6490a7d80f319c132c3d6a43c Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 5 Sep 2017 15:56:17 +0200 Subject: [locations] fix missing locationId on ip/uuid ... ... when the client's ip falls within a known range but its uuid has not been recorded yet and the client requests its config by uuid. --- modules-available/locations/inc/location.inc.php | 37 ++++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'modules-available/locations') diff --git a/modules-available/locations/inc/location.inc.php b/modules-available/locations/inc/location.inc.php index 8b2ef262..e3fd35b7 100644 --- a/modules-available/locations/inc/location.inc.php +++ b/modules-available/locations/inc/location.inc.php @@ -285,29 +285,28 @@ class Location { $locationId = false; $ipLoc = self::getFromIp($ip); - if ($ipLoc !== false && $uuid !== false) { - // Machine ip maps to a location, and we have a client supplied uuid - $uuidLoc = self::getFromMachineUuid($uuid); - if ($uuidLoc === $ipLoc) { - $locationId = $uuidLoc; - } else if ($uuidLoc !== false) { - // Validate that the location the IP maps to is in the chain we get using the - // location determined by the uuid - $uuidLocations = self::getLocationRootChain($uuidLoc); - $ipLocations = self::getLocationRootChain($ipLoc); - if (in_array($uuidLoc, $ipLocations) // UUID loc is further up, OK - || (in_array($ipLoc, $uuidLocations) && count($ipLocations) + 1 >= count($uuidLocations)) // UUID is max one level deeper than IP loc, accept as well - ) { - // Close enough, allow + if ($ipLoc !== false) { + // Set locationId to ipLoc for now, it will be overwritten later if another case applies. + $locationId = $ipLoc; + if ($uuid !== false) { + // Machine ip maps to a location, and we have a client supplied uuid (which might not be known if the client boots for the first time) + $uuidLoc = self::getFromMachineUuid($uuid); + if ($uuidLoc === $ipLoc) { $locationId = $uuidLoc; - } else { + } else if ($uuidLoc !== false) { + // Validate that the location the IP maps to is in the chain we get using the + // location determined by the uuid + $uuidLocations = self::getLocationRootChain($uuidLoc); + $ipLocations = self::getLocationRootChain($ipLoc); + if (in_array($uuidLoc, $ipLocations) // UUID loc is further up, OK + || (in_array($ipLoc, $uuidLocations) && count($ipLocations) + 1 >= count($uuidLocations)) // UUID is max one level deeper than IP loc, accept as well + ) { + // Close enough, allow + $locationId = $uuidLoc; + } // UUID and IP disagree too much, play safe and ignore the UUID - $locationId = $ipLoc; } } - } else if ($ipLoc !== false) { - // No uuid, but ip maps to location; use that - $locationId = $ipLoc; } return $locationId; } -- cgit v1.2.3-55-g7522 From e2af732497399ec6ccaf0da2b39d97348541915c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 3 Nov 2017 13:27:57 +0100 Subject: [locations] Missing typecast for locationid --- modules-available/locations/inc/location.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules-available/locations') diff --git a/modules-available/locations/inc/location.inc.php b/modules-available/locations/inc/location.inc.php index e3fd35b7..1dae4deb 100644 --- a/modules-available/locations/inc/location.inc.php +++ b/modules-available/locations/inc/location.inc.php @@ -66,7 +66,7 @@ class Location 'locationid' => (int)$node['locationid'], 'parentlocationid' => (int)$node['parentlocationid'], 'parents' => $parents, - 'children' => empty($node['children']) ? array() : array_map(function ($item) { return $item['locationid']; }, $node['children']), + 'children' => empty($node['children']) ? array() : array_map(function ($item) { return (int)$item['locationid']; }, $node['children']), 'locationname' => $node['locationname'], 'depth' => $depth, 'isleaf' => true, -- cgit v1.2.3-55-g7522 From eebe3608ea1687b6985ca0114094bec0e4bb3191 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 14 Nov 2017 16:02:44 +0100 Subject: [dozmod] Improve location & exam mode detection --- modules-available/dozmod/api.inc.php | 8 +++++--- modules-available/locations/inc/location.inc.php | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'modules-available/locations') diff --git a/modules-available/dozmod/api.inc.php b/modules-available/dozmod/api.inc.php index de0883a6..74aaa003 100644 --- a/modules-available/dozmod/api.inc.php +++ b/modules-available/dozmod/api.inc.php @@ -149,10 +149,12 @@ function getListForLocations($locationIds, $raw) $key = 'lectures_' . cache_hash($locationIds); $examMode = Request::get('exams', 'normal-mode', 'string') !== 'normal-mode'; $clientServerMismatch = false; - if ($raw && Module::isAvailable('exams')) { + if (Module::isAvailable('exams')) { // If we have the exam mode module, we can enforce a server side check and make sure it agrees with the client $serverExamMode = Exams::isInExamMode($locationIds); - $clientServerMismatch = ($serverExamMode !== $examMode); + if ($raw) { + $clientServerMismatch = ($serverExamMode !== $examMode); + } $examMode = $serverExamMode; } // Only enforce exam mode validity check if the client requests the raw xml data @@ -276,7 +278,7 @@ if (substr($ip, 0, 7) === '::ffff:') { /* lookup location id(s) */ -$location_ids = Location::getFromIp($ip); +$location_ids = Location::getFromIp($ip, true); $location_ids = Location::getLocationRootChain($location_ids); if ($resource === 'list') { diff --git a/modules-available/locations/inc/location.inc.php b/modules-available/locations/inc/location.inc.php index 1dae4deb..476f4c68 100644 --- a/modules-available/locations/inc/location.inc.php +++ b/modules-available/locations/inc/location.inc.php @@ -255,16 +255,25 @@ class Location * Ignores any manually assigned locationid (fixedlocationid). * * @param string $ip IP address of client + * @param bool $honorRoomPlanner consider a fixed location assigned manually by roomplanner * @return bool|int locationid, or false if no match */ - public static function getFromIp($ip) + public static function getFromIp($ip, $honorRoomPlanner = false) { if (Module::get('statistics') !== false) { // Shortcut - try to use subnetlocationid in machine table - $ret = Database::queryFirst("SELECT subnetlocationid FROM machine WHERE clientip = :ip", compact('ip')); + if ($honorRoomPlanner) { + $ret = Database::queryFirst("SELECT locationid AS loc FROM machine + WHERE clientip = :ip + ORDER BY lastseen DESC LIMIT 1", compact('ip')); + } else { + $ret = Database::queryFirst("SELECT subnetlocationid AS loc FROM machine + WHERE clientip = :ip + ORDER BY lastseen DESC LIMIT 1", compact('ip')); + } if ($ret !== false) { - if ($ret['subnetlocationid'] > 0) { - return (int)$ret['subnetlocationid']; + if ($ret['loc'] > 0) { + return (int)$ret['loc']; } return false; } -- cgit v1.2.3-55-g7522