#!/usr/bin/env python3
## Call with "--help" for documentation.

import argparse, configparser, itertools, stat, os, os.path, sys, subprocess, datetime

## Helper functions
def readConfig(fname, defSection = 'DEFAULT'):
    config = configparser.ConfigParser()
    with open(fname) as file:
        stream = itertools.chain(("["+defSection+"]\n",), file)
        config.read_file(stream)
    return config

def certfile(name):
    global config
    return os.path.join(config['dirs']['certs'], name + ".crt" )

def keyfile(name):
    global config
    return os.path.join(config['dirs']['keys'], name + ".key")

def make_backup(fname):
    if os.path.exists(fname):
        backupname = os.path.basename(fname) + "." + str(datetime.date.today())
        i = 0
        while True:
            backupfile = os.path.join(config['dirs']['backups'], backupname + "." + str(i))
            if not os.path.exists(backupfile):
                os.rename(fname, backupfile)
                break
            elif i >= 100:
                print("Somehow it's really hard to find a name for the backup file...")
            i += 1
    assert not os.path.exists(fname)

def trigger_hook(hook):
    global config
    exe = config['hooks'].get(hook)
    if exe is not None:
        subprocess.check_call([exe])

def key_mtime(name):
    return datetime.datetime.fromtimestamp(os.stat(keyfile(name)).st_mtime)

def cert_expiry(name):
    from certcheck import cert_expiry_date
    return cert_expiry_date(certfile(name))

## Work functions, operating on file names

## The interesting work
def acme(keyfilename, certfilename, domains):
    global config
    accountkey = config['acme']['account-key']
    csrfilename = certfilename + '.csr.tmp'
    assert os.path.exists(keyfilename)
    assert accountkey
    # Generating the CSR is done by a shell script
    exe = os.path.join(os.path.dirname(__file__), 'gencsr')
    csr = subprocess.check_output([exe, keyfilename] + domains)
    assert not os.path.exists(csrfilename), "The temporary CSR file {} still exists. It seems something went wrong on a previous request. You may want to remove the file manually.".format(csrfilename)
    with open(csrfilename, 'wb') as file:
        file.write(csr)
    # call acme-tiny as a script
    acme_tiny = os.path.join(config['acme']['acme-tiny'], 'acme_tiny.py')
    signed_crt = subprocess.check_output(["python", acme_tiny, "--quiet", "--account-key", accountkey, "--csr", csrfilename, "--acme-dir", config['acme']['challenge-dir']])
    # save new certificate
    make_backup(certfilename)
    with open(certfilename, 'wb') as f:
        f.write(signed_crt)
    # clean up
    os.remove(csrfilename)

