2
0

test-authz-pam.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * QEMU PAM authorization object tests
  3. *
  4. * Copyright (c) 2018 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/module.h"
  23. #include "authz/pamacct.h"
  24. #include <security/pam_appl.h>
  25. static bool failauth;
  26. /*
  27. * These two functions are exported by libpam.so.
  28. *
  29. * By defining them again here, our impls are resolved
  30. * by the linker instead of those in libpam.so
  31. *
  32. * The test suite is thus isolated from the host system
  33. * PAM setup, so we can do predictable test scenarios
  34. */
  35. int
  36. pam_start(const char *service_name, const char *user,
  37. const struct pam_conv *pam_conversation,
  38. pam_handle_t **pamh)
  39. {
  40. failauth = true;
  41. if (!g_str_equal(service_name, "qemu-vnc")) {
  42. return PAM_AUTH_ERR;
  43. }
  44. if (g_str_equal(user, "fred")) {
  45. failauth = false;
  46. }
  47. return PAM_SUCCESS;
  48. }
  49. int
  50. pam_acct_mgmt(pam_handle_t *pamh, int flags)
  51. {
  52. if (failauth) {
  53. return PAM_AUTH_ERR;
  54. }
  55. return PAM_SUCCESS;
  56. }
  57. static void test_authz_unknown_service(void)
  58. {
  59. Error *local_err = NULL;
  60. QAuthZPAM *auth = qauthz_pam_new("auth0",
  61. "qemu-does-not-exist",
  62. &error_abort);
  63. g_assert_nonnull(auth);
  64. g_assert_false(qauthz_is_allowed(QAUTHZ(auth), "fred", &local_err));
  65. error_free_or_abort(&local_err);
  66. object_unparent(OBJECT(auth));
  67. }
  68. static void test_authz_good_user(void)
  69. {
  70. QAuthZPAM *auth = qauthz_pam_new("auth0",
  71. "qemu-vnc",
  72. &error_abort);
  73. g_assert_nonnull(auth);
  74. g_assert_true(qauthz_is_allowed(QAUTHZ(auth), "fred", &error_abort));
  75. object_unparent(OBJECT(auth));
  76. }
  77. static void test_authz_bad_user(void)
  78. {
  79. Error *local_err = NULL;
  80. QAuthZPAM *auth = qauthz_pam_new("auth0",
  81. "qemu-vnc",
  82. &error_abort);
  83. g_assert_nonnull(auth);
  84. g_assert_false(qauthz_is_allowed(QAUTHZ(auth), "bob", &local_err));
  85. error_free_or_abort(&local_err);
  86. object_unparent(OBJECT(auth));
  87. }
  88. int main(int argc, char **argv)
  89. {
  90. g_test_init(&argc, &argv, NULL);
  91. module_call_init(MODULE_INIT_QOM);
  92. g_test_add_func("/auth/pam/unknown-service", test_authz_unknown_service);
  93. g_test_add_func("/auth/pam/good-user", test_authz_good_user);
  94. g_test_add_func("/auth/pam/bad-user", test_authz_bad_user);
  95. return g_test_run();
  96. }