pamacct.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * QEMU PAM authorization driver
  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.1 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 "authz/pamacct.h"
  22. #include "trace.h"
  23. #include "qemu/module.h"
  24. #include "qom/object_interfaces.h"
  25. #include <security/pam_appl.h>
  26. static bool qauthz_pam_is_allowed(QAuthZ *authz,
  27. const char *identity,
  28. Error **errp)
  29. {
  30. QAuthZPAM *pauthz = QAUTHZ_PAM(authz);
  31. const struct pam_conv pam_conversation = { 0 };
  32. pam_handle_t *pamh = NULL;
  33. int ret;
  34. trace_qauthz_pam_check(authz, identity, pauthz->service);
  35. ret = pam_start(pauthz->service,
  36. identity,
  37. &pam_conversation,
  38. &pamh);
  39. if (ret != PAM_SUCCESS) {
  40. error_setg(errp, "Unable to start PAM transaction: %s",
  41. pam_strerror(NULL, ret));
  42. return false;
  43. }
  44. ret = pam_acct_mgmt(pamh, PAM_SILENT);
  45. pam_end(pamh, ret);
  46. if (ret != PAM_SUCCESS) {
  47. error_setg(errp, "Unable to authorize user '%s': %s",
  48. identity, pam_strerror(pamh, ret));
  49. return false;
  50. }
  51. return true;
  52. }
  53. static void
  54. qauthz_pam_prop_set_service(Object *obj,
  55. const char *service,
  56. Error **errp G_GNUC_UNUSED)
  57. {
  58. QAuthZPAM *pauthz = QAUTHZ_PAM(obj);
  59. g_free(pauthz->service);
  60. pauthz->service = g_strdup(service);
  61. }
  62. static char *
  63. qauthz_pam_prop_get_service(Object *obj,
  64. Error **errp G_GNUC_UNUSED)
  65. {
  66. QAuthZPAM *pauthz = QAUTHZ_PAM(obj);
  67. return g_strdup(pauthz->service);
  68. }
  69. static void
  70. qauthz_pam_complete(UserCreatable *uc, Error **errp)
  71. {
  72. QAuthZPAM *pauthz = QAUTHZ_PAM(uc);
  73. if (!pauthz->service) {
  74. error_setg(errp, "The 'service' property must be set");
  75. return;
  76. }
  77. }
  78. static void
  79. qauthz_pam_finalize(Object *obj)
  80. {
  81. QAuthZPAM *pauthz = QAUTHZ_PAM(obj);
  82. g_free(pauthz->service);
  83. }
  84. static void
  85. qauthz_pam_class_init(ObjectClass *oc, void *data)
  86. {
  87. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  88. QAuthZClass *authz = QAUTHZ_CLASS(oc);
  89. ucc->complete = qauthz_pam_complete;
  90. authz->is_allowed = qauthz_pam_is_allowed;
  91. object_class_property_add_str(oc, "service",
  92. qauthz_pam_prop_get_service,
  93. qauthz_pam_prop_set_service);
  94. }
  95. QAuthZPAM *qauthz_pam_new(const char *id,
  96. const char *service,
  97. Error **errp)
  98. {
  99. return QAUTHZ_PAM(
  100. object_new_with_props(TYPE_QAUTHZ_PAM,
  101. object_get_objects_root(),
  102. id, errp,
  103. "service", service,
  104. NULL));
  105. }
  106. static const TypeInfo qauthz_pam_info = {
  107. .parent = TYPE_QAUTHZ,
  108. .name = TYPE_QAUTHZ_PAM,
  109. .instance_size = sizeof(QAuthZPAM),
  110. .instance_finalize = qauthz_pam_finalize,
  111. .class_init = qauthz_pam_class_init,
  112. .interfaces = (InterfaceInfo[]) {
  113. { TYPE_USER_CREATABLE },
  114. { }
  115. }
  116. };
  117. static void
  118. qauthz_pam_register_types(void)
  119. {
  120. type_register_static(&qauthz_pam_info);
  121. }
  122. type_init(qauthz_pam_register_types);