127 lines
2.8 KiB
Python
Executable file
127 lines
2.8 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
import argparse
|
|
import getpass
|
|
import logging
|
|
import sys
|
|
|
|
sys.path.insert(0,'/usr/local/src/python-mylib/')
|
|
import LdapServer
|
|
import Pbar
|
|
|
|
default_host = 'ldapi:///'
|
|
default_filter = '(objectClass=posixGroup)'
|
|
default_attr = 'uniqueMember'
|
|
|
|
parser = argparse.ArgumentParser(description="Update memberOf attributes")
|
|
|
|
# options
|
|
parser.add_argument(
|
|
'-d', '--debug',
|
|
action='store_true',
|
|
dest='debug',
|
|
help='Enable debug mode',
|
|
default=False
|
|
)
|
|
parser.add_argument(
|
|
'-H', '--host',
|
|
action="store",
|
|
type=str,
|
|
dest="host",
|
|
help="LDAP server URI (default: %s)" % default_host,
|
|
default=default_host
|
|
)
|
|
parser.add_argument(
|
|
'-D', '--dn',
|
|
action="store",
|
|
type=str,
|
|
dest="dn",
|
|
help="LDAP bind DN",
|
|
default=None
|
|
)
|
|
parser.add_argument(
|
|
'-P', '--password',
|
|
action="store",
|
|
type=str,
|
|
dest="pwd",
|
|
help="LDAP bind password",
|
|
default=None
|
|
)
|
|
parser.add_argument(
|
|
'-f', '--filter',
|
|
action="store",
|
|
type=str,
|
|
dest="filter",
|
|
help="LDAP groups filter (default: %s)" % default_filter,
|
|
default=default_filter
|
|
)
|
|
parser.add_argument(
|
|
'-b', '--base',
|
|
action="store",
|
|
type=str,
|
|
dest="base",
|
|
help="LDAP group base DN",
|
|
default=None
|
|
)
|
|
parser.add_argument(
|
|
'--v2',
|
|
action="store_true",
|
|
dest="ldapv2",
|
|
help="Utiliser le protocole LDAP v2.",
|
|
default=None
|
|
)
|
|
parser.add_argument(
|
|
'-a', '--attr',
|
|
action="store",
|
|
type=str,
|
|
dest="attr",
|
|
help="Group members attribute (default: %s)" % default_attr,
|
|
default=default_attr
|
|
)
|
|
parser.add_argument(
|
|
'-p', '--progress',
|
|
action='store_true',
|
|
dest='progress',
|
|
help='Show progress bar',
|
|
default=False
|
|
)
|
|
|
|
options = parser.parse_args()
|
|
|
|
if options.debug:
|
|
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')
|
|
else:
|
|
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
if options.base is None:
|
|
parser.error('You must specify base DN using --base parameter')
|
|
|
|
if options.dn and not options.pwd:
|
|
options.pwd=getpass.getpass()
|
|
|
|
# Start LDAP connection
|
|
myldap = LdapServer.LdapServer(options.host, options.dn, options.pwd, options.ldapv2)
|
|
myldap.connect()
|
|
|
|
ldap_data=myldap.search(options.base, options.filter, [ options.attr ])
|
|
|
|
logging.info('%s groups found', len(ldap_data))
|
|
|
|
pbar = Pbar.Pbar('Update memberOf', len(ldap_data), enabled=options.progress)
|
|
for dn in ldap_data:
|
|
old = myldap.get_attr(ldap_data[dn], options.attr, all=True)
|
|
if old is None:
|
|
continue
|
|
|
|
logging.debug('Update - remove values of %s', dn)
|
|
if myldap.update_object(
|
|
dn, {options.attr: old}, {options.attr: []}
|
|
):
|
|
logging.debug('Update - restore values of %s', dn)
|
|
myldap.update_object(dn, {options.attr: []}, {options.attr: old})
|
|
|
|
pbar.increment()
|
|
|
|
pbar.finish()
|
|
|