From cdb94e0ff2c7bc03b2f0064b77fedabfa0ae8121 Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Tue, 11 Jun 2019 18:05:29 +0300 Subject: ipaserver.install.installutils: move commonly used utils to ipapython.ipautil When creating ipa-client-samba tool, few common routines from the server installer code became useful for the client code as well. Move them to ipapython.ipautil and update references as well. Fixes: https://pagure.io/freeipa/issue/3999 Signed-off-by: Alexander Bokovoy Reviewed-By: Rob Crittenden Reviewed-By: Christian Heimes --- ipapython/ipautil.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'ipapython') diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py index 106745354..22434cba7 100644 --- a/ipapython/ipautil.py +++ b/ipapython/ipautil.py @@ -28,6 +28,7 @@ import random import math import os import sys +import errno import copy import shutil import socket @@ -54,6 +55,7 @@ except ImportError: netifaces = None from ipapython.dn import DN +from ipaplatform.paths import paths logger = logging.getLogger(__name__) @@ -1571,3 +1573,61 @@ class APIVersion(tuple): @property def minor(self): return self[1] + + +def remove_keytab(keytab_path): + """ + Remove Kerberos keytab and issue a warning if the procedure fails + + :param keytab_path: path to the keytab file + """ + try: + logger.debug("Removing service keytab: %s", keytab_path) + os.remove(keytab_path) + except OSError as e: + if e.errno != errno.ENOENT: + logger.warning("Failed to remove Kerberos keytab '%s': %s", + keytab_path, e) + logger.warning("You may have to remove it manually") + + +def remove_ccache(ccache_path=None, run_as=None): + """ + remove Kerberos credential cache, essentially a wrapper around kdestroy. + + :param ccache_path: path to the ccache file + :param run_as: run kdestroy as this user + """ + logger.debug("Removing service credentials cache") + kdestroy_cmd = [paths.KDESTROY] + if ccache_path is not None: + logger.debug("Ccache path: '%s'", ccache_path) + kdestroy_cmd.extend(['-c', ccache_path]) + + try: + run(kdestroy_cmd, runas=run_as, env={}) + except CalledProcessError as e: + logger.warning( + "Failed to clear Kerberos credentials cache: %s", e) + + +def remove_file(filename): + """Remove a file and log any exceptions raised. + """ + try: + os.unlink(filename) + except Exception as e: + # ignore missing file + if getattr(e, 'errno', None) != errno.ENOENT: + logger.error('Error removing %s: %s', filename, str(e)) + + +def rmtree(path): + """ + Remove a directory structure and log any exceptions raised. + """ + try: + if os.path.exists(path): + shutil.rmtree(path) + except Exception as e: + logger.error('Error removing %s: %s', path, str(e)) -- cgit