2
0

commands-common-ssh.c 1.1 KB

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