Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit f589a96

Browse files
committed
fix: all the things
1 parent ae316a7 commit f589a96

File tree

4 files changed

+93
-62
lines changed

4 files changed

+93
-62
lines changed

api.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ def __post_request(url, json_data):
99
headers = {'X-API-Key': api_key, 'Content-type': 'application/json'}
1010

1111
req = requests.post(api_url, headers=headers, json=json_data)
12-
rsp = req.json()
1312
req.close()
1413

14+
try:
15+
rsp = req.json()
16+
except:
17+
sys.exit(f"API {url}: not a valid JSON response")
18+
1519
if isinstance(rsp, list):
1620
rsp = rsp[0]
1721

@@ -88,9 +92,13 @@ def check_user(email):
8892
url = f"{api_host}/api/v1/get/mailbox/{email}"
8993
headers = {'X-API-Key': api_key, 'Content-type': 'application/json'}
9094
req = requests.get(url, headers=headers)
91-
rsp = req.json()
9295
req.close()
9396

97+
try:
98+
rsp = req.json()
99+
except:
100+
sys.exit("API get/mailbox: not a valid JSON response")
101+
94102
if not isinstance(rsp, dict):
95103
sys.exit("API get/mailbox: got response of a wrong type")
96104

@@ -101,3 +109,14 @@ def check_user(email):
101109
sys.exit(f"API {url}: {rsp['type']} - {rsp['msg']}")
102110

103111
return (True, bool(rsp['active_int']), rsp['name'])
112+
113+
114+
def check_api():
115+
api_url = f"{api_host}/api/v1/get/status/containers"
116+
headers = {'X-API-Key': api_key, 'Content-type': 'application/json'}
117+
118+
req = requests.get(api_url, headers=headers, verify=False)
119+
req.close()
120+
if req.status_code == 200:
121+
return True
122+
return False

syncer.py

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -47,68 +47,80 @@ def main():
4747

4848

4949
def sync():
50-
ldap_connector = ldap.initialize(f"{config['LDAP_URI']}")
51-
ldap_connector.set_option(ldap.OPT_REFERRALS, 0)
52-
ldap_connector.simple_bind_s(
53-
config['LDAP_BIND_DN'], config['LDAP_BIND_DN_PASSWORD'])
50+
api_status = api.check_api()
51+
52+
if api_status != True:
53+
logging.info(f"mailcow is not fully up, skipping this sync...")
54+
return
55+
56+
try:
57+
ldap_connector = ldap.initialize(f"{config['LDAP_URI']}")
58+
ldap_connector.set_option(ldap.OPT_REFERRALS, 0)
59+
ldap_connector.simple_bind_s(
60+
config['LDAP_BIND_DN'], config['LDAP_BIND_DN_PASSWORD'])
61+
except:
62+
logging.info(
63+
f"Can't connect to LDAP server {config['LDAP_URI']}, skipping this sync...")
64+
return
5465

5566
ldap_results = ldap_connector.search_s(config['LDAP_BASE_DN'], ldap.SCOPE_SUBTREE,
5667
config['LDAP_FILTER'],
57-
['userPrincipalName', 'cn', 'userAccountControl'])
68+
['mail', 'displayName', 'userAccountControl'])
5869

70+
logging.info(ldap_results)
5971
filedb.session_time = datetime.datetime.now()
6072

