Skip to content

Commit

Permalink
Fix sssd_enable_smartcards
Browse files Browse the repository at this point in the history
If `authselect` doesn't exist and no entry for group + control + module
is present in `/etc/pam.d/system-auth` the Bash remediation inserted
square brackets with backslashes to `system-auth`. The backslashes
shouldn't be inserted there.

The issue manifested when the Bash remediation is used during a build
of a bootable container image.

The solution is to escape the control string only if it's a part of
a regular expression (in `sed` and `grep` commands) but not escape the
control string if it's used in `echo` command.
  • Loading branch information
jan-cerny committed Nov 12, 2024
1 parent 64926d6 commit efd9322
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ if [ -f /usr/bin/authselect ]; then
{{{ bash_enable_authselect_feature('with-smartcard') | indent(4) }}}
else
{{{ bash_ensure_pam_module_option('/etc/pam.d/smartcard-auth', 'auth', 'sufficient', 'pam_sss.so', 'allow_missing_name', '', '') | indent(4) }}}
{{{ bash_ensure_pam_module_option('/etc/pam.d/system-auth', 'auth', '\[success=done authinfo_unavail=ignore ignore=ignore default=die\]', 'pam_sss.so', 'try_cert_auth', '', '') | indent(4) }}}
{{{ bash_ensure_pam_module_option('/etc/pam.d/system-auth', 'auth', '[success=done authinfo_unavail=ignore ignore=ignore default=die]', 'pam_sss.so', 'try_cert_auth', '', '') | indent(4) }}}
fi
{{% endif %}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
# packages = sssd
rm -rf /usr/bin/authselect
SSSD_FILE="/etc/sssd/sssd.conf"
rm -f $SSSD_FILE
12 changes: 7 additions & 5 deletions shared/macros/10-bash.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,8 @@ fi

#}}
{{%- macro bash_ensure_pam_module_line(pam_file, group, control, module, after_match='') -%}}
if ! grep -qP "^\s*{{{ group }}}\s+{{{ control }}}\s+{{{ module }}}\s*.*" "{{{ pam_file }}}"; then
{{% set control_regex = control | escape_regex %}}
if ! grep -qP "^\s*{{{ group }}}\s+{{{ control_regex }}}\s+{{{ module }}}\s*.*" "{{{ pam_file }}}"; then
# Line matching group + control + module was not found. Check group + module.
if [ "$(grep -cP '^\s*{{{ group }}}\s+.*\s+{{{ module }}}\s*' "{{{ pam_file }}}")" -eq 1 ]; then
# The control is updated only if one single line matches.
Expand Down Expand Up @@ -2207,19 +2208,20 @@ fi

#}}
{{%- macro bash_ensure_pam_module_option(pam_file, group, control, module, option, value='', after_match='') -%}}
{{% set control_regex = control | escape_regex %}}
{{{ bash_ensure_pam_module_line(pam_file, group, control, module, after_match) }}}
# Check the option
if ! grep -qP "^\s*{{{ group }}}\s+{{{ control }}}\s+{{{ module }}}\s*.*\s{{{ option }}}\b" "{{{ pam_file }}}"; then
if ! grep -qP "^\s*{{{ group }}}\s+{{{ control_regex }}}\s+{{{ module }}}\s*.*\s{{{ option }}}\b" "{{{ pam_file }}}"; then
{{%- if value == '' %}}
sed -i -E --follow-symlinks "/\s*{{{ group }}}\s+{{{ control }}}\s+{{{ module }}}.*/ s/$/ {{{ option }}}/" "{{{ pam_file }}}"
sed -i -E --follow-symlinks "/\s*{{{ group }}}\s+{{{ control_regex }}}\s+{{{ module }}}.*/ s/$/ {{{ option }}}/" "{{{ pam_file }}}"
{{%- else %}}
sed -i -E --follow-symlinks "/\s*{{{ group }}}\s+{{{ control }}}\s+{{{ module }}}.*/ s/$/ {{{ option }}}={{{ value }}}/" "{{{ pam_file }}}"
sed -i -E --follow-symlinks "/\s*{{{ group }}}\s+{{{ control_regex }}}\s+{{{ module }}}.*/ s/$/ {{{ option }}}={{{ value }}}/" "{{{ pam_file }}}"
{{%- endif %}}
{{%- if value == '' %}}
fi
{{%- else %}}
else
sed -i -E --follow-symlinks "s/(\s*{{{ group }}}\s+{{{ control }}}\s+{{{ module }}}\s+.*)({{{ option }}}=)[[:alnum:]]+\s*(.*)/\1\2{{{ value }}} \3/" "{{{ pam_file }}}"
sed -i -E --follow-symlinks "s/(\s*{{{ group }}}\s+{{{ control_regex }}}\s+{{{ module }}}\s+.*)({{{ option }}}=)[[:alnum:]]+\s*(.*)/\1\2{{{ value }}} \3/" "{{{ pam_file }}}"
fi
{{%- endif %}}
{{%- endmacro -%}}
Expand Down
2 changes: 1 addition & 1 deletion ssg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def escape_regex(text):
# In python 3.7 the set of charaters escaped by re.escape is reasonable, so lets mimic it.
# See https://docs.python.org/3/library/re.html#re.sub
# '!', '"', '%', "'", ',', '/', ':', ';', '<', '=', '>', '@', and "`" are not escaped.
return re.sub(r"([#$&*+.^`|~:()-])", r"\\\1", text)
return re.sub(r"([#$&*+.^`|~:()-\[\]])", r"\\\1", text)


def escape_id(text):
Expand Down

0 comments on commit efd9322

Please sign in to comment.