/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-03 20:16:22 UTC
  • Revision ID: teddy@recompile.se-20120503201622-d9yezi9t17i00lio
* mandos-ctl: Use new string format method.  Bug fix: --version now
              shows the program name.

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
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.Enable(dbus_interface=client_interface)
299
286
            if options.disable:
300
 
                set_client_prop("Enabled", dbus.Boolean(False))
 
287
                client.Disable(dbus_interface=client_interface)
301
288
            if options.bump_timeout:
302
 
                set_client_prop("LastCheckedOK", "")
 
289
                client.CheckedOK(dbus_interface=client_interface)
303
290
            if options.start_checker:
304
 
                set_client_prop("CheckerRunning", dbus.Boolean(True))
 
291
                client.StartChecker(dbus_interface=client_interface)
305
292
            if options.stop_checker:
306
 
                set_client_prop("CheckerRunning", dbus.Boolean(False))
 
293
                client.StopChecker(dbus_interface=client_interface)
307
294
            if options.is_enabled:
308
295
                sys.exit(0 if client.Get(client_interface,
309
296
                                         "Enabled",
311
298
                                         dbus.PROPERTIES_IFACE)
312
299
                         else 1)
313
300
            if options.checker is not None:
314
 
                set_client_prop("Checker", options.checker)
 
301
                client.Set(client_interface, "Checker",
 
302
                           options.checker,
 
303
                           dbus_interface=dbus.PROPERTIES_IFACE)
315
304
            if options.host is not None:
316
 
                set_client_prop("Host", options.host)
 
305
                client.Set(client_interface, "Host", options.host,
 
306
                           dbus_interface=dbus.PROPERTIES_IFACE)
317
307
            if options.interval is not None:
318
 
                set_client_prop_ms("Interval", options.interval)
 
308
                client.Set(client_interface, "Interval",
 
309
                           timedelta_to_milliseconds
 
310
                           (string_to_delta(options.interval)),
 
311
                           dbus_interface=dbus.PROPERTIES_IFACE)
319
312
            if options.approval_delay is not None:
320
 
                set_client_prop_ms("ApprovalDelay",
321
 
                                   options.approval_delay)
 
313
                client.Set(client_interface, "ApprovalDelay",
 
314
                           timedelta_to_milliseconds
 
315
                           (string_to_delta(options.
 
316
                                            approval_delay)),
 
317
                           dbus_interface=dbus.PROPERTIES_IFACE)
322
318
            if options.approval_duration is not None:
323
 
                set_client_prop_ms("ApprovalDuration",
324
 
                                   options.approval_duration)
 
319
                client.Set(client_interface, "ApprovalDuration",
 
320
                           timedelta_to_milliseconds
 
321
                           (string_to_delta(options.
 
322
                                            approval_duration)),
 
323
                           dbus_interface=dbus.PROPERTIES_IFACE)
325
324
            if options.timeout is not None:
326
 
                set_client_prop_ms("Timeout", options.timeout)
 
325
                client.Set(client_interface, "Timeout",
 
326
                           timedelta_to_milliseconds
 
327
                           (string_to_delta(options.timeout)),
 
328
                           dbus_interface=dbus.PROPERTIES_IFACE)
327
329
            if options.extended_timeout is not None:
328
 
                set_client_prop_ms("ExtendedTimeout",
329
 
                                   options.extended_timeout)
 
330
                client.Set(client_interface, "ExtendedTimeout",
 
331
                           timedelta_to_milliseconds
 
332
                           (string_to_delta(options.extended_timeout)),
 
333
                           dbus_interface=dbus.PROPERTIES_IFACE)
330
334
            if options.secret is not None:
331
 
                set_client_prop("Secret",
332
 
                                dbus.ByteArray(options.secret.read()))
 
335
                client.Set(client_interface, "Secret",
 
336
                           dbus.ByteArray(options.secret.read()),
 
337
                           dbus_interface=dbus.PROPERTIES_IFACE)
333
338
            if options.approved_by_default is not None:
334
 
                set_client_prop("ApprovedByDefault",
335
 
                                dbus.Boolean(options
336
 
                                             .approved_by_default))
 
339
                client.Set(client_interface, "ApprovedByDefault",
 
340
                           dbus.Boolean(options
 
341
                                        .approved_by_default),
 
342
                           dbus_interface=dbus.PROPERTIES_IFACE)
337
343
            if options.approve:
338
344
                client.Approve(dbus.Boolean(True),
339
345
                               dbus_interface=client_interface)