/mandos/release

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

« back to all changes in this revision

Viewing changes to mandos-list

  • Committer: Björn Påhlsson
  • Date: 2009-01-01 20:09:00 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 245.
  • Revision ID: belorn@braxen-20090101200900-cz42yvy51d330k0x
Added enable/disable

Show diffs side-by-side

added added

removed removed

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