Bash - 在plist文本文件中修改WhitelistedBlockedPlugins

时间:2013-07-11 23:01:03

标签: macos bash replace plist

我正在尝试自动添加针对Java的Safari阻止的应用程序和网站的白名单。我能够导出二进制plist并通过bash进行编辑,但我仍然坚持如何搜索和替换或只是添加到具有特定位置的文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>BookmarksSidebarWidth</key>
    <real>194</real>
        ...
    <key>WhitelistedBlockedPlugins</key>
    <array>
        <dict>
            <key>PluginHostname</key>
            <string>www.java.com</string>
            <key>PluginIdentifier</key>
            <string>com.oracle.java.JavaAppletPlugin</string>
            <key>PluginLastVisitedDate</key>
            <date>2013-06-05T17:03:26Z</date>
            <key>PluginName</key>
            <string>Java Applet Plug-in</string>
            <key>PluginPageURL</key>
            <string>http://www.java.com/en/download/testjava.jsp</string>
            <key>PluginPolicy</key>
            <string>PluginPolicyBlockWhenInsecure</string>
        </dict>
    </array>
    ...
    <key>com.apple.Safari.ContentPageGroupIdentifier.WebKit2UsesPageCache</key>
    <true/>
</dict>
</plist>

<key>WhitelistedBlockedPlugins</key>
<array>
    <dict>
        ....
        ....
    </dict>
</array>

是我想要搜索和替换的位,或者如果它不存在则添加。如果它存在,则可以替换从<key>WhitelistedBlockedPlugins</key>开始到结束</array>结束的所有内容,因为我们无论如何都要控制其中的内容。

如果它不存在,我想我可以在文件末尾的最后</dict>之前添加它。

这是我提出的逻辑,但我很难找到一个可以搜索的解决方案,如果找到替换,如果没有找到添加。

在Stack Overflow上阅读类似帖子后,我尝试了一些没有成功的事情。我愿意使用OSX原生的任何东西,包括XCode / CLI Tools附带的东西。

我尝试过的事情:

文件中的

echo有效,但我不知道如何在</dict>之前执行此操作,而我只想在<key>WhitelistedBlockedPlugins</key>和相应的数组中执行此操作无处可寻。有人有任何指导或解决方案吗?感谢

3 个答案:

答案 0 :(得分:1)

我解决了同样的问题但是能够使用默认值来修改com.apple.Safari.plist。

#!/bin/sh

# Get today's date
TODAY=$(/bin/date "+%FT%TZ")

# Determine OS version
osvers=$(sw_vers -productVersion | awk -F. '{print $2}')

# Server1's address
SERVER1=server1.name.here

# Server2's address
SERVER2=server2.name.here

# Get Java plug-in info
JAVA_PLUGIN=`/usr/bin/defaults read "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Info" CFBundleIdentifier`

# Check com.apple.Safari.plist for Server1 address
SERVER1_WHITELIST_CHECK=`/usr/bin/defaults read $HOME/Library/Preferences/com.apple.Safari WhitelistedBlockedPlugins | grep PluginHostname | awk '{print $3}' | grep $SERVER1 | tr -d '";'`

# Check com.apple.Safari.plist for Server2 address
SERVER2_WHITELIST_CHECK=`/usr/bin/defaults read $HOME/Library/Preferences/com.apple.Safari WhitelistedBlockedPlugins | grep PluginHostname | awk '{print $3}' | grep $SERVER2 | tr -d '";'`

