/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 from Björn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    'Fingerprint': u'Fingerprint',
24
24
    'CheckerRunning': u'Check Is Running',
25
25
    'LastEnabled': u'Last Enabled',
26
 
    'ApprovalPending': u'Approval Is Pending',
27
 
    'ApprovedByDefault': u'Approved By Default',
28
 
    'ApprovalDelay': u"Approval Delay",
29
 
    'ApprovalDuration': u"Approval Duration",
30
26
    'Checker': u'Checker',
31
27
    }
32
28
defaultkeywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK')
97
93
    def valuetostring(value, keyword):
98
94
        if type(value) is dbus.Boolean:
99
95
            return u"Yes" if value else u"No"
100
 
        if keyword in (u"Timeout", u"Interval", u"ApprovalDelay",
101
 
                       u"ApprovalDuration"):
 
96
        if keyword in (u"timeout", u"interval"):
102
97
            return milliseconds_to_string(value)
103
98
        return unicode(value)
104
99
    
115
110
    for client in clients:
116
111
        print format_string % tuple(valuetostring(client[key], key)
117
112
                                    for key in keywords)
118
 
 
119
113
def has_actions(options):
120
114
    return any((options.enable,
121
115
                options.disable,
127
121
                options.checker is not None,
128
122
                options.timeout is not None,
129
123
                options.interval is not None,
130
 
                options.approved_by_default is not None,
131
 
                options.approval_delay is not None,
132
 
                options.approval_duration is not None,
133
124
                options.host is not None,
134
125
                options.secret is not None,
135
126
                options.approve,
161
152
                          help="Set timeout for client")
162
153
        parser.add_option("-i", "--interval", type="string",
163
154
                          help="Set checker interval for client")
164
 
        parser.add_option("--approve-by-default", action="store_true",
165
 
                          dest=u"approved_by_default",
166
 
                          help="Set client to be approved by default")
167
 
        parser.add_option("--deny-by-default", action="store_false",
168
 
                          dest=u"approved_by_default",
169
 
                          help="Set client to be denied by default")
170
 
        parser.add_option("--approval-delay", type="string",
171
 
                          help="Set delay before client approve/deny")
172
 
        parser.add_option("--approval-duration", type="string",
173
 
                          help="Set duration of one client approval")
174
155
        parser.add_option("-H", "--host", type="string",
175
156
                          help="Set host for client")
176
157
        parser.add_option("-s", "--secret", type="string",
180
161
        parser.add_option("-D", "--deny", action="store_true",
181
162
                          help="Deny any current client request")
182
163
        options, client_names = parser.parse_args()
183
 
        
 
164
 
184
165
        if has_actions(options) and not client_names and not options.all:
185
 
            parser.error('Options require clients names or --all.')
 
166
            parser.error('Options requires clients names or --all.')
186
167
        if options.verbose and has_actions(options):
187
 
            parser.error('--verbose can only be used alone or with'
188
 
                         ' --all.')
 
168
            parser.error('Verbose option can only be used alone or with --all.')
189
169
        if options.all and not has_actions(options):
190
 
            parser.error('--all requires an action.')
191
 
        
 
170
            parser.error('--all requires an action')
 
171
            
192
172
        try:
193
173
            bus = dbus.SystemBus()
194
174
            mandos_dbus_objc = bus.get_object(busname, server_path)
215
195
            print >> sys.stderr, "Access denied: Accessing mandos server through dbus."
216
196
            sys.exit(1)
217
197
            
218
 
        # Compile dict of (clients: properties) to process
219
 
        clients={}
220
 
        
 
198
        # Compile list of clients to process
 
199
        clients=[]
 
200
 
221
201
        if options.all or not client_names:
222
 
            clients = dict((bus.get_object(busname, path), properties)
223
 
                           for path, properties in
224
 
                           mandos_clients.iteritems())
 
202
            clients = (bus.get_object(busname, path) for path in mandos_clients.iterkeys())
225
203
        else:
226
204
            for name in client_names:
227
205
                for path, client in mandos_clients.iteritems():
228
206
                    if client['Name'] == name:
229
207
                        client_objc = bus.get_object(busname, path)
230
 
                        clients[client_objc] = client
 
208
                        clients.append(client_objc)
231
209
                        break
232
210
                else:
233
211
                    print >> sys.stderr, "Client not found on server: %r" % name
235
213
            
236
214
        if not has_actions(options) and clients:
237
215
            if options.verbose:
238
 
                keywords = ('Name', 'Enabled', 'Timeout',
239
 
                            'LastCheckedOK', 'Created', 'Interval',
240
 
                            'Host', 'Fingerprint', 'CheckerRunning',
241
 
                            'LastEnabled', 'ApprovalPending',
242
 
                            'ApprovedByDefault', 'ApprovalDelay',
243
 
                            'ApprovalDuration', 'Checker')
 
216
                keywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK',
 
217
                            'Created', 'Interval', 'Host', 'Fingerprint',
 
218
                            'CheckerRunning', 'LastEnabled', 'Checker')
244
219
            else:
245
220
                keywords = defaultkeywords
246
221
                
247
 
            print_clients(clients.values(), keywords)
 
222
            print_clients(mandos_clients.values(), keywords)
248
223
        else:
249
224
            # Process each client in the list by all selected options
250
225
            for client in clients:
276
251
                               timedelta_to_milliseconds
277
252
                               (string_to_delta(options.interval)),
278
253
                               dbus_interface=dbus.PROPERTIES_IFACE)
279
 
                if options.approval_delay:
280
 
                    client.Set(client_interface, u"ApprovalDelay",
281
 
                               timedelta_to_milliseconds
282
 
                               (string_to_delta(options.
283
 
                                                approval_delay)),
284
 
                               dbus_interface=dbus.PROPERTIES_IFACE)
285
 
                if options.approval_duration:
286
 
                    client.Set(client_interface, u"ApprovalDuration",
287
 
                               timedelta_to_milliseconds
288
 
                               (string_to_delta(options.
289
 
                                                approval_duration)),
290
 
                               dbus_interface=dbus.PROPERTIES_IFACE)
291
254
                if options.timeout:
292
255
                    client.Set(client_interface, u"Timeout",
293
 
                               timedelta_to_milliseconds
294
 
                               (string_to_delta(options.timeout)),
 
256
                               timedelta_to_milliseconds(string_to_delta
 
257
                                                         (options.timeout)),
295
258
                               dbus_interface=dbus.PROPERTIES_IFACE)
296
259
                if options.secret:
297
260
                    client.Set(client_interface, u"Secret",
298
 
                               dbus.ByteArray(open(options.secret,
299
 
                                                   u'rb').read()),
300
 
                               dbus_interface=dbus.PROPERTIES_IFACE)
301
 
                if options.approved_by_default is not None:
302
 
                    client.Set(client_interface, u"ApprovedByDefault",
303
 
                               dbus.Boolean(options
304
 
                                            .approved_by_default),
 
261
                               dbus.ByteArray(open(options.secret, u'rb').read()),
305
262
                               dbus_interface=dbus.PROPERTIES_IFACE)
306
263
                if options.approve:
307
 
                    client.Approve(dbus.Boolean(True),
308
 
                                   dbus_interface=client_interface)
309
 
                elif options.deny:
310
 
                    client.Approve(dbus.Boolean(False),
311
 
                                   dbus_interface=client_interface)
 
264
                    client.Approve(dbus.Boolean(True), dbus_interface=client_interface)
 
265
                if options.deny:
 
266
                    client.Approve(dbus.Boolean(False), dbus_interface=client_interface)
312
267
 
313
268
if __name__ == '__main__':
314
269
    main()