2017-03-22 15:14:28 +01:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 boring-stuff <boring-stuff@users.noreply.github.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2017-03-22 15:14:28 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2017-03-22 15:14:28 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2017-03-22 15:14:28 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2017-03-22 15:14:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_char(input_char, extra_allowed_characters):
|
|
|
|
input_char_ord = ord(input_char)
|
|
|
|
|
2017-03-22 16:27:14 +01:00
|
|
|
if (ord('a') <= input_char_ord <= ord('z')) \
|
|
|
|
or (ord('A') <= input_char_ord <= ord('Z')) \
|
|
|
|
or (ord('0') <= input_char_ord <= ord('9')) \
|
2017-03-22 15:14:28 +01:00
|
|
|
or (input_char in ['$', '_', '-', '.']) \
|
2017-03-22 16:27:14 +01:00
|
|
|
or (input_char in extra_allowed_characters):
|
2017-03-22 15:14:28 +01:00
|
|
|
result = input_char
|
|
|
|
else:
|
|
|
|
result = '_'
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2017-03-22 16:27:14 +01:00
|
|
|
|
2017-03-22 15:14:28 +01:00
|
|
|
# This function needs to be synchronized with qrexec-daemon.c's sanitize_name()
|
|
|
|
# from the qubes-core-admin-linux repository.
|
|
|
|
#
|
2017-03-22 16:27:14 +01:00
|
|
|
# See https://github.com/QubesOS/qubes-core-admin-linux/blob/
|
|
|
|
# 4f0878ccbf8a95f8264b54d2b6f4dc433ca0793a/qrexec/qrexec-daemon.c#L627-L646
|
2017-03-22 15:14:28 +01:00
|
|
|
#
|
|
|
|
def _sanitize_name(input_string, extra_allowed_characters, assert_sanitized):
|
2017-03-22 16:27:14 +01:00
|
|
|
result = ''.join(_sanitize_char(character, extra_allowed_characters)
|
2017-03-22 15:14:28 +01:00
|
|
|
for character in input_string)
|
|
|
|
|
|
|
|
if assert_sanitized:
|
|
|
|
assert input_string == result, \
|
|
|
|
'Input string was expected to be sanitized, but was not.'
|
|
|
|
else:
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-03-22 16:27:14 +01:00
|
|
|
def sanitize_domain_name(input_string, assert_sanitized=False):
|
|
|
|
return _sanitize_name(input_string, {}, assert_sanitized)
|
|
|
|
|
|
|
|
|
|
|
|
def sanitize_service_name(input_string, assert_sanitized=False):
|
2017-03-22 15:14:28 +01:00
|
|
|
return _sanitize_name(input_string, {'+'}, assert_sanitized)
|