/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: 2019-02-09 23:23:26 UTC
  • Revision ID: teddy@recompile.se-20190209232326-z1z2kzpgfixz7iaj
Add support for using raw public keys in TLS (RFC 7250)

Since GnuTLS removed support for OpenPGP keys in TLS (RFC 6091), and
no other library supports it, we have to change the protocol to use
something else.  We choose to use "raw public keys" (RFC 7250).  Since
we still use OpenPGP keys to decrypt the secret password, this means
that each client will have two keys: One OpenPGP key and one TLS
public/private key, and the key ID of the latter key is used to
identify clients instead of the fingerprint of the OpenPGP key.

Note that this code is still compatible with GnuTLS before version
3.6.0 (when OpenPGP key support was removed).  This commit merely adds
support for using raw pulic keys instead with GnuTLS 3.6.6. or later.

* DBUS-API (Signals/ClientNotFound): Change name of first parameter
                                     from "Fingerprint" to "KeyID".
  (Mandos Client Interface/Properties/KeyID): New.
* INSTALL: Document conflict with GnuTLS 3.6.0 (which removed OpenPGP
           key support) up until 3.6.6, when support for raw public
           keys was added.  Also document new dependency of client on
           "gnutls-bin" package (for certtool).
* Makefile (run-client): Depend on TLS key files, and also pass them
                         as arguments to client.
  (keydir/tls-privkey.pem, keydir/tls-pubkey.pem): New.
  (confdir/clients.conf): Add dependency on TLS public key.
  (purge-client): Add removal of TLS key files.
* clients.conf ([foo]/key_id, [bar]/key_id): New.
* debian/control (Source: mandos/Build-Depends): Also allow
                                                 libgnutls30 (>= 3.6.6)
  (Package: mandos/Depends): - '' -
  (Package: mandos/Description): Alter description to match new
                                 design.
  (Package: mandos-client/Description): - '' -
  (Package: mandos-client/Depends): Move "gnutls-bin | openssl" to
                                    here from "Recommends".
* debian/mandos-client.README.Debian: Add --tls-privkey and
                                      --tls-pubkey options to test
                                      command.
* debian/mandos-client.postinst (create_key): Renamed to "create_keys"
                                             (all callers changed),
                                             and also create TLS key.
* debian/mandos-client.postrm (purge): Also remove TLS key files.
* intro.xml (DESCRIPTION): Describe new dual-key design.
* mandos (GnuTLS): Define different functions depending on whether
                   support for raw public keys is detected.
  (Client.key_id): New attribute.
  (ClientDBus.KeyID_dbus_property): New method.
  (ProxyClient.__init__): Take new "key_id" parameter.
  (ClientHandler.handle): Use key IDs when using raw public keys and
                          use fingerprints when using OpenPGP keys.
  (ClientHandler.peer_certificate): Also handle raw public keys.
  (ClientHandler.key_id): New.
  (MandosServer.handle_ipc): Pass key ID over the pipe IPC.  Also
                             check for key ID matches when looking up
                             clients.
  (main): Default GnuTLS priority string depends on whether we are
          using raw public keys or not.  When unpickling clients, set
          key_id if not set in the pickle.
  (main/MandosDBusService.ClientNotFound): Change name of first
                                           parameter from
                                           "Fingerprint" to "KeyID".
* mandos-clients.conf.xml (OPTIONS): Document new "key_id" option.
  (OPTIONS/secret): Mention new key ID matchning.
  (EXPANSION/RUNTIME EXPANSION): Add new "key_id" option.
  (EXAMPLE): - '' -
* mandos-ctl (tablewords, main/keywords): Add new "KeyID" property.
* mandos-keygen: Create TLS key files.  New "--tls-keytype" (-T)
                 option.  Alter help text to be more clear about key
                 types.  When in password mode, also output "key_id"
                 option.
* mandos-keygen.xml (SYNOPSIS): Add new "--tls-keytype" (-T) option.
  (DESCRIPTION): Alter to match new dual-key design.
  (OVERVIEW): - '' -
  (FILES): Add TLS key files.
* mandos-options.xml (priority): Document new default priority string
                                 when using raw public keys.
