From nobody Sat May 18 06:50:48 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=selinc.com); dmarc=pass(p=reject dis=none) header.from=selinc.com ARC-Seal: i=2; a=rsa-sha256; t=1713810325; cv=pass; d=zohomail.com; s=zohoarc; b=OvrU+JV83H2jyb+2y+Bdr1HYacnnBM1iJluRs7vXrGHAsKZMGayqvRoHIaz8U7e2/b4P539rAXwRaf5V8LAOD9ttIYvvNaZHjwpXN1jf5ZejiVS92wnxZZKSBkFebDDX4nIFMRIqj9Hd6kfROT9R+/kVGpx1HhCqneTr2iClP9Y= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1713810325; 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=me62+b6suYCCxRPp3P3QXxAGdpJR1MECJQmSW37H604=; b=mPhjRGxuEY6/FNMLz5eQprECL+qXJWH0BXNtvqAYY/mWnRbfg6WRYWjHcMsOefdLYJrxwRB+N0eXHNBQsfnpFH6u4YfsfXss6gw8thN3HZyMt1mjucQN849z5TEQPKb4EjNqcpK5/dfJU7UxsJm+qU+Vm3eNnG97LnTKBsXfL4k= 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=selinc.com); dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1713810325564180.10618227998327; Mon, 22 Apr 2024 11:25:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ryyLY-0007Cn-1e; Mon, 22 Apr 2024 14:24:56 -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 1ryyLW-0007CN-E8 for qemu-devel@nongnu.org; Mon, 22 Apr 2024 14:24:54 -0400 Received: from mx0a-000e8d01.pphosted.com ([148.163.147.191]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ryyLU-0001C6-EI for qemu-devel@nongnu.org; Mon, 22 Apr 2024 14:24:54 -0400 Received: from pps.filterd (m0136169.ppops.net [127.0.0.1]) by mx0b-000e8d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 43MD0mbj006751; Mon, 22 Apr 2024 11:24:51 -0700 Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2169.outbound.protection.outlook.com [104.47.56.169]) by mx0b-000e8d01.pphosted.com (PPS) with ESMTPS id 3xmcjcs50t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 Apr 2024 11:24:50 -0700 (PDT) Received: from CH3P220CA0005.NAMP220.PROD.OUTLOOK.COM (2603:10b6:610:1e8::7) by LV8PR22MB5824.namprd22.prod.outlook.com (2603:10b6:408:258::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Mon, 22 Apr 2024 18:24:47 +0000 Received: from DS3PEPF000099DE.namprd04.prod.outlook.com (2603:10b6:610:1e8:cafe::b9) by CH3P220CA0005.outlook.office365.com (2603:10b6:610:1e8::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.29 via Frontend Transport; Mon, 22 Apr 2024 18:24:47 +0000 Received: from email.selinc.com (74.117.212.83) by DS3PEPF000099DE.mail.protection.outlook.com (10.167.17.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Mon, 22 Apr 2024 18:24:46 +0000 Received: from AIDALEUCPC3.ad.selinc.com (10.100.90.200) by wpul-exchange1.ad.selinc.com (10.53.14.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Mon, 22 Apr 2024 11:24:43 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=selinc.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=sel1; bh=me62+b6suYCCxRPp3P3QXxAGdpJR1MECJQmSW37H604=; b=YiVMCUbB0BnV YEYnU6Um7geEwhkyxxJnIMyamomDF8ZBvA6aEAavu7pZ9xt0XTS3TCbqXj8uHwe6 76CAmOdAXLCABarBvdbNVv4nPGLoiFVGZav5/YcYQ/SGnwbcaMagPO5EIA2XmG9s T150h8eO646EFbJ73M3pUs2rKlWHlJfrg5s4gZzfoYrvwXsvBgRMXQQerZsFMTeU Tn/ArUnbNZ4LVGIuIOwrkT9YUWpQ6pyJLkw96QMmeTzPHEWKj2QSEUee6BOM3xM7 LquvcHZBzPNR8HT2Kry3wHeKjeW8Wri1cgqOqQeA5KgrA+ReLG+fGiS7+4KcY6iD tLMvDfxzew== ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H+K8w+oS/GtJ529h5CEErh+gSXQQdEqaF9tbI6pwJ9f0AVUw2ZRfbbsg0O0pzS/7lx5w2mGkPlMt6lXGYjJNbuZHLAv7ZyYTOsW8YeS9J1u/mSw6EvGmtj89S38wdHCSDychfep7GgIjFHbPLY7w6Jjn3BP3ka4MObbOMc0jBYWpMv5mmdKT0LtrFrr8w3xLycTZ26DdyrFcDS0Cxg9bff8GpuQHXsDpEp7x1qb7MlkMeFU/lQwzcwOMuKKZoHazEnyvE6EQq41ryNxUeW7Eb5u2meYB5Tg28himkgwJ6eJYlu+ylQJP2qUyDEWRfmQ3u0xWg8RuIFwwOiepg69oYQ== 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=me62+b6suYCCxRPp3P3QXxAGdpJR1MECJQmSW37H604=; b=MFNrhWU+udizhf8GeSxcFnGdiSqK36irgbhTDdwmGVTvp0LG7lARbmYL5/bf5k9MTg6STkaIctYLabiQPoLKGT7A7TG3eK766/psAwiHCiPmfFGjyVeuL/g/s1x4oQGchte5WtdFik3us3Rns28W22jATtuV5tQ9/joP8vQ0vciVgtHqD6fQCquNIlnfenadQrFKw2F02uqhM4DPG4KS47pC5cqCBEWhswUNZqzmQA2DZkB3LERIpj52imx/JQzBRbe9LSDDuyRD7/Y5+SZTQYlp2lepww/2RTnN5POo3V57PCTt9AvxIml6qgQlQhUtzJek8tNjB9OPq3ODSsezew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 74.117.212.83) smtp.rcpttodomain=nongnu.org smtp.mailfrom=selinc.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=selinc.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=selinc.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=me62+b6suYCCxRPp3P3QXxAGdpJR1MECJQmSW37H604=; b=xRp9ZDXWz0w0MKa7e+OGCN2yTGuw6yD45wOAbYkQD/95ZlpzBgeHQWVPjzPDjlF13szSXBV0DB7enSzT7ZwYwvxxnHLjt7gjkjGOJqIVDa8aTdnQXcQveouHxdvTr6vQqn7WB/gcsNuQnijT+dH6BIrOg0o+ekQ9eOTq8dFm+Xd6OfkxgQ2qGkukdz/mrTC8crSJZEs+sosYUwZQhwWFrtn9/mNPodSnFdmR46+55+2Fr+peroF/X9CQS2JQulxcW7paVqRd1qxExiW2lmn93li/nTAhVThALHYSf1hyN+AZ1qGCaeI6WSQsC3DFOFhmxpaY6//1WXqKGMfI8LU4dA== X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 74.117.212.83) smtp.mailfrom=selinc.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=selinc.com; 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 (protection.outlook.com: domain of selinc.com designates 74.117.212.83 as permitted sender) receiver=protection.outlook.com; client-ip=74.117.212.83; helo=email.selinc.com; pr=C From: To: CC: , aidaleuc Subject: [PATCH v7 1/2] Refactor common functions between POSIX and Windows implementation Date: Mon, 22 Apr 2024 12:24:12 -0600 Message-ID: <20240422182413.230378-2-aidan_leuck@selinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240422182413.230378-1-aidan_leuck@selinc.com> References: <20240422182413.230378-1-aidan_leuck@selinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.100.90.200] X-ClientProxiedBy: wpul-exchange1.ad.selinc.com (10.53.14.22) To wpul-exchange1.ad.selinc.com (10.53.14.22) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099DE:EE_|LV8PR22MB5824:EE_ X-MS-Office365-Filtering-Correlation-Id: 30719748-2f96-4393-536b-08dc62f97d72 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?xsMSgThxFGwZH0lyAW2d6zG9TpETD6OeVo5meXFDkRvgF/GfGEiXkwuhluNY?= =?us-ascii?Q?3VbcB9TG0g7hqN8drrbcIQzePrxedY45A04WhpuG21IFVeZHHJazEjvDSPUZ?= =?us-ascii?Q?wVIn9+Icbov82yukQJXVFLVNbrSyLomyMdVPfzGuKsDg0cDB0kldUoB0mmyO?= =?us-ascii?Q?oe92caiSrEWvCTq94hdaKC0csMN1yIDTjDuWoCGurRRWS9KGpwhGLyCf7kwA?= =?us-ascii?Q?QMRvTrgagawUY35rH1kvQC01Fc8HFNSF0r482Tee+B2t/dDfwL7r6QMnB4+X?= =?us-ascii?Q?BDa8mPhibimDA45F+WSQ9v4RTeis9+vm3atDL3ugZaoFAqxTJ8xMz9hR97yv?= =?us-ascii?Q?ZyL4ErEkmMfVj9jaeGwpIlhO6vgdQwqWvf7zXl0TEuw1WcAI1MGOAHiK8qTD?= =?us-ascii?Q?aKi/Cu99bn5G7c8Y4RrwonkMhCSMexd3mKb4S1h4POrKwiW6onbBgUaBFqop?= =?us-ascii?Q?72ZqXIHy08SzzNSTktbnu+mzTc+d6YJLs0zu4r72g5SfkF1UKHyY0w4k9aYp?= =?us-ascii?Q?WOOBevGesD/ZrcTGlXcNZBkCr0GSUlHyWgDQZLwsAwjUxSZTgiqHFpcAlosP?= =?us-ascii?Q?vH9wmw/+C/rsYQZryVBgAKF+0ruLXwY07hMwHdjxfSoQ2qF8NgP+/3b14EGQ?= =?us-ascii?Q?s7xkAatvkTIPdLy3LuQ4vLfBDF17k3o9rkyX/FodnwFpkf8defpK0b5rJwDe?= =?us-ascii?Q?kk3U+vJH51qYvAoG4/W9atqENYzUgSrXehXUvESai0Ulr9hvyLNOp66z7Yjh?= =?us-ascii?Q?yGR4GwpngT7cCBdvg6mwacoZMsv6aNn9TcpA9oJIZHJJNem4jbVYCUClJUv4?= =?us-ascii?Q?SivQpu84E99LIwMq7p/uAYe+uuLtniuIhz+ycy8t2U47MxxQWYarpU0+958T?= =?us-ascii?Q?r6kJeT8GT33icNhb4rfzePYTwSwcY908g2RVSVCrSxo5riIZNDKVNPMIqd6m?= =?us-ascii?Q?tld7qoj+WZctonoH2OUshROoM0eG5goNkb0mIjmSxDP4X7tgUTcHOEpY0tn+?= =?us-ascii?Q?SyAko4ZmXgKTMziyJrR3gBiDCUAwqyFLYLPtMx3mlGSVTIydtqCRUf0kvaXz?= =?us-ascii?Q?ha0ikkjNlJFfyMfVVR5wAklgWPmyvhK6XzWKcz/7HeNUrR4gOR/9eybseJfl?= =?us-ascii?Q?WxvAfocFZ2J62p7Jtj+1e+smjBAwg9mS5aqPHa5Uh77bU1jb9GELESi9bNrH?= =?us-ascii?Q?y0ewtmfZ4zwUtKjoKWePt7c1S0dsVtbBQMAJl4xPuV2V1vXYE7xb6xUan/e+?= =?us-ascii?Q?GCHxvQLL/DOgJWRZzHMLUZnoUrv2QI3ho/QVL98jSRfwe1Penqyziemi9l1M?= =?us-ascii?Q?IIgToY0vQkz6ExdwViGeufoPlB2BOyIixUpoViw8kXTVzMgK6o6GPZLXB9sV?= =?us-ascii?Q?JDk5rRo=3D?= X-Forefront-Antispam-Report: CIP:74.117.212.83; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:email.selinc.com; PTR:wpul-exchange1.selinc.com; CAT:NONE; SFS:(13230031)(376005)(36860700004)(82310400014)(1800799015); DIR:OUT; SFP:1102; X-OriginatorOrg: selinc.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2024 18:24:46.9072 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 30719748-2f96-4393-536b-08dc62f97d72 X-MS-Exchange-CrossTenant-Id: 12381f30-10fe-4e2c-aa3a-5e03ebeb59ec X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=12381f30-10fe-4e2c-aa3a-5e03ebeb59ec; Ip=[74.117.212.83]; Helo=[email.selinc.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR22MB5824 X-Proofpoint-ORIG-GUID: ayhFWbsKmpkP_NBoZiyVuiGiJnAKPrQC X-Proofpoint-GUID: ayhFWbsKmpkP_NBoZiyVuiGiJnAKPrQC X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 bulkscore=0 impostorscore=0 suspectscore=0 spamscore=0 adultscore=0 phishscore=0 mlxlogscore=999 priorityscore=1501 lowpriorityscore=0 mlxscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2404220078 Received-SPF: pass client-ip=148.163.147.191; envelope-from=prvs=8842f65a37=aidan_leuck@selinc.com; helo=mx0a-000e8d01.pphosted.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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 @selinc.com) X-ZM-MESSAGEID: 1713810326507100003 Content-Type: text/plain; charset="utf-8" From: aidaleuc Signed-off-by: aidaleuc --- qga/commands-common-ssh.c | 50 +++++++++++++++++++++++++++++++++++++++ qga/commands-common-ssh.h | 10 ++++++++ qga/commands-posix-ssh.c | 47 +----------------------------------- qga/meson.build | 1 + 4 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 qga/commands-common-ssh.c create mode 100644 qga/commands-common-ssh.h diff --git a/qga/commands-common-ssh.c b/qga/commands-common-ssh.c new file mode 100644 index 0000000000..537869fb98 --- /dev/null +++ b/qga/commands-common-ssh.c @@ -0,0 +1,50 @@ +/* + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "commands-common-ssh.h" + +GStrv read_authkeys(const char *path, Error **errp) +{ + g_autoptr(GError) err =3D NULL; + g_autofree char *contents =3D NULL; + + if (!g_file_get_contents(path, &contents, NULL, &err)) { + error_setg(errp, "failed to read '%s': %s", path, err->message); + return NULL; + } + + return g_strsplit(contents, "\n", -1); +} + +bool check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp) +{ + size_t n =3D 0; + strList *k; + + for (k =3D keys; k !=3D NULL; k =3D k->next) { + if (!check_openssh_pub_key(k->value, errp)) { + return false; + } + n++; + } + + if (nkeys) { + *nkeys =3D n; + } + return true; +} + +bool check_openssh_pub_key(const char *key, Error **errp) +{ + /* simple sanity-check, we may want more? */ + if (!key || key[0] =3D=3D '#' || strchr(key, '\n')) { + error_setg(errp, "invalid OpenSSH public key: '%s'", key); + return false; + } + + return true; +} diff --git a/qga/commands-common-ssh.h b/qga/commands-common-ssh.h new file mode 100644 index 0000000000..14d955fa84 --- /dev/null +++ b/qga/commands-common-ssh.h @@ -0,0 +1,10 @@ +/* + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qapi/qapi-builtin-types.h" + +GStrv read_authkeys(const char *path, Error **errp); +bool check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp); +bool check_openssh_pub_key(const char *key, Error **errp); diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c index 236f80de44..dd2ecb453a 100644 --- a/qga/commands-posix-ssh.c +++ b/qga/commands-posix-ssh.c @@ -9,6 +9,7 @@ #include #include =20 +#include "commands-common-ssh.h" #include "qapi/error.h" #include "qga-qapi-commands.h" =20 @@ -80,37 +81,6 @@ mkdir_for_user(const char *path, const struct passwd *p, return true; } =20 -static bool -check_openssh_pub_key(const char *key, Error **errp) -{ - /* simple sanity-check, we may want more? */ - if (!key || key[0] =3D=3D '#' || strchr(key, '\n')) { - error_setg(errp, "invalid OpenSSH public key: '%s'", key); - return false; - } - - return true; -} - -static bool -check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp) -{ - size_t n =3D 0; - strList *k; - - for (k =3D keys; k !=3D NULL; k =3D k->next) { - if (!check_openssh_pub_key(k->value, errp)) { - return false; - } - n++; - } - - if (nkeys) { - *nkeys =3D n; - } - return true; -} - static bool write_authkeys(const char *path, const GStrv keys, const struct passwd *p, Error **errp) @@ -139,21 +109,6 @@ write_authkeys(const char *path, const GStrv keys, return true; } =20 -static GStrv -read_authkeys(const char *path, Error **errp) -{ - g_autoptr(GError) err =3D NULL; - g_autofree char *contents =3D NULL; - - if (!g_file_get_contents(path, &contents, NULL, &err)) { - error_setg(errp, "failed to read '%s': %s", path, err->message); - return NULL; - } - - return g_strsplit(contents, "\n", -1); - -} - void qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, bool has_reset, bool reset, diff --git a/qga/meson.build b/qga/meson.build index 1c3d2a3d1b..4c3899751b 100644 --- a/qga/meson.build +++ b/qga/meson.build @@ -66,6 +66,7 @@ qga_ss.add(files( 'guest-agent-command-state.c', 'main.c', 'cutils.c', + 'commands-common-ssh.c' )) if host_os =3D=3D 'windows' qga_ss.add(files( --=20 2.44.0 From nobody Sat May 18 06:50:48 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=selinc.com); dmarc=pass(p=reject dis=none) header.from=selinc.com ARC-Seal: i=2; a=rsa-sha256; t=1713810338; cv=pass; d=zohomail.com; s=zohoarc; b=YLwm0un1stBDTeZRcxOYoDjrjIcf38A3kh2G0MYXS4Yt8dBOOiTzYvQ4aUmKysKD+SLT5QhfCkZLVOjWGZvniJV7DRkDZqi1w1u1FYLM+VfZpGnTo4P9v2MAb12nCshqgOWjvD/rwoVOui45Jt1DgAx730/Oys4t94EhPS6+gWY= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1713810338; 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=sKByA+rWceYEJ6tEXmEj7tCQa6h8Me7Lypd8zzCLR8Q=; b=jbVM0wQ7WwL9JrpfGb/LjcqVudQbAlLeUgzVOyBT6UO16nj4jDgL7kBYZo1VMM+8z6QyKsOZL8LLfviUsV9nGaSHr02raP7OH+3gL31J+edEFCv2KJfNNnz4BQkH4dLHETbjvgKZqsUFa56/SOph3I71ZhZZyDiJwyTFLuw9GkA= 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=selinc.com); dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1713810338502365.6429338529348; Mon, 22 Apr 2024 11:25:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ryyLd-0007DD-8T; Mon, 22 Apr 2024 14:25:01 -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 1ryyLb-0007D4-O8 for qemu-devel@nongnu.org; Mon, 22 Apr 2024 14:24:59 -0400 Received: from mx0a-000e8d01.pphosted.com ([148.163.147.191]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ryyLX-0001F7-W8 for qemu-devel@nongnu.org; Mon, 22 Apr 2024 14:24:59 -0400 Received: from pps.filterd (m0136169.ppops.net [127.0.0.1]) by mx0b-000e8d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 43MD0lPV006742; Mon, 22 Apr 2024 11:24:54 -0700 Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2169.outbound.protection.outlook.com [104.47.56.169]) by mx0b-000e8d01.pphosted.com (PPS) with ESMTPS id 3xmcjcs50v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 Apr 2024 11:24:54 -0700 (PDT) Received: from CH3P220CA0023.NAMP220.PROD.OUTLOOK.COM (2603:10b6:610:1e8::29) by SJ2PR22MB4328.namprd22.prod.outlook.com (2603:10b6:a03:547::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Mon, 22 Apr 2024 18:24:49 +0000 Received: from DS3PEPF000099DE.namprd04.prod.outlook.com (2603:10b6:610:1e8:cafe::2a) by CH3P220CA0023.outlook.office365.com (2603:10b6:610:1e8::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7495.26 via Frontend Transport; Mon, 22 Apr 2024 18:24:49 +0000 Received: from email.selinc.com (74.117.212.83) by DS3PEPF000099DE.mail.protection.outlook.com (10.167.17.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Mon, 22 Apr 2024 18:24:49 +0000 Received: from AIDALEUCPC3.ad.selinc.com (10.100.90.200) by wpul-exchange1.ad.selinc.com (10.53.14.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Mon, 22 Apr 2024 11:24:45 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=selinc.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=sel1; bh=sKByA+rWceYEJ6tEXmEj7tCQa6h8Me7Lypd8zzCLR8Q=; b=vFVrZ6dvdGKZ Xi9/JLtje5weIUWqkfcelCxONTgw70mMGN38TE1DXXkZKdj9Uv172YSc+tBtC3XP q09UVW6MNiIAoxHUOsdgeKAXdHo1KZBuzytLXMdmPluz3nrwBaYfUAQYeYSPRP54 Vs8n/2gGeZOFY8RUdoEZw+UQBYOTEO1PqDGk8zdsvY7QhcWPxo40C7f6KPWnuXKT vq3NITKLAfkiUAoqy8CE4hBmRWE6Cb8m7eFV/FOjC72+ofdi9LGZeCDuFAtq54ad yYzxM3Ztngg4+t7FLUy2r9aGva7hNYn6j6xhaJYMO0vtdkjqaGSci+yBr4Ll521u l8kVNoFk/A== ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UlOOGCv9pb5CpaVF2J1dYdU+178tFKQVAsfdAtbQz/oncFBN6xd7/wEJGolCv3sDgTJGQ2/IPYYD7Vuzy4oonizT+UIULxHqwH5lk2HWFgdSlclE9g91MAAZXKRCLR8nHonnOjziWSnmUg4ORkglILqp0C+O63p8gKNdOTRHODxVum5zvAGadZqvGuw/Sc0JyRX3IcUD50ymTbPP/pUFC7yjR8bldCLwmyWZr93yDq4sKvzlLTtJJjDo7IbcueKsM7zD9f5F7qC2QRP8YTBIZtHxRMFP9+ndio45T9PAD5ufZKKBXJrmOma8sc0WUy6oa1L2H6o3twO0I5pwNxRZ5A== 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=sKByA+rWceYEJ6tEXmEj7tCQa6h8Me7Lypd8zzCLR8Q=; b=Oywq9YMLvj0kFcVaGHJhzBUX9w8BhLgXYHx2Wo0TQMORoTHP+ItutGmxCnlMUP5Z8ZxR4n7Nc8HwwUMGvWLeGtEqiXjY/mIchmv1Twxs8LvFSfMKBB9esuA2bjUNBpq+GBi5BiI5vYclKyFrm1EG/qLGXXc07IWfLJRNtXAj+ixqsYpxc5cY7TADo7m4wtP5rZ4HLGBjtITFHF23YlGkLM0gZO7qtjlkPVcg/c3zKQHGePFrzo3Asrb7/r0KJK3pm0pKdLsWZ6wxvHdHHuFoBkoteSkRv070uZcC3FEsHRLeifTUQESmCyBo24y+3Ac4J7L3e3CVCJHjX4j5g7+MDQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 74.117.212.83) smtp.rcpttodomain=nongnu.org smtp.mailfrom=selinc.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=selinc.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=selinc.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=sKByA+rWceYEJ6tEXmEj7tCQa6h8Me7Lypd8zzCLR8Q=; b=cux3Dkwx1URjUFTEDj6FGcsAQ9tZVuev3b6ctCdN8cxUPJCDdtFbZyQq7Qxr/CsicfzN1NnCLVvpu1SsIqClBXj2jxBzwfWi6ylUWImjRUyNP/yPr2rWGC19G4wFcXwXFUrEZjEtVADA7DsgVNZq/URrauu05WD8TC/zSRDF+gz/9drwjo9z/H5jILdjU8Kh5u+0PIki5R+8M18QBq4h9I7h2jvXaClVEhgazk7Ix4SK2Qk6oY7q1q/Gnyghr+83a7jd9NlPT9q6PU3nl7vQEJDmXx+R5b0K9YwTCZlI3m2GjvWEBNY43HokXUeDJMqwHYmKM4B65lRS59xCHnGGJA== X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 74.117.212.83) smtp.mailfrom=selinc.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=selinc.com; 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 (protection.outlook.com: domain of selinc.com designates 74.117.212.83 as permitted sender) receiver=protection.outlook.com; client-ip=74.117.212.83; helo=email.selinc.com; pr=C From: To: CC: , aidaleuc Subject: [PATCH v7 2/2] Implement SSH commands in QEMU GA for Windows Date: Mon, 22 Apr 2024 12:24:13 -0600 Message-ID: <20240422182413.230378-3-aidan_leuck@selinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240422182413.230378-1-aidan_leuck@selinc.com> References: <20240422182413.230378-1-aidan_leuck@selinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.100.90.200] X-ClientProxiedBy: wpul-exchange1.ad.selinc.com (10.53.14.22) To wpul-exchange1.ad.selinc.com (10.53.14.22) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099DE:EE_|SJ2PR22MB4328:EE_ X-MS-Office365-Filtering-Correlation-Id: 659b373d-fde1-4738-6c5b-08dc62f97eaf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?Ds9qiEQ8bGSM0UV65cpfJALx5Fyf7irk9HNuknPD7B4t6RMmjjZfSWPFF8iW?= =?us-ascii?Q?PVVj0+fflDKrLyO9hI7qzS4E7A+eISjxQuH/1yWgx4ry5k4VgeXisgTG+D3m?= =?us-ascii?Q?GPHiSo2NjhkwxAWSvxwsrrT3htQrEgd67aw5TF6XoSSIjHNPvlp/8sdGA1jP?= =?us-ascii?Q?Tx5gnH7p1Ig+6JRmf0lellogC2j/h4TNRPz+mW2hg9BZrGmWARx/WcZKPagq?= =?us-ascii?Q?R8xuJXxRpZemJfm2Axm+5Bg2Mc3HjEyFeZj8qbiXQNUO9Qjo/01aZVnCy0T7?= =?us-ascii?Q?Gm2/oPMfC7yNH+XoKnk3xPsIZSRzSnI2DY+AXb88zd24rAjlObcGRwKwKoqW?= =?us-ascii?Q?v/gXriU7zVn/lKmlOBt3upeecz8sqC/seo+9bm4ao5qhIENYS7GeDpAHhNP1?= =?us-ascii?Q?D1/LrqtJGrpg+Ofq27sLF0TSaWkMHVDgQhVmoyEFbBQJ0OmCjORvXeqfwu7O?= =?us-ascii?Q?HXvjmUStkHGYSK7NCteOIh9syD9ye+NCi6hjMk9BnD1Ouq4Vd5mhK1/rb9xc?= =?us-ascii?Q?qqwZ4wRL8F6qlaZMj5NTY5lxSI7Yxrnzy+wsfhNkjmxoEQPuuNXkfIfUMZtw?= =?us-ascii?Q?258uvZUESqHOVyPfViH0hYEtKfRz6kJPV93Y3IBF45fcsSsi7jyIbrzaW6ss?= =?us-ascii?Q?6FV93EH5jvw5p/TMcf5Sbe1lzFR9Uu0fg6YtpmFLz6WlPQlss2Stz1+dvCtg?= =?us-ascii?Q?XonkkHG375D7UsJuRbc8h3RmhRt3QQJs0dPzM/kdz3hAt2xJ7yCFo2Tc8rJM?= =?us-ascii?Q?nBGjPYM4H+X30Skg+5ZzF80tcUBvIJof6pmwW9nWYE/+M/YgqQlIxMY5DKEs?= =?us-ascii?Q?6NHbfJ1koaI42yAm01h48ioqGdc1lpNtxBI6zNgwKYLIUrG03KsUiwc4XGvX?= =?us-ascii?Q?fX9+xBLFUJFD2BJQ7EErlW0H15jwCNFLvZCsPqF6ikbwcIxqAUkq2SbjAy9x?= =?us-ascii?Q?rnN2EFhYKfsKYDWw+McZS3FGMYK6D8SDgSL7egu8ZWGIPjHj29zivdp7FicC?= =?us-ascii?Q?9uVGrpUSDU9/s7Q8JUJfC1pTwQioznYq6JR8FLaJ7dA6i9iCpbp4wgmh1ih9?= =?us-ascii?Q?KgB8mS3JVtrOrRNRayf4Tm2FN2LAUzfrcGklrNrU2an8stmXEWV+9bd/5R9e?= =?us-ascii?Q?7N4+ZI/3uXWiy+XfHnvJbr6NrFZLG3nM9X/b/SqX3sWkSRm47ziHol4l9p+O?= =?us-ascii?Q?ddvB+GMrNsQgSTUXfpcMDkSaaX/01Xpdkmfu0IvDa3j4IrCOC8WayVSYs19A?= =?us-ascii?Q?GPr3cImoZQ/0xJFxf3hBIfO/wfPq7CIJ6huJOcd/JYaTN2S6va4rLgAfrmyb?= =?us-ascii?Q?eZjjiTPioriCwpQp4/+3yRj+bZ05YI+qvmk1OkRptluFImZ7WQkUmHnBcMIX?= =?us-ascii?Q?ppDG7dg=3D?= X-Forefront-Antispam-Report: CIP:74.117.212.83; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:email.selinc.com; PTR:wpul-exchange1.selinc.com; CAT:NONE; SFS:(13230031)(36860700004)(376005)(1800799015)(82310400014); DIR:OUT; SFP:1102; X-OriginatorOrg: selinc.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2024 18:24:49.0009 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 659b373d-fde1-4738-6c5b-08dc62f97eaf X-MS-Exchange-CrossTenant-Id: 12381f30-10fe-4e2c-aa3a-5e03ebeb59ec X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=12381f30-10fe-4e2c-aa3a-5e03ebeb59ec; Ip=[74.117.212.83]; Helo=[email.selinc.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR22MB4328 X-Proofpoint-ORIG-GUID: le3J_l4k3YxHMluwlD36tR-eGFbXs4Kg X-Proofpoint-GUID: le3J_l4k3YxHMluwlD36tR-eGFbXs4Kg X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 bulkscore=0 impostorscore=0 suspectscore=0 spamscore=0 adultscore=0 phishscore=0 mlxlogscore=999 priorityscore=1501 lowpriorityscore=0 mlxscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2404220078 Received-SPF: pass client-ip=148.163.147.191; envelope-from=prvs=8842f65a37=aidan_leuck@selinc.com; helo=mx0a-000e8d01.pphosted.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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 @selinc.com) X-ZM-MESSAGEID: 1713810340399100003 Content-Type: text/plain; charset="utf-8" From: aidaleuc Signed-off-by: aidaleuc --- qga/commands-windows-ssh.c | 712 +++++++++++++++++++++++++++++++++++++ qga/commands-windows-ssh.h | 26 ++ qga/meson.build | 7 +- qga/qapi-schema.json | 17 +- 4 files changed, 750 insertions(+), 12 deletions(-) create mode 100644 qga/commands-windows-ssh.c create mode 100644 qga/commands-windows-ssh.h diff --git a/qga/commands-windows-ssh.c b/qga/commands-windows-ssh.c new file mode 100644 index 0000000000..6a642e3ba8 --- /dev/null +++ b/qga/commands-windows-ssh.c @@ -0,0 +1,712 @@ +/* + * QEMU Guest Agent win32-specific command implementations for SSH keys. + * The implementation is opinionated and expects the SSH implementation to + * be OpenSSH. + * + * Copyright Schweitzer Engineering Laboratories. 2024 + * + * Authors: + * Aidan Leuck + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include +#include + +#include "commands-common-ssh.h" +#include "commands-windows-ssh.h" +#include "guest-agent-core.h" +#include "limits.h" +#include "lmaccess.h" +#include "lmapibuf.h" +#include "lmerr.h" +#include "qapi/error.h" + +#include "qga-qapi-commands.h" +#include "sddl.h" +#include "shlobj.h" +#include "userenv.h" + +#define AUTHORIZED_KEY_FILE "authorized_keys" +#define AUTHORIZED_KEY_FILE_ADMIN "administrators_authorized_keys" +#define LOCAL_SYSTEM_SID "S-1-5-18" +#define ADMIN_SID "S-1-5-32-544" + +/* + * Frees userInfo structure. This implements the g_auto cleanup + * for the structure. + */ +void free_userInfo(PWindowsUserInfo info) +{ + g_free(info->sshDirectory); + g_free(info->authorizedKeyFile); + LocalFree(info->SSID); + g_free(info->username); + g_free(info); +} + +/* + * Gets the admin SSH folder for OpenSSH. OpenSSH does not store + * the authorized_key file in the users home directory for security reason= s and + * instead stores it at %PROGRAMDATA%/ssh. This function returns the path = to + * that directory on the users machine + * + * parameters: + * errp -> error structure to set when an error occurs + * returns: The path to the ssh folder in %PROGRAMDATA% or NULL if an error + * occurred. + */ +static char *get_admin_ssh_folder(Error **errp) +{ + /* Allocate memory for the program data path */ + g_autofree char *programDataPath =3D NULL; + char *authkeys_path =3D NULL; + PWSTR pgDataW =3D NULL; + g_autoptr(GError) gerr =3D NULL; + + /* Get the KnownFolderPath on the machine. */ + HRESULT folderResult =3D + SHGetKnownFolderPath(&FOLDERID_ProgramData, 0, NULL, &pgDataW); + if (folderResult !=3D S_OK) { + error_setg(errp, "Failed to retrieve ProgramData folder"); + return NULL; + } + + /* Convert from a wide string back to a standard character string. */ + programDataPath =3D g_utf16_to_utf8(pgDataW, -1, NULL, NULL, &gerr); + CoTaskMemFree(pgDataW); + if (!programDataPath) { + error_setg(errp, + "Failed converting ProgramData folder path to UTF-16 %s= ", + gerr->message); + return NULL; + } + + /* Build the path to the file. */ + authkeys_path =3D g_build_filename(programDataPath, "ssh", NULL); + return authkeys_path; +} + +/* + * Gets the path to the SSH folder for the specified user. If the user is = an + * admin it returns the ssh folder located at %PROGRAMDATA%/ssh. If the us= er is + * not an admin it returns %USERPROFILE%/.ssh + * + * parameters: + * username -> Username to get the SSH folder for + * isAdmin -> Whether the user is an admin or not + * errp -> Error structure to set any errors that occur. + * returns: path to the ssh folder as a string. + */ +static char *get_ssh_folder(const char *username, const bool isAdmin, + Error **errp) +{ + DWORD maxSize =3D MAX_PATH; + g_autofree char *profilesDir =3D g_new0(char, maxSize); + + if (isAdmin) { + return get_admin_ssh_folder(errp); + } + + /* If not an Admin the SSH key is in the user directory. */ + /* Get the user profile directory on the machine. */ + BOOL ret =3D GetProfilesDirectory(profilesDir, &maxSize); + if (!ret) { + error_setg_win32(errp, GetLastError(), + "failed to retrieve profiles directory"); + return NULL; + } + + /* Builds the filename */ + return g_build_filename(profilesDir, username, ".ssh", NULL); +} + +/* + * Creates an entry for the user so they can access the ssh folder in their + * userprofile. + * + * parameters: + * userInfo -> Information about the current user + * pACL -> Pointer to an ACL structure + * errp -> Error structure to set any errors that occur + * returns -> 1 on success, 0 otherwise + */ +static bool create_acl_user(PWindowsUserInfo userInfo, PACL *pACL, Error *= *errp) +{ + const int aclSize =3D 1; + PACL newACL =3D NULL; + EXPLICIT_ACCESS eAccess[1]; + PSID userPSID =3D NULL; + + /* Get a pointer to the internal SID object in Windows */ + bool converted =3D ConvertStringSidToSid(userInfo->SSID, &userPSID); + if (!converted) { + error_setg_win32(errp, GetLastError(), "failed to retrieve user %s= SID", + userInfo->username); + goto error; + } + + /* Set the permissions for the user. */ + eAccess[0].grfAccessPermissions =3D GENERIC_ALL; + eAccess[0].grfAccessMode =3D SET_ACCESS; + eAccess[0].grfInheritance =3D NO_INHERITANCE; + eAccess[0].Trustee.TrusteeForm =3D TRUSTEE_IS_SID; + eAccess[0].Trustee.TrusteeType =3D TRUSTEE_IS_USER; + eAccess[0].Trustee.ptstrName =3D (LPTSTR)userPSID; + + /* Set the ACL entries */ + DWORD setResult; + + /* + * If we are given a pointer that is already initialized, then we can = merge + * the existing entries instead of overwriting them. + */ + if (*pACL) { + setResult =3D SetEntriesInAcl(aclSize, eAccess, *pACL, &newACL); + } else { + setResult =3D SetEntriesInAcl(aclSize, eAccess, NULL, &newACL); + } + + if (setResult !=3D ERROR_SUCCESS) { + error_setg_win32(errp, GetLastError(), + "failed to set ACL entries for user %s %lu", + userInfo->username, setResult); + goto error; + } + + /* Free any old memory since we are going to overwrite the users point= er. */ + LocalFree(*pACL); + *pACL =3D newACL; + + LocalFree(userPSID); + return true; +error: + LocalFree(userPSID); + return false; +} + +/* + * Creates a base ACL for both normal users and admins to share + * pACL -> Pointer to an ACL structure + * errp -> Error structure to set any errors that occur + * returns: 1 on success, 0 otherwise + */ +static bool create_acl_base(PACL *pACL, Error **errp) +{ + PSID adminGroupPSID =3D NULL; + PSID systemPSID =3D NULL; + + const int aclSize =3D 2; + EXPLICIT_ACCESS eAccess[2]; + + /* Create an entry for the system user. */ + const char *systemSID =3D LOCAL_SYSTEM_SID; + bool converted =3D ConvertStringSidToSid(systemSID, &systemPSID); + if (!converted) { + error_setg_win32(errp, GetLastError(), "failed to retrieve system = SID"); + goto error; + } + + /* set permissions for system user */ + eAccess[0].grfAccessPermissions =3D GENERIC_ALL; + eAccess[0].grfAccessMode =3D SET_ACCESS; + eAccess[0].grfInheritance =3D NO_INHERITANCE; + eAccess[0].Trustee.TrusteeForm =3D TRUSTEE_IS_SID; + eAccess[0].Trustee.TrusteeType =3D TRUSTEE_IS_USER; + eAccess[0].Trustee.ptstrName =3D (LPTSTR)systemPSID; + + /* Create an entry for the admin user. */ + const char *adminSID =3D ADMIN_SID; + converted =3D ConvertStringSidToSid(adminSID, &adminGroupPSID); + if (!converted) { + error_setg_win32(errp, GetLastError(), "failed to retrieve Admin S= ID"); + goto error; + } + + /* Set permissions for admin group. */ + eAccess[1].grfAccessPermissions =3D GENERIC_ALL; + eAccess[1].grfAccessMode =3D SET_ACCESS; + eAccess[1].grfInheritance =3D NO_INHERITANCE; + eAccess[1].Trustee.TrusteeForm =3D TRUSTEE_IS_SID; + eAccess[1].Trustee.TrusteeType =3D TRUSTEE_IS_GROUP; + eAccess[1].Trustee.ptstrName =3D (LPTSTR)adminGroupPSID; + + /* Put the entries in an ACL object. */ + PACL pNewACL =3D NULL; + DWORD setResult; + + /* + *If we are given a pointer that is already initialized, then we can m= erge + *the existing entries instead of overwriting them. + */ + if (*pACL) { + setResult =3D SetEntriesInAcl(aclSize, eAccess, *pACL, &pNewACL); + } else { + setResult =3D SetEntriesInAcl(aclSize, eAccess, NULL, &pNewACL); + } + + if (setResult !=3D ERROR_SUCCESS) { + error_setg_win32(errp, GetLastError(), + "failed to set base ACL entries for system user a= nd " + "admin group %lu", + setResult); + goto error; + } + + LocalFree(adminGroupPSID); + LocalFree(systemPSID); + + /* Free any old memory since we are going to overwrite the users point= er. */ + LocalFree(*pACL); + + *pACL =3D pNewACL; + + return true; + +error: + LocalFree(adminGroupPSID); + LocalFree(systemPSID); + return false; +} + +/* + * Sets the access control on the authorized_keys file and any ssh folders= that + * need to be created. For administrators the required permissions on the + * file/folders are that only administrators and the LocalSystem account c= an + * access the folders. For normal user accounts only the specified user, + * LocalSystem and Administrators can have access to the key. + * + * parameters: + * userInfo -> pointer to structure that contains information about the us= er + * PACL -> pointer to an access control structure that will be set upon + * successful completion of the function. + * errp -> error structure that will be set upon error. + * returns: 1 upon success 0 upon failure. + */ +static bool create_acl(PWindowsUserInfo userInfo, PACL *pACL, Error **errp) +{ + /* + * Creates a base ACL that both admins and users will share + * This adds the Administrators group and the SYSTEM group + */ + if (!create_acl_base(pACL, errp)) { + return false; + } + + /* + * If the user is not an admin give the user creating the key permissi= on to + * access the file. + */ + if (!userInfo->isAdmin) { + if (!create_acl_user(userInfo, pACL, errp)) { + return false; + } + + return true; + } + + return true; +} +/* + * Create the SSH directory for the user and d sets appropriate permission= s. + * In general the directory will be %PROGRAMDATA%/ssh if the user is an ad= min. + * %USERPOFILE%/.ssh if not an admin + * + * parameters: + * userInfo -> Contains information about the user + * errp -> Structure that will contain errors if the function fails. + * returns: zero upon failure, 1 upon success + */ +static bool create_ssh_directory(WindowsUserInfo *userInfo, Error **errp) +{ + PACL pNewACL =3D NULL; + g_autofree PSECURITY_DESCRIPTOR pSD =3D NULL; + + /* Gets the appropriate ACL for the user */ + if (!create_acl(userInfo, &pNewACL, errp)) { + goto error; + } + + /* Allocate memory for a security descriptor */ + pSD =3D g_malloc(SECURITY_DESCRIPTOR_MIN_LENGTH); + if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) { + error_setg_win32(errp, GetLastError(), + "Failed to initialize security descriptor"); + goto error; + } + + /* Associate the security descriptor with the ACL permissions. */ + if (!SetSecurityDescriptorDacl(pSD, TRUE, pNewACL, FALSE)) { + error_setg_win32(errp, GetLastError(), + "Failed to set security descriptor ACL"); + goto error; + } + + /* Set the security attributes on the folder */ + SECURITY_ATTRIBUTES sAttr; + sAttr.bInheritHandle =3D FALSE; + sAttr.nLength =3D sizeof(SECURITY_ATTRIBUTES); + sAttr.lpSecurityDescriptor =3D pSD; + + /* Create the directory with the created permissions */ + BOOL created =3D CreateDirectory(userInfo->sshDirectory, &sAttr); + if (!created) { + error_setg_win32(errp, GetLastError(), "failed to create directory= %s", + userInfo->sshDirectory); + goto error; + } + + /* Free memory */ + LocalFree(pNewACL); + return true; +error: + LocalFree(pNewACL); + return false; +} + +/* + * Sets permissions on the authorized_key_file that is created. + * + * parameters: userInfo -> Information about the user + * errp -> error structure that will contain errors upon failure + * returns: 1 upon success, zero upon failure. + */ +static bool set_file_permissions(PWindowsUserInfo userInfo, Error **errp) +{ + PACL pACL =3D NULL; + PSID userPSID; + + /* Creates the access control structure */ + if (!create_acl(userInfo, &pACL, errp)) { + goto error; + } + + /* Get the PSID structure for the user based off the string SID. */ + bool converted =3D ConvertStringSidToSid(userInfo->SSID, &userPSID); + if (!converted) { + error_setg_win32(errp, GetLastError(), "failed to retrieve user %s= SID", + userInfo->username); + goto error; + } + + /* Prevents permissions from being inherited and use the DACL provided= . */ + const SE_OBJECT_TYPE securityBitFlags =3D + DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION; + + /* Set the ACL on the file. */ + if (SetNamedSecurityInfo(userInfo->authorizedKeyFile, SE_FILE_OBJECT, + securityBitFlags, userPSID, NULL, pACL, + NULL) !=3D ERROR_SUCCESS) { + error_setg_win32(errp, GetLastError(), + "failed to set file security for file %s", + userInfo->authorizedKeyFile); + goto error; + } + + LocalFree(pACL); + LocalFree(userPSID); + return true; + +error: + LocalFree(pACL); + LocalFree(userPSID); + + return false; +} + +/* + * Writes the specified keys to the authenticated keys file. + * parameters: + * userInfo: Information about the user we are writing the authkeys file t= o. + * authkeys: Array of keys to write to disk + * errp: Error structure that will contain any errors if they occur. + * returns: 1 if successful, 0 otherwise. + */ +static bool write_authkeys(WindowsUserInfo *userInfo, GStrv authkeys, + Error **errp) +{ + g_autofree char *contents =3D NULL; + g_autoptr(GError) err =3D NULL; + + contents =3D g_strjoinv("\n", authkeys); + + if (!g_file_set_contents(userInfo->authorizedKeyFile, contents, -1, &e= rr)) { + error_setg(errp, "failed to write to '%s': %s", + userInfo->authorizedKeyFile, err->message); + return false; + } + + if (!set_file_permissions(userInfo, errp)) { + return false; + } + + return true; +} + +/* + * Retrieves information about a Windows user by their username + * + * parameters: + * userInfo -> Double pointer to a WindowsUserInfo structure. Upon success= , it + * will be allocated with information about the user and need to be freed. + * username -> Name of the user to lookup. + * errp -> Contains any errors that occur. + * returns: 1 upon success, 0 upon failure. + */ +static bool get_user_info(PWindowsUserInfo *userInfo, const char *username, + Error **errp) +{ + DWORD infoLevel =3D 4; + LPUSER_INFO_4 uBuf =3D NULL; + g_autofree wchar_t *wideUserName =3D NULL; + g_autoptr(GError) gerr =3D NULL; + PSID psid =3D NULL; + + /* + * Converts a string to a Windows wide string since the GetNetUserInfo + * function requires it. + */ + wideUserName =3D g_utf8_to_utf16(username, -1, NULL, NULL, &gerr); + if (!wideUserName) { + goto error; + } + + /* allocate data */ + PWindowsUserInfo uData =3D g_new0(WindowsUserInfo, 1); + + /* Set pointer so it can be cleaned up by the callee, even upon error.= */ + *userInfo =3D uData; + + /* Find the information */ + NET_API_STATUS result =3D + NetUserGetInfo(NULL, wideUserName, infoLevel, (LPBYTE *)&uBuf); + if (result !=3D NERR_Success) { + /* Give a friendlier error message if the user was not found. */ + if (result =3D=3D NERR_UserNotFound) { + error_setg(errp, "User %s was not found", username); + goto error; + } + + error_setg(errp, + "Received unexpected error when asking for user info: E= rror " + "Code %lu", + result); + goto error; + } + + /* Get information from the buffer returned by NetUserGetInfo. */ + uData->username =3D g_strdup(username); + uData->isAdmin =3D uBuf->usri4_priv =3D=3D USER_PRIV_ADMIN; + psid =3D uBuf->usri4_user_sid; + + char *sidStr =3D NULL; + + /* + * We store the string representation of the SID not SID structure in + * memory. Callees wanting to use the SID structure should call + * ConvertStringSidToSID. + */ + if (!ConvertSidToStringSid(psid, &sidStr)) { + error_setg_win32(errp, GetLastError(), + "failed to get SID string for user %s", username); + goto error; + } + + /* Store the SSID */ + uData->SSID =3D sidStr; + + /* Get the SSH folder for the user. */ + char *sshFolder =3D get_ssh_folder(username, uData->isAdmin, errp); + if (sshFolder =3D=3D NULL) { + goto error; + } + + /* Get the authorized key file path */ + const char *authorizedKeyFile =3D + uData->isAdmin ? AUTHORIZED_KEY_FILE_ADMIN : AUTHORIZED_KEY_FILE; + char *authorizedKeyPath =3D + g_build_filename(sshFolder, authorizedKeyFile, NULL); + uData->sshDirectory =3D sshFolder; + uData->authorizedKeyFile =3D authorizedKeyPath; + + /* Free */ + NetApiBufferFree(uBuf); + return true; +error: + if (uBuf) { + NetApiBufferFree(uBuf); + } + + return false; +} + +/* + * Gets the list of authorized keys for a user. + * + * parameters: + * username -> Username to retrieve the keys for. + * errp -> Error structure that will display any errors through QMP. + * returns: List of keys associated with the user. + */ +GuestAuthorizedKeys *qmp_guest_ssh_get_authorized_keys(const char *usernam= e, + Error **errp) +{ + GuestAuthorizedKeys *keys =3D NULL; + g_auto(GStrv) authKeys =3D NULL; + g_autoptr(GuestAuthorizedKeys) ret =3D NULL; + g_auto(PWindowsUserInfo) userInfo =3D NULL; + + /* Gets user information */ + if (!get_user_info(&userInfo, username, errp)) { + return NULL; + } + + /* Reads authkeys for the user */ + authKeys =3D read_authkeys(userInfo->authorizedKeyFile, errp); + if (authKeys =3D=3D NULL) { + return NULL; + } + + /* Set the GuestAuthorizedKey struct with keys from the file */ + ret =3D g_new0(GuestAuthorizedKeys, 1); + for (int i =3D 0; authKeys[i] !=3D NULL; i++) { + g_strstrip(authKeys[i]); + if (!authKeys[i][0] || authKeys[i][0] =3D=3D '#') { + continue; + } + + QAPI_LIST_PREPEND(ret->keys, g_strdup(authKeys[i])); + } + + /* + * Steal the pointer because it is up for the callee to deallocate the + * memory. + */ + keys =3D g_steal_pointer(&ret); + return keys; +} + +/* + * Adds an ssh key for a user. + * + * parameters: + * username -> User to add the SSH key to + * strList -> Array of keys to add to the list + * has_reset -> Whether the keys have been reset + * reset -> Boolean to reset the keys (If this is set the existing list wi= ll be + * cleared) and the other key reset. errp -> Pointer to an error structure= that + * will get returned over QMP if anything goes wrong. + */ +void qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys, + bool has_reset, bool reset, Error *= *errp) +{ + g_auto(PWindowsUserInfo) userInfo =3D NULL; + g_auto(GStrv) authkeys =3D NULL; + strList *k; + size_t nkeys, nauthkeys; + + /* Make sure the keys given are valid */ + if (!check_openssh_pub_keys(keys, &nkeys, errp)) { + return; + } + + /* Gets user information */ + if (!get_user_info(&userInfo, username, errp)) { + return; + } + + /* Determine whether we should reset the keys */ + reset =3D has_reset && reset; + if (!reset) { + /* Read existing keys into memory */ + authkeys =3D read_authkeys(userInfo->authorizedKeyFile, NULL); + } + + /* Check that the SSH key directory exists for the user. */ + if (!g_file_test(userInfo->sshDirectory, G_FILE_TEST_IS_DIR)) { + BOOL success =3D create_ssh_directory(userInfo, errp); + if (!success) { + return; + } + } + + /* Reallocates the buffer to fit the new keys. */ + nauthkeys =3D authkeys ? g_strv_length(authkeys) : 0; + authkeys =3D g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char = *)); + + /* zero out the memory for the reallocated buffer */ + memset(authkeys + nauthkeys, 0, (nkeys + 1) * sizeof(char *)); + + /* Adds the keys */ + for (k =3D keys; k !=3D NULL; k =3D k->next) { + /* Check that the key doesn't already exist */ + if (g_strv_contains((const gchar *const *)authkeys, k->value)) { + continue; + } + + authkeys[nauthkeys++] =3D g_strdup(k->value); + } + + /* Write the authkeys to the file. */ + write_authkeys(userInfo, authkeys, errp); +} + +/* + * Removes an SSH key for a user + * + * parameters: + * username -> Username to remove the key from + * strList -> List of strings to remove + * errp -> Contains any errors that occur. + */ +void qmp_guest_ssh_remove_authorized_keys(const char *username, strList *k= eys, + Error **errp) +{ + g_auto(PWindowsUserInfo) userInfo =3D NULL; + g_autofree struct passwd *p =3D NULL; + g_autofree GStrv new_keys =3D NULL; /* do not own the strings */ + g_auto(GStrv) authkeys =3D NULL; + GStrv a; + size_t nkeys =3D 0; + + /* Validates the keys passed in by the user */ + if (!check_openssh_pub_keys(keys, NULL, errp)) { + return; + } + + /* Gets user information */ + if (!get_user_info(&userInfo, username, errp)) { + return; + } + + /* Reads the authkeys for the user */ + authkeys =3D read_authkeys(userInfo->authorizedKeyFile, errp); + if (authkeys =3D=3D NULL) { + return; + } + + /* Create a new buffer to hold the keys */ + new_keys =3D g_new0(char *, g_strv_length(authkeys) + 1); + for (a =3D authkeys; *a !=3D NULL; a++) { + strList *k; + + /* Filters out keys that are equal to ones the user specified. */ + for (k =3D keys; k !=3D NULL; k =3D k->next) { + if (g_str_equal(k->value, *a)) { + break; + } + } + + if (k !=3D NULL) { + continue; + } + + new_keys[nkeys++] =3D *a; + } + + /* Write the new authkeys to the file. */ + write_authkeys(userInfo, new_keys, errp); +} diff --git a/qga/commands-windows-ssh.h b/qga/commands-windows-ssh.h new file mode 100644 index 0000000000..40ac67c4d9 --- /dev/null +++ b/qga/commands-windows-ssh.h @@ -0,0 +1,26 @@ +/* + * Header file for commands-windows-ssh.c + * + * Copyright Schweitzer Engineering Laboratories. 2024 + * + * Authors: + * Aidan Leuck + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include +#include +typedef struct WindowsUserInfo { + char *sshDirectory; + char *authorizedKeyFile; + char *username; + char *SSID; + bool isAdmin; +} WindowsUserInfo; + +typedef WindowsUserInfo *PWindowsUserInfo; + +void free_userInfo(PWindowsUserInfo info); +G_DEFINE_AUTO_CLEANUP_FREE_FUNC(PWindowsUserInfo, free_userInfo, NULL); diff --git a/qga/meson.build b/qga/meson.build index 4c3899751b..e9c72fcf5e 100644 --- a/qga/meson.build +++ b/qga/meson.build @@ -73,7 +73,8 @@ if host_os =3D=3D 'windows' 'channel-win32.c', 'commands-win32.c', 'service-win32.c', - 'vss-win32.c' + 'vss-win32.c', + 'commands-windows-ssh.c' )) else qga_ss.add(files( @@ -94,7 +95,7 @@ gen_tlb =3D [] qga_libs =3D [] if host_os =3D=3D 'windows' qga_libs +=3D ['-lws2_32', '-lwinmm', '-lpowrprof', '-lwtsapi32', '-lwin= inet', '-liphlpapi', '-lnetapi32', - '-lsetupapi', '-lcfgmgr32'] + '-lsetupapi', '-lcfgmgr32', '-luserenv'] if have_qga_vss qga_libs +=3D ['-lole32', '-loleaut32', '-lshlwapi', '-lstdc++', '-Wl,= --enable-stdcall-fixup'] subdir('vss-win32') @@ -204,4 +205,4 @@ if false qga_ssh_test, env: test_env, suite: ['unit', 'qga']) -endif +endif \ No newline at end of file diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index 9554b566a7..a64a6d91cf 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1562,9 +1562,8 @@ { 'struct': 'GuestAuthorizedKeys', 'data': { 'keys': ['str'] - }, - 'if': 'CONFIG_POSIX' } - + } +} =20 ## # @guest-ssh-get-authorized-keys: @@ -1580,8 +1579,8 @@ ## { 'command': 'guest-ssh-get-authorized-keys', 'data': { 'username': 'str' }, - 'returns': 'GuestAuthorizedKeys', - 'if': 'CONFIG_POSIX' } + 'returns': 'GuestAuthorizedKeys' +} =20 ## # @guest-ssh-add-authorized-keys: @@ -1599,8 +1598,8 @@ # Since: 5.2 ## { 'command': 'guest-ssh-add-authorized-keys', - 'data': { 'username': 'str', 'keys': ['str'], '*reset': 'bool' }, - 'if': 'CONFIG_POSIX' } + 'data': { 'username': 'str', 'keys': ['str'], '*reset': 'bool' } +} =20 ## # @guest-ssh-remove-authorized-keys: @@ -1617,8 +1616,8 @@ # Since: 5.2 ## { 'command': 'guest-ssh-remove-authorized-keys', - 'data': { 'username': 'str', 'keys': ['str'] }, - 'if': 'CONFIG_POSIX' } + 'data': { 'username': 'str', 'keys': ['str'] } +} =20 ## # @GuestDiskStats: --=20 2.44.0