"""
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.


This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.


You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.

Copyright © 2019 Cloud Linux Software Inc.

This software is also available under ImunifyAV commercial license,
see <https://www.imunify360.com/legal/eula>

Optional cleaner diagnostics; local paths never form telemetry fields."""

import json
from collections import OrderedDict
import re
import time

ERRNOS = {
    "EACCES",
    "EPERM",
    "ENOENT",
    "ENOTDIR",
    "EIO",
    "ENOSPC",
    "EROFS",
    "ELOOP",
    "ENAMETOOLONG",
    "EISDIR",
    "EMFILE",
    "ENFILE",
    "ETXTBSY",
}
OPERATIONS = {
    "stat",
    "traverse",
    "read",
    "write",
    "delete",
    "cloud_lookup",
    "cloud_download",
    "match",
    "rescan",
}
SCOPES = {"ancestor", "leaf", "unresolved"}

PROCESS_FAILED = "cleanup_process_failed"
# The last line rustcurator writes to stderr when a run fails as a whole.
FATAL_PREFIX = b"rustcurator-fatal "
FATAL_LINE_MAX = 4096
FATAL_REASONS = frozenset(
    "%s_failed" % step
    for step in (
        "log_open",
        "switch_user",
        "list_read",
        "blacklist_read",
        "signature_load",
        "report_create",
        "report_write",
        "progress_create",
        "worker_panic",
        "other",
    )
)
#: attempt metadata key holding what a failed run says about all its files
RUN_FAILURE = "run_failure"
_TOKEN = re.compile(r"[0-9a-f]{32}")
# The cleaner's own files, token hidden: the only paths a run-level reason
# may name, so neither an account path nor a token leaves the server.
_AGENT_FILE = re.compile(
    r"(/tmp|/var/tmp|/usr/tmp|/var/imunify360/tmp|\*)/"
    r"(malware_cleanup_file_list|malware_cleaner_log|malware_cleaner_progress"
    r"|revisium_csvfile|revisium_json_file)_\*\.(txt|json|csv)"
    r"|/var/imunify360(/[A-Za-z0-9_-][A-Za-z0-9._-]{0,63}){1,8}\.db"
)


def parse_reason(data):
    if isinstance(data, str):
        if len(data) > 16384:
            return {}
        try:
            data = json.loads(data)
        except (ValueError, RecursionError):
            return {}
    if not isinstance(data, dict):
        return {}
    version = data.get("reason_version")
    if type(version) is not int or not 1 <= version <= 2**32 - 1:
        return {}
    result = {"reason_version": version}
    for key in ("reason", "cleaner_reason"):
        reason = data.get(key)
        if isinstance(reason, str) and re.fullmatch(r"[ -~]{1,64}", reason):
            result[key] = reason
    path = data.get("reason_path")
    if isinstance(path, str) and len(path) <= 8192:
        result["reason_path"] = path
    value = data.get("reason_errno")
    if isinstance(value, str) and (
        value in ERRNOS or re.fullmatch(r"[1-9][0-9]{0,4}", value)
    ):
        result["reason_errno"] = value
    for key, allowed in (
        ("reason_operation", OPERATIONS),
        ("reason_path_scope", SCOPES),
    ):
        value = data.get(key)
        if isinstance(value, str) and value in allowed:
            result[key] = value
    status = data.get("reason_http_status")
    if type(status) is int and 100 <= status <= 599:
        result["reason_http_status"] = status
    return result


def path_pattern(path):
    """*path* of a cleaner's file with its random token replaced by ``*``."""
    head, _, name = str(path).rpartition("/")
    return "%s/%s" % (head, _TOKEN.sub("*", name))


def run_patterns(paths):
    """The patterns a fatal line may give for the run's own *paths*: the
    directory as it is, or ``*`` for one the cleaner does not name."""
    patterns = set()
    for path in paths:
        pattern = path_pattern(path)
        patterns.add(pattern)
        patterns.add("*/" + pattern.rpartition("/")[2])
    return patterns


def fatal_reason(stderr, patterns):
    """The step and errno the last fatal line of *stderr* names, or {}.

    *patterns* are the only paths the line may name (the files this run was
    given, see run_patterns); any other path is dropped. A line that is too
    long, not one JSON object, or names no known step yields nothing.
    """
    if not isinstance(stderr, (bytes, bytearray)):
        return {}
    start = stderr.rfind(b"\n" + FATAL_PREFIX)
    if start >= 0:
        start += 1
    elif stderr.startswith(FATAL_PREFIX):
        start = 0
    else:
        return {}
    end = stderr.find(b"\n", start)
    line = bytes(stderr[start : end if end >= 0 else len(stderr)])
    line = line.rstrip(b"\r")
    if len(line) > FATAL_LINE_MAX:
        return {}
    try:
        data = json.loads(line[len(FATAL_PREFIX) :].decode("ascii"))
    except (UnicodeDecodeError, ValueError, RecursionError):
        return {}
    if not isinstance(data, dict):
        return {}
    parsed = parse_reason(data)
    if parsed.get("reason") not in FATAL_REASONS:
        return {}
    failure = {"cleaner_reason": parsed["reason"]}
    for key in ("reason_errno", "reason_operation"):
        if key in parsed:
            failure[key] = parsed[key]
    path = data.get("reason_path")
    if isinstance(path, str) and path in patterns:
        failure["reason_path_pattern"] = path
    return failure