* mandos.xml (NETWORK PROTOCOL): Describe new protocol using key ID.
  (BUGS): Remove issue about checking expire times of OpenPGP keys,
          since TLS public keys do not have expiration times.
  (SECURITY/CLIENT): Alter description to match new design.
  (SEE ALSO/GnuTLS): - '' -
  (SEE ALSO): Add reference to RFC 7250, and alter description of when
              RFC 6091 is used.
* overview.xml: Alter text to match new design.
* plugin-runner.xml (EXAMPLE): Add --tls-pubkey and --tls-privkey
                               options to mandos-client options.
* plugins.d/mandos-client.c: Use raw public keys when compiling with
                             supporting GnuTLS versions. Add new
                             "--tls-pubkey" and "--tls-privkey"
                             options (which do nothing if GnuTLS
                             library does not support raw public
                             keys).  Alter text throughout to reflect
                             new design.  Only generate new DH
                             parameters (based on size of OpenPGP key)
                             when using OpenPGP in TLS.  Default
                             GnuTLS priority string depends on whether
                             we are using raw public keys or not.
* plugins.d/mandos-client.xml (SYNOPSIS): Add new "--tls-privkey" (-t)
                                          and "--tls-pubkey" (-T)
                                          options.
  (DESCRIPTION): Describe new dual-key design.
  (OPTIONS): Document new "--tls-privkey" (-t) and "--tls-pubkey" (-T)
             options.
  (OPTIONS/--dh-bits): No longer necessarily depends on OpenPGP key
                       size.
  (FILES): Add default locations for TLS public and private key files.
  (EXAMPLE): Use new --tls-pubkey and --tls-privkey options.
  (SECURITY): Alter wording slightly to reflect new dual-key design.
  (SEE ALSO/GnuTLS): Alter description to match new design.
  (SEE ALSO): Add reference to RFC 7250, and alter description of when
              RFC 6091 is used.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
# -*- mode: python; coding: utf-8 -*-
3
 
 
3
#
4
4
# Mandos Monitor - Control and monitor the Mandos server
5
 
6
 
# Copyright © 2008-2016 Teddy Hogeborn
7
 
# Copyright © 2008-2016 Björn Påhlsson
8
 
9
 
# This program is free software: you can redistribute it and/or modify
10
 
# it under the terms of the GNU General Public License as published by
 
5
#
 
6
# Copyright © 2008-2018 Teddy Hogeborn
 
7
# Copyright © 2008-2018 Björn Påhlsson
 
8
#
 
9
# This file is part of Mandos.
 
10
#
 
11
# Mandos is free software: you can redistribute it and/or modify it
 
12
# under the terms of the GNU General Public License as published by
11
13
# the Free Software Foundation, either version 3 of the License, or
12
14
# (at your option) any later version.
13
15
#
14
 
#     This program is distributed in the hope that it will be useful,
15
 
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#     Mandos is distributed in the hope that it will be useful, but
 
17
#     WITHOUT ANY WARRANTY; without even the implied warranty of
16
18
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
19
#     GNU General Public License for more details.
18
 
 
20
#
19
21
# You should have received a copy of the GNU General Public License
20
 
# along with this program.  If not, see
21
 
# <http://www.gnu.org/licenses/>.
22
 
 
22
# along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
23
#
23
24
# Contact the authors at <mandos@recompile.se>.
24
 
 
25
#
25
26
 
26
27
from __future__ import (division, absolute_import, print_function,
27
28
                        unicode_literals)
38
39
import re
39
40
import os
40
41
import collections
41
 
import doctest
 
42
import json
42
43
 
43
44
import dbus
44
45
 
57
58
    "Interval": "Interval",
58
59
    "Host": "Host",
59
60
    "Fingerprint": "Fingerprint",
 
61
    "KeyID": "Key ID",
60
62
    "CheckerRunning": "Check Is Running",
61
63
    "LastEnabled": "Last Enabled",
62
64
    "ApprovalPending": "Approval Is Pending",
64
66
    "ApprovalDelay": "Approval Delay",
65
67
    "ApprovalDuration": "Approval Duration",
66
68
    "Checker": "Checker",
67
 
    "ExtendedTimeout": "Extended Timeout"
 
69
    "ExtendedTimeout": "Extended Timeout",
 
70
    "Expires": "Expires",
 
71
    "LastCheckerStatus": "Last Checker Status",
