qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] PATCH: 8/9: Support ACLs for controlling VNC access


From: Daniel P. Berrange
Subject: Re: [Qemu-devel] PATCH: 8/9: Support ACLs for controlling VNC access
Date: Thu, 26 Feb 2009 11:57:30 +0000
User-agent: Mutt/1.4.1i

This patch introduces a generic internal API for access control lists
to be used by network servers in QEMU. It adds support for checking
these ACL in the VNC server, in two places. The first ACL is for the
SASL authentication mechanism, checking the SASL username. This ACL
is called 'vnc.username'. The second is for the TLS authentication
mechanism, when x509 client certificates are turned on, checking against
the Distinguished Name of the client. This ACL is called 'vnc.x509dname'

The internal API provides for an ACL with the following characteristics

 - A unique name, eg  vnc.username, and vnc.x509dname.
 - A default policy, allow or deny
 - An ordered series of match rules, with allow or deny policy

If none of the match rules apply, then the default policy is
used.

There is a monitor API to manipulate the ACLs, which I'll describe via
examples

  (qemu) acl show vnc.username
  policy: allow
  (qemu) acl policy vnc.username denya
  acl: policy set to 'deny'
  (qemu) acl allow vnc.username fred
  acl: added rule at position 1
  (qemu) acl allow vnc.username bob
  acl: added rule at position 2
  (qemu) acl allow vnc.username joe 1
  acl: added rule at position 1
  (qemu) acl show vnc.username
  policy: deny
  0: allow fred
  1: allow joe
  2: allow bob


  (qemu) acl show vnc.x509dname
  policy: allow
  (qemu) acl policy vnc.x509dname deny
  acl: policy set to 'deny'
  (qemu) acl allow vnc.x509dname C=GB,O=ACME,L=London,CN=*
  acl: added rule at position 1
  (qemu) acl allow vnc.x509dname C=GB,O=ACME,L=Boston,CN=bob
  acl: added rule at position 2
  (qemu) acl show vnc.x509dname
  policy: deny
  0: allow C=GB,O=ACME,L=London,CN=*
  1: allow C=GB,O=ACME,L=Boston,CN=bob

At startup the ACLs currently default to an allow policy. The
next patch will provide a way to load a pre-defined ACL when
starting up


 Makefile        |    6 +-
 b/acl.c         |  168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 b/acl.h         |   74 ++++++++++++++++++++++++
 monitor.c       |   95 +++++++++++++++++++++++++++++++
 vnc-auth-sasl.c |   16 ++++-
 vnc-auth-sasl.h |    7 ++
 vnc-tls.c       |   19 ++++++
 vnc-tls.h       |    3 +
 vnc.c           |   14 ++++
 vnc.h           |    3 +
 10 files changed, 398 insertions(+), 7 deletions(-)

   Signed-off-by: Daniel P. Berrange <address@hidden>

diff -r 102f57e60600 Makefile
--- a/Makefile  Fri Feb 20 11:23:38 2009 +0000
+++ b/Makefile  Fri Feb 20 11:26:59 2009 +0000
@@ -144,7 +144,7 @@ endif
 ifdef CONFIG_CURSES
 OBJS+=curses.o
 endif
-OBJS+=vnc.o d3des.o
+OBJS+=vnc.o acl.o d3des.o
 ifdef CONFIG_VNC_TLS
 OBJS+=vnc-tls.o vnc-auth-vencrypt.o
 endif
@@ -174,9 +174,11 @@ sdl.o: sdl.c keymaps.h sdl_keysym.h
 
 sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
 
+acl.o: acl.h acl.c
+
 vnc.h: vnc-tls.h vnc-auth-vencrypt.h vnc-auth-sasl.h keymaps.h
 
-vnc.o: vnc.c vnc.h vnc_keysym.h vnchextile.h d3des.c d3des.h
+vnc.o: vnc.c vnc.h vnc_keysym.h vnchextile.h d3des.c d3des.h acl.h
 
 vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
 
