/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-ctl

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    'last_enabled': u'Last Enabled',
25
25
    'checker': u'Checker',
26
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"
 
27
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
28
                   'checker')
 
29
busname = 'org.mandos-system.Mandos'
 
30
server_path = '/Mandos'
 
31
server_interface = 'org.mandos_system.Mandos'
 
32
client_interface = 'org.mandos_system.Mandos.Client'
 
33
version = "1.0.2"
 
34
 
34
35
bus = dbus.SystemBus()
35
36
mandos_dbus_objc = bus.get_object(busname, server_path)
36
37
mandos_serv = dbus.Interface(mandos_dbus_objc,
37
38
                             dbus_interface = server_interface)
38
39
mandos_clients = mandos_serv.GetAllClientsWithProperties()
39
40
 
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))
 
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))
45
46
 
46
47
def milliseconds_to_string(ms):
47
48
    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
 
                })
 
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
54
53
 
55
54
 
56
55
def string_to_delta(interval):
97
96
    def valuetostring(value, keyword):
98
97
        if type(value) is dbus.Boolean:
99
98
            return u"Yes" if value else u"No"
100
 
        if keyword in (u"timeout", u"interval"):
 
99
        if keyword in ("timeout", "interval"):
101
100
            return milliseconds_to_string(value)
102
101
        return unicode(value)
103
102
    
104
 
    # Create format string to print table rows
105
103
    format_string = u' '.join(u'%%-%ds' %
106
104
                              max(len(tablewords[key]),
107
105
                                  max(len(valuetostring(client[key], key))
108
106
                                      for client in
109
107
                                      clients))
110
108
                              for key in keywords)
111
 
    # Print header line
112
 
    print format_string % tuple(tablewords[key] for key in keywords)
 
109
    print format_string % tuple(tablewords[key] for key in keywords) 
113
110
    for client in clients:
114
111
        print format_string % tuple(valuetostring(client[key], key)
115
112
                                    for key in keywords)
118
115
parser.add_option("-a", "--all", action="store_true",
119
116
                  help="Print all fields")
120
117
parser.add_option("-e", "--enable", action="store_true",
121
 
                  help="Enable client")
 
118
                  help="Enable specified client")
122
119
parser.add_option("-d", "--disable", action="store_true",
123
 
                  help="disable client")
 
120
                  help="disable specified client")
124
121
parser.add_option("-b", "--bump-timeout", action="store_true",
125
 
                  help="Bump timeout for client")
 
122
                  help="Bump timeout of specified client")
126
123
parser.add_option("--start-checker", action="store_true",
127
 
                  help="Start checker for client")
 
124
                  help="Start checker for specified client")
128
125
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")
 
126
                  help="Stop checker for specified client")
 
127
parser.add_option("-v", "--is-valid", action="store_true",
 
128
                  help="Stop checker for specified client")
134
129
parser.add_option("-c", "--checker", type="string",
135
 
                  help="Set checker command for client")
 
130
                  help="Set checker command for specified client")
136
131
parser.add_option("-t", "--timeout", type="string",
137
 
                  help="Set timeout for client")
 
132
                  help="Set timeout for specified client")
138
133
parser.add_option("-i", "--interval", type="string",
139
 
                  help="Set checker interval for client")
 
134
                  help="Set checker interval for specified client")
140
135
parser.add_option("-H", "--host", type="string",
141
 
                  help="Set host for client")
 
136
                  help="Set host for specified client")
142
137
parser.add_option("-s", "--secret", type="string",
143
 
                  help="Set password blob (file) for client")
 
138
                  help="Set password blob (file) for specified client")
144
139
options, client_names = parser.parse_args()
145
140
 
146
 
# Compile list of clients to process
147
141
clients=[]
148
142
for name in client_names:
149
143
    for path, client in mandos_clients.iteritems():
150
144
        if client['name'] == name:
151
145
            client_objc = bus.get_object(busname, path)
152
 
            clients.append(client_objc)
 
146
            clients.append(dbus.Interface(client_objc,
 
147
                                          dbus_interface
 
148
                                          = client_interface))
153
149
            break
154
150
    else:
155
151
        print >> sys.stderr, "Client not found on server: %r" % name
156
152
        sys.exit(1)
157
153
 
158
 
if not clients and mandos_clients.values():
 
154
if not clients:
159
155
    keywords = defaultkeywords
160
156
    if options.all:
161
157
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
162
158
                    'created', 'interval', 'host', 'fingerprint',
163
159
                    'checker_running', 'last_enabled', 'checker')
164
160
    print_clients(mandos_clients.values())
165
 
 
166
 
# Process each client in the list by all selected options
 
161
    
167
162
for client in clients:
168
 
    if options.remove:
169
 
        mandos_serv.RemoveClient(client.__dbus_object_path__)
170
163
    if options.enable:
171
 
        client.Enable(dbus_interface=client_interface)
 
164
        client.Enable()
172
165
    if options.disable:
173
 
        client.Disable(dbus_interface=client_interface)
 
166
        client.Disable()
174
167
    if options.bump_timeout:
175
 
        client.CheckedOK(dbus_interface=client_interface)
 
168
        client.BumpTimeout()
176
169
    if options.start_checker:
177
 
        client.StartChecker(dbus_interface=client_interface)
 
170
        client.StartChecker()
178
171
    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)
 
172
        client.StopChecker()
 
173
    if options.is_valid:
 
174
        sys.exit(0 if client.IsStillValid() else 1)
185
175
    if options.checker:
186
 
        client.Set(client_interface, u"checker", options.checker,
187
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
176
        client.SetChecker(options.checker)
188
177
    if options.host:
189
 
        client.Set(client_interface, u"host", options.host,
190
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
178
        client.SetHost(options.host)
191
179
    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)
 
180
        client.SetInterval(datetime_to_milliseconds
 
181
                           (string_to_delta(options.interval)))
196
182
    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)
 
183
        client.SetTimeout(datetime_to_milliseconds
 
184
                          (string_to_delta(options.timeout)))
201
185
    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)
 
186
        client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read()))
 
187