From nobody Thu Jun 4 21:15:47 2026 X-Original-To: dev-commits-src-main@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 4gWcmh0fBrz6gxfM for ; Thu, 04 Jun 2026 21:15:48 +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 "R13" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gWcmg4dCcz3Lnv for ; Thu, 04 Jun 2026 21:15:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780607747; 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=aWfXW+CE4LyN74LTw5ZHIh3BEQtrHd9MH0a400wuous=; b=Bo2rmDZX+6RHhlh57X8Vdfa8fpX3PO8l7sjKbq+VAlQtBnisBS5KgpfHj4lMxUEtnHhx04 8spJWdzhrq5VcEoKg35P2qJ/49cNur1fzqPwtIexE4YEyNykC3RR1LuXfQQafLy/qgTg1v pr8xYAls3IOqmFZ/4SmcGdO75cDl56swEdF71ANJN6jP4+ujEGglQupR5EDu1g81sFFJCG oqndajXGiPPCHKS3fku5rlrmyXcYqf/GcLpnqCaZ2I3fNepP/SKnq9YI4NR3OxjsSxLUDr pjNncScQuOBTqPliGzaoW6y1ODxRVLXrPnkmUYzjvgrlO/YQRrAj4/zhicR7wQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1780607747; a=rsa-sha256; cv=none; b=IgUzSglIWTOqqyKTAoQq6XK8Aa/XoHbvbFQ7uVWpG0jhy9U3sxNiF7YdV+zE8jug75cHWI EI/PjfewOfrtcvHOb5YlmqW93hkKrkhmnqyc3zZ8K32nHOfA8zPLYZvtiyF7TTrcV6nLnc ITc1Pw4tyPN0qzvZBltNxtggwWJ+C3DWQBb5JUf5tPqOp07IIEVPzVGnYBjm52RYcnLTAH Y+Y/5J4EPOLt1Srm406XN7HXGJBlDPzsDPy/tQj8b4ucyK+/U/JtWM7W1+g/PV1hwDVZ6G cHH7WTjJ4UgWtctxJQpWD33Ba+0GE6OzxtvutD58cCqb20UPuzvx2xM5SWxokQ== 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=1780607747; 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=aWfXW+CE4LyN74LTw5ZHIh3BEQtrHd9MH0a400wuous=; b=rLsbg9elFIdLOMIJaQnkpA4CCj9+fl14BCvxfHx/mCOgzXN7B0EJ3cIav020BYPo0YJ/j5 vwoB/73o2ETdgYtDw5/wv7vXpPPDGkqKeZqRRVWPSNqYfOBQWzcJiMa8vsZ/3sDM3G6yqO muP6Hfk03IxF2FHczhShUYDzOVaCGYz2F/cbYsYwOLq/KQAx4TZLE+m02yxRutY8ofWXX5 fJ8C8P0FloXt4R+kvRQ4BVC44l8aTw3bkhYJ6K3uHAj4N+JDO7F+CyFXYahMF/H6gn7v+q I2eYSVZOpjviyUbLuZHIDP+1rUJlU0vwiv4pPDjZKEAHkz3zHOv99ssSYYUAMg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gWcmg48vcz1DYW for ; Thu, 04 Jun 2026 21:15:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3c652 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 04 Jun 2026 21:15:47 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 0ba9b7b7f815 - main - nuageinit: fix update_sshd_config crash when file does not exist List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 0ba9b7b7f815b57f1c121b0f78eaee02d2cdd414 Auto-Submitted: auto-generated Date: Thu, 04 Jun 2026 21:15:47 +0000 Message-Id: <6a21eb03.3c652.63cc2d@gitrepo.freebsd.org> The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=0ba9b7b7f815b57f1c121b0f78eaee02d2cdd414 commit 0ba9b7b7f815b57f1c121b0f78eaee02d2cdd414 Author: Baptiste Daroussin AuthorDate: 2026-06-04 19:10:37 +0000 Commit: Baptiste Daroussin CommitDate: 2026-06-04 19:10:37 +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. --- 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)