-
Notifications
You must be signed in to change notification settings - Fork 0
/
fw_dbus.py
128 lines (107 loc) · 3.71 KB
/
fw_dbus.py
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
125
126
127
128
#
# Copyright (C) 2009 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 gobject
import dbus
import dbus.service
import dbus.mainloop.glib
import slip.dbus
import slip.dbus.service
import json
import fw_sysconfig
import fw_lokkit
#
DBUS_DOMAIN = "org.fedoraproject.Config.Firewall"
DBUS_PATH = "/org/fedoraproject/Config/Firewall"
POLKIT_DOMAIN = "org.fedoraproject.config.firewall"
#
class DBusProxy(object):
def __init__(self):
try:
self.bus = slip.dbus.SystemBus()
self.bus.default_timeout = None
except:
self.bus = dbus.SystemBus()
self.dbus_obj = self.bus.get_object(DBUS_DOMAIN, DBUS_PATH)
@slip.dbus.polkit.enable_proxy
def auth(self):
return self.dbus_obj.auth(dbus_interface=DBUS_DOMAIN)
@slip.dbus.polkit.enable_proxy
def read(self):
rep = self.dbus_obj.read(dbus_interface=DBUS_DOMAIN)
try:
args = json.loads(rep.encode('utf-8'))
except:
return None
return args
@slip.dbus.polkit.enable_proxy
def write(self, args):
try:
rep = json.dumps(args)
except:
return -1
return self.dbus_obj.write(rep.encode('utf-8'),
dbus_interface=DBUS_DOMAIN)
#
class DBusService(slip.dbus.service.Object):
# create service object
def __init__(self, *p, **k):
super(DBusService, self).__init__(*p, **k)
self.persistent = True
# delete service object
def __del__(self):
pass
@slip.dbus.polkit.require_auth("%s.auth" % POLKIT_DOMAIN)
@dbus.service.method(DBUS_DOMAIN, in_signature='', out_signature='i')
def auth(self):
return 1
# read args
# @slip.dbus.polkit.require_auth("%s.read" % POLKIT_DOMAIN)
@slip.dbus.polkit.require_auth("%s.auth" % POLKIT_DOMAIN)
@dbus.service.method(DBUS_DOMAIN, in_signature='', out_signature='s')
def read(self):
args = fw_sysconfig.read_sysconfig_args()
try:
rep = json.dumps(args)
except:
return None
return rep.encode('utf-8')
# write args
# @slip.dbus.polkit.require_auth("%s.write" % POLKIT_DOMAIN)
@slip.dbus.polkit.require_auth("%s.auth" % POLKIT_DOMAIN)
@dbus.service.method(DBUS_DOMAIN, in_signature='s', out_signature='i')
def write(self, rep):
try:
args = json.loads(rep.encode('utf-8'))
except:
return -1
(config, old_config, \
old_se_config) = fw_lokkit.loadConfig(args, dbus_parser=1)
se_status = fw_lokkit.updateSELinux(config, old_se_config)
(c_status, ip4t_status, \
ip6t_status, log) = fw_lokkit.updateFirewall(config, old_config)
return (ip4t_status + ip6t_status + c_status + se_status)
#
def run_service():
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
name = dbus.service.BusName(DBUS_DOMAIN, bus)
service = DBusService(name, DBUS_PATH)
mainloop = gobject.MainLoop()
slip.dbus.service.set_mainloop(mainloop)
mainloop.run()