policy_module(rspamd, 1.0.0)

########################################
#
# Declarations
#

type rspamd_t;
type rspamd_exec_t;
init_daemon_domain(rspamd_t, rspamd_exec_t)

#permissive rspamd_t;

type rspamd_conf_t;
files_type(rspamd_conf_t)

type rspamd_tmpfs_t;
files_tmpfs_file(rspamd_tmpfs_t)

type rspamd_log_t;
logging_log_file(rspamd_log_t)

type rspamd_var_lib_t;
files_type(rspamd_var_lib_t)

type rspamd_var_run_t;
files_pid_file(rspamd_var_run_t)

type rspamd_wrkr_port_t;
corenet_port(rspamd_wrkr_port_t)
#portcon tcp     11333   gen_context(system_u:object_r:rspamd_wrkr_port_t,s0)

type rspamd_cntrllr_port_t;
corenet_port(rspamd_cntrllr_port_t)
#portcon tcp     11334   gen_context(system_u:object_r:rspamd_cntrllr_port_t,s0)

type rspamd_prx_port_t;
corenet_port(rspamd_prx_port_t)
#portcon tcp     11332   gen_context(system_u:object_r:rspamd_prx_port_t,s0)

########################################
#
# rspamd local policy
#
#allow rspamd_t self:capability { chown setgid setuid };
#allow rspamd_t self:process { fork setrlimit signal_perms };
allow rspamd_t self:unix_stream_socket create_stream_socket_perms;

allow rspamd_t self:fifo_file rw_fifo_file_perms;
allow rspamd_t self:capability { chown dac_override dac_read_search kill net_bind_service setgid setuid };
allow rspamd_t self:process { getsched setrlimit signal execmem };
allow rspamd_t self:tcp_socket { listen accept };

corenet_tcp_bind_generic_node(rspamd_t)
corenet_udp_bind_generic_node(rspamd_t)
corenet_tcp_bind_rspamd_wrkr_port(rspamd_t)
corenet_tcp_bind_rspamd_cntrllr_port(rspamd_t)
corenet_tcp_bind_rspamd_prx_port(rspamd_t)
corenet_tcp_connect_http_port(rspamd_t)
corenet_tcp_connect_smtp_port(rspamd_t)
corenet_tcp_connect_redis_port(rspamd_t)

kernel_read_kernel_sysctls(rspamd_t)

allow rspamd_t rspamd_conf_t:file map;
list_dirs_pattern(rspamd_t, rspamd_conf_t, rspamd_conf_t)
read_files_pattern(rspamd_t, rspamd_conf_t, rspamd_conf_t)
read_lnk_files_pattern(rspamd_t, rspamd_conf_t, rspamd_conf_t)

allow rspamd_t rspamd_tmpfs_t:file map;
manage_files_pattern(rspamd_t, rspamd_tmpfs_t, rspamd_tmpfs_t)
fs_tmpfs_filetrans(rspamd_t, rspamd_tmpfs_t, file)

manage_dirs_pattern(rspamd_t, rspamd_log_t, rspamd_log_t)
manage_files_pattern(rspamd_t, rspamd_log_t, rspamd_log_t)
manage_lnk_files_pattern(rspamd_t, rspamd_log_t, rspamd_log_t)
logging_log_filetrans(rspamd_t, rspamd_log_t, { dir file lnk_file })

files_list_var(rspamd_t)
allow rspamd_t rspamd_var_lib_t:file map;
manage_dirs_pattern(rspamd_t, rspamd_var_lib_t, rspamd_var_lib_t)
manage_files_pattern(rspamd_t, rspamd_var_lib_t, rspamd_var_lib_t)
manage_lnk_files_pattern(rspamd_t, rspamd_var_lib_t, rspamd_var_lib_t)
manage_sock_files_pattern(rspamd_t, rspamd_var_lib_t, rspamd_var_lib_t)
files_var_lib_filetrans(rspamd_t, rspamd_var_lib_t, { dir file lnk_file })

manage_dirs_pattern(rspamd_t, rspamd_var_run_t, rspamd_var_run_t)
manage_files_pattern(rspamd_t, rspamd_var_run_t, rspamd_var_run_t)
manage_lnk_files_pattern(rspamd_t, rspamd_var_run_t, rspamd_var_run_t)
files_pid_filetrans(rspamd_t, rspamd_var_run_t, { dir file lnk_file })

userdom_use_user_ptys(rspamd_t)
domain_use_interactive_fds(rspamd_t)

#files_read_etc_files(rspamd_t)
files_read_usr_files(rspamd_t)
files_map_usr_files(rspamd_t)

files_dontaudit_list_var(rspamd_t)

auth_use_nsswitch(rspamd_t)

logging_send_syslog_msg(rspamd_t)

miscfiles_read_localization(rspamd_t)

sysnet_dns_name_resolve(rspamd_t)

optional_policy(`
        gen_require(`
                type exim_t;
        ')

	corenet_tcp_connect_rspamd_wrkr_port(exim_t)

')

optional_policy(`
	clamav_stream_connect(rspamd_t)
')
