Skip to content

Commit 0bc2974

Browse files
author
Kristan Kenney
committed
Improvements to v-change-domain-owner
* Ensure mail SSL configuration is moved * Cleanup mail domain assets after move
1 parent a53d261 commit 0bc2974

File tree

1 file changed

+75
-7
lines changed

1 file changed

+75
-7
lines changed

bin/v-change-domain-owner

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ if [ -z "$owner" ]; then
3232
check_result $E_NOTEXIST "domain $domain doesn't exist"
3333
fi
3434
if [ "$owner" = "$user" ]; then
35+
echo "ERROR: $domain is already owned by $user."
3536
exit
3637
fi
3738

@@ -43,9 +44,12 @@ check_hestia_demo_mode
4344
# Action #
4445
#----------------------------------------------------------#
4546

47+
echo "Moving $domain from $owner to $user, please wait..."
48+
4649
# WEB domain
4750
web_data=$(grep "DOMAIN='$domain'" $HESTIA/data/users/$owner/web.conf)
4851
if [ ! -z "$web_data" ]; then
52+
echo "[*] Moving web domain..."
4953
$BIN/v-suspend-web-domain $owner $domain >> /dev/null 2>&1
5054
parse_object_kv_list "$web_data"
5155

@@ -95,6 +99,7 @@ fi
9599
# DNS domain
96100
dns_data=$(grep "DOMAIN='$domain'" $HESTIA/data/users/$owner/dns.conf)
97101
if [ ! -z "$dns_data" ]; then
102+
echo "[*] Moving DNS zone and records..."
98103
parse_object_kv_list "$dns_data"
99104

100105
# Change IP
@@ -125,9 +130,15 @@ fi
125130
# MAIL domain
126131
mail_data=$(grep "DOMAIN='$domain'" $HESTIA/data/users/$owner/mail.conf)
127132
if [ ! -z "$mail_data" ]; then
133+
echo "[*] Moving mail domain and accounts..."
128134

129135
parse_object_kv_list "$mail_data"
130136

137+
# Ensure mail configuration directory exists for receiving user
138+
if [ ! -e "$HOMEDIR/$user/conf/mail/$domain/" ]; then
139+
mkdir -p $HOMEDIR/$user/conf/mail/$domain/
140+
fi
141+
131142
# Move config
132143
sed -i "/DOMAIN='$domain'/d" $HESTIA/data/users/$owner/mail.conf
133144
echo "$mail_data" >> $HESTIA/data/users/$user/mail.conf
@@ -142,17 +153,54 @@ if [ ! -z "$mail_data" ]; then
142153
$HESTIA/data/users/$user/mail/
143154
fi
144155

