From 75d2348a92fa33a3d0e13dce2e94ff48dcd5d24b Mon Sep 17 00:00:00 2001 From: Juergen Kellerer Date: Thu, 20 Apr 2023 12:00:28 +0200 Subject: [PATCH] Sftp: Use a more general ls regex pattern Fixes cases where `ls -la` returns ? instead of a size or link count. Also supports all types and perms specified in GNU ls now --- .../OPNsense/AcmeClient/SftpClient.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/SftpClient.php b/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/SftpClient.php index 064b13403..1804596af 100644 --- a/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/SftpClient.php +++ b/security/acme-client/src/opnsense/mvc/app/library/OPNsense/AcmeClient/SftpClient.php @@ -199,17 +199,28 @@ class SftpClient $this->processAvailableInput(); $this->process->put("ls -la"); - $regex = '/^([bcdlsp\-][rwx\-]{9}[+@]?)\s+[0-9]+\s+([^\s]+)\s+([^\s]+)\s+([0-9]+)\s+(\w+\s+[0-9]+\s+[0-9:]+)\s+(.+)$/'; + $regex = '/^' + . '(?P[?bcCdDlMnpPs\-])' + . '(?P([rw\-]{2}[sStTx\-]){3})' + . '(?P[^\s])?' . '\s+' + . '(?P[^\s]+)' . '\s+' + . '(?P[^\s]+)' . '\s+' + . '(?P[^\s]+)' . '\s+' + . '(?P[^\s]+)' . '\s+' + . '(?P\w+\s+[0-9]+\s+[0-9:]+)' . '\s+' + . '(?P.+)' . '\s*' + . '$/'; + $this->processAvailableInput(self::COMMAND_REPLY_TIMEOUT, 2, function ($line) use (&$files, $regex) { if (preg_match($regex, $line, $matches)) { - $filename = trim(stripcslashes($matches[6])); // decodes octal UTF-8 sequences + $filename = trim(stripcslashes($matches["filename"])); // decodes octal UTF-8 sequences $files[$filename] = [ - "type" => $matches[1][0], - "permissions" => $matches[1], - "owner" => $matches[2], - "group" => $matches[3], - "size" => intval($matches[4]), - "mtime" => strtotime($matches[5]) + "type" => $matches["type"], + "permissions" => $matches["permissions"], + "owner" => stripcslashes($matches["owner"]), + "group" => stripcslashes($matches["group"]), + "size" => intval($matches["size"]), + "mtime" => strtotime($matches["mtime"]) ]; return true; }