From b8739e7f879a1115310ab57ae1b11f24eb0ca45d Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Fri, 5 Jul 2024 15:21:46 +0200 Subject: [PATCH] Better handle of SMB mount with special characters in username/password (#1448) --- music_assistant/server/helpers/process.py | 6 ++---- .../server/providers/filesystem_smb/__init__.py | 17 +++++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/music_assistant/server/helpers/process.py b/music_assistant/server/helpers/process.py index 492bca61..b6495186 100644 --- a/music_assistant/server/helpers/process.py +++ b/music_assistant/server/helpers/process.py @@ -257,12 +257,10 @@ class AsyncProcess: return self._returncode -async def check_output(*args: str) -> tuple[int, bytes]: +async def check_output(*args: str, env: dict[str, str] | None = None) -> tuple[int, bytes]: """Run subprocess and return returncode and output.""" proc = await asyncio.create_subprocess_exec( - *args, - stderr=asyncio.subprocess.STDOUT, - stdout=asyncio.subprocess.PIPE, + *args, stderr=asyncio.subprocess.STDOUT, stdout=asyncio.subprocess.PIPE, env=env ) stdout, _ = await proc.communicate() return (proc.returncode, stdout) diff --git a/music_assistant/server/providers/filesystem_smb/__init__.py b/music_assistant/server/providers/filesystem_smb/__init__.py index 6a0c4759..4fb2cf15 100644 --- a/music_assistant/server/providers/filesystem_smb/__init__.py +++ b/music_assistant/server/providers/filesystem_smb/__init__.py @@ -185,22 +185,18 @@ class SMBFileSystemProvider(LocalFileSystemProvider): subfolder = subfolder[:-1] if platform.system() == "Darwin": + # NOTE: MacOS does not support special characters in the username/password password_str = f":{password}" if password else "" mount_cmd = [ "mount", "-t", "smbfs", - f"//{username}:{password_str}@{server}/{share}{subfolder}", + f"//{username}{password_str}@{server}/{share}{subfolder}", self.base_path, ] elif platform.system() == "Linux": - options = [ - "rw", - f'username="{username}"', - ] - if password: - options.append(f'password="{password}"') + options = ["rw"] if mount_options := self.config.get_value(CONF_MOUNT_OPTIONS): options += mount_options.split(",") @@ -224,8 +220,13 @@ class SMBFileSystemProvider(LocalFileSystemProvider): "Using mount command: %s", [m.replace(password, "########") if password else m for m in mount_cmd], ) + env_vars = { + "USER": username, + } + if password: + env_vars["PASSWD"] = password - returncode, output = await check_output(*mount_cmd) + returncode, output = await check_output(*mount_cmd, env=env_vars) if returncode != 0: msg = f"SMB mount failed with error: {output.decode()}" raise LoginFailed(msg) -- 2.34.1