firmware: remove compat code from plugin register script #4500

We always assume metadata is present otherwise we end up
with undefined behaviour.
This commit is contained in:
Franco Fichtner 2021-02-15 09:05:13 +01:00
parent a58f21dc10
commit 9fb0b8d677
5 changed files with 45 additions and 45 deletions

View File

@ -45,6 +45,6 @@ if [ "${PACKAGE#os-}" != "${PACKAGE}" ]; then
fi
fi
pkg install -y ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
/usr/local/opnsense/scripts/firmware/register.php install ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
pkg autoremove -y >> ${PKG_PROGRESS_FILE} 2>&1
/usr/local/opnsense/scripts/firmware/register.php install ${PACKAGE}
echo '***DONE***' >> ${PKG_PROGRESS_FILE}

View File

@ -48,7 +48,7 @@ use OPNsense\Core\Config;
$config = Config::getInstance()->object();
function plugins_get($config)
function plugins_config_get($config)
{
$plugins = [];
@ -65,7 +65,7 @@ function plugins_get($config)
return array_flip($plugins);
}
function plugins_set($config, $plugins)
function plugins_config_set($config, $plugins)
{
$config->system->firmware->plugins = implode(',', array_keys($plugins));
@ -80,21 +80,11 @@ function plugins_set($config, $plugins)
Config::getInstance()->save();
}
function plugins_found($name, $found = [])
function plugins_disk_found($name, $found)
{
$bare = preg_replace('/^os-|-devel$/', '', $name);
if (file_exists('/usr/local/opnsense/version/' . $bare)) {
if (!isset($found[$bare])) {
/* backwards compat with non-JSON, remove in 21.7 */
return true;
}
if ($found[$bare] == $name) {
return true;
}
}
return false;
return isset($found[$bare]) && $found[$bare] == $name;
}
function plugins_remove_sibling($name, $plugins)
@ -111,49 +101,59 @@ function plugins_remove_sibling($name, $plugins)
return $plugins;
}
$plugins = plugins_get($config);
function plugins_disk_get()
{
$found = [];
foreach (glob('/usr/local/opnsense/version/*') as $name) {
$filename = basename($name);
if (strpos($filename, 'base') === 0) {
continue;
}
if (strpos($filename, 'kernel') === 0) {
continue;
}
if (strpos($filename, 'core') === 0) {
continue;
}
$ret = json_decode(@file_get_contents($name), true);
if ($ret == null || !isset($ret['product_id'])) {
echo "Ignoring invalid metadata: $name" . PHP_EOL;
continue;
}
$found[$filename] = $ret['product_id'];
}
return $found;
}
$plugins = plugins_config_get($config);
$found = plugins_disk_get();
switch ($action) {
case 'install':
if (!plugins_found($name)) {
if (!plugins_disk_found($name, $found)) {
return;
}
$plugins = plugins_remove_sibling($name, $plugins);
$plugins[$name] = 'hello';
break;
case 'remove':
if (plugins_found($name)) {
if (plugins_disk_found($name, $found)) {
return;
}
if (isset($plugins[$name])) {
unset($plugins[$name]);
}
$plugins = plugins_remove_sibling($name, $plugins);
break;
case 'resync_factory':
/* XXX handle core package */
/* FALLTHROUGH */
case 'resync':
$found = [];
foreach (glob('/usr/local/opnsense/version/*') as $name) {
$filename = basename($name);
if (strpos($filename, 'base') === 0) {
continue;
}
if (strpos($filename, 'kernel') === 0) {
continue;
}
if (strpos($filename, 'core') === 0) {
continue;
}
$ret = json_decode(@file_get_contents($name), true);
if ($ret == null || !isset($ret['product_id'])) {
/* ignore files without valid metadata */
continue;
}
$found[$filename] = $ret['product_id'];
}
foreach (array_keys($plugins) as $name) {
if (!plugins_found($name, $found)) {
if (!plugins_disk_found($name, $found)) {
echo "Unregistering missing plugin: $name" . PHP_EOL;
unset($plugins[$name]);
}
@ -170,4 +170,4 @@ switch ($action) {
exit();
}
plugins_set($config, $plugins);
plugins_config_set($config, $plugins);

View File

@ -56,8 +56,8 @@ elif [ "${PACKAGE}" = "kernel" ]; then
fi
else
opnsense-revert -l ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
/usr/local/opnsense/scripts/firmware/register.php install ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
pkg autoremove -y >> ${PKG_PROGRESS_FILE} 2>&1
/usr/local/opnsense/scripts/firmware/register.php install ${PACKAGE}
fi
if [ -n "${REBOOT}" ]; then

View File

@ -33,6 +33,6 @@ PACKAGE=$1
echo "***GOT REQUEST TO REMOVE: ${PACKAGE}***" >> ${PKG_PROGRESS_FILE}
pkg remove -y ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
/usr/local/opnsense/scripts/firmware/register.php remove ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
pkg autoremove -y >> ${PKG_PROGRESS_FILE} 2>&1
/usr/local/opnsense/scripts/firmware/register.php remove ${PACKAGE}
echo '***DONE***' >> ${PKG_PROGRESS_FILE}

View File

@ -35,7 +35,7 @@ echo "***GOT REQUEST TO SYNC: ${PACKAGES}***" >> ${PKG_PROGRESS_FILE}
for PACKAGE in ${PACKAGES}; do
if ! pkg query %n ${PACKAGE} > /dev/null; then
pkg install -y ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
/usr/local/opnsense/scripts/firmware/register.php install ${PACKAGE}
/usr/local/opnsense/scripts/firmware/register.php install ${PACKAGE} >> ${PKG_PROGRESS_FILE} 2>&1
fi
done
pkg autoremove -y >> ${PKG_PROGRESS_FILE} 2>&1