diff -r 102f57e60600 acl.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/acl.c     Fri Feb 20 11:26:59 2009 +0000
@@ -0,0 +1,168 @@
+/*
+ * QEMU access control list management
+ *
+ * Copyright (C) 2009 Red Hat, Inc
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+
+#include "qemu-common.h"
+#include "sysemu.h"
+#include "acl.h"
+#include <fnmatch.h>
+
+
+static unsigned int nacls = 0;
+static ACL **acls = NULL;
+
+
+
+ACL *qemu_acl_find(const char *aclname)
+{
+    int i;
+    for (i = 0 ; i < nacls ; i++) {
+       if (strcmp(acls[i]->aclname, aclname) == 0)
+           return acls[i];
+    }
+
+    return NULL;
+}
+
+ACL *qemu_acl_init(const char *aclname)
+{
+    ACL *acl;
+
+    acl = qemu_acl_find(aclname);
+    if (acl)
+       return acl;
+
+    acl = qemu_malloc(sizeof(*acl));
+    acl->aclname = qemu_strdup(aclname);
+    acl->defaultDeny = 0;
+
+    acl->nentries = 0;
+    TAILQ_INIT(&acl->entries);
+
+    acls = qemu_realloc(acls, sizeof(*acls) * (nacls +1));
+    acls[nacls] = acl;
+    nacls++;
+
+    return acl;
+}
+
+int qemu_acl_party_is_allowed(ACL *acl,
+                             const char *party)
+{
+    ACLEntry *entry;
+
+    TAILQ_FOREACH(entry, &acl->entries, next) {
+       if (fnmatch(entry->match, party, 0) == 0)
+           return entry->deny ? 0 : 1;
+    }
+
+    return acl->defaultDeny ? 0 : 1;
+}
+
+
+void qemu_acl_reset(ACL *acl)
+{
+    ACLEntry *entry;
+
+    TAILQ_FOREACH(entry, &acl->entries, next) {
+       TAILQ_REMOVE(&acl->entries, entry, next);
+       free(entry->match);
+       free(entry);
+    }
+    acl->nentries = 0;
+}
+
+
+int qemu_acl_append(ACL *acl,
+                   int deny,
+                   const char *match)
+{
+    ACLEntry *entry;
+
+    entry = qemu_malloc(sizeof(*entry));
+    entry->match = qemu_strdup(match);
+    entry->deny = deny;
+
+    TAILQ_INSERT_TAIL(&acl->entries, entry, next);
+    acl->nentries++;
+
+    return acl->nentries;
+}
+
+
+int qemu_acl_insert(ACL *acl,
+                   int deny,
+                   const char *match,
+                   int index)
+{
+    ACLEntry *entry;
+    ACLEntry *tmp;
+    int i = 0;
+
+    if (index <= 0)
+       return -1;
+    if (index >= acl->nentries)
+       return qemu_acl_append(acl, deny, match);
+
+
+    entry = qemu_malloc(sizeof(*entry));
+    entry->match = qemu_strdup(match);
+    entry->deny = deny;
+
+    TAILQ_FOREACH(tmp, &acl->entries, next) {
+       i++;
+       if (i == index) {
+           TAILQ_INSERT_BEFORE(tmp, entry, next);
+           acl->nentries++;
+           break;
+       }
+    }
+
+    return i;
+}
+
+int qemu_acl_remove(ACL *acl,
+                   const char *match)
+{
+    ACLEntry *entry;
+    int i = 0;
+
+    TAILQ_FOREACH(entry, &acl->entries, next) {
+       i++;
+       if (strcmp(entry->match, match) == 0) {
+           TAILQ_REMOVE(&acl->entries, entry, next);
+           return i;
+       }
+    }
+    return -1;
+}
+
+
+/*
+ * Local variables:
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 8
+ * End:
+ */
diff -r 102f57e60600 acl.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/acl.h     Fri Feb 20 11:26:59 2009 +0000
@@ -0,0 +1,74 @@
+/*
+ * QEMU access control list management
+ *
+ * Copyright (C) 2009 Red Hat, Inc
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef __QEMU_ACL_H__
+#define __QEMU_ACL_H__
+
+#include "sys-queue.h"
+
+typedef struct ACLEntry ACLEntry;
+typedef struct ACL ACL;
+
+struct ACLEntry {
+    char *match;
+    int deny;
+
+    TAILQ_ENTRY(ACLEntry) next;
+};
+
+struct ACL {
+    char *aclname;
+    unsigned int nentries;
+    TAILQ_HEAD(,ACLEntry) entries;
+    int defaultDeny;
+};
+
+ACL *qemu_acl_init(const char *aclname);
+
+ACL *qemu_acl_find(const char *aclname);
+
+int qemu_acl_party_is_allowed(ACL *acl,
+                             const char *party);
+
+void qemu_acl_reset(ACL *acl);
+
+int qemu_acl_append(ACL *acl,
+                   int deny,
+                   const char *match);
+int qemu_acl_insert(ACL *acl,
+                   int deny,
+                   const char *match,
+                   int index);
+int qemu_acl_remove(ACL *acl,
+                   const char *match);
+
+#endif /* __QEMU_ACL_H__ */
+
+/*
+ * Local variables:
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 8
+ * End:
+ */
diff -r 102f57e60600 monitor.c
--- a/monitor.c Fri Feb 20 11:23:38 2009 +0000
+++ b/monitor.c Fri Feb 20 11:26:59 2009 +0000
@@ -39,6 +39,7 @@
 #include "qemu-timer.h"
 #include "migration.h"
 #include "kvm.h"
+#include "acl.h"
 
 //#define DEBUG
 //#define DEBUG_COMPLETION
@@ -1425,6 +1426,85 @@ static void do_info_balloon(void)
         term_printf("balloon: actual=%d\n", (int)(actual >> 20));
 }
 
+static void do_acl(const char *command,
+                  const char *aclname,
+                  const char *match,
+                  int has_index,
+                  int index)
+{
+    ACL *acl;
+
+    acl = qemu_acl_find(aclname);
+    if (!acl) {
+       term_printf("acl: unknown list '%s'\n", aclname);
+       return;
+    }
+
+    if (strcmp(command, "show") == 0) {
+       int i = 0;
+       ACLEntry *entry;
+       term_printf("policy: %s\n",
+                   acl->defaultDeny ? "deny" : "allow");
+       TAILQ_FOREACH(entry, &acl->entries, next) {
+           i++;
+           term_printf("%d: %s %s\n", i,
+                       entry->deny ? "deny" : "allow",
+                       entry->match);
+       }
+    } else if (strcmp(command, "reset") == 0) {
+       qemu_acl_reset(acl);
+       term_printf("acl: removed all rules\n");
+    } else if (strcmp(command, "policy") == 0) {
+       if (!match) {
+           term_printf("acl: missing policy parameter\n");
+           return;
+       }
+
+       if (strcmp(match, "allow") == 0) {
+           acl->defaultDeny = 0;
+           term_printf("acl: policy set to 'allow'\n");
+       } else if (strcmp(match, "deny") == 0) {
+           acl->defaultDeny = 1;
+           term_printf("acl: policy set to 'deny'\n");
+       } else {
+           term_printf("acl: unknown policy '%s', expected 'deny' or 
'allow'\n", match);
+       }
+    } else if ((strcmp(command, "allow") == 0) ||
+              (strcmp(command, "deny") == 0)) {
+       int deny = strcmp(command, "deny") == 0 ? 1 : 0;
+       int ret;
+
+       if (!match) {
+           term_printf("acl: missing match parameter\n");
+           return;
+       }
+
+       if (has_index)
+           ret = qemu_acl_insert(acl, deny, match, index);
+       else
+           ret = qemu_acl_append(acl, deny, match);
+       if (ret < 0)
+           term_printf("acl: unable to add acl entry\n");
+       else
+           term_printf("acl: added rule at position %d\n", ret);
+    } else if (strcmp(command, "remove") == 0) {
+       int ret;
+
+       if (!match) {
+           term_printf("acl: missing match parameter\n");
+           return;
+       }
+
+       ret = qemu_acl_remove(acl, match);
+       if (ret < 0)
+           term_printf("acl: no matching acl entry\n");
+       else
+           term_printf("acl: removed rule at position %d\n", ret);
+    } else {
+       term_printf("acl: unknown command '%s'\n", command);
+    }
+}
+
 /* Please update qemu-doc.texi when adding or changing commands */
 static const term_cmd_t term_cmds[] = {
     { "help|?", "s?", do_help,
@@ -1529,6 +1609,12 @@ static const term_cmd_t term_cmds[] = {
       "target", "request VM to change it's memory allocation (in MB)" },
     { "set_link", "ss", do_set_link,
       "name [up|down]", "change the link status of a network adapter" },
+    { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
+                               "acl show vnc.username\n"
+                               "acl policy vnc.username deny\n"
+                               "acl allow vnc.username fred\n"
+                               "acl deny vnc.username bob\n"
+                               "acl reset vnc.username\n" },
     { NULL, NULL, },
 };
 
@@ -2891,3 +2977,12 @@ void monitor_readline(const char *prompt
                 monitor_hd[i]->focus = old_focus[i];
     }
 }
+
+
+/*
+ * Local variables:
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 8
+ * End:
+ */
diff -r 102f57e60600 vnc-auth-sasl.c
--- a/vnc-auth-sasl.c   Fri Feb 20 11:23:38 2009 +0000
+++ b/vnc-auth-sasl.c   Fri Feb 20 11:26:59 2009 +0000
@@ -120,22 +120,32 @@ static int vnc_auth_sasl_check_access(Vn
 {
     const void *val;
     int err;
+    int allow;
 
     err = sasl_getprop(vs->sasl.conn, SASL_USERNAME, &val);
     if (err != SASL_OK) {
-       VNC_DEBUG("cannot query SASL username on connection %d (%s)\n",
+       VNC_DEBUG("cannot query SASL username on connection %d (%s), denying 
access\n",
                  err, sasl_errstring(err, NULL, NULL));
        return -1;
     }
     if (val == NULL) {
-       VNC_DEBUG("no client username was found\n");
+       VNC_DEBUG("no client username was found, denying access\n");
        return -1;
     }
     VNC_DEBUG("SASL client username %s\n", (const char *)val);
 
     vs->sasl.username = qemu_strdup((const char*)val);
 
-    return 0;
+    if (vs->vd->sasl.acl == NULL) {
+       VNC_DEBUG("unexpectedly missing ACL, denying access\n");
+       return -1;
+    }
+
+    allow = qemu_acl_party_is_allowed(vs->vd->sasl.acl, vs->sasl.username);
+
+    VNC_DEBUG("SASL client %s %s by ACL\n", vs->sasl.username,
+             allow ? "allowed" : "denied");
+    return allow ? 0 : -1;
 }
 
 static int vnc_auth_sasl_check_ssf(VncState *vs)
diff -r 102f57e60600 vnc-auth-sasl.h
--- a/vnc-auth-sasl.h   Fri Feb 20 11:23:38 2009 +0000
+++ b/vnc-auth-sasl.h   Fri Feb 20 11:26:59 2009 +0000
@@ -30,6 +30,9 @@
 #include <sasl/sasl.h>
 
 typedef struct VncStateSASL VncStateSASL;
+typedef struct VncDisplaySASL VncDisplaySASL;
+
+#include "acl.h"
 
 struct VncStateSASL {
     sasl_conn_t *conn;
@@ -56,6 +59,10 @@ struct VncStateSASL {
     char *mechlist;
 };
 
+struct VncDisplaySASL {
+    ACL *acl;
+};
+
 void vnc_sasl_client_cleanup(VncState *vs);
 
 long vnc_client_read_sasl(VncState *vs);
diff -r 102f57e60600 vnc-tls.c
--- a/vnc-tls.c Fri Feb 20 11:23:38 2009 +0000
+++ b/vnc-tls.c Fri Feb 20 11:26:59 2009 +0000
@@ -255,6 +255,25 @@ int vnc_tls_validate_certificate(struct 
                          gnutls_strerror (ret));
                return -1;
            }
+
+           if (vs->vd->tls.x509verify) {
+               int allow;
+               if (!vs->vd->tls.acl) {
+                   VNC_DEBUG("unexpected missing acl, denying client\n");
+                   gnutls_x509_crt_deinit (cert);
+                   return -1;
+               }
+
+               allow = qemu_acl_party_is_allowed(vs->vd->tls.acl,
+                                                 vs->tls.dname);
+
+               VNC_DEBUG("TLS x509 ACL check for %s is %s\n",
+                         vs->tls.dname, allow ? "allowed" : "denied");
+               if (!allow) {
+                   gnutls_x509_crt_deinit (cert);
+                   return -1;
+               }
+           }
        }
 
        gnutls_x509_crt_deinit (cert);
diff -r 102f57e60600 vnc-tls.h
--- a/vnc-tls.h Fri Feb 20 11:23:38 2009 +0000
+++ b/vnc-tls.h Fri Feb 20 11:26:59 2009 +0000
@@ -31,6 +31,8 @@
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
 
+#include "acl.h"
+
 enum {
     VNC_WIREMODE_CLEAR,
     VNC_WIREMODE_TLS,
@@ -42,6 +44,7 @@ typedef struct VncStateTLS VncStateTLS;
 /* Server state */
 struct VncDisplayTLS {
     int x509verify; /* Non-zero if server requests & validates client cert */
+    ACL *acl;
 
     /* Paths to x509 certs/keys */
     char *x509cacert;
diff -r 102f57e60600 vnc.c
--- a/vnc.c     Fri Feb 20 11:23:38 2009 +0000
+++ b/vnc.c     Fri Feb 20 11:26:59 2009 +0000
@@ -28,6 +28,7 @@
 #include "sysemu.h"
 #include "qemu_socket.h"
 #include "qemu-timer.h"
+#include "acl.h"
 
 #define VNC_REFRESH_INTERVAL (1000 / 30)
 
@@ -2104,6 +2105,10 @@ int vnc_display_open(DisplayState *ds, c
 #ifdef CONFIG_VNC_SASL
        } else if (strncmp(options, "sasl", 4) == 0) {
            sasl = 1; /* Require SASL auth */
+           if (!(vs->sasl.acl = qemu_acl_init("vnc.username"))) {
+               fprintf(stderr, "Failed to create username ACL\n");
+               exit(1);
+           }
 #endif
 #ifdef CONFIG_VNC_TLS
        } else if (strncmp(options, "tls", 3) == 0) {
@@ -2111,8 +2116,13 @@ int vnc_display_open(DisplayState *ds, c
        } else if (strncmp(options, "x509", 4) == 0) {
            char *start, *end;
            x509 = 1; /* Require x509 certificates */
-           if (strncmp(options, "x509verify", 10) == 0)
-               vs->tls.x509verify = 1; /* ...and verify client certs */
+           if (strncmp(options, "x509verify", 10) == 0) {
+               vs->tls.x509verify = 1; /* ...and verify client certs */
+               if (!(vs->tls.acl = qemu_acl_init("vnc.x509dname"))) {
+                   fprintf(stderr, "Failed to create x509 dname ACL\n");
+                   exit(1);
+               }
+            }
 
            /* Now check for 'x509=/some/path' postfix
             * and use that to setup x509 certificate/key paths */
diff -r 102f57e60600 vnc.h
--- a/vnc.h     Fri Feb 20 11:23:38 2009 +0000
+++ b/vnc.h     Fri Feb 20 11:26:59 2009 +0000
@@ -98,6 +98,9 @@ struct VncDisplay
     int subauth; /* Used by VeNCrypt */
     VncDisplayTLS tls;
 #endif
+#ifdef CONFIG_VNC_SASL
+    VncDisplaySASL sasl;
+#endif
 };
 
 struct VncState

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




reply via email to

[Prev in Thread] Current Thread [Next in Thread]