xref: /openbmc/qemu/authz/simple.c (revision 49d755d0)
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 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 
21 #include "qemu/osdep.h"
22 #include "authz/simple.h"
23 #include "trace.h"
24 #include "qom/object_interfaces.h"
25 
26 static bool qauthz_simple_is_allowed(QAuthZ *authz,
27                                      const char *identity,
28                                      Error **errp)
29 {
30     QAuthZSimple *sauthz = QAUTHZ_SIMPLE(authz);
31 
32     trace_qauthz_simple_is_allowed(authz, sauthz->identity, identity);
33     return g_str_equal(identity, sauthz->identity);
34 }
35 
36 static void
37 qauthz_simple_prop_set_identity(Object *obj,
38                                 const char *value,
39                                 Error **errp G_GNUC_UNUSED)
40 {
41     QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj);
42 
43     g_free(sauthz->identity);
44     sauthz->identity = g_strdup(value);
45 }
46 
47 
48 static char *
49 qauthz_simple_prop_get_identity(Object *obj,
50                                 Error **errp G_GNUC_UNUSED)
51 {
52     QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj);
53 
54     return g_strdup(sauthz->identity);
55 }
56 
57 
58 static void
59 qauthz_simple_finalize(Object *obj)
60 {
61     QAuthZSimple *sauthz = QAUTHZ_SIMPLE(obj);
62 
63     g_free(sauthz->identity);
64 }
65 
66 
67 static void
68 qauthz_simple_class_init(ObjectClass *oc, void *data)
69 {
70     QAuthZClass *authz = QAUTHZ_CLASS(oc);
71 
72     authz->is_allowed = qauthz_simple_is_allowed;
73 
74     object_class_property_add_str(oc, "identity",
75                                   qauthz_simple_prop_get_identity,
76                                   qauthz_simple_prop_set_identity,
77                                   NULL);
78 }
79 
80 
81 QAuthZSimple *qauthz_simple_new(const char *id,
82                                 const char *identity,
83                                 Error **errp)
84 {
85     return QAUTHZ_SIMPLE(
86         object_new_with_props(TYPE_QAUTHZ_SIMPLE,
87                               object_get_objects_root(),
88                               id, errp,
89                               "identity", identity,
90                               NULL));
91 }
92 
93 
94 static const TypeInfo qauthz_simple_info = {
95     .parent = TYPE_QAUTHZ,
96     .name = TYPE_QAUTHZ_SIMPLE,
97     .instance_size = sizeof(QAuthZSimple),
98     .instance_finalize = qauthz_simple_finalize,
99     .class_size = sizeof(QAuthZSimpleClass),
100     .class_init = qauthz_simple_class_init,
101     .interfaces = (InterfaceInfo[]) {
102         { TYPE_USER_CREATABLE },
103         { }
104     }
105 };
106 
107 
108 static void
109 qauthz_simple_register_types(void)
110 {
111     type_register_static(&qauthz_simple_info);
112 }
113 
114 
115 type_init(qauthz_simple_register_types);
116