yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
6e7d01
From 1ed102f5489e6cf3168d9014e9a082909193b6fc Mon Sep 17 00:00:00 2001
6e7d01
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
6e7d01
Date: Thu, 29 Jul 2021 04:55:57 -0400
6e7d01
Subject: [PATCH 04/14] qga: add ssh-get-authorized-keys
6e7d01
MIME-Version: 1.0
6e7d01
Content-Type: text/plain; charset=UTF-8
6e7d01
Content-Transfer-Encoding: 8bit
6e7d01
6e7d01
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
Message-id: <20210609100615.2501448-5-marcandre.lureau@redhat.com>
6e7d01
Patchwork-id: 101690
6e7d01
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 4/4] qga: add ssh-get-authorized-keys
6e7d01
Bugzilla: 1967716
6e7d01
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
6e7d01
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
6e7d01
RH-Acked-by: Michal Privoznik <mprivozn@redhat.com>
6e7d01
6e7d01
From: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
6e7d01
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
*fix-up merge conflicts due to qga-ssh-test being disabled in earlier
6e7d01
 patch due to G_TEST_OPTION_ISOLATE_DIRS triggering build-oss-fuzz
6e7d01
 leak detector.
6e7d01
*fix up style and disallowed g_assert* usage reported by checkpatch
6e7d01
Signed-off-by: Michael Roth <michael.roth@amd.com>
6e7d01
6e7d01
(cherry picked from commit cad97c08a1c17830d77a46780088bc0199df89d1)
6e7d01
[ Fix trivial schema conflict ]
6e7d01
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
6e7d01
---
6e7d01
 qga/commands-posix-ssh.c | 66 ++++++++++++++++++++++++++++++++++++++++
6e7d01
 qga/qapi-schema.json     | 30 ++++++++++++++++++
6e7d01
 2 files changed, 96 insertions(+)
6e7d01
6e7d01
diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c
6e7d01
index 362c9e8816..749167e82d 100644
6e7d01
--- a/qga/commands-posix-ssh.c
6e7d01
+++ b/qga/commands-posix-ssh.c
6e7d01
@@ -268,6 +268,46 @@ qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
6e7d01
     write_authkeys(authkeys_path, new_keys, p, errp);
6e7d01
 }
6e7d01
 
6e7d01
+GuestAuthorizedKeys *
6e7d01
+qmp_guest_ssh_get_authorized_keys(const char *username, Error **errp)
6e7d01
+{
6e7d01
+    g_autofree struct passwd *p = NULL;
6e7d01
+    g_autofree char *authkeys_path = NULL;
6e7d01
+    g_auto(GStrv) authkeys = NULL;
6e7d01
+    g_autoptr(GuestAuthorizedKeys) ret = NULL;
6e7d01
+    int i;
6e7d01
+
6e7d01
+    ERRP_GUARD();
6e7d01
+
6e7d01
+    p = get_passwd_entry(username, errp);
6e7d01
+    if (p == NULL) {
6e7d01
+        return NULL;
6e7d01
+    }
6e7d01
+
6e7d01
+    authkeys_path = g_build_filename(p->pw_dir, ".ssh",
6e7d01
+                                     "authorized_keys", NULL);
6e7d01
+    authkeys = read_authkeys(authkeys_path, errp);
6e7d01
+    if (authkeys == NULL) {
6e7d01
+        return NULL;
6e7d01
+    }
6e7d01
+
6e7d01
+    ret = g_new0(GuestAuthorizedKeys, 1);
6e7d01
+    for (i = 0; authkeys[i] != NULL; i++) {
6e7d01
+        strList *new;
6e7d01
+
6e7d01
+        g_strstrip(authkeys[i]);
6e7d01
+        if (!authkeys[i][0] || authkeys[i][0] == '#') {
6e7d01
+            continue;
6e7d01
+        }
6e7d01
+
6e7d01
+        new = g_new0(strList, 1);
6e7d01
+        new->value = g_strdup(authkeys[i]);
6e7d01
+        new->next = ret->keys;
6e7d01
+        ret->keys = new;
6e7d01
+    }
6e7d01
+
6e7d01
+    return g_steal_pointer(&ret;;
6e7d01
+}
6e7d01
 
6e7d01
 #ifdef QGA_BUILD_UNIT_TEST
6e7d01
 #if GLIB_CHECK_VERSION(2, 60, 0)
6e7d01
@@ -426,6 +466,31 @@ test_remove_keys(void)
6e7d01
                                "algo some-key another\n");
