/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

  • Committer: Teddy Hogeborn
  • Date: 2019-11-03 19:17:57 UTC
  • Revision ID: teddy@recompile.se-20191103191757-1hdpp0u5fxa8iumo
INSTALL: Add "-" argument to "su" invocations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
 
1
#!/usr/bin/python3 -bI
2
2
# -*- mode: python; after-save-hook: (lambda () (let ((command (if (fboundp 'file-local-name) (file-local-name (buffer-file-name)) (or (file-remote-p (buffer-file-name) 'localname) (buffer-file-name))))) (if (= (progn (if (get-buffer "*Test*") (kill-buffer "*Test*")) (process-file-shell-command (format "%s --check" (shell-quote-argument command)) nil "*Test*")) 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w))) (progn (with-current-buffer "*Test*" (compilation-mode)) (display-buffer "*Test*" '(display-buffer-in-side-window)))))); coding: utf-8 -*-
3
3
#
4
4
# Mandos server - give out binary blobs to connecting clients.
128
128
if sys.version_info < (3, 2):
129
129
    configparser.Configparser = configparser.SafeConfigParser
130
130
 
131
 
version = "1.8.8"
 
131
version = "1.8.9"
132
132
stored_state_file = "clients.pickle"
133
133
 
134
134
logger = logging.getLogger()
203
203
            output = subprocess.check_output(["gpgconf"])
204
204
            for line in output.splitlines():
205
205
                name, text, path = line.split(b":")
206
 
                if name == "gpg":
 
206
                if name == b"gpg":
207
207
                    self.gpg = path
208
208
                    break
209
209
        except OSError as e:
214
214
                          '--force-mdc',
215
215
                          '--quiet']
216
216
        # Only GPG version 1 has the --no-use-agent option.
217
 
        if self.gpg == "gpg" or self.gpg.endswith("/gpg"):
 
217
        if self.gpg == b"gpg" or self.gpg.endswith(b"/gpg"):
218
218
            self.gnupgargs.append("--no-use-agent")
219
219
 
220
220
    def __enter__(self):
1053
1053
        # Read return code from connection (see call_pipe)
1054
1054
        returncode = connection.recv()
1055
1055
        connection.close()
1056
 
        self.checker.join()
 
1056
        if self.checker is not None:
 
1057
            self.checker.join()
1057
1058
        self.checker_callback_tag = None
1058
1059
        self.checker = None
1059
1060
 
1150
1151
                kwargs=popen_args)
1151
1152
            self.checker.start()
1152
1153
            self.checker_callback_tag = GLib.io_add_watch(
1153
 
                pipe[0].fileno(), GLib.IO_IN,
 
1154
                GLib.IOChannel.unix_new(pipe[0].fileno()),
 
1155
                GLib.PRIORITY_DEFAULT, GLib.IO_IN,
1154
1156
                self.checker_callback, pipe[0], command)
1155
1157
        # Re-run this periodically if run by GLib.timeout_add
1156
1158
        return True
2680
2682
    def add_pipe(self, parent_pipe, proc):
2681
2683
        # Call "handle_ipc" for both data and EOF events
2682
2684
        GLib.io_add_watch(
2683
 
            parent_pipe.fileno(),
2684
 
            GLib.IO_IN | GLib.IO_HUP,
 
2685
            GLib.IOChannel.unix_new(parent_pipe.fileno()),
 
2686
            GLib.PRIORITY_DEFAULT, GLib.IO_IN | GLib.IO_HUP,
2685
2687
            functools.partial(self.handle_ipc,
2686
2688
                              parent_pipe=parent_pipe,
2687
2689
                              proc=proc))
2725
2727
                return False
2726
2728
 
