isp-tools

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 6c2065335b6cd723bc5df7117b62b4c184f6b46e
parent cb9a66725360d35c795f045ef493ca9f53acb277
Author: dankert <devnull@localhost>
Date:   Wed, 24 Feb 2016 21:30:16 +0100

Update in das SCM

Diffstat:
update_dns | 39++++++++++++++++++++++-----------------
update_httpd | 170+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
update_mta | 36+++++++++++++++++++++++++++---------
3 files changed, 154 insertions(+), 91 deletions(-)

diff --git a/update_dns b/update_dns @@ -5,14 +5,15 @@ # Konfiguration: -# Bind9-Verzeichnis mit Zonen -ZONEDIR=/etc/bind/db.d -ALL_ZONES_FILE=/etc/bind/db.d/named.conf.local +CONFIG=/etc/default/ispconfig + +if [ ! -f $CONFIG ]; then + echo "File $CONFIG not found" + exit 4; +fi + +source $CONFIG -# Datenbank -MYSQL_USER=cron -MYSQL_PASSWORD=twGeL8nKbECs5aKU -MYSQL_DATABASE=verwaltung if [ ! -d $ZONEDIR/.hg ]; then hg -R $ZONEDIR init @@ -24,13 +25,14 @@ function sql { } +# Datei mit allen Zonen erzeugen last_domain_modified=`sql "SELECT max(unix_timestamp(modified)) FROM domain WHERE domain REGEXP('^[a-z0-9-]+\..[a-z]+\$') and active=1"` last_file_modified=`stat --format %Y $ALL_ZONES_FILE` if [ "$last_domain_modified" -gt "$last_file_modified" ]; then echo "" > $ALL_ZONES_FILE - sql "SELECT domain FROM domain WHERE domain REGEXP('^[a-z0-9-]+\..[a-z]+\$') and active=1"|while read domain; do + sql "SELECT domain FROM domain WHERE domain REGEXP('^[a-z0-9-]+\..[a-z]+\$') and active=1 order by domain"|while read domain; do cat >> $ALL_ZONES_FILE <<EOF zone "$domain" { @@ -75,31 +77,34 @@ sql "SELECT domain FROM domain WHERE domain REGEXP('^[a-z0-9-]+\..[a-z]+\$') and @ IN SOA ns1.jdhh.de. root.ns1.jdhh.de. $serial 86400 10800 1209600 57600 IN NS ns1.jdhh.de. IN NS nsb3.schlundtech.de. - IN MX 10 mail.jdhh.de. - IN MX 20 mail02.jdhh.de. IN A 85.214.76.132 IN TXT "v=spf1 a mx -all" ZEOF - # Hat die Domain Mailboxen? Falls ja, dann MX-Eintrag ergänzen - count_mailboxes=`sql "SELECT count(*) FROM mailbox WHERE domain='$domain'"` + # Hat die Domain Mailboxen oder Aliases? Falls ja, dann MX-Eintrag ergänzen + count_mailboxes=`sql "select sum(c) FROM + ( select count(*) as c from mailbox WHERE domain='$domain' union all + select count(*) as c FROM alias WHERE domain='$domain' ) as counts"` if [ "$count_mailboxes" -gt 0 ]; then echo " IN MX 10 mail.jdhh.de." >> $ZONEFILE echo " IN MX 20 mail02.jdhh.de." >> $ZONEFILE fi # Subdomain-Liste - sql "SELECT replace(domain,'.$domain','') FROM domain WHERE domain like '%.$domain' "|while read subdomain; do + sql "SELECT replace(domain,'.$domain','') as subdomain FROM domain WHERE domain like '%.$domain' order by subdomain"|while read subdomain; do echo -n $subdomain|sed -e :a -e 's/^.\{1,20\}$/& /;ta' >> $ZONEFILE # 24-stellig aufgefuellt mit Leerzeichen echo "IN A 85.214.76.132" >> $ZONEFILE - # Hat die Subdomain Mailboxen? Falls ja, dann MX-Eintrag ergänzen - count_mailboxes=`sql "SELECT count(*) FROM mailbox WHERE domain='$subdomain'"` + # Hat die Subdomain Mailboxen oder Aliases? Falls ja, dann MX-Eintrag ergänzen + count_mailboxes=`sql "select sum(c) FROM + ( select count(*) as c from mailbox WHERE domain='$subdomain.$domain' union all + select count(*) as c FROM alias WHERE domain='$subdomain.$domain' ) as counts"` + if [ "$count_mailboxes" -gt 0 ]; then echo -n $subdomain|sed -e :a -e 's/^.\{1,20\}$/& /;ta' >> $ZONEFILE # 24-stellig aufgefuellt mit Leerzeichen - echo " IN MX 10 mail.jdhh.de." >> $ZONEFILE + echo "IN MX 10 mail.jdhh.de." >> $ZONEFILE echo -n $subdomain|sed -e :a -e 's/^.\{1,20\}$/& /;ta' >> $ZONEFILE # 24-stellig aufgefuellt mit Leerzeichen - echo " IN MX 20 mail02.jdhh.de." >> $ZONEFILE + echo "IN MX 20 mail02.jdhh.de." >> $ZONEFILE fi done diff --git a/update_httpd b/update_httpd @@ -1,93 +1,133 @@ #!/bin/bash -# Erzeugt die Virtual-Host-Konfigurationen für den Apache-Webserver. +# Erzeugt die Virtual-Host-Konfigurationen fuer den Apache-Webserver. # -# Über ein Python-Skript werden die Dateien erzeugt. Falls diese von den -# aktuellen Dateien abweichen, werden diese einzeln kopiert und dabei -# versioniert. -# Apache-Konfigurationsverzeichnis -VHOSTDIR=/etc/apache2/sites.d -# Temporaeren Ordner erzeugen -TEMPDIR=`tempfile --prefix=ldap2zone` -rm $TEMPDIR -mkdir $TEMPDIR/ +# Konfiguration: -# Ordner für "nobody" beschreibbar machen -chown nobody $TEMPDIR +CONFIG=/etc/default/ispconfig -# LDAP auslesen und VHOST-Dateien erzeugen -su nobody --command="/usr/local/bin/ldap2vhost \ - --dn=cn=ldap2dns,ou=batch,ou=users,dc=ds,dc=jandankert,dc=de \ - -p ldap2dns \ - --base=ou=dns,dc=ds,dc=jandankert,dc=de \ - --output-dir=$TEMPDIR" +if [ ! -f $CONFIG ]; then + echo "File $CONFIG not found" + exit 4; +fi -reload=false +source $CONFIG -for f in $TEMPDIR/*; do - f=`basename $f` - - # Datei geaendert? Dann kopieren und dabei Backupfile erzeugen - if [ ! -f $VHOSTDIR/$f -o ! `diff -q $TEMPDIR/$f $VHOSTDIR/$f|wc -l` = "0" ]; then - reload=true - echo "Apache-VHOST ist geaendert oder neu: $VHOSTDIR/$f" - diff $VHOSTDIR/$f $TEMPDIR/$f - cp -v --backup=t $TEMPDIR/$f $VHOSTDIR/$f - fi -done +if [ ! -f $HTTP_VHOST_DIR ]; then + echo "File $HTTP_VHOST_DIR not found" + exit 4; +fi -# Temporären Ordner löschen -rm -r $TEMPDIR - -# Testen, ob alle DocumentRoot vorhanden sind -for f in `find /etc/apache2/sites.d/ -regex .+[a-z]$`; do - for dc in `grep DocumentRoot $f|cut -f 4 -d " "`; do - if [ ! -d $dc ]; then - echo "DocumentRoot $dc does not exist" - mkdir -v $dc - user=`echo $dc|cut -f 3 -d "/"` - echo User $user - chown -v $user:$user $dc - fi - done -done +if [ ! -d $HTTPD_VHOST_DIR/.hg ]; then + hg -R $HTTPD_VHOST_DIR init +fi -# Testen, ob alle AccessLog/ErrorLog vorhanden sind -for f in `find /etc/apache2/sites.d/ -regex .+[a-z]$`; do - for lf in `egrep "(CustomLog|ErrorLog)" $f|cut -f 4 -d " "`; do - ldir=`dirname $lf` - if [ ! -d $ldir ]; then - echo "Ausgabeordner $ldir does not exist" - mkdir -v $ldir - fi - done -done +function sql { + sql=$1 + mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -se "$sql" $MYSQL_DATABASE +} -# Apache-Webserver muss Konfiguration neu laden -if $reload; then - if [ ! `apache2ctl -t|grep "Syntax OK"|wc -l` = "0" ]; then - echo "Apache-Konfiguraton ist fehlerhaft! Kein Restart." - apache2ctl -t + +sql "SELECT UNIX_TIMESTAMP(modified),domain FROM domain WHERE active=1"|while read modified domain; do + + CONFFILE=$HTTPD_VHOST_DIR/$domain + + if [ ! -f $CONFFILE ]; then + last_file_modified=0 else - echo "Apache-Konfiguration ist OK." - echo "Reloading Apache Webserver." - apache2ctl restart + last_file_modified=`stat --format %Y $CONFFILE` fi -fi - + if [ "$modified" -gt "$last_file_modified" ]; then + echo "Aktualisiere $domain" + + # Versuchen, den Benutzer zur Domain zu finden. + dir=`find /home -mindepth 4 -maxdepth 4 -path "/home/*/var/www/*" -type d -name "$domain"` + user=`echo $dir|cut -f 3 -d "/"` + #echo "User: $user ==> Domain: $domain" + + if [ ! "$user" ]; then + + cat > $CONFFILE <<EOF + +# AUTO-GENERATED by $0 - DO NOT CHANGE! +<VirtualHost *> + ServerName $domain + + RewriteEngine on + RewriteRule (.*) - [L,R=410] +</VirtualHost> +EOF + else + #echo "Pruefe $HTTPD_EXTRA_CONFIG_DIR/$domain.conf" + if [ -f $HTTPD_EXTRA_CONFIG_DIR/$domain.conf ]; then + extra_config=" Include $HTTPD_EXTRA_CONFIG_DIR/$domain.conf" + else + extra_config= + fi + + log_dir=/home/$user/var/log/apache2/$domain + + if [ ! -d $log_dir ]; then + echo "Lege an: $log_dir" + mkdir $log_dir + fi + #echo "Logdir: $log_dir" + + + + + + cat > $CONFFILE <<EOF + +# AUTO-GENERATED by $0 - DO NOT CHANGE! +<VirtualHost *> + ServerName $domain + ServerAdmin webmaster@$domain + + DocumentRoot /home/$user/var/www/$domain + + php_admin_value sendmail_path "/usr/sbin/sendmail -t -i -r webmaster@$domain" + php_admin_value open_basedir /home/$user/var/www/$domain:/home/$user/tmp + php_admin_value upload_tmp_dir /home/$user/tmp + + ErrorLog $log_dir/error.log + CustomLog $lor_dir/access.log combined + + $extra_config +</VirtualHost> +EOF + hg -R $HTTPD_VHOST_DIR add $CONFFILE + fi + fi + +done +# Apache-Webserver muss Konfiguration neu laden +if [ `hg -R $HTTPD_VHOST_DIR status|wc -l` -gt 0 ]; then + echo "Änderungen durchgeführt" + hg -R $HTTPD_VHOST_DIR diff + hg -R $HTTPD_VHOST_DIR commit -u `whoami` -m "Updating HTTPD configuration" + if [ ! `apache2ctl -t|grep "Syntax OK"|wc -l` -eq "0" ]; then + echo "Apache-Konfiguraton ist fehlerhaft! Kein Restart." + #apache2ctl -t + else + echo "Apache-Konfiguration ist OK." + echo "Reloading Apache Webserver." + #apache2ctl restart + fi +fi diff --git a/update_mta b/update_mta @@ -1,10 +1,16 @@ #!/bin/bash -# Datenbank -MYSQL_USER=cron -MYSQL_PASSWORD=twGeL8nKbECs5aKU -MYSQL_DATABASE=verwaltung +CONFIG=/etc/default/ispconfig + +if [ ! -f $CONFIG ]; then + echo "File $CONFIG not found" + exit 4; +fi + +source $CONFIG + +# Datenbank function sql { sql=$1 mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -se "$sql" $MYSQL_DATABASE @@ -13,21 +19,30 @@ function sql { function create_map { - mapfile=/etc/postfix/ldap/$1 + mapfile=$MTA_CONFIG_DIR/$1 sql=$2 - echo "# AUTO-GENERATED - DO NOT CHANGE!" > $mapfile + echo "# AUTO-GENERATED BY $0 - DO NOT CHANGE!" > $mapfile sql "$sql"|while read key value; do echo $key $value >> $mapfile done echo "" >> $mapfile - #hg diff - hg add $mapfile - hg commit -u `whoami` -m "Updating Postfix configuration in $mapfile" $mapfile + # Adding to SCM + hg -R $MTA_CONFIG_DIR add $mapfile + + # Compile Postfix map /usr/sbin/postmap $mapfile } +last_mail_modified=`sql "select unix_timestamp(max(modified)) FROM (select modified from mailbox union all select modified FROM alias) as modified"` +last_file_modified=`stat --format %Y $MTA_CONFIG_DIR/transport` + +if [ ! "$last_mail_modified" -gt "$last_file_modified" ]; then + exit 0; # all up to date, nothing to do. +fi + + create_map transport "select distinct domain,'virtual:' from mailbox where active=1" create_map aliases "select address,goto from alias where active=1" @@ -37,4 +52,7 @@ create_map accounts "select concat(local_part,'@',domain) , concat(maild create_map accountsmap "select concat(local_part,'@',domain) , concat(local_part,'@',domain) from mailbox where active=1" create_map recipient_reject "select concat(local_part,'@',domain), 'REJECT' from mailbox where active=0" +hg -R $MTA_CONFIG_DIR diff +hg -R $MTA_CONFIG_DIR commit -u `whoami` -m "Updating Postfix configuration" + postfix reload