#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention S4 Connector
#  List all rejected objects
#
# Copyright 2004-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 sys, string, os, time, signal, shutil

import ldap, traceback
import univention
import univention.s4connector
import univention.s4connector.s4

import univention.config_registry

CONFIGBASENAME="connector"

def usage():
    print ""
    print "This is univention-connector-list-rejected"
    print ""
    print "Univention-connector-list-rejected tries to summarize the objects which are rejected due to sync failures"
    print ""
    print "Usage:"
    print "univention-connector-list-rejected [-c configbase]"
    print ""
    print "The default configbase is \"s4connector\"."


if len(sys.argv) > 1:
	if sys.argv[1] in ['-h', '--help', '-?']:
		usage()
		sys.exit(0)
	if sys.argv[1] == '-c':
		if len(sys.argv) != 3:
			print "ERROR: option \"-c\" needs an argument!"
			usage()
			sys.exit(1)
		else:
			CONFIGBASENAME = sys.argv[2]
			print "use configbase %s" % CONFIGBASENAME
	

sys.path=['/etc/univention/%s/s4/'%CONFIGBASENAME]+sys.path
import mapping

def main():
	configRegistry=univention.config_registry.ConfigRegistry()
	configRegistry.load()

	if not configRegistry.has_key('%s/s4/ldap/host' % CONFIGBASENAME):
		print '%s/s4/ldap/host not set' % CONFIGBASENAME
		sys.exit(1)
	if not configRegistry.has_key('%s/s4/ldap/port' % CONFIGBASENAME):
		print '%s/s4/ldap/port not set' % CONFIGBASENAME
		sys.exit(1)
	if not configRegistry.has_key('%s/s4/ldap/base' % CONFIGBASENAME):
		print '%s/s4/ldap/base not set' % CONFIGBASENAME
		sys.exit(1)

	if not configRegistry.has_key('%s/s4/ldap/certificate' % CONFIGBASENAME) and not (configRegistry.has_key('%s/s4/ldap/ssl' % CONFIGBASENAME) and configRegistry['%s/s4/ldap/ssl' % CONFIGBASENAME] == 'no') :
		print '%s/s4/ldap/certificate not set' % CONFIGBASENAME
		sys.exit(1)

	if not configRegistry.has_key('%s/s4/listener/dir' % CONFIGBASENAME):
		print '%s/s4/listener/dir not set' % CONFIGBASENAME
		sys.exit(1)

	if not configRegistry.has_key('%s/s4/retryrejected' % CONFIGBASENAME):
		baseconfig_retry_rejected=10
	else:
		baseconfig_retry_rejected=configRegistry['%s/s4/retryrejected' % CONFIGBASENAME]

	s4_ldap_bindpw=None
	if configRegistry.get('%s/s4/ldap/bindpw' % CONFIGBASENAME):
		s4_ldap_bindpw=open(configRegistry['%s/s4/ldap/bindpw' % CONFIGBASENAME]).read()
		if s4_ldap_bindpw[-1] == '\n':
			s4_ldap_bindpw=s4_ldap_bindpw[0:-1]
	
	poll_sleep=int(configRegistry['%s/s4/poll/sleep' % CONFIGBASENAME])


	#try:
	s4=univention.s4connector.s4.s4( CONFIGBASENAME,
				       mapping.s4_mapping,
				       configRegistry,
				       configRegistry['%s/s4/ldap/host' % CONFIGBASENAME],
				       configRegistry['%s/s4/ldap/port' % CONFIGBASENAME],
				       configRegistry['%s/s4/ldap/base' % CONFIGBASENAME],
				       configRegistry.get('%s/s4/ldap/binddn' % CONFIGBASENAME),
				       s4_ldap_bindpw,
				       configRegistry['%s/s4/ldap/certificate' % CONFIGBASENAME],
				       configRegistry['%s/s4/listener/dir' % CONFIGBASENAME],
				       False)
	#except:
	#	print "Failed to read Connector-Config. This may happen if the connector is running."		
	#	sys.exit(1)

	found_rejected = False
	i=1
	print "\nUCS rejected\n"
	for filename,dn in s4.list_rejected_ucs():
		found_rejected = True
		print "%5d:   UCS DN: %s" % (i, univention.s4connector.s4.encode_attrib(dn))
		s4_dn = univention.s4connector.s4.encode_attrib(s4.get_dn_by_ucs(dn))
		if s4_dn:
			print "          S4 DN: %s" % s4_dn
		else:
			print "          S4 DN: <not found>"
		print "         Filename: %s\n" % filename
		i+=1

	i=1
	print "\nS4 rejected\n"
	for id,dn in s4.list_rejected():
		found_rejected = True
		print "%5d:    S4 DN: %s" % (i,univention.s4connector.s4.encode_attrib(dn))
		ucs_dn = univention.s4connector.s4.encode_attrib(s4.get_dn_by_con(dn))
		if ucs_dn:
			print "         UCS DN: %s" % ucs_dn
		else:
			print "         UCS DN: <not found>"
		i+=1

	if not found_rejected:
		print "\nThere may be no rejected DNs if the connector is in progress, to be\nsure stop the connector before running this script.\n"

	print "\n\tlast synced USN: %s" % s4.get_lastUSN()	

if __name__ == "__main__":
	main()

