I'd be inclined to read the value of the enforceSmartCard
key directly from the com.apple.security.smartcard.plist
file located in the /Library/Preferences
folder.
This can be done using the defaults
command-line tool, which will be significantly faster than the system_profiler
, and you won't need to grep
its output:
defaults read /Library/Preferences/com.apple.security.smartcard enforceSmartCard
Here are the possible outputs for the system_profiler | grep
and defaults
commands, together with their meaning:
system_profiler | grep |
defaults |
|
enforceSmartCard=0 |
0 |
The enforceSmartCard key is set, and its value is false |
enforceSmartCard=1 |
1 |
The enforceSmartCard key is set, and its value is true |
non-zero exit status |
Error message and non-zero exit status |
The enforceSmartCard key is not set, or (defaults only) the property list does not exist |
The default value for enforceSmartCard
is false
, therefore a non-zero exit status is essentially equivalent in meaning—for our purposes—to enforceSmartCard=0
. In the case of the defaults
command, an error message is also printed that looks something like this:
The domain/default pair of (/Library/Preferences/com.apple.security.smartcard, enforceSmartCard) does not exist
so, provided there isn't a typographical error when you issue the command, then either the file /Library/Preferences/com.apple.security.smartcard.plist does not exist, or it does and the enforceSmartCard
key is not set. Either way, the error message is of little value, so this can be suppressed by redirecting stderr
into the void:
defaults read /Library/Preferences/com.apple.security.smartcard enforceSmartCard 2>/dev/null
Thus, the predicate we discriminate upon will be whether or not this command outputs "1"
(without quotes): if it does, then a user must authenticate using their smart card (in addition to whatever other form of authentication would be required); any other result (either an output of "0"
, or a non-zero exit status with no output) infers that a user may authenticate without using a smart card.
Here's the final script that should be equivalent to yours:
#!/usr/bin/env zsh
plist=/Library/Preferences/com.apple.security.smartcard
(( $( defaults read "$plist" \
enforceSmartCard 2>/dev/null )
)) && i=1 _not= ||
i=2 _not=not
printf '%s ' The system is ${_not} configured to \
enforce multi-factor authentication \
>/dev/fd/$i
shell
iszsh
.