Blob Blame History Raw
########################################
#
# Rules and Targets for building monolithic policies
#

POLICY_CONF = $(BUILDDIR)policy.conf
FC = $(BUILDDIR)file_contexts
POLVER = $(BUILDDIR)policy.$(PV)
HOMEDIR_TEMPLATE = $(BUILDDIR)homedir_template

M4PARAM += -D self_contained_policy

# install paths
POLICYPATH = $(INSTALLDIR)/policy
LOADPATH = $(POLICYPATH)/$(notdir $(POLVER))
HOMEDIRPATH = $(CONTEXTPATH)/files/homedir_template

APPFILES += $(INSTALLDIR)/booleans $(USERPATH)/local.users

# for monolithic policy use all base and module to create policy
ALL_MODULES := $(strip $(BASE_MODS) $(MOD_MODS))
# off module interfaces included to make sure all interfaces are expanded.
ALL_INTERFACES := $(ALL_MODULES:.te=.if) $(OFF_MODS:.te=.if)
ALL_TE_FILES := $(ALL_MODULES)
ALL_FC_FILES := $(ALL_MODULES:.te=.fc)

PRE_TE_FILES := $(SECCLASS) $(ISIDS) $(AVS) $(M4SUPPORT) $(POLDIR)/mls $(POLDIR)/mcs
POST_TE_FILES := $(USER_FILES) $(POLDIR)/constraints

POLICY_SECTIONS := $(TMPDIR)/pre_te_files.conf $(TMPDIR)/all_attrs_types.conf $(TMPDIR)/global_bools.conf $(TMPDIR)/only_te_rules.conf $(TMPDIR)/all_post.conf

# search layer dirs for source files
vpath %.te $(ALL_LAYERS)
vpath %.if $(ALL_LAYERS)
vpath %.fc $(ALL_LAYERS)

########################################
#
# default action: build policy locally
#
default: policy

policy: $(POLVER)

install: $(LOADPATH) $(FCPATH) $(NCPATH) $(APPFILES)

load: $(TMPDIR)/load

checklabels: $(FCPATH)
restorelabels: $(FCPATH)
relabel:  $(FCPATH)
resetlabels:  $(FCPATH)

########################################
#
# Build a binary policy locally
#
$(POLVER): $(POLICY_CONF)
	@echo "Compiling $(NAME) $(POLVER)"
ifneq ($(PV),$(KV))
	@echo
	@echo "WARNING: Policy version mismatch!  Is your OUTPUT_POLICY set correctly?"
	@echo
endif
	$(verbose) $(CHECKPOLICY) $^ -o $@

########################################
#
# Install a binary policy
#
$(LOADPATH): $(POLICY_CONF)
	@mkdir -p $(POLICYPATH)
	@echo "Compiling and installing $(NAME) $(LOADPATH)"
ifneq ($(PV),$(KV))
	@echo
	@echo "WARNING: Policy version mismatch!  Is your OUTPUT_POLICY set correctly?"
	@echo
endif
	$(verbose) $(CHECKPOLICY) $^ -o $@

########################################
#
# Load the binary policy
#
reload $(TMPDIR)/load: $(LOADPATH) $(FCPATH) $(NCPATH) $(APPFILES)
	@echo "Loading $(NAME) $(LOADPATH)"
	$(verbose) $(LOADPOLICY) -q $(LOADPATH)
	@touch $(TMPDIR)/load

########################################
#
# Construct a monolithic policy.conf
#
$(POLICY_CONF): $(POLICY_SECTIONS)
	@echo "Creating $(NAME) $(@F)"
	@test -d $(@D) || mkdir -p $(@D)
	$(verbose) cat $^ > $@

$(TMPDIR)/pre_te_files.conf: $(PRE_TE_FILES)
	@test -d $(TMPDIR) || mkdir -p $(TMPDIR)
	$(verbose) $(M4) $(M4PARAM) $^ > $@

$(TMPDIR)/generated_definitions.conf: $(ALL_TE_FILES)
	@test -d $(TMPDIR) || mkdir -p $(TMPDIR)
# define all available object classes
	$(verbose) $(GENPERM) $(AVS) $(SECCLASS) > $@