68
72
}
69
73
defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
70
74
domain = "se.recompile"
72
76
server_path = "/"
73
77
server_interface = domain + ".Mandos"
74
78
client_interface = domain + ".Mandos.Client"
75
 
version = "1.7.6"
 
79
version = "1.7.20"
76
80
 
77
81
 
78
82
try:
80
84
except AttributeError:
81
85
    dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
82
86
 
 
87
 
83
88
def milliseconds_to_string(ms):
84
89
    td = datetime.timedelta(0, 0, 0, ms)
85
 
    return ("{days}{hours:02}:{minutes:02}:{seconds:02}".format(
86
 
        days = "{}T".format(td.days) if td.days else "",
87
 
        hours = td.seconds // 3600,
88
 
        minutes = (td.seconds % 3600) // 60,
89
 
        seconds = td.seconds % 60))
 
90
    return ("{days}{hours:02}:{minutes:02}:{seconds:02}"
 
91
            .format(days="{}T".format(td.days) if td.days else "",
 
92
                    hours=td.seconds // 3600,
 
93
                    minutes=(td.seconds % 3600) // 60,
 
94
                    seconds=td.seconds % 60))
90
95
 
91
96
 
92
97
def rfc3339_duration_to_delta(duration):
93
98
    """Parse an RFC 3339 "duration" and return a datetime.timedelta
94
 
    
 
99
 
95
100
    >>> rfc3339_duration_to_delta("P7D")
96
101
    datetime.timedelta(7)
97
102
    >>> rfc3339_duration_to_delta("PT60S")
107
112
    >>> rfc3339_duration_to_delta("P1DT3M20S")
108
113
    datetime.timedelta(1, 200)
109
114
    """
110
 
    
 
115
 
111
116
    # Parsing an RFC 3339 duration with regular expressions is not
112
117
    # possible - there would have to be multiple places for the same
113
118
    # values, like seconds.  The current code, while more esoteric, is
114
119
    # cleaner without depending on a parsing library.  If Python had a
115
120
    # built-in library for parsing we would use it, but we'd like to
116
121
    # avoid excessive use of external libraries.
117
 
    
 
122
 
118
123
    # New type for defining tokens, syntax, and semantics all-in-one
119
124
    Token = collections.namedtuple("Token", (
120
125
        "regexp",  # To match token; if "value" is not None, must have
153
158
                           frozenset((token_year, token_month,
154
159
                                      token_day, token_time,
155
160
                                      token_week)))
156
 
    # Define starting values
157
 
    value = datetime.timedelta() # Value so far
 
161
    # Define starting values:
 
162
    # Value so far
 
163
    value = datetime.timedelta()
158
164
    found_token = None
159
 
    followers = frozenset((token_duration, )) # Following valid tokens
160
 
    s = duration                # String left to parse
 
165
    # Following valid tokens
 
166
    followers = frozenset((token_duration, ))
 
167
    # String left to parse
 
168
    s = duration
161
169
    # Loop until end token is found
162
170
    while found_token is not token_end:
163
171
        # Search for any currently valid tokens
187
195
 
188
196
def string_to_delta(interval):
189
197
    """Parse a string and return a datetime.timedelta
190
 
    
 
198
 
191
199
    >>> string_to_delta('7d')
192
200
    datetime.timedelta(7)
193
201
    >>> string_to_delta('60s')
201
209
    >>> string_to_delta('5m 30s')
202
210
    datetime.timedelta(0, 330)
203
211
    """
204
 
    
 
212
 
205
213
    try:
206
214
        return rfc3339_duration_to_delta(interval)
207
215
    except ValueError:
208
216
        pass
209
 
    
 
217
 
210
218
    value = datetime.timedelta(0)
211
219
    regexp = re.compile(r"(\d+)([dsmhw]?)")
212
 
    
 
220
 
213
221
    for num, suffix in regexp.findall(interval):
214
222
        if suffix == "d":
215
223
            value += datetime.timedelta(int(num))
234
242
                       "ApprovalDuration", "ExtendedTimeout"):
235
243
            return milliseconds_to_string(value)
236
244
        return str(value)
237
 
    
 
245
 
238
246
    # Create format string to print table rows
239
247
    format_string = " ".join("{{{key}:{width}}}".format(
240
 
        width = max(len(tablewords[key]),
241
 
                    max(len(valuetostring(client[key], key))
242
 
                        for client in clients)),
243
 
        key = key)
 
248
        width=max(len(tablewords[key]),
 
249
                  max(len(valuetostring(client[key], key))
 
250
                      for client in clients)),
 
251
        key=key)
