##
# Copyright (с) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2022 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# https://www.cloudlinux.com/legal/
##
import logging
import os
import pwd
from datetime import datetime, timedelta
from enum import Enum
from clcommon.cpapi import docroot
from secureio import disable_quota
from xray import gettext as _
from xray.internal.clwpos_safe_imports import wp_get_constant
from xray.internal.utils import repair_dir_mode, user_context
logger = logging.getLogger('smart_advice_plugin_helpers')
class PluginStatuses(Enum):
"""
INSTALLED = plugin exists in MU directory
ERROR = plugin should have been installed: there are advices w/o incompatibility
and created_at > 2days (wordpress-install cron expected to trigger install)
NOT_INSTALLED = plugin does not exist in MU directory:
in ideal world it means whether there are no compatible advices or
wordpress-plugin install cron has not worked yet
"""
INSTALLED = 'INSTALLED'
ERROR = 'ERROR'
NOT_INSTALLED = 'NOT_INSTALLED'
def create_mu_plugins_dir_if_not_exist(user, mu_plugin_dir):
"""
Creates MU plugin dir if does not exist yet
"""
user_data = pwd.getpwnam(user)
if os.path.isdir(mu_plugin_dir):
# Already-broken hosts: the wordpress-plugin-install cron ran under
# umask 077 from 0.6-51, so a directory it created back then is still
# 0700 and a web server that does not run as the site owner cannot read
# the must-use plugin (ZD 290458). We only ever create this directory
# when it is absent, so it never gets a second chance to be fixed.
# repair_dir_mode touches it only if it is EXACTLY 0700, so a site that
# deliberately locked it down differently is left alone.
with user_context(user_data.pw_uid, user_data.pw_gid), disable_quota():
repair_dir_mode(os.path.realpath(mu_plugin_dir), 0o700, 0o755)
return
with user_context(user_data.pw_uid, user_data.pw_gid), disable_quota():
try:
os.makedirs(mu_plugin_dir)
# makedirs()' mode is masked by the ambient umask, and the
# wordpress-plugin-install cron used to run under umask 077, which
# left mu-plugins at 0700 -- unreadable to a web server that does
# not run as the site owner (ZD 290458). Pin the 0755 WordPress
# expects on the leaf we just created.
os.chmod(mu_plugin_dir, 0o755)
except OSError as e:
logger.error("Failed creating must use plugin folder. Path: %s. Reason: %s", mu_plugin_dir, str(e))
raise ValueError(_('Unable to create MU plugins directory'))
def get_mu_directory(user, full_wp_path):
"""
Resolve path to MU plugins directory for given site.
Fast path: