/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: 2009-02-23 11:52:42 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090223115242-5930ewin7m4o6zpr
* README (The Plugin System): Improve wording.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
locale.setlocale(locale.LC_ALL, u'')
13
13
 
14
14
tablewords = {
15
 
    'Name': u'Name',
16
 
    'Enabled': u'Enabled',
17
 
    'Timeout': u'Timeout',
18
 
    'LastCheckedOK': u'Last Successful Check',
19
 
    'Created': u'Created',
20
 
    'Interval': u'Interval',
21
 
    'Host': u'Host',
22
 
    'Fingerprint': u'Fingerprint',
23
 
    'CheckerRunning': u'Check Is Running',
24
 
    'LastEnabled': u'Last Enabled',
25
 
    'Checker': u'Checker',
 
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
26
    }
27
 
defaultkeywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK')
 
27
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
28
                   'checker')
28
29
domain = 'se.bsnet.fukt'
29
30
busname = domain + '.Mandos'
30
31
server_path = '/'
31
32
server_interface = domain + '.Mandos'
32
33
client_interface = domain + '.Mandos.Client'
33
 
version = "1.0.14"
34
 
try:
35
 
    bus = dbus.SystemBus()
36
 
    mandos_dbus_objc = bus.get_object(busname, server_path)
37
 
except dbus.exceptions.DBusException:
38
 
    sys.exit(1)
39
 
    
 
34
version = "1.0.6"
 
35
bus = dbus.SystemBus()
 
36
mandos_dbus_objc = bus.get_object(busname, server_path)
40
37
mandos_serv = dbus.Interface(mandos_dbus_objc,
41
38
                             dbus_interface = server_interface)
42
39
mandos_clients = mandos_serv.GetAllClientsWithProperties()
43
40
 
44
 
def timedelta_to_milliseconds(td):
45
 
    "Convert a datetime.timedelta object to milliseconds"
46
 
    return ((td.days * 24 * 60 * 60 * 1000)
47
 
            + (td.seconds * 1000)
48
 
            + (td.microseconds // 1000))
 
41
def datetime_to_milliseconds(dt):
 
42
    "Return the 'timeout' attribute in milliseconds"
 
43
    return ((dt.days * 24 * 60 * 60 * 1000)
 
44
            + (dt.seconds * 1000)
 
45
            + (dt.microseconds // 1000))
49
46
 
50
47
def milliseconds_to_string(ms):
51
48
    td = datetime.timedelta(0, 0, 0, ms)
52
 
    return (u"%(days)s%(hours)02d:%(minutes)02d:%(seconds)02d"
53
 
            % { "days": "%dT" % td.days if td.days else "",
54
 
                "hours": td.seconds // 3600,
55
 
                "minutes": (td.seconds % 3600) // 60,
56
 
                "seconds": td.seconds % 60,
57
 
                })
 
49
    return "%s%02d:%02d:%02d" % (("%dT" % td.days) if td.days else "", # days
 
50
                           td.seconds // 3600,        # hours
 
51
                           (td.seconds % 3600) // 60, # minutes
 
52
                           (td.seconds % 60))         # seconds
58
53
 
59
54
 
60
55
def string_to_delta(interval):
101
96
    def valuetostring(value, keyword):
102
97
        if type(value) is dbus.Boolean:
103
98
            return u"Yes" if value else u"No"
104
 
        if keyword in (u"timeout", u"interval"):
 
99
        if keyword in ("timeout", "interval"):
105
100
            return milliseconds_to_string(value)
106
101
        return unicode(value)
107
102
    
108
 
    # Create format string to print table rows
109
103
    format_string = u' '.join(u'%%-%ds' %
110
104
                              max(len(tablewords[key]),
111
 
                                  max(len(valuetostring(client[key],
112
 
                                                        key))
 
105
                                  max(len(valuetostring(client[key], key))
113
106
                                      for client in
114
107
                                      clients))
115
108
                              for key in keywords)
116
 
    # Print header line
117
 
    print format_string % tuple(tablewords[key] for key in keywords)
 
109
    print format_string % tuple(tablewords[key] for key in keywords) 
118
110
    for client in clients:
119
111
        print format_string % tuple(valuetostring(client[key], key)
120
112
                                    for key in keywords)
132
124
                  help="Start checker for client")
133
125
parser.add_option("--stop-checker", action="store_true",
134
126
                  help="Stop checker for client")
135
 
parser.add_option("-V", "--is-enabled", action="store_true",
136
 
                  help="Check if client is enabled")
 
127
parser.add_option("-V", "--is-valid", action="store_true",
 
128
                  help="Check if client is still valid")
137
129
parser.add_option("-r", "--remove", action="store_true",
138
130
                  help="Remove client")
139
131
parser.add_option("-c", "--checker", type="string",
146
138
                  help="Set host for client")
147
139
parser.add_option("-s", "--secret", type="string",
148
140
                  help="Set password blob (file) for client")
149
 
parser.add_option("-A", "--approve", action="store_true",
150
 
                  help="Approve any current client request")
151
 
parser.add_option("-D", "--deny", action="store_true",
152
 
                  help="Deny any current client request")
153
141
options, client_names = parser.parse_args()
154
142
 
155
143
# Compile list of clients to process
158
146
    for path, client in mandos_clients.iteritems():
159
147
        if client['name'] == name:
160
148
            client_objc = bus.get_object(busname, path)
161
 
            clients.append(client_objc)
 
149
            clients.append(dbus.Interface(client_objc,
 
150
                                          dbus_interface
 
151
                                          = client_interface))
162
152
            break
163
153
    else:
164
154
        print >> sys.stderr, "Client not found on server: %r" % name
167
157
if not clients and mandos_clients.values():
168
158
    keywords = defaultkeywords
169
159
    if options.all:
170
 
        keywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK',
171
 
                    'Created', 'Interval', 'Host', 'Fingerprint',
172
 
                    'CheckerRunning', 'LastEnabled', 'Checker')
 
160
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
161
                    'created', 'interval', 'host', 'fingerprint',
 
162
                    'checker_running', 'last_enabled', 'checker')
173
163
    print_clients(mandos_clients.values())
174
164
 
175
165
# Process each client in the list by all selected options
177
167
    if options.remove:
178
168
        mandos_serv.RemoveClient(client.__dbus_object_path__)
179
169
    if options.enable:
180
 
        client.Enable(dbus_interface=client_interface)
 
170
        client.Enable()
181
171
    if options.disable:
182
 
        client.Disable(dbus_interface=client_interface)
 
172
        client.Disable()
183
173
    if options.bump_timeout:
184
 
        client.CheckedOK(dbus_interface=client_interface)
 
174
        client.BumpTimeout()
185
175
    if options.start_checker:
186
 
        client.StartChecker(dbus_interface=client_interface)
 
176
        client.StartChecker()
187
177
    if options.stop_checker:
188
 
        client.StopChecker(dbus_interface=client_interface)
189
 
    if options.is_enabled:
190
 
        sys.exit(0 if client.Get(client_interface,
191
 
                                 u"Enabled",
192
 
                                 dbus_interface=dbus.PROPERTIES_IFACE)
193
 
                 else 1)
 
178
        client.StopChecker()
 
179
    if options.is_valid:
 
180
        sys.exit(0 if client.IsStillValid() else 1)
194
181
    if options.checker:
195
 
        client.Set(client_interface, u"Checker", options.checker,
196
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
182
        client.SetChecker(options.checker)
197
183
    if options.host:
198
 
        client.Set(client_interface, u"Host", options.host,
199
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
184
        client.SetHost(options.host)
200
185
    if options.interval:
201
 
        client.Set(client_interface, u"Interval",
202
 
                   timedelta_to_milliseconds
203
 
                   (string_to_delta(options.interval)),
204
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
186
        client.SetInterval(datetime_to_milliseconds
 
187
                           (string_to_delta(options.interval)))
205
188
    if options.timeout:
206
 
        client.Set(client_interface, u"Timeout",
207
 
                   timedelta_to_milliseconds(string_to_delta
208
 
                                             (options.timeout)),
209
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
189
        client.SetTimeout(datetime_to_milliseconds
 
190
                          (string_to_delta(options.timeout)))
210
191
    if options.secret:
211
 
        client.Set(client_interface, u"Secret",
212
 
                   dbus.ByteArray(open(options.secret, u'rb').read()),
213
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
214
 
    if options.approve:
215
 
        client.Approve(dbus.Boolean(True),
216
 
                       dbus_interface=client_interface)
217
 
    if options.deny:
218
 
        client.Approve(dbus.Boolean(False),
219
 
                       dbus_interface=client_interface)
 
192
        client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read()))
 
193