# Top-level install directory.
SELINUXDIR=/usr/local/selinux

# Version numbers for modified packages.
UTILLINUX_VER=2.11f
OPENSSH_VER=3.1p1
CRON_VER=3.0.1
PROCPS_VER=2.0.7
SHUTILS_VER=2.0.11
TAR_VER=1.13.19
FINDUTILS_VER=4.1.7
STAT_VER=2.5
FILEUTILS_VER=4.1
PSMISC_VER=20.1

# For RH7.1, use these versions.
#LOGROTATE_VER=3.5.4-1

# For RH7.2, use these versions.
LOGROTATE_VER=3.5.9


# Directories in which to run 'make install'.
# For other packages (currently just util-linux), we'll handle installation
# manually under the install target rather than doing a 'make install'.
# For util-linux, we just install the modified login program and leave
# everything else alone.
MAKEINSTALLDIRS=newrole spasswd run_init openssh-$(OPENSSH_VER) vixie-cron-$(CRON_VER) logrotate-$(LOGROTATE_VER) fileutils-$(FILEUTILS_VER) procps-$(PROCPS_VER) sh-utils-$(SHUTILS_VER) tar-$(TAR_VER) findutils-$(FINDUTILS_VER) psmisc-$(PSMISC_VER) stat-$(STAT_VER)

# New or modified utility packages.
UTILS = xnewrole xspasswd xrun_init
UTILS += xutil-linux 
UTILS += xopenssh 
UTILS += xvixie-cron 
UTILS += xlogrotate
UTILS += xfileutils
UTILS += xprocps
UTILS += xsh-utils
UTILS += xtar
UTILS += xfindutils
UTILS += xpsmisc
UTILS += xstat

all: $(UTILS)

xnewrole: 
	$(MAKE) -C newrole

xspasswd: 
	$(MAKE) -C spasswd

xrun_init: 
	$(MAKE) -C run_init

xutil-linux-config:
	cd util-linux-$(UTILLINUX_VER) && ./configure
	touch xutil-linux-config

xutil-linux: xutil-linux-config
	$(MAKE) -C util-linux-$(UTILLINUX_VER) "OPT=-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE"

xopenssh-config:
	cd openssh-$(OPENSSH_VER) && \
	./configure --prefix=/usr --with-cflags="-DWITH_SELINUX -I$(SELINUXDIR)/include" --with-ldflags="-L$(SELINUXDIR)/lib" --with-libs="-lsecure" --sysconfdir="/etc/ssh" --libexecdir=/usr/libexec/openssh --with-tcp-wrappers --with-rsh=/usr/bin/rsh --with-kerberos5=/usr/kerberos --with-pam
	touch xopenssh-config

xopenssh: xopenssh-config
	$(MAKE) -C openssh-$(OPENSSH_VER)

xvixie-cron:
	$(MAKE) -C vixie-cron-$(CRON_VER)

xlogrotate:
	$(MAKE) -C logrotate-$(LOGROTATE_VER)

xfileutils-config:
	cd fileutils-$(FILEUTILS_VER) && \
	./configure --prefix=$(SELINUXDIR) && \
	touch aclocal.m4 configure.in config.h.in stamp-h.in Makefile.in */Makefile.in configure config.status config.h Makefile */Makefile
	touch xfileutils-config

xfileutils: xfileutils-config
	$(MAKE) -C fileutils-$(FILEUTILS_VER)

xprocps:
	$(MAKE) -C procps-$(PROCPS_VER)

xsh-utils-config:
	cd sh-utils-$(SHUTILS_VER) && \
	./configure --prefix=$(SELINUXDIR) --disable-largefile --enable-pam && \
	echo "#define USE_PAM 1" >> config.h && \
	perl -pi -e "s/-lcrypt/-lcrypt -ldl -lpam -lpam_misc/g" src/Makefile && \
	mv configure.in.sux configure.in && \
	echo "#!/bin/sh" >missing && \
	echo "exit 0" >>missing && \
	chmod +x missing
	touch xsh-utils-config

