Manage permissions of sieve script

This commit is contained in:
Antonio J. Delgado 2024-10-01 11:29:23 +03:00
parent 2aa0068798
commit 42f995f03b

View file

@ -11,6 +11,7 @@ import logging
from logging.handlers import SysLogHandler
import imaplib
import email
from pathlib import Path
from signal import signal, SIGINT
import json
import re
@ -136,6 +137,10 @@ class ImapFilter:
def _create_sieve_script(self, filters):
if os.path.exists(self.config['sieve_scripts_path']):
if os.geteuid() == 0:
file_path = Path(self.config['sieve_scripts_path'])
owner = file_path.owner()
group = file_path.group()
with open(self.config['sieve_scripts_path'], 'r', encoding='UTF-8') as sieve_script:
content = sieve_script.read()
else:
@ -176,6 +181,9 @@ class ImapFilter:
content += f"{replacement}\n"
with open(self.config['sieve_scripts_path'], 'w', encoding='UTF-8') as sieve_script:
sieve_script.write(content)
if os.geteuid() == 0:
os.chown(self.config['sieve_scripts_path'], owner, group)
os.chmod(self.config['sieve_scripts_path'], '0777')
return True
def _process_message(self, message_id, data, mfilter):