从iRedMail 创建用户脚本学习PostgreSQL数据库
原创
©著作权归作者所有:来自51CTO博客作者mb62de8abf75c00的原创作品,请联系作者获取转载授权,否则将追究法律责任
tools文件夹下面create_mail_user_SQL.sh用于创建新邮件用户,输入用户邮件名和密码。它将打印用于创建此新用户的SQL命令,您可以将其保存到文件中,然后以root用户身份登录SQL server并导入此文件。
cd iRedMail-0.9.6/tools/
bash create_mail_user_SQL.sh user1@example.com 'plain_password'
cd iRedMail-0.9.6/tools/
bash create_mail_user_SQL.sh user1@example.com 'plain_password' > /tmp/user.sql
# su - postgres
$ psql -d vmail
sql> \i output.sql;
Password scheme is defined in variable PASSWORD_SCHEME, default is SSHA512. BCRYPT is recommended on FreeBSD and OpenBSD. 建议在FreeBSD和OpenBSD上使用BCRYPT。
Per-user mailbox quota is defined in variable DEFAULT_QUOTA, default is 1024 (MB). Use 0 for unlimited quota. 每个用户邮箱配额在变量DEFAULT_quota中定义,默认值为1024(MB)。使用0表示无限配额。
Maildir path is hashed like domain.ltd/u/s/e/username-20150929. If you prefer domain.ltd/username/, please set MAILDIR_STYLE=‘normal’. Maildir路径像域一样被散列。有限公司/u/s/e/username-20150929。如果你喜欢域名。有限公司/username/,请设置MAILDIR_STYLE=‘normal’。
Mailbox storage path is defined in variable STORAGE_BASE_DIRECTORY, default is /var/vmail/vmail1. 邮箱存储路径在变量STORAGE_BASE_DIRECTORY中定义,默认为/var/vmail/vmail1。
看一下创建用户的sql
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);
用户密码如何创建,使用doveadm工具加密用户密码,可以选加密算法为BLF-CRYPT、SSHA512、SSHA、PLAIN。
export CRYPT_PASSWD="$(generate_password_hash ${PASSWORD_SCHEME} ${plain_password})"
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}"
}
写脚本批量创建用户
#!/usr/bin/env python
import csv
import commands,os
commands.getstatusoutput("rm -rf create_user_batch.sql")
with open('usermail.csv') as f:
f_csv = csv.reader(f)
for row in f_csv:
create_user_cmd = "bash create_mail_user_SQL.sh "+row[0]+" "+row[1]+" >> create_user_batch.sql"
commands.getstatusoutput(create_user_cmd)
importstr = "su - postgres -c 'psql -d vmail -f "+os.getcwd()+"/create_user_batch.sql'"
print importstr
(rc, op) = commands.getstatusoutput(importstr)
print op