|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright (c) 2013, Marius Cramer, pixcept KG |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without modification, |
| 8 | +are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + * Redistributions of source code must retain the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + this list of conditions and the following disclaimer in the documentation |
| 14 | + and/or other materials provided with the distribution. |
| 15 | + * Neither the name of ISPConfig nor the names of its contributors |
| 16 | + may be used to endorse or promote products derived from this software without |
| 17 | + specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 26 | +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 | +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +
|
| 30 | +
|
| 31 | +DNSSEC-Implementation by Alexander Täffner aka dark alex |
| 32 | +*/ |
| 33 | + |
| 34 | +class cronjob_bind_dnssec extends cronjob { |
| 35 | + |
| 36 | + // job schedule |
| 37 | + protected $_schedule = '30 3 * * *'; //daily at 3:30 a.m. |
| 38 | + |
| 39 | + public function onRunJob() { |
| 40 | + global $app, $conf; |
| 41 | + |
| 42 | + //* Load libraries |
| 43 | + $app->uses("getconf,tpl"); |
| 44 | + |
| 45 | + //* load the server configuration options |
| 46 | + $dns_config = $app->getconf->get_server_config($conf["server_id"], 'dns'); |
| 47 | + |
| 48 | + //TODO : change this when distribution information has been integrated into server record |
| 49 | + $filespre = (file_exists('/etc/gentoo-release')) ? 'pri/' : 'pri.'; |
| 50 | + |
| 51 | + $soas = $app->db->queryAllRecords('SELECT * FROM dns_soa WHERE dnssec_wanted=\'Y\' AND dnssec_initialized=\'Y\' AND dnssec_last_signed < '.(time()-(3600*24*5-900))); //Resign zones every 5 days (expiry is 16 days so we have enough safety, 15 minutes tolerance) |
| 52 | + |
| 53 | + while ($data = next($soas)) { |
| 54 | + $domain = substr($data['origin'], 0, strlen($data['origin'])-1); |
| 55 | + if (!file_exists($dns_config['bind_zonefiles_dir'].'/'.$filespre.$domain)) return false; |
| 56 | + |
| 57 | + $zonefile = file_get_contents($dns_config['bind_zonefiles_dir'].'/'.$filespre.$domain); |
| 58 | + $keycount=0; |
| 59 | + foreach (glob($dns_config['bind_zonefiles_dir'].'/K'.$domain.'*.key') as $keyfile) { |
| 60 | + $includeline = '$INCLUDE '.basename($keyfile); |
| 61 | + if (!preg_match('@'.preg_quote($includeline).'@', $zonefile)) $zonefile .= "\n".$includeline."\n"; |
| 62 | + $keycount++; |
| 63 | + } |
| 64 | + if ($keycount != 2) $app->log('DNSSEC Warning: There are more or less than 2 keyfiles for zone '.$domain, LOGLEVEL_WARN); |
| 65 | + file_put_contents($dns_config['bind_zonefiles_dir'].'/'.$filespre.$domain, $zonefile); |
| 66 | + |
| 67 | + //Sign the zone and set it valid for max. 16 days |
| 68 | + exec('cd '.escapeshellcmd($dns_config['bind_zonefiles_dir']).';'. |
| 69 | + '/usr/sbin/dnssec-signzone -A -e +1382400 -3 $(head -c 1000 /dev/random | sha1sum | cut -b 1-16) -N increment -o '.escapeshellcmd($domain).' -t '.$filespre.escapeshellcmd($domain)); |
| 70 | + |
| 71 | + //Write Data back ino DB |
| 72 | + $dnssecdata = "DS-Records:\n".file_get_contents($dns_config['bind_zonefiles_dir'].'/dsset-'.$domain.'.'); |
| 73 | + $dnssecdata .= "\n------------------------------------\n\nDNSKEY-Records:\n"; |
| 74 | + foreach (glob($dns_config['bind_zonefiles_dir'].'/K'.$domain.'*.key') as $keyfile) { |
| 75 | + $dnssecdata .= file_get_contents($keyfile)."\n\n"; |
| 76 | + } |
| 77 | + |
| 78 | + $app->db->query('UPDATE dns_soa SET dnssec_info=\''.$dnssecdata.'\', dnssec_initialized=\'Y\', dnssec_last_signed=\''.time().'\' WHERE id='.$data['id']); |
| 79 | + } |
| 80 | + |
| 81 | + parent::onRunJob(); |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +?> |
0 commit comments