commands-posix-ssh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 <glib-unix.h>
  7. #include <glib/gstdio.h>
  8. #include <locale.h>
  9. #include <pwd.h>
  10. #include "commands-common-ssh.h"
  11. #include "qapi/error.h"
  12. #include "qga-qapi-commands.h"
  13. #ifdef QGA_BUILD_UNIT_TEST
  14. static struct passwd *
  15. test_get_passwd_entry(const gchar *user_name, GError **error)
  16. {
  17. struct passwd *p;
  18. int ret;
  19. if (!user_name || g_strcmp0(user_name, g_get_user_name())) {
  20. g_set_error(error, G_UNIX_ERROR, 0, "Invalid user name");
  21. return NULL;
  22. }
  23. p = g_new0(struct passwd, 1);
  24. p->pw_dir = (char *)g_get_home_dir();
  25. p->pw_uid = geteuid();
  26. p->pw_gid = getegid();
  27. ret = g_mkdir_with_parents(p->pw_dir, 0700);
  28. g_assert(ret == 0);
  29. return p;
  30. }
  31. #define g_unix_get_passwd_entry(username, err) \
  32. test_get_passwd_entry(username, err)
  33. #endif
  34. static struct passwd *
  35. get_passwd_entry(const char *username, Error **errp)
  36. {
  37. g_autoptr(GError) err = NULL;
  38. struct passwd *p;
  39. p = g_unix_get_passwd_entry(username, &err);
  40. if (p == NULL) {
  41. error_setg(errp, "failed to lookup user '%s': %s",
  42. username, err->message);
  43. return NULL;
  44. }
  45. return p;
  46. }
  47. static bool
  48. mkdir_for_user(const char *path, const struct passwd *p,
  49. mode_t mode, Error **errp)
  50. {
  51. if (g_mkdir(path, mode) == -1) {
  52. error_setg(errp, "failed to create directory '%s': %s",
  53. path, g_strerror(errno));
  54. return false;
  55. }
  56. if (chown(path, p->pw_uid, p->pw_gid) == -1) {
  57. error_setg(errp, "failed to set ownership of directory '%s': %s",
  58. path, g_strerror(errno));
  59. return false;
  60. }
  61. if (chmod(path, mode) == -1) {
  62. error_setg(errp, "failed to set permissions of directory '%s': %s",
  63. path, g_strerror(errno));
  64. return false;
  65. }
  66. return true;
  67. }
  68. static bool
  69. write_authkeys(const char *path, const GStrv keys,
  70. const struct passwd *p, Error **errp)
  71. {
  72. g_autofree char *contents = NULL;
  73. g_autoptr(GError) err = NULL;
  74. contents = g_strjoinv("\n", keys);
  75. if (!g_file_set_contents(path, contents, -1, &err)) {
  76. error_setg(errp, "failed to write to '%s': %s", path, err->message);
  77. return false;
  78. }
  79. if (chown(path, p->pw_uid, p->pw_gid) == -1) {
  80. error_setg(errp, "failed to set ownership of directory '%s': %s",
  81. path, g_strerror(errno));
  82. return false;
  83. }
  84. if (chmod(path, 0600) == -1) {
  85. error_setg(errp, "failed to set permissions of '%s': %s",
  86. path, g_strerror(errno));
  87. return false;
  88. }
  89. return true;
  90. }
  91. void
  92. qmp_guest_ssh_add_authorized_keys(const char *username, strList *keys,
  93. bool has_reset, bool reset,
  94. Error **errp)
  95. {
  96. g_autofree struct passwd *p = NULL;
  97. g_autofree char *ssh_path = NULL;
  98. g_autofree char *authkeys_path = NULL;
  99. g_auto(GStrv) authkeys = NULL;
  100. strList *k;
  101. size_t nkeys, nauthkeys;
  102. reset = has_reset && reset;
  103. if (!check_openssh_pub_keys(keys, &nkeys, errp)) {
  104. return;
  105. }
  106. p = get_passwd_entry(username, errp);
  107. if (p == NULL) {
  108. return;
  109. }
  110. ssh_path = g_build_filename(p->pw_dir, ".ssh", NULL);
  111. authkeys_path = g_build_filename(ssh_path, "authorized_keys", NULL);
  112. if (!reset) {
  113. authkeys = read_authkeys(authkeys_path, NULL);
  114. }
  115. if (authkeys == NULL) {
  116. if (!g_file_test(ssh_path, G_FILE_TEST_IS_DIR) &&
  117. !mkdir_for_user(ssh_path, p, 0700, errp)) {
  118. return;
  119. }
  120. }
  121. nauthkeys = authkeys ? g_strv_length(authkeys) : 0;
  122. authkeys = g_realloc_n(authkeys, nauthkeys + nkeys + 1, sizeof(char *));
  123. memset(authkeys + nauthkeys, 0, (nkeys + 1) * sizeof(char *));
  124. for (k = keys; k != NULL; k = k->next) {
  125. if (g_strv_contains((const gchar * const *)authkeys, k->value)) {
  126. continue;
  127. }
  128. authkeys[nauthkeys++] = g_strdup(k->value);
  129. }
  130. write_authkeys(authkeys_path, authkeys, p, errp);
  131. }
  132. void
  133. qmp_guest_ssh_remove_authorized_keys(const char *username, strList *keys,
  134. Error **errp)
  135. {
  136. g_autofree struct passwd *p = NULL;
  137. g_autofree char *authkeys_path = NULL;
  138. g_autofree GStrv new_keys = NULL; /* do not own the strings */
  139. g_auto(GStrv) authkeys = NULL;
  140. GStrv a;
  141. size_t nkeys = 0;
  142. if (!check_openssh_pub_keys(keys, NULL, errp)) {
  143. return;
  144. }
  145. p = get_passwd_entry(username, errp);
  146. if (p == NULL) {
  147. return;
  148. }
  149. authkeys_path = g_build_filename(p->pw_dir, ".ssh",
  150. "authorized_keys", NULL);
  151. if (!g_file_test(authkeys_path, G_FILE_TEST_EXISTS)) {
  152. return;
  153. }
  154. authkeys = read_authkeys(authkeys_path, errp);
  155. if (authkeys == NULL) {
  156. return;
  157. }
  158. new_keys = g_new0(char *, g_strv_length(authkeys) + 1);
  159. for (a = authkeys; *a != NULL; a++) {
  160. strList *k;
  161. for (k = keys; k != NULL; k = k->next) {
  162. if (g_str_equal(k->value, *a)) {
  163. break;
  164. }
  165. }
  166. if (k != NULL) {
  167. continue;
  168. }
  169. new_keys[nkeys++] = *a;
  170. }
  171. write_authkeys(authkeys_path, new_keys, p, errp);
  172. }
  173. GuestAuthorizedKeys *
  174. qmp_guest_ssh_get_authorized_keys(const char *username, Error **errp)
  175. {
  176. g_autofree struct passwd *p = NULL;
  177. g_autofree char *authkeys_path = NULL;
  178. g_auto(GStrv) authkeys = NULL;
  179. g_autoptr(GuestAuthorizedKeys) ret = NULL;
  180. int i;
  181. p = get_passwd_entry(username, errp);
  182. if (p == NULL) {
  183. return NULL;
  184. }
  185. authkeys_path = g_build_filename(p->pw_dir, ".ssh",
  186. "authorized_keys", NULL);
  187. authkeys = read_authkeys(authkeys_path, errp);
  188. if (authkeys == NULL) {
  189. return NULL;
  190. }
  191. ret = g_new0(GuestAuthorizedKeys, 1);
  192. for (i = 0; authkeys[i] != NULL; i++) {
  193. g_strstrip(authkeys[i]);
  194. if (!authkeys[i][0] || authkeys[i][0] == '#') {
  195. continue;
  196. }
  197. QAPI_LIST_PREPEND(ret->keys, g_strdup(authkeys[i]));
  198. }
  199. return g_steal_pointer(&ret);
  200. }
  201. #ifdef QGA_BUILD_UNIT_TEST
  202. static const strList test_key2 = {
  203. .value = (char *)"algo key2 comments"
  204. };
  205. static const strList test_key1_2 = {
  206. .value = (char *)"algo key1 comments",
  207. .next = (strList *)&test_key2,
  208. };
  209. static char *
  210. test_get_authorized_keys_path(void)
  211. {
  212. return g_build_filename(g_get_home_dir(), ".ssh", "authorized_keys", NULL);
  213. }
  214. static void
  215. test_authorized_keys_set(const char *contents)
  216. {
  217. g_autoptr(GError) err = NULL;
  218. g_autofree char *path = NULL;
  219. int ret;
  220. path = g_build_filename(g_get_home_dir(), ".ssh", NULL);
  221. ret = g_mkdir_with_parents(path, 0700);
  222. g_assert(ret == 0);
  223. g_free(path);
  224. path = test_get_authorized_keys_path();
  225. g_file_set_contents(path, contents, -1, &err);
  226. g_assert(err == NULL);
  227. }
  228. static void
  229. test_authorized_keys_equal(const char *expected)
  230. {
  231. g_autoptr(GError) err = NULL;
  232. g_autofree char *path = NULL;
  233. g_autofree char *contents = NULL;
  234. path = test_get_authorized_keys_path();
  235. g_file_get_contents(path, &contents, NULL, &err);
  236. g_assert(err == NULL);
  237. g_assert(g_strcmp0(contents, expected) == 0);
  238. }
  239. static void
  240. test_invalid_user(void)
  241. {
  242. Error *err = NULL;
  243. qmp_guest_ssh_add_authorized_keys("", NULL, FALSE, FALSE, &err);
  244. error_free_or_abort(&err);
  245. qmp_guest_ssh_remove_authorized_keys("", NULL, &err);
  246. error_free_or_abort(&err);
  247. }
  248. static void
  249. test_invalid_key(void)
  250. {
  251. strList key = {
  252. .value = (char *)"not a valid\nkey"
  253. };
  254. Error *err = NULL;
  255. qmp_guest_ssh_add_authorized_keys(g_get_user_name(), &key,
  256. FALSE, FALSE, &err);
  257. error_free_or_abort(&err);
  258. qmp_guest_ssh_remove_authorized_keys(g_get_user_name(), &key, &err);
  259. error_free_or_abort(&err);
  260. }
  261. static void
  262. test_add_keys(void)
  263. {
  264. Error *err = NULL;
  265. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  266. (strList *)&test_key2,
  267. FALSE, FALSE,
  268. &err);
  269. g_assert(err == NULL);
  270. test_authorized_keys_equal("algo key2 comments");
  271. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  272. (strList *)&test_key1_2,
  273. FALSE, FALSE,
  274. &err);
  275. g_assert(err == NULL);
  276. /* key2 came first, and shouldn't be duplicated */
  277. test_authorized_keys_equal("algo key2 comments\n"
  278. "algo key1 comments");
  279. }
  280. static void
  281. test_add_reset_keys(void)
  282. {
  283. Error *err = NULL;
  284. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  285. (strList *)&test_key1_2,
  286. FALSE, FALSE,
  287. &err);
  288. g_assert(err == NULL);
  289. /* reset with key2 only */
  290. test_authorized_keys_equal("algo key1 comments\n"
  291. "algo key2 comments");
  292. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  293. (strList *)&test_key2,
  294. TRUE, TRUE,
  295. &err);
  296. g_assert(err == NULL);
  297. test_authorized_keys_equal("algo key2 comments");
  298. /* empty should clear file */
  299. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  300. (strList *)NULL,
  301. TRUE, TRUE,
  302. &err);
  303. g_assert(err == NULL);
  304. test_authorized_keys_equal("");
  305. }
  306. static void
  307. test_remove_keys(void)
  308. {
  309. Error *err = NULL;
  310. static const char *authkeys =
  311. "algo key1 comments\n"
  312. /* originally duplicated */
  313. "algo key1 comments\n"
  314. "# a commented line\n"
  315. "algo some-key another\n";
  316. test_authorized_keys_set(authkeys);
  317. qmp_guest_ssh_remove_authorized_keys(g_get_user_name(),
  318. (strList *)&test_key2, &err);
  319. g_assert(err == NULL);
  320. test_authorized_keys_equal(authkeys);
  321. qmp_guest_ssh_remove_authorized_keys(g_get_user_name(),
  322. (strList *)&test_key1_2, &err);
  323. g_assert(err == NULL);
  324. test_authorized_keys_equal("# a commented line\n"
  325. "algo some-key another\n");
  326. }
  327. static void
  328. test_get_keys(void)
  329. {
  330. Error *err = NULL;
  331. static const char *authkeys =
  332. "algo key1 comments\n"
  333. "# a commented line\n"
  334. "algo some-key another\n";
  335. g_autoptr(GuestAuthorizedKeys) ret = NULL;
  336. strList *k;
  337. size_t len = 0;
  338. test_authorized_keys_set(authkeys);
  339. ret = qmp_guest_ssh_get_authorized_keys(g_get_user_name(), &err);
  340. g_assert(err == NULL);
  341. for (len = 0, k = ret->keys; k != NULL; k = k->next) {
  342. g_assert(g_str_has_prefix(k->value, "algo "));
  343. len++;
  344. }
  345. g_assert(len == 2);
  346. }
  347. int main(int argc, char *argv[])
  348. {
  349. setlocale(LC_ALL, "");
  350. g_test_init(&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
  351. g_test_add_func("/qga/ssh/invalid_user", test_invalid_user);
  352. g_test_add_func("/qga/ssh/invalid_key", test_invalid_key);
  353. g_test_add_func("/qga/ssh/add_keys", test_add_keys);
  354. g_test_add_func("/qga/ssh/add_reset_keys", test_add_reset_keys);
  355. g_test_add_func("/qga/ssh/remove_keys", test_remove_keys);
  356. g_test_add_func("/qga/ssh/get_keys", test_get_keys);
  357. return g_test_run();
  358. }
  359. #endif /* BUILD_UNIT_TEST */