summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/core/configurable.py
blob: 3b97916db0fe32a455ccb64442da9e749bd38ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# -*- coding: utf-8 -*-
# configurable.py
# Copyright (C) 2015, 2016 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Configurable Backend for Bitmask Service.
"""
import ConfigParser
import locale
import os
import re
import sys

from twisted.application import service
from twisted.python import log

from leap.common import files


class MissingConfigEntry(Exception):
    """
    A required config entry was not found.
    """


class ConfigurableService(service.MultiService):

    config_file = u"bitmaskd.cfg"
    service_names = ('mail', 'eip', 'zmq', 'web')

    def __init__(self, basedir='~/.config/leap'):
        service.MultiService.__init__(self)

        path = os.path.abspath(os.path.expanduser(basedir))
        if not os.path.isdir(path):
            files.mkdir_p(path)
        self.basedir = path

        # creates self.config
        self.read_config()

    def get_config(self, section, option, default=None, boolean=False):
        try:
            if boolean:
                return self.config.getboolean(section, option)

            item = self.config.get(section, option)
            return item

        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
            if default is None:
                fn = os.path.join(self.basedir, self.config_file)
                raise MissingConfigEntry("%s is missing the [%s]%s entry"
                                         % (_quote_output(fn),
                                            section, option))
            return default

    def set_config(self, section, option, value):
        if not self.config.has_section(section):
            self.config.add_section(section)
        self.config.set(section, option, value)
        self.save_config()
        self.read_config()
        assert self.config.get(section, option) == value

    def read_config(self):
        self.config = ConfigParser.SafeConfigParser()
        bitmaskd_cfg = self._get_config_path()

        if not os.path.isfile(bitmaskd_cfg):
            self._create_default_config(bitmaskd_cfg)

        try:
            with open(bitmaskd_cfg, "rb") as f:
                self.config.readfp(f)
        except EnvironmentError:
            if os.path.exists(bitmaskd_cfg):
                raise

    def save_config(self):
        bitmaskd_cfg = self._get_config_path()
        with open(bitmaskd_cfg, 'wb') as f:
            self.config.write(f)

    def _create_default_config(self, path):
        with open(path, 'w') as outf:
            outf.write(DEFAULT_CONFIG)

    def _get_config_path(self):
        return os.path.join(self.basedir, self.config_file)


DEFAULT_CONFIG = """
[services]
mail = True
eip = True
zmq = True
web = False
"""


def canonical_encoding(encoding):
    if encoding is None:
        log.msg("Warning: falling back to UTF-8 encoding.", level=log.WEIRD)
        encoding = 'utf-8'
    encoding = encoding.lower()
    if encoding == "cp65001":
        encoding = 'utf-8'
    elif (encoding == "us-ascii" or encoding == "646" or encoding ==
          "ansi_x3.4-1968"):
        encoding = 'ascii'

    return encoding


def check_encoding(encoding):
    # sometimes Python returns an encoding name that it doesn't support for
    # conversion fail early if this happens
    try:
        u"test".encode(encoding)
    except (LookupError, AttributeError):
        raise AssertionError(
            "The character encoding '%s' is not supported for conversion." % (
                encoding,))

filesystem_encoding = None
io_encoding = None
is_unicode_platform = False


def _reload():
    global filesystem_encoding, io_encoding, is_unicode_platform

    filesystem_encoding = canonical_encoding(sys.getfilesystemencoding())
    check_encoding(filesystem_encoding)

    if sys.platform == 'win32':
        # On Windows we install UTF-8 stream wrappers for sys.stdout and
        # sys.stderr, and reencode the arguments as UTF-8 (see
        # scripts/runner.py).
        io_encoding = 'utf-8'
    else:
        ioenc = None
        if hasattr(sys.stdout, 'encoding'):
            ioenc = sys.stdout.encoding
        if ioenc is None:
            try:
                ioenc = locale.getpreferredencoding()
            except Exception:
                pass  # work around <http://bugs.python.org/issue1443504>
        io_encoding = canonical_encoding(ioenc)

    check_encoding(io_encoding)

    is_unicode_platform = sys.platform in ["win32", "darwin"]

_reload()


def _quote_output(s, quotemarks=True, quote_newlines=None, encoding=None):
    """
    Encode either a Unicode string or a UTF-8-encoded bytestring for
    representation on stdout or stderr, tolerating errors. If 'quotemarks' is
    True, the string is always quoted; otherwise, it is quoted only if
    necessary to avoid ambiguity or control bytes in the output. (Newlines are
    counted as control bytes iff quote_newlines is True.)

    Quoting may use either single or double quotes. Within single quotes, all
    characters stand for themselves, and ' will not appear. Within double
    quotes, Python-compatible backslash escaping is used.

    If not explicitly given, quote_newlines is True when quotemarks is True.
    """
    assert isinstance(s, (str, unicode))
    if quote_newlines is None:
        quote_newlines = quotemarks

    if isinstance(s, str):
        try:
            s = s.decode('utf-8')
        except UnicodeDecodeError:
            return 'b"%s"' % (
                ESCAPABLE_8BIT.sub(
                    lambda m: _str_escape(m, quote_newlines), s),)

    must_double_quote = (quote_newlines and MUST_DOUBLE_QUOTE_NL or
                         MUST_DOUBLE_QUOTE)
    if must_double_quote.search(s) is None:
        try:
            out = s.encode(encoding or io_encoding)
            if quotemarks or out.startswith('"'):
                return "'%s'" % (out,)
            else:
                return out
        except (UnicodeDecodeError, UnicodeEncodeError):
            pass

    escaped = ESCAPABLE_UNICODE.sub(
        lambda m: _unicode_escape(m, quote_newlines), s)
    return '"%s"' % (
        escaped.encode(encoding or io_encoding, 'backslashreplace'),)


def _unicode_escape(m, quote_newlines):
    u = m.group(0)
    if u == u'"' or u == u'$' or u == u'`' or u == u'\\':
        return u'\\' + u
    elif u == u'\n' and not quote_newlines:
        return u
    if len(u) == 2:
        codepoint = (
            ord(u[0]) - 0xD800) * 0x400 + ord(u[1]) - 0xDC00 + 0x10000
    else:
        codepoint = ord(u)
    if codepoint > 0xFFFF:
        return u'\\U%08x' % (codepoint,)
    elif codepoint > 0xFF:
        return u'\\u%04x' % (codepoint,)
    else:
        return u'\\x%02x' % (codepoint,)


def _str_escape(m, quote_newlines):
    c = m.group(0)
    if c == '"' or c == '$' or c == '`' or c == '\\':
        return '\\' + c
    elif c == '\n' and not quote_newlines:
        return c
    else:
        return '\\x%02x' % (ord(c),)

MUST_DOUBLE_QUOTE_NL = re.compile(
    ur'[^\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]',
    re.DOTALL)
MUST_DOUBLE_QUOTE = re.compile(
    ur'[^\n\x20-\x26\x28-\x7E\u00A0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFC]',
    re.DOTALL)

ESCAPABLE_8BIT = re.compile(
    r'[^ !#\x25-\x5B\x5D-\x5F\x61-\x7E]',
    re.DOTALL)

# if we must double-quote, then we have to escape ", $ and `, but need not
# escape '

ESCAPABLE_UNICODE = re.compile(
    ur'([\uD800-\uDBFF][\uDC00-\uDFFF])|'  # valid surrogate pairs
    ur'[^ !#\x25-\x5B\x5D-\x5F\x61-\x7E\u00A0-\uD7FF'
    ur'\uE000-\uFDCF\uFDF0-\uFFFC]',
    re.DOTALL)