/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: 2008-09-03 05:04:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080903050440-7cwzxestx6pvdy1i
* Makefile (mandos.8): Add dependency on "overview.xml" and
                       "legalnotice.xml".
  (mandos-keygen.8): New target.
  (mandos-conf.5): Added dependency on "legalnotice.xml".
  (plugin-runner.8mandos): New target
  (plugins.d/password-request.8mandos): - '' -

* mandos-options.xml (priority): Make wording server/client neutral.

* plugins.d/password-request.c (main): Changed .arg fields of the argp
                                       options struct to be more
                                       consistent with the manual.

* plugins.d/password-request.xml (OVERVIEW): Moved to after "OPTIONS".
  (OPTIONS): Improved wording and names of replaceables.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- mode: python; coding: utf-8 -*-
3
 
 
4
 
from __future__ import division
5
 
import sys
6
 
import dbus
7
 
from optparse import OptionParser
8
 
import locale
9
 
import datetime
10
 
import re
11
 
 
12
 
locale.setlocale(locale.LC_ALL, u'')
13
 
 
14
 
tablewords = {
15
 
    'name': u'Name',
16
 
    'enabled': u'Enabled',
17
 
    'timeout': u'Timeout',
18
 
    'last_checked_ok': u'Last Successful Check',
19
 
    'created': u'Created',
20
 
    'interval': u'Interval',
21
 
    'host': u'Host',
22
 
    'fingerprint': u'Fingerprint',
23
 
    'checker_running': u'Check Is Running',
24
 
    'last_enabled': u'Last Enabled',
25
 
    'checker': u'Checker',
26
 
    }
27
 
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok')
28
 
domain = 'se.bsnet.fukt'
29
 
busname = domain + '.Mandos'
30
 
server_path = '/'
31
 
server_interface = domain + '.Mandos'
32
 
client_interface = domain + '.Mandos.Client'
33
 
version = "1.0.14"
34
 
bus = dbus.SystemBus()
35
 
mandos_dbus_objc = bus.get_object(busname, server_path)
36
 
mandos_serv = dbus.Interface(mandos_dbus_objc,
37
 
                             dbus_interface = server_interface)
38
 
mandos_clients = mandos_serv.GetAllClientsWithProperties()
39
 
 
40
 
def timedelta_to_milliseconds(td):
41
 
    "Convert a datetime.timedelta object to milliseconds"