145-
# Check SSL
156+
# Move SSL certificates
146157
if [ "$SSL" = 'yes' ]; then
158+
# Ensure that SSL directory exists and move certificates
147159
mkdir -p $HESTIA/data/users/$user/ssl/
160+
mkdir -p $HOMEDIR/$user/conf/mail/$domain/ssl/
161+
148162
ssl_crt=$HESTIA/data/users/$owner/ssl/mail.$domain.crt
149163
ssl_key=$HESTIA/data/users/$owner/ssl/mail.$domain.key
150164
ssl_ca=$HESTIA/data/users/$owner/ssl/mail.$domain.ca
151165
ssl_pem=$HESTIA/data/users/$owner/ssl/mail.$domain.pem
152166
mv $ssl_crt $HESTIA/data/users/$user/ssl/
153167
mv $ssl_key $HESTIA/data/users/$user/ssl/
154168
mv $ssl_ca $HESTIA/data/users/$user/ssl/ >> /dev/null 2>&1
155-
mv $ssl_pem $HESTIA/data/users/$user/ssl/ >> /dev/null 2>&1
169+
mv $ssl_pem $HESTIA/data/users/$user/ssl/ >> /dev/null 2>&1
170+
171+
# Add certificate to user home directory
172+
cp -f $HESTIA/data/users/$user/ssl/mail.$domain.crt $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt
173+
cp -f $HESTIA/data/users/$user/ssl/mail.$domain.key $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key
174+
cp -f $HESTIA/data/users/$user/ssl/mail.$domain.pem $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem
175+
if [ -e "$HESTIA/data/users/$user/ssl/mail.$domain.ca" ]; then
176+
cp -f $HESTIA/data/users/$user/ssl/mail.$domain.ca $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca
177+
fi
178+
179+
# Add domain SSL configuration to dovecot
180+
if [ -f /etc/dovecot/conf.d/domains/$domain.conf ]; then
181+
rm -f /etc/dovecot/conf.d/domains/$domain.conf
182+
fi
183+
184+
echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
185+
echo "local_name mail.$domain {" >> /etc/dovecot/conf.d/domains/$domain.conf
186+
echo " ssl_cert = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem" >> /etc/dovecot/conf.d/domains/$domain.conf
187+
echo " ssl_key = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key" >> /etc/dovecot/conf.d/domains/$domain.conf
188+
echo "}" >> /etc/dovecot/conf.d/domains/$domain.conf
189+
190+
# Add domain SSL configuration to exim4
191+
# Cleanup symlinks
192+
find /usr/local/hestia/ssl/mail -xtype l -delete
193+
194+
ln -s -f $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem /usr/local/hestia/ssl/mail/mail.$domain.crt
195+
ln -s -f $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key /usr/local/hestia/ssl/mail/mail.$domain.key
196+
197+
# Set correct permissions on certificates
198+
chmod 750 $HOMEDIR/$user/conf/mail/$domain/ssl
199+
chown -R $MAIL_USER:mail $HOMEDIR/$user/conf/mail/$domain/ssl
200+
chmod 0644 $HOMEDIR/$user/conf/mail/$domain/ssl/*
201+
chown -h $user:mail $HOMEDIR/$user/conf/mail/$domain/ssl/*
202+
chmod -R 0644 /usr/local/hestia/ssl/mail/*
203+
chown -h $user:mail /usr/local/hestia/ssl/mail/*
156204
fi
157205

158206
# Move data
@@ -162,11 +210,6 @@ if [ ! -z "$mail_data" ]; then
162210
find $HOMEDIR/$user/mail/$domain -user $owner \
163211
-exec chown -h $user {} \;
164212

165-
# Rebuild config
166-
$BIN/v-unsuspend-mail-domain $user $domain no >> /dev/null 2>&1
167-
$BIN/v-rebuild-mail-domains $owner no
168-
$BIN/v-rebuild-mail-domains $user
169-
170213
# Checking exim username for later chowning
171214
exim_user="exim";
172215
check_exim_username=$(grep -c '^Debian-exim:' /etc/passwd)
@@ -178,18 +221,43 @@ if [ ! -z "$mail_data" ]; then
178221
find $HOMEDIR/$user/conf/mail/$domain -user root \
179222
-exec chown $exim_user {} \;
180223
fi
224+
225+
# Remove old mail directory from original owner
226+
if [ -e "$HOMEDIR/$owner/mail/$domain" ]; then
227+
rm -rf "$HOMEDIR/$owner/mail/$domain"
228+
fi
229+
230+
# Remove old mail configuration directory from original owner
231+
if [ -e "$HOMEDIR/$owner/conf/mail/$domain" ]; then
232+
rm -rf "$HOMEDIR/$owner/conf/mail/$domain"
233+
fi
234+
if [ -e "$HESTIA/data/users/$owner/mail/$domain.conf" ]; then
235+
rm -f "$HESTIA/data/users/$owner/mail/$domain.conf"
236+
fi
237+
238+
# Rebuild config
239+
$BIN/v-unsuspend-mail-domain $user $domain no >> /dev/null 2>&1
240+
$BIN/v-rebuild-mail-domains $owner no
241+
$BIN/v-rebuild-mail-domains $user
181242
fi
182243

183244
# Update counters
184245
$BIN/v-update-user-counters $owner
185246
$BIN/v-update-user-counters $user
186247

248+
# Send notification to panel
249+
if [ ! -z "$web_data" ] || [ ! -z "$dns_data" ] || [ ! -z "$mail_data" ]; then
250+
$HESTIA/bin/v-add-user-notification "$user" "$domain has been added to your account" ''
251+
fi
187252

188253
#----------------------------------------------------------#
189254
# Hestia #
190255
#----------------------------------------------------------#
191256

192257
# Logging
258+
log_history "moved domain $domain from $owner to $user" '' "admin"
259+
log_history "$domain was added to your account" '' "$user"
260+
log_history "$domain was removed from your account" '' "$owner"
193261
log_event "$OK" "$ARGUMENTS"
194262

195263
exit

0 commit comments

Comments
 (0)