policy_module(nut, 1.3.0)

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

attribute nut_domain;

nut_domain_template(upsd)
nut_domain_template(upsmon)
nut_domain_template(upsdrvctl)

type nut_conf_t;
files_config_file(nut_conf_t)

type nut_var_run_t;
files_pid_file(nut_var_run_t)

type nut_unit_file_t;
systemd_unit_file(nut_unit_file_t)

#######################################
#
# Local policy for upsd
#

allow nut_domain self:capability { setgid setuid dac_read_search  };

allow nut_domain self:process signal_perms;

allow nut_domain self:fifo_file rw_fifo_file_perms;
allow nut_domain self:netlink_kobject_uevent_socket create_socket_perms;

# pid file
manage_files_pattern(nut_domain, nut_var_run_t, nut_var_run_t)
manage_dirs_pattern(nut_domain, nut_var_run_t, nut_var_run_t)
manage_sock_files_pattern(nut_domain, nut_var_run_t, nut_var_run_t)
files_pid_filetrans(nut_domain, nut_var_run_t, dir)

########################################
#
# Local policy for upsd
#

allow nut_upsd_t self:unix_dgram_socket { create_socket_perms sendto };
allow nut_upsd_t self:tcp_socket connected_stream_socket_perms;

allow nut_upsd_t nut_upsdrvctl_t:unix_stream_socket connectto;

read_files_pattern(nut_upsd_t, nut_conf_t, nut_conf_t)

kernel_read_kernel_sysctls(nut_upsd_t)

corenet_tcp_bind_ups_port(nut_upsd_t)
corenet_tcp_bind_generic_port(nut_upsd_t)
corenet_tcp_bind_all_nodes(nut_upsd_t)

optional_policy(`
	unconfined_stream_connect(nut_upsd_t)
')

########################################
#
# Local policy for upsmon
#

allow nut_upsmon_t self:capability kill;
allow nut_upsmon_t self:tcp_socket create_socket_perms;
allow nut_upsmon_t self:unix_dgram_socket { create_socket_perms sendto };
allow nut_upsmon_t self:unix_stream_socket { create_socket_perms connectto };

read_files_pattern(nut_upsmon_t, nut_conf_t, nut_conf_t)

kernel_read_kernel_sysctls(nut_upsmon_t)
kernel_read_system_state(nut_upsmon_t)

corecmd_exec_bin(nut_upsmon_t)
corecmd_exec_shell(nut_upsmon_t)

corenet_tcp_connect_ups_port(nut_upsmon_t)
corenet_tcp_connect_generic_port(nut_upsmon_t)

dev_read_rand(nut_upsmon_t)
dev_read_urand(nut_upsmon_t)

# Creates /etc/killpower
files_manage_etc_runtime_files(nut_upsmon_t)
files_etc_filetrans_etc_runtime(nut_upsmon_t, file)
files_search_usr(nut_upsmon_t)

# /usr/bin/wall
term_write_all_terms(nut_upsmon_t)

# upsmon runs shutdown, probably need a shutdown domain
init_rw_utmp(nut_upsmon_t)
init_telinit(nut_upsmon_t)


mta_send_mail(nut_upsmon_t)

systemd_start_power_services(nut_upsmon_t)

optional_policy(`
	shutdown_domtrans(nut_upsmon_t)
')

optional_policy(`
	dbus_system_bus_client(nut_upsmon_t)
	systemd_dbus_chat_logind(nut_upsmon_t)
')

########################################
#
# Local policy for upsdrvctl
#

allow nut_upsdrvctl_t self:capability { kill };
allow nut_upsdrvctl_t self:fd use;
allow nut_upsdrvctl_t self:unix_dgram_socket { create_socket_perms sendto };
allow nut_upsdrvctl_t self:udp_socket create_socket_perms;

can_exec(nut_upsdrvctl_t, nut_upsdrvctl_exec_t)

read_files_pattern(nut_upsdrvctl_t, nut_conf_t, nut_conf_t)

kernel_read_kernel_sysctls(nut_upsdrvctl_t)

# /sbin/upsdrvctl executes other drivers
corecmd_exec_bin(nut_upsdrvctl_t)

dev_read_sysfs(nut_upsdrvctl_t)
dev_read_usbfs(nut_upsdrvctl_t)
dev_rw_generic_usb_dev(nut_upsdrvctl_t)

term_use_unallocated_ttys(nut_upsdrvctl_t)
term_use_usb_ttys(nut_upsdrvctl_t)

init_sigchld(nut_upsdrvctl_t)

udev_read_db(nut_upsdrvctl_t)

optional_policy(`
	unconfined_signal(nut_upsdrvctl_t)
')

#######################################
#
# Local policy for upscgi scripts
# requires httpd_enable_cgi and httpd_can_network_connect
#

optional_policy(`
	apache_content_template(nutups_cgi)
	apache_content_alias_template(nutups_cgi,nutups_cgi)

	read_files_pattern(nutups_cgi_script_t, nut_conf_t, nut_conf_t)

	corenet_all_recvfrom_netlabel(nutups_cgi_script_t)
	corenet_tcp_sendrecv_generic_if(nutups_cgi_script_t)
	corenet_tcp_sendrecv_generic_node(nutups_cgi_script_t)
	corenet_tcp_sendrecv_all_ports(nutups_cgi_script_t)
	corenet_tcp_connect_ups_port(nutups_cgi_script_t)
	corenet_udp_sendrecv_generic_if(nutups_cgi_script_t)
	corenet_udp_sendrecv_generic_node(nutups_cgi_script_t)
	corenet_udp_sendrecv_all_ports(nutups_cgi_script_t)

	sysnet_dns_name_resolve(nutups_cgi_script_t)
')
