From nobody Tue Nov 26 11:25:59 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1708967014; cv=none; d=zohomail.com; s=zohoarc; b=evwOJe9BlOr9Disd6nTO/9Gk6/8ZVkQfBq7CSgFFB8qR6xbalqzQJXUcXQe4V1LGhCbRODAq3BO5c75xFt2duQ+VZWBCaI3VnNDWfqoF5HNUJg6U81pzSUHIiNcgzazAnfqU0bvRx+APH/8ox95GRcc01UbbhIqSIMGgi3ua098= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708967014; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=H0hbpkn404mwmdH7XPrx5U+ELdeb94tjp4nhB1Ff+A8=; b=B4L7my2+H3c2CN7bpxr5IFSCUO8erypnoGu7CGQfTAxHI+tknhOVXeR5p5qMPerVrNKI46+0QI6Rq9Vi180ilusC6o5jCAR/bo64be+2VY+eexwNWPueqmlYx9/1aVxdRkam4TD/241W6pkmhaOd3X7vUyA8qiyktkzK2doHu4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1708967014396508.5272465505992; Mon, 26 Feb 2024 09:03:34 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1reeHu-0005ba-RZ; Mon, 26 Feb 2024 11:57:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHl-0005XU-BI for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:57:01 -0500 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1reeHX-0007aR-7l for qemu-devel@nongnu.org; Mon, 26 Feb 2024 11:56:53 -0500 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1reeG3-001AD7-0w; Mon, 26 Feb 2024 17:56:34 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=H0hbpkn404mwmdH7XPrx5U+ELdeb94tjp4nhB1Ff+A8=; b=wba8/XTv1azl TIO+8csWhO3ezT84yk5UNqi87YhlGxvNl34y2F4omTqd9+n2Sno4w36816XhSG3SCL3YwJWqFOPrN bxACcR8xXO/tL2FYgEbGkTpsSit5YCy0+ad/lM4kdlHOJCuMDSfMLaiAlmb/7JDseDZQmiztbFXza /VeC+OLYIFYt+l5qq577xdyGwJ3CK/OoRSEm08gbAT9ZKZ+5KyR7toKfFA9NODvSHxO8s7SunY+iC 2MkKHNIIE6ELVDrwrR3FSZIk6mzurCcqMfGkN5GATz/vSAHwhGc/5BB8dawxPI0gpMZrhG2PmVlBX CQlQGBHHKcqRpjGAPArGQA==; From: Andrey Drobyshev To: qemu-devel@nongnu.org Cc: michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH 7/7] qga/commands-posix: qmp_guest_set_user_password: use ga_run_command helper Date: Mon, 26 Feb 2024 18:56:42 +0200 Message-Id: <20240226165642.807350-8-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> References: <20240226165642.807350-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1708967014852100001 Content-Type: text/plain; charset="utf-8" There's no need to check for the existence of the "chpasswd", "pw" executables, as the exec() call will do that for us. Signed-off-by: Andrey Drobyshev --- qga/commands-posix.c | 96 ++++++-------------------------------------- 1 file changed, 13 insertions(+), 83 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 599f59a323..7f39636417 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -2140,14 +2140,8 @@ void qmp_guest_set_user_password(const char *usernam= e, Error **errp) { Error *local_err =3D NULL; - char *passwd_path =3D NULL; - pid_t pid; - int status; - int datafd[2] =3D { -1, -1 }; - char *rawpasswddata =3D NULL; + g_autofree char *rawpasswddata =3D NULL; size_t rawpasswdlen; - char *chpasswddata =3D NULL; - size_t chpasswdlen; =20 rawpasswddata =3D (char *)qbase64_decode(password, -1, &rawpasswdlen, = errp); if (!rawpasswddata) { @@ -2158,95 +2152,31 @@ void qmp_guest_set_user_password(const char *userna= me, =20 if (strchr(rawpasswddata, '\n')) { error_setg(errp, "forbidden characters in raw password"); - goto out; + return; } =20 if (strchr(username, '\n') || strchr(username, ':')) { error_setg(errp, "forbidden characters in username"); - goto out; + return; } =20 #ifdef __FreeBSD__ - chpasswddata =3D g_strdup(rawpasswddata); - passwd_path =3D g_find_program_in_path("pw"); + g_autofree char *chpasswdata =3D g_strdup(rawpasswddata); + const char *crypt_flag =3D (crypted) ? "-H" : "-h"; + const char *argv[] =3D {"pw", "usermod", "-n", username, + crypt_flag, "0", NULL}; #else - chpasswddata =3D g_strdup_printf("%s:%s\n", username, rawpasswddata); - passwd_path =3D g_find_program_in_path("chpasswd"); + g_autofree char *chpasswddata =3D g_strdup_printf("%s:%s\n", username, + rawpasswddata); + const char *crypt_flag =3D (crypted) ? "-e" : NULL; + const char *argv[] =3D {"chpasswd", crypt_flag, NULL}; #endif =20 - chpasswdlen =3D strlen(chpasswddata); - - if (!passwd_path) { - error_setg(errp, "cannot find 'passwd' program in PATH"); - goto out; - } - - if (!g_unix_open_pipe(datafd, FD_CLOEXEC, NULL)) { - error_setg(errp, "cannot create pipe FDs"); - goto out; - } - - pid =3D fork(); - if (pid =3D=3D 0) { - close(datafd[1]); - /* child */ - setsid(); - dup2(datafd[0], 0); - reopen_fd_to_null(1); - reopen_fd_to_null(2); - -#ifdef __FreeBSD__ - const char *h_arg; - h_arg =3D (crypted) ? "-H" : "-h"; - execl(passwd_path, "pw", "usermod", "-n", username, h_arg, "0", NU= LL); -#else - if (crypted) { - execl(passwd_path, "chpasswd", "-e", NULL); - } else { - execl(passwd_path, "chpasswd", NULL); - } -#endif - _exit(EXIT_FAILURE); - } else if (pid < 0) { - error_setg_errno(errp, errno, "failed to create child process"); - goto out; - } - close(datafd[0]); - datafd[0] =3D -1; - - if (qemu_write_full(datafd[1], chpasswddata, chpasswdlen) !=3D chpassw= dlen) { - error_setg_errno(errp, errno, "cannot write new account password"); - goto out; - } - close(datafd[1]); - datafd[1] =3D -1; - - ga_wait_child(pid, &status, &local_err); + ga_run_command(argv, chpasswddata, "set user password", &local_err); if (local_err) { error_propagate(errp, local_err); - goto out; - } - - if (!WIFEXITED(status)) { - error_setg(errp, "child process has terminated abnormally"); - goto out; - } - - if (WEXITSTATUS(status)) { - error_setg(errp, "child process has failed to set user password"); - goto out; - } - -out: - g_free(chpasswddata); - g_free(rawpasswddata); - g_free(passwd_path); - if (datafd[0] !=3D -1) { - close(datafd[0]); - } - if (datafd[1] !=3D -1) { - close(datafd[1]); + return; } } #else /* __linux__ || __FreeBSD__ */ --=20 2.39.3