244
252
                             for key in keywords)
245
253
    # Print header line
246
254
    print(format_string.format(**tablewords))
247
255
    for client in clients:
248
 
        print(format_string.format(**{
249
 
            key: valuetostring(client[key], key)
250
 
            for key in keywords }))
 
256
        print(format_string
 
257
              .format(**{key: valuetostring(client[key], key)
 
258
                         for key in keywords}))
251
259
 
252
260
 
253
261
def has_actions(options):
274
282
def main():
275
283
    parser = argparse.ArgumentParser()
276
284
    parser.add_argument("--version", action="version",
277
 
                        version = "%(prog)s {}".format(version),
 
285
                        version="%(prog)s {}".format(version),
278
286
                        help="show version number and exit")
279
287
    parser.add_argument("-a", "--all", action="store_true",
280
288
                        help="Select all clients")
281
289
    parser.add_argument("-v", "--verbose", action="store_true",
282
290
                        help="Print all fields")
 
291
    parser.add_argument("-j", "--dump-json", action="store_true",
 
292
                        help="Dump client data in JSON format")
283
293
    parser.add_argument("-e", "--enable", action="store_true",
284
294
                        help="Enable client")
285
295
    parser.add_argument("-d", "--disable", action="store_true",
324
334
                        help="Run self-test")
325
335
    parser.add_argument("client", nargs="*", help="Client name")
326
336
    options = parser.parse_args()
327
 
    
 
337
 
328
338
    if has_actions(options) and not (options.client or options.all):
329
339
        parser.error("Options require clients names or --all.")
330
340
    if options.verbose and has_actions(options):
331
 
        parser.error("--verbose can only be used alone or with"
332
 
                     " --all.")
 
341
        parser.error("--verbose can only be used alone.")
 
342
    if options.dump_json and (options.verbose
 
343
                              or has_actions(options)):
 
344
        parser.error("--dump-json can only be used alone.")
333
345
    if options.all and not has_actions(options):
334
346
        parser.error("--all requires an action.")
335
347
 
336
348
    if options.check:
 
349
        import doctest
337
350
        fail_count, test_count = doctest.testmod()
338
351
        sys.exit(os.EX_OK if fail_count == 0 else 1)
339
 
    
 
352
 
340
353
    try:
341
354
        bus = dbus.SystemBus()
342
355
        mandos_dbus_objc = bus.get_object(busname, server_path)
343
356
    except dbus.exceptions.DBusException:
344
357
        print("Could not connect to Mandos server", file=sys.stderr)
345
358
        sys.exit(1)
346
 
    
 
359
 
347
360
    mandos_serv = dbus.Interface(mandos_dbus_objc,
348
 
                                 dbus_interface = server_interface)
 
361
                                 dbus_interface=server_interface)
349
362
    mandos_serv_object_manager = dbus.Interface(
350
 
        mandos_dbus_objc, dbus_interface = dbus.OBJECT_MANAGER_IFACE)
351
 
    
352
 
    #block stderr since dbus library prints to stderr
 
363
        mandos_dbus_objc, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
 
364
 
 
365
    # block stderr since dbus library prints to stderr
353
366
    null = os.open(os.path.devnull, os.O_RDWR)
354
367
    stderrcopy = os.dup(sys.stderr.fileno())
355
368
    os.dup2(null, sys.stderr.fileno())
356
369
    os.close(null)
357
370
    try:
358
371
        try:
359
 
            mandos_clients = { path: ifs_and_props[client_interface]
360
 
                               for path, ifs_and_props in
361
 
                               mandos_serv_object_manager
362
 
                               .GetManagedObjects().items()
363
 
                               if client_interface in ifs_and_props }
 
372
            mandos_clients = {path: ifs_and_props[client_interface]
 
373
                              for path, ifs_and_props in
 
374
                              mandos_serv_object_manager
 
375
                              .GetManagedObjects().items()
 
376
                              if client_interface in ifs_and_props}
364
377
        finally:
365
 
            #restore stderr
 
378
            # restore stderr
366
379
            os.dup2(stderrcopy, sys.stderr.fileno())
367
380
            os.close(stderrcopy)
