/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-04-04 23:51:07 UTC
  • mfrom: (24.1.147 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20100404235107-mjnuq6gjdeq030w7
MergeĀ fromĀ Belorn.

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