### this file is sourced not run
PKGVER=4.13
PKGBUILD=1

# source: BLFS 11.3
TARBALL=shadow-$PKGVER.tar.xz
MD5SUM=b1ab01b5462ddcf43588374d57bec123
BUNDLE=fatdog-base-11.3.tar
SRC_URL=http://distro.ibiblio.org/fatdog/source/900/$BUNDLE

SLACKREQ='cracklib linux-pam'
SLACKDESC="shadow-linux-pam: The Shadow package contains programs for handling passwords in a 
shadow-linux-pam: secure way. This package contains the Shadow package that works with 
shadow-linux-pam: Linux-PAM.
shadow-linux-pam: 
shadow-linux-pam: EXPERIMENTAL PACKAGE WARNING: This integration of this package into Fatdog
shadow-linux-pam: has __NOT__ been tested. Installing this affects the way the system does 
shadow-linux-pam: its logins, and it may break your system: inablity to login, among others.
shadow-linux-pam: YOU HAVE BEEN WARNED. Only install this if you know what you are doing.
shadow-linux-pam:
"
SLACKINSTALL='
if [ -f ./etc/login.access ]; then mv -v ./etc/login.access{,.NOUSE}; fi
if [ -f ./etc/limits ]; then mv -v ./etc/limits{,.NOUSE}; fi
'


### override download for LFS sources bundle
# $1-src-dir $2-tarball 
pkg_download() {
	echo downloading $BUNDLE for $TARBALL
	wget -P $1 $SRC_URL
	tar -xf $1/$BUNDLE --strip-components=1 -C $1 
	rm $1/$BUNDLE
}

### default pkg_prepare
### default pkg_package
### build
pkg_build() {
	cd /tmp/shadow* &&

	# BLFS 11.3 note: installing shadow with cracklib but no Linux-PAM:
	#sed -i 's@DICTPATH.*@DICTPATH\t/lib/cracklib/pw_dict@' etc/login.defs
	
	# disable 'groups' and man-pages (will come from coreutils/util-linux)
	sed -i 's/groups$(EXEEXT) //' src/Makefile.in          &&
	find man -name Makefile.in -exec sed -i 's/groups\.1 / /'   {} \; &&
	find man -name Makefile.in -exec sed -i 's/getspnam\.3 / /' {} \; &&
	find man -name Makefile.in -exec sed -i 's/passwd\.5 / /'   {} \; &&	
	
	# use sha-512 by default, use /var/mail - this isn't compatible with busybox
	# increase crypt rounds from 5000 to 500,000
	#sed -i -e 's@#ENCRYPT_METHOD DES@ENCRYPT_METHOD SHA512@' \
	#       -e 's@#\(SHA_CRYPT_..._ROUNDS 5000\)@\100@'       \
    #   -e 's@/var/spool/mail@/var/mail@' etc/login.defs &&

	# use sha-256 by default, use /var/mail - works with busybox
	# increase crypt rounds from 5000 to 500,000
	sed -i -e 's@#ENCRYPT_METHOD DES@ENCRYPT_METHOD SHA256@' \
	   -e 's@#\(SHA_CRYPT_..._ROUNDS 5000\)@\100@'           \
       -e 's@/var/spool/mail@/var/mail@' etc/login.defs &&
    
    # build
    ./configure \
		--sysconfdir=/etc \
		--libdir=/$LIBDIR \
		--with-group-name-max-length=32 \
		--with-libcrack \
	&&
    make $MAKEFLAGS &&
    make install &&
    make -C man install-man &&
    
    # FHS
    mv -v /usr/bin/passwd /bin && # sh &&

	# BLFS configuration
	install -v -m644 /etc/login.defs /etc/login.defs.orig &&
	for FUNCTION in FAIL_DELAY               \
					FAILLOG_ENAB             \
					LASTLOG_ENAB             \
					MAIL_CHECK_ENAB          \
					OBSCURE_CHECKS_ENAB      \
					PORTTIME_CHECKS_ENAB     \
					QUOTAS_ENAB              \
					CONSOLE MOTD_FILE        \
					FTMP_FILE NOLOGINS_FILE  \
					ENV_HZ PASS_MIN_LEN      \
					SU_WHEEL_ONLY            \
					CRACKLIB_DICTPATH        \
					PASS_CHANGE_TRIES        \
					PASS_ALWAYS_WARN         \
					CHFN_AUTH ENCRYPT_METHOD \
					ENVIRON_FILE
	do
		sed -i "s/^${FUNCTION}/# &/" /etc/login.defs
	done &&

	cat > /etc/pam.d/login << "EOF" &&
# Begin /etc/pam.d/login

# Set failure delay before next prompt to 3 seconds
auth      optional    pam_faildelay.so  delay=3000000

# Check to make sure that the user is allowed to login
auth      requisite   pam_nologin.so

# Check to make sure that root is allowed to login
# Disabled by default. You will need to create /etc/securetty
# file for this module to function. See man 5 securetty.
#auth      required    pam_securetty.so

# Additional group memberships - disabled by default
#auth      optional    pam_group.so

# include system auth settings
auth      include     system-auth

# check access for the user
account   required    pam_access.so

# include system account settings
account   include     system-account

# Set default environment variables for the user
session   required    pam_env.so

# Set resource limits for the user
session   required    pam_limits.so

# Display date of last login - Disabled by default
#session   optional    pam_lastlog.so

# Display the message of the day - Disabled by default
#session   optional    pam_motd.so

# Check user's mail - Disabled by default
#session   optional    pam_mail.so      standard quiet

# include system session and password settings
session   include     system-session
password  include     system-password

# End /etc/pam.d/login
EOF


	cat > /etc/pam.d/passwd << "EOF" &&
# Begin /etc/pam.d/passwd

password  include     system-password

# End /etc/pam.d/passwd
EOF



	cat > /etc/pam.d/su << "EOF" &&
# Begin /etc/pam.d/su

# always allow root
auth      sufficient  pam_rootok.so

# Allow users in the wheel group to execute su without a password
# disabled by default
#auth      sufficient  pam_wheel.so trust use_uid

# include system auth settings
auth      include     system-auth

# limit su to users in the wheel group
# disabled by default
#auth      required    pam_wheel.so use_uid

# include system account settings
account   include     system-account

# Set default environment variables for the service user
session   required    pam_env.so

# include system session settings
session   include     system-session

# End /etc/pam.d/su
EOF



	cat > /etc/pam.d/chpasswd << "EOF" &&
# Begin /etc/pam.d/chpasswd

# always allow root
auth      sufficient  pam_rootok.so

# include system auth and account settings
auth      include     system-auth
account   include     system-account
password  include     system-password

# End /etc/pam.d/chpasswd
EOF
	sed -e s/chpasswd/newusers/ /etc/pam.d/chpasswd >/etc/pam.d/newusers &&



	cat > /etc/pam.d/chage << "EOF" &&
# Begin /etc/pam.d/chage

# always allow root
auth      sufficient  pam_rootok.so

# include system auth and account settings
auth      include     system-auth
account   include     system-account

# End /etc/pam.d/chage
EOF


	for PROGRAM in chfn chgpasswd chsh groupadd groupdel \
				   groupmems groupmod useradd userdel usermod
	do
		install -v -m644 /etc/pam.d/chage /etc/pam.d/${PROGRAM}
		sed -i "s/chage/$PROGRAM/" /etc/pam.d/${PROGRAM}
	done &&

	pkg_build_slackdesc
}