42
 
    return ((td.days * 24 * 60 * 60 * 1000)
43
 
            + (td.seconds * 1000)
44
 
            + (td.microseconds // 1000))
45
 
 
46
 
def milliseconds_to_string(ms):
47
 
    td = datetime.timedelta(0, 0, 0, ms)
48
 
    return (u"%(days)s%(hours)02d:%(minutes)02d:%(seconds)02d"
49
 
            % { "days": "%dT" % td.days if td.days else "",
50
 
                "hours": td.seconds // 3600,
51
 
                "minutes": (td.seconds % 3600) // 60,
52
 
                "seconds": td.seconds % 60,
53
 
                })
54
 
 
55
 
 
56
 
def string_to_delta(interval):
57
 
    """Parse a string and return a datetime.timedelta
58
 
 
59
 
    >>> string_to_delta('7d')
60
 
    datetime.timedelta(7)
61
 
    >>> string_to_delta('60s')
62
 
    datetime.timedelta(0, 60)
63
 
    >>> string_to_delta('60m')
64
 
    datetime.timedelta(0, 3600)
65
 
    >>> string_to_delta('24h')
66
 
    datetime.timedelta(1)
67
 
    >>> string_to_delta(u'1w')
68
 
    datetime.timedelta(7)
69
 
    >>> string_to_delta('5m 30s')
70
 
    datetime.timedelta(0, 330)
71
 
    """
72
 
    timevalue = datetime.timedelta(0)
73
 
    regexp = re.compile("\d+[dsmhw]")
74
 
    
75
 
    for s in regexp.findall(interval):
76
 
        try:
77
 
            suffix = unicode(s[-1])
78
 
            value = int(s[:-1])
79
 
            if suffix == u"d":
80
 
                delta = datetime.timedelta(value)
81
 
            elif suffix == u"s":
82
 
                delta = datetime.timedelta(0, value)
83
 
            elif suffix == u"m":
84
 
                delta = datetime.timedelta(0, 0, 0, 0, value)
85
 
            elif suffix == u"h":
86
 
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
87
 
            elif suffix == u"w":
88
 
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
89
 
            else:
90
 
                raise ValueError
91
 
        except (ValueError, IndexError):
92
 
            raise ValueError
93
 
        timevalue += delta
94
 
    return timevalue
95
 
 
96
 
def print_clients(clients):
97
 
    def valuetostring(value, keyword):
98
 
        if type(value) is dbus.Boolean:
99
 
            return u"Yes" if value else u"No"
100
 
        if keyword in (u"timeout", u"interval"):
101
 
            return milliseconds_to_string(value)
102
 
        return unicode(value)
103
 
    
104
 
    # Create format string to print table rows
105
 
    format_string = u' '.join(u'%%-%ds' %
106
 
                              max(len(tablewords[key]),
107
 
                                  max(len(valuetostring(client[key], key))
108
 
                                      for client in
109
 
                                      clients))
110
 
                              for key in keywords)
111
 
    # Print header line
112
 
    print format_string % tuple(tablewords[key] for key in keywords)
113
 
    for client in clients:
114
 
        print format_string % tuple(valuetostring(client[key], key)
115
 
                                    for key in keywords)
116
 
 
117
 
parser = OptionParser(version = "%%prog %s" % version)
118
 
parser.add_option("-a", "--all", action="store_true",
119
 
                  help="Print all fields")
120
 
parser.add_option("-e", "--enable", action="store_true",
121
 
                  help="Enable client")
122
 
parser.add_option("-d", "--disable", action="store_true",
123
 
                  help="disable client")
124
 
parser.add_option("-b", "--bump-timeout", action="store_true",
125
 
                  help="Bump timeout for client")
126
 
parser.add_option("--start-checker", action="store_true",
127
 
                  help="Start checker for client")
128
 
parser.add_option("--stop-checker", action="store_true",
129
 
                  help="Stop checker for client")
130
 
parser.add_option("-V", "--is-enabled", action="store_true",
131
 
                  help="Check if client is enabled")
132
 
parser.add_option("-r", "--remove", action="store_true",
133
 
                  help="Remove client")
134
 
parser.add_option("-c", "--checker", type="string",
135
 
                  help="Set checker command for client")
136
 
parser.add_option("-t", "--timeout", type="string",
137
 
                  help="Set timeout for client")
138
 
parser.add_option("-i", "--interval", type="string",
139
 
                  help="Set checker interval for client")
140
 
parser.add_option("-H", "--host", type="string",
141
 
                  help="Set host for client")
142
 
parser.add_option("-s", "--secret", type="string",
143
 
                  help="Set password blob (file) for client")
144
 
options, client_names = parser.parse_args()
145
 
 
146
 
# Compile list of clients to process
147
 
clients=[]
148
 
for name in client_names:
149
 
    for path, client in mandos_clients.iteritems():
150
 
        if client['name'] == name:
151
 
            client_objc = bus.get_object(busname, path)
152
 
            clients.append(client_objc)
153
 
            break
154
 
    else:
155
 
        print >> sys.stderr, "Client not found on server: %r" % name
156
 
        sys.exit(1)
157
 
 
158
 
if not clients and mandos_clients.values():
159
 
    keywords = defaultkeywords
160
 
    if options.all:
161
 
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
162
 
                    'created', 'interval', 'host', 'fingerprint',
163
 
                    'checker_running', 'last_enabled', 'checker')
164
 
    print_clients(mandos_clients.values())
165
 
 
166
 
# Process each client in the list by all selected options
167
 
for client in clients:
168
 
    if options.remove:
169
 
        mandos_serv.RemoveClient(client.__dbus_object_path__)
170
 
    if options.enable:
171
 
        client.Enable(dbus_interface=client_interface)
172
 
    if options.disable:
173
 
        client.Disable(dbus_interface=client_interface)
174
 
    if options.bump_timeout:
175
 
        client.CheckedOK(dbus_interface=client_interface)
176
 
    if options.start_checker:
177
 
        client.StartChecker(dbus_interface=client_interface)
178
 
    if options.stop_checker:
179
 
        client.StopChecker(dbus_interface=client_interface)
180
 
    if options.is_enabled:
181
 
        sys.exit(0 if client.Get(client_interface,
182
 
                                 u"enabled",
183
 
                                 dbus_interface=dbus.PROPERTIES_IFACE)
184
 
                 else 1)
185
 
    if options.checker:
186
 
        client.Set(client_interface, u"checker", options.checker,
187
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
188
 
    if options.host:
189
 
        client.Set(client_interface, u"host", options.host,
190
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
191
 
    if options.interval:
192
 
        client.Set(client_interface, u"interval",
193
 
                   timedelta_to_milliseconds
194
 
                   (string_to_delta(options.interval)),
195
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
196
 
    if options.timeout:
197
 
        client.Set(client_interface, u"timeout",
198
 
                   timedelta_to_milliseconds(string_to_delta
199
 
                                             (options.timeout)),
200
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
201
 
    if options.secret:
202
 
        client.Set(client_interface, u"secret",
203
 
                   dbus.ByteArray(open(options.secret, u'rb').read()),
204
 
                   dbus_interface=dbus.PROPERTIES_IFACE)