This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
forked from rinkp/custommailcow-ldap
-
Notifications
You must be signed in to change notification settings - Fork 3
/
syncer.py
234 lines (176 loc) · 7.59 KB
/
syncer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import logging
import sys
import os
import string
import time
import datetime
import ldap
import filedb
import api
from string import Template
from pathlib import Path
import urllib3
urllib3.disable_warnings()
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%d.%m.%y %H:%M:%S', level=logging.INFO)
def main():
global config
config = read_config()
passdb_conf = read_dovecot_passdb_conf_template()
plist_ldap = read_sogo_plist_ldap_template()
extra_conf = read_dovecot_extra_conf()
passdb_conf_changed = apply_config(
'conf/dovecot/ldap/passdb.conf', config_data=passdb_conf)
extra_conf_changed = apply_config(
'conf/dovecot/extra.conf', config_data=extra_conf)
plist_ldap_changed = apply_config(
'conf/sogo/plist_ldap', config_data=plist_ldap)
if passdb_conf_changed or extra_conf_changed or plist_ldap_changed:
logging.info(
"One or more config files have been changed, please make sure to restart dovecot-mailcow and sogo-mailcow!")
api.api_host = config['API_HOST']
api.api_key = config['API_KEY']
while (True):
sync()
interval = int(config['SYNC_INTERVAL'])
logging.info(
f"Sync finished, sleeping {interval} seconds before next cycle")
time.sleep(interval)
def sync():
api_status = api.check_api()
if api_status != True:
logging.info(f"mailcow is not fully up, skipping this sync...")
return
try:
ldap_connector = ldap.initialize(f"{config['LDAP_URI']}")
ldap_connector.set_option(ldap.OPT_REFERRALS, 0)
ldap_connector.simple_bind_s(
config['LDAP_BIND_DN'], config['LDAP_BIND_DN_PASSWORD'])
except:
logging.info(
f"Can't connect to LDAP server {config['LDAP_URI']}, skipping this sync...")
return
ldap_results = ldap_connector.search_s(config['LDAP_BASE_DN'], ldap.SCOPE_SUBTREE,
config['LDAP_FILTER'],
['mail', 'displayName', 'userAccountControl'])
logging.info(ldap_results)
filedb.session_time = datetime.datetime.now()
for x in ldap_results:
try:
ldap_item = x[1]
logging.info(f"Working on {ldap_item['mail']}")
except:
logging.info(
f"An error occurred while iterating through the LDAP users, skipping this sync...")
return
email = ldap_item['mail'][0].decode()
ldap_name = ldap_item['displayName'][0].decode()
ldap_active = True
(db_user_exists, db_user_active) = filedb.check_user(email)
(api_user_exists, api_user_active, api_name) = api.check_user(email)
unchanged = True
if not db_user_exists:
filedb.add_user(email, ldap_active)
(db_user_exists, db_user_active) = (True, ldap_active)
logging.info(f"Added filedb user: {email} (Active: {ldap_active})")
unchanged = False
if not api_user_exists:
api.add_user(email, ldap_name, ldap_active, 5120)
(api_user_exists, api_user_active, api_name) = (
True, ldap_active, ldap_name)
logging.info(
f"Added Mailcow user: {email} (Active: {ldap_active})")
unchanged = False
if db_user_active != ldap_active:
filedb.user_set_active_to(email, ldap_active)
logging.info(
f"{'Activated' if ldap_active else 'Deactived'} {email} in filedb")
unchanged = False
if api_user_active != ldap_active:
api.edit_user(email, active=ldap_active)
logging.info(
f"{'Activated' if ldap_active else 'Deactived'} {email} in Mailcow")
unchanged = False
if api_name != ldap_name:
api.edit_user(email, name=ldap_name)
logging.info(f"Changed name of {email} in Mailcow to {ldap_name}")
unchanged = False
if unchanged:
logging.info(f"Checked user {email}, unchanged")
for email in filedb.get_unchecked_active_users():
(api_user_exists, api_user_active, _) = api.check_user(email)
if (api_user_active and api_user_active):
api.edit_user(email, active=False)
logging.info(
f"Deactivated user {email} in Mailcow, not found in LDAP")
filedb.user_set_active_to(email, False)
logging.info(f"Deactivated user {email} in filedb, not found in LDAP")
def apply_config(config_file, config_data):
if os.path.isfile(config_file):
with open(config_file) as f:
old_data = f.read()
if old_data.strip() == config_data.strip():
logging.info(f"Config file {config_file} unchanged")
return False
backup_index = 1
backup_file = f"{config_file}.ldap_mailcow_bak"
while os.path.exists(backup_file):
backup_file = f"{config_file}.ldap_mailcow_bak.{backup_index}"
backup_index += 1
os.rename(config_file, backup_file)
logging.info(f"Backed up {config_file} to {backup_file}")
Path(os.path.dirname(config_file)).mkdir(parents=True, exist_ok=True)
print(config_data, file=open(config_file, 'w'))
logging.info(f"Saved generated config file to {config_file}")
return True
def read_config():
required_config_keys = [
'LDAP-MAILCOW_LDAP_URI',
'LDAP-MAILCOW_LDAP_BASE_DN',
'LDAP-MAILCOW_LDAP_BIND_DN',
'LDAP-MAILCOW_LDAP_BIND_DN_PASSWORD',
'LDAP-MAILCOW_API_HOST',
'LDAP-MAILCOW_API_KEY',
'LDAP-MAILCOW_SYNC_INTERVAL'
]
config = {}
for config_key in required_config_keys:
if config_key not in os.environ:
sys.exit(f"Required environment value {config_key} is not set")
config[config_key.replace('LDAP-MAILCOW_', '')
] = os.environ[config_key]
if 'LDAP-MAILCOW_LDAP_FILTER' in os.environ and 'LDAP-MAILCOW_SOGO_LDAP_FILTER' not in os.environ:
sys.exit(
'LDAP-MAILCOW_SOGO_LDAP_FILTER is required when you specify LDAP-MAILCOW_LDAP_FILTER')
if 'LDAP-MAILCOW_SOGO_LDAP_FILTER' in os.environ and 'LDAP-MAILCOW_LDAP_FILTER' not in os.environ:
sys.exit(
'LDAP-MAILCOW_LDAP_FILTER is required when you specify LDAP-MAILCOW_SOGO_LDAP_FILTER')
config['LDAP_FILTER'] = os.environ[
'LDAP-MAILCOW_LDAP_FILTER'] if 'LDAP-MAILCOW_LDAP_FILTER' in os.environ else '(&(objectClass=user)(objectCategory=person))'
config['SOGO_LDAP_FILTER'] = os.environ['LDAP-MAILCOW_SOGO_LDAP_FILTER'] if 'LDAP-MAILCOW_SOGO_LDAP_FILTER' in os.environ else "objectClass='user' AND objectCategory='person'"
return config
def read_dovecot_passdb_conf_template():
with open('templates/dovecot/ldap/passdb.conf') as f:
data = Template(f.read())
return data.substitute(
ldap_uri=config['LDAP_URI'],
ldap_base_dn=config['LDAP_BASE_DN'],
ldap_bind_dn=config['LDAP_BIND_DN'],
ldap_bind_dn_password=config['LDAP_BIND_DN_PASSWORD']
)
def read_sogo_plist_ldap_template():
with open('templates/sogo/plist_ldap') as f:
data = Template(f.read())
return data.substitute(
ldap_uri=config['LDAP_URI'],
ldap_base_dn=config['LDAP_BASE_DN'],
ldap_bind_dn=config['LDAP_BIND_DN'],
ldap_bind_dn_password=config['LDAP_BIND_DN_PASSWORD'],
sogo_ldap_filter=config['SOGO_LDAP_FILTER']
)
def read_dovecot_extra_conf():
with open('templates/dovecot/extra.conf') as f:
data = f.read()
return data
if __name__ == '__main__':
main()