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
+29
View File
@@ -0,0 +1,29 @@
-- If subject contains emoji, varchar doesn't work well.
ALTER TABLE msgs MODIFY COLUMN subject VARBINARY(255) DEFAULT '';
ALTER TABLE msgs MODIFY COLUMN from_addr VARBINARY(255) DEFAULT '';
-- mail address without address extension: user+abc@domain.com -> user@domain.com
ALTER TABLE maddr ADD COLUMN email_raw varbinary(255) NOT NULL DEFAULT '';
-- index
CREATE INDEX maddr_idx_email_raw ON maddr (email_raw);
-- Create trigger to save email address withou address extension
-- user+abc@domain.com -> user@domain.com
DELIMITER //
CREATE TRIGGER `maddr_email_raw`
BEFORE INSERT
ON `maddr`
FOR EACH ROW
BEGIN
IF (NEW.email LIKE '%+%') THEN
SET NEW.email_raw = CONCAT(SUBSTRING_INDEX(NEW.email, '+', 1), '@', SUBSTRING_INDEX(new.email, '@', -1));
ELSE
SET NEW.email_raw = NEW.email;
END IF;
END;
//
DELIMITER ;
-- Update all existing records.
UPDATE maddr SET email_raw=email WHERE email_raw='';
+39
View File
@@ -0,0 +1,39 @@
-- If subject/from_addr contains emoji, varchar doesn't work well.
ALTER TABLE msgs ALTER COLUMN subject DROP DEFAULT;
ALTER TABLE msgs ALTER COLUMN subject TYPE bytea USING subject::bytea;
ALTER TABLE msgs ALTER COLUMN subject SET DEFAULT '';
ALTER TABLE msgs ALTER COLUMN from_addr DROP DEFAULT;
ALTER TABLE msgs ALTER COLUMN from_addr TYPE bytea USING subject::bytea;
ALTER TABLE msgs ALTER COLUMN from_addr SET DEFAULT '';
-- mail address without address extension: user+abc@domain.com -> user@domain.com
ALTER TABLE maddr ADD COLUMN email_raw VARCHAR(255) NOT NULL DEFAULT '';
-- index
CREATE INDEX maddr_idx_email ON maddr (email);
CREATE INDEX maddr_idx_email_raw ON maddr (email_raw);
CREATE INDEX maddr_idx_domain ON maddr (domain);
-- Create trigger to save email address withou address extension
-- user+abc@domain.com -> user@domain.com
-- CREATE OR REPLACE FUNCTION strip_addr_extension(bytea, varchar, integer) RETURNS TRIGGER AS $$
CREATE OR REPLACE FUNCTION strip_addr_extension()
RETURNS TRIGGER AS $$
BEGIN
IF (NEW.email LIKE '%+%') THEN
NEW.email_raw := split_part(convert_from(NEW.email, 'UTF8'), '+', 1) || '@' || split_part(convert_from(NEW.email, 'UTF8'), '@', 2);
ELSE
NEW.email_raw := convert_from(NEW.email, 'UTF8');
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER maddr_email_raw
BEFORE INSERT ON maddr
FOR EACH ROW
EXECUTE PROCEDURE strip_addr_extension();
-- Update all existing records.
UPDATE maddr SET email_raw=email WHERE email_raw='';
+53
View File
@@ -0,0 +1,53 @@
-- USE vmail;
-- DROP column
ALTER TABLE mailbox DROP COLUMN local_part;
-- New SQL column
ALTER TABLE mailbox ADD COLUMN enableimaptls TINYINT(1) NOT NULL DEFAULT 1;
ALTER TABLE mailbox ADD INDEX (enableimaptls);
ALTER TABLE mailbox ADD COLUMN enablepop3tls TINYINT(1) NOT NULL DEFAULT 1;
ALTER TABLE mailbox ADD INDEX (enablepop3tls);
ALTER TABLE mailbox ADD COLUMN enablesievetls TINYINT(1) NOT NULL DEFAULT 1;
ALTER TABLE mailbox ADD INDEX (enablesievetls);
-- Rename table
RENAME TABLE alias_moderators TO moderators;
-- Column used to limit number of mailing lists a domain admin can create
ALTER TABLE domain ADD COLUMN maillists INT(10) NOT NULL DEFAULT 0;
-- Column used to mark sql record is a mailing list
ALTER TABLE forwardings ADD COLUMN `is_maillist` TINYINT(1) NOT NULL DEFAULT 0;
ALTER TABLE forwardings ADD INDEX (`is_maillist`);
-- Table used to store mailing list accounts
CREATE TABLE IF NOT EXISTS maillists (
id BIGINT(20) UNSIGNED AUTO_INCREMENT,
address VARCHAR(255) NOT NULL DEFAULT '',
domain VARCHAR(255) NOT NULL DEFAULT '',
-- Per mailing list transport. for example: 'mlmmj:example.com/listname'.
transport VARCHAR(255) NOT NULL DEFAULT '',
accesspolicy VARCHAR(30) NOT NULL DEFAULT '',
maxmsgsize BIGINT(20) NOT NULL DEFAULT 0,
-- name of the mailing list
name VARCHAR(255) NOT NULL DEFAULT '',
-- short introduction of the mailing list on subscription page
description TEXT,
-- a server-wide unique id (a 36-characters string) for each mailing list
mlid VARCHAR(36) NOT NULL DEFAULT '',
-- control whether newsletter-style subscription from website is enabled
-- 1 -> enabled, 0 -> disabled
is_newsletter TINYINT(1) NOT NULL DEFAULT 0,
settings TEXT,
created DATETIME NOT NULL DEFAULT '1970-01-01 01:01:01',
modified DATETIME NOT NULL DEFAULT '1970-01-01 01:01:01',
expired DATETIME NOT NULL DEFAULT '9999-12-31 00:00:00',
active TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE INDEX (address),
UNIQUE INDEX (mlid),
INDEX (is_newsletter),
INDEX (domain),
INDEX (active)
) ENGINE=InnoDB;
+61
View File
@@ -0,0 +1,61 @@
-- \c vmail;
-- DROP column
ALTER TABLE mailbox DROP COLUMN local_part;
-- New SQL column
ALTER TABLE mailbox ADD COLUMN enableimaptls INT2 NOT NULL DEFAULT 1;
CREATE INDEX idx_mailbox_enableimaptls ON mailbox (enableimaptls);
ALTER TABLE mailbox ADD COLUMN enablepop3tls INT2 NOT NULL DEFAULT 1;
CREATE INDEX idx_mailbox_enablepop3tls ON mailbox (enablepop3tls);
ALTER TABLE mailbox ADD COLUMN enablesievetls INT2 NOT NULL DEFAULT 1;
CREATE INDEX idx_mailbox_enablesievetls ON mailbox (enablesievetls);
-- Rename table
ALTER TABLE alias_moderators RENAME TO moderators;
-- Column used to limit number of mailing lists a domain admin can create
ALTER TABLE domain ADD COLUMN maillists INT8 NOT NULL DEFAULT 0;
-- Column used to mark sql record is a mailing list
ALTER TABLE forwardings ADD COLUMN is_maillist INT2 NOT NULL DEFAULT 0;
CREATE INDEX idx_forwardings_is_maillist ON forwardings (is_maillist);
CREATE TABLE maillists (
id SERIAL PRIMARY KEY,
address VARCHAR(255) NOT NULL DEFAULT '',
domain VARCHAR(255) NOT NULL DEFAULT '',
-- Per mailing list transport. for example: 'mlmmj:example.com/listname'.
transport VARCHAR(255) NOT NULL DEFAULT '',
accesspolicy VARCHAR(30) NOT NULL DEFAULT '',
maxmsgsize INT8 NOT NULL DEFAULT 0,
-- name of the mailing list
name VARCHAR(255) NOT NULL DEFAULT '',
-- short introduction of the mailing list on subscription page
description TEXT,
-- a server-wide unique id (a 36-characters string) for each mailing list
mlid VARCHAR(36) NOT NULL DEFAULT '',
-- control whether newsletter-style subscription from website is enabled
-- 1 -> enabled, 0 -> disabled
is_newsletter INT2 NOT NULL DEFAULT 0,
settings TEXT,
created TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT '1970-01-01 00:00:00',
modified TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT '1970-01-01 00:00:00',
expired TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT '9999-12-31 00:00:00',
active INT2 NOT NULL DEFAULT 1
);
CREATE UNIQUE INDEX idx_maillists_address ON maillists (address);
CREATE INDEX idx_maillists_domain ON maillists (domain);
CREATE UNIQUE INDEX idx_maillists_mlid ON maillists (mlid);
CREATE INDEX idx_maillists_is_newsletter ON maillists (is_newsletter);
CREATE INDEX idx_maillists_active ON maillists (active);
GRANT SELECT ON maillists TO vmail;
GRANT SELECT ON maillists_id_seq TO vmail;
GRANT SELECT ON alias_moderators_id_seq TO vmail;
GRANT SELECT ON forwardings_id_seq TO vmail;
GRANT ALL ON maillists TO vmailadmin;
GRANT ALL ON maillists_id_seq TO vmailadmin;
GRANT ALL ON alias_moderators_id_seq TO vmailadmin;
GRANT ALL ON forwardings_id_seq TO vmailadmin;
+2
View File
@@ -0,0 +1,2 @@
ALTER TABLE mailbox ADD COLUMN mailboxformat VARCHAR(50) NOT NULL DEFAULT 'maildir';
ALTER TABLE mailbox ADD COLUMN mailboxfolder VARCHAR(50) NOT NULL DEFAULT 'Maildir';
+2
View File
@@ -0,0 +1,2 @@
ALTER TABLE mailbox ADD COLUMN mailboxformat VARCHAR(50) NOT NULL DEFAULT 'maildir';
ALTER TABLE mailbox ADD COLUMN mailboxfolder VARCHAR(50) NOT NULL DEFAULT 'Maildir';
+2
View File
@@ -0,0 +1,2 @@
ALTER TABLE mailbox ADD COLUMN `enablequota-status` TINYINT(1) NOT NULL DEFAULT 1;
CREATE INDEX `enablequota-status` ON mailbox (`enablequota-status`);
+2
View File
@@ -0,0 +1,2 @@
ALTER TABLE mailbox ADD COLUMN "enablequota-status" INT2 NOT NULL DEFAULT 1;
CREATE INDEX idx_mailbox_enablequota_status ON mailbox ("enablequota-status");
+16
View File
@@ -0,0 +1,16 @@
-- For MySQL/MariaDB backends, please update database `vmail`.
-- For OpenLDAP backend, please update database `iredadmin`.
DROP TABLE `last_login`;
CREATE TABLE `last_login` (
`username` VARCHAR(255) NOT NULL DEFAULT '',
`domain` VARCHAR(255) NOT NULL DEFAULT '',
`imap` INT(11) DEFAULT NULL,
`pop3` INT(11) DEFAULT NULL,
`lda` INT(11) DEFAULT NULL,
PRIMARY KEY (`username`),
INDEX last_login_domain (`domain`),
INDEX last_login_imap (`imap`),
INDEX last_login_pop3 (`pop3`),
INDEX last_login_lda (`lda`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+1
View File
@@ -0,0 +1 @@
Please follow [iRedMail upgrade tutorials](https://docs.iredmail.org/iredmail.releases.html) to use SQL/Shell/Python scripts in this directory, do not run any of them blindly.
+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."