From 97e9be80cf37c69a592f4987e5d96eea2d381cf4 Mon Sep 17 00:00:00 2001 From: Kishi85 Date: Fri, 25 Oct 2019 18:03:50 +0200 Subject: [PATCH] acertmgr: Fix module/function issues on windows --- acertmgr/__init__.py | 37 +++++++++++++++++++++++++------------ acertmgr/tools.py | 22 ++++++++++++++-------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/acertmgr/__init__.py b/acertmgr/__init__.py index b690138..88c201a 100755 --- a/acertmgr/__init__.py +++ b/acertmgr/__init__.py @@ -6,18 +6,24 @@ # Copyright (c) Rudolf Mayerhofer, 2019. # available under the ISC license, see LICENSE -import grp import io import os -import pwd import stat import subprocess +import sys from acertmgr import configuration, tools from acertmgr.authority import authority from acertmgr.modes import challenge_handler from acertmgr.tools import log +try: + import pwd + import grp +except ImportError: + # Warnings will be reported upon usage below + pass + # @brief fetch new certificate from letsencrypt # @param settings the domain's configuration options @@ -90,18 +96,25 @@ def cert_put(settings): # set owner and group if 'user' in settings or 'group' in settings: - try: - uid = pwd.getpwnam(settings['user']).pw_uid if 'user' in settings else os.geteuid() - gid = grp.getgrnam(settings['group']).gr_gid if 'group' in settings else os.getegid() - os.chown(settings['path'], uid, gid) - except OSError as e: - log('Could not set certificate file ownership', e, warning=True) + if 'pwd' in sys.modules and 'grp' in sys.modules and hasattr(os, 'chown') and hasattr(os, 'geteuid') and \ + hasattr(os, 'getegid'): + try: + uid = pwd.getpwnam(settings['user']).pw_uid if 'user' in settings else os.geteuid() + gid = grp.getgrnam(settings['group']).gr_gid if 'group' in settings else os.getegid() + os.chown(settings['path'], uid, gid) + except OSError as e: + log('Could not set certificate file ownership', e, warning=True) + else: + log('File user and group handling unavailable on this platform', warning=True) # set permissions if 'perm' in settings: - try: - os.chmod(settings['path'], int(settings['perm'], 8)) - except OSError as e: - log('Could not set certificate file permissions', e, warning=True) + if hasattr(os, 'chmod'): + try: + os.chmod(settings['path'], int(settings['perm'], 8)) + except OSError as e: + log('Could not set certificate file permissions', e, warning=True) + else: + log('File permission handling unavailable on this platform', warning=True) return settings['action'] diff --git a/acertmgr/tools.py b/acertmgr/tools.py index efe0360..1e66788 100644 --- a/acertmgr/tools.py +++ b/acertmgr/tools.py @@ -159,10 +159,13 @@ def new_ssl_key(path=None, key_algo=None, key_size=None): ) with io.open(path, 'wb') as pem_out: pem_out.write(pem) - try: - os.chmod(path, int("0400", 8)) - except OSError: - log('Could not set file permissions on {0}!'.format(path), warning=True) + if hasattr(os, 'chmod'): + try: + os.chmod(path, int("0400", 8)) + except OSError: + log('Could not set file permissions on {0}!'.format(path), warning=True) + else: + log('Keyfile permission handling unavailable on this platform', warning=True) return private_key @@ -186,10 +189,13 @@ def write_pem_file(crt, path, perms=None): with io.open(path, "w") as f: f.write(convert_cert_to_pem_str(crt)) if perms: - try: - os.chmod(path, perms) - except OSError: - log('Could not set file permissions ({0}) on {1}!'.format(perms, path), warning=True) + if hasattr(os, 'chmod'): + try: + os.chmod(path, perms) + except OSError: + log('Could not set file permissions ({0}) on {1}!'.format(perms, path), warning=True) + else: + log('PEM-File permission handling unavailable on this platform', warning=True) # @brief download the issuer ca for a given certificate