6e7d01
 }
6e7d01
 
6e7d01
+static void
6e7d01
+test_get_keys(void)
6e7d01
+{
6e7d01
+    Error *err = NULL;
6e7d01
+    static const char *authkeys =
6e7d01
+        "algo key1 comments\n"
6e7d01
+        "# a commented line\n"
6e7d01
+        "algo some-key another\n";
6e7d01
+    g_autoptr(GuestAuthorizedKeys) ret = NULL;
6e7d01
+    strList *k;
6e7d01
+    size_t len = 0;
6e7d01
+
6e7d01
+    test_authorized_keys_set(authkeys);
6e7d01
+
6e7d01
+    ret = qmp_guest_ssh_get_authorized_keys(g_get_user_name(), &err;;
6e7d01
+    g_assert(err == NULL);
6e7d01
+
6e7d01
+    for (len = 0, k = ret->keys; k != NULL; k = k->next) {
6e7d01
+        g_assert(g_str_has_prefix(k->value, "algo "));
6e7d01
+        len++;
6e7d01
+    }
6e7d01
+
6e7d01
+    g_assert(len == 2);
6e7d01
+}
6e7d01
+
6e7d01
 int main(int argc, char *argv[])
6e7d01
 {
6e7d01
     setlocale(LC_ALL, "");
6e7d01
@@ -437,6 +502,7 @@ int main(int argc, char *argv[])
6e7d01
     g_test_add_func("/qga/ssh/add_keys", test_add_keys);
6e7d01
     g_test_add_func("/qga/ssh/add_reset_keys", test_add_reset_keys);
6e7d01
     g_test_add_func("/qga/ssh/remove_keys", test_remove_keys);
6e7d01
+    g_test_add_func("/qga/ssh/get_keys", test_get_keys);
6e7d01
 
6e7d01
     return g_test_run();
6e7d01
 }
6e7d01
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
6e7d01
index a70ea5da77..97bf96712e 100644
6e7d01
--- a/qga/qapi-schema.json
6e7d01
+++ b/qga/qapi-schema.json
6e7d01
@@ -1274,6 +1274,36 @@
6e7d01
 { 'command': 'guest-get-osinfo',
6e7d01
   'returns': 'GuestOSInfo' }
6e7d01
 
6e7d01
+##
6e7d01
+# @GuestAuthorizedKeys:
6e7d01
+#
6e7d01
+# @keys: public keys (in OpenSSH/sshd(8) authorized_keys format)
6e7d01
+#
6e7d01
+# Since: 5.2
6e7d01
+##
6e7d01
+{ 'struct': 'GuestAuthorizedKeys',
6e7d01
+  'data': {
6e7d01
+      'keys': ['str']
6e7d01
+  },
6e7d01
+  'if': 'defined(CONFIG_POSIX)' }
6e7d01
+
6e7d01
+##
6e7d01
+# @guest-ssh-get-authorized-keys:
6e7d01
+#
6e7d01
+# @username: the user account to add the authorized keys
6e7d01
+#
6e7d01
+# Return the public keys from user .ssh/authorized_keys on Unix systems (not
6e7d01
+# implemented for other systems).
6e7d01
+#
6e7d01
+# Returns: @GuestAuthorizedKeys
6e7d01
+#
6e7d01
+# Since: 5.2
6e7d01
+##
6e7d01
+{ 'command': 'guest-ssh-get-authorized-keys',
6e7d01
+  'data': { 'username': 'str' },
6e7d01
+  'returns': 'GuestAuthorizedKeys',
6e7d01
+  'if': 'defined(CONFIG_POSIX)' }
6e7d01
+
6e7d01
 ##
6e7d01
 # @guest-ssh-add-authorized-keys:
6e7d01
 #
6e7d01
-- 
6e7d01
2.27.0
6e7d01