2727
2729
            GLib.io_add_watch(
2728
 
                parent_pipe.fileno(),
2729
 
                GLib.IO_IN | GLib.IO_HUP,
 
2730
                GLib.IOChannel.unix_new(parent_pipe.fileno()),
 
2731
                GLib.PRIORITY_DEFAULT, GLib.IO_IN | GLib.IO_HUP,
2730
2732
                functools.partial(self.handle_ipc,
2731
2733
                                  parent_pipe=parent_pipe,
2732
2734
                                  proc=proc,
2764
2766
def rfc3339_duration_to_delta(duration):
2765
2767
    """Parse an RFC 3339 "duration" and return a datetime.timedelta
2766
2768
 
2767
 
    >>> rfc3339_duration_to_delta("P7D")
2768
 
    datetime.timedelta(7)
2769
 
    >>> rfc3339_duration_to_delta("PT60S")
2770
 
    datetime.timedelta(0, 60)
2771
 
    >>> rfc3339_duration_to_delta("PT60M")
2772
 
    datetime.timedelta(0, 3600)
2773
 
    >>> rfc3339_duration_to_delta("PT24H")
2774
 
    datetime.timedelta(1)
2775
 
    >>> rfc3339_duration_to_delta("P1W")
2776
 
    datetime.timedelta(7)
2777
 
    >>> rfc3339_duration_to_delta("PT5M30S")
2778
 
    datetime.timedelta(0, 330)
2779
 
    >>> rfc3339_duration_to_delta("P1DT3M20S")
2780
 
    datetime.timedelta(1, 200)
 
2769
    >>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
 
2770
    True
 
2771
    >>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
 
2772
    True
 
2773
    >>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(0, 3600)
 
2774
    True
 
2775
    >>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
 
2776
    True
 
2777
    >>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
 
2778
    True
 
2779
    >>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
 
2780
    True
 
2781
    >>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
 
2782
    True
2781
2783
    """
2782
2784
 
2783
2785
    # Parsing an RFC 3339 duration with regular expressions is not
2863
2865
def string_to_delta(interval):
2864
2866
    """Parse a string and return a datetime.timedelta
2865
2867
 
2866
 
    >>> string_to_delta('7d')
2867
 
    datetime.timedelta(7)
2868
 
    >>> string_to_delta('60s')
2869
 
    datetime.timedelta(0, 60)
2870
 
    >>> string_to_delta('60m')
2871
 
    datetime.timedelta(0, 3600)
2872
 
    >>> string_to_delta('24h')
2873
 
    datetime.timedelta(1)
2874
 
    >>> string_to_delta('1w')
2875
 
    datetime.timedelta(7)
2876
 
    >>> string_to_delta('5m 30s')
2877
 
    datetime.timedelta(0, 330)
 
2868
    >>> string_to_delta('7d') == datetime.timedelta(7)
 
2869
    True
 
2870
    >>> string_to_delta('60s') == datetime.timedelta(0, 60)
 
2871
    True
 
2872
    >>> string_to_delta('60m') == datetime.timedelta(0, 3600)
 
2873
    True
 
2874
    >>> string_to_delta('24h') == datetime.timedelta(1)
 
2875
    True
 
2876
    >>> string_to_delta('1w') == datetime.timedelta(7)
 
2877
    True
 
2878
    >>> string_to_delta('5m 30s') == datetime.timedelta(0, 330)
 
2879
    True
2878
2880
    """
2879
2881
 
2880
2882
    try:
3250
3252
                             if isinstance(s, bytes)
3251
3253
                             else s) for s in
3252
3254
                            value["client_structure"]]
3253
 
                        # .name & .host
3254
 
                        for k in ("name", "host"):
 
3255
                        # .name, .host, and .checker_command
 
3256
                        for k in ("name", "host", "checker_command"):
3255
3257
                            if isinstance(value[k], bytes):
3256
3258
                                value[k] = value[k].decode("utf-8")
3257
3259
                        if "key_id" not in value:
3267
3269
                        for key, value in
3268
3270
                        bytes_old_client_settings.items()}
3269
3271
                    del bytes_old_client_settings
3270
 
                    # .host
 
3272
                    # .host and .checker_command
3271
3273
                    for value in old_client_settings.values():
3272
 
                        if isinstance(value["host"], bytes):
3273
 
                            value["host"] = (value["host"]
3274
 
                                             .decode("utf-8"))
 
3274
                        for attribute in ("host", "checker_command"):
 
3275
                            if isinstance(value[attribute], bytes):
 
3276
                                value[attribute] = (value[attribute]
 
3277
                                                    .decode("utf-8"))
3275
3278
            os.remove(stored_state_path)
3276
3279
        except IOError as e:
3277
3280
            if e.errno == errno.ENOENT:
3602
3605
                sys.exit(1)
3603
3606
            # End of Avahi example code
3604
3607
 
3605
 
        GLib.io_add_watch(tcp_server.fileno(), GLib.IO_IN,
3606
 
                          lambda *args, **kwargs:
3607
 
                          (tcp_server.handle_request
3608
 
                           (*args[2:], **kwargs) or True))
 
3608
        GLib.io_add_watch(
 
3609
            GLib.IOChannel.unix_new(tcp_server.fileno()),
 
3610
            GLib.PRIORITY_DEFAULT, GLib.IO_IN,
 
3611
            lambda *args, **kwargs: (tcp_server.handle_request
 
3612
                                     (*args[2:], **kwargs) or True))
3609
3613
 
3610
3614
        logger.debug("Starting main loop")
3611
3615
        main_loop.run()