2 * QEMU access control list management
4 * Copyright (C) 2009 Red Hat, Inc
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #include "qemu-common.h"
34 static unsigned int nacls = 0;
35 static qemu_acl **acls = NULL;
39 qemu_acl *qemu_acl_find(const char *aclname)
42 for (i = 0 ; i < nacls ; i++) {
43 if (strcmp(acls[i]->aclname, aclname) == 0)
50 qemu_acl *qemu_acl_init(const char *aclname)
54 acl = qemu_acl_find(aclname);
58 acl = g_malloc(sizeof(*acl));
59 acl->aclname = g_strdup(aclname);
60 /* Deny by default, so there is no window of "open
61 * access" between QEMU starting, and the user setting
62 * up ACLs in the monitor */
66 QTAILQ_INIT(&acl->entries);
68 acls = g_realloc(acls, sizeof(*acls) * (nacls +1));
75 int qemu_acl_party_is_allowed(qemu_acl *acl,
78 qemu_acl_entry *entry;
80 QTAILQ_FOREACH(entry, &acl->entries, next) {
82 if (fnmatch(entry->match, party, 0) == 0)
83 return entry->deny ? 0 : 1;
85 /* No fnmatch, so fallback to exact string matching
86 * instead of allowing wildcards */
87 if (strcmp(entry->match, party) == 0)
88 return entry->deny ? 0 : 1;
92 return acl->defaultDeny ? 0 : 1;
96 void qemu_acl_reset(qemu_acl *acl)
98 qemu_acl_entry *entry, *next_entry;
100 /* Put back to deny by default, so there is no window
101 * of "open access" while the user re-initializes the
102 * access control list */
103 acl->defaultDeny = 1;
104 QTAILQ_FOREACH_SAFE(entry, &acl->entries, next, next_entry) {
105 QTAILQ_REMOVE(&acl->entries, entry, next);
106 g_free(entry->match);
113 int qemu_acl_append(qemu_acl *acl,
117 qemu_acl_entry *entry;
119 entry = g_malloc(sizeof(*entry));
120 entry->match = g_strdup(match);
123 QTAILQ_INSERT_TAIL(&acl->entries, entry, next);
126 return acl->nentries;
130 int qemu_acl_insert(qemu_acl *acl,
140 if (index > acl->nentries) {
141 return qemu_acl_append(acl, deny, match);
144 QTAILQ_FOREACH(tmp, &acl->entries, next) {
147 qemu_acl_entry *entry;
148 entry = g_malloc(sizeof(*entry));
149 entry->match = g_strdup(match);
152 QTAILQ_INSERT_BEFORE(tmp, entry, next);
161 int qemu_acl_remove(qemu_acl *acl,
164 qemu_acl_entry *entry;
167 QTAILQ_FOREACH(entry, &acl->entries, next) {
169 if (strcmp(entry->match, match) == 0) {
170 QTAILQ_REMOVE(&acl->entries, entry, next);
172 g_free(entry->match);