524
434
class AvahiServiceToSyslog(AvahiService):
525
435
def rename(self, *args, **kwargs):
526
436
"""Add the new name to the syslog messages"""
527
ret = super(AvahiServiceToSyslog, self).rename(*args,
437
ret = AvahiService.rename(self, *args, **kwargs)
529
438
syslogger.setFormatter(logging.Formatter(
530
"Mandos ({}) [%(process)d]: %(levelname)s: %(message)s"
439
'Mandos ({}) [%(process)d]: %(levelname)s: %(message)s'
531
440
.format(self.name)))
535
443
# Pretend that we have a GnuTLS module
537
"""This isn't so much a class as it is a module-like namespace."""
539
library = ctypes.util.find_library("gnutls")
541
library = ctypes.util.find_library("gnutls-deb0")
542
_library = ctypes.cdll.LoadLibrary(library)
444
class GnuTLS(object):
445
"""This isn't so much a class as it is a module-like namespace.
446
It is instantiated once, and simulates having a GnuTLS module."""
448
_library = ctypes.cdll.LoadLibrary(
449
ctypes.util.find_library("gnutls"))
450
_need_version = "3.3.0"
452
# Need to use class name "GnuTLS" here, since this method is
453
# called before the assignment to the "gnutls" global variable
455
if GnuTLS.check_version(self._need_version) is None:
456
raise GnuTLS.Error("Needs GnuTLS {} or later"
457
.format(self._need_version))
545
459
# Unless otherwise indicated, the constants and types below are
546
460
# all from the gnutls/gnutls.h C header file.
550
464
E_INTERRUPTED = -52
556
469
CRD_CERTIFICATE = 1
557
470
E_NO_CERTIFICATE_FOUND = -49
562
KEYID_USE_SHA256 = 1 # gnutls/x509.h
563
471
OPENPGP_FMT_RAW = 0 # gnutls/openpgp.h
566
class _session_int(ctypes.Structure):
474
class session_int(ctypes.Structure):
568
session_t = ctypes.POINTER(_session_int)
476
session_t = ctypes.POINTER(session_int)
570
477
class certificate_credentials_st(ctypes.Structure):
572
479
certificate_credentials_t = ctypes.POINTER(
573
480
certificate_credentials_st)
574
481
certificate_type_t = ctypes.c_int
576
482
class datum_t(ctypes.Structure):
577
_fields_ = [("data", ctypes.POINTER(ctypes.c_ubyte)),
578
("size", ctypes.c_uint)]
580
class _openpgp_crt_int(ctypes.Structure):
483
_fields_ = [('data', ctypes.POINTER(ctypes.c_ubyte)),
484
('size', ctypes.c_uint)]
485
class openpgp_crt_int(ctypes.Structure):
582
openpgp_crt_t = ctypes.POINTER(_openpgp_crt_int)
583
openpgp_crt_fmt_t = ctypes.c_int # gnutls/openpgp.h
487
openpgp_crt_t = ctypes.POINTER(openpgp_crt_int)
488
openpgp_crt_fmt_t = ctypes.c_int # gnutls/openpgp.h
584
489
log_func = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)
585
credentials_type_t = ctypes.c_int
490
credentials_type_t = ctypes.c_int #
586
491
transport_ptr_t = ctypes.c_void_p
587
492
close_request_t = ctypes.c_int
590
495
class Error(Exception):
591
def __init__(self, message=None, code=None, args=()):
496
# We need to use the class name "GnuTLS" here, since this
497
# exception might be raised from within GnuTLS.__init__,
498
# which is called before the assignment to the "gnutls"
499
# global variable has happened.
500
def __init__(self, message = None, code = None, args=()):
592
501
# Default usage is by a message string, but if a return
593
502
# code is passed, convert it to a string with
594
503
# gnutls.strerror()
596
505
if message is None and code is not None:
597
message = gnutls.strerror(code).decode(
598
"utf-8", errors="replace")
599
return super(gnutls.Error, self).__init__(
506
message = GnuTLS.strerror(code)
507
return super(GnuTLS.Error, self).__init__(
602
510
class CertificateSecurityError(Error):
606
def __init__(self, cls):
609
def from_param(self, obj):
610
if not isinstance(obj, self.cls):
611
raise TypeError("Not of type {}: {!r}"
612
.format(self.cls.__name__, obj))
613
return ctypes.byref(obj.from_param(obj))
615
class CastToVoidPointer:
616
def __init__(self, cls):
619
def from_param(self, obj):
620
if not isinstance(obj, self.cls):
621
raise TypeError("Not of type {}: {!r}"
622
.format(self.cls.__name__, obj))
623
return ctypes.cast(obj.from_param(obj), ctypes.c_void_p)
625
class With_from_param:
627
def from_param(cls, obj):
628
return obj._as_parameter_
631
class Credentials(With_from_param):
514
class Credentials(object):
632
515
def __init__(self):
633
self._as_parameter_ = gnutls.certificate_credentials_t()
634
gnutls.certificate_allocate_credentials(self)
516
self._c_object = gnutls.certificate_credentials_t()
517
gnutls.certificate_allocate_credentials(
518
ctypes.byref(self._c_object))
635
519
self.type = gnutls.CRD_CERTIFICATE
637
521
def __del__(self):
638
gnutls.certificate_free_credentials(self)
640
class ClientSession(With_from_param):
641
def __init__(self, socket, credentials=None):
642
self._as_parameter_ = gnutls.session_t()
643
gnutls_flags = gnutls.CLIENT
644
if gnutls.check_version(b"3.5.6"):
645
gnutls_flags |= gnutls.NO_TICKETS
647
gnutls_flags |= gnutls.ENABLE_RAWPK
648
gnutls.init(self, gnutls_flags)
650
gnutls.set_default_priority(self)
651
gnutls.transport_set_ptr(self, socket.fileno())
652
gnutls.handshake_set_private_extensions(self, True)
522
gnutls.certificate_free_credentials(self._c_object)
524
class ClientSession(object):
525
def __init__(self, socket, credentials = None):
526
self._c_object = gnutls.session_t()
527
gnutls.init(ctypes.byref(self._c_object), gnutls.CLIENT)
528
gnutls.set_default_priority(self._c_object)
529
gnutls.transport_set_ptr(self._c_object, socket.fileno())
530
gnutls.handshake_set_private_extensions(self._c_object,
653
532
self.socket = socket
654
533
if credentials is None:
655
534
credentials = gnutls.Credentials()
656
gnutls.credentials_set(self, credentials.type,
535
gnutls.credentials_set(self._c_object, credentials.type,
536
ctypes.cast(credentials._c_object,
658
538
self.credentials = credentials
660
540
def __del__(self):
541
gnutls.deinit(self._c_object)
663
543
def handshake(self):
664
return gnutls.handshake(self)
544
return gnutls.handshake(self._c_object)
666
546
def send(self, data):
667
547
data = bytes(data)
668
548
data_len = len(data)
669
549
while data_len > 0:
670
data_len -= gnutls.record_send(self, data[-data_len:],
550
data_len -= gnutls.record_send(self._c_object,
674
return gnutls.bye(self, gnutls.SHUT_RDWR)
555
return gnutls.bye(self._c_object, gnutls.SHUT_RDWR)
676
557
# Error handling functions
677
558
def _error_code(result):
678
559
"""A function to raise exceptions on errors, suitable
679
for the "restype" attribute on ctypes functions"""
680
if result >= gnutls.E_SUCCESS:
560
for the 'restype' attribute on ctypes functions"""
682
563
if result == gnutls.E_NO_CERTIFICATE_FOUND:
683
raise gnutls.CertificateSecurityError(code=result)
684
raise gnutls.Error(code=result)
686
def _retry_on_error(result, func, arguments,
687
_error_code=_error_code):
564
raise gnutls.CertificateSecurityError(code = result)
565
raise gnutls.Error(code = result)
567
def _retry_on_error(result, func, arguments):
688
568
"""A function to retry on some errors, suitable
689
for the "errcheck" attribute on ctypes functions"""
690
while result < gnutls.E_SUCCESS:
569
for the 'errcheck' attribute on ctypes functions"""
691
571
if result not in (gnutls.E_INTERRUPTED, gnutls.E_AGAIN):
692
572
return _error_code(result)
693
573
result = func(*arguments)
696
576
# Unless otherwise indicated, the function declarations below are
697
577
# all from the gnutls/gnutls.h C header file.
700
580
priority_set_direct = _library.gnutls_priority_set_direct
701
priority_set_direct.argtypes = [ClientSession, ctypes.c_char_p,
581
priority_set_direct.argtypes = [session_t, ctypes.c_char_p,
702
582
ctypes.POINTER(ctypes.c_char_p)]
703
583
priority_set_direct.restype = _error_code
705
585
init = _library.gnutls_init
706
init.argtypes = [PointerTo(ClientSession), ctypes.c_int]
586
init.argtypes = [ctypes.POINTER(session_t), ctypes.c_int]
707
587
init.restype = _error_code
709
589
set_default_priority = _library.gnutls_set_default_priority
710
set_default_priority.argtypes = [ClientSession]
590
set_default_priority.argtypes = [session_t]
711
591
set_default_priority.restype = _error_code
713
593
record_send = _library.gnutls_record_send
714
record_send.argtypes = [ClientSession, ctypes.c_void_p,
594
record_send.argtypes = [session_t, ctypes.c_void_p,
716
596
record_send.restype = ctypes.c_ssize_t
717
597
record_send.errcheck = _retry_on_error
719
599
certificate_allocate_credentials = (
720
600
_library.gnutls_certificate_allocate_credentials)
721
601
certificate_allocate_credentials.argtypes = [
722
PointerTo(Credentials)]
602
ctypes.POINTER(certificate_credentials_t)]
723
603
certificate_allocate_credentials.restype = _error_code
725
605
certificate_free_credentials = (
726
606
_library.gnutls_certificate_free_credentials)
727
certificate_free_credentials.argtypes = [Credentials]
607
certificate_free_credentials.argtypes = [certificate_credentials_t]
728
608
certificate_free_credentials.restype = None
730
610
handshake_set_private_extensions = (
731
611
_library.gnutls_handshake_set_private_extensions)
732
handshake_set_private_extensions.argtypes = [ClientSession,
612
handshake_set_private_extensions.argtypes = [session_t,
734
614
handshake_set_private_extensions.restype = None
736
616
credentials_set = _library.gnutls_credentials_set
737
credentials_set.argtypes = [ClientSession, credentials_type_t,
738
CastToVoidPointer(Credentials)]
617
credentials_set.argtypes = [session_t, credentials_type_t,
739
619
credentials_set.restype = _error_code
741
621
strerror = _library.gnutls_strerror
742
622
strerror.argtypes = [ctypes.c_int]
743
623
strerror.restype = ctypes.c_char_p
745
625
certificate_type_get = _library.gnutls_certificate_type_get
746
certificate_type_get.argtypes = [ClientSession]
626
certificate_type_get.argtypes = [session_t]
747
627
certificate_type_get.restype = _error_code
749
629
certificate_get_peers = _library.gnutls_certificate_get_peers
750
certificate_get_peers.argtypes = [ClientSession,
630
certificate_get_peers.argtypes = [session_t,
751
631
ctypes.POINTER(ctypes.c_uint)]
752
632
certificate_get_peers.restype = ctypes.POINTER(datum_t)
754
634
global_set_log_level = _library.gnutls_global_set_log_level
755
635
global_set_log_level.argtypes = [ctypes.c_int]
756
636
global_set_log_level.restype = None
758
638
global_set_log_function = _library.gnutls_global_set_log_function
759
639
global_set_log_function.argtypes = [log_func]
760
640
global_set_log_function.restype = None
762
642
deinit = _library.gnutls_deinit
763
deinit.argtypes = [ClientSession]
643
deinit.argtypes = [session_t]
764
644
deinit.restype = None
766
646
handshake = _library.gnutls_handshake
767
handshake.argtypes = [ClientSession]
768
handshake.restype = ctypes.c_int
647
handshake.argtypes = [session_t]
648
handshake.restype = _error_code
769
649
handshake.errcheck = _retry_on_error
771
651
transport_set_ptr = _library.gnutls_transport_set_ptr
772
transport_set_ptr.argtypes = [ClientSession, transport_ptr_t]
652
transport_set_ptr.argtypes = [session_t, transport_ptr_t]
773
653
transport_set_ptr.restype = None
775
655
bye = _library.gnutls_bye
776
bye.argtypes = [ClientSession, close_request_t]
777
bye.restype = ctypes.c_int
656
bye.argtypes = [session_t, close_request_t]
657
bye.restype = _error_code
778
658
bye.errcheck = _retry_on_error
780
660
check_version = _library.gnutls_check_version
781
661
check_version.argtypes = [ctypes.c_char_p]
782
662
check_version.restype = ctypes.c_char_p
784
_need_version = b"3.3.0"
785
if check_version(_need_version) is None:
786
raise self.Error("Needs GnuTLS {} or later"
787
.format(_need_version))
789
_tls_rawpk_version = b"3.6.6"
790
has_rawpk = bool(check_version(_tls_rawpk_version))
794
class pubkey_st(ctypes.Structure):
796
pubkey_t = ctypes.POINTER(pubkey_st)
798
x509_crt_fmt_t = ctypes.c_int
800
# All the function declarations below are from
802
pubkey_init = _library.gnutls_pubkey_init
803
pubkey_init.argtypes = [ctypes.POINTER(pubkey_t)]
804
pubkey_init.restype = _error_code
806
pubkey_import = _library.gnutls_pubkey_import
807
pubkey_import.argtypes = [pubkey_t, ctypes.POINTER(datum_t),
809
pubkey_import.restype = _error_code
811
pubkey_get_key_id = _library.gnutls_pubkey_get_key_id
812
pubkey_get_key_id.argtypes = [pubkey_t, ctypes.c_int,
813
ctypes.POINTER(ctypes.c_ubyte),
814
ctypes.POINTER(ctypes.c_size_t)]
815
pubkey_get_key_id.restype = _error_code
817
pubkey_deinit = _library.gnutls_pubkey_deinit
818
pubkey_deinit.argtypes = [pubkey_t]
819
pubkey_deinit.restype = None
821
# All the function declarations below are from
824
openpgp_crt_init = _library.gnutls_openpgp_crt_init
825
openpgp_crt_init.argtypes = [ctypes.POINTER(openpgp_crt_t)]
826
openpgp_crt_init.restype = _error_code
828
openpgp_crt_import = _library.gnutls_openpgp_crt_import
829
openpgp_crt_import.argtypes = [openpgp_crt_t,
830
ctypes.POINTER(datum_t),
832
openpgp_crt_import.restype = _error_code
834
openpgp_crt_verify_self = \
835
_library.gnutls_openpgp_crt_verify_self
836
openpgp_crt_verify_self.argtypes = [
839
ctypes.POINTER(ctypes.c_uint),
841
openpgp_crt_verify_self.restype = _error_code
843
openpgp_crt_deinit = _library.gnutls_openpgp_crt_deinit
844
openpgp_crt_deinit.argtypes = [openpgp_crt_t]
845
openpgp_crt_deinit.restype = None
847
openpgp_crt_get_fingerprint = (
848
_library.gnutls_openpgp_crt_get_fingerprint)
849
openpgp_crt_get_fingerprint.argtypes = [openpgp_crt_t,
853
openpgp_crt_get_fingerprint.restype = _error_code
855
if check_version(b"3.6.4"):
856
certificate_type_get2 = _library.gnutls_certificate_type_get2
857
certificate_type_get2.argtypes = [ClientSession, ctypes.c_int]
858
certificate_type_get2.restype = _error_code
664
# All the function declarations below are from gnutls/openpgp.h
666
openpgp_crt_init = _library.gnutls_openpgp_crt_init
667
openpgp_crt_init.argtypes = [ctypes.POINTER(openpgp_crt_t)]
668
openpgp_crt_init.restype = _error_code
670
openpgp_crt_import = _library.gnutls_openpgp_crt_import
671
openpgp_crt_import.argtypes = [openpgp_crt_t,
672
ctypes.POINTER(datum_t),
674
openpgp_crt_import.restype = _error_code
676
openpgp_crt_verify_self = _library.gnutls_openpgp_crt_verify_self
677
openpgp_crt_verify_self.argtypes = [openpgp_crt_t, ctypes.c_uint,
678
ctypes.POINTER(ctypes.c_uint)]
679
openpgp_crt_verify_self.restype = _error_code
681
openpgp_crt_deinit = _library.gnutls_openpgp_crt_deinit
682
openpgp_crt_deinit.argtypes = [openpgp_crt_t]
683
openpgp_crt_deinit.restype = None
685
openpgp_crt_get_fingerprint = (
686
_library.gnutls_openpgp_crt_get_fingerprint)
687
openpgp_crt_get_fingerprint.argtypes = [openpgp_crt_t,
691
openpgp_crt_get_fingerprint.restype = _error_code
860
693
# Remove non-public functions
861
694
del _error_code, _retry_on_error
695
# Create the global "gnutls" object, simulating a module
864
698
def call_pipe(connection, # : multiprocessing.Connection
865
699
func, *args, **kwargs):
866
700
"""This function is meant to be called by multiprocessing.Process
868
702
This function runs func(*args, **kwargs), and writes the resulting
869
703
return value on the provided multiprocessing.Connection.
871
705
connection.send(func(*args, **kwargs))
872
706
connection.close()
708
class Client(object):
876
709
"""A representation of a client host served by this server.
879
approved: bool(); None if not yet approved/disapproved
712
approved: bool(); 'None' if not yet approved/disapproved
880
713
approval_delay: datetime.timedelta(); Time to wait for approval
881
714
approval_duration: datetime.timedelta(); Duration of one approval
882
checker: multiprocessing.Process(); a running checker process used
883
to see if the client lives. None if no process is
885
checker_callback_tag: a GLib event source tag, or None
715
checker: subprocess.Popen(); a running checker process used
716
to see if the client lives.
717
'None' if no process is running.
718
checker_callback_tag: a gobject event source tag, or None
886
719
checker_command: string; External command which is run to check
887
720
if client lives. %() expansions are done at
888
721
runtime with vars(self) as dict, so that for
889
722
instance %(name)s can be used in the command.
890
checker_initiator_tag: a GLib event source tag, or None
723
checker_initiator_tag: a gobject event source tag, or None
891
724
created: datetime.datetime(); (UTC) object creation
892
725
client_structure: Object describing what attributes a client has
893
726
and is used for storing the client at exit
894
727
current_checker_command: string; current running checker_command
895
disable_initiator_tag: a GLib event source tag, or None
728
disable_initiator_tag: a gobject event source tag, or None
897
730
fingerprint: string (40 or 32 hexadecimal digits); used to
898
uniquely identify an OpenPGP client
899
key_id: string (64 hexadecimal digits); used to uniquely identify
900
a client using raw public keys
731
uniquely identify the client
901
732
host: string; available for use by the checker command
902
733
interval: datetime.timedelta(); How often to start a new checker
903
734
last_approval_request: datetime.datetime(); (UTC) or None
2714
2480
self.gnutls_priority = gnutls_priority
2715
2481
IPv6_TCPServer.__init__(self, server_address,
2716
2482
RequestHandlerClass,
2717
interface=interface,
2483
interface = interface,
2484
use_ipv6 = use_ipv6,
2485
socketfd = socketfd)
2721
2487
def server_activate(self):
2722
2488
if self.enabled:
2723
2489
return socketserver.TCPServer.server_activate(self)
2725
2491
def enable(self):
2726
2492
self.enabled = True
2728
2494
def add_pipe(self, parent_pipe, proc):
2729
2495
# Call "handle_ipc" for both data and EOF events
2731
GLib.IOChannel.unix_new(parent_pipe.fileno()),
2732
GLib.PRIORITY_DEFAULT, GLib.IO_IN | GLib.IO_HUP,
2496
gobject.io_add_watch(
2497
parent_pipe.fileno(),
2498
gobject.IO_IN | gobject.IO_HUP,
2733
2499
functools.partial(self.handle_ipc,
2734
parent_pipe=parent_pipe,
2500
parent_pipe = parent_pipe,
2737
2503
def handle_ipc(self, source, condition,
2738
2504
parent_pipe=None,
2740
2506
client_object=None):
2741
2507
# error, or the other end of multiprocessing.Pipe has closed
2742
if condition & (GLib.IO_ERR | GLib.IO_HUP):
2508
if condition & (gobject.IO_ERR | gobject.IO_HUP):
2743
2509
# Wait for other process to exit
2747
2513
# Read a request from the child
2748
2514
request = parent_pipe.recv()
2749
2515
command = request[0]
2751
if command == "init":
2752
key_id = request[1].decode("ascii")
2753
fpr = request[2].decode("ascii")
2754
address = request[3]
2756
for c in self.clients.values():
2757
if key_id == ("E3B0C44298FC1C149AFBF4C8996FB924"
2758
"27AE41E4649B934CA495991B7852B855"):
2760
if key_id and c.key_id == key_id:
2763
if fpr and c.fingerprint == fpr:
2517
if command == 'init':
2519
address = request[2]
2521
for c in self.clients.itervalues():
2522
if c.fingerprint == fpr:
2767
logger.info("Client not found for key ID: %s, address"
2768
": %s", key_id or fpr, address)
2526
logger.info("Client not found for fingerprint: %s, ad"
2527
"dress: %s", fpr, address)
2769
2528
if self.use_dbus:
2770
2529
# Emit D-Bus signal
2771
mandos_dbus_service.ClientNotFound(key_id or fpr,
2530
mandos_dbus_service.ClientNotFound(fpr,
2773
2532
parent_pipe.send(False)
2777
GLib.IOChannel.unix_new(parent_pipe.fileno()),
2778
GLib.PRIORITY_DEFAULT, GLib.IO_IN | GLib.IO_HUP,
2535
gobject.io_add_watch(
2536
parent_pipe.fileno(),
2537
gobject.IO_IN | gobject.IO_HUP,
2779
2538
functools.partial(self.handle_ipc,
2780
parent_pipe=parent_pipe,
2782
client_object=client))
2539
parent_pipe = parent_pipe,
2541
client_object = client))
2783
2542
parent_pipe.send(True)
2784
2543
# remove the old hook in favor of the new above hook on
2787
if command == "funcall":
2546
if command == 'funcall':
2788
2547
funcname = request[1]
2789
2548
args = request[2]
2790
2549
kwargs = request[3]
2792
parent_pipe.send(("data", getattr(client_object,
2551
parent_pipe.send(('data', getattr(client_object,
2793
2552
funcname)(*args,
2796
if command == "getattr":
2555
if command == 'getattr':
2797
2556
attrname = request[1]
2798
2557
if isinstance(client_object.__getattribute__(attrname),
2799
collections.abc.Callable):
2800
parent_pipe.send(("function", ))
2558
collections.Callable):
2559
parent_pipe.send(('function', ))
2802
2561
parent_pipe.send((
2803
"data", client_object.__getattribute__(attrname)))
2805
if command == "setattr":
2562
'data', client_object.__getattribute__(attrname)))
2564
if command == 'setattr':
2806
2565
attrname = request[1]
2807
2566
value = request[2]
2808
2567
setattr(client_object, attrname, value)
2813
2572
def rfc3339_duration_to_delta(duration):
2814
2573
"""Parse an RFC 3339 "duration" and return a datetime.timedelta
2816
>>> timedelta = datetime.timedelta
2817
>>> rfc3339_duration_to_delta("P7D") == timedelta(7)
2819
>>> rfc3339_duration_to_delta("PT60S") == timedelta(0, 60)
2821
>>> rfc3339_duration_to_delta("PT60M") == timedelta(0, 3600)
2823
>>> rfc3339_duration_to_delta("PT24H") == timedelta(1)
2825
>>> rfc3339_duration_to_delta("P1W") == timedelta(7)
2827
>>> rfc3339_duration_to_delta("PT5M30S") == timedelta(0, 330)
2829
>>> rfc3339_duration_to_delta("P1DT3M20S") == timedelta(1, 200)
2575
>>> rfc3339_duration_to_delta("P7D")
2576
datetime.timedelta(7)
2577
>>> rfc3339_duration_to_delta("PT60S")
2578
datetime.timedelta(0, 60)
2579
>>> rfc3339_duration_to_delta("PT60M")
2580
datetime.timedelta(0, 3600)
2581
>>> rfc3339_duration_to_delta("PT24H")
2582
datetime.timedelta(1)
2583
>>> rfc3339_duration_to_delta("P1W")
2584
datetime.timedelta(7)
2585
>>> rfc3339_duration_to_delta("PT5M30S")
2586
datetime.timedelta(0, 330)
2587
>>> rfc3339_duration_to_delta("P1DT3M20S")
2588
datetime.timedelta(1, 200)
2834
2591
# Parsing an RFC 3339 duration with regular expressions is not
2835
2592
# possible - there would have to be multiple places for the same
2836
2593
# values, like seconds. The current code, while more esoteric, is
2837
2594
# cleaner without depending on a parsing library. If Python had a
2838
2595
# built-in library for parsing we would use it, but we'd like to
2839
2596
# avoid excessive use of external libraries.
2841
2598
# New type for defining tokens, syntax, and semantics all-in-one
2842
2599
Token = collections.namedtuple("Token", (
2843
2600
"regexp", # To match token; if "value" is not None, must have