policy_module(authlogin, 2.5.1)

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

## <desc>
## <p>
## Allow users to login using a radius server
## </p>
## </desc>
gen_tunable(authlogin_radius, false)

## <desc>
## <p>
## Allow users to login using a yubikey OTP server or challenge response mode
## </p>
## </desc>
gen_tunable(authlogin_yubikey, false)

## <desc>
## <p>
## Allow users to resolve user passwd entries directly from ldap rather then using a sssd server
## </p>
## </desc>
gen_tunable(authlogin_nsswitch_use_ldap, false)

attribute can_read_shadow_passwords;
attribute can_write_shadow_passwords;
attribute can_relabelto_shadow_passwords;
attribute polydomain;
attribute nsswitch_domain;
attribute login_pgm;

type auth_cache_t;
logging_log_file(auth_cache_t)

type auth_home_t;
userdom_user_home_content(auth_home_t)

type chkpwd_t, can_read_shadow_passwords;
type chkpwd_exec_t;
typealias chkpwd_t alias { user_chkpwd_t staff_chkpwd_t sysadm_chkpwd_t };
typealias chkpwd_t alias { auditadm_chkpwd_t secadm_chkpwd_t system_chkpwd_t };
application_domain(chkpwd_t, chkpwd_exec_t)
role system_r types chkpwd_t;

type faillog_t;
logging_log_file(faillog_t)
mls_trusted_object(faillog_t)

type lastlog_t;
logging_log_file(lastlog_t)

type login_exec_t;
application_executable_file(login_exec_t)

type pam_console_t;
type pam_console_exec_t;
init_system_domain(pam_console_t, pam_console_exec_t)
role system_r types pam_console_t;

type pam_timestamp_t alias pam_t;
domain_type(pam_timestamp_t)
role system_r types pam_timestamp_t;

type pam_timestamp_exec_t  alias pam_exec_t;
domain_entry_file(pam_timestamp_t, pam_timestamp_exec_t)

type pam_timestamp_tmp_t;
files_tmp_file(pam_timestamp_tmp_t)

type pam_var_console_t;
files_pid_file(pam_var_console_t)

type pam_var_run_t;
files_pid_file(pam_var_run_t)

type shadow_t;
files_auth_file(shadow_t)
neverallow ~can_read_shadow_passwords shadow_t:file read;
neverallow ~can_write_shadow_passwords shadow_t:file { create write };
neverallow ~can_relabelto_shadow_passwords shadow_t:file relabelto;

type passwd_file_t;
files_type(passwd_file_t)

type updpwd_t;
type updpwd_exec_t;
domain_type(updpwd_t)
domain_entry_file(updpwd_t, updpwd_exec_t)
domain_obj_id_change_exemption(updpwd_t)
role system_r types updpwd_t;

type utempter_t;
type utempter_exec_t;
application_domain(utempter_t, utempter_exec_t)

#
# var_auth_t is the type of /var/lib/auth, usually
# used for auth data in pam_able
#
type var_auth_t;
files_type(var_auth_t)

type wtmp_t;
logging_log_file(wtmp_t)

########################################
#
# Check password local policy
#

allow chkpwd_t self:capability { dac_read_search  setuid };
dontaudit chkpwd_t self:capability sys_tty_config;
allow chkpwd_t self:process { getattr signal };

allow chkpwd_t shadow_t:file { read_file_perms map };
files_list_etc(chkpwd_t)

kernel_read_crypto_sysctls(chkpwd_t)
# is_selinux_enabled
kernel_read_system_state(chkpwd_t)

domain_dontaudit_use_interactive_fds(chkpwd_t)

dev_read_rand(chkpwd_t)
dev_read_urand(chkpwd_t)

files_read_etc_files(chkpwd_t)
# for nscd
files_dontaudit_search_var(chkpwd_t)
files_read_usr_symlinks(chkpwd_t)
files_list_tmp(chkpwd_t)
files_map_system_db_files(chkpwd_t)

fs_dontaudit_getattr_xattr_fs(chkpwd_t)

term_dontaudit_use_console(chkpwd_t)
term_dontaudit_use_unallocated_ttys(chkpwd_t)
term_dontaudit_use_generic_ptys(chkpwd_t)
term_dontaudit_use_all_ptys(chkpwd_t)
term_use_usb_ttys(chkpwd_t)

auth_use_nsswitch(chkpwd_t)

logging_send_audit_msgs(chkpwd_t)
logging_send_syslog_msg(chkpwd_t)


