27
27
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
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'
 
 
29
domain = 'se.bsnet.fukt'
 
 
30
busname = domain + '.Mandos'
 
 
32
server_interface = domain + '.Mandos'
 
 
33
client_interface = domain + '.Mandos.Client'
 
35
35
bus = dbus.SystemBus()
 
36
36
mandos_dbus_objc = bus.get_object(busname, server_path)
 
37
37
mandos_serv = dbus.Interface(mandos_dbus_objc,
 
38
38
                             dbus_interface = server_interface)
 
39
39
mandos_clients = mandos_serv.GetAllClientsWithProperties()
 
41
 
def datetime_to_milliseconds(dt):
 
42
 
    "Return the 'timeout' attribute in milliseconds"
 
43
 
    return ((dt.days * 24 * 60 * 60 * 1000)
 
45
 
            + (dt.microseconds // 1000))
 
 
41
def timedelta_to_milliseconds(td):
 
 
42
    "Convert a datetime.timedelta object to milliseconds"
 
 
43
    return ((td.days * 24 * 60 * 60 * 1000)
 
 
45
            + (td.microseconds // 1000))
 
47
47
def milliseconds_to_string(ms):
 
48
48
    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
 
 
49
    return (u"%(days)s%(hours)02d:%(minutes)02d:%(seconds)02d"
 
 
50
            % { "days": "%dT" % td.days if td.days else "",
 
 
51
                "hours": td.seconds // 3600,
 
 
52
                "minutes": (td.seconds % 3600) // 60,
 
 
53
                "seconds": td.seconds % 60,
 
55
57
def string_to_delta(interval):
 
 
115
119
parser.add_option("-a", "--all", action="store_true",
 
116
120
                  help="Print all fields")
 
117
121
parser.add_option("-e", "--enable", action="store_true",
 
118
 
                  help="Enable specified client")
 
 
122
                  help="Enable client")
 
119
123
parser.add_option("-d", "--disable", action="store_true",
 
120
 
                  help="disable specified client")
 
 
124
                  help="disable client")
 
121
125
parser.add_option("-b", "--bump-timeout", action="store_true",
 
122
 
                  help="Bump timeout of specified client")
 
 
126
                  help="Bump timeout for client")
 
123
127
parser.add_option("--start-checker", action="store_true",
 
124
 
                  help="Start checker for specified client")
 
 
128
                  help="Start checker for client")
 
125
129
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")
 
 
130
                  help="Stop checker for client")
 
 
131
parser.add_option("-V", "--is-valid", action="store_true",
 
 
132
                  help="Check if client is still valid")
 
 
133
parser.add_option("-r", "--remove", action="store_true",
 
 
134
                  help="Remove client")
 
129
135
parser.add_option("-c", "--checker", type="string",
 
130
 
                  help="Set checker command for specified client")
 
 
136
                  help="Set checker command for client")
 
131
137
parser.add_option("-t", "--timeout", type="string",
 
132
 
                  help="Set timeout for specified client")
 
 
138
                  help="Set timeout for client")
 
133
139
parser.add_option("-i", "--interval", type="string",
 
134
 
                  help="Set checker interval for specified client")
 
 
140
                  help="Set checker interval for client")
 
135
141
parser.add_option("-H", "--host", type="string",
 
136
 
                  help="Set host for specified client")
 
 
142
                  help="Set host for client")
 
137
143
parser.add_option("-s", "--secret", type="string",
 
138
 
                  help="Set password blob (file) for specified client")
 
 
144
                  help="Set password blob (file) for client")
 
139
145
options, client_names = parser.parse_args()
 
 
147
# Compile list of clients to process
 
142
149
for name in client_names:
 
143
150
    for path, client in mandos_clients.iteritems():
 
144
151
        if client['name'] == name:
 
145
152
            client_objc = bus.get_object(busname, path)
 
146
 
            clients.append(dbus.Interface(client_objc,
 
 
153
            clients.append(client_objc)
 
151
156
        print >> sys.stderr, "Client not found on server: %r" % name
 
 
159
if not clients and mandos_clients.values():
 
155
160
    keywords = defaultkeywords
 
157
162
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
158
163
                    'created', 'interval', 'host', 'fingerprint',
 
159
164
                    'checker_running', 'last_enabled', 'checker')
 
160
165
    print_clients(mandos_clients.values())
 
 
167
# Process each client in the list by all selected options
 
162
168
for client in clients:
 
 
170
        mandos_serv.RemoveClient(client.__dbus_object_path__)
 
163
171
    if options.enable:
 
 
172
        client.Enable(dbus_interface=client_interface)
 
165
173
    if options.disable:
 
 
174
        client.Disable(dbus_interface=client_interface)
 
167
175
    if options.bump_timeout:
 
 
176
        client.CheckedOK(dbus_interface=client_interface)
 
169
177
    if options.start_checker:
 
170
 
        client.StartChecker()
 
 
178
        client.StartChecker(dbus_interface=client_interface)
 
171
179
    if options.stop_checker:
 
 
180
        client.StopChecker(dbus_interface=client_interface)
 
173
181
    if options.is_valid:
 
174
 
        sys.exit(0 if client.IsStillValid() else 1)
 
 
182
        sys.exit(0 if client.Get(client_interface,
 
 
184
                                 dbus_interface=dbus.PROPERTIES_IFACE)
 
175
186
    if options.checker:
 
176
 
        client.SetChecker(options.checker)
 
 
187
        client.Set(client_interface, u"checker", options.checker,
 
 
188
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
178
 
        client.SetHost(options.host)
 
 
190
        client.Set(client_interface, u"host", options.host,
 
 
191
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
179
192
    if options.interval:
 
180
 
        client.SetInterval(datetime_to_milliseconds
 
181
 
                           (string_to_delta(options.interval)))
 
 
193
        client.Set(client_interface, u"interval",
 
 
194
                   timedelta_to_milliseconds
 
 
195
                   (string_to_delta(options.interval)),
 
 
196
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
182
197
    if options.timeout:
 
183
 
        client.SetTimeout(datetime_to_milliseconds
 
184
 
                          (string_to_delta(options.timeout)))
 
 
198
        client.Set(client_interface, u"timeout",
 
 
199
                   timedelta_to_milliseconds(string_to_delta
 
 
201
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
185
202
    if options.secret:
 
186
 
        client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read()))
 
 
203
        client.Set(client_interface, u"secret",
 
 
204
                   dbus.ByteArray(open(options.secret, u'rb').read()),
 
 
205
                   dbus_interface=dbus.PROPERTIES_IFACE)