/usr/share/pyshared/firewall/dbus_utils.py is in firewalld 0.3.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2011,2012 Red Hat, Inc.
#
# Authors:
# Thomas Woerner <twoerner@redhat.com>
#
# This program 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import dbus
import pwd
def command_of_pid(pid):
""" Get command for pid from /proc """
try:
with open("/proc/%d/cmdline" % pid, "r") as f:
cmd = f.readlines()[0].replace('\0', " ").strip()
except:
return None
return cmd
def pid_of_sender(bus, sender):
""" Get pid from sender string using
org.freedesktop.DBus.GetConnectionUnixProcessID """
dbus_obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
dbus_iface = dbus.Interface(dbus_obj, 'org.freedesktop.DBus')
try:
pid = int(dbus_iface.GetConnectionUnixProcessID(sender))
except:
return None
return pid
def uid_of_sender(bus, sender):
""" Get user id from sender string using
org.freedesktop.DBus.GetConnectionUnixUser """
dbus_obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
dbus_iface = dbus.Interface(dbus_obj, 'org.freedesktop.DBus')
try:
uid = int(dbus_iface.GetConnectionUnixUser(sender))
except:
return None
return uid
def user_of_uid(uid):
""" Get user for uid from pwd """
try:
pws = pwd.getpwuid(uid)
except Exception as msg:
return None
return pws[0]
def context_of_sender(bus, sender):
""" Get SELinux context from sender string using
org.freedesktop.DBus.GetConnectionSELinuxSecurityContext """
dbus_obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
dbus_iface = dbus.Interface(dbus_obj, 'org.freedesktop.DBus')
try:
context = dbus_iface.GetConnectionSELinuxSecurityContext(sender)
except:
return None
return "".join(map(chr, dbus_to_python(context)))
def command_of_sender(bus, sender):
""" Return command of D-BUS sender """
return command_of_pid(pid_of_sender(bus, sender))
def user_of_sender(bus, sender):
return user_of_uid(uid_of_sender(bus, sender))
def dbus_to_python(obj):
if obj == None:
return obj
elif isinstance(obj, dbus.Boolean):
return obj == True
elif isinstance(obj, dbus.String):
return obj.encode('utf-8')
elif isinstance(obj, dbus.UTF8String) or \
isinstance(obj, dbus.ObjectPath):
return str(obj)
elif isinstance(obj, dbus.Byte) or \
isinstance(obj, dbus.Int16) or \
isinstance(obj, dbus.Int32) or \
isinstance(obj, dbus.Int64) or \
isinstance(obj, dbus.UInt16) or \
isinstance(obj, dbus.UInt32) or \
isinstance(obj, dbus.UInt64):
return int(obj)
elif isinstance(obj, dbus.Double):
return float(obj)
elif isinstance(obj, dbus.Array):
return [dbus_to_python(x) for x in obj]
elif isinstance(obj, dbus.Struct):
return tuple([dbus_to_python(x) for x in obj])
elif isinstance(obj, dbus.Dictionary):
return {dbus_to_python(k):dbus_to_python(v) for k,v in obj.iteritems()}
elif isinstance(obj, bool) or \
isinstance(obj, str) or isinstance(obj, bytes) or \
isinstance(obj, int) or isinstance(obj, float) or \
isinstance(obj, list) or isinstance(obj, tuple) or \
isinstance(obj, dict):
return obj
else:
raise TypeError("Unhandled %s" % obj)
|