seutil_read_config(chkpwd_t)
seutil_dontaudit_use_newrole_fds(chkpwd_t)

userdom_dontaudit_use_user_ttys(chkpwd_t)

ifdef(`distro_ubuntu',`
	optional_policy(`
		unconfined_domain(chkpwd_t)
	')
')

optional_policy(`
	# apache leaks file descriptors
	apache_dontaudit_rw_tcp_sockets(chkpwd_t)
')

optional_policy(`
    	dbus_system_bus_client(chkpwd_t)
')

optional_policy(`
	kerberos_use(chkpwd_t)
')

optional_policy(`
	nis_authenticate(chkpwd_t)
')

optional_policy(`
	systemd_machined_rw_devpts_chr_files(chkpwd_t)
')

########################################
#
# PAM local policy
#

allow pam_timestamp_t self:process ~{ ptrace setcurrent setexec setfscreate setrlimit execmem execstack execheap };
dontaudit pam_timestamp_t self:capability sys_tty_config;

allow pam_timestamp_t self:fd use;
allow pam_timestamp_t self:fifo_file rw_file_perms;
allow pam_timestamp_t self:unix_dgram_socket create_socket_perms;
allow pam_timestamp_t self:unix_stream_socket rw_stream_socket_perms;
allow pam_timestamp_t self:unix_dgram_socket sendto;
allow pam_timestamp_t self:unix_stream_socket connectto;
allow pam_timestamp_t self:shm create_shm_perms;
allow pam_timestamp_t self:sem create_sem_perms;
allow pam_timestamp_t self:msgq create_msgq_perms;
allow pam_timestamp_t self:msg { send receive };

delete_files_pattern(pam_timestamp_t, pam_var_run_t, pam_var_run_t)
read_files_pattern(pam_timestamp_t, pam_var_run_t, pam_var_run_t)
files_list_pids(pam_timestamp_t)

allow pam_timestamp_t pam_timestamp_tmp_t:dir manage_dir_perms;
allow pam_timestamp_t pam_timestamp_tmp_t:file manage_file_perms;
files_tmp_filetrans(pam_timestamp_t, pam_timestamp_tmp_t, { file dir })

auth_use_nsswitch(pam_timestamp_t)

kernel_read_system_state(pam_timestamp_t)

files_read_etc_files(pam_timestamp_t)

fs_search_auto_mountpoints(pam_timestamp_t)


term_use_all_ttys(pam_timestamp_t)
term_use_all_ptys(pam_timestamp_t)

init_dontaudit_rw_utmp(pam_timestamp_t)

logging_send_syslog_msg(pam_timestamp_t)

ifdef(`distro_ubuntu',`
	optional_policy(`
		unconfined_domain(pam_timestamp_t)
	')
')

optional_policy(`
	locallogin_use_fds(pam_timestamp_t)
')

########################################
#
# PAM console local policy
#

allow pam_console_t self:capability { chown fowner fsetid };
dontaudit pam_console_t self:capability sys_tty_config;

allow pam_console_t self:process { sigchld sigkill sigstop signull signal };

# for /var/run/console.lock checking
read_files_pattern(pam_console_t, pam_var_console_t, pam_var_console_t)
read_lnk_files_pattern(pam_console_t, pam_var_console_t, pam_var_console_t)
dontaudit pam_console_t pam_var_console_t:file write;

kernel_read_kernel_sysctls(pam_console_t)
kernel_use_fds(pam_console_t)
# Read /proc/meminfo
kernel_read_system_state(pam_console_t)

dev_read_sysfs(pam_console_t)
dev_getattr_apm_bios_dev(pam_console_t)
dev_setattr_apm_bios_dev(pam_console_t)
dev_getattr_dri_dev(pam_console_t)
dev_setattr_dri_dev(pam_console_t)
dev_getattr_input_dev(pam_console_t)
dev_setattr_input_dev(pam_console_t)
dev_getattr_framebuffer_dev(pam_console_t)
dev_setattr_framebuffer_dev(pam_console_t)
dev_getattr_generic_usb_dev(pam_console_t)
dev_setattr_generic_usb_dev(pam_console_t)
dev_getattr_misc_dev(pam_console_t)
dev_setattr_misc_dev(pam_console_t)
dev_getattr_mouse_dev(pam_console_t)
dev_setattr_mouse_dev(pam_console_t)
dev_getattr_power_mgmt_dev(pam_console_t)
dev_setattr_power_mgmt_dev(pam_console_t)
dev_getattr_printer_dev(pam_console_t)
dev_setattr_printer_dev(pam_console_t)
dev_getattr_scanner_dev(pam_console_t)
dev_setattr_scanner_dev(pam_console_t)
dev_getattr_sound_dev(pam_console_t)
dev_setattr_sound_dev(pam_console_t)
dev_getattr_video_dev(pam_console_t)
dev_setattr_video_dev(pam_console_t)
dev_getattr_xserver_misc_dev(pam_console_t)
dev_setattr_xserver_misc_dev(pam_console_t)
dev_read_urand(pam_console_t)

files_read_etc_files(pam_console_t)
files_search_pids(pam_console_t)
files_list_mnt(pam_console_t)
files_dontaudit_search_isid_type_dirs(pam_console_t)
# read /etc/mtab
files_read_etc_runtime_files(pam_console_t)

fs_list_auto_mountpoints(pam_console_t)
fs_list_noxattr_fs(pam_console_t)
fs_getattr_all_fs(pam_console_t)

mls_file_read_all_levels(pam_console_t)
mls_file_write_all_levels(pam_console_t)

storage_getattr_fixed_disk_dev(pam_console_t)
storage_setattr_fixed_disk_dev(pam_console_t)
storage_getattr_removable_dev(pam_console_t)
storage_setattr_removable_dev(pam_console_t)
storage_getattr_scsi_generic_dev(pam_console_t)
storage_setattr_scsi_generic_dev(pam_console_t)

term_use_console(pam_console_t)
term_use_all_ttys(pam_console_t)
term_use_all_ptys(pam_console_t)
term_setattr_console(pam_console_t)
term_getattr_unallocated_ttys(pam_console_t)
term_setattr_unallocated_ttys(pam_console_t)
term_use_unallocated_ttys(pam_console_t)

auth_use_nsswitch(pam_console_t)

domain_use_interactive_fds(pam_console_t)

init_use_fds(pam_console_t)
init_use_script_ptys(pam_console_t)

logging_send_syslog_msg(pam_console_t)

miscfiles_read_generic_certs(pam_console_t)

seutil_read_file_contexts(pam_console_t)

userdom_dontaudit_use_unpriv_user_fds(pam_console_t)

ifdef(`distro_ubuntu',`
	optional_policy(`
		unconfined_domain(pam_console_t)
	')
')

optional_policy(`
	gpm_getattr_gpmctl(pam_console_t)
	gpm_setattr_gpmctl(pam_console_t)
')

optional_policy(`
	hotplug_use_fds(pam_console_t)
	hotplug_dontaudit_search_config(pam_console_t)
')

optional_policy(`
	seutil_sigchld_newrole(pam_console_t)
')

optional_policy(`
	udev_read_db(pam_console_t)
')

optional_policy(`
	xserver_read_xdm_pid(pam_console_t)
	xserver_dontaudit_write_log(pam_console_t)
')

########################################
#
# updpwd local policy
#

allow updpwd_t self:capability { chown dac_read_search fsetid };
allow updpwd_t self:process setfscreate;
allow updpwd_t self:fifo_file rw_fifo_file_perms;
allow updpwd_t self:unix_stream_socket create_stream_socket_perms;
allow updpwd_t self:unix_dgram_socket create_socket_perms;

kernel_read_system_state(updpwd_t)

dev_read_urand(updpwd_t)

files_manage_etc_files(updpwd_t)
auth_manage_passwd(updpwd_t)

mls_file_read_all_levels(updpwd_t)
mls_file_write_all_levels(updpwd_t)
mls_file_downgrade(updpwd_t)

term_dontaudit_use_console(updpwd_t)
term_dontaudit_use_unallocated_ttys(updpwd_t)

auth_manage_shadow(updpwd_t)
auth_etc_filetrans_shadow(updpwd_t)
auth_use_nsswitch(updpwd_t)

logging_send_syslog_msg(updpwd_t)

userdom_use_inherited_user_terminals(updpwd_t)

ifdef(`distro_ubuntu',`
	optional_policy(`
		unconfined_domain(updpwd_t)
	')
')

########################################
#
# Utempter local policy
#

allow utempter_t self:capability setgid;
allow utempter_t self:unix_stream_socket create_stream_socket_perms;

allow utempter_t wtmp_t:file rw_file_perms;

dev_read_urand(utempter_t)

files_read_etc_files(utempter_t)

term_getattr_all_ttys(utempter_t)
term_getattr_all_ptys(utempter_t)
term_dontaudit_use_all_ttys(utempter_t)
term_dontaudit_use_all_ptys(utempter_t)
term_dontaudit_use_ptmx(utempter_t)

auth_use_nsswitch(utempter_t)

init_rw_utmp(utempter_t)

domain_use_interactive_fds(utempter_t)

logging_search_logs(utempter_t)

userdom_use_inherited_user_terminals(utempter_t)
# Allow utemper to write to /tmp/.xses-*
userdom_write_user_tmp_files(utempter_t)

ifdef(`distro_ubuntu',`
	optional_policy(`
		unconfined_domain(utempter_t)
	')
')

optional_policy(`
	xserver_use_xdm_fds(utempter_t)
	xserver_rw_xdm_pipes(utempter_t)
')

files_filetrans_tmp_named_files(polydomain)

tunable_policy(`polyinstantiation_enabled',`
	files_polyinstantiate_all(polydomain)
')

optional_policy(`
	tunable_policy(`polyinstantiation_enabled',`
		namespace_init_domtrans(polydomain)
	')
')

######################################
#
# nsswitch_domain local policy
#

allow nsswitch_domain self:key manage_key_perms;

auth_read_passwd(nsswitch_domain)
auth_map_passwd(nsswitch_domain)

files_list_var_lib(nsswitch_domain)

# read /etc/nsswitch.conf
files_read_etc_files(nsswitch_domain)

sysnet_dns_name_resolve(nsswitch_domain)

systemd_hostnamed_read_config(nsswitch_domain)


tunable_policy(`authlogin_nsswitch_use_ldap',`
    allow nsswitch_domain self:tcp_socket create_socket_perms;
')

tunable_policy(`authlogin_nsswitch_use_ldap',`
	corenet_tcp_sendrecv_generic_if(nsswitch_domain)
	corenet_tcp_sendrecv_generic_node(nsswitch_domain)
	corenet_tcp_sendrecv_ldap_port(nsswitch_domain)
	corenet_tcp_connect_ldap_port(nsswitch_domain)
	corenet_sendrecv_ldap_client_packets(nsswitch_domain)
')

tunable_policy(`authlogin_nsswitch_use_ldap',`
	# Support for LDAPS
	dev_read_rand(nsswitch_domain)
	# LDAP Configuration using encrypted requires
	dev_read_urand(nsswitch_domain)
	sysnet_read_config(nsswitch_domain)
')

tunable_policy(`authlogin_nsswitch_use_ldap',`
	miscfiles_read_generic_certs(nsswitch_domain)
')

optional_policy(`
	tunable_policy(`authlogin_nsswitch_use_ldap',`
		dirsrv_stream_connect(nsswitch_domain)
	')
')

optional_policy(`
	tunable_policy(`authlogin_nsswitch_use_ldap',`
        ldap_read_certs(nsswitch_domain)
		ldap_stream_connect(nsswitch_domain)
	')
')

optional_policy(`
    dbus_system_bus_client(nsswitch_domain)
')

optional_policy(`
	avahi_stream_connect(nsswitch_domain)
')

optional_policy(`
	likewise_stream_connect_lsassd(nsswitch_domain)
')

# can not wrap nis_use_ypbind or kerberos_use, but they both have booleans you can turn off.
optional_policy(`
	kerberos_use(nsswitch_domain)
')

optional_policy(`
	nis_use_ypbind(nsswitch_domain)
')

optional_policy(`
	nscd_use(nsswitch_domain)
')

optional_policy(`
	nslcd_stream_connect(nsswitch_domain)
')

optional_policy(`
	sssd_stream_connect(nsswitch_domain)
	sssd_read_public_files(nsswitch_domain)
	sssd_read_lib_files(nsswitch_domain)
')

#1134389
userdom_manage_all_users_keys(nsswitch_domain)
optional_policy(`
    sssd_manage_keys(nsswitch_domain)
')

optional_policy(`
    rolekit_manage_keys(nsswitch_domain)
')

optional_policy(`
	samba_stream_connect_winbind(nsswitch_domain)
    samba_stream_connect_nmbd(nsswitch_domain)
	samba_read_var_files(nsswitch_domain)
	samba_dontaudit_write_var_files(nsswitch_domain)
')

optional_policy(`
    virt_read_lib_files(nsswitch_domain)
')

#######################################
#
# Login Program local policy
#

domain_read_all_domains_state(login_pgm)
corecmd_getattr_all_executables(login_pgm)
domain_kill_all_domains(login_pgm)

allow login_pgm self:netlink_kobject_uevent_socket create_socket_perms;
allow login_pgm self:capability ipc_lock;
dontaudit login_pgm self:capability net_admin;
allow login_pgm self:process setkeycreate;
allow login_pgm self:key manage_key_perms;
userdom_manage_all_users_keys(login_pgm)
allow login_pgm nsswitch_domain:key manage_key_perms;

files_list_var_lib(login_pgm)
manage_dirs_pattern(login_pgm, var_auth_t, var_auth_t)
manage_files_pattern(login_pgm, var_auth_t, var_auth_t)
manage_sock_files_pattern(login_pgm, var_auth_t, var_auth_t)

manage_dirs_pattern(login_pgm, auth_cache_t, auth_cache_t)
manage_files_pattern(login_pgm, auth_cache_t, auth_cache_t)
manage_sock_files_pattern(login_pgm, auth_cache_t, auth_cache_t)
files_var_filetrans(login_pgm, auth_cache_t, dir, "coolkey")
allow login_pgm auth_cache_t:file map;

manage_dirs_pattern(login_pgm, auth_home_t, auth_home_t)
manage_files_pattern(login_pgm, auth_home_t, auth_home_t)
auth_filetrans_admin_home_content(login_pgm)
auth_filetrans_home_content(login_pgm)

# needed for afs - https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=253321
kernel_search_network_sysctl(login_pgm)
kernel_rw_afs_state(login_pgm)

tunable_policy(`authlogin_radius',`
	corenet_udp_bind_all_unreserved_ports(login_pgm)
')

tunable_policy(`authlogin_yubikey',`
	corenet_tcp_connect_http_port(login_pgm)
')

corenet_tcp_connect_pki_ca_port(login_pgm)

# for fingerprint readers
dev_rw_input_dev(login_pgm)
dev_rw_generic_usb_dev(login_pgm)

files_read_config_files(login_pgm)

fs_list_auto_mountpoints(login_pgm)
fs_manage_cgroup_dirs(login_pgm)
fs_manage_cgroup_files(login_pgm)
fs_read_ecryptfs_symlinks(login_pgm)
fs_read_ecryptfs_files(login_pgm)

#fs_manage_kdbus_files(login_pgm)
#fs_manage_kdbus_dirs(login_pgm)

selinux_validate_context(login_pgm)
selinux_compute_access_vector(login_pgm)
selinux_compute_create_context(login_pgm)
selinux_compute_relabel_context(login_pgm)
selinux_compute_user_contexts(login_pgm)

auth_manage_faillog(login_pgm)
auth_manage_pam_pid(login_pgm)

init_rw_utmp(login_pgm)

logging_set_loginuid(login_pgm)
logging_set_tty_audit(login_pgm)

miscfiles_dontaudit_write_generic_cert_files(login_pgm)
miscfiles_filetrans_named_content(login_pgm)

seutil_read_config(login_pgm)
seutil_read_login_config(login_pgm)
seutil_read_default_contexts(login_pgm)
systemd_login_read_pid_files(login_pgm)

userdom_set_rlimitnh(login_pgm)
userdom_read_user_home_content_symlinks(login_pgm)
userdom_delete_user_tmp_files(login_pgm)
userdom_search_admin_dir(login_pgm)
userdom_stream_connect(login_pgm)
userdom_manage_user_tmp_dirs(login_pgm)
userdom_manage_user_tmp_files(login_pgm)

optional_policy(`
	afs_read_config(login_pgm)
	afs_rw_udp_sockets(login_pgm)
')

optional_policy(`
	kerberos_read_config(login_pgm)
')

optional_policy(`
	oddjob_dbus_chat(login_pgm)
	oddjob_domtrans_mkhomedir(login_pgm)
')

optional_policy(`
	openct_stream_connect(login_pgm)
	openct_signull(login_pgm)
	openct_read_pid_files(login_pgm)
')

optional_policy(`
	corecmd_exec_bin(login_pgm)
	storage_getattr_fixed_disk_dev(login_pgm)
	mount_domtrans(login_pgm)
	mount_domtrans_ecryptmount(login_pgm)
')

optional_policy(`
	fprintd_dbus_chat(login_pgm)
')

optional_policy(`
	realmd_dbus_chat(login_pgm)
')

optional_policy(`
	# allow execute tmux
	screen_exec(login_pgm)
')

optional_policy(`
	ssh_agent_exec(login_pgm)
	ssh_read_user_home_files(login_pgm)
')
