Files
seafile-sso/seafile-ldap.py

56 lines
2.0 KiB
Python

#!/usr/bin/env python
from ldap3 import Connection, Server, ANONYMOUS, SIMPLE, SYNC, ASYNC
from getpass import getpass
import configparser
import logging
#logger = logging.getLogger(__main__)
#logger.setLevel(logging.INFO)
#logging.basicConfig(level=logging.INFO,format='%(asctime)s - [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
#logLevel = config['DEFAULT']['logLevel']
#logLevel = debug
#logging.basicConfig(level=logging.logLevel,format='%(asctime)s - [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
# import the config file
config = configparser.ConfigParser()
config.read(cwd + '/config.ini')
serverDNS = config['LDAP SERVER']['server']
serverPort = config['LDAP SERVER']['port']
serverSSL = config['LDAP SERVER'].getboolean('ssl')
bindAccount = config['Bind Account']['username']
bindPassword = config['Bind Account']['password']
# setup the server
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)
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
# get ad groups and import them into seafile
# loop through each group and list members
# compare members to users in seafile group
# add users to group if missing and in the seafile group
# remove members in not in group or seafile group
# remove seafile groups if ad group is removed