@@ -87,6 +87,10 @@ function get_amavis_config() {
8787 break ;
8888 }
8989 }
90+ //* If we can use seperate config-files with amavis use 60-dkim
91+ if (substr_compare ($ amavis_configfile , '50-user ' , -7 ) === 0 )
92+ $ amavis_configfile = str_replace ('50-user ' , '60-dkim ' , $ amavis_configfile );
93+
9094 return $ amavis_configfile ;
9195 }
9296
@@ -98,10 +102,18 @@ function get_amavis_config() {
98102 */
99103 function check_system ($ data ) {
100104 global $ app , $ mail_config ;
105+
101106 $ app ->uses ('getconf ' );
102107 $ check =true ;
108+
103109 /* check for amavis-config */
104- if ( $ this ->get_amavis_config () == '' || !is_writeable ($ this ->get_amavis_config ()) ) {
110+ $ amavis_configfile = $ this ->get_amavis_config ();
111+
112+ //* When we can use 60-dkim for the dkim-keys create the file if it does not exists.
113+ if (substr_compare ($ amavis_configfile , '60-dkim ' , -7 ) === 0 && !file_exists ($ amavis_configfile ))
114+ file_put_contents ($ amavis_configfile , '' );
115+
116+ if ( $ amavis_configfile == '' || !is_writeable ($ amavis_configfile ) ) {
105117 $ app ->log ('Amavis-config not found or not writeable. ' , LOGLEVEL_ERROR );
106118 $ check =false ;
107119 }
@@ -198,11 +210,25 @@ function add_to_amavis($key_domain) {
198210
199211 $ restart = false ;
200212 $ selector = 'default ' ;
201- $ amavis_config = file_get_contents ($ this ->get_amavis_config ());
213+ $ amavis_configfile = $ this ->get_amavis_config ();
214+
215+ //* If we are using seperate config-files with amavis remove existing keys from 50-user to avoid duplicate keys
216+ if (substr_compare ($ amavis_configfile , '60-dkim ' , -7 ) === 0 ) {
217+ $ temp_configfile = str_replace ('60-dkim ' , '50-user ' , $ amavis_configfile );
218+ $ temp_config = file_get_contents ($ temp_configfile );
219+ if (preg_match ("/( \n| \r)?dkim_key.* " .$ key_domain .".*/ " , $ temp_config )) {
220+ $ temp_config = preg_replace ("/( \n| \r)?dkim_key.* " .$ key_domain .".*( \n| \r)?/ " , '' , $ temp_config )."\n" ;
221+ file_put_contents ($ temp_configfile , $ temp_config );
222+ }
223+ unset($ temp_configfile );
224+ unset($ temp_config );
225+ }
226+
202227 $ key_value ="dkim_key(' " .$ key_domain ."', ' " .$ selector ."', ' " .$ mail_config ['dkim_path ' ]."/ " .$ key_domain .".private'); \n" ;
228+ $ amavis_config = file_get_contents ($ amavis_configfile );
203229 $ amavis_config = preg_replace ("/( \n| \r)?dkim_key.* " .$ key_domain .".*/ " , '' , $ amavis_config ).$ key_value ;
204230
205- if (file_put_contents ($ this -> get_amavis_config () , $ amavis_config )) {
231+ if (file_put_contents ($ amavis_configfile , $ amavis_config )) {
206232 $ app ->log ('Adding DKIM Private-key to amavis-config. ' , LOGLEVEL_DEBUG );
207233 $ restart = true ;
208234 } else {
@@ -220,15 +246,29 @@ function remove_from_amavis($key_domain) {
220246 global $ app ;
221247
222248 $ restart = false ;
223- $ amavis_config = file_get_contents ($ this ->get_amavis_config ());
249+ $ amavis_configfile = $ this ->get_amavis_config ();
250+ $ amavis_config = file_get_contents ($ amavis_configfile );
224251
225252 if (preg_match ("/( \n| \r)?dkim_key.* " .$ key_domain .".*/ " , $ amavis_config )) {
226253 $ amavis_config = preg_replace ("/( \n| \r)?dkim_key.* " .$ key_domain .".*( \n| \r)?/ " , '' , $ amavis_config );
227- file_put_contents ($ this -> get_amavis_config () , $ amavis_config );
254+ file_put_contents ($ amavis_configfile , $ amavis_config );
228255 $ app ->log ('Deleted the DKIM settings from amavis-config for ' .$ key_domain .'. ' , LOGLEVEL_DEBUG );
229256 $ restart = true ;
230257 }
231258
259+ //* If we are using seperate config-files with amavis remove existing keys from 50-user, too
260+ if (substr_compare ($ amavis_configfile , '60-dkim ' , -7 ) === 0 ) {
261+ $ temp_configfile = str_replace ('60-dkim ' , '50-user ' , $ amavis_configfile );
262+ $ temp_config = file_get_contents ($ temp_configfile );
263+ if (preg_match ("/( \n| \r)?dkim_key.* " .$ key_domain .".*/ " , $ temp_config )) {
264+ $ temp_config = preg_replace ("/dkim_key.* " .$ key_domain .".*/ " , '' , $ temp_config );
265+ file_put_contents ($ temp_configfile , $ temp_config );
266+ $ restart = true ;
267+ }
268+ unset($ temp_configfile );
269+ unset($ temp_config );
270+ }
271+
232272 return $ restart ;
233273 }
234274
0 commit comments