From 9a810db1093ea448c487148f2a72b66db2cbd11a Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Thu, 6 Jan 2022 12:27:38 +0100 Subject: [PATCH] networkinterfaces: support relative paths in source/source-directory statements According to interfaces(5) [1]: When sourcing files or directories, if a path doesn't have a leading slash, it's considered relative to the directory containing the file in which the keyword is placed. In the example above, if the file is located at /etc/network/interfaces, paths to the included files are understood to be under /etc/network. Adapt the implementation to adhere to the documented behaviour. [1] https://manpages.debian.org/buster/ifupdown/interfaces.5.en.html --- ifupdown2/ifupdown/networkinterfaces.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ifupdown2/ifupdown/networkinterfaces.py b/ifupdown2/ifupdown/networkinterfaces.py index ce47e8d..910214d 100644 --- a/ifupdown2/ifupdown/networkinterfaces.py +++ b/ifupdown2/ifupdown/networkinterfaces.py @@ -161,9 +161,13 @@ class networkInterfaces(): def process_source(self, lines, cur_idx, lineno): # Support regex - self.logger.debug('processing sourced line ..\'%s\'' %lines[cur_idx]) + self.logger.debug('processing sourced line ..\'%s\'' % lines[cur_idx]) sourced_file = re.split(self._ws_split_regex, lines[cur_idx], 2)[1] + if sourced_file: + if not os.path.isabs(sourced_file): + sourced_file = os.path.join(os.path.dirname(self._currentfile), sourced_file) + filenames = sorted(glob.glob(sourced_file)) if not filenames: if '*' not in sourced_file: @@ -180,7 +184,11 @@ class networkInterfaces(): def process_source_directory(self, lines, cur_idx, lineno): self.logger.debug('processing source-directory line ..\'%s\'' % lines[cur_idx]) sourced_directory = re.split(self._ws_split_regex, lines[cur_idx], 2)[1] + if sourced_directory: + if not os.path.isabs(sourced_directory): + sourced_directory = os.path.join(os.path.dirname(self._currentfile), sourced_directory) + folders = glob.glob(sourced_directory) for folder in folders: filenames = [file for file in os.listdir(folder) if re.match(r'^[a-zA-Z0-9_-]+$', file)]