/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: 2010-09-26 17:44:43 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100926174443-452k363uuvczuuzg
* mandos-ctl: Also show "LastApprovalRequest" property.

Show diffs side-by-side

added added

removed removed

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