#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention Create Keytab Script
#  Tool to create a keytab from a known password
#
# Copyright 2011-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

import heimdal
import subprocess
import binascii
import univention.config_registry
from optparse import OptionParser
import tempfile
import sys
import os

parser = OptionParser()
parser.add_option("-k", "--keytab", dest="keytab",
	help="write keytab to FILE", metavar="FILE")
parser.add_option("-p", "--principal", dest="principal",
	help="create keys for PRINCIPAL", metavar="PRINCIPAL")
parser.add_option("-a", "--alias", dest="service_principal_names",
	help="add alias", action="append")
parser.add_option("-V", "--kvno", dest="kvno",
	help="key version number")
parser.add_option("-w", "--password", dest="password",
	help="password")

(options, args) = parser.parse_args()

if not options.keytab:
	print "keytab argument missing"
	sys.exit(1)
if not options.principal:
	print "principal argument missing"
	sys.exit(1)
if not options.service_principal_names:
	print "alias argument missing"
	sys.exit(1)
if not options.kvno:
	print "kvno argument missing"
	sys.exit(1)
if not options.password:
	print "password argument missing"
	sys.exit(1)

keytab_filename = options.keytab

ucr = univention.config_registry.ConfigRegistry()
ucr.load()

krb5_context = heimdal.context()
permitted_enctypes = krb5_context.get_permitted_enctypes()
permitted_enctypes.reverse()
temp_keytab_filename = tempfile.mktemp()
for krb5_enctype in permitted_enctypes:
	enctype = str(krb5_enctype)
	if enctype in ('des-cbc-md4', 'des3-cbc-md5', 'des3-cbc-sha1'):
		continue
	p1 = subprocess.Popen(['/usr/sbin/ktutil', '-k', temp_keytab_filename, 'add', '-p', options.principal, '-V', str(options.kvno), '-e', enctype, '-w', options.password])
	p1.wait()

keytab = heimdal.keytab(krb5_context, temp_keytab_filename)
for (kvno, enctype, principal, timestamp, keyblock) in keytab.list():
	keyblock_hex = binascii.b2a_hex(keyblock)
	for spn in options.service_principal_names:
		p1 = subprocess.Popen(['/usr/sbin/ktutil', '-k', keytab_filename, 'add', '-p', spn, '-V', str(kvno), '-e', enctype, '-w', keyblock_hex, '--hex'])
		p1.wait()
os.unlink(temp_keytab_filename)

