simple.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * QEMU simple 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/simple.h"
  22. #include "trace.h"
  23. #include "qemu/module.h"
  24. #include "qom/object_interfaces.h"
  25. static bool qauthz_simple_is_allowed(QAuthZ *authz,
  26. const char *identity,
  27. Error **errp)
  28. {
  29. QAuthZSimple *sauthz = QAUTHZ_SIMPLE(authz);
  30. trace_qauthz_simple_is_allowed(authz, sauthz->identity, identity);
  31. return g_str_equal(identity, sauthz->identity);
  32. }
  33. static void
  34. qauthz_simple_prop_set_identity(Object *obj,
  35. const char *value,
  36. Error **errp G_GNUC_UNUSED)
  37. {
  38. QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj);
  39. g_free(sauthz->identity);
  40. sauthz->identity = g_strdup(value);
  41. }
  42. static char *
  43. qauthz_simple_prop_get_identity(Object *obj,
  44. Error **errp G_GNUC_UNUSED)
  45. {
  46. QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj);
  47. return g_strdup(sauthz->identity);
  48. }
  49. static void
  50. qauthz_simple_finalize(Object *obj)
  51. {
  52. QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj);
  53. g_free(sauthz->identity);
  54. }
  55. static void
  56. qauthz_simple_complete(UserCreatable *uc, Error **errp)
  57. {
  58. QAuthZSimple *sauthz = QAUTHZ_SIMPLE(uc);
  59. if (!sauthz->identity) {
  60. error_setg(errp, "The 'identity' property must be set");
  61. return;
  62. }
  63. }
  64. static void
  65. qauthz_simple_class_init(ObjectClass *oc, void *data)
  66. {
  67. QAuthZClass *authz = QAUTHZ_CLASS(oc);
  68. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  69. ucc->complete = qauthz_simple_complete;
  70. authz->is_allowed = qauthz_simple_is_allowed;
  71. object_class_property_add_str(oc, "identity",
  72. qauthz_simple_prop_get_identity,
  73. qauthz_simple_prop_set_identity);
  74. }
  75. QAuthZSimple *qauthz_simple_new(const char *id,
  76. const char *identity,
  77. Error **errp)
  78. {
  79. return QAUTHZ_SIMPLE(
  80. object_new_with_props(TYPE_QAUTHZ_SIMPLE,
  81. object_get_objects_root(),
  82. id, errp,
  83. "identity", identity,
  84. NULL));
  85. }
  86. static const TypeInfo qauthz_simple_info = {
  87. .parent = TYPE_QAUTHZ,
  88. .name = TYPE_QAUTHZ_SIMPLE,
  89. .instance_size = sizeof(QAuthZSimple),
  90. .instance_finalize = qauthz_simple_finalize,
  91. .class_init = qauthz_simple_class_init,
  92. .interfaces = (InterfaceInfo[]) {
  93. { TYPE_USER_CREATABLE },
  94. { }
  95. }
  96. };
  97. static void
  98. qauthz_simple_register_types(void)
  99. {
  100. type_register_static(&qauthz_simple_info);
  101. }
  102. type_init(qauthz_simple_register_types);