Update to libpam 1.5.2

This updates to libpam 1.5.2. This version removes support for
pam_cracklib and pam_tally2. They are replaced by pam_pwquality and
pam_faillock respectively.

Since parameters of pam_cracklb and pam_tally2 are configurable through
Redfish, it's possible that they will remain in the overlay of
/etc/pam.d with the old module names preventing PAM from working
correctly. To avoid this, this commit includes a script that will detect
if the old modules are in the overlay and update the overlay with the
new modules and configuration.

The script will allow updates from libpam 1.3.1 to libpam 1.5.2, but if there
are configured parameters during a downgrade from libpam 1.5.2 to libpam
1.3.1, it will require a factory reset before the downgrade.

pam_pwquality was selected over pam_passwdqc because of better security
and compatibility with pam_cracklib.

Note pam_faillock is necessarily configured into the pam module stack
differently than pam_tally2.

This patchset causes a BMC operational change:
- The pam_tally2 command (invoked from the BMC's command line) is no
  longer present.  If you used the "pam_tally2 -u USER -r" command
  to unlock a user after repeated authentication failures, change to
  use: faillock --user USER --reset

Compatibility note / migration issue.  If your BMC cannot authenticate
users after installing this change, the cause might be an overlayfs file
hiding the new /etc/pam.d/common-auth file.  To find out, use
`grep deny= /etc/pam.d/common-auth` on your BMC.  If it shows "tally2"
then your BMC is affected.  The recovery is to delete the overlay file,
to factory reset the BMC, or manually-install the changed files.
The convert-pam-configs service is intended to handle this problem.

Tested: as follows, for local users only (not tested with LDAP)

Note OpenBMC configuration defaults to an AccountLockoutThreshold
value of 0 which does not lock account passwords no matter how many
consecutive failed authentication attempts.  To configure this on
the BMC, for example, use:
curl -X PATCH https://${bmc}/redfish/v1/AccountService
  -d '{"AccountLockoutThreshold": 3, "AccountLockoutDuration": 60}'

Tested update scenarios:
1. Install from scratch.  Success.
2. Install over firmware which had old PAM configs.  Success.

Tested update scenarios for the convert-pam-configs service.

Tested changing the password via various interfaces:
- the passwd command
- the PATCH Refish AccountService {Password: NEW}
- SSH (accessible only when the password is expired)
- IPMI user set password (accessible for unexpired password)

Tested both good and bad (unacceptable) passwords.

Tested account lockout after N bad passwords
Tested unlock via Redfish.

Also, because its implementation changed, ensure reading and writing the
D-Bus User AccountPolicy RememberOldPasswordTimes property continues to
work.  There is no Redfish API for this.

Signed-off-by: Joseph Reynolds <joseph-reynolds@charter.net>
Signed-off-by: Jason M. Bills <jason.m.bills@linux.intel.com>
Change-Id: I7b712cf7cfbf7b0bc79da42f822540baee66ca4f
diff --git a/meta-phosphor/recipes-extended/pam/libpam/convert-pam-configs.service b/meta-phosphor/recipes-extended/pam/libpam/convert-pam-configs.service
new file mode 100644
index 0000000..099a5c6
--- /dev/null
+++ b/meta-phosphor/recipes-extended/pam/libpam/convert-pam-configs.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=Convert PAM config files
+
+[Service]
+RemainAfterExit=yes
+Type=oneshot
+ExecStart=/usr/bin/convert-pam-configs.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-phosphor/recipes-extended/pam/libpam/convert-pam-configs.sh b/meta-phosphor/recipes-extended/pam/libpam/convert-pam-configs.sh
new file mode 100755
index 0000000..27ec218
--- /dev/null
+++ b/meta-phosphor/recipes-extended/pam/libpam/convert-pam-configs.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+# Convert OpenBMC linux-PAM config files
+
+# Location of config files this script modifies:
+#   PAM_CONF_DIR - path to the PAM config files
+#   SECURITY_CONF_DIR - path to the security config files
+PAM_CONF_DIR=/etc/pam.d
+SECURITY_CONF_DIR=/etc/security
+
+# Handle common-password:
+#   Change cracklib to pwquality and handle the minlen parameter
+pam_cracklib=$(grep "^password.*pam_cracklib.so" ${PAM_CONF_DIR}/common-password)
+if [ -n "${pam_cracklib}" ]
+then
+    echo "Changing ${PAM_CONF_DIR}/common-password to use pam_pwquality.so (was pam_cracklib.so)" >&2
+    minlen=$(echo "${pam_cracklib}" | sed -e "s/.*minlen=\([[:alnum:]]*\).*/\1/")
+    echo "  Converting parameter minlen=${minlen} to ${SECURITY_CONF_DIR}/pwquality.conf minlen" >&2
+    sed -i.bak -e "s/^minlen=.*/minlen=$minlen/" ${SECURITY_CONF_DIR}/pwquality.conf
+    pwquality='password        [success=ok default=die]        pam_pwquality.so debug'
+    sed -i.bak -e "s/^password.*pam_cracklib.so.*/$pwquality/" ${PAM_CONF_DIR}/common-password
+    echo "# This file was converted by $0" >>${PAM_CONF_DIR}/common-password
+fi
+
+# Handle common-auth:
+#   Change tally2 to faillock and handle the deny & unlock_time parameters
+pam_tally2=$(grep "^auth.*pam_tally2.so" ${PAM_CONF_DIR}/common-auth)
+if [ -n "${pam_tally2}" ]
+then
+    echo "Changing ${PAM_CONF_DIR}/common-auth to use pam_faillock.so (was pam_tally2.so)" >&2
+    deny=$(echo "${pam_tally2}" | sed -e "s/.*deny=\([[:alnum:]]*\).*/\1/")
+    unlock_time=$(echo "${pam_tally2}" | sed -e "s/.*unlock_time=\([[:alnum:]]*\).*/\1/")
+    # Change faillock.conf parameters
+    echo "  Converting parameter deny=${deny} to ${SECURITY_CONF_DIR}/faillock.conf deny" >&2
+    echo "  Converting parameter unlock_time=${unlock_time} to ${SECURITY_CONF_DIR}/faillock.conf unlock_time" >&2
+    sed -i.bak \
+        -e "s/^deny=.*/deny=$deny/" \
+        -e "s/^unlock_time=.*/unlock_time=$unlock_time/" \
+        ${SECURITY_CONF_DIR}/faillock.conf
+    # Change pam_tally2 to pam_faillock (changes the overall auth stack)
+    authfail='auth    [default=die]                   pam_faillock.so authfail'
+    authsucc='auth    sufficient                      pam_faillock.so authsucc'
+    sed -i.bak \
+        -e "/^auth.*pam_tally2.so.*$/d" \
+        -e "/^auth.*pam_deny.so/i $authfail\n$authsucc" \
+        ${PAM_CONF_DIR}/common-auth
+    echo "# This file was converted by $0" >>${PAM_CONF_DIR}/common-auth
+fi
+
diff --git a/meta-phosphor/recipes-extended/pam/libpam/faillock.conf b/meta-phosphor/recipes-extended/pam/libpam/faillock.conf
new file mode 100644
index 0000000..dba79c4
--- /dev/null
+++ b/meta-phosphor/recipes-extended/pam/libpam/faillock.conf
@@ -0,0 +1,2 @@
+deny=0
+unlock_time=0
diff --git a/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-account b/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-account
index 82449ca..9a739ec 100644
--- a/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-account
+++ b/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-account
@@ -18,7 +18,8 @@
 -account [success=1 new_authtok_reqd=done default=ignore]    	pam_ldap.so	 ignore_unknown_user ignore_authinfo_unavail
 # here's the fallback if no module succeeds
 account	requisite			pam_deny.so
-account	required			pam_tally2.so
+# Announce if faillock is blocking access
+account	required			pam_faillock.so
 # prime the stack with a positive return value if there isn't one already;
 # this avoids us returning an error just because nothing sets a success code
 # since the modules above will each just jump around
diff --git a/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-auth b/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-auth
index 8eef164..c051ab7 100644
--- a/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-auth
+++ b/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-auth
@@ -8,14 +8,19 @@
 # traditional Unix authentication mechanisms.
 
 # here are the per-package modules (the "Primary" block)
-auth	[success=ok user_unknown=ignore default=2]	pam_tally2.so deny=0 unlock_time=0
 # Try for local user first, and then try for ldap
 auth	[success=2 default=ignore]	pam_unix.so quiet
 -auth    [success=1 default=ignore]  	pam_ldap.so ignore_unknown_user ignore_authinfo_unavail
-# here's the fallback if no module succeeds
-auth	requisite			pam_deny.so
+# Control gets here when no authentication module succeeds.  Increment the
+# failure tally and return failure status to PAM.
+auth    [default=die]                   pam_faillock.so authfail
+# Control gets here when authentication succeeds.  Check if the user is locked
+# out due to consecutive authentication failures and return status accordingly.
+auth    sufficient                      pam_faillock.so authsucc
+# If authsucc failed, deny access
+auth    requisite                       pam_deny.so
 # prime the stack with a positive return value if there isn't one already;
 # this avoids us returning an error just because nothing sets a success code
 # since the modules above will each just jump around
-auth	required			pam_permit.so
+auth    required                        pam_permit.so
 # and here are more per-package modules (the "Additional" block)
diff --git a/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-password b/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-password
index ef706f3..2fc4011 100644
--- a/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-password
+++ b/meta-phosphor/recipes-extended/pam/libpam/pam.d/common-password
@@ -10,13 +10,10 @@
 # The "sha512" option enables salted SHA512 passwords.  Without this option,
 # the default is Unix crypt.  Prior releases used the option "md5".
 #
-# The "obscure" option replaces the old `OBSCURE_CHECKS_ENAB' option in
-# login.defs.
-#
 # See the pam_unix manpage for other options.
 
 # here are the per-package modules (the "Primary" block)
-password	[success=ok default=die]	pam_cracklib.so debug enforce_for_root reject_username minlen=8 difok=0 lcredit=0 ocredit=0 dcredit=0 ucredit=0
+password	[success=ok default=die]	pam_pwquality.so debug
 password	[success=ok default=die]	pam_ipmicheck.so spec_grp_name=ipmi use_authtok
 password	[success=ok ignore=ignore default=die]	pam_pwhistory.so debug enforce_for_root remember=0 use_authtok
 password	[success=ok default=die]	pam_unix.so sha512 use_authtok
diff --git a/meta-phosphor/recipes-extended/pam/libpam_%.bbappend b/meta-phosphor/recipes-extended/pam/libpam_%.bbappend
index 658dc0b..d9ffdac 100644
--- a/meta-phosphor/recipes-extended/pam/libpam_%.bbappend
+++ b/meta-phosphor/recipes-extended/pam/libpam_%.bbappend
@@ -4,19 +4,80 @@
              file://pam.d/common-account \
              file://pam.d/common-auth \
              file://pam.d/common-session \
+             file://faillock.conf \
+             file://convert-pam-configs.service \
+             file://convert-pam-configs.sh \
             "
 
+inherit systemd
+SYSTEMD_SERVICE:${PN} += "convert-pam-configs.service"
+
+FILES:${PN} += "${bindir}/convert-pam-configs.sh \
+                ${systemd_system_unitdir}/convert-pam-configs.service \
+               "
+
 do_install:append() {
     # The libpam recipe will always add a pam_systemd.so line to
     # common-session if systemd is enabled; however systemd only
     # builds pam_systemd.so if logind is enabled, and we disable
     # that package.  So, remove the pam_systemd.so line here.
     sed -i '/pam_systemd.so/d' ${D}${sysconfdir}/pam.d/common-session
+
+    install -d ${D}/etc/security
+    install -m 0644 ${WORKDIR}/faillock.conf ${D}/etc/security
+
+    install -d ${D}${bindir}
+    install -m 0755 ${WORKDIR}/convert-pam-configs.sh ${D}${bindir}
+
+    install -d ${D}${systemd_system_unitdir}
+    install -m 0644 ${WORKDIR}/convert-pam-configs.service ${D}${systemd_system_unitdir}
 }
 
-RDEPENDS:${PN}-runtime += "${MLPREFIX}pam-plugin-cracklib-${libpam_suffix} \
-                           ${MLPREFIX}pam-plugin-tally2-${libpam_suffix} \
+RDEPENDS:${PN}-runtime += "libpwquality \
+                           ${MLPREFIX}pam-plugin-faillock-${libpam_suffix} \
                            ${MLPREFIX}pam-plugin-pwhistory-${libpam_suffix} \
                            ${MLPREFIX}pam-plugin-succeed-if-${libpam_suffix} \
                            ${MLPREFIX}pam-plugin-localuser-${libpam_suffix} \
                           "
+
+#
+# Background:
+# 1. Linux-PAM modules tally2 and cracklib were removed in libpam_1.5,
+# which prompted OpenBMC to change to the faillock and pwquality modules.
+# The PAM config files under /etc/pam.d were changed accordingly.
+# 2. OpenBMC implementations store Redfish property values in PAM config files.
+# For example, the D-Bus property maxLoginAttemptBeforeLockout is stored in
+# /etc/pam.d/common-auth as the pam_tally2.so deny= parameter value.
+# 3. The /etc directory is readonly and has a readwrite overlayfs.  That
+# means when a config file changes, an overlay file is created which hides
+# the readonly version.
+#
+# Problem scenario:
+# 1. Begin with a BMC that has a firmware image which has the old PAM
+# modules and the old PAM config files which have modified parameters.
+# For example, there is an overlay file for /etc/pam.d/common-auth.
+# 2. Perform a firmware update to a firmware image which has the new PAM
+# modules.  The updated image will have not have the old PAM modules.
+# It will have the new PAM config files in its readonly file system and
+# the old PAM config files in its readwrite overlay.
+# 3. Note that PAM authentication will always fail at this point because
+# the old PAM config files in the overlay tell PAM to use the old PAM
+# modules which are not present on the system.
+#
+# Two possible recoveries are:
+# A. Factory reset the BMC.  This will clear the readwrite overlay,
+# allowing PAM to use the readonly version.
+# B. Convert the old PAM config files to the new style.  See below.
+#
+# Service: The convert-pam-configs.service updates the old-style PAM config
+# files on the BMC: it changes uses of the old modules to the new modules
+# and carries forward configuration parameters.  A key point is that files
+# are written to *only* as needed to convert uses of the old modules to the
+# new modules.  See the conversion tool for details.
+#
+# This service can be removed when the BMC no longer supports a direct
+# firware update path from a version which has the old PAM configs to a
+# version which has the new PAM configs.
+#
+# In case of downgrade, Factory reset is recommended. Current logic in existing
+# images won't be able to take care of these settings during downgrade.