368
381
    except dbus.exceptions.DBusException as e:
369
 
        print("Access denied: Accessing mandos server through D-Bus: {}"
370
 
              .format(e), file=sys.stderr)
 
382
        print("Access denied: "
 
383
              "Accessing mandos server through D-Bus: {}".format(e),
 
384
              file=sys.stderr)
371
385
        sys.exit(1)
372
 
    
 
386
 
373
387
    # Compile dict of (clients: properties) to process
374
 
    clients={}
375
 
    
 
388
    clients = {}
 
389
 
376
390
    if options.all or not options.client:
377
 
        clients = { bus.get_object(busname, path): properties
378
 
                    for path, properties in mandos_clients.items() }
 
391
        clients = {bus.get_object(busname, path): properties
 
392
                   for path, properties in mandos_clients.items()}
379
393
    else:
380
394
        for name in options.client:
381
395
            for path, client in mandos_clients.items():
387
401
                print("Client not found on server: {!r}"
388
402
                      .format(name), file=sys.stderr)
389
403
                sys.exit(1)
390
 
    
 
404
 
391
405
    if not has_actions(options) and clients:
392
 
        if options.verbose:
 
406
        if options.verbose or options.dump_json:
393
407
            keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK",
394
 
                        "Created", "Interval", "Host", "Fingerprint",
395
 
                        "CheckerRunning", "LastEnabled",
396
 
                        "ApprovalPending", "ApprovedByDefault",
397
 
                        "LastApprovalRequest", "ApprovalDelay",
398
 
                        "ApprovalDuration", "Checker",
399
 
                        "ExtendedTimeout")
 
408
                        "Created", "Interval", "Host", "KeyID",
 
409
                        "Fingerprint", "CheckerRunning",
 
410
                        "LastEnabled", "ApprovalPending",
 
411
                        "ApprovedByDefault", "LastApprovalRequest",
 
412
                        "ApprovalDelay", "ApprovalDuration",
 
413
                        "Checker", "ExtendedTimeout", "Expires",
 
414
                        "LastCheckerStatus")
400
415
        else:
401
416
            keywords = defaultkeywords
402
 
        
403
 
        print_clients(clients.values(), keywords)
 
417
 
 
418
        if options.dump_json:
 
419
            json.dump({client["Name"]: {key:
 
420
                                        bool(client[key])
 
421
                                        if isinstance(client[key],
 
422
                                                      dbus.Boolean)
 
423
                                        else client[key]
 
424
                                        for key in keywords}
 
425
                       for client in clients.values()},
 
426
                      fp=sys.stdout, indent=4,
 
427
                      separators=(',', ': '))
 
428
            print()
 
429
        else:
 
430
            print_clients(clients.values(), keywords)
404
431
    else:
405
432
        # Process each client in the list by all selected options
406
433
        for client in clients:
407
 
            
 
434
 
408
435
            def set_client_prop(prop, value):
409
436
                """Set a Client D-Bus property"""
410
437
                client.Set(client_interface, prop, value,
411
438
                           dbus_interface=dbus.PROPERTIES_IFACE)
412
 
            
 
439
 
413
440
            def set_client_prop_ms(prop, value):
414
441
                """Set a Client D-Bus property, converted
415
442
                from a string to milliseconds."""
416
443
                set_client_prop(prop,
417
444
                                string_to_delta(value).total_seconds()
418
445
                                * 1000)
419
 
            
 
446
 
420
447
            if options.remove:
421
448
                mandos_serv.RemoveClient(client.__dbus_object_path__)
422
449
            if options.enable:
430
457
            if options.stop_checker:
431
458
                set_client_prop("CheckerRunning", dbus.Boolean(False))
432
459
            if options.is_enabled:
433
 
                sys.exit(0 if client.Get(client_interface,
434
 
                                         "Enabled",
435
 
                                         dbus_interface=
436
 
                                         dbus.PROPERTIES_IFACE)
437
 
                         else 1)
 
460
                if client.Get(client_interface, "Enabled",
 
461
                              dbus_interface=dbus.PROPERTIES_IFACE):
 
462
                    sys.exit(0)
 
463
                else:
 
464
                    sys.exit(1)
438
465
            if options.checker is not None:
439
466
                set_client_prop("Checker", options.checker)
440
467
            if options.host is not None: