base.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * QEMU authorization framework base class
  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/base.h"
  22. #include "qemu/module.h"
  23. #include "trace.h"
  24. bool qauthz_is_allowed(QAuthZ *authz,
  25. const char *identity,
  26. Error **errp)
  27. {
  28. QAuthZClass *cls = QAUTHZ_GET_CLASS(authz);
  29. bool allowed;
  30. allowed = cls->is_allowed(authz, identity, errp);
  31. trace_qauthz_is_allowed(authz, identity, allowed);
  32. return allowed;
  33. }
  34. bool qauthz_is_allowed_by_id(const char *authzid,
  35. const char *identity,
  36. Error **errp)
  37. {
  38. QAuthZ *authz;
  39. Object *obj;
  40. Object *container;
  41. container = object_get_objects_root();
  42. obj = object_resolve_path_component(container,
  43. authzid);
  44. if (!obj) {
  45. error_setg(errp, "Cannot find QAuthZ object ID %s",
  46. authzid);
  47. return false;
  48. }
  49. if (!object_dynamic_cast(obj, TYPE_QAUTHZ)) {
  50. error_setg(errp, "Object '%s' is not a QAuthZ subclass",
  51. authzid);
  52. return false;
  53. }
  54. authz = QAUTHZ(obj);
  55. return qauthz_is_allowed(authz, identity, errp);
  56. }
  57. static const TypeInfo authz_info = {
  58. .parent = TYPE_OBJECT,
  59. .name = TYPE_QAUTHZ,
  60. .instance_size = sizeof(QAuthZ),
  61. .class_size = sizeof(QAuthZClass),
  62. .abstract = true,
  63. };
  64. static void qauthz_register_types(void)
  65. {
  66. type_register_static(&authz_info);
  67. }
  68. type_init(qauthz_register_types)