cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-qga-add-reset-argument-to-ssh-add-authorized-keys.patch

1072c8
From 7f8888f2c53060c4536856859d5ea94d23ea9e45 Mon Sep 17 00:00:00 2001
1072c8
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
1072c8
Date: Thu, 29 Jul 2021 04:55:54 -0400
1072c8
Subject: [PATCH 03/14] qga: add *reset argument to ssh-add-authorized-keys
1072c8
MIME-Version: 1.0
1072c8
Content-Type: text/plain; charset=UTF-8
1072c8
Content-Transfer-Encoding: 8bit
1072c8
1072c8
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
Message-id: <20210609100615.2501448-4-marcandre.lureau@redhat.com>
1072c8
Patchwork-id: 101689
1072c8
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 3/4] qga: add *reset argument to ssh-add-authorized-keys
1072c8
Bugzilla: 1967716
1072c8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
1072c8
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
1072c8
RH-Acked-by: Michal Privoznik <mprivozn@redhat.com>
1072c8
1072c8
From: Michael Roth <michael.roth@amd.com>
1072c8
1072c8
I prefer 'reset' over 'clear', since 'clear' and keys may have some
1072c8
other relations or meaning.
1072c8
1072c8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
*fix disallowed g_assert* usage reported by checkpatch
1072c8
Signed-off-by: Michael Roth <michael.roth@amd.com>
1072c8
1072c8
(cherry picked from commit 0e3c94758e3851f0ab30d2a1e63a73284499775d)
1072c8
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1072c8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1072c8
---
1072c8
 qga/commands-posix-ssh.c | 53 ++++++++++++++++++++++++++++++++++++----
1072c8
 qga/qapi-schema.json     |  3 ++-
1072c8
 2 files changed, 50 insertions(+), 6 deletions(-)
1072c8
1072c8
diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c
1072c8
index f74d89679c..362c9e8816 100644
1072c8
--- a/qga/commands-posix-ssh.c
1072c8
+++ b/qga/commands-posix-ssh.c
1072c8
@@ -168,6 +168,7 @@ read_authkeys(const char *path, Error **errp)
1072c8
 
1072c8
 void
1072c8
 qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
1072c8
+                                  bool has_reset, bool reset,
1072c8
                                   Error **errp)
