/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-12 02:51:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100912025119-7vvpqofpnbxf7i22
Rename all D-Bus properties to conform to D-Bus naming conventions;
all programs changed.

Also rename client setting "approved_delay" to "approval_delay", and
"approved_duration" to "approval_duration"; all programs changed.

* mandos (ClientDBus.NeedApproval): Bug fix: send an UInt64, not a
                                    float.
  (ClientDBus.ApprovedByDefault_dbus_property,
  ClientDBus.ApprovalDelay_dbus_property,
  ClientDBus.ApprovalDuration_dbus_property): Bug fix: send
                                              PropertyChanged signal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
locale.setlocale(locale.LC_ALL, u'')
13
13
 
14
14
tablewords = {
15
 
    'name': u'Name',
16
 
    'enabled': u'Enabled',
17
 
    'timeout': u'Timeout',
18
 
    'last_checked_ok': u'Last Successful Check',
19
 
    'created': u'Created',
20
 
    'interval': u'Interval',
21
 
    'host': u'Host',
22
 
    'fingerprint': u'Fingerprint',
23
 
    'checker_running': u'Check Is Running',
24
 
    'last_enabled': u'Last Enabled',
25
 
    'checker': u'Checker',
 
15
    'Name': u'Name',
 
16
    'Enabled': u'Enabled',
 
17
    'Timeout': u'Timeout',
 
18
    'LastCheckedOK': u'Last Successful Check',
 
19
    'Created': u'Created',
 
20
    'Interval': u'Interval',
 
21
    'Host': u'Host',
 
22
    'Fingerprint': u'Fingerprint',
 
23
    'CheckerRunning': u'Check Is Running',
 
24
    'LastEnabled': u'Last Enabled',
 
25
    'Checker': u'Checker',
26
26
    }
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.5"
34
 
 
35
 
bus = dbus.SystemBus()
36
 
mandos_dbus_objc = bus.get_object(busname, server_path)
 
27
defaultkeywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK')
 
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"
 
34
try:
 
35
    bus = dbus.SystemBus()
 
36
    mandos_dbus_objc = bus.get_object(busname, server_path)
 
37
except dbus.exceptions.DBusException:
 
38
    sys.exit(1)
 
39
    
37
40
mandos_serv = dbus.Interface(mandos_dbus_objc,
38
41
                             dbus_interface = server_interface)
39
42
mandos_clients = mandos_serv.GetAllClientsWithProperties()
40
43
 
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))
 
44
def timedelta_to_milliseconds(td):
 
45
    "Convert a datetime.timedelta object to milliseconds"
 
46
    return ((td.days * 24 * 60 * 60 * 1000)
 
47
            + (td.seconds * 1000)
 
48
            + (td.microseconds // 1000))
46
49
 
47
50
def milliseconds_to_string(ms):
48
51
    td = datetime.timedelta(0, 0, 0, ms)
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
 
52
    return (u"%(days)s%(hours)02d:%(minutes)02d:%(seconds)02d"
 
53
            % { "days": "%dT" % td.days if td.days else "",
 
54
                "hours": td.seconds // 3600,
 
55
                "minutes": (td.seconds % 3600) // 60,
 
56
                "seconds": td.seconds % 60,
 
57
                })
53
58
 
54
59
 
55
60
def string_to_delta(interval):
96
101
    def valuetostring(value, keyword):
97
102
        if type(value) is dbus.Boolean:
98
103
            return u"Yes" if value else u"No"
99
 
        if keyword in ("timeout", "interval"):
 
104
        if keyword in (u"timeout", u"interval"):
100
105
            return milliseconds_to_string(value)
101
106
        return unicode(value)
102
107
    
 
108
    # Create format string to print table rows
103
109
    format_string = u' '.join(u'%%-%ds' %
104
110
                              max(len(tablewords[key]),
105
 
                                  max(len(valuetostring(client[key], key))
 
111
                                  max(len(valuetostring(client[key],
 
112
                                                        key))
106
113
                                      for client in
107
114
                                      clients))
108
115
                              for key in keywords)
109
 
    print format_string % tuple(tablewords[key] for key in keywords) 
 
116
    # Print header line
 
117
    print format_string % tuple(tablewords[key] for key in keywords)
110
118
    for client in clients:
111
119
        print format_string % tuple(valuetostring(client[key], key)
112
120
                                    for key in keywords)
115
123
parser.add_option("-a", "--all", action="store_true",
116
124
                  help="Print all fields")
117
125
parser.add_option("-e", "--enable", action="store_true",
118
 
                  help="Enable specified client")
 
126
                  help="Enable client")
119
127
parser.add_option("-d", "--disable", action="store_true",
120
 
                  help="disable specified client")
 
128
                  help="disable client")
121
129
parser.add_option("-b", "--bump-timeout", action="store_true",
122
 
                  help="Bump timeout of specified client")
 
130
                  help="Bump timeout for client")
123
131
parser.add_option("--start-checker", action="store_true",
124
 
                  help="Start checker for specified client")
 
132
                  help="Start checker for client")
125
133
parser.add_option("--stop-checker", action="store_true",
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
                  help="Stop checker for client")
 
135
parser.add_option("-V", "--is-enabled", action="store_true",
 
136
                  help="Check if client is enabled")
 
