/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: 2009-01-18 00:16:57 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090118001657-6mo3ae73ldy5tegf
* debian/mandos-client.postinst: Converted to Bourne shell.  Also
                                 minor message change.
* debian/mandos-client.postrm: Minor message change.
* debian/mandos.postinst: Converted to Bourne shell.  Also minor
                          message change.
* debian/mandos.prerm: Minor message change.

Show diffs side-by-side

added added

removed removed

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