diff mbox series

[v3,3/7] fixup! qga: add ssh-{add,remove}-authorized-keys

Message ID 20201020081257.2054548-4-marcandre.lureau@redhat.com
State New
Headers show
Series qemu-ga: add ssh-{get,add,remove}-authorized-keys | expand

Commit Message

Marc-André Lureau Oct. 20, 2020, 8:12 a.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

I forgot to reset the file ownership after it is written.
---
 qga/commands-posix-ssh.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/qga/commands-posix-ssh.c b/qga/commands-posix-ssh.c
index d41c114c3c..a7bc9a1c24 100644
--- a/qga/commands-posix-ssh.c
+++ b/qga/commands-posix-ssh.c
@@ -120,7 +120,8 @@  check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp)
 }
 
 static bool
-write_authkeys(const char *path, const GStrv keys, Error **errp)
+write_authkeys(const char *path, const GStrv keys,
+               const struct passwd *p, Error **errp)
 {
     g_autofree char *contents = NULL;
     g_autoptr(GError) err = NULL;
@@ -133,6 +134,12 @@  write_authkeys(const char *path, const GStrv keys, Error **errp)
         return false;
     }
 
+    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
+        error_setg(errp, "failed to set ownership of directory '%s': %s",
+                   path, g_strerror(errno));
+        return false;
+    }
+
     if (chmod(path, 0600) == -1) {
         error_setg(errp, "failed to set permissions of '%s': %s",
                    path, g_strerror(errno));
@@ -203,7 +210,7 @@  qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
         authkeys[nauthkeys++] = g_strdup(k->value);
     }
 
-    write_authkeys(authkeys_path, authkeys, errp);
+    write_authkeys(authkeys_path, authkeys, p, errp);
 }
 
 void
@@ -254,7 +261,7 @@  qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
         new_keys[nkeys++] = *a;
     }
 
-    write_authkeys(authkeys_path, new_keys, errp);
+    write_authkeys(authkeys_path, new_keys, p, errp);
 }