0

I have this file on my laptop /etc/dbus-1/system.d/org.freedesktop.thermald.conf:

<?xml version="1.0"?> <!--*-nxml-*-->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
        "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">

<!--
  This file is part of systemd.

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
-->

<busconfig>

        <policy user="root">
                <allow own="org.freedesktop.thermald"/>
                <allow send_destination="org.freedesktop.thermald"/>
                <allow receive_sender="org.freedesktop.thermald"/>
        </policy>

        <policy context="default">
                <deny send_destination="org.freedesktop.thermald"/>
                <allow receive_sender="org.freedesktop.thermald"/>
        </policy>

</busconfig>

I need to know if:

<allow send_destination="org.freedesktop.thermald"/>

exists but only if it's not under root and must be under default.

Searching the file must only be between:

    <policy context="default">

and the next line that contains:

    </policy>

I'm looking for the most elegant (and readable) way of doing this without any bias towards grep, sed, awk or any other common utility.

This is part of a python program displaying a expandable/collapsible tkinter treeview of DBus. I am trapping out permission errors on system bus that can't be introspected. Introspect and deny are already successfully trapping but trapping too much. Because it is python the json and xmltree modules are already being used and python built-in list and dict functions are available.

8
  • Not elegant, but might work for you: sed -n '/<policy context="default">/,/<\/policy>/p' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
    – Terrance
    Feb 20, 2020 at 2:51
  • @Terrance Thanks. I'll give that a shot tomorrow and let you know. FYI allow own was yesterday's mission which worked great. Today's mission is allow send_destination which is causing problems. Feb 20, 2020 at 2:53
  • Yeah, I caught that and corrected my mistake. =)
    – Terrance
    Feb 20, 2020 at 2:53
  • @Terrance Uncanny coincidence though :) Feb 20, 2020 at 2:54
  • 1
    @waltinator Just a little XML in python to date. But I did parse HTML from Ask Ubuntu Q&A SQL Database in Bash, and JSON from gmail.com metadadta in Bash and that was "complicated". Learning python has been a breath of fresh air and I look forward to rewriting all that bash stuff in python. Feb 20, 2020 at 3:14

2 Answers 2

1

Install xmllint:

apt-get install libxml2-utils

Check wether your xml path exists in context default:

xmllint --xpath "//busconfig/policy[@context='default']/allow[@send_destination='org.freedesktop.thermald']" /etc/dbus-1/system.d/org.freedesktop.thermald.conf >/dev/null 2>&1 && echo good || echo bad

Check vice-versa if your path is inexistent in the other context:

! xmllint --xpath "//busconfig/policy[@user='root']/allow[@send_destination='org.freedesktop.thermald']" /etc/dbus-1/system.d/org.freedesktop.thermald.conf >/dev/null 2>&1 && echo good || echo bad
1
  • I did a little digging and it appears xmllint is an excellent command line utility. My program already requires lots of extra Python libraries be installed and I'm not sure I want to burden users with another. Also I already use this in two places: import xml.etree.cElementTree as ET which can do what xmllint can do, only I didn't realize it until now. Most of my Python program is copy and pasted from Stack Overflow dozens upon dozens of Q&As. Feb 20, 2020 at 23:52
1

Using sed, grep and awk (but not all together):

sed -n '/<policy context="default">/,/<\/policy>/p' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"

or

awk '/<policy context="default">/,/<\/policy>/' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"

Examples:

Searching for allow send_destination:

$ sed -n '/<policy context="default">/,/<\/policy>/p' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
Not Found

Searching for deny send_destination:

$ sed -n '/<policy context="default">/,/<\/policy>/p' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<deny send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
                <deny send_destination="org.freedesktop.thermald"/>

awk works pretty much the same as sed:

$ awk '/<policy context="default">/,/<\/policy>/' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<allow send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
Not Found

and

$ awk '/<policy context="default">/,/<\/policy>/' /etc/dbus-1/system.d/org.freedesktop.thermald.conf | grep '<deny send_destination="org.freedesktop.thermald"\/>' || echo "Not Found"
                <deny send_destination="org.freedesktop.thermald"/>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .