commands-posix-ssh.c (c5ea91da443b458352c1b629b490ee6631775cb4) commands-posix-ssh.c (1cc9932700339042e83c6c54114734231630548c)
1 /*
2 * This work is licensed under the terms of the GNU GPL, version 2 or later.
3 * See the COPYING file in the top-level directory.
4 */
5#include "qemu/osdep.h"
6
7#include <glib-unix.h>
8#include <glib/gstdio.h>
9#include <locale.h>
10#include <pwd.h>
11
1 /*
2 * This work is licensed under the terms of the GNU GPL, version 2 or later.
3 * See the COPYING file in the top-level directory.
4 */
5#include "qemu/osdep.h"
6
7#include <glib-unix.h>
8#include <glib/gstdio.h>
9#include <locale.h>
10#include <pwd.h>
11
12#include "commands-common-ssh.h"
12#include "qapi/error.h"
13#include "qga-qapi-commands.h"
14
15#ifdef QGA_BUILD_UNIT_TEST
16static struct passwd *
17test_get_passwd_entry(const gchar *user_name, GError **error)
18{
19 struct passwd *p;

--- 56 unchanged lines hidden (view full) ---

76 path, g_strerror(errno));
77 return false;
78 }
79
80 return true;
81}
82
83static bool
13#include "qapi/error.h"
14#include "qga-qapi-commands.h"
15
16#ifdef QGA_BUILD_UNIT_TEST
17static struct passwd *
18test_get_passwd_entry(const gchar *user_name, GError **error)
19{
20 struct passwd *p;

--- 56 unchanged lines hidden (view full) ---

77 path, g_strerror(errno));
78 return false;
79 }
80
81 return true;
82}
83
84static bool
84check_openssh_pub_key(const char *key, Error **errp)
85{
86 /* simple sanity-check, we may want more? */
87 if (!key || key[0] == '#' || strchr(key, '\n')) {
88 error_setg(errp, "invalid OpenSSH public key: '%s'", key);
89 return false;
90 }
91
92 return true;
93}
94
95static bool
96check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp)
97{
98 size_t n = 0;
99 strList *k;
100
101 for (k = keys; k != NULL; k = k->next) {
102 if (!check_openssh_pub_key(k->value, errp)) {
103 return false;
104 }
105 n++;
106 }
107
108 if (nkeys) {
109 *nkeys = n;
110 }
111 return true;
112}
113
114static bool
115write_authkeys(const char *path, const GStrv keys,
116 const struct passwd *p, Error **errp)
117{
118 g_autofree char *contents = NULL;
119 g_autoptr(GError) err = NULL;
120
121 contents = g_strjoinv("\n", keys);
122 if (!g_file_set_contents(path, contents, -1, &err)) {

--- 11 unchanged lines hidden (view full) ---

134 error_setg(errp, "failed to set permissions of '%s': %s",
135 path, g_strerror(errno));
136 return false;
137 }
138
139 return true;
140}
141
85write_authkeys(const char *path, const GStrv keys,
86 const struct passwd *p, Error **errp)
87{
88 g_autofree char *contents = NULL;
89 g_autoptr(GError) err = NULL;
90
91 contents = g_strjoinv("\n", keys);
92 if (!g_file_set_contents(path, contents, -1, &err)) {

--- 11 unchanged lines hidden (view full) ---

104 error_setg(errp, "failed to set permissions of '%s': %s",
105 path, g_strerror(errno));
106 return false;
107 }
108
109 return true;
110}
111
142static GStrv
143read_authkeys(const char *path, Error **errp)
144{
145 g_autoptr(GError) err = NULL;
146 g_autofree char *contents = NULL;
147
148 if (!g_file_get_contents(path, &contents, NULL, &err)) {
149 error_setg(errp, "failed to read '%s': %s", path, err->message);
150 return NULL;
151 }
152
153 return g_strsplit(contents, "\n", -1);
154
155}
156
157void
158qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
159 bool has_reset, bool reset,
160 Error **errp)
161{
162 g_autofree struct passwd *p = NULL;
163 g_autofree char *ssh_path = NULL;
164 g_autofree char *authkeys_path = NULL;

--- 330 unchanged lines hidden ---
112void
113qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
114 bool has_reset, bool reset,
115 Error **errp)
116{
117 g_autofree struct passwd *p = NULL;
118 g_autofree char *ssh_path = NULL;
119 g_autofree char *authkeys_path = NULL;

--- 330 unchanged lines hidden ---