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"
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/qobject-input-visitor.h"
37 qauthz_list_file_is_allowed(QAuthZ
*authz
,
41 QAuthZListFile
*fauthz
= QAUTHZ_LIST_FILE(authz
);
43 return qauthz_is_allowed(fauthz
->list
, identity
, errp
);
51 qauthz_list_file_load(QAuthZListFile
*fauthz
, Error
**errp
)
54 gchar
*content
= NULL
;
61 trace_qauthz_list_file_load(fauthz
, fauthz
->filename
);
62 if (!g_file_get_contents(fauthz
->filename
, &content
, &len
, &err
)) {
63 error_setg(errp
, "Unable to read '%s': %s",
64 fauthz
->filename
, err
->message
);
68 obj
= qobject_from_json(content
, errp
);
73 pdict
= qobject_to(QDict
, obj
);
75 error_setg(errp
, "File '%s' must contain a JSON object",
80 v
= qobject_input_visitor_new(obj
);
82 ret
= (QAuthZ
*)user_creatable_add_type(TYPE_QAUTHZ_LIST
,
83 NULL
, pdict
, v
, errp
);
97 qauthz_list_file_event(int64_t wd G_GNUC_UNUSED
,
98 QFileMonitorEvent ev G_GNUC_UNUSED
,
99 const char *name G_GNUC_UNUSED
,
102 QAuthZListFile
*fauthz
= opaque
;
105 if (ev
!= QFILE_MONITOR_EVENT_MODIFIED
&&
106 ev
!= QFILE_MONITOR_EVENT_CREATED
) {
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);
115 error_report_err(err
);
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");
130 fauthz
->list
= qauthz_list_file_load(fauthz
, errp
);
135 if (!fauthz
->refresh
) {
139 fauthz
->file_monitor
= qemu_file_monitor_new(errp
);
140 if (!fauthz
->file_monitor
) {
144 dir
= g_path_get_dirname(fauthz
->filename
);
145 if (g_str_equal(dir
, ".")) {
146 error_setg(errp
, "Filename must be an absolute path");
149 file
= g_path_get_basename(fauthz
->filename
);
150 if (g_str_equal(file
, ".")) {
151 error_setg(errp
, "Path has no trailing filename component");
155 fauthz
->file_watch
= qemu_file_monitor_add_watch(
156 fauthz
->file_monitor
, dir
, file
,
157 qauthz_list_file_event
, fauthz
, errp
);
158 if (fauthz
->file_watch
< 0) {
169 qauthz_list_file_prop_set_filename(Object
*obj
,
171 Error
**errp G_GNUC_UNUSED
)
173 QAuthZListFile
*fauthz
= QAUTHZ_LIST_FILE(obj
);
175 g_free(fauthz
->filename
);
176 fauthz
->filename
= g_strdup(value
);
181 qauthz_list_file_prop_get_filename(Object
*obj
,
182 Error
**errp G_GNUC_UNUSED
)
184 QAuthZListFile
*fauthz
= QAUTHZ_LIST_FILE(obj
);
186 return g_strdup(fauthz
->filename
);
191 qauthz_list_file_prop_set_refresh(Object
*obj
,
193 Error
**errp G_GNUC_UNUSED
)
195 QAuthZListFile
*fauthz
= QAUTHZ_LIST_FILE(obj
);
197 fauthz
->refresh
= value
;
202 qauthz_list_file_prop_get_refresh(Object
*obj
,
203 Error
**errp G_GNUC_UNUSED
)
205 QAuthZListFile
*fauthz
= QAUTHZ_LIST_FILE(obj
);
207 return fauthz
->refresh
;
212 qauthz_list_file_finalize(Object
*obj
)
214 QAuthZListFile
*fauthz
= QAUTHZ_LIST_FILE(obj
);
216 object_unref(OBJECT(fauthz
->list
));
217 g_free(fauthz
->filename
);
218 qemu_file_monitor_free(fauthz
->file_monitor
);
223 qauthz_list_file_class_init(ObjectClass
*oc
, void *data
)
225 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
226 QAuthZClass
*authz
= QAUTHZ_CLASS(oc
);
228 ucc
->complete
= qauthz_list_file_complete
;
230 object_class_property_add_str(oc
, "filename",
231 qauthz_list_file_prop_get_filename
,
232 qauthz_list_file_prop_set_filename
);
233 object_class_property_add_bool(oc
, "refresh",
234 qauthz_list_file_prop_get_refresh
,
235 qauthz_list_file_prop_set_refresh
);
237 authz
->is_allowed
= qauthz_list_file_is_allowed
;
242 qauthz_list_file_init(Object
*obj
)
244 QAuthZListFile
*authz
= QAUTHZ_LIST_FILE(obj
);
246 authz
->file_watch
= -1;
247 #ifdef CONFIG_INOTIFY1
248 authz
->refresh
= true;
253 QAuthZListFile
*qauthz_list_file_new(const char *id
,
254 const char *filename
,
258 return QAUTHZ_LIST_FILE(
259 object_new_with_props(TYPE_QAUTHZ_LIST_FILE
,
260 object_get_objects_root(),
262 "filename", filename
,
263 "refresh", refresh
? "yes" : "no",
268 static const TypeInfo qauthz_list_file_info
= {
269 .parent
= TYPE_QAUTHZ
,
270 .name
= TYPE_QAUTHZ_LIST_FILE
,
271 .instance_init
= qauthz_list_file_init
,
272 .instance_size
= sizeof(QAuthZListFile
),
273 .instance_finalize
= qauthz_list_file_finalize
,
274 .class_init
= qauthz_list_file_class_init
,
275 .interfaces
= (InterfaceInfo
[]) {
276 { TYPE_USER_CREATABLE
},
283 qauthz_list_file_register_types(void)
285 type_register_static(&qauthz_list_file_info
);
289 type_init(qauthz_list_file_register_types
);