/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:36:30 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100926173630-zk7pe17fp2bv6zr7
* DBUS-API: Document new "LastApprovalRequest" client property.

* mandos (Client.last_approval_request): New attribute.
  (Client.need_approval): New method.
  (ClientDBus.need_approval): - '' -
  (ClientDBus.NeedApproval): Call self.need_approval().
  (ClientDBus.LastApprovalRequest_dbus_property): New D-Bus property.

* mandos-monitor: Show timeout counter during approval delay.
  (MandosClientWidget._update_timer_callback_lock): New.
  (MandosClientWidget.property_changed): Override to also call
                                         using_timer if
                                         ApprovalPending property is
                                         changed.
  (MandosClientWidget.using_timer): New method.
  (MandosClientWidget.checker_completed): Use "using_timer".
  (MandosClientWidget.need_approval): - '' -
  (MandosClientWidget.update): Show approval delay timer.

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