0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# MIT Kerberos server class
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# Copyright (c) 2016 Red Hat, Inc.
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# This is free software; you can redistribute it and/or modify it
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# under the terms of the GNU General Public License as published by
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# the Free Software Foundation; version 2 only
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# This program is distributed in the hope that it will be useful, but
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# WITHOUT ANY WARRANTY; without even the implied warranty of
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# General Public License for more details.
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# You should have received a copy of the GNU General Public License
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek# along with this program. If not, see <http://www.gnu.org/licenses/>.
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek Exception.__init__(self, 'No principals in the collection')
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek Exception.__init__(self, 'Principal %s not found' % principal)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek Helper class to test Kerberos command line utilities
274489b092bba5fc81cb0f803843d56b267c5aafJakub Hrozek def spawn_in_env(self, args, stdin=None, extra_env=None):
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek if extra_env is not None:
274489b092bba5fc81cb0f803843d56b267c5aafJakub Hrozek def _run_in_env(self, args, stdin=None, extra_env=None):
274489b092bba5fc81cb0f803843d56b267c5aafJakub Hrozek cmd = self.spawn_in_env(args, stdin, extra_env)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek return cmd.returncode, out.decode('utf-8'), err.decode('utf-8')
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek def kinit(self, principal, password, env=None):
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek return self._run_in_env(args, password.encode('utf-8'), env)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek def kdestroy(self, all_ccaches=False, env=None):
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek def _check_klist_l(self, line, exp_principal, exp_cache):
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek if exp_cache is not None and cache != exp_cache:
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek retval, out, err = self._run_in_env(args, extra_env=env)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek outlines = [l for l in out.split('\n') if len(l) > 1]
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek retval, out, err = self._run_in_env(args, extra_env=env)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek raise Exception("klist failed: %d: %s\n", retval, err)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek raise Exception("Not enough output from klist -l")
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek def has_principal(self, exp_principal, exp_cache=None, env=None):
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek matches = self._check_klist_l(line, exp_principal, exp_cache)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek for line in [l for l in out.split('\n') if len(l) > 0]:
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek elif thisrealm is not None and line.endswith(thisrealm):
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek retval, out, err = self._run_in_env(args, extra_env=env)
0700118d8388c38b8cb28279510b206b76a3a411Jakub Hrozek raise Exception("klist -A failed: %d: %s\n", retval, err)