137
parser.add_option("-r", "--remove", action="store_true",
 
138
                  help="Remove client")
129
139
parser.add_option("-c", "--checker", type="string",
130
 
                  help="Set checker command for specified client")
 
140
                  help="Set checker command for client")
131
141
parser.add_option("-t", "--timeout", type="string",
132
 
                  help="Set timeout for specified client")
 
142
                  help="Set timeout for client")
133
143
parser.add_option("-i", "--interval", type="string",
134
 
                  help="Set checker interval for specified client")
 
144
                  help="Set checker interval for client")
135
145
parser.add_option("-H", "--host", type="string",
136
 
                  help="Set host for specified client")
 
146
                  help="Set host for client")
137
147
parser.add_option("-s", "--secret", type="string",
138
 
                  help="Set password blob (file) for specified client")
 
148
                  help="Set password blob (file) for client")
 
149
parser.add_option("-A", "--approve", action="store_true",
 
150
                  help="Approve any current client request")
 
151
parser.add_option("-D", "--deny", action="store_true",
 
152
                  help="Deny any current client request")
139
153
options, client_names = parser.parse_args()
140
154
 
 
155
# Compile list of clients to process
141
156
clients=[]
142
157
for name in client_names:
143
158
    for path, client in mandos_clients.iteritems():
144
159
        if client['name'] == name:
145
160
            client_objc = bus.get_object(busname, path)
146
 
            clients.append(dbus.Interface(client_objc,
147
 
                                          dbus_interface
148
 
                                          = client_interface))
 
161
            clients.append(client_objc)
149
162
            break
150
163
    else:
151
164
        print >> sys.stderr, "Client not found on server: %r" % name
152
165
        sys.exit(1)
153
166
 
154
 
if not clients:
 
167
if not clients and mandos_clients.values():
155
168
    keywords = defaultkeywords
156
169
    if options.all:
157
 
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
158
 
                    'created', 'interval', 'host', 'fingerprint',
159
 
                    'checker_running', 'last_enabled', 'checker')
 
170
        keywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK',
 
171
                    'Created', 'Interval', 'Host', 'Fingerprint',
 
172
                    'CheckerRunning', 'LastEnabled', 'Checker')
160
173
    print_clients(mandos_clients.values())
161
 
    
 
174
 
 
175
# Process each client in the list by all selected options
162
176
for client in clients:
 
177
    if options.remove:
 
178
        mandos_serv.RemoveClient(client.__dbus_object_path__)
163
179
    if options.enable:
164
 
        client.Enable()
 
180
        client.Enable(dbus_interface=client_interface)
165
181
    if options.disable:
166
 
        client.Disable()
 
182
        client.Disable(dbus_interface=client_interface)
167
183
    if options.bump_timeout:
168
 
        client.BumpTimeout()
 
184
        client.CheckedOK(dbus_interface=client_interface)
169
185
    if options.start_checker:
170
 
        client.StartChecker()
 
186
        client.StartChecker(dbus_interface=client_interface)
171
187
    if options.stop_checker:
172
 
        client.StopChecker()
173
 
    if options.is_valid:
174
 
        sys.exit(0 if client.IsStillValid() else 1)
 
188
        client.StopChecker(dbus_interface=client_interface)
 
189
    if options.is_enabled:
 
190
        sys.exit(0 if client.Get(client_interface,
 
191
                                 u"Enabled",
 
192
                                 dbus_interface=dbus.PROPERTIES_IFACE)
 
193
                 else 1)
175
194
    if options.checker:
176
 
        client.SetChecker(options.checker)
 
195
        client.Set(client_interface, u"Checker", options.checker,
 
196
                   dbus_interface=dbus.PROPERTIES_IFACE)
177
197
    if options.host:
178
 
        client.SetHost(options.host)
 
198
        client.Set(client_interface, u"Host", options.host,
 
199
                   dbus_interface=dbus.PROPERTIES_IFACE)
179
200
    if options.interval:
180
 
        client.SetInterval(datetime_to_milliseconds
181
 
                           (string_to_delta(options.interval)))
 
201
        client.Set(client_interface, u"Interval",
 
202
                   timedelta_to_milliseconds
 
203
                   (string_to_delta(options.interval)),
 
204
                   dbus_interface=dbus.PROPERTIES_IFACE)
182
205
    if options.timeout:
183
 
        client.SetTimeout(datetime_to_milliseconds
184
 
                          (string_to_delta(options.timeout)))
 
206
        client.Set(client_interface, u"Timeout",
 
207
                   timedelta_to_milliseconds(string_to_delta
 
208
                                             (options.timeout)),
 
209
                   dbus_interface=dbus.PROPERTIES_IFACE)
185
210
    if options.secret:
186
 
        client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read()))
187
 
    
 
211
        client.Set(client_interface, u"Secret",
 
212
                   dbus.ByteArray(open(options.secret, u'rb').read()),
 
213
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
214
    if options.approve:
 
215
        client.Approve(dbus.Boolean(True),
 
216
                       dbus_interface=client_interface)
 
217
    if options.deny:
 
218
        client.Approve(dbus.Boolean(False),
 
219
                       dbus_interface=client_interface)