iRedMail-1.3.1/tools/create_mail_user_SQL.sh

-4 Removals
+6 Additions
#!/usr/bin/env bash#!/usr/bin/env bash
# Author: Zhang Huangbin (zhb _at_ iredmail.org)# Author: Zhang Huangbin (zhb _at_ iredmail.org)
# Purpose: Import users to SQL database from plain text file.# Purpose: Import users to SQL database from plain text file.
# Project: iRedMail (http://www.iredmail.org/)# Project: iRedMail (http://www.iredmail.org/)
# -------------------------------------------------------------------# -------------------------------------------------------------------
# Usage:# Usage:
# * Edit these variables:# * Edit these variables:
# STORAGE_BASE_DIRECTORY# STORAGE_BASE_DIRECTORY
# DEFAULT_QUOTA='1024' # 1024 is 1GB# DEFAULT_QUOTA='1024' # 1024 is 1GB
##
# * Run this script to generate SQL command used to create new user.# * Run this script to generate SQL command used to create new user.
##
# bash create_mail_user_SQL.sh <new-email> <plain-password># bash create_mail_user_SQL.sh <new-email> <plain-password>
##
# For example:# For example:
##
# bash create_mail_user_SQL.sh [email protected] plain_password# bash create_mail_user_SQL.sh [email protected] plain_password mail_quota
##
# It will print SQL commands on console directly, you can redirect the# It will print SQL commands on console directly, you can redirect the
# output to a file for further use like this:# output to a file for further use like this:
##
# bash create_mail_user_SQL.sh [email protected] plain_password > output.sql# bash create_mail_user_SQL.sh [email protected] plain_password mail_quota > output.sql
##
# * Import output.sql into SQL database like below:# * Import output.sql into SQL database like below:
##
# # mysql -uroot -p# # mysql -uroot -p
# mysql> USE vmail;# mysql> USE vmail;
# mysql> SOURCE /path/to/output.sql;# mysql> SOURCE /path/to/output.sql;
##
# # psql -d vmail# # psql -d vmail
# sql> \i /path/to/output.sql;# sql> \i /path/to/output.sql;
# --------- CHANGE THESE VALUES ----------# --------- CHANGE THESE VALUES ----------
# Storage base directory used to store users' mail.# Storage base directory used to store users' mail.
STORAGE_BASE_DIRECTORY="/var/vmail/vmail1"STORAGE_BASE_DIRECTORY="/var/vmail/vmail1"
######################
# Password# Password
##
# Password scheme. Available schemes: BCRYPT, SSHA512, SSHA, PLAIN.# Password scheme. Available schemes: BCRYPT, SSHA512, SSHA, PLAIN.
# Check file Available# Check file Available
PASSWORD_SCHEME='SSHA512'PASSWORD_SCHEME='SSHA512'
# Default mail quota (in MB).# Default mail quota (in MB).
DEFAULT_QUOTA='1024'DEFAULT_QUOTA=$3
##
# Maildir settings# Maildir settings
##
# Maildir style: hashed, normal.# Maildir style: hashed, normal.
# Hashed maildir style, so that there won't be many large directories# Hashed maildir style, so that there won't be many large directories
# in your mail storage file system. Better performance in large scale# in your mail storage file system. Better performance in large scale
# deployment.# deployment.
# Format: e.g. [email protected]# Format: e.g. [email protected]
# hashed -> domain.ltd/u/us/use/username/# hashed -> domain.ltd/u/us/use/username/
# normal -> domain.ltd/username/# normal -> domain.ltd/username/
# Default hash level is 3.# Default hash level is 3.
MAILDIR_STYLE='hashed' # hashed, normal.MAILDIR_STYLE='hashed' # hashed, normal.
if [ X"$#" != X'2' ]; thenif [ X"$#" != X'3' ]; then
echo "Invalid command arguments. Usage:" echo "Invalid command arguments. Usage:"
echo "bash create_mail_user_SQL.sh [email protected] plain_password" echo "bash create_mail_user_SQL.sh [email protected] plain_password mail_quota"
exit 255 exit 255
fifi
# Time stamp, will be appended in maildir.# Time stamp, will be appended in maildir.
DATE="$(date +%Y.%m.%d.%H.%M.%S)"DATE="$(date +%Y.%m.%d.%H.%M.%S)"
WC_L='wc -L'WC_L='wc -L'
if [ X"$(uname -s)" == X'OpenBSD' ]; thenif [ X"$(uname -s)" == X'OpenBSD' ]; then
WC_L='wc -l' WC_L='wc -l'
fifi
STORAGE_BASE="$(dirname ${STORAGE_BASE_DIRECTORY})"STORAGE_BASE="$(dirname ${STORAGE_BASE_DIRECTORY})"
STORAGE_NODE="$(basename ${STORAGE_BASE_DIRECTORY})"STORAGE_NODE="$(basename ${STORAGE_BASE_DIRECTORY})"
generate_password_hash()generate_password_hash()
{{
# Usage: generate_password_hash <password-scheme> <plain-password> # Usage: generate_password_hash <password-scheme> <plain-password>
_scheme="${1}" _scheme="${1}"
_password="${2}" _password="${2}"
if [ X"${_scheme}" == X'BCRYPT' ]; then if [ X"${_scheme}" == X'BCRYPT' ]; then
_scheme='BLF-CRYPT' _scheme='BLF-CRYPT'
fi fi
doveadm pw -s "${_scheme}" -p "${_password}" doveadm pw -s "${_scheme}" -p "${_password}"
}}
# Read input# Read input
mail="$1"mail="$1"
plain_password="$2"plain_password="$2"
username="$(echo $mail | awk -F'@' '{print $1}')"username="$(echo $mail | awk -F'@' '{print $1}')"
domain="$(echo $mail | awk -F'@' '{print $2}')"domain="$(echo $mail | awk -F'@' '{print $2}')"
# Cyrpt default password.# Cyrpt default password.
export CRYPT_PASSWD="$(generate_password_hash ${PASSWORD_SCHEME} ${plain_password})"export CRYPT_PASSWD="$(generate_password_hash ${PASSWORD_SCHEME} ${plain_password})"
# Different maildir style: hashed, normal.# Different maildir style: hashed, normal.
if [ X"${MAILDIR_STYLE}" == X"hashed" ]; thenif [ X"${MAILDIR_STYLE}" == X"hashed" ]; then
length="$(echo ${username} | ${WC_L} )" length="$(echo ${username} | ${WC_L} )"
str1="$(echo ${username} | cut -c1)" str1="$(echo ${username} | cut -c1)"
str2="$(echo ${username} | cut -c2)" str2="$(echo ${username} | cut -c2)"
str3="$(echo ${username} | cut -c3)" str3="$(echo ${username} | cut -c3)"
test -z "${str2}" && str2="${str1}" test -z "${str2}" && str2="${str1}"
test -z "${str3}" && str3="${str2}" test -z "${str3}" && str3="${str2}"
# Use mbox, will be changed later. # Use mbox, will be changed later.
maildir="${domain}/${str1}/${str2}/${str3}/${username}-${DATE}/" maildir="${domain}/${str1}/${str2}/${str3}/${username}-${DATE}/"
elseelse
maildir="${domain}/${username}-${DATE}/" maildir="${domain}/${username}-${DATE}/"
fifi
cat <<EOFcat <<EOF
INSERT INTO mailbox (username, password, name,INSERT INTO mailbox (username, password, name,
storagebasedirectory,storagenode, maildir, storagebasedirectory,storagenode, maildir,
quota, domain, active, passwordlastchange, created) quota, domain, active, passwordlastchange, created)
VALUES ('${mail}', '${CRYPT_PASSWD}', '${username}', VALUES ('${mail}', '${CRYPT_PASSWD}', "$(echo ${username} | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}')",
'${STORAGE_BASE}','${STORAGE_NODE}', '${maildir}', '${STORAGE_BASE}','${STORAGE_NODE}', '${maildir}',
'${DEFAULT_QUOTA}', '${domain}', '1', NOW(), NOW()); '${DEFAULT_QUOTA}', '${domain}', '1', NOW(), NOW());
INSERT INTO forwardings (address, forwarding, domain, dest_domain, is_forwarding)INSERT INTO forwardings (address, forwarding, domain, dest_domain, is_forwarding)
VALUES ('${mail}', '${mail}','${domain}', '${domain}', 1); VALUES ('${mail}', '${mail}','${domain}', '${domain}', 1);
EOFEOF
Editor
Clear
Original Text
Changed Text