|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + Copyright (c) 2015, Florian Schaal, schaal @it |
| 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 | + |
| 32 | + |
| 33 | +/* define your settings here */ |
| 34 | +$username = 'admin'; |
| 35 | +$password = 'admin'; |
| 36 | +$soap_location = 'http://192.168.0.105:8080/remote/index.php'; |
| 37 | +$soap_uri = 'http://192.168.0.105:8080/remote/'; |
| 38 | +/* stop editing */ |
| 39 | + |
| 40 | + |
| 41 | +error_reporting(E_ALL^ E_WARNING); |
| 42 | + |
| 43 | +exec('which amavisd-new 2> /dev/null', $tmp_output, $tmp_retval); |
| 44 | +if ($tmp_retval != 0) { |
| 45 | + exec('which amavisd 2> /dev/null', $tmp_output, $tmp_retval); |
| 46 | + if ($tmp_retval == 0) $amavis = $tmp_output[0]; |
| 47 | +} else $amavis = $tmp_output[0]; |
| 48 | + |
| 49 | +if (!isset($amavis)) die ("amavisd not found"); |
| 50 | + |
| 51 | + |
| 52 | +echo "Importing dkim-settings from amavis.\n\nTo import the settings even when the public-key is not available, use ".$argv[0]." --force\nNOTE: In force-mode dkim will be set to 'no' if no public-key was found.\n\n"; |
| 53 | + |
| 54 | +if ( isset($argv) && isset ($argv[1]) && $argv[1] == '--force' ) $force = true; else $force = false; |
| 55 | + |
| 56 | +$client = new SoapClient(null, array('location' => $soap_location, |
| 57 | + 'uri' => $soap_uri, |
| 58 | + 'trace' => 1, |
| 59 | + 'exceptions' => 1)); |
| 60 | + |
| 61 | + |
| 62 | +exec($amavis.' showkeys', $tmp_output, $tmp_retval); |
| 63 | + |
| 64 | +foreach ( $tmp_output as $line ) { |
| 65 | + //* get domain and private key-file |
| 66 | + if ( preg_match('#^; key#', $line) ) { |
| 67 | + $line_array = explode(' ', $line); |
| 68 | + if ( $line_array[2] = 'domain' ) { |
| 69 | + $domain = rtrim($line_array[3], ','); |
| 70 | + $private_keyfile = $line_array[4]; |
| 71 | + //* get the public-key from private-key |
| 72 | + unset($public_key); |
| 73 | + unset($pubkey); |
| 74 | + unset($private_key); |
| 75 | + $private_key = file_get_contents($private_keyfile); |
| 76 | + if ( isset($private_key) && !empty($private_key)) { |
| 77 | + exec('echo '.escapeshellarg($private_key).'|openssl rsa -pubout -outform PEM 2> /dev/null',$pubkey,$result); |
| 78 | + $public_key=''; |
| 79 | + foreach($pubkey as $values) $public_key=$public_key.$values."\n"; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + //* get selector |
| 85 | + if ( isset($domain) ) { |
| 86 | + if ( preg_match('/_domainkey.'.$domain.'.* TXT \(/', $line) ) { |
| 87 | + $line_array = explode(' ', $line); |
| 88 | + $selector = substr ( $line_array[0], 0, strpos($line_array[0], '.') ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if ( isset($domain) && isset($selector) && isset($private_keyfile) && isset($public_key) ) { |
| 93 | + |
| 94 | + try { |
| 95 | + if ( !$session_id = $client->login($username, $password) ) { |
| 96 | + echo 'SOAP-ERROR: Can´t login'; |
| 97 | + } |
| 98 | + |
| 99 | + echo "\nprocessing ".$domain."...\n"; |
| 100 | + |
| 101 | + $record = $client->mail_domain_get_by_domain($session_id, $domain); |
| 102 | + |
| 103 | + if ( !empty($record) ) { |
| 104 | + $record = $record[0]; |
| 105 | + echo " OK: domain exists in the database\n"; |
| 106 | + //* check if the public-key is available |
| 107 | + exec($amavis.' testkeys '.escapeshellarg($domain).'', $test_output, $test_retval); |
| 108 | + $pub_key = false; |
| 109 | + if ( preg_match('/^TESTING.*'.$selector.'._domainkey.'.$domain.'.*pass/',$test_output[0]) ) $pub_key = true; |
| 110 | + $client_id = $client->client_get_id($session_id, $record['sys_userid']); |
| 111 | + unset($test_output); |
| 112 | + if ( $pub_key ) { |
| 113 | + $record['dkim_selector'] = $selector; |
| 114 | + $record['dkim'] = 'y'; |
| 115 | + if ( preg_match("/(^-----BEGIN PUBLIC KEY-----)[a-zA-Z0-9\r\n\/\+=]{1,221}(-----END PUBLIC KEY-----(\n|\r)?$)/", $record['dkim_public'] ) ) { |
| 116 | + $record['dkim_public'] = $public_key; |
| 117 | + echo " OK: public key\n"; |
| 118 | + } else { |
| 119 | + $record['dkim_public'] = ''; |
| 120 | + $record['dkim'] = 'n'; |
| 121 | + echo " ERROR: public key invalid\n disable dkim for ".$domain."\n"; |
| 122 | + } |
| 123 | + if ( preg_match("/(^-----BEGIN RSA PRIVATE KEY-----)[a-zA-Z0-9\r\n\/\+=]{1,850}(-----END RSA PRIVATE KEY-----(\n|\r)?$)/", $private_key) ) { |
| 124 | + $record['dkim_private'] = $private_key; |
| 125 | + echo " OK: private key\n"; |
| 126 | + } else { |
| 127 | + $record['dkim_private'] = ''; |
| 128 | + $record['dkim'] = 'n'; |
| 129 | + echo " ERROR: private key invalid\n disable dkim for ".$domain."\n"; |
| 130 | + } |
| 131 | + $client->mail_domain_update($session_id, $client_id, $record['domain_id'], $record); |
| 132 | + echo " OK: updating database\n"; |
| 133 | + } else { |
| 134 | + echo " ERROR: no public-key available - skipping ".$domain."\n"; |
| 135 | + } |
| 136 | + } else { |
| 137 | + echo " ERROR: domain not in the database - skipping ".$domain."\n"; |
| 138 | + } |
| 139 | + $client->logout($session_id); |
| 140 | + } catch (SoapFault $e) { |
| 141 | + echo $client->__getLastResponse(); |
| 142 | + die('SOAP Error: '.$e->getMessage()); |
| 143 | + } |
| 144 | + unset($domain); |
| 145 | + unset($selector); |
| 146 | + } |
| 147 | +} |
| 148 | +?> |
0 commit comments