6173
for x in ldap_results:
6274
try:
63-
logging.info("Working on " + x[1]['sAMAccountName'])
64-
email = x[1]['userPrincipalName'][0].decode()
65-
ldap_name = x[1]['displayName'][0].decode()
66-
ldap_active = False if int(
67-
x[1]['userAccountControl'][0].decode()) & 0b10 else True
68-
69-
(db_user_exists, db_user_active) = filedb.check_user(email)
70-
(api_user_exists, api_user_active, api_name) = api.check_user(email)
71-
72-
unchanged = True
73-
74-
if not db_user_exists:
75-
filedb.add_user(email, ldap_active)
76-
(db_user_exists, db_user_active) = (True, ldap_active)
77-
logging.info(
78-
f"Added filedb user: {email} (Active: {ldap_active})")
79-
unchanged = False
80-
81-
if not api_user_exists:
82-
api.add_user(email, ldap_name, ldap_active, 5120)
83-
(api_user_exists, api_user_active, api_name) = (
84-
True, ldap_active, ldap_name)
85-
logging.info(
86-
f"Added Mailcow user: {email} (Active: {ldap_active})")
87-
unchanged = False
88-
89-
if db_user_active != ldap_active:
90-
filedb.user_set_active_to(email, ldap_active)
91-
logging.info(
92-
f"{'Activated' if ldap_active else 'Deactived'} {email} in filedb")
93-
unchanged = False
94-
95-
if api_user_active != ldap_active:
96-
api.edit_user(email, active=ldap_active)
97-
logging.info(
98-
f"{'Activated' if ldap_active else 'Deactived'} {email} in Mailcow")
99-
unchanged = False
100-
101-
if api_name != ldap_name:
102-
api.edit_user(email, name=ldap_name)
103-
logging.info(
104-
f"Changed name of {email} in Mailcow to {ldap_name}")
105-
unchanged = False
106-
107-
if unchanged:
108-
logging.info(f"Checked user {email}, unchanged")
109-
except Exception:
110-
logging.info(f"Exception during something. See above")
111-
pass
75+
ldap_item = x[1]
76+
logging.info(f"Working on {ldap_item['mail']}")
77+
except:
78+
logging.info(
79+
f"An error occurred while iterating through the LDAP users, skipping this sync...")
80+
return
81+
82+
email = ldap_item['mail'][0].decode()
83+
ldap_name = ldap_item['displayName'][0].decode()
84+
ldap_active = True
85+
86+
(db_user_exists, db_user_active) = filedb.check_user(email)
87+
(api_user_exists, api_user_active, api_name) = api.check_user(email)
88+
89+
unchanged = True
90+
91+
if not db_user_exists:
92+
filedb.add_user(email, ldap_active)
93+
(db_user_exists, db_user_active) = (True, ldap_active)
94+
logging.info(f"Added filedb user: {email} (Active: {ldap_active})")
95+
unchanged = False
96+
97+
if not api_user_exists:
98+
api.add_user(email, ldap_name, ldap_active, 5120)
99+
(api_user_exists, api_user_active, api_name) = (
100+
True, ldap_active, ldap_name)
101+
logging.info(
102+
f"Added Mailcow user: {email} (Active: {ldap_active})")
103+
unchanged = False
104+
105+
if db_user_active != ldap_active:
106+
filedb.user_set_active_to(email, ldap_active)
107+
logging.info(
108+
f"{'Activated' if ldap_active else 'Deactived'} {email} in filedb")
109+
unchanged = False
110+
111+
if api_user_active != ldap_active:
112+
api.edit_user(email, active=ldap_active)
113+
logging.info(
114+
f"{'Activated' if ldap_active else 'Deactived'} {email} in Mailcow")
115+
unchanged = False
116+
117+
if api_name != ldap_name:
118+
api.edit_user(email, name=ldap_name)
119+
logging.info(f"Changed name of {email} in Mailcow to {ldap_name}")
120+
unchanged = False
121+
122+
if unchanged:
123+
logging.info(f"Checked user {email}, unchanged")
112124

113125
for email in filedb.get_unchecked_active_users():
114126
(api_user_exists, api_user_active, _) = api.check_user(email)

templates/dovecot/ldap/passdb.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ uris = $ldap_uri
22
ldap_version = 3
33
base = $ldap_base_dn
44
auth_bind = yes
5-
auth_bind_userdn = %u
5+
auth_bind_userdn = uid=%Ln,$ldap_base_dn

templates/sogo/plist_ldap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<key>CNFieldName</key>
99
<string>cn</string>
1010
<key>IDFieldName</key>
11-
<string>cn</string>
11+
<string>uidNumber</string>
1212
<key>UIDFieldName</key>
13-
<string>userPrincipalName</string>
13+
<string>mail</string>
1414

1515
<key>baseDN</key>
1616
<string>$ldap_base_dn</string>
@@ -21,7 +21,7 @@
2121
<string>$ldap_bind_dn_password</string>
2222
<key>bindFields</key>
2323
<array>
24-
<string>userPrincipalName</string>
24+
<string>mail</string>
2525
</array>
2626

2727
<key>bindAsCurrentUser</key>
@@ -36,9 +36,9 @@
3636
<string>$sogo_ldap_filter</string>
3737

3838
<key>isAddressBook</key>
39-
<string>NO</string>
39+
<string>YES</string>
4040
<key>displayName</key>
41-
<string>Active Directory</string>
41+
<string>Webba adresboek</string>
4242

4343
<key>scope</key>
4444
<string>SUB</string>

0 commit comments

Comments
 (0)