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
+227
View File
@@ -0,0 +1,227 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb@iredmail.org)
# Date: 16/09/2007
# Purpose: Backup specified mysql databases with command 'mysqldump'.
# License: This shell script is part of iRedMail project, released under
# GPL v2.
###########################
# REQUIREMENTS
###########################
#
# * Required commands:
# + mysqldump
# + du
# + bzip2 # If bzip2 is not available, change 'CMD_COMPRESS'
# # to use 'gzip'.
#
###########################
# USAGE
###########################
#
# * It stores all backup copies in directory '/var/vmail/backup' by default,
# You can change it in variable $BACKUP_ROOTDIR below.
#
# * Set correct values for below variables:
#
# BACKUP_ROOTDIR
# MYSQL_ROOT_USER
# DATABASES
# DB_CHARACTER_SET
#
# * Add crontab job for root user (or whatever user you want):
#
# # crontab -e -u root
# 1 4 * * * bash /path/to/backup_mysql.sh
#
# * Make sure 'crond' service is running, and will start automatically when
# system startup:
#
# # ---- On RHEL/CentOS ----
# # chkconfig --level 345 crond on
# # /etc/init.d/crond status
#
# # ---- On Debian/Ubuntu ----
# # update-rc.d cron defaults
# # /etc/init.d/cron status
#
#########################################################
# Modify below variables to fit your need ----
#########################################################
# Keep backup for how many days. Default is 90 days.
KEEP_DAYS='90'
# Where to store backup copies.
export BACKUP_ROOTDIR='/var/vmail/backup'
# MySQL username. Root user is required to dump all databases.
export MYSQL_ROOT_USER='root'
# ~/.my.cnf
export MYSQL_DOT_MY_CNF='/root/.my.cnf'
# Databases we should backup.
# Multiple databases MUST be seperated by SPACE.
export DATABASES='mysql vmail roundcubemail amavisd iredadmin sogo iredapd sa_bayes'
# Database character set for ALL databases.
# Note: Currently, it doesn't support to specify character set for each databases.
export DB_CHARACTER_SET="utf8"
#########################################################
# You do *NOT* need to modify below lines.
#########################################################
export PATH='/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin'
# Commands.
export CMD_DATE='/bin/date'
export CMD_DU='du -sh'
export CMD_COMPRESS='bzip2 -9'
export COMPRESS_SUFFIX='bz2'
export CMD_MYSQL="mysql --defaults-file=${MYSQL_DOT_MY_CNF} -u${MYSQL_ROOT_USER}"
export CMD_MYSQLDUMP="mysqldump --defaults-file=${MYSQL_DOT_MY_CNF} -u${MYSQL_ROOT_USER} --events --ignore-table=mysql.event --default-character-set=${DB_CHARACTER_SET} --skip-comments"
# Date.
export YEAR="$(${CMD_DATE} +%Y)"
export MONTH="$(${CMD_DATE} +%m)"
export DAY="$(${CMD_DATE} +%d)"
export TIME="$(${CMD_DATE} +%H-%M-%S)"
export TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
# Pre-defined backup status
export BACKUP_SUCCESS='YES'
# Define, check, create directories.
export BACKUP_DIR="${BACKUP_ROOTDIR}/mysql/${YEAR}/${MONTH}/${DAY}"
# Find the old backup which should be removed.
export REMOVE_OLD_BACKUP='YES'
export KERNEL="$(uname -s)"
if [[ X"${KERNEL}" == X'Linux' ]]; then
shift_year=$(date --date="${KEEP_DAYS} days ago" "+%Y")
shift_month=$(date --date="${KEEP_DAYS} days ago" "+%m")
shift_day=$(date --date="${KEEP_DAYS} days ago" "+%d")
elif [[ X"${KERNEL}" == X'FreeBSD' ]]; then
shift_year=$(date -j -v-${KEEP_DAYS}d "+%Y")
shift_month=$(date -j -v-${KEEP_DAYS}d "+%m")
shift_day=$(date -j -v-${KEEP_DAYS}d "+%d")
elif [[ X"${KERNEL}" == X'OpenBSD' ]]; then
epoch_seconds_now="$(date +%s)"
epoch_shift="$((${KEEP_DAYS} * 86400))"
epoch_seconds_old="$((epoch_seconds_now - epoch_shift))"
shift_year=$(date -r ${epoch_seconds_old} "+%Y")
shift_month=$(date -r ${epoch_seconds_old} "+%m")
shift_day=$(date -r ${epoch_seconds_old} "+%d")
else
export REMOVE_OLD_BACKUP='NO'
fi
export REMOVED_BACKUP_DIR="${BACKUP_ROOTDIR}/mysql/${shift_year}/${shift_month}/${shift_day}"
export REMOVED_BACKUP_MONTH_DIR="${BACKUP_ROOTDIR}/mysql/${shift_year}/${shift_month}"
export REMOVED_BACKUP_YEAR_DIR="${BACKUP_ROOTDIR}/mysql/${shift_year}"
# Log file
export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
# Verify MySQL connection.
${CMD_MYSQL} -e "show databases" &>/dev/null
if [ X"$?" != X"0" ]; then
echo "[ERROR] MySQL username or password is incorrect in file ${0}." 1>&2
echo "Please fix them first." 1>&2
exit 255
fi
# Check and create directories.
[ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} 2>/dev/null
chown root ${BACKUP_DIR}
chmod 0400 ${BACKUP_DIR}
# Initialize log file.
echo "* Starting backup: ${TIMESTAMP}." >${LOGFILE}
echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE}
chmod 0400 ${LOGFILE}
# Backup.
echo "* Backing up databases: ${DATABASES}." >> ${LOGFILE}
for db in ${DATABASES}; do
#backup_db ${db} >>${LOGFILE}
#if [ X"$?" == X"0" ]; then
# echo " - ${db} [DONE]" >> ${LOGFILE}
#else
# [ X"${BACKUP_SUCCESS}" == X'YES' ] && export BACKUP_SUCCESS='NO'
#fi
output_sql="${BACKUP_DIR}/${db}-${TIMESTAMP}.sql"
# Check database existence
${CMD_MYSQL} -e "USE ${db}" &>/dev/null
if [ X"$?" == X'0' ]; then
# Dump
${CMD_MYSQLDUMP} ${db} > ${output_sql}
chmod 0400 ${output_sql}
if [ X"$?" == X'0' ]; then
# Get original SQL file size
original_size="$(${CMD_DU} ${output_sql} | awk '{print $1}')"
# Compress
${CMD_COMPRESS} ${output_sql} >>${LOGFILE}
chmod 0400 ${output_sql}.*
if [ X"$?" == X'0' ]; then
rm -f ${output_sql} >> ${LOGFILE}
fi
# Get compressed file size
compressed_file_name="${output_sql}.${COMPRESS_SUFFIX}"
compressed_size="$(${CMD_DU} ${compressed_file_name} | awk '{print $1}')"
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Database: ${db}, size: ${compressed_size} (original: ${original_size})', 'cron_backup_sql', '127.0.0.1', UTC_TIMESTAMP());"
else
# backup failed
export BACKUP_SUCCESS='NO'
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Database backup failed: ${db}. Log: $(cat ${LOGFILE})', 'cron_backup_sql', '127.0.0.1', UTC_TIMESTAMP());"
fi
# Log to SQL table `iredadmin.log`, so that global domain admins can
# check backup status (System -> Admin Log)
${CMD_MYSQL} iredadmin -e "${sql_log_msg}"
fi
done
# Append file size of backup files.
echo -e "* File size:\n----" >>${LOGFILE}
cd ${BACKUP_DIR} && \
${CMD_DU} *${TIMESTAMP}*sql* >>${LOGFILE}
echo "----" >>${LOGFILE}
echo "* Backup completed (Success? ${BACKUP_SUCCESS})." >>${LOGFILE}
if [ X"${BACKUP_SUCCESS}" == X'YES' ]; then
echo "==> Backup completed successfully."
else
echo -e "==> Backup completed with !!!ERRORS!!!.\n" 1>&2
fi
if [ X"${REMOVE_OLD_BACKUP}" == X'YES' -a -d ${REMOVED_BACKUP_DIR} ]; then
echo -e "* Old backup found. Deleting: ${REMOVED_BACKUP_DIR}." >>${LOGFILE}
rm -rf ${REMOVED_BACKUP_DIR} >> ${LOGFILE} 2>&1
# Try to remove empty directory.
rmdir ${REMOVED_BACKUP_MONTH_DIR} 2>/dev/null
rmdir ${REMOVED_BACKUP_YEAR_DIR} 2>/dev/null
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Remove old backup: ${REMOVED_BACKUP_DIR}.', 'cron_backup_sql', '127.0.0.1', UTC_TIMESTAMP());"
${CMD_MYSQL} iredadmin -e "${sql_log_msg}"
fi
echo "==> Detailed log (${LOGFILE}):"
echo "========================="
cat ${LOGFILE}
+230
View File
@@ -0,0 +1,230 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb@iredmail.org)
# Date: Mar 15, 2012
# Purpose: Dump whole LDAP tree with command 'slapcat'.
# License: This shell script is part of iRedMail project, released under
# GPL v2.
###########################
# REQUIREMENTS
###########################
#
# * Required commands:
# + slapcat
# + du
# + bzip2 # If bzip2 is not available, change 'CMD_COMPRESS' to use 'gzip'.
#
###########################
# USAGE
###########################
#
# * It stores all backup copies in directory '/var/vmail/backup' by default,
# You can change it with variable $BACKUP_ROOTDIR below.
#
# * Set correct values for below variables:
#
# BACKUP_ROOTDIR
#
# * Add crontab job for root user (or whatever user you want):
#
# # crontab -e -u root
# 1 4 * * * bash /path/to/backup_openldap.sh
#
# * Make sure 'crond' service is running, and will start automatically when
# system startup:
#
# # ---- On RHEL/CentOS ----
# # chkconfig --level 345 crond on
# # /etc/init.d/crond status
#
# # ---- On Debian/Ubuntu ----
# # update-rc.d cron defaults
# # /etc/init.d/cron status
#
###############################
# How to restore backup file:
###############################
# Please refer to wiki tutorial for detail steps:
# http://www.iredmail.org/docs/backup.restore.html
#
#########################################################
# Modify below variables to fit your need ----
#########################################################
# Where to store backup copies.
export BACKUP_ROOTDIR='/var/vmail/backup'
# Keep backup for how many days. Default is 90 days.
export KEEP_DAYS='90'
#########################################################
# You do *NOT* need to modify below lines.
#########################################################
export PATH="$PATH:/usr/sbin:/usr/local/sbin/"
# Commands.
export CMD_DATE='/bin/date'
export CMD_DU='du -sh'
export CMD_COMPRESS='bzip2 -9'
export COMPRESS_SUFFIX='bz2'
export CMD_MYSQL='mysql'
# MySQL user and password, used to log backup status to sql table `iredadmin.log`.
# You can find password of SQL user 'iredadmin' in iRedAdmin config file 'settings.py'.
#
# If MYSQL_PASSWD is empty, read password from /root/.my.cnf instead.
export MYSQL_USER='root'
export MYSQL_PASSWD=''
export MYSQL_DOT_MY_CNF='/root/.my.cnf'
if [ -f /etc/ldap/slapd.conf ]; then
export CMD_SLAPCAT='slapcat -f /etc/ldap/slapd.conf'
elif [ -f /etc/openldap/slapd.conf ]; then
export CMD_SLAPCAT='slapcat -f /etc/openldap/slapd.conf'
elif [ -f /usr/local/etc/openldap/slapd.conf ]; then
export CMD_SLAPCAT='slapcat -f /usr/local/etc/openldap/slapd.conf'
else
export CMD_SLAPCAT='slapcat'
fi
# Date.
export YEAR="$(${CMD_DATE} +%Y)"
export MONTH="$(${CMD_DATE} +%m)"
export DAY="$(${CMD_DATE} +%d)"
export TIME="$(${CMD_DATE} +%H-%M-%S)"
export TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
# Pre-defined backup status
export BACKUP_SUCCESS='NO'
#########
# Define, check, create directories.
#
# Backup directory.
export BACKUP_DIR="${BACKUP_ROOTDIR}/ldap/${YEAR}/${MONTH}"
export BACKUP_FILE="${BACKUP_DIR}/${TIMESTAMP}.ldif"
# Find the old backup which should be removed.
export REMOVE_OLD_BACKUP='YES'
export KERNEL="$(uname -s)"
if [[ X"${KERNEL}" == X'Linux' ]]; then
shift_year=$(date --date="${KEEP_DAYS} days ago" "+%Y")
shift_month=$(date --date="${KEEP_DAYS} days ago" "+%m")
shift_day=$(date --date="${KEEP_DAYS} days ago" "+%d")
elif [[ X"${KERNEL}" == X'FreeBSD' ]]; then
shift_year=$(date -j -v-${KEEP_DAYS}d "+%Y")
shift_month=$(date -j -v-${KEEP_DAYS}d "+%m")
shift_day=$(date -j -v-${KEEP_DAYS}d "+%d")
elif [[ X"${KERNEL}" == X'OpenBSD' ]]; then
epoch_seconds_now="$(date +%s)"
epoch_shift="$((${KEEP_DAYS} * 86400))"
epoch_seconds_old="$((epoch_seconds_now - epoch_shift))"
shift_year=$(date -r ${epoch_seconds_old} "+%Y")
shift_month=$(date -r ${epoch_seconds_old} "+%m")
shift_day=$(date -r ${epoch_seconds_old} "+%d")
else
export REMOVE_OLD_BACKUP='NO'
fi
export REMOVED_BACKUP_DIR="${BACKUP_ROOTDIR}/ldap/${shift_year}/${shift_month}"
export REMOVED_BACKUP_MONTH_DIR="${BACKUP_ROOTDIR}/ldap/${shift_year}/${shift_month}"
export REMOVED_BACKUP_YEAR_DIR="${BACKUP_ROOTDIR}/ldap/${shift_year}"
export REMOVED_BACKUPS="${BACKUP_ROOTDIR}/ldap/${shift_year}/${shift_month}/${shift_year}-${shift_month}-${shift_day}-*"
# Log file
export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
# Check and create directories.
[ -d ${BACKUP_DIR} ] || mkdir -p ${BACKUP_DIR}
chown root ${BACKUP_DIR}
chmod 0700 ${BACKUP_DIR}
# Initialize log file.
echo "* Starting backup at ${TIMESTAMP}" >> ${LOGFILE}
echo "* Backup directory: ${BACKUP_DIR}." >> ${LOGFILE}
# Backup
echo "* Dumping LDAP data into file: ${BACKUP_FILE}..." >> ${LOGFILE}
${CMD_SLAPCAT} > ${BACKUP_FILE}
if [ X"$?" == X"0" ]; then
export BACKUP_SUCCESS='YES'
# Get original backup file size
original_size="$(${CMD_DU} ${BACKUP_FILE} | awk '{print $1}')"
# Compress backup file.
echo "* Compressing LDIF file with command: '${CMD_COMPRESS}' ..." >> ${LOGFILE}
${CMD_COMPRESS} ${BACKUP_FILE} >> ${LOGFILE} 2>&1
echo "* [DONE]" >>${LOGFILE}
# Get compressed file size
compressed_file_name="${BACKUP_FILE}.${COMPRESS_SUFFIX}"
compressed_size="$(${CMD_DU} ${compressed_file_name} | awk '{print $1}')"
echo -n "* Removing plain LDIF file: ${BACKUP_FILE}..." >>${LOGFILE}
rm -f ${BACKUP_FILE} >> ${LOGFILE} 2>&1
[ X"$?" == X"0" ] && echo -e "\t[DONE]" >>${LOGFILE}
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Backup LDAP data, size: ${original_size}, compressed: ${compressed_size}', 'cron_backup_ldap', '127.0.0.1', UTC_TIMESTAMP());"
else
# Log failure
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Backup LDAP data failed, check log file ${LOGFILE} for more details.', 'cron_backup_ldap', '127.0.0.1', UTC_TIMESTAMP());"
fi
# Log to SQL table `iredadmin.log`, so that global domain admins can
# check backup status (System -> Admin Log)
export LOG_BACKUP_STATUS='YES'
if [[ -n ${MYSQL_USER} ]]; then
if [[ -n ${MYSQL_PASSWD} ]]; then
export CMD_MYSQL_ROOT="${CMD_MYSQL} -u${MYSQL_USER} -p${MYSQL_PASSWD}"
else
export CMD_MYSQL_ROOT="${CMD_MYSQL} --defaults-file=${MYSQL_DOT_MY_CNF} -u${MYSQL_USER}"
fi
${CMD_MYSQL_ROOT} -e "USE iredadmin;" &>/dev/null
if [[ X"$?" != X'0' ]]; then
export LOG_BACKUP_STATUS='NO'
fi
fi
if [[ X"${LOG_BACKUP_STATUS}" == X'YES' ]]; then
${CMD_MYSQL_ROOT} iredadmin -e "${sql_log_msg}" >>${LOGFILE} 2>&1
fi
# Append file size of backup files to log file.
echo "* File size:" >>${LOGFILE}
echo "=================" >>${LOGFILE}
${CMD_DU} ${BACKUP_FILE}* >>${LOGFILE}
echo "=================" >>${LOGFILE}
# Print some message. It will cause cron generates an email to root user.
if [ X"${BACKUP_SUCCESS}" == X'YES' ]; then
echo "* [ OK ] Backup completes successfully." >> ${LOGFILE}
else
echo "* <<< ERROR >>> Backup not successfully complete." >> ${LOGFILE}
fi
if [ X"${REMOVE_OLD_BACKUP}" == X'YES' -a -d ${REMOVED_BACKUP_DIR} ]; then
echo -e "* Delete old backup under ${REMOVED_BACKUP_DIR}." >> ${LOGFILE}
echo -e "* Suppose to delete: ${REMOVED_BACKUPS}" >> ${LOGFILE}
rm -rf ${REMOVED_BACKUPS} >> ${LOGFILE} 2>&1
# Try to remove empty directory.
rmdir ${REMOVED_BACKUP_MONTH_DIR} 2>/dev/null
rmdir ${REMOVED_BACKUP_YEAR_DIR} 2>/dev/null
if [[ X"${LOG_BACKUP_STATUS}" == X'YES' ]]; then
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Remove old backup: ${REMOVED_BACKUPS}.', 'cron_backup_sql', '127.0.0.1', UTC_TIMESTAMP());"
${CMD_MYSQL_ROOT} iredadmin -e "${sql_log_msg}"
fi
fi
cat ${LOGFILE}
+205
View File
@@ -0,0 +1,205 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb _at_ iredmail.org)
# Date: 2012-09-06
# Purpose: Backup specified PostgreSQL databases with command 'pg_dump'.
# License: This shell script is part of iRedMail project, released under GPLv2.
###########################
# REQUIREMENTS
###########################
#
# * Required commands:
# + pg_dump
# + du
# + bzip2 # If bzip2 is not available, change 'CMD_COMPRESS'
# # to use 'gzip'.
#
###########################
# USAGE
###########################
#
# * It stores all backup copies in directory '/var/vmail/backup' by default,
# You can change it in variable $BACKUP_ROOTDIR below.
#
# * Set correct values for below variables:
#
# SYS_USER_PGSQL
# BACKUP_ROOTDIR
# DATABASES
#
# * Add crontab job for root user (or whatever user you want):
#
# # crontab -e -u root
# 1 4 * * * bash /path/to/backup_pgsql.sh
#
# * Make sure 'crond' service is running.
#
#########################################################
# Modify below variables to fit your need ----
#########################################################
# Keep backup for how many days. Default is 90 days.
KEEP_DAYS='90'
# System user used to run PostgreSQL daemon.
# - On Linux, it's postgres.
# - On FreeBSD, it's pgsql.
# - On OpenBSD, it's _postgresql.
export SYS_USER_PGSQL='postgres'
# Where to store backup copies.
export BACKUP_ROOTDIR='/var/vmail/backup'
# Databases we should backup.
# Multiple databases MUST be seperated by SPACE.
# Your iRedMail server might have below databases:
# vmail, roundcubemail, cluebringer, amavisd, iredadmin.
export DATABASES='vmail roundcubemail amavisd iredadmin sogo iredapd sa_bayes'
#########################################################
# You do *NOT* need to modify below lines.
#########################################################
export PATH='/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin'
# Commands.
export CMD_DATE='/bin/date'
export CMD_DU='du -sh'
export CMD_COMPRESS='bzip2 -9'
export COMPRESS_SUFFIX='bz2'
export CMD_PG_DUMP='pg_dump'
# Date.
export YEAR="$(${CMD_DATE} +%Y)"
export MONTH="$(${CMD_DATE} +%m)"
export DAY="$(${CMD_DATE} +%d)"
export TIME="$(${CMD_DATE} +%H-%M-%S)"
export TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
# Pre-defined backup status
export BACKUP_SUCCESS='YES'
# Define, check, create directories.
export BACKUP_DIR="${BACKUP_ROOTDIR}/pgsql/${YEAR}/${MONTH}/${DAY}"
# Find the old backup which should be removed.
export REMOVE_OLD_BACKUP='YES'
export KERNEL="$(uname -s)"
if [[ X"${KERNEL}" == X'Linux' ]]; then
shift_year=$(date --date="${KEEP_DAYS} days ago" "+%Y")
shift_month=$(date --date="${KEEP_DAYS} days ago" "+%m")
shift_day=$(date --date="${KEEP_DAYS} days ago" "+%d")
elif [[ X"${KERNEL}" == X'FreeBSD' ]]; then
shift_year=$(date -j -v-${KEEP_DAYS}d "+%Y")
shift_month=$(date -j -v-${KEEP_DAYS}d "+%m")
shift_day=$(date -j -v-${KEEP_DAYS}d "+%d")
elif [[ X"${KERNEL}" == X'OpenBSD' ]]; then
epoch_seconds_now="$(date +%s)"
epoch_shift="$((${KEEP_DAYS} * 86400))"
epoch_seconds_old="$((epoch_seconds_now - epoch_shift))"
shift_year=$(date -r ${epoch_seconds_old} "+%Y")
shift_month=$(date -r ${epoch_seconds_old} "+%m")
shift_day=$(date -r ${epoch_seconds_old} "+%d")
else
export REMOVE_OLD_BACKUP='NO'
fi
export REMOVED_BACKUP_DIR="${BACKUP_ROOTDIR}/pgsql/${shift_year}/${shift_month}/${shift_day}"
export REMOVED_BACKUP_MONTH_DIR="${BACKUP_ROOTDIR}/pgsql/${shift_year}/${shift_month}"
export REMOVED_BACKUP_YEAR_DIR="${BACKUP_ROOTDIR}/pgsql/${shift_year}"
# Log file
export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
# Check and create directories.
[ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} 2>/dev/null
chown root ${BACKUP_DIR}
chmod 0700 ${BACKUP_DIR}
# Get HOME directory of SYS_USER_PGSQL
export PGSQL_USER_HOMEDIR="$(su - ${SYS_USER_PGSQL} -c 'echo $HOME')"
# Initialize log file.
echo "* Starting at: ${YEAR}-${MONTH}-${DAY}-${TIME}." >${LOGFILE}
echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE}
# Backup.
echo "* Backing up databases: ${DATABASES}." >> ${LOGFILE}
for db in ${DATABASES}; do
output_sql="${db}-${TIMESTAMP}.sql"
# Check database existence
su - "${SYS_USER_PGSQL}" -c "psql -d ${db} -c '\q' >/dev/null 2>&1"
# Dump
if [ X"$?" == X'0' ]; then
su - "${SYS_USER_PGSQL}" -c "${CMD_PG_DUMP} ${db} > ${PGSQL_USER_HOMEDIR}/${output_sql}"
if [ X"$?" == X'0' ]; then
# Move to backup directory.
mv ${PGSQL_USER_HOMEDIR}/${output_sql} ${BACKUP_DIR}
cd ${BACKUP_DIR}
# Get original SQL file size
original_size="$(${CMD_DU} ${output_sql} | awk '{print $1}')"
# Compress
${CMD_COMPRESS} ${output_sql} >>${LOGFILE}
rm -f ${output_sql} >> ${LOGFILE}
echo -e " + ${db} [DONE]" >> ${LOGFILE}
# Get compressed file size
compressed_file_name="${output_sql}.${COMPRESS_SUFFIX}"
compressed_size="$(${CMD_DU} ${compressed_file_name} | awk '{print $1}')"
# Log to SQL table `iredadmin.log`, so that global domain admins
# can check backup status
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Database: ${db}, size: ${compressed_size} (original: ${original_size})', 'cron_backup_sql', '127.0.0.1', NOW());"
else
export BACKUP_SUCCESS='NO'
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'Database backup failed: ${db}, check log file ${LOGFILE} for more details.', 'cron_backup_sql', '127.0.0.1', NOW());"
fi
su - "${SYS_USER_PGSQL}" >/dev/null <<EOF
psql -d iredadmin <<EOF2
${sql_log_msg}
EOF2
EOF
fi
done
# Append file size of backup files.
echo -e "* File size:\n----" >>${LOGFILE}
cd ${BACKUP_DIR} && \
${CMD_DU} *${TIMESTAMP}*sql* >>${LOGFILE}
echo "----" >>${LOGFILE}
echo "* Backup completed (Success? ${BACKUP_SUCCESS})." >>${LOGFILE}
if [ X"${BACKUP_SUCCESS}" == X'YES' ]; then
echo -e "\n[OK] Backup successfully completed.\n"
else
echo -e "\n[ERROR] Backup completed with ERRORS.\n" 1>&2
fi
if [ X"${REMOVE_OLD_BACKUP}" == X'YES' -a -d ${REMOVED_BACKUP_DIR} ]; then
echo -e "* Old backup found. Deleting: ${REMOVED_BACKUP_DIR}." >>${LOGFILE}
rm -rf ${REMOVED_BACKUP_DIR} >> ${LOGFILE} 2>&1
# Try to remove empty directory.
rmdir ${REMOVED_BACKUP_MONTH_DIR} 2>/dev/null
rmdir ${REMOVED_BACKUP_YEAR_DIR} 2>/dev/null
su - ${SYS_USER_PGSQL} -c "psql -d iredadmin" <<EOF
INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES
('backup', 'info', 'Remove old backup: ${REMOVED_BACKUP_DIR}.', 'cron_backup_sql', '127.0.0.1', NOW());
EOF
fi
echo "* Backup log: ${LOGFILE}:"
cat ${LOGFILE}
+156
View File
@@ -0,0 +1,156 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb@iredmail.org)
# Purpose: Backup SOGo database with `sogo-tool` command.
# You can run this script manually or via crontab (recommended).
#
# Backing up SOGo data by dumping SQL database to a plain SQL file is not ideal
# because:
#
# - it's hard to restore single user's data
# - if SOGo changes some SQL structure, it's hard to restore all data.
#
# This script does backup with 'sogo-tool backup' command to avoid issues
# mentioned above, it stores each user's data in a single file, so you can
# restore a single user's data or all users data with 'sogo-tool restore'.
# Required commands:
#
# + sogo-tool
# + tar
# + bzip2
# USAGE
#
# * It stores backup copies under '/var/vmail/backup/sogo' by default,
# feel free to change it with variable BACKUP_ROOTDIR below.
#
# * If you want to log a message in iRedAdmin database (`iredadmin.log`),
# please specify correct MySQL username in variable MYSQL_USER, and
# write the same MySQL username and password in file /root/.my.cnf.
#
# * Add crontab job for root user:
#
# # crontab -e -u root
# 1 4 * * * bash /var/vmail/backup/backup_sogo.sh
# Keep backup for how many days. Defaults to 90 days.
export KEEP_DAYS='90'
# Where to store backup copies.
export BACKUP_ROOTDIR='/var/vmail/backup'
# MySQL username. Root user is required to dump all databases.
export MYSQL_USER='root'
# ~/.my.cnf
export MYSQL_DOT_MY_CNF='/root/.my.cnf'
export CMD_MYSQL="mysql --defaults-file=${MYSQL_DOT_MY_CNF} -u${MYSQL_USER}"
#########################################################
# You do *NOT* need to modify below lines.
#########################################################
# FreeBSD has 'sogo-tool' in different directory.
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/GNUstep/Local/Tools/Admin:$PATH
# Commands.
export CMD_DATE='/bin/date'
export CMD_DU='du -sh'
export CMD_COMPRESS='bzip2 -9'
export COMPRESS_SUFFIX='bz2'
# Path to sogo-tool command.
export CMD_SOGO_TOOL='sogo-tool'
# Date.
export YEAR="$(${CMD_DATE} +%Y)"
export MONTH="$(${CMD_DATE} +%m)"
export DAY="$(${CMD_DATE} +%d)"
export TIME="$(${CMD_DATE} +%H-%M-%S)"
export TIMESTAMP="${YEAR}-${MONTH}-${DAY}-${TIME}"
# Pre-defined backup status
export BACKUP_SUCCESS='YES'
# Define, check, create directories.
export BACKUP_DIR="${BACKUP_ROOTDIR}/sogo/${YEAR}/${MONTH}/${DAY}"
# Find the old backup which should be removed.
export REMOVE_OLD_BACKUP='YES'
export KERNEL="$(uname -s)"
if [[ X"${KERNEL}" == X'Linux' ]]; then
shift_year=$(date --date="${KEEP_DAYS} days ago" "+%Y")
shift_month=$(date --date="${KEEP_DAYS} days ago" "+%m")
shift_day=$(date --date="${KEEP_DAYS} days ago" "+%d")
elif [[ X"${KERNEL}" == X'FreeBSD' ]]; then
shift_year=$(date -j -v-${KEEP_DAYS}d "+%Y")
shift_month=$(date -j -v-${KEEP_DAYS}d "+%m")
shift_day=$(date -j -v-${KEEP_DAYS}d "+%d")
elif [[ X"${KERNEL}" == X'OpenBSD' ]]; then
epoch_seconds_now="$(date +%s)"
epoch_shift="$((${KEEP_DAYS} * 86400))"
epoch_seconds_old="$((epoch_seconds_now - epoch_shift))"
shift_year=$(date -r ${epoch_seconds_old} "+%Y")
shift_month=$(date -r ${epoch_seconds_old} "+%m")
shift_day=$(date -r ${epoch_seconds_old} "+%d")
else
export REMOVE_OLD_BACKUP='NO'
fi
export REMOVED_BACKUP="${BACKUP_ROOTDIR}/sogo/${shift_year}/${shift_month}/${shift_day}.tar.bz2"
export REMOVED_BACKUP_MONTH_DIR="${BACKUP_ROOTDIR}/sogo/${shift_year}/${shift_month}"
export REMOVED_BACKUP_YEAR_DIR="${BACKUP_ROOTDIR}/sogo/${shift_year}"
# Check and create directories.
if [ ! -d ${BACKUP_DIR} ]; then
mkdir -p ${BACKUP_DIR} 2>/dev/null
chown -R root ${BACKUP_DIR}
chmod -R 0700 ${BACKUP_DIR}
fi
# Backup
echo "* Backup all users' data under ${BACKUP_DIR}"
${CMD_SOGO_TOOL} backup ${BACKUP_DIR} ALL
dir_name="$(dirname ${BACKUP_DIR})"
base_name="$(basename ${BACKUP_DIR})"
if ls ${BACKUP_DIR} | grep -i '[a-z0-9]' &>/dev/null; then
# Backup is not empty.
chown -R root ${BACKUP_DIR}
chmod -R 0700 ${BACKUP_DIR}
# Get original size of backup files
original_size="$(${CMD_DU} ${BACKUP_DIR} | awk '{print $1}')"
# Compress the directory.
echo "* Compress backup files."
cd ${dir_name} &&\
tar cjf ${base_name}.tar.bz2 ${base_name} && \
chown root ${base_name}.tar.bz2 && \
chmod 0400 ${base_name}.tar.bz2 && \
rm -rf ${base_name}
compressed_size="$(${CMD_DU} ${base_name}.tar.bz2 | awk '{print $1}')"
if [ -n X"${MYSQL_USER}" -a -f ${MYSQL_DOT_MY_CNF} ]; then
sql_log_msg="INSERT INTO log (event, loglevel, msg, admin, ip, timestamp) VALUES ('backup', 'info', 'SOGo data. size: ${compressed_size} (original: ${original_size})', 'cron_backup_sogo', '127.0.0.1', UTC_TIMESTAMP());"
${CMD_MYSQL} iredadmin -e "${sql_log_msg}"
fi
else
echo "* Backup is empty, remove temporary backup directory: ${BACKUP_DIR}."
rm -rf ${BACKUP_DIR} &>/dev/null
fi
if [ X"${REMOVE_OLD_BACKUP}" == X'YES' -a -f ${REMOVED_BACKUP} ]; then
echo -e "* Old backup found. Deleting: ${REMOVED_BACKUP}."
rm -rf ${REMOVED_BACKUP} &>/dev/null
# Try to remove empty directory.
rmdir ${REMOVED_BACKUP_MONTH_DIR} 2>/dev/null
rmdir ${REMOVED_BACKUP_YEAR_DIR} 2>/dev/null
fi
exit 0
+283
View File
@@ -0,0 +1,283 @@
#!/usr/bin/env python3
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add new OpenLDAP user for postfix mail server.
# Project: iRedMail (http://www.iredmail.org/)
# --------------------------- WARNING ------------------------------
# This script only works under iRedMail >= 0.4.0 due to ldap schema
# changes.
# ------------------------------------------------------------------
# ---------------------------- USAGE -------------------------------
# Put your user list in a csv format file, e.g. users.csv, and then
# import users listed in the file:
#
# $ python3 create_mail_user_OpenLDAP.py users.csv
#
# ------------------------------------------------------------------
# ------------------------- SETTINGS -------------------------------
# LDAP server address.
LDAP_URI = 'ldap://127.0.0.1:389'
# LDAP base dn.
BASEDN = 'o=domains,dc=example,dc=com'
# Bind dn/password
BINDDN = 'cn=Manager,dc=example,dc=com'
BINDPW = 'password'
# Storage base directory.
STORAGE_BASE_DIRECTORY = '/var/vmail/vmail1'
# Append timestamp in maildir path.
APPEND_TIMESTAMP_IN_MAILDIR = True
# Get base directory and storage node.
std = STORAGE_BASE_DIRECTORY.rstrip('/').split('/')
STORAGE_NODE = std.pop()
STORAGE_BASE = '/'.join(std)
# Hashed maildir: True, False.
# Example:
# domain: domain.ltd,
# user: zhang (zhang@domain.ltd)
#
# - hashed: d/do/domain.ltd/z/zh/zha/zhang/
# - normal: domain.ltd/zhang/
HASHED_MAILDIR = True
# Default password schemes.
# Multiple passwords are supported if you separate schemes with '+'.
# For example: 'SSHA+NTLM', 'CRAM-MD5+SSHA', 'CRAM-MD5+SSHA+MD5'.
DEFAULT_PASSWORD_SCHEME = 'SSHA512'
# Do not prefix password scheme name in password hash.
HASHES_WITHOUT_PREFIXED_PASSWORD_SCHEME = ['NTLM']
# ------------------------------------------------------------------
import os
import sys
import time
import datetime
from subprocess import Popen, PIPE
import re
try:
#import ldap
import ldif
except ImportError:
print("""
Error: You don't have python-ldap installed, Please install it first.
You can install it like this:
- On RHEL/CentOS 5.x:
$ sudo yum install python-ldap
- On Debian & Ubuntu:
$ sudo apt install python-ldap
""")
sys.exit()
def usage():
print("""
CSV file format:
domain name, username, password, [common name], [quota_in_bytes], [groups]
Example #1:
iredmail.org, zhang, plain_password, Zhang Huangbin, 104857600, group1:group2
Example #2:
iredmail.org, zhang, plain_password, Zhang Huangbin, ,
Example #3:
iredmail.org, zhang, plain_password, , 104857600, group1:group2
Note:
- Domain name, username and password are REQUIRED, others are optional:
+ common name.
* It will be the same as username if it's empty.
* Non-ascii character is allowed in this field, they will be
encoded automaticly. Such as Chinese, Korea, Japanese, etc.
+ quota. It will be 0 (unlimited quota) if it's empty.
+ groups.
* valid group name (hr@a.cn): hr
* incorrect group name: hr@a.cn
* Do *NOT* include domain name in group name, it will be
appended automaticly.
* Multiple groups must be seperated by colon.
- Leading and trailing Space will be ignored.
""")
def mail_to_user_dn(mail):
"""Convert email address to ldap dn of normail mail user."""
if mail.count('@') != 1:
return ''
user, domain = mail.split('@')
# User DN format.
# mail=user@domain.ltd,domainName=domain.ltd,[LDAP_BASEDN]
dn = 'mail=%s,ou=Users,domainName=%s,%s' % (mail, domain, BASEDN)
return dn
def generate_password_with_doveadmpw(plain_password, scheme=None):
"""Generate password hash with `doveadm pw` command.
Return SSHA instead if no 'doveadm' command found or other error raised."""
if not scheme:
scheme = DEFAULT_PASSWORD_SCHEME
scheme = scheme.upper()
p = str(plain_password).strip()
pp = Popen(['doveadm', 'pw', '-s', scheme, '-p', p], stdout=PIPE)
pw = pp.communicate()[0]
if scheme in HASHES_WITHOUT_PREFIXED_PASSWORD_SCHEME:
pw.lstrip('{' + scheme + '}')
# remove '\n'
pw = pw.strip()
return pw
def get_days_of_today():
"""Return number of days since 1970-01-01."""
today = datetime.date.today()
try:
return (datetime.date(today.year, today.month, today.day) - datetime.date(1970, 1, 1)).days
except:
return 0
def ldif_mailuser(domain, username, passwd, cn, quota, groups=''):
# Append timestamp in maildir path
DATE = time.strftime('%Y.%m.%d.%H.%M.%S')
TIMESTAMP_IN_MAILDIR = ''
if APPEND_TIMESTAMP_IN_MAILDIR:
TIMESTAMP_IN_MAILDIR = '-%s' % DATE
if quota == '':
quota = '0'
# Remove SPACE in username.
username = str(username).lower().strip().replace(' ', '')
if cn == '':
cn = username
mail = username + '@' + domain
dn = mail_to_user_dn(mail)
# Get group list.
if groups.strip() != '':
groups = groups.strip().split(':')
for i in range(len(groups)):
groups[i] = groups[i] + '@' + domain
maildir_domain = str(domain).lower()
if HASHED_MAILDIR is True:
str1 = str2 = str3 = username[0]
if len(username) >= 3:
str2 = username[1]
str3 = username[2]
elif len(username) == 2:
str2 = str3 = username[1]
maildir_user = "%s/%s/%s/%s%s/" % (str1, str2, str3, username, TIMESTAMP_IN_MAILDIR, )
mailMessageStore = maildir_domain + '/' + maildir_user
else:
mailMessageStore = "%s/%s%s/" % (domain, username, TIMESTAMP_IN_MAILDIR)
homeDirectory = STORAGE_BASE_DIRECTORY + '/' + mailMessageStore
mailMessageStore = STORAGE_NODE + '/' + mailMessageStore
ldif = [
('objectClass', ['inetOrgPerson', 'mailUser', 'shadowAccount', 'amavisAccount']),
('mail', [mail]),
('userPassword', generate_password_with_doveadmpw(passwd)),
('mailQuota', [quota]),
('cn', [cn]),
('sn', [username]),
('uid', [username]),
('storageBaseDirectory', [STORAGE_BASE]),
('mailMessageStore', [mailMessageStore]),
('homeDirectory', [homeDirectory]),
('accountStatus', ['active']),
('enabledService', ['internal', 'doveadm', 'lib-storage',
'indexer-worker', 'dsync', 'quota-status',
'mail',
'smtp', 'smtpsecured', 'smtptls',
'pop3', 'pop3secured', 'pop3tls',
'imap', 'imapsecured', 'imaptls',
'deliver', 'lda', 'forward', 'senderbcc', 'recipientbcc',
'managesieve', 'managesievesecured',
'sieve', 'sievesecured', 'lmtp', 'sogo',
'shadowaddress',
'displayedInGlobalAddressBook']),
('memberOfGroup', groups),
# shadowAccount integration.
('shadowLastChange', [str(get_days_of_today())]),
# Amavisd integration.
('amavisLocal', ['TRUE'])]
return dn, ldif
if len(sys.argv) != 2 or len(sys.argv) > 2:
print("""Usage: $ python3 %s users.csv""" % sys.argv[0])
usage()
sys.exit()
else:
CSV = sys.argv[1]
if not os.path.exists(CSV):
print("Error: file not exist:", CSV)
sys.exit()
ldif_file = CSV + '.ldif'
# Remove exist LDIF file.
if os.path.exists(ldif_file):
print("< INFO > Remove exist file:", ldif_file)
os.remove(ldif_file)
# Read user list.
userList = open(CSV, 'rb')
# Convert to LDIF format.
for entry in userList.readlines():
entry = entry.rstrip()
domain, username, passwd, cn, quota, groups = re.split(r'\s?,\s?', entry)
dn, data = ldif_mailuser(domain, username, passwd, cn, quota, groups)
# Write LDIF data.
result = open(ldif_file, 'a')
ldif_writer = ldif.LDIFWriter(result)
ldif_writer.unparse(dn, data)
ldif_file_path = os.path.abspath(ldif_file)
print("< INFO > User data are stored in %s, you can verify it before importing it." % ldif_file_path)
print("< INFO > You can import it with below command:")
print("ldapadd -x -D %s -W -f %s" % (BINDDN, ldif_file_path))
# Prompt to import user data.
"""
answer = raw_input("Would you like to import them now? [y|N]").lower().strip()
if answer == 'y':
# Import data.
conn = ldap.initialize(LDAP_URI)
conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3) # Use LDAP v3
conn.bind_s(BINDDN, BINDPW)
conn.unbind()
else:
pass
"""
+312
View File
@@ -0,0 +1,312 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb _at_ iredmail.org)
# Purpose: Add new OpenLDAP user for postfix mail server.
# Project: iRedMail (http://www.iredmail.org/)
# --------------------------- WARNING ------------------------------
# This script only works under iRedMail >= 0.8.4 due to ldap schema
# changes.
# ------------------------------------------------------------------
# --------------------------- USAGE --------------------------------
# 1) Please change variables below to fit your env:
#
# - In 'Global Setting' section:
# * STORAGE_BASE_DIRECTORY
#
# - In 'LDAP Setting' section:
# * LDAP_SUFFIX
# * BINDDN
# * BINDPW
# * QUOTA
#
# - In 'Virtual Domains & Users' section:
# * QUOTA
# * TRANSPORT
# * PASSWORD_SCHEME # SSHA is recommended.
# * DEFAULT_PASSWD
# * USE_DEFAULT_PASSWD
#
# - Pure-FTPd integration:
# * PUREFTPD_INTEGRATION # <- set to 'YES' if you want to integrate it.
# * FTP_BASE_DIRECTORY # <- directory used to store FTP data.
#
# - Optional variables:
# * SEND_WELCOME_MSG
#
# 2) Execute this script with domain name and username (without @domain) directly:
#
# shell# bash create_mail_user_OpenLDAP.sh example.com new_user
#
# It will create a mail user with mail address "new_user@example.com".
# To add multiple mail users, just list all usernames:
#
# shell# bash create_mail_user_OpenLDAP.sh example.com new_user new_user2 new_user3
#
# ------------------------------------------------------------------
# Source functions.
. ../conf/global
. ../conf/core
# ----------------------------------------------
# ------------ Global Setting ------------------
# ----------------------------------------------
# Storage base directory used to store users' mail.
# mailbox of LDAP user will be:
# ${STORAGE_BASE_DIRECTORY}/${DOMAIN_NAME}/${USERNAME}/
# Such as:
# /var/vmail/vmail1/iredmail.org/zhb/
# -------------------|===========|-----|
# STORAGE_BASE_DIRECTORY|DOMAIN_NAME|USERNAME
#
STORAGE_BASE_DIRECTORY="/var/vmail/vmail1"
# ------------------------------------------------------------------
# -------------------------- LDAP Setting --------------------------
# ------------------------------------------------------------------
LDAP_SUFFIX="dc=example,dc=com"
# Setting 'BASE_DN'.
BASE_DN="o=domains,${LDAP_SUFFIX}"
# Setting 'DOMAIN_NAME' and DOMAIN_DN':
# * DOMAIN will be used in mail address: ${USERNAME}@${DOMAIN}
# * DOMAIN_DN will be used in LDAP dn.
DOMAIN_NAME="$1"
DOMAIN_DN="domainName=${DOMAIN_NAME}"
OU_USER_DN="ou=Users"
# ---------- rootdn of LDAP Server ----------
# Setting rootdn of LDAP.
BINDDN="cn=Manager,${LDAP_SUFFIX}"
# Setting rootpw of LDAP.
BINDPW='passwd'
# ---------- Virtual Domains & Users --------------
# Set default quota for LDAP users: 104857600 = 100M
QUOTA='1048576000'
# Default MTA Transport (Defined in postfix master.cf).
TRANSPORT='dovecot'
# Password scheme. Support schemes: BCRYPT, SSHA512, SSHA, PLAIN.
PASSWORD_SCHEME='SSHA'
DEFAULT_PASSWD='888888'
USE_DEFAULT_PASSWD='NO'
# ------------------------------------------------------------------
# -------------------- Pure-FTPd Integration -----------------------
# ------------------------------------------------------------------
# Add objectClass and attributes for pure-ftpd integration.
# Note: You must inlucde pureftpd.schema in OpenLDAP slapd.conf first.
PUREFTPD_INTEGRATION='NO'
FTP_BASE_DIRECTORY='/home/ftp'
# ------------------------------------------------------------------
# ------------------------- Welcome Msg ----------------------------
# ------------------------------------------------------------------
# Send a welcome mail after user created.
SEND_WELCOME_MSG='NO'
# Set welcome mail info.
WELCOME_MSG_SUBJECT="Welcome!"
WELCOME_MSG_BODY="Welcome, new user."
# -------------------------------------------
# ----------- End Global Setting ------------
# -------------------------------------------
# Time stamp, will be appended in maildir.
DATE="$(date +%Y.%m.%d.%H.%M.%S)"
STORAGE_BASE="$(dirname ${STORAGE_BASE_DIRECTORY})"
STORAGE_NODE="$(basename ${STORAGE_BASE_DIRECTORY})"
# Get days since 1970-01-01
EPOCH_SECONDS="$(date +%s)"
DAYS_SINCE_EPOCH="$((EPOCH_SECONDS / 24 / 60 / 60))"
generate_password_hash()
{
# Usage: generate_password_hash <password-scheme> <plain-password>
_scheme="${1}"
_password="${2}"
if [ X"${_scheme}" == X'BCRYPT' ]; then
_scheme='BLF-CRYPT'
fi
doveadm pw -s "${_scheme}" -p "${_password}"
}
add_new_domain()
{
domain="$(echo ${1} | tr '[A-Z]' '[a-z]')"
ldapsearch -x -D "${BINDDN}" -w "${BINDPW}" -b "${BASE_DN}" | grep "domainName: ${domain}" >/dev/null
if [ X"$?" != X"0" ]; then
echo "Add new domain: ${domain}."
ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
dn: ${DOMAIN_DN},${BASE_DN}
objectClass: mailDomain
domainName: ${domain}
mtaTransport: ${TRANSPORT}
accountStatus: active
enabledService: mail
EOF
else
:
fi
ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
dn: ${OU_USER_DN},${DOMAIN_DN},${BASE_DN}
objectClass: organizationalUnit
objectClass: top
ou: Users
EOF
ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
dn: ou=Groups,${DOMAIN_DN},${BASE_DN}
objectClass: organizationalUnit
objectClass: top
ou: Groups
EOF
ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
dn: ou=Aliases,${DOMAIN_DN},${BASE_DN}
objectClass: organizationalUnit
objectClass: top
ou: Aliases
EOF
ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
dn: ou=Externals,${DOMAIN_DN},${BASE_DN}
objectClass: organizationalUnit
objectClass: top
ou: Externals
EOF
}
add_new_user()
{
USERNAME="$(echo $1 | tr [A-Z] [a-z])"
MAIL="$( echo $2 | tr [A-Z] [a-z])"
maildir="${DOMAIN_NAME}/$(hash_maildir ${USERNAME})"
# Generate user password.
if [ X"${USE_DEFAULT_PASSWD}" == X'YES' ]; then
PASSWD="$(generate_password_hash ${PASSWORD_SCHEME} ${DEFAULT_PASSWD})"
else
PASSWD="$(generate_password_hash ${PASSWORD_SCHEME} ${USERNAME})"
fi
if [ X"${PUREFTPD_INTEGRATION}" == X'YES' ]; then
LDIF_PUREFTPD_USER="objectClass: PureFTPdUser
FTPStatus: enabled
FTPQuotaFiles: 50
FTPQuotaMBytes: 10
FTPDownloadBandwidth: 50
FTPUploadBandwidth: 50
FTPDownloadRatio: 5
FTPUploadRatio: 1
FTPHomeDir: ${FTP_BASE_DIRECTORY}/${DOMAIN_NAME}/${USERNAME}/
"
else
LDIF_PUREFTPD_USER=''
fi
ldapadd -x -D "${BINDDN}" -w "${BINDPW}" <<EOF
dn: mail=${MAIL},${OU_USER_DN},${DOMAIN_DN},${BASE_DN}
objectClass: inetOrgPerson
objectClass: shadowAccount
objectClass: amavisAccount
objectClass: mailUser
objectClass: top
accountStatus: active
storageBaseDirectory: ${STORAGE_BASE}
homeDirectory: ${STORAGE_BASE_DIRECTORY}/${maildir}
mailMessageStore: ${STORAGE_NODE}/${maildir}
mail: ${MAIL}
mailQuota: ${QUOTA}
userPassword: ${PASSWD}
cn: ${USERNAME}
sn: ${USERNAME}
givenName: ${USERNAME}
uid: ${USERNAME}
shadowLastChange: ${DAYS_SINCE_EPOCH}
amavisLocal: TRUE
enabledService: internal
enabledService: doveadm
enabledService: lib-storage
enabledService: quota-status
enabledService: indexer-worker
enabledService: dsync
enabledService: mail
enabledService: pop3
enabledService: pop3secured
enabledService: pop3tls
enabledService: imap
enabledService: imapsecured
enabledService: imaptls
enabledService: smtp
enabledService: smtpsecured
enabledService: smtptls
enabledService: managesieve
enabledService: managesievesecured
enabledService: sieve
enabledService: sievesecured
enabledService: deliver
enabledService: lda
enabledService: lmtp
enabledService: forward
enabledService: senderbcc
enabledService: recipientbcc
enabledService: shadowaddress
enabledService: displayedInGlobalAddressBook
enabledService: sogo
${LDIF_PUREFTPD_USER}
EOF
}
send_welcome_mail()
{
MAIL="$1"
echo "Send a welcome mail to new user: ${MAIL}"
echo "${WELCOME_MSG_BODY}" | mail -s "${WELCOME_MSG_SUBJECT}" ${MAIL}
}
usage()
{
echo "Usage:"
echo -e "\t$0 DOMAIN USERNAME"
echo -e "\t$0 DOMAIN USER1 USER2 USER3 ..."
}
if [ $# -lt 2 ]; then
usage
else
# Promopt to check settings.
[ X"${LDAP_SUFFIX}" == X"dc=example,dc=com" ] && echo "You should change 'LDAP_SUFFIX' in $0."
# Get domain name.
DOMAIN_NAME="$(echo $1 | tr '[A-Z]' '[a-z]')"
shift 1
add_new_domain ${DOMAIN_NAME}
for i in $@; do
USERNAME="$(echo $i | tr '[A-Z]' '[a-z]')"
MAIL="${USERNAME}@${DOMAIN_NAME}"
# Add new user in LDAP.
add_new_user ${USERNAME} ${MAIL}
# Send welcome msg to new user.
[ X"${SEND_WELCOME_MSG}" == X'YES' ] && send_welcome_mail ${MAIL}
done
fi
+126
View File
@@ -0,0 +1,126 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb _at_ iredmail.org)
# Purpose: Import users to SQL database from plain text file.
# Project: iRedMail (http://www.iredmail.org/)
# -------------------------------------------------------------------
# Usage:
# * Edit these variables:
# STORAGE_BASE_DIRECTORY
# DEFAULT_QUOTA='1024' # 1024 is 1GB
#
# * Run this script to generate SQL command used to create new user.
#
# bash create_mail_user_SQL.sh <new-email> <plain-password>
#
# For example:
#
# bash create_mail_user_SQL.sh user@domain.ltd plain_password
#
# It will print SQL commands on console directly, you can redirect the
# output to a file for further use like this:
#
# bash create_mail_user_SQL.sh user@domain.ltd plain_password > output.sql
#
# * Import output.sql into SQL database like below:
#
# # mysql -uroot -p
# mysql> USE vmail;
# mysql> SOURCE /path/to/output.sql;
#
# # psql -d vmail
# sql> \i /path/to/output.sql;
# --------- CHANGE THESE VALUES ----------
# Storage base directory used to store users' mail.
STORAGE_BASE_DIRECTORY="/var/vmail/vmail1"
###########
# Password
#
# Password scheme. Available schemes: BCRYPT, SSHA512, SSHA, PLAIN.
# Check file Available
PASSWORD_SCHEME='SSHA512'
# Default mail quota (in MB).
DEFAULT_QUOTA='1024'
#
# Maildir settings
#
# Maildir style: hashed, normal.
# Hashed maildir style, so that there won't be many large directories
# in your mail storage file system. Better performance in large scale
# deployment.
# Format: e.g. username@domain.td
# hashed -> domain.ltd/u/us/use/username/
# normal -> domain.ltd/username/
# Default hash level is 3.
MAILDIR_STYLE='hashed' # hashed, normal.
if [ X"$#" != X'2' ]; then
echo "Invalid command arguments. Usage:"
echo "bash create_mail_user_SQL.sh user@domain.ltd plain_password"
exit 255
fi
# Time stamp, will be appended in maildir.
DATE="$(date +%Y.%m.%d.%H.%M.%S)"
WC_L='wc -L'
if [ X"$(uname -s)" == X'OpenBSD' ]; then
WC_L='wc -l'
fi
STORAGE_BASE="$(dirname ${STORAGE_BASE_DIRECTORY})"
STORAGE_NODE="$(basename ${STORAGE_BASE_DIRECTORY})"
generate_password_hash()
{
# Usage: generate_password_hash <password-scheme> <plain-password>
_scheme="${1}"
_password="${2}"
if [ X"${_scheme}" == X'BCRYPT' ]; then
_scheme='BLF-CRYPT'
fi
doveadm pw -s "${_scheme}" -p "${_password}"
}
# Read input
mail="$1"
plain_password="$2"
username="$(echo $mail | awk -F'@' '{print $1}')"
domain="$(echo $mail | awk -F'@' '{print $2}')"
# Cyrpt default password.
export CRYPT_PASSWD="$(generate_password_hash ${PASSWORD_SCHEME} ${plain_password})"
# Different maildir style: hashed, normal.
if [ X"${MAILDIR_STYLE}" == X"hashed" ]; then
length="$(echo ${username} | ${WC_L} )"
str1="$(echo ${username} | cut -c1)"
str2="$(echo ${username} | cut -c2)"
str3="$(echo ${username} | cut -c3)"
test -z "${str2}" && str2="${str1}"
test -z "${str3}" && str3="${str2}"
# Use mbox, will be changed later.
maildir="${domain}/${str1}/${str2}/${str3}/${username}-${DATE}/"
else
maildir="${domain}/${username}-${DATE}/"
fi
cat <<EOF
INSERT INTO mailbox (username, password, name,
storagebasedirectory,storagenode, maildir,
quota, domain, active, passwordlastchange, created)
VALUES ('${mail}', '${CRYPT_PASSWD}', '${username}',
'${STORAGE_BASE}','${STORAGE_NODE}', '${maildir}',
'${DEFAULT_QUOTA}', '${domain}', '1', NOW(), NOW());
INSERT INTO forwardings (address, forwarding, domain, dest_domain, is_forwarding)
VALUES ('${mail}', '${mail}','${domain}', '${domain}', 1);
EOF
+144
View File
@@ -0,0 +1,144 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin <zhb@iredmail.org>
# Purpose: Enable Postfix postscreen.
# Last update: Dec 1, 2015
export KERNEL_NAME="$(uname -s | tr '[a-z]' '[A-Z]')"
export DATE="$(/bin/date +%Y.%m.%d.%H.%M.%S)"
export SYS_GROUP_ROOT='root'
export SYS_USER_POSTFIX='postfix'
export SYS_GROUP_POSTFIX='postfix'
export POSTFIX_ROOT_DIR='/etc/postfix'
export POSTFIX_DATA_DIRECTORY='/var/lib/postfix' # postconf data_directory
if [ X"${KERNEL_NAME}" == X'FREEBSD' ]; then
export SYS_GROUP_ROOT='wheel'
export POSTFIX_ROOT_DIR='/usr/local/etc/postfix'
export POSTFIX_DATA_DIRECTORY='/var/db/postfix'
elif [ X"${KERNEL_NAME}" == X'OPENBSD' ]; then
export SYS_GROUP_ROOT='wheel'
export SYS_USER_POSTFIX='_postfix'
export SYS_GROUP_POSTFIX='_postfix'
export POSTFIX_DATA_DIRECTORY='/var/postfix'
fi
# path to some config files
export MAIN_CF="${POSTFIX_ROOT_DIR}/main.cf"
export MASTER_CF="${POSTFIX_ROOT_DIR}/master.cf"
export POSTSCREEN_DNSBL_REPLY="${POSTFIX_ROOT_DIR}/postscreen_dnsbl_reply"
export POSTSCREEN_ACCESS_CIDR="${POSTFIX_ROOT_DIR}/postscreen_access.cidr"
# Get Postfix version number.
export POSTFIX_VERSION="$(postconf mail_version 2>/dev/null | awk '{print $NF}')"
# postscreen requires Postfix 2.8 or later.
if echo ${POSTFIX_VERSION} | grep '^2\.[01234567]\.' &>/dev/null; then
echo "<WARNING> postscreen requires Postfix 2.8 or later, you're running ${POSTFIX_VERSION}."
exit 255
fi
backup_file()
{
# Usage: backup_file file1 [file2 file3 ... fileN]
if [ X"$#" != X"0" ]; then
for f in $@; do
if [ -f ${f} ]; then
echo -e "* [BACKUP] ${f} -> ${f}.${DATE}."
cp -f ${f} ${f}.${DATE}
fi
done
fi
}
backup_file ${MAIN_CF} ${MASTER_CF} ${POSTSCREEN_ACCESS_CIDR} ${POSTSCREEN_DNSBL_REPLY}
echo "* Comment out 'smtp inet ... smtpd' service in ${MASTER_CF}."
perl -pi -e 's/^(smtp .*inet.*smtpd)$/#${1}/g' ${MASTER_CF}
echo "* Uncomment the new 'smtpd pass ... smtpd' service in ${MASTER_CF}."
perl -pi -e 's/^#(smtpd.*pass.*smtpd)$/${1}/g' ${MASTER_CF}
echo "* Uncomment the new "smtp inet ... postscreen" service in ${MASTER_CF}."
perl -pi -e 's/^#(smtp *.*inet.*postscreen)$/${1}/g' ${MASTER_CF}
echo "* Uncomment the new 'tlsproxy unix ... tlsproxy' service in ${MASTER_CF}."
perl -pi -e 's/^#(tlsproxy.*unix.*tlsproxy)$/${1}/g' ${MASTER_CF}
echo "* Uncomment the new 'dnsblog unix ... dnsblog' service in ${MASTER_CF}."
perl -pi -e 's/^#(dnsblog.*unix.*dnsblog)$/${1}/g' ${MASTER_CF}
echo "* Update ${MAIN_CF} to enable postscreen."
postconf -e postscreen_dnsbl_threshold=2
postconf -e postscreen_dnsbl_sites='zen.spamhaus.org=127.0.0.[2..11]*3 b.barracudacentral.org=127.0.0.2*2'
postconf -e postscreen_dnsbl_reply_map="texthash:${POSTSCREEN_DNSBL_REPLY}"
cat > ${POSTSCREEN_DNSBL_REPLY} <<EOF
# Secret DNSBL name Name in postscreen(8) replies
EOF
postconf -e postscreen_access_list="permit_mynetworks, cidr:${POSTSCREEN_ACCESS_CIDR}"
cat > ${POSTSCREEN_ACCESS_CIDR} <<EOF
# Rules are evaluated in the order as specified.
#1.2.3.4 permit
#2.3.4.5 reject
# Permit local clients
127.0.0.0/8 permit
EOF
postconf -e postscreen_greet_action='drop'
postconf -e postscreen_dnsbl_action='drop'
postconf -e postscreen_blacklist_action='drop'
# Require Postfix-2.11.
if echo ${POSTFIX_VERSION} | grep '^2\.[123456789][123456789]' &>/dev/null; then
postconf -e postscreen_dnsbl_whitelist_threshold='-2'
fi
# From Postfix author Wietse Venema, posted in Postfix mailing list on Jul 14, 2015:
# ----
# I would not enable the "after 220 greeting" protocol tests, because
# some senders that pass the tests will not retry (mail will never
# be delivered), and some will retry from a different client IP address
# (mail will be delayed). Whitelisting Google does not solve the
# problem because it also affects other senders.
#
# The amount of mail stopped by these tests is so small that it is not
# worth the trouble at this time.
# ----
#postscreen_pipelining_enable=yes
#postscreen_pipelining_action=
#
#postscreen_non_smtp_command_enable=yes
#postscreen_non_smtp_command_action=
#
#postscreen_bare_newline_enable=yes
#postscreen_bare_newline_action=
# Create directory inside chroot directory used to store file `postscreen_cache`.
# queue directory. Postfix will be chrooted to this directory.
queue_directory="$(postconf queue_directory | awk '{print $3}')"
# data directory. used to store additional files.
data_directory="$(postconf data_directory | awk '{print $3}')"
chrooted_data_directory="${queue_directory}/${data_directory}"
echo "* Create ${chrooted_data_directory}/postscreen_cache.db."
mkdir -p ${chrooted_data_directory}
chown ${SYS_USER_POSTFIX}:${SYS_GROUP_ROOT} ${chrooted_data_directory}
chmod 0700 ${chrooted_data_directory}
# Create db file.
cd ${chrooted_data_directory}
touch postscreen_cache
postmap btree:postscreen_cache
rm postscreen_cache
chown ${SYS_USER_POSTFIX}:${SYS_GROUP_POSTFIX} postscreen_cache.db
chmod 0700 postscreen_cache.db
echo "* Reloading postfix service to read the new configuration."
postfix reload
echo "* postscreen is now enabled."
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Purpose: Unban specified IP address(es) from all Fail2ban jails.
# Author: Zhang Huangbin
# Usage:
#
# bash fail2ban_unban_ip.sh 192.168.1.1 [192.168.2.2 ...]
if [ X"$#" == X'0' ]; then
cat <<EOF
Usage: specify the IP addresses you want to unban on command line. For example:
$ bash fail2ban_unban_ip.sh 173.254.22.21
Multiple IP addresses must be spearated by space.
$ bash fail2ban_unban_ip.sh 173.254.22.21 8.8.8.8 8.8.4.4
EOF
fi
# Get all IP addresses from command line.
IPS="$@"
# Path to command 'fail2ban-client'
F2B='fail2ban-client'
# Get all jails.
JAILS="$(${F2B} status | grep 'Jail list:' | awk -F'Jail list:' '{print $2}' | sed 's/,//g')"
for jail in ${JAILS}; do
for ip in ${IPS}; do
${F2B} set ${jail} unbanip ${ip} &>/dev/null
if [ X"$?" == X'0' ]; then
echo "Removed ${ip} from jail '${jail}'"
fi
done
done
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Purpose: Find login IP address of specified username which is used for smtp
# authentication.
MAIL_LOG="$1"
USER="$2"
if [ -z ${MAIL_LOG} -o -z ${USER} ]; then
echo "Please run script with a log file and email address: $0 /path/to/maillog mail_address"
exit 255
fi
tmpfile="/tmp/sasl_username_${RANDOM}"
# extract 'client=xxx[__IP__]' lines
grep "sasl_username=${USER}" ${MAIL_LOG} | awk '{print $7}' | sort | uniq -c | sort -n > sort > ${tmpfile}
cat ${tmpfile}
rm -f ${tmpfile}
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin <zhb@iredmail.org>
# Purpose: Find usernames used for smtp authentication in Postfix log file,
# sorted by login times.
MAILLOG_FILE="$1"
# Detect mail log file if not specified on command line.
if [ -z ${MAILLOG_FILE} ]; then
for f in /var/log/maillog /var/log/mail.log; do
if [ -f ${f} ]; then
MAILLOG_FILE="${f}"
break
fi
done
fi
if [ -z ${MAILLOG_FILE} ]; then
echo "Please specify Postfix log file on command line: $0 /path/to/maillog"
exit 255
fi
grep 'sasl_username=' ${MAILLOG_FILE} \
| awk -F'sasl_username=' '{print $2}' \
| awk '{print $NF}' \
| sort \
| uniq -c \
| sort -nr
+85
View File
@@ -0,0 +1,85 @@
#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb _at_ iredmail.org)
# Purpose: Create new SSL keys.
# Project: iRedMail (http://www.iredmail.org/)
# USAGE:
# 1) Edit variables which starts with TLS_ below, then save file.
# 2) Execute shell command:
#
# # bash generate_ssl_keys.sh
#
# It will create two new files under CURRENT directory:
#
# - certs/iRedMail.crt: Used to replace file on iRedMail server:
# + on RHEL/CentOS/Scientific Linux: /etc/pki/tls/certs/iRedMail.crt
# + on Debian/Ubuntu: /etc/ssl/certs/iRedMail.crt
# + on FreeBSD: /etc/ssl/certs/iRedMail.crt
# - private/iRedMail.key: Used to replace file on iRedMail server:
# + on RHEL/CentOS/Scientific Linux: /etc/pki/tls/private/iRedMail.key
# + on Debian/Ubuntu: /etc/ssl/private/iRedMail.key
# + on FreeBSD: /etc/ssl/private/iRedMail.key
#
# 3) Grant read access to all users. e.g. on RHEL/CentOS/Scientific Linux:
#
# # chmod +r /etc/ssl/certs/iRedMail.crt
# # chmod +r /etc/ssl/private/iRedMail.key
#
# If you need more restrict file permission, please use file system ACL instead.
# Refer to command 'setfacl' and 'getfacl' for more detail.
#
# 4) Restart all services which provides SSL secure connection. e.g. http,
# dovecot, postfix, etc. A system reboot should be easier if possible.
#
export HOSTNAME="$(hostname -f)"
# SSL key related settings.
# Country.
export TLS_COUNTRY='CN'
# State.
export TLS_STATE='GuangDong'
# City.
export TLS_CITY='ShenZhen'
# Company name here, e.g. Apple Inc.
export TLS_COMPANY="${HOSTNAME}"
# Department name.
export TLS_DEPARTMENT='IT'
# Hostname of your mail server.
export TLS_HOSTNAME="${HOSTNAME}"
# Server admininistrator's email address.
export TLS_ADMIN="root@${HOSTNAME}"
# Do not edit below lines.
export ROOTDIR="$(pwd)"
export SSL_CERT_FILE="${ROOTDIR}/certs/iRedMail.crt"
export SSL_KEY_FILE="${ROOTDIR}/private/iRedMail.key"
# Create SSL certs/private files.
generate_ssl_keys()
{
# Create necessary directories.
mkdir -p {certs,private} 2>/dev/null
openssl req \
-x509 -nodes -days 3650 -newkey rsa:2048 \
-subj "/C=${TLS_COUNTRY}/ST=${TLS_STATE}/L=${TLS_CITY}/O=${TLS_COMPANY}/OU=${TLS_DEPARTMENT}/CN=${TLS_HOSTNAME}/emailAddress=${TLS_ADMIN}/" \
-out ${SSL_CERT_FILE} -keyout ${SSL_KEY_FILE} >/dev/null 2>&1
# Set correct file permission.
chmod 0444 ${SSL_CERT_FILE}
chmod 0444 ${SSL_KEY_FILE}
}
generate_ssl_keys && cat <<EOF
SSL keys were generated:
- ${SSL_CERT_FILE}
- ${SSL_KEY_FILE}
EOF
@@ -0,0 +1,151 @@
#!/usr/bin/env python3
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Assign one user to specified groups.
# USAGE
#
# * Set correct LDAP server address (LDAP_URI), LDAP suffix (LDAP_SUFFIX),
# bind dn (BINDDN) and password (BINDPW).
#
# * Run this script:
#
# $ python3 ldap_assign_user_to_groups.py [options] user@domain.com group@domain.com [group2@domain.com ...]
# LDAP server address.
LDAP_URI = 'ldap://127.0.0.1:389'
LDAP_SUFFIX = 'dc=example,dc=com'
# LDAP base dn, bind dn/password.
BASEDN = 'o=domains,%s' % LDAP_SUFFIX
BINDDN = 'cn=Manager,%s' % LDAP_SUFFIX
BINDPW = 'www'
import sys
import ldap
def usage():
print("""Usage:
$ python3 ldap_assign_user_to_groups.py [options] user@domain.com new_group@domain.com [new_group_2@domain.com ...]
Note: Non-existing group will be ignored.
Optional arguments:
--remove Remove memberships. If no mailing list specified, it will remove
ALL assigned mailing lists.
Samples:
*) Remove all memberships:
python3 ldap_assign_user_to_groups.py --remove user@domain.com
*) Remove memberships of specified mailing lists:
python3 ldap_assign_user_to_groups.py --remove user@domain.com group1@domain.com group2@domain.com
*) Assign user to new mailing lists:
python3 ldap_assign_user_to_groups.py user@domain.com group1@domain.com group2@domain.com
""")
if len(sys.argv) < 3:
usage()
sys.exit()
args = sys.argv
args.pop(0)
is_remove = False
if '--remove' in args:
args.remove('--remove')
is_remove = True
user = args.pop(0)
groups = args
if not groups and not is_remove:
print("<<< ERROR >>> No group specified.")
usage()
sys.exit()
print("* Connecting to LDAP server: %s" % LDAP_URI)
conn = ldap.initialize(uri=LDAP_URI, trace_level=0)
conn.bind_s(BINDDN, BINDPW)
if not is_remove:
print("* Querying existing mailing lists.")
qr_filter = '(&(objectClass=mailList)(|'
for g in groups:
qr_filter += '(mail=%s)(shadowAddress=%s)' % (g, g)
qr_filter += '))'
print("* Query filter:", qr_filter)
qr = conn.search_s(BASEDN,
ldap.SCOPE_SUBTREE,
qr_filter,
['mail'])
if qr:
groups = []
for (dn, entry) in qr:
groups += entry['mail']
print("* Found %d mailing list(s)." % len(groups))
else:
sys.exit("* No specified groups found in LDAP.")
print("* Get user's dn and membership of mailing list(s).")
(user_name, domain) = user.split('@', 1)
user_dn = 'mail=%s,ou=Users,domainName=%s,%s' % (user, domain, BASEDN)
try:
qr = conn.search_s(user_dn,
ldap.SCOPE_BASE,
'(mail=%s)' % user,
['mail', 'memberOfGroup'])
except ldap.NO_SUCH_OBJECT:
print("* <<< ERROR >>> User doesn't exist: %s" % user)
sys.exit()
(dn, entry) = qr[0]
# Get existing, assigned mailing lists.
existing_groups = entry.get('memberOfGroup', [])
if existing_groups:
print("* Existing membership: %s" % ', '.join(existing_groups))
else:
print("* User is not member of any mailing list.")
# New groups
if is_remove:
if groups:
print("* Remove membership: %s" % ', '.join(groups))
new_groups = list(set(existing_groups) - set(groups))
else:
print("* Remove ALL memberships.")
new_groups = []
# Set to None to remove attribute `memberOfGroup`
if not new_groups:
print("* No membership left, remove attribute `memberOfGroup`.")
new_groups = None
else:
new_groups = list(set(existing_groups + groups))
print("* New membership: %s" % ', '.join(new_groups))
mod_attrs = [(ldap.MOD_REPLACE, 'memberOfGroup', new_groups)]
try:
print("* Updating membership.")
conn.modify_s(dn, mod_attrs)
print("* Updated.")
# Unbind connection.
print("* Unbind LDAP server.")
conn.unbind()
except Exception as e:
print("<<< ERROR >>> %s" % repr(e))
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Move or copy ALL members of specified mailing list to another
# mailing list.
# USAGE
#
# * Set correct LDAP server address (LDAP_URI), LDAP suffix (LDAP_SUFFIX),
# bind dn (BINDDN) and password (BINDPW).
#
# * Run this script:
#
# $ python3 ldap_move_members_to_another_group.py [options] old_group@domain.com new_group@domain.com [new_group2@domain.com ...]
# LDAP server address.
LDAP_URI = 'ldap://127.0.0.1:389'
LDAP_SUFFIX = 'dc=example,dc=com'
# LDAP base dn, bind dn/password.
BASEDN = 'o=domains,%s' % LDAP_SUFFIX
BINDDN = 'cn=Manager,%s' % LDAP_SUFFIX
BINDPW = 'www'
import sys
import ldap
def usage():
print("""Usage:
$ python3 ldap_move_members_to_another_group.py [options] old_group@domain.com [new_group@domain.com new_group_2@domain.com ...]
Available optional options:
--copy Assign members of old group to new groups without removing
membership of old group. That means user will be member of both
old and new groups.
If no --copy specified, this script will remove membership of old
group.
Samples:
*) Copy all members of old_group@domain.com to new_group@domain.com
python3 ldap_move_members_to_another_group --copy old_group@domain.com new_group@domain.com
*) Copy all members of old_group@domain.com to new_group@domain.com, and remove
ALL members of old_group@domain.com.
python3 ldap_move_members_to_another_group old_group@domain.com new_group@domain.com
*) Remove all members (just remove membership, not remove mail accounts) of
old_group@domain.com.
python3 ldap_move_members_to_another_group old_group@domain.com
""")
if len(sys.argv) < 3:
usage()
sys.exit()
args = sys.argv
args.pop(0)
is_copy = False
if '--copy' in args:
is_copy = True
args.remove('--copy')
old_group = args.pop(0)
new_groups = args
# Initialize LDAP connection.
print("* Connecting to LDAP server: %s" % LDAP_URI)
conn = ldap.initialize(uri=LDAP_URI, trace_level=0)
conn.bind_s(BINDDN, BINDPW)
# Get all members of old mailing list.
print("* Get all members of old mailing list: %s" % old_group)
qr_filter = '(&(objectClass=mailUser)(memberOfGroup=%s))' % old_group
print("* Query filter:", qr_filter)
qr = conn.search_s(BASEDN,
ldap.SCOPE_SUBTREE,
qr_filter,
['memberOfGroup'])
total = len(qr)
if total:
print("* Old mailing list has %d member(s)." % total)
else:
sys.exit("* Old mailing list doesn't have any member. Exit.")
# accumulate counter
count = 1
for user in qr:
(dn, entry) = user
# Get all assigned mailing lists.
groups = entry['memberOfGroup']
if not is_copy:
# Remove old mailing list
groups.remove(old_group)
# Assign to new mailing list
if new_groups:
groups = list(set(groups + new_groups))
# Use only value of rdn.
mod_attrs = [(ldap.MOD_REPLACE, 'memberOfGroup', groups)]
try:
print("* (%d of %d) Updating object: %s" % (count, total, dn))
conn.modify_s(dn, mod_attrs)
except Exception as e:
print("<<< ERROR >>> %s" % repr(e))
count += 1
# Unbind connection.
print("* Unbind LDAP server.")
conn.unbind()
print("* Done.")
+145
View File
@@ -0,0 +1,145 @@
"""
This file is used to migrate SQL table `vmail.alias` for SQL backends.
Requirements:
* Make sure you have new SQL tables ready: alias_moderators, forwardings.
* For PostgreSQL, please make sure SQL user 'vmail' has 'SELECT' privilege on
newly created sql tables.
"""
import sys
import web
web.config.debug = False
# Read iRedAdmin config file `settings.py` from one of expected paths
sys.path.insert(0, '/usr/local/www/iredadmin') # FreeBSD
sys.path.insert(0, '/opt/www/iredadmin') # Debian/Ubuntu
sys.path.insert(0, '/var/www/iredadmin') # CentOS/OpenBSD
sys.path.insert(0, '/usr/share/apache2/iredadmin') # Debian/Ubuntu (old iRedMail releases)
import settings
print "* Read SQL username/password from iRedAdmin config file:", settings.__file__
print "* Backend:", settings.backend
if settings.backend in ['ldap', 'mysql']:
sql_dbn = 'mysql'
elif settings.backend in ['pgsql']:
sql_dbn = 'postgres'
else:
sys.exit('* << ERROR >>: Unsupported backend (%s).' % settings.backend)
try:
if sql_dbn == 'postgres':
db = web.database(dbn=sql_dbn,
host=settings.vmail_db_host,
port=int(settings.vmail_db_port),
db=settings.vmail_db_name,
user=settings.vmail_db_user,
pw=settings.vmail_db_password)
else:
db = web.database(dbn=sql_dbn,
host=settings.vmail_db_host,
port=int(settings.vmail_db_port),
db=settings.vmail_db_name,
user=settings.vmail_db_user,
pw=settings.vmail_db_password,
charset='utf8',
use_unicode=False)
db.supports_multiple_insert = True
except Exception as e:
print "<< ERROR >> Cannot connecting to SQL server:", e
sys.exit()
# Check required tables
for tbl in ['forwardings', 'alias_moderators']:
try:
db.select(tbl, limit=1)
except Exception as e:
print "<<< ERROR >>> SQL table '%s' doesn't exist. Please create it first." % tbl
sys.exit()
# Get all existing accounts
records = db.select('alias', what='address,goto,moderators,domain,active,islist,is_alias')
total = len(records)
counter = 1
for r in records:
# standard mail alias account
is_list = 0
# per-account alias address
is_alias = 0
# forwarding addresses of mail user
is_forwarding = 0
if r.islist == 1:
is_list = 1
_type = 'mail alias'
elif r.is_alias == 1:
is_alias = 1
_type = 'per-account alias address'
else:
is_forwarding = 1
_type = 'mail user'
account = str(r.address).lower()
active = int(r.active)
domain = str(r.domain).lower()
goto = r.goto.strip(' ')
goto = goto.replace(' ', '')
goto = goto.replace(';', ',')
members = list(set([i.strip().lower() for i in goto.strip(' ').split(',')]))
# Migrating forwardings
print "* [%d/%d] Migrating %s %s" % (counter, total, _type, account)
for m in members:
if m:
try:
dest_domain = m.split('@', 1)[-1]
db.insert('forwardings',
address=account,
forwarding=m,
domain=domain,
dest_domain=dest_domain,
active=active,
is_list=is_list,
is_alias=is_alias,
is_forwarding=is_forwarding)
except Exception as e:
if e[0] == 1062 or 'duplicate' in repr(e):
# Duplicate record
pass
else:
print "Error while migrating %s %s: %s" % (_type, m, repr(e))
# Migrating moderators of mail alias account
moderators = []
if is_list == 1:
if r.moderators:
moderators = [i.lower() for i in set(r.moderators.strip(' ').split(','))]
if moderators:
for m in moderators:
try:
dest_domain = m.split('@', 1)[-1]
db.insert('alias_moderators',
address=account,
moderator=m,
domain=domain,
dest_domain=dest_domain)
except Exception as e:
if e[0] == 1062 or 'duplicate' in repr(e):
# Duplicate record
pass
else:
print "Error while migrating moderators of alias account %s: %s" % (m, repr(e))
counter += 1
print "* DONE."