From dd29eb1e90fe3b71f5637c0b106accc2f52d2756 Mon Sep 17 00:00:00 2001 From: John Gaunt Date: Tue, 12 Oct 2021 13:21:44 -0400 Subject: [PATCH] update arg parse, correctly setup cwd, fixed tab/sapces --- seafile-ldap.py | 56 ++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/seafile-ldap.py b/seafile-ldap.py index 4b6dc1f..6353f63 100644 --- a/seafile-ldap.py +++ b/seafile-ldap.py @@ -5,26 +5,27 @@ from getpass import getpass import configparser import logging import argparse +import os parser = argparse.ArgumentParser(description='Sync LDAP with Seafile') - #group = parser.add_mutually_exclusive_group() - #group.add_argument('-e', '--encrypt', action='store_true', help='encrypt') - #group.add_argument('-d', '--decrypt', action='store_true', help='decrypt') - #parser.add_argument('-t', '--text', type=str, help='text to encrypt/decrypt') - #group2 = parser.add_mutually_exclusive_group() - #group2.add_argument('-t', '--text', type=str, help='text to encrypt/decrypt') - #group2.add_argument('-f', '--file', type=str, help='file to encrypt/decrypt, will create a .pynacl file when encrypting, and requires .pynacl file to decrypt') - parser.add_argument('-v', '--verbose', type='store_true', help='increase log level to debug') - args = parser.parse_args() - + #group = parser.add_mutually_exclusive_group() + #group.add_argument('-e', '--encrypt', action='store_true', help='encrypt') + #group.add_argument('-d', '--decrypt', action='store_true', help='decrypt') + #parser.add_argument('-t', '--text', type=str, help='text to encrypt/decrypt') + #group2 = parser.add_mutually_exclusive_group() + #group2.add_argument('-t', '--text', type=str, help='text to encrypt/decrypt') + #group2.add_argument('-f', '--file', type=str, help='file to encrypt/decrypt, will create a .pynacl file when encrypting, and requires .pynacl file to decrypt') +parser.add_argument('-v', '--verbose', action='store_true', help='increase log level to debug') +args = parser.parse_args() -logger = logging.getLogger(__main__) + +logger = logging.getLogger(__name__) logFormatter = logging.Formatter('%(asctime)s - [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S') logger.setLevel(logging.INFO) if args.verbose: - logger.setLevel(logging.DEBUG) - else: - logger.setLevel(logging.INFO) + logger.setLevel(logging.DEBUG) +else: + logger.setLevel(logging.INFO) #fileHandler = logging.FileHandler("/tmp/seafile-ldap.log") #fileHandler.setFormatter(logFormatter) @@ -34,22 +35,26 @@ consoleHandler = logging.StreamHandler() consoleHandler.setFormatter(logFormatter) logger.addHandler(consoleHandler) +# get directory of script +cwd = os.path.dirname(os.path.realpath(__file__)) + # import the config file logger.debug("Starting to read the ini config.") +logger.debug("Using file {}/config.ini.".format(cwd)) config = configparser.ConfigParser() config.read(cwd + '/config.ini') serverDNS = config['LDAP SERVER']['server'] serverPort = config['LDAP SERVER']['port'] serverSSL = config['LDAP SERVER'].getboolean('ssl') -logger.debug("Server: %0, Server Port: %1, Using SSL: %3".format(serverDNS, serverPort, serverSSL)) +logger.debug("Server: {}, Server Port: {}, Using SSL: {}".format(serverDNS, serverPort, serverSSL)) bindAccount = config['Bind Account']['username'] bindPassword = config['Bind Account']['password'] -logger.debug("Bind Account: %0, Bind Password: ".format(bindAccount)) +logger.debug("Bind Account: {}, Bind Password: ".format(bindAccount)) logger.debug("Finished reading the ini config.") -break +exit() # setup the server server = Server(serverDNS, port=serverPort, use_ssl=serverSSL) @@ -57,18 +62,17 @@ server = Server(serverDNS, port=serverPort, use_ssl=serverSSL) #Create a connection object, and bind with the given DN and password. try: conn = Connection(server, bindAccount, bindPassword, auto_bind=True) - print('LDAP Bind Successful.') - # Perform a search for a pre-defined criteria. - # Mention the search filter / filter type and attributes. - conn.search('CN=Users,dc=home,dc=johnhgaunt,dc=com', '(&(mail=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(memberOf=CN=Seafile,CN=Users,DC=home,DC=johnhgaunt,DC=com))') - # Print the resulting entries. - for entry in conn.entries: - print(entry) + print('LDAP Bind Successful.') + # Perform a search for a pre-defined criteria. + # Mention the search filter / filter type and attributes. + conn.search('CN=Users,dc=home,dc=johnhgaunt,dc=com', '(&(mail=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(memberOf=CN=Seafile,CN=Users,DC=home,DC=johnhgaunt,DC=com))') + # Print the resulting entries. + for entry in conn.entries: + print(entry) except core.exceptions.LDAPBindError as e: # If the LDAP bind failed for reasons such as authentication failure. print('LDAP Bind Failed: ', e) - - + # sync ad users with seafile, if disabled or deleted ad user, disable in seafile