This commit is contained in:
temasky
2020-07-31 12:45:56 +00:00
commit 5d419635ed
306 changed files with 26949 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
# Update LDAP data
[TOC]
## Summary
If you're restoring from an old iRedMail release, you need to add missing LDAP
attribute/values, which are introduced in new iRedMail releases, by running
Python scripts below: <https://github.com/iredmail/iredmail/tree/master/update/>.
For example:
* If you're restoring iRedMail from `0.9.1` to `0.9.5`, you must run all update
scripts for iRedMail-0.9.1 and newer releases. In this case, only file
`updateLDAPValues_094_to_095.py` listed in above link is required.
* If you're restoring iRedMail from `0.8.6` to `0.9.5`, you need 3 files:
* `updateLDAPValues_086_to_087.py`
* `updateLDAPValues_087_to_090.py`
* `updateLDAPValues_094_to_095.py`
## How to use those upgrade scripts
Please open the file you need to run, for example, `updateLDAPValues_094_to_095.py`,
find parameters like below:
```
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
```
Please update them with the correct LDAP prefix (`dc=xx,dc=xx`) and bind
password, then run it with `python` command:
```
python updateLDAPValues_094_to_095.py
```
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add missing attribute/value pairs required by Dovecot-2.3.
# Date: Apr 12, 2018.
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'password'
# Initialize LDAP connection.
print "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print "* Get mail accounts ..."
allUsers = conn.search_s(basedn,
ldap.SCOPE_SUBTREE,
"(&(objectClass=mailUser)(|(enabledService=imapsecured)(enabledService=pop3secured)(enabledService=smtpsecured)))",
['mail', 'enabledService'])
total = len(allUsers)
print "* Updating %d user(s)." % (total)
# Counter.
count = 1
for (dn, entry) in allUsers:
mail = entry['mail'][0]
if 'enabledService' not in entry:
continue
enabledService = entry['enabledService']
_update = False
if 'imaptls' not in enabledService:
enabledService += ['imaptls']
_update = True
if 'pop3tls' not in enabledService:
enabledService += ['pop3tls']
_update = True
if 'smtptls' not in enabledService:
enabledService += ['smtptls']
_update = True
if _update:
print "* (%d of %d) Updating user: %s" % (count, total, mail)
mod_attr = [(ldap.MOD_REPLACE, 'enabledService', enabledService)]
try:
conn.modify_s(dn, mod_attr)
except Exception, e:
print "Error while updating user:", mail, e
else:
print "* [SKIP] No update required for user:", mail
count += 1
# Unbind connection.
print "* Unbind LDAP server."
conn.unbind()
print "* Update completed."
@@ -0,0 +1,62 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <michaelbibby (at) gmail.com>
# Purpose: Add three new services name in 'enabledService' attribute.
# Required in iRedMail-0.5.1.
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=iredmail,dc=org'
bind_dn = 'cn=Manager,dc=example,dc=com'
#bind_dn = 'cn=Manager,dc=iredmail,dc=org'
bind_pw = 'passwd'
# Initialize LDAP connection.
conn = ldap.initialize(uri=uri, trace_level=0,)
# Bind.
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['dn', 'mail', 'enabledService'],
)
# Debug.
#print >> sys.stderr, allUsers
# Values which need to be added.
services = ['pop3secured', 'imapsecured', 'managesievesecured',]
# Counter.
count = 1
for user in allUsers:
dn = user[0]
mail = user[1]['mail'][0]
enabledService = user[1]['enabledService']
# Get missing values.
values = [ s for s in services if s not in enabledService ]
# Update it if there are something missed..
if len(values) != 0:
print >> sys.stderr, """Updating user (%d): %s""" % (count, mail)
mod_attrs = [ (ldap.MOD_ADD, 'enabledService', values) ]
conn.modify_s(dn, mod_attrs)
count += 1
# Unbind connection.
conn.unbind()
print >> sys.stderr, 'Updated.'
@@ -0,0 +1,74 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb@iredmail.org>
# Purpose: Add three new services name in 'enabledService' attribute.
# Required in iRedMail-0.6.0.
# Date: 2010-05-31
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=iredmail,dc=org'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['dn', 'mail', 'enabledService', 'objectClass',],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['sieve', 'sievesecured', 'internal',]
# Counter.
count = 1
for user in allUsers:
dn = user[0]
mail = user[1]['mail'][0]
enabledService = user[1]['enabledService']
objectClasses = user[1]['objectClass']
# Get missing values.
values = [s for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Add missing values of 'objectClass'.
if 'amavisAccount' not in objectClasses:
mod_attrs += [(ldap.MOD_ADD, 'objectClass', 'amavisAccount')]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* Updating user (%d/%d): %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* Updating user (%d/%d): %s. [SKIP. No update required.]" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb@iredmail.org>
# Purpose: Add three service name in 'enabledService' attribute.
# Required in iRedMail-0.7.0.
# Date: 2011-01-28
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=iredmail,dc=org'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['dn', 'mail', 'enabledService', 'objectClass',],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['smtpsecured']
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if entry.has_key('enabledService'):
enabledService = entry['enabledService']
else:
continue
objectClasses = entry['objectClass']
# Get missing values.
values = [str(s).lower() for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Add missing values of 'objectClass'.
if 'amavisAccount' not in objectClasses:
mod_attrs += [(ldap.MOD_ADD, 'objectClass', 'amavisAccount')]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* Updating user (%d/%d): %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* Updating user (%d/%d): %s. [SKIP. No update required.]" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,73 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb@iredmail.org>
# Purpose: Add enabledService=lda for all mail users.
# Required in iRedMail-0.7.3.
# Date: 2011-07-12
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=iredmail,dc=org'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['dn', 'mail', 'enabledService', 'objectClass',],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['lda']
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if entry.has_key('enabledService'):
enabledService = entry['enabledService']
else:
continue
objectClasses = entry['objectClass']
# Get missing values.
values = [str(s).lower() for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* (%d of %d) [SKIP] No update required: %s" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=doveadm for all mail users.
# Required by command doveadm which provided by Dovecot-2.0.
# Date: 2012-04-19
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['doveadm']
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if not 'enabledService' in entry:
continue
enabledService = entry['enabledService']
# Get missing values.
values = [str(s).lower() for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* (%d of %d) [SKIP] No update required: %s" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=lib-storage for all mail users.
# Required by IMAP folder sharing in Dovecot-2.0.
# Date: 2012-05-18
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['lib-storage']
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if not 'enabledService' in entry:
continue
enabledService = entry['enabledService']
# Get missing values.
values = [str(s).lower() for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* (%d of %d) [SKIP] No update required: %s" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,68 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Use shadowAddress instead of memberOfGroup for alias domains
# in objects objectClass=mailExternaluser.
# Date: 2013-03-25
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all accounts with objectClass=mailExternalUser..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailExternalUser)",
['memberOfGroup'],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
# Get all values in attribute memberOfGroup, they're value of shadowAddress
# we will set later.
shadow_addresses = entry['memberOfGroup']
if len(shadow_addresses) > 1:
# Get memberOfGroup in dn
value_of_rdn = dn.split(',', 1)[0].split('=')[-1]
# Use only value of rdn.
mod_attrs = [(ldap.MOD_REPLACE, 'memberOfGroup', [value_of_rdn])]
# Add shadowAddress
shadow_addresses.remove(value_of_rdn)
mod_attrs += [(ldap.MOD_REPLACE, 'shadowAddress', shadow_addresses)]
# Update.
print >> sys.stderr, "* (%d of %d) Updating object: %s" % (count, total, dn)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* (%d of %d) [SKIP] No update required: %s" % (count, total, dn)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=lmtp for all mail users.
# Required by LMTP service used in Dovecot-2.x.
# Date: 2014-02-20
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'])
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['lmtp']
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if not 'enabledService' in entry:
continue
enabledService = entry['enabledService']
# Get missing values.
values = [str(s).lower() for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* (%d of %d) [SKIP] No update required: %s" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=indexer-worker for existing mail users.
# Required by Dovecot.
# Date: 2014-06-07
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'])
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['indexer-worker']
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if not 'enabledService' in entry:
continue
enabledService = entry['enabledService']
# Get missing values.
values = [str(s).lower() for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* (%d of %d) [SKIP] No update required: %s" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=sogo for existing mail users. Used in SOGo to
# restrict who can access SOGo services (webmail, calendar, contacts).
# Date: Mar 23, 2016.
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'])
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Values of 'enabledService' which need to be added.
services = ['sogo']
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
if not 'enabledService' in entry:
continue
enabledService = entry['enabledService']
# Get missing values.
values = [str(s).lower() for s in services if s not in enabledService]
mod_attrs = []
# Add missing values of 'enabledService'.
if len(values) > 0:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', values)]
# Update.
if len(mod_attrs) > 0:
print >> sys.stderr, "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print >> sys.stderr, "* (%d of %d) [SKIP] No update required: %s" % (count, total, mail)
count += 1
# Unbind connection.
print >> sys.stderr, "* Unbind LDAP server."
conn.unbind()
print >> sys.stderr, "* Update completed."
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add `domainStatus=disabled` to all mail users/aliases/lists if
# domain is disabled.
# Date: Oct 21, 2016.
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'password'
# Initialize LDAP connection.
print "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
print "* Authenticate as dn: %s" % bind_dn
conn.bind_s(bind_dn, bind_pw)
# Get all disabled domains.
print "* Get all disabled mail domains ..."
qf = "(&(objectClass=mailDomain)(accountStatus=disabled))"
qr_domains = conn.search_s(basedn,
ldap.SCOPE_ONELEVEL,
qf,
['dn', 'domainName'])
if not qr_domains:
print "* No disabled domain. Exit."
sys.exit()
print "* Found %d disabled domains." % len(qr_domains)
qf = "(&"
qf += "(!(domainStatus=disabled))"
qf += "(|(objectClass=mailUser)(objectClass=mailAlias)(objectClass=mailList)(objectClass=mailExternalUser))"
qf += ")"
mod_attr = [(ldap.MOD_ADD, 'domainStatus', ['disabled'])]
for (dn, _ldif) in qr_domains:
domain = _ldif['domainName'][0]
# Get all mail accounts which don't have required mod_attr.
qr_accounts = conn.search_s(dn,
ldap.SCOPE_SUBTREE,
qf,
['objectClass', 'mail', 'domainStatus'])
total = len(qr_accounts)
if total:
print "* Updating %d accounts(s) under domain %s." % (total, domain)
else:
print "* No update required for domain %s." % domain
# Counter.
count = 1
for (_dn, _ldif2) in qr_accounts:
if 'mail' not in _ldif2:
continue
mail = _ldif2['mail'][0]
try:
print "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(_dn, mod_attr)
except Exception, e:
print "* (%d of %d) <<< ERROR >>> %s: %s" % (count, total, mail, repr(e))
count += 1
conn.unbind()
print "* Update completed."
@@ -0,0 +1,62 @@
#!/usr/bin/env python2
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=quota-status for existing mail users.
# Used by Dovecot quota-status service.
# Date: Jul 17, 2019.
import ldap
# Note: bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'passwd'
# Initialize LDAP connection.
print "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print "* Get all mail accounts..."
all_users = conn.search_s(basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'])
total = len(all_users)
print "* Total %d user(s)." % (total)
# New values of the 'enabledService' attribute.
new_services = ['quota-status']
# Counter.
count = 1
for user in all_users:
(dn, ldif) = user
mail = ldif['mail'][0]
if 'enabledService' not in ldif:
continue
existing_services = ldif['enabledService']
missed_services = [str(s).lower() for s in new_services if s not in existing_services]
mod_attrs = []
if missed_services:
mod_attrs += [(ldap.MOD_ADD, 'enabledService', missed_services)]
# Update.
print "* (%d of %d) Updating user: %s" % (count, total, mail)
conn.modify_s(dn, mod_attrs)
else:
print "* (%d of %d) [SKIP] No update required: %s" % (count, total, mail)
count += 1
# Unbind connection.
print "* Unbind LDAP server."
conn.unbind()
print "* Update completed."