Skip to content

Commit cc07bae

Browse files
committed
Warns about attempts to overwrite Managed Preferences
1 parent b33ec30 commit cc07bae

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

src/user-defaults

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ fi
9090

9191
isQuiet=false
9292
isType=false
93+
isWrite=false
94+
isForce=false
9395
args=()
9496
while [ "$1" != "" ]; do
9597
# See if user specified a type
@@ -102,6 +104,7 @@ while [ "$1" != "" ]; do
102104
case "$1" in
103105
-u | -user | --user ) targetUser="$2"; shift;;
104106
-q | -quiet | --quiet ) isQuiet=true; ;;
107+
-f | -force | --force ) isForce=true; ;;
105108
-currentHost ) host="current"; ;;
106109
-host ) host="$2"; shift;;
107110
-h | --help | help ) output::usage; exit;; # quit and show usage
@@ -115,12 +118,45 @@ done
115118

116119
## region ############################################## Main Code
117120

121+
# Get Particulars
118122
prefix="sudo -u $targetUser"
119123
action="${args[0]}"
124+
domain="${args[1]}"
125+
key="${args[2]}"
120126

121127
# Determine if this will write
122128
if echo "$action" | grep -q 'write\|array\|array-add\|dict\|dict-add'; then
123129
isWrite=true
130+
# Warn about trying to overwrite Managed Preferences
131+
if ! echo "$domain" | /usr/bin/grep -q "/"; then
132+
managedPath="/Library/Managed Preferences/${domain}"
133+
if ! echo "$domain" | /usr/bin/grep ".plist"; then
134+
managedPath="${managedPath}.plist"
135+
fi
136+
137+
if [ -f "$managedPath" ]; then
138+
# Attempt to read for same key
139+
testCommand="$binDefaults read "
140+
[ -n "$host" ] && testCommand="$testCommand $host "
141+
output=$($testCommand "$managedPath" 2>&1)
142+
retval=$?
143+
absent=$(echo "$output" | /usr/bin/grep "does not exist")
144+
if [ -z "$absent" ]; then
145+
# If we didn't get a "does not exist" error, issue a warning.
146+
if ! $isQuiet; then
147+
{
148+
echo "$output" | head -n 1
149+
echo "The domain/default pair of ($domain, $key) is a Managed Preference and cannot be modified."
150+
} >&2
151+
fi
152+
153+
if ! $isForce; then
154+
# Exit instead of attempting to write
155+
exit "$retval"
156+
fi
157+
fi
158+
fi
159+
fi
124160
fi
125161

126162
if [ "$action" == "write" ] && $isSingle; then

0 commit comments

Comments
 (0)