def failure_for_server(details):
    """The reason fields sent for a file a failed run left without a result
    row, from its local details; {} when the details name no such run."""
    if (
        not isinstance(details, dict)
        or details.get("kind") != "cleanup"
        or details.get("version") != 1
        or details.get("reason") != PROCESS_FAILED
    ):
        return {}
    entry = parse_reason(
        {
            "reason_version": 1,
            "reason": PROCESS_FAILED,
            "cleaner_reason": details.get("cleaner_reason"),
            "reason_errno": details.get("errno"),
            "reason_operation": details.get("operation"),
        }
    )
    if entry.get("cleaner_reason") not in FATAL_REASONS:
        entry.pop("cleaner_reason", None)
    pattern = details.get("path_pattern")
    if isinstance(pattern, str) and _AGENT_FILE.fullmatch(pattern):
        entry["reason_path_pattern"] = pattern
    for key in ("curator", "curator_version"):
        value = details.get(key)
        if isinstance(value, str) and re.fullmatch(r"[ -~]{1,64}", value):
            entry[key] = value
    return entry


def local_details(result, user, process_error=False):
    reason = result.get("reason")
    if not reason:
        reason = {
            0: "rescan_detected",
            1: "file_too_large",
            2: "read_error",
            3: "write_error",
            4: "legacy_reason_unavailable",
            5: "legacy_reason_unavailable",
            6: "matched_not_cleaned",
            7: "system_owner",
        }.get(result.get("e"), "unsupported_result")
        if "e" not in result:
            reason = (
                "cleanup_process_failed" if process_error else "report_missing"
            )
    details = {"kind": "cleanup", "version": 1, "reason": reason}
    if isinstance(user, str):
        details["cleanup_user"] = user
    for source, target in (
        ("cleaner_reason", "cleaner_reason"),
        ("reason_errno", "errno"),
        ("reason_path", "path"),
        ("reason_path_scope", "path_scope"),
        ("reason_operation", "operation"),
        ("reason_http_status", "http_status"),
        ("curator", "curator"),
        ("curator_version", "curator_version"),
        ("hash_before", "hash_before"),
        ("hash_after", "hash_after"),
    ):
        if result.get(source):
            details[target] = result[source]
    failure = result.get(RUN_FAILURE)
    if reason == PROCESS_FAILED and isinstance(failure, dict):
        # what the cleaner said about the whole run; never a "path", which
        # the UI shows as the file's own
        for source, target in (
            ("cleaner_reason", "cleaner_reason"),
            ("reason_errno", "errno"),
            ("reason_operation", "operation"),
            ("reason_path_pattern", "path_pattern"),
        ):
            if failure.get(source):
                details[target] = failure[source]
    return details


def result_for_server(result, details=None):
    legacy = {
        "d",
        "e",
        "s",
        "r",
        "f",
        "t",
        "mtime_before",
        "mtime_after",
        "hash_before",
        "hash_after",
        "ph",
    }
    projected = {key: value for key, value in result.items() if key in legacy}
    diagnostic = parse_reason(result)
    if isinstance(details, dict) and not diagnostic.get("reason"):
        # Only classified unsuccessful rows have local cleanup details.
        # Successful no-write e=4 rows retain their existing projection.
        diagnostic.update(
            parse_reason(
                {
                    "reason_version": details.get("version"),
                    "reason": details.get("reason"),
                }
            )
        )
    path = diagnostic.pop("reason_path", None)
    if path:
        diagnostic["reason_path_pattern"] = "/{path}"
    projected.update(diagnostic)
    for key in ("curator", "curator_version"):
        value = result.get(key)
        if isinstance(value, str) and re.fullmatch(r"[ -~]{1,64}", value):
            projected[key] = value
    return projected


class DiagnosticLogLimiter:
    """One diagnostic per file and reason per day, with bounded memory."""

    def __init__(self):
        self.entries = OrderedDict()

    def allow(self, path, reason, now):
        while (
            self.entries and next(iter(self.entries.values())) <= now - 86400
        ):
            self.entries.popitem(last=False)
        key = (path, reason)
        if key in self.entries:
            return False
        self.entries[key] = now
        if len(self.entries) > 10000:
            self.entries.popitem(last=False)
        return True


_diagnostic_log_limiter = DiagnosticLogLimiter()


def log_diagnostic(logger, path, details, event):
    if not _diagnostic_log_limiter.allow(
        path, details["reason"], time.monotonic()
    ):
        return
    logger.info(
        "Cleanup %s file=%s reason=%s errno=%s failed_path=%s"
        " user=%s curator=%s",
        event,
        json.dumps(path),
        details["reason"],
        details.get("errno", ""),
        json.dumps(details.get("path", "")),
        json.dumps(details.get("cleanup_user", "")),
        details.get("curator", "unknown"),
    )
