update arg parse, correctly setup cwd, fixed tab/sapces
This commit is contained in:
@@ -5,26 +5,27 @@ from getpass import getpass
|
|||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Sync LDAP with Seafile')
|
parser = argparse.ArgumentParser(description='Sync LDAP with Seafile')
|
||||||
#group = parser.add_mutually_exclusive_group()
|
#group = parser.add_mutually_exclusive_group()
|
||||||
#group.add_argument('-e', '--encrypt', action='store_true', help='encrypt')
|
#group.add_argument('-e', '--encrypt', action='store_true', help='encrypt')
|
||||||
#group.add_argument('-d', '--decrypt', action='store_true', help='decrypt')
|
#group.add_argument('-d', '--decrypt', action='store_true', help='decrypt')
|
||||||
#parser.add_argument('-t', '--text', type=str, help='text to encrypt/decrypt')
|
#parser.add_argument('-t', '--text', type=str, help='text to encrypt/decrypt')
|
||||||
#group2 = parser.add_mutually_exclusive_group()
|
#group2 = parser.add_mutually_exclusive_group()
|
||||||
#group2.add_argument('-t', '--text', type=str, help='text to encrypt/decrypt')
|
#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')
|
#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')
|
parser.add_argument('-v', '--verbose', action='store_true', help='increase log level to debug')
|
||||||
args = parser.parse_args()
|
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')
|
logFormatter = logging.Formatter('%(asctime)s - [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
else:
|
else:
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
#fileHandler = logging.FileHandler("/tmp/seafile-ldap.log")
|
#fileHandler = logging.FileHandler("/tmp/seafile-ldap.log")
|
||||||
#fileHandler.setFormatter(logFormatter)
|
#fileHandler.setFormatter(logFormatter)
|
||||||
@@ -34,22 +35,26 @@ consoleHandler = logging.StreamHandler()
|
|||||||
consoleHandler.setFormatter(logFormatter)
|
consoleHandler.setFormatter(logFormatter)
|
||||||
logger.addHandler(consoleHandler)
|
logger.addHandler(consoleHandler)
|
||||||
|
|
||||||
|
# get directory of script
|
||||||
|
cwd = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
# import the config file
|
# import the config file
|
||||||
logger.debug("Starting to read the ini config.")
|
logger.debug("Starting to read the ini config.")
|
||||||
|
logger.debug("Using file {}/config.ini.".format(cwd))
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(cwd + '/config.ini')
|
config.read(cwd + '/config.ini')
|
||||||
|
|
||||||
serverDNS = config['LDAP SERVER']['server']
|
serverDNS = config['LDAP SERVER']['server']
|
||||||
serverPort = config['LDAP SERVER']['port']
|
serverPort = config['LDAP SERVER']['port']
|
||||||
serverSSL = config['LDAP SERVER'].getboolean('ssl')
|
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']
|
bindAccount = config['Bind Account']['username']
|
||||||
bindPassword = config['Bind Account']['password']
|
bindPassword = config['Bind Account']['password']
|
||||||
logger.debug("Bind Account: %0, Bind Password: <hidden>".format(bindAccount))
|
logger.debug("Bind Account: {}, Bind Password: <hidden>".format(bindAccount))
|
||||||
logger.debug("Finished reading the ini config.")
|
logger.debug("Finished reading the ini config.")
|
||||||
|
|
||||||
break
|
exit()
|
||||||
|
|
||||||
# setup the server
|
# setup the server
|
||||||
server = Server(serverDNS, port=serverPort, use_ssl=serverSSL)
|
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.
|
#Create a connection object, and bind with the given DN and password.
|
||||||
try:
|
try:
|
||||||
conn = Connection(server, bindAccount, bindPassword, auto_bind=True)
|
conn = Connection(server, bindAccount, bindPassword, auto_bind=True)
|
||||||
print('LDAP Bind Successful.')
|
print('LDAP Bind Successful.')
|
||||||
# Perform a search for a pre-defined criteria.
|
# Perform a search for a pre-defined criteria.
|
||||||
# Mention the search filter / filter type and attributes.
|
# 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))')
|
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.
|
# Print the resulting entries.
|
||||||
for entry in conn.entries:
|
for entry in conn.entries:
|
||||||
print(entry)
|
print(entry)
|
||||||
except core.exceptions.LDAPBindError as e:
|
except core.exceptions.LDAPBindError as e:
|
||||||
# If the LDAP bind failed for reasons such as authentication failure.
|
# If the LDAP bind failed for reasons such as authentication failure.
|
||||||
print('LDAP Bind Failed: ', e)
|
print('LDAP Bind Failed: ', e)
|
||||||
|
|
||||||
|
|
||||||
# sync ad users with seafile, if disabled or deleted ad user, disable in seafile
|
# sync ad users with seafile, if disabled or deleted ad user, disable in seafile
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user