listfile.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * QEMU access control list file 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.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/listfile.h"
  22. #include "trace.h"
  23. #include "qemu/error-report.h"
  24. #include "qemu/main-loop.h"
  25. #include "qemu/module.h"
  26. #include "qemu/sockets.h"
  27. #include "qemu/filemonitor.h"
  28. #include "qom/object_interfaces.h"
  29. #include "qapi/qapi-visit-authz.h"
  30. #include "qapi/qmp/qjson.h"
  31. #include "qapi/qmp/qobject.h"
  32. #include "qapi/qmp/qerror.h"
  33. #include "qapi/qobject-input-visitor.h"
  34. static bool
  35. qauthz_list_file_is_allowed(QAuthZ *authz,
  36. const char *identity,
  37. Error **errp)
  38. {
  39. QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(authz);
  40. if (fauthz->list) {
  41. return qauthz_is_allowed(fauthz->list, identity, errp);
  42. }
  43. return false;
  44. }
  45. static QAuthZ *
  46. qauthz_list_file_load(QAuthZListFile *fauthz, Error **errp)
  47. {
  48. GError *err = NULL;
  49. gchar *content = NULL;
  50. gsize len;
  51. QObject *obj = NULL;
  52. QDict *pdict;
  53. Visitor *v = NULL;
  54. QAuthZ *ret = NULL;
  55. trace_qauthz_list_file_load(fauthz, fauthz->filename);
  56. if (!g_file_get_contents(fauthz->filename, &content, &len, &err)) {
  57. error_setg(errp, "Unable to read '%s': %s",
  58. fauthz->filename, err->message);
  59. goto cleanup;
  60. }
  61. obj = qobject_from_json(content, errp);
  62. if (!obj) {
  63. goto cleanup;
  64. }
  65. pdict = qobject_to(QDict, obj);
  66. if (!pdict) {
  67. error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "obj", "dict");
  68. goto cleanup;
  69. }
  70. v = qobject_input_visitor_new(obj);
  71. ret = (QAuthZ *)user_creatable_add_type(TYPE_QAUTHZ_LIST,
  72. NULL, pdict, v, errp);
  73. cleanup:
  74. visit_free(v);
  75. qobject_unref(obj);
  76. if (err) {
  77. g_error_free(err);
  78. }
  79. g_free(content);
  80. return ret;
  81. }
  82. static void
  83. qauthz_list_file_event(int64_t wd G_GNUC_UNUSED,
  84. QFileMonitorEvent ev G_GNUC_UNUSED,
  85. const char *name G_GNUC_UNUSED,
  86. void *opaque)
  87. {
  88. QAuthZListFile *fauthz = opaque;
  89. Error *err = NULL;
  90. if (ev != QFILE_MONITOR_EVENT_MODIFIED &&
  91. ev != QFILE_MONITOR_EVENT_CREATED) {
  92. return;
  93. }
  94. object_unref(OBJECT(fauthz->list));
  95. fauthz->list = qauthz_list_file_load(fauthz, &err);
  96. trace_qauthz_list_file_refresh(fauthz,
  97. fauthz->filename, fauthz->list ? 1 : 0);
  98. if (!fauthz->list) {
  99. error_report_err(err);
  100. }
  101. }
  102. static void
  103. qauthz_list_file_complete(UserCreatable *uc, Error **errp)
  104. {
  105. QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(uc);
  106. gchar *dir = NULL, *file = NULL;
  107. if (!fauthz->filename) {
  108. error_setg(errp, "filename not provided");
  109. return;
  110. }
  111. fauthz->list = qauthz_list_file_load(fauthz, errp);
  112. if (!fauthz->refresh) {
  113. return;
  114. }
  115. fauthz->file_monitor = qemu_file_monitor_new(errp);
  116. if (!fauthz->file_monitor) {
  117. return;
  118. }
  119. dir = g_path_get_dirname(fauthz->filename);
  120. if (g_str_equal(dir, ".")) {
  121. error_setg(errp, "Filename must be an absolute path");
  122. goto cleanup;
  123. }
  124. file = g_path_get_basename(fauthz->filename);
  125. if (g_str_equal(file, ".")) {
  126. error_setg(errp, "Path has no trailing filename component");
  127. goto cleanup;
  128. }
  129. fauthz->file_watch = qemu_file_monitor_add_watch(
  130. fauthz->file_monitor, dir, file,
  131. qauthz_list_file_event, fauthz, errp);
  132. if (fauthz->file_watch < 0) {
  133. goto cleanup;
  134. }
  135. cleanup:
  136. g_free(file);
  137. g_free(dir);
  138. }
  139. static void
  140. qauthz_list_file_prop_set_filename(Object *obj,
  141. const char *value,
  142. Error **errp G_GNUC_UNUSED)
  143. {
  144. QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
  145. g_free(fauthz->filename);
  146. fauthz->filename = g_strdup(value);
  147. }
  148. static char *
  149. qauthz_list_file_prop_get_filename(Object *obj,
  150. Error **errp G_GNUC_UNUSED)
  151. {
  152. QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
  153. return g_strdup(fauthz->filename);
  154. }
  155. static void
  156. qauthz_list_file_prop_set_refresh(Object *obj,
  157. bool value,
  158. Error **errp G_GNUC_UNUSED)
  159. {
  160. QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
  161. fauthz->refresh = value;
  162. }
  163. static bool
  164. qauthz_list_file_prop_get_refresh(Object *obj,
  165. Error **errp G_GNUC_UNUSED)
  166. {
  167. QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
  168. return fauthz->refresh;
  169. }
  170. static void
  171. qauthz_list_file_finalize(Object *obj)
  172. {
  173. QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(obj);
  174. object_unref(OBJECT(fauthz->list));
  175. g_free(fauthz->filename);
  176. qemu_file_monitor_free(fauthz->file_monitor);
  177. }
  178. static void
  179. qauthz_list_file_class_init(ObjectClass *oc, void *data)
  180. {
  181. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  182. QAuthZClass *authz = QAUTHZ_CLASS(oc);
  183. ucc->complete = qauthz_list_file_complete;
  184. object_class_property_add_str(oc, "filename",
  185. qauthz_list_file_prop_get_filename,
  186. qauthz_list_file_prop_set_filename);
  187. object_class_property_add_bool(oc, "refresh",
  188. qauthz_list_file_prop_get_refresh,
  189. qauthz_list_file_prop_set_refresh);
  190. authz->is_allowed = qauthz_list_file_is_allowed;
  191. }
  192. static void
  193. qauthz_list_file_init(Object *obj)
  194. {
  195. QAuthZListFile *authz = QAUTHZ_LIST_FILE(obj);
  196. authz->file_watch = -1;
  197. #ifdef CONFIG_INOTIFY1
  198. authz->refresh = true;
  199. #endif
  200. }
  201. QAuthZListFile *qauthz_list_file_new(const char *id,
  202. const char *filename,
  203. bool refresh,
  204. Error **errp)
  205. {
  206. return QAUTHZ_LIST_FILE(
  207. object_new_with_props(TYPE_QAUTHZ_LIST_FILE,
  208. object_get_objects_root(),
  209. id, errp,
  210. "filename", filename,
  211. "refresh", refresh ? "yes" : "no",
  212. NULL));
  213. }
  214. static const TypeInfo qauthz_list_file_info = {
  215. .parent = TYPE_QAUTHZ,
  216. .name = TYPE_QAUTHZ_LIST_FILE,
  217. .instance_init = qauthz_list_file_init,
  218. .instance_size = sizeof(QAuthZListFile),
  219. .instance_finalize = qauthz_list_file_finalize,
  220. .class_init = qauthz_list_file_class_init,
  221. .interfaces = (InterfaceInfo[]) {
  222. { TYPE_USER_CREATABLE },
  223. { }
  224. }
  225. };
  226. static void
  227. qauthz_list_file_register_types(void)
  228. {
  229. type_register_static(&qauthz_list_file_info);
  230. }
  231. type_init(qauthz_list_file_register_types);