| 1 |
#!/bin/sh |
|---|
| 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 |
PATH=/sbin:/bin:/usr/sbin:/usr/bin |
|---|
| 32 |
|
|---|
| 33 |
set -e |
|---|
| 34 |
|
|---|
| 35 |
umask 022 |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
CONFIG_FILE="/etc/alternc/local.sh" |
|---|
| 42 |
|
|---|
| 43 |
DOMAIN_LOG_FILE="/var/log/alternc/update_domains.log" |
|---|
| 44 |
DATA_ROOT="/var/alternc" |
|---|
| 45 |
|
|---|
| 46 |
NAMED_TEMPLATE="/etc/bind/templates/named.template" |
|---|
| 47 |
ZONE_TEMPLATE="/etc/bind/templates/zone.template" |
|---|
| 48 |
|
|---|
| 49 |
ACTION_INSERT=0 |
|---|
| 50 |
ACTION_UPDATE=1 |
|---|
| 51 |
ACTION_DELETE=2 |
|---|
| 52 |
TYPE_LOCAL=0 |
|---|
| 53 |
TYPE_URL=1 |
|---|
| 54 |
TYPE_IP=2 |
|---|
| 55 |
TYPE_WEBMAIL=3 |
|---|
| 56 |
YES=1 |
|---|
| 57 |
|
|---|
| 58 |
if [ `id -u` -ne 0 ]; then |
|---|
| 59 |
echo "update_domains.sh must be launched as root" |
|---|
| 60 |
exit 1 |
|---|
| 61 |
fi |
|---|
| 62 |
|
|---|
| 63 |
if [ ! -x "/usr/bin/get_account_by_domain" ]; then |
|---|
| 64 |
echo "Your AlternC installation is incorrect ! If you are using pre 0.9.4, " |
|---|
| 65 |
echo "you have to install alternc-admintools: " |
|---|
| 66 |
echo " apt-get update ; apt-get install alternc-admintools" |
|---|
| 67 |
exit 1 |
|---|
| 68 |
fi |
|---|
| 69 |
|
|---|
| 70 |
if [ ! -r "$CONFIG_FILE" ]; then |
|---|
| 71 |
echo "Can't access $CONFIG_FILE." |
|---|
| 72 |
exit 1 |
|---|
| 73 |
fi |
|---|
| 74 |
|
|---|
| 75 |
. "$CONFIG_FILE" |
|---|
| 76 |
|
|---|
| 77 |
if [ -z "$MYSQL_HOST" -o -z "$MYSQL_DATABASE" -o -z "$MYSQL_USER" -o \ |
|---|
| 78 |
-z "$MYSQL_PASS" -o -z "$DEFAULT_MX" -o -z "$PUBLIC_IP" ]; then |
|---|
| 79 |
echo "Bad configuration. Please use:" |
|---|
| 80 |
echo " dpkg-reconfigure alternc" |
|---|
| 81 |
exit 1 |
|---|
| 82 |
fi |
|---|
| 83 |
|
|---|
| 84 |
if [ -f "$LOCK_FILE" ]; then |
|---|
| 85 |
echo "`date` $0: last cron unfinished or stale lock file." | |
|---|
| 86 |
tee -a "$DOMAIN_LOG_FILE" >&2 |
|---|
| 87 |
exit 1 |
|---|
| 88 |
fi |
|---|
| 89 |
|
|---|
| 90 |
NAMED_CONF_FILE="$DATA_ROOT/bind/automatic.conf" |
|---|
| 91 |
ZONES_DIR="$DATA_ROOT/bind/zones" |
|---|
| 92 |
APACHECONF_DIR="$DATA_ROOT/apacheconf" |
|---|
| 93 |
OVERRIDE_PHP_FILE="$APACHECONF_DIR/override_php.conf" |
|---|
| 94 |
WEBMAIL_DIR="$DATA_ROOT/bureau/admin/webmail" |
|---|
| 95 |
LOCK_FILE="$DATA_ROOT/bureau/cron.lock" |
|---|
| 96 |
HTTP_DNS="$DATA_ROOT/dns" |
|---|
| 97 |
HTML_HOME="$DATA_ROOT/html" |
|---|
| 98 |
|
|---|
| 99 |
MYSQL_SELECT="mysql -h${MYSQL_HOST} -u${MYSQL_USER} |
|---|
| 100 |
-p${MYSQL_PASS} -Bs ${MYSQL_DATABASE}" |
|---|
| 101 |
MYSQL_DELETE="mysql -h${MYSQL_HOST} -u${MYSQL_USER} |
|---|
| 102 |
-p${MYSQL_PASS} ${MYSQL_DATABASE}" |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
. /usr/lib/alternc/functions.sh |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
touch "$LOCK_FILE" |
|---|
| 116 |
DOMAINS_TMP_FILE=`mktemp -t alternc.update_domains.XXXXXX` |
|---|
| 117 |
HOSTS_TMP_FILE=`mktemp -t alternc.update_domains.XXXXXX` |
|---|
| 118 |
RELOAD_ZONES_TMP_FILE=`mktemp -t alternc.update_domains.XXXXXX` |
|---|
| 119 |
|
|---|
| 120 |
cleanup() { |
|---|
| 121 |
rm -f "$LOCK_FILE" "$DOMAINS_TMP_FILE" "$HOSTS_TMP_FILE" |
|---|
| 122 |
rm -f "$RELOAD_ZONES_TMP_FILE" |
|---|
| 123 |
exit 0 |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
trap cleanup 0 1 2 15 |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
$MYSQL_SELECT <<EOF | tail -n '+1' > "$DOMAINS_TMP_FILE" |
|---|
| 131 |
SELECT membres.login, |
|---|
| 132 |
domaines_standby.domaine, |
|---|
| 133 |
if (domaines_standby.mx = '', '@', domaines_standby.mx), |
|---|
| 134 |
domaines_standby.gesdns, |
|---|
| 135 |
domaines_standby.gesmx, |
|---|
| 136 |
domaines_standby.action |
|---|
| 137 |
FROM domaines_standby |
|---|
| 138 |
LEFT JOIN membres membres |
|---|
| 139 |
ON membres.uid = domaines_standby.compte |
|---|
| 140 |
ORDER BY domaines_standby.action |
|---|
| 141 |
EOF |
|---|
| 142 |
|
|---|
| 143 |
$MYSQL_SELECT <<EOF | tail -n '+1' > "$HOSTS_TMP_FILE" |
|---|
| 144 |
SELECT membres.login, |
|---|
| 145 |
sub_domaines_standby.domaine, |
|---|
| 146 |
if (sub_domaines_standby.sub = '', '@', sub_domaines_standby.sub), |
|---|
| 147 |
if (sub_domaines_standby.valeur = '', 'NULL', |
|---|
| 148 |
sub_domaines_standby.valeur), |
|---|
| 149 |
sub_domaines_standby.type, |
|---|
| 150 |
sub_domaines_standby.action |
|---|
| 151 |
FROM sub_domaines_standby |
|---|
| 152 |
LEFT JOIN membres membres |
|---|
| 153 |
ON membres.uid = sub_domaines_standby.compte |
|---|
| 154 |
ORDER BY sub_domaines_standby.action desc |
|---|
| 155 |
EOF |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
if [ "`wc -l < $DOMAINS_TMP_FILE`" -gt 0 ]; then |
|---|
| 160 |
echo `date` >> $DOMAIN_LOG_FILE |
|---|
| 161 |
cat "$DOMAINS_TMP_FILE" >> $DOMAIN_LOG_FILE |
|---|
| 162 |
fi |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
OLD_IFS="$IFS" |
|---|
| 166 |
IFS=" " |
|---|
| 167 |
while read user domain mx are_we_dns are_we_mx action ; do |
|---|
| 168 |
IFS="$OLD_IFS" |
|---|
| 169 |
|
|---|
| 170 |
DOMAIN_LETTER=`print_domain_letter "$domain"` |
|---|
| 171 |
USER_LETTER=`print_user_letter "$user"` |
|---|
| 172 |
|
|---|
| 173 |
case "$action" in |
|---|
| 174 |
$ACTION_INSERT) |
|---|
| 175 |
if [ "$are_we_dns" = "$YES" ] ; then |
|---|
| 176 |
init_zone "$domain" |
|---|
| 177 |
fi |
|---|
| 178 |
;; |
|---|
| 179 |
|
|---|
| 180 |
$ACTION_UPDATE) |
|---|
| 181 |
if [ "$are_we_dns" = "$YES" ] ; then |
|---|
| 182 |
init_zone "$domain" |
|---|
| 183 |
change_mx "$domain" "$mx" |
|---|
| 184 |
else |
|---|
| 185 |
remove_zone "$domain" |
|---|
| 186 |
fi |
|---|
| 187 |
;; |
|---|
| 188 |
|
|---|
| 189 |
$ACTION_DELETE) |
|---|
| 190 |
remove_zone "$domain" |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
rm -f "${HTTP_DNS}/${DOMAIN_LETTER}/"*".$domain" |
|---|
| 194 |
rm -f "${HTTP_DNS}/${DOMAIN_LETTER}/$domain" |
|---|
| 195 |
rm -rf "${HTTP_DNS}/redir/${DOMAIN_LETTER}/"*".$domain" |
|---|
| 196 |
rm -rf "${HTTP_DNS}/redir/${DOMAIN_LETTER}/$domain" |
|---|
| 197 |
;; |
|---|
| 198 |
|
|---|
| 199 |
*) |
|---|
| 200 |
echo "Unknown action code: $action" >> "$DOMAIN_LOG_FILE" |
|---|
| 201 |
;; |
|---|
| 202 |
esac |
|---|
| 203 |
|
|---|
| 204 |
IFS=" " |
|---|
| 205 |
done < "$DOMAINS_TMP_FILE" |
|---|
| 206 |
IFS="$OLD_IFS" |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
if [ "`wc -l < $HOSTS_TMP_FILE`" -gt 0 ] ; then |
|---|
| 211 |
echo `date` >> $DOMAIN_LOG_FILE |
|---|
| 212 |
cat "$HOSTS_TMP_FILE" >> $DOMAIN_LOG_FILE |
|---|
| 213 |
fi |
|---|
| 214 |
|
|---|
| 215 |
OLD_IFS="$IFS" |
|---|
| 216 |
IFS=" " |
|---|
| 217 |
while read user domain host value type action; do |
|---|
| 218 |
IFS="$OLD_IFS" |
|---|
| 219 |
|
|---|
| 220 |
case "$action" in |
|---|
| 221 |
$ACTION_UPDATE | $ACTION_INSERT) |
|---|
| 222 |
add_host "$domain" "$type" "$host" "$value" "$user" |
|---|
| 223 |
;; |
|---|
| 224 |
|
|---|
| 225 |
$ACTION_DELETE) |
|---|
| 226 |
delete_host "$domain" "$host" |
|---|
| 227 |
;; |
|---|
| 228 |
|
|---|
| 229 |
*) |
|---|
| 230 |
echo "Unknown action code: $action" >> "$DOMAIN_LOG_FILE" |
|---|
| 231 |
;; |
|---|
| 232 |
esac |
|---|
| 233 |
|
|---|
| 234 |
IFS=" " |
|---|
| 235 |
done < "$HOSTS_TMP_FILE" |
|---|
| 236 |
IFS="$OLD_IFS" |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
RELOAD_ZONES=`cat "$RELOAD_ZONES_TMP_FILE"` |
|---|
| 241 |
if [ ! -z "$RELOAD_ZONES" ]; then |
|---|
| 242 |
if [ "$RELOAD_ZONES" = "all" ]; then |
|---|
| 243 |
rndc reload > /dev/null || echo "Cannot reload bind" >> "$DOMAIN_LOG_FILE" |
|---|
| 244 |
else |
|---|
| 245 |
for zone in $RELOAD_ZONES; do |
|---|
| 246 |
rndc reload "$zone" > /dev/null || echo "Cannot reload bind for zone $zone" >> "$DOMAIN_LOG_FILE" |
|---|
| 247 |
done |
|---|
| 248 |
fi |
|---|
| 249 |
apachectl graceful > /dev/null || echo "Cannot restart apache" >> "$DOMAIN_LOG_FILE" |
|---|
| 250 |
fi |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
echo "DELETE FROM domaines_standby" | $MYSQL_DELETE |
|---|
| 255 |
echo "DELETE FROM sub_domaines_standby" | $MYSQL_DELETE |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|