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.
5 #include "qemu/osdep.h"
8 #include <glib/gstdio.h>
12 #include "qapi/error.h"
13 #include "qga-qapi-commands.h"
15 #ifdef QGA_BUILD_UNIT_TEST
16 static struct passwd *
17 test_get_passwd_entry(const gchar *user_name, GError **error)
22 if (!user_name || g_strcmp0(user_name, g_get_user_name())) {
23 g_set_error(error, G_UNIX_ERROR, 0, "Invalid user name");
27 p = g_new0(struct passwd, 1);
28 p->pw_dir = (char *)g_get_home_dir();
29 p->pw_uid = geteuid();
30 p->pw_gid = getegid();
32 ret = g_mkdir_with_parents(p->pw_dir, 0700);
38 #define g_unix_get_passwd_entry_qemu(username, err) \
39 test_get_passwd_entry(username, err)
42 static struct passwd *
43 get_passwd_entry(const char *username, Error **errp)
45 g_autoptr(GError) err = NULL;
50 p = g_unix_get_passwd_entry_qemu(username, &err);
52 error_setg(errp, "failed to lookup user '%s': %s",
53 username, err->message);
61 mkdir_for_user(const char *path, const struct passwd *p,
62 mode_t mode, Error **errp)
66 if (g_mkdir(path, mode) == -1) {
67 error_setg(errp, "failed to create directory '%s': %s",
68 path, g_strerror(errno));
72 if (chown(path, p->pw_uid, p->pw_gid) == -1) {
73 error_setg(errp, "failed to set ownership of directory '%s': %s",
74 path, g_strerror(errno));
78 if (chmod(path, mode) == -1) {
79 error_setg(errp, "failed to set permissions of directory '%s': %s",
80 path, g_strerror(errno));
88 check_openssh_pub_key(const char *key, Error **errp)
92 /* simple sanity-check, we may want more? */
93 if (!key || key[0] == '#' || strchr(key, '\n')) {
94 error_setg(errp, "invalid OpenSSH public key: '%s'", key);
102 check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp)
109 for (k = keys; k != NULL; k = k->next) {
110 if (!check_openssh_pub_key(k->value, errp)) {
123 write_authkeys(const char *path, const GStrv keys,
124 const struct passwd *p, Error **errp)
126 g_autofree char *contents = NULL;
127 g_autoptr(GError) err = NULL;
131 contents = g_strjoinv("\n", keys);
132 if (!g_file_set_contents(path, contents, -1, &err)) {
133 error_setg(errp, "failed to write to '%s': %s", path, err->message);
137 if (chown(path, p->pw_uid, p->pw_gid) == -1) {
138 error_setg(errp, "failed to set ownership of directory '%s': %s",
139 path, g_strerror(errno));
143 if (chmod(path, 0600) == -1) {
144 error_setg(errp, "failed to set permissions of '%s': %s",
145 path, g_strerror(errno));
153 read_authkeys(const char *path, Error **errp)
155 g_autoptr(GError) err = NULL;
156 g_autofree char *contents = NULL;
160 if (!g_file_get_contents(path, &contents, NULL, &err)) {
161 error_setg(errp, "failed to read '%s': %s", path, err->message);
165 return g_strsplit(contents, "\n", -1);
170 qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
171 bool has_reset, bool reset,
174 g_autofree struct passwd *p = NULL;
175 g_autofree char *ssh_path = NULL;
176 g_autofree char *authkeys_path = NULL;
177 g_auto(GStrv) authkeys = NULL;
179 size_t nkeys, nauthkeys;
182 reset = has_reset && reset;
184 if (!check_openssh_pub_keys(keys, &nkeys, errp)) {
188 p = get_passwd_entry(username, errp);
193 ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
194 authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
197 authkeys = read_authkeys(authkeys_path, NULL);
199 if (authkeys == NULL) {
200 if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
201 !mkdir_for_user(ssh_path, p, 0700, errp)) {
206 nauthkeys = authkeys ? g_strv_length(authkeys) : 0;
207 authkeys = g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char *));
208 memset(authkeys + nauthkeys, 0, (nkeys + 1) * sizeof(char *));
210 for (k = keys; k != NULL; k = k->next) {
211 if (g_strv_contains((const gchar * const *)authkeys, k->value)) {
214 authkeys[nauthkeys++] = g_strdup(k->value);
217 write_authkeys(authkeys_path, authkeys, p, errp);
221 qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
224 g_autofree struct passwd *p = NULL;
225 g_autofree char *authkeys_path = NULL;
226 g_autofree GStrv new_keys = NULL; /* do not own the strings */
227 g_auto(GStrv) authkeys = NULL;
233 if (!check_openssh_pub_keys(keys, NULL, errp)) {
237 p = get_passwd_entry(username, errp);
242 authkeys_path = g_build_filename(p->pw_dir, ".ssh",
243 "authorized_keys", NULL);
244 if (!g_file_test(authkeys_path, G_FILE_TEST_EXISTS)) {
247 authkeys = read_authkeys(authkeys_path, errp);
248 if (authkeys == NULL) {
252 new_keys = g_new0(char *, g_strv_length(authkeys) + 1);
253 for (a = authkeys; *a != NULL; a++) {
256 for (k = keys; k != NULL; k = k->next) {
257 if (g_str_equal(k->value, *a)) {
265 new_keys[nkeys++] = *a;
268 write_authkeys(authkeys_path, new_keys, p, errp);
271 GuestAuthorizedKeys *
272 qmp_guest_ssh_get_authorized_keys(const char *username, Error **errp)
274 g_autofree struct passwd *p = NULL;
275 g_autofree char *authkeys_path = NULL;
276 g_auto(GStrv) authkeys = NULL;
277 g_autoptr(GuestAuthorizedKeys) ret = NULL;
282 p = get_passwd_entry(username, errp);
287 authkeys_path = g_build_filename(p->pw_dir, ".ssh",
288 "authorized_keys", NULL);
289 authkeys = read_authkeys(authkeys_path, errp);
290 if (authkeys == NULL) {
294 ret = g_new0(GuestAuthorizedKeys, 1);
295 for (i = 0; authkeys[i] != NULL; i++) {
296 g_strstrip(authkeys[i]);
297 if (!authkeys[i][0] || authkeys[i][0] == '#') {
301 QAPI_LIST_PREPEND(ret->keys, g_strdup(authkeys[i]));
304 return g_steal_pointer(&ret);
307 #ifdef QGA_BUILD_UNIT_TEST
308 #if GLIB_CHECK_VERSION(2, 60, 0)
309 static const strList test_key2 = {
310 .value = (char *)"algo key2 comments"
313 static const strList test_key1_2 = {
314 .value = (char *)"algo key1 comments",
315 .next = (strList *)&test_key2,
319 test_get_authorized_keys_path(void)
321 return g_build_filename(g_get_home_dir(), ".ssh", "authorized_keys", NULL);
325 test_authorized_keys_set(const char *contents)
327 g_autoptr(GError) err = NULL;
328 g_autofree char *path = NULL;
331 path = g_build_filename(g_get_home_dir(), ".ssh", NULL);
332 ret = g_mkdir_with_parents(path, 0700);
336 path = test_get_authorized_keys_path();
337 g_file_set_contents(path, contents, -1, &err);
338 g_assert(err == NULL);
342 test_authorized_keys_equal(const char *expected)
344 g_autoptr(GError) err = NULL;
345 g_autofree char *path = NULL;
346 g_autofree char *contents = NULL;
348 path = test_get_authorized_keys_path();
349 g_file_get_contents(path, &contents, NULL, &err);
350 g_assert(err == NULL);
352 g_assert(g_strcmp0(contents, expected) == 0);
356 test_invalid_user(void)
360 qmp_guest_ssh_add_authorized_keys("", NULL, FALSE, FALSE, &err);
361 error_free_or_abort(&err);
363 qmp_guest_ssh_remove_authorized_keys("", NULL, &err);
364 error_free_or_abort(&err);
368 test_invalid_key(void)
371 .value = (char *)"not a valid\nkey"
375 qmp_guest_ssh_add_authorized_keys(g_get_user_name(), &key,
377 error_free_or_abort(&err);
379 qmp_guest_ssh_remove_authorized_keys(g_get_user_name(), &key, &err);
380 error_free_or_abort(&err);
388 qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
389 (strList *)&test_key2,
392 g_assert(err == NULL);
394 test_authorized_keys_equal("algo key2 comments");
396 qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
397 (strList *)&test_key1_2,
400 g_assert(err == NULL);
402 /* key2 came first, and should'nt be duplicated */
403 test_authorized_keys_equal("algo key2 comments\n"
404 "algo key1 comments");
408 test_add_reset_keys(void)
412 qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
413 (strList *)&test_key1_2,
416 g_assert(err == NULL);
418 /* reset with key2 only */
419 test_authorized_keys_equal("algo key1 comments\n"
420 "algo key2 comments");
422 qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
423 (strList *)&test_key2,
426 g_assert(err == NULL);
428 test_authorized_keys_equal("algo key2 comments");
430 /* empty should clear file */
431 qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
435 g_assert(err == NULL);
437 test_authorized_keys_equal("");
441 test_remove_keys(void)
444 static const char *authkeys =
445 "algo key1 comments\n"
446 /* originally duplicated */
447 "algo key1 comments\n"
448 "# a commented line\n"
449 "algo some-key another\n";
451 test_authorized_keys_set(authkeys);
452 qmp_guest_ssh_remove_authorized_keys(g_get_user_name(),
453 (strList *)&test_key2, &err);
454 g_assert(err == NULL);
455 test_authorized_keys_equal(authkeys);
457 qmp_guest_ssh_remove_authorized_keys(g_get_user_name(),
458 (strList *)&test_key1_2, &err);
459 g_assert(err == NULL);
460 test_authorized_keys_equal("# a commented line\n"
461 "algo some-key another\n");
468 static const char *authkeys =
469 "algo key1 comments\n"
470 "# a commented line\n"
471 "algo some-key another\n";
472 g_autoptr(GuestAuthorizedKeys) ret = NULL;
476 test_authorized_keys_set(authkeys);
478 ret = qmp_guest_ssh_get_authorized_keys(g_get_user_name(), &err);
479 g_assert(err == NULL);
481 for (len = 0, k = ret->keys; k != NULL; k = k->next) {
482 g_assert(g_str_has_prefix(k->value, "algo "));
489 int main(int argc, char *argv[])
491 setlocale(LC_ALL, "");
493 g_test_init(&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
495 g_test_add_func("/qga/ssh/invalid_user", test_invalid_user);
496 g_test_add_func("/qga/ssh/invalid_key", test_invalid_key);
497 g_test_add_func("/qga/ssh/add_keys", test_add_keys);
498 g_test_add_func("/qga/ssh/add_reset_keys", test_add_reset_keys);
499 g_test_add_func("/qga/ssh/remove_keys", test_remove_keys);
500 g_test_add_func("/qga/ssh/get_keys", test_get_keys);
505 int main(int argc, char *argv[])
507 g_test_message("test skipped, needs glib >= 2.60");
510 #endif /* GLIB_2_60 */
511 #endif /* BUILD_UNIT_TEST */