-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_mail_user_SQL.sh
executable file
·109 lines (93 loc) · 3.36 KB
/
create_mail_user_SQL.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/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 [email protected] 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 [email protected] 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, MD5, NTLM, 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. [email protected]
# 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 [email protected] 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})"
# 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="$(python ./generate_password_hash.py ${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