* [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server
@ 2009-02-12 14:53 Daniel P. Berrange
2009-02-12 15:01 ` [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client Daniel P. Berrange
` (8 more replies)
0 siblings, 9 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 14:53 UTC (permalink / raw)
To: qemu-devel
Previously I provided patches for QEMU's VNC server to support SSL/TLS
and x509 certificates. This provides good encryption capabilities for
the VNC session. It doesn't really address the authentication problem
though.
I have been working to create a new authentication type in the RFB
protocol to address this need in a generic, extendable way, by mapping
the SASL API into the RFB protocol. Since SASL is a generic plugin
based API, this will allow use of a huge range of auth mechanims over
VNC, without us having to add any more auth code. For example, PAM,
Digest-MD5, GSSAPI/Kerberos, One-time key/password, LDAP password
lookup, SQL db password lookup, and more.
I have got a VNC auth type assigned by the RFB spec maintainers:
http://realvnc.com/pipermail/vnc-list/2008-December/059463.html
With the full current spec for the SASL extension currently documented
here:
http://realvnc.com/pipermail/vnc-list/2008-December/059462.html
This is the 2nd version of the patches I initially posted here
http://lists.gnu.org/archive/html/qemu-devel/2009-02/msg00255.html
Changes since last time
- Re-factor the code to move TLS and SASL methods into separate files,
vnc-tls.c, vnc-auth-vencrypt.c and vnc-auth-vencrypt.h
- Added simple access control lists for authorization of client users
on either SASL username, or x509 distinguished name
- Added proof of concept external file format for persisting ACLs
- Extend 'info vnc' to show much more information about clients and
auth
- Tested with SASL + Digest-MD5, SASL + GSSAPI. TLS + SASL + Digest-MD5
and TLS + SASL + GSSAPI. This gives coverage off all interesting
code paths and/or I/O encryption combinations.
The combined diffstat for all 7 patches about to follow, is
.hgignore | 16
Makefile | 27 +
Makefile.target | 5
b/acl.c | 264 ++++++++++++
b/acl.h | 71 +++
b/keymaps.h | 60 ++
b/qemu.sasl | 34 +
b/vnc-auth-sasl.c | 640 +++++++++++++++++++++++++++++
b/vnc-auth-sasl.h | 76 +++
b/vnc-auth-vencrypt.c | 175 +++++++
b/vnc-auth-vencrypt.h | 33 +
b/vnc-tls.c | 456 ++++++++++++++++++++
b/vnc-tls.h | 76 +++
configure | 34 +
curses.c | 3
curses_keys.h | 9
keymaps.c | 45 --
monitor.c | 80 +++
qemu-doc.texi | 109 ++++
sdl.c | 3
sdl_keysym.h | 7
vl.c | 12
vnc.c | 1100 ++++++++++++++++++--------------------------------
vnc.h | 215 +++++++++
vnc_keysym.h | 5
25 files changed, 2795 insertions(+), 760 deletions(-)
Daniel
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
@ 2009-02-12 15:01 ` Daniel P. Berrange
2009-02-13 18:30 ` Anthony Liguori
2009-02-12 15:02 ` [Qemu-devel] PATCH: 2/7: Push VncState struct into vnc.h Daniel P. Berrange
` (7 subsequent siblings)
8 siblings, 1 reply; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:01 UTC (permalink / raw)
To: qemu-devel
The current 'info vnc' monitor output just displays the VNC server address
as provided by the -vnc command line flag. This isn't particularly useful
since it doesn't tell you what VNC is actually listening on. eg, if you
use '-vnc :1' it is useful to know whether this translated to '0.0.0.0:5901'
or chose IPv6 ':::5901'. It is also useful to know the address of the
client that is currently connected. It is also useful to know the active
authentication (if any).
This patch tweaks the monitor output to look like:
(qemu) info vnc
Server: active
address: 0.0.0.0:5901
auth: vencrypt+x509+sasl
Client: none
And when a client is connected
(qemu) info vnc
Server: active
address: 0.0.0.0:5901
auth: vencrypt+x509+sasl
Client: active
address: 127.0.0.1:42956
More data will be added to this later in the patch series...
The 'addr_to_string' helper method in this patch is overly generic
for the needs of this patch alone. This is because it will be re-used
by the later SASL patches in this series, where the flexibility is
important.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
vnc.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 124 insertions(+), 8 deletions(-)
Daniel
diff -r 34bb2693ebd2 vnc.c
--- a/vnc.c Thu Feb 12 12:28:12 2009 +0000
+++ b/vnc.c Thu Feb 12 12:28:19 2009 +0000
@@ -154,19 +154,127 @@ struct VncState
static VncState *vnc_state; /* needed for info vnc */
static DisplayChangeListener *dcl;
+static char *addr_to_string(const char *format,
+ struct sockaddr_storage *sa,
+ socklen_t salen) {
+ char *addr;
+ char host[NI_MAXHOST];
+ char serv[NI_MAXSERV];
+ int err;
+
+ if ((err = getnameinfo((struct sockaddr *)sa, salen,
+ host, sizeof(host),
+ serv, sizeof(serv),
+ NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
+ VNC_DEBUG("Cannot resolve address %d: %s\n",
+ err, gai_strerror(err));
+ return NULL;
+ }
+
+ if (asprintf(&addr, format, host, serv) < 0)
+ return NULL;
+
+ return addr;
+}
+
+static char *vnc_socket_local_addr(const char *format, int fd) {
+ struct sockaddr_storage sa;
+ socklen_t salen;
+
+ salen = sizeof(sa);
+ if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0)
+ return NULL;
+
+ return addr_to_string(format, &sa, salen);
+}
+
+static char *vnc_socket_remote_addr(const char *format, int fd) {
+ struct sockaddr_storage sa;
+ socklen_t salen;
+
+ salen = sizeof(sa);
+ if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0)
+ return NULL;
+
+ return addr_to_string(format, &sa, salen);
+}
+
+static const char *vnc_auth_name(VncState *vs) {
+ switch (vs->auth) {
+ case VNC_AUTH_INVALID:
+ return "invalid";
+ case VNC_AUTH_NONE:
+ return "none";
+ case VNC_AUTH_VNC:
+ return "vnc";
+ case VNC_AUTH_RA2:
+ return "ra2";
+ case VNC_AUTH_RA2NE:
+ return "ra2ne";
+ case VNC_AUTH_TIGHT:
+ return "tight";
+ case VNC_AUTH_ULTRA:
+ return "ultra";
+ case VNC_AUTH_TLS:
+ return "tls";
+ case VNC_AUTH_VENCRYPT:
+#ifdef CONFIG_VNC_TLS
+ switch (vs->subauth) {
+ case VNC_AUTH_VENCRYPT_PLAIN:
+ return "vencrypt+plain";
+ case VNC_AUTH_VENCRYPT_TLSNONE:
+ return "vencrypt+tls+none";
+ case VNC_AUTH_VENCRYPT_TLSVNC:
+ return "vencrypt+tls+vnc";
+ case VNC_AUTH_VENCRYPT_TLSPLAIN:
+ return "vencrypt+tls+plain";
+ case VNC_AUTH_VENCRYPT_X509NONE:
+ return "vencrypt+x509+none";
+ case VNC_AUTH_VENCRYPT_X509VNC:
+ return "vencrypt+x509+vnc";
+ case VNC_AUTH_VENCRYPT_X509PLAIN:
+ return "vencrypt+x509+plain";
+ default:
+ return "vencrypt";
+ }
+#else
+ return "vencrypt";
+#endif
+ }
+ return "unknown";
+}
+
+#define VNC_SOCKET_FORMAT_PRETTY "local %s:%s"
+
void do_info_vnc(void)
{
if (vnc_state == NULL || vnc_state->display == NULL)
- term_printf("VNC server disabled\n");
+ term_printf("Server: disabled\n");
else {
- term_printf("VNC server active on: ");
- term_print_filename(vnc_state->display);
- term_printf("\n");
+ char *serverAddr = vnc_socket_local_addr(" address: %s:%s\n",
+ vnc_state->lsock);
- if (vnc_state->csock == -1)
- term_printf("No client connected\n");
- else
- term_printf("Client connected\n");
+ if (!serverAddr)
+ return;
+
+ term_puts("Server: active\n");
+ term_puts(serverAddr);
+ free(serverAddr);
+ term_printf(" auth: %s\n", vnc_auth_name(vnc_state));
+
+ if (vnc_state->csock == -1) {
+ term_puts("Client: none\n");
+ } else {
+ char *clientAddr =
+ vnc_socket_remote_addr(" address: %s:%s\n",
+ vnc_state->csock);
+ if (!clientAddr)
+ return;
+
+ term_puts("Client: active\n");
+ term_puts(clientAddr);
+ free(clientAddr);
+ }
}
}
@@ -2518,3 +2626,11 @@ int vnc_display_open(DisplayState *ds, c
return qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
}
+
+/*
+ * Local variables:
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 8
+ * End:
+ */
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 2/7: Push VncState struct into vnc.h
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
2009-02-12 15:01 ` [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client Daniel P. Berrange
@ 2009-02-12 15:02 ` Daniel P. Berrange
2009-02-14 22:09 ` Anthony Liguori
2009-02-12 15:02 ` [Qemu-devel] PATCH: 3/7: Split out VNC TLS auth code to separate file Daniel P. Berrange
` (6 subsequent siblings)
8 siblings, 1 reply; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:02 UTC (permalink / raw)
To: qemu-devel
This patch moves the declaration for the VncState struct out of the
vnc.c file and into vnc.h. This is to prepare for next patches which
have the auth mechanisms implementated in separate vnc-auth-vencrypt.c
and vnc-auth-sasl.c files
In doing this, I discovered that there is a pile of duplicated keymap
code statically compiled into all the console frontends. A couple of
trivial changes allowed this to be sanitized, so instead of doing
a #include "keymaps.c", duplicating all code, we can have a shared
keymaps.h file, and only compile code once.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Makefile | 9 ++-
b/keymaps.h | 60 ++++++++++++++++++++++++
curses.c | 3 -
curses_keys.h | 9 +--
keymaps.c | 45 +++++++-----------
sdl.c | 3 -
sdl_keysym.h | 7 +-
vnc.c | 101 ++---------------------------------------
vnc.h | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
vnc_keysym.h | 5 --
10 files changed, 235 insertions(+), 147 deletions(-)
Daniel
diff -r 1e8d80609fe1 Makefile
--- a/Makefile Wed Feb 11 17:31:30 2009 +0000
+++ b/Makefile Wed Feb 11 17:31:34 2009 +0000
@@ -137,6 +137,7 @@ endif
AUDIO_OBJS+= wavcapture.o
OBJS+=$(addprefix audio/, $(AUDIO_OBJS))
+OBJS+=keymaps.o
ifdef CONFIG_SDL
OBJS+=sdl.o x_keymap.o
endif
@@ -161,15 +162,17 @@ LIBS+=$(VDE_LIBS)
cocoa.o: cocoa.m
-sdl.o: sdl.c keymaps.c sdl_keysym.h
+keymaps.o: keymaps.c keymaps.h
+
+sdl.o: sdl.c keymaps.h sdl_keysym.h
sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
-vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h d3des.c d3des.h
+vnc.o: vnc.c keymaps.h sdl_keysym.h vnchextile.h d3des.c d3des.h
vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
-curses.o: curses.c keymaps.c curses_keys.h
+curses.o: curses.c keymaps.h curses_keys.h
bt-host.o: CFLAGS += $(CONFIG_BLUEZ_CFLAGS)
diff -r 1e8d80609fe1 curses.c
--- a/curses.c Wed Feb 11 17:31:30 2009 +0000
+++ b/curses.c Wed Feb 11 17:31:34 2009 +0000
@@ -158,7 +158,6 @@ static void curses_cursor_position(Displ
/* generic keyboard conversion */
#include "curses_keys.h"
-#include "keymaps.c"
static kbd_layout_t *kbd_layout = 0;
static int keycode2keysym[CURSES_KEYS];
@@ -311,7 +310,7 @@ static void curses_keyboard_setup(void)
keyboard_layout = "en-us";
#endif
if(keyboard_layout) {
- kbd_layout = init_keyboard_layout(keyboard_layout);
+ kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
if (!kbd_layout)
exit(1);
}
diff -r 1e8d80609fe1 curses_keys.h
--- a/curses_keys.h Wed Feb 11 17:31:30 2009 +0000
+++ b/curses_keys.h Wed Feb 11 17:31:34 2009 +0000
@@ -21,6 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
+
+#include "keymaps.h"
+
+
#define KEY_RELEASE 0x80
#define KEY_MASK 0x7f
#define SHIFT_CODE 0x2a
@@ -239,11 +243,6 @@ static const int curses2keysym[CURSES_KE
};
-typedef struct {
- const char* name;
- int keysym;
-} name2keysym_t;
-
static const name2keysym_t name2keysym[] = {
/* Plain ASCII */
{ "space", 0x020 },
diff -r 1e8d80609fe1 keymaps.c
--- a/keymaps.c Wed Feb 11 17:31:30 2009 +0000
+++ b/keymaps.c Wed Feb 11 17:31:34 2009 +0000
@@ -22,34 +22,20 @@
* THE SOFTWARE.
*/
-static int get_keysym(const char *name)
+#include "keymaps.h"
+#include "sysemu.h"
+
+static int get_keysym(const name2keysym_t *table,
+ const char *name)
{
const name2keysym_t *p;
- for(p = name2keysym; p->name != NULL; p++) {
+ for(p = table; p->name != NULL; p++) {
if (!strcmp(p->name, name))
return p->keysym;
}
return 0;
}
-struct key_range {
- int start;
- int end;
- struct key_range *next;
-};
-
-#define MAX_NORMAL_KEYCODE 512
-#define MAX_EXTRA_COUNT 256
-typedef struct {
- uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];
- struct {
- int keysym;
- uint16_t keycode;
- } keysym2keycode_extra[MAX_EXTRA_COUNT];
- int extra_count;
- struct key_range *keypad_range;
- struct key_range *numlock_range;
-} kbd_layout_t;
static void add_to_key_range(struct key_range **krp, int code) {
struct key_range *kr;
@@ -73,7 +59,8 @@ static void add_to_key_range(struct key_
}
}
-static kbd_layout_t *parse_keyboard_layout(const char *language,
+static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
+ const char *language,
kbd_layout_t * k)
{
FILE *f;
@@ -102,7 +89,7 @@ static kbd_layout_t *parse_keyboard_layo
if (!strncmp(line, "map ", 4))
continue;
if (!strncmp(line, "include ", 8)) {
- parse_keyboard_layout(line + 8, k);
+ parse_keyboard_layout(table, line + 8, k);
} else {
char *end_of_keysym = line;
while (*end_of_keysym != 0 && *end_of_keysym != ' ')
@@ -110,7 +97,7 @@ static kbd_layout_t *parse_keyboard_layo
if (*end_of_keysym) {
int keysym;
*end_of_keysym = 0;
- keysym = get_keysym(line);
+ keysym = get_keysym(table, line);
if (keysym == 0) {
// fprintf(stderr, "Warning: unknown keysym %s\n", line);
} else {
@@ -154,12 +141,14 @@ static kbd_layout_t *parse_keyboard_layo
return k;
}
-static void *init_keyboard_layout(const char *language)
+
+void *init_keyboard_layout(const name2keysym_t *table, const char *language)
{
- return parse_keyboard_layout(language, 0);
+ return parse_keyboard_layout(table, language, 0);
}
-static int keysym2scancode(void *kbd_layout, int keysym)
+
+int keysym2scancode(void *kbd_layout, int keysym)
{
kbd_layout_t *k = kbd_layout;
if (keysym < MAX_NORMAL_KEYCODE) {
@@ -180,7 +169,7 @@ static int keysym2scancode(void *kbd_lay
return 0;
}
-static inline int keycode_is_keypad(void *kbd_layout, int keycode)
+int keycode_is_keypad(void *kbd_layout, int keycode)
{
kbd_layout_t *k = kbd_layout;
struct key_range *kr;
@@ -191,7 +180,7 @@ static inline int keycode_is_keypad(void
return 0;
}
-static inline int keysym_is_numlock(void *kbd_layout, int keysym)
+int keysym_is_numlock(void *kbd_layout, int keysym)
{
kbd_layout_t *k = kbd_layout;
struct key_range *kr;
diff -r 1e8d80609fe1 keymaps.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/keymaps.h Wed Feb 11 17:31:34 2009 +0000
@@ -0,0 +1,60 @@
+/*
+ * QEMU keysym to keycode conversion using rdesktop keymaps
+ *
+ * Copyright (c) 2004 Johannes Schindelin
+ *
+ * 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_KEYMAPS_H__
+#define __QEMU_KEYMAPS_H__
+
+#include "qemu-common.h"
+
+typedef struct {
+ const char* name;
+ int keysym;
+} name2keysym_t;
+
+struct key_range {
+ int start;
+ int end;
+ struct key_range *next;
+};
+
+#define MAX_NORMAL_KEYCODE 512
+#define MAX_EXTRA_COUNT 256
+typedef struct {
+ uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];
+ struct {
+ int keysym;
+ uint16_t keycode;
+ } keysym2keycode_extra[MAX_EXTRA_COUNT];
+ int extra_count;
+ struct key_range *keypad_range;
+ struct key_range *numlock_range;
+} kbd_layout_t;
+
+
+void *init_keyboard_layout(const name2keysym_t *table, const char *language);
+int keysym2scancode(void *kbd_layout, int keysym);
+int keycode_is_keypad(void *kbd_layout, int keycode);
+int keysym_is_numlock(void *kbd_layout, int keysym);
+
+#endif /* __QEMU_KEYMAPS_H__ */
diff -r 1e8d80609fe1 sdl.c
--- a/sdl.c Wed Feb 11 17:31:30 2009 +0000
+++ b/sdl.c Wed Feb 11 17:31:34 2009 +0000
@@ -107,7 +107,6 @@ static void sdl_resize(DisplayState *ds)
/* generic keyboard conversion */
#include "sdl_keysym.h"
-#include "keymaps.c"
static kbd_layout_t *kbd_layout = NULL;
@@ -623,7 +622,7 @@ void sdl_display_init(DisplayState *ds,
keyboard_layout = "en-us";
#endif
if(keyboard_layout) {
- kbd_layout = init_keyboard_layout(keyboard_layout);
+ kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
if (!kbd_layout)
exit(1);
}
diff -r 1e8d80609fe1 sdl_keysym.h
--- a/sdl_keysym.h Wed Feb 11 17:31:30 2009 +0000
+++ b/sdl_keysym.h Wed Feb 11 17:31:34 2009 +0000
@@ -1,7 +1,6 @@
-typedef struct {
- const char* name;
- int keysym;
-} name2keysym_t;
+
+#include "keymaps.h"
+
static const name2keysym_t name2keysym[]={
/* ascii */
{ "space", 0x020},
diff -r 1e8d80609fe1 vnc.c
--- a/vnc.c Wed Feb 11 17:31:30 2009 +0000
+++ b/vnc.c Wed Feb 11 17:31:34 2009 +0000
@@ -3,6 +3,7 @@
*
* Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
* Copyright (C) 2006 Fabrice Bellard
+ * 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
@@ -23,26 +24,17 @@
* THE SOFTWARE.
*/
-#include "qemu-common.h"
-#include "console.h"
+#include "vnc.h"
+
#include "sysemu.h"
#include "qemu_socket.h"
#include "qemu-timer.h"
-#include "audio/audio.h"
-#include <zlib.h>
#define VNC_REFRESH_INTERVAL (1000 / 30)
-#include "vnc.h"
#include "vnc_keysym.h"
-#include "keymaps.c"
#include "d3des.h"
-#ifdef CONFIG_VNC_TLS
-#include <gnutls/gnutls.h>
-#include <gnutls/x509.h>
-#endif /* CONFIG_VNC_TLS */
-
// #define _VNC_DEBUG 1
#ifdef _VNC_DEBUG
@@ -65,91 +57,8 @@ static void vnc_debug_gnutls_log(int lev
} \
}
-typedef struct Buffer
-{
- size_t capacity;
- size_t offset;
- uint8_t *buffer;
-} Buffer;
-typedef struct VncState VncState;
-typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len);
-
-typedef void VncWritePixels(VncState *vs, void *data, int size);
-
-typedef void VncSendHextileTile(VncState *vs,
- int x, int y, int w, int h,
- void *last_bg,
- void *last_fg,
- int *has_bg, int *has_fg);
-
-#define VNC_MAX_WIDTH 2048
-#define VNC_MAX_HEIGHT 2048
-#define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
-
-#define VNC_AUTH_CHALLENGE_SIZE 16
-
-struct VncState
-{
- QEMUTimer *timer;
- int lsock;
- int csock;
- DisplayState *ds;
- int need_update;
- uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
- char *old_data;
- uint32_t features;
- int absolute;
- int last_x;
- int last_y;
-
- uint32_t vnc_encoding;
- uint8_t tight_quality;
- uint8_t tight_compression;
-
- int major;
- int minor;
-
- char *display;
- char *password;
- int auth;
-#ifdef CONFIG_VNC_TLS
- int subauth;
- int x509verify;
-
- char *x509cacert;
- char *x509cacrl;
- char *x509cert;
- char *x509key;
-#endif
- char challenge[VNC_AUTH_CHALLENGE_SIZE];
-
-#ifdef CONFIG_VNC_TLS
- int wiremode;
- gnutls_session_t tls_session;
-#endif
-
- Buffer output;
- Buffer input;
- kbd_layout_t *kbd_layout;
- /* current output mode information */
- VncWritePixels *write_pixels;
- VncSendHextileTile *send_hextile_tile;
- DisplaySurface clientds, serverds;
-
- CaptureVoiceOut *audio_cap;
- struct audsettings as;
-
- VncReadEvent *read_handler;
- size_t read_handler_expect;
- /* input */
- uint8_t modifiers_state[256];
-
- Buffer zlib;
- Buffer zlib_tmp;
- z_stream zlib_stream[4];
-};
static VncState *vnc_state; /* needed for info vnc */
static DisplayChangeListener *dcl;
@@ -2352,9 +2261,9 @@ void vnc_display_init(DisplayState *ds)
vs->ds = ds;
if (keyboard_layout)
- vs->kbd_layout = init_keyboard_layout(keyboard_layout);
+ vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
else
- vs->kbd_layout = init_keyboard_layout("en-us");
+ vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
if (!vs->kbd_layout)
exit(1);
diff -r 1e8d80609fe1 vnc.h
--- a/vnc.h Wed Feb 11 17:31:30 2009 +0000
+++ b/vnc.h Wed Feb 11 17:31:34 2009 +0000
@@ -1,5 +1,139 @@
-#ifndef __VNCTIGHT_H
-#define __VNCTIGHT_H
+/*
+ * QEMU VNC display driver
+ *
+ * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
+ * Copyright (C) 2006 Fabrice Bellard
+ * 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 __VNC_H
+#define __VNC_H
+
+
+#include "qemu-common.h"
+#include "console.h"
+#include "audio/audio.h"
+#include <zlib.h>
+
+#ifdef CONFIG_VNC_TLS
+#include <gnutls/gnutls.h>
+#include <gnutls/x509.h>
+#endif /* CONFIG_VNC_TLS */
+
+#include "keymaps.h"
+
+
+/*****************************************************************************
+ *
+ * Core data structures
+ *
+ *****************************************************************************/
+
+
+#define VNC_MAX_WIDTH 2048
+#define VNC_MAX_HEIGHT 2048
+#define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
+
+#define VNC_AUTH_CHALLENGE_SIZE 16
+
+typedef struct VncState VncState;
+
+typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len);
+
+typedef void VncWritePixels(VncState *vs, void *data, int size);
+
+typedef void VncSendHextileTile(VncState *vs,
+ int x, int y, int w, int h,
+ void *last_bg,
+ void *last_fg,
+ int *has_bg, int *has_fg);
+
+
+typedef struct Buffer
+{
+ size_t capacity;
+ size_t offset;
+ uint8_t *buffer;
+} Buffer;
+
+struct VncState
+{
+ QEMUTimer *timer;
+ int lsock;
+ int csock;
+ DisplayState *ds;
+ int need_update;
+ uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
+ char *old_data;
+ uint32_t features;
+ int absolute;
+ int last_x;
+ int last_y;
+
+ uint32_t vnc_encoding;
+ uint8_t tight_quality;
+ uint8_t tight_compression;
+
+ int major;
+ int minor;
+
+ char *display;
+ char *password;
+ int auth;
+#ifdef CONFIG_VNC_TLS
+ int subauth;
+ int x509verify;
+
+ char *x509cacert;
+ char *x509cacrl;
+ char *x509cert;
+ char *x509key;
+#endif
+ char challenge[VNC_AUTH_CHALLENGE_SIZE];
+
+#ifdef CONFIG_VNC_TLS
+ int wiremode;
+ gnutls_session_t tls_session;
+#endif
+
+ Buffer output;
+ Buffer input;
+ kbd_layout_t *kbd_layout;
+ /* current output mode information */
+ VncWritePixels *write_pixels;
+ VncSendHextileTile *send_hextile_tile;
+ DisplaySurface clientds, serverds;
+
+ CaptureVoiceOut *audio_cap;
+ struct audsettings as;
+
+ VncReadEvent *read_handler;
+ size_t read_handler_expect;
+ /* input */
+ uint8_t modifiers_state[256];
+
+ Buffer zlib;
+ Buffer zlib_tmp;
+ z_stream zlib_stream[4];
+};
/*****************************************************************************
*
@@ -109,4 +243,4 @@ enum {
#define VNC_FEATURE_TIGHT_MASK (1 << VNC_FEATURE_TIGHT)
#define VNC_FEATURE_ZLIB_MASK (1 << VNC_FEATURE_ZLIB)
-#endif /* __VNCTIGHT_H */
+#endif /* __VNC_H */
diff -r 1e8d80609fe1 vnc_keysym.h
--- a/vnc_keysym.h Wed Feb 11 17:31:30 2009 +0000
+++ b/vnc_keysym.h Wed Feb 11 17:31:34 2009 +0000
@@ -1,7 +1,4 @@
-typedef struct {
- const char* name;
- int keysym;
-} name2keysym_t;
+
static const name2keysym_t name2keysym[]={
/* ascii */
{ "space", 0x020},
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 3/7: Split out VNC TLS auth code to separate file
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
2009-02-12 15:01 ` [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client Daniel P. Berrange
2009-02-12 15:02 ` [Qemu-devel] PATCH: 2/7: Push VncState struct into vnc.h Daniel P. Berrange
@ 2009-02-12 15:02 ` Daniel P. Berrange
2009-02-12 15:03 ` [Qemu-devel] PATCH: 4/7: Add SASL authentication extension to VNC Daniel P. Berrange
` (5 subsequent siblings)
8 siblings, 0 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:02 UTC (permalink / raw)
To: qemu-devel
This patch refactors the existing TLS code to make the main VNC code
more managable. The code moves to two new files
- vnc-tls.c: generic helpers for TLS handshake & credential setup
- vnc-auth-vencrypt.c: the actual VNC TLS authentication mechanism.
The reason for this split is that there are other TLS based auth
mechanisms which we may like to use in the future. These can all
share the same vnc-tls.c routines. In addition this will facilitate
anyone who may want to port the vnc-tls.c file to allow for choice
of GNUTLS & NSS for impl.
The TLS state is moved out of the VncState struct, and into a separate
VncStateTLS struct, defined in vnc-tls.h. This is then referenced from
the main VncState. End size of the struct is the same, but it keeps
things a little more managable.
The vnc.h file gains a bunch more function prototypes, for functions
in vnc.c that were previously static, but now need to be accessed
from the separate auth code files.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Makefile | 11
b/vnc-auth-vencrypt.c | 167 ++++++++++++++
b/vnc-auth-vencrypt.h | 33 ++
b/vnc-tls.c | 420 +++++++++++++++++++++++++++++++++++
b/vnc-tls.h | 70 +++++
vnc.c | 588 +++-----------------------------------------------
vnc.h | 78 ++++--
7 files changed, 786 insertions(+), 581 deletions(-)
Daniel
diff -r 95baa502a2b8 Makefile
--- a/Makefile Thu Feb 12 12:28:21 2009 +0000
+++ b/Makefile Thu Feb 12 12:28:24 2009 +0000
@@ -145,6 +145,9 @@ ifdef CONFIG_CURSES
OBJS+=curses.o
endif
OBJS+=vnc.o d3des.o
+ifdef CONFIG_VNC_TLS
+OBJS+=vnc-tls.o vnc-auth-vencrypt.o
+endif
ifdef CONFIG_COCOA
OBJS+=cocoa.o
@@ -168,10 +171,16 @@ sdl.o: sdl.c keymaps.h sdl_keysym.h
sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
-vnc.o: vnc.c keymaps.h sdl_keysym.h vnchextile.h d3des.c d3des.h
+vnc.h: vnc-tls.h vnc-auth-vencrypt.h keymaps.h
+
+vnc.o: vnc.c vnc.h vnc_keysym.h vnchextile.h d3des.c d3des.h
vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
+vnc-tls.o: vnc-tls.c vnc.h
+
+vnc-auth-vencrypt.o: vnc-auth-vencrypt.c vnc.h
+
curses.o: curses.c keymaps.h curses_keys.h
bt-host.o: CFLAGS += $(CONFIG_BLUEZ_CFLAGS)
diff -r 95baa502a2b8 vnc-auth-vencrypt.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vnc-auth-vencrypt.c Thu Feb 12 12:28:24 2009 +0000
@@ -0,0 +1,167 @@
+/*
+ * QEMU VNC display driver: VeNCrypt authentication setup
+ *
+ * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
+ * Copyright (C) 2006 Fabrice Bellard
+ * 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 "vnc.h"
+
+
+static void start_auth_vencrypt_subauth(VncState *vs)
+{
+ switch (vs->subauth) {
+ case VNC_AUTH_VENCRYPT_TLSNONE:
+ case VNC_AUTH_VENCRYPT_X509NONE:
+ VNC_DEBUG("Accept TLS auth none\n");
+ vnc_write_u32(vs, 0); /* Accept auth completion */
+ start_client_init(vs);
+ break;
+
+ case VNC_AUTH_VENCRYPT_TLSVNC:
+ case VNC_AUTH_VENCRYPT_X509VNC:
+ VNC_DEBUG("Start TLS auth VNC\n");
+ start_auth_vnc(vs);
+ break;
+
+ default: /* Should not be possible, but just in case */
+ VNC_DEBUG("Reject auth %d\n", vs->auth);
+ vnc_write_u8(vs, 1);
+ if (vs->minor >= 8) {
+ static const char err[] = "Unsupported authentication type";
+ vnc_write_u32(vs, sizeof(err));
+ vnc_write(vs, err, sizeof(err));
+ }
+ vnc_client_error(vs);
+ }
+}
+
+static void vnc_tls_handshake_io(void *opaque);
+
+static int vnc_start_vencrypt_handshake(struct VncState *vs) {
+ int ret;
+
+ if ((ret = gnutls_handshake(vs->tls.session)) < 0) {
+ if (!gnutls_error_is_fatal(ret)) {
+ VNC_DEBUG("Handshake interrupted (blocking)\n");
+ if (!gnutls_record_get_direction(vs->tls.session))
+ qemu_set_fd_handler(vs->csock, vnc_tls_handshake_io, NULL, vs);
+ else
+ qemu_set_fd_handler(vs->csock, NULL, vnc_tls_handshake_io, vs);
+ return 0;
+ }
+ VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret));
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (vs->tls.x509verify) {
+ if (vnc_tls_validate_certificate(vs) < 0) {
+ VNC_DEBUG("Client verification failed\n");
+ vnc_client_error(vs);
+ return -1;
+ } else {
+ VNC_DEBUG("Client verification passed\n");
+ }
+ }
+
+ VNC_DEBUG("Handshake done, switching to TLS data mode\n");
+ vs->tls.wiremode = VNC_WIREMODE_TLS;
+ qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
+
+ start_auth_vencrypt_subauth(vs);
+
+ return 0;
+}
+
+static void vnc_tls_handshake_io(void *opaque) {
+ struct VncState *vs = (struct VncState *)opaque;
+
+ VNC_DEBUG("Handshake IO continue\n");
+ vnc_start_vencrypt_handshake(vs);
+}
+
+
+
+#define NEED_X509_AUTH(vs) \
+ ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
+ (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
+ (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
+
+
+static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
+{
+ int auth = read_u32(data, 0);
+
+ if (auth != vs->subauth) {
+ VNC_DEBUG("Rejecting auth %d\n", auth);
+ vnc_write_u8(vs, 0); /* Reject auth */
+ vnc_flush(vs);
+ vnc_client_error(vs);
+ } else {
+ VNC_DEBUG("Accepting auth %d, setting up TLS for handshake\n", auth);
+ vnc_write_u8(vs, 1); /* Accept auth */
+ vnc_flush(vs);
+
+ if (vnc_tls_client_setup(vs, NEED_X509_AUTH(vs)) < 0) {
+ VNC_DEBUG("Failed to setup TLS\n");
+ return 0;
+ }
+
+ VNC_DEBUG("Start TLS VeNCrypt handshake process\n");
+ if (vnc_start_vencrypt_handshake(vs) < 0) {
+ VNC_DEBUG("Failed to start TLS handshake\n");
+ return 0;
+ }
+ }
+ return 0;
+}
+
+static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
+{
+ if (data[0] != 0 ||
+ data[1] != 2) {
+ VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
+ vnc_write_u8(vs, 1); /* Reject version */
+ vnc_flush(vs);
+ vnc_client_error(vs);
+ } else {
+ VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
+ vnc_write_u8(vs, 0); /* Accept version */
+ vnc_write_u8(vs, 1); /* Number of sub-auths */
+ vnc_write_u32(vs, vs->subauth); /* The supported auth */
+ vnc_flush(vs);
+ vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
+ }
+ return 0;
+}
+
+
+void start_auth_vencrypt(VncState *vs)
+{
+ /* Send VeNCrypt version 0.2 */
+ vnc_write_u8(vs, 0);
+ vnc_write_u8(vs, 2);
+
+ vnc_read_when(vs, protocol_client_vencrypt_init, 2);
+}
+
diff -r 95baa502a2b8 vnc-auth-vencrypt.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vnc-auth-vencrypt.h Thu Feb 12 12:28:24 2009 +0000
@@ -0,0 +1,33 @@
+/*
+ * QEMU VNC display driver
+ *
+ * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
+ * Copyright (C) 2006 Fabrice Bellard
+ * 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_VNC_AUTH_VENCRYPT_H__
+#define __QEMU_VNC_AUTH_VENCRYPT_H__
+
+void start_auth_vencrypt(VncState *vs);
+
+#endif /* __QEMU_VNC_AUTH_VENCRYPT_H__ */
diff -r 95baa502a2b8 vnc-tls.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vnc-tls.c Thu Feb 12 12:28:24 2009 +0000
@@ -0,0 +1,420 @@
+/*
+ * QEMU VNC display driver: TLS helpers
+ *
+ * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
+ * Copyright (C) 2006 Fabrice Bellard
+ * 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 "vnc.h"
+#include "qemu_socket.h"
+
+#if defined(_VNC_DEBUG) && _VNC_DEBUG >= 2
+/* Very verbose, so only enabled for _VNC_DEBUG >= 2 */
+static void vnc_debug_gnutls_log(int level, const char* str) {
+ VNC_DEBUG("%d %s", level, str);
+}
+#endif /* defined(_VNC_DEBUG) && _VNC_DEBUG >= 2 */
+
+
+#define DH_BITS 1024
+static gnutls_dh_params_t dh_params;
+
+static int vnc_tls_initialize(void)
+{
+ static int tlsinitialized = 0;
+
+ if (tlsinitialized)
+ return 1;
+
+ if (gnutls_global_init () < 0)
+ return 0;
+
+ /* XXX ought to re-generate diffie-hellmen params periodically */
+ if (gnutls_dh_params_init (&dh_params) < 0)
+ return 0;
+ if (gnutls_dh_params_generate2 (dh_params, DH_BITS) < 0)
+ return 0;
+
+#if defined(_VNC_DEBUG) && _VNC_DEBUG >= 2
+ gnutls_global_set_log_level(10);
+ gnutls_global_set_log_function(vnc_debug_gnutls_log);
+#endif
+
+ tlsinitialized = 1;
+
+ return 1;
+}
+
+static ssize_t vnc_tls_push(gnutls_transport_ptr_t transport,
+ const void *data,
+ size_t len) {
+ struct VncState *vs = (struct VncState *)transport;
+ int ret;
+
+ retry:
+ ret = send(vs->csock, data, len, 0);
+ if (ret < 0) {
+ if (errno == EINTR)
+ goto retry;
+ return -1;
+ }
+ return ret;
+}
+
+
+static ssize_t vnc_tls_pull(gnutls_transport_ptr_t transport,
+ void *data,
+ size_t len) {
+ struct VncState *vs = (struct VncState *)transport;
+ int ret;
+
+ retry:
+ ret = recv(vs->csock, data, len, 0);
+ if (ret < 0) {
+ if (errno == EINTR)
+ goto retry;
+ return -1;
+ }
+ return ret;
+}
+
+
+static gnutls_anon_server_credentials vnc_tls_initialize_anon_cred(void)
+{
+ gnutls_anon_server_credentials anon_cred;
+ int ret;
+
+ if ((ret = gnutls_anon_allocate_server_credentials(&anon_cred)) < 0) {
+ VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
+ return NULL;
+ }
+
+ gnutls_anon_set_server_dh_params(anon_cred, dh_params);
+
+ return anon_cred;
+}
+
+
+static gnutls_certificate_credentials_t vnc_tls_initialize_x509_cred(VncState *vs)
+{
+ gnutls_certificate_credentials_t x509_cred;
+ int ret;
+
+ if (!vs->tls.x509cacert) {
+ VNC_DEBUG("No CA x509 certificate specified\n");
+ return NULL;
+ }
+ if (!vs->tls.x509cert) {
+ VNC_DEBUG("No server x509 certificate specified\n");
+ return NULL;
+ }
+ if (!vs->tls.x509key) {
+ VNC_DEBUG("No server private key specified\n");
+ return NULL;
+ }
+
+ if ((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0) {
+ VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
+ return NULL;
+ }
+ if ((ret = gnutls_certificate_set_x509_trust_file(x509_cred,
+ vs->tls.x509cacert,
+ GNUTLS_X509_FMT_PEM)) < 0) {
+ VNC_DEBUG("Cannot load CA certificate %s\n", gnutls_strerror(ret));
+ gnutls_certificate_free_credentials(x509_cred);
+ return NULL;
+ }
+
+ if ((ret = gnutls_certificate_set_x509_key_file (x509_cred,
+ vs->tls.x509cert,
+ vs->tls.x509key,
+ GNUTLS_X509_FMT_PEM)) < 0) {
+ VNC_DEBUG("Cannot load certificate & key %s\n", gnutls_strerror(ret));
+ gnutls_certificate_free_credentials(x509_cred);
+ return NULL;
+ }
+
+ if (vs->tls.x509cacrl) {
+ if ((ret = gnutls_certificate_set_x509_crl_file(x509_cred,
+ vs->tls.x509cacrl,
+ GNUTLS_X509_FMT_PEM)) < 0) {
+ VNC_DEBUG("Cannot load CRL %s\n", gnutls_strerror(ret));
+ gnutls_certificate_free_credentials(x509_cred);
+ return NULL;
+ }
+ }
+
+ gnutls_certificate_set_dh_params (x509_cred, dh_params);
+
+ return x509_cred;
+}
+
+
+int vnc_tls_validate_certificate(struct VncState *vs)
+{
+ int ret;
+ unsigned int status;
+ const gnutls_datum_t *certs;
+ unsigned int nCerts, i;
+ time_t now;
+
+ VNC_DEBUG("Validating client certificate\n");
+ if ((ret = gnutls_certificate_verify_peers2 (vs->tls.session, &status)) < 0) {
+ VNC_DEBUG("Verify failed %s\n", gnutls_strerror(ret));
+ return -1;
+ }
+
+ if ((now = time(NULL)) == ((time_t)-1)) {
+ return -1;
+ }
+
+ if (status != 0) {
+ if (status & GNUTLS_CERT_INVALID)
+ VNC_DEBUG("The certificate is not trusted.\n");
+
+ if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
+ VNC_DEBUG("The certificate hasn't got a known issuer.\n");
+
+ if (status & GNUTLS_CERT_REVOKED)
+ VNC_DEBUG("The certificate has been revoked.\n");
+
+ if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
+ VNC_DEBUG("The certificate uses an insecure algorithm\n");
+
+ return -1;
+ } else {
+ VNC_DEBUG("Certificate is valid!\n");
+ }
+
+ /* Only support x509 for now */
+ if (gnutls_certificate_type_get(vs->tls.session) != GNUTLS_CRT_X509)
+ return -1;
+
+ if (!(certs = gnutls_certificate_get_peers(vs->tls.session, &nCerts)))
+ return -1;
+
+ for (i = 0 ; i < nCerts ; i++) {
+ gnutls_x509_crt_t cert;
+ VNC_DEBUG ("Checking certificate chain %d\n", i);
+ if (gnutls_x509_crt_init (&cert) < 0)
+ return -1;
+
+ if (gnutls_x509_crt_import(cert, &certs[i], GNUTLS_X509_FMT_DER) < 0) {
+ gnutls_x509_crt_deinit (cert);
+ return -1;
+ }
+
+ if (gnutls_x509_crt_get_expiration_time (cert) < now) {
+ VNC_DEBUG("The certificate has expired\n");
+ gnutls_x509_crt_deinit (cert);
+ return -1;
+ }
+
+ if (gnutls_x509_crt_get_activation_time (cert) > now) {
+ VNC_DEBUG("The certificate is not yet activated\n");
+ gnutls_x509_crt_deinit (cert);
+ return -1;
+ }
+
+ if (gnutls_x509_crt_get_activation_time (cert) > now) {
+ VNC_DEBUG("The certificate is not yet activated\n");
+ gnutls_x509_crt_deinit (cert);
+ return -1;
+ }
+
+ gnutls_x509_crt_deinit (cert);
+ }
+
+ return 0;
+}
+
+
+int vnc_tls_client_setup(struct VncState *vs, int needX509Creds) {
+ static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 };
+ static const int protocol_priority[]= { GNUTLS_TLS1_1, GNUTLS_TLS1_0, GNUTLS_SSL3, 0 };
+ static const int kx_anon[] = {GNUTLS_KX_ANON_DH, 0};
+ static const int kx_x509[] = {GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0};
+
+ VNC_DEBUG("Do TLS setup\n");
+ if (vnc_tls_initialize() < 0) {
+ VNC_DEBUG("Failed to init TLS\n");
+ vnc_client_error(vs);
+ return -1;
+ }
+ if (vs->tls.session == NULL) {
+ if (gnutls_init(&vs->tls.session, GNUTLS_SERVER) < 0) {
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (gnutls_set_default_priority(vs->tls.session) < 0) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (gnutls_kx_set_priority(vs->tls.session, needX509Creds ? kx_x509 : kx_anon) < 0) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (gnutls_certificate_type_set_priority(vs->tls.session, cert_type_priority) < 0) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (gnutls_protocol_set_priority(vs->tls.session, protocol_priority) < 0) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (needX509Creds) {
+ gnutls_certificate_server_credentials x509_cred = vnc_tls_initialize_x509_cred(vs);
+ if (!x509_cred) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ vnc_client_error(vs);
+ return -1;
+ }
+ if (gnutls_credentials_set(vs->tls.session, GNUTLS_CRD_CERTIFICATE, x509_cred) < 0) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ gnutls_certificate_free_credentials(x509_cred);
+ vnc_client_error(vs);
+ return -1;
+ }
+ if (vs->tls.x509verify) {
+ VNC_DEBUG("Requesting a client certificate\n");
+ gnutls_certificate_server_set_request (vs->tls.session, GNUTLS_CERT_REQUEST);
+ }
+
+ } else {
+ gnutls_anon_server_credentials anon_cred = vnc_tls_initialize_anon_cred();
+ if (!anon_cred) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ vnc_client_error(vs);
+ return -1;
+ }
+ if (gnutls_credentials_set(vs->tls.session, GNUTLS_CRD_ANON, anon_cred) < 0) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ gnutls_anon_free_server_credentials(anon_cred);
+ vnc_client_error(vs);
+ return -1;
+ }
+ }
+
+ gnutls_transport_set_ptr(vs->tls.session, (gnutls_transport_ptr_t)vs);
+ gnutls_transport_set_push_function(vs->tls.session, vnc_tls_push);
+ gnutls_transport_set_pull_function(vs->tls.session, vnc_tls_pull);
+ }
+ return 0;
+}
+
+
+void vnc_tls_client_cleanup(struct VncState *vs)
+{
+ if (vs->tls.session) {
+ gnutls_deinit(vs->tls.session);
+ vs->tls.session = NULL;
+ }
+ vs->tls.wiremode = VNC_WIREMODE_CLEAR;
+}
+
+
+
+static int vnc_set_x509_credential(VncState *vs,
+ const char *certdir,
+ const char *filename,
+ char **cred,
+ int ignoreMissing)
+{
+ struct stat sb;
+
+ if (*cred) {
+ qemu_free(*cred);
+ *cred = NULL;
+ }
+
+ *cred = qemu_malloc(strlen(certdir) + strlen(filename) + 2);
+
+ strcpy(*cred, certdir);
+ strcat(*cred, "/");
+ strcat(*cred, filename);
+
+ VNC_DEBUG("Check %s\n", *cred);
+ if (stat(*cred, &sb) < 0) {
+ qemu_free(*cred);
+ *cred = NULL;
+ if (ignoreMissing && errno == ENOENT)
+ return 0;
+ return -1;
+ }
+
+ return 0;
+}
+
+
+#define X509_CA_CERT_FILE "ca-cert.pem"
+#define X509_CA_CRL_FILE "ca-crl.pem"
+#define X509_SERVER_KEY_FILE "server-key.pem"
+#define X509_SERVER_CERT_FILE "server-cert.pem"
+
+
+int vnc_tls_set_x509_creds_dir(VncState *vs,
+ const char *certdir)
+{
+ if (vnc_set_x509_credential(vs, certdir, X509_CA_CERT_FILE, &vs->tls.x509cacert, 0) < 0)
+ goto cleanup;
+ if (vnc_set_x509_credential(vs, certdir, X509_CA_CRL_FILE, &vs->tls.x509cacrl, 1) < 0)
+ goto cleanup;
+ if (vnc_set_x509_credential(vs, certdir, X509_SERVER_CERT_FILE, &vs->tls.x509cert, 0) < 0)
+ goto cleanup;
+ if (vnc_set_x509_credential(vs, certdir, X509_SERVER_KEY_FILE, &vs->tls.x509key, 0) < 0)
+ goto cleanup;
+
+ return 0;
+
+ cleanup:
+ qemu_free(vs->tls.x509cacert);
+ qemu_free(vs->tls.x509cacrl);
+ qemu_free(vs->tls.x509cert);
+ qemu_free(vs->tls.x509key);
+ vs->tls.x509cacert = vs->tls.x509cacrl = vs->tls.x509cert = vs->tls.x509key = NULL;
+ return -1;
+}
+
+/*
+ * Local variables:
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 8
+ * End:
+ */
diff -r 95baa502a2b8 vnc-tls.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vnc-tls.h Thu Feb 12 12:28:24 2009 +0000
@@ -0,0 +1,70 @@
+/*
+ * QEMU VNC display driver. TLS helpers
+ *
+ * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
+ * Copyright (C) 2006 Fabrice Bellard
+ * 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_VNC_TLS_H__
+#define __QEMU_VNC_TLS_H__
+
+#include <gnutls/gnutls.h>
+#include <gnutls/x509.h>
+
+enum {
+ VNC_WIREMODE_CLEAR,
+ VNC_WIREMODE_TLS,
+};
+
+struct VncStateTLS {
+ int x509verify; /* Non-zero if server requests & validates client cert */
+
+ /* Paths to x509 certs/keys */
+ char *x509cacert;
+ char *x509cacrl;
+ char *x509cert;
+ char *x509key;
+
+ /* Whether data is being TLS encrypted yet */
+ int wiremode;
+ gnutls_session_t session;
+};
+
+int vnc_tls_client_setup(VncState *vs, int x509Creds);
+void vnc_tls_client_cleanup(VncState *vs);
+
+int vnc_tls_validate_certificate(VncState *vs);
+
+int vnc_tls_set_x509_creds_dir(VncState *vs,
+ const char *path);
+
+
+#endif /* __QEMU_VNC_TLS_H__ */
+
+/*
+ * Local variables:
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 8
+ * End:
+ */
diff -r 95baa502a2b8 vnc.c
--- a/vnc.c Thu Feb 12 12:28:21 2009 +0000
+++ b/vnc.c Thu Feb 12 12:28:24 2009 +0000
@@ -35,21 +35,6 @@
#include "vnc_keysym.h"
#include "d3des.h"
-// #define _VNC_DEBUG 1
-
-#ifdef _VNC_DEBUG
-#define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
-
-#if defined(CONFIG_VNC_TLS) && _VNC_DEBUG >= 2
-/* Very verbose, so only enabled for _VNC_DEBUG >= 2 */
-static void vnc_debug_gnutls_log(int level, const char* str) {
- VNC_DEBUG("%d %s", level, str);
-}
-#endif /* CONFIG_VNC_TLS && _VNC_DEBUG */
-#else
-#define VNC_DEBUG(fmt, ...) do { } while (0)
-#endif
-
#define count_bits(c, v) { \
for (c = 0; v; v >>= 1) \
{ \
@@ -198,14 +183,7 @@ static inline uint32_t vnc_has_feature(V
3) resolutions > 1024
*/
-static void vnc_write(VncState *vs, const void *data, size_t len);
-static void vnc_write_u32(VncState *vs, uint32_t value);
-static void vnc_write_s32(VncState *vs, int32_t value);
-static void vnc_write_u16(VncState *vs, uint16_t value);
-static void vnc_write_u8(VncState *vs, uint8_t value);
-static void vnc_flush(VncState *vs);
static void vnc_update_client(void *opaque);
-static void vnc_client_read(void *opaque);
static void vnc_colordepth(DisplayState *ds);
@@ -840,11 +818,7 @@ static int vnc_client_io_error(VncState
buffer_reset(&vs->output);
vs->need_update = 0;
#ifdef CONFIG_VNC_TLS
- if (vs->tls_session) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- }
- vs->wiremode = VNC_WIREMODE_CLEAR;
+ vnc_tls_client_cleanup(vs);
#endif /* CONFIG_VNC_TLS */
audio_del(vs);
return 0;
@@ -852,19 +826,20 @@ static int vnc_client_io_error(VncState
return ret;
}
-static void vnc_client_error(VncState *vs)
+
+void vnc_client_error(VncState *vs)
{
vnc_client_io_error(vs, -1, EINVAL);
}
-static void vnc_client_write(void *opaque)
+void vnc_client_write(void *opaque)
{
long ret;
VncState *vs = opaque;
#ifdef CONFIG_VNC_TLS
- if (vs->tls_session) {
- ret = gnutls_write(vs->tls_session, vs->output.buffer, vs->output.offset);
+ if (vs->tls.session) {
+ ret = gnutls_write(vs->tls.session, vs->output.buffer, vs->output.offset);
if (ret < 0) {
if (ret == GNUTLS_E_AGAIN)
errno = EAGAIN;
@@ -887,13 +862,13 @@ static void vnc_client_write(void *opaqu
}
}
-static void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
+void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
{
vs->read_handler = func;
vs->read_handler_expect = expecting;
}
-static void vnc_client_read(void *opaque)
+void vnc_client_read(void *opaque)
{
VncState *vs = opaque;
long ret;
@@ -901,8 +876,8 @@ static void vnc_client_read(void *opaque
buffer_reserve(&vs->input, 4096);
#ifdef CONFIG_VNC_TLS
- if (vs->tls_session) {
- ret = gnutls_read(vs->tls_session, buffer_end(&vs->input), 4096);
+ if (vs->tls.session) {
+ ret = gnutls_read(vs->tls.session, buffer_end(&vs->input), 4096);
if (ret < 0) {
if (ret == GNUTLS_E_AGAIN)
errno = EAGAIN;
@@ -936,7 +911,7 @@ static void vnc_client_read(void *opaque
}
}
-static void vnc_write(VncState *vs, const void *data, size_t len)
+void vnc_write(VncState *vs, const void *data, size_t len)
{
buffer_reserve(&vs->output, len);
@@ -947,12 +922,12 @@ static void vnc_write(VncState *vs, cons
buffer_append(&vs->output, data, len);
}
-static void vnc_write_s32(VncState *vs, int32_t value)
+void vnc_write_s32(VncState *vs, int32_t value)
{
vnc_write_u32(vs, *(uint32_t *)&value);
}
-static void vnc_write_u32(VncState *vs, uint32_t value)
+void vnc_write_u32(VncState *vs, uint32_t value)
{
uint8_t buf[4];
@@ -964,7 +939,7 @@ static void vnc_write_u32(VncState *vs,
vnc_write(vs, buf, 4);
}
-static void vnc_write_u16(VncState *vs, uint16_t value)
+void vnc_write_u16(VncState *vs, uint16_t value)
{
uint8_t buf[2];
@@ -974,74 +949,39 @@ static void vnc_write_u16(VncState *vs,
vnc_write(vs, buf, 2);
}
-static void vnc_write_u8(VncState *vs, uint8_t value)
+void vnc_write_u8(VncState *vs, uint8_t value)
{
vnc_write(vs, (char *)&value, 1);
}
-static void vnc_flush(VncState *vs)
+void vnc_flush(VncState *vs)
{
if (vs->output.offset)
vnc_client_write(vs);
}
-static uint8_t read_u8(uint8_t *data, size_t offset)
+uint8_t read_u8(uint8_t *data, size_t offset)
{
return data[offset];
}
-static uint16_t read_u16(uint8_t *data, size_t offset)
+uint16_t read_u16(uint8_t *data, size_t offset)
{
return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF);
}
-static int32_t read_s32(uint8_t *data, size_t offset)
+int32_t read_s32(uint8_t *data, size_t offset)
{
return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) |
(data[offset + 2] << 8) | data[offset + 3]);
}
-static uint32_t read_u32(uint8_t *data, size_t offset)
+uint32_t read_u32(uint8_t *data, size_t offset)
{
return ((data[offset] << 24) | (data[offset + 1] << 16) |
(data[offset + 2] << 8) | data[offset + 3]);
}
-#ifdef CONFIG_VNC_TLS
-static ssize_t vnc_tls_push(gnutls_transport_ptr_t transport,
- const void *data,
- size_t len) {
- struct VncState *vs = (struct VncState *)transport;
- int ret;
-
- retry:
- ret = send(vs->csock, data, len, 0);
- if (ret < 0) {
- if (errno == EINTR)
- goto retry;
- return -1;
- }
- return ret;
-}
-
-
-static ssize_t vnc_tls_pull(gnutls_transport_ptr_t transport,
- void *data,
- size_t len) {
- struct VncState *vs = (struct VncState *)transport;
- int ret;
-
- retry:
- ret = recv(vs->csock, data, len, 0);
- if (ret < 0) {
- if (errno == EINTR)
- goto retry;
- return -1;
- }
- return ret;
-}
-#endif /* CONFIG_VNC_TLS */
-
static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
{
}
@@ -1627,6 +1567,11 @@ static int protocol_client_init(VncState
return 0;
}
+void start_client_init(VncState *vs)
+{
+ vnc_read_when(vs, protocol_client_init, 1);
+}
+
static void make_challenge(VncState *vs)
{
int i;
@@ -1682,12 +1627,12 @@ static int protocol_client_auth_vnc(VncS
vnc_write_u32(vs, 0); /* Accept auth */
vnc_flush(vs);
- vnc_read_when(vs, protocol_client_init, 1);
+ start_client_init(vs);
}
return 0;
}
-static int start_auth_vnc(VncState *vs)
+void start_auth_vnc(VncState *vs)
{
make_challenge(vs);
/* Send client a 'random' challenge */
@@ -1695,411 +1640,9 @@ static int start_auth_vnc(VncState *vs)
vnc_flush(vs);
vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
- return 0;
}
-#ifdef CONFIG_VNC_TLS
-#define DH_BITS 1024
-static gnutls_dh_params_t dh_params;
-
-static int vnc_tls_initialize(void)
-{
- static int tlsinitialized = 0;
-
- if (tlsinitialized)
- return 1;
-
- if (gnutls_global_init () < 0)
- return 0;
-
- /* XXX ought to re-generate diffie-hellmen params periodically */
- if (gnutls_dh_params_init (&dh_params) < 0)
- return 0;
- if (gnutls_dh_params_generate2 (dh_params, DH_BITS) < 0)
- return 0;
-
-#if defined(_VNC_DEBUG) && _VNC_DEBUG >= 2
- gnutls_global_set_log_level(10);
- gnutls_global_set_log_function(vnc_debug_gnutls_log);
-#endif
-
- tlsinitialized = 1;
-
- return 1;
-}
-
-static gnutls_anon_server_credentials vnc_tls_initialize_anon_cred(void)
-{
- gnutls_anon_server_credentials anon_cred;
- int ret;
-
- if ((ret = gnutls_anon_allocate_server_credentials(&anon_cred)) < 0) {
- VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
- return NULL;
- }
-
- gnutls_anon_set_server_dh_params(anon_cred, dh_params);
-
- return anon_cred;
-}
-
-
-static gnutls_certificate_credentials_t vnc_tls_initialize_x509_cred(VncState *vs)
-{
- gnutls_certificate_credentials_t x509_cred;
- int ret;
-
- if (!vs->x509cacert) {
- VNC_DEBUG("No CA x509 certificate specified\n");
- return NULL;
- }
- if (!vs->x509cert) {
- VNC_DEBUG("No server x509 certificate specified\n");
- return NULL;
- }
- if (!vs->x509key) {
- VNC_DEBUG("No server private key specified\n");
- return NULL;
- }
-
- if ((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0) {
- VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
- return NULL;
- }
- if ((ret = gnutls_certificate_set_x509_trust_file(x509_cred,
- vs->x509cacert,
- GNUTLS_X509_FMT_PEM)) < 0) {
- VNC_DEBUG("Cannot load CA certificate %s\n", gnutls_strerror(ret));
- gnutls_certificate_free_credentials(x509_cred);
- return NULL;
- }
-
- if ((ret = gnutls_certificate_set_x509_key_file (x509_cred,
- vs->x509cert,
- vs->x509key,
- GNUTLS_X509_FMT_PEM)) < 0) {
- VNC_DEBUG("Cannot load certificate & key %s\n", gnutls_strerror(ret));
- gnutls_certificate_free_credentials(x509_cred);
- return NULL;
- }
-
- if (vs->x509cacrl) {
- if ((ret = gnutls_certificate_set_x509_crl_file(x509_cred,
- vs->x509cacrl,
- GNUTLS_X509_FMT_PEM)) < 0) {
- VNC_DEBUG("Cannot load CRL %s\n", gnutls_strerror(ret));
- gnutls_certificate_free_credentials(x509_cred);
- return NULL;
- }
- }
-
- gnutls_certificate_set_dh_params (x509_cred, dh_params);
-
- return x509_cred;
-}
-
-static int vnc_validate_certificate(struct VncState *vs)
-{
- int ret;
- unsigned int status;
- const gnutls_datum_t *certs;
- unsigned int nCerts, i;
- time_t now;
-
- VNC_DEBUG("Validating client certificate\n");
- if ((ret = gnutls_certificate_verify_peers2 (vs->tls_session, &status)) < 0) {
- VNC_DEBUG("Verify failed %s\n", gnutls_strerror(ret));
- return -1;
- }
-
- if ((now = time(NULL)) == ((time_t)-1)) {
- return -1;
- }
-
- if (status != 0) {
- if (status & GNUTLS_CERT_INVALID)
- VNC_DEBUG("The certificate is not trusted.\n");
-
- if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
- VNC_DEBUG("The certificate hasn't got a known issuer.\n");
-
- if (status & GNUTLS_CERT_REVOKED)
- VNC_DEBUG("The certificate has been revoked.\n");
-
- if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
- VNC_DEBUG("The certificate uses an insecure algorithm\n");
-
- return -1;
- } else {
- VNC_DEBUG("Certificate is valid!\n");
- }
-
- /* Only support x509 for now */
- if (gnutls_certificate_type_get(vs->tls_session) != GNUTLS_CRT_X509)
- return -1;
-
- if (!(certs = gnutls_certificate_get_peers(vs->tls_session, &nCerts)))
- return -1;
-
- for (i = 0 ; i < nCerts ; i++) {
- gnutls_x509_crt_t cert;
- VNC_DEBUG ("Checking certificate chain %d\n", i);
- if (gnutls_x509_crt_init (&cert) < 0)
- return -1;
-
- if (gnutls_x509_crt_import(cert, &certs[i], GNUTLS_X509_FMT_DER) < 0) {
- gnutls_x509_crt_deinit (cert);
- return -1;
- }
-
- if (gnutls_x509_crt_get_expiration_time (cert) < now) {
- VNC_DEBUG("The certificate has expired\n");
- gnutls_x509_crt_deinit (cert);
- return -1;
- }
-
- if (gnutls_x509_crt_get_activation_time (cert) > now) {
- VNC_DEBUG("The certificate is not yet activated\n");
- gnutls_x509_crt_deinit (cert);
- return -1;
- }
-
- if (gnutls_x509_crt_get_activation_time (cert) > now) {
- VNC_DEBUG("The certificate is not yet activated\n");
- gnutls_x509_crt_deinit (cert);
- return -1;
- }
-
- gnutls_x509_crt_deinit (cert);
- }
-
- return 0;
-}
-
-
-static int start_auth_vencrypt_subauth(VncState *vs)
-{
- switch (vs->subauth) {
- case VNC_AUTH_VENCRYPT_TLSNONE:
- case VNC_AUTH_VENCRYPT_X509NONE:
- VNC_DEBUG("Accept TLS auth none\n");
- vnc_write_u32(vs, 0); /* Accept auth completion */
- vnc_read_when(vs, protocol_client_init, 1);
- break;
-
- case VNC_AUTH_VENCRYPT_TLSVNC:
- case VNC_AUTH_VENCRYPT_X509VNC:
- VNC_DEBUG("Start TLS auth VNC\n");
- return start_auth_vnc(vs);
-
- default: /* Should not be possible, but just in case */
- VNC_DEBUG("Reject auth %d\n", vs->auth);
- vnc_write_u8(vs, 1);
- if (vs->minor >= 8) {
- static const char err[] = "Unsupported authentication type";
- vnc_write_u32(vs, sizeof(err));
- vnc_write(vs, err, sizeof(err));
- }
- vnc_client_error(vs);
- }
-
- return 0;
-}
-
-static void vnc_handshake_io(void *opaque);
-
-static int vnc_continue_handshake(struct VncState *vs) {
- int ret;
-
- if ((ret = gnutls_handshake(vs->tls_session)) < 0) {
- if (!gnutls_error_is_fatal(ret)) {
- VNC_DEBUG("Handshake interrupted (blocking)\n");
- if (!gnutls_record_get_direction(vs->tls_session))
- qemu_set_fd_handler(vs->csock, vnc_handshake_io, NULL, vs);
- else
- qemu_set_fd_handler(vs->csock, NULL, vnc_handshake_io, vs);
- return 0;
- }
- VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret));
- vnc_client_error(vs);
- return -1;
- }
-
- if (vs->x509verify) {
- if (vnc_validate_certificate(vs) < 0) {
- VNC_DEBUG("Client verification failed\n");
- vnc_client_error(vs);
- return -1;
- } else {
- VNC_DEBUG("Client verification passed\n");
- }
- }
-
- VNC_DEBUG("Handshake done, switching to TLS data mode\n");
- vs->wiremode = VNC_WIREMODE_TLS;
- qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
-
- return start_auth_vencrypt_subauth(vs);
-}
-
-static void vnc_handshake_io(void *opaque) {
- struct VncState *vs = (struct VncState *)opaque;
-
- VNC_DEBUG("Handshake IO continue\n");
- vnc_continue_handshake(vs);
-}
-
-#define NEED_X509_AUTH(vs) \
- ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
- (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
- (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
-
-
-static int vnc_start_tls(struct VncState *vs) {
- static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 };
- static const int protocol_priority[]= { GNUTLS_TLS1_1, GNUTLS_TLS1_0, GNUTLS_SSL3, 0 };
- static const int kx_anon[] = {GNUTLS_KX_ANON_DH, 0};
- static const int kx_x509[] = {GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0};
-
- VNC_DEBUG("Do TLS setup\n");
- if (vnc_tls_initialize() < 0) {
- VNC_DEBUG("Failed to init TLS\n");
- vnc_client_error(vs);
- return -1;
- }
- if (vs->tls_session == NULL) {
- if (gnutls_init(&vs->tls_session, GNUTLS_SERVER) < 0) {
- vnc_client_error(vs);
- return -1;
- }
-
- if (gnutls_set_default_priority(vs->tls_session) < 0) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- vnc_client_error(vs);
- return -1;
- }
-
- if (gnutls_kx_set_priority(vs->tls_session, NEED_X509_AUTH(vs) ? kx_x509 : kx_anon) < 0) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- vnc_client_error(vs);
- return -1;
- }
-
- if (gnutls_certificate_type_set_priority(vs->tls_session, cert_type_priority) < 0) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- vnc_client_error(vs);
- return -1;
- }
-
- if (gnutls_protocol_set_priority(vs->tls_session, protocol_priority) < 0) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- vnc_client_error(vs);
- return -1;
- }
-
- if (NEED_X509_AUTH(vs)) {
- gnutls_certificate_server_credentials x509_cred = vnc_tls_initialize_x509_cred(vs);
- if (!x509_cred) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- vnc_client_error(vs);
- return -1;
- }
- if (gnutls_credentials_set(vs->tls_session, GNUTLS_CRD_CERTIFICATE, x509_cred) < 0) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- gnutls_certificate_free_credentials(x509_cred);
- vnc_client_error(vs);
- return -1;
- }
- if (vs->x509verify) {
- VNC_DEBUG("Requesting a client certificate\n");
- gnutls_certificate_server_set_request (vs->tls_session, GNUTLS_CERT_REQUEST);
- }
-
- } else {
- gnutls_anon_server_credentials anon_cred = vnc_tls_initialize_anon_cred();
- if (!anon_cred) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- vnc_client_error(vs);
- return -1;
- }
- if (gnutls_credentials_set(vs->tls_session, GNUTLS_CRD_ANON, anon_cred) < 0) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- gnutls_anon_free_server_credentials(anon_cred);
- vnc_client_error(vs);
- return -1;
- }
- }
-
- gnutls_transport_set_ptr(vs->tls_session, (gnutls_transport_ptr_t)vs);
- gnutls_transport_set_push_function(vs->tls_session, vnc_tls_push);
- gnutls_transport_set_pull_function(vs->tls_session, vnc_tls_pull);
- }
-
- VNC_DEBUG("Start TLS handshake process\n");
- return vnc_continue_handshake(vs);
-}
-
-static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
-{
- int auth = read_u32(data, 0);
-
- if (auth != vs->subauth) {
- VNC_DEBUG("Rejecting auth %d\n", auth);
- vnc_write_u8(vs, 0); /* Reject auth */
- vnc_flush(vs);
- vnc_client_error(vs);
- } else {
- VNC_DEBUG("Accepting auth %d, starting handshake\n", auth);
- vnc_write_u8(vs, 1); /* Accept auth */
- vnc_flush(vs);
-
- if (vnc_start_tls(vs) < 0) {
- VNC_DEBUG("Failed to complete TLS\n");
- return 0;
- }
- }
- return 0;
-}
-
-static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
-{
- if (data[0] != 0 ||
- data[1] != 2) {
- VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
- vnc_write_u8(vs, 1); /* Reject version */
- vnc_flush(vs);
- vnc_client_error(vs);
- } else {
- VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
- vnc_write_u8(vs, 0); /* Accept version */
- vnc_write_u8(vs, 1); /* Number of sub-auths */
- vnc_write_u32(vs, vs->subauth); /* The supported auth */
- vnc_flush(vs);
- vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
- }
- return 0;
-}
-
-static int start_auth_vencrypt(VncState *vs)
-{
- /* Send VeNCrypt version 0.2 */
- vnc_write_u8(vs, 0);
- vnc_write_u8(vs, 2);
-
- vnc_read_when(vs, protocol_client_vencrypt_init, 2);
- return 0;
-}
-#endif /* CONFIG_VNC_TLS */
-
static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len)
{
/* We only advertise 1 auth scheme at a time, so client
@@ -2122,17 +1665,19 @@ static int protocol_client_auth(VncState
vnc_write_u32(vs, 0); /* Accept auth completion */
vnc_flush(vs);
}
- vnc_read_when(vs, protocol_client_init, 1);
+ start_client_init(vs);
break;
case VNC_AUTH_VNC:
VNC_DEBUG("Start VNC auth\n");
- return start_auth_vnc(vs);
+ start_auth_vnc(vs);
+ break;
#ifdef CONFIG_VNC_TLS
case VNC_AUTH_VENCRYPT:
VNC_DEBUG("Accept VeNCrypt auth\n");;
- return start_auth_vencrypt(vs);
+ start_auth_vencrypt(vs);
+ break;
#endif /* CONFIG_VNC_TLS */
default: /* Should not be possible, but just in case */
@@ -2185,7 +1730,7 @@ static int protocol_version(VncState *vs
VNC_DEBUG("Tell client auth none\n");
vnc_write_u32(vs, vs->auth);
vnc_flush(vs);
- vnc_read_when(vs, protocol_client_init, 1);
+ start_client_init(vs);
} else if (vs->auth == VNC_AUTH_VNC) {
VNC_DEBUG("Tell client VNC auth\n");
vnc_write_u32(vs, vs->auth);
@@ -2282,61 +1827,6 @@ void vnc_display_init(DisplayState *ds)
vs->as.endianness = 0;
}
-#ifdef CONFIG_VNC_TLS
-static int vnc_set_x509_credential(VncState *vs,
- const char *certdir,
- const char *filename,
- char **cred,
- int ignoreMissing)
-{
- struct stat sb;
-
- if (*cred) {
- qemu_free(*cred);
- *cred = NULL;
- }
-
- *cred = qemu_malloc(strlen(certdir) + strlen(filename) + 2);
-
- strcpy(*cred, certdir);
- strcat(*cred, "/");
- strcat(*cred, filename);
-
- VNC_DEBUG("Check %s\n", *cred);
- if (stat(*cred, &sb) < 0) {
- qemu_free(*cred);
- *cred = NULL;
- if (ignoreMissing && errno == ENOENT)
- return 0;
- return -1;
- }
-
- return 0;
-}
-
-static int vnc_set_x509_credential_dir(VncState *vs,
- const char *certdir)
-{
- if (vnc_set_x509_credential(vs, certdir, X509_CA_CERT_FILE, &vs->x509cacert, 0) < 0)
- goto cleanup;
- if (vnc_set_x509_credential(vs, certdir, X509_CA_CRL_FILE, &vs->x509cacrl, 1) < 0)
- goto cleanup;
- if (vnc_set_x509_credential(vs, certdir, X509_SERVER_CERT_FILE, &vs->x509cert, 0) < 0)
- goto cleanup;
- if (vnc_set_x509_credential(vs, certdir, X509_SERVER_KEY_FILE, &vs->x509key, 0) < 0)
- goto cleanup;
-
- return 0;
-
- cleanup:
- qemu_free(vs->x509cacert);
- qemu_free(vs->x509cacrl);
- qemu_free(vs->x509cert);
- qemu_free(vs->x509key);
- vs->x509cacert = vs->x509cacrl = vs->x509cert = vs->x509key = NULL;
- return -1;
-}
-#endif /* CONFIG_VNC_TLS */
void vnc_display_close(DisplayState *ds)
{
@@ -2361,17 +1851,13 @@ void vnc_display_close(DisplayState *ds)
buffer_reset(&vs->output);
vs->need_update = 0;
#ifdef CONFIG_VNC_TLS
- if (vs->tls_session) {
- gnutls_deinit(vs->tls_session);
- vs->tls_session = NULL;
- }
- vs->wiremode = VNC_WIREMODE_CLEAR;
+ vnc_tls_client_cleanup(vs);
#endif /* CONFIG_VNC_TLS */
}
vs->auth = VNC_AUTH_INVALID;
#ifdef CONFIG_VNC_TLS
vs->subauth = VNC_AUTH_INVALID;
- vs->x509verify = 0;
+ vs->tls.x509verify = 0;
#endif
audio_del(vs);
}
@@ -2428,7 +1914,7 @@ int vnc_display_open(DisplayState *ds, c
char *start, *end;
x509 = 1; /* Require x509 certificates */
if (strncmp(options, "x509verify", 10) == 0)
- vs->x509verify = 1; /* ...and verify client certs */
+ vs->tls.x509verify = 1; /* ...and verify client certs */
/* Now check for 'x509=/some/path' postfix
* and use that to setup x509 certificate/key paths */
@@ -2439,7 +1925,7 @@ int vnc_display_open(DisplayState *ds, c
char *path = qemu_strndup(start + 1, len);
VNC_DEBUG("Trying certificate path '%s'\n", path);
- if (vnc_set_x509_credential_dir(vs, path) < 0) {
+ if (vnc_tls_set_x509_creds_dir(vs, path) < 0) {
fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path);
qemu_free(path);
qemu_free(vs->display);
diff -r 95baa502a2b8 vnc.h
--- a/vnc.h Thu Feb 12 12:28:21 2009 +0000
+++ b/vnc.h Thu Feb 12 12:28:24 2009 +0000
@@ -33,19 +33,22 @@
#include "audio/audio.h"
#include <zlib.h>
-#ifdef CONFIG_VNC_TLS
-#include <gnutls/gnutls.h>
-#include <gnutls/x509.h>
-#endif /* CONFIG_VNC_TLS */
-
#include "keymaps.h"
+// #define _VNC_DEBUG 1
+
/*****************************************************************************
*
* Core data structures
*
*****************************************************************************/
+#ifdef _VNC_DEBUG
+#define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define VNC_DEBUG(fmt, ...) do { } while (0)
+#endif
+
#define VNC_MAX_WIDTH 2048
@@ -74,6 +77,11 @@ typedef struct Buffer
uint8_t *buffer;
} Buffer;
+#ifdef CONFIG_VNC_TLS
+#include "vnc-tls.h"
+#include "vnc-auth-vencrypt.h"
+#endif
+
struct VncState
{
QEMUTimer *timer;
@@ -99,21 +107,11 @@ struct VncState
char *password;
int auth;
#ifdef CONFIG_VNC_TLS
- int subauth;
- int x509verify;
-
- char *x509cacert;
- char *x509cacrl;
- char *x509cert;
- char *x509key;
+ int subauth; /* Used by VeNCrypt */
+ struct VncStateTLS tls;
#endif
char challenge[VNC_AUTH_CHALLENGE_SIZE];
-#ifdef CONFIG_VNC_TLS
- int wiremode;
- gnutls_session_t tls_session;
-#endif
-
Buffer output;
Buffer input;
kbd_layout_t *kbd_layout;
@@ -153,12 +151,6 @@ enum {
VNC_AUTH_VENCRYPT = 19
};
-#ifdef CONFIG_VNC_TLS
-enum {
- VNC_WIREMODE_CLEAR,
- VNC_WIREMODE_TLS,
-};
-
enum {
VNC_AUTH_VENCRYPT_PLAIN = 256,
VNC_AUTH_VENCRYPT_TLSNONE = 257,
@@ -169,12 +161,6 @@ enum {
VNC_AUTH_VENCRYPT_X509PLAIN = 262,
};
-#define X509_CA_CERT_FILE "ca-cert.pem"
-#define X509_CA_CRL_FILE "ca-crl.pem"
-#define X509_SERVER_KEY_FILE "server-key.pem"
-#define X509_SERVER_CERT_FILE "server-cert.pem"
-
-#endif /* CONFIG_VNC_TLS */
/*****************************************************************************
*
@@ -243,4 +229,38 @@ enum {
#define VNC_FEATURE_TIGHT_MASK (1 << VNC_FEATURE_TIGHT)
#define VNC_FEATURE_ZLIB_MASK (1 << VNC_FEATURE_ZLIB)
+
+/*****************************************************************************
+ *
+ * Internal APIs
+ *
+ *****************************************************************************/
+
+/* Event loop functions */
+void vnc_client_read(void *opaque);
+void vnc_client_write(void *opaque);
+
+
+/* Protocol I/O functions */
+void vnc_write(VncState *vs, const void *data, size_t len);
+void vnc_write_u32(VncState *vs, uint32_t value);
+void vnc_write_s32(VncState *vs, int32_t value);
+void vnc_write_u16(VncState *vs, uint16_t value);
+void vnc_write_u8(VncState *vs, uint8_t value);
+void vnc_flush(VncState *vs);
+void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting);
+
+
+/* Buffer I/O functions */
+uint8_t read_u8(uint8_t *data, size_t offset);
+uint16_t read_u16(uint8_t *data, size_t offset);
+int32_t read_s32(uint8_t *data, size_t offset);
+uint32_t read_u32(uint8_t *data, size_t offset);
+
+/* Protocol stage functions */
+void vnc_client_error(VncState *vs);
+
+void start_client_init(VncState *vs);
+void start_auth_vnc(VncState *vs);
+
#endif /* __VNC_H */
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 4/7: Add SASL authentication extension to VNC
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
` (2 preceding siblings ...)
2009-02-12 15:02 ` [Qemu-devel] PATCH: 3/7: Split out VNC TLS auth code to separate file Daniel P. Berrange
@ 2009-02-12 15:03 ` Daniel P. Berrange
2009-02-12 15:03 ` [Qemu-devel] PATCH: 5/7: Include auth credentials in 'info vnc' Daniel P. Berrange
` (4 subsequent siblings)
8 siblings, 0 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:03 UTC (permalink / raw)
To: qemu-devel
This patch adds the new SASL authentication protocol to the VNC server.
It is enabled by setting the 'sasl' flag when launching VNC. SASL can
optionally provide encryption via its SSF layer, if a suitable mechanism
is configured (eg, GSSAPI/Kerberos, or Digest-MD5). If an SSF layer is
not available, then it should be combined with the x509 VNC authentication
protocol which provides encryption.
eg, if using GSSAPI
qemu -vnc localhost:1,sasl
eg if using TLS/x509 for encryption
qemu -vnc localhost:1,sasl,tls,x509
By default the Cyrus SASL library will look for its configuration in
the file /etc/sasl2/qemu.conf. For non-root users, this can be overridden
by setting the SASL_CONF_PATH environment variable, eg to make it look in
$HOME/.sasl2. NB unprivileged users may not have access to the full range
of SASL mechanisms, since some of them require some administrative privileges
to configure. The patch includes an example SASL configuration file which
illustrates config for GSSAPI and Digest-MD5, though it should be noted that
the latter is not really considered secure any more.
Most of the SASL authentication code is located in a separate source file,
vnc-auth-sasl.c. The main vnc.c file only contains minimal integration
glue, specifically parsing of command line flags / setup, and calls to
start the SASL auth process, to do encoding/decoding for data.
There are several possible stacks for reading & writing of data, depending
on the combo of VNC authentication methods in use
- Clear. read/write straight to socket
- TLS. read/write via GNUTLS helpers
- SASL. encode/decode via SASL SSF layer, then read/write to socket
- SASL+TLS. encode/decode via SASL SSF layer, then read/write via GNUTLS
Hence, the vnc_client_read & vnc_client_write methods have been refactored
a little.
vnc_client_read: main entry point for reading, calls either
- vnc_client_read_plain reading, with no intermediate decoding
- vnc_client_read_sasl reading, with SASL SSF decoding
These two methods, then call vnc_client_read_buf(). This decides
whether to write to the socket directly or write via GNUTLS.
The situation is the same for writing data. More extensive comments
have been added in the code / patch. The vnc_client_read_sasl and
vnc_client_write_sasl method implementations live in the separate
vnc-auth-sasl.c file.
The state required for the SASL auth mechanism is kept in a separate
VncStateSASL struct, defined in vnc-auth-sasl.h and included in the
main VncState.
The configure script probes for SASL and automatically enables it
if found, unless --disable-vnc-sasl was given to override it.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Makefile | 7
Makefile.target | 5
b/qemu.sasl | 34 ++
b/vnc-auth-sasl.c | 629 ++++++++++++++++++++++++++++++++++++++++++++++++++++
b/vnc-auth-sasl.h | 72 +++++
configure | 34 ++
qemu-doc.texi | 109 ++++++++-
vnc-auth-vencrypt.c | 12
vnc.c | 252 ++++++++++++++++++--
vnc.h | 31 ++
10 files changed, 1146 insertions(+), 39 deletions(-)
Daniel
diff -r e8caf420f587 Makefile
--- a/Makefile Thu Feb 12 12:28:24 2009 +0000
+++ b/Makefile Thu Feb 12 12:28:30 2009 +0000
@@ -148,6 +148,9 @@ OBJS+=vnc.o d3des.o
ifdef CONFIG_VNC_TLS
OBJS+=vnc-tls.o vnc-auth-vencrypt.o
endif
+ifdef CONFIG_VNC_SASL
+OBJS+=vnc-auth-sasl.o
+endif
ifdef CONFIG_COCOA
OBJS+=cocoa.o
@@ -171,7 +174,7 @@ sdl.o: sdl.c keymaps.h sdl_keysym.h
sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
-vnc.h: vnc-tls.h vnc-auth-vencrypt.h keymaps.h
+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
@@ -181,6 +184,8 @@ vnc-tls.o: vnc-tls.c vnc.h
vnc-auth-vencrypt.o: vnc-auth-vencrypt.c vnc.h
+vnc-auth-sasl.o: vnc-auth-sasl.c vnc.h
+
curses.o: curses.c keymaps.h curses_keys.h
bt-host.o: CFLAGS += $(CONFIG_BLUEZ_CFLAGS)
diff -r e8caf420f587 Makefile.target
--- a/Makefile.target Thu Feb 12 12:28:24 2009 +0000
+++ b/Makefile.target Thu Feb 12 12:28:30 2009 +0000
@@ -554,6 +554,11 @@ CPPFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
LIBS += $(CONFIG_VNC_TLS_LIBS)
endif
+ifdef CONFIG_VNC_SASL
+CPPFLAGS += $(CONFIG_VNC_SASL_CFLAGS)
+LIBS += $(CONFIG_VNC_SASL_LIBS)
+endif
+
ifdef CONFIG_BLUEZ
LIBS += $(CONFIG_BLUEZ_LIBS)
endif
diff -r e8caf420f587 configure
--- a/configure Thu Feb 12 12:28:24 2009 +0000
+++ b/configure Thu Feb 12 12:28:30 2009 +0000
@@ -164,6 +164,7 @@ fmod_lib=""
fmod_inc=""
oss_lib=""
vnc_tls="yes"
+vnc_sasl="yes"
bsd="no"
linux="no"
solaris="no"
@@ -387,6 +388,8 @@ for opt do
;;
--disable-vnc-tls) vnc_tls="no"
;;
+ --disable-vnc-sasl) vnc_sasl="no"
+ ;;
--disable-slirp) slirp="no"
;;
--disable-vde) vde="no"
@@ -544,6 +547,7 @@ echo " Availab
echo " --enable-mixemu enable mixer emulation"
echo " --disable-brlapi disable BrlAPI"
echo " --disable-vnc-tls disable TLS encryption for VNC server"
+echo " --disable-vnc-sasl disable SASL encryption for VNC server"
echo " --disable-curses disable curses output"
echo " --disable-bluez disable bluez stack connectivity"
echo " --disable-kvm disable KVM acceleration support"
@@ -823,6 +827,25 @@ EOF
fi
##########################################
+# VNC SASL detection
+if test "$vnc_sasl" = "yes" ; then
+cat > $TMPC <<EOF
+#include <sasl/sasl.h>
+#include <stdio.h>
+int main(void) { sasl_server_init(NULL, "qemu"); return 0; }
+EOF
+ # Assuming Cyrus-SASL installed in /usr prefix
+ vnc_sasl_cflags=""
+ vnc_sasl_libs="-lsasl2"
+ if $cc $ARCH_CFLAGS -o $TMPE ${OS_CFLAGS} $vnc_sasl_cflags $TMPC \
+ $vnc_sasl_libs 2> /dev/null ; then
+ :
+ else
+ vnc_sasl="no"
+ fi
+fi
+
+##########################################
# vde libraries probe
if test "$vde" = "yes" ; then
cat > $TMPC << EOF
@@ -1130,6 +1153,11 @@ if test "$vnc_tls" = "yes" ; then
echo " TLS CFLAGS $vnc_tls_cflags"
echo " TLS LIBS $vnc_tls_libs"
fi
+echo "VNC SASL support $vnc_sasl"
+if test "$vnc_sasl" = "yes" ; then
+ echo " SASL CFLAGS $vnc_sasl_cflags"
+ echo " SASL LIBS $vnc_sasl_libs"
+fi
if test -n "$sparc_cpu"; then
echo "Target Sparc Arch $sparc_cpu"
fi
@@ -1371,6 +1399,12 @@ if test "$vnc_tls" = "yes" ; then
echo "CONFIG_VNC_TLS_LIBS=$vnc_tls_libs" >> $config_mak
echo "#define CONFIG_VNC_TLS 1" >> $config_h
fi
+if test "$vnc_sasl" = "yes" ; then
+ echo "CONFIG_VNC_SASL=yes" >> $config_mak
+ echo "CONFIG_VNC_SASL_CFLAGS=$vnc_sasl_cflags" >> $config_mak
+ echo "CONFIG_VNC_SASL_LIBS=$vnc_sasl_libs" >> $config_mak
+ echo "#define CONFIG_VNC_SASL 1" >> $config_h
+fi
qemu_version=`head $source_path/VERSION`
echo "VERSION=$qemu_version" >>$config_mak
echo "#define QEMU_VERSION \"$qemu_version\"" >> $config_h
diff -r e8caf420f587 qemu-doc.texi
--- a/qemu-doc.texi Thu Feb 12 12:28:24 2009 +0000
+++ b/qemu-doc.texi Thu Feb 12 12:28:30 2009 +0000
@@ -616,6 +616,21 @@ path following this option specifies whe
be loaded from. See the @ref{vnc_security} section for details on generating
certificates.
+@item sasl
+
+Require that the client use SASL to authenticate with the VNC server.
+The exact choice of authentication method used is controlled from the
+system / user's SASL configuration file for the 'qemu' service. This
+is typically found in /etc/sasl2/qemu.conf. If running QEMU as an
+unprivileged user, an environment variable SASL_CONF_PATH can be used
+to make it search alternate locations for the service config.
+While some SASL auth methods can also provide data encryption (eg GSSAPI),
+it is recommended that SASL always be combined with the 'tls' and
+'x509' settings to enable use of SSL and server certificates. This
+ensures a data encryption preventing compromise of authentication
+credentials. See the @ref{vnc_security} section for details on using
+SASL authentication.
+
@end table
@end table
@@ -961,8 +976,6 @@ This implements UDP Net Console.
When @var{remote_host} or @var{src_ip} are not specified
they default to @code{0.0.0.0}.
When not using a specified @var{src_port} a random port is automatically chosen.
-@item msmouse
-Three button serial mouse. Configure the guest to use Microsoft protocol.
If you just want a simple readonly console you can use @code{netcat} or
@code{nc}, by starting qemu with: @code{-serial udp::4555} and nc as:
@@ -2047,7 +2060,10 @@ considerations depending on the deployme
* vnc_sec_certificate::
* vnc_sec_certificate_verify::
* vnc_sec_certificate_pw::
+* vnc_sec_sasl::
+* vnc_sec_certificate_sasl::
* vnc_generate_cert::
+* vnc_setup_sasl::
@end menu
@node vnc_sec_none
@subsection Without passwords
@@ -2130,6 +2146,41 @@ Password: ********
(qemu)
@end example
+
+@node vnc_sec_sasl
+@subsection With SASL authentication
+
+The SASL authentication method is a VNC extension, that provides an
+easily extendable, pluggable authentication method. This allows for
+integration with a wide range of authentication mechanisms, such as
+PAM, GSSAPI/Kerberos, LDAP, SQL databases, one-time keys and more.
+The strength of the authentication depends on the exact mechanism
+configured. If the chosen mechanism also provides a SSF layer, then
+it will encrypt the datastream as well.
+
+Refer to the later docs on how to choose the exact SASL mechanism
+used for authentication, but assuming use of one supporting SSF,
+then QEMU can be launched with:
+
+@example
+qemu [...OPTIONS...] -vnc :1,sasl -monitor stdio
+@end example
+
+@node vnc_sec_certificate_sasl
+@subsection With x509 certificates and SASL authentication
+
+If the desired SASL authentication mechanism does not supported
+SSF layers, then it is strongly advised to run it in combination
+with TLS and x509 certificates. This provides securely encrypted
+data stream, avoiding risk of compromising of the security
+credentials. This can be enabled, by combining the 'sasl' option
+with the aforementioned TLS + x509 options:
+
+@example
+qemu [...OPTIONS...] -vnc :1,tls,x509,sasl -monitor stdio
+@end example
+
+
@node vnc_generate_cert
@subsection Generating certificates for VNC
@@ -2241,6 +2292,50 @@ EOF
The @code{client-key.pem} and @code{client-cert.pem} files should now be securely
copied to the client for which they were generated.
+
+@node vnc_setup_sasl
+
+@subsection Configuring SASL mechanisms
+
+The following documentation assumes use of the Cyrus SASL implementation on a
+Linux host, but the principals should apply to any other SASL impl. When SASL
+is enabled, the mechanism configuration will be loaded from system default
+SASL service config /etc/sasl2/qemu.conf. If running QEMU as an
+unprivileged user, an environment variable SASL_CONF_PATH can be used
+to make it search alternate locations for the service config.
+
+The default configuration might contain
+
+@example
+mech_list: digest-md5
+sasldb_path: /etc/qemu/passwd.db
+@end example
+
+This says to use the 'Digest MD5' mechanism, which is similar to the HTTP
+Digest-MD5 mechanism. The list of valid usernames & passwords is maintained
+in the /etc/qemu/passwd.db file, and can be updated using the saslpasswd2
+command. While this mechanism is easy to configure and use, it is not
+considered secure by modern standards, so only suitable for developers /
+ad-hoc testing.
+
+A more serious deployment might use Kerberos, which is done with the 'gssapi'
+mechanism
+
+@example
+mech_list: gssapi
+keytab: /etc/qemu/krb5.tab
+@end example
+
+For this to work the administrator of your KDC must generate a Kerberos
+principal for the server, with a name of 'qemu/somehost.example.com@@EXAMPLE.COM'
+replacing 'somehost.example.com' with the fully qualified host name of the
+machine running QEMU, and 'EXAMPLE.COM' with the Keberos Realm.
+
+Other configurations will be left as an exercise for the reader. It should
+be noted that only Digest-MD5 and GSSAPI provides a SSF layer for data
+encryption. For all other mechanisms, VNC should always be configured to
+use TLS and x509 certificates to protect security credentials from snooping.
+
@node gdb_usage
@section GDB usage
@@ -2433,7 +2528,7 @@ QEMU emulates the following PowerMac per
@itemize @minus
@item
-UniNorth or Grackle PCI Bridge
+UniNorth PCI Bridge
@item
PCI VGA compatible card with VESA Bochs Extensions
@item
@@ -2471,9 +2566,9 @@ QEMU uses the Open Hack'Ware Open Firmwa
@url{http://perso.magic.fr/l_indien/OpenHackWare/index.htm}.
Since version 0.9.1, QEMU uses OpenBIOS @url{http://www.openbios.org/}
-for the g3beige and mac99 PowerMac machines. OpenBIOS is a free (GPL
-v2) portable firmware implementation. The goal is to implement a 100%
-IEEE 1275-1994 (referred to as Open Firmware) compliant firmware.
+for the g3beige PowerMac machine. OpenBIOS is a free (GPL v2) portable
+firmware implementation. The goal is to implement a 100% IEEE
+1275-1994 (referred to as Open Firmware) compliant firmware.
@c man begin OPTIONS
@@ -2702,7 +2797,7 @@ PCnet32 PCI network card
@item
Malta FPGA serial device
@item
-Cirrus (default) or any other PCI VGA graphics card
+Cirrus VGA graphics card
@end itemize
The ACER Pica emulation supports:
diff -r e8caf420f587 qemu.sasl
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/qemu.sasl Thu Feb 12 12:28:30 2009 +0000
@@ -0,0 +1,34 @@
+# If you want to use the non-TLS socket, then you *must* include
+# the GSSAPI or DIGEST-MD5 mechanisms, because they are the only
+# ones that can offer session encryption as well as authentication.
+#
+# If you're only using TLS, then you can turn on any mechanisms
+# you like for authentication, because TLS provides the encryption
+#
+# Default to a simple username+password mechanism
+# NB digest-md5 is no longer considered secure by current standards
+mech_list: digest-md5
+
+# Before you can use GSSAPI, you need a service principle on the
+# KDC server for libvirt, and that to be exported to the keytab
+# file listed below
+#mech_list: gssapi
+#
+# You can also list many mechanisms at once, then the user can choose
+# by adding '?auth=sasl.gssapi' to their libvirt URI, eg
+# qemu+tcp://hostname/system?auth=sasl.gssapi
+#mech_list: digest-md5 gssapi
+
+# Some older builds of MIT kerberos on Linux ignore this option &
+# instead need KRB5_KTNAME env var.
+# For modern Linux, and other OS, this should be sufficient
+keytab: /etc/qemu/krb5.tab
+
+# If using digest-md5 for username/passwds, then this is the file
+# containing the passwds. Use 'saslpasswd2 -a qemu [username]'
+# to add entries, and 'sasldblistusers2 -a qemu' to browse it
+sasldb_path: /etc/qemu/passwd.db
+
+
+auxprop_plugin: sasldb
+
diff -r e8caf420f587 vnc-auth-sasl.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vnc-auth-sasl.c Thu Feb 12 12:28:30 2009 +0000
@@ -0,0 +1,629 @@
+/*
+ * QEMU VNC display driver: SASL auth protocol
+ *
+ * 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 "vnc.h"
+
+/* Max amount of data we send/recv for SASL steps to prevent DOS */
+#define SASL_DATA_MAX_LEN (1024 * 1024)
+
+
+void vnc_sasl_client_cleanup(VncState *vs)
+{
+ if (vs->sasl.conn) {
+ vs->sasl.runSSF = vs->sasl.waitWriteSSF = vs->sasl.wantSSF = 0;
+ vs->sasl.encodedLength = vs->sasl.encodedOffset = 0;
+ vs->sasl.encoded = NULL;
+ free(vs->sasl.username);
+ free(vs->sasl.mechlist);
+ vs->sasl.username = vs->sasl.mechlist = NULL;
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ }
+}
+
+
+long vnc_client_write_sasl(VncState *vs)
+{
+ long ret;
+
+ VNC_DEBUG("Write SASL: Pending output %p size %d offset %d Encoded: %p size %d offset %d\n",
+ vs->output.buffer, vs->output.capacity, vs->output.offset,
+ vs->sasl.encoded, vs->sasl.encodedLength, vs->sasl.encodedOffset);
+
+ if (!vs->sasl.encoded) {
+ int err;
+ err = sasl_encode(vs->sasl.conn,
+ (char *)vs->output.buffer,
+ vs->output.offset,
+ (const char **)&vs->sasl.encoded,
+ &vs->sasl.encodedLength);
+ if (err != SASL_OK)
+ return vnc_client_io_error(vs, -1, EIO);
+
+ vs->sasl.encodedOffset = 0;
+ }
+
+ ret = vnc_client_write_buf(vs,
+ vs->sasl.encoded + vs->sasl.encodedOffset,
+ vs->sasl.encodedLength - vs->sasl.encodedOffset);
+ if (!ret)
+ return 0;
+
+ vs->sasl.encodedOffset += ret;
+ if (vs->sasl.encodedOffset == vs->sasl.encodedLength) {
+ vs->output.offset = 0;
+ vs->sasl.encoded = NULL;
+ vs->sasl.encodedOffset = vs->sasl.encodedLength = 0;
+ }
+
+ /* Can't merge this block with one above, because
+ * someone might have written more unencrypted
+ * data in vs->output while we were processing
+ * SASL encoded output
+ */
+ if (vs->output.offset == 0) {
+ qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
+ }
+
+ return ret;
+}
+
+
+long vnc_client_read_sasl(VncState *vs)
+{
+ long ret;
+ uint8_t encoded[4096];
+ const char *decoded;
+ unsigned int decodedLen;
+ int err;
+
+ ret = vnc_client_read_buf(vs, encoded, sizeof(encoded));
+ if (!ret)
+ return 0;
+
+ err = sasl_decode(vs->sasl.conn,
+ (char *)encoded, ret,
+ &decoded, &decodedLen);
+
+ if (err != SASL_OK)
+ return vnc_client_io_error(vs, -1, -EIO);
+ VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
+ encoded, ret, decoded, decodedLen);
+ buffer_reserve(&vs->input, decodedLen);
+ buffer_append(&vs->input, decoded, decodedLen);
+ return decodedLen;
+}
+
+
+static int vnc_auth_sasl_check_access(VncState *vs)
+{
+ const void *val;
+ int err;
+
+ err = sasl_getprop(vs->sasl.conn, SASL_USERNAME, &val);
+ if (err != SASL_OK) {
+ VNC_DEBUG("cannot query SASL username on connection %d (%s)\n",
+ err, sasl_errstring(err, NULL, NULL));
+ return -1;
+ }
+ if (val == NULL) {
+ VNC_DEBUG("no client username was found\n");
+ return -1;
+ }
+ VNC_DEBUG("SASL client username %s\n", (const char *)val);
+
+ vs->sasl.username = strdup((const char*)val);
+ if (vs->sasl.username == NULL) {
+ VNC_DEBUG("out of memory copying username\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int vnc_auth_sasl_check_ssf(VncState *vs)
+{
+ const void *val;
+ int err, ssf;
+
+ if (!vs->sasl.wantSSF)
+ return 1;
+
+ err = sasl_getprop(vs->sasl.conn, SASL_SSF, &val);
+ if (err != SASL_OK)
+ return 0;
+
+ ssf = *(const int *)val;
+ VNC_DEBUG("negotiated an SSF of %d\n", ssf);
+ if (ssf < 56)
+ return 0; /* 56 is good for Kerberos */
+
+ /* Only setup for read initially, because we're about to send an RPC
+ * reply which must be in plain text. When the next incoming RPC
+ * arrives, we'll switch on writes too
+ *
+ * cf qemudClientReadSASL in qemud.c
+ */
+ vs->sasl.runSSF = 1;
+
+ /* We have a SSF that's good enough */
+ return 1;
+}
+
+/*
+ * Step Msg
+ *
+ * Input from client:
+ *
+ * u32 clientin-length
+ * u8-array clientin-string
+ *
+ * Output to client:
+ *
+ * u32 serverout-length
+ * u8-array serverout-strin
+ * u8 continue
+ */
+
+static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len);
+
+static int protocol_client_auth_sasl_step(VncState *vs, uint8_t *data, size_t len)
+{
+ uint32_t datalen = len;
+ const char *serverout;
+ unsigned int serveroutlen;
+ int err;
+ char *clientdata = NULL;
+
+ /* NB, distinction of NULL vs "" is *critical* in SASL */
+ if (datalen) {
+ clientdata = (char*)data;
+ clientdata[datalen-1] = '\0'; /* Wire includes '\0', but make sure */
+ datalen--; /* Don't count NULL byte when passing to _start() */
+ }
+
+ VNC_DEBUG("Step using SASL Data %p (%d bytes)\n",
+ clientdata, datalen);
+ err = sasl_server_step(vs->sasl.conn,
+ clientdata,
+ datalen,
+ &serverout,
+ &serveroutlen);
+ if (err != SASL_OK &&
+ err != SASL_CONTINUE) {
+ VNC_DEBUG("sasl step failed %d (%s)\n",
+ err, sasl_errdetail(vs->sasl.conn));
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+
+ if (serveroutlen > SASL_DATA_MAX_LEN) {
+ VNC_DEBUG("sasl step reply data too long %d\n",
+ serveroutlen);
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+
+ VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
+ serveroutlen, serverout ? 0 : 1);
+
+ if (serveroutlen) {
+ vnc_write_u32(vs, serveroutlen + 1);
+ vnc_write(vs, serverout, serveroutlen + 1);
+ } else {
+ vnc_write_u32(vs, 0);
+ }
+
+ /* Whether auth is complete */
+ vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
+
+ if (err == SASL_CONTINUE) {
+ VNC_DEBUG("%s", "Authentication must continue\n");
+ /* Wait for step length */
+ vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
+ } else {
+ if (!vnc_auth_sasl_check_ssf(vs)) {
+ VNC_DEBUG("Authentication rejected for weak SSF %d\n", vs->csock);
+ goto authreject;
+ }
+
+ /* Check username whitelist ACL */
+ if (vnc_auth_sasl_check_access(vs) < 0) {
+ VNC_DEBUG("Authentication rejected for ACL %d\n", vs->csock);
+ goto authreject;
+ }
+
+ VNC_DEBUG("Authentication successful %d\n", vs->csock);
+ vnc_write_u32(vs, 0); /* Accept auth */
+ /*
+ * Delay writing in SSF encoded mode until pending output
+ * buffer is written
+ */
+ if (vs->sasl.runSSF)
+ vs->sasl.waitWriteSSF = vs->output.offset;
+ start_client_init(vs);
+ }
+
+ return 0;
+
+ authreject:
+ vnc_write_u32(vs, 1); /* Reject auth */
+ vnc_write_u32(vs, sizeof("Authentication failed"));
+ vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
+ vnc_flush(vs);
+ vnc_client_error(vs);
+ return -1;
+
+ authabort:
+ vnc_client_error(vs);
+ return -1;
+}
+
+static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len)
+{
+ uint32_t steplen = read_u32(data, 0);
+ VNC_DEBUG("Got client step len %d\n", steplen);
+ if (steplen > SASL_DATA_MAX_LEN) {
+ VNC_DEBUG("Too much SASL data %d\n", steplen);
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (steplen == 0)
+ return protocol_client_auth_sasl_step(vs, NULL, 0);
+ else
+ vnc_read_when(vs, protocol_client_auth_sasl_step, steplen);
+ return 0;
+}
+
+/*
+ * Start Msg
+ *
+ * Input from client:
+ *
+ * u32 clientin-length
+ * u8-array clientin-string
+ *
+ * Output to client:
+ *
+ * u32 serverout-length
+ * u8-array serverout-strin
+ * u8 continue
+ */
+
+#define SASL_DATA_MAX_LEN (1024 * 1024)
+
+static int protocol_client_auth_sasl_start(VncState *vs, uint8_t *data, size_t len)
+{
+ uint32_t datalen = len;
+ const char *serverout;
+ unsigned int serveroutlen;
+ int err;
+ char *clientdata = NULL;
+
+ /* NB, distinction of NULL vs "" is *critical* in SASL */
+ if (datalen) {
+ clientdata = (char*)data;
+ clientdata[datalen-1] = '\0'; /* Should be on wire, but make sure */
+ datalen--; /* Don't count NULL byte when passing to _start() */
+ }
+
+ VNC_DEBUG("Start SASL auth with mechanism %s. Data %p (%d bytes)\n",
+ vs->sasl.mechlist, clientdata, datalen);
+ err = sasl_server_start(vs->sasl.conn,
+ vs->sasl.mechlist,
+ clientdata,
+ datalen,
+ &serverout,
+ &serveroutlen);
+ if (err != SASL_OK &&
+ err != SASL_CONTINUE) {
+ VNC_DEBUG("sasl start failed %d (%s)\n",
+ err, sasl_errdetail(vs->sasl.conn));
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+ if (serveroutlen > SASL_DATA_MAX_LEN) {
+ VNC_DEBUG("sasl start reply data too long %d\n",
+ serveroutlen);
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+
+ VNC_DEBUG("SASL return data %d bytes, nil; %d\n",
+ serveroutlen, serverout ? 0 : 1);
+
+ if (serveroutlen) {
+ vnc_write_u32(vs, serveroutlen + 1);
+ vnc_write(vs, serverout, serveroutlen + 1);
+ } else {
+ vnc_write_u32(vs, 0);
+ }
+
+ /* Whether auth is complete */
+ vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
+
+ if (err == SASL_CONTINUE) {
+ VNC_DEBUG("%s", "Authentication must continue\n");
+ /* Wait for step length */
+ vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
+ } else {
+ if (!vnc_auth_sasl_check_ssf(vs)) {
+ VNC_DEBUG("Authentication rejected for weak SSF %d\n", vs->csock);
+ goto authreject;
+ }
+
+ /* Check username whitelist ACL */
+ if (vnc_auth_sasl_check_access(vs) < 0) {
+ VNC_DEBUG("Authentication rejected for ACL %d\n", vs->csock);
+ goto authreject;
+ }
+
+ VNC_DEBUG("Authentication successful %d\n", vs->csock);
+ vnc_write_u32(vs, 0); /* Accept auth */
+ start_client_init(vs);
+ }
+
+ return 0;
+
+ authreject:
+ vnc_write_u32(vs, 1); /* Reject auth */
+ vnc_write_u32(vs, sizeof("Authentication failed"));
+ vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
+ vnc_flush(vs);
+ vnc_client_error(vs);
+ return -1;
+
+ authabort:
+ vnc_client_error(vs);
+ return -1;
+}
+
+static int protocol_client_auth_sasl_start_len(VncState *vs, uint8_t *data, size_t len)
+{
+ uint32_t startlen = read_u32(data, 0);
+ VNC_DEBUG("Got client start len %d\n", startlen);
+ if (startlen > SASL_DATA_MAX_LEN) {
+ VNC_DEBUG("Too much SASL data %d\n", startlen);
+ vnc_client_error(vs);
+ return -1;
+ }
+
+ if (startlen == 0)
+ return protocol_client_auth_sasl_start(vs, NULL, 0);
+
+ vnc_read_when(vs, protocol_client_auth_sasl_start, startlen);
+ return 0;
+}
+
+static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, size_t len)
+{
+ char *mechname = malloc(len + 1);
+ if (!mechname) {
+ VNC_DEBUG("Out of memory reading mechname\n");
+ vnc_client_error(vs);
+ }
+ strncpy(mechname, (char*)data, len);
+ mechname[len] = '\0';
+ VNC_DEBUG("Got client mechname '%s' check against '%s'\n",
+ mechname, vs->sasl.mechlist);
+
+ if (strncmp(vs->sasl.mechlist, mechname, len) == 0) {
+ if (vs->sasl.mechlist[len] != '\0' &&
+ vs->sasl.mechlist[len] != ',') {
+ VNC_DEBUG("One %d", vs->sasl.mechlist[len]);
+ vnc_client_error(vs);
+ return -1;
+ }
+ } else {
+ char *offset = strstr(vs->sasl.mechlist, mechname);
+ VNC_DEBUG("Two %p\n", offset);
+ if (!offset) {
+ vnc_client_error(vs);
+ return -1;
+ }
+ VNC_DEBUG("Two '%s'\n", offset);
+ if (offset[-1] != ',' ||
+ (offset[len] != '\0'&&
+ offset[len] != ',')) {
+ vnc_client_error(vs);
+ return -1;
+ }
+ }
+
+ free(vs->sasl.mechlist);
+ vs->sasl.mechlist = mechname;
+
+ VNC_DEBUG("Validated mechname '%s'\n", mechname);
+ vnc_read_when(vs, protocol_client_auth_sasl_start_len, 4);
+ return 0;
+}
+
+static int protocol_client_auth_sasl_mechname_len(VncState *vs, uint8_t *data, size_t len)
+{
+ uint32_t mechlen = read_u32(data, 0);
+ VNC_DEBUG("Got client mechname len %d\n", mechlen);
+ if (mechlen > 100) {
+ VNC_DEBUG("Too long SASL mechname data %d\n", mechlen);
+ vnc_client_error(vs);
+ return -1;
+ }
+ if (mechlen < 1) {
+ VNC_DEBUG("Too short SASL mechname %d\n", mechlen);
+ vnc_client_error(vs);
+ return -1;
+ }
+ vnc_read_when(vs, protocol_client_auth_sasl_mechname,mechlen);
+ return 0;
+}
+
+
+void start_auth_sasl(VncState *vs)
+{
+ const char *mechlist = NULL;
+ sasl_security_properties_t secprops;
+ int err;
+ char *localAddr, *remoteAddr;
+ int mechlistlen;
+
+ VNC_DEBUG("Initialize SASL auth %d\n", vs->csock);
+
+ /* Get local & remote client addresses in form IPADDR;PORT */
+ if (!(localAddr = vnc_socket_local_addr("%s;%s", vs->csock)))
+ goto authabort;
+
+ if (!(remoteAddr = vnc_socket_remote_addr("%s;%s", vs->csock))) {
+ free(localAddr);
+ goto authabort;
+ }
+
+ err = sasl_server_new("vnc",
+ NULL, /* FQDN - just delegates to gethostname */
+ NULL, /* User realm */
+ localAddr,
+ remoteAddr,
+ NULL, /* Callbacks, not needed */
+ SASL_SUCCESS_DATA,
+ &vs->sasl.conn);
+ free(localAddr);
+ free(remoteAddr);
+ localAddr = remoteAddr = NULL;
+
+ if (err != SASL_OK) {
+ VNC_DEBUG("sasl context setup failed %d (%s)",
+ err, sasl_errstring(err, NULL, NULL));
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+
+#ifdef CONFIG_VNC_TLS
+ /* Inform SASL that we've got an external SSF layer from TLS */
+ if (vs->auth == VNC_AUTH_VENCRYPT &&
+ vs->subauth != VNC_AUTH_VENCRYPT_PLAIN) { /* XXX fileter out other sub-auth ? */
+ gnutls_cipher_algorithm_t cipher;
+ sasl_ssf_t ssf;
+
+ cipher = gnutls_cipher_get(vs->tls.session);
+ if (!(ssf = (sasl_ssf_t)gnutls_cipher_get_key_size(cipher))) {
+ VNC_DEBUG("%s", "cannot TLS get cipher size\n");
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+ ssf *= 8; /* tls key size is bytes, sasl wants bits */
+
+ err = sasl_setprop(vs->sasl.conn, SASL_SSF_EXTERNAL, &ssf);
+ if (err != SASL_OK) {
+ VNC_DEBUG("cannot set SASL external SSF %d (%s)\n",
+ err, sasl_errstring(err, NULL, NULL));
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+ } else
+#endif /* CONFIG_VNC_TLS */
+ vs->sasl.wantSSF = 1;
+
+ memset (&secprops, 0, sizeof secprops);
+ /* Inform SASL that we've got an external SSF layer from TLS */
+ if (strncmp(vs->display, "unix:", 5) == 0
+#ifdef CONFIG_VNC_TLS
+ || (vs->auth == VNC_AUTH_VENCRYPT &&
+ vs->subauth != VNC_AUTH_VENCRYPT_PLAIN)
+#endif /* CONFIG_VNC_TLS */
+ ) { /* XXX fileter out other sub-auth ? */
+ /* If we've got TLS or UNIX domain sock, we don't care about SSF */
+ secprops.min_ssf = 0;
+ secprops.max_ssf = 0;
+ secprops.maxbufsize = 8192;
+ secprops.security_flags = 0;
+ } else {
+ /* Plain TCP, better get an SSF layer */
+ secprops.min_ssf = 56; /* Good enough to require kerberos */
+ secprops.max_ssf = 100000; /* Arbitrary big number */
+ secprops.maxbufsize = 8192;
+ /* Forbid any anonymous or trivially crackable auth */
+ secprops.security_flags =
+ SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
+ }
+
+ err = sasl_setprop(vs->sasl.conn, SASL_SEC_PROPS, &secprops);
+ if (err != SASL_OK) {
+ VNC_DEBUG("cannot set SASL security props %d (%s)\n",
+ err, sasl_errstring(err, NULL, NULL));
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+
+ err = sasl_listmech(vs->sasl.conn,
+ NULL, /* Don't need to set user */
+ "", /* Prefix */
+ ",", /* Separator */
+ "", /* Suffix */
+ &mechlist,
+ NULL,
+ NULL);
+ if (err != SASL_OK) {
+ VNC_DEBUG("cannot list SASL mechanisms %d (%s)\n",
+ err, sasl_errdetail(vs->sasl.conn));
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+ VNC_DEBUG("Available mechanisms for client: '%s'\n", mechlist);
+
+ if (!(vs->sasl.mechlist = strdup(mechlist))) {
+ VNC_DEBUG("Out of memory");
+ sasl_dispose(&vs->sasl.conn);
+ vs->sasl.conn = NULL;
+ goto authabort;
+ }
+ mechlistlen = strlen(mechlist);
+ vnc_write_u32(vs, mechlistlen);
+ vnc_write(vs, mechlist, mechlistlen);
+ vnc_flush(vs);
+
+ VNC_DEBUG("Wait for client mechname length\n");
+ vnc_read_when(vs, protocol_client_auth_sasl_mechname_len, 4);
+
+ return;
+
+ authabort:
+ vnc_client_error(vs);
+ return;
+}
+
+
+/*
+ * Local variables:
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 8
+ * End:
+ */
diff -r e8caf420f587 vnc-auth-sasl.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/vnc-auth-sasl.h Thu Feb 12 12:28:30 2009 +0000
@@ -0,0 +1,72 @@
+/*
+ * QEMU VNC display driver: SASL auth protocol
+ *
+ * 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_VNC_AUTH_SASL_H__
+#define __QEMU_VNC_AUTH_SASL_H__
+
+
+#include <sasl/sasl.h>
+
+struct VncStateSASL {
+ sasl_conn_t *conn;
+ /* If we want to negotiate an SSF layer with client */
+ int wantSSF :1;
+ /* If we are now running the SSF layer */
+ int runSSF :1;
+ /*
+ * If this is non-zero, then wait for that many bytes
+ * to be written plain, before switching to SSF encoding
+ * This allows the VNC auth result to finish being
+ * written in plain.
+ */
+ unsigned int waitWriteSSF;
+
+ /*
+ * Buffering encoded data to allow more clear data
+ * to be stuffed onto the output buffer
+ */
+ const uint8_t *encoded;
+ unsigned int encodedLength;
+ unsigned int encodedOffset;
+ char *username;
+ char *mechlist;
+};
+
+void vnc_sasl_client_cleanup(VncState *vs);
+
+long vnc_client_read_sasl(VncState *vs);
+long vnc_client_write_sasl(VncState *vs);
+
+void start_auth_sasl(VncState *vs);
+
+#endif /* __QEMU_VNC_AUTH_SASL_H__ */
+
+/*
+ * Local variables:
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 8
+ * End:
+ */
diff -r e8caf420f587 vnc-auth-vencrypt.c
--- a/vnc-auth-vencrypt.c Thu Feb 12 12:28:24 2009 +0000
+++ b/vnc-auth-vencrypt.c Thu Feb 12 12:28:30 2009 +0000
@@ -43,8 +43,15 @@ static void start_auth_vencrypt_subauth(
start_auth_vnc(vs);
break;
+#ifdef CONFIG_VNC_SASL
+ case VNC_AUTH_VENCRYPT_TLSSASL:
+ case VNC_AUTH_VENCRYPT_X509SASL:
+ VNC_DEBUG("Start TLS auth SASL\n");
+ return start_auth_sasl(vs);
+#endif /* CONFIG_VNC_SASL */
+
default: /* Should not be possible, but just in case */
- VNC_DEBUG("Reject auth %d\n", vs->auth);
+ VNC_DEBUG("Reject subauth %d server bug\n", vs->auth);
vnc_write_u8(vs, 1);
if (vs->minor >= 8) {
static const char err[] = "Unsupported authentication type";
@@ -105,7 +112,8 @@ static void vnc_tls_handshake_io(void *o
#define NEED_X509_AUTH(vs) \
((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
(vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
- (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
+ (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN || \
+ (vs)->subauth == VNC_AUTH_VENCRYPT_X509SASL)
static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
diff -r e8caf420f587 vnc.c
--- a/vnc.c Thu Feb 12 12:28:24 2009 +0000
+++ b/vnc.c Thu Feb 12 12:28:30 2009 +0000
@@ -71,7 +71,8 @@ static char *addr_to_string(const char *
return addr;
}
-static char *vnc_socket_local_addr(const char *format, int fd) {
+
+char *vnc_socket_local_addr(const char *format, int fd) {
struct sockaddr_storage sa;
socklen_t salen;
@@ -82,7 +83,8 @@ static char *vnc_socket_local_addr(const
return addr_to_string(format, &sa, salen);
}
-static char *vnc_socket_remote_addr(const char *format, int fd) {
+
+char *vnc_socket_remote_addr(const char *format, int fd) {
struct sockaddr_storage sa;
socklen_t salen;
@@ -128,12 +130,18 @@ static const char *vnc_auth_name(VncStat
return "vencrypt+x509+vnc";
case VNC_AUTH_VENCRYPT_X509PLAIN:
return "vencrypt+x509+plain";
+ case VNC_AUTH_VENCRYPT_TLSSASL:
+ return "vencrypt+tls+sasl";
+ case VNC_AUTH_VENCRYPT_X509SASL:
+ return "vencrypt+x509+sasl";
default:
return "vencrypt";
}
#else
return "vencrypt";
#endif
+ case VNC_AUTH_SASL:
+ return "sasl";
}
return "unknown";
}
@@ -263,7 +271,7 @@ static void vnc_framebuffer_update(VncSt
vnc_write_s32(vs, encoding);
}
-static void buffer_reserve(Buffer *buffer, size_t len)
+void buffer_reserve(Buffer *buffer, size_t len)
{
if ((buffer->capacity - buffer->offset) < len) {
buffer->capacity += (len + 1024);
@@ -275,22 +283,22 @@ static void buffer_reserve(Buffer *buffe
}
}
-static int buffer_empty(Buffer *buffer)
+int buffer_empty(Buffer *buffer)
{
return buffer->offset == 0;
}
-static uint8_t *buffer_end(Buffer *buffer)
+uint8_t *buffer_end(Buffer *buffer)
{
return buffer->buffer + buffer->offset;
}
-static void buffer_reset(Buffer *buffer)
+void buffer_reset(Buffer *buffer)
{
buffer->offset = 0;
}
-static void buffer_append(Buffer *buffer, const void *data, size_t len)
+void buffer_append(Buffer *buffer, const void *data, size_t len)
{
memcpy(buffer->buffer + buffer->offset, data, len);
buffer->offset += len;
@@ -793,7 +801,8 @@ static void audio_del(VncState *vs)
}
}
-static int vnc_client_io_error(VncState *vs, int ret, int last_errno)
+
+int vnc_client_io_error(VncState *vs, int ret, int last_errno)
{
if (ret == 0 || ret == -1) {
if (ret == -1) {
@@ -820,6 +829,9 @@ static int vnc_client_io_error(VncState
#ifdef CONFIG_VNC_TLS
vnc_tls_client_cleanup(vs);
#endif /* CONFIG_VNC_TLS */
+#ifdef CONFIG_VNC_SASL
+ vnc_sasl_client_cleanup(vs);
+#endif /* CONFIG_VNC_SASL */
audio_del(vs);
return 0;
}
@@ -832,14 +844,28 @@ void vnc_client_error(VncState *vs)
vnc_client_io_error(vs, -1, EINVAL);
}
-void vnc_client_write(void *opaque)
+
+/*
+ * Called to write a chunk of data to the client socket. The data may
+ * be the raw data, or may have already been encoded by SASL.
+ * The data will be written either straight onto the socket, or
+ * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled
+ *
+ * NB, it is theoretically possible to have 2 layers of encryption,
+ * both SASL, and this TLS layer. It is highly unlikely in practice
+ * though, since SASL encryption will typically be a no-op if TLS
+ * is active
+ *
+ * Returns the number of bytes written, which may be less than
+ * the requested 'datalen' if the socket would block. Returns
+ * -1 on error, and disconnects the client socket.
+ */
+long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen)
{
long ret;
- VncState *vs = opaque;
-
#ifdef CONFIG_VNC_TLS
if (vs->tls.session) {
- ret = gnutls_write(vs->tls.session, vs->output.buffer, vs->output.offset);
+ ret = gnutls_write(vs->tls.session, data, datalen);
if (ret < 0) {
if (ret == GNUTLS_E_AGAIN)
errno = EAGAIN;
@@ -849,10 +875,42 @@ void vnc_client_write(void *opaque)
}
} else
#endif /* CONFIG_VNC_TLS */
- ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
- ret = vnc_client_io_error(vs, ret, socket_error());
+ ret = send(vs->csock, data, datalen, 0);
+ VNC_DEBUG("Wrote wire %p %d -> %ld\n", data, datalen, ret);
+ return vnc_client_io_error(vs, ret, socket_error());
+}
+
+
+/*
+ * Called to write buffered data to the client socket, when not
+ * using any SASL SSF encryption layers. Will write as much data
+ * as possible without blocking. If all buffered data is written,
+ * will switch the FD poll() handler back to read monitoring.
+ *
+ * Returns the number of bytes written, which may be less than
+ * the buffered output data if the socket would block. Returns
+ * -1 on error, and disconnects the client socket.
+ */
+static long vnc_client_write_plain(VncState *vs)
+{
+ long ret;
+
+#ifdef CONFIG_VNC_SASL
+ VNC_DEBUG("Write Plain: Pending output %p size %d offset %d. Wait SSF %d\n",
+ vs->output.buffer, vs->output.capacity, vs->output.offset,
+ vs->sasl.waitWriteSSF);
+
+ if (vs->sasl.conn &&
+ vs->sasl.runSSF &&
+ vs->sasl.waitWriteSSF) {
+ ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF);
+ if (ret)
+ vs->sasl.waitWriteSSF -= ret;
+ } else
+#endif /* CONFIG_VNC_SASL */
+ ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset);
if (!ret)
- return;
+ return 0;
memmove(vs->output.buffer, vs->output.buffer + ret, (vs->output.offset - ret));
vs->output.offset -= ret;
@@ -860,6 +918,29 @@ void vnc_client_write(void *opaque)
if (vs->output.offset == 0) {
qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
}
+
+ return ret;
+}
+
+
+/*
+ * First function called whenever there is data to be written to
+ * the client socket. Will delegate actual work according to whether
+ * SASL SSF layers are enabled (thus requiring encryption calls)
+ */
+void vnc_client_write(void *opaque)
+{
+ long ret;
+ VncState *vs = opaque;
+
+#ifdef CONFIG_VNC_SASL
+ if (vs->sasl.conn &&
+ vs->sasl.runSSF &&
+ !vs->sasl.waitWriteSSF)
+ ret = vnc_client_write_sasl(vs);
+ else
+#endif /* CONFIG_VNC_SASL */
+ ret = vnc_client_write_plain(vs);
}
void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting)
@@ -868,16 +949,28 @@ void vnc_read_when(VncState *vs, VncRead
vs->read_handler_expect = expecting;
}
-void vnc_client_read(void *opaque)
+
+/*
+ * Called to read a chunk of data from the client socket. The data may
+ * be the raw data, or may need to be further decoded by SASL.
+ * The data will be read either straight from to the socket, or
+ * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled
+ *
+ * NB, it is theoretically possible to have 2 layers of encryption,
+ * both SASL, and this TLS layer. It is highly unlikely in practice
+ * though, since SASL encryption will typically be a no-op if TLS
+ * is active
+ *
+ * Returns the number of bytes read, which may be less than
+ * the requested 'datalen' if the socket would block. Returns
+ * -1 on error, and disconnects the client socket.
+ */
+long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen)
{
- VncState *vs = opaque;
long ret;
-
- buffer_reserve(&vs->input, 4096);
-
#ifdef CONFIG_VNC_TLS
if (vs->tls.session) {
- ret = gnutls_read(vs->tls.session, buffer_end(&vs->input), 4096);
+ ret = gnutls_read(vs->tls.session, data, datalen);
if (ret < 0) {
if (ret == GNUTLS_E_AGAIN)
errno = EAGAIN;
@@ -887,13 +980,53 @@ void vnc_client_read(void *opaque)
}
} else
#endif /* CONFIG_VNC_TLS */
- ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
- ret = vnc_client_io_error(vs, ret, socket_error());
+ ret = recv(vs->csock, data, datalen, 0);
+ VNC_DEBUG("Read wire %p %d -> %ld\n", data, datalen, ret);
+ return vnc_client_io_error(vs, ret, socket_error());
+}
+
+
+/*
+ * Called to read data from the client socket to the input buffer,
+ * when not using any SASL SSF encryption layers. Will read as much
+ * data as possible without blocking.
+ *
+ * Returns the number of bytes read. Returns -1 on error, and
+ * disconnects the client socket.
+ */
+static long vnc_client_read_plain(VncState *vs)
+{
+ int ret;
+ VNC_DEBUG("Read plain %p size %d offset %d\n",
+ vs->input.buffer, vs->input.capacity, vs->input.offset);
+ buffer_reserve(&vs->input, 4096);
+ ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096);
+ if (!ret)
+ return 0;
+ vs->input.offset += ret;
+ return ret;
+}
+
+
+/*
+ * First function called whenever there is more data to be read from
+ * the client socket. Will delegate actual work according to whether
+ * SASL SSF layers are enabled (thus requiring decryption calls)
+ */
+void vnc_client_read(void *opaque)
+{
+ VncState *vs = opaque;
+ long ret;
+
+#if CONFIG_VNC_SASL
+ if (vs->sasl.conn && vs->sasl.runSSF)
+ ret = vnc_client_read_sasl(vs);
+ else
+#endif /* CONFIG_VNC_SASL */
+ ret = vnc_client_read_plain(vs);
if (!ret)
return;
- vs->input.offset += ret;
-
while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) {
size_t len = vs->read_handler_expect;
int ret;
@@ -1680,6 +1813,13 @@ static int protocol_client_auth(VncState
break;
#endif /* CONFIG_VNC_TLS */
+#ifdef CONFIG_VNC_SASL
+ case VNC_AUTH_SASL:
+ VNC_DEBUG("Accept SASL auth\n");
+ start_auth_sasl(vs);
+ break;
+#endif /* CONFIG_VNC_SASL */
+
default: /* Should not be possible, but just in case */
VNC_DEBUG("Reject auth %d\n", vs->auth);
vnc_write_u8(vs, 1);
@@ -1853,6 +1993,9 @@ void vnc_display_close(DisplayState *ds)
#ifdef CONFIG_VNC_TLS
vnc_tls_client_cleanup(vs);
#endif /* CONFIG_VNC_TLS */
+#ifdef CONFIG_VNC_SASL
+ vnc_sasl_client_cleanup(vs);
+#endif /* CONFIG_VNC_SASL */
}
vs->auth = VNC_AUTH_INVALID;
#ifdef CONFIG_VNC_TLS
@@ -1888,6 +2031,10 @@ int vnc_display_open(DisplayState *ds, c
#ifdef CONFIG_VNC_TLS
int tls = 0, x509 = 0;
#endif
+#ifdef CONFIG_VNC_SASL
+ int sasl = 0;
+ int saslErr;
+#endif
if (!vnc_state)
return -1;
@@ -1907,6 +2054,10 @@ int vnc_display_open(DisplayState *ds, c
reverse = 1;
} else if (strncmp(options, "to=", 3) == 0) {
to_port = atoi(options+3) + 5900;
+#ifdef CONFIG_VNC_SASL
+ } else if (strncmp(options, "sasl", 4) == 0) {
+ sasl = 1; /* Require SASL auth */
+#endif
#ifdef CONFIG_VNC_TLS
} else if (strncmp(options, "tls", 3) == 0) {
tls = 1; /* Require TLS */
@@ -1943,6 +2094,22 @@ int vnc_display_open(DisplayState *ds, c
}
}
+ /*
+ * Combinations we support here:
+ *
+ * - no-auth (clear text, no auth)
+ * - password (clear text, weak auth)
+ * - sasl (encrypt, good auth *IF* using Kerberos via GSSAPI)
+ * - tls (encrypt, weak anonymous creds, no auth)
+ * - tls + password (encrypt, weak anonymous creds, weak auth)
+ * - tls + sasl (encrypt, weak anonymous creds, good auth)
+ * - tls + x509 (encrypt, good x509 creds, no auth)
+ * - tls + x509 + password (encrypt, good x509 creds, weak auth)
+ * - tls + x509 + sasl (encrypt, good x509 creds, good auth)
+ *
+ * NB1. TLS is a stackable auth scheme.
+ * NB2. the x509 schemes have option to validate a client cert dname
+ */
if (password) {
#ifdef CONFIG_VNC_TLS
if (tls) {
@@ -1955,13 +2122,34 @@ int vnc_display_open(DisplayState *ds, c
vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
}
} else {
-#endif
+#endif /* CONFIG_VNC_TLS */
VNC_DEBUG("Initializing VNC server with password auth\n");
vs->auth = VNC_AUTH_VNC;
#ifdef CONFIG_VNC_TLS
vs->subauth = VNC_AUTH_INVALID;
}
-#endif
+#endif /* CONFIG_VNC_TLS */
+#ifdef CONFIG_VNC_SASL
+ } else if (sasl) {
+#ifdef CONFIG_VNC_TLS
+ if (tls) {
+ vs->auth = VNC_AUTH_VENCRYPT;
+ if (x509) {
+ VNC_DEBUG("Initializing VNC server with x509 SASL auth\n");
+ vs->subauth = VNC_AUTH_VENCRYPT_X509SASL;
+ } else {
+ VNC_DEBUG("Initializing VNC server with TLS SASL auth\n");
+ vs->subauth = VNC_AUTH_VENCRYPT_TLSSASL;
+ }
+ } else {
+#endif /* CONFIG_VNC_TLS */
+ VNC_DEBUG("Initializing VNC server with SASL auth\n");
+ vs->auth = VNC_AUTH_SASL;
+#ifdef CONFIG_VNC_TLS
+ vs->subauth = VNC_AUTH_INVALID;
+ }
+#endif /* CONFIG_VNC_TLS */
+#endif /* CONFIG_VNC_SASL */
} else {
#ifdef CONFIG_VNC_TLS
if (tls) {
@@ -1983,6 +2171,16 @@ int vnc_display_open(DisplayState *ds, c
#endif
}
+#ifdef CONFIG_VNC_SASL
+ if ((saslErr = sasl_server_init(NULL, "qemu")) != SASL_OK) {
+ fprintf(stderr, "Failed to initialize SASL auth %s",
+ sasl_errstring(saslErr, NULL, NULL));
+ free(vs->display);
+ vs->display = NULL;
+ return -1;
+ }
+#endif
+
if (reverse) {
/* connect to viewer */
if (strncmp(display, "unix:", 5) == 0)
diff -r e8caf420f587 vnc.h
--- a/vnc.h Thu Feb 12 12:28:24 2009 +0000
+++ b/vnc.h Thu Feb 12 12:28:30 2009 +0000
@@ -82,6 +82,10 @@ typedef struct Buffer
#include "vnc-auth-vencrypt.h"
#endif
+#ifdef CONFIG_VNC_SASL
+#include "vnc-auth-sasl.h"
+#endif
+
struct VncState
{
QEMUTimer *timer;
@@ -111,6 +115,9 @@ struct VncState
struct VncStateTLS tls;
#endif
char challenge[VNC_AUTH_CHALLENGE_SIZE];
+#ifdef CONFIG_VNC_SASL
+ struct VncStateSASL sasl;
+#endif
Buffer output;
Buffer input;
@@ -147,8 +154,9 @@ enum {
VNC_AUTH_RA2NE = 6,
VNC_AUTH_TIGHT = 16,
VNC_AUTH_ULTRA = 17,
- VNC_AUTH_TLS = 18,
- VNC_AUTH_VENCRYPT = 19
+ VNC_AUTH_TLS = 18, /* Supported in GTK-VNC & VINO */
+ VNC_AUTH_VENCRYPT = 19, /* Supported in GTK-VNC & VeNCrypt */
+ VNC_AUTH_SASL = 20, /* Supported in GTK-VNC & VINO */
};
enum {
@@ -159,6 +167,8 @@ enum {
VNC_AUTH_VENCRYPT_X509NONE = 260,
VNC_AUTH_VENCRYPT_X509VNC = 261,
VNC_AUTH_VENCRYPT_X509PLAIN = 262,
+ VNC_AUTH_VENCRYPT_X509SASL = 263,
+ VNC_AUTH_VENCRYPT_TLSSASL = 264,
};
@@ -240,6 +250,8 @@ enum {
void vnc_client_read(void *opaque);
void vnc_client_write(void *opaque);
+long vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen);
+long vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen);
/* Protocol I/O functions */
void vnc_write(VncState *vs, const void *data, size_t len);
@@ -259,8 +271,23 @@ uint32_t read_u32(uint8_t *data, size_t
/* Protocol stage functions */
void vnc_client_error(VncState *vs);
+int vnc_client_io_error(VncState *vs, int ret, int last_errno);
void start_client_init(VncState *vs);
void start_auth_vnc(VncState *vs);
+
+/* Buffer management */
+void buffer_reserve(Buffer *buffer, size_t len);
+int buffer_empty(Buffer *buffer);
+uint8_t *buffer_end(Buffer *buffer);
+void buffer_reset(Buffer *buffer);
+void buffer_append(Buffer *buffer, const void *data, size_t len);
+
+
+/* Misc helpers */
+
+char *vnc_socket_local_addr(const char *format, int fd);
+char *vnc_socket_remote_addr(const char *format, int fd);
+
#endif /* __VNC_H */
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 5/7: Include auth credentials in 'info vnc'
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
` (3 preceding siblings ...)
2009-02-12 15:03 ` [Qemu-devel] PATCH: 4/7: Add SASL authentication extension to VNC Daniel P. Berrange
@ 2009-02-12 15:03 ` Daniel P. Berrange
2009-02-12 15:04 ` [Qemu-devel] PATCH: 6/7: Support simple ACL for client authorization Daniel P. Berrange
` (3 subsequent siblings)
8 siblings, 0 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:03 UTC (permalink / raw)
To: qemu-devel
This patch extends the 'info vnc' monitor output to include information
about the VNC client authentication credentials.
For clients authenticated using SASL, this will output the username.
For clients authenticated using x509 certificates, this will output
the x509 distinguished name.
Auth can be stacked, so both username & x509 dname may be shown.
(qemu) info vnc
Server: active
address: 0.0.0.0:5901
auth: vencrypt+x509+sasl
Client: active
address: 127.0.0.1:42956
x509 dname: C=GB,O=Red Hat,L=London,ST=London,CN=localhost
username: test
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
vnc-tls.c | 17 +++++++++++++++++
vnc-tls.h | 3 +++
vnc.c | 19 +++++++++++++++++--
3 files changed, 37 insertions(+), 2 deletions(-)
Daniel
diff -r 122f8a90f465 vnc-tls.c
--- a/vnc-tls.c Wed Feb 11 17:34:08 2009 +0000
+++ b/vnc-tls.c Wed Feb 11 17:34:11 2009 +0000
@@ -241,6 +241,22 @@ int vnc_tls_validate_certificate(struct
return -1;
}
+ if (i == 0) {
+ size_t dnameSize = 1024;
+ vs->tls.dname = qemu_malloc(dnameSize);
+ requery:
+ if ((ret = gnutls_x509_crt_get_dn (cert, vs->tls.dname, &dnameSize)) != 0) {
+ if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
+ vs->tls.dname = qemu_realloc(vs->tls.dname, dnameSize);
+ goto requery;
+ }
+ gnutls_x509_crt_deinit (cert);
+ VNC_DEBUG("Cannot get client distinguished name: %s",
+ gnutls_strerror (ret));
+ return -1;
+ }
+ }
+
gnutls_x509_crt_deinit (cert);
}
@@ -346,6 +362,7 @@ void vnc_tls_client_cleanup(struct VncSt
vs->tls.session = NULL;
}
vs->tls.wiremode = VNC_WIREMODE_CLEAR;
+ free(vs->tls.dname);
}
diff -r 122f8a90f465 vnc-tls.h
--- a/vnc-tls.h Wed Feb 11 17:34:08 2009 +0000
+++ b/vnc-tls.h Wed Feb 11 17:34:11 2009 +0000
@@ -48,6 +48,9 @@ struct VncStateTLS {
/* Whether data is being TLS encrypted yet */
int wiremode;
gnutls_session_t session;
+
+ /* Client's Distinguished Name from the x509 cert */
+ char *dname;
};
int vnc_tls_client_setup(VncState *vs, int x509Creds);
diff -r 122f8a90f465 vnc.c
--- a/vnc.c Wed Feb 11 17:34:08 2009 +0000
+++ b/vnc.c Wed Feb 11 17:34:11 2009 +0000
@@ -176,6 +176,21 @@ void do_info_vnc(void)
term_puts("Client: active\n");
term_puts(clientAddr);
free(clientAddr);
+
+#ifdef CONFIG_VNC_TLS
+ if (vnc_state->tls.session &&
+ vnc_state->tls.dname)
+ term_printf(" x509 dname: %s\n", vnc_state->tls.dname);
+ else
+ term_puts(" x509 dname: none\n");
+#endif
+#ifdef CONFIG_VNC_SASL
+ if (vnc_state->sasl.conn &&
+ vnc_state->sasl.username)
+ term_printf(" username: %s\n", vnc_state->sasl.username);
+ else
+ term_puts(" username: none\n");
+#endif
}
}
}
@@ -1781,7 +1796,7 @@ static int protocol_client_auth(VncState
/* We only advertise 1 auth scheme at a time, so client
* must pick the one we sent. Verify this */
if (data[0] != vs->auth) { /* Reject auth */
- VNC_DEBUG("Reject auth %d\n", (int)data[0]);
+ VNC_DEBUG("Reject auth %d because it didn't match advertized\n", (int)data[0]);
vnc_write_u32(vs, 1);
if (vs->minor >= 8) {
static const char err[] = "Authentication failed";
@@ -1821,7 +1836,7 @@ static int protocol_client_auth(VncState
#endif /* CONFIG_VNC_SASL */
default: /* Should not be possible, but just in case */
- VNC_DEBUG("Reject auth %d\n", vs->auth);
+ VNC_DEBUG("Reject auth %d server code bug\n", vs->auth);
vnc_write_u8(vs, 1);
if (vs->minor >= 8) {
static const char err[] = "Authentication failed";
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 6/7: Support simple ACL for client authorization
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
` (4 preceding siblings ...)
2009-02-12 15:03 ` [Qemu-devel] PATCH: 5/7: Include auth credentials in 'info vnc' Daniel P. Berrange
@ 2009-02-12 15:04 ` Daniel P. Berrange
2009-02-14 22:14 ` Anthony Liguori
2009-02-12 15:04 ` [Qemu-devel] PATCH: 7/7: Add external persistent ACL file Daniel P. Berrange
` (2 subsequent siblings)
8 siblings, 1 reply; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:04 UTC (permalink / raw)
To: qemu-devel
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
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Makefile | 6 +-
b/acl.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
b/acl.h | 68 ++++++++++++++++++++++++
monitor.c | 80 ++++++++++++++++++++++++++++
vnc-auth-sasl.c | 19 +++++-
vnc-auth-sasl.h | 4 +
vnc-tls.c | 19 ++++++
vnc-tls.h | 3 +
vnc.c | 14 ++++
9 files changed, 363 insertions(+), 8 deletions(-)
Daniel
diff -r efb50f6c8c69 Makefile
--- a/Makefile Thu Feb 12 12:33:38 2009 +0000
+++ b/Makefile Thu Feb 12 12:48:43 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 efb50f6c8c69 acl.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/acl.c Thu Feb 12 12:48:43 2009 +0000
@@ -0,0 +1,158 @@
+/*
+ * 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;
+ char *name;
+
+ acl = qemu_acl_find(aclname);
+ if (acl)
+ return acl;
+
+ name = strdup(aclname);
+ if (!name)
+ return NULL;
+
+ acl = qemu_malloc(sizeof(*acl));
+ acl->aclname = name;
+ acl->defaultDeny = 0;
+ acl->nentries = 0;
+ acl->entries = NULL;
+
+ 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)
+{
+ int i;
+
+ for (i = 0 ; i < acl->nentries ; i++) {
+ if (fnmatch(acl->entries[i].match, party, 0) == 0)
+ return acl->entries[i].deny ? 0 : 1;
+ }
+
+ return acl->defaultDeny ? 0 : 1;
+}
+
+
+void qemu_acl_reset(ACL *acl)
+{
+ int i;
+
+ for (i = 0 ; i < acl->nentries ; i++) {
+ free(acl->entries[i].match);
+ }
+ free(acl->entries);
+ acl->entries = NULL;
+ acl->nentries = 0;
+}
+
+
+int qemu_acl_append(ACL *acl,
+ int deny,
+ const char *match)
+{
+ char *m = strdup(match);
+ if (!m)
+ return -1;
+
+ acl->entries = qemu_realloc(acl->entries,
+ sizeof(acl->entries[0]) *
+ (acl->nentries + 1));
+ acl->entries[acl->nentries].match = m;
+ acl->entries[acl->nentries].deny = deny;
+ acl->nentries++;
+
+ return acl->nentries;
+}
+
+
+int qemu_acl_insert(ACL *acl,
+ int deny,
+ const char *match,
+ int index)
+{
+ char *m;
+
+ if (index < 0)
+ return -1;
+ if (index >= acl->nentries)
+ return qemu_acl_append(acl, deny, match);
+
+ m = strdup(match);
+ if (!m)
+ return -1;
+
+ acl->entries = qemu_realloc(acl->entries,
+ sizeof(acl->entries[0]) *
+ (acl->nentries + 1));
+
+ memmove(acl->entries + index + 1,
+ acl->entries + index,
+ sizeof(acl->entries[0]) * (acl->nentries - index));
+
+ acl->entries[index].match = m;
+ acl->entries[index].deny = deny;
+ acl->nentries++;
+
+ return index;
+}
+
+
+/*
+ * Local variables:
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 8
+ * End:
+ */
diff -r efb50f6c8c69 acl.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/acl.h Thu Feb 12 12:48:43 2009 +0000
@@ -0,0 +1,68 @@
+/*
+ * 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__
+
+typedef struct ACLEntry ACLEntry;
+typedef struct ACL ACL;
+
+struct ACLEntry {
+ char *match;
+ int deny;
+};
+
+struct ACL {
+ char *aclname;
+ unsigned int nentries;
+ 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);
+
+#endif /* __QEMU_ACL_H__ */
+
+/*
+ * Local variables:
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 8
+ * End:
+ */
diff -r efb50f6c8c69 monitor.c
--- a/monitor.c Thu Feb 12 12:33:38 2009 +0000
+++ b/monitor.c Thu Feb 12 12:48:43 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,70 @@ 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;
+ term_printf("policy: %s\n",
+ acl->defaultDeny ? "deny" : "allow");
+ for (i = 0 ; i < acl->nentries ; i++) {
+ term_printf("%d: %s %s\n", i,
+ acl->entries[i].deny ? "deny" : "allow",
+ acl->entries[i].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 {
+ 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 +1594,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 +2962,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 efb50f6c8c69 vnc-auth-sasl.c
--- a/vnc-auth-sasl.c Thu Feb 12 12:33:38 2009 +0000
+++ b/vnc-auth-sasl.c Thu Feb 12 12:48:43 2009 +0000
@@ -120,26 +120,37 @@ 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 = strdup((const char*)val);
if (vs->sasl.username == NULL) {
- VNC_DEBUG("out of memory copying username\n");
+ VNC_DEBUG("out of memory copying username, denying access\n");
return -1;
}
- return 0;
+ if (vs->sasl.acl == NULL) {
+ VNC_DEBUG("unexpectedly missing ACL, denying access\n");
+ return -1;
+ }
+
+ allow = qemu_acl_party_is_allowed(vs->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 efb50f6c8c69 vnc-auth-sasl.h
--- a/vnc-auth-sasl.h Thu Feb 12 12:33:38 2009 +0000
+++ b/vnc-auth-sasl.h Thu Feb 12 12:48:43 2009 +0000
@@ -29,6 +29,8 @@
#include <sasl/sasl.h>
+#include "acl.h"
+
struct VncStateSASL {
sasl_conn_t *conn;
/* If we want to negotiate an SSF layer with client */
@@ -52,6 +54,8 @@ struct VncStateSASL {
unsigned int encodedOffset;
char *username;
char *mechlist;
+
+ ACL *acl;
};
void vnc_sasl_client_cleanup(VncState *vs);
diff -r efb50f6c8c69 vnc-tls.c
--- a/vnc-tls.c Thu Feb 12 12:33:38 2009 +0000
+++ b/vnc-tls.c Thu Feb 12 12:48:43 2009 +0000
@@ -255,6 +255,25 @@ int vnc_tls_validate_certificate(struct
gnutls_strerror (ret));
return -1;
}
+
+ if (vs->tls.x509verify) {
+ int allow;
+ if (!vs->tls.acl) {
+ VNC_DEBUG("unexpected missing acl, denying client\n");
+ gnutls_x509_crt_deinit (cert);
+ return -1;
+ }
+
+ allow = qemu_acl_party_is_allowed(vs->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 efb50f6c8c69 vnc-tls.h
--- a/vnc-tls.h Thu Feb 12 12:33:38 2009 +0000
+++ b/vnc-tls.h Thu Feb 12 12:48:43 2009 +0000
@@ -31,6 +31,8 @@
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
+#include "acl.h"
+
enum {
VNC_WIREMODE_CLEAR,
VNC_WIREMODE_TLS,
@@ -38,6 +40,7 @@ enum {
struct VncStateTLS {
int x509verify; /* Non-zero if server requests & validates client cert */
+ ACL *acl;
/* Paths to x509 certs/keys */
char *x509cacert;
diff -r efb50f6c8c69 vnc.c
--- a/vnc.c Thu Feb 12 12:33:38 2009 +0000
+++ b/vnc.c Thu Feb 12 12:48:43 2009 +0000
@@ -29,6 +29,7 @@
#include "sysemu.h"
#include "qemu_socket.h"
#include "qemu-timer.h"
+#include "acl.h"
#define VNC_REFRESH_INTERVAL (1000 / 30)
@@ -2072,6 +2073,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) {
@@ -2079,8 +2084,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 */
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 7/7: Add external persistent ACL file
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
` (5 preceding siblings ...)
2009-02-12 15:04 ` [Qemu-devel] PATCH: 6/7: Support simple ACL for client authorization Daniel P. Berrange
@ 2009-02-12 15:04 ` Daniel P. Berrange
2009-02-14 22:16 ` Anthony Liguori
2009-02-12 15:43 ` [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
2009-02-14 22:17 ` Anthony Liguori
8 siblings, 1 reply; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:04 UTC (permalink / raw)
To: qemu-devel
This patch introduces a simple access control file capability
for authorizing clients of QEMU's various network services.
The file is designed such that it can be shared amongst multiple
QEMU instances. The style of commands is similar to that used
in the monitor ACL commands. It is a line oriented format, with
comments indicated by leading '#'.Each non-comment line consists
of 4 fields, 'scope', 'aclname', 'action' and 'value'.
The scope allows control over what VMs the rule applies to. This
is a glob, so '*' matches any VM. An explicit value can be match
against the VM name, as given by the '-name' argument.
The aclname is one of the ACLs defined by QEMU, either vnc.username
or vnc.x509dname for now. More later perhaps.
The action can be one of 'policy' 'allow', or 'deny'. The policy
sets the default allow/deny state for the ACL, if no rules match.
Finally the 'value' is another glob matching against the client
name being checked.
An example showing use of both SASL username ACLs, and x509 client
certificate distinguished name ACLs.
# Default deny all for all SASL authenticated users in all VMs
* vnc.username policy deny
# Allow bob access to all VMs
* vnc.username allow bob
# Allow fred and test access to the VM named 'demo'
demo vnc.username allow fred
demo vnc.username allow test
# Deny all x509 client certificates on all VMs
* vnc.x509dname policy deny
# Allow all users from the ACME, London office to all VMs
* vnc.x509dname allow "C=GB,O=ACME,L=London,CN=*"
# Allow Joe from Boston, access to VM 'demo'
demo vnc.x509dname allow "C=GB,O=ACME,L=Boston,CN=joe"
Save this to a file, and give it to QEMU at startup with '-acl demo.acl'
The code for parsing the file is a little crude because I didn't want
to spend too much time writing this. Its more of a proof of concept
of how it might be possible to store ACL rules in a simple way.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
acl.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
acl.h | 3 +
vl.c | 12 +++++++
3 files changed, 121 insertions(+)
Daniel
diff -r 2914a8816f5a acl.c
--- a/acl.c Thu Feb 12 12:48:44 2009 +0000
+++ b/acl.c Thu Feb 12 12:48:48 2009 +0000
@@ -71,6 +71,112 @@ ACL *qemu_acl_init(const char *aclname)
return acl;
}
+#define SKIP_SPACES(buf) while (*buf == ' ') { buf++; }
+
+int qemu_acl_load_file(const char *path,
+ const char *name)
+{
+ FILE *f = fopen(path, "r");
+ char line[1024];
+ int ret = -1;
+ int linenum = 0;
+
+ if (!f)
+ return -1;
+
+ /* * vnc.username policy deny */
+ /* * vnc.username allow fred */
+ /* somevm vnc.username allow joe */
+ /* somevm vnc.username deny bob*/
+ while (fgets(line, sizeof(line), f)) {
+ char *scope, *aclname, *action, *value;
+ char *tmp = line;
+ ACL *acl;
+ int quoted = 0;
+
+ linenum++;
+
+ SKIP_SPACES(tmp);
+ if (line[0] == '#' ||
+ line[0] == '\n' ||
+ line[0] == '\0')
+ continue;
+
+ scope = tmp;
+
+ tmp = strchr(tmp, ' ');
+ if (!tmp) {
+ fprintf(stderr, "missing aclname data at line %d\n", linenum);
+ continue;
+ }
+ *tmp++ = '\0';
+ SKIP_SPACES(tmp);
+ aclname = tmp;
+
+ tmp = strchr(tmp, ' ');
+ if (!tmp) {
+ fprintf(stderr, "missing action data at line %d\n", linenum);
+ continue;
+ }
+ *tmp++ = '\0';
+ SKIP_SPACES(tmp);
+ action = tmp;
+
+ tmp = strchr(tmp, ' ');
+ if (!tmp) {
+ fprintf(stderr, "missing value data at line %d\n", linenum);
+ continue;
+ }
+ *tmp++ = '\0';
+ SKIP_SPACES(tmp);
+ value = tmp;
+
+
+ if (*tmp == '"') {
+ quoted = 1;
+ value++;
+ tmp++;
+ }
+ while (*tmp != '\0' && *tmp != '\n') {
+ if ((quoted && *tmp == '"') ||
+ (!quoted && *tmp == ' '))
+ break;
+ tmp++;
+ }
+ if (tmp)
+ *tmp = '\0';
+
+ if (fnmatch(scope, name ? name : "", 0) != 0) {
+ continue;
+ }
+
+ acl = qemu_acl_init(aclname);
+ if (!acl)
+ goto cleanup;
+
+ if (strcmp(action, "policy") == 0) {
+ if (strcmp(value, "allow") == 0)
+ acl->defaultDeny = 0;
+ else if (strcmp(value, "deny") == 0)
+ acl->defaultDeny = 1;
+ else
+ fprintf(stderr, "unknown ACL policy '%s'\n", value);
+ } else if (strcmp(action, "allow") == 0 ||
+ strcmp(action, "deny") == 0) {
+ int deny = strcmp(action, "deny") == 0 ? 1 : 0;
+ qemu_acl_append(acl, deny, value);
+ } else {
+ fprintf(stderr, "unknown ACL action '%s'\n", action);
+ }
+ }
+ ret = 0;
+
+ cleanup:
+ fclose(f);
+
+ return ret;
+}
+
int qemu_acl_party_is_allowed(ACL *acl,
const char *party)
{
diff -r 2914a8816f5a acl.h
--- a/acl.h Thu Feb 12 12:48:44 2009 +0000
+++ b/acl.h Thu Feb 12 12:48:48 2009 +0000
@@ -44,6 +44,9 @@ ACL *qemu_acl_init(const char *aclname);
ACL *qemu_acl_find(const char *aclname);
+int qemu_acl_load_file(const char *path,
+ const char *name);
+
int qemu_acl_party_is_allowed(ACL *acl,
const char *party);
diff -r 2914a8816f5a vl.c
--- a/vl.c Thu Feb 12 12:48:44 2009 +0000
+++ b/vl.c Thu Feb 12 12:48:48 2009 +0000
@@ -42,6 +42,7 @@
#include "migration.h"
#include "kvm.h"
#include "balloon.h"
+#include "acl.h"
#include <unistd.h>
#include <fcntl.h>
@@ -3948,6 +3949,7 @@ static void help(int exitcode)
"-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
#endif
"-vnc display start a VNC server on display\n"
+ "-acl path path to access control list for network services\n"
"\n"
"Network options:\n"
"-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
@@ -4129,6 +4131,7 @@ enum {
QEMU_OPTION_full_screen,
QEMU_OPTION_g,
QEMU_OPTION_vnc,
+ QEMU_OPTION_acl,
/* Network options: */
QEMU_OPTION_net,
@@ -4243,6 +4246,7 @@ static const QEMUOption qemu_options[] =
{ "g", 1, QEMU_OPTION_g },
#endif
{ "vnc", HAS_ARG, QEMU_OPTION_vnc },
+ { "acl", HAS_ARG, QEMU_OPTION_acl },
/* Network options: */
{ "net", HAS_ARG, QEMU_OPTION_net},
@@ -4621,6 +4625,7 @@ int main(int argc, char **argv, char **e
const char *pid_file = NULL;
int autostart;
const char *incoming = NULL;
+ const char *acl_file = NULL;
qemu_cache_utils_init(envp);
@@ -5160,6 +5165,9 @@ int main(int argc, char **argv, char **e
case QEMU_OPTION_vnc:
vnc_display = optarg;
break;
+ case QEMU_OPTION_acl:
+ acl_file = optarg;
+ break;
case QEMU_OPTION_no_acpi:
acpi_enabled = 0;
break;
@@ -5611,6 +5619,10 @@ int main(int argc, char **argv, char **e
}
}
+ /* Load access control data */
+ if (acl_file)
+ qemu_acl_load_file(acl_file, qemu_name);
+
if (!display_state)
dumb_display_init();
/* just use the first displaystate for the moment */
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
` (6 preceding siblings ...)
2009-02-12 15:04 ` [Qemu-devel] PATCH: 7/7: Add external persistent ACL file Daniel P. Berrange
@ 2009-02-12 15:43 ` Daniel P. Berrange
2009-02-14 22:17 ` Anthony Liguori
8 siblings, 0 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-12 15:43 UTC (permalink / raw)
To: qemu-devel
On Thu, Feb 12, 2009 at 02:53:02PM +0000, Daniel P. Berrange wrote:
> The combined diffstat for all 7 patches about to follow, is
>
> .hgignore | 16
> Makefile | 27 +
> Makefile.target | 5
> b/acl.c | 264 ++++++++++++
> b/acl.h | 71 +++
> b/keymaps.h | 60 ++
> b/qemu.sasl | 34 +
> b/vnc-auth-sasl.c | 640 +++++++++++++++++++++++++++++
> b/vnc-auth-sasl.h | 76 +++
> b/vnc-auth-vencrypt.c | 175 +++++++
> b/vnc-auth-vencrypt.h | 33 +
> b/vnc-tls.c | 456 ++++++++++++++++++++
> b/vnc-tls.h | 76 +++
> configure | 34 +
> curses.c | 3
> curses_keys.h | 9
> keymaps.c | 45 --
> monitor.c | 80 +++
> qemu-doc.texi | 109 ++++
> sdl.c | 3
> sdl_keysym.h | 7
> vl.c | 12
> vnc.c | 1100 ++++++++++++++++++--------------------------------
> vnc.h | 215 +++++++++
> vnc_keysym.h | 5
> 25 files changed, 2795 insertions(+), 760 deletions(-)
FYI, the patches in this series are all diffed against the following SVN
changeset:
r6617 | aliguori | 2009-02-11 21:00:43 +0000 (Wed, 11 Feb 2009) | 8 lines
KVM: cpuid function 2: store all values (Amit Shah)
And also assume my TLS bugfix from earlier today is applied
http://lists.gnu.org/archive/html/qemu-devel/2009-02/msg00820.html
Regards,
Daniel
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client
2009-02-12 15:01 ` [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client Daniel P. Berrange
@ 2009-02-13 18:30 ` Anthony Liguori
2009-02-15 11:43 ` Daniel P. Berrange
0 siblings, 1 reply; 19+ messages in thread
From: Anthony Liguori @ 2009-02-13 18:30 UTC (permalink / raw)
To: Daniel P. Berrange, qemu-devel
Daniel P. Berrange wrote:
> The current 'info vnc' monitor output just displays the VNC server address
> as provided by the -vnc command line flag. This isn't particularly useful
> since it doesn't tell you what VNC is actually listening on. eg, if you
> use '-vnc :1' it is useful to know whether this translated to '0.0.0.0:5901'
> or chose IPv6 ':::5901'. It is also useful to know the address of the
> client that is currently connected. It is also useful to know the active
> authentication (if any).
>
> @@ -2518,3 +2626,11 @@ int vnc_display_open(DisplayState *ds, c
>
> return qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
> }
> +
> +/*
> + * Local variables:
> + * c-indent-level: 4
> + * c-basic-offset: 4
> + * tab-width: 8
> + * End:
> + */
>
I'd prefer you not add this randomly in a patch.
Regards,
Anthony Liguori
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 2/7: Push VncState struct into vnc.h
2009-02-12 15:02 ` [Qemu-devel] PATCH: 2/7: Push VncState struct into vnc.h Daniel P. Berrange
@ 2009-02-14 22:09 ` Anthony Liguori
2009-02-15 11:43 ` Daniel P. Berrange
0 siblings, 1 reply; 19+ messages in thread
From: Anthony Liguori @ 2009-02-14 22:09 UTC (permalink / raw)
To: Daniel P. Berrange, qemu-devel
Daniel P. Berrange wrote:
> This patch moves the declaration for the VncState struct out of the
> vnc.c file and into vnc.h. This is to prepare for next patches which
> have the auth mechanisms implementated in separate vnc-auth-vencrypt.c
> and vnc-auth-sasl.c files
>
> In doing this, I discovered that there is a pile of duplicated keymap
> code statically compiled into all the console frontends. A couple of
> trivial changes allowed this to be sanitized, so instead of doing
> a #include "keymaps.c", duplicating all code, we can have a shared
> keymaps.h file, and only compile code once.
>
It would be better if you split the keymap cleanup into a different patch.
Regards,
Anthony Liguori
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
>
>
> Makefile | 9 ++-
> b/keymaps.h | 60 ++++++++++++++++++++++++
> curses.c | 3 -
> curses_keys.h | 9 +--
> keymaps.c | 45 +++++++-----------
> sdl.c | 3 -
> sdl_keysym.h | 7 +-
> vnc.c | 101 ++---------------------------------------
> vnc.h | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
> vnc_keysym.h | 5 --
> 10 files changed, 235 insertions(+), 147 deletions(-)
>
>
> Daniel
>
>
> diff -r 1e8d80609fe1 Makefile
> --- a/Makefile Wed Feb 11 17:31:30 2009 +0000
> +++ b/Makefile Wed Feb 11 17:31:34 2009 +0000
> @@ -137,6 +137,7 @@ endif
> AUDIO_OBJS+= wavcapture.o
> OBJS+=$(addprefix audio/, $(AUDIO_OBJS))
>
> +OBJS+=keymaps.o
> ifdef CONFIG_SDL
> OBJS+=sdl.o x_keymap.o
> endif
> @@ -161,15 +162,17 @@ LIBS+=$(VDE_LIBS)
>
> cocoa.o: cocoa.m
>
> -sdl.o: sdl.c keymaps.c sdl_keysym.h
> +keymaps.o: keymaps.c keymaps.h
> +
> +sdl.o: sdl.c keymaps.h sdl_keysym.h
>
> sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
>
> -vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h d3des.c d3des.h
> +vnc.o: vnc.c keymaps.h sdl_keysym.h vnchextile.h d3des.c d3des.h
>
> vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
>
> -curses.o: curses.c keymaps.c curses_keys.h
> +curses.o: curses.c keymaps.h curses_keys.h
>
> bt-host.o: CFLAGS += $(CONFIG_BLUEZ_CFLAGS)
>
> diff -r 1e8d80609fe1 curses.c
> --- a/curses.c Wed Feb 11 17:31:30 2009 +0000
> +++ b/curses.c Wed Feb 11 17:31:34 2009 +0000
> @@ -158,7 +158,6 @@ static void curses_cursor_position(Displ
> /* generic keyboard conversion */
>
> #include "curses_keys.h"
> -#include "keymaps.c"
>
> static kbd_layout_t *kbd_layout = 0;
> static int keycode2keysym[CURSES_KEYS];
> @@ -311,7 +310,7 @@ static void curses_keyboard_setup(void)
> keyboard_layout = "en-us";
> #endif
> if(keyboard_layout) {
> - kbd_layout = init_keyboard_layout(keyboard_layout);
> + kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
> if (!kbd_layout)
> exit(1);
> }
> diff -r 1e8d80609fe1 curses_keys.h
> --- a/curses_keys.h Wed Feb 11 17:31:30 2009 +0000
> +++ b/curses_keys.h Wed Feb 11 17:31:34 2009 +0000
> @@ -21,6 +21,10 @@
> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> * THE SOFTWARE.
> */
> +
> +#include "keymaps.h"
> +
> +
> #define KEY_RELEASE 0x80
> #define KEY_MASK 0x7f
> #define SHIFT_CODE 0x2a
> @@ -239,11 +243,6 @@ static const int curses2keysym[CURSES_KE
>
> };
>
> -typedef struct {
> - const char* name;
> - int keysym;
> -} name2keysym_t;
> -
> static const name2keysym_t name2keysym[] = {
> /* Plain ASCII */
> { "space", 0x020 },
> diff -r 1e8d80609fe1 keymaps.c
> --- a/keymaps.c Wed Feb 11 17:31:30 2009 +0000
> +++ b/keymaps.c Wed Feb 11 17:31:34 2009 +0000
> @@ -22,34 +22,20 @@
> * THE SOFTWARE.
> */
>
> -static int get_keysym(const char *name)
> +#include "keymaps.h"
> +#include "sysemu.h"
> +
> +static int get_keysym(const name2keysym_t *table,
> + const char *name)
> {
> const name2keysym_t *p;
> - for(p = name2keysym; p->name != NULL; p++) {
> + for(p = table; p->name != NULL; p++) {
> if (!strcmp(p->name, name))
> return p->keysym;
> }
> return 0;
> }
>
> -struct key_range {
> - int start;
> - int end;
> - struct key_range *next;
> -};
> -
> -#define MAX_NORMAL_KEYCODE 512
> -#define MAX_EXTRA_COUNT 256
> -typedef struct {
> - uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];
> - struct {
> - int keysym;
> - uint16_t keycode;
> - } keysym2keycode_extra[MAX_EXTRA_COUNT];
> - int extra_count;
> - struct key_range *keypad_range;
> - struct key_range *numlock_range;
> -} kbd_layout_t;
>
> static void add_to_key_range(struct key_range **krp, int code) {
> struct key_range *kr;
> @@ -73,7 +59,8 @@ static void add_to_key_range(struct key_
> }
> }
>
> -static kbd_layout_t *parse_keyboard_layout(const char *language,
> +static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
> + const char *language,
> kbd_layout_t * k)
> {
> FILE *f;
> @@ -102,7 +89,7 @@ static kbd_layout_t *parse_keyboard_layo
> if (!strncmp(line, "map ", 4))
> continue;
> if (!strncmp(line, "include ", 8)) {
> - parse_keyboard_layout(line + 8, k);
> + parse_keyboard_layout(table, line + 8, k);
> } else {
> char *end_of_keysym = line;
> while (*end_of_keysym != 0 && *end_of_keysym != ' ')
> @@ -110,7 +97,7 @@ static kbd_layout_t *parse_keyboard_layo
> if (*end_of_keysym) {
> int keysym;
> *end_of_keysym = 0;
> - keysym = get_keysym(line);
> + keysym = get_keysym(table, line);
> if (keysym == 0) {
> // fprintf(stderr, "Warning: unknown keysym %s\n", line);
> } else {
> @@ -154,12 +141,14 @@ static kbd_layout_t *parse_keyboard_layo
> return k;
> }
>
> -static void *init_keyboard_layout(const char *language)
> +
> +void *init_keyboard_layout(const name2keysym_t *table, const char *language)
> {
> - return parse_keyboard_layout(language, 0);
> + return parse_keyboard_layout(table, language, 0);
> }
>
> -static int keysym2scancode(void *kbd_layout, int keysym)
> +
> +int keysym2scancode(void *kbd_layout, int keysym)
> {
> kbd_layout_t *k = kbd_layout;
> if (keysym < MAX_NORMAL_KEYCODE) {
> @@ -180,7 +169,7 @@ static int keysym2scancode(void *kbd_lay
> return 0;
> }
>
> -static inline int keycode_is_keypad(void *kbd_layout, int keycode)
> +int keycode_is_keypad(void *kbd_layout, int keycode)
> {
> kbd_layout_t *k = kbd_layout;
> struct key_range *kr;
> @@ -191,7 +180,7 @@ static inline int keycode_is_keypad(void
> return 0;
> }
>
> -static inline int keysym_is_numlock(void *kbd_layout, int keysym)
> +int keysym_is_numlock(void *kbd_layout, int keysym)
> {
> kbd_layout_t *k = kbd_layout;
> struct key_range *kr;
> diff -r 1e8d80609fe1 keymaps.h
> --- /dev/null Thu Jan 01 00:00:00 1970 +0000
> +++ b/keymaps.h Wed Feb 11 17:31:34 2009 +0000
> @@ -0,0 +1,60 @@
> +/*
> + * QEMU keysym to keycode conversion using rdesktop keymaps
> + *
> + * Copyright (c) 2004 Johannes Schindelin
> + *
> + * 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_KEYMAPS_H__
> +#define __QEMU_KEYMAPS_H__
> +
> +#include "qemu-common.h"
> +
> +typedef struct {
> + const char* name;
> + int keysym;
> +} name2keysym_t;
> +
> +struct key_range {
> + int start;
> + int end;
> + struct key_range *next;
> +};
> +
> +#define MAX_NORMAL_KEYCODE 512
> +#define MAX_EXTRA_COUNT 256
> +typedef struct {
> + uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];
> + struct {
> + int keysym;
> + uint16_t keycode;
> + } keysym2keycode_extra[MAX_EXTRA_COUNT];
> + int extra_count;
> + struct key_range *keypad_range;
> + struct key_range *numlock_range;
> +} kbd_layout_t;
> +
> +
> +void *init_keyboard_layout(const name2keysym_t *table, const char *language);
> +int keysym2scancode(void *kbd_layout, int keysym);
> +int keycode_is_keypad(void *kbd_layout, int keycode);
> +int keysym_is_numlock(void *kbd_layout, int keysym);
> +
> +#endif /* __QEMU_KEYMAPS_H__ */
> diff -r 1e8d80609fe1 sdl.c
> --- a/sdl.c Wed Feb 11 17:31:30 2009 +0000
> +++ b/sdl.c Wed Feb 11 17:31:34 2009 +0000
> @@ -107,7 +107,6 @@ static void sdl_resize(DisplayState *ds)
> /* generic keyboard conversion */
>
> #include "sdl_keysym.h"
> -#include "keymaps.c"
>
> static kbd_layout_t *kbd_layout = NULL;
>
> @@ -623,7 +622,7 @@ void sdl_display_init(DisplayState *ds,
> keyboard_layout = "en-us";
> #endif
> if(keyboard_layout) {
> - kbd_layout = init_keyboard_layout(keyboard_layout);
> + kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
> if (!kbd_layout)
> exit(1);
> }
> diff -r 1e8d80609fe1 sdl_keysym.h
> --- a/sdl_keysym.h Wed Feb 11 17:31:30 2009 +0000
> +++ b/sdl_keysym.h Wed Feb 11 17:31:34 2009 +0000
> @@ -1,7 +1,6 @@
> -typedef struct {
> - const char* name;
> - int keysym;
> -} name2keysym_t;
> +
> +#include "keymaps.h"
> +
> static const name2keysym_t name2keysym[]={
> /* ascii */
> { "space", 0x020},
> diff -r 1e8d80609fe1 vnc.c
> --- a/vnc.c Wed Feb 11 17:31:30 2009 +0000
> +++ b/vnc.c Wed Feb 11 17:31:34 2009 +0000
> @@ -3,6 +3,7 @@
> *
> * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
> * Copyright (C) 2006 Fabrice Bellard
> + * 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
> @@ -23,26 +24,17 @@
> * THE SOFTWARE.
> */
>
> -#include "qemu-common.h"
> -#include "console.h"
> +#include "vnc.h"
> +
> #include "sysemu.h"
> #include "qemu_socket.h"
> #include "qemu-timer.h"
> -#include "audio/audio.h"
> -#include <zlib.h>
>
> #define VNC_REFRESH_INTERVAL (1000 / 30)
>
> -#include "vnc.h"
> #include "vnc_keysym.h"
> -#include "keymaps.c"
> #include "d3des.h"
>
> -#ifdef CONFIG_VNC_TLS
> -#include <gnutls/gnutls.h>
> -#include <gnutls/x509.h>
> -#endif /* CONFIG_VNC_TLS */
> -
> // #define _VNC_DEBUG 1
>
> #ifdef _VNC_DEBUG
> @@ -65,91 +57,8 @@ static void vnc_debug_gnutls_log(int lev
> } \
> }
>
> -typedef struct Buffer
> -{
> - size_t capacity;
> - size_t offset;
> - uint8_t *buffer;
> -} Buffer;
>
> -typedef struct VncState VncState;
>
> -typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len);
> -
> -typedef void VncWritePixels(VncState *vs, void *data, int size);
> -
> -typedef void VncSendHextileTile(VncState *vs,
> - int x, int y, int w, int h,
> - void *last_bg,
> - void *last_fg,
> - int *has_bg, int *has_fg);
> -
> -#define VNC_MAX_WIDTH 2048
> -#define VNC_MAX_HEIGHT 2048
> -#define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
> -
> -#define VNC_AUTH_CHALLENGE_SIZE 16
> -
> -struct VncState
> -{
> - QEMUTimer *timer;
> - int lsock;
> - int csock;
> - DisplayState *ds;
> - int need_update;
> - uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
> - char *old_data;
> - uint32_t features;
> - int absolute;
> - int last_x;
> - int last_y;
> -
> - uint32_t vnc_encoding;
> - uint8_t tight_quality;
> - uint8_t tight_compression;
> -
> - int major;
> - int minor;
> -
> - char *display;
> - char *password;
> - int auth;
> -#ifdef CONFIG_VNC_TLS
> - int subauth;
> - int x509verify;
> -
> - char *x509cacert;
> - char *x509cacrl;
> - char *x509cert;
> - char *x509key;
> -#endif
> - char challenge[VNC_AUTH_CHALLENGE_SIZE];
> -
> -#ifdef CONFIG_VNC_TLS
> - int wiremode;
> - gnutls_session_t tls_session;
> -#endif
> -
> - Buffer output;
> - Buffer input;
> - kbd_layout_t *kbd_layout;
> - /* current output mode information */
> - VncWritePixels *write_pixels;
> - VncSendHextileTile *send_hextile_tile;
> - DisplaySurface clientds, serverds;
> -
> - CaptureVoiceOut *audio_cap;
> - struct audsettings as;
> -
> - VncReadEvent *read_handler;
> - size_t read_handler_expect;
> - /* input */
> - uint8_t modifiers_state[256];
> -
> - Buffer zlib;
> - Buffer zlib_tmp;
> - z_stream zlib_stream[4];
> -};
>
> static VncState *vnc_state; /* needed for info vnc */
> static DisplayChangeListener *dcl;
> @@ -2352,9 +2261,9 @@ void vnc_display_init(DisplayState *ds)
> vs->ds = ds;
>
> if (keyboard_layout)
> - vs->kbd_layout = init_keyboard_layout(keyboard_layout);
> + vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
> else
> - vs->kbd_layout = init_keyboard_layout("en-us");
> + vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
>
> if (!vs->kbd_layout)
> exit(1);
> diff -r 1e8d80609fe1 vnc.h
> --- a/vnc.h Wed Feb 11 17:31:30 2009 +0000
> +++ b/vnc.h Wed Feb 11 17:31:34 2009 +0000
> @@ -1,5 +1,139 @@
> -#ifndef __VNCTIGHT_H
> -#define __VNCTIGHT_H
> +/*
> + * QEMU VNC display driver
> + *
> + * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
> + * Copyright (C) 2006 Fabrice Bellard
> + * 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 __VNC_H
> +#define __VNC_H
> +
> +
> +#include "qemu-common.h"
> +#include "console.h"
> +#include "audio/audio.h"
> +#include <zlib.h>
> +
> +#ifdef CONFIG_VNC_TLS
> +#include <gnutls/gnutls.h>
> +#include <gnutls/x509.h>
> +#endif /* CONFIG_VNC_TLS */
> +
> +#include "keymaps.h"
> +
> +
> +/*****************************************************************************
> + *
> + * Core data structures
> + *
> + *****************************************************************************/
> +
> +
> +#define VNC_MAX_WIDTH 2048
> +#define VNC_MAX_HEIGHT 2048
> +#define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
> +
> +#define VNC_AUTH_CHALLENGE_SIZE 16
> +
> +typedef struct VncState VncState;
> +
> +typedef int VncReadEvent(VncState *vs, uint8_t *data, size_t len);
> +
> +typedef void VncWritePixels(VncState *vs, void *data, int size);
> +
> +typedef void VncSendHextileTile(VncState *vs,
> + int x, int y, int w, int h,
> + void *last_bg,
> + void *last_fg,
> + int *has_bg, int *has_fg);
> +
> +
> +typedef struct Buffer
> +{
> + size_t capacity;
> + size_t offset;
> + uint8_t *buffer;
> +} Buffer;
> +
> +struct VncState
> +{
> + QEMUTimer *timer;
> + int lsock;
> + int csock;
> + DisplayState *ds;
> + int need_update;
> + uint32_t dirty_row[VNC_MAX_HEIGHT][VNC_DIRTY_WORDS];
> + char *old_data;
> + uint32_t features;
> + int absolute;
> + int last_x;
> + int last_y;
> +
> + uint32_t vnc_encoding;
> + uint8_t tight_quality;
> + uint8_t tight_compression;
> +
> + int major;
> + int minor;
> +
> + char *display;
> + char *password;
> + int auth;
> +#ifdef CONFIG_VNC_TLS
> + int subauth;
> + int x509verify;
> +
> + char *x509cacert;
> + char *x509cacrl;
> + char *x509cert;
> + char *x509key;
> +#endif
> + char challenge[VNC_AUTH_CHALLENGE_SIZE];
> +
> +#ifdef CONFIG_VNC_TLS
> + int wiremode;
> + gnutls_session_t tls_session;
> +#endif
> +
> + Buffer output;
> + Buffer input;
> + kbd_layout_t *kbd_layout;
> + /* current output mode information */
> + VncWritePixels *write_pixels;
> + VncSendHextileTile *send_hextile_tile;
> + DisplaySurface clientds, serverds;
> +
> + CaptureVoiceOut *audio_cap;
> + struct audsettings as;
> +
> + VncReadEvent *read_handler;
> + size_t read_handler_expect;
> + /* input */
> + uint8_t modifiers_state[256];
> +
> + Buffer zlib;
> + Buffer zlib_tmp;
> + z_stream zlib_stream[4];
> +};
>
> /*****************************************************************************
> *
> @@ -109,4 +243,4 @@ enum {
> #define VNC_FEATURE_TIGHT_MASK (1 << VNC_FEATURE_TIGHT)
> #define VNC_FEATURE_ZLIB_MASK (1 << VNC_FEATURE_ZLIB)
>
> -#endif /* __VNCTIGHT_H */
> +#endif /* __VNC_H */
> diff -r 1e8d80609fe1 vnc_keysym.h
> --- a/vnc_keysym.h Wed Feb 11 17:31:30 2009 +0000
> +++ b/vnc_keysym.h Wed Feb 11 17:31:34 2009 +0000
> @@ -1,7 +1,4 @@
> -typedef struct {
> - const char* name;
> - int keysym;
> -} name2keysym_t;
> +
> static const name2keysym_t name2keysym[]={
> /* ascii */
> { "space", 0x020},
>
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 6/7: Support simple ACL for client authorization
2009-02-12 15:04 ` [Qemu-devel] PATCH: 6/7: Support simple ACL for client authorization Daniel P. Berrange
@ 2009-02-14 22:14 ` Anthony Liguori
0 siblings, 0 replies; 19+ messages in thread
From: Anthony Liguori @ 2009-02-14 22:14 UTC (permalink / raw)
To: Daniel P. Berrange, qemu-devel
Daniel P. Berrange wrote:
> 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
>
>
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
>
>
> Makefile | 6 +-
> b/acl.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> b/acl.h | 68 ++++++++++++++++++++++++
> monitor.c | 80 ++++++++++++++++++++++++++++
> vnc-auth-sasl.c | 19 +++++-
> vnc-auth-sasl.h | 4 +
> vnc-tls.c | 19 ++++++
> vnc-tls.h | 3 +
> vnc.c | 14 ++++
> 9 files changed, 363 insertions(+), 8 deletions(-)
>
> Daniel
>
>
> diff -r efb50f6c8c69 Makefile
> --- a/Makefile Thu Feb 12 12:33:38 2009 +0000
> +++ b/Makefile Thu Feb 12 12:48:43 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 efb50f6c8c69 acl.c
> --- /dev/null Thu Jan 01 00:00:00 1970 +0000
> +++ b/acl.c Thu Feb 12 12:48:43 2009 +0000
> @@ -0,0 +1,158 @@
> +/*
> + * 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;
>
I'd prefer you make this a list (using sys-queue.h). An advantage would
be that you could support removing rules in the monitor as that seems
like an obvious feature.
BTW, there is a qemu_strdup and you don't have to check it's results.
Regards,
Anthony Liguori
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 7/7: Add external persistent ACL file
2009-02-12 15:04 ` [Qemu-devel] PATCH: 7/7: Add external persistent ACL file Daniel P. Berrange
@ 2009-02-14 22:16 ` Anthony Liguori
2009-02-15 11:28 ` Daniel P. Berrange
0 siblings, 1 reply; 19+ messages in thread
From: Anthony Liguori @ 2009-02-14 22:16 UTC (permalink / raw)
To: Daniel P. Berrange, qemu-devel
Daniel P. Berrange wrote:
> This patch introduces a simple access control file capability
> for authorizing clients of QEMU's various network services.
> The file is designed such that it can be shared amongst multiple
> QEMU instances. The style of commands is similar to that used
> in the monitor ACL commands. It is a line oriented format, with
> comments indicated by leading '#'.Each non-comment line consists
> of 4 fields, 'scope', 'aclname', 'action' and 'value'.
>
> The scope allows control over what VMs the rule applies to. This
> is a glob, so '*' matches any VM. An explicit value can be match
> against the VM name, as given by the '-name' argument.
>
> The aclname is one of the ACLs defined by QEMU, either vnc.username
> or vnc.x509dname for now. More later perhaps.
>
> The action can be one of 'policy' 'allow', or 'deny'. The policy
> sets the default allow/deny state for the ACL, if no rules match.
>
> Finally the 'value' is another glob matching against the client
> name being checked.
>
> An example showing use of both SASL username ACLs, and x509 client
> certificate distinguished name ACLs.
>
> # Default deny all for all SASL authenticated users in all VMs
> * vnc.username policy deny
>
> # Allow bob access to all VMs
> * vnc.username allow bob
>
> # Allow fred and test access to the VM named 'demo'
> demo vnc.username allow fred
> demo vnc.username allow test
>
>
> # Deny all x509 client certificates on all VMs
> * vnc.x509dname policy deny
>
> # Allow all users from the ACME, London office to all VMs
> * vnc.x509dname allow "C=GB,O=ACME,L=London,CN=*"
>
> # Allow Joe from Boston, access to VM 'demo'
> demo vnc.x509dname allow "C=GB,O=ACME,L=Boston,CN=joe"
>
I feel really uncomfortable with this especially since Markus is now
working on configuration file support. It seems to me that we'll want
to store any ACL information in the host configuration file.
Unless there's a really strong case that you always want ACLs to be
stored in a separate file, I'd rather wait to see how the host
configuration file stuff turns out before applying this.
I assume that libvirt will use the monitor interface anyway so
presumably, it's not a huge problem to wait on this?
Regards,
Anthony Liguori
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
` (7 preceding siblings ...)
2009-02-12 15:43 ` [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
@ 2009-02-14 22:17 ` Anthony Liguori
8 siblings, 0 replies; 19+ messages in thread
From: Anthony Liguori @ 2009-02-14 22:17 UTC (permalink / raw)
To: Daniel P. Berrange, qemu-devel
Daniel P. Berrange wrote:
> Previously I provided patches for QEMU's VNC server to support SSL/TLS
> and x509 certificates. This provides good encryption capabilities for
> the VNC session. It doesn't really address the authentication problem
> though.
>
> I have been working to create a new authentication type in the RFB
> protocol to address this need in a generic, extendable way, by mapping
> the SASL API into the RFB protocol. Since SASL is a generic plugin
> based API, this will allow use of a huge range of auth mechanims over
> VNC, without us having to add any more auth code. For example, PAM,
> Digest-MD5, GSSAPI/Kerberos, One-time key/password, LDAP password
> lookup, SQL db password lookup, and more.
>
> I have got a VNC auth type assigned by the RFB spec maintainers:
>
> http://realvnc.com/pipermail/vnc-list/2008-December/059463.html
>
> With the full current spec for the SASL extension currently documented
> here:
>
> http://realvnc.com/pipermail/vnc-list/2008-December/059462.html
>
>
> This is the 2nd version of the patches I initially posted here
>
> http://lists.gnu.org/archive/html/qemu-devel/2009-02/msg00255.html
>
Modulo the comments I made, this series look really nice. I'm going to
apply Brian Kress' multiple clients patch once he adds a SoB so you'll
want to make sure to rebase against that for the next series.
Regards,
Anthony Liguori
> Changes since last time
>
> - Re-factor the code to move TLS and SASL methods into separate files,
> vnc-tls.c, vnc-auth-vencrypt.c and vnc-auth-vencrypt.h
>
> - Added simple access control lists for authorization of client users
> on either SASL username, or x509 distinguished name
>
> - Added proof of concept external file format for persisting ACLs
>
> - Extend 'info vnc' to show much more information about clients and
> auth
>
> - Tested with SASL + Digest-MD5, SASL + GSSAPI. TLS + SASL + Digest-MD5
> and TLS + SASL + GSSAPI. This gives coverage off all interesting
> code paths and/or I/O encryption combinations.
>
>
> The combined diffstat for all 7 patches about to follow, is
>
> .hgignore | 16
> Makefile | 27 +
> Makefile.target | 5
> b/acl.c | 264 ++++++++++++
> b/acl.h | 71 +++
> b/keymaps.h | 60 ++
> b/qemu.sasl | 34 +
> b/vnc-auth-sasl.c | 640 +++++++++++++++++++++++++++++
> b/vnc-auth-sasl.h | 76 +++
> b/vnc-auth-vencrypt.c | 175 +++++++
> b/vnc-auth-vencrypt.h | 33 +
> b/vnc-tls.c | 456 ++++++++++++++++++++
> b/vnc-tls.h | 76 +++
> configure | 34 +
> curses.c | 3
> curses_keys.h | 9
> keymaps.c | 45 --
> monitor.c | 80 +++
> qemu-doc.texi | 109 ++++
> sdl.c | 3
> sdl_keysym.h | 7
> vl.c | 12
> vnc.c | 1100 ++++++++++++++++++--------------------------------
> vnc.h | 215 +++++++++
> vnc_keysym.h | 5
> 25 files changed, 2795 insertions(+), 760 deletions(-)
>
>
> Daniel
>
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 7/7: Add external persistent ACL file
2009-02-14 22:16 ` Anthony Liguori
@ 2009-02-15 11:28 ` Daniel P. Berrange
0 siblings, 0 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-15 11:28 UTC (permalink / raw)
To: Anthony Liguori; +Cc: qemu-devel
On Sat, Feb 14, 2009 at 04:16:25PM -0600, Anthony Liguori wrote:
>
> I feel really uncomfortable with this especially since Markus is now
> working on configuration file support. It seems to me that we'll want
> to store any ACL information in the host configuration file.
>
> Unless there's a really strong case that you always want ACLs to be
> stored in a separate file, I'd rather wait to see how the host
> configuration file stuff turns out before applying this.
I rather wanted the ACL configuration to be separate from the
general emulator configuration. This file format was intended to
allow you to have one ACL file that is used across all your QEMU
instances, regardless of what emulator configuration file they
might be using.
Though, perhaps if the general config file allowed '#include acl.cfg'
that would be sufficient flexibilty, allowing a shared ACL for all
configs.
> I assume that libvirt will use the monitor interface anyway so
> presumably, it's not a huge problem to wait on this?
I'm not really decided on what the best way to approach things is from
the libvirt POV. We could certainly use the monitor interface to set it
up - just have to decide how/where to persist it.
Daniel
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client
2009-02-13 18:30 ` Anthony Liguori
@ 2009-02-15 11:43 ` Daniel P. Berrange
2009-02-15 18:22 ` Anthony Liguori
2009-02-18 21:10 ` [Qemu-devel] " Mike Day
0 siblings, 2 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-15 11:43 UTC (permalink / raw)
To: Anthony Liguori; +Cc: qemu-devel
On Fri, Feb 13, 2009 at 12:30:59PM -0600, Anthony Liguori wrote:
> Daniel P. Berrange wrote:
> >The current 'info vnc' monitor output just displays the VNC server address
> >as provided by the -vnc command line flag. This isn't particularly useful
> >since it doesn't tell you what VNC is actually listening on. eg, if you
> >use '-vnc :1' it is useful to know whether this translated to
> >'0.0.0.0:5901'
> >or chose IPv6 ':::5901'. It is also useful to know the address of the
> >client that is currently connected. It is also useful to know the active
> >authentication (if any).
> >
> >@@ -2518,3 +2626,11 @@ int vnc_display_open(DisplayState *ds, c
> >
> > return qemu_set_fd_handler2(vs->lsock, vnc_listen_poll,
> > vnc_listen_read, NULL, vs);
> > }
> >+
> >+/*
> >+ * Local variables:
> >+ * c-indent-level: 4
> >+ * c-basic-offset: 4
> >+ * tab-width: 8
> >+ * End:
> >+ */
> >
>
> I'd prefer you not add this randomly in a patch.
Sorry, I added this to all the files I touched as QEMU's indentation rules
don't match the default emacs & emacs will thus mess up all the whitespace.
That said vnc.c already has alot of inconsistent whitespace :-( I'll strip
this chunk out of future patches before submission.
Daniel
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 2/7: Push VncState struct into vnc.h
2009-02-14 22:09 ` Anthony Liguori
@ 2009-02-15 11:43 ` Daniel P. Berrange
0 siblings, 0 replies; 19+ messages in thread
From: Daniel P. Berrange @ 2009-02-15 11:43 UTC (permalink / raw)
To: Anthony Liguori; +Cc: qemu-devel
On Sat, Feb 14, 2009 at 04:09:31PM -0600, Anthony Liguori wrote:
> Daniel P. Berrange wrote:
> >This patch moves the declaration for the VncState struct out of the
> >vnc.c file and into vnc.h. This is to prepare for next patches which
> >have the auth mechanisms implementated in separate vnc-auth-vencrypt.c
> >and vnc-auth-sasl.c files
> >
> >In doing this, I discovered that there is a pile of duplicated keymap
> >code statically compiled into all the console frontends. A couple of
> >trivial changes allowed this to be sanitized, so instead of doing
> >a #include "keymaps.c", duplicating all code, we can have a shared
> >keymaps.h file, and only compile code once.
> >
>
> It would be better if you split the keymap cleanup into a different patch.
Ok, easy enough todo. I'll post new patch to deal with this keymap change
first.
Daniel
--
|: 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 :|
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client
2009-02-15 11:43 ` Daniel P. Berrange
@ 2009-02-15 18:22 ` Anthony Liguori
2009-02-18 21:10 ` [Qemu-devel] " Mike Day
1 sibling, 0 replies; 19+ messages in thread
From: Anthony Liguori @ 2009-02-15 18:22 UTC (permalink / raw)
To: Daniel P. Berrange, qemu-devel
Daniel P. Berrange wrote:
> Sorry, I added this to all the files I touched as QEMU's indentation rules
> don't match the default emacs & emacs will thus mess up all the whitespace.
>
(c-add-style "qemu"
'("stroustrup"
(indent-tabs-mode . nil)
(c-basic-offset . 4)
(tab-width . 8)
)
nil) ; t = set this style, nil = don't
Is what I'm using FWIW.
Regards,
Anthony Liguori
^ permalink raw reply [flat|nested] 19+ messages in thread
* [Qemu-devel] Re: PATCH: 1/7: Extend 'info vnc' output to show client
2009-02-15 11:43 ` Daniel P. Berrange
2009-02-15 18:22 ` Anthony Liguori
@ 2009-02-18 21:10 ` Mike Day
1 sibling, 0 replies; 19+ messages in thread
From: Mike Day @ 2009-02-18 21:10 UTC (permalink / raw)
To: Daniel P. Berrange, qemu-devel; +Cc: Anthony Liguori
On 15/02/09 11:43 +0000, Daniel P. Berrange wrote:
> On Fri, Feb 13, 2009 at 12:30:59PM -0600, Anthony Liguori wrote:
> > Daniel P. Berrange wrote:
> > >+
> > >+/*
> > >+ * Local variables:
> > >+ * c-indent-level: 4
> > >+ * c-basic-offset: 4
> > >+ * tab-width: 8
> > >+ * End:
> > >+ */
> > >
> >
> > I'd prefer you not add this randomly in a patch.
>
> Sorry, I added this to all the files I touched as QEMU's indentation rules
> don't match the default emacs & emacs will thus mess up all the whitespace.
> That said vnc.c already has alot of inconsistent whitespace :-( I'll strip
> this chunk out of future patches before submission.
Jimmy Xenidis helped me solve this problem automatically using the
following e-lisp. Put it in your custom.el or a file sourced by it. It
will automatically set the indent, offset, and tab width for you based
on the path of the file you are editing. The example below has styles
for Linux and Xen. You simply need to extrapolate and add another
style for Qemu.
;; xen c-style
;; Jimmy Xenidis <jimix@us.ibm.com>
(defconst xen-style
'( "bsd"
(c-basic-offset . 4)
(indent-tabs-mode . nil)
(tab-width . 4)
(label . 1)
)
"Xen C Programming Style")
(c-add-style "xen" xen-style t)
(defconst linux-c-style
'( "K&R"
(c-basic-offset . 8)
(indent-tabs-mode . t)
(tab-width . 8)
)
"Linux CodingStyle")
(c-add-style "linux" linux-c-style t)
(defun my-c-mode-hooks ()
"Look at auto-c-mode-alist to decide on the c style mode"
(save-excursion
(let ((name (file-name-sans-versions buffer-file-name))
(alist auto-c-mode-alist)
(mode nil))
(while (and (not mode) alist)
(if (string-match (car (car alist)) name)
(if (and (consp (cdr (car alist)))
(nth 2 (car alist)))
(progn
(setq mode (car (cdr (car alist)))
name (substring name 0 (match-beginning 0))
keep-going t))
(setq mode (cdr (car alist))
keep-going nil)))
(setq alist (cdr alist)))
(c-set-style mode))))
(setq c-style-variables-are-local-p t)
(setq auto-c-mode-alist
'(
("/linux" . "linux")
("/xen" . "xen")
("" . "bsd")))
(add-hook 'c-mode-hook 'my-c-mode-hooks)
--
Mike Day
http://www.ncultra.org
AIM: ncmikeday | Yahoo IM: ultra.runner
PGP key: http://www.ncultra.org/ncmike/pubkey.asc
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2009-02-18 21:02 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-12 14:53 [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
2009-02-12 15:01 ` [Qemu-devel] PATCH: 1/7: Extend 'info vnc' output to show client Daniel P. Berrange
2009-02-13 18:30 ` Anthony Liguori
2009-02-15 11:43 ` Daniel P. Berrange
2009-02-15 18:22 ` Anthony Liguori
2009-02-18 21:10 ` [Qemu-devel] " Mike Day
2009-02-12 15:02 ` [Qemu-devel] PATCH: 2/7: Push VncState struct into vnc.h Daniel P. Berrange
2009-02-14 22:09 ` Anthony Liguori
2009-02-15 11:43 ` Daniel P. Berrange
2009-02-12 15:02 ` [Qemu-devel] PATCH: 3/7: Split out VNC TLS auth code to separate file Daniel P. Berrange
2009-02-12 15:03 ` [Qemu-devel] PATCH: 4/7: Add SASL authentication extension to VNC Daniel P. Berrange
2009-02-12 15:03 ` [Qemu-devel] PATCH: 5/7: Include auth credentials in 'info vnc' Daniel P. Berrange
2009-02-12 15:04 ` [Qemu-devel] PATCH: 6/7: Support simple ACL for client authorization Daniel P. Berrange
2009-02-14 22:14 ` Anthony Liguori
2009-02-12 15:04 ` [Qemu-devel] PATCH: 7/7: Add external persistent ACL file Daniel P. Berrange
2009-02-14 22:16 ` Anthony Liguori
2009-02-15 11:28 ` Daniel P. Berrange
2009-02-12 15:43 ` [Qemu-devel] PATCH: 0/7: Support SASL authentication in VNC server Daniel P. Berrange
2009-02-14 22:17 ` Anthony Liguori
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).