xsh-utils: xsh-utils-config
	$(MAKE) -C sh-utils-$(SHUTILS_VER) 

xtar-config:
	cd tar-$(TAR_VER) && \
	./configure --prefix=$(SELINUXDIR) && \
	touch aclocal.m4 configure.in config.h.in stamp-h.in Makefile.in */Makefile.in configure config.status config.h Makefile */Makefile
	touch xtar-config

xtar: xtar-config
	$(MAKE) -C tar-$(TAR_VER) 

xfindutils-config:
	cd findutils-$(FINDUTILS_VER) && \
	./configure --prefix=$(SELINUXDIR) && \
	touch aclocal.m4 configure.in config.h.in stamp-h.in Makefile.in */Makefile.in configure config.status config.h Makefile */Makefile
	touch xfindutils-config

xfindutils: xfindutils-config
	 $(MAKE) -C findutils-$(FINDUTILS_VER)

xpsmisc-config:
	cd psmisc-$(PSMISC_VER) && \
	./configure --prefix=$(SELINUXDIR) && \
	touch aclocal.m4 configure.in config.h.in stamp-h.in Makefile.in */Makefile.in configure config.status config.h Makefile */Makefile
	touch xpsmisc-config

xpsmisc: xpsmisc-config
	$(MAKE) -C psmisc-$(PSMISC_VER)

xstat:
	$(MAKE) -C stat-$(STAT_VER)

install: all
	mkdir -p $(SELINUXDIR)/sbin
	mkdir -p $(SELINUXDIR)/X11R6/bin
	mkdir -p $(SELINUXDIR)/etc/X11/applnk/Utilities
	mkdir -p $(SELINUXDIR)/man/man1
	mkdir -p $(SELINUXDIR)/man/man5
	mkdir -p $(SELINUXDIR)/man/man8
	if [ ! -f /bin/login.old ] && [ -f /bin/login ]; then mv /bin/login /bin/login.old; fi
	install util-linux-$(UTILLINUX_VER)/login-utils/login /bin/login
	if [ ! -f /usr/sbin/crond.old ] && [ -f /usr/sbin/crond ]; then mv /usr/sbin/crond /usr/sbin/crond.old; fi
	if [ ! -f /usr/bin/crontab.old ] && [ -f /usr/bin/crontab ]; then mv /usr/bin/crontab /usr/bin/crontab.old; fi
	if [ ! -f /usr/sbin/logrotate.old ] && [ -f /usr/sbin/logrotate ]; then mv /usr/sbin/logrotate /usr/sbin/logrotate.old; fi
	if [ ! -f /usr/sbin/sshd.old ] && [ -f /usr/sbin/sshd ]; then mv /usr/sbin/sshd /usr/sbin/sshd.old; fi
	@for subdir in $(MAKEINSTALLDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	done
	(cd $(SELINUXDIR)/bin; rm -f basename chage chfn chgrp chmod chown chroot chsh date dd dirname du echo env expiry expr factor faillog false free fuser gpasswd groups hostid hostname kill lastlog ln locate login logname mv newgrp nice nohup passwd pathchk pinky printenv printf pwd rm rmdir seq sg skill sleep snice stty su sync tee test tload top touch true tty uname updatedb uptime users vmstat w watch who whoami xargs yes)

relabel:
	chcon system_u:object_r:login_exec_t /bin/login
	chcon system_u:object_r:crond_exec_t /usr/sbin/crond
	chcon system_u:object_r:sshd_exec_t /usr/sbin/sshd
	chcon system_u:object_r:ssh_exec_t /usr/bin/ssh
	chcon system_u:object_r:crontab_exec_t /usr/bin/crontab
	chcon system_u:object_r:logrotate_exec_t /usr/sbin/logrotate
	chcon system_u:object_r:newrole_exec_t $(SELINUXDIR)/bin/newrole
	chcon system_u:object_r:run_init_exec_t $(SELINUXDIR)/bin/run_init
	chcon system_u:object_r:passwd_exec_t $(SELINUXDIR)/bin/spasswd

