#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention Directory Manager Modules
#  check the samba account flags
#
# 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 ldap, sys

import univention.baseconfig

baseConfig=univention.baseconfig.baseConfig()
baseConfig.load()

baseDN=baseConfig['ldap/base']

lo=ldap.open('localhost', 7389)
bindpw=open('/etc/ldap.secret').read()
if bindpw[-1] == '\n':
	bindpw=bindpw[0:-1]
lo.simple_bind_s("cn=admin,"+baseDN, bindpw)


def print_usage():
	print "\nUse: proof_sambaAccountFlags [query|update] \n"
	print "Without options you will see a list of SambaSamAccounts with possibly wrong Flags.",
	print "The query-option will request each time for update of such elements, the update-option will",
	print "update every found Flag. If you use both options only the first will define",
	print "the behavior !\n"

# update all if command update is given
update_all = 0
query = 0
if len(sys.argv) > 1:
	if sys.argv[1] == "update":
		update_all = 1
	elif sys.argv[1] == "query":
		query = 1
	else:
		print_usage()
		sys.exit()
	
# What we gonna do:
# 1. list all objectClass=univentionWindows
# 2. look if Flag M is set, if yes query

count_flags = 0
count_updated_flags = 0

res_uPR=lo.search_s(baseDN, ldap.SCOPE_SUBTREE, 'objectClass=univentionWindows', ['sambaAcctFlags'])
for i in range(0,len(res_uPR)):
	flags = res_uPR[i][1]["sambaAcctFlags"][0]
	for p in range(1,len(flags)-1):
		if flags[p:p+1] == "M":
			print "found Windows-Account with old (wrong) Flag M (should be W) in %s (%s,%d)" % (res_uPR[i][0],flags,p)
			count_flags = count_flags + 1
			inp = ""
			if not update_all:
				if query:
					print "update Flag (y/N)?"
					inp = sys.stdin.readline()
			else: inp="y"
			if inp[:1] == "y":
				modlist=[(ldap.MOD_REPLACE,"sambaAcctFlags",flags[:p]+"W"+flags[p+1:])]
				lo.modify(res_uPR[i][0],modlist)
				print "updated"
				count_updated_flags = count_updated_flags + 1


print "Found",count_flags,"flags that seemed to be wrong, updated",
print count_updated_flags,"of them."

if not query and not update_all:
	print "\nRun this script with an option to change the listed flags,"
	print "call \"proof_sambaAccountFlags help\" for details"