def openssl_genrsa(keyfilename):
    with subprocess.Popen(["openssl", "genrsa", str(int(config['DEFAULT']['key-length']))], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as f:
        (stdout, stderr) = f.communicate()
        if f.returncode:
            sys.stderr.write(stderr)
            raise Exception("Error while generating private key")
    # Now we have a key, save it. This should never overwrite anything.
    assert not os.path.exists(keyfilename)
    with open(keyfilename, 'wb') as f:
        f.write(stdout)

## High-level functions, operating on nice key names
def request_cert(name):
    global config
    if not os.path.exists(keyfile(name)):
        raise Exception("No such key: {}".format(name))
    domains = config['DEFAULT']['domains'].split()
    print("Obtaining certificate '{}' for domains: {}".format(name, ' '.join(domains)))
    acme(keyfile(name), certfile(name), domains)

def generate_key(name):
    print("Generating new private key '{}'".format(name))
    openssl_genrsa(keyfile(name))

def check_staging(live, staging):
    '''Returns 0 if nothing was done, 1 if a stage key is present but has to be kept, 2 is a stage key was unstaged.'''
    if not os.path.exists(keyfile(staging)):
        return 0
    
    staging_time = datetime.timedelta(hours = int(config['timing'].get('staging-hours', 0)))
    key_age = datetime.datetime.now() - key_mtime(staging)
    if key_age < staging_time:
        return 1
    print("Unstaging '{}' to '{}'".format(staging, live))
    # unstage the key!
    make_backup(keyfile(live))
    os.rename(keyfile(staging), keyfile(live))
    make_backup(certfile(live))
    os.rename(certfile(staging), certfile(live))
    return 2

def auto_renewal(live, staging):
    '''Returns 0 if nothing was done, 1 if only certs were changed, 2 if certs and keys were changed.'''
    max_key_age = datetime.timedelta(days = int(config['timing']['max-key-age-days']))
    renew_cert_time = datetime.timedelta(days = int(config['timing']['renew-cert-before-expiry-days']))
    
    # determine what to do
    now = datetime.datetime.now()
    key_age = now - key_mtime(live)
    cert_validity = cert_expiry(live) - now
    need_new_key = key_age >= max_key_age
    need_new_cert = cert_validity <= renew_cert_time
    if need_new_cert and key_age + renew_cert_time >= max_key_age:
        # We are about to request a new certificate, and within <renew_cert_time>, we need a new key: Get the new key now
        need_new_key = True
    
    # Do it
    if need_new_key:
        generate_key(staging)
        request_cert(staging)
        check_staging(live, staging) # we may want to immediately enable the new key & cert
        return 2
    elif need_new_cert:
        request_cert(live)
        return 1
    else:
        return 0

## Main
if __name__ == "__main__":
    # allow overwriting some values on the command-line
    parser = argparse.ArgumentParser(description='Generate and (automatically) renew certificates, optionally providing staging for new keys')
    parser.add_argument("-c", "--config",
                        dest="config",
                        help="The configuration file")
    parser.add_argument("-k", "--hooks",
                        dest="hooks", action="store_true",
                        help="Trigger hooks.")
    parser.add_argument("action", metavar='ACTION', nargs=1,
                        help="The action to perform. Possible values: init, renew, cron")
    args = parser.parse_args()
    
    # read config, sanity check
    if not os.path.isfile(args.config):
        raise Exception("The config file does not exist: "+args.config)
    global config
    config = readConfig(args.config)
    
    live = config['files']['live']
    staging = config['files']['staging']
    if args.action[0] == 'renew':
        request_cert(live)
        if os.path.exists(keyfile(staging)) and os.path.exists(certfile(staging)):
            request_cert(staging)
        # trigger the "new cert" hook
        if args.hooks:
            trigger_hook('post-certchange')
    elif args.action[0] == 'cron':
        # First, check if we need to unstage a staging key
        unstaged = check_staging(live, staging)
        if unstaged >= 1:
            # A staging key is present, do *not* check for renewal
            if unstaged >= 2 and args.hooks:
                # trigger all the hooks
                trigger_hook('post-certchange')
                trigger_hook('post-keychange')
        else:
            # Check if we need to renew anything
            renewed = auto_renewal(live, staging)
            if args.hooks:
                if renewed >= 1:
                    trigger_hook('post-certchange')
                if renewed >= 2:
                    trigger_hook('post-keychange')
    elif args.action[0] == 'init':
        # create directories with appropriate permissions
        try:
            os.makedirs(config['dirs']['certs'], mode = 0o755, exist_ok = True)
            os.makedirs(config['dirs']['keys'], mode = 0o710, exist_ok = True)
            os.makedirs(config['dirs']['backups'], mode = 0o700, exist_ok = True)
        except OSError:
            pass
        # if necessary, generate ACME account key
        accountkey = config['acme']['account-key']
        if not os.path.exists(accountkey):
            print("Generating new ACME key")
            openssl_genrsa(accountkey)
        # if necessary, generate key + certificate
        live = config['files']['live']
        if not os.path.exists(keyfile(live)):
            generate_key(live)
            request_cert(live)
            if args.hooks:
                trigger_hook('post-certchange')
                trigger_hook('post-keychange')
    else:
        raise Exception("Unknown action {}".format(args.action))