blob: c105addadcbad8b6c9a6aab66e45273a8777e18d [file] [log] [blame]
aliguori76655d62009-03-06 20:27:37 +00001/*
2 * QEMU access control list management
3 *
4 * Copyright (C) 2009 Red Hat, Inc
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25
Peter Maydellaafd7582016-01-29 17:49:55 +000026#include "qemu/osdep.h"
aliguori76655d62009-03-06 20:27:37 +000027#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/acl.h"
aliguori76655d62009-03-06 20:27:37 +000029
Juan Quintela56ffaf22009-07-27 16:13:00 +020030#ifdef CONFIG_FNMATCH
aliguori76655d62009-03-06 20:27:37 +000031#include <fnmatch.h>
32#endif
33
34
35static unsigned int nacls = 0;
36static qemu_acl **acls = NULL;
37
38
39
40qemu_acl *qemu_acl_find(const char *aclname)
41{
42 int i;
43 for (i = 0 ; i < nacls ; i++) {
aliguori28a76be2009-03-06 20:27:40 +000044 if (strcmp(acls[i]->aclname, aclname) == 0)
45 return acls[i];
aliguori76655d62009-03-06 20:27:37 +000046 }
47
48 return NULL;
49}
50
51qemu_acl *qemu_acl_init(const char *aclname)
52{
53 qemu_acl *acl;
54
55 acl = qemu_acl_find(aclname);
56 if (acl)
aliguori28a76be2009-03-06 20:27:40 +000057 return acl;
aliguori76655d62009-03-06 20:27:37 +000058
Anthony Liguori7267c092011-08-20 22:09:37 -050059 acl = g_malloc(sizeof(*acl));
60 acl->aclname = g_strdup(aclname);
aliguori76655d62009-03-06 20:27:37 +000061 /* Deny by default, so there is no window of "open
62 * access" between QEMU starting, and the user setting
63 * up ACLs in the monitor */
64 acl->defaultDeny = 1;
65
66 acl->nentries = 0;
Blue Swirl72cf2d42009-09-12 07:36:22 +000067 QTAILQ_INIT(&acl->entries);
aliguori76655d62009-03-06 20:27:37 +000068
Anthony Liguori7267c092011-08-20 22:09:37 -050069 acls = g_realloc(acls, sizeof(*acls) * (nacls +1));
aliguori76655d62009-03-06 20:27:37 +000070 acls[nacls] = acl;
71 nacls++;
72
73 return acl;
74}
75
76int qemu_acl_party_is_allowed(qemu_acl *acl,
aliguori28a76be2009-03-06 20:27:40 +000077 const char *party)
aliguori76655d62009-03-06 20:27:37 +000078{
79 qemu_acl_entry *entry;
80
Blue Swirl72cf2d42009-09-12 07:36:22 +000081 QTAILQ_FOREACH(entry, &acl->entries, next) {
Juan Quintela56ffaf22009-07-27 16:13:00 +020082#ifdef CONFIG_FNMATCH
aliguori28a76be2009-03-06 20:27:40 +000083 if (fnmatch(entry->match, party, 0) == 0)
84 return entry->deny ? 0 : 1;
aliguori76655d62009-03-06 20:27:37 +000085#else
aliguori28a76be2009-03-06 20:27:40 +000086 /* No fnmatch, so fallback to exact string matching
87 * instead of allowing wildcards */
88 if (strcmp(entry->match, party) == 0)
89 return entry->deny ? 0 : 1;
aliguori76655d62009-03-06 20:27:37 +000090#endif
91 }
92
93 return acl->defaultDeny ? 0 : 1;
94}
95
96
97void qemu_acl_reset(qemu_acl *acl)
98{
Markus Armbruster0ce6a432011-10-28 17:07:02 +020099 qemu_acl_entry *entry, *next_entry;
aliguori76655d62009-03-06 20:27:37 +0000100
101 /* Put back to deny by default, so there is no window
102 * of "open access" while the user re-initializes the
103 * access control list */
104 acl->defaultDeny = 1;
Markus Armbruster0ce6a432011-10-28 17:07:02 +0200105 QTAILQ_FOREACH_SAFE(entry, &acl->entries, next, next_entry) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000106 QTAILQ_REMOVE(&acl->entries, entry, next);
Markus Armbruster038794c2013-01-15 15:24:16 +0100107 g_free(entry->match);
108 g_free(entry);
aliguori76655d62009-03-06 20:27:37 +0000109 }
110 acl->nentries = 0;
111}
112
113
114int qemu_acl_append(qemu_acl *acl,
aliguori28a76be2009-03-06 20:27:40 +0000115 int deny,
116 const char *match)
aliguori76655d62009-03-06 20:27:37 +0000117{
118 qemu_acl_entry *entry;
119
Anthony Liguori7267c092011-08-20 22:09:37 -0500120 entry = g_malloc(sizeof(*entry));
121 entry->match = g_strdup(match);
aliguori76655d62009-03-06 20:27:37 +0000122 entry->deny = deny;
123
Blue Swirl72cf2d42009-09-12 07:36:22 +0000124 QTAILQ_INSERT_TAIL(&acl->entries, entry, next);
aliguori76655d62009-03-06 20:27:37 +0000125 acl->nentries++;
126
127 return acl->nentries;
128}
129
130
131int qemu_acl_insert(qemu_acl *acl,
aliguori28a76be2009-03-06 20:27:40 +0000132 int deny,
133 const char *match,
134 int index)
aliguori76655d62009-03-06 20:27:37 +0000135{
aliguori76655d62009-03-06 20:27:37 +0000136 qemu_acl_entry *tmp;
137 int i = 0;
138
139 if (index <= 0)
aliguori28a76be2009-03-06 20:27:40 +0000140 return -1;
Markus Armbruster4999f3a2013-06-18 10:05:23 +0200141 if (index > acl->nentries) {
aliguori28a76be2009-03-06 20:27:40 +0000142 return qemu_acl_append(acl, deny, match);
Markus Armbruster4999f3a2013-06-18 10:05:23 +0200143 }
aliguori76655d62009-03-06 20:27:37 +0000144
Blue Swirl72cf2d42009-09-12 07:36:22 +0000145 QTAILQ_FOREACH(tmp, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +0000146 i++;
147 if (i == index) {
Gonglei6cfcd862014-11-15 18:06:45 +0800148 qemu_acl_entry *entry;
149 entry = g_malloc(sizeof(*entry));
150 entry->match = g_strdup(match);
151 entry->deny = deny;
152
Blue Swirl72cf2d42009-09-12 07:36:22 +0000153 QTAILQ_INSERT_BEFORE(tmp, entry, next);
aliguori28a76be2009-03-06 20:27:40 +0000154 acl->nentries++;
155 break;
156 }
aliguori76655d62009-03-06 20:27:37 +0000157 }
158
159 return i;
160}
161
162int qemu_acl_remove(qemu_acl *acl,
aliguori28a76be2009-03-06 20:27:40 +0000163 const char *match)
aliguori76655d62009-03-06 20:27:37 +0000164{
165 qemu_acl_entry *entry;
166 int i = 0;
167
Blue Swirl72cf2d42009-09-12 07:36:22 +0000168 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +0000169 i++;
170 if (strcmp(entry->match, match) == 0) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000171 QTAILQ_REMOVE(&acl->entries, entry, next);
Markus Armbrusterc23c15d2013-01-15 15:24:15 +0100172 acl->nentries--;
173 g_free(entry->match);
174 g_free(entry);
aliguori28a76be2009-03-06 20:27:40 +0000175 return i;
176 }
aliguori76655d62009-03-06 20:27:37 +0000177 }
178 return -1;
179}