# per-userdomain templates:
	$(verbose) echo "define(\`base_per_userdomain_template',\`" >> $@
	$(verbose) $(foreach mod,$(basename $(notdir $(ALL_MODULES))), \
		echo "ifdef(\`""$(mod)""_per_userdomain_template',\`""$(mod)""_per_userdomain_template("'$$*'")')" >> $@ ;)
	$(verbose) echo "')" >> $@
	$(verbose) test -f $(BOOLEANS) && $(SETBOOLS) $(BOOLEANS) >> $@ || true

$(TMPDIR)/global_bools.conf: $(M4SUPPORT) $(TMPDIR)/generated_definitions.conf $(GLOBALBOOL) $(GLOBALTUN)
	$(verbose) $(M4) $(M4PARAM) $^ > $@

$(TMPDIR)/all_interfaces.conf: $(M4SUPPORT) $(ALL_INTERFACES)
	@test -d $(TMPDIR) || mkdir -p $(TMPDIR)
	@echo "ifdef(\`__if_error',\`m4exit(1)')" > $(TMPDIR)/iferror.m4
	@echo "divert(-1)" > $@
	$(verbose) $(M4) $^ $(TMPDIR)/iferror.m4 >> $(TMPDIR)/$(@F).tmp
	$(verbose) $(SED) -e s/dollarsstar/\$$\*/g $(TMPDIR)/$(@F).tmp >> $@
	@echo "divert" >> $@

$(TMPDIR)/rolemap.conf: $(ROLEMAP)
	$(call parse-rolemap,base,$@)

$(TMPDIR)/all_te_files.conf: $(M4SUPPORT) $(TMPDIR)/generated_definitions.conf $(TMPDIR)/all_interfaces.conf $(ALL_TE_FILES) $(TMPDIR)/rolemap.conf
ifeq "$(strip $(ALL_TE_FILES))" ""
	$(error No enabled modules! $(notdir $(MOD_CONF)) may need to be generated by using "make conf")
endif
	@test -d $(TMPDIR) || mkdir -p $(TMPDIR)
	$(verbose) $(M4) $(M4PARAM) -s $^ > $@

$(TMPDIR)/post_te_files.conf: $(M4SUPPORT) $(POST_TE_FILES)
	@test -d $(TMPDIR) || mkdir -p $(TMPDIR)
	$(verbose) $(M4) $(M4PARAM) $^ > $@

# extract attributes and put them first. extract post te stuff
# like genfscon and put last.
$(TMPDIR)/all_attrs_types.conf $(TMPDIR)/only_te_rules.conf $(TMPDIR)/all_post.conf: $(TMPDIR)/all_te_files.conf $(TMPDIR)/post_te_files.conf
	$(verbose) $(get_type_attr_decl) $(TMPDIR)/all_te_files.conf | $(SORT) > $(TMPDIR)/all_attrs_types.conf
	$(verbose) cat $(TMPDIR)/post_te_files.conf > $(TMPDIR)/all_post.conf
# these have to run individually because order matters:
	$(verbose) $(GREP) '^sid ' $(TMPDIR)/all_te_files.conf >> $(TMPDIR)/all_post.conf || true
	$(verbose) $(GREP) '^fs_use_(xattr|task|trans)' $(TMPDIR)/all_te_files.conf >> $(TMPDIR)/all_post.conf || true
	$(verbose) $(GREP) ^genfscon $(TMPDIR)/all_te_files.conf >> $(TMPDIR)/all_post.conf || true
	$(verbose) $(GREP) ^portcon $(TMPDIR)/all_te_files.conf >> $(TMPDIR)/all_post.conf || true
	$(verbose) $(GREP) ^netifcon $(TMPDIR)/all_te_files.conf >> $(TMPDIR)/all_post.conf || true
	$(verbose) $(GREP) ^nodecon $(TMPDIR)/all_te_files.conf >> $(TMPDIR)/all_post.conf || true
	$(verbose) $(comment_move_decl) $(TMPDIR)/all_te_files.conf > $(TMPDIR)/only_te_rules.conf

