2929 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
3131 @author Florian Schaal, info@schaal-24.de
32+ @author Marius Burkard, m.burkard@ispconfig.org (modified for rspamd)
3233 @copyright Florian Schaal, info@schaal-24.de
3334 */
3435
@@ -59,8 +60,8 @@ function onInstall() {
5960 /**
6061 * This function is called when the plugin is loaded
6162 */
62- function onLoad () {
63- global $ app, $ conf ;
63+ public function onLoad () {
64+ global $ app ;
6465 /*
6566 Register for the events
6667 */
@@ -73,7 +74,7 @@ function onLoad() {
7374 * This function gets the amavisd-config file
7475 * @return string path to the amavisd-config for dkim-keys
7576 */
76- function get_amavis_config () {
77+ private function get_amavis_config () {
7778 $ pos_config =array (
7879 '/etc/amavisd.conf/50-user ' ,
7980 '/etc/amavis/conf.d/50-user ' ,
@@ -101,45 +102,59 @@ function get_amavis_config() {
101102 * @param array $data mail-settings
102103 * @return boolean - true when the amavis-config and the dkim-dir are writeable
103104 */
104- function check_system ($ data ) {
105- global $ app , $ mail_config ;
105+ private function check_system () {
106+ global $ app , $ conf , $ mail_config ;
106107
107- /** TODO: FIX IF ONLY RSPAMD IS INSTALLED AND NO AMAVIS! **/
108- /** TODO: FIX DKIM FOR RSPAMD, RSPAMD CANNOT READ FILES OF amavis:root **/
109-
110108 $ app ->uses ('getconf ' );
111- $ check =true ;
112-
113- /* check for amavis-config */
114- $ amavis_configfile = $ this ->get_amavis_config ();
109+ $ check = true ;
115110
116- //* Create the file if it does not exists.
117- if (substr_compare ($ amavis_configfile , '60-dkim ' , -7 ) === 0 && !file_exists ($ amavis_configfile ))
118- $ app ->system ->touch ($ amavis_configfile );
111+ $ mail_config = $ app ->getconf ->get_server_config ($ conf ['server_id ' ], 'mail ' );
112+ if ($ mail_config ['content_filter ' ] != 'rspamd ' ) {
113+ /* check for amavis-config */
114+ $ amavis_configfile = $ this ->get_amavis_config ();
119115
120- if ( $ amavis_configfile == '' || !is_writeable ($ amavis_configfile ) ) {
121- $ app ->log ('Amavis-config not found or not writeable. ' , LOGLEVEL_ERROR );
122- $ check =false ;
116+ //* Create the file if it does not exists.
117+ if (substr_compare ($ amavis_configfile , '60-dkim ' , -7 ) === 0 && !file_exists ($ amavis_configfile )) {
118+ $ app ->system ->touch ($ amavis_configfile );
119+ }
120+
121+ if ( $ amavis_configfile == '' || !is_writeable ($ amavis_configfile ) ) {
122+ $ app ->log ('Amavis-config not found or not writeable. ' , LOGLEVEL_ERROR );
123+ $ check = false ;
124+ }
123125 }
124126
125127 /* dir for dkim-keys writeable? */
126- $ mail_config = $ app ->getconf ->get_server_config ($ conf ['server_id ' ], 'mail ' );
127128 if (isset ($ mail_config ['dkim_path ' ]) && !empty ($ mail_config ['dkim_path ' ]) && $ mail_config ['dkim_path ' ] != '/ ' ) {
128129 if (!is_dir ($ mail_config ['dkim_path ' ])) {
129130 $ app ->log ('DKIM Path ' .$ mail_config ['dkim_path ' ].' not found - (re)created. ' , LOGLEVEL_DEBUG );
130131 if ($ app ->system ->is_user ('amavis ' )) {
131132 $ amavis_user ='amavis ' ;
133+ } elseif ($ app ->system ->is_user ('_rspamd ' )) {
134+ $ amavis_user = '_rspamd ' ;
135+ } elseif ($ app ->system ->is_user ('rspamd ' )) {
136+ $ amavis_user = 'rspamd ' ;
132137 } elseif ($ app ->system ->is_user ('vscan ' )) {
133- $ amavis_user ='vscan ' ;
138+ $ amavis_user = 'vscan ' ;
139+ } else {
140+ $ amavis_user = '' ;
134141 }
135- else {
136- $ amavis_user ='' ;
142+ if ($ app ->system ->is_user ('amavis ' )) {
143+ $ amavis_group ='amavis ' ;
144+ } elseif ($ app ->system ->is_user ('_rspamd ' )) {
145+ $ amavis_group = '_rspamd ' ;
146+ } elseif ($ app ->system ->is_user ('rspamd ' )) {
147+ $ amavis_group = 'rspamd ' ;
148+ } elseif ($ app ->system ->is_user ('vscan ' )) {
149+ $ amavis_group = 'vscan ' ;
150+ } else {
151+ $ amavis_group = '' ;
137152 }
153+
138154 if (!empty ($ amavis_user )) {
139- mkdir ($ mail_config ['dkim_path ' ], 0750 , true );
140- $ app ->system ->chown ($ mail_config ['dkim_path ' ], $ amavis_user );
155+ $ app ->system ->mkdirpath ($ mail_config ['dkim_path ' ], 0750 , $ amavis_user , $ amavis_group );
141156 } else {
142- mkdir ($ mail_config ['dkim_path ' ], 0755 , true );
157+ $ app -> system -> mkdirpath ($ mail_config ['dkim_path ' ], 0755 );
143158 $ app ->log ('No user amavis or vscan found - using root for ' .$ mail_config ['dkim_path ' ], LOGLEVEL_WARNING );
144159 }
145160 } else {
@@ -169,12 +184,15 @@ function check_system($data) {
169184 /**
170185 * This function restarts amavis
171186 */
172- function restart_amavis () {
187+ private function restart_amavis () {
173188 global $ app ;
189+ $ output = null ;
174190 $ initcommand = $ app ->system ->getinitcommand (array ('amavis ' , 'amavisd ' ), 'restart ' );
175191 $ app ->log ('Restarting amavis: ' .$ initcommand .'. ' , LOGLEVEL_DEBUG );
176192 exec ($ initcommand , $ output );
177- foreach ($ output as $ logline ) $ app ->log ($ logline , LOGLEVEL_DEBUG );
193+ foreach ($ output as $ logline ) {
194+ $ app ->log ($ logline , LOGLEVEL_DEBUG );
195+ }
178196 }
179197
180198 /**
@@ -184,8 +202,8 @@ function restart_amavis() {
184202 * @param string $key_domain mail-domain
185203 * @return bool - true when the private key was written to disk
186204 */
187- function write_dkim_key ($ key_file , $ key_value , $ key_domain ) {
188- global $ app, $ mailconfig ;
205+ private function write_dkim_key ($ key_file , $ key_value , $ key_domain ) {
206+ global $ app ;
189207 $ success =false ;
190208 if ($ key_file == '' || $ key_value == '' || $ key_domain == '' ) {
191209 $ app ->log ('DKIM internal error for domain ' .$ key_domain , LOGLEVEL_ERROR );
@@ -194,14 +212,20 @@ function write_dkim_key($key_file, $key_value, $key_domain) {
194212 if ( $ app ->system ->file_put_contents ($ key_file .'.private ' , $ key_value ) ) {
195213 $ app ->log ('Saved DKIM Private-key to ' .$ key_file .'.private ' , LOGLEVEL_DEBUG );
196214 $ success =true ;
215+ $ pubkey = null ;
216+ $ result = 0 ;
197217 /* now we get the DKIM Public-key */
198218 exec ('cat ' .escapeshellarg ($ key_file .'.private ' ).'|openssl rsa -pubout 2> /dev/null ' , $ pubkey , $ result );
199219 $ public_key ='' ;
200- foreach ($ pubkey as $ values ) $ public_key =$ public_key .$ values ."\n" ;
220+ foreach ($ pubkey as $ values ) {
221+ $ public_key = $ public_key . $ values . "\n" ;
222+ }
201223 /* save the DKIM Public-key in dkim-dir */
202- if ( $ app ->system ->file_put_contents ($ key_file .'.public ' , $ public_key ) )
224+ if ( $ app ->system ->file_put_contents ($ key_file .'.public ' , $ public_key )) {
203225 $ app ->log ('Saved DKIM Public to ' .$ key_domain .'. ' , LOGLEVEL_DEBUG );
204- else $ app ->log ('Unable to save DKIM Public to ' .$ key_domain .'. ' , LOGLEVEL_DEBUG );
226+ } else {
227+ $ app ->log ('Unable to save DKIM Public to ' .$ key_domain .'. ' , LOGLEVEL_DEBUG );
228+ }
205229 } else {
206230 $ app ->log ('Unable to save DKIM Private-key to ' .$ key_file .'.private ' , LOGLEVEL_ERROR );
207231 }
@@ -213,26 +237,32 @@ function write_dkim_key($key_file, $key_value, $key_domain) {
213237 * @param string $key_file full path to the key-file
214238 * @param string $key_domain mail-domain
215239 */
216- function remove_dkim_key ($ key_file , $ key_domain ) {
240+ private function remove_dkim_key ($ key_file , $ key_domain ) {
217241 global $ app ;
218242 if (file_exists ($ key_file .'.private ' )) {
219243 $ app ->system ->unlink ($ key_file .'.private ' );
220244 $ app ->log ('Deleted the DKIM Private-key for ' .$ key_domain .'. ' , LOGLEVEL_DEBUG );
221- } else $ app ->log ('Unable to delete the DKIM Private-key for ' .$ key_domain .' (not found). ' , LOGLEVEL_DEBUG );
245+ } else {
246+ $ app ->log ('Unable to delete the DKIM Private-key for ' .$ key_domain .' (not found). ' , LOGLEVEL_DEBUG );
247+ }
222248 if (file_exists ($ key_file .'.public ' )) {
223249 $ app ->system ->unlink ($ key_file .'.public ' );
224250 $ app ->log ('Deleted the DKIM Public-key for ' .$ key_domain .'. ' , LOGLEVEL_DEBUG );
225- } else $ app ->log ('Unable to delete the DKIM Public-key for ' .$ key_domain .' (not found). ' , LOGLEVEL_DEBUG );
251+ } else {
252+ $ app ->log ('Unable to delete the DKIM Public-key for ' .$ key_domain .' (not found). ' , LOGLEVEL_DEBUG );
253+ }
226254 }
227255
228256 /**
229257 * This function adds the entry to the amavisd-config
230258 * @param string $key_domain mail-domain
231259 */
232- function add_to_amavis ($ key_domain , $ selector , $ old_selector ) {
260+ private function add_to_amavis ($ key_domain , $ selector , $ old_selector ) {
233261 global $ app , $ mail_config ;
234262
235- if (empty ($ selector )) $ selector = 'default ' ;
263+ if (empty ($ selector )) {
264+ $ selector = 'default ' ;
265+ }
236266 $ restart = false ;
237267 $ amavis_configfile = $ this ->get_amavis_config ();
238268
@@ -270,7 +300,7 @@ function add_to_amavis($key_domain, $selector, $old_selector) {
270300 * This function removes the entry from the amavisd-config
271301 * @param string $key_domain mail-domain
272302 */
273- function remove_from_amavis ($ key_domain ) {
303+ private function remove_from_amavis ($ key_domain ) {
274304 global $ app ;
275305
276306 $ restart = false ;
@@ -308,14 +338,17 @@ function remove_from_amavis($key_domain) {
308338 * This function controlls new key-files and amavisd-entries
309339 * @param array $data mail-settings
310340 */
311- function add_dkim ($ data ) {
312- global $ app ;
341+ private function add_dkim ($ data ) {
342+ global $ app, $ conf ;
313343 if ($ data ['new ' ]['active ' ] == 'y ' ) {
314344 $ mail_config = $ app ->getconf ->get_server_config ($ conf ['server_id ' ], 'mail ' );
315- if ( substr ($ mail_config ['dkim_path ' ], strlen ($ mail_config ['dkim_path ' ])-1 ) == '/ ' )
345+ if ( substr ($ mail_config ['dkim_path ' ], strlen ($ mail_config ['dkim_path ' ])-1 ) == '/ ' ) {
316346 $ mail_config ['dkim_path ' ] = substr ($ mail_config ['dkim_path ' ], 0 , strlen ($ mail_config ['dkim_path ' ])-1 );
347+ }
317348 if ($ this ->write_dkim_key ($ mail_config ['dkim_path ' ]."/ " .$ data ['new ' ]['domain ' ], $ data ['new ' ]['dkim_private ' ], $ data ['new ' ]['domain ' ])) {
318- if ($ this ->add_to_amavis ($ data ['new ' ]['domain ' ], $ data ['new ' ]['dkim_selector ' ], $ data ['old ' ]['dkim_selector ' ] )) {
349+ if ($ mail_config ['content_filter ' ] == 'rspamd ' ) {
350+ $ app ->services ->restartServiceDelayed ('rspamd ' , 'reload ' );
351+ } elseif ($ this ->add_to_amavis ($ data ['new ' ]['domain ' ], $ data ['new ' ]['dkim_selector ' ], $ data ['old ' ]['dkim_selector ' ] )) {
319352 $ this ->restart_amavis ();
320353 } else {
321354 $ this ->remove_dkim_key ($ mail_config ['dkim_path ' ]."/ " .$ data ['new ' ]['domain ' ], $ data ['new ' ]['domain ' ]);
@@ -329,86 +362,89 @@ function add_dkim($data) {
329362 /**
330363 * This function controlls the removement of keyfiles (public and private)
331364 * and the entry in the amavisd-config
332- * @param array $data mail-settings
365+ * @param array $_data mail-settings
333366 */
334- function remove_dkim ($ _data ) {
335- global $ app ;
367+ private function remove_dkim ($ _data ) {
368+ global $ app, $ conf ;
336369 $ mail_config = $ app ->getconf ->get_server_config ($ conf ['server_id ' ], 'mail ' );
337- if ( substr ($ mail_config ['dkim_path ' ], strlen ($ mail_config ['dkim_path ' ])-1 ) == '/ ' )
370+ if ( substr ($ mail_config ['dkim_path ' ], strlen ($ mail_config ['dkim_path ' ])-1 ) == '/ ' ) {
338371 $ mail_config ['dkim_path ' ] = substr ($ mail_config ['dkim_path ' ], 0 , strlen ($ mail_config ['dkim_path ' ])-1 );
372+ }
339373 $ this ->remove_dkim_key ($ mail_config ['dkim_path ' ]."/ " .$ _data ['domain ' ], $ _data ['domain ' ]);
340- if ($ this ->remove_from_amavis ($ _data ['domain ' ]))
374+
375+ if ($ mail_config ['content_filter ' ] == 'rspamd ' ) {
376+ $ app ->services ->restartServiceDelayed ('rspamd ' , 'reload ' );
377+ } elseif ($ this ->remove_from_amavis ($ _data ['domain ' ])) {
341378 $ this ->restart_amavis ();
379+ }
342380 }
343381
344382 /**
345383 * Function called by onLoad
346384 * deletes dkim-keys
347385 */
348- function domain_dkim_delete ($ event_name , $ data ) {
349- if (isset ($ data ['old ' ]['dkim ' ]) && $ data ['old ' ]['dkim ' ] == 'y ' && $ data ['old ' ]['active ' ] == 'y ' )
386+ public function domain_dkim_delete ($ event_name , $ data ) {
387+ if (isset ($ data ['old ' ]['dkim ' ]) && $ data ['old ' ]['dkim ' ] == 'y ' && $ data ['old ' ]['active ' ] == 'y ' ) {
350388 $ this ->remove_dkim ($ data ['old ' ]);
389+ }
351390 }
352391
353392 /**
354393 * Function called by onLoad
355394 * insert dkim-keys
356395 */
357- function domain_dkim_insert ($ event_name , $ data ) {
358- if (isset ($ data ['new ' ]['dkim ' ]) && $ data ['new ' ]['dkim ' ]=='y ' && $ this ->check_system ($ data ))
396+ public function domain_dkim_insert ($ event_name , $ data ) {
397+ if (isset ($ data ['new ' ]['dkim ' ]) && $ data ['new ' ]['dkim ' ]=='y ' && $ this ->check_system ()) {
359398 $ this ->add_dkim ($ data );
399+ }
360400 }
361401
362402 /**
363403 * Function called by onLoad
364404 * chang dkim-settings
365405 */
366- function domain_dkim_update ($ event_name , $ data ) {
406+ public function domain_dkim_update ($ event_name , $ data ) {
367407 global $ app ;
368408 if ($ data ['new ' ]['dkim ' ] == 'y ' || $ data ['old ' ]['dkim ' ] == 'y ' ){
369- if ($ this ->check_system ($ data )) {
409+ if ($ this ->check_system ()) {
370410 /* maildomain disabled */
371411 if ($ data ['new ' ]['active ' ] == 'n ' && $ data ['old ' ]['active ' ] == 'y ' && $ data ['new ' ]['dkim ' ]=='y ' ) {
372412 $ app ->log ('Maildomain ' .$ data ['new ' ]['domain ' ].' disabled - remove DKIM-settings ' , LOGLEVEL_DEBUG );
373413 $ this ->remove_dkim ($ data ['new ' ]);
374414 }
375415 /* maildomain re-enabled */
376- if ($ data ['new ' ]['active ' ] == 'y ' && $ data ['old ' ]['active ' ] == 'n ' && $ data ['new ' ]['dkim ' ]=='y ' )
416+ if ($ data ['new ' ]['active ' ] == 'y ' && $ data ['old ' ]['active ' ] == 'n ' && $ data ['new ' ]['dkim ' ]=='y ' ) {
377417 $ this ->add_dkim ($ data );
378-
418+ }
419+
379420 /* maildomain active - only dkim changes */
380421 if ($ data ['new ' ]['active ' ] == 'y ' && $ data ['old ' ]['active ' ] == 'y ' ) {
381422 /* dkim disabled */
382423 if ($ data ['new ' ]['dkim ' ] != $ data ['old ' ]['dkim ' ] && $ data ['new ' ]['dkim ' ] == 'n ' ) {
383424 $ this ->remove_dkim ($ data ['new ' ]);
384425 }
385- /* dkim enabled */
386- elseif ($ data ['new ' ]['dkim ' ] != $ data ['old ' ]['dkim ' ] && $ data ['new ' ]['dkim ' ] == 'y ' ) {
387- $ this ->add_dkim ($ data );
388- }
389- /* new private-key */
390- if ($ data ['new ' ]['dkim_private ' ] != $ data ['old ' ]['dkim_private ' ] && $ data ['new ' ]['dkim ' ] == 'y ' ) {
391- $ this ->add_dkim ($ data );
392- }
393- /* new selector */
394- if ($ data ['new ' ]['dkim_selector ' ] != $ data ['old ' ]['dkim_selector ' ] && $ data ['new ' ]['dkim ' ] == 'y ' ) {
426+ /* dkim enabled
427+ * or new private-key
428+ * or new selector
429+ * or new domain-name
430+ */
431+ elseif (
432+ ($ data ['new ' ]['dkim ' ] != $ data ['old ' ]['dkim ' ] && $ data ['new ' ]['dkim ' ] == 'y ' )
433+ || ($ data ['new ' ]['dkim_private ' ] != $ data ['old ' ]['dkim_private ' ] && $ data ['new ' ]['dkim ' ] == 'y ' )
434+ || ($ data ['new ' ]['dkim_selector ' ] != $ data ['old ' ]['dkim_selector ' ] && $ data ['new ' ]['dkim ' ] == 'y ' )
435+ || ($ data ['new ' ]['domain ' ] != $ data ['old ' ]['domain ' ])
436+ ) {
437+ if ($ data ['new ' ]['domain ' ] != $ data ['old ' ]['domain ' ]) {
438+ $ this ->remove_dkim ($ data ['old ' ]);
439+ }
395440 $ this ->add_dkim ($ data );
396441 }
397- /* new domain-name */
398- if ($ data ['new ' ]['domain ' ] != $ data ['old ' ]['domain ' ]) {
399- $ this ->remove_dkim ($ data ['old ' ]);
442+ /* resync */
443+ elseif ($ data ['new ' ] == $ data ['old ' ] && $ data ['new ' ]['dkim ' ]=='y ' ) {
400444 $ this ->add_dkim ($ data );
401445 }
402446 }
403-
404- /* resync */
405- if ($ data ['new ' ]['active ' ] == 'y ' && $ data ['new ' ] == $ data ['old ' ] && $ data ['new ' ]['dkim ' ]=='y ' ) {
406- $ this ->add_dkim ($ data );
407- }
408447 }
409448 }
410449 }
411-
412450}
413-
414- ?>
0 commit comments