if [[ ${osvers} -ge 6 ]]; then
  if [[ -n ${SERVER1_WHITELIST_CHECK} ]]; then

        # Server1 settings are present
    /usr/bin/logger "${SERVER1_WHITELIST_CHECK} is part of the Java whitelist in Safari. Nothing to do here."
    else        
    # Add Server1 to Java whitelist
        /usr/bin/defaults write $HOME/Library/Preferences/com.apple.Safari "WhitelistedBlockedPlugins" -array-add '{"PluginHostname" = "'$SERVER1'"; "PluginIdentifier" = "'$JAVA_PLUGIN'"; "PluginLastVisitedDate" = "'$TODAY'"; "PluginName" = "Java Applet Plug-in"; "PluginPageURL" = "https://'$SERVER1'"; "PluginPolicy" = "PluginPolicyNeverBlock";}'
        /usr/bin/logger "$SERVER1 has been added to the Java whitelist in Safari."
  fi

  if [[ -n ${SERVER2_WHITELIST_CHECK} ]]; then

    # Server2 settings are present
    /usr/bin/logger "${SERVER2_WHITELIST_CHECK} is part of the Java whitelist in Safari. Nothing to do here."
     else       
        # Add Server2 to Java whitelist
    /usr/bin/defaults write $HOME/Library/Preferences/com.apple.Safari "WhitelistedBlockedPlugins" -array-add '{"PluginHostname" = "'$SERVER2'"; "PluginIdentifier" = "'$JAVA_PLUGIN'"; "PluginLastVisitedDate" = "'$TODAY'"; "PluginName" = "Java Applet Plug-in"; "PluginPageURL" = "https://'$SERVER2'"; "PluginPolicy" = "PluginPolicyNeverBlock";}'
        /usr/bin/logger "$SERVER2 has been added to the Java whitelist in Safari."
  fi

fi

exit 0

The code's also available here on my GitHub repo.

答案 1 :(得分:0)

您可能只使用默认值:

defaults write ./file.plist WhitelistedBlockedPlugins -array '<dict><key>a</key><string>b</string></dict>'

答案 2 :(得分:0)

因为这是修改plist文件并使用OSX执行此操作的情况,我发现使用Plistbuddy是我最简单的解决方案。如果我需要查找和替换,保留某些网站,或添加某些条件,我最终会使用Perl或Python我确定....但我有幸擦除整个阵列并写入我想要的是什么。

以下是我的解决方案,我可能最终会创建LaunchDaemon或Agent,因此每个用户和每次启动都会发生这种情况(消除未经授权或批准的Java小程序并确保添加所需的站点)。

#!/bin/bash

# Convert the com.apple.Safari.plist from binary plist to readable text file
plutil -convert xml1 -o - ~/Library/Preferences/com.apple.Safari.plist > /tmp/com.apple.Safari.plist

# Deletes the WhitelistedBlockedPlugins Key and corresponding array, if it exists
/usr/libexec/PlistBuddy -c "Delete WhitelistedBlockedPlugins" /tmp/com.apple.Safari.plist

# Adds the WhitelistedBlockedPlugins Key and array including two whitelisted sites
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins array" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:0 dict" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:0:PluginHostname string 'www.java.com'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:0:PluginIdentifier string 'com.oracle.java.JavaAppletPlugin'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:0:PluginLastVisitedDate date 'Wed Jul 10 12:00:00 PST 2013'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:0:PluginName string 'Java Applet Plug-in'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:0:PluginPageURL string 'http://www.java.com/en/download/testjava.jsp'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:0:PluginPolicy string 'PluginPolicyNeverBlock'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:1 dict" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:1:PluginHostname string 'another.siteexample.com'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:1:PluginIdentifier string 'com.oracle.java.JavaAppletPlugin'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:1:PluginLastVisitedDate date 'Wed Jul 10 12:05:00 PST 2013'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:1:PluginName string 'Java Applet Plug-in'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:1:PluginPageURL string 'http://another.siteexample.com/some/path'" /tmp/com.apple.Safari.plist
/usr/libexec/PlistBuddy -c "Add :WhitelistedBlockedPlugins:1:PluginPolicy string 'PluginPolicyNeverBlock'" /tmp/com.apple.Safari.plist

# copy the modified plist back where Safari will then use it and convert it back to binary plist itself
cp /tmp/com.apple.Safari.plist ~/Library/Preferences/com.apple.Safari.plist

同样,作为参考,这是一种自动将已批准网站列表添加到Safari的方法。在看到需要更轻松地在客户端计算机上添加我们已批准的网站并阅读http://nakedsecurity.sophos.com/2013/04/18/apple-updates-safari-gives-better-control-over-java-applets/

后,我受到启发