1072c8
 {
1072c8
     g_autofree struct passwd *p = NULL;
1072c8
@@ -178,6 +179,7 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
1072c8
     size_t nkeys, nauthkeys;
1072c8
 
1072c8
     ERRP_GUARD();
1072c8
+    reset = has_reset && reset;
1072c8
 
1072c8
     if (!check_openssh_pub_keys(keys, &nkeys, errp)) {
1072c8
         return;
1072c8
@@ -191,7 +193,9 @@ qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
1072c8
     ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
1072c8
     authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
1072c8
 
1072c8
-    authkeys = read_authkeys(authkeys_path, NULL);
1072c8
+    if (!reset) {
1072c8
+        authkeys = read_authkeys(authkeys_path, NULL);
1072c8
+    }
1072c8
     if (authkeys == NULL) {
1072c8
         if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
1072c8
             !mkdir_for_user(ssh_path, p, 0700, errp)) {
1072c8
@@ -318,7 +322,7 @@ test_invalid_user(void)
1072c8
 {
1072c8
     Error *err = NULL;
1072c8
 
1072c8
-    qmp_guest_ssh_add_authorized_keys("", NULL, &err;;
1072c8
+    qmp_guest_ssh_add_authorized_keys("", NULL, FALSE, FALSE, &err;;
1072c8
     error_free_or_abort(&err;;
1072c8
 
1072c8
     qmp_guest_ssh_remove_authorized_keys("", NULL, &err;;
1072c8
@@ -333,7 +337,8 @@ test_invalid_key(void)
1072c8
     };
1072c8
     Error *err = NULL;
1072c8
 
1072c8
-    qmp_guest_ssh_add_authorized_keys(g_get_user_name(), &key, &err;;
1072c8
+    qmp_guest_ssh_add_authorized_keys(g_get_user_name(), &key,
1072c8
+                                      FALSE, FALSE, &err;;
1072c8
     error_free_or_abort(&err;;
1072c8
 
1072c8
     qmp_guest_ssh_remove_authorized_keys(g_get_user_name(), &key, &err;;
1072c8
@@ -346,13 +351,17 @@ test_add_keys(void)
1072c8
     Error *err = NULL;
1072c8
 
1072c8
     qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
1072c8
-                                      (strList *)&test_key2, &err;;
1072c8
+                                      (strList *)&test_key2,
1072c8
+                                      FALSE, FALSE,
1072c8
+                                      &err;;
1072c8
     g_assert(err == NULL);
1072c8
 
1072c8
     test_authorized_keys_equal("algo key2 comments");
1072c8
 
1072c8
     qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
1072c8
-                                      (strList *)&test_key1_2, &err;;
1072c8
+                                      (strList *)&test_key1_2,
1072c8
+                                      FALSE, FALSE,
1072c8
+                                      &err;;
1072c8
     g_assert(err == NULL);
1072c8
 
1072c8
     /*  key2 came first, and should'nt be duplicated */
1072c8
@@ -360,6 +369,39 @@ test_add_keys(void)
1072c8
                                "algo key1 comments");
1072c8
 }
1072c8
 
1072c8
+static void
1072c8
+test_add_reset_keys(void)
1072c8
+{
1072c8
+    Error *err = NULL;
1072c8
+
1072c8
+    qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
1072c8
+                                      (strList *)&test_key1_2,
1072c8
+                                      FALSE, FALSE,
1072c8
+                                      &err;;
1072c8
+    g_assert(err == NULL);
1072c8
+
1072c8
+    /* reset with key2 only */
1072c8
+    test_authorized_keys_equal("algo key1 comments\n"
1072c8
+                               "algo key2 comments");
1072c8
+
1072c8
+    qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
1072c8
+                                      (strList *)&test_key2,
1072c8
+                                      TRUE, TRUE,
1072c8
+                                      &err;;
1072c8
+    g_assert(err == NULL);
1072c8
+
1072c8
+    test_authorized_keys_equal("algo key2 comments");
1072c8
+
1072c8
+    /* empty should clear file */
1072c8
+    qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
1072c8
+                                      (strList *)NULL,
1072c8
+                                      TRUE, TRUE,
1072c8
+                                      &err;;
1072c8
+    g_assert(err == NULL);
1072c8
+
1072c8
+    test_authorized_keys_equal("");
1072c8
+}
1072c8
+
1072c8
 static void
1072c8
 test_remove_keys(void)
1072c8
 {
1072c8
@@ -393,6 +435,7 @@ int main(int argc, char *argv[])
1072c8
     g_test_add_func("/qga/ssh/invalid_user", test_invalid_user);
1072c8
     g_test_add_func("/qga/ssh/invalid_key", test_invalid_key);
1072c8
     g_test_add_func("/qga/ssh/add_keys", test_add_keys);
1072c8
+    g_test_add_func("/qga/ssh/add_reset_keys", test_add_reset_keys);
1072c8
     g_test_add_func("/qga/ssh/remove_keys", test_remove_keys);
1072c8
 
1072c8
     return g_test_run();
1072c8
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
1072c8
index 3b85f5a03f..a70ea5da77 100644
1072c8
--- a/qga/qapi-schema.json
1072c8
+++ b/qga/qapi-schema.json
1072c8
@@ -1279,6 +1279,7 @@
1072c8
 #
1072c8
 # @username: the user account to add the authorized keys
1072c8
 # @keys: the public keys to add (in OpenSSH/sshd(8) authorized_keys format)
1072c8
+# @reset: ignore the existing content, set it with the given keys only
1072c8
 #
1072c8
 # Append public keys to user .ssh/authorized_keys on Unix systems (not
1072c8
 # implemented for other systems).
1072c8
@@ -1288,7 +1289,7 @@
1072c8
 # Since: 5.2
1072c8
 ##
1072c8
 { 'command': 'guest-ssh-add-authorized-keys',
1072c8
-  'data': { 'username': 'str', 'keys': ['str'] },
1072c8
+  'data': { 'username': 'str', 'keys': ['str'], '*reset': 'bool' },
1072c8
   'if': 'defined(CONFIG_POSIX)' }
1072c8
 
1072c8
 ##
1072c8
-- 
1072c8
2.27.0
1072c8