policy_module(nsd, 1.8.0)

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

type nsd_t;
type nsd_exec_t;
init_daemon_domain(nsd_t, nsd_exec_t)

# A type for configuration files of nsd
type nsd_conf_t;
files_type(nsd_conf_t)

type nsd_crond_t;
domain_type(nsd_crond_t)
domain_entry_file(nsd_crond_t, nsd_exec_t)
role system_r types nsd_crond_t;

type nsd_log_t;
logging_log_file(nsd_log_t)

type nsd_var_run_t;
files_pid_file(nsd_var_run_t)

# A type for zone files
type nsd_zone_t alias nsd_db_t;
files_type(nsd_zone_t)

type nsd_tmp_t;
files_tmp_file(nsd_tmp_t)

########################################
#
# NSD Local policy
#

allow nsd_t self:capability { chown dac_read_search  dac_override kill setgid setuid net_admin };
dontaudit nsd_t self:capability sys_tty_config;
allow nsd_t self:process signal_perms;
allow nsd_t self:tcp_socket create_stream_socket_perms;
allow nsd_t self:udp_socket create_socket_perms;
allow nsd_t self:fifo_file rw_fifo_file_perms;

manage_dirs_pattern(nsd_t, nsd_conf_t, nsd_conf_t)
manage_files_pattern(nsd_t, nsd_conf_t, nsd_conf_t)
read_lnk_files_pattern(nsd_t, nsd_conf_t, nsd_conf_t)

manage_files_pattern(nsd_t, nsd_var_run_t, nsd_var_run_t)
manage_sock_files_pattern(nsd_t, nsd_var_run_t, nsd_var_run_t)
files_pid_filetrans(nsd_t, nsd_var_run_t, { file sock_file })

manage_files_pattern(nsd_t, nsd_log_t, nsd_log_t)
logging_log_filetrans(nsd_t, nsd_log_t, file)

manage_dirs_pattern(nsd_t, nsd_zone_t, nsd_zone_t)
manage_files_pattern(nsd_t, nsd_zone_t, nsd_zone_t)
manage_lnk_files_pattern(nsd_t, nsd_zone_t, nsd_zone_t)
files_var_lib_filetrans(nsd_t, nsd_zone_t, dir)
allow nsd_t nsd_zone_t:file { map } ;

manage_dirs_pattern(nsd_t, nsd_tmp_t, nsd_tmp_t)
manage_files_pattern(nsd_t, nsd_tmp_t, nsd_tmp_t)
files_tmp_filetrans(nsd_t, nsd_tmp_t, { file dir })
allow nsd_t nsd_tmp_t:file { map } ;

can_exec(nsd_t, nsd_exec_t)

kernel_read_system_state(nsd_t)
kernel_read_kernel_sysctls(nsd_t)

corecmd_exec_bin(nsd_t)

corenet_all_recvfrom_netlabel(nsd_t)
corenet_tcp_sendrecv_generic_if(nsd_t)
corenet_udp_sendrecv_generic_if(nsd_t)
corenet_tcp_sendrecv_generic_node(nsd_t)
corenet_udp_sendrecv_generic_node(nsd_t)
corenet_tcp_sendrecv_all_ports(nsd_t)
corenet_udp_sendrecv_all_ports(nsd_t)
corenet_tcp_bind_generic_node(nsd_t)
corenet_udp_bind_generic_node(nsd_t)
corenet_tcp_bind_dns_port(nsd_t)
corenet_udp_bind_dns_port(nsd_t)
corenet_sendrecv_dns_server_packets(nsd_t)
corenet_tcp_bind_nsd_control_port(nsd_t)
corenet_sendrecv_nsd_control_server_packets(nsd_t)
corenet_tcp_connect_nsd_control_port(nsd_t)

dev_read_sysfs(nsd_t)
dev_read_urand(nsd_t)

domain_use_interactive_fds(nsd_t)

files_read_etc_runtime_files(nsd_t)
files_search_var_lib(nsd_t)

fs_getattr_all_fs(nsd_t)
fs_search_auto_mountpoints(nsd_t)

auth_use_nsswitch(nsd_t)

logging_send_syslog_msg(nsd_t)

userdom_dontaudit_use_unpriv_user_fds(nsd_t)
userdom_dontaudit_search_user_home_dirs(nsd_t)

optional_policy(`
	seutil_sigchld_newrole(nsd_t)
')

optional_policy(`
	udev_read_db(nsd_t)
')

########################################
#
# Zone update cron job local policy
#

# kill capability for root cron job and non-root daemon
allow nsd_crond_t self:capability { dac_read_search  kill };
dontaudit nsd_crond_t self:capability sys_nice;
allow nsd_crond_t self:process { setsched signal_perms };
allow nsd_crond_t self:fifo_file rw_fifo_file_perms;
allow nsd_crond_t self:tcp_socket create_socket_perms;
allow nsd_crond_t self:udp_socket create_socket_perms;

allow nsd_crond_t nsd_conf_t:file read_file_perms;

files_search_var_lib(nsd_crond_t)

allow nsd_crond_t nsd_t:process signal;

ps_process_pattern(nsd_crond_t, nsd_t)

manage_files_pattern(nsd_crond_t, nsd_zone_t, nsd_zone_t)
filetrans_pattern(nsd_crond_t, nsd_conf_t, nsd_zone_t, file)

can_exec(nsd_crond_t, nsd_exec_t)

kernel_read_system_state(nsd_crond_t)

corecmd_exec_bin(nsd_crond_t)
corecmd_exec_shell(nsd_crond_t)

corenet_all_recvfrom_netlabel(nsd_crond_t)
corenet_tcp_sendrecv_generic_if(nsd_crond_t)
corenet_udp_sendrecv_generic_if(nsd_crond_t)
corenet_tcp_sendrecv_generic_node(nsd_crond_t)
corenet_udp_sendrecv_generic_node(nsd_crond_t)
corenet_tcp_sendrecv_all_ports(nsd_crond_t)
corenet_udp_sendrecv_all_ports(nsd_crond_t)
corenet_tcp_connect_all_ports(nsd_crond_t)
corenet_sendrecv_all_client_packets(nsd_crond_t)

dev_read_urand(nsd_crond_t)

domain_dontaudit_read_all_domains_state(nsd_crond_t)

files_read_etc_runtime_files(nsd_crond_t)
files_search_var_lib(nsd_t)

auth_use_nsswitch(nsd_crond_t)

logging_send_syslog_msg(nsd_crond_t)

userdom_dontaudit_search_user_home_dirs(nsd_crond_t)

optional_policy(`
	nsd_read_pid(nsd_crond_t)
')

optional_policy(`
	cron_system_entry(nsd_crond_t, nsd_exec_t)
')
