Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 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 |
Chetan Pant | 036a80c | 2020-10-14 13:49:02 +0000 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 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 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include "authz/simple.h" |
Markus Armbruster | 45b1f68 | 2019-03-15 15:51:17 +0100 | [diff] [blame] | 23 | #include "trace.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | #include "qemu/module.h" |
Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 25 | #include "qom/object_interfaces.h" |
| 26 | |
| 27 | static bool qauthz_simple_is_allowed(QAuthZ *authz, |
| 28 | const char *identity, |
| 29 | Error **errp) |
| 30 | { |
| 31 | QAuthZSimple *sauthz = QAUTHZ_SIMPLE(authz); |
| 32 | |
| 33 | trace_qauthz_simple_is_allowed(authz, sauthz->identity, identity); |
| 34 | return g_str_equal(identity, sauthz->identity); |
| 35 | } |
| 36 | |
| 37 | static void |
| 38 | qauthz_simple_prop_set_identity(Object *obj, |
| 39 | const char *value, |
| 40 | Error **errp G_GNUC_UNUSED) |
| 41 | { |
| 42 | QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj); |
| 43 | |
| 44 | g_free(sauthz->identity); |
| 45 | sauthz->identity = g_strdup(value); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static char * |
| 50 | qauthz_simple_prop_get_identity(Object *obj, |
| 51 | Error **errp G_GNUC_UNUSED) |
| 52 | { |
| 53 | QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj); |
| 54 | |
| 55 | return g_strdup(sauthz->identity); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | static void |
| 60 | qauthz_simple_finalize(Object *obj) |
| 61 | { |
| 62 | QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj); |
| 63 | |
| 64 | g_free(sauthz->identity); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static void |
Kevin Wolf | c2aa8a3 | 2020-11-17 17:30:45 +0100 | [diff] [blame] | 69 | qauthz_simple_complete(UserCreatable *uc, Error **errp) |
| 70 | { |
| 71 | QAuthZSimple *sauthz = QAUTHZ_SIMPLE(uc); |
| 72 | |
| 73 | if (!sauthz->identity) { |
| 74 | error_setg(errp, "The 'identity' property must be set"); |
| 75 | return; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | static void |
Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 81 | qauthz_simple_class_init(ObjectClass *oc, void *data) |
| 82 | { |
| 83 | QAuthZClass *authz = QAUTHZ_CLASS(oc); |
Kevin Wolf | c2aa8a3 | 2020-11-17 17:30:45 +0100 | [diff] [blame] | 84 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); |
Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 85 | |
Kevin Wolf | c2aa8a3 | 2020-11-17 17:30:45 +0100 | [diff] [blame] | 86 | ucc->complete = qauthz_simple_complete; |
Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 87 | authz->is_allowed = qauthz_simple_is_allowed; |
| 88 | |
| 89 | object_class_property_add_str(oc, "identity", |
| 90 | qauthz_simple_prop_get_identity, |
Markus Armbruster | d262312 | 2020-05-05 17:29:22 +0200 | [diff] [blame] | 91 | qauthz_simple_prop_set_identity); |
Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
| 95 | QAuthZSimple *qauthz_simple_new(const char *id, |
| 96 | const char *identity, |
| 97 | Error **errp) |
| 98 | { |
| 99 | return QAUTHZ_SIMPLE( |
| 100 | object_new_with_props(TYPE_QAUTHZ_SIMPLE, |
| 101 | object_get_objects_root(), |
| 102 | id, errp, |
| 103 | "identity", identity, |
| 104 | NULL)); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static const TypeInfo qauthz_simple_info = { |
| 109 | .parent = TYPE_QAUTHZ, |
| 110 | .name = TYPE_QAUTHZ_SIMPLE, |
| 111 | .instance_size = sizeof(QAuthZSimple), |
| 112 | .instance_finalize = qauthz_simple_finalize, |
Daniel P. Berrangé | fb5c4eb | 2018-05-02 15:40:33 +0100 | [diff] [blame] | 113 | .class_init = qauthz_simple_class_init, |
| 114 | .interfaces = (InterfaceInfo[]) { |
| 115 | { TYPE_USER_CREATABLE }, |
| 116 | { } |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | static void |
| 122 | qauthz_simple_register_types(void) |
| 123 | { |
| 124 | type_register_static(&qauthz_simple_info); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | type_init(qauthz_simple_register_types); |