From nobody Sun Jun 28 18:40:27 2026 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4gpJBM4rJlz6jkYv for ; Sun, 28 Jun 2026 18:40:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gpJBM2lCBz3Xlv for ; Sun, 28 Jun 2026 18:40:27 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1782672027; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=QEVPKIAX/mVYtAk/ejGBiSrbDTzYEq2zLjh1AFhfiqQ=; b=IaH5HdBeX4TWplM/K7P49NPnp0stJBaiCDmwU0ambUizuz460GvPi3cB2lE306uUKHRqdo MBgwrMGESbpL/jIjt3nd4Qkp8c7d5k/SHdP22RpjDlsYfS8mV39/MyQMe3F2R9yG1krhD5 omYWk00iD2ge3ZgHvYU02mxmkk+dsR6EnvhzzhjSq2IrmTClB2lOq1iRH6HxX/nUlIy4of YwngRSEM/XBEgZJIR7HGr3rLkoCNc+0pFsyvFu/PSgEs6BTYqdinxJy+i7eFkHQMJBsef2 Vajhuu56hAjRPZQHgDV6s7oPlQgyhFzoWb24aCm7jcF45rEwY2iqkhoqn9uweA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1782672027; a=rsa-sha256; cv=none; b=oXM9iJM2WyuE29TpMw2K3TJvV2Xhdkq01/vD05IjA1XVuHSIQRNcfcxTCsRDJthpFrgWMs FM4LUSKkeE7OD5Apz7Q+5dF/rKDAPmomZkbUiNrm5wfs7ZQIbXP9jvhkrpbiLwbxhLHFiP mXROkxgnHy7rokK+lEiI8d8RV9dV6rIKST6K+UMwF/nRHM4cW7men+DEaz8BCCv21ckSWw PsX+ySwkCSzMvhcXVYaDiXdPpS5Hu+w0+Bt1v+7yKxYvqrRgPK2DTWneI74HHozLfPxQaw e/4OPRsN8iFPqEx77QqsadJdQ60vhEyQ2G6NI+oV4HJSaBwCM4DgADATMuY5EA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1782672027; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=QEVPKIAX/mVYtAk/ejGBiSrbDTzYEq2zLjh1AFhfiqQ=; b=CLtAzZDyiiBDrMh2uG0Y02Zzfeh8vwOcaNW2TeCABmZT/hb6ozq22vTXHuWHT63SP0gcsx pX2lvKnklShEKz2X4HgoC/wLOsmgUH28I6Dl2hcVvRQEvcaIl9+p+eGEW5sZSQF1M9M+rW sDtUvXBF1gBWZKPhlfoKY6s35NctsIXRSKyufIhJFw5vpq9xX7XyrzLqDCPwR+w9HAWEog McDjy+i8hqscpPEuYtzF/R8TWlCp68nPFgab0WNpanh1qgph7uHw/T4n9L6Nftpof+s3BH 9/GAfgeed0Bv+7zhHA49t+b7m2GvKPNtHWECQwg3pQAUhiIVHUXJJKT+F7p2ag== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gpJBM1ncPz707 for ; Sun, 28 Jun 2026 18:40:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1c8ea by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sun, 28 Jun 2026 18:40:27 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Baptiste Daroussin Subject: git: 41fc76cd796d - stable/15 - nuageinit: fix update_sshd_config crash when file does not exist List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 41fc76cd796d8e324673c769a267e3858941bac6 Auto-Submitted: auto-generated Date: Sun, 28 Jun 2026 18:40:27 +0000 Message-Id: <6a416a9b.1c8ea.6fba50ac@gitrepo.freebsd.org> The branch stable/15 has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=41fc76cd796d8e324673c769a267e3858941bac6 commit 41fc76cd796d8e324673c769a267e3858941bac6 Author: Baptiste Daroussin AuthorDate: 2026-06-04 19:10:37 +0000 Commit: Baptiste Daroussin CommitDate: 2026-06-28 18:32:25 +0000 nuageinit: fix update_sshd_config crash when file does not exist Previously update_sshd_config() would assert-fail if sshd_config did not exist. Now it creates a new file with the given key/value. Also replace the fragile simultaneous r+ + temp file approach with a cleaner read-then-write pattern: read all lines into memory, modify as needed, then write to a temp file and rename. All assert() calls replaced with proper error handling via warnmsg(). Add test case for missing file creation. (cherry picked from commit 0ba9b7b7f815b57f1c121b0f78eaee02d2cdd414) --- libexec/nuageinit/nuage.lua | 43 +++++++++++++++++++------- libexec/nuageinit/tests/update_sshd_config.lua | 7 +++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/libexec/nuageinit/nuage.lua b/libexec/nuageinit/nuage.lua index 56d1ccf9d52e..bdd4bf60007e 100644 --- a/libexec/nuageinit/nuage.lua +++ b/libexec/nuageinit/nuage.lua @@ -493,30 +493,49 @@ local function update_sshd_config(key, value) if root then sshd_config = root .. sshd_config end - local f = assert(io.open(sshd_config, "r+")) - local tgt = assert(io.open(sshd_config .. ".nuageinit", "w")) + local f = io.open(sshd_config, "r") + if not f then + -- File does not exist, create it with the given key/value + f = io.open(sshd_config, "w") + if not f then + warnmsg("Unable to open " .. sshd_config .. " for writing") + return + end + f:write(key .. " " .. value .. "\n") + f:close() + return + end + -- Read existing content + local lines = {} local found = false local pattern = "^%s*"..key:lower().."%s+(%w+)%s*#?.*$" - while true do - local line = f:read() - if line == nil then break end + for line in f:lines() do local _, _, val = line:lower():find(pattern) if val then found = true - if val == value then - assert(tgt:write(line .. "\n")) + if val ~= value then + table.insert(lines, key .. " " .. value) else - assert(tgt:write(key .. " " .. value .. "\n")) + table.insert(lines, line) end else - assert(tgt:write(line .. "\n")) + table.insert(lines, line) end end + f:close() if not found then - assert(tgt:write(key .. " " .. value .. "\n")) + table.insert(lines, key .. " " .. value) end - assert(f:close()) - assert(tgt:close()) + -- Write back + f = io.open(sshd_config .. ".nuageinit", "w") + if not f then + warnmsg("Unable to open " .. sshd_config .. ".nuageinit for writing") + return + end + for _, l in ipairs(lines) do + f:write(l .. "\n") + end + f:close() os.rename(sshd_config .. ".nuageinit", sshd_config) end diff --git a/libexec/nuageinit/tests/update_sshd_config.lua b/libexec/nuageinit/tests/update_sshd_config.lua index ac56c29986ac..95aea59bd993 100644 --- a/libexec/nuageinit/tests/update_sshd_config.lua +++ b/libexec/nuageinit/tests/update_sshd_config.lua @@ -70,4 +70,11 @@ if read_config() ~= "PasswordAuthentication yes\n" then n.err("Extra spaces handling failed: '" .. read_config() .. "'") end +-- File does not exist: should be created with key/value +os.remove(sshd_config) +n.update_sshd_config("PasswordAuthentication", "yes") +if read_config() ~= "PasswordAuthentication yes\n" then + n.err("Missing file: should create: '" .. read_config() .. "'") +end + os.exit(0)