########################################
#
# Remove the dontaudit rules from the policy.conf
#
enableaudit: $(POLICY_CONF)
	@test -d $(TMPDIR) || mkdir -p $(TMPDIR)
	@echo "Removing dontaudit rules from $(notdir $(POLICY_CONF))"
	$(verbose) $(GREP) -v dontaudit $^ > $(TMPDIR)/policy.audit
	$(verbose) mv $(TMPDIR)/policy.audit $(POLICY_CONF)

########################################
#
# Construct file_contexts
#
$(FC): $(TMPDIR)/$(notdir $(FC)).tmp $(FCSORT)
	$(verbose) $(FCSORT) $< $@
	$(verbose) $(GREP) -e HOME -e ROLE $@ > $(HOMEDIR_TEMPLATE)
	$(verbose) $(SED) -i -e /HOME/d -e /ROLE/d $@

$(TMPDIR)/$(notdir $(FC)).tmp: $(M4SUPPORT) $(TMPDIR)/generated_definitions.conf $(ALL_FC_FILES)
ifeq ($(ALL_FC_FILES),)
	$(error No enabled modules! $(notdir $(MOD_CONF)) may need to be generated by using "make conf")
endif
	@echo "Creating $(NAME) file_contexts."
	@test -d $(TMPDIR) || mkdir -p $(TMPDIR)
	$(verbose) $(M4) $(M4PARAM) $^ > $@

$(HOMEDIR_TEMPLATE): $(FC)

########################################
#
# Install file_contexts
#
$(FCPATH): $(FC) $(LOADPATH) $(USERPATH)/system.users
	@echo "Validating $(NAME) file_contexts."
	$(verbose) $(SETFILES) -q -c $(LOADPATH) $(FC)
	@echo "Installing file_contexts."
	@mkdir -p $(CONTEXTPATH)/files
	$(verbose) $(INSTALL) -m 644 $(FC) $(FCPATH)
	$(verbose) $(INSTALL) -m 644 $(HOMEDIR_TEMPLATE) $(HOMEDIRPATH)
	$(verbose) $(genhomedircon) -d $(TOPDIR) -t $(NAME) $(USEPWD)
ifeq "$(DISTRO)" "rhel4"
# Setfiles in RHEL4 does not look at file_contexts.homedirs.
	$(verbose) cat $@.homedirs >> $@
# Delete the file_contexts.homedirs in case the toolchain has
# been updated, to prevent duplicate match errors.
	$(verbose) rm -f $@.homedirs
endif

########################################
#
# Intall netfilter_contexts
#
$(NCPATH): $(net_contexts)
	@echo "Installing $(NAME) netfilter_contexts."
	$(verbose) $(INSTALL) -m 0644 $^ $@

########################################
#
# Run policy source checks
#
check: $(BUILDDIR)check.res
$(BUILDDIR)check.res: $(POLICY_CONF) $(FC)
	$(SECHECK) -s --profile=development --policy=$(POLICY_CONF) --fcfile=$(FC) > $@

longcheck: $(BUILDDIR)longcheck.res
$(BUILDDIR)longcheck.res: $(POLICY_CONF) $(FC)
	$(SECHECK) -s --profile=all --policy=$(POLICY_CONF) --fcfile=$(FC) > $@

########################################
#
# Appconfig files
#
$(APPDIR)/customizable_types: $(POLICY_CONF)
	@mkdir -p $(APPDIR)
	$(verbose) $(GREP) '^[[:blank:]]*type .*customizable' $< | cut -d';' -f1 | cut -d',' -f1 | cut -d' ' -f2 | $(SORT) -u > $(TMPDIR)/customizable_types
	$(verbose) $(INSTALL) -m 644 $(TMPDIR)/customizable_types $@ 

########################################
#
# Clean the sources
#
clean:
	rm -f $(POLICY_CONF)
	rm -f $(POLVER)
	rm -f $(FC)
	rm -f $(HOMEDIR_TEMPLATE)
	rm -f $(net_contexts)
	rm -f *.res
	rm -fR $(TMPDIR)

.PHONY: default policy install load reload enableaudit checklabels restorelabels relabel check longcheck clean