-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathadmin.sh
233 lines (192 loc) · 6.72 KB
/
admin.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/sh
source ./options.conf
function add_mail_user {
check_user_exist
if [ $? = 0 ]; then
echo "Error: Mail user already exists, stopping operation."
echo "Check that user is not in /etc/postfix/virtual_mailbox_users."
echo "Then remove /home/vmail/$domain/$user directory if it exists."
exit 1
fi
echo "Adding user $user@$domain to /etc/dovecot/passwd. Please enter a password when prompted."
# First check if passwd file exists. Create one if missing.
if [ ! -x /etc/dovecot/passwd ]; then
touch /etc/dovecot/passwd
chmod 640 /etc/dovecot/passwd
chown dovecot:dovecot /etc/dovecot/passwd
fi
# Use doveadm pw tool to generate cram-md5 password
# From options.conf, Debian = 1, Ubuntu = 2
if [ $DISTRO -eq 1 ]; then
user_password=`dovecotpw`
else
user_password=`doveadm pw`
fi
echo "$user@$domain:$user_password" >> /etc/dovecot/passwd
# Create the needed Maildir directories
echo "Creating user directory /home/vmail/$domain/$user"
if [ ! -x /home/vmail/$domain ]; then
mkdir -p /home/vmail/$domain
chown 5000:5000 /home/vmail/$domain
chmod 700 /home/vmail/$domain
fi
mkdir -p /home/vmail/$domain/$user/Maildir/{cur,new,tmp}
mkdir -p /home/vmail/$domain/$user/Maildir/.Drafts/{cur,new,tmp}
mkdir -p /home/vmail/$domain/$user/Maildir/.Sent/{cur,new,tmp}
mkdir -p /home/vmail/$domain/$user/Maildir/.Junk/{cur,new,tmp}
mkdir -p /home/vmail/$domain/$user/Maildir/.Trash/{cur,new,tmp}
chmod -R 0700 /home/vmail/$domain/$user
chown -R vmail:vmail /home/vmail/$domain/$user
# Add user to Postfix virtual map file and reload Postfix
echo "Adding user to /etc/postfix/virtual_mailbox_users"
echo $mail_user $domain/$user/Maildir/ >> /etc/postfix/virtual_mailbox_users
postmap /etc/postfix/virtual_mailbox_users
exit 0
}
function remove_mail_user {
check_user_exist
if [ $? = 1 ]; then
echo "Error: Mail user does not exist, stopping operation."
echo "Check that user is in /etc/postfix/virtual_mailbox_users."
echo "Then check /home/vmail/$domain/$user directory if it exists."
exit 1
fi
echo "Removing user from /etc/postfix/virtual_mailbox_users"
sed -i '/\<'${mail_user}'\>/ d' /etc/postfix/virtual_mailbox_users
postmap /etc/postfix/virtual_mailbox_users
echo "Removing user from /etc/dovecot/passwd"
sed -i '/\<'${mail_user}'\>/ d' /etc/dovecot/passwd
# Remove the Maildir directories
echo "Removing user directory /home/vmail/$domain/$user"
rm -rf /home/vmail/$domain/$user
exit 0
}
function check_user_exist {
# First time running this script, file not created yet
if [ ! -e /etc/postfix/virtual_mailbox_users ]; then
return 1
fi
count=`grep $mail_user /etc/postfix/virtual_mailbox_users | wc -l`
if [ $count -gt 0 ] || [ -d /home/vmail/$domain/$user ]; then
# If user already exists, return 0
return 0
else
return 1
fi
}
function add_virtual_domain {
test_domain=`grep virtual_mailbox_domains /etc/postfix/main.cf | grep -w $domain | wc -l`
if [ $test_domain -eq 0 ]; then
# If domain is not in config, line count will be equals to 0
# Get current virtual_mailbox_domains config and append new domain to it
vdomains=`grep virtual_mailbox_domains /etc/postfix/main.cf`
postconf -e "$vdomains $domain"
echo "Domain added successfully."
else
echo "Domain already exists. Nothing added to postfix configuration file."
fi
}
function remove_virtual_domain {
test_domain=`grep virtual_mailbox_domains /etc/postfix/main.cf | grep -w $domain | wc -l`
if [ $test_domain -gt 0 ]; then
# If domain is in config, line count will be greater than 0
# Sed removes exact match of domain only. Using \< and \> boundary
vdomains=`grep virtual_mailbox_domains /etc/postfix/main.cf | sed 's/\<'${domain}'\s*\>//'`
postconf -e "$vdomains"
echo "Domain removed successfully."
else
echo "Domain does not exist. Please enter a valid domain to remove."
fi
}
function change_virtual_user_password {
check_user_exist
if [ $? = 1 ]; then
echo "Error: Mail user does not exist, stopping operation."
echo "Check that user is in /etc/postfix/virtual_mailbox_users."
echo "Then check /home/vmail/$domain/$user directory if it exists."
exit 1
fi
# Remove user from /etc/dovecot/passwd before adding again
sed -i '/\<'${mail_user}'\>/ d' /etc/dovecot/passwd
# Use doveadm pw tool to generate cram-md5 password
user_password=`doveadm pw`
echo "$user@$domain:$user_password" >> /etc/dovecot/passwd
}
# Start main program
if [ ! -n "$1" ]; then
echo ""
echo -e "\033[35;1mUse this script to add/remove mail users/domains.\033[0m"
echo -e "\033[35;1mAdd your domains first before adding users.\033[0m"
echo ""
echo -n "$0"
echo -ne "\033[36m ad domain\033[0m"
echo " - Add new domain to mail server."
echo -n "$0"
echo -ne "\033[36m rd domain\033[0m"
echo " - Remove existing domain from mail server."
echo -n "$0"
echo -ne "\033[36m au username@domain\033[0m"
echo " - Add a new user to mail server."
echo -n "$0"
echo -ne "\033[36m ru username@domain\033[0m"
echo " - Remove existing user from mail server."
echo -n "$0"
echo -ne "\033[36m cpw username@domain\033[0m"
echo " - Change password of existing user."
echo ""
exit
fi
case $1 in
au)
if [ ! $# = 2 ]; then
echo "Please enter a new username@domain."
exit 1
else
user=`echo "$2" | cut -f1 -d "@"`
domain=`echo "$2" | cut -s -f2 -d "@"`
mail_user=$2
fi
add_mail_user
echo -e "\033[35;1m Varnish now installed and configured with a ${VARNISH_CACHE_SIZE} cache size. \033[0m"
;;
ad)
if [ ! $# = 2 ]; then
echo "Please enter a new domain name."
exit 1
else
domain=$2
fi
add_virtual_domain
;;
cpw)
if [ ! $# = 2 ]; then
echo "Please enter an existing username@domain."
exit 1
else
user=`echo "$2" | cut -f1 -d "@"`
domain=`echo "$2" | cut -s -f2 -d "@"`
mail_user=$2
fi
change_virtual_user_password
;;
rd)
if [ ! $# = 2 ]; then
echo "Please enter an existing domain."
exit 1
else
domain=$2
fi
remove_virtual_domain
;;
ru)
if [ ! $# = 2 ]; then
echo "Please enter an existing username@domain."
exit 1
else
user=`echo "$2" | cut -f1 -d "@"`
domain=`echo "$2" | cut -s -f2 -d "@"`
mail_user=$2
fi
remove_mail_user
;;
esac