Skip to content

Commit 70839d2

Browse files
author
Kristan Kenney
committed
Merge branch 'bugfix/2020-07_v-change-domain-owner' into staging/fixes
2 parents 135a795 + 0bc2974 commit 70839d2

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

bin/v-change-domain-owner

Lines changed: 88 additions & 5 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,6 +130,14 @@ 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..."
134+
135+
parse_object_kv_list "$mail_data"
136+
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
128141

129142
# Move config
130143
sed -i "/DOMAIN='$domain'/d" $HESTIA/data/users/$owner/mail.conf
@@ -140,18 +153,63 @@ if [ ! -z "$mail_data" ]; then
140153
$HESTIA/data/users/$user/mail/
141154
fi
142155

156+
# Move SSL certificates
157+
if [ "$SSL" = 'yes' ]; then
158+
# Ensure that SSL directory exists and move certificates
159+
mkdir -p $HESTIA/data/users/$user/ssl/
160+
mkdir -p $HOMEDIR/$user/conf/mail/$domain/ssl/
161+
162+
ssl_crt=$HESTIA/data/users/$owner/ssl/mail.$domain.crt
163+
ssl_key=$HESTIA/data/users/$owner/ssl/mail.$domain.key
164+
ssl_ca=$HESTIA/data/users/$owner/ssl/mail.$domain.ca
165+
ssl_pem=$HESTIA/data/users/$owner/ssl/mail.$domain.pem
166+
mv $ssl_crt $HESTIA/data/users/$user/ssl/
167+
mv $ssl_key $HESTIA/data/users/$user/ssl/
168+
mv $ssl_ca $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/*
204+
fi
205+
143206
# Move data
144207
mv $HOMEDIR/$owner/mail/$domain $HOMEDIR/$user/mail/
145208

146209
# Change ownership
147210
find $HOMEDIR/$user/mail/$domain -user $owner \
148211
-exec chown -h $user {} \;
149212

150-
# Rebuild config
151-
$BIN/v-unsuspend-mail-domain $user $domain no >> /dev/null 2>&1
152-
$BIN/v-rebuild-mail-domains $owner no
153-
$BIN/v-rebuild-mail-domains $user
154-
155213
# Checking exim username for later chowning
156214
exim_user="exim";
157215
check_exim_username=$(grep -c '^Debian-exim:' /etc/passwd)
@@ -163,18 +221,43 @@ if [ ! -z "$mail_data" ]; then
163221
find $HOMEDIR/$user/conf/mail/$domain -user root \
164222
-exec chown $exim_user {} \;
165223
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
166242
fi
167243

168244
# Update counters
169245
$BIN/v-update-user-counters $owner
170246
$BIN/v-update-user-counters $user
171247

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
172252

173253
#----------------------------------------------------------#
174254
# Hestia #
175255
#----------------------------------------------------------#
176256

177257
# 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"
178261
log_event "$OK" "$ARGUMENTS"
179262

180263
exit

0 commit comments

Comments
 (0)