From nobody Wed Nov 27 14:39:31 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; arc=pass (i=1 dmarc=pass fromdomain=virtuozzo.com); dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=2; a=rsa-sha256; t=1698242523; cv=pass; d=zohomail.com; s=zohoarc; b=GV2aYH7sBiXUAjOdiAWktMUiuGfho6NKJveDUOJTG/gpsJxCmrTjNuqUtL5hKaPXoQCHJ292+vfl3eNToGHU5q8IU+RLnXDww6GjA3vkPl1RhanXyZ1QVB2NoiA3bJhUTCChuhCaK14aykHjKoTkRA9grpIYUqwFSe352Jo81qs= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1698242523; h=Content-Type: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=gIS1iMBgN5PYmhLo4ft1oZBzpwvYIL4FppP3JuHN5VA=; b=iy75fVMOfvKO9v/nu/3U1VLKSJf9Xb4zOkUvf0ro8Ss812MuJ31THa9rBhtZ/fMOoStx8B1oPA1W+zbNpCwRooMI4BmfF+zPGVM0P8amnbob7HxCR55HToYSPBmNlMFgVq83xNdteuGWHg7eZAtkvy+kHRLkDxwA5s6/bSABB/A= ARC-Authentication-Results: i=2; 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; arc=pass (i=1 dmarc=pass fromdomain=virtuozzo.com); 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 1698242523060533.2526375536312; Wed, 25 Oct 2023 07:02:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qveRq-0003yl-Sj; Wed, 25 Oct 2023 10:01:26 -0400 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 1qveRp-0003ya-9T for qemu-devel@nongnu.org; Wed, 25 Oct 2023 10:01:25 -0400 Received: from mail-vi1eur04on070f.outbound.protection.outlook.com ([2a01:111:f400:fe0e::70f] helo=EUR04-VI1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qveRk-0005dH-L6 for qemu-devel@nongnu.org; Wed, 25 Oct 2023 10:01:25 -0400 Received: from VI0PR08MB10743.eurprd08.prod.outlook.com (2603:10a6:800:205::19) by AS8PR08MB10363.eurprd08.prod.outlook.com (2603:10a6:20b:56b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.31; Wed, 25 Oct 2023 14:01:09 +0000 Received: from VI0PR08MB10743.eurprd08.prod.outlook.com ([fe80::22f7:6498:de53:5371]) by VI0PR08MB10743.eurprd08.prod.outlook.com ([fe80::22f7:6498:de53:5371%4]) with mapi id 15.20.6863.043; Wed, 25 Oct 2023 14:01:09 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jBqgWxtk6wMbQKZXyTyl2OskNcGyaWvxDJrNZXnwSG7vBt2Uvf3Nyj8zkQyEjCl2QlOeWAvMgHhlWbiQKvpj0QUVPSgChxzLL64FZqvlh2lGTC6ed338yJawYB1xQKZWlw0HZ4elKHEpovtGwYIK/OEME03c87Uv1vZPS44JSJufIQ0nbCdWpCIUFV3Y3jkCKhBkM7OWmh+D1UOBrLZaR2bMlEkQAYY+ABdZte3RzI/80DJokej37G4TUXutK7nZbzYMAUtBogIU2IWMmsSE5WG9bbxuZ/wrj3FwBP7lqxkzXbHII4Ou/fXyLsbBHwBVAqvOf72cFS1gStxijU1RPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gIS1iMBgN5PYmhLo4ft1oZBzpwvYIL4FppP3JuHN5VA=; b=N4VYvETkqrXE0y6ELFaGs4SXH8o+WaUKYs+5CLiaHIM07HJpHLl1uetLAi7hUCqFw0NNE655sWIUyIO2NV0GpDoiL5x9aSzbefnCkVKo6JveVPKzmtytOwyJK77Cl854IXoeCtC2Tk1SQtgWDw/7qDV3KwNPFZC94fd1QxsXqwEBJAixJa6W9ouAANAbJWROVZ6vumhEc8pPCJjK1Doax+fJf2rPAYxILGanrV7OQMYxGNvIRBq8Gw3ZW6evuoRQOv2p4k/O/FUBA9ogjCbk7IH7hx5FBnJURDRo6oD6htdYq3vhz8ToIEkPKtwvhMrFodnuc9GThf5IVuMknx8lgQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gIS1iMBgN5PYmhLo4ft1oZBzpwvYIL4FppP3JuHN5VA=; b=pIQrtt6kWQu34ZbXSKAZ1YftpoyJWg1JWN25YNvQUmG77yidUgJ29jx1o9LlL+nHPRMw2mYRyeEfPFX41b/aWpg2UrHu60G4SzxcHdDOm0uQObgYf8gVZ7RdTfaRU1wkfyjj47eqCldJ+HHqNAxMazqJw5A83HaT5HoEip4scbkVu+DDL8qe28howzM0v7BE3nLZ9HMgn2T8HpyJCHap+pYiG2gC53aopHNnxL9hxCt3UvNFomVgJ29sGDmnJg8paE/WCq6AeC97aPgKyiNHNPi5HlmSu/muti55g8Py5Qm+74f+skXBTSJsIMW0mE7rIGbGitKW/YDF58l9m49zhw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=virtuozzo.com; From: Alexander Ivanov To: qemu-devel@nongnu.org Cc: den@virtuozzo.com, michael.roth@amd.com, kkostiuk@redhat.com, marcandre.lureau@gmail.com Subject: [PATCH 2/6] qga: Move command execution code to a separate function Date: Wed, 25 Oct 2023 16:00:54 +0200 Message-Id: <20231025140058.113376-3-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231025140058.113376-1-alexander.ivanov@virtuozzo.com> References: <20231025140058.113376-1-alexander.ivanov@virtuozzo.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: VI1PR06CA0167.eurprd06.prod.outlook.com (2603:10a6:803:c8::24) To VI0PR08MB10743.eurprd08.prod.outlook.com (2603:10a6:800:205::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI0PR08MB10743:EE_|AS8PR08MB10363:EE_ X-MS-Office365-Filtering-Correlation-Id: c419e42b-4f95-4308-4b25-08dbd562d6f5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 17KxugMFTzMVca1b05Th4q7M5gPtBPZwW35mknUO7ZbhUQxJUdPdt5iXNPTjZXPysqqy4gNB3q7U6apNive1GYD8WiYJqplKXQ2boe8Pq1sQnEM6hMViyMENw4VJqx6kCOgCmt7gbbpwPcAlFhEqdVTZjVCVr4krbuYbguPErld3Hsg0QLeyxiNsa8vYX0Ixtz/qYZa4YBzFjtFmZoZXFVNMNgiHvlEO84QZScAh2xIKgOWE3eiA6PYzvRtTyRC/BM5PbkDcIP/oVzVp0653mvvUFMJ/GypJ3QA9jOGvboSv6zWAoXVlXx5MY4Cc819k2w9UQW1AJ4u2/480b3fWoseYSKNOoJ7ta0D3qiRMKhKqTTLWPk6tLItQO2RghUlk3eaGRbxt7WerkBooRxlNg0MrLtU2gkJ3edx5AiCPs2BKrgnTfxDijjTzWakXvIR+AQy1Q+deCv/2JYjrLBxqf59GTgsjPK8HiL+DVK5PdwALETBRsBU/QMI+Y6/GsfgOPwwrJbs80BXyVkKYH55u7drwTk8FWuYGBHVCWM+uHhYqUFAxA/OUq7kU+BjRCTJyXK9eP6wiHVfA1Oqu0FvVAulRyr74geSdWcp5qitwHLcBvZWS3GwUZTtbZg5VqAD2 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI0PR08MB10743.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(396003)(136003)(346002)(39850400004)(366004)(376002)(230922051799003)(64100799003)(1800799009)(451199024)(186009)(2906002)(38100700002)(66946007)(2616005)(66476007)(316002)(66556008)(6916009)(6666004)(6506007)(478600001)(1076003)(6486002)(6512007)(52116002)(83380400001)(36756003)(44832011)(5660300002)(86362001)(4326008)(8676002)(41300700001)(8936002)(26005)(38350700005); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?yWq0Xd0THeXnK6nj+jjtjFwMazsRtquS4W+AL/2FnuT4rzv6IUYTniSNerTC?= =?us-ascii?Q?7OPrr6Amnw00yjRw3sRvCf9Sfy08J6ZeVlEJz5WYIXydISUhH4mRQTLkQgyV?= =?us-ascii?Q?CQbV1wUtnVlEIV0eX5vhQnoPo4zTc9hB85V6icAXqMWBdW4Lu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haqdkPj/OmaCgC0jg6FPkN2V4oKBpA0F/C7H5ZNRyfPt0WjL/BnYjFXdEy?= =?us-ascii?Q?X10p2V5X14in9Mv/VroaImy70uPXrxkRu6o8bSkvkphCROxjDZ/4ktXqZiB/?= =?us-ascii?Q?QzWk3j2rMrZ5S4DXKB4TOmXxmli1xlfXQXZ0VlxobVFc6swgcBf3HFmDZNUw?= =?us-ascii?Q?AHE2Yb7GUNYlxJZ4SUjcGlWOpNTy2DkOUlDpsy+400ezOLppoelaOEqwH+6w?= =?us-ascii?Q?YBwPCyqyVGO5/AaFhIwV+tBazURWsORKv4HtRs4oACi0zocTQOqkd+GQzvYM?= =?us-ascii?Q?RnphLLlPZpsLd5TTAaJrHEI7W0vpALeAmccc95/MnhgvI8oUiC0ROKsPOaPe?= =?us-ascii?Q?6ui7Z6McKXC1FihOX811MSKAlTOCV0LavR9mWZLYXU2s815tRSTftm9L3GQ4?= =?us-ascii?Q?U/abFyljYZuKU5yCUaCLqWBIRrlr0dDIf1TLyAUB1L7OrfObAlUW+/oneZgF?= =?us-ascii?Q?vN7QWYqzuGVl5WjaNP74cQG5QnHzqu5sbMPtLlV+WQbFdOIfVUbyWVEVg+18?= =?us-ascii?Q?B384+1NikxJRWfOiWsSbaOQ+N8OMFK/lF6vX/myumb15Kigiie0PVl9ALm52?= =?us-ascii?Q?M6UToKYLKX5eK+vjvW/AM/4=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: c419e42b-4f95-4308-4b25-08dbd562d6f5 X-MS-Exchange-CrossTenant-AuthSource: VI0PR08MB10743.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2023 14:01:09.4390 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hh/b/Zo54BQneKhM3qyGLbGASb2x4lZaXDF9RlyzdPTphfo/rku7PmC+NCcFuPX7KUCccmoJhKZigZob0yJNE6VHmhjl/z3Yz/R6LMWrAw0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB10363 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=2a01:111:f400:fe0e::70f; envelope-from=alexander.ivanov@virtuozzo.com; helo=EUR04-VI1-obe.outbound.protection.outlook.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 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, FORGED_SPF_HELO=1, SPF_HELO_PASS=-0.001, T_SPF_TEMPERROR=0.01 autolearn=no 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: 1698242524209100003 Content-Type: text/plain; charset="utf-8" In qmp_guest_set_user_password() we have a part of code that we can reuse in the future commits. Move this code to a separate function. Signed-off-by: Alexander Ivanov --- qga/commands-posix.c | 139 ++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 67 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 6169bbf7a0..e7b82aaf37 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -2114,20 +2114,78 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorLi= st *vcpus, Error **errp) #endif /* __linux__ */ =20 #if defined(__linux__) || defined(__FreeBSD__) -void qmp_guest_set_user_password(const char *username, - const char *password, - bool crypted, - Error **errp) + +static void run_command(const char *argv[], const char *in_str, Error **er= rp) { Error *local_err =3D NULL; - char *passwd_path =3D NULL; pid_t pid; - int status; + int in_len, status; int datafd[2] =3D { -1, -1 }; + + 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]); + setsid(); + dup2(datafd[0], 0); + reopen_fd_to_null(1); + reopen_fd_to_null(2); + + execve(argv[0], (char *const *)argv, environ); + _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; + + in_len =3D strlen(in_str); + + if (qemu_write_full(datafd[1], in_str, in_len) !=3D in_len) { + 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); + 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: %s", argv[0]); + } + +out: + if (datafd[0] !=3D -1) { + close(datafd[0]); + } + if (datafd[1] !=3D -1) { + close(datafd[1]); + } +} + +void qmp_guest_set_user_password(const char *username, + const char *password, + bool crypted, + Error **errp) +{ + char *passwd_path =3D NULL; char *rawpasswddata =3D NULL; - size_t rawpasswdlen; char *chpasswddata =3D NULL; - size_t chpasswdlen; + size_t rawpasswdlen; =20 rawpasswddata =3D (char *)qbase64_decode(password, -1, &rawpasswdlen, = errp); if (!rawpasswddata) { @@ -2155,79 +2213,26 @@ void qmp_guest_set_user_password(const char *userna= me, passwd_path =3D g_find_program_in_path("chpasswd"); #endif =20 - chpasswdlen =3D strlen(chpasswddata); - if (!passwd_path) { error_setg(errp, "cannot find 'passwd' program in PATH"); goto out; } =20 - 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); - + const char *argv[] =3D { #ifdef __FreeBSD__ - const char *h_arg; - h_arg =3D (crypted) ? "-H" : "-h"; - execl(passwd_path, "pw", "usermod", "-n", username, h_arg, "0", NU= LL); + passwd_path, "pw", "usermod", "-n", username, + (crypted) ? "-H" : "-h", "0", NULL}; #else - if (crypted) { - execl(passwd_path, "chpasswd", "-e", NULL); - } else { - execl(passwd_path, "chpasswd", NULL); - } + passwd_path, "chpasswd", (crypted) ? "-e" : NULL, 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; + }; =20 - 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); - 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; - } + run_command(argv, chpasswddata, errp); =20 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]); - } } #else /* __linux__ || __FreeBSD__ */ void qmp_guest_set_user_password(const char *username, --=20 2.34.1