qga: Rename guest-get-devices return member 'address' to 'id'
[qemu/ar7.git] / authz / listfile.c
blob24feac35abe86fea2d016a412a1eddf9cc24a12d
1 /*
2 * QEMU access control list file authorization driver
4 * Copyright (c) 2018 Red Hat, Inc.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "authz/listfile.h"
23 #include "trace.h"
24 #include "qemu/error-report.h"
25 #include "qemu/main-loop.h"
26 #include "qemu/module.h"
27 #include "qemu/sockets.h"
28 #include "qemu/filemonitor.h"
29 #include "qom/object_interfaces.h"
30 #include "qapi/qapi-visit-authz.h"
31 #include "qapi/qmp/qjson.h"
32 #include "qapi/qmp/qobject.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/qobject-input-visitor.h"
37 static bool
38 qauthz_list_file_is_allowed(QAuthZ *authz,
39 const char *identity,
40 Error **errp)
42 QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(authz);
43 if (fauthz->list) {
44 return qauthz_is_allowed(fauthz->list, identity, errp);
47 return false;
51 static QAuthZ *
52 qauthz_list_file_load(QAuthZListFile *fauthz, Error **errp)
54 GError *err = NULL;
55 gchar *content = NULL;
56 gsize len;
57 QObject *obj = NULL;
58 QDict *pdict;
59 Visitor *v = NULL;
60 QAuthZ *ret = NULL;
62 trace_qauthz_list_file_load(fauthz, fauthz->filename);
63 if (!g_file_get_contents(fauthz->filename, &content, &len, &err)) {
64 error_setg(errp, "Unable to read '%s': %s",
65 fauthz->filename, err->message);
66 goto cleanup;
69 obj = qobject_from_json(content, errp);
70 if (!obj) {
71 goto cleanup;
74 pdict = qobject_to(QDict, obj);
75 if (!pdict) {
76 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "obj", "dict");
77 goto cleanup;
80 v = qobject_input_visitor_new(obj);
82 ret = (QAuthZ *)user_creatable_add_type(TYPE_QAUTHZ_LIST,
83 NULL, pdict, v, errp);
85 cleanup:
86 visit_free(v);
87 qobject_unref(obj);
88 if (err) {
89 g_error_free(err);
91 g_free(content);
92 return ret;
96 static void
97 qauthz_list_file_event(int64_t wd G_GNUC_UNUSED,
98 QFileMonitorEvent ev G_GNUC_UNUSED,
99 const char *name G_GNUC_UNUSED,
100 void *opaque)
102 QAuthZListFile *fauthz = opaque;
103 Error *err = NULL;
105 if (ev != QFILE_MONITOR_EVENT_MODIFIED &&
106 ev != QFILE_MONITOR_EVENT_CREATED) {
107 return;
110 object_unref(OBJECT(fauthz->list));
111 fauthz->list = qauthz_list_file_load(fauthz, &err);
112 trace_qauthz_list_file_refresh(fauthz,
113 fauthz->filename, fauthz->list ? 1 : 0);
114 if (!fauthz->list) {
115 error_report_err(err);
119 static void
120 qauthz_list_file_complete(UserCreatable *uc, Error **errp)
122 QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(uc);
123 gchar *dir = NULL, *file = NULL;
125 if (!fauthz->filename) {
126 error_setg(errp, "filename not provided");
127 return;
130 fauthz->list = qauthz_list_file_load(fauthz, errp);
132 if (!fauthz->refresh) {
133 return;
136 fauthz->file_monitor = qemu_file_monitor_new(errp);
137 if (!fauthz->file_monitor) {
138 return;
141 dir = g_path_get_dirname(fauthz->filename);
142 if (g_str_equal(dir, ".")) {
143 error_setg(errp, "Filename must be an absolute path");
144 goto cleanup;
146 file = g_path_get_basename(fauthz->filename);
147 if (g_str_equal(file, ".")) {
148 error_setg(errp, "Path has no trailing filename component");
149 goto cleanup;
152 fauthz->file_watch = qemu_file_monitor_add_watch(
153 fauthz->file_monitor, dir, file,
154 qauthz_list_file_event, fauthz, errp);
155 if (fauthz->file_watch < 0) {
156 goto cleanup;
159 cleanup:
160 g_free(file);
161 g_free(dir);
165 static void
166 qauthz_list_file_prop_set_filename(Object *obj,
167 const char *value,
168 Error **errp G_GNUC_UNUSED)
170 QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
172 g_free(fauthz->filename);
173 fauthz->filename = g_strdup(value);
177 static char *
178 qauthz_list_file_prop_get_filename(Object *obj,
179 Error **errp G_GNUC_UNUSED)
181 QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
183 return g_strdup(fauthz->filename);
187 static void
188 qauthz_list_file_prop_set_refresh(Object *obj,
189 bool value,
190 Error **errp G_GNUC_UNUSED)
192 QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
194 fauthz->refresh = value;
198 static bool
199 qauthz_list_file_prop_get_refresh(Object *obj,
200 Error **errp G_GNUC_UNUSED)
202 QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
204 return fauthz->refresh;
208 static void
209 qauthz_list_file_finalize(Object *obj)
211 QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
213 object_unref(OBJECT(fauthz->list));
214 g_free(fauthz->filename);
215 qemu_file_monitor_free(fauthz->file_monitor);
219 static void
220 qauthz_list_file_class_init(ObjectClass *oc, void *data)
222 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
223 QAuthZClass *authz = QAUTHZ_CLASS(oc);
225 ucc->complete = qauthz_list_file_complete;
227 object_class_property_add_str(oc, "filename",
228 qauthz_list_file_prop_get_filename,
229 qauthz_list_file_prop_set_filename);
230 object_class_property_add_bool(oc, "refresh",
231 qauthz_list_file_prop_get_refresh,
232 qauthz_list_file_prop_set_refresh);
234 authz->is_allowed = qauthz_list_file_is_allowed;
238 static void
239 qauthz_list_file_init(Object *obj)
241 QAuthZListFile *authz = QAUTHZ_LIST_FILE(obj);
243 authz->file_watch = -1;
244 #ifdef CONFIG_INOTIFY1
245 authz->refresh = true;
246 #endif
250 QAuthZListFile *qauthz_list_file_new(const char *id,
251 const char *filename,
252 bool refresh,
253 Error **errp)
255 return QAUTHZ_LIST_FILE(
256 object_new_with_props(TYPE_QAUTHZ_LIST_FILE,
257 object_get_objects_root(),
258 id, errp,
259 "filename", filename,
260 "refresh", refresh ? "yes" : "no",
261 NULL));
265 static const TypeInfo qauthz_list_file_info = {
266 .parent = TYPE_QAUTHZ,
267 .name = TYPE_QAUTHZ_LIST_FILE,
268 .instance_init = qauthz_list_file_init,
269 .instance_size = sizeof(QAuthZListFile),
270 .instance_finalize = qauthz_list_file_finalize,
271 .class_init = qauthz_list_file_class_init,
272 .interfaces = (InterfaceInfo[]) {
273 { TYPE_USER_CREATABLE },
279 static void
280 qauthz_list_file_register_types(void)
282 type_register_static(&qauthz_list_file_info);
286 type_init(qauthz_list_file_register_types);