commands-posix-ssh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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_qemu(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_qemu(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. #if GLIB_CHECK_VERSION(2, 60, 0)
  203. static const strList test_key2 = {
  204. .value = (char *)"algo key2 comments"
  205. };
  206. static const strList test_key1_2 = {
  207. .value = (char *)"algo key1 comments",
  208. .next = (strList *)&test_key2,
  209. };
  210. static char *
  211. test_get_authorized_keys_path(void)
  212. {
  213. return g_build_filename(g_get_home_dir(), ".ssh", "authorized_keys", NULL);
  214. }
  215. static void
  216. test_authorized_keys_set(const char *contents)
  217. {
  218. g_autoptr(GError) err = NULL;
  219. g_autofree char *path = NULL;
  220. int ret;
  221. path = g_build_filename(g_get_home_dir(), ".ssh", NULL);
  222. ret = g_mkdir_with_parents(path, 0700);
  223. g_assert(ret == 0);
  224. g_free(path);
  225. path = test_get_authorized_keys_path();
  226. g_file_set_contents(path, contents, -1, &err);
  227. g_assert(err == NULL);
  228. }
  229. static void
  230. test_authorized_keys_equal(const char *expected)
  231. {
  232. g_autoptr(GError) err = NULL;
  233. g_autofree char *path = NULL;
  234. g_autofree char *contents = NULL;
  235. path = test_get_authorized_keys_path();
  236. g_file_get_contents(path, &contents, NULL, &err);
  237. g_assert(err == NULL);
  238. g_assert(g_strcmp0(contents, expected) == 0);
  239. }
  240. static void
  241. test_invalid_user(void)
  242. {
  243. Error *err = NULL;
  244. qmp_guest_ssh_add_authorized_keys("", NULL, FALSE, FALSE, &err);
  245. error_free_or_abort(&err);
  246. qmp_guest_ssh_remove_authorized_keys("", NULL, &err);
  247. error_free_or_abort(&err);
  248. }
  249. static void
  250. test_invalid_key(void)
  251. {
  252. strList key = {
  253. .value = (char *)"not a valid\nkey"
  254. };
  255. Error *err = NULL;
  256. qmp_guest_ssh_add_authorized_keys(g_get_user_name(), &key,
  257. FALSE, FALSE, &err);
  258. error_free_or_abort(&err);
  259. qmp_guest_ssh_remove_authorized_keys(g_get_user_name(), &key, &err);
  260. error_free_or_abort(&err);
  261. }
  262. static void
  263. test_add_keys(void)
  264. {
  265. Error *err = NULL;
  266. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  267. (strList *)&test_key2,
  268. FALSE, FALSE,
  269. &err);
  270. g_assert(err == NULL);
  271. test_authorized_keys_equal("algo key2 comments");
  272. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  273. (strList *)&test_key1_2,
  274. FALSE, FALSE,
  275. &err);
  276. g_assert(err == NULL);
  277. /* key2 came first, and shouldn't be duplicated */
  278. test_authorized_keys_equal("algo key2 comments\n"
  279. "algo key1 comments");
  280. }
  281. static void
  282. test_add_reset_keys(void)
  283. {
  284. Error *err = NULL;
  285. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  286. (strList *)&test_key1_2,
  287. FALSE, FALSE,
  288. &err);
  289. g_assert(err == NULL);
  290. /* reset with key2 only */
  291. test_authorized_keys_equal("algo key1 comments\n"
  292. "algo key2 comments");
  293. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  294. (strList *)&test_key2,
  295. TRUE, TRUE,
  296. &err);
  297. g_assert(err == NULL);
  298. test_authorized_keys_equal("algo key2 comments");
  299. /* empty should clear file */
  300. qmp_guest_ssh_add_authorized_keys(g_get_user_name(),
  301. (strList *)NULL,
  302. TRUE, TRUE,
  303. &err);
  304. g_assert(err == NULL);
  305. test_authorized_keys_equal("");
  306. }
  307. static void
  308. test_remove_keys(void)
  309. {
  310. Error *err = NULL;
  311. static const char *authkeys =
  312. "algo key1 comments\n"
  313. /* originally duplicated */
  314. "algo key1 comments\n"
  315. "# a commented line\n"
  316. "algo some-key another\n";
  317. test_authorized_keys_set(authkeys);
  318. qmp_guest_ssh_remove_authorized_keys(g_get_user_name(),
  319. (strList *)&test_key2, &err);
  320. g_assert(err == NULL);
  321. test_authorized_keys_equal(authkeys);
  322. qmp_guest_ssh_remove_authorized_keys(g_get_user_name(),
  323. (strList *)&test_key1_2, &err);
  324. g_assert(err == NULL);
  325. test_authorized_keys_equal("# a commented line\n"
  326. "algo some-key another\n");
  327. }
  328. static void
  329. test_get_keys(void)
  330. {
  331. Error *err = NULL;
  332. static const char *authkeys =
  333. "algo key1 comments\n"
  334. "# a commented line\n"
  335. "algo some-key another\n";
  336. g_autoptr(GuestAuthorizedKeys) ret = NULL;
  337. strList *k;
  338. size_t len = 0;
  339. test_authorized_keys_set(authkeys);
  340. ret = qmp_guest_ssh_get_authorized_keys(g_get_user_name(), &err);
  341. g_assert(err == NULL);
  342. for (len = 0, k = ret->keys; k != NULL; k = k->next) {
  343. g_assert(g_str_has_prefix(k->value, "algo "));
  344. len++;
  345. }
  346. g_assert(len == 2);
  347. }
  348. int main(int argc, char *argv[])
  349. {
  350. setlocale(LC_ALL, "");
  351. g_test_init(&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
  352. g_test_add_func("/qga/ssh/invalid_user", test_invalid_user);
  353. g_test_add_func("/qga/ssh/invalid_key", test_invalid_key);
  354. g_test_add_func("/qga/ssh/add_keys", test_add_keys);
  355. g_test_add_func("/qga/ssh/add_reset_keys", test_add_reset_keys);
  356. g_test_add_func("/qga/ssh/remove_keys", test_remove_keys);
  357. g_test_add_func("/qga/ssh/get_keys", test_get_keys);
  358. return g_test_run();
  359. }
  360. #else
  361. int main(int argc, char *argv[])
  362. {
  363. g_test_message("test skipped, needs glib >= 2.60");
  364. return 0;
  365. }
  366. #endif /* GLIB_2_60 */
  367. #endif /* BUILD_UNIT_TEST */