xref: /openbmc/qemu/qga/commands-common-ssh.c (revision ddd17313)
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 
6 #include "qemu/osdep.h"
7 #include "qapi/error.h"
8 #include "commands-common-ssh.h"
9 
10 GStrv read_authkeys(const char *path, Error **errp)
11 {
12     g_autoptr(GError) err = NULL;
13     g_autofree char *contents = NULL;
14 
15     if (!g_file_get_contents(path, &contents, NULL, &err)) {
16         error_setg(errp, "failed to read '%s': %s", path, err->message);
17         return NULL;
18     }
19 
20     return g_strsplit(contents, "\n", -1);
21 }
22 
23 bool check_openssh_pub_keys(strList *keys, size_t *nkeys, Error **errp)
24 {
25     size_t n = 0;
26     strList *k;
27 
28     for (k = keys; k != NULL; k = k->next) {
29         if (!check_openssh_pub_key(k->value, errp)) {
30             return false;
31         }
32         n++;
33     }
34 
35     if (nkeys) {
36         *nkeys = n;
37     }
38     return true;
39 }
40 
41 bool check_openssh_pub_key(const char *key, Error **errp)
42 {
43     /* simple sanity-check, we may want more? */
44     if (!key || key[0] == '#' || strchr(key, '\n')) {
45         error_setg(errp, "invalid OpenSSH public key: '%s'", key);
46         return false;
47     }
48 
49     return true;
50 }
51