/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2012-05-05 08:53:14 UTC
  • mto: This revision was merged to the branch mainline in revision 570.
  • Revision ID: teddy@recompile.se-20120505085314-57lf21cjfybyljkl
* mandos-ctl (main): Use D-Bus properties instead of methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#     GNU General Public License for more details.
18
18
19
19
# You should have received a copy of the GNU General Public License
20
 
# along with this program.  If not, see
21
 
# <http://www.gnu.org/licenses/>.
 
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
21
23
22
# Contact the authors at <mandos@recompile.se>.
24
23
26
25
from __future__ import (division, absolute_import, print_function,
27
26
                        unicode_literals)
28
27
 
29
 
from future_builtins import *
30
 
 
31
28
import sys
32
29
import dbus
33
30
import argparse
63
60
server_path = "/"
64
61
server_interface = domain + ".Mandos"
65
62
client_interface = domain + ".Mandos.Client"
66
 
version = "1.5.4"
 
63
version = "1.5.3"
67
64
 
68
65
def timedelta_to_milliseconds(td):
69
66
    """Convert a datetime.timedelta object to milliseconds"""
119
116
        if type(value) is dbus.Boolean:
120
117
            return "Yes" if value else "No"
121
118
        if keyword in ("Timeout", "Interval", "ApprovalDelay",
122
 
                       "ApprovalDuration", "ExtendedTimeout"):
 
119
                       "ApprovalDuration"):
123
120
            return milliseconds_to_string(value)
124
121
        return unicode(value)
125
122
    
210
207
    parser.add_argument("client", nargs="*", help="Client name")
211
208
    options = parser.parse_args()
212
209
    
213
 
    if has_actions(options) and not (options.client or options.all):
 
210
    if has_actions(options) and not options.client and not options.all:
214
211
        parser.error("Options require clients names or --all.")
215
212
    if options.verbose and has_actions(options):
216
213
        parser.error("--verbose can only be used alone or with"
282
279
    else:
283
280
        # Process each client in the list by all selected options
284
281
        for client in clients:
285
 
            def set_client_prop(prop, value):
286
 
                """Set a Client D-Bus property"""
287
 
                client.Set(client_interface, prop, value,
288
 
                           dbus_interface=dbus.PROPERTIES_IFACE)
289
 
            def set_client_prop_ms(prop, value):
290
 
                """Set a Client D-Bus property, converted
291
 
                from a string to milliseconds."""
292
 
                set_client_prop(prop,
293
 
                                timedelta_to_milliseconds
294
 
                                (string_to_delta(value)))
295
282
            if options.remove:
296
283
                mandos_serv.RemoveClient(client.__dbus_object_path__)
297
284
            if options.enable:
298
 
                set_client_prop("Enabled", dbus.Boolean(True))
 
285
                client.Set(client_interface, "Enabled",
 
286
                           dbus.Boolean(True),
 
287
                           dbus_interface=dbus.PROPERTIES_IFACE)
299
288
            if options.disable:
300
 
                set_client_prop("Enabled", dbus.Boolean(False))
 
289
                client.Set(client_interface, "Enabled",
 
290
                           dbus.Boolean(False),
 
291
                           dbus_interface=dbus.PROPERTIES_IFACE)
301
292
            if options.bump_timeout:
302
 
                set_client_prop("LastCheckedOK", "")
 
293
                client.Set(client_interface, "LastCheckedOK", "",
 
294
                           dbus_interface=dbus.PROPERTIES_IFACE)
303
295
            if options.start_checker:
304
 
                set_client_prop("CheckerRunning", dbus.Boolean(True))
 
296
                client.Set(client_interface, "CheckerRunning",
 
297
                           dbus.Boolean(True),
 
298
                           dbus_interface=dbus.PROPERTIES_IFACE)
305
299
            if options.stop_checker:
306
 
                set_client_prop("CheckerRunning", dbus.Boolean(False))
 
300
                client.Set(client_interface, "CheckerRunning",
 
301
                           dbus.Boolean(False),
 
302
                           dbus_interface=dbus.PROPERTIES_IFACE)
307
303
            if options.is_enabled:
308
304
                sys.exit(0 if client.Get(client_interface,
309
305
                                         "Enabled",
311
307
                                         dbus.PROPERTIES_IFACE)
312
308
                         else 1)
313
309
            if options.checker is not None:
314
 
                set_client_prop("Checker", options.checker)
 
310
                client.Set(client_interface, "Checker",
 
311
                           options.checker,
 
312
                           dbus_interface=dbus.PROPERTIES_IFACE)
315
313
            if options.host is not None:
316
 
                set_client_prop("Host", options.host)
 
314
                client.Set(client_interface, "Host", options.host,
 
315
                           dbus_interface=dbus.PROPERTIES_IFACE)
317
316
            if options.interval is not None:
318
 
                set_client_prop_ms("Interval", options.interval)
 
317
                client.Set(client_interface, "Interval",
 
318
                           timedelta_to_milliseconds
 
319
                           (string_to_delta(options.interval)),
 
320
                           dbus_interface=dbus.PROPERTIES_IFACE)
319
321
            if options.approval_delay is not None:
320
 
                set_client_prop_ms("ApprovalDelay",
321
 
                                   options.approval_delay)
 
322
                client.Set(client_interface, "ApprovalDelay",
 
323
                           timedelta_to_milliseconds
 
324
                           (string_to_delta(options.
 
325
                                            approval_delay)),
 
326
                           dbus_interface=dbus.PROPERTIES_IFACE)
322
327
            if options.approval_duration is not None:
323
 
                set_client_prop_ms("ApprovalDuration",
324
 
                                   options.approval_duration)
 
328
                client.Set(client_interface, "ApprovalDuration",
 
329
                           timedelta_to_milliseconds
 
330
                           (string_to_delta(options.
 
331
                                            approval_duration)),
 
332
                           dbus_interface=dbus.PROPERTIES_IFACE)
325
333
            if options.timeout is not None:
326
 
                set_client_prop_ms("Timeout", options.timeout)
 
334
                client.Set(client_interface, "Timeout",
 
335
                           timedelta_to_milliseconds
 
336
                           (string_to_delta(options.timeout)),
 
337
                           dbus_interface=dbus.PROPERTIES_IFACE)
327
338
            if options.extended_timeout is not None:
328
 
                set_client_prop_ms("ExtendedTimeout",
329
 
                                   options.extended_timeout)
 
339
                client.Set(client_interface, "ExtendedTimeout",
 
340
                           timedelta_to_milliseconds
 
341
                           (string_to_delta(options.extended_timeout)),
 
342
                           dbus_interface=dbus.PROPERTIES_IFACE)
330
343
            if options.secret is not None:
331
 
                set_client_prop("Secret",
332
 
                                dbus.ByteArray(options.secret.read()))
 
344
                client.Set(client_interface, "Secret",
 
345
                           dbus.ByteArray(options.secret.read()),
 
346
                           dbus_interface=dbus.PROPERTIES_IFACE)
333
347
            if options.approved_by_default is not None:
334
 
                set_client_prop("ApprovedByDefault",
335
 
                                dbus.Boolean(options
336
 
                                             .approved_by_default))
 
348
                client.Set(client_interface, "ApprovedByDefault",
 
349
                           dbus.Boolean(options
 
350
                                        .approved_by_default),
 
351
                           dbus_interface=dbus.PROPERTIES_IFACE)
337
352
            if options.approve:
338
353
                client.Approve(dbus.Boolean(True),
339
354
                               dbus_interface=client_interface)