# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; coding:utf-8 -*-
#
# Copyright 2002 Ben Escoto
# Copyright 2007 Kenneth Loafman
#
# This file is part of duplicity.
#
# Duplicity 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 2 of the License, or (at your
# option) any later version.
#
# Duplicity 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 duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
duplicity's gpg interface, builds upon python-gnupg
"""

import gzip
import locale
import os
import re
import sys
import tempfile
import threading

try:
    import gnupg
except ImportError:
    import sys
    import os

    print(f"DEBUG: sys.path={sys.path}", file=sys.stderr)
    print(f"DEBUG: sys.executable={sys.executable}", file=sys.stderr)
    print(f"DEBUG: os.environ['PYTHONPATH']={os.environ.get('PYTHONPATH')}", file=sys.stderr)
    raise

from duplicity import config
from duplicity import errors
from duplicity import log
from duplicity import tempdir
from duplicity import util

from hashlib import sha1
from hashlib import md5

blocksize = 256 * 1024


class GPGError(Exception):
    """
    Indicate some GPG Error
    """

    pass


class DuplicityGPG(gnupg.GPG):
    def __init__(self, *args, **kwargs):
        # python-gnupg 0.5.x calls --list-config during init, which gpgsm doesn't support.
        try:
            super().__init__(*args, **kwargs)
        except Exception:
            pass

    def make_args(self, args, passphrase):
        full_args = super().make_args(args, passphrase)
        if "_hidden_" in full_args:
            idx = full_args.index("_hidden_")
            if idx > 0 and full_args[idx - 1] == "--recipient":
                full_args.pop(idx)
                full_args.pop(idx - 1)
        if not passphrase:
            for i in range(len(full_args) - 1):
                if full_args[i] == "--pinentry-mode" and full_args[i + 1] == "loopback":
                    full_args.pop(i + 1)
                    full_args.pop(i)
                    break
        if config.use_gpgsm:
            for a in ["--fixed-list-mode", "--batch", "--with-colons"]:
                if a in full_args:
                    full_args.remove(a)
        return full_args

    def encrypt_file(self, fileobj_or_path, recipients, **kwargs):
        # python-gnupg 0.5.6 requires recipients or symmetric=True.
        # If we have hidden recipients in extra_args, we want to allow recipients to be empty.
        if not recipients and not kwargs.get("symmetric"):
            extra_args = kwargs.get("extra_args", [])
            if any("--hidden-recipient" in a for a in extra_args):
                recipients = ["_hidden_"]
        return super().encrypt_file(fileobj_or_path, recipients, **kwargs)


class GPGProfile(object):
    """
    Just hold some GPG settings, avoid passing tons of arguments
    """

    _version_re = re.compile(b"^gpg(sm)?( \\([^\\)]+\\))? (?P<maj>[0-9]+)\\.(?P<min>[0-9]+)\\.(?P<bug>[0-9]+)(-.+)?$")

    def __init__(self, passphrase=None, sign_key=None, recipients=None, hidden_recipients=None):
        """
        Set all data with initializer

        passphrase is the passphrase.  If it is None (not ""), assume
        it hasn't been set.  sign_key can be blank if no signing is
        indicated, and recipients should be a list of keys.  For all
        keys, the format should be an hex key like 'AA0E73D2'.
        """
        assert passphrase is None or isinstance(
            passphrase, str
        ), f"passphrase must be a string or None, got {type(passphrase).__name__}"

        self.passphrase = passphrase
        self.signing_passphrase = passphrase
        self.sign_key = sign_key
        self.encrypt_secring = None
        if recipients is not None:
            assert isinstance(
                recipients, list
            ), f"recipients must be a list (not tuple), got {type(recipients).__name__}"  # must be list, not tuple
            self.recipients = recipients
        else:
            self.recipients = []

        if hidden_recipients is not None:
            assert isinstance(
                hidden_recipients, list
            ), f"hidden_recipients must be a list (not tuple), got {type(hidden_recipients).__name__}"
            self.hidden_recipients = hidden_recipients
        else:
            self.hidden_recipients = []

        self.gpg_version = self.get_gpg_version(config.gpg_binary)

    def get_gpg_version(self, binary):
        binary = binary or "gpg"
        try:
            gpg = DuplicityGPG(gpgbinary=binary)
            if gpg.version:
                if isinstance(gpg.version, tuple):
                    return gpg.version
                return tuple(int(x) for x in re.split(r"[.-]", gpg.version)[:3])
        except Exception:
            pass

        # Fallback to manual check if python-gnupg fails (e.g. gpgsm)
        import subprocess

        try:
            args = [binary, "--version"]
            if config.gpg_options:
                args.extend(config.gpg_options.split())
            res = subprocess.run(args, capture_output=True)
            line = res.stdout.splitlines()[0]
            m = self._version_re.search(line)
            if m:
                return int(m.group("maj")), int(m.group("min")), int(m.group("bug"))
        except Exception:
            pass
        raise GPGError(f"failed to determine gnupg version of {binary}")


class GPGFile(object):
    """
    File-like object that encrypts decrypts another file on the fly
    """

    def __init__(self, encrypt, encrypt_path, profile):
        """
        GPGFile initializer

        If recipients is set, use public key encryption and encrypt to
        the given keys.  Otherwise, use symmetric encryption.

        encrypt_path is the Path of the gpg encrypted file.  Right now
        only symmetric encryption/decryption is supported.

        If passphrase is false, do not set passphrase - GPG program
        should prompt for it.
        """
        self.closed = False
        self.name = encrypt_path
        self.byte_count = 0
        self.signature = None
        self.encrypt = encrypt
        self.result = None
        self.thread_exception = None

        self.gpg = DuplicityGPG(gpgbinary=config.gpg_binary or "gpg")
        self.extra_args = ["--no-secmem-warning"]
        if not config.use_gpgsm:
            self.extra_args.append("--ignore-mdc-error")

        # user supplied options
        if config.gpg_options:
            for opt in config.gpg_options.split():
                self.extra_args.append(opt)

        passphrase = profile.passphrase or None
        if encrypt and profile.sign_key and profile.signing_passphrase:
            passphrase = profile.signing_passphrase

        # Support three versions of gpg present 1.x, 2.0.x, 2.1.x
        if profile.gpg_version[:1] == (1,):
            if config.use_agent:
                # gpg1 agent use is optional
                self.extra_args.append("--use-agent")
        elif profile.gpg_version[:2] >= (2, 1):
            if not config.use_agent and passphrase is not None and not config.use_gpgsm:
                # This forces gpg2 to ignore the agent.
                # Necessary to enforce truly non-interactive operation.
                self.extra_args.append("--pinentry-mode=loopback")

        self.gnupg_kwargs = {
            "passphrase": passphrase,
        }
        if encrypt:
            self.gnupg_kwargs["armor"] = False

        r, w = os.pipe()
        if encrypt:
            if profile.sign_key:
                self.gnupg_kwargs["sign"] = profile.sign_key

            if profile.recipients or profile.hidden_recipients:
                self.gnupg_kwargs["recipients"] = profile.recipients
                for h in profile.hidden_recipients:
                    self.extra_args.extend(["--hidden-recipient", h])
            else:
                self.gnupg_kwargs["symmetric"] = True

            if not config.use_gpgsm:
                # use integrity protection
                self.extra_args.append("--force-mdc")

            # Turn off compression if needed
            if not config.compression:
                self.extra_args.append("--compress-algo=none")

            self.gpg_input = os.fdopen(w, "wb")
            self.pipe_read = os.fdopen(r, "rb")
            self.output_file = encrypt_path.open("wb")

            def on_data(data):
                self.output_file.write(data)
                return False

            self.gpg.on_data = on_data

            def run_gpg():
                try:
                    recipients = self.gnupg_kwargs.pop("recipients", None)
                    self.result = self.gpg.encrypt_file(
                        self.pipe_read, recipients, extra_args=self.extra_args, **self.gnupg_kwargs
                    )
                except Exception as e:
                    self.thread_exception = e
                    log.Debug(f"GPG encryption thread failed: {str(e)}")
                finally:
                    self.pipe_read.close()
                    self.output_file.close()

            self.thread = threading.Thread(target=run_gpg)
            self.thread.start()
        else:
            if (profile.recipients or profile.hidden_recipients) and profile.encrypt_secring:
                self.extra_args.extend(["--secret-keyring", profile.encrypt_secring])

            self.gpg_output = os.fdopen(r, "rb")
            self.pipe_write = os.fdopen(w, "wb")
            self.input_file = encrypt_path.open("rb")

            def on_data(data):
                if data:
                    self.pipe_write.write(data)
                else:
                    try:
                        self.pipe_write.close()
                    except (IOError, ValueError):
                        pass
                return False

            self.gpg.on_data = on_data

            def run_gpg():
                try:
                    self.result = self.gpg.decrypt_file(
                        self.input_file, extra_args=self.extra_args, **self.gnupg_kwargs
                    )
                except Exception as e:
                    self.thread_exception = e
                    log.Debug(f"GPG decryption thread failed: {str(e)}")
                finally:
                    self.pipe_write.close()
                    self.input_file.close()

            self.thread = threading.Thread(target=run_gpg)
            self.thread.start()

    def read(self, length=-1):
        try:
            res = self.gpg_output.read(length)
            if res is not None:
                self.byte_count += len(res)
        except Exception:
            self.gpg_failed()
        return res

    def write(self, buf):
        try:
            res = self.gpg_input.write(buf)
            if res is not None:
                self.byte_count += len(buf)
        except Exception:
            self.gpg_failed()
        return res

    def tell(self):
        return self.byte_count

    def seek(self, offset):
        assert not self.encrypt, "seek() is only supported when decrypting (encrypt=False)"
        assert offset >= self.byte_count, f"{int(offset)} < {int(self.byte_count)}"
        if offset > self.byte_count:
            self.read(offset - self.byte_count)

    def gpg_failed(self):
        if self.thread.is_alive():
            self.thread.join()
        msg = "GPG Failed, see log below:\n"
        msg += "===== Begin GnuPG log =====\n"
        if self.result is not None:
            msg += f"{self.result.stderr}\n"
        if self.thread_exception is not None:
            msg += f"Thread exception: {str(self.thread_exception)}\n"
        msg += "===== End GnuPG log =====\n"
        if not (msg.find("invalid packet (ctb=14)") > -1):
            raise GPGError(msg)
        else:
            return ""

    def close(self):
        if self.encrypt:
            try:
                self.gpg_input.close()
            except Exception:
                self.gpg_failed()
        else:
            try:
                while True:
                    res = self.gpg_output.read(blocksize)
                    if not res:
                        break
                self.gpg_output.close()
            except Exception:
                self.gpg_failed()

        self.thread.join()
        if self.result is None or not self.result:
            if config.use_gpgsm and self.result is not None and self.result.returncode == 0:
                pass
            else:
                self.gpg_failed()

        if not self.encrypt:
            self.set_signature()
        self.closed = True

    def set_signature(self):
        """
        Set self.signature to signature keyID

        This only applies to decrypted files.  If the file was not
        signed, set self.signature to None.
        """
        if self.result and self.result.key_id and (self.result.fingerprint or self.result.valid):
            self.signature = self.result.key_id
        else:
            self.signature = None

    def get_signature(self):
        """
        Return  keyID of signature, or None if none
        """
        assert self.closed, "file not closed yet"
        return self.signature


def GPGWriteFile(block_iter, filename, profile, size=200 * 1024 * 1024, max_footer_size=16 * 1024):
    """
    Write GPG compressed file of given size

    This function writes a gpg compressed file by reading from the
    input iter and writing to filename.  When it has read an amount
    close to the size limit, it "tops off" the incoming data with
    incompressible data, to try to hit the limit exactly.

    block_iter should have methods .next(size), which returns the next
    block of data, which should be at most size bytes long.  Also
    .get_footer() returns a string to write at the end of the input
    file.  The footer should have max length max_footer_size.

    Because gpg uses compression, we don't assume that putting
    bytes_in bytes into gpg will result in bytes_out = bytes_in out.
    However, do assume that bytes_out <= bytes_in approximately.

    Returns true if succeeded in writing until end of block_iter.
    """

    # workaround for circular module imports
    from duplicity import path

    def top_off(bytelen, file):
        """
        Add bytelen of incompressible data to to_gpg_fp

        In this case we take the incompressible data from the
        beginning of filename (it should contain enough because size
        >> largest block size).
        """
        if bytelen <= 0:
            return
        incompressible_fp = open(filename, "rb")
        try:
            while bytelen > 0:
                incompressible_fp.seek(0)
                written = util.copyfileobj(incompressible_fp, file.gpg_input, bytelen)
                if written > 0:
                    bytelen -= written
                else:
                    # Fallback if file is empty
                    import os

                    data = os.urandom(min(bytelen, 64 * 1024))
                    file.gpg_input.write(data)
                    bytelen -= len(data)
        finally:
            incompressible_fp.close()

    def get_current_size():
        return os.stat(filename).st_size

    target_size = size - 300 * 1024  # fudge factor, compensate for gpg buffering
    data_size = target_size - max_footer_size
    file = GPGFile(True, path.Path(filename), profile)
    at_end_of_blockiter = 0
    try:
        while True:
            read_size = block_iter.get_read_size()
            cur_size = get_current_size()
            bytes_to_go = data_size - cur_size
            if bytes_to_go < read_size:
                break
            try:
                data = block_iter.__next__().data
            except StopIteration:
                at_end_of_blockiter = 1
                break
            except Exception as e:
                raise errors.FatalError(f"Read error on {os.fsdecode(filename)}: {str(e)}")
            file.write(data)

        file.write(block_iter.get_footer())
        if not at_end_of_blockiter:
            # don't pad last volume
            cursize = get_current_size()
            if cursize < target_size:
                top_off(target_size - cursize, file)
        file.close()
        return at_end_of_blockiter
    except Exception:
        # ensure that GPG processing terminates
        file.close()
        raise


def GzipWriteFile(block_iter, filename, size=200 * 1024 * 1024, gzipped=True):
    """
    Write gzipped compressed file of given size

    This is like the earlier GPGWriteFile except it writes a gzipped
    file instead of a gpg'd file.  This function is somewhat out of
    place, because it doesn't deal with GPG at all, but it is very
    similar to GPGWriteFile so they might as well be defined together.

    The input requirements on block_iter and the output is the same as
    GPGWriteFile (returns true if wrote until end of block_iter).
    """

    class FileCounted(object):
        """
        Wrapper around file object that counts number of bytes written
        """

        def __init__(self, fileobj):
            self.fileobj = fileobj
            self.byte_count = 0

        def write(self, buf):
            result = self.fileobj.write(buf)
            self.byte_count += len(buf)
            return result

        def flush(self):
            return self.fileobj.flush()

        def close(self):
            return self.fileobj.close()

    target_size = size - 50 * 1024
    file_counted = FileCounted(open(filename, "wb"))

    # if gzipped wrap with GzipFile else plain file out
    if gzipped:
        outfile = gzip.GzipFile(None, "wb", 6, file_counted)
        size -= 32 * 1024
    else:
        outfile = file_counted
    at_end_of_blockiter = 0
    while True:
        bytes_to_go = target_size - file_counted.byte_count
        if bytes_to_go < block_iter.get_read_size():
            break
        try:
            new_block = next(block_iter)
        except StopIteration:
            at_end_of_blockiter = 1
            break
        outfile.write(new_block.data)
        outfile.flush()

    outfile.write(block_iter.get_footer())
    assert not outfile.close(), "outfile failed to close"
    assert not file_counted.close(), "file_counted failed to close"
    return at_end_of_blockiter


def PlainWriteFile(block_iter, filename, size=200 * 1024 * 1024, gzipped=False):
    """
    Write plain uncompressed file of given size

    This is like the earlier GPGWriteFile except it writes a gzipped
    file instead of a gpg'd file.  This function is somewhat out of
    place, because it doesn't deal with GPG at all, but it is very
    similar to GPGWriteFile so they might as well be defined together.

    The input requirements on block_iter and the output is the same as
    GPGWriteFile (returns true if wrote until end of block_iter).
    """
    return GzipWriteFile(block_iter, filename, size, gzipped)


def get_hash(hash, path, hex=1):  # pylint: disable=redefined-builtin
    """
    Return hash of path

    hash should be "MD5" or "SHA1".  The output will be in hexadecimal
    form if hex is true, and in text (base64) otherwise.
    """
    # assert path.isreg()
    fp = path.open("rb")
    if hash == "SHA1":
        hash_obj = sha1(usedforsecurity=False)
    elif hash == "MD5":
        hash_obj = md5(usedforsecurity=False)
    else:
        assert 0, f"Unknown hash {hash}"

    while True:
        buf = fp.read(blocksize)
        if not buf:
            break
        hash_obj.update(buf)
    assert not fp.close(), "fp failed to close"
    if hex:
        return hash_obj.hexdigest()
    else:
        return hash_obj.digest()
