policy_module(ctdb, 1.1.0)

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

type ctdbd_t;
type ctdbd_exec_t;
init_daemon_domain(ctdbd_t, ctdbd_exec_t)

type ctdbd_initrc_exec_t;
init_script_file(ctdbd_initrc_exec_t)

type ctdbd_log_t;
logging_log_file(ctdbd_log_t)

type ctdbd_spool_t;
files_type(ctdbd_spool_t)

type ctdbd_tmp_t;
files_tmp_file(ctdbd_tmp_t)

type ctdbd_var_lib_t;
files_type(ctdbd_var_lib_t)

type ctdbd_var_t;
files_type(ctdbd_var_t)

type ctdbd_var_run_t;
files_pid_file(ctdbd_var_run_t)

########################################
#
# Local policy
#

allow ctdbd_t self:capability { chown dac_read_search ipc_lock net_admin net_raw sys_nice sys_resource };
allow ctdbd_t self:capability2 block_suspend;
allow ctdbd_t self:process { setpgid setrlimit signal_perms setsched };
allow ctdbd_t self:fifo_file rw_fifo_file_perms;
allow ctdbd_t self:unix_stream_socket { accept connectto listen };
allow ctdbd_t self:netlink_route_socket r_netlink_socket_perms;
allow ctdbd_t self:packet_socket create_socket_perms;
allow ctdbd_t self:tcp_socket create_stream_socket_perms;
allow ctdbd_t self:udp_socket create_socket_perms;
allow ctdbd_t self:rawip_socket create_socket_perms;
allow ctdbd_t self:netlink_tcpdiag_socket create_socket_perms;

append_files_pattern(ctdbd_t, ctdbd_log_t, ctdbd_log_t)
create_files_pattern(ctdbd_t, ctdbd_log_t, ctdbd_log_t)
setattr_files_pattern(ctdbd_t, ctdbd_log_t, ctdbd_log_t)
logging_log_filetrans(ctdbd_t, ctdbd_log_t, file)

manage_files_pattern(ctdbd_t, ctdbd_tmp_t, ctdbd_tmp_t)
manage_sock_files_pattern(ctdbd_t, ctdbd_tmp_t, ctdbd_tmp_t)
files_tmp_filetrans(ctdbd_t, ctdbd_tmp_t, { file sock_file })

manage_dirs_pattern(ctdbd_t, ctdbd_spool_t, ctdbd_spool_t)
manage_files_pattern(ctdbd_t, ctdbd_spool_t, ctdbd_spool_t)
manage_lnk_files_pattern(ctdbd_t, ctdbd_spool_t, ctdbd_spool_t)
files_spool_filetrans(ctdbd_t, ctdbd_spool_t, dir)

exec_files_pattern(ctdbd_t, ctdbd_var_lib_t, ctdbd_var_lib_t)
manage_dirs_pattern(ctdbd_t, ctdbd_var_lib_t, ctdbd_var_lib_t)
manage_files_pattern(ctdbd_t, ctdbd_var_lib_t, ctdbd_var_lib_t)
files_var_lib_filetrans(ctdbd_t, ctdbd_var_lib_t, dir, "ctdb")

manage_dirs_pattern(ctdbd_t, ctdbd_var_t, ctdbd_var_t)
manage_files_pattern(ctdbd_t, ctdbd_var_t, ctdbd_var_t)
manage_lnk_files_pattern(ctdbd_t, ctdbd_var_t, ctdbd_var_t)
files_var_filetrans(ctdbd_t, ctdbd_var_t, dir, "ctdbd")
files_var_filetrans(ctdbd_t, ctdbd_var_t, dir, "ctdb")
allow ctdbd_t ctdbd_var_t:file map;

manage_dirs_pattern(ctdbd_t, ctdbd_var_run_t, ctdbd_var_run_t)
manage_files_pattern(ctdbd_t, ctdbd_var_run_t, ctdbd_var_run_t)
manage_sock_files_pattern(ctdbd_t, ctdbd_var_run_t, ctdbd_var_run_t)
files_pid_filetrans(ctdbd_t, ctdbd_var_run_t, dir)
allow ctdbd_t ctdbd_var_run_t:file map;

setattr_files_pattern(ctdbd_t, ctdbd_exec_t, ctdbd_exec_t)
write_files_pattern(ctdbd_t, ctdbd_exec_t, ctdbd_exec_t)

can_exec(ctdbd_t, ctdbd_exec_t)

kernel_read_network_state(ctdbd_t)
kernel_read_system_state(ctdbd_t)
kernel_rw_net_sysctls(ctdbd_t)

corenet_all_recvfrom_unlabeled(ctdbd_t)
corenet_all_recvfrom_netlabel(ctdbd_t)
corenet_tcp_sendrecv_generic_if(ctdbd_t)
corenet_tcp_sendrecv_generic_node(ctdbd_t)
corenet_tcp_bind_generic_node(ctdbd_t)
corenet_udp_bind_generic_node(ctdbd_t)

corenet_sendrecv_ctdb_server_packets(ctdbd_t)
corenet_tcp_bind_ctdb_port(ctdbd_t)
corenet_udp_bind_ctdb_port(ctdbd_t)
corenet_tcp_bind_smbd_port(ctdbd_t)
corenet_tcp_bind_all_rpc_ports(ctdbd_t)
corenet_udp_bind_all_rpc_ports(ctdbd_t)
corenet_tcp_connect_ctdb_port(ctdbd_t)
corenet_tcp_sendrecv_ctdb_port(ctdbd_t)
corenet_tcp_connect_gluster_port(ctdbd_t)
corenet_tcp_connect_nfs_port(ctdbd_t)
corenet_tcp_connect_portmap_port(ctdbd_t)

corecmd_exec_bin(ctdbd_t)
corecmd_exec_shell(ctdbd_t)
corecmd_getattr_all_executables(ctdbd_t)

dev_read_sysfs(ctdbd_t)
dev_read_urand(ctdbd_t)

domain_dontaudit_read_all_domains_state(ctdbd_t)

files_search_all_mountpoints(ctdbd_t)

fs_getattr_all_fs(ctdbd_t)

auth_use_nsswitch(ctdbd_t)

logging_send_syslog_msg(ctdbd_t)

miscfiles_read_public_files(ctdbd_t)

userdom_home_manager(ctdbd_t)

optional_policy(`
	consoletype_exec(ctdbd_t)
')

optional_policy(`
	hostname_exec(ctdbd_t)
')

optional_policy(`
	iptables_domtrans(ctdbd_t)
')

optional_policy(`
    rpc_domtrans_rpcd(ctdbd_t)
    rpc_manage_nfs_state_data_dir(ctdbd_t)
    rpc_read_nfs_state_data(ctdbd_t)
')

optional_policy(`
    samba_signull_smbd(ctdbd_t)
	samba_initrc_domtrans(ctdbd_t)
	samba_domtrans_net(ctdbd_t)
	samba_manage_var_dirs(ctdbd_t)
	samba_manage_var_files(ctdbd_t)
	samba_systemctl(ctdbd_t)
')

optional_policy(`
    samba_signull_winbind(ctdbd_t)
    samba_signull_unconfined_net(ctdbd_t)
')

optional_policy(`
	sysnet_domtrans_ifconfig(ctdbd_t)
')
